blob: 56d079b63ad3fe552db8dab66e9e18ae9edf2b3c [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
Patrick McHardy0c122952010-04-13 05:03:22 +0000130static int ip_mr_forward(struct net *net, struct mr_table *mrt,
131 struct sk_buff *skb, struct mfc_cache *cache,
132 int local);
133static 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 Sowad6489022014-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);
431 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (dev_open(dev))
434 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700435 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437 }
438 return dev;
439
440failure:
441 /* allow the register to be completed before unregistering. */
442 rtnl_unlock();
443 rtnl_lock();
444
445 unregister_netdevice(dev);
446 return NULL;
447}
448
449#ifdef CONFIG_IP_PIMSM
450
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000451static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000453 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000454 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500455 struct flowi4 fl4 = {
456 .flowi4_oif = dev->ifindex,
457 .flowi4_iif = skb->skb_iif,
458 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000459 };
460 int err;
461
David S. Millerda919812011-03-12 02:04:50 -0500462 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000463 if (err < 0) {
464 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000465 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000466 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700469 dev->stats.tx_bytes += skb->len;
470 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000471 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 read_unlock(&mrt_lock);
473 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000474 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Stephen Hemminger007c3832008-11-20 20:28:35 -0800477static const struct net_device_ops reg_vif_netdev_ops = {
478 .ndo_start_xmit = reg_vif_xmit,
479};
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static void reg_vif_setup(struct net_device *dev)
482{
483 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800484 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800486 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700488 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000491static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct net_device *dev;
494 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000495 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000497 if (mrt->id == RT_TABLE_DEFAULT)
498 sprintf(name, "pimreg");
499 else
500 sprintf(name, "pimreg%u", mrt->id);
501
502 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (dev == NULL)
505 return NULL;
506
Tom Goff403dbb92009-06-14 03:16:13 -0700507 dev_net_set(dev, net);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (register_netdevice(dev)) {
510 free_netdev(dev);
511 return NULL;
512 }
513 dev->iflink = 0;
514
Herbert Xu71e27da2007-06-04 23:36:06 -0700515 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000516 in_dev = __in_dev_get_rcu(dev);
517 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700518 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Herbert Xu71e27da2007-06-04 23:36:06 -0700522 ipv4_devconf_setall(in_dev);
523 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
524 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (dev_open(dev))
527 goto failure;
528
Wang Chen7dc00c82008-07-14 20:56:34 -0700529 dev_hold(dev);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return dev;
532
533failure:
534 /* allow the register to be completed before unregistering. */
535 rtnl_unlock();
536 rtnl_lock();
537
538 unregister_netdevice(dev);
539 return NULL;
540}
541#endif
542
Ben Hutchings2c530402012-07-10 10:55:09 +0000543/**
544 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700545 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900547
Patrick McHardy0c122952010-04-13 05:03:22 +0000548static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000549 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct vif_device *v;
552 struct net_device *dev;
553 struct in_device *in_dev;
554
Patrick McHardy0c122952010-04-13 05:03:22 +0000555 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -EADDRNOTAVAIL;
557
Patrick McHardy0c122952010-04-13 05:03:22 +0000558 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 write_lock_bh(&mrt_lock);
561 dev = v->dev;
562 v->dev = NULL;
563
564 if (!dev) {
565 write_unlock_bh(&mrt_lock);
566 return -EADDRNOTAVAIL;
567 }
568
569#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000570 if (vifi == mrt->mroute_reg_vif_num)
571 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#endif
573
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000574 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000576
577 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000578 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000581 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
584 write_unlock_bh(&mrt_lock);
585
586 dev_set_allmulti(dev, -1);
587
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000588 in_dev = __in_dev_get_rtnl(dev);
589 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700590 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000591 inet_netconf_notify_devconf(dev_net(dev),
592 NETCONFA_MC_FORWARDING,
593 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 ip_rt_multicast_event(in_dev);
595 }
596
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000597 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000598 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 dev_put(dev);
601 return 0;
602}
603
Eric Dumazeta8c94862010-10-01 16:15:08 +0000604static void ipmr_cache_free_rcu(struct rcu_head *head)
605{
606 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
607
608 kmem_cache_free(mrt_cachep, c);
609}
610
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000611static inline void ipmr_cache_free(struct mfc_cache *c)
612{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000613 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000617 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
619
Patrick McHardy0c122952010-04-13 05:03:22 +0000620static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200622 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700624 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Patrick McHardy0c122952010-04-13 05:03:22 +0000626 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Jianjun Kongc354e122008-11-03 00:28:02 -0800628 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700629 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
631 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000632 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000634 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700635 e->error = -ETIMEDOUT;
636 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700637
Eric W. Biederman15e47302012-09-07 20:12:54 +0000638 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000639 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000644 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647
Patrick McHardye258beb2010-04-13 05:03:19 +0000648/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Patrick McHardye258beb2010-04-13 05:03:19 +0000650static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Patrick McHardy0c122952010-04-13 05:03:22 +0000652 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 unsigned long now;
654 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000655 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000658 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return;
660 }
661
Patrick McHardy0c122952010-04-13 05:03:22 +0000662 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto out;
664
665 now = jiffies;
666 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Patrick McHardy0c122952010-04-13 05:03:22 +0000668 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (time_after(c->mfc_un.unres.expires, now)) {
670 unsigned long interval = c->mfc_un.unres.expires - now;
671 if (interval < expires)
672 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 continue;
674 }
675
Patrick McHardy862465f2010-04-13 05:03:21 +0000676 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000677 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000678 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
Patrick McHardy0c122952010-04-13 05:03:22 +0000681 if (!list_empty(&mrt->mfc_unres_queue))
682 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684out:
685 spin_unlock(&mfc_unres_lock);
686}
687
688/* Fill oifs list. It is called under write locked mrt_lock. */
689
Patrick McHardy0c122952010-04-13 05:03:22 +0000690static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000691 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 int vifi;
694
695 cache->mfc_un.res.minvif = MAXVIFS;
696 cache->mfc_un.res.maxvif = 0;
697 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
698
Patrick McHardy0c122952010-04-13 05:03:22 +0000699 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
700 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000701 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
703 if (cache->mfc_un.res.minvif > vifi)
704 cache->mfc_un.res.minvif = vifi;
705 if (cache->mfc_un.res.maxvif <= vifi)
706 cache->mfc_un.res.maxvif = vifi + 1;
707 }
708 }
709}
710
Patrick McHardy0c122952010-04-13 05:03:22 +0000711static int vif_add(struct net *net, struct mr_table *mrt,
712 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000715 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct net_device *dev;
717 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700718 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000721 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EADDRINUSE;
723
724 switch (vifc->vifc_flags) {
725#ifdef CONFIG_IP_PIMSM
726 case VIFF_REGISTER:
727 /*
728 * Special Purpose VIF in PIM
729 * All the packets will be sent to the daemon
730 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000731 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000733 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (!dev)
735 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700736 err = dev_set_allmulti(dev, 1);
737 if (err) {
738 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700739 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700740 return err;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900744 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000745 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!dev)
747 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700748 err = dev_set_allmulti(dev, 1);
749 if (err) {
750 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700751 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700752 return err;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000755
756 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000758 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
759 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000760 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000761 dev_put(dev);
762 return -EADDRNOTAVAIL;
763 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000764 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000765 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (!dev)
768 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700769 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700770 if (err) {
771 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700772 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 break;
775 default:
776 return -EINVAL;
777 }
778
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000779 in_dev = __in_dev_get_rtnl(dev);
780 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000781 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000783 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700784 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000785 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
786 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 ip_rt_multicast_event(in_dev);
788
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000789 /* Fill in the VIF structures */
790
Jianjun Kongc354e122008-11-03 00:28:02 -0800791 v->rate_limit = vifc->vifc_rate_limit;
792 v->local = vifc->vifc_lcl_addr.s_addr;
793 v->remote = vifc->vifc_rmt_addr.s_addr;
794 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!mrtsock)
796 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800797 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 v->bytes_in = 0;
799 v->bytes_out = 0;
800 v->pkt_in = 0;
801 v->pkt_out = 0;
802 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000803 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 v->link = dev->iflink;
805
806 /* And finish update writing critical data */
807 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800808 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000810 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000811 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000813 if (vifi+1 > mrt->maxvif)
814 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 write_unlock_bh(&mrt_lock);
816 return 0;
817}
818
Eric Dumazeta8c94862010-10-01 16:15:08 +0000819/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000820static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000821 __be32 origin,
822 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Jianjun Kongc354e122008-11-03 00:28:02 -0800824 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct mfc_cache *c;
826
Eric Dumazeta8c94862010-10-01 16:15:08 +0000827 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000828 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
829 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000831 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000834/* Look for a (*,*,oif) entry */
835static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
836 int vifi)
837{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100838 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000839 struct mfc_cache *c;
840
841 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100842 if (c->mfc_origin == htonl(INADDR_ANY) &&
843 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000844 c->mfc_un.res.ttls[vifi] < 255)
845 return c;
846
847 return NULL;
848}
849
850/* Look for a (*,G) entry */
851static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
852 __be32 mcastgrp, int vifi)
853{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100854 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000855 struct mfc_cache *c, *proxy;
856
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100857 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000858 goto skip;
859
860 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100861 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000862 c->mfc_mcastgrp == mcastgrp) {
863 if (c->mfc_un.res.ttls[vifi] < 255)
864 return c;
865
866 /* It's ok if the vifi is part of the static tree */
867 proxy = ipmr_cache_find_any_parent(mrt,
868 c->mfc_parent);
869 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
870 return c;
871 }
872
873skip:
874 return ipmr_cache_find_any_parent(mrt, vifi);
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * Allocate a multicast cache entry
879 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000880static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Jianjun Kongc354e122008-11-03 00:28:02 -0800882 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000883
884 if (c)
885 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return c;
887}
888
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000889static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Jianjun Kongc354e122008-11-03 00:28:02 -0800891 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000892
893 if (c) {
894 skb_queue_head_init(&c->mfc_un.unres.unresolved);
895 c->mfc_un.unres.expires = jiffies + 10*HZ;
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return c;
898}
899
900/*
901 * A cache entry has gone into a resolved state from queued
902 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900903
Patrick McHardy0c122952010-04-13 05:03:22 +0000904static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
905 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700908 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000910 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Jianjun Kongc354e122008-11-03 00:28:02 -0800912 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700913 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
915
Hong zhi guo573ce262013-03-27 06:47:04 +0000916 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000917 nlh->nlmsg_len = skb_tail_pointer(skb) -
918 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 } else {
920 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000921 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000923 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700924 e->error = -EMSGSIZE;
925 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Thomas Graf2942e902006-08-15 00:30:25 -0700927
Eric W. Biederman15e47302012-09-07 20:12:54 +0000928 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000929 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000930 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933}
934
935/*
936 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
937 * expects the following bizarre scheme.
938 *
939 * Called under mrt_lock.
940 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900941
Patrick McHardy0c122952010-04-13 05:03:22 +0000942static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000943 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300946 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct igmphdr *igmp;
948 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000949 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int ret;
951
952#ifdef CONFIG_IP_PIMSM
953 if (assert == IGMPMSG_WHOLEPKT)
954 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
955 else
956#endif
957 skb = alloc_skb(128, GFP_ATOMIC);
958
Stephen Hemminger132adf52007-03-08 20:44:43 -0800959 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -ENOBUFS;
961
962#ifdef CONFIG_IP_PIMSM
963 if (assert == IGMPMSG_WHOLEPKT) {
964 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000965 * Duplicate old header, fix ihl, length etc.
966 * And all this only to mangle msg->im_msgtype and
967 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300969 skb_push(skb, sizeof(struct iphdr));
970 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300971 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300972 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700973 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 msg->im_msgtype = IGMPMSG_WHOLEPKT;
975 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000976 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700977 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
978 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
979 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900980 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900982 {
983
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000984 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700986 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300987 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300988 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000989 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700990 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000992 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000994 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000996 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 igmp->type =
998 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000999 igmp->code = 0;
1000 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001001 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Eric Dumazet4c968702010-10-01 16:15:01 +00001004 rcu_read_lock();
1005 mroute_sk = rcu_dereference(mrt->mroute_sk);
1006 if (mroute_sk == NULL) {
1007 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 kfree_skb(skb);
1009 return -EINVAL;
1010 }
1011
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001012 /* Deliver to mrouted */
1013
Eric Dumazet4c968702010-10-01 16:15:01 +00001014 ret = sock_queue_rcv_skb(mroute_sk, skb);
1015 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001016 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001017 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 kfree_skb(skb);
1019 }
1020
1021 return ret;
1022}
1023
1024/*
1025 * Queue a packet for resolution. It gets locked cache entry!
1026 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001029ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Patrick McHardy862465f2010-04-13 05:03:21 +00001031 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int err;
1033 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001034 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001037 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001038 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001039 c->mfc_origin == iph->saddr) {
1040 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Patrick McHardy862465f2010-04-13 05:03:21 +00001045 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001046 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Patrick McHardy0c122952010-04-13 05:03:22 +00001048 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001049 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 spin_unlock_bh(&mfc_unres_lock);
1051
1052 kfree_skb(skb);
1053 return -ENOBUFS;
1054 }
1055
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001056 /* Fill in the new cache entry */
1057
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001058 c->mfc_parent = -1;
1059 c->mfc_origin = iph->saddr;
1060 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001062 /* Reflect first query at mrouted. */
1063
Patrick McHardy0c122952010-04-13 05:03:22 +00001064 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001065 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001066 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 out - Brad Parker
1068 */
1069 spin_unlock_bh(&mfc_unres_lock);
1070
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001071 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 kfree_skb(skb);
1073 return err;
1074 }
1075
Patrick McHardy0c122952010-04-13 05:03:22 +00001076 atomic_inc(&mrt->cache_resolve_queue_len);
1077 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001078 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
David S. Miller278554b2010-05-12 00:05:35 -07001080 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1081 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001084 /* See if we can append the packet */
1085
1086 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 kfree_skb(skb);
1088 err = -ENOBUFS;
1089 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001090 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 err = 0;
1092 }
1093
1094 spin_unlock_bh(&mfc_unres_lock);
1095 return err;
1096}
1097
1098/*
1099 * MFC cache manipulation by user space mroute daemon
1100 */
1101
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001102static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001105 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Jianjun Kongc354e122008-11-03 00:28:02 -08001107 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Patrick McHardy0c122952010-04-13 05:03:22 +00001109 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001111 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1112 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001113 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001114 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001115 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return 0;
1117 }
1118 }
1119 return -ENOENT;
1120}
1121
Patrick McHardy0c122952010-04-13 05:03:22 +00001122static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001123 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Patrick McHardy862465f2010-04-13 05:03:21 +00001125 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001127 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Patrick McHardya50436f22010-03-17 06:04:14 +00001129 if (mfc->mfcc_parent >= MAXVIFS)
1130 return -ENFILE;
1131
Jianjun Kongc354e122008-11-03 00:28:02 -08001132 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Patrick McHardy0c122952010-04-13 05:03:22 +00001134 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001136 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1137 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001138 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142
Patrick McHardy862465f2010-04-13 05:03:21 +00001143 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 write_lock_bh(&mrt_lock);
1145 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001146 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!mrtsock)
1148 c->mfc_flags |= MFC_STATIC;
1149 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001150 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 0;
1152 }
1153
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001154 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001155 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return -EINVAL;
1157
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001158 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001159 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -ENOMEM;
1161
Jianjun Kongc354e122008-11-03 00:28:02 -08001162 c->mfc_origin = mfc->mfcc_origin.s_addr;
1163 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1164 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001165 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!mrtsock)
1167 c->mfc_flags |= MFC_STATIC;
1168
Eric Dumazeta8c94862010-10-01 16:15:08 +00001169 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /*
1172 * Check to see if we resolved a queued list. If so we
1173 * need to send on the frames and tidy up.
1174 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001175 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001177 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001178 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001180 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001181 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001182 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
1184 }
1185 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001186 if (list_empty(&mrt->mfc_unres_queue))
1187 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 spin_unlock_bh(&mfc_unres_lock);
1189
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001190 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001191 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001192 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001194 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return 0;
1196}
1197
1198/*
1199 * Close the multicast socket, and clear the vif tables etc
1200 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001201
Patrick McHardy0c122952010-04-13 05:03:22 +00001202static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001205 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001206 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001207
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001208 /* Shut down all active vif entries */
1209
Patrick McHardy0c122952010-04-13 05:03:22 +00001210 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001211 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001212 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001214 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001216 /* Wipe the cache */
1217
Patrick McHardy862465f2010-04-13 05:03:21 +00001218 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001219 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001220 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001222 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001223 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001224 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 }
1227
Patrick McHardy0c122952010-04-13 05:03:22 +00001228 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001230 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001231 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001232 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001233 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 spin_unlock_bh(&mfc_unres_lock);
1236 }
1237}
1238
Eric Dumazet4c968702010-10-01 16:15:01 +00001239/* called from ip_ra_control(), before an RCU grace period,
1240 * we dont need to call synchronize_rcu() here
1241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242static void mrtsock_destruct(struct sock *sk)
1243{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001244 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001245 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001248 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001249 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001250 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001251 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1252 NETCONFA_IFINDEX_ALL,
1253 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001254 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001255 mroute_clean_tables(mrt);
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 rtnl_unlock();
1259}
1260
1261/*
1262 * Socket options and virtual interface manipulation. The whole
1263 * virtual interface system is a complete heap, but unfortunately
1264 * that's how BSD mrouted happens to think. Maybe one day with a proper
1265 * MOSPF/PIM router set up we can clean this up.
1266 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001267
David S. Millerb7058842009-09-30 16:12:20 -07001268int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001270 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct vifctl vif;
1272 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001273 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001274 struct mr_table *mrt;
1275
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001276 if (sk->sk_type != SOCK_RAW ||
1277 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1278 return -EOPNOTSUPP;
1279
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001280 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1281 if (mrt == NULL)
1282 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001283
Stephen Hemminger132adf52007-03-08 20:44:43 -08001284 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001285 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001286 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -EACCES;
1288 }
1289
Stephen Hemminger132adf52007-03-08 20:44:43 -08001290 switch (optname) {
1291 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001292 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Stephen Hemminger132adf52007-03-08 20:44:43 -08001295 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001296 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001298 return -EADDRINUSE;
1299 }
1300
1301 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1302 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001303 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001304 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001305 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1306 NETCONFA_IFINDEX_ALL,
1307 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001308 }
1309 rtnl_unlock();
1310 return ret;
1311 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001312 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001313 return -EACCES;
1314 return ip_ra_control(sk, 0, NULL);
1315 case MRT_ADD_VIF:
1316 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001317 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001319 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001320 return -EFAULT;
1321 if (vif.vifc_vifi >= MAXVIFS)
1322 return -ENFILE;
1323 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001324 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001325 ret = vif_add(net, mrt, &vif,
1326 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001327 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001328 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001329 }
1330 rtnl_unlock();
1331 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /*
1334 * Manipulate the forwarding caches. These live
1335 * in a sort of kernel/user symbiosis.
1336 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001337 case MRT_ADD_MFC:
1338 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001339 parent = -1;
1340 case MRT_ADD_MFC_PROXY:
1341 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001342 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001343 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001344 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001345 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001346 if (parent == 0)
1347 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001348 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001349 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1350 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001351 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001352 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001353 sk == rtnl_dereference(mrt->mroute_sk),
1354 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001355 rtnl_unlock();
1356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
1358 * Control PIM assert.
1359 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001360 case MRT_ASSERT:
1361 {
1362 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001363 if (optlen != sizeof(v))
1364 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001365 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001366 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001367 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001368 return 0;
1369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001371 case MRT_PIM:
1372 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001373 int v;
1374
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001375 if (optlen != sizeof(v))
1376 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001377 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001378 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001379 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001380
Stephen Hemminger132adf52007-03-08 20:44:43 -08001381 rtnl_lock();
1382 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001383 if (v != mrt->mroute_do_pim) {
1384 mrt->mroute_do_pim = v;
1385 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001387 rtnl_unlock();
1388 return ret;
1389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001391#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1392 case MRT_TABLE:
1393 {
1394 u32 v;
1395
1396 if (optlen != sizeof(u32))
1397 return -EINVAL;
1398 if (get_user(v, (u32 __user *)optval))
1399 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001400
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001401 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1402 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1403 return -EINVAL;
1404
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001405 rtnl_lock();
1406 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001407 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1408 ret = -EBUSY;
1409 } else {
1410 if (!ipmr_new_table(net, v))
1411 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001412 else
1413 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001414 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001415 rtnl_unlock();
1416 return ret;
1417 }
1418#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001419 /*
1420 * Spurious command, or MRT_VERSION which you cannot
1421 * set.
1422 */
1423 default:
1424 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426}
1427
1428/*
1429 * Getsock opt support for the multicast routing system.
1430 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001431
Jianjun Kongc354e122008-11-03 00:28:02 -08001432int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 int olr;
1435 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001436 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001437 struct mr_table *mrt;
1438
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001439 if (sk->sk_type != SOCK_RAW ||
1440 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1441 return -EOPNOTSUPP;
1442
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001443 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1444 if (mrt == NULL)
1445 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Jianjun Kongc354e122008-11-03 00:28:02 -08001447 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001449 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001451 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -ENOPROTOOPT;
1453
1454 if (get_user(olr, optlen))
1455 return -EFAULT;
1456
1457 olr = min_t(unsigned int, olr, sizeof(int));
1458 if (olr < 0)
1459 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001460
Jianjun Kongc354e122008-11-03 00:28:02 -08001461 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001463 if (optname == MRT_VERSION)
1464 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001466 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001467 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468#endif
1469 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001470 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001471 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EFAULT;
1473 return 0;
1474}
1475
1476/*
1477 * The IP multicast ioctl support routines.
1478 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1481{
1482 struct sioc_sg_req sr;
1483 struct sioc_vif_req vr;
1484 struct vif_device *vif;
1485 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001486 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001487 struct mr_table *mrt;
1488
1489 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1490 if (mrt == NULL)
1491 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001492
Stephen Hemminger132adf52007-03-08 20:44:43 -08001493 switch (cmd) {
1494 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001495 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001496 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001497 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498 return -EINVAL;
1499 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001500 vif = &mrt->vif_table[vr.vifi];
1501 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001502 vr.icount = vif->pkt_in;
1503 vr.ocount = vif->pkt_out;
1504 vr.ibytes = vif->bytes_in;
1505 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001507
Jianjun Kongc354e122008-11-03 00:28:02 -08001508 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001510 return 0;
1511 }
1512 read_unlock(&mrt_lock);
1513 return -EADDRNOTAVAIL;
1514 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001515 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001516 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Eric Dumazeta8c94862010-10-01 16:15:08 +00001518 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001519 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520 if (c) {
1521 sr.pktcnt = c->mfc_un.res.pkt;
1522 sr.bytecnt = c->mfc_un.res.bytes;
1523 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001524 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001525
Jianjun Kongc354e122008-11-03 00:28:02 -08001526 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001527 return -EFAULT;
1528 return 0;
1529 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001530 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001531 return -EADDRNOTAVAIL;
1532 default:
1533 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535}
1536
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001537#ifdef CONFIG_COMPAT
1538struct compat_sioc_sg_req {
1539 struct in_addr src;
1540 struct in_addr grp;
1541 compat_ulong_t pktcnt;
1542 compat_ulong_t bytecnt;
1543 compat_ulong_t wrong_if;
1544};
1545
David S. Millerca6b8bb02011-02-03 17:24:28 -08001546struct compat_sioc_vif_req {
1547 vifi_t vifi; /* Which iface */
1548 compat_ulong_t icount;
1549 compat_ulong_t ocount;
1550 compat_ulong_t ibytes;
1551 compat_ulong_t obytes;
1552};
1553
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001554int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1555{
David S. Miller0033d5a2011-02-03 17:21:31 -08001556 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001557 struct compat_sioc_vif_req vr;
1558 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001559 struct mfc_cache *c;
1560 struct net *net = sock_net(sk);
1561 struct mr_table *mrt;
1562
1563 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1564 if (mrt == NULL)
1565 return -ENOENT;
1566
1567 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001568 case SIOCGETVIFCNT:
1569 if (copy_from_user(&vr, arg, sizeof(vr)))
1570 return -EFAULT;
1571 if (vr.vifi >= mrt->maxvif)
1572 return -EINVAL;
1573 read_lock(&mrt_lock);
1574 vif = &mrt->vif_table[vr.vifi];
1575 if (VIF_EXISTS(mrt, vr.vifi)) {
1576 vr.icount = vif->pkt_in;
1577 vr.ocount = vif->pkt_out;
1578 vr.ibytes = vif->bytes_in;
1579 vr.obytes = vif->bytes_out;
1580 read_unlock(&mrt_lock);
1581
1582 if (copy_to_user(arg, &vr, sizeof(vr)))
1583 return -EFAULT;
1584 return 0;
1585 }
1586 read_unlock(&mrt_lock);
1587 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001588 case SIOCGETSGCNT:
1589 if (copy_from_user(&sr, arg, sizeof(sr)))
1590 return -EFAULT;
1591
1592 rcu_read_lock();
1593 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1594 if (c) {
1595 sr.pktcnt = c->mfc_un.res.pkt;
1596 sr.bytecnt = c->mfc_un.res.bytes;
1597 sr.wrong_if = c->mfc_un.res.wrong_if;
1598 rcu_read_unlock();
1599
1600 if (copy_to_user(arg, &sr, sizeof(sr)))
1601 return -EFAULT;
1602 return 0;
1603 }
1604 rcu_read_unlock();
1605 return -EADDRNOTAVAIL;
1606 default:
1607 return -ENOIOCTLCMD;
1608 }
1609}
1610#endif
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1614{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001615 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001616 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001617 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 struct vif_device *v;
1619 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (event != NETDEV_UNREGISTER)
1622 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001623
1624 ipmr_for_each_table(mrt, net) {
1625 v = &mrt->vif_table[0];
1626 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1627 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001628 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 return NOTIFY_DONE;
1632}
1633
1634
Jianjun Kongc354e122008-11-03 00:28:02 -08001635static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 .notifier_call = ipmr_device_event,
1637};
1638
1639/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001640 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 * This avoids tunnel drivers and other mess and gives us the speed so
1642 * important for multicast video.
1643 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001644
Al Viro114c7842006-09-27 18:39:29 -07001645static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001647 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001648 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001649
1650 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001651 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001652 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001653 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001655 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001656 iph->tos = old_iph->tos;
1657 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 iph->frag_off = 0;
1659 iph->daddr = daddr;
1660 iph->saddr = saddr;
1661 iph->protocol = IPPROTO_IPIP;
1662 iph->ihl = 5;
1663 iph->tot_len = htons(skb->len);
Eric Dumazetff1f69a82014-06-02 05:26:03 -07001664 ip_select_ident(skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 ip_send_check(iph);
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1668 nf_reset(skb);
1669}
1670
1671static inline int ipmr_forward_finish(struct sk_buff *skb)
1672{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001673 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Eric Dumazetadf30902009-06-02 05:19:30 +00001675 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Vincent Bernat2d8dbb02012-06-05 03:41:42 +00001676 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 if (unlikely(opt->optlen))
1679 ip_forward_options(skb);
1680
1681 return dst_output(skb);
1682}
1683
1684/*
1685 * Processing handlers for ipmr_forward
1686 */
1687
Patrick McHardy0c122952010-04-13 05:03:22 +00001688static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1689 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001691 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001692 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 struct net_device *dev;
1694 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001695 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 int encap = 0;
1697
1698 if (vif->dev == NULL)
1699 goto out_free;
1700
1701#ifdef CONFIG_IP_PIMSM
1702 if (vif->flags & VIFF_REGISTER) {
1703 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001704 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001705 vif->dev->stats.tx_bytes += skb->len;
1706 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001707 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001708 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710#endif
1711
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001712 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001713 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001714 vif->remote, vif->local,
1715 0, 0,
1716 IPPROTO_IPIP,
1717 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001718 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 goto out_free;
1720 encap = sizeof(struct iphdr);
1721 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001722 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001723 0, 0,
1724 IPPROTO_IPIP,
1725 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001726 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto out_free;
1728 }
1729
Changli Gaod8d1f302010-06-10 23:31:35 -07001730 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Changli Gaod8d1f302010-06-10 23:31:35 -07001732 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001734 * allow to send ICMP, so that packets will disappear
1735 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
1737
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001738 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 ip_rt_put(rt);
1740 goto out_free;
1741 }
1742
Changli Gaod8d1f302010-06-10 23:31:35 -07001743 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001746 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto out_free;
1748 }
1749
1750 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001751 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Eric Dumazetadf30902009-06-02 05:19:30 +00001753 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001754 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001755 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001758 * What do we do with netfilter? -- RR
1759 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (vif->flags & VIFF_TUNNEL) {
1761 ip_encap(skb, vif->local, vif->remote);
1762 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001763 vif->dev->stats.tx_packets++;
1764 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
1767 IPCB(skb)->flags |= IPSKB_FORWARDED;
1768
1769 /*
1770 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1771 * not only before forwarding, but after forwarding on all output
1772 * interfaces. It is clear, if mrouter runs a multicasting
1773 * program, it should receive packets not depending to what interface
1774 * program is joined.
1775 * If we will not make it, the program will have to join on all
1776 * interfaces. On the other hand, multihoming host (or router, but
1777 * not mrouter) cannot join to more than one interface - it will
1778 * result in receiving multiple packets.
1779 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001780 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 ipmr_forward_finish);
1782 return;
1783
1784out_free:
1785 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Patrick McHardy0c122952010-04-13 05:03:22 +00001788static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001791
1792 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1793 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 break;
1795 }
1796 return ct;
1797}
1798
1799/* "local" means that we should preserve one skb (for local delivery) */
1800
Patrick McHardy0c122952010-04-13 05:03:22 +00001801static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1802 struct sk_buff *skb, struct mfc_cache *cache,
1803 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 int psend = -1;
1806 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001807 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 vif = cache->mfc_parent;
1810 cache->mfc_un.res.pkt++;
1811 cache->mfc_un.res.bytes += skb->len;
1812
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001813 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001814 struct mfc_cache *cache_proxy;
1815
1816 /* For an (*,G) entry, we only check that the incomming
1817 * interface is part of the static tree.
1818 */
1819 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1820 if (cache_proxy &&
1821 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1822 goto forward;
1823 }
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /*
1826 * Wrong interface: drop packet and (maybe) send PIM assert.
1827 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001828 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001829 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001831 * Very complicated situation...
1832 *
1833 * The best workaround until routing daemons will be
1834 * fixed is not to redistribute packet, if it was
1835 * send through wrong interface. It means, that
1836 * multicast applications WILL NOT work for
1837 * (S,G), which have default multicast route pointing
1838 * to wrong oif. In any case, it is not a good
1839 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 */
1841 goto dont_forward;
1842 }
1843
1844 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Patrick McHardy0c122952010-04-13 05:03:22 +00001846 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001848 * so that we cannot check that packet arrived on an oif.
1849 * It is bad, but otherwise we would need to move pretty
1850 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001852 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001853 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001854 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1856 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001857 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 goto dont_forward;
1860 }
1861
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001862forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001863 mrt->vif_table[vif].pkt_in++;
1864 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /*
1867 * Forward the frame
1868 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001869 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1870 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001871 if (true_vifi >= 0 &&
1872 true_vifi != cache->mfc_parent &&
1873 ip_hdr(skb)->ttl >
1874 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1875 /* It's an (*,*) entry and the packet is not coming from
1876 * the upstream: forward the packet to the upstream
1877 * only.
1878 */
1879 psend = cache->mfc_parent;
1880 goto last_forward;
1881 }
1882 goto dont_forward;
1883 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001884 for (ct = cache->mfc_un.res.maxvif - 1;
1885 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001886 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001887 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1888 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001889 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (psend != -1) {
1891 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001894 ipmr_queue_xmit(net, mrt, skb2, cache,
1895 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001897 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001900last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (psend != -1) {
1902 if (local) {
1903 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001906 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001908 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return 0;
1910 }
1911 }
1912
1913dont_forward:
1914 if (!local)
1915 kfree_skb(skb);
1916 return 0;
1917}
1918
David S. Miller417da662011-05-03 19:42:43 -07001919static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001920{
David S. Miller417da662011-05-03 19:42:43 -07001921 struct rtable *rt = skb_rtable(skb);
1922 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001923 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001924 .daddr = iph->daddr,
1925 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001926 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001927 .flowi4_oif = (rt_is_output_route(rt) ?
1928 skb->dev->ifindex : 0),
1929 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001930 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001931 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001932 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001933 };
1934 struct mr_table *mrt;
1935 int err;
1936
David S. Millerda919812011-03-12 02:04:50 -05001937 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001938 if (err)
1939 return ERR_PTR(err);
1940 return mrt;
1941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943/*
1944 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001945 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
1947
1948int ip_mr_input(struct sk_buff *skb)
1949{
1950 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001951 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001952 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001953 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001956 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001958 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 goto dont_forward;
1960
David S. Miller417da662011-05-03 19:42:43 -07001961 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001962 if (IS_ERR(mrt)) {
1963 kfree_skb(skb);
1964 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001967 if (IPCB(skb)->opt.router_alert) {
1968 if (ip_call_ra_chain(skb))
1969 return 0;
1970 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1971 /* IGMPv1 (and broken IGMPv2 implementations sort of
1972 * Cisco IOS <= 11.2(8)) do not put router alert
1973 * option to IGMP packets destined to routable
1974 * groups. It is very bad, because it means
1975 * that we can forward NO IGMP messages.
1976 */
1977 struct sock *mroute_sk;
1978
1979 mroute_sk = rcu_dereference(mrt->mroute_sk);
1980 if (mroute_sk) {
1981 nf_reset(skb);
1982 raw_rcv(mroute_sk, skb);
1983 return 0;
1984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986 }
1987
Eric Dumazeta8c94862010-10-01 16:15:08 +00001988 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001989 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001990 if (cache == NULL) {
1991 int vif = ipmr_find_vif(mrt, skb->dev);
1992
1993 if (vif >= 0)
1994 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1995 vif);
1996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /*
1999 * No usable cache entry
2000 */
Jianjun Kongc354e122008-11-03 00:28:02 -08002001 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 int vif;
2003
2004 if (local) {
2005 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2006 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002007 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 skb = skb2;
2010 }
2011
Eric Dumazeta8c94862010-10-01 16:15:08 +00002012 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002013 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002015 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 read_unlock(&mrt_lock);
2017
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002018 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020 read_unlock(&mrt_lock);
2021 kfree_skb(skb);
2022 return -ENODEV;
2023 }
2024
Eric Dumazeta8c94862010-10-01 16:15:08 +00002025 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002026 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 read_unlock(&mrt_lock);
2028
2029 if (local)
2030 return ip_local_deliver(skb);
2031
2032 return 0;
2033
2034dont_forward:
2035 if (local)
2036 return ip_local_deliver(skb);
2037 kfree_skb(skb);
2038 return 0;
2039}
2040
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002041#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00002042/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002043static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2044 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002046 struct net_device *reg_dev = NULL;
2047 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002049 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002051 * Check that:
2052 * a. packet is really sent to a multicast group
2053 * b. packet is not a NULL-REGISTER
2054 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002056 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002058 ntohs(encap->tot_len) + pimlen > skb->len)
2059 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002062 if (mrt->mroute_reg_vif_num >= 0)
2063 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 read_unlock(&mrt_lock);
2065
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002066 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002067 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002069 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00002070 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03002071 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00002073 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002075
2076 skb_tunnel_rx(skb, reg_dev);
2077
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 Dichtel38c50cd2014-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 Dichtel38c50cd2014-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 Dichtel38c50cd2014-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 Dichtel38c50cd2014-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 Dichtel38c50cd2014-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}