blob: 65bcaa7890436df5ad2ee16f4465af51e141357b [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;
Patrick McHardy8de53df2010-04-15 13:29:28 +020076#ifdef CONFIG_NET_NS
77 struct net *net;
78#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +000079 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000080 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000081 struct timer_list ipmr_expire_timer;
82 struct list_head mfc_unres_queue;
83 struct list_head mfc_cache_array[MFC_LINES];
84 struct vif_device vif_table[MAXVIFS];
85 int maxvif;
86 atomic_t cache_resolve_queue_len;
Joe Perches53d68412012-11-25 09:35:30 +000087 bool mroute_do_assert;
88 bool mroute_do_pim;
Patrick McHardy0c122952010-04-13 05:03:22 +000089#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num;
91#endif
92};
93
Patrick McHardyf0ad0862010-04-13 05:03:23 +000094struct ipmr_rule {
95 struct fib_rule common;
96};
97
98struct ipmr_result {
99 struct mr_table *mrt;
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000103 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105
106static DEFINE_RWLOCK(mrt_lock);
107
108/*
109 * Multicast router control variables
110 */
111
Patrick McHardy0c122952010-04-13 05:03:22 +0000112#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* Special spinlock for queue of unresolved entries */
115static DEFINE_SPINLOCK(mfc_unres_lock);
116
117/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
121 *
122 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124
Christoph Lametere18b8902006-12-06 20:33:20 -0800125static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000127static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000128static void ipmr_free_table(struct mr_table *mrt);
129
Rami Rosenc4854ec2013-07-20 15:09:28 +0300130static void ip_mr_forward(struct net *net, struct mr_table *mrt,
131 struct sk_buff *skb, struct mfc_cache *cache,
132 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000133static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000134 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200135static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000137static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
138 int cmd);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000139static void mroute_clean_tables(struct mr_table *mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000140static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000142#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
143#define ipmr_for_each_table(mrt, net) \
144 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
145
146static struct mr_table *ipmr_get_table(struct net *net, u32 id)
147{
148 struct mr_table *mrt;
149
150 ipmr_for_each_table(mrt, net) {
151 if (mrt->id == id)
152 return mrt;
153 }
154 return NULL;
155}
156
David S. Millerda919812011-03-12 02:04:50 -0500157static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000158 struct mr_table **mrt)
159{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000160 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100161 struct ipmr_result res;
162 struct fib_lookup_arg arg = {
163 .result = &res,
164 .flags = FIB_LOOKUP_NOREF,
165 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000166
David S. Millerda919812011-03-12 02:04:50 -0500167 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
168 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000169 if (err < 0)
170 return err;
171 *mrt = res.mrt;
172 return 0;
173}
174
175static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
176 int flags, struct fib_lookup_arg *arg)
177{
178 struct ipmr_result *res = arg->result;
179 struct mr_table *mrt;
180
181 switch (rule->action) {
182 case FR_ACT_TO_TBL:
183 break;
184 case FR_ACT_UNREACHABLE:
185 return -ENETUNREACH;
186 case FR_ACT_PROHIBIT:
187 return -EACCES;
188 case FR_ACT_BLACKHOLE:
189 default:
190 return -EINVAL;
191 }
192
193 mrt = ipmr_get_table(rule->fr_net, rule->table);
194 if (mrt == NULL)
195 return -EAGAIN;
196 res->mrt = mrt;
197 return 0;
198}
199
200static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
201{
202 return 1;
203}
204
205static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
206 FRA_GENERIC_POLICY,
207};
208
209static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
210 struct fib_rule_hdr *frh, struct nlattr **tb)
211{
212 return 0;
213}
214
215static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
216 struct nlattr **tb)
217{
218 return 1;
219}
220
221static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
222 struct fib_rule_hdr *frh)
223{
224 frh->dst_len = 0;
225 frh->src_len = 0;
226 frh->tos = 0;
227 return 0;
228}
229
Andi Kleen04a6f822012-10-04 17:12:11 -0700230static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200231 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000232 .rule_size = sizeof(struct ipmr_rule),
233 .addr_size = sizeof(u32),
234 .action = ipmr_rule_action,
235 .match = ipmr_rule_match,
236 .configure = ipmr_rule_configure,
237 .compare = ipmr_rule_compare,
238 .default_pref = fib_default_rule_pref,
239 .fill = ipmr_rule_fill,
240 .nlgroup = RTNLGRP_IPV4_RULE,
241 .policy = ipmr_rule_policy,
242 .owner = THIS_MODULE,
243};
244
245static int __net_init ipmr_rules_init(struct net *net)
246{
247 struct fib_rules_ops *ops;
248 struct mr_table *mrt;
249 int err;
250
251 ops = fib_rules_register(&ipmr_rules_ops_template, net);
252 if (IS_ERR(ops))
253 return PTR_ERR(ops);
254
255 INIT_LIST_HEAD(&net->ipv4.mr_tables);
256
257 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
258 if (mrt == NULL) {
259 err = -ENOMEM;
260 goto err1;
261 }
262
263 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
264 if (err < 0)
265 goto err2;
266
267 net->ipv4.mr_rules_ops = ops;
268 return 0;
269
270err2:
271 kfree(mrt);
272err1:
273 fib_rules_unregister(ops);
274 return err;
275}
276
277static void __net_exit ipmr_rules_exit(struct net *net)
278{
279 struct mr_table *mrt, *next;
280
Eric Dumazet035320d2010-06-06 23:48:40 +0000281 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
282 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000283 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000284 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000285 fib_rules_unregister(net->ipv4.mr_rules_ops);
286}
287#else
288#define ipmr_for_each_table(mrt, net) \
289 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
290
291static struct mr_table *ipmr_get_table(struct net *net, u32 id)
292{
293 return net->ipv4.mrt;
294}
295
David S. Millerda919812011-03-12 02:04:50 -0500296static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000297 struct mr_table **mrt)
298{
299 *mrt = net->ipv4.mrt;
300 return 0;
301}
302
303static int __net_init ipmr_rules_init(struct net *net)
304{
305 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
306 return net->ipv4.mrt ? 0 : -ENOMEM;
307}
308
309static void __net_exit ipmr_rules_exit(struct net *net)
310{
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000311 ipmr_free_table(net->ipv4.mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000312}
313#endif
314
315static struct mr_table *ipmr_new_table(struct net *net, u32 id)
316{
317 struct mr_table *mrt;
318 unsigned int i;
319
320 mrt = ipmr_get_table(net, id);
321 if (mrt != NULL)
322 return mrt;
323
324 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
325 if (mrt == NULL)
326 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200327 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000328 mrt->id = id;
329
330 /* Forwarding cache */
331 for (i = 0; i < MFC_LINES; i++)
332 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
333
334 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
335
336 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
337 (unsigned long)mrt);
338
339#ifdef CONFIG_IP_PIMSM
340 mrt->mroute_reg_vif_num = -1;
341#endif
342#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
343 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
344#endif
345 return mrt;
346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000348static void ipmr_free_table(struct mr_table *mrt)
349{
350 del_timer_sync(&mrt->ipmr_expire_timer);
351 mroute_clean_tables(mrt);
352 kfree(mrt);
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
356
Wang Chend6070322008-07-14 20:55:26 -0700357static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
358{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000359 struct net *net = dev_net(dev);
360
Wang Chend6070322008-07-14 20:55:26 -0700361 dev_close(dev);
362
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000363 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700364 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800365 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700366 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700367 struct ip_tunnel_parm p;
368
369 memset(&p, 0, sizeof(p));
370 p.iph.daddr = v->vifc_rmt_addr.s_addr;
371 p.iph.saddr = v->vifc_lcl_addr.s_addr;
372 p.iph.version = 4;
373 p.iph.ihl = 5;
374 p.iph.protocol = IPPROTO_IPIP;
375 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
376 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
377
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800378 if (ops->ndo_do_ioctl) {
379 mm_segment_t oldfs = get_fs();
380
381 set_fs(KERNEL_DS);
382 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
383 set_fs(oldfs);
384 }
Wang Chend6070322008-07-14 20:55:26 -0700385 }
386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000389struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct net_device *dev;
392
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000393 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800396 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int err;
398 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct ip_tunnel_parm p;
400 struct in_device *in_dev;
401
402 memset(&p, 0, sizeof(p));
403 p.iph.daddr = v->vifc_rmt_addr.s_addr;
404 p.iph.saddr = v->vifc_lcl_addr.s_addr;
405 p.iph.version = 4;
406 p.iph.ihl = 5;
407 p.iph.protocol = IPPROTO_IPIP;
408 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800409 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800411 if (ops->ndo_do_ioctl) {
412 mm_segment_t oldfs = get_fs();
413
414 set_fs(KERNEL_DS);
415 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
416 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000417 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800418 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 dev = NULL;
421
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000422 if (err == 0 &&
423 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 dev->flags |= IFF_MULTICAST;
425
Herbert Xue5ed6392005-10-03 14:35:55 -0700426 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700427 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700429
430 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100431 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700432 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (dev_open(dev))
435 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700436 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438 }
439 return dev;
440
441failure:
442 /* allow the register to be completed before unregistering. */
443 rtnl_unlock();
444 rtnl_lock();
445
446 unregister_netdevice(dev);
447 return NULL;
448}
449
450#ifdef CONFIG_IP_PIMSM
451
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000452static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000454 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000455 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500456 struct flowi4 fl4 = {
457 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700458 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500459 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000460 };
461 int err;
462
David S. Millerda919812011-03-12 02:04:50 -0500463 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000464 if (err < 0) {
465 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000466 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000467 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700470 dev->stats.tx_bytes += skb->len;
471 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000472 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 read_unlock(&mrt_lock);
474 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000475 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Stephen Hemminger007c3832008-11-20 20:28:35 -0800478static const struct net_device_ops reg_vif_netdev_ops = {
479 .ndo_start_xmit = reg_vif_xmit,
480};
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static void reg_vif_setup(struct net_device *dev)
483{
484 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800485 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530487 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700489 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000492static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct net_device *dev;
495 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000496 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000498 if (mrt->id == RT_TABLE_DEFAULT)
499 sprintf(name, "pimreg");
500 else
501 sprintf(name, "pimreg%u", mrt->id);
502
503 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if (dev == NULL)
506 return NULL;
507
Tom Goff403dbb92009-06-14 03:16:13 -0700508 dev_net_set(dev, net);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (register_netdevice(dev)) {
511 free_netdev(dev);
512 return NULL;
513 }
514 dev->iflink = 0;
515
Herbert Xu71e27da2007-06-04 23:36:06 -0700516 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000517 in_dev = __in_dev_get_rcu(dev);
518 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700519 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Herbert Xu71e27da2007-06-04 23:36:06 -0700523 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100524 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700525 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
526 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if (dev_open(dev))
529 goto failure;
530
Wang Chen7dc00c82008-07-14 20:56:34 -0700531 dev_hold(dev);
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return dev;
534
535failure:
536 /* allow the register to be completed before unregistering. */
537 rtnl_unlock();
538 rtnl_lock();
539
540 unregister_netdevice(dev);
541 return NULL;
542}
543#endif
544
Ben Hutchings2c530402012-07-10 10:55:09 +0000545/**
546 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700547 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900549
Patrick McHardy0c122952010-04-13 05:03:22 +0000550static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000551 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct vif_device *v;
554 struct net_device *dev;
555 struct in_device *in_dev;
556
Patrick McHardy0c122952010-04-13 05:03:22 +0000557 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EADDRNOTAVAIL;
559
Patrick McHardy0c122952010-04-13 05:03:22 +0000560 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 write_lock_bh(&mrt_lock);
563 dev = v->dev;
564 v->dev = NULL;
565
566 if (!dev) {
567 write_unlock_bh(&mrt_lock);
568 return -EADDRNOTAVAIL;
569 }
570
571#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000572 if (vifi == mrt->mroute_reg_vif_num)
573 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574#endif
575
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000576 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000578
579 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000580 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000583 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586 write_unlock_bh(&mrt_lock);
587
588 dev_set_allmulti(dev, -1);
589
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000590 in_dev = __in_dev_get_rtnl(dev);
591 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700592 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000593 inet_netconf_notify_devconf(dev_net(dev),
594 NETCONFA_MC_FORWARDING,
595 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ip_rt_multicast_event(in_dev);
597 }
598
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000599 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000600 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 dev_put(dev);
603 return 0;
604}
605
Eric Dumazeta8c94862010-10-01 16:15:08 +0000606static void ipmr_cache_free_rcu(struct rcu_head *head)
607{
608 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
609
610 kmem_cache_free(mrt_cachep, c);
611}
612
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000613static inline void ipmr_cache_free(struct mfc_cache *c)
614{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000615 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000619 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
621
Patrick McHardy0c122952010-04-13 05:03:22 +0000622static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200624 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700626 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Patrick McHardy0c122952010-04-13 05:03:22 +0000628 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jianjun Kongc354e122008-11-03 00:28:02 -0800630 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700631 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
633 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000634 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000636 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700637 e->error = -ETIMEDOUT;
638 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700639
Eric W. Biederman15e47302012-09-07 20:12:54 +0000640 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000641 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000646 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649
Patrick McHardye258beb2010-04-13 05:03:19 +0000650/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Patrick McHardye258beb2010-04-13 05:03:19 +0000652static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Patrick McHardy0c122952010-04-13 05:03:22 +0000654 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 unsigned long now;
656 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000657 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000660 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return;
662 }
663
Patrick McHardy0c122952010-04-13 05:03:22 +0000664 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 goto out;
666
667 now = jiffies;
668 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Patrick McHardy0c122952010-04-13 05:03:22 +0000670 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (time_after(c->mfc_un.unres.expires, now)) {
672 unsigned long interval = c->mfc_un.unres.expires - now;
673 if (interval < expires)
674 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 continue;
676 }
677
Patrick McHardy862465f2010-04-13 05:03:21 +0000678 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000679 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000680 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
Patrick McHardy0c122952010-04-13 05:03:22 +0000683 if (!list_empty(&mrt->mfc_unres_queue))
684 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686out:
687 spin_unlock(&mfc_unres_lock);
688}
689
690/* Fill oifs list. It is called under write locked mrt_lock. */
691
Patrick McHardy0c122952010-04-13 05:03:22 +0000692static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000693 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 int vifi;
696
697 cache->mfc_un.res.minvif = MAXVIFS;
698 cache->mfc_un.res.maxvif = 0;
699 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
700
Patrick McHardy0c122952010-04-13 05:03:22 +0000701 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
702 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000703 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
705 if (cache->mfc_un.res.minvif > vifi)
706 cache->mfc_un.res.minvif = vifi;
707 if (cache->mfc_un.res.maxvif <= vifi)
708 cache->mfc_un.res.maxvif = vifi + 1;
709 }
710 }
711}
712
Patrick McHardy0c122952010-04-13 05:03:22 +0000713static int vif_add(struct net *net, struct mr_table *mrt,
714 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000717 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct net_device *dev;
719 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700720 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000723 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return -EADDRINUSE;
725
726 switch (vifc->vifc_flags) {
727#ifdef CONFIG_IP_PIMSM
728 case VIFF_REGISTER:
729 /*
730 * Special Purpose VIF in PIM
731 * All the packets will be sent to the daemon
732 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000733 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000735 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!dev)
737 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700738 err = dev_set_allmulti(dev, 1);
739 if (err) {
740 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700741 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700742 return err;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
745#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900746 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000747 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (!dev)
749 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700750 err = dev_set_allmulti(dev, 1);
751 if (err) {
752 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700753 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700754 return err;
755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000757
758 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000760 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
761 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000762 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000763 dev_put(dev);
764 return -EADDRNOTAVAIL;
765 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000766 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000767 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (!dev)
770 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700771 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700772 if (err) {
773 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700774 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777 default:
778 return -EINVAL;
779 }
780
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000781 in_dev = __in_dev_get_rtnl(dev);
782 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000783 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000785 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700786 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000787 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
788 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 ip_rt_multicast_event(in_dev);
790
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000791 /* Fill in the VIF structures */
792
Jianjun Kongc354e122008-11-03 00:28:02 -0800793 v->rate_limit = vifc->vifc_rate_limit;
794 v->local = vifc->vifc_lcl_addr.s_addr;
795 v->remote = vifc->vifc_rmt_addr.s_addr;
796 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!mrtsock)
798 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800799 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 v->bytes_in = 0;
801 v->bytes_out = 0;
802 v->pkt_in = 0;
803 v->pkt_out = 0;
804 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000805 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 v->link = dev->iflink;
807
808 /* And finish update writing critical data */
809 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800810 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000812 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000813 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000815 if (vifi+1 > mrt->maxvif)
816 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 write_unlock_bh(&mrt_lock);
818 return 0;
819}
820
Eric Dumazeta8c94862010-10-01 16:15:08 +0000821/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000822static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000823 __be32 origin,
824 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Jianjun Kongc354e122008-11-03 00:28:02 -0800826 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct mfc_cache *c;
828
Eric Dumazeta8c94862010-10-01 16:15:08 +0000829 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000830 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
831 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000833 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000836/* Look for a (*,*,oif) entry */
837static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
838 int vifi)
839{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100840 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000841 struct mfc_cache *c;
842
843 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100844 if (c->mfc_origin == htonl(INADDR_ANY) &&
845 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000846 c->mfc_un.res.ttls[vifi] < 255)
847 return c;
848
849 return NULL;
850}
851
852/* Look for a (*,G) entry */
853static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
854 __be32 mcastgrp, int vifi)
855{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100856 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000857 struct mfc_cache *c, *proxy;
858
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100859 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000860 goto skip;
861
862 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100863 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000864 c->mfc_mcastgrp == mcastgrp) {
865 if (c->mfc_un.res.ttls[vifi] < 255)
866 return c;
867
868 /* It's ok if the vifi is part of the static tree */
869 proxy = ipmr_cache_find_any_parent(mrt,
870 c->mfc_parent);
871 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
872 return c;
873 }
874
875skip:
876 return ipmr_cache_find_any_parent(mrt, vifi);
877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879/*
880 * Allocate a multicast cache entry
881 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000882static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Jianjun Kongc354e122008-11-03 00:28:02 -0800884 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000885
886 if (c)
887 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return c;
889}
890
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000891static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Jianjun Kongc354e122008-11-03 00:28:02 -0800893 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000894
895 if (c) {
896 skb_queue_head_init(&c->mfc_un.unres.unresolved);
897 c->mfc_un.unres.expires = jiffies + 10*HZ;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return c;
900}
901
902/*
903 * A cache entry has gone into a resolved state from queued
904 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900905
Patrick McHardy0c122952010-04-13 05:03:22 +0000906static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
907 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700910 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000912 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Jianjun Kongc354e122008-11-03 00:28:02 -0800914 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700915 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
917
Hong zhi guo573ce262013-03-27 06:47:04 +0000918 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000919 nlh->nlmsg_len = skb_tail_pointer(skb) -
920 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 } else {
922 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000923 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000925 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700926 e->error = -EMSGSIZE;
927 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Thomas Graf2942e902006-08-15 00:30:25 -0700929
Eric W. Biederman15e47302012-09-07 20:12:54 +0000930 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000931 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000932 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935}
936
937/*
938 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
939 * expects the following bizarre scheme.
940 *
941 * Called under mrt_lock.
942 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900943
Patrick McHardy0c122952010-04-13 05:03:22 +0000944static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000945 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300948 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 struct igmphdr *igmp;
950 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000951 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 int ret;
953
954#ifdef CONFIG_IP_PIMSM
955 if (assert == IGMPMSG_WHOLEPKT)
956 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
957 else
958#endif
959 skb = alloc_skb(128, GFP_ATOMIC);
960
Stephen Hemminger132adf52007-03-08 20:44:43 -0800961 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -ENOBUFS;
963
964#ifdef CONFIG_IP_PIMSM
965 if (assert == IGMPMSG_WHOLEPKT) {
966 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000967 * Duplicate old header, fix ihl, length etc.
968 * And all this only to mangle msg->im_msgtype and
969 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300971 skb_push(skb, sizeof(struct iphdr));
972 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300973 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300974 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700975 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 msg->im_msgtype = IGMPMSG_WHOLEPKT;
977 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000978 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700979 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
980 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
981 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900982 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900984 {
985
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000986 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Cong Wang30f3a402013-06-05 20:14:10 +0800988 skb_set_network_header(skb, skb->len);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300989 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300990 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000991 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700992 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000994 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000996 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000998 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 igmp->type =
1000 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001001 igmp->code = 0;
1002 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001003 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Eric Dumazet4c968702010-10-01 16:15:01 +00001006 rcu_read_lock();
1007 mroute_sk = rcu_dereference(mrt->mroute_sk);
1008 if (mroute_sk == NULL) {
1009 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 kfree_skb(skb);
1011 return -EINVAL;
1012 }
1013
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001014 /* Deliver to mrouted */
1015
Eric Dumazet4c968702010-10-01 16:15:01 +00001016 ret = sock_queue_rcv_skb(mroute_sk, skb);
1017 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001018 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001019 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 kfree_skb(skb);
1021 }
1022
1023 return ret;
1024}
1025
1026/*
1027 * Queue a packet for resolution. It gets locked cache entry!
1028 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001031ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Patrick McHardy862465f2010-04-13 05:03:21 +00001033 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int err;
1035 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001036 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001039 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001040 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001041 c->mfc_origin == iph->saddr) {
1042 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
Patrick McHardy862465f2010-04-13 05:03:21 +00001047 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001048 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Patrick McHardy0c122952010-04-13 05:03:22 +00001050 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001051 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 spin_unlock_bh(&mfc_unres_lock);
1053
1054 kfree_skb(skb);
1055 return -ENOBUFS;
1056 }
1057
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001058 /* Fill in the new cache entry */
1059
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001060 c->mfc_parent = -1;
1061 c->mfc_origin = iph->saddr;
1062 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001064 /* Reflect first query at mrouted. */
1065
Patrick McHardy0c122952010-04-13 05:03:22 +00001066 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001067 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001068 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 out - Brad Parker
1070 */
1071 spin_unlock_bh(&mfc_unres_lock);
1072
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001073 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 kfree_skb(skb);
1075 return err;
1076 }
1077
Patrick McHardy0c122952010-04-13 05:03:22 +00001078 atomic_inc(&mrt->cache_resolve_queue_len);
1079 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001080 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
David S. Miller278554b2010-05-12 00:05:35 -07001082 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1083 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001086 /* See if we can append the packet */
1087
1088 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 kfree_skb(skb);
1090 err = -ENOBUFS;
1091 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001092 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 err = 0;
1094 }
1095
1096 spin_unlock_bh(&mfc_unres_lock);
1097 return err;
1098}
1099
1100/*
1101 * MFC cache manipulation by user space mroute daemon
1102 */
1103
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001104static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001107 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Jianjun Kongc354e122008-11-03 00:28:02 -08001109 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Patrick McHardy0c122952010-04-13 05:03:22 +00001111 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001113 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1114 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001115 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001116 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001117 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
1119 }
1120 }
1121 return -ENOENT;
1122}
1123
Patrick McHardy0c122952010-04-13 05:03:22 +00001124static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001125 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Patrick McHardy862465f2010-04-13 05:03:21 +00001127 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001129 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Patrick McHardya50436f22010-03-17 06:04:14 +00001131 if (mfc->mfcc_parent >= MAXVIFS)
1132 return -ENFILE;
1133
Jianjun Kongc354e122008-11-03 00:28:02 -08001134 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Patrick McHardy0c122952010-04-13 05:03:22 +00001136 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001138 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1139 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001140 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
Patrick McHardy862465f2010-04-13 05:03:21 +00001145 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 write_lock_bh(&mrt_lock);
1147 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001148 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (!mrtsock)
1150 c->mfc_flags |= MFC_STATIC;
1151 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001152 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return 0;
1154 }
1155
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001156 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001157 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return -EINVAL;
1159
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001160 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001161 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -ENOMEM;
1163
Jianjun Kongc354e122008-11-03 00:28:02 -08001164 c->mfc_origin = mfc->mfcc_origin.s_addr;
1165 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1166 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001167 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!mrtsock)
1169 c->mfc_flags |= MFC_STATIC;
1170
Eric Dumazeta8c94862010-10-01 16:15:08 +00001171 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /*
1174 * Check to see if we resolved a queued list. If so we
1175 * need to send on the frames and tidy up.
1176 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001177 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001179 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001180 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001182 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001183 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001184 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
1186 }
1187 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001188 if (list_empty(&mrt->mfc_unres_queue))
1189 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 spin_unlock_bh(&mfc_unres_lock);
1191
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001192 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001193 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001194 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001196 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return 0;
1198}
1199
1200/*
1201 * Close the multicast socket, and clear the vif tables etc
1202 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001203
Patrick McHardy0c122952010-04-13 05:03:22 +00001204static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001207 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001208 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001209
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001210 /* Shut down all active vif entries */
1211
Patrick McHardy0c122952010-04-13 05:03:22 +00001212 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001213 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001214 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001216 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001218 /* Wipe the cache */
1219
Patrick McHardy862465f2010-04-13 05:03:21 +00001220 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001221 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001222 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001224 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001225 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001226 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228 }
1229
Patrick McHardy0c122952010-04-13 05:03:22 +00001230 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001232 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001233 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001234 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001235 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 spin_unlock_bh(&mfc_unres_lock);
1238 }
1239}
1240
Eric Dumazet4c968702010-10-01 16:15:01 +00001241/* called from ip_ra_control(), before an RCU grace period,
1242 * we dont need to call synchronize_rcu() here
1243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244static void mrtsock_destruct(struct sock *sk)
1245{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001246 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001247 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001250 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001251 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001252 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001253 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1254 NETCONFA_IFINDEX_ALL,
1255 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001256 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001257 mroute_clean_tables(mrt);
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 rtnl_unlock();
1261}
1262
1263/*
1264 * Socket options and virtual interface manipulation. The whole
1265 * virtual interface system is a complete heap, but unfortunately
1266 * that's how BSD mrouted happens to think. Maybe one day with a proper
1267 * MOSPF/PIM router set up we can clean this up.
1268 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001269
David S. Millerb7058842009-09-30 16:12:20 -07001270int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001272 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct vifctl vif;
1274 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001275 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001276 struct mr_table *mrt;
1277
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001278 if (sk->sk_type != SOCK_RAW ||
1279 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1280 return -EOPNOTSUPP;
1281
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001282 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1283 if (mrt == NULL)
1284 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001285
Stephen Hemminger132adf52007-03-08 20:44:43 -08001286 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001287 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001288 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return -EACCES;
1290 }
1291
Stephen Hemminger132adf52007-03-08 20:44:43 -08001292 switch (optname) {
1293 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001294 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001298 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001300 return -EADDRINUSE;
1301 }
1302
1303 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1304 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001305 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001306 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001307 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1308 NETCONFA_IFINDEX_ALL,
1309 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001310 }
1311 rtnl_unlock();
1312 return ret;
1313 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001314 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001315 return -EACCES;
1316 return ip_ra_control(sk, 0, NULL);
1317 case MRT_ADD_VIF:
1318 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001319 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001320 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001321 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001322 return -EFAULT;
1323 if (vif.vifc_vifi >= MAXVIFS)
1324 return -ENFILE;
1325 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001326 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001327 ret = vif_add(net, mrt, &vif,
1328 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001329 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001330 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001331 }
1332 rtnl_unlock();
1333 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /*
1336 * Manipulate the forwarding caches. These live
1337 * in a sort of kernel/user symbiosis.
1338 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 case MRT_ADD_MFC:
1340 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001341 parent = -1;
1342 case MRT_ADD_MFC_PROXY:
1343 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001344 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001345 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001346 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001347 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001348 if (parent == 0)
1349 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001350 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001351 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1352 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001353 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001354 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001355 sk == rtnl_dereference(mrt->mroute_sk),
1356 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001357 rtnl_unlock();
1358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /*
1360 * Control PIM assert.
1361 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001362 case MRT_ASSERT:
1363 {
1364 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001365 if (optlen != sizeof(v))
1366 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001367 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001368 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001369 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001370 return 0;
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001373 case MRT_PIM:
1374 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001375 int v;
1376
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001377 if (optlen != sizeof(v))
1378 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001379 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001380 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001381 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001382
Stephen Hemminger132adf52007-03-08 20:44:43 -08001383 rtnl_lock();
1384 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001385 if (v != mrt->mroute_do_pim) {
1386 mrt->mroute_do_pim = v;
1387 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001389 rtnl_unlock();
1390 return ret;
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001393#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1394 case MRT_TABLE:
1395 {
1396 u32 v;
1397
1398 if (optlen != sizeof(u32))
1399 return -EINVAL;
1400 if (get_user(v, (u32 __user *)optval))
1401 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001402
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001403 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1404 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1405 return -EINVAL;
1406
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001407 rtnl_lock();
1408 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001409 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1410 ret = -EBUSY;
1411 } else {
1412 if (!ipmr_new_table(net, v))
1413 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001414 else
1415 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001416 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001417 rtnl_unlock();
1418 return ret;
1419 }
1420#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001421 /*
1422 * Spurious command, or MRT_VERSION which you cannot
1423 * set.
1424 */
1425 default:
1426 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428}
1429
1430/*
1431 * Getsock opt support for the multicast routing system.
1432 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001433
Jianjun Kongc354e122008-11-03 00:28:02 -08001434int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 int olr;
1437 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001438 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001439 struct mr_table *mrt;
1440
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001441 if (sk->sk_type != SOCK_RAW ||
1442 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1443 return -EOPNOTSUPP;
1444
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001445 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1446 if (mrt == NULL)
1447 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Jianjun Kongc354e122008-11-03 00:28:02 -08001449 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001451 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001453 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -ENOPROTOOPT;
1455
1456 if (get_user(olr, optlen))
1457 return -EFAULT;
1458
1459 olr = min_t(unsigned int, olr, sizeof(int));
1460 if (olr < 0)
1461 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001462
Jianjun Kongc354e122008-11-03 00:28:02 -08001463 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001465 if (optname == MRT_VERSION)
1466 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001468 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001469 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470#endif
1471 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001472 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001473 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -EFAULT;
1475 return 0;
1476}
1477
1478/*
1479 * The IP multicast ioctl support routines.
1480 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1483{
1484 struct sioc_sg_req sr;
1485 struct sioc_vif_req vr;
1486 struct vif_device *vif;
1487 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001488 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001489 struct mr_table *mrt;
1490
1491 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1492 if (mrt == NULL)
1493 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001494
Stephen Hemminger132adf52007-03-08 20:44:43 -08001495 switch (cmd) {
1496 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001497 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001499 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001500 return -EINVAL;
1501 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001502 vif = &mrt->vif_table[vr.vifi];
1503 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001504 vr.icount = vif->pkt_in;
1505 vr.ocount = vif->pkt_out;
1506 vr.ibytes = vif->bytes_in;
1507 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001509
Jianjun Kongc354e122008-11-03 00:28:02 -08001510 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001512 return 0;
1513 }
1514 read_unlock(&mrt_lock);
1515 return -EADDRNOTAVAIL;
1516 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001517 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001518 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Eric Dumazeta8c94862010-10-01 16:15:08 +00001520 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001521 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522 if (c) {
1523 sr.pktcnt = c->mfc_un.res.pkt;
1524 sr.bytecnt = c->mfc_un.res.bytes;
1525 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001526 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001527
Jianjun Kongc354e122008-11-03 00:28:02 -08001528 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001529 return -EFAULT;
1530 return 0;
1531 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001532 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001533 return -EADDRNOTAVAIL;
1534 default:
1535 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537}
1538
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001539#ifdef CONFIG_COMPAT
1540struct compat_sioc_sg_req {
1541 struct in_addr src;
1542 struct in_addr grp;
1543 compat_ulong_t pktcnt;
1544 compat_ulong_t bytecnt;
1545 compat_ulong_t wrong_if;
1546};
1547
David S. Millerca6b8bb02011-02-03 17:24:28 -08001548struct compat_sioc_vif_req {
1549 vifi_t vifi; /* Which iface */
1550 compat_ulong_t icount;
1551 compat_ulong_t ocount;
1552 compat_ulong_t ibytes;
1553 compat_ulong_t obytes;
1554};
1555
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001556int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1557{
David S. Miller0033d5a2011-02-03 17:21:31 -08001558 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001559 struct compat_sioc_vif_req vr;
1560 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001561 struct mfc_cache *c;
1562 struct net *net = sock_net(sk);
1563 struct mr_table *mrt;
1564
1565 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1566 if (mrt == NULL)
1567 return -ENOENT;
1568
1569 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001570 case SIOCGETVIFCNT:
1571 if (copy_from_user(&vr, arg, sizeof(vr)))
1572 return -EFAULT;
1573 if (vr.vifi >= mrt->maxvif)
1574 return -EINVAL;
1575 read_lock(&mrt_lock);
1576 vif = &mrt->vif_table[vr.vifi];
1577 if (VIF_EXISTS(mrt, vr.vifi)) {
1578 vr.icount = vif->pkt_in;
1579 vr.ocount = vif->pkt_out;
1580 vr.ibytes = vif->bytes_in;
1581 vr.obytes = vif->bytes_out;
1582 read_unlock(&mrt_lock);
1583
1584 if (copy_to_user(arg, &vr, sizeof(vr)))
1585 return -EFAULT;
1586 return 0;
1587 }
1588 read_unlock(&mrt_lock);
1589 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001590 case SIOCGETSGCNT:
1591 if (copy_from_user(&sr, arg, sizeof(sr)))
1592 return -EFAULT;
1593
1594 rcu_read_lock();
1595 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1596 if (c) {
1597 sr.pktcnt = c->mfc_un.res.pkt;
1598 sr.bytecnt = c->mfc_un.res.bytes;
1599 sr.wrong_if = c->mfc_un.res.wrong_if;
1600 rcu_read_unlock();
1601
1602 if (copy_to_user(arg, &sr, sizeof(sr)))
1603 return -EFAULT;
1604 return 0;
1605 }
1606 rcu_read_unlock();
1607 return -EADDRNOTAVAIL;
1608 default:
1609 return -ENOIOCTLCMD;
1610 }
1611}
1612#endif
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1616{
Jiri Pirko351638e2013-05-28 01:30:21 +00001617 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001618 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001619 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct vif_device *v;
1621 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (event != NETDEV_UNREGISTER)
1624 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001625
1626 ipmr_for_each_table(mrt, net) {
1627 v = &mrt->vif_table[0];
1628 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1629 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001630 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633 return NOTIFY_DONE;
1634}
1635
1636
Jianjun Kongc354e122008-11-03 00:28:02 -08001637static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 .notifier_call = ipmr_device_event,
1639};
1640
1641/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001642 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * This avoids tunnel drivers and other mess and gives us the speed so
1644 * important for multicast video.
1645 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001646
Al Viro114c7842006-09-27 18:39:29 -07001647static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001649 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001650 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001651
1652 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001653 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001654 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001655 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001657 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001658 iph->tos = old_iph->tos;
1659 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 iph->frag_off = 0;
1661 iph->daddr = daddr;
1662 iph->saddr = saddr;
1663 iph->protocol = IPPROTO_IPIP;
1664 iph->ihl = 5;
1665 iph->tot_len = htons(skb->len);
Eric Dumazet73f156a2014-06-02 05:26:03 -07001666 ip_select_ident(skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 ip_send_check(iph);
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1670 nf_reset(skb);
1671}
1672
1673static inline int ipmr_forward_finish(struct sk_buff *skb)
1674{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001675 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Eric Dumazetadf30902009-06-02 05:19:30 +00001677 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Vincent Bernat2d8dbb02012-06-05 03:41:42 +00001678 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (unlikely(opt->optlen))
1681 ip_forward_options(skb);
1682
1683 return dst_output(skb);
1684}
1685
1686/*
1687 * Processing handlers for ipmr_forward
1688 */
1689
Patrick McHardy0c122952010-04-13 05:03:22 +00001690static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1691 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001693 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001694 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 struct net_device *dev;
1696 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001697 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 int encap = 0;
1699
1700 if (vif->dev == NULL)
1701 goto out_free;
1702
1703#ifdef CONFIG_IP_PIMSM
1704 if (vif->flags & VIFF_REGISTER) {
1705 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001706 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001707 vif->dev->stats.tx_bytes += skb->len;
1708 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001709 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001710 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712#endif
1713
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001714 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001715 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001716 vif->remote, vif->local,
1717 0, 0,
1718 IPPROTO_IPIP,
1719 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001720 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto out_free;
1722 encap = sizeof(struct iphdr);
1723 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001724 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001725 0, 0,
1726 IPPROTO_IPIP,
1727 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001728 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto out_free;
1730 }
1731
Changli Gaod8d1f302010-06-10 23:31:35 -07001732 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Changli Gaod8d1f302010-06-10 23:31:35 -07001734 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001736 * allow to send ICMP, so that packets will disappear
1737 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
1739
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001740 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 ip_rt_put(rt);
1742 goto out_free;
1743 }
1744
Changli Gaod8d1f302010-06-10 23:31:35 -07001745 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001748 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 goto out_free;
1750 }
1751
1752 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001753 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Eric Dumazetadf30902009-06-02 05:19:30 +00001755 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001756 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001757 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001760 * What do we do with netfilter? -- RR
1761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (vif->flags & VIFF_TUNNEL) {
1763 ip_encap(skb, vif->local, vif->remote);
1764 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001765 vif->dev->stats.tx_packets++;
1766 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768
1769 IPCB(skb)->flags |= IPSKB_FORWARDED;
1770
1771 /*
1772 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1773 * not only before forwarding, but after forwarding on all output
1774 * interfaces. It is clear, if mrouter runs a multicasting
1775 * program, it should receive packets not depending to what interface
1776 * program is joined.
1777 * If we will not make it, the program will have to join on all
1778 * interfaces. On the other hand, multihoming host (or router, but
1779 * not mrouter) cannot join to more than one interface - it will
1780 * result in receiving multiple packets.
1781 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001782 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 ipmr_forward_finish);
1784 return;
1785
1786out_free:
1787 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Patrick McHardy0c122952010-04-13 05:03:22 +00001790static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001793
1794 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1795 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 break;
1797 }
1798 return ct;
1799}
1800
1801/* "local" means that we should preserve one skb (for local delivery) */
1802
Rami Rosenc4854ec2013-07-20 15:09:28 +03001803static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1804 struct sk_buff *skb, struct mfc_cache *cache,
1805 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 int psend = -1;
1808 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001809 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 vif = cache->mfc_parent;
1812 cache->mfc_un.res.pkt++;
1813 cache->mfc_un.res.bytes += skb->len;
1814
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001815 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001816 struct mfc_cache *cache_proxy;
1817
1818 /* For an (*,G) entry, we only check that the incomming
1819 * interface is part of the static tree.
1820 */
1821 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1822 if (cache_proxy &&
1823 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1824 goto forward;
1825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /*
1828 * Wrong interface: drop packet and (maybe) send PIM assert.
1829 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001830 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001831 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001833 * Very complicated situation...
1834 *
1835 * The best workaround until routing daemons will be
1836 * fixed is not to redistribute packet, if it was
1837 * send through wrong interface. It means, that
1838 * multicast applications WILL NOT work for
1839 * (S,G), which have default multicast route pointing
1840 * to wrong oif. In any case, it is not a good
1841 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 */
1843 goto dont_forward;
1844 }
1845
1846 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Patrick McHardy0c122952010-04-13 05:03:22 +00001848 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001850 * so that we cannot check that packet arrived on an oif.
1851 * It is bad, but otherwise we would need to move pretty
1852 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001854 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001855 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001856 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1858 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001859 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861 goto dont_forward;
1862 }
1863
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001864forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001865 mrt->vif_table[vif].pkt_in++;
1866 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 /*
1869 * Forward the frame
1870 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001871 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1872 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001873 if (true_vifi >= 0 &&
1874 true_vifi != cache->mfc_parent &&
1875 ip_hdr(skb)->ttl >
1876 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1877 /* It's an (*,*) entry and the packet is not coming from
1878 * the upstream: forward the packet to the upstream
1879 * only.
1880 */
1881 psend = cache->mfc_parent;
1882 goto last_forward;
1883 }
1884 goto dont_forward;
1885 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001886 for (ct = cache->mfc_un.res.maxvif - 1;
1887 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001888 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001889 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1890 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001891 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (psend != -1) {
1893 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001896 ipmr_queue_xmit(net, mrt, skb2, cache,
1897 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001899 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001902last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (psend != -1) {
1904 if (local) {
1905 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001908 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001910 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001911 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 }
1914
1915dont_forward:
1916 if (!local)
1917 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
David S. Miller417da662011-05-03 19:42:43 -07001920static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001921{
David S. Miller417da662011-05-03 19:42:43 -07001922 struct rtable *rt = skb_rtable(skb);
1923 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001924 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001925 .daddr = iph->daddr,
1926 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001927 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001928 .flowi4_oif = (rt_is_output_route(rt) ?
1929 skb->dev->ifindex : 0),
1930 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001931 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001932 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001933 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001934 };
1935 struct mr_table *mrt;
1936 int err;
1937
David S. Millerda919812011-03-12 02:04:50 -05001938 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001939 if (err)
1940 return ERR_PTR(err);
1941 return mrt;
1942}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944/*
1945 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001946 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
1948
1949int ip_mr_input(struct sk_buff *skb)
1950{
1951 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001952 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001953 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001954 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001957 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001959 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto dont_forward;
1961
David S. Miller417da662011-05-03 19:42:43 -07001962 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001963 if (IS_ERR(mrt)) {
1964 kfree_skb(skb);
1965 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001968 if (IPCB(skb)->opt.router_alert) {
1969 if (ip_call_ra_chain(skb))
1970 return 0;
1971 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1972 /* IGMPv1 (and broken IGMPv2 implementations sort of
1973 * Cisco IOS <= 11.2(8)) do not put router alert
1974 * option to IGMP packets destined to routable
1975 * groups. It is very bad, because it means
1976 * that we can forward NO IGMP messages.
1977 */
1978 struct sock *mroute_sk;
1979
1980 mroute_sk = rcu_dereference(mrt->mroute_sk);
1981 if (mroute_sk) {
1982 nf_reset(skb);
1983 raw_rcv(mroute_sk, skb);
1984 return 0;
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 }
1988
Eric Dumazeta8c94862010-10-01 16:15:08 +00001989 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001990 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001991 if (cache == NULL) {
1992 int vif = ipmr_find_vif(mrt, skb->dev);
1993
1994 if (vif >= 0)
1995 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1996 vif);
1997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 /*
2000 * No usable cache entry
2001 */
Jianjun Kongc354e122008-11-03 00:28:02 -08002002 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 int vif;
2004
2005 if (local) {
2006 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2007 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002008 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 skb = skb2;
2011 }
2012
Eric Dumazeta8c94862010-10-01 16:15:08 +00002013 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002014 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002016 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 read_unlock(&mrt_lock);
2018
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002019 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 read_unlock(&mrt_lock);
2022 kfree_skb(skb);
2023 return -ENODEV;
2024 }
2025
Eric Dumazeta8c94862010-10-01 16:15:08 +00002026 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002027 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 read_unlock(&mrt_lock);
2029
2030 if (local)
2031 return ip_local_deliver(skb);
2032
2033 return 0;
2034
2035dont_forward:
2036 if (local)
2037 return ip_local_deliver(skb);
2038 kfree_skb(skb);
2039 return 0;
2040}
2041
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002042#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00002043/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002044static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2045 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002047 struct net_device *reg_dev = NULL;
2048 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002050 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002052 * Check that:
2053 * a. packet is really sent to a multicast group
2054 * b. packet is not a NULL-REGISTER
2055 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002057 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002059 ntohs(encap->tot_len) + pimlen > skb->len)
2060 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002063 if (mrt->mroute_reg_vif_num >= 0)
2064 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 read_unlock(&mrt_lock);
2066
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002067 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002068 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002070 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00002071 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03002072 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00002074 skb->ip_summed = CHECKSUM_NONE;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002075
Nicolas Dichtelea231922013-09-02 15:34:58 +02002076 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002079
Eric Dumazet55747a02010-10-01 16:14:55 +00002080 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002081}
2082#endif
2083
2084#ifdef CONFIG_IP_PIMSM_V1
2085/*
2086 * Handle IGMP messages of PIMv1
2087 */
2088
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002089int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002090{
2091 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002092 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002093 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002094
2095 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2096 goto drop;
2097
2098 pim = igmp_hdr(skb);
2099
David S. Miller417da662011-05-03 19:42:43 -07002100 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002101 if (IS_ERR(mrt))
2102 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002103 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002104 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2105 goto drop;
2106
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002107 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002108drop:
2109 kfree_skb(skb);
2110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return 0;
2112}
2113#endif
2114
2115#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002116static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002119 struct net *net = dev_net(skb->dev);
2120 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002122 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 goto drop;
2124
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002125 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002126 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2127 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002128 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002129 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 goto drop;
2131
David S. Miller417da662011-05-03 19:42:43 -07002132 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002133 if (IS_ERR(mrt))
2134 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002135 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002136drop:
2137 kfree_skb(skb);
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return 0;
2140}
2141#endif
2142
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002143static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2144 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 int ct;
2147 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002148 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002149 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Nicolas Dichtel74381892010-03-25 23:45:35 +00002151 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002152 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002153 return -ENOENT;
2154
Thomas Graf92a395e2012-06-26 23:36:13 +00002155 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2156 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2157 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Thomas Graf92a395e2012-06-26 23:36:13 +00002159 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2160 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002163 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002164 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2165 nla_nest_cancel(skb, mp_attr);
2166 return -EMSGSIZE;
2167 }
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 nhp->rtnh_flags = 0;
2170 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002171 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 nhp->rtnh_len = sizeof(*nhp);
2173 }
2174 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002175
2176 nla_nest_end(skb, mp_attr);
2177
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002178 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2179 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2180 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2181 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2182 return -EMSGSIZE;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 rtm->rtm_type = RTN_MULTICAST;
2185 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
David S. Miller9a1b9492011-05-04 12:18:54 -07002188int ipmr_get_route(struct net *net, struct sk_buff *skb,
2189 __be32 saddr, __be32 daddr,
2190 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002193 struct mr_table *mrt;
2194 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002196 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2197 if (mrt == NULL)
2198 return -ENOENT;
2199
Eric Dumazeta8c94862010-10-01 16:15:08 +00002200 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002201 cache = ipmr_cache_find(mrt, saddr, daddr);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002202 if (cache == NULL && skb->dev) {
2203 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002205 if (vif >= 0)
2206 cache = ipmr_cache_find_any(mrt, daddr, vif);
2207 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002208 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002209 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002210 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002212 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002215 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return -EAGAIN;
2217 }
2218
2219 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002220 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002221 if (dev)
2222 vif = ipmr_find_vif(mrt, dev);
2223 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002225 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return -ENODEV;
2227 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002228 skb2 = skb_clone(skb, GFP_ATOMIC);
2229 if (!skb2) {
2230 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002231 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002232 return -ENOMEM;
2233 }
2234
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002235 skb_push(skb2, sizeof(struct iphdr));
2236 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002237 iph = ip_hdr(skb2);
2238 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002239 iph->saddr = saddr;
2240 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002241 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002242 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002244 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return err;
2246 }
2247
Eric Dumazeta8c94862010-10-01 16:15:08 +00002248 read_lock(&mrt_lock);
2249 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002251 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002253 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return err;
2255}
2256
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002257static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002258 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2259 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002260{
2261 struct nlmsghdr *nlh;
2262 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002263 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002264
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002265 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002266 if (nlh == NULL)
2267 return -EMSGSIZE;
2268
2269 rtm = nlmsg_data(nlh);
2270 rtm->rtm_family = RTNL_FAMILY_IPMR;
2271 rtm->rtm_dst_len = 32;
2272 rtm->rtm_src_len = 32;
2273 rtm->rtm_tos = 0;
2274 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002275 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2276 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002277 rtm->rtm_type = RTN_MULTICAST;
2278 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002279 if (c->mfc_flags & MFC_STATIC)
2280 rtm->rtm_protocol = RTPROT_STATIC;
2281 else
2282 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002283 rtm->rtm_flags = 0;
2284
David S. Millerf3756b72012-04-01 20:39:02 -04002285 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2286 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2287 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002288 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2289 /* do not break the dump if cache is unresolved */
2290 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002291 goto nla_put_failure;
2292
2293 return nlmsg_end(skb, nlh);
2294
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}