blob: a9454cbd953ce700139fdb010a333128b565b800 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/ipip.h>
65#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 Dumazet4c9687092010-10-01 16:15:01 +000080 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000081 struct timer_list ipmr_expire_timer;
82 struct list_head mfc_unres_queue;
83 struct list_head mfc_cache_array[MFC_LINES];
84 struct vif_device vif_table[MAXVIFS];
85 int maxvif;
86 atomic_t cache_resolve_queue_len;
Joe Perches53d68412012-11-25 09:35:30 +000087 bool mroute_do_assert;
88 bool mroute_do_pim;
Patrick McHardy0c122952010-04-13 05:03:22 +000089#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num;
91#endif
92};
93
Patrick McHardyf0ad0862010-04-13 05:03:23 +000094struct ipmr_rule {
95 struct fib_rule common;
96};
97
98struct ipmr_result {
99 struct mr_table *mrt;
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000103 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105
106static DEFINE_RWLOCK(mrt_lock);
107
108/*
109 * Multicast router control variables
110 */
111
Patrick McHardy0c122952010-04-13 05:03:22 +0000112#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* Special spinlock for queue of unresolved entries */
115static DEFINE_SPINLOCK(mfc_unres_lock);
116
117/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
121 *
122 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124
Christoph Lametere18b8902006-12-06 20:33:20 -0800125static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000127static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000128static void ipmr_free_table(struct mr_table *mrt);
129
Patrick McHardy0c122952010-04-13 05:03:22 +0000130static int ip_mr_forward(struct net *net, struct mr_table *mrt,
131 struct sk_buff *skb, struct mfc_cache *cache,
132 int local);
133static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000134 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200135static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000137static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
138 int cmd);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000139static void mroute_clean_tables(struct mr_table *mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000140static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000142#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
143#define ipmr_for_each_table(mrt, net) \
144 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
145
146static struct mr_table *ipmr_get_table(struct net *net, u32 id)
147{
148 struct mr_table *mrt;
149
150 ipmr_for_each_table(mrt, net) {
151 if (mrt->id == id)
152 return mrt;
153 }
154 return NULL;
155}
156
David S. Millerda919812011-03-12 02:04:50 -0500157static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000158 struct mr_table **mrt)
159{
160 struct ipmr_result res;
161 struct fib_lookup_arg arg = { .result = &res, };
162 int err;
163
David S. Millerda919812011-03-12 02:04:50 -0500164 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
165 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000166 if (err < 0)
167 return err;
168 *mrt = res.mrt;
169 return 0;
170}
171
172static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
173 int flags, struct fib_lookup_arg *arg)
174{
175 struct ipmr_result *res = arg->result;
176 struct mr_table *mrt;
177
178 switch (rule->action) {
179 case FR_ACT_TO_TBL:
180 break;
181 case FR_ACT_UNREACHABLE:
182 return -ENETUNREACH;
183 case FR_ACT_PROHIBIT:
184 return -EACCES;
185 case FR_ACT_BLACKHOLE:
186 default:
187 return -EINVAL;
188 }
189
190 mrt = ipmr_get_table(rule->fr_net, rule->table);
191 if (mrt == NULL)
192 return -EAGAIN;
193 res->mrt = mrt;
194 return 0;
195}
196
197static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
198{
199 return 1;
200}
201
202static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
203 FRA_GENERIC_POLICY,
204};
205
206static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
207 struct fib_rule_hdr *frh, struct nlattr **tb)
208{
209 return 0;
210}
211
212static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
213 struct nlattr **tb)
214{
215 return 1;
216}
217
218static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
219 struct fib_rule_hdr *frh)
220{
221 frh->dst_len = 0;
222 frh->src_len = 0;
223 frh->tos = 0;
224 return 0;
225}
226
Andi Kleen04a6f822012-10-04 17:12:11 -0700227static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200228 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000229 .rule_size = sizeof(struct ipmr_rule),
230 .addr_size = sizeof(u32),
231 .action = ipmr_rule_action,
232 .match = ipmr_rule_match,
233 .configure = ipmr_rule_configure,
234 .compare = ipmr_rule_compare,
235 .default_pref = fib_default_rule_pref,
236 .fill = ipmr_rule_fill,
237 .nlgroup = RTNLGRP_IPV4_RULE,
238 .policy = ipmr_rule_policy,
239 .owner = THIS_MODULE,
240};
241
242static int __net_init ipmr_rules_init(struct net *net)
243{
244 struct fib_rules_ops *ops;
245 struct mr_table *mrt;
246 int err;
247
248 ops = fib_rules_register(&ipmr_rules_ops_template, net);
249 if (IS_ERR(ops))
250 return PTR_ERR(ops);
251
252 INIT_LIST_HEAD(&net->ipv4.mr_tables);
253
254 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
255 if (mrt == NULL) {
256 err = -ENOMEM;
257 goto err1;
258 }
259
260 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
261 if (err < 0)
262 goto err2;
263
264 net->ipv4.mr_rules_ops = ops;
265 return 0;
266
267err2:
268 kfree(mrt);
269err1:
270 fib_rules_unregister(ops);
271 return err;
272}
273
274static void __net_exit ipmr_rules_exit(struct net *net)
275{
276 struct mr_table *mrt, *next;
277
Eric Dumazet035320d2010-06-06 23:48:40 +0000278 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
279 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000280 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000281 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000282 fib_rules_unregister(net->ipv4.mr_rules_ops);
283}
284#else
285#define ipmr_for_each_table(mrt, net) \
286 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
287
288static struct mr_table *ipmr_get_table(struct net *net, u32 id)
289{
290 return net->ipv4.mrt;
291}
292
David S. Millerda919812011-03-12 02:04:50 -0500293static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000294 struct mr_table **mrt)
295{
296 *mrt = net->ipv4.mrt;
297 return 0;
298}
299
300static int __net_init ipmr_rules_init(struct net *net)
301{
302 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
303 return net->ipv4.mrt ? 0 : -ENOMEM;
304}
305
306static void __net_exit ipmr_rules_exit(struct net *net)
307{
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000308 ipmr_free_table(net->ipv4.mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000309}
310#endif
311
312static struct mr_table *ipmr_new_table(struct net *net, u32 id)
313{
314 struct mr_table *mrt;
315 unsigned int i;
316
317 mrt = ipmr_get_table(net, id);
318 if (mrt != NULL)
319 return mrt;
320
321 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
322 if (mrt == NULL)
323 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200324 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000325 mrt->id = id;
326
327 /* Forwarding cache */
328 for (i = 0; i < MFC_LINES; i++)
329 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
330
331 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
332
333 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
334 (unsigned long)mrt);
335
336#ifdef CONFIG_IP_PIMSM
337 mrt->mroute_reg_vif_num = -1;
338#endif
339#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
340 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
341#endif
342 return mrt;
343}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000345static void ipmr_free_table(struct mr_table *mrt)
346{
347 del_timer_sync(&mrt->ipmr_expire_timer);
348 mroute_clean_tables(mrt);
349 kfree(mrt);
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
353
Wang Chend6070322008-07-14 20:55:26 -0700354static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
355{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000356 struct net *net = dev_net(dev);
357
Wang Chend6070322008-07-14 20:55:26 -0700358 dev_close(dev);
359
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000360 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700361 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800362 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700363 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700364 struct ip_tunnel_parm p;
365
366 memset(&p, 0, sizeof(p));
367 p.iph.daddr = v->vifc_rmt_addr.s_addr;
368 p.iph.saddr = v->vifc_lcl_addr.s_addr;
369 p.iph.version = 4;
370 p.iph.ihl = 5;
371 p.iph.protocol = IPPROTO_IPIP;
372 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
373 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
374
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800375 if (ops->ndo_do_ioctl) {
376 mm_segment_t oldfs = get_fs();
377
378 set_fs(KERNEL_DS);
379 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
380 set_fs(oldfs);
381 }
Wang Chend6070322008-07-14 20:55:26 -0700382 }
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000386struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct net_device *dev;
389
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000390 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800393 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int err;
395 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct ip_tunnel_parm p;
397 struct in_device *in_dev;
398
399 memset(&p, 0, sizeof(p));
400 p.iph.daddr = v->vifc_rmt_addr.s_addr;
401 p.iph.saddr = v->vifc_lcl_addr.s_addr;
402 p.iph.version = 4;
403 p.iph.ihl = 5;
404 p.iph.protocol = IPPROTO_IPIP;
405 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800406 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800408 if (ops->ndo_do_ioctl) {
409 mm_segment_t oldfs = get_fs();
410
411 set_fs(KERNEL_DS);
412 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
413 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000414 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800415 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 dev = NULL;
418
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000419 if (err == 0 &&
420 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dev->flags |= IFF_MULTICAST;
422
Herbert Xue5ed6392005-10-03 14:35:55 -0700423 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700424 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700426
427 ipv4_devconf_setall(in_dev);
428 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (dev_open(dev))
431 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700432 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 }
435 return dev;
436
437failure:
438 /* allow the register to be completed before unregistering. */
439 rtnl_unlock();
440 rtnl_lock();
441
442 unregister_netdevice(dev);
443 return NULL;
444}
445
446#ifdef CONFIG_IP_PIMSM
447
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000448static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000450 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000451 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500452 struct flowi4 fl4 = {
453 .flowi4_oif = dev->ifindex,
454 .flowi4_iif = skb->skb_iif,
455 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000456 };
457 int err;
458
David S. Millerda919812011-03-12 02:04:50 -0500459 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000460 if (err < 0) {
461 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000462 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000463 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700466 dev->stats.tx_bytes += skb->len;
467 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000468 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 read_unlock(&mrt_lock);
470 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000471 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Stephen Hemminger007c3832008-11-20 20:28:35 -0800474static const struct net_device_ops reg_vif_netdev_ops = {
475 .ndo_start_xmit = reg_vif_xmit,
476};
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static void reg_vif_setup(struct net_device *dev)
479{
480 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800481 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800483 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700485 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000488static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct net_device *dev;
491 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000492 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000494 if (mrt->id == RT_TABLE_DEFAULT)
495 sprintf(name, "pimreg");
496 else
497 sprintf(name, "pimreg%u", mrt->id);
498
499 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (dev == NULL)
502 return NULL;
503
Tom Goff403dbb92009-06-14 03:16:13 -0700504 dev_net_set(dev, net);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (register_netdevice(dev)) {
507 free_netdev(dev);
508 return NULL;
509 }
510 dev->iflink = 0;
511
Herbert Xu71e27da2007-06-04 23:36:06 -0700512 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000513 in_dev = __in_dev_get_rcu(dev);
514 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700515 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Herbert Xu71e27da2007-06-04 23:36:06 -0700519 ipv4_devconf_setall(in_dev);
520 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
521 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if (dev_open(dev))
524 goto failure;
525
Wang Chen7dc00c82008-07-14 20:56:34 -0700526 dev_hold(dev);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return dev;
529
530failure:
531 /* allow the register to be completed before unregistering. */
532 rtnl_unlock();
533 rtnl_lock();
534
535 unregister_netdevice(dev);
536 return NULL;
537}
538#endif
539
Ben Hutchings2c530402012-07-10 10:55:09 +0000540/**
541 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700542 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900544
Patrick McHardy0c122952010-04-13 05:03:22 +0000545static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000546 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct vif_device *v;
549 struct net_device *dev;
550 struct in_device *in_dev;
551
Patrick McHardy0c122952010-04-13 05:03:22 +0000552 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -EADDRNOTAVAIL;
554
Patrick McHardy0c122952010-04-13 05:03:22 +0000555 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 write_lock_bh(&mrt_lock);
558 dev = v->dev;
559 v->dev = NULL;
560
561 if (!dev) {
562 write_unlock_bh(&mrt_lock);
563 return -EADDRNOTAVAIL;
564 }
565
566#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000567 if (vifi == mrt->mroute_reg_vif_num)
568 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#endif
570
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000571 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000573
574 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000575 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
577 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000578 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
581 write_unlock_bh(&mrt_lock);
582
583 dev_set_allmulti(dev, -1);
584
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000585 in_dev = __in_dev_get_rtnl(dev);
586 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700587 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000588 inet_netconf_notify_devconf(dev_net(dev),
589 NETCONFA_MC_FORWARDING,
590 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ip_rt_multicast_event(in_dev);
592 }
593
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000594 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000595 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 dev_put(dev);
598 return 0;
599}
600
Eric Dumazeta8c94862010-10-01 16:15:08 +0000601static void ipmr_cache_free_rcu(struct rcu_head *head)
602{
603 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
604
605 kmem_cache_free(mrt_cachep, c);
606}
607
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000608static inline void ipmr_cache_free(struct mfc_cache *c)
609{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000610 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000614 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
616
Patrick McHardy0c122952010-04-13 05:03:22 +0000617static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200619 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700621 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Patrick McHardy0c122952010-04-13 05:03:22 +0000623 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jianjun Kongc354e122008-11-03 00:28:02 -0800625 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700626 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
628 nlh->nlmsg_type = NLMSG_ERROR;
629 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
630 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700631 e = NLMSG_DATA(nlh);
632 e->error = -ETIMEDOUT;
633 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700634
Eric W. Biederman15e47302012-09-07 20:12:54 +0000635 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000636 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000641 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644
Patrick McHardye258beb2010-04-13 05:03:19 +0000645/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Patrick McHardye258beb2010-04-13 05:03:19 +0000647static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Patrick McHardy0c122952010-04-13 05:03:22 +0000649 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 unsigned long now;
651 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000652 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000655 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return;
657 }
658
Patrick McHardy0c122952010-04-13 05:03:22 +0000659 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto out;
661
662 now = jiffies;
663 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Patrick McHardy0c122952010-04-13 05:03:22 +0000665 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (time_after(c->mfc_un.unres.expires, now)) {
667 unsigned long interval = c->mfc_un.unres.expires - now;
668 if (interval < expires)
669 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 continue;
671 }
672
Patrick McHardy862465f2010-04-13 05:03:21 +0000673 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000674 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000675 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
Patrick McHardy0c122952010-04-13 05:03:22 +0000678 if (!list_empty(&mrt->mfc_unres_queue))
679 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681out:
682 spin_unlock(&mfc_unres_lock);
683}
684
685/* Fill oifs list. It is called under write locked mrt_lock. */
686
Patrick McHardy0c122952010-04-13 05:03:22 +0000687static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000688 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 int vifi;
691
692 cache->mfc_un.res.minvif = MAXVIFS;
693 cache->mfc_un.res.maxvif = 0;
694 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
695
Patrick McHardy0c122952010-04-13 05:03:22 +0000696 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
697 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000698 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
700 if (cache->mfc_un.res.minvif > vifi)
701 cache->mfc_un.res.minvif = vifi;
702 if (cache->mfc_un.res.maxvif <= vifi)
703 cache->mfc_un.res.maxvif = vifi + 1;
704 }
705 }
706}
707
Patrick McHardy0c122952010-04-13 05:03:22 +0000708static int vif_add(struct net *net, struct mr_table *mrt,
709 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000712 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct net_device *dev;
714 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700715 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000718 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return -EADDRINUSE;
720
721 switch (vifc->vifc_flags) {
722#ifdef CONFIG_IP_PIMSM
723 case VIFF_REGISTER:
724 /*
725 * Special Purpose VIF in PIM
726 * All the packets will be sent to the daemon
727 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000728 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000730 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!dev)
732 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700733 err = dev_set_allmulti(dev, 1);
734 if (err) {
735 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700736 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700737 return err;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900741 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000742 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!dev)
744 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700745 err = dev_set_allmulti(dev, 1);
746 if (err) {
747 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700748 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700749 return err;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000752
753 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000755 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
756 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000757 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000758 dev_put(dev);
759 return -EADDRNOTAVAIL;
760 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000761 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000762 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (!dev)
765 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700766 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700767 if (err) {
768 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700769 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
772 default:
773 return -EINVAL;
774 }
775
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000776 in_dev = __in_dev_get_rtnl(dev);
777 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000778 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000780 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700781 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000782 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
783 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 ip_rt_multicast_event(in_dev);
785
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000786 /* Fill in the VIF structures */
787
Jianjun Kongc354e122008-11-03 00:28:02 -0800788 v->rate_limit = vifc->vifc_rate_limit;
789 v->local = vifc->vifc_lcl_addr.s_addr;
790 v->remote = vifc->vifc_rmt_addr.s_addr;
791 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!mrtsock)
793 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800794 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 v->bytes_in = 0;
796 v->bytes_out = 0;
797 v->pkt_in = 0;
798 v->pkt_out = 0;
799 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000800 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 v->link = dev->iflink;
802
803 /* And finish update writing critical data */
804 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800805 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000807 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000808 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000810 if (vifi+1 > mrt->maxvif)
811 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 write_unlock_bh(&mrt_lock);
813 return 0;
814}
815
Eric Dumazeta8c94862010-10-01 16:15:08 +0000816/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000817static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000818 __be32 origin,
819 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Jianjun Kongc354e122008-11-03 00:28:02 -0800821 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct mfc_cache *c;
823
Eric Dumazeta8c94862010-10-01 16:15:08 +0000824 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000825 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
826 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000828 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831/*
832 * Allocate a multicast cache entry
833 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000834static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Jianjun Kongc354e122008-11-03 00:28:02 -0800836 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000837
838 if (c)
839 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return c;
841}
842
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000843static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Jianjun Kongc354e122008-11-03 00:28:02 -0800845 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000846
847 if (c) {
848 skb_queue_head_init(&c->mfc_un.unres.unresolved);
849 c->mfc_un.unres.expires = jiffies + 10*HZ;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return c;
852}
853
854/*
855 * A cache entry has gone into a resolved state from queued
856 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900857
Patrick McHardy0c122952010-04-13 05:03:22 +0000858static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
859 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700862 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000864 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Jianjun Kongc354e122008-11-03 00:28:02 -0800866 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700867 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
869
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200870 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000871 nlh->nlmsg_len = skb_tail_pointer(skb) -
872 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } else {
874 nlh->nlmsg_type = NLMSG_ERROR;
875 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
876 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700877 e = NLMSG_DATA(nlh);
878 e->error = -EMSGSIZE;
879 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Thomas Graf2942e902006-08-15 00:30:25 -0700881
Eric W. Biederman15e47302012-09-07 20:12:54 +0000882 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000883 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000884 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887}
888
889/*
890 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
891 * expects the following bizarre scheme.
892 *
893 * Called under mrt_lock.
894 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900895
Patrick McHardy0c122952010-04-13 05:03:22 +0000896static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000897 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300900 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct igmphdr *igmp;
902 struct igmpmsg *msg;
Eric Dumazet4c9687092010-10-01 16:15:01 +0000903 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 int ret;
905
906#ifdef CONFIG_IP_PIMSM
907 if (assert == IGMPMSG_WHOLEPKT)
908 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
909 else
910#endif
911 skb = alloc_skb(128, GFP_ATOMIC);
912
Stephen Hemminger132adf52007-03-08 20:44:43 -0800913 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -ENOBUFS;
915
916#ifdef CONFIG_IP_PIMSM
917 if (assert == IGMPMSG_WHOLEPKT) {
918 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000919 * Duplicate old header, fix ihl, length etc.
920 * And all this only to mangle msg->im_msgtype and
921 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300923 skb_push(skb, sizeof(struct iphdr));
924 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300925 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300926 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700927 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 msg->im_msgtype = IGMPMSG_WHOLEPKT;
929 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000930 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700931 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
932 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
933 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900934 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900936 {
937
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000938 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700940 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300941 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300942 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000943 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700944 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000946 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000948 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000950 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 igmp->type =
952 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000953 igmp->code = 0;
954 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700955 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Eric Dumazet4c9687092010-10-01 16:15:01 +0000958 rcu_read_lock();
959 mroute_sk = rcu_dereference(mrt->mroute_sk);
960 if (mroute_sk == NULL) {
961 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 kfree_skb(skb);
963 return -EINVAL;
964 }
965
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000966 /* Deliver to mrouted */
967
Eric Dumazet4c9687092010-10-01 16:15:01 +0000968 ret = sock_queue_rcv_skb(mroute_sk, skb);
969 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +0000970 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000971 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 kfree_skb(skb);
973 }
974
975 return ret;
976}
977
978/*
979 * Queue a packet for resolution. It gets locked cache entry!
980 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982static int
Patrick McHardy0c122952010-04-13 05:03:22 +0000983ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Patrick McHardy862465f2010-04-13 05:03:21 +0000985 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 int err;
987 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700988 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000991 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000992 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000993 c->mfc_origin == iph->saddr) {
994 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998
Patrick McHardy862465f2010-04-13 05:03:21 +0000999 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001000 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Patrick McHardy0c122952010-04-13 05:03:22 +00001002 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001003 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 spin_unlock_bh(&mfc_unres_lock);
1005
1006 kfree_skb(skb);
1007 return -ENOBUFS;
1008 }
1009
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001010 /* Fill in the new cache entry */
1011
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001012 c->mfc_parent = -1;
1013 c->mfc_origin = iph->saddr;
1014 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001016 /* Reflect first query at mrouted. */
1017
Patrick McHardy0c122952010-04-13 05:03:22 +00001018 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001019 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001020 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 out - Brad Parker
1022 */
1023 spin_unlock_bh(&mfc_unres_lock);
1024
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001025 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 kfree_skb(skb);
1027 return err;
1028 }
1029
Patrick McHardy0c122952010-04-13 05:03:22 +00001030 atomic_inc(&mrt->cache_resolve_queue_len);
1031 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001032 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
David S. Miller278554b2010-05-12 00:05:35 -07001034 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1035 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001038 /* See if we can append the packet */
1039
1040 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 kfree_skb(skb);
1042 err = -ENOBUFS;
1043 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001044 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 err = 0;
1046 }
1047
1048 spin_unlock_bh(&mfc_unres_lock);
1049 return err;
1050}
1051
1052/*
1053 * MFC cache manipulation by user space mroute daemon
1054 */
1055
Patrick McHardy0c122952010-04-13 05:03:22 +00001056static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001059 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Jianjun Kongc354e122008-11-03 00:28:02 -08001061 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Patrick McHardy0c122952010-04-13 05:03:22 +00001063 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1065 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001066 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001067 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001068 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return 0;
1070 }
1071 }
1072 return -ENOENT;
1073}
1074
Patrick McHardy0c122952010-04-13 05:03:22 +00001075static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1076 struct mfcctl *mfc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Patrick McHardy862465f2010-04-13 05:03:21 +00001078 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001080 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Patrick McHardya50436f22010-03-17 06:04:14 +00001082 if (mfc->mfcc_parent >= MAXVIFS)
1083 return -ENFILE;
1084
Jianjun Kongc354e122008-11-03 00:28:02 -08001085 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Patrick McHardy0c122952010-04-13 05:03:22 +00001087 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001089 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1090 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
Patrick McHardy862465f2010-04-13 05:03:21 +00001095 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 write_lock_bh(&mrt_lock);
1097 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001098 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (!mrtsock)
1100 c->mfc_flags |= MFC_STATIC;
1101 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001102 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return 0;
1104 }
1105
Joe Perchesf97c1e02007-12-16 13:45:43 -08001106 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return -EINVAL;
1108
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001109 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001110 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -ENOMEM;
1112
Jianjun Kongc354e122008-11-03 00:28:02 -08001113 c->mfc_origin = mfc->mfcc_origin.s_addr;
1114 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1115 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001116 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!mrtsock)
1118 c->mfc_flags |= MFC_STATIC;
1119
Eric Dumazeta8c94862010-10-01 16:15:08 +00001120 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /*
1123 * Check to see if we resolved a queued list. If so we
1124 * need to send on the frames and tidy up.
1125 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001126 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001128 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001129 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001131 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001132 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001133 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 }
1136 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001137 if (list_empty(&mrt->mfc_unres_queue))
1138 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 spin_unlock_bh(&mfc_unres_lock);
1140
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001141 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001142 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001143 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001145 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147}
1148
1149/*
1150 * Close the multicast socket, and clear the vif tables etc
1151 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001152
Patrick McHardy0c122952010-04-13 05:03:22 +00001153static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001156 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001157 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001158
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001159 /* Shut down all active vif entries */
1160
Patrick McHardy0c122952010-04-13 05:03:22 +00001161 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001162 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001163 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001165 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001167 /* Wipe the cache */
1168
Patrick McHardy862465f2010-04-13 05:03:21 +00001169 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001170 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001171 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001173 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001174 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001175 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 }
1178
Patrick McHardy0c122952010-04-13 05:03:22 +00001179 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001181 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001182 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001183 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001184 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 spin_unlock_bh(&mfc_unres_lock);
1187 }
1188}
1189
Eric Dumazet4c9687092010-10-01 16:15:01 +00001190/* called from ip_ra_control(), before an RCU grace period,
1191 * we dont need to call synchronize_rcu() here
1192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193static void mrtsock_destruct(struct sock *sk)
1194{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001195 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001196 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001199 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001200 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001201 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001202 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1203 NETCONFA_IFINDEX_ALL,
1204 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001205 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001206 mroute_clean_tables(mrt);
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 rtnl_unlock();
1210}
1211
1212/*
1213 * Socket options and virtual interface manipulation. The whole
1214 * virtual interface system is a complete heap, but unfortunately
1215 * that's how BSD mrouted happens to think. Maybe one day with a proper
1216 * MOSPF/PIM router set up we can clean this up.
1217 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001218
David S. Millerb7058842009-09-30 16:12:20 -07001219int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 int ret;
1222 struct vifctl vif;
1223 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001224 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001225 struct mr_table *mrt;
1226
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001227 if (sk->sk_type != SOCK_RAW ||
1228 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1229 return -EOPNOTSUPP;
1230
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001231 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1232 if (mrt == NULL)
1233 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001234
Stephen Hemminger132adf52007-03-08 20:44:43 -08001235 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001236 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001237 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EACCES;
1239 }
1240
Stephen Hemminger132adf52007-03-08 20:44:43 -08001241 switch (optname) {
1242 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001243 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001244 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Stephen Hemminger132adf52007-03-08 20:44:43 -08001246 rtnl_lock();
Eric Dumazet4c9687092010-10-01 16:15:01 +00001247 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001249 return -EADDRINUSE;
1250 }
1251
1252 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1253 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001254 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001255 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001256 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1257 NETCONFA_IFINDEX_ALL,
1258 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001259 }
1260 rtnl_unlock();
1261 return ret;
1262 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001263 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001264 return -EACCES;
1265 return ip_ra_control(sk, 0, NULL);
1266 case MRT_ADD_VIF:
1267 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001268 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001269 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001270 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001271 return -EFAULT;
1272 if (vif.vifc_vifi >= MAXVIFS)
1273 return -ENFILE;
1274 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001275 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001276 ret = vif_add(net, mrt, &vif,
1277 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001278 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001279 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001280 }
1281 rtnl_unlock();
1282 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 /*
1285 * Manipulate the forwarding caches. These live
1286 * in a sort of kernel/user symbiosis.
1287 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001288 case MRT_ADD_MFC:
1289 case MRT_DEL_MFC:
Jianjun Kongc354e122008-11-03 00:28:02 -08001290 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001291 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001292 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001293 return -EFAULT;
1294 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001295 if (optname == MRT_DEL_MFC)
Patrick McHardy0c122952010-04-13 05:03:22 +00001296 ret = ipmr_mfc_delete(mrt, &mfc);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001298 ret = ipmr_mfc_add(net, mrt, &mfc,
1299 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001300 rtnl_unlock();
1301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * Control PIM assert.
1304 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 case MRT_ASSERT:
1306 {
1307 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001308 if (optlen != sizeof(v))
1309 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001310 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001311 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001312 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001313 return 0;
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001316 case MRT_PIM:
1317 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001318 int v;
1319
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001320 if (optlen != sizeof(v))
1321 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001322 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001323 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001324 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001325
Stephen Hemminger132adf52007-03-08 20:44:43 -08001326 rtnl_lock();
1327 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001328 if (v != mrt->mroute_do_pim) {
1329 mrt->mroute_do_pim = v;
1330 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001332 rtnl_unlock();
1333 return ret;
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001336#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1337 case MRT_TABLE:
1338 {
1339 u32 v;
1340
1341 if (optlen != sizeof(u32))
1342 return -EINVAL;
1343 if (get_user(v, (u32 __user *)optval))
1344 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001345
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001346 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1347 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1348 return -EINVAL;
1349
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001350 rtnl_lock();
1351 ret = 0;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001352 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1353 ret = -EBUSY;
1354 } else {
1355 if (!ipmr_new_table(net, v))
1356 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001357 else
1358 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001359 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001360 rtnl_unlock();
1361 return ret;
1362 }
1363#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001364 /*
1365 * Spurious command, or MRT_VERSION which you cannot
1366 * set.
1367 */
1368 default:
1369 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371}
1372
1373/*
1374 * Getsock opt support for the multicast routing system.
1375 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001376
Jianjun Kongc354e122008-11-03 00:28:02 -08001377int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 int olr;
1380 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001381 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001382 struct mr_table *mrt;
1383
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001384 if (sk->sk_type != SOCK_RAW ||
1385 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1386 return -EOPNOTSUPP;
1387
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001388 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1389 if (mrt == NULL)
1390 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Jianjun Kongc354e122008-11-03 00:28:02 -08001392 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001394 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001396 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -ENOPROTOOPT;
1398
1399 if (get_user(olr, optlen))
1400 return -EFAULT;
1401
1402 olr = min_t(unsigned int, olr, sizeof(int));
1403 if (olr < 0)
1404 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001405
Jianjun Kongc354e122008-11-03 00:28:02 -08001406 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001408 if (optname == MRT_VERSION)
1409 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001411 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001412 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413#endif
1414 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001415 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001416 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EFAULT;
1418 return 0;
1419}
1420
1421/*
1422 * The IP multicast ioctl support routines.
1423 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1426{
1427 struct sioc_sg_req sr;
1428 struct sioc_vif_req vr;
1429 struct vif_device *vif;
1430 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001431 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001432 struct mr_table *mrt;
1433
1434 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1435 if (mrt == NULL)
1436 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001437
Stephen Hemminger132adf52007-03-08 20:44:43 -08001438 switch (cmd) {
1439 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001440 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001441 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001442 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001443 return -EINVAL;
1444 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001445 vif = &mrt->vif_table[vr.vifi];
1446 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001447 vr.icount = vif->pkt_in;
1448 vr.ocount = vif->pkt_out;
1449 vr.ibytes = vif->bytes_in;
1450 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001452
Jianjun Kongc354e122008-11-03 00:28:02 -08001453 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001455 return 0;
1456 }
1457 read_unlock(&mrt_lock);
1458 return -EADDRNOTAVAIL;
1459 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001460 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001461 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Eric Dumazeta8c94862010-10-01 16:15:08 +00001463 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001464 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001465 if (c) {
1466 sr.pktcnt = c->mfc_un.res.pkt;
1467 sr.bytecnt = c->mfc_un.res.bytes;
1468 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001469 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001470
Jianjun Kongc354e122008-11-03 00:28:02 -08001471 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001472 return -EFAULT;
1473 return 0;
1474 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001475 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001476 return -EADDRNOTAVAIL;
1477 default:
1478 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480}
1481
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001482#ifdef CONFIG_COMPAT
1483struct compat_sioc_sg_req {
1484 struct in_addr src;
1485 struct in_addr grp;
1486 compat_ulong_t pktcnt;
1487 compat_ulong_t bytecnt;
1488 compat_ulong_t wrong_if;
1489};
1490
David S. Millerca6b8bb02011-02-03 17:24:28 -08001491struct compat_sioc_vif_req {
1492 vifi_t vifi; /* Which iface */
1493 compat_ulong_t icount;
1494 compat_ulong_t ocount;
1495 compat_ulong_t ibytes;
1496 compat_ulong_t obytes;
1497};
1498
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001499int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1500{
David S. Miller0033d5a2011-02-03 17:21:31 -08001501 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001502 struct compat_sioc_vif_req vr;
1503 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001504 struct mfc_cache *c;
1505 struct net *net = sock_net(sk);
1506 struct mr_table *mrt;
1507
1508 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1509 if (mrt == NULL)
1510 return -ENOENT;
1511
1512 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001513 case SIOCGETVIFCNT:
1514 if (copy_from_user(&vr, arg, sizeof(vr)))
1515 return -EFAULT;
1516 if (vr.vifi >= mrt->maxvif)
1517 return -EINVAL;
1518 read_lock(&mrt_lock);
1519 vif = &mrt->vif_table[vr.vifi];
1520 if (VIF_EXISTS(mrt, vr.vifi)) {
1521 vr.icount = vif->pkt_in;
1522 vr.ocount = vif->pkt_out;
1523 vr.ibytes = vif->bytes_in;
1524 vr.obytes = vif->bytes_out;
1525 read_unlock(&mrt_lock);
1526
1527 if (copy_to_user(arg, &vr, sizeof(vr)))
1528 return -EFAULT;
1529 return 0;
1530 }
1531 read_unlock(&mrt_lock);
1532 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001533 case SIOCGETSGCNT:
1534 if (copy_from_user(&sr, arg, sizeof(sr)))
1535 return -EFAULT;
1536
1537 rcu_read_lock();
1538 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1539 if (c) {
1540 sr.pktcnt = c->mfc_un.res.pkt;
1541 sr.bytecnt = c->mfc_un.res.bytes;
1542 sr.wrong_if = c->mfc_un.res.wrong_if;
1543 rcu_read_unlock();
1544
1545 if (copy_to_user(arg, &sr, sizeof(sr)))
1546 return -EFAULT;
1547 return 0;
1548 }
1549 rcu_read_unlock();
1550 return -EADDRNOTAVAIL;
1551 default:
1552 return -ENOIOCTLCMD;
1553 }
1554}
1555#endif
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1559{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001560 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001561 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001562 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 struct vif_device *v;
1564 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (event != NETDEV_UNREGISTER)
1567 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001568
1569 ipmr_for_each_table(mrt, net) {
1570 v = &mrt->vif_table[0];
1571 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1572 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001573 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576 return NOTIFY_DONE;
1577}
1578
1579
Jianjun Kongc354e122008-11-03 00:28:02 -08001580static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 .notifier_call = ipmr_device_event,
1582};
1583
1584/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001585 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 * This avoids tunnel drivers and other mess and gives us the speed so
1587 * important for multicast video.
1588 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001589
Al Viro114c7842006-09-27 18:39:29 -07001590static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001592 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001593 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001594
1595 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001596 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001597 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001598 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001600 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001601 iph->tos = old_iph->tos;
1602 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 iph->frag_off = 0;
1604 iph->daddr = daddr;
1605 iph->saddr = saddr;
1606 iph->protocol = IPPROTO_IPIP;
1607 iph->ihl = 5;
1608 iph->tot_len = htons(skb->len);
Eric Dumazetadf30902009-06-02 05:19:30 +00001609 ip_select_ident(iph, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ip_send_check(iph);
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1613 nf_reset(skb);
1614}
1615
1616static inline int ipmr_forward_finish(struct sk_buff *skb)
1617{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001618 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Eric Dumazetadf30902009-06-02 05:19:30 +00001620 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Vincent Bernat2d8dbb02012-06-05 03:41:42 +00001621 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 if (unlikely(opt->optlen))
1624 ip_forward_options(skb);
1625
1626 return dst_output(skb);
1627}
1628
1629/*
1630 * Processing handlers for ipmr_forward
1631 */
1632
Patrick McHardy0c122952010-04-13 05:03:22 +00001633static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1634 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001636 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001637 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 struct net_device *dev;
1639 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001640 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int encap = 0;
1642
1643 if (vif->dev == NULL)
1644 goto out_free;
1645
1646#ifdef CONFIG_IP_PIMSM
1647 if (vif->flags & VIFF_REGISTER) {
1648 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001649 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001650 vif->dev->stats.tx_bytes += skb->len;
1651 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001652 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001653 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655#endif
1656
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001657 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001658 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001659 vif->remote, vif->local,
1660 0, 0,
1661 IPPROTO_IPIP,
1662 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001663 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto out_free;
1665 encap = sizeof(struct iphdr);
1666 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001667 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001668 0, 0,
1669 IPPROTO_IPIP,
1670 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001671 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto out_free;
1673 }
1674
Changli Gaod8d1f302010-06-10 23:31:35 -07001675 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Changli Gaod8d1f302010-06-10 23:31:35 -07001677 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001679 * allow to send ICMP, so that packets will disappear
1680 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
1682
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001683 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 ip_rt_put(rt);
1685 goto out_free;
1686 }
1687
Changli Gaod8d1f302010-06-10 23:31:35 -07001688 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001691 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto out_free;
1693 }
1694
1695 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001696 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Eric Dumazetadf30902009-06-02 05:19:30 +00001698 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001699 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001700 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001703 * What do we do with netfilter? -- RR
1704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (vif->flags & VIFF_TUNNEL) {
1706 ip_encap(skb, vif->local, vif->remote);
1707 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001708 vif->dev->stats.tx_packets++;
1709 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
1712 IPCB(skb)->flags |= IPSKB_FORWARDED;
1713
1714 /*
1715 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1716 * not only before forwarding, but after forwarding on all output
1717 * interfaces. It is clear, if mrouter runs a multicasting
1718 * program, it should receive packets not depending to what interface
1719 * program is joined.
1720 * If we will not make it, the program will have to join on all
1721 * interfaces. On the other hand, multihoming host (or router, but
1722 * not mrouter) cannot join to more than one interface - it will
1723 * result in receiving multiple packets.
1724 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001725 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 ipmr_forward_finish);
1727 return;
1728
1729out_free:
1730 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Patrick McHardy0c122952010-04-13 05:03:22 +00001733static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001736
1737 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1738 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 break;
1740 }
1741 return ct;
1742}
1743
1744/* "local" means that we should preserve one skb (for local delivery) */
1745
Patrick McHardy0c122952010-04-13 05:03:22 +00001746static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1747 struct sk_buff *skb, struct mfc_cache *cache,
1748 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 int psend = -1;
1751 int vif, ct;
1752
1753 vif = cache->mfc_parent;
1754 cache->mfc_un.res.pkt++;
1755 cache->mfc_un.res.bytes += skb->len;
1756
1757 /*
1758 * Wrong interface: drop packet and (maybe) send PIM assert.
1759 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001760 if (mrt->vif_table[vif].dev != skb->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 int true_vifi;
1762
David S. Millerc7537962010-11-11 17:07:48 -08001763 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001765 * Very complicated situation...
1766 *
1767 * The best workaround until routing daemons will be
1768 * fixed is not to redistribute packet, if it was
1769 * send through wrong interface. It means, that
1770 * multicast applications WILL NOT work for
1771 * (S,G), which have default multicast route pointing
1772 * to wrong oif. In any case, it is not a good
1773 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 */
1775 goto dont_forward;
1776 }
1777
1778 cache->mfc_un.res.wrong_if++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001779 true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Patrick McHardy0c122952010-04-13 05:03:22 +00001781 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001783 * so that we cannot check that packet arrived on an oif.
1784 * It is bad, but otherwise we would need to move pretty
1785 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001787 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001788 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001789 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1791 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001792 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 goto dont_forward;
1795 }
1796
Patrick McHardy0c122952010-04-13 05:03:22 +00001797 mrt->vif_table[vif].pkt_in++;
1798 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /*
1801 * Forward the frame
1802 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001803 for (ct = cache->mfc_un.res.maxvif - 1;
1804 ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001805 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (psend != -1) {
1807 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001810 ipmr_queue_xmit(net, mrt, skb2, cache,
1811 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001813 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815 }
1816 if (psend != -1) {
1817 if (local) {
1818 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001821 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001823 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return 0;
1825 }
1826 }
1827
1828dont_forward:
1829 if (!local)
1830 kfree_skb(skb);
1831 return 0;
1832}
1833
David S. Miller417da662011-05-03 19:42:43 -07001834static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001835{
David S. Miller417da662011-05-03 19:42:43 -07001836 struct rtable *rt = skb_rtable(skb);
1837 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001838 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001839 .daddr = iph->daddr,
1840 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001841 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001842 .flowi4_oif = (rt_is_output_route(rt) ?
1843 skb->dev->ifindex : 0),
1844 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001845 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001846 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001847 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001848 };
1849 struct mr_table *mrt;
1850 int err;
1851
David S. Millerda919812011-03-12 02:04:50 -05001852 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001853 if (err)
1854 return ERR_PTR(err);
1855 return mrt;
1856}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858/*
1859 * Multicast packets for forwarding arrive here
Eric Dumazet4c9687092010-10-01 16:15:01 +00001860 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 */
1862
1863int ip_mr_input(struct sk_buff *skb)
1864{
1865 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001866 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001867 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001868 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001871 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001873 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto dont_forward;
1875
David S. Miller417da662011-05-03 19:42:43 -07001876 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001877 if (IS_ERR(mrt)) {
1878 kfree_skb(skb);
1879 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001882 if (IPCB(skb)->opt.router_alert) {
1883 if (ip_call_ra_chain(skb))
1884 return 0;
1885 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1886 /* IGMPv1 (and broken IGMPv2 implementations sort of
1887 * Cisco IOS <= 11.2(8)) do not put router alert
1888 * option to IGMP packets destined to routable
1889 * groups. It is very bad, because it means
1890 * that we can forward NO IGMP messages.
1891 */
1892 struct sock *mroute_sk;
1893
1894 mroute_sk = rcu_dereference(mrt->mroute_sk);
1895 if (mroute_sk) {
1896 nf_reset(skb);
1897 raw_rcv(mroute_sk, skb);
1898 return 0;
1899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901 }
1902
Eric Dumazeta8c94862010-10-01 16:15:08 +00001903 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001904 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /*
1907 * No usable cache entry
1908 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001909 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 int vif;
1911
1912 if (local) {
1913 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1914 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001915 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 skb = skb2;
1918 }
1919
Eric Dumazeta8c94862010-10-01 16:15:08 +00001920 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001921 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001923 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 read_unlock(&mrt_lock);
1925
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001926 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928 read_unlock(&mrt_lock);
1929 kfree_skb(skb);
1930 return -ENODEV;
1931 }
1932
Eric Dumazeta8c94862010-10-01 16:15:08 +00001933 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001934 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 read_unlock(&mrt_lock);
1936
1937 if (local)
1938 return ip_local_deliver(skb);
1939
1940 return 0;
1941
1942dont_forward:
1943 if (local)
1944 return ip_local_deliver(skb);
1945 kfree_skb(skb);
1946 return 0;
1947}
1948
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001949#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00001950/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001951static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1952 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001954 struct net_device *reg_dev = NULL;
1955 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001957 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001959 * Check that:
1960 * a. packet is really sent to a multicast group
1961 * b. packet is not a NULL-REGISTER
1962 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001964 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001966 ntohs(encap->tot_len) + pimlen > skb->len)
1967 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001970 if (mrt->mroute_reg_vif_num >= 0)
1971 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 read_unlock(&mrt_lock);
1973
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001974 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001975 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001977 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00001978 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001979 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00001981 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07001983
1984 skb_tunnel_rx(skb, reg_dev);
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001987
Eric Dumazet55747a02010-10-01 16:14:55 +00001988 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001989}
1990#endif
1991
1992#ifdef CONFIG_IP_PIMSM_V1
1993/*
1994 * Handle IGMP messages of PIMv1
1995 */
1996
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001997int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001998{
1999 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002000 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002001 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002002
2003 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2004 goto drop;
2005
2006 pim = igmp_hdr(skb);
2007
David S. Miller417da662011-05-03 19:42:43 -07002008 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002009 if (IS_ERR(mrt))
2010 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002011 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002012 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2013 goto drop;
2014
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002015 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002016drop:
2017 kfree_skb(skb);
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return 0;
2020}
2021#endif
2022
2023#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002024static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
2026 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002027 struct net *net = dev_net(skb->dev);
2028 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002030 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto drop;
2032
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002033 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002034 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2035 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002036 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002037 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 goto drop;
2039
David S. Miller417da662011-05-03 19:42:43 -07002040 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002041 if (IS_ERR(mrt))
2042 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002043 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002044drop:
2045 kfree_skb(skb);
2046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return 0;
2048}
2049#endif
2050
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002051static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2052 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 int ct;
2055 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002056 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002057 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Nicolas Dichtel74381892010-03-25 23:45:35 +00002059 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002060 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002061 return -ENOENT;
2062
Thomas Graf92a395e2012-06-26 23:36:13 +00002063 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2064 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2065 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Thomas Graf92a395e2012-06-26 23:36:13 +00002067 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2068 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002071 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002072 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2073 nla_nest_cancel(skb, mp_attr);
2074 return -EMSGSIZE;
2075 }
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 nhp->rtnh_flags = 0;
2078 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002079 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 nhp->rtnh_len = sizeof(*nhp);
2081 }
2082 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002083
2084 nla_nest_end(skb, mp_attr);
2085
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002086 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2087 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2088 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2089 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2090 return -EMSGSIZE;
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 rtm->rtm_type = RTN_MULTICAST;
2093 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
David S. Miller9a1b9492011-05-04 12:18:54 -07002096int ipmr_get_route(struct net *net, struct sk_buff *skb,
2097 __be32 saddr, __be32 daddr,
2098 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002101 struct mr_table *mrt;
2102 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002104 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2105 if (mrt == NULL)
2106 return -ENOENT;
2107
Eric Dumazeta8c94862010-10-01 16:15:08 +00002108 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002109 cache = ipmr_cache_find(mrt, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Jianjun Kongc354e122008-11-03 00:28:02 -08002111 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002112 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002113 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002115 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002118 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return -EAGAIN;
2120 }
2121
2122 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002123 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002124 if (dev)
2125 vif = ipmr_find_vif(mrt, dev);
2126 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002128 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return -ENODEV;
2130 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002131 skb2 = skb_clone(skb, GFP_ATOMIC);
2132 if (!skb2) {
2133 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002134 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002135 return -ENOMEM;
2136 }
2137
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002138 skb_push(skb2, sizeof(struct iphdr));
2139 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002140 iph = ip_hdr(skb2);
2141 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002142 iph->saddr = saddr;
2143 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002144 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002145 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002147 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return err;
2149 }
2150
Eric Dumazeta8c94862010-10-01 16:15:08 +00002151 read_lock(&mrt_lock);
2152 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002154 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002156 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return err;
2158}
2159
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002160static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002161 u32 portid, u32 seq, struct mfc_cache *c, int cmd)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002162{
2163 struct nlmsghdr *nlh;
2164 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002165 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002166
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002167 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), NLM_F_MULTI);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002168 if (nlh == NULL)
2169 return -EMSGSIZE;
2170
2171 rtm = nlmsg_data(nlh);
2172 rtm->rtm_family = RTNL_FAMILY_IPMR;
2173 rtm->rtm_dst_len = 32;
2174 rtm->rtm_src_len = 32;
2175 rtm->rtm_tos = 0;
2176 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002177 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2178 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002179 rtm->rtm_type = RTN_MULTICAST;
2180 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002181 if (c->mfc_flags & MFC_STATIC)
2182 rtm->rtm_protocol = RTPROT_STATIC;
2183 else
2184 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002185 rtm->rtm_flags = 0;
2186
David S. Millerf3756b72012-04-01 20:39:02 -04002187 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2188 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2189 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002190 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2191 /* do not break the dump if cache is unresolved */
2192 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002193 goto nla_put_failure;
2194
2195 return nlmsg_end(skb, nlh);
2196
2197nla_put_failure:
2198 nlmsg_cancel(skb, nlh);
2199 return -EMSGSIZE;
2200}
2201
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002202static size_t mroute_msgsize(bool unresolved, int maxvif)
2203{
2204 size_t len =
2205 NLMSG_ALIGN(sizeof(struct rtmsg))
2206 + nla_total_size(4) /* RTA_TABLE */
2207 + nla_total_size(4) /* RTA_SRC */
2208 + nla_total_size(4) /* RTA_DST */
2209 ;
2210
2211 if (!unresolved)
2212 len = len
2213 + nla_total_size(4) /* RTA_IIF */
2214 + nla_total_size(0) /* RTA_MULTIPATH */
2215 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2216 /* RTA_MFC_STATS */
2217 + nla_total_size(sizeof(struct rta_mfc_stats))
2218 ;
2219
2220 return len;
2221}
2222
2223static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2224 int cmd)
2225{
2226 struct net *net = read_pnet(&mrt->net);
2227 struct sk_buff *skb;
2228 int err = -ENOBUFS;
2229
2230 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2231 GFP_ATOMIC);
2232 if (skb == NULL)
2233 goto errout;
2234
2235 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd);
2236 if (err < 0)
2237 goto errout;
2238
2239 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2240 return;
2241
2242errout:
2243 kfree_skb(skb);
2244 if (err < 0)
2245 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2246}
2247
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002248static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2249{
2250 struct net *net = sock_net(skb->sk);
2251 struct mr_table *mrt;
2252 struct mfc_cache *mfc;
2253 unsigned int t = 0, s_t;
2254 unsigned int h = 0, s_h;
2255 unsigned int e = 0, s_e;
2256
2257 s_t = cb->args[0];
2258 s_h = cb->args[1];
2259 s_e = cb->args[2];
2260
Eric Dumazeta8c94862010-10-01 16:15:08 +00002261 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002262 ipmr_for_each_table(mrt, net) {
2263 if (t < s_t)
2264 goto next_table;
2265 if (t > s_t)
2266 s_h = 0;
2267 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002268 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002269 if (e < s_e)
2270 goto next_entry;
2271 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002272 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002273 cb->nlh->nlmsg_seq,
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002274 mfc, RTM_NEWROUTE) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002275 goto done;
2276next_entry:
2277 e++;
2278 }
2279 e = s_e = 0;
2280 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002281 spin_lock_bh(&mfc_unres_lock);
2282 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2283 if (e < s_e)
2284 goto next_entry2;
2285 if (ipmr_fill_mroute(mrt, skb,
2286 NETLINK_CB(cb->skb).portid,
2287 cb->nlh->nlmsg_seq,
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002288 mfc, RTM_NEWROUTE) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002289 spin_unlock_bh(&mfc_unres_lock);
2290 goto done;
2291 }
2292next_entry2:
2293 e++;
2294 }
2295 spin_unlock_bh(&mfc_unres_lock);
2296 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002297 s_h = 0;
2298next_table:
2299 t++;
2300 }
2301done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002302 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002303
2304 cb->args[2] = e;
2305 cb->args[1] = h;
2306 cb->args[0] = t;
2307
2308 return skb->len;
2309}
2310
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002311#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002313 * The /proc interfaces to multicast routing :
2314 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 */
2316struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002317 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002318 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 int ct;
2320};
2321
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002322static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2323 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 loff_t pos)
2325{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002326 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002327
2328 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2329 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002331 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002332 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
2334 return NULL;
2335}
2336
2337static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002338 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002340 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002341 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002342 struct mr_table *mrt;
2343
2344 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2345 if (mrt == NULL)
2346 return ERR_PTR(-ENOENT);
2347
2348 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002351 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 : SEQ_START_TOKEN;
2353}
2354
2355static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2356{
2357 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002358 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002359 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 ++*pos;
2362 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002363 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002364
Patrick McHardy0c122952010-04-13 05:03:22 +00002365 while (++iter->ct < mrt->maxvif) {
2366 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002368 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 return NULL;
2371}
2372
2373static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002374 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
2376 read_unlock(&mrt_lock);
2377}
2378
2379static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2380{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002381 struct ipmr_vif_iter *iter = seq->private;
2382 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002385 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2387 } else {
2388 const struct vif_device *vif = v;
2389 const char *name = vif->dev ? vif->dev->name : "none";
2390
2391 seq_printf(seq,
2392 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002393 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002394 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 vif->bytes_out, vif->pkt_out,
2396 vif->flags, vif->local, vif->remote);
2397 }
2398 return 0;
2399}
2400
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002401static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 .start = ipmr_vif_seq_start,
2403 .next = ipmr_vif_seq_next,
2404 .stop = ipmr_vif_seq_stop,
2405 .show = ipmr_vif_seq_show,
2406};
2407
2408static int ipmr_vif_open(struct inode *inode, struct file *file)
2409{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002410 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2411 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
Arjan van de Ven9a321442007-02-12 00:55:35 -08002414static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 .owner = THIS_MODULE,
2416 .open = ipmr_vif_open,
2417 .read = seq_read,
2418 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002419 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420};
2421
2422struct ipmr_mfc_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;
Patrick McHardy862465f2010-04-13 05:03:21 +00002425 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 int ct;
2427};
2428
2429
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002430static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2431 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002433 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 struct mfc_cache *mfc;
2435
Eric Dumazeta8c94862010-10-01 16:15:08 +00002436 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002437 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002438 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002439 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002440 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002442 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002443 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002446 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002447 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002448 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return mfc;
2450 spin_unlock_bh(&mfc_unres_lock);
2451
2452 it->cache = NULL;
2453 return NULL;
2454}
2455
2456
2457static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2458{
2459 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002460 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002461 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002462
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002463 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2464 if (mrt == NULL)
2465 return ERR_PTR(-ENOENT);
2466
2467 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 it->cache = NULL;
2469 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002470 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 : SEQ_START_TOKEN;
2472}
2473
2474static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2475{
2476 struct mfc_cache *mfc = v;
2477 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002478 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002479 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 ++*pos;
2482
2483 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002484 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Patrick McHardy862465f2010-04-13 05:03:21 +00002486 if (mfc->list.next != it->cache)
2487 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002488
Patrick McHardy0c122952010-04-13 05:03:22 +00002489 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 goto end_of_list;
2491
Patrick McHardy0c122952010-04-13 05:03:22 +00002492 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002495 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002496 if (list_empty(it->cache))
2497 continue;
2498 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
2500
2501 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002502 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002503 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002507 if (!list_empty(it->cache))
2508 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002510end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 spin_unlock_bh(&mfc_unres_lock);
2512 it->cache = NULL;
2513
2514 return NULL;
2515}
2516
2517static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2518{
2519 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002520 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Patrick McHardy0c122952010-04-13 05:03:22 +00002522 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002524 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002525 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
2528static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2529{
2530 int n;
2531
2532 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002533 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2535 } else {
2536 const struct mfc_cache *mfc = v;
2537 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002538 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002539
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002540 seq_printf(seq, "%08X %08X %-3hd",
2541 (__force u32) mfc->mfc_mcastgrp,
2542 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002543 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Patrick McHardy0c122952010-04-13 05:03:22 +00002545 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002546 seq_printf(seq, " %8lu %8lu %8lu",
2547 mfc->mfc_un.res.pkt,
2548 mfc->mfc_un.res.bytes,
2549 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002550 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002551 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002552 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002553 mfc->mfc_un.res.ttls[n] < 255)
2554 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002555 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 n, mfc->mfc_un.res.ttls[n]);
2557 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002558 } else {
2559 /* unresolved mfc_caches don't contain
2560 * pkt, bytes and wrong_if values
2561 */
2562 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564 seq_putc(seq, '\n');
2565 }
2566 return 0;
2567}
2568
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002569static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 .start = ipmr_mfc_seq_start,
2571 .next = ipmr_mfc_seq_next,
2572 .stop = ipmr_mfc_seq_stop,
2573 .show = ipmr_mfc_seq_show,
2574};
2575
2576static int ipmr_mfc_open(struct inode *inode, struct file *file)
2577{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002578 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2579 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Arjan van de Ven9a321442007-02-12 00:55:35 -08002582static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 .owner = THIS_MODULE,
2584 .open = ipmr_mfc_open,
2585 .read = seq_read,
2586 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002587 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002589#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002592static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002594 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595};
2596#endif
2597
2598
2599/*
2600 * Setup for IP multicast routing
2601 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002602static int __net_init ipmr_net_init(struct net *net)
2603{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002604 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002605
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002606 err = ipmr_rules_init(net);
2607 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002608 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002609
2610#ifdef CONFIG_PROC_FS
2611 err = -ENOMEM;
2612 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2613 goto proc_vif_fail;
2614 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2615 goto proc_cache_fail;
2616#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002617 return 0;
2618
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002619#ifdef CONFIG_PROC_FS
2620proc_cache_fail:
2621 proc_net_remove(net, "ip_mr_vif");
2622proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002623 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002624#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002625fail:
2626 return err;
2627}
2628
2629static void __net_exit ipmr_net_exit(struct net *net)
2630{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002631#ifdef CONFIG_PROC_FS
2632 proc_net_remove(net, "ip_mr_cache");
2633 proc_net_remove(net, "ip_mr_vif");
2634#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002635 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002636}
2637
2638static struct pernet_operations ipmr_net_ops = {
2639 .init = ipmr_net_init,
2640 .exit = ipmr_net_exit,
2641};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002642
Wang Chen03d2f892008-07-03 12:13:36 +08002643int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Wang Chen03d2f892008-07-03 12:13:36 +08002645 int err;
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2648 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002649 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002650 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002651 if (!mrt_cachep)
2652 return -ENOMEM;
2653
Benjamin Therycf958ae32009-01-22 04:56:16 +00002654 err = register_pernet_subsys(&ipmr_net_ops);
2655 if (err)
2656 goto reg_pernet_fail;
2657
Wang Chen03d2f892008-07-03 12:13:36 +08002658 err = register_netdevice_notifier(&ip_mr_notifier);
2659 if (err)
2660 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002661#ifdef CONFIG_IP_PIMSM_V2
2662 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002663 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002664 err = -EAGAIN;
2665 goto add_proto_fail;
2666 }
2667#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002668 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2669 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002670 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002671
Tom Goff403dbb92009-06-14 03:16:13 -07002672#ifdef CONFIG_IP_PIMSM_V2
2673add_proto_fail:
2674 unregister_netdevice_notifier(&ip_mr_notifier);
2675#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002676reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002677 unregister_pernet_subsys(&ipmr_net_ops);
2678reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002679 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002680 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}