blob: c204b728bbc14f3abbc6964cb42dbf7ef226bda1 [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);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000137static void mroute_clean_tables(struct mr_table *mrt);
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,
236 .default_pref = fib_default_rule_pref,
237 .fill = ipmr_rule_fill,
238 .nlgroup = RTNLGRP_IPV4_RULE,
239 .policy = ipmr_rule_policy,
240 .owner = THIS_MODULE,
241};
242
243static int __net_init ipmr_rules_init(struct net *net)
244{
245 struct fib_rules_ops *ops;
246 struct mr_table *mrt;
247 int err;
248
249 ops = fib_rules_register(&ipmr_rules_ops_template, net);
250 if (IS_ERR(ops))
251 return PTR_ERR(ops);
252
253 INIT_LIST_HEAD(&net->ipv4.mr_tables);
254
255 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +0100256 if (!mrt) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000257 err = -ENOMEM;
258 goto err1;
259 }
260
261 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
262 if (err < 0)
263 goto err2;
264
265 net->ipv4.mr_rules_ops = ops;
266 return 0;
267
268err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700269 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000270err1:
271 fib_rules_unregister(ops);
272 return err;
273}
274
275static void __net_exit ipmr_rules_exit(struct net *net)
276{
277 struct mr_table *mrt, *next;
278
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);
284}
285#else
286#define ipmr_for_each_table(mrt, net) \
287 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
288
289static struct mr_table *ipmr_get_table(struct net *net, u32 id)
290{
291 return net->ipv4.mrt;
292}
293
David S. Millerda919812011-03-12 02:04:50 -0500294static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000295 struct mr_table **mrt)
296{
297 *mrt = net->ipv4.mrt;
298 return 0;
299}
300
301static int __net_init ipmr_rules_init(struct net *net)
302{
303 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
304 return net->ipv4.mrt ? 0 : -ENOMEM;
305}
306
307static void __net_exit ipmr_rules_exit(struct net *net)
308{
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000309 ipmr_free_table(net->ipv4.mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000310}
311#endif
312
313static struct mr_table *ipmr_new_table(struct net *net, u32 id)
314{
315 struct mr_table *mrt;
316 unsigned int i;
317
318 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100319 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000320 return mrt;
321
322 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100323 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000324 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200325 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000326 mrt->id = id;
327
328 /* Forwarding cache */
329 for (i = 0; i < MFC_LINES; i++)
330 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
331
332 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
333
334 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
335 (unsigned long)mrt);
336
337#ifdef CONFIG_IP_PIMSM
338 mrt->mroute_reg_vif_num = -1;
339#endif
340#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
341 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
342#endif
343 return mrt;
344}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000346static void ipmr_free_table(struct mr_table *mrt)
347{
348 del_timer_sync(&mrt->ipmr_expire_timer);
349 mroute_clean_tables(mrt);
350 kfree(mrt);
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
354
Wang Chend6070322008-07-14 20:55:26 -0700355static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
356{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000357 struct net *net = dev_net(dev);
358
Wang Chend6070322008-07-14 20:55:26 -0700359 dev_close(dev);
360
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000361 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700362 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800363 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700364 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700365 struct ip_tunnel_parm p;
366
367 memset(&p, 0, sizeof(p));
368 p.iph.daddr = v->vifc_rmt_addr.s_addr;
369 p.iph.saddr = v->vifc_lcl_addr.s_addr;
370 p.iph.version = 4;
371 p.iph.ihl = 5;
372 p.iph.protocol = IPPROTO_IPIP;
373 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
374 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
375
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800376 if (ops->ndo_do_ioctl) {
377 mm_segment_t oldfs = get_fs();
378
379 set_fs(KERNEL_DS);
380 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
381 set_fs(oldfs);
382 }
Wang Chend6070322008-07-14 20:55:26 -0700383 }
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000387struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct net_device *dev;
390
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000391 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800394 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int err;
396 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct ip_tunnel_parm p;
398 struct in_device *in_dev;
399
400 memset(&p, 0, sizeof(p));
401 p.iph.daddr = v->vifc_rmt_addr.s_addr;
402 p.iph.saddr = v->vifc_lcl_addr.s_addr;
403 p.iph.version = 4;
404 p.iph.ihl = 5;
405 p.iph.protocol = IPPROTO_IPIP;
406 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800407 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800409 if (ops->ndo_do_ioctl) {
410 mm_segment_t oldfs = get_fs();
411
412 set_fs(KERNEL_DS);
413 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
414 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000415 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800416 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 dev = NULL;
419
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000420 if (err == 0 &&
421 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 dev->flags |= IFF_MULTICAST;
423
Herbert Xue5ed6392005-10-03 14:35:55 -0700424 in_dev = __in_dev_get_rtnl(dev);
Ian Morris51456b22015-04-03 09:17:26 +0100425 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700427
428 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100429 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700430 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (dev_open(dev))
433 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700434 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436 }
437 return dev;
438
439failure:
440 /* allow the register to be completed before unregistering. */
441 rtnl_unlock();
442 rtnl_lock();
443
444 unregister_netdevice(dev);
445 return NULL;
446}
447
448#ifdef CONFIG_IP_PIMSM
449
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000450static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000452 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000453 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500454 struct flowi4 fl4 = {
455 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700456 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500457 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000458 };
459 int err;
460
David S. Millerda919812011-03-12 02:04:50 -0500461 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000462 if (err < 0) {
463 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000464 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000465 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700468 dev->stats.tx_bytes += skb->len;
469 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000470 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 read_unlock(&mrt_lock);
472 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000473 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200476static int reg_vif_get_iflink(const struct net_device *dev)
477{
478 return 0;
479}
480
Stephen Hemminger007c3832008-11-20 20:28:35 -0800481static const struct net_device_ops reg_vif_netdev_ops = {
482 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200483 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800484};
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static void reg_vif_setup(struct net_device *dev)
487{
488 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800489 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530491 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700493 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000496static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct net_device *dev;
499 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000500 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000502 if (mrt->id == RT_TABLE_DEFAULT)
503 sprintf(name, "pimreg");
504 else
505 sprintf(name, "pimreg%u", mrt->id);
506
Tom Gundersenc835a672014-07-14 16:37:24 +0200507 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Ian Morris51456b22015-04-03 09:17:26 +0100509 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return NULL;
511
Tom Goff403dbb92009-06-14 03:16:13 -0700512 dev_net_set(dev, net);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (register_netdevice(dev)) {
515 free_netdev(dev);
516 return NULL;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Herbert Xu71e27da2007-06-04 23:36:06 -0700519 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000520 in_dev = __in_dev_get_rcu(dev);
521 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700522 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Herbert Xu71e27da2007-06-04 23:36:06 -0700526 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100527 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700528 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
529 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (dev_open(dev))
532 goto failure;
533
Wang Chen7dc00c82008-07-14 20:56:34 -0700534 dev_hold(dev);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return dev;
537
538failure:
539 /* allow the register to be completed before unregistering. */
540 rtnl_unlock();
541 rtnl_lock();
542
543 unregister_netdevice(dev);
544 return NULL;
545}
546#endif
547
Ben Hutchings2c530402012-07-10 10:55:09 +0000548/**
549 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700550 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900552
Patrick McHardy0c122952010-04-13 05:03:22 +0000553static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000554 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct vif_device *v;
557 struct net_device *dev;
558 struct in_device *in_dev;
559
Patrick McHardy0c122952010-04-13 05:03:22 +0000560 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return -EADDRNOTAVAIL;
562
Patrick McHardy0c122952010-04-13 05:03:22 +0000563 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 write_lock_bh(&mrt_lock);
566 dev = v->dev;
567 v->dev = NULL;
568
569 if (!dev) {
570 write_unlock_bh(&mrt_lock);
571 return -EADDRNOTAVAIL;
572 }
573
574#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000575 if (vifi == mrt->mroute_reg_vif_num)
576 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577#endif
578
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000579 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000581
582 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000583 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 break;
585 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000586 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
589 write_unlock_bh(&mrt_lock);
590
591 dev_set_allmulti(dev, -1);
592
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000593 in_dev = __in_dev_get_rtnl(dev);
594 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700595 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000596 inet_netconf_notify_devconf(dev_net(dev),
597 NETCONFA_MC_FORWARDING,
598 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 ip_rt_multicast_event(in_dev);
600 }
601
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000602 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000603 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 dev_put(dev);
606 return 0;
607}
608
Eric Dumazeta8c94862010-10-01 16:15:08 +0000609static void ipmr_cache_free_rcu(struct rcu_head *head)
610{
611 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
612
613 kmem_cache_free(mrt_cachep, c);
614}
615
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000616static inline void ipmr_cache_free(struct mfc_cache *c)
617{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000618 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000622 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
624
Patrick McHardy0c122952010-04-13 05:03:22 +0000625static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200627 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700629 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Patrick McHardy0c122952010-04-13 05:03:22 +0000631 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jianjun Kongc354e122008-11-03 00:28:02 -0800633 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700634 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
636 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000637 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000639 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700640 e->error = -ETIMEDOUT;
641 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700642
Eric W. Biederman15e47302012-09-07 20:12:54 +0000643 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000644 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000649 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652
Patrick McHardye258beb2010-04-13 05:03:19 +0000653/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Patrick McHardye258beb2010-04-13 05:03:19 +0000655static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Patrick McHardy0c122952010-04-13 05:03:22 +0000657 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 unsigned long now;
659 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000660 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000663 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return;
665 }
666
Patrick McHardy0c122952010-04-13 05:03:22 +0000667 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 goto out;
669
670 now = jiffies;
671 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Patrick McHardy0c122952010-04-13 05:03:22 +0000673 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (time_after(c->mfc_un.unres.expires, now)) {
675 unsigned long interval = c->mfc_un.unres.expires - now;
676 if (interval < expires)
677 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 continue;
679 }
680
Patrick McHardy862465f2010-04-13 05:03:21 +0000681 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000682 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000683 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
Patrick McHardy0c122952010-04-13 05:03:22 +0000686 if (!list_empty(&mrt->mfc_unres_queue))
687 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689out:
690 spin_unlock(&mfc_unres_lock);
691}
692
693/* Fill oifs list. It is called under write locked mrt_lock. */
694
Patrick McHardy0c122952010-04-13 05:03:22 +0000695static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000696 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 int vifi;
699
700 cache->mfc_un.res.minvif = MAXVIFS;
701 cache->mfc_un.res.maxvif = 0;
702 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
703
Patrick McHardy0c122952010-04-13 05:03:22 +0000704 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
705 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000706 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
708 if (cache->mfc_un.res.minvif > vifi)
709 cache->mfc_un.res.minvif = vifi;
710 if (cache->mfc_un.res.maxvif <= vifi)
711 cache->mfc_un.res.maxvif = vifi + 1;
712 }
713 }
714}
715
Patrick McHardy0c122952010-04-13 05:03:22 +0000716static int vif_add(struct net *net, struct mr_table *mrt,
717 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000720 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct net_device *dev;
722 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700723 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000726 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -EADDRINUSE;
728
729 switch (vifc->vifc_flags) {
730#ifdef CONFIG_IP_PIMSM
731 case VIFF_REGISTER:
732 /*
733 * Special Purpose VIF in PIM
734 * All the packets will be sent to the daemon
735 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000736 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000738 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!dev)
740 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700741 err = dev_set_allmulti(dev, 1);
742 if (err) {
743 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700744 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700745 return err;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900749 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000750 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!dev)
752 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700753 err = dev_set_allmulti(dev, 1);
754 if (err) {
755 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700756 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700757 return err;
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000760
761 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000763 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
764 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100765 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000766 dev_put(dev);
767 return -EADDRNOTAVAIL;
768 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000769 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000770 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!dev)
773 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700774 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700775 if (err) {
776 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700777 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
780 default:
781 return -EINVAL;
782 }
783
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000784 in_dev = __in_dev_get_rtnl(dev);
785 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000786 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000788 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700789 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000790 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
791 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ip_rt_multicast_event(in_dev);
793
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000794 /* Fill in the VIF structures */
795
Jianjun Kongc354e122008-11-03 00:28:02 -0800796 v->rate_limit = vifc->vifc_rate_limit;
797 v->local = vifc->vifc_lcl_addr.s_addr;
798 v->remote = vifc->vifc_rmt_addr.s_addr;
799 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!mrtsock)
801 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800802 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 v->bytes_in = 0;
804 v->bytes_out = 0;
805 v->pkt_in = 0;
806 v->pkt_out = 0;
807 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000808 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200809 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* And finish update writing critical data */
812 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800813 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000815 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000816 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000818 if (vifi+1 > mrt->maxvif)
819 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 write_unlock_bh(&mrt_lock);
821 return 0;
822}
823
Eric Dumazeta8c94862010-10-01 16:15:08 +0000824/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000825static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000826 __be32 origin,
827 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Jianjun Kongc354e122008-11-03 00:28:02 -0800829 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct mfc_cache *c;
831
Eric Dumazeta8c94862010-10-01 16:15:08 +0000832 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000833 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
834 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000836 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000839/* Look for a (*,*,oif) entry */
840static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
841 int vifi)
842{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100843 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000844 struct mfc_cache *c;
845
846 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100847 if (c->mfc_origin == htonl(INADDR_ANY) &&
848 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000849 c->mfc_un.res.ttls[vifi] < 255)
850 return c;
851
852 return NULL;
853}
854
855/* Look for a (*,G) entry */
856static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
857 __be32 mcastgrp, int vifi)
858{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100859 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000860 struct mfc_cache *c, *proxy;
861
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100862 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000863 goto skip;
864
865 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100866 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000867 c->mfc_mcastgrp == mcastgrp) {
868 if (c->mfc_un.res.ttls[vifi] < 255)
869 return c;
870
871 /* It's ok if the vifi is part of the static tree */
872 proxy = ipmr_cache_find_any_parent(mrt,
873 c->mfc_parent);
874 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
875 return c;
876 }
877
878skip:
879 return ipmr_cache_find_any_parent(mrt, vifi);
880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882/*
883 * Allocate a multicast cache entry
884 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000885static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Jianjun Kongc354e122008-11-03 00:28:02 -0800887 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000888
889 if (c)
890 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return c;
892}
893
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000894static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Jianjun Kongc354e122008-11-03 00:28:02 -0800896 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000897
898 if (c) {
899 skb_queue_head_init(&c->mfc_un.unres.unresolved);
900 c->mfc_un.unres.expires = jiffies + 10*HZ;
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return c;
903}
904
905/*
906 * A cache entry has gone into a resolved state from queued
907 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900908
Patrick McHardy0c122952010-04-13 05:03:22 +0000909static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
910 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700913 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000915 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Jianjun Kongc354e122008-11-03 00:28:02 -0800917 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700918 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
920
Hong zhi guo573ce262013-03-27 06:47:04 +0000921 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000922 nlh->nlmsg_len = skb_tail_pointer(skb) -
923 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 } else {
925 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000926 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000928 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700929 e->error = -EMSGSIZE;
930 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Thomas Graf2942e902006-08-15 00:30:25 -0700932
Eric W. Biederman15e47302012-09-07 20:12:54 +0000933 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000934 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000935 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938}
939
940/*
941 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
942 * expects the following bizarre scheme.
943 *
944 * Called under mrt_lock.
945 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900946
Patrick McHardy0c122952010-04-13 05:03:22 +0000947static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000948 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300951 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct igmphdr *igmp;
953 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000954 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int ret;
956
957#ifdef CONFIG_IP_PIMSM
958 if (assert == IGMPMSG_WHOLEPKT)
959 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
960 else
961#endif
962 skb = alloc_skb(128, GFP_ATOMIC);
963
Stephen Hemminger132adf52007-03-08 20:44:43 -0800964 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -ENOBUFS;
966
967#ifdef CONFIG_IP_PIMSM
968 if (assert == IGMPMSG_WHOLEPKT) {
969 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000970 * Duplicate old header, fix ihl, length etc.
971 * And all this only to mangle msg->im_msgtype and
972 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300974 skb_push(skb, sizeof(struct iphdr));
975 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300976 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300977 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700978 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 msg->im_msgtype = IGMPMSG_WHOLEPKT;
980 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000981 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700982 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
983 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
984 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900985 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900987 {
988
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000989 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Cong Wang30f3a402013-06-05 20:14:10 +0800991 skb_set_network_header(skb, skb->len);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300992 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300993 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000994 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700995 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000997 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000999 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001001 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 igmp->type =
1003 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001004 igmp->code = 0;
1005 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001006 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Eric Dumazet4c968702010-10-01 16:15:01 +00001009 rcu_read_lock();
1010 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001011 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001012 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 kfree_skb(skb);
1014 return -EINVAL;
1015 }
1016
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001017 /* Deliver to mrouted */
1018
Eric Dumazet4c968702010-10-01 16:15:01 +00001019 ret = sock_queue_rcv_skb(mroute_sk, skb);
1020 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001021 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001022 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 kfree_skb(skb);
1024 }
1025
1026 return ret;
1027}
1028
1029/*
1030 * Queue a packet for resolution. It gets locked cache entry!
1031 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001034ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Patrick McHardy862465f2010-04-13 05:03:21 +00001036 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 int err;
1038 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001039 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001042 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001043 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001044 c->mfc_origin == iph->saddr) {
1045 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
Patrick McHardy862465f2010-04-13 05:03:21 +00001050 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001051 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Patrick McHardy0c122952010-04-13 05:03:22 +00001053 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001054 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 spin_unlock_bh(&mfc_unres_lock);
1056
1057 kfree_skb(skb);
1058 return -ENOBUFS;
1059 }
1060
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001061 /* Fill in the new cache entry */
1062
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001063 c->mfc_parent = -1;
1064 c->mfc_origin = iph->saddr;
1065 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001067 /* Reflect first query at mrouted. */
1068
Patrick McHardy0c122952010-04-13 05:03:22 +00001069 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001070 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001071 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 out - Brad Parker
1073 */
1074 spin_unlock_bh(&mfc_unres_lock);
1075
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001076 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 kfree_skb(skb);
1078 return err;
1079 }
1080
Patrick McHardy0c122952010-04-13 05:03:22 +00001081 atomic_inc(&mrt->cache_resolve_queue_len);
1082 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001083 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
David S. Miller278554b2010-05-12 00:05:35 -07001085 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1086 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001089 /* See if we can append the packet */
1090
1091 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 kfree_skb(skb);
1093 err = -ENOBUFS;
1094 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001095 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 err = 0;
1097 }
1098
1099 spin_unlock_bh(&mfc_unres_lock);
1100 return err;
1101}
1102
1103/*
1104 * MFC cache manipulation by user space mroute daemon
1105 */
1106
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001107static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001110 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Jianjun Kongc354e122008-11-03 00:28:02 -08001112 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Patrick McHardy0c122952010-04-13 05:03:22 +00001114 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001116 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1117 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001118 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001119 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001120 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return 0;
1122 }
1123 }
1124 return -ENOENT;
1125}
1126
Patrick McHardy0c122952010-04-13 05:03:22 +00001127static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001128 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Patrick McHardy862465f2010-04-13 05:03:21 +00001130 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001132 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Patrick McHardya50436f22010-03-17 06:04:14 +00001134 if (mfc->mfcc_parent >= MAXVIFS)
1135 return -ENFILE;
1136
Jianjun Kongc354e122008-11-03 00:28:02 -08001137 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Patrick McHardy0c122952010-04-13 05:03:22 +00001139 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001141 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1142 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001143 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
Patrick McHardy862465f2010-04-13 05:03:21 +00001148 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 write_lock_bh(&mrt_lock);
1150 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001151 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (!mrtsock)
1153 c->mfc_flags |= MFC_STATIC;
1154 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001155 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return 0;
1157 }
1158
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001159 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001160 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return -EINVAL;
1162
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001163 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001164 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return -ENOMEM;
1166
Jianjun Kongc354e122008-11-03 00:28:02 -08001167 c->mfc_origin = mfc->mfcc_origin.s_addr;
1168 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1169 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001170 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (!mrtsock)
1172 c->mfc_flags |= MFC_STATIC;
1173
Eric Dumazeta8c94862010-10-01 16:15:08 +00001174 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /*
1177 * Check to see if we resolved a queued list. If so we
1178 * need to send on the frames and tidy up.
1179 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001180 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001182 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001183 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001185 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001186 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001187 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 break;
1189 }
1190 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001191 if (list_empty(&mrt->mfc_unres_queue))
1192 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 spin_unlock_bh(&mfc_unres_lock);
1194
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001195 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001196 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001197 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001199 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
1203/*
1204 * Close the multicast socket, and clear the vif tables etc
1205 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001206
Patrick McHardy0c122952010-04-13 05:03:22 +00001207static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001210 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001211 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001212
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001213 /* Shut down all active vif entries */
1214
Patrick McHardy0c122952010-04-13 05:03:22 +00001215 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001216 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001217 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001219 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001221 /* Wipe the cache */
1222
Patrick McHardy862465f2010-04-13 05:03:21 +00001223 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001224 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001225 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001227 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001228 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001229 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231 }
1232
Patrick McHardy0c122952010-04-13 05:03:22 +00001233 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001235 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001236 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001237 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001238 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240 spin_unlock_bh(&mfc_unres_lock);
1241 }
1242}
1243
Eric Dumazet4c968702010-10-01 16:15:01 +00001244/* called from ip_ra_control(), before an RCU grace period,
1245 * we dont need to call synchronize_rcu() here
1246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247static void mrtsock_destruct(struct sock *sk)
1248{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001249 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001250 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001253 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001254 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001255 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001256 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1257 NETCONFA_IFINDEX_ALL,
1258 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001259 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001260 mroute_clean_tables(mrt);
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 rtnl_unlock();
1264}
1265
1266/*
1267 * Socket options and virtual interface manipulation. The whole
1268 * virtual interface system is a complete heap, but unfortunately
1269 * that's how BSD mrouted happens to think. Maybe one day with a proper
1270 * MOSPF/PIM router set up we can clean this up.
1271 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001272
David S. Millerb7058842009-09-30 16:12:20 -07001273int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001275 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 struct vifctl vif;
1277 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001278 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001279 struct mr_table *mrt;
1280
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001281 if (sk->sk_type != SOCK_RAW ||
1282 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1283 return -EOPNOTSUPP;
1284
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001285 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001286 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001287 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001288
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001290 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001291 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return -EACCES;
1293 }
1294
Stephen Hemminger132adf52007-03-08 20:44:43 -08001295 switch (optname) {
1296 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001297 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001298 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Stephen Hemminger132adf52007-03-08 20:44:43 -08001300 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001301 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001303 return -EADDRINUSE;
1304 }
1305
1306 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1307 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001308 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001309 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001310 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1311 NETCONFA_IFINDEX_ALL,
1312 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001313 }
1314 rtnl_unlock();
1315 return ret;
1316 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001317 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 return -EACCES;
1319 return ip_ra_control(sk, 0, NULL);
1320 case MRT_ADD_VIF:
1321 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001322 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001323 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001324 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001325 return -EFAULT;
1326 if (vif.vifc_vifi >= MAXVIFS)
1327 return -ENFILE;
1328 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001329 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001330 ret = vif_add(net, mrt, &vif,
1331 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001332 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001333 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001334 }
1335 rtnl_unlock();
1336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /*
1339 * Manipulate the forwarding caches. These live
1340 * in a sort of kernel/user symbiosis.
1341 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001342 case MRT_ADD_MFC:
1343 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001344 parent = -1;
1345 case MRT_ADD_MFC_PROXY:
1346 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001347 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001348 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001349 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001350 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001351 if (parent == 0)
1352 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001353 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001354 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1355 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001356 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001357 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001358 sk == rtnl_dereference(mrt->mroute_sk),
1359 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001360 rtnl_unlock();
1361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /*
1363 * Control PIM assert.
1364 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001365 case MRT_ASSERT:
1366 {
1367 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001368 if (optlen != sizeof(v))
1369 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001370 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001371 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001372 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001373 return 0;
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001376 case MRT_PIM:
1377 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001378 int v;
1379
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001380 if (optlen != sizeof(v))
1381 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001382 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001383 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001384 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001385
Stephen Hemminger132adf52007-03-08 20:44:43 -08001386 rtnl_lock();
1387 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001388 if (v != mrt->mroute_do_pim) {
1389 mrt->mroute_do_pim = v;
1390 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001392 rtnl_unlock();
1393 return ret;
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001396#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1397 case MRT_TABLE:
1398 {
1399 u32 v;
1400
1401 if (optlen != sizeof(u32))
1402 return -EINVAL;
1403 if (get_user(v, (u32 __user *)optval))
1404 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001405
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001406 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1407 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1408 return -EINVAL;
1409
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001410 rtnl_lock();
1411 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001412 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1413 ret = -EBUSY;
1414 } else {
1415 if (!ipmr_new_table(net, v))
1416 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001417 else
1418 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001419 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001420 rtnl_unlock();
1421 return ret;
1422 }
1423#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001424 /*
1425 * Spurious command, or MRT_VERSION which you cannot
1426 * set.
1427 */
1428 default:
1429 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431}
1432
1433/*
1434 * Getsock opt support for the multicast routing system.
1435 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001436
Jianjun Kongc354e122008-11-03 00:28:02 -08001437int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 int olr;
1440 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001441 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001442 struct mr_table *mrt;
1443
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001444 if (sk->sk_type != SOCK_RAW ||
1445 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1446 return -EOPNOTSUPP;
1447
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001448 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001449 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001450 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Jianjun Kongc354e122008-11-03 00:28:02 -08001452 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001454 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001456 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return -ENOPROTOOPT;
1458
1459 if (get_user(olr, optlen))
1460 return -EFAULT;
1461
1462 olr = min_t(unsigned int, olr, sizeof(int));
1463 if (olr < 0)
1464 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001465
Jianjun Kongc354e122008-11-03 00:28:02 -08001466 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001468 if (optname == MRT_VERSION)
1469 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001471 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001472 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473#endif
1474 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001475 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001476 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return -EFAULT;
1478 return 0;
1479}
1480
1481/*
1482 * The IP multicast ioctl support routines.
1483 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1486{
1487 struct sioc_sg_req sr;
1488 struct sioc_vif_req vr;
1489 struct vif_device *vif;
1490 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001491 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001492 struct mr_table *mrt;
1493
1494 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001495 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001496 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001497
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498 switch (cmd) {
1499 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001500 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001501 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001502 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001503 return -EINVAL;
1504 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001505 vif = &mrt->vif_table[vr.vifi];
1506 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001507 vr.icount = vif->pkt_in;
1508 vr.ocount = vif->pkt_out;
1509 vr.ibytes = vif->bytes_in;
1510 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001512
Jianjun Kongc354e122008-11-03 00:28:02 -08001513 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001515 return 0;
1516 }
1517 read_unlock(&mrt_lock);
1518 return -EADDRNOTAVAIL;
1519 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001520 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001521 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Eric Dumazeta8c94862010-10-01 16:15:08 +00001523 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001524 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001525 if (c) {
1526 sr.pktcnt = c->mfc_un.res.pkt;
1527 sr.bytecnt = c->mfc_un.res.bytes;
1528 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001529 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001530
Jianjun Kongc354e122008-11-03 00:28:02 -08001531 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001532 return -EFAULT;
1533 return 0;
1534 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001535 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001536 return -EADDRNOTAVAIL;
1537 default:
1538 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540}
1541
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001542#ifdef CONFIG_COMPAT
1543struct compat_sioc_sg_req {
1544 struct in_addr src;
1545 struct in_addr grp;
1546 compat_ulong_t pktcnt;
1547 compat_ulong_t bytecnt;
1548 compat_ulong_t wrong_if;
1549};
1550
David S. Millerca6b8bb02011-02-03 17:24:28 -08001551struct compat_sioc_vif_req {
1552 vifi_t vifi; /* Which iface */
1553 compat_ulong_t icount;
1554 compat_ulong_t ocount;
1555 compat_ulong_t ibytes;
1556 compat_ulong_t obytes;
1557};
1558
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001559int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1560{
David S. Miller0033d5a2011-02-03 17:21:31 -08001561 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001562 struct compat_sioc_vif_req vr;
1563 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001564 struct mfc_cache *c;
1565 struct net *net = sock_net(sk);
1566 struct mr_table *mrt;
1567
1568 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001569 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001570 return -ENOENT;
1571
1572 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001573 case SIOCGETVIFCNT:
1574 if (copy_from_user(&vr, arg, sizeof(vr)))
1575 return -EFAULT;
1576 if (vr.vifi >= mrt->maxvif)
1577 return -EINVAL;
1578 read_lock(&mrt_lock);
1579 vif = &mrt->vif_table[vr.vifi];
1580 if (VIF_EXISTS(mrt, vr.vifi)) {
1581 vr.icount = vif->pkt_in;
1582 vr.ocount = vif->pkt_out;
1583 vr.ibytes = vif->bytes_in;
1584 vr.obytes = vif->bytes_out;
1585 read_unlock(&mrt_lock);
1586
1587 if (copy_to_user(arg, &vr, sizeof(vr)))
1588 return -EFAULT;
1589 return 0;
1590 }
1591 read_unlock(&mrt_lock);
1592 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001593 case SIOCGETSGCNT:
1594 if (copy_from_user(&sr, arg, sizeof(sr)))
1595 return -EFAULT;
1596
1597 rcu_read_lock();
1598 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1599 if (c) {
1600 sr.pktcnt = c->mfc_un.res.pkt;
1601 sr.bytecnt = c->mfc_un.res.bytes;
1602 sr.wrong_if = c->mfc_un.res.wrong_if;
1603 rcu_read_unlock();
1604
1605 if (copy_to_user(arg, &sr, sizeof(sr)))
1606 return -EFAULT;
1607 return 0;
1608 }
1609 rcu_read_unlock();
1610 return -EADDRNOTAVAIL;
1611 default:
1612 return -ENOIOCTLCMD;
1613 }
1614}
1615#endif
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1619{
Jiri Pirko351638e2013-05-28 01:30:21 +00001620 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001621 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001622 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct vif_device *v;
1624 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (event != NETDEV_UNREGISTER)
1627 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001628
1629 ipmr_for_each_table(mrt, net) {
1630 v = &mrt->vif_table[0];
1631 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1632 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001633 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 return NOTIFY_DONE;
1637}
1638
1639
Jianjun Kongc354e122008-11-03 00:28:02 -08001640static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 .notifier_call = ipmr_device_event,
1642};
1643
1644/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001645 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 * This avoids tunnel drivers and other mess and gives us the speed so
1647 * important for multicast video.
1648 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001649
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001650static void ip_encap(struct net *net, struct sk_buff *skb,
1651 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001653 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001654 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001655
1656 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001657 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001658 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001659 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001661 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001662 iph->tos = old_iph->tos;
1663 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 iph->frag_off = 0;
1665 iph->daddr = daddr;
1666 iph->saddr = saddr;
1667 iph->protocol = IPPROTO_IPIP;
1668 iph->ihl = 5;
1669 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001670 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 ip_send_check(iph);
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1674 nf_reset(skb);
1675}
1676
1677static inline int ipmr_forward_finish(struct sk_buff *skb)
1678{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001679 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Eric Dumazetadf30902009-06-02 05:19:30 +00001681 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Vincent Bernat2d8dbb02012-06-05 03:41:42 +00001682 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 if (unlikely(opt->optlen))
1685 ip_forward_options(skb);
1686
1687 return dst_output(skb);
1688}
1689
1690/*
1691 * Processing handlers for ipmr_forward
1692 */
1693
Patrick McHardy0c122952010-04-13 05:03:22 +00001694static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1695 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001697 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001698 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 struct net_device *dev;
1700 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001701 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 int encap = 0;
1703
Ian Morris51456b22015-04-03 09:17:26 +01001704 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 goto out_free;
1706
1707#ifdef CONFIG_IP_PIMSM
1708 if (vif->flags & VIFF_REGISTER) {
1709 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001710 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001711 vif->dev->stats.tx_bytes += skb->len;
1712 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001713 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001714 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716#endif
1717
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001718 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001719 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001720 vif->remote, vif->local,
1721 0, 0,
1722 IPPROTO_IPIP,
1723 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001724 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 goto out_free;
1726 encap = sizeof(struct iphdr);
1727 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001728 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001729 0, 0,
1730 IPPROTO_IPIP,
1731 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001732 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 goto out_free;
1734 }
1735
Changli Gaod8d1f302010-06-10 23:31:35 -07001736 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Changli Gaod8d1f302010-06-10 23:31:35 -07001738 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001740 * allow to send ICMP, so that packets will disappear
1741 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
1743
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001744 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 ip_rt_put(rt);
1746 goto out_free;
1747 }
1748
Changli Gaod8d1f302010-06-10 23:31:35 -07001749 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001752 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 goto out_free;
1754 }
1755
1756 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001757 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Eric Dumazetadf30902009-06-02 05:19:30 +00001759 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001760 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001761 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001764 * What do we do with netfilter? -- RR
1765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001767 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001769 vif->dev->stats.tx_packets++;
1770 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772
1773 IPCB(skb)->flags |= IPSKB_FORWARDED;
1774
1775 /*
1776 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1777 * not only before forwarding, but after forwarding on all output
1778 * interfaces. It is clear, if mrouter runs a multicasting
1779 * program, it should receive packets not depending to what interface
1780 * program is joined.
1781 * If we will not make it, the program will have to join on all
1782 * interfaces. On the other hand, multihoming host (or router, but
1783 * not mrouter) cannot join to more than one interface - it will
1784 * result in receiving multiple packets.
1785 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001786 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 ipmr_forward_finish);
1788 return;
1789
1790out_free:
1791 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
Patrick McHardy0c122952010-04-13 05:03:22 +00001794static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001797
1798 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1799 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 break;
1801 }
1802 return ct;
1803}
1804
1805/* "local" means that we should preserve one skb (for local delivery) */
1806
Rami Rosenc4854ec2013-07-20 15:09:28 +03001807static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1808 struct sk_buff *skb, struct mfc_cache *cache,
1809 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
1811 int psend = -1;
1812 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001813 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 vif = cache->mfc_parent;
1816 cache->mfc_un.res.pkt++;
1817 cache->mfc_un.res.bytes += skb->len;
1818
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001819 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001820 struct mfc_cache *cache_proxy;
1821
1822 /* For an (*,G) entry, we only check that the incomming
1823 * interface is part of the static tree.
1824 */
1825 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1826 if (cache_proxy &&
1827 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1828 goto forward;
1829 }
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /*
1832 * Wrong interface: drop packet and (maybe) send PIM assert.
1833 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001834 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001835 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001837 * Very complicated situation...
1838 *
1839 * The best workaround until routing daemons will be
1840 * fixed is not to redistribute packet, if it was
1841 * send through wrong interface. It means, that
1842 * multicast applications WILL NOT work for
1843 * (S,G), which have default multicast route pointing
1844 * to wrong oif. In any case, it is not a good
1845 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 */
1847 goto dont_forward;
1848 }
1849
1850 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Patrick McHardy0c122952010-04-13 05:03:22 +00001852 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001854 * so that we cannot check that packet arrived on an oif.
1855 * It is bad, but otherwise we would need to move pretty
1856 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001858 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001859 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001860 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1862 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001863 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865 goto dont_forward;
1866 }
1867
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001868forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001869 mrt->vif_table[vif].pkt_in++;
1870 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 /*
1873 * Forward the frame
1874 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001875 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1876 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001877 if (true_vifi >= 0 &&
1878 true_vifi != cache->mfc_parent &&
1879 ip_hdr(skb)->ttl >
1880 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1881 /* It's an (*,*) entry and the packet is not coming from
1882 * the upstream: forward the packet to the upstream
1883 * only.
1884 */
1885 psend = cache->mfc_parent;
1886 goto last_forward;
1887 }
1888 goto dont_forward;
1889 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001890 for (ct = cache->mfc_un.res.maxvif - 1;
1891 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001892 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001893 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1894 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001895 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (psend != -1) {
1897 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001900 ipmr_queue_xmit(net, mrt, skb2, cache,
1901 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001903 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001906last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (psend != -1) {
1908 if (local) {
1909 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001912 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001914 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001915 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917 }
1918
1919dont_forward:
1920 if (!local)
1921 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
David S. Miller417da662011-05-03 19:42:43 -07001924static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001925{
David S. Miller417da662011-05-03 19:42:43 -07001926 struct rtable *rt = skb_rtable(skb);
1927 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001928 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001929 .daddr = iph->daddr,
1930 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001931 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001932 .flowi4_oif = (rt_is_output_route(rt) ?
1933 skb->dev->ifindex : 0),
1934 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001935 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001936 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001937 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001938 };
1939 struct mr_table *mrt;
1940 int err;
1941
David S. Millerda919812011-03-12 02:04:50 -05001942 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001943 if (err)
1944 return ERR_PTR(err);
1945 return mrt;
1946}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948/*
1949 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001950 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 */
1952
1953int ip_mr_input(struct sk_buff *skb)
1954{
1955 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001956 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001957 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001958 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001961 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001963 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 goto dont_forward;
1965
David S. Miller417da662011-05-03 19:42:43 -07001966 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001967 if (IS_ERR(mrt)) {
1968 kfree_skb(skb);
1969 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001972 if (IPCB(skb)->opt.router_alert) {
1973 if (ip_call_ra_chain(skb))
1974 return 0;
1975 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1976 /* IGMPv1 (and broken IGMPv2 implementations sort of
1977 * Cisco IOS <= 11.2(8)) do not put router alert
1978 * option to IGMP packets destined to routable
1979 * groups. It is very bad, because it means
1980 * that we can forward NO IGMP messages.
1981 */
1982 struct sock *mroute_sk;
1983
1984 mroute_sk = rcu_dereference(mrt->mroute_sk);
1985 if (mroute_sk) {
1986 nf_reset(skb);
1987 raw_rcv(mroute_sk, skb);
1988 return 0;
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991 }
1992
Eric Dumazeta8c94862010-10-01 16:15:08 +00001993 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001994 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001995 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001996 int vif = ipmr_find_vif(mrt, skb->dev);
1997
1998 if (vif >= 0)
1999 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2000 vif);
2001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /*
2004 * No usable cache entry
2005 */
Ian Morris51456b22015-04-03 09:17:26 +01002006 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int vif;
2008
2009 if (local) {
2010 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2011 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002012 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 skb = skb2;
2015 }
2016
Eric Dumazeta8c94862010-10-01 16:15:08 +00002017 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002018 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002020 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 read_unlock(&mrt_lock);
2022
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002023 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025 read_unlock(&mrt_lock);
2026 kfree_skb(skb);
2027 return -ENODEV;
2028 }
2029
Eric Dumazeta8c94862010-10-01 16:15:08 +00002030 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002031 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 read_unlock(&mrt_lock);
2033
2034 if (local)
2035 return ip_local_deliver(skb);
2036
2037 return 0;
2038
2039dont_forward:
2040 if (local)
2041 return ip_local_deliver(skb);
2042 kfree_skb(skb);
2043 return 0;
2044}
2045
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002046#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00002047/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002048static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2049 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002051 struct net_device *reg_dev = NULL;
2052 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002054 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002056 * Check that:
2057 * a. packet is really sent to a multicast group
2058 * b. packet is not a NULL-REGISTER
2059 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002061 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002063 ntohs(encap->tot_len) + pimlen > skb->len)
2064 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002067 if (mrt->mroute_reg_vif_num >= 0)
2068 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 read_unlock(&mrt_lock);
2070
Ian Morris51456b22015-04-03 09:17:26 +01002071 if (!reg_dev)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002072 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002074 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00002075 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03002076 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00002078 skb->ip_summed = CHECKSUM_NONE;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002079
Nicolas Dichtelea231922013-09-02 15:34:58 +02002080 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002083
Eric Dumazet55747a02010-10-01 16:14:55 +00002084 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002085}
2086#endif
2087
2088#ifdef CONFIG_IP_PIMSM_V1
2089/*
2090 * Handle IGMP messages of PIMv1
2091 */
2092
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002093int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002094{
2095 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002096 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002097 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002098
2099 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2100 goto drop;
2101
2102 pim = igmp_hdr(skb);
2103
David S. Miller417da662011-05-03 19:42:43 -07002104 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002105 if (IS_ERR(mrt))
2106 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002107 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002108 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2109 goto drop;
2110
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002111 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002112drop:
2113 kfree_skb(skb);
2114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return 0;
2116}
2117#endif
2118
2119#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002120static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002123 struct net *net = dev_net(skb->dev);
2124 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002126 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 goto drop;
2128
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002129 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002130 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2131 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002132 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002133 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 goto drop;
2135
David S. Miller417da662011-05-03 19:42:43 -07002136 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002137 if (IS_ERR(mrt))
2138 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002139 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002140drop:
2141 kfree_skb(skb);
2142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return 0;
2144}
2145#endif
2146
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002147static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2148 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 int ct;
2151 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002152 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002153 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Nicolas Dichtel74381892010-03-25 23:45:35 +00002155 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002156 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002157 return -ENOENT;
2158
Thomas Graf92a395e2012-06-26 23:36:13 +00002159 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2160 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2161 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Thomas Graf92a395e2012-06-26 23:36:13 +00002163 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2164 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002167 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002168 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2169 nla_nest_cancel(skb, mp_attr);
2170 return -EMSGSIZE;
2171 }
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 nhp->rtnh_flags = 0;
2174 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002175 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 nhp->rtnh_len = sizeof(*nhp);
2177 }
2178 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002179
2180 nla_nest_end(skb, mp_attr);
2181
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002182 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2183 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2184 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2185 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2186 return -EMSGSIZE;
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 rtm->rtm_type = RTN_MULTICAST;
2189 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
David S. Miller9a1b9492011-05-04 12:18:54 -07002192int ipmr_get_route(struct net *net, struct sk_buff *skb,
2193 __be32 saddr, __be32 daddr,
2194 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002197 struct mr_table *mrt;
2198 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002200 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002201 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002202 return -ENOENT;
2203
Eric Dumazeta8c94862010-10-01 16:15:08 +00002204 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002205 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002206 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002207 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002209 if (vif >= 0)
2210 cache = ipmr_cache_find_any(mrt, daddr, vif);
2211 }
Ian Morris51456b22015-04-03 09:17:26 +01002212 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002213 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002214 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002216 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002219 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return -EAGAIN;
2221 }
2222
2223 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002224 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002225 if (dev)
2226 vif = ipmr_find_vif(mrt, dev);
2227 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002229 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return -ENODEV;
2231 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002232 skb2 = skb_clone(skb, GFP_ATOMIC);
2233 if (!skb2) {
2234 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002235 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002236 return -ENOMEM;
2237 }
2238
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002239 skb_push(skb2, sizeof(struct iphdr));
2240 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002241 iph = ip_hdr(skb2);
2242 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002243 iph->saddr = saddr;
2244 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002245 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002246 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002248 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return err;
2250 }
2251
Eric Dumazeta8c94862010-10-01 16:15:08 +00002252 read_lock(&mrt_lock);
2253 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002255 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002257 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return err;
2259}
2260
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002261static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002262 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2263 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002264{
2265 struct nlmsghdr *nlh;
2266 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002267 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002268
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002269 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002270 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002271 return -EMSGSIZE;
2272
2273 rtm = nlmsg_data(nlh);
2274 rtm->rtm_family = RTNL_FAMILY_IPMR;
2275 rtm->rtm_dst_len = 32;
2276 rtm->rtm_src_len = 32;
2277 rtm->rtm_tos = 0;
2278 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002279 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2280 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002281 rtm->rtm_type = RTN_MULTICAST;
2282 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002283 if (c->mfc_flags & MFC_STATIC)
2284 rtm->rtm_protocol = RTPROT_STATIC;
2285 else
2286 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002287 rtm->rtm_flags = 0;
2288
Jiri Benc930345e2015-03-29 16:59:25 +02002289 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2290 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002291 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002292 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2293 /* do not break the dump if cache is unresolved */
2294 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002295 goto nla_put_failure;
2296
Johannes Berg053c0952015-01-16 22:09:00 +01002297 nlmsg_end(skb, nlh);
2298 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002299
2300nla_put_failure:
2301 nlmsg_cancel(skb, nlh);
2302 return -EMSGSIZE;
2303}
2304
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002305static size_t mroute_msgsize(bool unresolved, int maxvif)
2306{
2307 size_t len =
2308 NLMSG_ALIGN(sizeof(struct rtmsg))
2309 + nla_total_size(4) /* RTA_TABLE */
2310 + nla_total_size(4) /* RTA_SRC */
2311 + nla_total_size(4) /* RTA_DST */
2312 ;
2313
2314 if (!unresolved)
2315 len = len
2316 + nla_total_size(4) /* RTA_IIF */
2317 + nla_total_size(0) /* RTA_MULTIPATH */
2318 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2319 /* RTA_MFC_STATS */
2320 + nla_total_size(sizeof(struct rta_mfc_stats))
2321 ;
2322
2323 return len;
2324}
2325
2326static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2327 int cmd)
2328{
2329 struct net *net = read_pnet(&mrt->net);
2330 struct sk_buff *skb;
2331 int err = -ENOBUFS;
2332
2333 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2334 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002335 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002336 goto errout;
2337
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002338 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002339 if (err < 0)
2340 goto errout;
2341
2342 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2343 return;
2344
2345errout:
2346 kfree_skb(skb);
2347 if (err < 0)
2348 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2349}
2350
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002351static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2352{
2353 struct net *net = sock_net(skb->sk);
2354 struct mr_table *mrt;
2355 struct mfc_cache *mfc;
2356 unsigned int t = 0, s_t;
2357 unsigned int h = 0, s_h;
2358 unsigned int e = 0, s_e;
2359
2360 s_t = cb->args[0];
2361 s_h = cb->args[1];
2362 s_e = cb->args[2];
2363
Eric Dumazeta8c94862010-10-01 16:15:08 +00002364 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002365 ipmr_for_each_table(mrt, net) {
2366 if (t < s_t)
2367 goto next_table;
2368 if (t > s_t)
2369 s_h = 0;
2370 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002371 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002372 if (e < s_e)
2373 goto next_entry;
2374 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002375 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002376 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002377 mfc, RTM_NEWROUTE,
2378 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002379 goto done;
2380next_entry:
2381 e++;
2382 }
2383 e = s_e = 0;
2384 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002385 spin_lock_bh(&mfc_unres_lock);
2386 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2387 if (e < s_e)
2388 goto next_entry2;
2389 if (ipmr_fill_mroute(mrt, skb,
2390 NETLINK_CB(cb->skb).portid,
2391 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002392 mfc, RTM_NEWROUTE,
2393 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002394 spin_unlock_bh(&mfc_unres_lock);
2395 goto done;
2396 }
2397next_entry2:
2398 e++;
2399 }
2400 spin_unlock_bh(&mfc_unres_lock);
2401 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002402 s_h = 0;
2403next_table:
2404 t++;
2405 }
2406done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002407 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002408
2409 cb->args[2] = e;
2410 cb->args[1] = h;
2411 cb->args[0] = t;
2412
2413 return skb->len;
2414}
2415
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002416#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002418 * The /proc interfaces to multicast routing :
2419 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 */
2421struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002422 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002423 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int ct;
2425};
2426
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002427static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2428 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 loff_t pos)
2430{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002431 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002432
2433 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2434 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002436 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002437 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439 return NULL;
2440}
2441
2442static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002443 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002445 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002446 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002447 struct mr_table *mrt;
2448
2449 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002450 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002451 return ERR_PTR(-ENOENT);
2452
2453 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002456 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 : SEQ_START_TOKEN;
2458}
2459
2460static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2461{
2462 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002463 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002464 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 ++*pos;
2467 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002468 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002469
Patrick McHardy0c122952010-04-13 05:03:22 +00002470 while (++iter->ct < mrt->maxvif) {
2471 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002473 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
2475 return NULL;
2476}
2477
2478static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002479 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
2481 read_unlock(&mrt_lock);
2482}
2483
2484static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2485{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002486 struct ipmr_vif_iter *iter = seq->private;
2487 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002490 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2492 } else {
2493 const struct vif_device *vif = v;
2494 const char *name = vif->dev ? vif->dev->name : "none";
2495
2496 seq_printf(seq,
2497 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002498 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002499 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 vif->bytes_out, vif->pkt_out,
2501 vif->flags, vif->local, vif->remote);
2502 }
2503 return 0;
2504}
2505
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002506static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 .start = ipmr_vif_seq_start,
2508 .next = ipmr_vif_seq_next,
2509 .stop = ipmr_vif_seq_stop,
2510 .show = ipmr_vif_seq_show,
2511};
2512
2513static int ipmr_vif_open(struct inode *inode, struct file *file)
2514{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002515 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2516 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
Arjan van de Ven9a321442007-02-12 00:55:35 -08002519static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 .owner = THIS_MODULE,
2521 .open = ipmr_vif_open,
2522 .read = seq_read,
2523 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002524 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525};
2526
2527struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002528 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002529 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002530 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 int ct;
2532};
2533
2534
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002535static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2536 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002538 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 struct mfc_cache *mfc;
2540
Eric Dumazeta8c94862010-10-01 16:15:08 +00002541 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002542 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002543 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002544 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002545 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002547 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002548 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002551 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002552 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002553 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return mfc;
2555 spin_unlock_bh(&mfc_unres_lock);
2556
2557 it->cache = NULL;
2558 return NULL;
2559}
2560
2561
2562static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2563{
2564 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002565 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002566 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002567
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002568 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002569 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002570 return ERR_PTR(-ENOENT);
2571
2572 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 it->cache = NULL;
2574 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002575 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 : SEQ_START_TOKEN;
2577}
2578
2579static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2580{
2581 struct mfc_cache *mfc = v;
2582 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002583 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002584 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 ++*pos;
2587
2588 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002589 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Patrick McHardy862465f2010-04-13 05:03:21 +00002591 if (mfc->list.next != it->cache)
2592 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002593
Patrick McHardy0c122952010-04-13 05:03:22 +00002594 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 goto end_of_list;
2596
Patrick McHardy0c122952010-04-13 05:03:22 +00002597 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002600 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002601 if (list_empty(it->cache))
2602 continue;
2603 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
2605
2606 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002607 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002608 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002612 if (!list_empty(it->cache))
2613 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002615end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 spin_unlock_bh(&mfc_unres_lock);
2617 it->cache = NULL;
2618
2619 return NULL;
2620}
2621
2622static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2623{
2624 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002625 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Patrick McHardy0c122952010-04-13 05:03:22 +00002627 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002629 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002630 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
2633static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2634{
2635 int n;
2636
2637 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002638 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2640 } else {
2641 const struct mfc_cache *mfc = v;
2642 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002643 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002644
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002645 seq_printf(seq, "%08X %08X %-3hd",
2646 (__force u32) mfc->mfc_mcastgrp,
2647 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002648 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Patrick McHardy0c122952010-04-13 05:03:22 +00002650 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002651 seq_printf(seq, " %8lu %8lu %8lu",
2652 mfc->mfc_un.res.pkt,
2653 mfc->mfc_un.res.bytes,
2654 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002655 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002656 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002657 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002658 mfc->mfc_un.res.ttls[n] < 255)
2659 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002660 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 n, mfc->mfc_un.res.ttls[n]);
2662 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002663 } else {
2664 /* unresolved mfc_caches don't contain
2665 * pkt, bytes and wrong_if values
2666 */
2667 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
2669 seq_putc(seq, '\n');
2670 }
2671 return 0;
2672}
2673
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002674static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 .start = ipmr_mfc_seq_start,
2676 .next = ipmr_mfc_seq_next,
2677 .stop = ipmr_mfc_seq_stop,
2678 .show = ipmr_mfc_seq_show,
2679};
2680
2681static int ipmr_mfc_open(struct inode *inode, struct file *file)
2682{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002683 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2684 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
Arjan van de Ven9a321442007-02-12 00:55:35 -08002687static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 .owner = THIS_MODULE,
2689 .open = ipmr_mfc_open,
2690 .read = seq_read,
2691 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002692 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002694#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002697static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002699 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700};
2701#endif
2702
2703
2704/*
2705 * Setup for IP multicast routing
2706 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002707static int __net_init ipmr_net_init(struct net *net)
2708{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002709 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002710
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002711 err = ipmr_rules_init(net);
2712 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002713 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002714
2715#ifdef CONFIG_PROC_FS
2716 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002717 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002718 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002719 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002720 goto proc_cache_fail;
2721#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002722 return 0;
2723
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002724#ifdef CONFIG_PROC_FS
2725proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002726 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002727proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002728 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002729#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002730fail:
2731 return err;
2732}
2733
2734static void __net_exit ipmr_net_exit(struct net *net)
2735{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002736#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002737 remove_proc_entry("ip_mr_cache", net->proc_net);
2738 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002739#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002740 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002741}
2742
2743static struct pernet_operations ipmr_net_ops = {
2744 .init = ipmr_net_init,
2745 .exit = ipmr_net_exit,
2746};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002747
Wang Chen03d2f892008-07-03 12:13:36 +08002748int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Wang Chen03d2f892008-07-03 12:13:36 +08002750 int err;
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2753 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002754 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002755 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002756 if (!mrt_cachep)
2757 return -ENOMEM;
2758
Benjamin Therycf958ae32009-01-22 04:56:16 +00002759 err = register_pernet_subsys(&ipmr_net_ops);
2760 if (err)
2761 goto reg_pernet_fail;
2762
Wang Chen03d2f892008-07-03 12:13:36 +08002763 err = register_netdevice_notifier(&ip_mr_notifier);
2764 if (err)
2765 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002766#ifdef CONFIG_IP_PIMSM_V2
2767 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002768 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002769 err = -EAGAIN;
2770 goto add_proto_fail;
2771 }
2772#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002773 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2774 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002775 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002776
Tom Goff403dbb92009-06-14 03:16:13 -07002777#ifdef CONFIG_IP_PIMSM_V2
2778add_proto_fail:
2779 unregister_netdevice_notifier(&ip_mr_notifier);
2780#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002781reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002782 unregister_pernet_subsys(&ipmr_net_ops);
2783reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002784 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002785 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}