blob: bc40115bc39481de40cd725356742c19f274c884 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71#define CONFIG_IP_PIMSM 1
72#endif
73
Patrick McHardy0c122952010-04-13 05:03:22 +000074struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000075 struct list_head list;
Patrick McHardy8de53df2010-04-15 13:29:28 +020076#ifdef CONFIG_NET_NS
77 struct net *net;
78#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +000079 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000080 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000081 struct timer_list ipmr_expire_timer;
82 struct list_head mfc_unres_queue;
83 struct list_head mfc_cache_array[MFC_LINES];
84 struct vif_device vif_table[MAXVIFS];
85 int maxvif;
86 atomic_t cache_resolve_queue_len;
Joe Perches53d68412012-11-25 09:35:30 +000087 bool mroute_do_assert;
88 bool mroute_do_pim;
Patrick McHardy0c122952010-04-13 05:03:22 +000089#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num;
91#endif
92};
93
Patrick McHardyf0ad0862010-04-13 05:03:23 +000094struct ipmr_rule {
95 struct fib_rule common;
96};
97
98struct ipmr_result {
99 struct mr_table *mrt;
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000103 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105
106static DEFINE_RWLOCK(mrt_lock);
107
108/*
109 * Multicast router control variables
110 */
111
Patrick McHardy0c122952010-04-13 05:03:22 +0000112#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* Special spinlock for queue of unresolved entries */
115static DEFINE_SPINLOCK(mfc_unres_lock);
116
117/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
121 *
122 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124
Christoph Lametere18b8902006-12-06 20:33:20 -0800125static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000127static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000128static void ipmr_free_table(struct mr_table *mrt);
129
Rami Rosenc4854ec2013-07-20 15:09:28 +0300130static void ip_mr_forward(struct net *net, struct mr_table *mrt,
131 struct sk_buff *skb, struct mfc_cache *cache,
132 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000133static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000134 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200135static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000137static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
138 int cmd);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000139static void mroute_clean_tables(struct mr_table *mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000140static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000142#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
143#define ipmr_for_each_table(mrt, net) \
144 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
145
146static struct mr_table *ipmr_get_table(struct net *net, u32 id)
147{
148 struct mr_table *mrt;
149
150 ipmr_for_each_table(mrt, net) {
151 if (mrt->id == id)
152 return mrt;
153 }
154 return NULL;
155}
156
David S. Millerda919812011-03-12 02:04:50 -0500157static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000158 struct mr_table **mrt)
159{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000160 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100161 struct ipmr_result res;
162 struct fib_lookup_arg arg = {
163 .result = &res,
164 .flags = FIB_LOOKUP_NOREF,
165 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000166
David S. Millerda919812011-03-12 02:04:50 -0500167 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
168 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000169 if (err < 0)
170 return err;
171 *mrt = res.mrt;
172 return 0;
173}
174
175static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
176 int flags, struct fib_lookup_arg *arg)
177{
178 struct ipmr_result *res = arg->result;
179 struct mr_table *mrt;
180
181 switch (rule->action) {
182 case FR_ACT_TO_TBL:
183 break;
184 case FR_ACT_UNREACHABLE:
185 return -ENETUNREACH;
186 case FR_ACT_PROHIBIT:
187 return -EACCES;
188 case FR_ACT_BLACKHOLE:
189 default:
190 return -EINVAL;
191 }
192
193 mrt = ipmr_get_table(rule->fr_net, rule->table);
194 if (mrt == NULL)
195 return -EAGAIN;
196 res->mrt = mrt;
197 return 0;
198}
199
200static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
201{
202 return 1;
203}
204
205static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
206 FRA_GENERIC_POLICY,
207};
208
209static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
210 struct fib_rule_hdr *frh, struct nlattr **tb)
211{
212 return 0;
213}
214
215static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
216 struct nlattr **tb)
217{
218 return 1;
219}
220
221static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
222 struct fib_rule_hdr *frh)
223{
224 frh->dst_len = 0;
225 frh->src_len = 0;
226 frh->tos = 0;
227 return 0;
228}
229
Andi Kleen04a6f822012-10-04 17:12:11 -0700230static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200231 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000232 .rule_size = sizeof(struct ipmr_rule),
233 .addr_size = sizeof(u32),
234 .action = ipmr_rule_action,
235 .match = ipmr_rule_match,
236 .configure = ipmr_rule_configure,
237 .compare = ipmr_rule_compare,
238 .default_pref = fib_default_rule_pref,
239 .fill = ipmr_rule_fill,
240 .nlgroup = RTNLGRP_IPV4_RULE,
241 .policy = ipmr_rule_policy,
242 .owner = THIS_MODULE,
243};
244
245static int __net_init ipmr_rules_init(struct net *net)
246{
247 struct fib_rules_ops *ops;
248 struct mr_table *mrt;
249 int err;
250
251 ops = fib_rules_register(&ipmr_rules_ops_template, net);
252 if (IS_ERR(ops))
253 return PTR_ERR(ops);
254
255 INIT_LIST_HEAD(&net->ipv4.mr_tables);
256
257 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
258 if (mrt == NULL) {
259 err = -ENOMEM;
260 goto err1;
261 }
262
263 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
264 if (err < 0)
265 goto err2;
266
267 net->ipv4.mr_rules_ops = ops;
268 return 0;
269
270err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700271 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000272err1:
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
WANG Conged785302015-03-31 11:01:45 -0700281 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000282 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
283 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000284 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000285 }
WANG Conged785302015-03-31 11:01:45 -0700286 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000287 fib_rules_unregister(net->ipv4.mr_rules_ops);
288}
289#else
290#define ipmr_for_each_table(mrt, net) \
291 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
292
293static struct mr_table *ipmr_get_table(struct net *net, u32 id)
294{
295 return net->ipv4.mrt;
296}
297
David S. Millerda919812011-03-12 02:04:50 -0500298static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000299 struct mr_table **mrt)
300{
301 *mrt = net->ipv4.mrt;
302 return 0;
303}
304
305static int __net_init ipmr_rules_init(struct net *net)
306{
307 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
308 return net->ipv4.mrt ? 0 : -ENOMEM;
309}
310
311static void __net_exit ipmr_rules_exit(struct net *net)
312{
WANG Conged785302015-03-31 11:01:45 -0700313 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000314 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700315 net->ipv4.mrt = NULL;
316 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000317}
318#endif
319
320static struct mr_table *ipmr_new_table(struct net *net, u32 id)
321{
322 struct mr_table *mrt;
323 unsigned int i;
324
325 mrt = ipmr_get_table(net, id);
326 if (mrt != NULL)
327 return mrt;
328
329 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
330 if (mrt == NULL)
331 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200332 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000333 mrt->id = id;
334
335 /* Forwarding cache */
336 for (i = 0; i < MFC_LINES; i++)
337 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
338
339 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
340
341 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
342 (unsigned long)mrt);
343
344#ifdef CONFIG_IP_PIMSM
345 mrt->mroute_reg_vif_num = -1;
346#endif
347#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
348 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
349#endif
350 return mrt;
351}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000353static void ipmr_free_table(struct mr_table *mrt)
354{
355 del_timer_sync(&mrt->ipmr_expire_timer);
356 mroute_clean_tables(mrt);
357 kfree(mrt);
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
361
Wang Chend6070322008-07-14 20:55:26 -0700362static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
363{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000364 struct net *net = dev_net(dev);
365
Wang Chend6070322008-07-14 20:55:26 -0700366 dev_close(dev);
367
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000368 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700369 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800370 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700371 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700372 struct ip_tunnel_parm p;
373
374 memset(&p, 0, sizeof(p));
375 p.iph.daddr = v->vifc_rmt_addr.s_addr;
376 p.iph.saddr = v->vifc_lcl_addr.s_addr;
377 p.iph.version = 4;
378 p.iph.ihl = 5;
379 p.iph.protocol = IPPROTO_IPIP;
380 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
381 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
382
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800383 if (ops->ndo_do_ioctl) {
384 mm_segment_t oldfs = get_fs();
385
386 set_fs(KERNEL_DS);
387 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
388 set_fs(oldfs);
389 }
Wang Chend6070322008-07-14 20:55:26 -0700390 }
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000394struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct net_device *dev;
397
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000398 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800401 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int err;
403 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct ip_tunnel_parm p;
405 struct in_device *in_dev;
406
407 memset(&p, 0, sizeof(p));
408 p.iph.daddr = v->vifc_rmt_addr.s_addr;
409 p.iph.saddr = v->vifc_lcl_addr.s_addr;
410 p.iph.version = 4;
411 p.iph.ihl = 5;
412 p.iph.protocol = IPPROTO_IPIP;
413 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800414 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800416 if (ops->ndo_do_ioctl) {
417 mm_segment_t oldfs = get_fs();
418
419 set_fs(KERNEL_DS);
420 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
421 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000422 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800423 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 dev = NULL;
426
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000427 if (err == 0 &&
428 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dev->flags |= IFF_MULTICAST;
430
Herbert Xue5ed6392005-10-03 14:35:55 -0700431 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700432 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700434
435 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100436 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700437 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (dev_open(dev))
440 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700441 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 }
444 return dev;
445
446failure:
447 /* allow the register to be completed before unregistering. */
448 rtnl_unlock();
449 rtnl_lock();
450
451 unregister_netdevice(dev);
452 return NULL;
453}
454
455#ifdef CONFIG_IP_PIMSM
456
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000457static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000459 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000460 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500461 struct flowi4 fl4 = {
462 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700463 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500464 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000465 };
466 int err;
467
David S. Millerda919812011-03-12 02:04:50 -0500468 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000469 if (err < 0) {
470 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000471 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000472 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700475 dev->stats.tx_bytes += skb->len;
476 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000477 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 read_unlock(&mrt_lock);
479 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000480 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Stephen Hemminger007c3832008-11-20 20:28:35 -0800483static const struct net_device_ops reg_vif_netdev_ops = {
484 .ndo_start_xmit = reg_vif_xmit,
485};
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static void reg_vif_setup(struct net_device *dev)
488{
489 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800490 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530492 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700494 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000497static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct net_device *dev;
500 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000501 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000503 if (mrt->id == RT_TABLE_DEFAULT)
504 sprintf(name, "pimreg");
505 else
506 sprintf(name, "pimreg%u", mrt->id);
507
Tom Gundersenc835a672014-07-14 16:37:24 +0200508 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (dev == NULL)
511 return NULL;
512
Tom Goff403dbb92009-06-14 03:16:13 -0700513 dev_net_set(dev, net);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (register_netdevice(dev)) {
516 free_netdev(dev);
517 return NULL;
518 }
519 dev->iflink = 0;
520
Herbert Xu71e27da2007-06-04 23:36:06 -0700521 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000522 in_dev = __in_dev_get_rcu(dev);
523 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700524 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Herbert Xu71e27da2007-06-04 23:36:06 -0700528 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100529 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700530 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
531 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (dev_open(dev))
534 goto failure;
535
Wang Chen7dc00c82008-07-14 20:56:34 -0700536 dev_hold(dev);
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return dev;
539
540failure:
541 /* allow the register to be completed before unregistering. */
542 rtnl_unlock();
543 rtnl_lock();
544
545 unregister_netdevice(dev);
546 return NULL;
547}
548#endif
549
Ben Hutchings2c530402012-07-10 10:55:09 +0000550/**
551 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700552 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900554
Patrick McHardy0c122952010-04-13 05:03:22 +0000555static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000556 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct vif_device *v;
559 struct net_device *dev;
560 struct in_device *in_dev;
561
Patrick McHardy0c122952010-04-13 05:03:22 +0000562 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -EADDRNOTAVAIL;
564
Patrick McHardy0c122952010-04-13 05:03:22 +0000565 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 write_lock_bh(&mrt_lock);
568 dev = v->dev;
569 v->dev = NULL;
570
571 if (!dev) {
572 write_unlock_bh(&mrt_lock);
573 return -EADDRNOTAVAIL;
574 }
575
576#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000577 if (vifi == mrt->mroute_reg_vif_num)
578 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif
580
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000581 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000583
584 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000585 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 break;
587 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000588 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
591 write_unlock_bh(&mrt_lock);
592
593 dev_set_allmulti(dev, -1);
594
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000595 in_dev = __in_dev_get_rtnl(dev);
596 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700597 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000598 inet_netconf_notify_devconf(dev_net(dev),
599 NETCONFA_MC_FORWARDING,
600 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 ip_rt_multicast_event(in_dev);
602 }
603
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000604 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000605 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 dev_put(dev);
608 return 0;
609}
610
Eric Dumazeta8c94862010-10-01 16:15:08 +0000611static void ipmr_cache_free_rcu(struct rcu_head *head)
612{
613 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
614
615 kmem_cache_free(mrt_cachep, c);
616}
617
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000618static inline void ipmr_cache_free(struct mfc_cache *c)
619{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000620 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000624 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
626
Patrick McHardy0c122952010-04-13 05:03:22 +0000627static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200629 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700631 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Patrick McHardy0c122952010-04-13 05:03:22 +0000633 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Jianjun Kongc354e122008-11-03 00:28:02 -0800635 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700636 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
638 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000639 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000641 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700642 e->error = -ETIMEDOUT;
643 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700644
Eric W. Biederman15e47302012-09-07 20:12:54 +0000645 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000646 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000651 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654
Patrick McHardye258beb2010-04-13 05:03:19 +0000655/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Patrick McHardye258beb2010-04-13 05:03:19 +0000657static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Patrick McHardy0c122952010-04-13 05:03:22 +0000659 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned long now;
661 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000662 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000665 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return;
667 }
668
Patrick McHardy0c122952010-04-13 05:03:22 +0000669 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 goto out;
671
672 now = jiffies;
673 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Patrick McHardy0c122952010-04-13 05:03:22 +0000675 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (time_after(c->mfc_un.unres.expires, now)) {
677 unsigned long interval = c->mfc_un.unres.expires - now;
678 if (interval < expires)
679 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 continue;
681 }
682
Patrick McHardy862465f2010-04-13 05:03:21 +0000683 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000684 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000685 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
Patrick McHardy0c122952010-04-13 05:03:22 +0000688 if (!list_empty(&mrt->mfc_unres_queue))
689 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691out:
692 spin_unlock(&mfc_unres_lock);
693}
694
695/* Fill oifs list. It is called under write locked mrt_lock. */
696
Patrick McHardy0c122952010-04-13 05:03:22 +0000697static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000698 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 int vifi;
701
702 cache->mfc_un.res.minvif = MAXVIFS;
703 cache->mfc_un.res.maxvif = 0;
704 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
705
Patrick McHardy0c122952010-04-13 05:03:22 +0000706 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
707 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000708 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
710 if (cache->mfc_un.res.minvif > vifi)
711 cache->mfc_un.res.minvif = vifi;
712 if (cache->mfc_un.res.maxvif <= vifi)
713 cache->mfc_un.res.maxvif = vifi + 1;
714 }
715 }
716}
717
Patrick McHardy0c122952010-04-13 05:03:22 +0000718static int vif_add(struct net *net, struct mr_table *mrt,
719 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000722 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct net_device *dev;
724 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700725 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000728 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -EADDRINUSE;
730
731 switch (vifc->vifc_flags) {
732#ifdef CONFIG_IP_PIMSM
733 case VIFF_REGISTER:
734 /*
735 * Special Purpose VIF in PIM
736 * All the packets will be sent to the daemon
737 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000738 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000740 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!dev)
742 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700743 err = dev_set_allmulti(dev, 1);
744 if (err) {
745 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700746 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700747 return err;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900751 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000752 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!dev)
754 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700755 err = dev_set_allmulti(dev, 1);
756 if (err) {
757 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700758 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700759 return err;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000762
763 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000765 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
766 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000767 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000768 dev_put(dev);
769 return -EADDRNOTAVAIL;
770 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000771 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000772 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!dev)
775 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700776 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700777 if (err) {
778 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700779 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 break;
782 default:
783 return -EINVAL;
784 }
785
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000786 in_dev = __in_dev_get_rtnl(dev);
787 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000788 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000790 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700791 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000792 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
793 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 ip_rt_multicast_event(in_dev);
795
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000796 /* Fill in the VIF structures */
797
Jianjun Kongc354e122008-11-03 00:28:02 -0800798 v->rate_limit = vifc->vifc_rate_limit;
799 v->local = vifc->vifc_lcl_addr.s_addr;
800 v->remote = vifc->vifc_rmt_addr.s_addr;
801 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!mrtsock)
803 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800804 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 v->bytes_in = 0;
806 v->bytes_out = 0;
807 v->pkt_in = 0;
808 v->pkt_out = 0;
809 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000810 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 v->link = dev->iflink;
812
813 /* And finish update writing critical data */
814 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800815 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000817 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000818 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000820 if (vifi+1 > mrt->maxvif)
821 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 write_unlock_bh(&mrt_lock);
823 return 0;
824}
825
Eric Dumazeta8c94862010-10-01 16:15:08 +0000826/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000827static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000828 __be32 origin,
829 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Jianjun Kongc354e122008-11-03 00:28:02 -0800831 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct mfc_cache *c;
833
Eric Dumazeta8c94862010-10-01 16:15:08 +0000834 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000835 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
836 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000838 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000841/* Look for a (*,*,oif) entry */
842static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
843 int vifi)
844{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100845 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000846 struct mfc_cache *c;
847
848 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100849 if (c->mfc_origin == htonl(INADDR_ANY) &&
850 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000851 c->mfc_un.res.ttls[vifi] < 255)
852 return c;
853
854 return NULL;
855}
856
857/* Look for a (*,G) entry */
858static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
859 __be32 mcastgrp, int vifi)
860{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100861 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000862 struct mfc_cache *c, *proxy;
863
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100864 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000865 goto skip;
866
867 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100868 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000869 c->mfc_mcastgrp == mcastgrp) {
870 if (c->mfc_un.res.ttls[vifi] < 255)
871 return c;
872
873 /* It's ok if the vifi is part of the static tree */
874 proxy = ipmr_cache_find_any_parent(mrt,
875 c->mfc_parent);
876 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
877 return c;
878 }
879
880skip:
881 return ipmr_cache_find_any_parent(mrt, vifi);
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/*
885 * Allocate a multicast cache entry
886 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000887static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Jianjun Kongc354e122008-11-03 00:28:02 -0800889 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000890
891 if (c)
892 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return c;
894}
895
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000896static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Jianjun Kongc354e122008-11-03 00:28:02 -0800898 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000899
900 if (c) {
901 skb_queue_head_init(&c->mfc_un.unres.unresolved);
902 c->mfc_un.unres.expires = jiffies + 10*HZ;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return c;
905}
906
907/*
908 * A cache entry has gone into a resolved state from queued
909 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900910
Patrick McHardy0c122952010-04-13 05:03:22 +0000911static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
912 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700915 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000917 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Jianjun Kongc354e122008-11-03 00:28:02 -0800919 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700920 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
922
Hong zhi guo573ce262013-03-27 06:47:04 +0000923 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000924 nlh->nlmsg_len = skb_tail_pointer(skb) -
925 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 } else {
927 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000928 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000930 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700931 e->error = -EMSGSIZE;
932 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Thomas Graf2942e902006-08-15 00:30:25 -0700934
Eric W. Biederman15e47302012-09-07 20:12:54 +0000935 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000936 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000937 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940}
941
942/*
943 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
944 * expects the following bizarre scheme.
945 *
946 * Called under mrt_lock.
947 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900948
Patrick McHardy0c122952010-04-13 05:03:22 +0000949static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000950 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300953 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct igmphdr *igmp;
955 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000956 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int ret;
958
959#ifdef CONFIG_IP_PIMSM
960 if (assert == IGMPMSG_WHOLEPKT)
961 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
962 else
963#endif
964 skb = alloc_skb(128, GFP_ATOMIC);
965
Stephen Hemminger132adf52007-03-08 20:44:43 -0800966 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return -ENOBUFS;
968
969#ifdef CONFIG_IP_PIMSM
970 if (assert == IGMPMSG_WHOLEPKT) {
971 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000972 * Duplicate old header, fix ihl, length etc.
973 * And all this only to mangle msg->im_msgtype and
974 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300976 skb_push(skb, sizeof(struct iphdr));
977 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300978 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300979 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700980 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 msg->im_msgtype = IGMPMSG_WHOLEPKT;
982 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000983 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700984 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
985 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
986 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900987 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900989 {
990
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000991 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Cong Wang30f3a402013-06-05 20:14:10 +0800993 skb_set_network_header(skb, skb->len);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300994 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300995 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000996 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700997 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000999 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001001 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001003 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 igmp->type =
1005 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001006 igmp->code = 0;
1007 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001008 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Eric Dumazet4c968702010-10-01 16:15:01 +00001011 rcu_read_lock();
1012 mroute_sk = rcu_dereference(mrt->mroute_sk);
1013 if (mroute_sk == NULL) {
1014 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 kfree_skb(skb);
1016 return -EINVAL;
1017 }
1018
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001019 /* Deliver to mrouted */
1020
Eric Dumazet4c968702010-10-01 16:15:01 +00001021 ret = sock_queue_rcv_skb(mroute_sk, skb);
1022 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001023 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001024 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 kfree_skb(skb);
1026 }
1027
1028 return ret;
1029}
1030
1031/*
1032 * Queue a packet for resolution. It gets locked cache entry!
1033 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001036ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Patrick McHardy862465f2010-04-13 05:03:21 +00001038 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int err;
1040 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001041 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001044 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001045 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001046 c->mfc_origin == iph->saddr) {
1047 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
Patrick McHardy862465f2010-04-13 05:03:21 +00001052 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001053 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Patrick McHardy0c122952010-04-13 05:03:22 +00001055 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001056 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 spin_unlock_bh(&mfc_unres_lock);
1058
1059 kfree_skb(skb);
1060 return -ENOBUFS;
1061 }
1062
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001063 /* Fill in the new cache entry */
1064
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001065 c->mfc_parent = -1;
1066 c->mfc_origin = iph->saddr;
1067 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001069 /* Reflect first query at mrouted. */
1070
Patrick McHardy0c122952010-04-13 05:03:22 +00001071 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001072 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001073 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 out - Brad Parker
1075 */
1076 spin_unlock_bh(&mfc_unres_lock);
1077
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001078 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 kfree_skb(skb);
1080 return err;
1081 }
1082
Patrick McHardy0c122952010-04-13 05:03:22 +00001083 atomic_inc(&mrt->cache_resolve_queue_len);
1084 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001085 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
David S. Miller278554b2010-05-12 00:05:35 -07001087 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1088 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001091 /* See if we can append the packet */
1092
1093 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 kfree_skb(skb);
1095 err = -ENOBUFS;
1096 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001097 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 err = 0;
1099 }
1100
1101 spin_unlock_bh(&mfc_unres_lock);
1102 return err;
1103}
1104
1105/*
1106 * MFC cache manipulation by user space mroute daemon
1107 */
1108
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001109static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001112 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Jianjun Kongc354e122008-11-03 00:28:02 -08001114 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Patrick McHardy0c122952010-04-13 05:03:22 +00001116 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001118 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1119 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001120 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001121 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001122 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
1124 }
1125 }
1126 return -ENOENT;
1127}
1128
Patrick McHardy0c122952010-04-13 05:03:22 +00001129static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001130 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Patrick McHardy862465f2010-04-13 05:03:21 +00001132 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001134 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Patrick McHardya50436f22010-03-17 06:04:14 +00001136 if (mfc->mfcc_parent >= MAXVIFS)
1137 return -ENFILE;
1138
Jianjun Kongc354e122008-11-03 00:28:02 -08001139 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Patrick McHardy0c122952010-04-13 05:03:22 +00001141 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001143 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1144 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001145 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
Patrick McHardy862465f2010-04-13 05:03:21 +00001150 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 write_lock_bh(&mrt_lock);
1152 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001153 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (!mrtsock)
1155 c->mfc_flags |= MFC_STATIC;
1156 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001157 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return 0;
1159 }
1160
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001161 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001162 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return -EINVAL;
1164
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001165 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001166 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return -ENOMEM;
1168
Jianjun Kongc354e122008-11-03 00:28:02 -08001169 c->mfc_origin = mfc->mfcc_origin.s_addr;
1170 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1171 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001172 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (!mrtsock)
1174 c->mfc_flags |= MFC_STATIC;
1175
Eric Dumazeta8c94862010-10-01 16:15:08 +00001176 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /*
1179 * Check to see if we resolved a queued list. If so we
1180 * need to send on the frames and tidy up.
1181 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001182 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001184 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001185 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001187 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001188 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001189 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 break;
1191 }
1192 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001193 if (list_empty(&mrt->mfc_unres_queue))
1194 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 spin_unlock_bh(&mfc_unres_lock);
1196
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001197 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001198 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001199 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001201 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203}
1204
1205/*
1206 * Close the multicast socket, and clear the vif tables etc
1207 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001208
Patrick McHardy0c122952010-04-13 05:03:22 +00001209static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001212 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001213 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001214
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001215 /* Shut down all active vif entries */
1216
Patrick McHardy0c122952010-04-13 05:03:22 +00001217 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001218 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001219 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001221 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001223 /* Wipe the cache */
1224
Patrick McHardy862465f2010-04-13 05:03:21 +00001225 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001226 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001227 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001229 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001230 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001231 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233 }
1234
Patrick McHardy0c122952010-04-13 05:03:22 +00001235 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001237 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001238 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001239 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001240 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 spin_unlock_bh(&mfc_unres_lock);
1243 }
1244}
1245
Eric Dumazet4c968702010-10-01 16:15:01 +00001246/* called from ip_ra_control(), before an RCU grace period,
1247 * we dont need to call synchronize_rcu() here
1248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249static void mrtsock_destruct(struct sock *sk)
1250{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001251 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001252 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001255 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001256 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001257 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001258 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1259 NETCONFA_IFINDEX_ALL,
1260 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001261 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001262 mroute_clean_tables(mrt);
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 rtnl_unlock();
1266}
1267
1268/*
1269 * Socket options and virtual interface manipulation. The whole
1270 * virtual interface system is a complete heap, but unfortunately
1271 * that's how BSD mrouted happens to think. Maybe one day with a proper
1272 * MOSPF/PIM router set up we can clean this up.
1273 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001274
David S. Millerb7058842009-09-30 16:12:20 -07001275int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001277 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct vifctl vif;
1279 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001280 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001281 struct mr_table *mrt;
1282
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001283 if (sk->sk_type != SOCK_RAW ||
1284 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1285 return -EOPNOTSUPP;
1286
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001287 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1288 if (mrt == NULL)
1289 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001290
Stephen Hemminger132adf52007-03-08 20:44:43 -08001291 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001292 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001293 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return -EACCES;
1295 }
1296
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 switch (optname) {
1298 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001299 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001300 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Stephen Hemminger132adf52007-03-08 20:44:43 -08001302 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001303 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 return -EADDRINUSE;
1306 }
1307
1308 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1309 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001310 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001311 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001312 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1313 NETCONFA_IFINDEX_ALL,
1314 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001315 }
1316 rtnl_unlock();
1317 return ret;
1318 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001319 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001320 return -EACCES;
1321 return ip_ra_control(sk, 0, NULL);
1322 case MRT_ADD_VIF:
1323 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001324 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001325 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001326 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001327 return -EFAULT;
1328 if (vif.vifc_vifi >= MAXVIFS)
1329 return -ENFILE;
1330 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001331 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001332 ret = vif_add(net, mrt, &vif,
1333 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001334 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001335 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001336 }
1337 rtnl_unlock();
1338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /*
1341 * Manipulate the forwarding caches. These live
1342 * in a sort of kernel/user symbiosis.
1343 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001344 case MRT_ADD_MFC:
1345 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001346 parent = -1;
1347 case MRT_ADD_MFC_PROXY:
1348 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001349 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001350 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001351 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001352 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001353 if (parent == 0)
1354 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001355 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001356 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1357 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001358 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001359 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001360 sk == rtnl_dereference(mrt->mroute_sk),
1361 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001362 rtnl_unlock();
1363 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /*
1365 * Control PIM assert.
1366 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001367 case MRT_ASSERT:
1368 {
1369 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001370 if (optlen != sizeof(v))
1371 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001372 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001373 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001374 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001375 return 0;
1376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001378 case MRT_PIM:
1379 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001380 int v;
1381
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001382 if (optlen != sizeof(v))
1383 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001384 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001385 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001386 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001387
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388 rtnl_lock();
1389 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001390 if (v != mrt->mroute_do_pim) {
1391 mrt->mroute_do_pim = v;
1392 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001394 rtnl_unlock();
1395 return ret;
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001398#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1399 case MRT_TABLE:
1400 {
1401 u32 v;
1402
1403 if (optlen != sizeof(u32))
1404 return -EINVAL;
1405 if (get_user(v, (u32 __user *)optval))
1406 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001407
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001408 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1409 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1410 return -EINVAL;
1411
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001412 rtnl_lock();
1413 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001414 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1415 ret = -EBUSY;
1416 } else {
1417 if (!ipmr_new_table(net, v))
1418 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001419 else
1420 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001421 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001422 rtnl_unlock();
1423 return ret;
1424 }
1425#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001426 /*
1427 * Spurious command, or MRT_VERSION which you cannot
1428 * set.
1429 */
1430 default:
1431 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433}
1434
1435/*
1436 * Getsock opt support for the multicast routing system.
1437 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001438
Jianjun Kongc354e122008-11-03 00:28:02 -08001439int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 int olr;
1442 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001443 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001444 struct mr_table *mrt;
1445
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001446 if (sk->sk_type != SOCK_RAW ||
1447 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1448 return -EOPNOTSUPP;
1449
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001450 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1451 if (mrt == NULL)
1452 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jianjun Kongc354e122008-11-03 00:28:02 -08001454 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001456 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001458 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -ENOPROTOOPT;
1460
1461 if (get_user(olr, optlen))
1462 return -EFAULT;
1463
1464 olr = min_t(unsigned int, olr, sizeof(int));
1465 if (olr < 0)
1466 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001467
Jianjun Kongc354e122008-11-03 00:28:02 -08001468 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001470 if (optname == MRT_VERSION)
1471 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001473 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001474 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475#endif
1476 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001477 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001478 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return -EFAULT;
1480 return 0;
1481}
1482
1483/*
1484 * The IP multicast ioctl support routines.
1485 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1488{
1489 struct sioc_sg_req sr;
1490 struct sioc_vif_req vr;
1491 struct vif_device *vif;
1492 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001493 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001494 struct mr_table *mrt;
1495
1496 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1497 if (mrt == NULL)
1498 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001499
Stephen Hemminger132adf52007-03-08 20:44:43 -08001500 switch (cmd) {
1501 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001502 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001503 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001504 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001505 return -EINVAL;
1506 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001507 vif = &mrt->vif_table[vr.vifi];
1508 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001509 vr.icount = vif->pkt_in;
1510 vr.ocount = vif->pkt_out;
1511 vr.ibytes = vif->bytes_in;
1512 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001514
Jianjun Kongc354e122008-11-03 00:28:02 -08001515 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001517 return 0;
1518 }
1519 read_unlock(&mrt_lock);
1520 return -EADDRNOTAVAIL;
1521 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001522 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001523 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Eric Dumazeta8c94862010-10-01 16:15:08 +00001525 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001526 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001527 if (c) {
1528 sr.pktcnt = c->mfc_un.res.pkt;
1529 sr.bytecnt = c->mfc_un.res.bytes;
1530 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001531 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001532
Jianjun Kongc354e122008-11-03 00:28:02 -08001533 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001534 return -EFAULT;
1535 return 0;
1536 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001537 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001538 return -EADDRNOTAVAIL;
1539 default:
1540 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542}
1543
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001544#ifdef CONFIG_COMPAT
1545struct compat_sioc_sg_req {
1546 struct in_addr src;
1547 struct in_addr grp;
1548 compat_ulong_t pktcnt;
1549 compat_ulong_t bytecnt;
1550 compat_ulong_t wrong_if;
1551};
1552
David S. Millerca6b8bb02011-02-03 17:24:28 -08001553struct compat_sioc_vif_req {
1554 vifi_t vifi; /* Which iface */
1555 compat_ulong_t icount;
1556 compat_ulong_t ocount;
1557 compat_ulong_t ibytes;
1558 compat_ulong_t obytes;
1559};
1560
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001561int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1562{
David S. Miller0033d5a2011-02-03 17:21:31 -08001563 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001564 struct compat_sioc_vif_req vr;
1565 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001566 struct mfc_cache *c;
1567 struct net *net = sock_net(sk);
1568 struct mr_table *mrt;
1569
1570 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1571 if (mrt == NULL)
1572 return -ENOENT;
1573
1574 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001575 case SIOCGETVIFCNT:
1576 if (copy_from_user(&vr, arg, sizeof(vr)))
1577 return -EFAULT;
1578 if (vr.vifi >= mrt->maxvif)
1579 return -EINVAL;
1580 read_lock(&mrt_lock);
1581 vif = &mrt->vif_table[vr.vifi];
1582 if (VIF_EXISTS(mrt, vr.vifi)) {
1583 vr.icount = vif->pkt_in;
1584 vr.ocount = vif->pkt_out;
1585 vr.ibytes = vif->bytes_in;
1586 vr.obytes = vif->bytes_out;
1587 read_unlock(&mrt_lock);
1588
1589 if (copy_to_user(arg, &vr, sizeof(vr)))
1590 return -EFAULT;
1591 return 0;
1592 }
1593 read_unlock(&mrt_lock);
1594 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001595 case SIOCGETSGCNT:
1596 if (copy_from_user(&sr, arg, sizeof(sr)))
1597 return -EFAULT;
1598
1599 rcu_read_lock();
1600 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1601 if (c) {
1602 sr.pktcnt = c->mfc_un.res.pkt;
1603 sr.bytecnt = c->mfc_un.res.bytes;
1604 sr.wrong_if = c->mfc_un.res.wrong_if;
1605 rcu_read_unlock();
1606
1607 if (copy_to_user(arg, &sr, sizeof(sr)))
1608 return -EFAULT;
1609 return 0;
1610 }
1611 rcu_read_unlock();
1612 return -EADDRNOTAVAIL;
1613 default:
1614 return -ENOIOCTLCMD;
1615 }
1616}
1617#endif
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1621{
Jiri Pirko351638e2013-05-28 01:30:21 +00001622 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001623 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001624 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 struct vif_device *v;
1626 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (event != NETDEV_UNREGISTER)
1629 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001630
1631 ipmr_for_each_table(mrt, net) {
1632 v = &mrt->vif_table[0];
1633 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1634 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001635 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638 return NOTIFY_DONE;
1639}
1640
1641
Jianjun Kongc354e122008-11-03 00:28:02 -08001642static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 .notifier_call = ipmr_device_event,
1644};
1645
1646/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001647 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 * This avoids tunnel drivers and other mess and gives us the speed so
1649 * important for multicast video.
1650 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001651
Al Viro114c7842006-09-27 18:39:29 -07001652static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001654 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001655 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001656
1657 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001658 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001659 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001660 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001662 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001663 iph->tos = old_iph->tos;
1664 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 iph->frag_off = 0;
1666 iph->daddr = daddr;
1667 iph->saddr = saddr;
1668 iph->protocol = IPPROTO_IPIP;
1669 iph->ihl = 5;
1670 iph->tot_len = htons(skb->len);
Eric Dumazet73f156a2014-06-02 05:26:03 -07001671 ip_select_ident(skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 ip_send_check(iph);
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1675 nf_reset(skb);
1676}
1677
1678static inline int ipmr_forward_finish(struct sk_buff *skb)
1679{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001680 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Eric Dumazetadf30902009-06-02 05:19:30 +00001682 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Vincent Bernat2d8dbb02012-06-05 03:41:42 +00001683 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 if (unlikely(opt->optlen))
1686 ip_forward_options(skb);
1687
1688 return dst_output(skb);
1689}
1690
1691/*
1692 * Processing handlers for ipmr_forward
1693 */
1694
Patrick McHardy0c122952010-04-13 05:03:22 +00001695static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1696 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001698 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001699 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 struct net_device *dev;
1701 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001702 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 int encap = 0;
1704
1705 if (vif->dev == NULL)
1706 goto out_free;
1707
1708#ifdef CONFIG_IP_PIMSM
1709 if (vif->flags & VIFF_REGISTER) {
1710 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001711 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001712 vif->dev->stats.tx_bytes += skb->len;
1713 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001714 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001715 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717#endif
1718
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001719 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001720 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001721 vif->remote, vif->local,
1722 0, 0,
1723 IPPROTO_IPIP,
1724 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001725 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 goto out_free;
1727 encap = sizeof(struct iphdr);
1728 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001729 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001730 0, 0,
1731 IPPROTO_IPIP,
1732 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001733 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 goto out_free;
1735 }
1736
Changli Gaod8d1f302010-06-10 23:31:35 -07001737 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Changli Gaod8d1f302010-06-10 23:31:35 -07001739 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001741 * allow to send ICMP, so that packets will disappear
1742 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
1744
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001745 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 ip_rt_put(rt);
1747 goto out_free;
1748 }
1749
Changli Gaod8d1f302010-06-10 23:31:35 -07001750 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001753 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto out_free;
1755 }
1756
1757 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001758 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Eric Dumazetadf30902009-06-02 05:19:30 +00001760 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001761 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001762 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001765 * What do we do with netfilter? -- RR
1766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (vif->flags & VIFF_TUNNEL) {
1768 ip_encap(skb, vif->local, vif->remote);
1769 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001770 vif->dev->stats.tx_packets++;
1771 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
1774 IPCB(skb)->flags |= IPSKB_FORWARDED;
1775
1776 /*
1777 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1778 * not only before forwarding, but after forwarding on all output
1779 * interfaces. It is clear, if mrouter runs a multicasting
1780 * program, it should receive packets not depending to what interface
1781 * program is joined.
1782 * If we will not make it, the program will have to join on all
1783 * interfaces. On the other hand, multihoming host (or router, but
1784 * not mrouter) cannot join to more than one interface - it will
1785 * result in receiving multiple packets.
1786 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001787 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 ipmr_forward_finish);
1789 return;
1790
1791out_free:
1792 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Patrick McHardy0c122952010-04-13 05:03:22 +00001795static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001798
1799 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1800 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 break;
1802 }
1803 return ct;
1804}
1805
1806/* "local" means that we should preserve one skb (for local delivery) */
1807
Rami Rosenc4854ec2013-07-20 15:09:28 +03001808static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1809 struct sk_buff *skb, struct mfc_cache *cache,
1810 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 int psend = -1;
1813 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001814 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 vif = cache->mfc_parent;
1817 cache->mfc_un.res.pkt++;
1818 cache->mfc_un.res.bytes += skb->len;
1819
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001820 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001821 struct mfc_cache *cache_proxy;
1822
1823 /* For an (*,G) entry, we only check that the incomming
1824 * interface is part of the static tree.
1825 */
1826 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1827 if (cache_proxy &&
1828 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1829 goto forward;
1830 }
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /*
1833 * Wrong interface: drop packet and (maybe) send PIM assert.
1834 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001835 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001836 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001838 * Very complicated situation...
1839 *
1840 * The best workaround until routing daemons will be
1841 * fixed is not to redistribute packet, if it was
1842 * send through wrong interface. It means, that
1843 * multicast applications WILL NOT work for
1844 * (S,G), which have default multicast route pointing
1845 * to wrong oif. In any case, it is not a good
1846 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 */
1848 goto dont_forward;
1849 }
1850
1851 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Patrick McHardy0c122952010-04-13 05:03:22 +00001853 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001855 * so that we cannot check that packet arrived on an oif.
1856 * It is bad, but otherwise we would need to move pretty
1857 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001859 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001860 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001861 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1863 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001864 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
1866 goto dont_forward;
1867 }
1868
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001869forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001870 mrt->vif_table[vif].pkt_in++;
1871 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 /*
1874 * Forward the frame
1875 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001876 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1877 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001878 if (true_vifi >= 0 &&
1879 true_vifi != cache->mfc_parent &&
1880 ip_hdr(skb)->ttl >
1881 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1882 /* It's an (*,*) entry and the packet is not coming from
1883 * the upstream: forward the packet to the upstream
1884 * only.
1885 */
1886 psend = cache->mfc_parent;
1887 goto last_forward;
1888 }
1889 goto dont_forward;
1890 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001891 for (ct = cache->mfc_un.res.maxvif - 1;
1892 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001893 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001894 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1895 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001896 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (psend != -1) {
1898 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001901 ipmr_queue_xmit(net, mrt, skb2, cache,
1902 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001904 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001907last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (psend != -1) {
1909 if (local) {
1910 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001913 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001915 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001916 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918 }
1919
1920dont_forward:
1921 if (!local)
1922 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
David S. Miller417da662011-05-03 19:42:43 -07001925static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001926{
David S. Miller417da662011-05-03 19:42:43 -07001927 struct rtable *rt = skb_rtable(skb);
1928 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001929 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001930 .daddr = iph->daddr,
1931 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001932 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001933 .flowi4_oif = (rt_is_output_route(rt) ?
1934 skb->dev->ifindex : 0),
1935 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001936 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001937 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001938 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001939 };
1940 struct mr_table *mrt;
1941 int err;
1942
David S. Millerda919812011-03-12 02:04:50 -05001943 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001944 if (err)
1945 return ERR_PTR(err);
1946 return mrt;
1947}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949/*
1950 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001951 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 */
1953
1954int ip_mr_input(struct sk_buff *skb)
1955{
1956 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001957 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001958 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001959 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001962 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001964 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 goto dont_forward;
1966
David S. Miller417da662011-05-03 19:42:43 -07001967 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001968 if (IS_ERR(mrt)) {
1969 kfree_skb(skb);
1970 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001973 if (IPCB(skb)->opt.router_alert) {
1974 if (ip_call_ra_chain(skb))
1975 return 0;
1976 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1977 /* IGMPv1 (and broken IGMPv2 implementations sort of
1978 * Cisco IOS <= 11.2(8)) do not put router alert
1979 * option to IGMP packets destined to routable
1980 * groups. It is very bad, because it means
1981 * that we can forward NO IGMP messages.
1982 */
1983 struct sock *mroute_sk;
1984
1985 mroute_sk = rcu_dereference(mrt->mroute_sk);
1986 if (mroute_sk) {
1987 nf_reset(skb);
1988 raw_rcv(mroute_sk, skb);
1989 return 0;
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
1992 }
1993
Eric Dumazeta8c94862010-10-01 16:15:08 +00001994 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001995 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001996 if (cache == NULL) {
1997 int vif = ipmr_find_vif(mrt, skb->dev);
1998
1999 if (vif >= 0)
2000 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2001 vif);
2002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /*
2005 * No usable cache entry
2006 */
Jianjun Kongc354e122008-11-03 00:28:02 -08002007 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int vif;
2009
2010 if (local) {
2011 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2012 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002013 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 skb = skb2;
2016 }
2017
Eric Dumazeta8c94862010-10-01 16:15:08 +00002018 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002019 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002021 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 read_unlock(&mrt_lock);
2023
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002024 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026 read_unlock(&mrt_lock);
2027 kfree_skb(skb);
2028 return -ENODEV;
2029 }
2030
Eric Dumazeta8c94862010-10-01 16:15:08 +00002031 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002032 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 read_unlock(&mrt_lock);
2034
2035 if (local)
2036 return ip_local_deliver(skb);
2037
2038 return 0;
2039
2040dont_forward:
2041 if (local)
2042 return ip_local_deliver(skb);
2043 kfree_skb(skb);
2044 return 0;
2045}
2046
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002047#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00002048/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002049static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2050 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002052 struct net_device *reg_dev = NULL;
2053 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002055 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002057 * Check that:
2058 * a. packet is really sent to a multicast group
2059 * b. packet is not a NULL-REGISTER
2060 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002062 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002064 ntohs(encap->tot_len) + pimlen > skb->len)
2065 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002068 if (mrt->mroute_reg_vif_num >= 0)
2069 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 read_unlock(&mrt_lock);
2071
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002072 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002073 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002075 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00002076 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03002077 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00002079 skb->ip_summed = CHECKSUM_NONE;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002080
Nicolas Dichtelea231922013-09-02 15:34:58 +02002081 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002084
Eric Dumazet55747a02010-10-01 16:14:55 +00002085 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002086}
2087#endif
2088
2089#ifdef CONFIG_IP_PIMSM_V1
2090/*
2091 * Handle IGMP messages of PIMv1
2092 */
2093
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002094int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002095{
2096 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002097 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002098 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002099
2100 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2101 goto drop;
2102
2103 pim = igmp_hdr(skb);
2104
David S. Miller417da662011-05-03 19:42:43 -07002105 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002106 if (IS_ERR(mrt))
2107 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002108 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002109 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2110 goto drop;
2111
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002112 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002113drop:
2114 kfree_skb(skb);
2115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return 0;
2117}
2118#endif
2119
2120#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002121static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002124 struct net *net = dev_net(skb->dev);
2125 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002127 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 goto drop;
2129
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002130 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002131 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2132 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002133 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002134 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 goto drop;
2136
David S. Miller417da662011-05-03 19:42:43 -07002137 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002138 if (IS_ERR(mrt))
2139 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002140 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002141drop:
2142 kfree_skb(skb);
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return 0;
2145}
2146#endif
2147
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002148static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2149 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 int ct;
2152 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002153 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002154 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Nicolas Dichtel74381892010-03-25 23:45:35 +00002156 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002157 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002158 return -ENOENT;
2159
Thomas Graf92a395e2012-06-26 23:36:13 +00002160 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2161 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2162 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Thomas Graf92a395e2012-06-26 23:36:13 +00002164 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2165 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002168 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002169 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2170 nla_nest_cancel(skb, mp_attr);
2171 return -EMSGSIZE;
2172 }
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 nhp->rtnh_flags = 0;
2175 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002176 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 nhp->rtnh_len = sizeof(*nhp);
2178 }
2179 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002180
2181 nla_nest_end(skb, mp_attr);
2182
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002183 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2184 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2185 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2186 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2187 return -EMSGSIZE;
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 rtm->rtm_type = RTN_MULTICAST;
2190 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
David S. Miller9a1b9492011-05-04 12:18:54 -07002193int ipmr_get_route(struct net *net, struct sk_buff *skb,
2194 __be32 saddr, __be32 daddr,
2195 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002198 struct mr_table *mrt;
2199 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002201 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2202 if (mrt == NULL)
2203 return -ENOENT;
2204
Eric Dumazeta8c94862010-10-01 16:15:08 +00002205 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002206 cache = ipmr_cache_find(mrt, saddr, daddr);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002207 if (cache == NULL && skb->dev) {
2208 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002210 if (vif >= 0)
2211 cache = ipmr_cache_find_any(mrt, daddr, vif);
2212 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002213 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002214 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002215 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002217 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002220 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return -EAGAIN;
2222 }
2223
2224 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002225 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002226 if (dev)
2227 vif = ipmr_find_vif(mrt, dev);
2228 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002230 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return -ENODEV;
2232 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002233 skb2 = skb_clone(skb, GFP_ATOMIC);
2234 if (!skb2) {
2235 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002236 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002237 return -ENOMEM;
2238 }
2239
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002240 skb_push(skb2, sizeof(struct iphdr));
2241 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002242 iph = ip_hdr(skb2);
2243 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002244 iph->saddr = saddr;
2245 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002246 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002247 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002249 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return err;
2251 }
2252
Eric Dumazeta8c94862010-10-01 16:15:08 +00002253 read_lock(&mrt_lock);
2254 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002256 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002258 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return err;
2260}
2261
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002262static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002263 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2264 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002265{
2266 struct nlmsghdr *nlh;
2267 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002268 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002269
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002270 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002271 if (nlh == NULL)
2272 return -EMSGSIZE;
2273
2274 rtm = nlmsg_data(nlh);
2275 rtm->rtm_family = RTNL_FAMILY_IPMR;
2276 rtm->rtm_dst_len = 32;
2277 rtm->rtm_src_len = 32;
2278 rtm->rtm_tos = 0;
2279 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002280 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2281 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002282 rtm->rtm_type = RTN_MULTICAST;
2283 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002284 if (c->mfc_flags & MFC_STATIC)
2285 rtm->rtm_protocol = RTPROT_STATIC;
2286 else
2287 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002288 rtm->rtm_flags = 0;
2289
David S. Millerf3756b72012-04-01 20:39:02 -04002290 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2291 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2292 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002293 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2294 /* do not break the dump if cache is unresolved */
2295 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002296 goto nla_put_failure;
2297
Johannes Berg053c0952015-01-16 22:09:00 +01002298 nlmsg_end(skb, nlh);
2299 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002300
2301nla_put_failure:
2302 nlmsg_cancel(skb, nlh);
2303 return -EMSGSIZE;
2304}
2305
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002306static size_t mroute_msgsize(bool unresolved, int maxvif)
2307{
2308 size_t len =
2309 NLMSG_ALIGN(sizeof(struct rtmsg))
2310 + nla_total_size(4) /* RTA_TABLE */
2311 + nla_total_size(4) /* RTA_SRC */
2312 + nla_total_size(4) /* RTA_DST */
2313 ;
2314
2315 if (!unresolved)
2316 len = len
2317 + nla_total_size(4) /* RTA_IIF */
2318 + nla_total_size(0) /* RTA_MULTIPATH */
2319 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2320 /* RTA_MFC_STATS */
2321 + nla_total_size(sizeof(struct rta_mfc_stats))
2322 ;
2323
2324 return len;
2325}
2326
2327static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2328 int cmd)
2329{
2330 struct net *net = read_pnet(&mrt->net);
2331 struct sk_buff *skb;
2332 int err = -ENOBUFS;
2333
2334 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2335 GFP_ATOMIC);
2336 if (skb == NULL)
2337 goto errout;
2338
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002339 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002340 if (err < 0)
2341 goto errout;
2342
2343 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2344 return;
2345
2346errout:
2347 kfree_skb(skb);
2348 if (err < 0)
2349 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2350}
2351
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002352static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2353{
2354 struct net *net = sock_net(skb->sk);
2355 struct mr_table *mrt;
2356 struct mfc_cache *mfc;
2357 unsigned int t = 0, s_t;
2358 unsigned int h = 0, s_h;
2359 unsigned int e = 0, s_e;
2360
2361 s_t = cb->args[0];
2362 s_h = cb->args[1];
2363 s_e = cb->args[2];
2364
Eric Dumazeta8c94862010-10-01 16:15:08 +00002365 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002366 ipmr_for_each_table(mrt, net) {
2367 if (t < s_t)
2368 goto next_table;
2369 if (t > s_t)
2370 s_h = 0;
2371 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002372 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002373 if (e < s_e)
2374 goto next_entry;
2375 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002376 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002377 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002378 mfc, RTM_NEWROUTE,
2379 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002380 goto done;
2381next_entry:
2382 e++;
2383 }
2384 e = s_e = 0;
2385 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002386 spin_lock_bh(&mfc_unres_lock);
2387 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2388 if (e < s_e)
2389 goto next_entry2;
2390 if (ipmr_fill_mroute(mrt, skb,
2391 NETLINK_CB(cb->skb).portid,
2392 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002393 mfc, RTM_NEWROUTE,
2394 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002395 spin_unlock_bh(&mfc_unres_lock);
2396 goto done;
2397 }
2398next_entry2:
2399 e++;
2400 }
2401 spin_unlock_bh(&mfc_unres_lock);
2402 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002403 s_h = 0;
2404next_table:
2405 t++;
2406 }
2407done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002408 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002409
2410 cb->args[2] = e;
2411 cb->args[1] = h;
2412 cb->args[0] = t;
2413
2414 return skb->len;
2415}
2416
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002417#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002419 * The /proc interfaces to multicast routing :
2420 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 */
2422struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002423 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002424 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 int ct;
2426};
2427
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002428static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2429 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 loff_t pos)
2431{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002432 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002433
2434 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2435 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002437 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002438 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440 return NULL;
2441}
2442
2443static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002444 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002446 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002447 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002448 struct mr_table *mrt;
2449
2450 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2451 if (mrt == NULL)
2452 return ERR_PTR(-ENOENT);
2453
2454 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002457 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 : SEQ_START_TOKEN;
2459}
2460
2461static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2462{
2463 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002464 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002465 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 ++*pos;
2468 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002469 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002470
Patrick McHardy0c122952010-04-13 05:03:22 +00002471 while (++iter->ct < mrt->maxvif) {
2472 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002474 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476 return NULL;
2477}
2478
2479static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002480 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
2482 read_unlock(&mrt_lock);
2483}
2484
2485static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2486{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002487 struct ipmr_vif_iter *iter = seq->private;
2488 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002491 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2493 } else {
2494 const struct vif_device *vif = v;
2495 const char *name = vif->dev ? vif->dev->name : "none";
2496
2497 seq_printf(seq,
2498 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002499 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002500 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 vif->bytes_out, vif->pkt_out,
2502 vif->flags, vif->local, vif->remote);
2503 }
2504 return 0;
2505}
2506
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002507static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 .start = ipmr_vif_seq_start,
2509 .next = ipmr_vif_seq_next,
2510 .stop = ipmr_vif_seq_stop,
2511 .show = ipmr_vif_seq_show,
2512};
2513
2514static int ipmr_vif_open(struct inode *inode, struct file *file)
2515{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002516 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2517 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
Arjan van de Ven9a321442007-02-12 00:55:35 -08002520static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 .owner = THIS_MODULE,
2522 .open = ipmr_vif_open,
2523 .read = seq_read,
2524 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002525 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526};
2527
2528struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002529 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002530 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002531 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 int ct;
2533};
2534
2535
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002536static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2537 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002539 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 struct mfc_cache *mfc;
2541
Eric Dumazeta8c94862010-10-01 16:15:08 +00002542 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002543 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002544 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002545 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002546 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002548 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002549 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002552 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002553 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002554 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return mfc;
2556 spin_unlock_bh(&mfc_unres_lock);
2557
2558 it->cache = NULL;
2559 return NULL;
2560}
2561
2562
2563static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2564{
2565 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002566 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002567 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002568
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002569 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2570 if (mrt == NULL)
2571 return ERR_PTR(-ENOENT);
2572
2573 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 it->cache = NULL;
2575 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002576 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 : SEQ_START_TOKEN;
2578}
2579
2580static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2581{
2582 struct mfc_cache *mfc = v;
2583 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002584 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002585 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 ++*pos;
2588
2589 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002590 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Patrick McHardy862465f2010-04-13 05:03:21 +00002592 if (mfc->list.next != it->cache)
2593 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002594
Patrick McHardy0c122952010-04-13 05:03:22 +00002595 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 goto end_of_list;
2597
Patrick McHardy0c122952010-04-13 05:03:22 +00002598 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002601 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002602 if (list_empty(it->cache))
2603 continue;
2604 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
2606
2607 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002608 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002609 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002613 if (!list_empty(it->cache))
2614 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002616end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 spin_unlock_bh(&mfc_unres_lock);
2618 it->cache = NULL;
2619
2620 return NULL;
2621}
2622
2623static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2624{
2625 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002626 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Patrick McHardy0c122952010-04-13 05:03:22 +00002628 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002630 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002631 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
2634static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2635{
2636 int n;
2637
2638 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002639 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2641 } else {
2642 const struct mfc_cache *mfc = v;
2643 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002644 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002645
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002646 seq_printf(seq, "%08X %08X %-3hd",
2647 (__force u32) mfc->mfc_mcastgrp,
2648 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002649 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Patrick McHardy0c122952010-04-13 05:03:22 +00002651 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002652 seq_printf(seq, " %8lu %8lu %8lu",
2653 mfc->mfc_un.res.pkt,
2654 mfc->mfc_un.res.bytes,
2655 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002656 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002657 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002658 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002659 mfc->mfc_un.res.ttls[n] < 255)
2660 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002661 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 n, mfc->mfc_un.res.ttls[n]);
2663 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002664 } else {
2665 /* unresolved mfc_caches don't contain
2666 * pkt, bytes and wrong_if values
2667 */
2668 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670 seq_putc(seq, '\n');
2671 }
2672 return 0;
2673}
2674
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002675static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 .start = ipmr_mfc_seq_start,
2677 .next = ipmr_mfc_seq_next,
2678 .stop = ipmr_mfc_seq_stop,
2679 .show = ipmr_mfc_seq_show,
2680};
2681
2682static int ipmr_mfc_open(struct inode *inode, struct file *file)
2683{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002684 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2685 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
Arjan van de Ven9a321442007-02-12 00:55:35 -08002688static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 .owner = THIS_MODULE,
2690 .open = ipmr_mfc_open,
2691 .read = seq_read,
2692 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002693 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002698static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002700 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701};
2702#endif
2703
2704
2705/*
2706 * Setup for IP multicast routing
2707 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002708static int __net_init ipmr_net_init(struct net *net)
2709{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002710 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002711
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002712 err = ipmr_rules_init(net);
2713 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002714 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002715
2716#ifdef CONFIG_PROC_FS
2717 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002718 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002719 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002720 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002721 goto proc_cache_fail;
2722#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002723 return 0;
2724
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002725#ifdef CONFIG_PROC_FS
2726proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002727 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002728proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002729 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002730#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002731fail:
2732 return err;
2733}
2734
2735static void __net_exit ipmr_net_exit(struct net *net)
2736{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002737#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002738 remove_proc_entry("ip_mr_cache", net->proc_net);
2739 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002740#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002741 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002742}
2743
2744static struct pernet_operations ipmr_net_ops = {
2745 .init = ipmr_net_init,
2746 .exit = ipmr_net_exit,
2747};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002748
Wang Chen03d2f892008-07-03 12:13:36 +08002749int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
Wang Chen03d2f892008-07-03 12:13:36 +08002751 int err;
2752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2754 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002755 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002756 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002757 if (!mrt_cachep)
2758 return -ENOMEM;
2759
Benjamin Therycf958ae32009-01-22 04:56:16 +00002760 err = register_pernet_subsys(&ipmr_net_ops);
2761 if (err)
2762 goto reg_pernet_fail;
2763
Wang Chen03d2f892008-07-03 12:13:36 +08002764 err = register_netdevice_notifier(&ip_mr_notifier);
2765 if (err)
2766 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002767#ifdef CONFIG_IP_PIMSM_V2
2768 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002769 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002770 err = -EAGAIN;
2771 goto add_proto_fail;
2772 }
2773#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002774 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2775 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002776 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002777
Tom Goff403dbb92009-06-14 03:16:13 -07002778#ifdef CONFIG_IP_PIMSM_V2
2779add_proto_fail:
2780 unregister_netdevice_notifier(&ip_mr_notifier);
2781#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002782reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002783 unregister_pernet_subsys(&ipmr_net_ops);
2784reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002785 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002786 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}