blob: b4a545d24adbf5c88b490c2bc7cccea3f901eb29 [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);
192 if (mrt == NULL)
193 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);
256 if (mrt == NULL) {
257 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:
269 kfree(mrt);
270err1:
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);
319 if (mrt != NULL)
320 return mrt;
321
322 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
323 if (mrt == NULL)
324 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);
Herbert Xu71e27da2007-06-04 23:36:06 -0700425 if (in_dev == NULL)
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
Stephen Hemminger007c3832008-11-20 20:28:35 -0800476static const struct net_device_ops reg_vif_netdev_ops = {
477 .ndo_start_xmit = reg_vif_xmit,
478};
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480static void reg_vif_setup(struct net_device *dev)
481{
482 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800483 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530485 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700487 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000490static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct net_device *dev;
493 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000494 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000496 if (mrt->id == RT_TABLE_DEFAULT)
497 sprintf(name, "pimreg");
498 else
499 sprintf(name, "pimreg%u", mrt->id);
500
Tom Gundersenc835a672014-07-14 16:37:24 +0200501 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (dev == NULL)
504 return NULL;
505
Tom Goff403dbb92009-06-14 03:16:13 -0700506 dev_net_set(dev, net);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (register_netdevice(dev)) {
509 free_netdev(dev);
510 return NULL;
511 }
512 dev->iflink = 0;
513
Herbert Xu71e27da2007-06-04 23:36:06 -0700514 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000515 in_dev = __in_dev_get_rcu(dev);
516 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700517 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Herbert Xu71e27da2007-06-04 23:36:06 -0700521 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100522 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700523 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
524 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (dev_open(dev))
527 goto failure;
528
Wang Chen7dc00c82008-07-14 20:56:34 -0700529 dev_hold(dev);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return dev;
532
533failure:
534 /* allow the register to be completed before unregistering. */
535 rtnl_unlock();
536 rtnl_lock();
537
538 unregister_netdevice(dev);
539 return NULL;
540}
541#endif
542
Ben Hutchings2c530402012-07-10 10:55:09 +0000543/**
544 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700545 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900547
Patrick McHardy0c122952010-04-13 05:03:22 +0000548static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000549 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct vif_device *v;
552 struct net_device *dev;
553 struct in_device *in_dev;
554
Patrick McHardy0c122952010-04-13 05:03:22 +0000555 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -EADDRNOTAVAIL;
557
Patrick McHardy0c122952010-04-13 05:03:22 +0000558 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 write_lock_bh(&mrt_lock);
561 dev = v->dev;
562 v->dev = NULL;
563
564 if (!dev) {
565 write_unlock_bh(&mrt_lock);
566 return -EADDRNOTAVAIL;
567 }
568
569#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000570 if (vifi == mrt->mroute_reg_vif_num)
571 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#endif
573
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000574 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000576
577 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000578 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000581 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
584 write_unlock_bh(&mrt_lock);
585
586 dev_set_allmulti(dev, -1);
587
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000588 in_dev = __in_dev_get_rtnl(dev);
589 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700590 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000591 inet_netconf_notify_devconf(dev_net(dev),
592 NETCONFA_MC_FORWARDING,
593 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 ip_rt_multicast_event(in_dev);
595 }
596
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000597 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000598 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 dev_put(dev);
601 return 0;
602}
603
Eric Dumazeta8c94862010-10-01 16:15:08 +0000604static void ipmr_cache_free_rcu(struct rcu_head *head)
605{
606 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
607
608 kmem_cache_free(mrt_cachep, c);
609}
610
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000611static inline void ipmr_cache_free(struct mfc_cache *c)
612{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000613 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000617 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
619
Patrick McHardy0c122952010-04-13 05:03:22 +0000620static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200622 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700624 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Patrick McHardy0c122952010-04-13 05:03:22 +0000626 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Jianjun Kongc354e122008-11-03 00:28:02 -0800628 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700629 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
631 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000632 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000634 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700635 e->error = -ETIMEDOUT;
636 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700637
Eric W. Biederman15e47302012-09-07 20:12:54 +0000638 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000639 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000644 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647
Patrick McHardye258beb2010-04-13 05:03:19 +0000648/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Patrick McHardye258beb2010-04-13 05:03:19 +0000650static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Patrick McHardy0c122952010-04-13 05:03:22 +0000652 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 unsigned long now;
654 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000655 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000658 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return;
660 }
661
Patrick McHardy0c122952010-04-13 05:03:22 +0000662 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto out;
664
665 now = jiffies;
666 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Patrick McHardy0c122952010-04-13 05:03:22 +0000668 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (time_after(c->mfc_un.unres.expires, now)) {
670 unsigned long interval = c->mfc_un.unres.expires - now;
671 if (interval < expires)
672 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 continue;
674 }
675
Patrick McHardy862465f2010-04-13 05:03:21 +0000676 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000677 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000678 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
Patrick McHardy0c122952010-04-13 05:03:22 +0000681 if (!list_empty(&mrt->mfc_unres_queue))
682 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684out:
685 spin_unlock(&mfc_unres_lock);
686}
687
688/* Fill oifs list. It is called under write locked mrt_lock. */
689
Patrick McHardy0c122952010-04-13 05:03:22 +0000690static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000691 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 int vifi;
694
695 cache->mfc_un.res.minvif = MAXVIFS;
696 cache->mfc_un.res.maxvif = 0;
697 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
698
Patrick McHardy0c122952010-04-13 05:03:22 +0000699 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
700 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000701 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
703 if (cache->mfc_un.res.minvif > vifi)
704 cache->mfc_un.res.minvif = vifi;
705 if (cache->mfc_un.res.maxvif <= vifi)
706 cache->mfc_un.res.maxvif = vifi + 1;
707 }
708 }
709}
710
Patrick McHardy0c122952010-04-13 05:03:22 +0000711static int vif_add(struct net *net, struct mr_table *mrt,
712 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000715 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct net_device *dev;
717 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700718 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000721 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EADDRINUSE;
723
724 switch (vifc->vifc_flags) {
725#ifdef CONFIG_IP_PIMSM
726 case VIFF_REGISTER:
727 /*
728 * Special Purpose VIF in PIM
729 * All the packets will be sent to the daemon
730 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000731 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000733 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!dev)
735 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700736 err = dev_set_allmulti(dev, 1);
737 if (err) {
738 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700739 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700740 return err;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900744 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000745 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!dev)
747 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700748 err = dev_set_allmulti(dev, 1);
749 if (err) {
750 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700751 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700752 return err;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000755
756 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000758 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
759 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000760 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000761 dev_put(dev);
762 return -EADDRNOTAVAIL;
763 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000764 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000765 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (!dev)
768 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700769 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700770 if (err) {
771 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700772 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 break;
775 default:
776 return -EINVAL;
777 }
778
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000779 in_dev = __in_dev_get_rtnl(dev);
780 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000781 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000783 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700784 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000785 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
786 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 ip_rt_multicast_event(in_dev);
788
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000789 /* Fill in the VIF structures */
790
Jianjun Kongc354e122008-11-03 00:28:02 -0800791 v->rate_limit = vifc->vifc_rate_limit;
792 v->local = vifc->vifc_lcl_addr.s_addr;
793 v->remote = vifc->vifc_rmt_addr.s_addr;
794 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!mrtsock)
796 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800797 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 v->bytes_in = 0;
799 v->bytes_out = 0;
800 v->pkt_in = 0;
801 v->pkt_out = 0;
802 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000803 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 v->link = dev->iflink;
805
806 /* And finish update writing critical data */
807 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800808 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000810 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000811 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000813 if (vifi+1 > mrt->maxvif)
814 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 write_unlock_bh(&mrt_lock);
816 return 0;
817}
818
Eric Dumazeta8c94862010-10-01 16:15:08 +0000819/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000820static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000821 __be32 origin,
822 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Jianjun Kongc354e122008-11-03 00:28:02 -0800824 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct mfc_cache *c;
826
Eric Dumazeta8c94862010-10-01 16:15:08 +0000827 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000828 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
829 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000831 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000834/* Look for a (*,*,oif) entry */
835static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
836 int vifi)
837{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100838 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000839 struct mfc_cache *c;
840
841 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100842 if (c->mfc_origin == htonl(INADDR_ANY) &&
843 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000844 c->mfc_un.res.ttls[vifi] < 255)
845 return c;
846
847 return NULL;
848}
849
850/* Look for a (*,G) entry */
851static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
852 __be32 mcastgrp, int vifi)
853{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100854 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000855 struct mfc_cache *c, *proxy;
856
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100857 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000858 goto skip;
859
860 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100861 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000862 c->mfc_mcastgrp == mcastgrp) {
863 if (c->mfc_un.res.ttls[vifi] < 255)
864 return c;
865
866 /* It's ok if the vifi is part of the static tree */
867 proxy = ipmr_cache_find_any_parent(mrt,
868 c->mfc_parent);
869 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
870 return c;
871 }
872
873skip:
874 return ipmr_cache_find_any_parent(mrt, vifi);
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * Allocate a multicast cache entry
879 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000880static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Jianjun Kongc354e122008-11-03 00:28:02 -0800882 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000883
884 if (c)
885 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return c;
887}
888
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000889static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Jianjun Kongc354e122008-11-03 00:28:02 -0800891 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000892
893 if (c) {
894 skb_queue_head_init(&c->mfc_un.unres.unresolved);
895 c->mfc_un.unres.expires = jiffies + 10*HZ;
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return c;
898}
899
900/*
901 * A cache entry has gone into a resolved state from queued
902 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900903
Patrick McHardy0c122952010-04-13 05:03:22 +0000904static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
905 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700908 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000910 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Jianjun Kongc354e122008-11-03 00:28:02 -0800912 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700913 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
915
Hong zhi guo573ce262013-03-27 06:47:04 +0000916 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000917 nlh->nlmsg_len = skb_tail_pointer(skb) -
918 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 } else {
920 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000921 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000923 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700924 e->error = -EMSGSIZE;
925 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Thomas Graf2942e902006-08-15 00:30:25 -0700927
Eric W. Biederman15e47302012-09-07 20:12:54 +0000928 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000929 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000930 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933}
934
935/*
936 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
937 * expects the following bizarre scheme.
938 *
939 * Called under mrt_lock.
940 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900941
Patrick McHardy0c122952010-04-13 05:03:22 +0000942static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000943 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300946 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct igmphdr *igmp;
948 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000949 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int ret;
951
952#ifdef CONFIG_IP_PIMSM
953 if (assert == IGMPMSG_WHOLEPKT)
954 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
955 else
956#endif
957 skb = alloc_skb(128, GFP_ATOMIC);
958
Stephen Hemminger132adf52007-03-08 20:44:43 -0800959 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -ENOBUFS;
961
962#ifdef CONFIG_IP_PIMSM
963 if (assert == IGMPMSG_WHOLEPKT) {
964 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000965 * Duplicate old header, fix ihl, length etc.
966 * And all this only to mangle msg->im_msgtype and
967 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300969 skb_push(skb, sizeof(struct iphdr));
970 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300971 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300972 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700973 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 msg->im_msgtype = IGMPMSG_WHOLEPKT;
975 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000976 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700977 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
978 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
979 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900980 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900982 {
983
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000984 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Cong Wang30f3a402013-06-05 20:14:10 +0800986 skb_set_network_header(skb, skb->len);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300987 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300988 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000989 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700990 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000992 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000994 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000996 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 igmp->type =
998 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000999 igmp->code = 0;
1000 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001001 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Eric Dumazet4c968702010-10-01 16:15:01 +00001004 rcu_read_lock();
1005 mroute_sk = rcu_dereference(mrt->mroute_sk);
1006 if (mroute_sk == NULL) {
1007 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 kfree_skb(skb);
1009 return -EINVAL;
1010 }
1011
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001012 /* Deliver to mrouted */
1013
Eric Dumazet4c968702010-10-01 16:15:01 +00001014 ret = sock_queue_rcv_skb(mroute_sk, skb);
1015 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001016 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001017 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 kfree_skb(skb);
1019 }
1020
1021 return ret;
1022}
1023
1024/*
1025 * Queue a packet for resolution. It gets locked cache entry!
1026 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001029ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Patrick McHardy862465f2010-04-13 05:03:21 +00001031 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int err;
1033 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001034 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001037 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001038 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001039 c->mfc_origin == iph->saddr) {
1040 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Patrick McHardy862465f2010-04-13 05:03:21 +00001045 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001046 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Patrick McHardy0c122952010-04-13 05:03:22 +00001048 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001049 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 spin_unlock_bh(&mfc_unres_lock);
1051
1052 kfree_skb(skb);
1053 return -ENOBUFS;
1054 }
1055
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001056 /* Fill in the new cache entry */
1057
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001058 c->mfc_parent = -1;
1059 c->mfc_origin = iph->saddr;
1060 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001062 /* Reflect first query at mrouted. */
1063
Patrick McHardy0c122952010-04-13 05:03:22 +00001064 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001065 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001066 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 out - Brad Parker
1068 */
1069 spin_unlock_bh(&mfc_unres_lock);
1070
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001071 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 kfree_skb(skb);
1073 return err;
1074 }
1075
Patrick McHardy0c122952010-04-13 05:03:22 +00001076 atomic_inc(&mrt->cache_resolve_queue_len);
1077 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001078 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
David S. Miller278554b2010-05-12 00:05:35 -07001080 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1081 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001084 /* See if we can append the packet */
1085
1086 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 kfree_skb(skb);
1088 err = -ENOBUFS;
1089 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001090 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 err = 0;
1092 }
1093
1094 spin_unlock_bh(&mfc_unres_lock);
1095 return err;
1096}
1097
1098/*
1099 * MFC cache manipulation by user space mroute daemon
1100 */
1101
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001102static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001105 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Jianjun Kongc354e122008-11-03 00:28:02 -08001107 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Patrick McHardy0c122952010-04-13 05:03:22 +00001109 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001111 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1112 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001113 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001114 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001115 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return 0;
1117 }
1118 }
1119 return -ENOENT;
1120}
1121
Patrick McHardy0c122952010-04-13 05:03:22 +00001122static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001123 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Patrick McHardy862465f2010-04-13 05:03:21 +00001125 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001127 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Patrick McHardya50436f22010-03-17 06:04:14 +00001129 if (mfc->mfcc_parent >= MAXVIFS)
1130 return -ENFILE;
1131
Jianjun Kongc354e122008-11-03 00:28:02 -08001132 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Patrick McHardy0c122952010-04-13 05:03:22 +00001134 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001136 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1137 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001138 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142
Patrick McHardy862465f2010-04-13 05:03:21 +00001143 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 write_lock_bh(&mrt_lock);
1145 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001146 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!mrtsock)
1148 c->mfc_flags |= MFC_STATIC;
1149 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001150 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 0;
1152 }
1153
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001154 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001155 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return -EINVAL;
1157
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001158 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001159 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -ENOMEM;
1161
Jianjun Kongc354e122008-11-03 00:28:02 -08001162 c->mfc_origin = mfc->mfcc_origin.s_addr;
1163 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1164 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001165 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!mrtsock)
1167 c->mfc_flags |= MFC_STATIC;
1168
Eric Dumazeta8c94862010-10-01 16:15:08 +00001169 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /*
1172 * Check to see if we resolved a queued list. If so we
1173 * need to send on the frames and tidy up.
1174 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001175 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001177 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001178 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001180 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001181 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001182 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
1184 }
1185 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001186 if (list_empty(&mrt->mfc_unres_queue))
1187 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 spin_unlock_bh(&mfc_unres_lock);
1189
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001190 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001191 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001192 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001194 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return 0;
1196}
1197
1198/*
1199 * Close the multicast socket, and clear the vif tables etc
1200 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001201
Patrick McHardy0c122952010-04-13 05:03:22 +00001202static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001205 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001206 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001207
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001208 /* Shut down all active vif entries */
1209
Patrick McHardy0c122952010-04-13 05:03:22 +00001210 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001211 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001212 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001214 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001216 /* Wipe the cache */
1217
Patrick McHardy862465f2010-04-13 05:03:21 +00001218 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001219 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001220 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001222 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001223 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001224 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 }
1227
Patrick McHardy0c122952010-04-13 05:03:22 +00001228 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001230 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001231 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001232 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001233 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 spin_unlock_bh(&mfc_unres_lock);
1236 }
1237}
1238
Eric Dumazet4c968702010-10-01 16:15:01 +00001239/* called from ip_ra_control(), before an RCU grace period,
1240 * we dont need to call synchronize_rcu() here
1241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242static void mrtsock_destruct(struct sock *sk)
1243{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001244 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001245 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001248 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001249 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001250 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001251 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1252 NETCONFA_IFINDEX_ALL,
1253 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001254 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001255 mroute_clean_tables(mrt);
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 rtnl_unlock();
1259}
1260
1261/*
1262 * Socket options and virtual interface manipulation. The whole
1263 * virtual interface system is a complete heap, but unfortunately
1264 * that's how BSD mrouted happens to think. Maybe one day with a proper
1265 * MOSPF/PIM router set up we can clean this up.
1266 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001267
David S. Millerb7058842009-09-30 16:12:20 -07001268int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001270 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct vifctl vif;
1272 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001273 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001274 struct mr_table *mrt;
1275
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001276 if (sk->sk_type != SOCK_RAW ||
1277 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1278 return -EOPNOTSUPP;
1279
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001280 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1281 if (mrt == NULL)
1282 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001283
Stephen Hemminger132adf52007-03-08 20:44:43 -08001284 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001285 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001286 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -EACCES;
1288 }
1289
Stephen Hemminger132adf52007-03-08 20:44:43 -08001290 switch (optname) {
1291 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001292 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Stephen Hemminger132adf52007-03-08 20:44:43 -08001295 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001296 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001298 return -EADDRINUSE;
1299 }
1300
1301 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1302 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001303 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001304 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001305 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1306 NETCONFA_IFINDEX_ALL,
1307 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001308 }
1309 rtnl_unlock();
1310 return ret;
1311 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001312 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001313 return -EACCES;
1314 return ip_ra_control(sk, 0, NULL);
1315 case MRT_ADD_VIF:
1316 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001317 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001319 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001320 return -EFAULT;
1321 if (vif.vifc_vifi >= MAXVIFS)
1322 return -ENFILE;
1323 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001324 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001325 ret = vif_add(net, mrt, &vif,
1326 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001327 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001328 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001329 }
1330 rtnl_unlock();
1331 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /*
1334 * Manipulate the forwarding caches. These live
1335 * in a sort of kernel/user symbiosis.
1336 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001337 case MRT_ADD_MFC:
1338 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001339 parent = -1;
1340 case MRT_ADD_MFC_PROXY:
1341 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001342 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001343 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001344 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001345 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001346 if (parent == 0)
1347 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001348 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001349 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1350 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001351 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001352 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001353 sk == rtnl_dereference(mrt->mroute_sk),
1354 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001355 rtnl_unlock();
1356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
1358 * Control PIM assert.
1359 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001360 case MRT_ASSERT:
1361 {
1362 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001363 if (optlen != sizeof(v))
1364 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001365 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001366 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001367 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001368 return 0;
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001371 case MRT_PIM:
1372 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001373 int v;
1374
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001375 if (optlen != sizeof(v))
1376 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001377 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001378 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001379 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001380
Stephen Hemminger132adf52007-03-08 20:44:43 -08001381 rtnl_lock();
1382 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001383 if (v != mrt->mroute_do_pim) {
1384 mrt->mroute_do_pim = v;
1385 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001387 rtnl_unlock();
1388 return ret;
1389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001391#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1392 case MRT_TABLE:
1393 {
1394 u32 v;
1395
1396 if (optlen != sizeof(u32))
1397 return -EINVAL;
1398 if (get_user(v, (u32 __user *)optval))
1399 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001400
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001401 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1402 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1403 return -EINVAL;
1404
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001405 rtnl_lock();
1406 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001407 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1408 ret = -EBUSY;
1409 } else {
1410 if (!ipmr_new_table(net, v))
1411 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001412 else
1413 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001414 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001415 rtnl_unlock();
1416 return ret;
1417 }
1418#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001419 /*
1420 * Spurious command, or MRT_VERSION which you cannot
1421 * set.
1422 */
1423 default:
1424 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426}
1427
1428/*
1429 * Getsock opt support for the multicast routing system.
1430 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001431
Jianjun Kongc354e122008-11-03 00:28:02 -08001432int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 int olr;
1435 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001436 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001437 struct mr_table *mrt;
1438
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001439 if (sk->sk_type != SOCK_RAW ||
1440 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1441 return -EOPNOTSUPP;
1442
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001443 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1444 if (mrt == NULL)
1445 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Jianjun Kongc354e122008-11-03 00:28:02 -08001447 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001449 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001451 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -ENOPROTOOPT;
1453
1454 if (get_user(olr, optlen))
1455 return -EFAULT;
1456
1457 olr = min_t(unsigned int, olr, sizeof(int));
1458 if (olr < 0)
1459 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001460
Jianjun Kongc354e122008-11-03 00:28:02 -08001461 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001463 if (optname == MRT_VERSION)
1464 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001466 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001467 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468#endif
1469 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001470 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001471 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EFAULT;
1473 return 0;
1474}
1475
1476/*
1477 * The IP multicast ioctl support routines.
1478 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1481{
1482 struct sioc_sg_req sr;
1483 struct sioc_vif_req vr;
1484 struct vif_device *vif;
1485 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001486 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001487 struct mr_table *mrt;
1488
1489 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1490 if (mrt == NULL)
1491 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001492
Stephen Hemminger132adf52007-03-08 20:44:43 -08001493 switch (cmd) {
1494 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001495 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001496 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001497 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498 return -EINVAL;
1499 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001500 vif = &mrt->vif_table[vr.vifi];
1501 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001502 vr.icount = vif->pkt_in;
1503 vr.ocount = vif->pkt_out;
1504 vr.ibytes = vif->bytes_in;
1505 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001507
Jianjun Kongc354e122008-11-03 00:28:02 -08001508 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001510 return 0;
1511 }
1512 read_unlock(&mrt_lock);
1513 return -EADDRNOTAVAIL;
1514 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001515 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001516 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Eric Dumazeta8c94862010-10-01 16:15:08 +00001518 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001519 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520 if (c) {
1521 sr.pktcnt = c->mfc_un.res.pkt;
1522 sr.bytecnt = c->mfc_un.res.bytes;
1523 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001524 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001525
Jianjun Kongc354e122008-11-03 00:28:02 -08001526 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001527 return -EFAULT;
1528 return 0;
1529 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001530 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001531 return -EADDRNOTAVAIL;
1532 default:
1533 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535}
1536
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001537#ifdef CONFIG_COMPAT
1538struct compat_sioc_sg_req {
1539 struct in_addr src;
1540 struct in_addr grp;
1541 compat_ulong_t pktcnt;
1542 compat_ulong_t bytecnt;
1543 compat_ulong_t wrong_if;
1544};
1545
David S. Millerca6b8bb02011-02-03 17:24:28 -08001546struct compat_sioc_vif_req {
1547 vifi_t vifi; /* Which iface */
1548 compat_ulong_t icount;
1549 compat_ulong_t ocount;
1550 compat_ulong_t ibytes;
1551 compat_ulong_t obytes;
1552};
1553
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001554int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1555{
David S. Miller0033d5a2011-02-03 17:21:31 -08001556 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001557 struct compat_sioc_vif_req vr;
1558 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001559 struct mfc_cache *c;
1560 struct net *net = sock_net(sk);
1561 struct mr_table *mrt;
1562
1563 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1564 if (mrt == NULL)
1565 return -ENOENT;
1566
1567 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001568 case SIOCGETVIFCNT:
1569 if (copy_from_user(&vr, arg, sizeof(vr)))
1570 return -EFAULT;
1571 if (vr.vifi >= mrt->maxvif)
1572 return -EINVAL;
1573 read_lock(&mrt_lock);
1574 vif = &mrt->vif_table[vr.vifi];
1575 if (VIF_EXISTS(mrt, vr.vifi)) {
1576 vr.icount = vif->pkt_in;
1577 vr.ocount = vif->pkt_out;
1578 vr.ibytes = vif->bytes_in;
1579 vr.obytes = vif->bytes_out;
1580 read_unlock(&mrt_lock);
1581
1582 if (copy_to_user(arg, &vr, sizeof(vr)))
1583 return -EFAULT;
1584 return 0;
1585 }
1586 read_unlock(&mrt_lock);
1587 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001588 case SIOCGETSGCNT:
1589 if (copy_from_user(&sr, arg, sizeof(sr)))
1590 return -EFAULT;
1591
1592 rcu_read_lock();
1593 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1594 if (c) {
1595 sr.pktcnt = c->mfc_un.res.pkt;
1596 sr.bytecnt = c->mfc_un.res.bytes;
1597 sr.wrong_if = c->mfc_un.res.wrong_if;
1598 rcu_read_unlock();
1599
1600 if (copy_to_user(arg, &sr, sizeof(sr)))
1601 return -EFAULT;
1602 return 0;
1603 }
1604 rcu_read_unlock();
1605 return -EADDRNOTAVAIL;
1606 default:
1607 return -ENOIOCTLCMD;
1608 }
1609}
1610#endif
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1614{
Jiri Pirko351638e2013-05-28 01:30:21 +00001615 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001616 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001617 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 struct vif_device *v;
1619 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (event != NETDEV_UNREGISTER)
1622 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001623
1624 ipmr_for_each_table(mrt, net) {
1625 v = &mrt->vif_table[0];
1626 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1627 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001628 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 return NOTIFY_DONE;
1632}
1633
1634
Jianjun Kongc354e122008-11-03 00:28:02 -08001635static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 .notifier_call = ipmr_device_event,
1637};
1638
1639/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001640 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 * This avoids tunnel drivers and other mess and gives us the speed so
1642 * important for multicast video.
1643 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001644
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001645static void ip_encap(struct net *net, struct sk_buff *skb,
1646 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001648 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001649 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001650
1651 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001652 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001653 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001654 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001656 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001657 iph->tos = old_iph->tos;
1658 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 iph->frag_off = 0;
1660 iph->daddr = daddr;
1661 iph->saddr = saddr;
1662 iph->protocol = IPPROTO_IPIP;
1663 iph->ihl = 5;
1664 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001665 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 ip_send_check(iph);
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1669 nf_reset(skb);
1670}
1671
1672static inline int ipmr_forward_finish(struct sk_buff *skb)
1673{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001674 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Eric Dumazetadf30902009-06-02 05:19:30 +00001676 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Vincent Bernat2d8dbb02012-06-05 03:41:42 +00001677 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 if (unlikely(opt->optlen))
1680 ip_forward_options(skb);
1681
1682 return dst_output(skb);
1683}
1684
1685/*
1686 * Processing handlers for ipmr_forward
1687 */
1688
Patrick McHardy0c122952010-04-13 05:03:22 +00001689static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1690 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001692 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001693 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct net_device *dev;
1695 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001696 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int encap = 0;
1698
1699 if (vif->dev == NULL)
1700 goto out_free;
1701
1702#ifdef CONFIG_IP_PIMSM
1703 if (vif->flags & VIFF_REGISTER) {
1704 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001705 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001706 vif->dev->stats.tx_bytes += skb->len;
1707 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001708 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001709 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711#endif
1712
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001713 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001714 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001715 vif->remote, vif->local,
1716 0, 0,
1717 IPPROTO_IPIP,
1718 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001719 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 goto out_free;
1721 encap = sizeof(struct iphdr);
1722 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001723 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001724 0, 0,
1725 IPPROTO_IPIP,
1726 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001727 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 goto out_free;
1729 }
1730
Changli Gaod8d1f302010-06-10 23:31:35 -07001731 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Changli Gaod8d1f302010-06-10 23:31:35 -07001733 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001735 * allow to send ICMP, so that packets will disappear
1736 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 */
1738
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001739 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 ip_rt_put(rt);
1741 goto out_free;
1742 }
1743
Changli Gaod8d1f302010-06-10 23:31:35 -07001744 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001747 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 goto out_free;
1749 }
1750
1751 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001752 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Eric Dumazetadf30902009-06-02 05:19:30 +00001754 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001755 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001756 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001759 * What do we do with netfilter? -- RR
1760 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001762 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001764 vif->dev->stats.tx_packets++;
1765 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767
1768 IPCB(skb)->flags |= IPSKB_FORWARDED;
1769
1770 /*
1771 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1772 * not only before forwarding, but after forwarding on all output
1773 * interfaces. It is clear, if mrouter runs a multicasting
1774 * program, it should receive packets not depending to what interface
1775 * program is joined.
1776 * If we will not make it, the program will have to join on all
1777 * interfaces. On the other hand, multihoming host (or router, but
1778 * not mrouter) cannot join to more than one interface - it will
1779 * result in receiving multiple packets.
1780 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001781 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 ipmr_forward_finish);
1783 return;
1784
1785out_free:
1786 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
Patrick McHardy0c122952010-04-13 05:03:22 +00001789static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001792
1793 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1794 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 break;
1796 }
1797 return ct;
1798}
1799
1800/* "local" means that we should preserve one skb (for local delivery) */
1801
Rami Rosenc4854ec2013-07-20 15:09:28 +03001802static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1803 struct sk_buff *skb, struct mfc_cache *cache,
1804 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 int psend = -1;
1807 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001808 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 vif = cache->mfc_parent;
1811 cache->mfc_un.res.pkt++;
1812 cache->mfc_un.res.bytes += skb->len;
1813
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001814 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001815 struct mfc_cache *cache_proxy;
1816
1817 /* For an (*,G) entry, we only check that the incomming
1818 * interface is part of the static tree.
1819 */
1820 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1821 if (cache_proxy &&
1822 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1823 goto forward;
1824 }
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /*
1827 * Wrong interface: drop packet and (maybe) send PIM assert.
1828 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001829 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001830 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001832 * Very complicated situation...
1833 *
1834 * The best workaround until routing daemons will be
1835 * fixed is not to redistribute packet, if it was
1836 * send through wrong interface. It means, that
1837 * multicast applications WILL NOT work for
1838 * (S,G), which have default multicast route pointing
1839 * to wrong oif. In any case, it is not a good
1840 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
1842 goto dont_forward;
1843 }
1844
1845 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Patrick McHardy0c122952010-04-13 05:03:22 +00001847 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001849 * so that we cannot check that packet arrived on an oif.
1850 * It is bad, but otherwise we would need to move pretty
1851 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001853 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001854 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001855 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1857 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001858 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860 goto dont_forward;
1861 }
1862
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001863forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001864 mrt->vif_table[vif].pkt_in++;
1865 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 /*
1868 * Forward the frame
1869 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001870 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1871 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001872 if (true_vifi >= 0 &&
1873 true_vifi != cache->mfc_parent &&
1874 ip_hdr(skb)->ttl >
1875 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1876 /* It's an (*,*) entry and the packet is not coming from
1877 * the upstream: forward the packet to the upstream
1878 * only.
1879 */
1880 psend = cache->mfc_parent;
1881 goto last_forward;
1882 }
1883 goto dont_forward;
1884 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001885 for (ct = cache->mfc_un.res.maxvif - 1;
1886 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001887 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001888 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1889 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001890 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (psend != -1) {
1892 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001895 ipmr_queue_xmit(net, mrt, skb2, cache,
1896 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001898 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001901last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (psend != -1) {
1903 if (local) {
1904 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001907 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001909 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001910 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 }
1913
1914dont_forward:
1915 if (!local)
1916 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
David S. Miller417da662011-05-03 19:42:43 -07001919static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001920{
David S. Miller417da662011-05-03 19:42:43 -07001921 struct rtable *rt = skb_rtable(skb);
1922 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001923 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001924 .daddr = iph->daddr,
1925 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001926 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001927 .flowi4_oif = (rt_is_output_route(rt) ?
1928 skb->dev->ifindex : 0),
1929 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001930 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001931 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001932 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001933 };
1934 struct mr_table *mrt;
1935 int err;
1936
David S. Millerda919812011-03-12 02:04:50 -05001937 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001938 if (err)
1939 return ERR_PTR(err);
1940 return mrt;
1941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943/*
1944 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001945 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
1947
1948int ip_mr_input(struct sk_buff *skb)
1949{
1950 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001951 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001952 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001953 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001956 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001958 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 goto dont_forward;
1960
David S. Miller417da662011-05-03 19:42:43 -07001961 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001962 if (IS_ERR(mrt)) {
1963 kfree_skb(skb);
1964 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001967 if (IPCB(skb)->opt.router_alert) {
1968 if (ip_call_ra_chain(skb))
1969 return 0;
1970 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1971 /* IGMPv1 (and broken IGMPv2 implementations sort of
1972 * Cisco IOS <= 11.2(8)) do not put router alert
1973 * option to IGMP packets destined to routable
1974 * groups. It is very bad, because it means
1975 * that we can forward NO IGMP messages.
1976 */
1977 struct sock *mroute_sk;
1978
1979 mroute_sk = rcu_dereference(mrt->mroute_sk);
1980 if (mroute_sk) {
1981 nf_reset(skb);
1982 raw_rcv(mroute_sk, skb);
1983 return 0;
1984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986 }
1987
Eric Dumazeta8c94862010-10-01 16:15:08 +00001988 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001989 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001990 if (cache == NULL) {
1991 int vif = ipmr_find_vif(mrt, skb->dev);
1992
1993 if (vif >= 0)
1994 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1995 vif);
1996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /*
1999 * No usable cache entry
2000 */
Jianjun Kongc354e122008-11-03 00:28:02 -08002001 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int vif;
2003
2004 if (local) {
2005 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2006 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002007 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 skb = skb2;
2010 }
2011
Eric Dumazeta8c94862010-10-01 16:15:08 +00002012 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002013 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002015 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 read_unlock(&mrt_lock);
2017
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002018 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020 read_unlock(&mrt_lock);
2021 kfree_skb(skb);
2022 return -ENODEV;
2023 }
2024
Eric Dumazeta8c94862010-10-01 16:15:08 +00002025 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002026 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 read_unlock(&mrt_lock);
2028
2029 if (local)
2030 return ip_local_deliver(skb);
2031
2032 return 0;
2033
2034dont_forward:
2035 if (local)
2036 return ip_local_deliver(skb);
2037 kfree_skb(skb);
2038 return 0;
2039}
2040
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002041#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00002042/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002043static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2044 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002046 struct net_device *reg_dev = NULL;
2047 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002049 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002051 * Check that:
2052 * a. packet is really sent to a multicast group
2053 * b. packet is not a NULL-REGISTER
2054 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002056 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002058 ntohs(encap->tot_len) + pimlen > skb->len)
2059 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002062 if (mrt->mroute_reg_vif_num >= 0)
2063 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 read_unlock(&mrt_lock);
2065
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002066 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002067 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002069 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00002070 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03002071 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00002073 skb->ip_summed = CHECKSUM_NONE;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002074
Nicolas Dichtelea231922013-09-02 15:34:58 +02002075 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002078
Eric Dumazet55747a02010-10-01 16:14:55 +00002079 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002080}
2081#endif
2082
2083#ifdef CONFIG_IP_PIMSM_V1
2084/*
2085 * Handle IGMP messages of PIMv1
2086 */
2087
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002088int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002089{
2090 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002091 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002092 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002093
2094 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2095 goto drop;
2096
2097 pim = igmp_hdr(skb);
2098
David S. Miller417da662011-05-03 19:42:43 -07002099 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002100 if (IS_ERR(mrt))
2101 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002102 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002103 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2104 goto drop;
2105
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002106 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002107drop:
2108 kfree_skb(skb);
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return 0;
2111}
2112#endif
2113
2114#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002115static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
2117 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002118 struct net *net = dev_net(skb->dev);
2119 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002121 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 goto drop;
2123
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002124 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002125 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2126 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002127 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002128 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 goto drop;
2130
David S. Miller417da662011-05-03 19:42:43 -07002131 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002132 if (IS_ERR(mrt))
2133 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002134 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002135drop:
2136 kfree_skb(skb);
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 0;
2139}
2140#endif
2141
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002142static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2143 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 int ct;
2146 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002147 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002148 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Nicolas Dichtel74381892010-03-25 23:45:35 +00002150 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002151 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002152 return -ENOENT;
2153
Thomas Graf92a395e2012-06-26 23:36:13 +00002154 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2155 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2156 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Thomas Graf92a395e2012-06-26 23:36:13 +00002158 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2159 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002162 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002163 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2164 nla_nest_cancel(skb, mp_attr);
2165 return -EMSGSIZE;
2166 }
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 nhp->rtnh_flags = 0;
2169 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002170 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 nhp->rtnh_len = sizeof(*nhp);
2172 }
2173 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002174
2175 nla_nest_end(skb, mp_attr);
2176
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002177 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2178 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2179 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2180 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2181 return -EMSGSIZE;
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 rtm->rtm_type = RTN_MULTICAST;
2184 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
David S. Miller9a1b9492011-05-04 12:18:54 -07002187int ipmr_get_route(struct net *net, struct sk_buff *skb,
2188 __be32 saddr, __be32 daddr,
2189 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002192 struct mr_table *mrt;
2193 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002195 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2196 if (mrt == NULL)
2197 return -ENOENT;
2198
Eric Dumazeta8c94862010-10-01 16:15:08 +00002199 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002200 cache = ipmr_cache_find(mrt, saddr, daddr);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002201 if (cache == NULL && skb->dev) {
2202 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002204 if (vif >= 0)
2205 cache = ipmr_cache_find_any(mrt, daddr, vif);
2206 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002207 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002208 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002209 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002211 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002214 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return -EAGAIN;
2216 }
2217
2218 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002219 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002220 if (dev)
2221 vif = ipmr_find_vif(mrt, dev);
2222 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002224 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return -ENODEV;
2226 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002227 skb2 = skb_clone(skb, GFP_ATOMIC);
2228 if (!skb2) {
2229 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002230 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002231 return -ENOMEM;
2232 }
2233
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002234 skb_push(skb2, sizeof(struct iphdr));
2235 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002236 iph = ip_hdr(skb2);
2237 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002238 iph->saddr = saddr;
2239 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002240 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002241 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002243 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return err;
2245 }
2246
Eric Dumazeta8c94862010-10-01 16:15:08 +00002247 read_lock(&mrt_lock);
2248 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002250 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002252 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return err;
2254}
2255
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002256static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002257 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2258 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002259{
2260 struct nlmsghdr *nlh;
2261 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002262 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002263
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002264 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002265 if (nlh == NULL)
2266 return -EMSGSIZE;
2267
2268 rtm = nlmsg_data(nlh);
2269 rtm->rtm_family = RTNL_FAMILY_IPMR;
2270 rtm->rtm_dst_len = 32;
2271 rtm->rtm_src_len = 32;
2272 rtm->rtm_tos = 0;
2273 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002274 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2275 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002276 rtm->rtm_type = RTN_MULTICAST;
2277 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002278 if (c->mfc_flags & MFC_STATIC)
2279 rtm->rtm_protocol = RTPROT_STATIC;
2280 else
2281 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002282 rtm->rtm_flags = 0;
2283
Jiri Benc930345e2015-03-29 16:59:25 +02002284 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2285 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002286 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002287 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2288 /* do not break the dump if cache is unresolved */
2289 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002290 goto nla_put_failure;
2291
Johannes Berg053c0952015-01-16 22:09:00 +01002292 nlmsg_end(skb, nlh);
2293 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002294
2295nla_put_failure:
2296 nlmsg_cancel(skb, nlh);
2297 return -EMSGSIZE;
2298}
2299
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002300static size_t mroute_msgsize(bool unresolved, int maxvif)
2301{
2302 size_t len =
2303 NLMSG_ALIGN(sizeof(struct rtmsg))
2304 + nla_total_size(4) /* RTA_TABLE */
2305 + nla_total_size(4) /* RTA_SRC */
2306 + nla_total_size(4) /* RTA_DST */
2307 ;
2308
2309 if (!unresolved)
2310 len = len
2311 + nla_total_size(4) /* RTA_IIF */
2312 + nla_total_size(0) /* RTA_MULTIPATH */
2313 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2314 /* RTA_MFC_STATS */
2315 + nla_total_size(sizeof(struct rta_mfc_stats))
2316 ;
2317
2318 return len;
2319}
2320
2321static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2322 int cmd)
2323{
2324 struct net *net = read_pnet(&mrt->net);
2325 struct sk_buff *skb;
2326 int err = -ENOBUFS;
2327
2328 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2329 GFP_ATOMIC);
2330 if (skb == NULL)
2331 goto errout;
2332
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002333 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002334 if (err < 0)
2335 goto errout;
2336
2337 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2338 return;
2339
2340errout:
2341 kfree_skb(skb);
2342 if (err < 0)
2343 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2344}
2345
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002346static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2347{
2348 struct net *net = sock_net(skb->sk);
2349 struct mr_table *mrt;
2350 struct mfc_cache *mfc;
2351 unsigned int t = 0, s_t;
2352 unsigned int h = 0, s_h;
2353 unsigned int e = 0, s_e;
2354
2355 s_t = cb->args[0];
2356 s_h = cb->args[1];
2357 s_e = cb->args[2];
2358
Eric Dumazeta8c94862010-10-01 16:15:08 +00002359 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002360 ipmr_for_each_table(mrt, net) {
2361 if (t < s_t)
2362 goto next_table;
2363 if (t > s_t)
2364 s_h = 0;
2365 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002366 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002367 if (e < s_e)
2368 goto next_entry;
2369 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002370 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002371 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002372 mfc, RTM_NEWROUTE,
2373 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002374 goto done;
2375next_entry:
2376 e++;
2377 }
2378 e = s_e = 0;
2379 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002380 spin_lock_bh(&mfc_unres_lock);
2381 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2382 if (e < s_e)
2383 goto next_entry2;
2384 if (ipmr_fill_mroute(mrt, skb,
2385 NETLINK_CB(cb->skb).portid,
2386 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002387 mfc, RTM_NEWROUTE,
2388 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002389 spin_unlock_bh(&mfc_unres_lock);
2390 goto done;
2391 }
2392next_entry2:
2393 e++;
2394 }
2395 spin_unlock_bh(&mfc_unres_lock);
2396 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002397 s_h = 0;
2398next_table:
2399 t++;
2400 }
2401done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002402 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002403
2404 cb->args[2] = e;
2405 cb->args[1] = h;
2406 cb->args[0] = t;
2407
2408 return skb->len;
2409}
2410
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002411#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002413 * The /proc interfaces to multicast routing :
2414 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 */
2416struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002417 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002418 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 int ct;
2420};
2421
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002422static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2423 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 loff_t pos)
2425{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002426 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002427
2428 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2429 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002431 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002432 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434 return NULL;
2435}
2436
2437static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002438 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002440 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002441 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002442 struct mr_table *mrt;
2443
2444 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2445 if (mrt == NULL)
2446 return ERR_PTR(-ENOENT);
2447
2448 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002451 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 : SEQ_START_TOKEN;
2453}
2454
2455static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2456{
2457 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002458 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002459 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 ++*pos;
2462 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002463 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002464
Patrick McHardy0c122952010-04-13 05:03:22 +00002465 while (++iter->ct < mrt->maxvif) {
2466 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002468 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470 return NULL;
2471}
2472
2473static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002474 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 read_unlock(&mrt_lock);
2477}
2478
2479static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2480{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002481 struct ipmr_vif_iter *iter = seq->private;
2482 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002485 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2487 } else {
2488 const struct vif_device *vif = v;
2489 const char *name = vif->dev ? vif->dev->name : "none";
2490
2491 seq_printf(seq,
2492 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002493 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002494 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 vif->bytes_out, vif->pkt_out,
2496 vif->flags, vif->local, vif->remote);
2497 }
2498 return 0;
2499}
2500
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002501static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 .start = ipmr_vif_seq_start,
2503 .next = ipmr_vif_seq_next,
2504 .stop = ipmr_vif_seq_stop,
2505 .show = ipmr_vif_seq_show,
2506};
2507
2508static int ipmr_vif_open(struct inode *inode, struct file *file)
2509{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002510 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2511 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
Arjan van de Ven9a321442007-02-12 00:55:35 -08002514static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 .owner = THIS_MODULE,
2516 .open = ipmr_vif_open,
2517 .read = seq_read,
2518 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002519 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520};
2521
2522struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002523 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002524 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002525 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 int ct;
2527};
2528
2529
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002530static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2531 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002533 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 struct mfc_cache *mfc;
2535
Eric Dumazeta8c94862010-10-01 16:15:08 +00002536 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002537 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002538 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002539 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002540 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002542 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002543 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002546 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002547 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002548 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return mfc;
2550 spin_unlock_bh(&mfc_unres_lock);
2551
2552 it->cache = NULL;
2553 return NULL;
2554}
2555
2556
2557static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2558{
2559 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002560 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002561 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002562
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002563 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2564 if (mrt == NULL)
2565 return ERR_PTR(-ENOENT);
2566
2567 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 it->cache = NULL;
2569 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002570 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 : SEQ_START_TOKEN;
2572}
2573
2574static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2575{
2576 struct mfc_cache *mfc = v;
2577 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002578 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002579 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 ++*pos;
2582
2583 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002584 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Patrick McHardy862465f2010-04-13 05:03:21 +00002586 if (mfc->list.next != it->cache)
2587 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002588
Patrick McHardy0c122952010-04-13 05:03:22 +00002589 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 goto end_of_list;
2591
Patrick McHardy0c122952010-04-13 05:03:22 +00002592 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002595 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002596 if (list_empty(it->cache))
2597 continue;
2598 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 }
2600
2601 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002602 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002603 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002607 if (!list_empty(it->cache))
2608 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002610end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 spin_unlock_bh(&mfc_unres_lock);
2612 it->cache = NULL;
2613
2614 return NULL;
2615}
2616
2617static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2618{
2619 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002620 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Patrick McHardy0c122952010-04-13 05:03:22 +00002622 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002624 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002625 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
2628static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2629{
2630 int n;
2631
2632 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002633 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2635 } else {
2636 const struct mfc_cache *mfc = v;
2637 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002638 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002639
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002640 seq_printf(seq, "%08X %08X %-3hd",
2641 (__force u32) mfc->mfc_mcastgrp,
2642 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002643 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Patrick McHardy0c122952010-04-13 05:03:22 +00002645 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002646 seq_printf(seq, " %8lu %8lu %8lu",
2647 mfc->mfc_un.res.pkt,
2648 mfc->mfc_un.res.bytes,
2649 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002650 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002651 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002652 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002653 mfc->mfc_un.res.ttls[n] < 255)
2654 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002655 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 n, mfc->mfc_un.res.ttls[n]);
2657 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002658 } else {
2659 /* unresolved mfc_caches don't contain
2660 * pkt, bytes and wrong_if values
2661 */
2662 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664 seq_putc(seq, '\n');
2665 }
2666 return 0;
2667}
2668
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002669static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 .start = ipmr_mfc_seq_start,
2671 .next = ipmr_mfc_seq_next,
2672 .stop = ipmr_mfc_seq_stop,
2673 .show = ipmr_mfc_seq_show,
2674};
2675
2676static int ipmr_mfc_open(struct inode *inode, struct file *file)
2677{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002678 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2679 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Arjan van de Ven9a321442007-02-12 00:55:35 -08002682static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 .owner = THIS_MODULE,
2684 .open = ipmr_mfc_open,
2685 .read = seq_read,
2686 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002687 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002689#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002692static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002694 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695};
2696#endif
2697
2698
2699/*
2700 * Setup for IP multicast routing
2701 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002702static int __net_init ipmr_net_init(struct net *net)
2703{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002704 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002705
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002706 err = ipmr_rules_init(net);
2707 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002708 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002709
2710#ifdef CONFIG_PROC_FS
2711 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002712 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002713 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002714 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002715 goto proc_cache_fail;
2716#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002717 return 0;
2718
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002719#ifdef CONFIG_PROC_FS
2720proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002721 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002722proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002723 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002724#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002725fail:
2726 return err;
2727}
2728
2729static void __net_exit ipmr_net_exit(struct net *net)
2730{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002731#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002732 remove_proc_entry("ip_mr_cache", net->proc_net);
2733 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002734#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002735 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002736}
2737
2738static struct pernet_operations ipmr_net_ops = {
2739 .init = ipmr_net_init,
2740 .exit = ipmr_net_exit,
2741};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002742
Wang Chen03d2f892008-07-03 12:13:36 +08002743int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
Wang Chen03d2f892008-07-03 12:13:36 +08002745 int err;
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2748 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002749 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002750 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002751 if (!mrt_cachep)
2752 return -ENOMEM;
2753
Benjamin Therycf958ae32009-01-22 04:56:16 +00002754 err = register_pernet_subsys(&ipmr_net_ops);
2755 if (err)
2756 goto reg_pernet_fail;
2757
Wang Chen03d2f892008-07-03 12:13:36 +08002758 err = register_netdevice_notifier(&ip_mr_notifier);
2759 if (err)
2760 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002761#ifdef CONFIG_IP_PIMSM_V2
2762 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002763 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002764 err = -EAGAIN;
2765 goto add_proto_fail;
2766 }
2767#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002768 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2769 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002770 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002771
Tom Goff403dbb92009-06-14 03:16:13 -07002772#ifdef CONFIG_IP_PIMSM_V2
2773add_proto_fail:
2774 unregister_netdevice_notifier(&ip_mr_notifier);
2775#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002776reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002777 unregister_pernet_subsys(&ipmr_net_ops);
2778reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002779 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}