blob: 421a24934ffd13a06e97d99b8235c2cd915a4fe7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71#define CONFIG_IP_PIMSM 1
72#endif
73
Patrick McHardy0c122952010-04-13 05:03:22 +000074struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000075 struct list_head list;
Patrick McHardy8de53df2010-04-15 13:29:28 +020076#ifdef CONFIG_NET_NS
77 struct net *net;
78#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +000079 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000080 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000081 struct timer_list ipmr_expire_timer;
82 struct list_head mfc_unres_queue;
83 struct list_head mfc_cache_array[MFC_LINES];
84 struct vif_device vif_table[MAXVIFS];
85 int maxvif;
86 atomic_t cache_resolve_queue_len;
Joe Perches53d68412012-11-25 09:35:30 +000087 bool mroute_do_assert;
88 bool mroute_do_pim;
Patrick McHardy0c122952010-04-13 05:03:22 +000089#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num;
91#endif
92};
93
Patrick McHardyf0ad0862010-04-13 05:03:23 +000094struct ipmr_rule {
95 struct fib_rule common;
96};
97
98struct ipmr_result {
99 struct mr_table *mrt;
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000103 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105
106static DEFINE_RWLOCK(mrt_lock);
107
108/*
109 * Multicast router control variables
110 */
111
Patrick McHardy0c122952010-04-13 05:03:22 +0000112#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* Special spinlock for queue of unresolved entries */
115static DEFINE_SPINLOCK(mfc_unres_lock);
116
117/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
121 *
122 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124
Christoph Lametere18b8902006-12-06 20:33:20 -0800125static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000127static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000128static void ipmr_free_table(struct mr_table *mrt);
129
Rami Rosenc4854ec2013-07-20 15:09:28 +0300130static void ip_mr_forward(struct net *net, struct mr_table *mrt,
131 struct sk_buff *skb, struct mfc_cache *cache,
132 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000133static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000134 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200135static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000137static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
138 int cmd);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000139static void mroute_clean_tables(struct mr_table *mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000140static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000142#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
143#define ipmr_for_each_table(mrt, net) \
144 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
145
146static struct mr_table *ipmr_get_table(struct net *net, u32 id)
147{
148 struct mr_table *mrt;
149
150 ipmr_for_each_table(mrt, net) {
151 if (mrt->id == id)
152 return mrt;
153 }
154 return NULL;
155}
156
David S. Millerda919812011-03-12 02:04:50 -0500157static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000158 struct mr_table **mrt)
159{
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);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100428 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700429 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 if (dev_open(dev))
432 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700433 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 }
436 return dev;
437
438failure:
439 /* allow the register to be completed before unregistering. */
440 rtnl_unlock();
441 rtnl_lock();
442
443 unregister_netdevice(dev);
444 return NULL;
445}
446
447#ifdef CONFIG_IP_PIMSM
448
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000449static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000451 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000452 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500453 struct flowi4 fl4 = {
454 .flowi4_oif = dev->ifindex,
455 .flowi4_iif = skb->skb_iif,
456 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000457 };
458 int err;
459
David S. Millerda919812011-03-12 02:04:50 -0500460 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000461 if (err < 0) {
462 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000463 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000464 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700467 dev->stats.tx_bytes += skb->len;
468 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000469 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 read_unlock(&mrt_lock);
471 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000472 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Stephen Hemminger007c3832008-11-20 20:28:35 -0800475static const struct net_device_ops reg_vif_netdev_ops = {
476 .ndo_start_xmit = reg_vif_xmit,
477};
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static void reg_vif_setup(struct net_device *dev)
480{
481 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800482 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800484 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700486 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000489static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct net_device *dev;
492 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000493 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000495 if (mrt->id == RT_TABLE_DEFAULT)
496 sprintf(name, "pimreg");
497 else
498 sprintf(name, "pimreg%u", mrt->id);
499
500 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (dev == NULL)
503 return NULL;
504
Tom Goff403dbb92009-06-14 03:16:13 -0700505 dev_net_set(dev, net);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (register_netdevice(dev)) {
508 free_netdev(dev);
509 return NULL;
510 }
511 dev->iflink = 0;
512
Herbert Xu71e27da2007-06-04 23:36:06 -0700513 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000514 in_dev = __in_dev_get_rcu(dev);
515 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700516 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Herbert Xu71e27da2007-06-04 23:36:06 -0700520 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100521 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700522 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
523 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 if (dev_open(dev))
526 goto failure;
527
Wang Chen7dc00c82008-07-14 20:56:34 -0700528 dev_hold(dev);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return dev;
531
532failure:
533 /* allow the register to be completed before unregistering. */
534 rtnl_unlock();
535 rtnl_lock();
536
537 unregister_netdevice(dev);
538 return NULL;
539}
540#endif
541
Ben Hutchings2c530402012-07-10 10:55:09 +0000542/**
543 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700544 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900546
Patrick McHardy0c122952010-04-13 05:03:22 +0000547static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000548 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct vif_device *v;
551 struct net_device *dev;
552 struct in_device *in_dev;
553
Patrick McHardy0c122952010-04-13 05:03:22 +0000554 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -EADDRNOTAVAIL;
556
Patrick McHardy0c122952010-04-13 05:03:22 +0000557 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 write_lock_bh(&mrt_lock);
560 dev = v->dev;
561 v->dev = NULL;
562
563 if (!dev) {
564 write_unlock_bh(&mrt_lock);
565 return -EADDRNOTAVAIL;
566 }
567
568#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000569 if (vifi == mrt->mroute_reg_vif_num)
570 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571#endif
572
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000573 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000575
576 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000577 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000580 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
583 write_unlock_bh(&mrt_lock);
584
585 dev_set_allmulti(dev, -1);
586
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000587 in_dev = __in_dev_get_rtnl(dev);
588 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700589 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000590 inet_netconf_notify_devconf(dev_net(dev),
591 NETCONFA_MC_FORWARDING,
592 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 ip_rt_multicast_event(in_dev);
594 }
595
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000596 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000597 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 dev_put(dev);
600 return 0;
601}
602
Eric Dumazeta8c94862010-10-01 16:15:08 +0000603static void ipmr_cache_free_rcu(struct rcu_head *head)
604{
605 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
606
607 kmem_cache_free(mrt_cachep, c);
608}
609
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000610static inline void ipmr_cache_free(struct mfc_cache *c)
611{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000612 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000616 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
618
Patrick McHardy0c122952010-04-13 05:03:22 +0000619static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200621 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700623 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Patrick McHardy0c122952010-04-13 05:03:22 +0000625 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Jianjun Kongc354e122008-11-03 00:28:02 -0800627 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700628 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
630 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000631 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000633 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700634 e->error = -ETIMEDOUT;
635 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700636
Eric W. Biederman15e47302012-09-07 20:12:54 +0000637 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000638 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000643 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646
Patrick McHardye258beb2010-04-13 05:03:19 +0000647/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Patrick McHardye258beb2010-04-13 05:03:19 +0000649static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Patrick McHardy0c122952010-04-13 05:03:22 +0000651 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unsigned long now;
653 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000654 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000657 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return;
659 }
660
Patrick McHardy0c122952010-04-13 05:03:22 +0000661 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto out;
663
664 now = jiffies;
665 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Patrick McHardy0c122952010-04-13 05:03:22 +0000667 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (time_after(c->mfc_un.unres.expires, now)) {
669 unsigned long interval = c->mfc_un.unres.expires - now;
670 if (interval < expires)
671 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 continue;
673 }
674
Patrick McHardy862465f2010-04-13 05:03:21 +0000675 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000676 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000677 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Patrick McHardy0c122952010-04-13 05:03:22 +0000680 if (!list_empty(&mrt->mfc_unres_queue))
681 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683out:
684 spin_unlock(&mfc_unres_lock);
685}
686
687/* Fill oifs list. It is called under write locked mrt_lock. */
688
Patrick McHardy0c122952010-04-13 05:03:22 +0000689static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000690 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 int vifi;
693
694 cache->mfc_un.res.minvif = MAXVIFS;
695 cache->mfc_un.res.maxvif = 0;
696 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
697
Patrick McHardy0c122952010-04-13 05:03:22 +0000698 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
699 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000700 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
702 if (cache->mfc_un.res.minvif > vifi)
703 cache->mfc_un.res.minvif = vifi;
704 if (cache->mfc_un.res.maxvif <= vifi)
705 cache->mfc_un.res.maxvif = vifi + 1;
706 }
707 }
708}
709
Patrick McHardy0c122952010-04-13 05:03:22 +0000710static int vif_add(struct net *net, struct mr_table *mrt,
711 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000714 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct net_device *dev;
716 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700717 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000720 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return -EADDRINUSE;
722
723 switch (vifc->vifc_flags) {
724#ifdef CONFIG_IP_PIMSM
725 case VIFF_REGISTER:
726 /*
727 * Special Purpose VIF in PIM
728 * All the packets will be sent to the daemon
729 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000730 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000732 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!dev)
734 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700735 err = dev_set_allmulti(dev, 1);
736 if (err) {
737 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700738 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700739 return err;
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900743 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000744 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!dev)
746 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700747 err = dev_set_allmulti(dev, 1);
748 if (err) {
749 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700750 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700751 return err;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000754
755 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000757 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
758 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000759 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000760 dev_put(dev);
761 return -EADDRNOTAVAIL;
762 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000763 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000764 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!dev)
767 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700768 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700769 if (err) {
770 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700771 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
774 default:
775 return -EINVAL;
776 }
777
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000778 in_dev = __in_dev_get_rtnl(dev);
779 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000780 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000782 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700783 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000784 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
785 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 ip_rt_multicast_event(in_dev);
787
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000788 /* Fill in the VIF structures */
789
Jianjun Kongc354e122008-11-03 00:28:02 -0800790 v->rate_limit = vifc->vifc_rate_limit;
791 v->local = vifc->vifc_lcl_addr.s_addr;
792 v->remote = vifc->vifc_rmt_addr.s_addr;
793 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (!mrtsock)
795 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800796 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 v->bytes_in = 0;
798 v->bytes_out = 0;
799 v->pkt_in = 0;
800 v->pkt_out = 0;
801 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000802 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 v->link = dev->iflink;
804
805 /* And finish update writing critical data */
806 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800807 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000809 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000810 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000812 if (vifi+1 > mrt->maxvif)
813 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 write_unlock_bh(&mrt_lock);
815 return 0;
816}
817
Eric Dumazeta8c94862010-10-01 16:15:08 +0000818/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000819static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000820 __be32 origin,
821 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Jianjun Kongc354e122008-11-03 00:28:02 -0800823 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct mfc_cache *c;
825
Eric Dumazeta8c94862010-10-01 16:15:08 +0000826 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000827 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
828 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000830 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000833/* Look for a (*,*,oif) entry */
834static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
835 int vifi)
836{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100837 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000838 struct mfc_cache *c;
839
840 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100841 if (c->mfc_origin == htonl(INADDR_ANY) &&
842 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000843 c->mfc_un.res.ttls[vifi] < 255)
844 return c;
845
846 return NULL;
847}
848
849/* Look for a (*,G) entry */
850static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
851 __be32 mcastgrp, int vifi)
852{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100853 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000854 struct mfc_cache *c, *proxy;
855
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100856 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000857 goto skip;
858
859 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100860 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000861 c->mfc_mcastgrp == mcastgrp) {
862 if (c->mfc_un.res.ttls[vifi] < 255)
863 return c;
864
865 /* It's ok if the vifi is part of the static tree */
866 proxy = ipmr_cache_find_any_parent(mrt,
867 c->mfc_parent);
868 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
869 return c;
870 }
871
872skip:
873 return ipmr_cache_find_any_parent(mrt, vifi);
874}
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876/*
877 * Allocate a multicast cache entry
878 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000879static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Jianjun Kongc354e122008-11-03 00:28:02 -0800881 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000882
883 if (c)
884 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return c;
886}
887
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000888static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Jianjun Kongc354e122008-11-03 00:28:02 -0800890 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000891
892 if (c) {
893 skb_queue_head_init(&c->mfc_un.unres.unresolved);
894 c->mfc_un.unres.expires = jiffies + 10*HZ;
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return c;
897}
898
899/*
900 * A cache entry has gone into a resolved state from queued
901 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900902
Patrick McHardy0c122952010-04-13 05:03:22 +0000903static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
904 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
906 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700907 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000909 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Jianjun Kongc354e122008-11-03 00:28:02 -0800911 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700912 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
914
Hong zhi guo573ce262013-03-27 06:47:04 +0000915 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000916 nlh->nlmsg_len = skb_tail_pointer(skb) -
917 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 } else {
919 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000920 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000922 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700923 e->error = -EMSGSIZE;
924 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Thomas Graf2942e902006-08-15 00:30:25 -0700926
Eric W. Biederman15e47302012-09-07 20:12:54 +0000927 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000928 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000929 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932}
933
934/*
935 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
936 * expects the following bizarre scheme.
937 *
938 * Called under mrt_lock.
939 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900940
Patrick McHardy0c122952010-04-13 05:03:22 +0000941static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000942 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300945 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 struct igmphdr *igmp;
947 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000948 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 int ret;
950
951#ifdef CONFIG_IP_PIMSM
952 if (assert == IGMPMSG_WHOLEPKT)
953 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
954 else
955#endif
956 skb = alloc_skb(128, GFP_ATOMIC);
957
Stephen Hemminger132adf52007-03-08 20:44:43 -0800958 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return -ENOBUFS;
960
961#ifdef CONFIG_IP_PIMSM
962 if (assert == IGMPMSG_WHOLEPKT) {
963 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000964 * Duplicate old header, fix ihl, length etc.
965 * And all this only to mangle msg->im_msgtype and
966 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300968 skb_push(skb, sizeof(struct iphdr));
969 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300970 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300971 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700972 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 msg->im_msgtype = IGMPMSG_WHOLEPKT;
974 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000975 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700976 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
977 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
978 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900979 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900981 {
982
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000983 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Cong Wang30f3a402013-06-05 20:14:10 +0800985 skb_set_network_header(skb, skb->len);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300986 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300987 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000988 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700989 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000991 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000993 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000995 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 igmp->type =
997 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000998 igmp->code = 0;
999 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001000 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Dumazet4c968702010-10-01 16:15:01 +00001003 rcu_read_lock();
1004 mroute_sk = rcu_dereference(mrt->mroute_sk);
1005 if (mroute_sk == NULL) {
1006 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 kfree_skb(skb);
1008 return -EINVAL;
1009 }
1010
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001011 /* Deliver to mrouted */
1012
Eric Dumazet4c968702010-10-01 16:15:01 +00001013 ret = sock_queue_rcv_skb(mroute_sk, skb);
1014 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001015 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001016 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 kfree_skb(skb);
1018 }
1019
1020 return ret;
1021}
1022
1023/*
1024 * Queue a packet for resolution. It gets locked cache entry!
1025 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001028ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Patrick McHardy862465f2010-04-13 05:03:21 +00001030 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 int err;
1032 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001033 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001036 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001037 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001038 c->mfc_origin == iph->saddr) {
1039 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Patrick McHardy862465f2010-04-13 05:03:21 +00001044 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001045 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Patrick McHardy0c122952010-04-13 05:03:22 +00001047 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001048 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 spin_unlock_bh(&mfc_unres_lock);
1050
1051 kfree_skb(skb);
1052 return -ENOBUFS;
1053 }
1054
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001055 /* Fill in the new cache entry */
1056
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001057 c->mfc_parent = -1;
1058 c->mfc_origin = iph->saddr;
1059 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001061 /* Reflect first query at mrouted. */
1062
Patrick McHardy0c122952010-04-13 05:03:22 +00001063 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001064 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001065 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 out - Brad Parker
1067 */
1068 spin_unlock_bh(&mfc_unres_lock);
1069
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001070 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 kfree_skb(skb);
1072 return err;
1073 }
1074
Patrick McHardy0c122952010-04-13 05:03:22 +00001075 atomic_inc(&mrt->cache_resolve_queue_len);
1076 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001077 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
David S. Miller278554b2010-05-12 00:05:35 -07001079 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1080 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001083 /* See if we can append the packet */
1084
1085 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 kfree_skb(skb);
1087 err = -ENOBUFS;
1088 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001089 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 err = 0;
1091 }
1092
1093 spin_unlock_bh(&mfc_unres_lock);
1094 return err;
1095}
1096
1097/*
1098 * MFC cache manipulation by user space mroute daemon
1099 */
1100
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001101static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001104 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Jianjun Kongc354e122008-11-03 00:28:02 -08001106 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Patrick McHardy0c122952010-04-13 05:03:22 +00001108 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001110 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1111 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001112 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001113 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001114 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return 0;
1116 }
1117 }
1118 return -ENOENT;
1119}
1120
Patrick McHardy0c122952010-04-13 05:03:22 +00001121static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001122 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Patrick McHardy862465f2010-04-13 05:03:21 +00001124 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001126 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Patrick McHardya50436f22010-03-17 06:04:14 +00001128 if (mfc->mfcc_parent >= MAXVIFS)
1129 return -ENFILE;
1130
Jianjun Kongc354e122008-11-03 00:28:02 -08001131 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Patrick McHardy0c122952010-04-13 05:03:22 +00001133 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001135 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1136 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001137 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
Patrick McHardy862465f2010-04-13 05:03:21 +00001142 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 write_lock_bh(&mrt_lock);
1144 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001145 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!mrtsock)
1147 c->mfc_flags |= MFC_STATIC;
1148 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001149 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
1151 }
1152
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001153 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001154 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return -EINVAL;
1156
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001157 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001158 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return -ENOMEM;
1160
Jianjun Kongc354e122008-11-03 00:28:02 -08001161 c->mfc_origin = mfc->mfcc_origin.s_addr;
1162 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1163 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001164 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!mrtsock)
1166 c->mfc_flags |= MFC_STATIC;
1167
Eric Dumazeta8c94862010-10-01 16:15:08 +00001168 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /*
1171 * Check to see if we resolved a queued list. If so we
1172 * need to send on the frames and tidy up.
1173 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001174 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001176 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001177 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001179 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001180 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001181 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
1183 }
1184 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001185 if (list_empty(&mrt->mfc_unres_queue))
1186 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 spin_unlock_bh(&mfc_unres_lock);
1188
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001189 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001190 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001191 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001193 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return 0;
1195}
1196
1197/*
1198 * Close the multicast socket, and clear the vif tables etc
1199 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001200
Patrick McHardy0c122952010-04-13 05:03:22 +00001201static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001204 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001205 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001206
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001207 /* Shut down all active vif entries */
1208
Patrick McHardy0c122952010-04-13 05:03:22 +00001209 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001210 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001211 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001213 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001215 /* Wipe the cache */
1216
Patrick McHardy862465f2010-04-13 05:03:21 +00001217 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001218 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001219 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001221 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001222 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001223 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225 }
1226
Patrick McHardy0c122952010-04-13 05:03:22 +00001227 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001229 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001230 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001231 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001232 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 spin_unlock_bh(&mfc_unres_lock);
1235 }
1236}
1237
Eric Dumazet4c968702010-10-01 16:15:01 +00001238/* called from ip_ra_control(), before an RCU grace period,
1239 * we dont need to call synchronize_rcu() here
1240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241static void mrtsock_destruct(struct sock *sk)
1242{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001243 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001244 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001247 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001248 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001249 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001250 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1251 NETCONFA_IFINDEX_ALL,
1252 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001253 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001254 mroute_clean_tables(mrt);
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257 rtnl_unlock();
1258}
1259
1260/*
1261 * Socket options and virtual interface manipulation. The whole
1262 * virtual interface system is a complete heap, but unfortunately
1263 * that's how BSD mrouted happens to think. Maybe one day with a proper
1264 * MOSPF/PIM router set up we can clean this up.
1265 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001266
David S. Millerb7058842009-09-30 16:12:20 -07001267int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001269 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct vifctl vif;
1271 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001272 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001273 struct mr_table *mrt;
1274
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001275 if (sk->sk_type != SOCK_RAW ||
1276 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1277 return -EOPNOTSUPP;
1278
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001279 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1280 if (mrt == NULL)
1281 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001282
Stephen Hemminger132adf52007-03-08 20:44:43 -08001283 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001284 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001285 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -EACCES;
1287 }
1288
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289 switch (optname) {
1290 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001291 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001292 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Stephen Hemminger132adf52007-03-08 20:44:43 -08001294 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001295 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 return -EADDRINUSE;
1298 }
1299
1300 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1301 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001302 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001303 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001304 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1305 NETCONFA_IFINDEX_ALL,
1306 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001307 }
1308 rtnl_unlock();
1309 return ret;
1310 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001311 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001312 return -EACCES;
1313 return ip_ra_control(sk, 0, NULL);
1314 case MRT_ADD_VIF:
1315 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001316 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001317 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001318 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001319 return -EFAULT;
1320 if (vif.vifc_vifi >= MAXVIFS)
1321 return -ENFILE;
1322 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001323 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001324 ret = vif_add(net, mrt, &vif,
1325 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001326 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001327 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001328 }
1329 rtnl_unlock();
1330 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /*
1333 * Manipulate the forwarding caches. These live
1334 * in a sort of kernel/user symbiosis.
1335 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001336 case MRT_ADD_MFC:
1337 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001338 parent = -1;
1339 case MRT_ADD_MFC_PROXY:
1340 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001341 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001342 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001343 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001344 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001345 if (parent == 0)
1346 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001347 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001348 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1349 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001350 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001351 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001352 sk == rtnl_dereference(mrt->mroute_sk),
1353 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001354 rtnl_unlock();
1355 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
1357 * Control PIM assert.
1358 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001359 case MRT_ASSERT:
1360 {
1361 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001362 if (optlen != sizeof(v))
1363 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001364 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001365 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001366 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001367 return 0;
1368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001370 case MRT_PIM:
1371 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001372 int v;
1373
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001374 if (optlen != sizeof(v))
1375 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001376 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001377 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001378 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001379
Stephen Hemminger132adf52007-03-08 20:44:43 -08001380 rtnl_lock();
1381 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001382 if (v != mrt->mroute_do_pim) {
1383 mrt->mroute_do_pim = v;
1384 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001386 rtnl_unlock();
1387 return ret;
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001390#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1391 case MRT_TABLE:
1392 {
1393 u32 v;
1394
1395 if (optlen != sizeof(u32))
1396 return -EINVAL;
1397 if (get_user(v, (u32 __user *)optval))
1398 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001399
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001400 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1401 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1402 return -EINVAL;
1403
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001404 rtnl_lock();
1405 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001406 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1407 ret = -EBUSY;
1408 } else {
1409 if (!ipmr_new_table(net, v))
1410 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001411 else
1412 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001413 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001414 rtnl_unlock();
1415 return ret;
1416 }
1417#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001418 /*
1419 * Spurious command, or MRT_VERSION which you cannot
1420 * set.
1421 */
1422 default:
1423 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425}
1426
1427/*
1428 * Getsock opt support for the multicast routing system.
1429 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001430
Jianjun Kongc354e122008-11-03 00:28:02 -08001431int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 int olr;
1434 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001435 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001436 struct mr_table *mrt;
1437
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001438 if (sk->sk_type != SOCK_RAW ||
1439 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1440 return -EOPNOTSUPP;
1441
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001442 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1443 if (mrt == NULL)
1444 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Jianjun Kongc354e122008-11-03 00:28:02 -08001446 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001448 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001450 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -ENOPROTOOPT;
1452
1453 if (get_user(olr, optlen))
1454 return -EFAULT;
1455
1456 olr = min_t(unsigned int, olr, sizeof(int));
1457 if (olr < 0)
1458 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001459
Jianjun Kongc354e122008-11-03 00:28:02 -08001460 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001462 if (optname == MRT_VERSION)
1463 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001465 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001466 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467#endif
1468 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001469 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001470 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return -EFAULT;
1472 return 0;
1473}
1474
1475/*
1476 * The IP multicast ioctl support routines.
1477 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1480{
1481 struct sioc_sg_req sr;
1482 struct sioc_vif_req vr;
1483 struct vif_device *vif;
1484 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001485 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001486 struct mr_table *mrt;
1487
1488 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1489 if (mrt == NULL)
1490 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001491
Stephen Hemminger132adf52007-03-08 20:44:43 -08001492 switch (cmd) {
1493 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001494 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001495 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001496 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001497 return -EINVAL;
1498 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001499 vif = &mrt->vif_table[vr.vifi];
1500 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001501 vr.icount = vif->pkt_in;
1502 vr.ocount = vif->pkt_out;
1503 vr.ibytes = vif->bytes_in;
1504 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001506
Jianjun Kongc354e122008-11-03 00:28:02 -08001507 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001509 return 0;
1510 }
1511 read_unlock(&mrt_lock);
1512 return -EADDRNOTAVAIL;
1513 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001514 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001515 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Eric Dumazeta8c94862010-10-01 16:15:08 +00001517 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001518 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001519 if (c) {
1520 sr.pktcnt = c->mfc_un.res.pkt;
1521 sr.bytecnt = c->mfc_un.res.bytes;
1522 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001523 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001524
Jianjun Kongc354e122008-11-03 00:28:02 -08001525 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001526 return -EFAULT;
1527 return 0;
1528 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001529 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001530 return -EADDRNOTAVAIL;
1531 default:
1532 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534}
1535
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001536#ifdef CONFIG_COMPAT
1537struct compat_sioc_sg_req {
1538 struct in_addr src;
1539 struct in_addr grp;
1540 compat_ulong_t pktcnt;
1541 compat_ulong_t bytecnt;
1542 compat_ulong_t wrong_if;
1543};
1544
David S. Millerca6b8bb02011-02-03 17:24:28 -08001545struct compat_sioc_vif_req {
1546 vifi_t vifi; /* Which iface */
1547 compat_ulong_t icount;
1548 compat_ulong_t ocount;
1549 compat_ulong_t ibytes;
1550 compat_ulong_t obytes;
1551};
1552
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001553int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1554{
David S. Miller0033d5a2011-02-03 17:21:31 -08001555 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001556 struct compat_sioc_vif_req vr;
1557 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001558 struct mfc_cache *c;
1559 struct net *net = sock_net(sk);
1560 struct mr_table *mrt;
1561
1562 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1563 if (mrt == NULL)
1564 return -ENOENT;
1565
1566 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001567 case SIOCGETVIFCNT:
1568 if (copy_from_user(&vr, arg, sizeof(vr)))
1569 return -EFAULT;
1570 if (vr.vifi >= mrt->maxvif)
1571 return -EINVAL;
1572 read_lock(&mrt_lock);
1573 vif = &mrt->vif_table[vr.vifi];
1574 if (VIF_EXISTS(mrt, vr.vifi)) {
1575 vr.icount = vif->pkt_in;
1576 vr.ocount = vif->pkt_out;
1577 vr.ibytes = vif->bytes_in;
1578 vr.obytes = vif->bytes_out;
1579 read_unlock(&mrt_lock);
1580
1581 if (copy_to_user(arg, &vr, sizeof(vr)))
1582 return -EFAULT;
1583 return 0;
1584 }
1585 read_unlock(&mrt_lock);
1586 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001587 case SIOCGETSGCNT:
1588 if (copy_from_user(&sr, arg, sizeof(sr)))
1589 return -EFAULT;
1590
1591 rcu_read_lock();
1592 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1593 if (c) {
1594 sr.pktcnt = c->mfc_un.res.pkt;
1595 sr.bytecnt = c->mfc_un.res.bytes;
1596 sr.wrong_if = c->mfc_un.res.wrong_if;
1597 rcu_read_unlock();
1598
1599 if (copy_to_user(arg, &sr, sizeof(sr)))
1600 return -EFAULT;
1601 return 0;
1602 }
1603 rcu_read_unlock();
1604 return -EADDRNOTAVAIL;
1605 default:
1606 return -ENOIOCTLCMD;
1607 }
1608}
1609#endif
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1613{
Jiri Pirko351638e2013-05-28 01:30:21 +00001614 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001615 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001616 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 struct vif_device *v;
1618 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (event != NETDEV_UNREGISTER)
1621 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001622
1623 ipmr_for_each_table(mrt, net) {
1624 v = &mrt->vif_table[0];
1625 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1626 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001627 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630 return NOTIFY_DONE;
1631}
1632
1633
Jianjun Kongc354e122008-11-03 00:28:02 -08001634static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 .notifier_call = ipmr_device_event,
1636};
1637
1638/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001639 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 * This avoids tunnel drivers and other mess and gives us the speed so
1641 * important for multicast video.
1642 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001643
Al Viro114c7842006-09-27 18:39:29 -07001644static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001646 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001647 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001648
1649 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001650 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001651 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001652 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001654 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001655 iph->tos = old_iph->tos;
1656 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 iph->frag_off = 0;
1658 iph->daddr = daddr;
1659 iph->saddr = saddr;
1660 iph->protocol = IPPROTO_IPIP;
1661 iph->ihl = 5;
1662 iph->tot_len = htons(skb->len);
Ansis Atteka703133d2013-09-18 15:29:53 -07001663 ip_select_ident(skb, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 ip_send_check(iph);
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1667 nf_reset(skb);
1668}
1669
1670static inline int ipmr_forward_finish(struct sk_buff *skb)
1671{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001672 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Eric Dumazetadf30902009-06-02 05:19:30 +00001674 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Vincent Bernat2d8dbb02012-06-05 03:41:42 +00001675 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 if (unlikely(opt->optlen))
1678 ip_forward_options(skb);
1679
1680 return dst_output(skb);
1681}
1682
1683/*
1684 * Processing handlers for ipmr_forward
1685 */
1686
Patrick McHardy0c122952010-04-13 05:03:22 +00001687static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1688 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001690 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001691 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct net_device *dev;
1693 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001694 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 int encap = 0;
1696
1697 if (vif->dev == NULL)
1698 goto out_free;
1699
1700#ifdef CONFIG_IP_PIMSM
1701 if (vif->flags & VIFF_REGISTER) {
1702 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001703 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001704 vif->dev->stats.tx_bytes += skb->len;
1705 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001706 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001707 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709#endif
1710
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001711 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001712 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001713 vif->remote, vif->local,
1714 0, 0,
1715 IPPROTO_IPIP,
1716 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001717 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 goto out_free;
1719 encap = sizeof(struct iphdr);
1720 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001721 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001722 0, 0,
1723 IPPROTO_IPIP,
1724 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001725 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 goto out_free;
1727 }
1728
Changli Gaod8d1f302010-06-10 23:31:35 -07001729 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Changli Gaod8d1f302010-06-10 23:31:35 -07001731 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001733 * allow to send ICMP, so that packets will disappear
1734 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
1736
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001737 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 ip_rt_put(rt);
1739 goto out_free;
1740 }
1741
Changli Gaod8d1f302010-06-10 23:31:35 -07001742 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001745 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto out_free;
1747 }
1748
1749 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001750 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Eric Dumazetadf30902009-06-02 05:19:30 +00001752 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001753 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001754 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001757 * What do we do with netfilter? -- RR
1758 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (vif->flags & VIFF_TUNNEL) {
1760 ip_encap(skb, vif->local, vif->remote);
1761 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001762 vif->dev->stats.tx_packets++;
1763 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765
1766 IPCB(skb)->flags |= IPSKB_FORWARDED;
1767
1768 /*
1769 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1770 * not only before forwarding, but after forwarding on all output
1771 * interfaces. It is clear, if mrouter runs a multicasting
1772 * program, it should receive packets not depending to what interface
1773 * program is joined.
1774 * If we will not make it, the program will have to join on all
1775 * interfaces. On the other hand, multihoming host (or router, but
1776 * not mrouter) cannot join to more than one interface - it will
1777 * result in receiving multiple packets.
1778 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001779 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 ipmr_forward_finish);
1781 return;
1782
1783out_free:
1784 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Patrick McHardy0c122952010-04-13 05:03:22 +00001787static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001790
1791 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1792 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 break;
1794 }
1795 return ct;
1796}
1797
1798/* "local" means that we should preserve one skb (for local delivery) */
1799
Rami Rosenc4854ec2013-07-20 15:09:28 +03001800static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1801 struct sk_buff *skb, struct mfc_cache *cache,
1802 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 int psend = -1;
1805 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001806 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 vif = cache->mfc_parent;
1809 cache->mfc_un.res.pkt++;
1810 cache->mfc_un.res.bytes += skb->len;
1811
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001812 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001813 struct mfc_cache *cache_proxy;
1814
1815 /* For an (*,G) entry, we only check that the incomming
1816 * interface is part of the static tree.
1817 */
1818 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1819 if (cache_proxy &&
1820 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1821 goto forward;
1822 }
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /*
1825 * Wrong interface: drop packet and (maybe) send PIM assert.
1826 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001827 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001828 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001830 * Very complicated situation...
1831 *
1832 * The best workaround until routing daemons will be
1833 * fixed is not to redistribute packet, if it was
1834 * send through wrong interface. It means, that
1835 * multicast applications WILL NOT work for
1836 * (S,G), which have default multicast route pointing
1837 * to wrong oif. In any case, it is not a good
1838 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 */
1840 goto dont_forward;
1841 }
1842
1843 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Patrick McHardy0c122952010-04-13 05:03:22 +00001845 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001847 * so that we cannot check that packet arrived on an oif.
1848 * It is bad, but otherwise we would need to move pretty
1849 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001851 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001852 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001853 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1855 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001856 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
1858 goto dont_forward;
1859 }
1860
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001861forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001862 mrt->vif_table[vif].pkt_in++;
1863 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 /*
1866 * Forward the frame
1867 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001868 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1869 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001870 if (true_vifi >= 0 &&
1871 true_vifi != cache->mfc_parent &&
1872 ip_hdr(skb)->ttl >
1873 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1874 /* It's an (*,*) entry and the packet is not coming from
1875 * the upstream: forward the packet to the upstream
1876 * only.
1877 */
1878 psend = cache->mfc_parent;
1879 goto last_forward;
1880 }
1881 goto dont_forward;
1882 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001883 for (ct = cache->mfc_un.res.maxvif - 1;
1884 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001885 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001886 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1887 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001888 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (psend != -1) {
1890 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001893 ipmr_queue_xmit(net, mrt, skb2, cache,
1894 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001896 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001899last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (psend != -1) {
1901 if (local) {
1902 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001905 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001907 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001908 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910 }
1911
1912dont_forward:
1913 if (!local)
1914 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
David S. Miller417da662011-05-03 19:42:43 -07001917static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001918{
David S. Miller417da662011-05-03 19:42:43 -07001919 struct rtable *rt = skb_rtable(skb);
1920 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001921 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001922 .daddr = iph->daddr,
1923 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001924 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001925 .flowi4_oif = (rt_is_output_route(rt) ?
1926 skb->dev->ifindex : 0),
1927 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001928 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001929 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001930 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001931 };
1932 struct mr_table *mrt;
1933 int err;
1934
David S. Millerda919812011-03-12 02:04:50 -05001935 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001936 if (err)
1937 return ERR_PTR(err);
1938 return mrt;
1939}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941/*
1942 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001943 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 */
1945
1946int ip_mr_input(struct sk_buff *skb)
1947{
1948 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001949 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001950 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001951 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001954 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001956 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 goto dont_forward;
1958
David S. Miller417da662011-05-03 19:42:43 -07001959 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001960 if (IS_ERR(mrt)) {
1961 kfree_skb(skb);
1962 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001965 if (IPCB(skb)->opt.router_alert) {
1966 if (ip_call_ra_chain(skb))
1967 return 0;
1968 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1969 /* IGMPv1 (and broken IGMPv2 implementations sort of
1970 * Cisco IOS <= 11.2(8)) do not put router alert
1971 * option to IGMP packets destined to routable
1972 * groups. It is very bad, because it means
1973 * that we can forward NO IGMP messages.
1974 */
1975 struct sock *mroute_sk;
1976
1977 mroute_sk = rcu_dereference(mrt->mroute_sk);
1978 if (mroute_sk) {
1979 nf_reset(skb);
1980 raw_rcv(mroute_sk, skb);
1981 return 0;
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984 }
1985
Eric Dumazeta8c94862010-10-01 16:15:08 +00001986 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001987 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001988 if (cache == NULL) {
1989 int vif = ipmr_find_vif(mrt, skb->dev);
1990
1991 if (vif >= 0)
1992 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1993 vif);
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 /*
1997 * No usable cache entry
1998 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001999 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 int vif;
2001
2002 if (local) {
2003 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2004 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002005 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 skb = skb2;
2008 }
2009
Eric Dumazeta8c94862010-10-01 16:15:08 +00002010 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002011 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002013 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 read_unlock(&mrt_lock);
2015
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002016 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 read_unlock(&mrt_lock);
2019 kfree_skb(skb);
2020 return -ENODEV;
2021 }
2022
Eric Dumazeta8c94862010-10-01 16:15:08 +00002023 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002024 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 read_unlock(&mrt_lock);
2026
2027 if (local)
2028 return ip_local_deliver(skb);
2029
2030 return 0;
2031
2032dont_forward:
2033 if (local)
2034 return ip_local_deliver(skb);
2035 kfree_skb(skb);
2036 return 0;
2037}
2038
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002039#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00002040/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002041static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2042 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002044 struct net_device *reg_dev = NULL;
2045 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002047 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002049 * Check that:
2050 * a. packet is really sent to a multicast group
2051 * b. packet is not a NULL-REGISTER
2052 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002054 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002056 ntohs(encap->tot_len) + pimlen > skb->len)
2057 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002060 if (mrt->mroute_reg_vif_num >= 0)
2061 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 read_unlock(&mrt_lock);
2063
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002064 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002065 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002067 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00002068 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03002069 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00002071 skb->ip_summed = CHECKSUM_NONE;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002072
Nicolas Dichtelea231922013-09-02 15:34:58 +02002073 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002076
Eric Dumazet55747a02010-10-01 16:14:55 +00002077 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002078}
2079#endif
2080
2081#ifdef CONFIG_IP_PIMSM_V1
2082/*
2083 * Handle IGMP messages of PIMv1
2084 */
2085
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002086int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002087{
2088 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002089 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002090 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002091
2092 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2093 goto drop;
2094
2095 pim = igmp_hdr(skb);
2096
David S. Miller417da662011-05-03 19:42:43 -07002097 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002098 if (IS_ERR(mrt))
2099 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002100 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002101 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2102 goto drop;
2103
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002104 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002105drop:
2106 kfree_skb(skb);
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return 0;
2109}
2110#endif
2111
2112#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002113static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002116 struct net *net = dev_net(skb->dev);
2117 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002119 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 goto drop;
2121
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002122 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002123 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2124 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002125 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002126 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 goto drop;
2128
David S. Miller417da662011-05-03 19:42:43 -07002129 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002130 if (IS_ERR(mrt))
2131 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002132 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002133drop:
2134 kfree_skb(skb);
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return 0;
2137}
2138#endif
2139
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002140static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2141 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 int ct;
2144 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002145 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002146 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Nicolas Dichtel74381892010-03-25 23:45:35 +00002148 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002149 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002150 return -ENOENT;
2151
Thomas Graf92a395e2012-06-26 23:36:13 +00002152 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2153 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2154 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Thomas Graf92a395e2012-06-26 23:36:13 +00002156 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2157 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002160 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002161 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2162 nla_nest_cancel(skb, mp_attr);
2163 return -EMSGSIZE;
2164 }
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 nhp->rtnh_flags = 0;
2167 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002168 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 nhp->rtnh_len = sizeof(*nhp);
2170 }
2171 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002172
2173 nla_nest_end(skb, mp_attr);
2174
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002175 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2176 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2177 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2178 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2179 return -EMSGSIZE;
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 rtm->rtm_type = RTN_MULTICAST;
2182 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
David S. Miller9a1b9492011-05-04 12:18:54 -07002185int ipmr_get_route(struct net *net, struct sk_buff *skb,
2186 __be32 saddr, __be32 daddr,
2187 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002190 struct mr_table *mrt;
2191 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002193 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2194 if (mrt == NULL)
2195 return -ENOENT;
2196
Eric Dumazeta8c94862010-10-01 16:15:08 +00002197 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002198 cache = ipmr_cache_find(mrt, saddr, daddr);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002199 if (cache == NULL && skb->dev) {
2200 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002202 if (vif >= 0)
2203 cache = ipmr_cache_find_any(mrt, daddr, vif);
2204 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002205 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002206 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002207 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002209 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002212 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return -EAGAIN;
2214 }
2215
2216 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002217 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002218 if (dev)
2219 vif = ipmr_find_vif(mrt, dev);
2220 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002222 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return -ENODEV;
2224 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002225 skb2 = skb_clone(skb, GFP_ATOMIC);
2226 if (!skb2) {
2227 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002228 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002229 return -ENOMEM;
2230 }
2231
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002232 skb_push(skb2, sizeof(struct iphdr));
2233 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002234 iph = ip_hdr(skb2);
2235 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002236 iph->saddr = saddr;
2237 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002238 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002239 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002241 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return err;
2243 }
2244
Eric Dumazeta8c94862010-10-01 16:15:08 +00002245 read_lock(&mrt_lock);
2246 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002248 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002250 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return err;
2252}
2253
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002254static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002255 u32 portid, u32 seq, struct mfc_cache *c, int cmd)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002256{
2257 struct nlmsghdr *nlh;
2258 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002259 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002260
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002261 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), NLM_F_MULTI);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002262 if (nlh == NULL)
2263 return -EMSGSIZE;
2264
2265 rtm = nlmsg_data(nlh);
2266 rtm->rtm_family = RTNL_FAMILY_IPMR;
2267 rtm->rtm_dst_len = 32;
2268 rtm->rtm_src_len = 32;
2269 rtm->rtm_tos = 0;
2270 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002271 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2272 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002273 rtm->rtm_type = RTN_MULTICAST;
2274 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002275 if (c->mfc_flags & MFC_STATIC)
2276 rtm->rtm_protocol = RTPROT_STATIC;
2277 else
2278 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002279 rtm->rtm_flags = 0;
2280
David S. Millerf3756b72012-04-01 20:39:02 -04002281 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2282 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2283 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002284 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2285 /* do not break the dump if cache is unresolved */
2286 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002287 goto nla_put_failure;
2288
2289 return nlmsg_end(skb, nlh);
2290
2291nla_put_failure:
2292 nlmsg_cancel(skb, nlh);
2293 return -EMSGSIZE;
2294}
2295
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002296static size_t mroute_msgsize(bool unresolved, int maxvif)
2297{
2298 size_t len =
2299 NLMSG_ALIGN(sizeof(struct rtmsg))
2300 + nla_total_size(4) /* RTA_TABLE */
2301 + nla_total_size(4) /* RTA_SRC */
2302 + nla_total_size(4) /* RTA_DST */
2303 ;
2304
2305 if (!unresolved)
2306 len = len
2307 + nla_total_size(4) /* RTA_IIF */
2308 + nla_total_size(0) /* RTA_MULTIPATH */
2309 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2310 /* RTA_MFC_STATS */
2311 + nla_total_size(sizeof(struct rta_mfc_stats))
2312 ;
2313
2314 return len;
2315}
2316
2317static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2318 int cmd)
2319{
2320 struct net *net = read_pnet(&mrt->net);
2321 struct sk_buff *skb;
2322 int err = -ENOBUFS;
2323
2324 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2325 GFP_ATOMIC);
2326 if (skb == NULL)
2327 goto errout;
2328
2329 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd);
2330 if (err < 0)
2331 goto errout;
2332
2333 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2334 return;
2335
2336errout:
2337 kfree_skb(skb);
2338 if (err < 0)
2339 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2340}
2341
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002342static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2343{
2344 struct net *net = sock_net(skb->sk);
2345 struct mr_table *mrt;
2346 struct mfc_cache *mfc;
2347 unsigned int t = 0, s_t;
2348 unsigned int h = 0, s_h;
2349 unsigned int e = 0, s_e;
2350
2351 s_t = cb->args[0];
2352 s_h = cb->args[1];
2353 s_e = cb->args[2];
2354
Eric Dumazeta8c94862010-10-01 16:15:08 +00002355 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002356 ipmr_for_each_table(mrt, net) {
2357 if (t < s_t)
2358 goto next_table;
2359 if (t > s_t)
2360 s_h = 0;
2361 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002362 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002363 if (e < s_e)
2364 goto next_entry;
2365 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002366 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002367 cb->nlh->nlmsg_seq,
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002368 mfc, RTM_NEWROUTE) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002369 goto done;
2370next_entry:
2371 e++;
2372 }
2373 e = s_e = 0;
2374 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002375 spin_lock_bh(&mfc_unres_lock);
2376 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2377 if (e < s_e)
2378 goto next_entry2;
2379 if (ipmr_fill_mroute(mrt, skb,
2380 NETLINK_CB(cb->skb).portid,
2381 cb->nlh->nlmsg_seq,
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002382 mfc, RTM_NEWROUTE) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002383 spin_unlock_bh(&mfc_unres_lock);
2384 goto done;
2385 }
2386next_entry2:
2387 e++;
2388 }
2389 spin_unlock_bh(&mfc_unres_lock);
2390 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002391 s_h = 0;
2392next_table:
2393 t++;
2394 }
2395done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002396 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002397
2398 cb->args[2] = e;
2399 cb->args[1] = h;
2400 cb->args[0] = t;
2401
2402 return skb->len;
2403}
2404
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002405#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002407 * The /proc interfaces to multicast routing :
2408 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 */
2410struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002411 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002412 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 int ct;
2414};
2415
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002416static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2417 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 loff_t pos)
2419{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002420 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002421
2422 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2423 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002425 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002426 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428 return NULL;
2429}
2430
2431static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002432 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002434 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002435 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002436 struct mr_table *mrt;
2437
2438 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2439 if (mrt == NULL)
2440 return ERR_PTR(-ENOENT);
2441
2442 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002445 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 : SEQ_START_TOKEN;
2447}
2448
2449static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2450{
2451 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002452 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002453 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 ++*pos;
2456 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002457 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002458
Patrick McHardy0c122952010-04-13 05:03:22 +00002459 while (++iter->ct < mrt->maxvif) {
2460 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002462 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464 return NULL;
2465}
2466
2467static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002468 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 read_unlock(&mrt_lock);
2471}
2472
2473static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2474{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002475 struct ipmr_vif_iter *iter = seq->private;
2476 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002479 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2481 } else {
2482 const struct vif_device *vif = v;
2483 const char *name = vif->dev ? vif->dev->name : "none";
2484
2485 seq_printf(seq,
2486 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002487 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002488 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 vif->bytes_out, vif->pkt_out,
2490 vif->flags, vif->local, vif->remote);
2491 }
2492 return 0;
2493}
2494
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002495static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 .start = ipmr_vif_seq_start,
2497 .next = ipmr_vif_seq_next,
2498 .stop = ipmr_vif_seq_stop,
2499 .show = ipmr_vif_seq_show,
2500};
2501
2502static int ipmr_vif_open(struct inode *inode, struct file *file)
2503{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002504 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2505 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
Arjan van de Ven9a321442007-02-12 00:55:35 -08002508static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 .owner = THIS_MODULE,
2510 .open = ipmr_vif_open,
2511 .read = seq_read,
2512 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002513 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514};
2515
2516struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002517 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002518 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002519 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 int ct;
2521};
2522
2523
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002524static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2525 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002527 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 struct mfc_cache *mfc;
2529
Eric Dumazeta8c94862010-10-01 16:15:08 +00002530 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002531 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002532 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002533 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002534 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002536 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002537 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002540 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002541 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002542 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return mfc;
2544 spin_unlock_bh(&mfc_unres_lock);
2545
2546 it->cache = NULL;
2547 return NULL;
2548}
2549
2550
2551static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2552{
2553 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002554 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002555 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002556
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002557 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2558 if (mrt == NULL)
2559 return ERR_PTR(-ENOENT);
2560
2561 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 it->cache = NULL;
2563 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002564 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 : SEQ_START_TOKEN;
2566}
2567
2568static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2569{
2570 struct mfc_cache *mfc = v;
2571 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002572 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002573 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 ++*pos;
2576
2577 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002578 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Patrick McHardy862465f2010-04-13 05:03:21 +00002580 if (mfc->list.next != it->cache)
2581 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002582
Patrick McHardy0c122952010-04-13 05:03:22 +00002583 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 goto end_of_list;
2585
Patrick McHardy0c122952010-04-13 05:03:22 +00002586 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002589 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002590 if (list_empty(it->cache))
2591 continue;
2592 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594
2595 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002596 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002597 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002601 if (!list_empty(it->cache))
2602 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002604end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 spin_unlock_bh(&mfc_unres_lock);
2606 it->cache = NULL;
2607
2608 return NULL;
2609}
2610
2611static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2612{
2613 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002614 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Patrick McHardy0c122952010-04-13 05:03:22 +00002616 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002618 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002619 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
2621
2622static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2623{
2624 int n;
2625
2626 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002627 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2629 } else {
2630 const struct mfc_cache *mfc = v;
2631 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002632 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002633
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002634 seq_printf(seq, "%08X %08X %-3hd",
2635 (__force u32) mfc->mfc_mcastgrp,
2636 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002637 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Patrick McHardy0c122952010-04-13 05:03:22 +00002639 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002640 seq_printf(seq, " %8lu %8lu %8lu",
2641 mfc->mfc_un.res.pkt,
2642 mfc->mfc_un.res.bytes,
2643 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002644 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002645 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002646 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002647 mfc->mfc_un.res.ttls[n] < 255)
2648 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002649 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 n, mfc->mfc_un.res.ttls[n]);
2651 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002652 } else {
2653 /* unresolved mfc_caches don't contain
2654 * pkt, bytes and wrong_if values
2655 */
2656 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658 seq_putc(seq, '\n');
2659 }
2660 return 0;
2661}
2662
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002663static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 .start = ipmr_mfc_seq_start,
2665 .next = ipmr_mfc_seq_next,
2666 .stop = ipmr_mfc_seq_stop,
2667 .show = ipmr_mfc_seq_show,
2668};
2669
2670static int ipmr_mfc_open(struct inode *inode, struct file *file)
2671{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002672 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2673 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
Arjan van de Ven9a321442007-02-12 00:55:35 -08002676static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 .owner = THIS_MODULE,
2678 .open = ipmr_mfc_open,
2679 .read = seq_read,
2680 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002681 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002683#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
2685#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002686static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002688 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689};
2690#endif
2691
2692
2693/*
2694 * Setup for IP multicast routing
2695 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002696static int __net_init ipmr_net_init(struct net *net)
2697{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002698 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002699
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002700 err = ipmr_rules_init(net);
2701 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002702 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002703
2704#ifdef CONFIG_PROC_FS
2705 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002706 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002707 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002708 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002709 goto proc_cache_fail;
2710#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002711 return 0;
2712
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002713#ifdef CONFIG_PROC_FS
2714proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002715 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002716proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002717 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002718#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002719fail:
2720 return err;
2721}
2722
2723static void __net_exit ipmr_net_exit(struct net *net)
2724{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002725#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002726 remove_proc_entry("ip_mr_cache", net->proc_net);
2727 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002728#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002729 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002730}
2731
2732static struct pernet_operations ipmr_net_ops = {
2733 .init = ipmr_net_init,
2734 .exit = ipmr_net_exit,
2735};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002736
Wang Chen03d2f892008-07-03 12:13:36 +08002737int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Wang Chen03d2f892008-07-03 12:13:36 +08002739 int err;
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2742 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002743 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002744 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002745 if (!mrt_cachep)
2746 return -ENOMEM;
2747
Benjamin Therycf958ae32009-01-22 04:56:16 +00002748 err = register_pernet_subsys(&ipmr_net_ops);
2749 if (err)
2750 goto reg_pernet_fail;
2751
Wang Chen03d2f892008-07-03 12:13:36 +08002752 err = register_netdevice_notifier(&ip_mr_notifier);
2753 if (err)
2754 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002755#ifdef CONFIG_IP_PIMSM_V2
2756 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002757 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002758 err = -EAGAIN;
2759 goto add_proto_fail;
2760 }
2761#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002762 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2763 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002764 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002765
Tom Goff403dbb92009-06-14 03:16:13 -07002766#ifdef CONFIG_IP_PIMSM_V2
2767add_proto_fail:
2768 unregister_netdevice_notifier(&ip_mr_notifier);
2769#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002770reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002771 unregister_pernet_subsys(&ipmr_net_ops);
2772reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002773 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002774 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775}