blob: 292123bc30faad247395fb178813bec8395423ac [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:
444 /* allow the register to be completed before unregistering. */
445 rtnl_unlock();
446 rtnl_lock();
447
448 unregister_netdevice(dev);
449 return NULL;
450}
451
452#ifdef CONFIG_IP_PIMSM
453
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000454static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000456 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000457 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500458 struct flowi4 fl4 = {
459 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700460 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500461 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000462 };
463 int err;
464
David S. Millerda919812011-03-12 02:04:50 -0500465 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000466 if (err < 0) {
467 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000468 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000469 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700472 dev->stats.tx_bytes += skb->len;
473 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000474 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 read_unlock(&mrt_lock);
476 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000477 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200480static int reg_vif_get_iflink(const struct net_device *dev)
481{
482 return 0;
483}
484
Stephen Hemminger007c3832008-11-20 20:28:35 -0800485static const struct net_device_ops reg_vif_netdev_ops = {
486 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200487 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800488};
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static void reg_vif_setup(struct net_device *dev)
491{
492 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800493 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530495 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700497 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000500static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct net_device *dev;
503 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000504 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000506 if (mrt->id == RT_TABLE_DEFAULT)
507 sprintf(name, "pimreg");
508 else
509 sprintf(name, "pimreg%u", mrt->id);
510
Tom Gundersenc835a672014-07-14 16:37:24 +0200511 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Ian Morris51456b22015-04-03 09:17:26 +0100513 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return NULL;
515
Tom Goff403dbb92009-06-14 03:16:13 -0700516 dev_net_set(dev, net);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (register_netdevice(dev)) {
519 free_netdev(dev);
520 return NULL;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Herbert Xu71e27da2007-06-04 23:36:06 -0700523 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000524 in_dev = __in_dev_get_rcu(dev);
525 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700526 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Herbert Xu71e27da2007-06-04 23:36:06 -0700530 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100531 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700532 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
533 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (dev_open(dev))
536 goto failure;
537
Wang Chen7dc00c82008-07-14 20:56:34 -0700538 dev_hold(dev);
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return dev;
541
542failure:
543 /* allow the register to be completed before unregistering. */
544 rtnl_unlock();
545 rtnl_lock();
546
547 unregister_netdevice(dev);
548 return NULL;
549}
550#endif
551
Ben Hutchings2c530402012-07-10 10:55:09 +0000552/**
553 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700554 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900556
Patrick McHardy0c122952010-04-13 05:03:22 +0000557static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000558 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct vif_device *v;
561 struct net_device *dev;
562 struct in_device *in_dev;
563
Patrick McHardy0c122952010-04-13 05:03:22 +0000564 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EADDRNOTAVAIL;
566
Patrick McHardy0c122952010-04-13 05:03:22 +0000567 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 write_lock_bh(&mrt_lock);
570 dev = v->dev;
571 v->dev = NULL;
572
573 if (!dev) {
574 write_unlock_bh(&mrt_lock);
575 return -EADDRNOTAVAIL;
576 }
577
578#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000579 if (vifi == mrt->mroute_reg_vif_num)
580 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581#endif
582
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000583 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000585
586 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000587 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 break;
589 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000590 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
593 write_unlock_bh(&mrt_lock);
594
595 dev_set_allmulti(dev, -1);
596
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000597 in_dev = __in_dev_get_rtnl(dev);
598 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700599 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000600 inet_netconf_notify_devconf(dev_net(dev),
601 NETCONFA_MC_FORWARDING,
602 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 ip_rt_multicast_event(in_dev);
604 }
605
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000606 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000607 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 dev_put(dev);
610 return 0;
611}
612
Eric Dumazeta8c94862010-10-01 16:15:08 +0000613static void ipmr_cache_free_rcu(struct rcu_head *head)
614{
615 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
616
617 kmem_cache_free(mrt_cachep, c);
618}
619
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000620static inline void ipmr_cache_free(struct mfc_cache *c)
621{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000622 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000626 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628
Patrick McHardy0c122952010-04-13 05:03:22 +0000629static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200631 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700633 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Patrick McHardy0c122952010-04-13 05:03:22 +0000635 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jianjun Kongc354e122008-11-03 00:28:02 -0800637 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700638 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
640 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000641 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000643 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700644 e->error = -ETIMEDOUT;
645 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700646
Eric W. Biederman15e47302012-09-07 20:12:54 +0000647 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000648 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000653 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656
Patrick McHardye258beb2010-04-13 05:03:19 +0000657/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Patrick McHardye258beb2010-04-13 05:03:19 +0000659static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Patrick McHardy0c122952010-04-13 05:03:22 +0000661 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 unsigned long now;
663 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000664 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000667 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return;
669 }
670
Patrick McHardy0c122952010-04-13 05:03:22 +0000671 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto out;
673
674 now = jiffies;
675 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Patrick McHardy0c122952010-04-13 05:03:22 +0000677 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (time_after(c->mfc_un.unres.expires, now)) {
679 unsigned long interval = c->mfc_un.unres.expires - now;
680 if (interval < expires)
681 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 continue;
683 }
684
Patrick McHardy862465f2010-04-13 05:03:21 +0000685 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000686 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000687 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Patrick McHardy0c122952010-04-13 05:03:22 +0000690 if (!list_empty(&mrt->mfc_unres_queue))
691 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693out:
694 spin_unlock(&mfc_unres_lock);
695}
696
697/* Fill oifs list. It is called under write locked mrt_lock. */
698
Patrick McHardy0c122952010-04-13 05:03:22 +0000699static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000700 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 int vifi;
703
704 cache->mfc_un.res.minvif = MAXVIFS;
705 cache->mfc_un.res.maxvif = 0;
706 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
707
Patrick McHardy0c122952010-04-13 05:03:22 +0000708 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
709 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000710 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
712 if (cache->mfc_un.res.minvif > vifi)
713 cache->mfc_un.res.minvif = vifi;
714 if (cache->mfc_un.res.maxvif <= vifi)
715 cache->mfc_un.res.maxvif = vifi + 1;
716 }
717 }
718}
719
Patrick McHardy0c122952010-04-13 05:03:22 +0000720static int vif_add(struct net *net, struct mr_table *mrt,
721 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000724 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct net_device *dev;
726 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700727 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000730 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -EADDRINUSE;
732
733 switch (vifc->vifc_flags) {
734#ifdef CONFIG_IP_PIMSM
735 case VIFF_REGISTER:
736 /*
737 * Special Purpose VIF in PIM
738 * All the packets will be sent to the daemon
739 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000740 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000742 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!dev)
744 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700745 err = dev_set_allmulti(dev, 1);
746 if (err) {
747 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700748 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700749 return err;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
752#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900753 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000754 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!dev)
756 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700757 err = dev_set_allmulti(dev, 1);
758 if (err) {
759 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700760 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700761 return err;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000764
765 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000767 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
768 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100769 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000770 dev_put(dev);
771 return -EADDRNOTAVAIL;
772 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000773 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000774 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (!dev)
777 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700778 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700779 if (err) {
780 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700781 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 break;
784 default:
785 return -EINVAL;
786 }
787
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000788 in_dev = __in_dev_get_rtnl(dev);
789 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000790 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000792 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700793 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000794 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
795 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 ip_rt_multicast_event(in_dev);
797
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000798 /* Fill in the VIF structures */
799
Jianjun Kongc354e122008-11-03 00:28:02 -0800800 v->rate_limit = vifc->vifc_rate_limit;
801 v->local = vifc->vifc_lcl_addr.s_addr;
802 v->remote = vifc->vifc_rmt_addr.s_addr;
803 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!mrtsock)
805 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800806 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 v->bytes_in = 0;
808 v->bytes_out = 0;
809 v->pkt_in = 0;
810 v->pkt_out = 0;
811 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000812 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200813 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* And finish update writing critical data */
816 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800817 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000819 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000820 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000822 if (vifi+1 > mrt->maxvif)
823 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 write_unlock_bh(&mrt_lock);
825 return 0;
826}
827
Eric Dumazeta8c94862010-10-01 16:15:08 +0000828/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000829static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000830 __be32 origin,
831 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Jianjun Kongc354e122008-11-03 00:28:02 -0800833 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct mfc_cache *c;
835
Eric Dumazeta8c94862010-10-01 16:15:08 +0000836 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000837 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
838 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000840 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000843/* Look for a (*,*,oif) entry */
844static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
845 int vifi)
846{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100847 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000848 struct mfc_cache *c;
849
850 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100851 if (c->mfc_origin == htonl(INADDR_ANY) &&
852 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000853 c->mfc_un.res.ttls[vifi] < 255)
854 return c;
855
856 return NULL;
857}
858
859/* Look for a (*,G) entry */
860static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
861 __be32 mcastgrp, int vifi)
862{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100863 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000864 struct mfc_cache *c, *proxy;
865
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100866 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000867 goto skip;
868
869 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100870 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000871 c->mfc_mcastgrp == mcastgrp) {
872 if (c->mfc_un.res.ttls[vifi] < 255)
873 return c;
874
875 /* It's ok if the vifi is part of the static tree */
876 proxy = ipmr_cache_find_any_parent(mrt,
877 c->mfc_parent);
878 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
879 return c;
880 }
881
882skip:
883 return ipmr_cache_find_any_parent(mrt, vifi);
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/*
887 * Allocate a multicast cache entry
888 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000889static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Jianjun Kongc354e122008-11-03 00:28:02 -0800891 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000892
893 if (c)
894 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return c;
896}
897
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000898static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Jianjun Kongc354e122008-11-03 00:28:02 -0800900 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000901
902 if (c) {
903 skb_queue_head_init(&c->mfc_un.unres.unresolved);
904 c->mfc_un.unres.expires = jiffies + 10*HZ;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return c;
907}
908
909/*
910 * A cache entry has gone into a resolved state from queued
911 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900912
Patrick McHardy0c122952010-04-13 05:03:22 +0000913static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
914 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700917 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000919 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Jianjun Kongc354e122008-11-03 00:28:02 -0800921 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700922 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
924
Hong zhi guo573ce262013-03-27 06:47:04 +0000925 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000926 nlh->nlmsg_len = skb_tail_pointer(skb) -
927 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 } else {
929 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000930 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000932 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700933 e->error = -EMSGSIZE;
934 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Thomas Graf2942e902006-08-15 00:30:25 -0700936
Eric W. Biederman15e47302012-09-07 20:12:54 +0000937 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000938 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000939 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942}
943
944/*
945 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
946 * expects the following bizarre scheme.
947 *
948 * Called under mrt_lock.
949 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900950
Patrick McHardy0c122952010-04-13 05:03:22 +0000951static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000952 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300955 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct igmphdr *igmp;
957 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000958 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int ret;
960
961#ifdef CONFIG_IP_PIMSM
962 if (assert == IGMPMSG_WHOLEPKT)
963 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
964 else
965#endif
966 skb = alloc_skb(128, GFP_ATOMIC);
967
Stephen Hemminger132adf52007-03-08 20:44:43 -0800968 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return -ENOBUFS;
970
971#ifdef CONFIG_IP_PIMSM
972 if (assert == IGMPMSG_WHOLEPKT) {
973 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000974 * Duplicate old header, fix ihl, length etc.
975 * And all this only to mangle msg->im_msgtype and
976 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300978 skb_push(skb, sizeof(struct iphdr));
979 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300980 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300981 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700982 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 msg->im_msgtype = IGMPMSG_WHOLEPKT;
984 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000985 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700986 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
987 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
988 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900989 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900991 {
992
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000993 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Cong Wang30f3a402013-06-05 20:14:10 +0800995 skb_set_network_header(skb, skb->len);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300996 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300997 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000998 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700999 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +00001001 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001003 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001005 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 igmp->type =
1007 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001008 igmp->code = 0;
1009 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001010 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Eric Dumazet4c968702010-10-01 16:15:01 +00001013 rcu_read_lock();
1014 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001015 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001016 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 kfree_skb(skb);
1018 return -EINVAL;
1019 }
1020
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001021 /* Deliver to mrouted */
1022
Eric Dumazet4c968702010-10-01 16:15:01 +00001023 ret = sock_queue_rcv_skb(mroute_sk, skb);
1024 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001025 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001026 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 kfree_skb(skb);
1028 }
1029
1030 return ret;
1031}
1032
1033/*
1034 * Queue a packet for resolution. It gets locked cache entry!
1035 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001038ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Patrick McHardy862465f2010-04-13 05:03:21 +00001040 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 int err;
1042 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001043 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001046 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001047 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001048 c->mfc_origin == iph->saddr) {
1049 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Patrick McHardy862465f2010-04-13 05:03:21 +00001054 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001055 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Patrick McHardy0c122952010-04-13 05:03:22 +00001057 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001058 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 spin_unlock_bh(&mfc_unres_lock);
1060
1061 kfree_skb(skb);
1062 return -ENOBUFS;
1063 }
1064
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001065 /* Fill in the new cache entry */
1066
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001067 c->mfc_parent = -1;
1068 c->mfc_origin = iph->saddr;
1069 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001071 /* Reflect first query at mrouted. */
1072
Patrick McHardy0c122952010-04-13 05:03:22 +00001073 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001074 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001075 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 out - Brad Parker
1077 */
1078 spin_unlock_bh(&mfc_unres_lock);
1079
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001080 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 kfree_skb(skb);
1082 return err;
1083 }
1084
Patrick McHardy0c122952010-04-13 05:03:22 +00001085 atomic_inc(&mrt->cache_resolve_queue_len);
1086 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001087 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
David S. Miller278554b2010-05-12 00:05:35 -07001089 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1090 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001093 /* See if we can append the packet */
1094
1095 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kfree_skb(skb);
1097 err = -ENOBUFS;
1098 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001099 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 err = 0;
1101 }
1102
1103 spin_unlock_bh(&mfc_unres_lock);
1104 return err;
1105}
1106
1107/*
1108 * MFC cache manipulation by user space mroute daemon
1109 */
1110
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001111static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
1113 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001114 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Jianjun Kongc354e122008-11-03 00:28:02 -08001116 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Patrick McHardy0c122952010-04-13 05:03:22 +00001118 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001120 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1121 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001122 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001123 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001124 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return 0;
1126 }
1127 }
1128 return -ENOENT;
1129}
1130
Patrick McHardy0c122952010-04-13 05:03:22 +00001131static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001132 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
Patrick McHardy862465f2010-04-13 05:03:21 +00001134 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001136 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Patrick McHardya50436f22010-03-17 06:04:14 +00001138 if (mfc->mfcc_parent >= MAXVIFS)
1139 return -ENFILE;
1140
Jianjun Kongc354e122008-11-03 00:28:02 -08001141 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Patrick McHardy0c122952010-04-13 05:03:22 +00001143 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001145 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1146 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001147 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
Patrick McHardy862465f2010-04-13 05:03:21 +00001152 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 write_lock_bh(&mrt_lock);
1154 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001155 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!mrtsock)
1157 c->mfc_flags |= MFC_STATIC;
1158 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001159 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return 0;
1161 }
1162
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001163 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001164 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return -EINVAL;
1166
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001167 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001168 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return -ENOMEM;
1170
Jianjun Kongc354e122008-11-03 00:28:02 -08001171 c->mfc_origin = mfc->mfcc_origin.s_addr;
1172 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1173 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001174 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (!mrtsock)
1176 c->mfc_flags |= MFC_STATIC;
1177
Eric Dumazeta8c94862010-10-01 16:15:08 +00001178 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 /*
1181 * Check to see if we resolved a queued list. If so we
1182 * need to send on the frames and tidy up.
1183 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001184 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001186 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001187 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001189 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001190 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001191 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 break;
1193 }
1194 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001195 if (list_empty(&mrt->mfc_unres_queue))
1196 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 spin_unlock_bh(&mfc_unres_lock);
1198
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001199 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001200 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001201 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001203 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return 0;
1205}
1206
1207/*
1208 * Close the multicast socket, and clear the vif tables etc
1209 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001210
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001211static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001214 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001215 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001216
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001217 /* Shut down all active vif entries */
1218
Patrick McHardy0c122952010-04-13 05:03:22 +00001219 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001220 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1221 continue;
1222 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001224 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001226 /* Wipe the cache */
1227
Patrick McHardy862465f2010-04-13 05:03:21 +00001228 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001229 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001230 if (!all && (c->mfc_flags & MFC_STATIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001232 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001233 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001234 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236 }
1237
Patrick McHardy0c122952010-04-13 05:03:22 +00001238 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001240 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001241 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001242 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001243 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245 spin_unlock_bh(&mfc_unres_lock);
1246 }
1247}
1248
Eric Dumazet4c968702010-10-01 16:15:01 +00001249/* called from ip_ra_control(), before an RCU grace period,
1250 * we dont need to call synchronize_rcu() here
1251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static void mrtsock_destruct(struct sock *sk)
1253{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001254 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001255 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001258 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001259 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001260 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001261 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1262 NETCONFA_IFINDEX_ALL,
1263 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001264 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001265 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 rtnl_unlock();
1269}
1270
1271/*
1272 * Socket options and virtual interface manipulation. The whole
1273 * virtual interface system is a complete heap, but unfortunately
1274 * that's how BSD mrouted happens to think. Maybe one day with a proper
1275 * MOSPF/PIM router set up we can clean this up.
1276 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001277
David S. Millerb7058842009-09-30 16:12:20 -07001278int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001280 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct vifctl vif;
1282 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001283 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001284 struct mr_table *mrt;
1285
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001286 if (sk->sk_type != SOCK_RAW ||
1287 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1288 return -EOPNOTSUPP;
1289
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001290 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001291 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001292 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001293
Stephen Hemminger132adf52007-03-08 20:44:43 -08001294 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001295 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001296 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EACCES;
1298 }
1299
Stephen Hemminger132adf52007-03-08 20:44:43 -08001300 switch (optname) {
1301 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001302 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001306 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001308 return -EADDRINUSE;
1309 }
1310
1311 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1312 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001313 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001314 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001315 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1316 NETCONFA_IFINDEX_ALL,
1317 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 }
1319 rtnl_unlock();
1320 return ret;
1321 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001322 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001323 return -EACCES;
1324 return ip_ra_control(sk, 0, NULL);
1325 case MRT_ADD_VIF:
1326 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001327 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001328 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001329 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001330 return -EFAULT;
1331 if (vif.vifc_vifi >= MAXVIFS)
1332 return -ENFILE;
1333 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001334 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001335 ret = vif_add(net, mrt, &vif,
1336 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001337 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001338 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 }
1340 rtnl_unlock();
1341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /*
1344 * Manipulate the forwarding caches. These live
1345 * in a sort of kernel/user symbiosis.
1346 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001347 case MRT_ADD_MFC:
1348 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001349 parent = -1;
1350 case MRT_ADD_MFC_PROXY:
1351 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001352 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001353 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001354 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001355 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001356 if (parent == 0)
1357 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001358 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001359 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1360 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001362 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001363 sk == rtnl_dereference(mrt->mroute_sk),
1364 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001365 rtnl_unlock();
1366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /*
1368 * Control PIM assert.
1369 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001370 case MRT_ASSERT:
1371 {
1372 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001373 if (optlen != sizeof(v))
1374 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001375 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001376 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001377 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001378 return 0;
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001381 case MRT_PIM:
1382 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001383 int v;
1384
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001385 if (optlen != sizeof(v))
1386 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001387 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001389 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001390
Stephen Hemminger132adf52007-03-08 20:44:43 -08001391 rtnl_lock();
1392 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001393 if (v != mrt->mroute_do_pim) {
1394 mrt->mroute_do_pim = v;
1395 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001397 rtnl_unlock();
1398 return ret;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001401#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1402 case MRT_TABLE:
1403 {
1404 u32 v;
1405
1406 if (optlen != sizeof(u32))
1407 return -EINVAL;
1408 if (get_user(v, (u32 __user *)optval))
1409 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001410
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001411 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1412 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1413 return -EINVAL;
1414
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001415 rtnl_lock();
1416 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001417 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1418 ret = -EBUSY;
1419 } else {
1420 if (!ipmr_new_table(net, v))
1421 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001422 else
1423 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001424 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001425 rtnl_unlock();
1426 return ret;
1427 }
1428#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001429 /*
1430 * Spurious command, or MRT_VERSION which you cannot
1431 * set.
1432 */
1433 default:
1434 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436}
1437
1438/*
1439 * Getsock opt support for the multicast routing system.
1440 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001441
Jianjun Kongc354e122008-11-03 00:28:02 -08001442int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 int olr;
1445 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001446 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001447 struct mr_table *mrt;
1448
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001449 if (sk->sk_type != SOCK_RAW ||
1450 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1451 return -EOPNOTSUPP;
1452
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001453 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001454 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001455 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Jianjun Kongc354e122008-11-03 00:28:02 -08001457 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001459 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001461 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -ENOPROTOOPT;
1463
1464 if (get_user(olr, optlen))
1465 return -EFAULT;
1466
1467 olr = min_t(unsigned int, olr, sizeof(int));
1468 if (olr < 0)
1469 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001470
Jianjun Kongc354e122008-11-03 00:28:02 -08001471 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001473 if (optname == MRT_VERSION)
1474 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001476 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001477 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478#endif
1479 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001480 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001481 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EFAULT;
1483 return 0;
1484}
1485
1486/*
1487 * The IP multicast ioctl support routines.
1488 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1491{
1492 struct sioc_sg_req sr;
1493 struct sioc_vif_req vr;
1494 struct vif_device *vif;
1495 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001496 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001497 struct mr_table *mrt;
1498
1499 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001500 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001501 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001502
Stephen Hemminger132adf52007-03-08 20:44:43 -08001503 switch (cmd) {
1504 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001505 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001506 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001507 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001508 return -EINVAL;
1509 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001510 vif = &mrt->vif_table[vr.vifi];
1511 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001512 vr.icount = vif->pkt_in;
1513 vr.ocount = vif->pkt_out;
1514 vr.ibytes = vif->bytes_in;
1515 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001517
Jianjun Kongc354e122008-11-03 00:28:02 -08001518 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520 return 0;
1521 }
1522 read_unlock(&mrt_lock);
1523 return -EADDRNOTAVAIL;
1524 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001525 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001526 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Eric Dumazeta8c94862010-10-01 16:15:08 +00001528 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001529 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001530 if (c) {
1531 sr.pktcnt = c->mfc_un.res.pkt;
1532 sr.bytecnt = c->mfc_un.res.bytes;
1533 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001534 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001535
Jianjun Kongc354e122008-11-03 00:28:02 -08001536 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001537 return -EFAULT;
1538 return 0;
1539 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001540 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001541 return -EADDRNOTAVAIL;
1542 default:
1543 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545}
1546
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001547#ifdef CONFIG_COMPAT
1548struct compat_sioc_sg_req {
1549 struct in_addr src;
1550 struct in_addr grp;
1551 compat_ulong_t pktcnt;
1552 compat_ulong_t bytecnt;
1553 compat_ulong_t wrong_if;
1554};
1555
David S. Millerca6b8bb02011-02-03 17:24:28 -08001556struct compat_sioc_vif_req {
1557 vifi_t vifi; /* Which iface */
1558 compat_ulong_t icount;
1559 compat_ulong_t ocount;
1560 compat_ulong_t ibytes;
1561 compat_ulong_t obytes;
1562};
1563
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001564int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1565{
David S. Miller0033d5a2011-02-03 17:21:31 -08001566 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001567 struct compat_sioc_vif_req vr;
1568 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001569 struct mfc_cache *c;
1570 struct net *net = sock_net(sk);
1571 struct mr_table *mrt;
1572
1573 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001574 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001575 return -ENOENT;
1576
1577 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001578 case SIOCGETVIFCNT:
1579 if (copy_from_user(&vr, arg, sizeof(vr)))
1580 return -EFAULT;
1581 if (vr.vifi >= mrt->maxvif)
1582 return -EINVAL;
1583 read_lock(&mrt_lock);
1584 vif = &mrt->vif_table[vr.vifi];
1585 if (VIF_EXISTS(mrt, vr.vifi)) {
1586 vr.icount = vif->pkt_in;
1587 vr.ocount = vif->pkt_out;
1588 vr.ibytes = vif->bytes_in;
1589 vr.obytes = vif->bytes_out;
1590 read_unlock(&mrt_lock);
1591
1592 if (copy_to_user(arg, &vr, sizeof(vr)))
1593 return -EFAULT;
1594 return 0;
1595 }
1596 read_unlock(&mrt_lock);
1597 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001598 case SIOCGETSGCNT:
1599 if (copy_from_user(&sr, arg, sizeof(sr)))
1600 return -EFAULT;
1601
1602 rcu_read_lock();
1603 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1604 if (c) {
1605 sr.pktcnt = c->mfc_un.res.pkt;
1606 sr.bytecnt = c->mfc_un.res.bytes;
1607 sr.wrong_if = c->mfc_un.res.wrong_if;
1608 rcu_read_unlock();
1609
1610 if (copy_to_user(arg, &sr, sizeof(sr)))
1611 return -EFAULT;
1612 return 0;
1613 }
1614 rcu_read_unlock();
1615 return -EADDRNOTAVAIL;
1616 default:
1617 return -ENOIOCTLCMD;
1618 }
1619}
1620#endif
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1624{
Jiri Pirko351638e2013-05-28 01:30:21 +00001625 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001626 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001627 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct vif_device *v;
1629 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (event != NETDEV_UNREGISTER)
1632 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001633
1634 ipmr_for_each_table(mrt, net) {
1635 v = &mrt->vif_table[0];
1636 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1637 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001638 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641 return NOTIFY_DONE;
1642}
1643
1644
Jianjun Kongc354e122008-11-03 00:28:02 -08001645static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 .notifier_call = ipmr_device_event,
1647};
1648
1649/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001650 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * This avoids tunnel drivers and other mess and gives us the speed so
1652 * important for multicast video.
1653 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001654
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001655static void ip_encap(struct net *net, struct sk_buff *skb,
1656 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001658 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001659 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001660
1661 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001662 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001663 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001664 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001666 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001667 iph->tos = old_iph->tos;
1668 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 iph->frag_off = 0;
1670 iph->daddr = daddr;
1671 iph->saddr = saddr;
1672 iph->protocol = IPPROTO_IPIP;
1673 iph->ihl = 5;
1674 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001675 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 ip_send_check(iph);
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1679 nf_reset(skb);
1680}
1681
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001682static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1683 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001685 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
David S. Miller73186df2015-11-03 13:41:45 -05001687 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1688 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (unlikely(opt->optlen))
1691 ip_forward_options(skb);
1692
Eric W. Biederman13206b62015-10-07 16:48:35 -05001693 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696/*
1697 * Processing handlers for ipmr_forward
1698 */
1699
Patrick McHardy0c122952010-04-13 05:03:22 +00001700static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1701 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001703 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001704 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 struct net_device *dev;
1706 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001707 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int encap = 0;
1709
Ian Morris51456b22015-04-03 09:17:26 +01001710 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto out_free;
1712
1713#ifdef CONFIG_IP_PIMSM
1714 if (vif->flags & VIFF_REGISTER) {
1715 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001716 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001717 vif->dev->stats.tx_bytes += skb->len;
1718 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001719 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001720 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722#endif
1723
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001724 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001725 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001726 vif->remote, vif->local,
1727 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 encap = sizeof(struct iphdr);
1733 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001734 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001735 0, 0,
1736 IPPROTO_IPIP,
1737 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001738 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 goto out_free;
1740 }
1741
Changli Gaod8d1f302010-06-10 23:31:35 -07001742 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Changli Gaod8d1f302010-06-10 23:31:35 -07001744 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001746 * allow to send ICMP, so that packets will disappear
1747 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 */
1749
David S. Miller73186df2015-11-03 13:41:45 -05001750 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 ip_rt_put(rt);
1752 goto out_free;
1753 }
1754
Changli Gaod8d1f302010-06-10 23:31:35 -07001755 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001758 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 goto out_free;
1760 }
1761
1762 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001763 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Eric Dumazetadf30902009-06-02 05:19:30 +00001765 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001766 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001767 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001770 * What do we do with netfilter? -- RR
1771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001773 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001775 vif->dev->stats.tx_packets++;
1776 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778
1779 IPCB(skb)->flags |= IPSKB_FORWARDED;
1780
1781 /*
1782 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1783 * not only before forwarding, but after forwarding on all output
1784 * interfaces. It is clear, if mrouter runs a multicasting
1785 * program, it should receive packets not depending to what interface
1786 * program is joined.
1787 * If we will not make it, the program will have to join on all
1788 * interfaces. On the other hand, multihoming host (or router, but
1789 * not mrouter) cannot join to more than one interface - it will
1790 * result in receiving multiple packets.
1791 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001792 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1793 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 ipmr_forward_finish);
1795 return;
1796
1797out_free:
1798 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
Patrick McHardy0c122952010-04-13 05:03:22 +00001801static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
1803 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001804
1805 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1806 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 break;
1808 }
1809 return ct;
1810}
1811
1812/* "local" means that we should preserve one skb (for local delivery) */
1813
Rami Rosenc4854ec2013-07-20 15:09:28 +03001814static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1815 struct sk_buff *skb, struct mfc_cache *cache,
1816 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 int psend = -1;
1819 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001820 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 vif = cache->mfc_parent;
1823 cache->mfc_un.res.pkt++;
1824 cache->mfc_un.res.bytes += skb->len;
1825
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001826 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001827 struct mfc_cache *cache_proxy;
1828
1829 /* For an (*,G) entry, we only check that the incomming
1830 * interface is part of the static tree.
1831 */
1832 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1833 if (cache_proxy &&
1834 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1835 goto forward;
1836 }
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /*
1839 * Wrong interface: drop packet and (maybe) send PIM assert.
1840 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001841 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001842 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001844 * Very complicated situation...
1845 *
1846 * The best workaround until routing daemons will be
1847 * fixed is not to redistribute packet, if it was
1848 * send through wrong interface. It means, that
1849 * multicast applications WILL NOT work for
1850 * (S,G), which have default multicast route pointing
1851 * to wrong oif. In any case, it is not a good
1852 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
1854 goto dont_forward;
1855 }
1856
1857 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Patrick McHardy0c122952010-04-13 05:03:22 +00001859 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001861 * so that we cannot check that packet arrived on an oif.
1862 * It is bad, but otherwise we would need to move pretty
1863 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001865 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001866 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001867 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1869 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001870 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872 goto dont_forward;
1873 }
1874
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001875forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001876 mrt->vif_table[vif].pkt_in++;
1877 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 /*
1880 * Forward the frame
1881 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001882 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1883 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001884 if (true_vifi >= 0 &&
1885 true_vifi != cache->mfc_parent &&
1886 ip_hdr(skb)->ttl >
1887 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1888 /* It's an (*,*) entry and the packet is not coming from
1889 * the upstream: forward the packet to the upstream
1890 * only.
1891 */
1892 psend = cache->mfc_parent;
1893 goto last_forward;
1894 }
1895 goto dont_forward;
1896 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001897 for (ct = cache->mfc_un.res.maxvif - 1;
1898 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001899 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001900 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1901 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001902 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (psend != -1) {
1904 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001907 ipmr_queue_xmit(net, mrt, skb2, cache,
1908 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001910 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001913last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (psend != -1) {
1915 if (local) {
1916 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001919 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001921 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001922 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924 }
1925
1926dont_forward:
1927 if (!local)
1928 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
David S. Miller417da662011-05-03 19:42:43 -07001931static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001932{
David S. Miller417da662011-05-03 19:42:43 -07001933 struct rtable *rt = skb_rtable(skb);
1934 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001935 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001936 .daddr = iph->daddr,
1937 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001938 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001939 .flowi4_oif = (rt_is_output_route(rt) ?
1940 skb->dev->ifindex : 0),
1941 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001942 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001943 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001944 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001945 };
1946 struct mr_table *mrt;
1947 int err;
1948
David S. Millerda919812011-03-12 02:04:50 -05001949 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001950 if (err)
1951 return ERR_PTR(err);
1952 return mrt;
1953}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955/*
1956 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001957 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 */
1959
1960int ip_mr_input(struct sk_buff *skb)
1961{
1962 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001963 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001964 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001965 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001968 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001970 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 goto dont_forward;
1972
David S. Miller417da662011-05-03 19:42:43 -07001973 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001974 if (IS_ERR(mrt)) {
1975 kfree_skb(skb);
1976 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001979 if (IPCB(skb)->opt.router_alert) {
1980 if (ip_call_ra_chain(skb))
1981 return 0;
1982 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1983 /* IGMPv1 (and broken IGMPv2 implementations sort of
1984 * Cisco IOS <= 11.2(8)) do not put router alert
1985 * option to IGMP packets destined to routable
1986 * groups. It is very bad, because it means
1987 * that we can forward NO IGMP messages.
1988 */
1989 struct sock *mroute_sk;
1990
1991 mroute_sk = rcu_dereference(mrt->mroute_sk);
1992 if (mroute_sk) {
1993 nf_reset(skb);
1994 raw_rcv(mroute_sk, skb);
1995 return 0;
1996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998 }
1999
Eric Dumazeta8c94862010-10-01 16:15:08 +00002000 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002001 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002002 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002003 int vif = ipmr_find_vif(mrt, skb->dev);
2004
2005 if (vif >= 0)
2006 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2007 vif);
2008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 /*
2011 * No usable cache entry
2012 */
Ian Morris51456b22015-04-03 09:17:26 +01002013 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 int vif;
2015
2016 if (local) {
2017 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2018 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002019 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 skb = skb2;
2022 }
2023
Eric Dumazeta8c94862010-10-01 16:15:08 +00002024 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002025 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002027 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 read_unlock(&mrt_lock);
2029
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002030 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
2032 read_unlock(&mrt_lock);
2033 kfree_skb(skb);
2034 return -ENODEV;
2035 }
2036
Eric Dumazeta8c94862010-10-01 16:15:08 +00002037 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002038 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 read_unlock(&mrt_lock);
2040
2041 if (local)
2042 return ip_local_deliver(skb);
2043
2044 return 0;
2045
2046dont_forward:
2047 if (local)
2048 return ip_local_deliver(skb);
2049 kfree_skb(skb);
2050 return 0;
2051}
2052
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002053#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00002054/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002055static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2056 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002058 struct net_device *reg_dev = NULL;
2059 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002061 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002063 * Check that:
2064 * a. packet is really sent to a multicast group
2065 * b. packet is not a NULL-REGISTER
2066 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002068 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002070 ntohs(encap->tot_len) + pimlen > skb->len)
2071 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002074 if (mrt->mroute_reg_vif_num >= 0)
2075 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 read_unlock(&mrt_lock);
2077
Ian Morris51456b22015-04-03 09:17:26 +01002078 if (!reg_dev)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002079 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002081 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00002082 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03002083 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00002085 skb->ip_summed = CHECKSUM_NONE;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002086
Nicolas Dichtelea231922013-09-02 15:34:58 +02002087 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002090
Eric Dumazet55747a02010-10-01 16:14:55 +00002091 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002092}
2093#endif
2094
2095#ifdef CONFIG_IP_PIMSM_V1
2096/*
2097 * Handle IGMP messages of PIMv1
2098 */
2099
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002100int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002101{
2102 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002103 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002104 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002105
2106 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2107 goto drop;
2108
2109 pim = igmp_hdr(skb);
2110
David S. Miller417da662011-05-03 19:42:43 -07002111 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002112 if (IS_ERR(mrt))
2113 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002114 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002115 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2116 goto drop;
2117
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002118 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002119drop:
2120 kfree_skb(skb);
2121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return 0;
2123}
2124#endif
2125
2126#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002127static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002130 struct net *net = dev_net(skb->dev);
2131 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002133 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 goto drop;
2135
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002136 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002137 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2138 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002139 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002140 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 goto drop;
2142
David S. Miller417da662011-05-03 19:42:43 -07002143 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002144 if (IS_ERR(mrt))
2145 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002146 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002147drop:
2148 kfree_skb(skb);
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return 0;
2151}
2152#endif
2153
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002154static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2155 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 int ct;
2158 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002159 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002160 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Nicolas Dichtel74381892010-03-25 23:45:35 +00002162 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002163 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002164 return -ENOENT;
2165
Thomas Graf92a395e2012-06-26 23:36:13 +00002166 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2167 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2168 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Thomas Graf92a395e2012-06-26 23:36:13 +00002170 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2171 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002174 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002175 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2176 nla_nest_cancel(skb, mp_attr);
2177 return -EMSGSIZE;
2178 }
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 nhp->rtnh_flags = 0;
2181 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002182 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 nhp->rtnh_len = sizeof(*nhp);
2184 }
2185 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002186
2187 nla_nest_end(skb, mp_attr);
2188
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002189 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2190 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2191 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2192 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2193 return -EMSGSIZE;
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 rtm->rtm_type = RTN_MULTICAST;
2196 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
David S. Miller9a1b9492011-05-04 12:18:54 -07002199int ipmr_get_route(struct net *net, struct sk_buff *skb,
2200 __be32 saddr, __be32 daddr,
2201 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002204 struct mr_table *mrt;
2205 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002207 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002208 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002209 return -ENOENT;
2210
Eric Dumazeta8c94862010-10-01 16:15:08 +00002211 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002212 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002213 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002214 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002216 if (vif >= 0)
2217 cache = ipmr_cache_find_any(mrt, daddr, vif);
2218 }
Ian Morris51456b22015-04-03 09:17:26 +01002219 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002220 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002221 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002223 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002226 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return -EAGAIN;
2228 }
2229
2230 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002231 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002232 if (dev)
2233 vif = ipmr_find_vif(mrt, dev);
2234 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002236 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return -ENODEV;
2238 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002239 skb2 = skb_clone(skb, GFP_ATOMIC);
2240 if (!skb2) {
2241 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002242 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002243 return -ENOMEM;
2244 }
2245
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002246 skb_push(skb2, sizeof(struct iphdr));
2247 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002248 iph = ip_hdr(skb2);
2249 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002250 iph->saddr = saddr;
2251 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002252 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002253 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002255 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return err;
2257 }
2258
Eric Dumazeta8c94862010-10-01 16:15:08 +00002259 read_lock(&mrt_lock);
2260 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002262 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002264 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return err;
2266}
2267
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002268static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002269 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2270 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002271{
2272 struct nlmsghdr *nlh;
2273 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002274 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002275
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002276 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002277 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002278 return -EMSGSIZE;
2279
2280 rtm = nlmsg_data(nlh);
2281 rtm->rtm_family = RTNL_FAMILY_IPMR;
2282 rtm->rtm_dst_len = 32;
2283 rtm->rtm_src_len = 32;
2284 rtm->rtm_tos = 0;
2285 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002286 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2287 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002288 rtm->rtm_type = RTN_MULTICAST;
2289 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002290 if (c->mfc_flags & MFC_STATIC)
2291 rtm->rtm_protocol = RTPROT_STATIC;
2292 else
2293 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002294 rtm->rtm_flags = 0;
2295
Jiri Benc930345e2015-03-29 16:59:25 +02002296 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2297 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002298 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002299 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2300 /* do not break the dump if cache is unresolved */
2301 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002302 goto nla_put_failure;
2303
Johannes Berg053c0952015-01-16 22:09:00 +01002304 nlmsg_end(skb, nlh);
2305 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002306
2307nla_put_failure:
2308 nlmsg_cancel(skb, nlh);
2309 return -EMSGSIZE;
2310}
2311
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002312static size_t mroute_msgsize(bool unresolved, int maxvif)
2313{
2314 size_t len =
2315 NLMSG_ALIGN(sizeof(struct rtmsg))
2316 + nla_total_size(4) /* RTA_TABLE */
2317 + nla_total_size(4) /* RTA_SRC */
2318 + nla_total_size(4) /* RTA_DST */
2319 ;
2320
2321 if (!unresolved)
2322 len = len
2323 + nla_total_size(4) /* RTA_IIF */
2324 + nla_total_size(0) /* RTA_MULTIPATH */
2325 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2326 /* RTA_MFC_STATS */
2327 + nla_total_size(sizeof(struct rta_mfc_stats))
2328 ;
2329
2330 return len;
2331}
2332
2333static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2334 int cmd)
2335{
2336 struct net *net = read_pnet(&mrt->net);
2337 struct sk_buff *skb;
2338 int err = -ENOBUFS;
2339
2340 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2341 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002342 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002343 goto errout;
2344
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002345 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002346 if (err < 0)
2347 goto errout;
2348
2349 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2350 return;
2351
2352errout:
2353 kfree_skb(skb);
2354 if (err < 0)
2355 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2356}
2357
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002358static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2359{
2360 struct net *net = sock_net(skb->sk);
2361 struct mr_table *mrt;
2362 struct mfc_cache *mfc;
2363 unsigned int t = 0, s_t;
2364 unsigned int h = 0, s_h;
2365 unsigned int e = 0, s_e;
2366
2367 s_t = cb->args[0];
2368 s_h = cb->args[1];
2369 s_e = cb->args[2];
2370
Eric Dumazeta8c94862010-10-01 16:15:08 +00002371 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002372 ipmr_for_each_table(mrt, net) {
2373 if (t < s_t)
2374 goto next_table;
2375 if (t > s_t)
2376 s_h = 0;
2377 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002378 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002379 if (e < s_e)
2380 goto next_entry;
2381 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002382 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002383 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002384 mfc, RTM_NEWROUTE,
2385 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002386 goto done;
2387next_entry:
2388 e++;
2389 }
2390 e = s_e = 0;
2391 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002392 spin_lock_bh(&mfc_unres_lock);
2393 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2394 if (e < s_e)
2395 goto next_entry2;
2396 if (ipmr_fill_mroute(mrt, skb,
2397 NETLINK_CB(cb->skb).portid,
2398 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002399 mfc, RTM_NEWROUTE,
2400 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002401 spin_unlock_bh(&mfc_unres_lock);
2402 goto done;
2403 }
2404next_entry2:
2405 e++;
2406 }
2407 spin_unlock_bh(&mfc_unres_lock);
2408 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002409 s_h = 0;
2410next_table:
2411 t++;
2412 }
2413done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002414 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002415
2416 cb->args[2] = e;
2417 cb->args[1] = h;
2418 cb->args[0] = t;
2419
2420 return skb->len;
2421}
2422
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002423#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002425 * The /proc interfaces to multicast routing :
2426 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 */
2428struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002429 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002430 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 int ct;
2432};
2433
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002434static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2435 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 loff_t pos)
2437{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002438 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002439
2440 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2441 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002443 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002444 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446 return NULL;
2447}
2448
2449static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002450 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002452 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002453 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002454 struct mr_table *mrt;
2455
2456 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002457 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002458 return ERR_PTR(-ENOENT);
2459
2460 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002463 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 : SEQ_START_TOKEN;
2465}
2466
2467static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2468{
2469 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002470 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002471 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 ++*pos;
2474 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002475 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002476
Patrick McHardy0c122952010-04-13 05:03:22 +00002477 while (++iter->ct < mrt->maxvif) {
2478 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002480 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482 return NULL;
2483}
2484
2485static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002486 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
2488 read_unlock(&mrt_lock);
2489}
2490
2491static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2492{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002493 struct ipmr_vif_iter *iter = seq->private;
2494 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002497 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2499 } else {
2500 const struct vif_device *vif = v;
2501 const char *name = vif->dev ? vif->dev->name : "none";
2502
2503 seq_printf(seq,
2504 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002505 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002506 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 vif->bytes_out, vif->pkt_out,
2508 vif->flags, vif->local, vif->remote);
2509 }
2510 return 0;
2511}
2512
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002513static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 .start = ipmr_vif_seq_start,
2515 .next = ipmr_vif_seq_next,
2516 .stop = ipmr_vif_seq_stop,
2517 .show = ipmr_vif_seq_show,
2518};
2519
2520static int ipmr_vif_open(struct inode *inode, struct file *file)
2521{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002522 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2523 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
Arjan van de Ven9a321442007-02-12 00:55:35 -08002526static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 .owner = THIS_MODULE,
2528 .open = ipmr_vif_open,
2529 .read = seq_read,
2530 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002531 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532};
2533
2534struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002535 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002536 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002537 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 int ct;
2539};
2540
2541
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002542static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2543 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002545 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 struct mfc_cache *mfc;
2547
Eric Dumazeta8c94862010-10-01 16:15:08 +00002548 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002549 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002550 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002551 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002552 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002554 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002555 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002558 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002559 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002560 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return mfc;
2562 spin_unlock_bh(&mfc_unres_lock);
2563
2564 it->cache = NULL;
2565 return NULL;
2566}
2567
2568
2569static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2570{
2571 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002572 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002573 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002574
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002575 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002576 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002577 return ERR_PTR(-ENOENT);
2578
2579 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 it->cache = NULL;
2581 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002582 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 : SEQ_START_TOKEN;
2584}
2585
2586static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2587{
2588 struct mfc_cache *mfc = v;
2589 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002590 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002591 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 ++*pos;
2594
2595 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002596 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Patrick McHardy862465f2010-04-13 05:03:21 +00002598 if (mfc->list.next != it->cache)
2599 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002600
Patrick McHardy0c122952010-04-13 05:03:22 +00002601 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 goto end_of_list;
2603
Patrick McHardy0c122952010-04-13 05:03:22 +00002604 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002607 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002608 if (list_empty(it->cache))
2609 continue;
2610 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612
2613 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002614 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002615 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002619 if (!list_empty(it->cache))
2620 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002622end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 spin_unlock_bh(&mfc_unres_lock);
2624 it->cache = NULL;
2625
2626 return NULL;
2627}
2628
2629static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2630{
2631 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002632 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Patrick McHardy0c122952010-04-13 05:03:22 +00002634 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002636 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002637 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
2640static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2641{
2642 int n;
2643
2644 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002645 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2647 } else {
2648 const struct mfc_cache *mfc = v;
2649 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002650 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002651
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002652 seq_printf(seq, "%08X %08X %-3hd",
2653 (__force u32) mfc->mfc_mcastgrp,
2654 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002655 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Patrick McHardy0c122952010-04-13 05:03:22 +00002657 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002658 seq_printf(seq, " %8lu %8lu %8lu",
2659 mfc->mfc_un.res.pkt,
2660 mfc->mfc_un.res.bytes,
2661 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002662 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002663 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002664 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002665 mfc->mfc_un.res.ttls[n] < 255)
2666 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002667 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 n, mfc->mfc_un.res.ttls[n]);
2669 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002670 } else {
2671 /* unresolved mfc_caches don't contain
2672 * pkt, bytes and wrong_if values
2673 */
2674 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676 seq_putc(seq, '\n');
2677 }
2678 return 0;
2679}
2680
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002681static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 .start = ipmr_mfc_seq_start,
2683 .next = ipmr_mfc_seq_next,
2684 .stop = ipmr_mfc_seq_stop,
2685 .show = ipmr_mfc_seq_show,
2686};
2687
2688static int ipmr_mfc_open(struct inode *inode, struct file *file)
2689{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002690 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2691 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
Arjan van de Ven9a321442007-02-12 00:55:35 -08002694static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 .owner = THIS_MODULE,
2696 .open = ipmr_mfc_open,
2697 .read = seq_read,
2698 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002699 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002704static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002706 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707};
2708#endif
2709
2710
2711/*
2712 * Setup for IP multicast routing
2713 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002714static int __net_init ipmr_net_init(struct net *net)
2715{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002716 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002717
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002718 err = ipmr_rules_init(net);
2719 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002720 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002721
2722#ifdef CONFIG_PROC_FS
2723 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002724 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002725 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002726 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002727 goto proc_cache_fail;
2728#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002729 return 0;
2730
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002731#ifdef CONFIG_PROC_FS
2732proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002733 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002734proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002735 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002736#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002737fail:
2738 return err;
2739}
2740
2741static void __net_exit ipmr_net_exit(struct net *net)
2742{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002743#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002744 remove_proc_entry("ip_mr_cache", net->proc_net);
2745 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002746#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002747 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002748}
2749
2750static struct pernet_operations ipmr_net_ops = {
2751 .init = ipmr_net_init,
2752 .exit = ipmr_net_exit,
2753};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002754
Wang Chen03d2f892008-07-03 12:13:36 +08002755int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
Wang Chen03d2f892008-07-03 12:13:36 +08002757 int err;
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2760 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002761 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002762 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002763 if (!mrt_cachep)
2764 return -ENOMEM;
2765
Benjamin Therycf958ae32009-01-22 04:56:16 +00002766 err = register_pernet_subsys(&ipmr_net_ops);
2767 if (err)
2768 goto reg_pernet_fail;
2769
Wang Chen03d2f892008-07-03 12:13:36 +08002770 err = register_netdevice_notifier(&ip_mr_notifier);
2771 if (err)
2772 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002773#ifdef CONFIG_IP_PIMSM_V2
2774 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002775 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002776 err = -EAGAIN;
2777 goto add_proto_fail;
2778 }
2779#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002780 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2781 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002782 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002783
Tom Goff403dbb92009-06-14 03:16:13 -07002784#ifdef CONFIG_IP_PIMSM_V2
2785add_proto_fail:
2786 unregister_netdevice_notifier(&ip_mr_notifier);
2787#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002788reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002789 unregister_pernet_subsys(&ipmr_net_ops);
2790reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002791 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}