blob: 8626b645ec62047b29e362989062e148ae92af7d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/ipip.h>
65#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
70#define CONFIG_IP_PIMSM 1
71#endif
72
Patrick McHardy0c122952010-04-13 05:03:22 +000073struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000074 struct list_head list;
Patrick McHardy8de53df2010-04-15 13:29:28 +020075#ifdef CONFIG_NET_NS
76 struct net *net;
77#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +000078 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000079 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000080 struct timer_list ipmr_expire_timer;
81 struct list_head mfc_unres_queue;
82 struct list_head mfc_cache_array[MFC_LINES];
83 struct vif_device vif_table[MAXVIFS];
84 int maxvif;
85 atomic_t cache_resolve_queue_len;
86 int mroute_do_assert;
87 int mroute_do_pim;
88#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
89 int mroute_reg_vif_num;
90#endif
91};
92
Patrick McHardyf0ad0862010-04-13 05:03:23 +000093struct ipmr_rule {
94 struct fib_rule common;
95};
96
97struct ipmr_result {
98 struct mr_table *mrt;
99};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000102 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104
105static DEFINE_RWLOCK(mrt_lock);
106
107/*
108 * Multicast router control variables
109 */
110
Patrick McHardy0c122952010-04-13 05:03:22 +0000111#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* Special spinlock for queue of unresolved entries */
114static DEFINE_SPINLOCK(mfc_unres_lock);
115
116/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000117 * entries is changed only in process context and protected
118 * with weak lock mrt_lock. Queue of unresolved entries is protected
119 * with strong spinlock mfc_unres_lock.
120 *
121 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123
Christoph Lametere18b8902006-12-06 20:33:20 -0800124static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000126static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggerib9d798a2012-08-24 07:38:35 +0000127static void ipmr_free_table(struct mr_table *mrt);
128
Patrick McHardy0c122952010-04-13 05:03:22 +0000129static int ip_mr_forward(struct net *net, struct mr_table *mrt,
130 struct sk_buff *skb, struct mfc_cache *cache,
131 int local);
132static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000133 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200134static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
135 struct mfc_cache *c, struct rtmsg *rtm);
Francesco Ruggerib9d798a2012-08-24 07:38:35 +0000136static void mroute_clean_tables(struct mr_table *mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000137static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000139#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
140#define ipmr_for_each_table(mrt, net) \
141 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
142
143static struct mr_table *ipmr_get_table(struct net *net, u32 id)
144{
145 struct mr_table *mrt;
146
147 ipmr_for_each_table(mrt, net) {
148 if (mrt->id == id)
149 return mrt;
150 }
151 return NULL;
152}
153
David S. Millerda919812011-03-12 02:04:50 -0500154static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000155 struct mr_table **mrt)
156{
157 struct ipmr_result res;
158 struct fib_lookup_arg arg = { .result = &res, };
159 int err;
160
David S. Millerda919812011-03-12 02:04:50 -0500161 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
162 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000163 if (err < 0)
164 return err;
165 *mrt = res.mrt;
166 return 0;
167}
168
169static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
170 int flags, struct fib_lookup_arg *arg)
171{
172 struct ipmr_result *res = arg->result;
173 struct mr_table *mrt;
174
175 switch (rule->action) {
176 case FR_ACT_TO_TBL:
177 break;
178 case FR_ACT_UNREACHABLE:
179 return -ENETUNREACH;
180 case FR_ACT_PROHIBIT:
181 return -EACCES;
182 case FR_ACT_BLACKHOLE:
183 default:
184 return -EINVAL;
185 }
186
187 mrt = ipmr_get_table(rule->fr_net, rule->table);
188 if (mrt == NULL)
189 return -EAGAIN;
190 res->mrt = mrt;
191 return 0;
192}
193
194static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
195{
196 return 1;
197}
198
199static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
200 FRA_GENERIC_POLICY,
201};
202
203static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
204 struct fib_rule_hdr *frh, struct nlattr **tb)
205{
206 return 0;
207}
208
209static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
210 struct nlattr **tb)
211{
212 return 1;
213}
214
215static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
216 struct fib_rule_hdr *frh)
217{
218 frh->dst_len = 0;
219 frh->src_len = 0;
220 frh->tos = 0;
221 return 0;
222}
223
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200224static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200225 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000226 .rule_size = sizeof(struct ipmr_rule),
227 .addr_size = sizeof(u32),
228 .action = ipmr_rule_action,
229 .match = ipmr_rule_match,
230 .configure = ipmr_rule_configure,
231 .compare = ipmr_rule_compare,
232 .default_pref = fib_default_rule_pref,
233 .fill = ipmr_rule_fill,
234 .nlgroup = RTNLGRP_IPV4_RULE,
235 .policy = ipmr_rule_policy,
236 .owner = THIS_MODULE,
237};
238
239static int __net_init ipmr_rules_init(struct net *net)
240{
241 struct fib_rules_ops *ops;
242 struct mr_table *mrt;
243 int err;
244
245 ops = fib_rules_register(&ipmr_rules_ops_template, net);
246 if (IS_ERR(ops))
247 return PTR_ERR(ops);
248
249 INIT_LIST_HEAD(&net->ipv4.mr_tables);
250
251 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
252 if (mrt == NULL) {
253 err = -ENOMEM;
254 goto err1;
255 }
256
257 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
258 if (err < 0)
259 goto err2;
260
261 net->ipv4.mr_rules_ops = ops;
262 return 0;
263
264err2:
265 kfree(mrt);
266err1:
267 fib_rules_unregister(ops);
268 return err;
269}
270
271static void __net_exit ipmr_rules_exit(struct net *net)
272{
273 struct mr_table *mrt, *next;
274
Eric Dumazet035320d2010-06-06 23:48:40 +0000275 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
276 list_del(&mrt->list);
Francesco Ruggerib9d798a2012-08-24 07:38:35 +0000277 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000278 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000279 fib_rules_unregister(net->ipv4.mr_rules_ops);
280}
281#else
282#define ipmr_for_each_table(mrt, net) \
283 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
284
285static struct mr_table *ipmr_get_table(struct net *net, u32 id)
286{
287 return net->ipv4.mrt;
288}
289
David S. Millerda919812011-03-12 02:04:50 -0500290static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000291 struct mr_table **mrt)
292{
293 *mrt = net->ipv4.mrt;
294 return 0;
295}
296
297static int __net_init ipmr_rules_init(struct net *net)
298{
299 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
300 return net->ipv4.mrt ? 0 : -ENOMEM;
301}
302
303static void __net_exit ipmr_rules_exit(struct net *net)
304{
Francesco Ruggerib9d798a2012-08-24 07:38:35 +0000305 ipmr_free_table(net->ipv4.mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000306}
307#endif
308
309static struct mr_table *ipmr_new_table(struct net *net, u32 id)
310{
311 struct mr_table *mrt;
312 unsigned int i;
313
314 mrt = ipmr_get_table(net, id);
315 if (mrt != NULL)
316 return mrt;
317
318 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
319 if (mrt == NULL)
320 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200321 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000322 mrt->id = id;
323
324 /* Forwarding cache */
325 for (i = 0; i < MFC_LINES; i++)
326 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
327
328 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
329
330 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
331 (unsigned long)mrt);
332
333#ifdef CONFIG_IP_PIMSM
334 mrt->mroute_reg_vif_num = -1;
335#endif
336#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
337 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
338#endif
339 return mrt;
340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Francesco Ruggerib9d798a2012-08-24 07:38:35 +0000342static void ipmr_free_table(struct mr_table *mrt)
343{
344 del_timer_sync(&mrt->ipmr_expire_timer);
345 mroute_clean_tables(mrt);
346 kfree(mrt);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
350
Wang Chend6070322008-07-14 20:55:26 -0700351static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
352{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000353 struct net *net = dev_net(dev);
354
Wang Chend6070322008-07-14 20:55:26 -0700355 dev_close(dev);
356
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000357 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700358 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800359 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700360 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700361 struct ip_tunnel_parm p;
362
363 memset(&p, 0, sizeof(p));
364 p.iph.daddr = v->vifc_rmt_addr.s_addr;
365 p.iph.saddr = v->vifc_lcl_addr.s_addr;
366 p.iph.version = 4;
367 p.iph.ihl = 5;
368 p.iph.protocol = IPPROTO_IPIP;
369 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
370 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
371
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800372 if (ops->ndo_do_ioctl) {
373 mm_segment_t oldfs = get_fs();
374
375 set_fs(KERNEL_DS);
376 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
377 set_fs(oldfs);
378 }
Wang Chend6070322008-07-14 20:55:26 -0700379 }
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000383struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct net_device *dev;
386
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000387 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800390 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int err;
392 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct ip_tunnel_parm p;
394 struct in_device *in_dev;
395
396 memset(&p, 0, sizeof(p));
397 p.iph.daddr = v->vifc_rmt_addr.s_addr;
398 p.iph.saddr = v->vifc_lcl_addr.s_addr;
399 p.iph.version = 4;
400 p.iph.ihl = 5;
401 p.iph.protocol = IPPROTO_IPIP;
402 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800403 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800405 if (ops->ndo_do_ioctl) {
406 mm_segment_t oldfs = get_fs();
407
408 set_fs(KERNEL_DS);
409 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
410 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000411 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800412 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 dev = NULL;
415
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000416 if (err == 0 &&
417 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 dev->flags |= IFF_MULTICAST;
419
Herbert Xue5ed6392005-10-03 14:35:55 -0700420 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700421 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700423
424 ipv4_devconf_setall(in_dev);
425 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if (dev_open(dev))
428 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700429 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431 }
432 return dev;
433
434failure:
435 /* allow the register to be completed before unregistering. */
436 rtnl_unlock();
437 rtnl_lock();
438
439 unregister_netdevice(dev);
440 return NULL;
441}
442
443#ifdef CONFIG_IP_PIMSM
444
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000445static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000447 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000448 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500449 struct flowi4 fl4 = {
450 .flowi4_oif = dev->ifindex,
451 .flowi4_iif = skb->skb_iif,
452 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000453 };
454 int err;
455
David S. Millerda919812011-03-12 02:04:50 -0500456 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000457 if (err < 0) {
458 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000459 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000460 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700463 dev->stats.tx_bytes += skb->len;
464 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000465 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 read_unlock(&mrt_lock);
467 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000468 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Stephen Hemminger007c3832008-11-20 20:28:35 -0800471static const struct net_device_ops reg_vif_netdev_ops = {
472 .ndo_start_xmit = reg_vif_xmit,
473};
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static void reg_vif_setup(struct net_device *dev)
476{
477 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800478 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800480 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700482 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000485static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct net_device *dev;
488 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000489 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000491 if (mrt->id == RT_TABLE_DEFAULT)
492 sprintf(name, "pimreg");
493 else
494 sprintf(name, "pimreg%u", mrt->id);
495
496 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (dev == NULL)
499 return NULL;
500
Tom Goff403dbb92009-06-14 03:16:13 -0700501 dev_net_set(dev, net);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (register_netdevice(dev)) {
504 free_netdev(dev);
505 return NULL;
506 }
507 dev->iflink = 0;
508
Herbert Xu71e27da2007-06-04 23:36:06 -0700509 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000510 in_dev = __in_dev_get_rcu(dev);
511 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700512 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Herbert Xu71e27da2007-06-04 23:36:06 -0700516 ipv4_devconf_setall(in_dev);
517 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
518 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (dev_open(dev))
521 goto failure;
522
Wang Chen7dc00c82008-07-14 20:56:34 -0700523 dev_hold(dev);
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return dev;
526
527failure:
528 /* allow the register to be completed before unregistering. */
529 rtnl_unlock();
530 rtnl_lock();
531
532 unregister_netdevice(dev);
533 return NULL;
534}
535#endif
536
537/*
538 * Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700539 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900541
Patrick McHardy0c122952010-04-13 05:03:22 +0000542static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000543 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct vif_device *v;
546 struct net_device *dev;
547 struct in_device *in_dev;
548
Patrick McHardy0c122952010-04-13 05:03:22 +0000549 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return -EADDRNOTAVAIL;
551
Patrick McHardy0c122952010-04-13 05:03:22 +0000552 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 write_lock_bh(&mrt_lock);
555 dev = v->dev;
556 v->dev = NULL;
557
558 if (!dev) {
559 write_unlock_bh(&mrt_lock);
560 return -EADDRNOTAVAIL;
561 }
562
563#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000564 if (vifi == mrt->mroute_reg_vif_num)
565 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#endif
567
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000568 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000570
571 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000572 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
574 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000575 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 write_unlock_bh(&mrt_lock);
579
580 dev_set_allmulti(dev, -1);
581
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000582 in_dev = __in_dev_get_rtnl(dev);
583 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700584 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ip_rt_multicast_event(in_dev);
586 }
587
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000588 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000589 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 dev_put(dev);
592 return 0;
593}
594
Eric Dumazeta8c94862010-10-01 16:15:08 +0000595static void ipmr_cache_free_rcu(struct rcu_head *head)
596{
597 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
598
599 kmem_cache_free(mrt_cachep, c);
600}
601
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000602static inline void ipmr_cache_free(struct mfc_cache *c)
603{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000604 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000608 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
610
Patrick McHardy0c122952010-04-13 05:03:22 +0000611static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200613 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700615 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Patrick McHardy0c122952010-04-13 05:03:22 +0000617 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Jianjun Kongc354e122008-11-03 00:28:02 -0800619 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700620 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
622 nlh->nlmsg_type = NLMSG_ERROR;
623 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
624 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700625 e = NLMSG_DATA(nlh);
626 e->error = -ETIMEDOUT;
627 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700628
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000629 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000630 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000635 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638
Patrick McHardye258beb2010-04-13 05:03:19 +0000639/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Patrick McHardye258beb2010-04-13 05:03:19 +0000641static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Patrick McHardy0c122952010-04-13 05:03:22 +0000643 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 unsigned long now;
645 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000646 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000649 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return;
651 }
652
Patrick McHardy0c122952010-04-13 05:03:22 +0000653 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto out;
655
656 now = jiffies;
657 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Patrick McHardy0c122952010-04-13 05:03:22 +0000659 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (time_after(c->mfc_un.unres.expires, now)) {
661 unsigned long interval = c->mfc_un.unres.expires - now;
662 if (interval < expires)
663 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 continue;
665 }
666
Patrick McHardy862465f2010-04-13 05:03:21 +0000667 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000668 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
Patrick McHardy0c122952010-04-13 05:03:22 +0000671 if (!list_empty(&mrt->mfc_unres_queue))
672 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674out:
675 spin_unlock(&mfc_unres_lock);
676}
677
678/* Fill oifs list. It is called under write locked mrt_lock. */
679
Patrick McHardy0c122952010-04-13 05:03:22 +0000680static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000681 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 int vifi;
684
685 cache->mfc_un.res.minvif = MAXVIFS;
686 cache->mfc_un.res.maxvif = 0;
687 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
688
Patrick McHardy0c122952010-04-13 05:03:22 +0000689 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
690 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000691 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
693 if (cache->mfc_un.res.minvif > vifi)
694 cache->mfc_un.res.minvif = vifi;
695 if (cache->mfc_un.res.maxvif <= vifi)
696 cache->mfc_un.res.maxvif = vifi + 1;
697 }
698 }
699}
700
Patrick McHardy0c122952010-04-13 05:03:22 +0000701static int vif_add(struct net *net, struct mr_table *mrt,
702 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000705 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct net_device *dev;
707 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700708 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000711 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return -EADDRINUSE;
713
714 switch (vifc->vifc_flags) {
715#ifdef CONFIG_IP_PIMSM
716 case VIFF_REGISTER:
717 /*
718 * Special Purpose VIF in PIM
719 * All the packets will be sent to the daemon
720 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000721 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000723 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!dev)
725 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700726 err = dev_set_allmulti(dev, 1);
727 if (err) {
728 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700729 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700730 return err;
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900734 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000735 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!dev)
737 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700738 err = dev_set_allmulti(dev, 1);
739 if (err) {
740 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700741 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700742 return err;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000745
746 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000748 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
749 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000750 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000751 dev_put(dev);
752 return -EADDRNOTAVAIL;
753 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000754 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000755 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!dev)
758 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700759 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700760 if (err) {
761 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700762 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
765 default:
766 return -EINVAL;
767 }
768
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000769 in_dev = __in_dev_get_rtnl(dev);
770 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000771 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000773 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700774 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ip_rt_multicast_event(in_dev);
776
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000777 /* Fill in the VIF structures */
778
Jianjun Kongc354e122008-11-03 00:28:02 -0800779 v->rate_limit = vifc->vifc_rate_limit;
780 v->local = vifc->vifc_lcl_addr.s_addr;
781 v->remote = vifc->vifc_rmt_addr.s_addr;
782 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!mrtsock)
784 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800785 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 v->bytes_in = 0;
787 v->bytes_out = 0;
788 v->pkt_in = 0;
789 v->pkt_out = 0;
790 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000791 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 v->link = dev->iflink;
793
794 /* And finish update writing critical data */
795 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800796 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000798 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000799 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000801 if (vifi+1 > mrt->maxvif)
802 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 write_unlock_bh(&mrt_lock);
804 return 0;
805}
806
Eric Dumazeta8c94862010-10-01 16:15:08 +0000807/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000808static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000809 __be32 origin,
810 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Jianjun Kongc354e122008-11-03 00:28:02 -0800812 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct mfc_cache *c;
814
Eric Dumazeta8c94862010-10-01 16:15:08 +0000815 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000816 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
817 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000819 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822/*
823 * Allocate a multicast cache entry
824 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000825static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Jianjun Kongc354e122008-11-03 00:28:02 -0800827 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000828
829 if (c)
830 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return c;
832}
833
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000834static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Jianjun Kongc354e122008-11-03 00:28:02 -0800836 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000837
838 if (c) {
839 skb_queue_head_init(&c->mfc_un.unres.unresolved);
840 c->mfc_un.unres.expires = jiffies + 10*HZ;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return c;
843}
844
845/*
846 * A cache entry has gone into a resolved state from queued
847 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900848
Patrick McHardy0c122952010-04-13 05:03:22 +0000849static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
850 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700853 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000855 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Jianjun Kongc354e122008-11-03 00:28:02 -0800857 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700858 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
860
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200861 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000862 nlh->nlmsg_len = skb_tail_pointer(skb) -
863 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 } else {
865 nlh->nlmsg_type = NLMSG_ERROR;
866 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
867 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700868 e = NLMSG_DATA(nlh);
869 e->error = -EMSGSIZE;
870 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Thomas Graf2942e902006-08-15 00:30:25 -0700872
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000873 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000874 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000875 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878}
879
880/*
881 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
882 * expects the following bizarre scheme.
883 *
884 * Called under mrt_lock.
885 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900886
Patrick McHardy0c122952010-04-13 05:03:22 +0000887static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000888 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300891 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 struct igmphdr *igmp;
893 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000894 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 int ret;
896
897#ifdef CONFIG_IP_PIMSM
898 if (assert == IGMPMSG_WHOLEPKT)
899 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
900 else
901#endif
902 skb = alloc_skb(128, GFP_ATOMIC);
903
Stephen Hemminger132adf52007-03-08 20:44:43 -0800904 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -ENOBUFS;
906
907#ifdef CONFIG_IP_PIMSM
908 if (assert == IGMPMSG_WHOLEPKT) {
909 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000910 * Duplicate old header, fix ihl, length etc.
911 * And all this only to mangle msg->im_msgtype and
912 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300914 skb_push(skb, sizeof(struct iphdr));
915 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300916 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300917 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700918 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 msg->im_msgtype = IGMPMSG_WHOLEPKT;
920 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000921 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700922 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
923 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
924 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900925 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900927 {
928
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000929 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700931 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300932 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300933 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000934 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700935 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000937 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000939 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000941 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 igmp->type =
943 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000944 igmp->code = 0;
945 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700946 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Eric Dumazet4c968702010-10-01 16:15:01 +0000949 rcu_read_lock();
950 mroute_sk = rcu_dereference(mrt->mroute_sk);
951 if (mroute_sk == NULL) {
952 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 kfree_skb(skb);
954 return -EINVAL;
955 }
956
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000957 /* Deliver to mrouted */
958
Eric Dumazet4c968702010-10-01 16:15:01 +0000959 ret = sock_queue_rcv_skb(mroute_sk, skb);
960 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +0000961 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +0000963 pr_warn("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 kfree_skb(skb);
965 }
966
967 return ret;
968}
969
970/*
971 * Queue a packet for resolution. It gets locked cache entry!
972 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static int
Patrick McHardy0c122952010-04-13 05:03:22 +0000975ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Patrick McHardy862465f2010-04-13 05:03:21 +0000977 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 int err;
979 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700980 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000983 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000984 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000985 c->mfc_origin == iph->saddr) {
986 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
Patrick McHardy862465f2010-04-13 05:03:21 +0000991 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000992 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Patrick McHardy0c122952010-04-13 05:03:22 +0000994 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000995 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 spin_unlock_bh(&mfc_unres_lock);
997
998 kfree_skb(skb);
999 return -ENOBUFS;
1000 }
1001
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001002 /* Fill in the new cache entry */
1003
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001004 c->mfc_parent = -1;
1005 c->mfc_origin = iph->saddr;
1006 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001008 /* Reflect first query at mrouted. */
1009
Patrick McHardy0c122952010-04-13 05:03:22 +00001010 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001011 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001012 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 out - Brad Parker
1014 */
1015 spin_unlock_bh(&mfc_unres_lock);
1016
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001017 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 kfree_skb(skb);
1019 return err;
1020 }
1021
Patrick McHardy0c122952010-04-13 05:03:22 +00001022 atomic_inc(&mrt->cache_resolve_queue_len);
1023 list_add(&c->list, &mrt->mfc_unres_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
David S. Miller278554b2010-05-12 00:05:35 -07001025 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1026 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001029 /* See if we can append the packet */
1030
1031 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 kfree_skb(skb);
1033 err = -ENOBUFS;
1034 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001035 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 err = 0;
1037 }
1038
1039 spin_unlock_bh(&mfc_unres_lock);
1040 return err;
1041}
1042
1043/*
1044 * MFC cache manipulation by user space mroute daemon
1045 */
1046
Patrick McHardy0c122952010-04-13 05:03:22 +00001047static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001050 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Jianjun Kongc354e122008-11-03 00:28:02 -08001052 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Patrick McHardy0c122952010-04-13 05:03:22 +00001054 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1056 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001057 list_del_rcu(&c->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001059 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 0;
1061 }
1062 }
1063 return -ENOENT;
1064}
1065
Patrick McHardy0c122952010-04-13 05:03:22 +00001066static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1067 struct mfcctl *mfc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Patrick McHardy862465f2010-04-13 05:03:21 +00001069 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001071 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Patrick McHardya50436f2010-03-17 06:04:14 +00001073 if (mfc->mfcc_parent >= MAXVIFS)
1074 return -ENFILE;
1075
Jianjun Kongc354e122008-11-03 00:28:02 -08001076 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Patrick McHardy0c122952010-04-13 05:03:22 +00001078 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001080 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1081 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
Patrick McHardy862465f2010-04-13 05:03:21 +00001086 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 write_lock_bh(&mrt_lock);
1088 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001089 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (!mrtsock)
1091 c->mfc_flags |= MFC_STATIC;
1092 write_unlock_bh(&mrt_lock);
1093 return 0;
1094 }
1095
Joe Perchesf97c1e02007-12-16 13:45:43 -08001096 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -EINVAL;
1098
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001099 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001100 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return -ENOMEM;
1102
Jianjun Kongc354e122008-11-03 00:28:02 -08001103 c->mfc_origin = mfc->mfcc_origin.s_addr;
1104 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1105 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001106 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!mrtsock)
1108 c->mfc_flags |= MFC_STATIC;
1109
Eric Dumazeta8c94862010-10-01 16:15:08 +00001110 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /*
1113 * Check to see if we resolved a queued list. If so we
1114 * need to send on the frames and tidy up.
1115 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001116 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001118 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001119 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001121 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001122 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001123 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125 }
1126 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001127 if (list_empty(&mrt->mfc_unres_queue))
1128 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 spin_unlock_bh(&mfc_unres_lock);
1130
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001131 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001132 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001133 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 return 0;
1136}
1137
1138/*
1139 * Close the multicast socket, and clear the vif tables etc
1140 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001141
Patrick McHardy0c122952010-04-13 05:03:22 +00001142static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001145 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001146 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001147
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001148 /* Shut down all active vif entries */
1149
Patrick McHardy0c122952010-04-13 05:03:22 +00001150 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001151 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001152 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001154 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001156 /* Wipe the cache */
1157
Patrick McHardy862465f2010-04-13 05:03:21 +00001158 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001159 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001160 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001162 list_del_rcu(&c->list);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001163 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 }
1166
Patrick McHardy0c122952010-04-13 05:03:22 +00001167 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001169 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001170 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001171 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 spin_unlock_bh(&mfc_unres_lock);
1174 }
1175}
1176
Eric Dumazet4c968702010-10-01 16:15:01 +00001177/* called from ip_ra_control(), before an RCU grace period,
1178 * we dont need to call synchronize_rcu() here
1179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180static void mrtsock_destruct(struct sock *sk)
1181{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001182 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001183 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001186 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001187 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001188 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001189 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001190 mroute_clean_tables(mrt);
1191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193 rtnl_unlock();
1194}
1195
1196/*
1197 * Socket options and virtual interface manipulation. The whole
1198 * virtual interface system is a complete heap, but unfortunately
1199 * that's how BSD mrouted happens to think. Maybe one day with a proper
1200 * MOSPF/PIM router set up we can clean this up.
1201 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001202
David S. Millerb7058842009-09-30 16:12:20 -07001203int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 int ret;
1206 struct vifctl vif;
1207 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001208 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001209 struct mr_table *mrt;
1210
1211 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1212 if (mrt == NULL)
1213 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001214
Stephen Hemminger132adf52007-03-08 20:44:43 -08001215 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001216 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric Dumazet4c968702010-10-01 16:15:01 +00001217 !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return -EACCES;
1219 }
1220
Stephen Hemminger132adf52007-03-08 20:44:43 -08001221 switch (optname) {
1222 case MRT_INIT:
1223 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001224 inet_sk(sk)->inet_num != IPPROTO_IGMP)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001225 return -EOPNOTSUPP;
Jianjun Kongc354e122008-11-03 00:28:02 -08001226 if (optlen != sizeof(int))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001227 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Stephen Hemminger132adf52007-03-08 20:44:43 -08001229 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001230 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001232 return -EADDRINUSE;
1233 }
1234
1235 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1236 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001237 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001238 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001239 }
1240 rtnl_unlock();
1241 return ret;
1242 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001243 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001244 return -EACCES;
1245 return ip_ra_control(sk, 0, NULL);
1246 case MRT_ADD_VIF:
1247 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001248 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001249 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001250 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001251 return -EFAULT;
1252 if (vif.vifc_vifi >= MAXVIFS)
1253 return -ENFILE;
1254 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001255 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001256 ret = vif_add(net, mrt, &vif,
1257 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001258 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001259 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001260 }
1261 rtnl_unlock();
1262 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /*
1265 * Manipulate the forwarding caches. These live
1266 * in a sort of kernel/user symbiosis.
1267 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001268 case MRT_ADD_MFC:
1269 case MRT_DEL_MFC:
Jianjun Kongc354e122008-11-03 00:28:02 -08001270 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001271 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001272 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001273 return -EFAULT;
1274 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001275 if (optname == MRT_DEL_MFC)
Patrick McHardy0c122952010-04-13 05:03:22 +00001276 ret = ipmr_mfc_delete(mrt, &mfc);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001277 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001278 ret = ipmr_mfc_add(net, mrt, &mfc,
1279 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001280 rtnl_unlock();
1281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * Control PIM assert.
1284 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001285 case MRT_ASSERT:
1286 {
1287 int v;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001288 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001290 mrt->mroute_do_assert = (v) ? 1 : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001291 return 0;
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001294 case MRT_PIM:
1295 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001296 int v;
1297
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001298 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001299 return -EFAULT;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001300 v = (v) ? 1 : 0;
1301
Stephen Hemminger132adf52007-03-08 20:44:43 -08001302 rtnl_lock();
1303 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001304 if (v != mrt->mroute_do_pim) {
1305 mrt->mroute_do_pim = v;
1306 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001308 rtnl_unlock();
1309 return ret;
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001312#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1313 case MRT_TABLE:
1314 {
1315 u32 v;
1316
1317 if (optlen != sizeof(u32))
1318 return -EINVAL;
1319 if (get_user(v, (u32 __user *)optval))
1320 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001321
1322 rtnl_lock();
1323 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001324 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1325 ret = -EBUSY;
1326 } else {
1327 if (!ipmr_new_table(net, v))
1328 ret = -ENOMEM;
1329 raw_sk(sk)->ipmr_table = v;
1330 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001331 rtnl_unlock();
1332 return ret;
1333 }
1334#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001335 /*
1336 * Spurious command, or MRT_VERSION which you cannot
1337 * set.
1338 */
1339 default:
1340 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342}
1343
1344/*
1345 * Getsock opt support for the multicast routing system.
1346 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001347
Jianjun Kongc354e122008-11-03 00:28:02 -08001348int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 int olr;
1351 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001352 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001353 struct mr_table *mrt;
1354
1355 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1356 if (mrt == NULL)
1357 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Jianjun Kongc354e122008-11-03 00:28:02 -08001359 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001361 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001363 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -ENOPROTOOPT;
1365
1366 if (get_user(olr, optlen))
1367 return -EFAULT;
1368
1369 olr = min_t(unsigned int, olr, sizeof(int));
1370 if (olr < 0)
1371 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001372
Jianjun Kongc354e122008-11-03 00:28:02 -08001373 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001375 if (optname == MRT_VERSION)
1376 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001378 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001379 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#endif
1381 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001382 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001383 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return -EFAULT;
1385 return 0;
1386}
1387
1388/*
1389 * The IP multicast ioctl support routines.
1390 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1393{
1394 struct sioc_sg_req sr;
1395 struct sioc_vif_req vr;
1396 struct vif_device *vif;
1397 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001398 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001399 struct mr_table *mrt;
1400
1401 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1402 if (mrt == NULL)
1403 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001404
Stephen Hemminger132adf52007-03-08 20:44:43 -08001405 switch (cmd) {
1406 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001407 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001408 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001409 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001410 return -EINVAL;
1411 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001412 vif = &mrt->vif_table[vr.vifi];
1413 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001414 vr.icount = vif->pkt_in;
1415 vr.ocount = vif->pkt_out;
1416 vr.ibytes = vif->bytes_in;
1417 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001419
Jianjun Kongc354e122008-11-03 00:28:02 -08001420 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001422 return 0;
1423 }
1424 read_unlock(&mrt_lock);
1425 return -EADDRNOTAVAIL;
1426 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001427 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001428 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Eric Dumazeta8c94862010-10-01 16:15:08 +00001430 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001431 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001432 if (c) {
1433 sr.pktcnt = c->mfc_un.res.pkt;
1434 sr.bytecnt = c->mfc_un.res.bytes;
1435 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001436 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001437
Jianjun Kongc354e122008-11-03 00:28:02 -08001438 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001439 return -EFAULT;
1440 return 0;
1441 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001442 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001443 return -EADDRNOTAVAIL;
1444 default:
1445 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447}
1448
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001449#ifdef CONFIG_COMPAT
1450struct compat_sioc_sg_req {
1451 struct in_addr src;
1452 struct in_addr grp;
1453 compat_ulong_t pktcnt;
1454 compat_ulong_t bytecnt;
1455 compat_ulong_t wrong_if;
1456};
1457
David S. Millerca6b8bb2011-02-03 17:24:28 -08001458struct compat_sioc_vif_req {
1459 vifi_t vifi; /* Which iface */
1460 compat_ulong_t icount;
1461 compat_ulong_t ocount;
1462 compat_ulong_t ibytes;
1463 compat_ulong_t obytes;
1464};
1465
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001466int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1467{
David S. Miller0033d5a2011-02-03 17:21:31 -08001468 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb2011-02-03 17:24:28 -08001469 struct compat_sioc_vif_req vr;
1470 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001471 struct mfc_cache *c;
1472 struct net *net = sock_net(sk);
1473 struct mr_table *mrt;
1474
1475 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1476 if (mrt == NULL)
1477 return -ENOENT;
1478
1479 switch (cmd) {
David S. Millerca6b8bb2011-02-03 17:24:28 -08001480 case SIOCGETVIFCNT:
1481 if (copy_from_user(&vr, arg, sizeof(vr)))
1482 return -EFAULT;
1483 if (vr.vifi >= mrt->maxvif)
1484 return -EINVAL;
1485 read_lock(&mrt_lock);
1486 vif = &mrt->vif_table[vr.vifi];
1487 if (VIF_EXISTS(mrt, vr.vifi)) {
1488 vr.icount = vif->pkt_in;
1489 vr.ocount = vif->pkt_out;
1490 vr.ibytes = vif->bytes_in;
1491 vr.obytes = vif->bytes_out;
1492 read_unlock(&mrt_lock);
1493
1494 if (copy_to_user(arg, &vr, sizeof(vr)))
1495 return -EFAULT;
1496 return 0;
1497 }
1498 read_unlock(&mrt_lock);
1499 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001500 case SIOCGETSGCNT:
1501 if (copy_from_user(&sr, arg, sizeof(sr)))
1502 return -EFAULT;
1503
1504 rcu_read_lock();
1505 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1506 if (c) {
1507 sr.pktcnt = c->mfc_un.res.pkt;
1508 sr.bytecnt = c->mfc_un.res.bytes;
1509 sr.wrong_if = c->mfc_un.res.wrong_if;
1510 rcu_read_unlock();
1511
1512 if (copy_to_user(arg, &sr, sizeof(sr)))
1513 return -EFAULT;
1514 return 0;
1515 }
1516 rcu_read_unlock();
1517 return -EADDRNOTAVAIL;
1518 default:
1519 return -ENOIOCTLCMD;
1520 }
1521}
1522#endif
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1526{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001527 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001528 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001529 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 struct vif_device *v;
1531 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (event != NETDEV_UNREGISTER)
1534 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001535
1536 ipmr_for_each_table(mrt, net) {
1537 v = &mrt->vif_table[0];
1538 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1539 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001540 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543 return NOTIFY_DONE;
1544}
1545
1546
Jianjun Kongc354e122008-11-03 00:28:02 -08001547static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 .notifier_call = ipmr_device_event,
1549};
1550
1551/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001552 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * This avoids tunnel drivers and other mess and gives us the speed so
1554 * important for multicast video.
1555 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001556
Al Viro114c7842006-09-27 18:39:29 -07001557static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001559 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001560 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001561
1562 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001563 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001564 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001565 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001567 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001568 iph->tos = old_iph->tos;
1569 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 iph->frag_off = 0;
1571 iph->daddr = daddr;
1572 iph->saddr = saddr;
1573 iph->protocol = IPPROTO_IPIP;
1574 iph->ihl = 5;
1575 iph->tot_len = htons(skb->len);
Eric Dumazetadf30902009-06-02 05:19:30 +00001576 ip_select_ident(iph, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 ip_send_check(iph);
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1580 nf_reset(skb);
1581}
1582
1583static inline int ipmr_forward_finish(struct sk_buff *skb)
1584{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001585 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Eric Dumazetadf30902009-06-02 05:19:30 +00001587 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 if (unlikely(opt->optlen))
1590 ip_forward_options(skb);
1591
1592 return dst_output(skb);
1593}
1594
1595/*
1596 * Processing handlers for ipmr_forward
1597 */
1598
Patrick McHardy0c122952010-04-13 05:03:22 +00001599static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1600 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001602 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001603 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 struct net_device *dev;
1605 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001606 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 int encap = 0;
1608
1609 if (vif->dev == NULL)
1610 goto out_free;
1611
1612#ifdef CONFIG_IP_PIMSM
1613 if (vif->flags & VIFF_REGISTER) {
1614 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001615 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001616 vif->dev->stats.tx_bytes += skb->len;
1617 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001618 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001619 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621#endif
1622
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001623 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001624 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001625 vif->remote, vif->local,
1626 0, 0,
1627 IPPROTO_IPIP,
1628 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001629 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto out_free;
1631 encap = sizeof(struct iphdr);
1632 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001633 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001634 0, 0,
1635 IPPROTO_IPIP,
1636 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001637 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 goto out_free;
1639 }
1640
Changli Gaod8d1f302010-06-10 23:31:35 -07001641 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Changli Gaod8d1f302010-06-10 23:31:35 -07001643 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001645 * allow to send ICMP, so that packets will disappear
1646 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
1648
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001649 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ip_rt_put(rt);
1651 goto out_free;
1652 }
1653
Changli Gaod8d1f302010-06-10 23:31:35 -07001654 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001657 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 goto out_free;
1659 }
1660
1661 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001662 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Eric Dumazetadf30902009-06-02 05:19:30 +00001664 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001665 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001666 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001669 * What do we do with netfilter? -- RR
1670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (vif->flags & VIFF_TUNNEL) {
1672 ip_encap(skb, vif->local, vif->remote);
1673 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001674 vif->dev->stats.tx_packets++;
1675 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677
1678 IPCB(skb)->flags |= IPSKB_FORWARDED;
1679
1680 /*
1681 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1682 * not only before forwarding, but after forwarding on all output
1683 * interfaces. It is clear, if mrouter runs a multicasting
1684 * program, it should receive packets not depending to what interface
1685 * program is joined.
1686 * If we will not make it, the program will have to join on all
1687 * interfaces. On the other hand, multihoming host (or router, but
1688 * not mrouter) cannot join to more than one interface - it will
1689 * result in receiving multiple packets.
1690 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001691 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 ipmr_forward_finish);
1693 return;
1694
1695out_free:
1696 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Patrick McHardy0c122952010-04-13 05:03:22 +00001699static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001702
1703 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1704 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706 }
1707 return ct;
1708}
1709
1710/* "local" means that we should preserve one skb (for local delivery) */
1711
Patrick McHardy0c122952010-04-13 05:03:22 +00001712static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1713 struct sk_buff *skb, struct mfc_cache *cache,
1714 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 int psend = -1;
1717 int vif, ct;
1718
1719 vif = cache->mfc_parent;
1720 cache->mfc_un.res.pkt++;
1721 cache->mfc_un.res.bytes += skb->len;
1722
1723 /*
1724 * Wrong interface: drop packet and (maybe) send PIM assert.
1725 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001726 if (mrt->vif_table[vif].dev != skb->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int true_vifi;
1728
David S. Millerc7537962010-11-11 17:07:48 -08001729 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001731 * Very complicated situation...
1732 *
1733 * The best workaround until routing daemons will be
1734 * fixed is not to redistribute packet, if it was
1735 * send through wrong interface. It means, that
1736 * multicast applications WILL NOT work for
1737 * (S,G), which have default multicast route pointing
1738 * to wrong oif. In any case, it is not a good
1739 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
1741 goto dont_forward;
1742 }
1743
1744 cache->mfc_un.res.wrong_if++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001745 true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Patrick McHardy0c122952010-04-13 05:03:22 +00001747 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001749 * so that we cannot check that packet arrived on an oif.
1750 * It is bad, but otherwise we would need to move pretty
1751 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001753 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001754 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001755 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1757 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001758 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760 goto dont_forward;
1761 }
1762
Patrick McHardy0c122952010-04-13 05:03:22 +00001763 mrt->vif_table[vif].pkt_in++;
1764 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /*
1767 * Forward the frame
1768 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001769 for (ct = cache->mfc_un.res.maxvif - 1;
1770 ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001771 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (psend != -1) {
1773 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001776 ipmr_queue_xmit(net, mrt, skb2, cache,
1777 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001779 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 }
1782 if (psend != -1) {
1783 if (local) {
1784 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001787 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001789 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
1791 }
1792 }
1793
1794dont_forward:
1795 if (!local)
1796 kfree_skb(skb);
1797 return 0;
1798}
1799
David S. Miller417da662011-05-03 19:42:43 -07001800static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001801{
David S. Miller417da662011-05-03 19:42:43 -07001802 struct rtable *rt = skb_rtable(skb);
1803 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001804 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001805 .daddr = iph->daddr,
1806 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001807 .flowi4_tos = RT_TOS(iph->tos),
David S. Millerda919812011-03-12 02:04:50 -05001808 .flowi4_oif = rt->rt_oif,
1809 .flowi4_iif = rt->rt_iif,
1810 .flowi4_mark = rt->rt_mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001811 };
1812 struct mr_table *mrt;
1813 int err;
1814
David S. Millerda919812011-03-12 02:04:50 -05001815 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001816 if (err)
1817 return ERR_PTR(err);
1818 return mrt;
1819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821/*
1822 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001823 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
1825
1826int ip_mr_input(struct sk_buff *skb)
1827{
1828 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001829 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001830 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001831 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001834 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001836 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 goto dont_forward;
1838
David S. Miller417da662011-05-03 19:42:43 -07001839 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001840 if (IS_ERR(mrt)) {
1841 kfree_skb(skb);
1842 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001845 if (IPCB(skb)->opt.router_alert) {
1846 if (ip_call_ra_chain(skb))
1847 return 0;
1848 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1849 /* IGMPv1 (and broken IGMPv2 implementations sort of
1850 * Cisco IOS <= 11.2(8)) do not put router alert
1851 * option to IGMP packets destined to routable
1852 * groups. It is very bad, because it means
1853 * that we can forward NO IGMP messages.
1854 */
1855 struct sock *mroute_sk;
1856
1857 mroute_sk = rcu_dereference(mrt->mroute_sk);
1858 if (mroute_sk) {
1859 nf_reset(skb);
1860 raw_rcv(mroute_sk, skb);
1861 return 0;
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 }
1865
Eric Dumazeta8c94862010-10-01 16:15:08 +00001866 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001867 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /*
1870 * No usable cache entry
1871 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001872 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 int vif;
1874
1875 if (local) {
1876 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1877 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001878 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 skb = skb2;
1881 }
1882
Eric Dumazeta8c94862010-10-01 16:15:08 +00001883 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001884 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001886 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 read_unlock(&mrt_lock);
1888
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001889 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891 read_unlock(&mrt_lock);
1892 kfree_skb(skb);
1893 return -ENODEV;
1894 }
1895
Eric Dumazeta8c94862010-10-01 16:15:08 +00001896 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001897 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 read_unlock(&mrt_lock);
1899
1900 if (local)
1901 return ip_local_deliver(skb);
1902
1903 return 0;
1904
1905dont_forward:
1906 if (local)
1907 return ip_local_deliver(skb);
1908 kfree_skb(skb);
1909 return 0;
1910}
1911
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001912#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00001913/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001914static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1915 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001917 struct net_device *reg_dev = NULL;
1918 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001920 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001922 * Check that:
1923 * a. packet is really sent to a multicast group
1924 * b. packet is not a NULL-REGISTER
1925 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001927 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001929 ntohs(encap->tot_len) + pimlen > skb->len)
1930 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001933 if (mrt->mroute_reg_vif_num >= 0)
1934 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 read_unlock(&mrt_lock);
1936
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001937 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001938 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001940 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00001941 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001942 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00001944 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07001946
1947 skb_tunnel_rx(skb, reg_dev);
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001950
Eric Dumazet55747a02010-10-01 16:14:55 +00001951 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001952}
1953#endif
1954
1955#ifdef CONFIG_IP_PIMSM_V1
1956/*
1957 * Handle IGMP messages of PIMv1
1958 */
1959
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001960int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001961{
1962 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001963 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001964 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001965
1966 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1967 goto drop;
1968
1969 pim = igmp_hdr(skb);
1970
David S. Miller417da662011-05-03 19:42:43 -07001971 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001972 if (IS_ERR(mrt))
1973 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00001974 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001975 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1976 goto drop;
1977
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001978 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001979drop:
1980 kfree_skb(skb);
1981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return 0;
1983}
1984#endif
1985
1986#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001987static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001990 struct net *net = dev_net(skb->dev);
1991 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001993 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto drop;
1995
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001996 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001997 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1998 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001999 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002000 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 goto drop;
2002
David S. Miller417da662011-05-03 19:42:43 -07002003 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002004 if (IS_ERR(mrt))
2005 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002006 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002007drop:
2008 kfree_skb(skb);
2009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return 0;
2011}
2012#endif
2013
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002014static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2015 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 int ct;
2018 struct rtnexthop *nhp;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002019 u8 *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 struct rtattr *mp_head;
2021
Nicolas Dichtel74381892010-03-25 23:45:35 +00002022 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f1602010-05-26 00:38:56 -07002023 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002024 return -ENOENT;
2025
Patrick McHardy0c122952010-04-13 05:03:22 +00002026 if (VIF_EXISTS(mrt, c->mfc_parent))
2027 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Jianjun Kongc354e122008-11-03 00:28:02 -08002029 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002032 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2034 goto rtattr_failure;
Jianjun Kongc354e122008-11-03 00:28:02 -08002035 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 nhp->rtnh_flags = 0;
2037 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002038 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 nhp->rtnh_len = sizeof(*nhp);
2040 }
2041 }
2042 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002043 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 rtm->rtm_type = RTN_MULTICAST;
2045 return 1;
2046
2047rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002048 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return -EMSGSIZE;
2050}
2051
David S. Miller9a1b9492011-05-04 12:18:54 -07002052int ipmr_get_route(struct net *net, struct sk_buff *skb,
2053 __be32 saddr, __be32 daddr,
2054 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002057 struct mr_table *mrt;
2058 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002060 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2061 if (mrt == NULL)
2062 return -ENOENT;
2063
Eric Dumazeta8c94862010-10-01 16:15:08 +00002064 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002065 cache = ipmr_cache_find(mrt, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Jianjun Kongc354e122008-11-03 00:28:02 -08002067 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002068 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002069 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002071 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002074 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return -EAGAIN;
2076 }
2077
2078 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002079 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002080 if (dev)
2081 vif = ipmr_find_vif(mrt, dev);
2082 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002084 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return -ENODEV;
2086 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002087 skb2 = skb_clone(skb, GFP_ATOMIC);
2088 if (!skb2) {
2089 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002090 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002091 return -ENOMEM;
2092 }
2093
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002094 skb_push(skb2, sizeof(struct iphdr));
2095 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002096 iph = ip_hdr(skb2);
2097 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002098 iph->saddr = saddr;
2099 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002100 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002101 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002103 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return err;
2105 }
2106
Eric Dumazeta8c94862010-10-01 16:15:08 +00002107 read_lock(&mrt_lock);
2108 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002110 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002112 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return err;
2114}
2115
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002116static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2117 u32 pid, u32 seq, struct mfc_cache *c)
2118{
2119 struct nlmsghdr *nlh;
2120 struct rtmsg *rtm;
2121
2122 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2123 if (nlh == NULL)
2124 return -EMSGSIZE;
2125
2126 rtm = nlmsg_data(nlh);
2127 rtm->rtm_family = RTNL_FAMILY_IPMR;
2128 rtm->rtm_dst_len = 32;
2129 rtm->rtm_src_len = 32;
2130 rtm->rtm_tos = 0;
2131 rtm->rtm_table = mrt->id;
2132 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2133 rtm->rtm_type = RTN_MULTICAST;
2134 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2135 rtm->rtm_protocol = RTPROT_UNSPEC;
2136 rtm->rtm_flags = 0;
2137
2138 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2139 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2140
2141 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2142 goto nla_put_failure;
2143
2144 return nlmsg_end(skb, nlh);
2145
2146nla_put_failure:
2147 nlmsg_cancel(skb, nlh);
2148 return -EMSGSIZE;
2149}
2150
2151static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2152{
2153 struct net *net = sock_net(skb->sk);
2154 struct mr_table *mrt;
2155 struct mfc_cache *mfc;
2156 unsigned int t = 0, s_t;
2157 unsigned int h = 0, s_h;
2158 unsigned int e = 0, s_e;
2159
2160 s_t = cb->args[0];
2161 s_h = cb->args[1];
2162 s_e = cb->args[2];
2163
Eric Dumazeta8c94862010-10-01 16:15:08 +00002164 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002165 ipmr_for_each_table(mrt, net) {
2166 if (t < s_t)
2167 goto next_table;
2168 if (t > s_t)
2169 s_h = 0;
2170 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002171 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002172 if (e < s_e)
2173 goto next_entry;
2174 if (ipmr_fill_mroute(mrt, skb,
2175 NETLINK_CB(cb->skb).pid,
2176 cb->nlh->nlmsg_seq,
2177 mfc) < 0)
2178 goto done;
2179next_entry:
2180 e++;
2181 }
2182 e = s_e = 0;
2183 }
2184 s_h = 0;
2185next_table:
2186 t++;
2187 }
2188done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002189 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002190
2191 cb->args[2] = e;
2192 cb->args[1] = h;
2193 cb->args[0] = t;
2194
2195 return skb->len;
2196}
2197
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002198#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002200 * The /proc interfaces to multicast routing :
2201 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 */
2203struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002204 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002205 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 int ct;
2207};
2208
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002209static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2210 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 loff_t pos)
2212{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002213 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002214
2215 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2216 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002218 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002219 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221 return NULL;
2222}
2223
2224static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002225 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002227 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002228 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002229 struct mr_table *mrt;
2230
2231 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2232 if (mrt == NULL)
2233 return ERR_PTR(-ENOENT);
2234
2235 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002238 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 : SEQ_START_TOKEN;
2240}
2241
2242static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2243{
2244 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002245 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002246 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 ++*pos;
2249 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002250 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002251
Patrick McHardy0c122952010-04-13 05:03:22 +00002252 while (++iter->ct < mrt->maxvif) {
2253 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002255 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257 return NULL;
2258}
2259
2260static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002261 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 read_unlock(&mrt_lock);
2264}
2265
2266static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2267{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002268 struct ipmr_vif_iter *iter = seq->private;
2269 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002272 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2274 } else {
2275 const struct vif_device *vif = v;
2276 const char *name = vif->dev ? vif->dev->name : "none";
2277
2278 seq_printf(seq,
2279 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002280 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002281 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 vif->bytes_out, vif->pkt_out,
2283 vif->flags, vif->local, vif->remote);
2284 }
2285 return 0;
2286}
2287
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002288static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 .start = ipmr_vif_seq_start,
2290 .next = ipmr_vif_seq_next,
2291 .stop = ipmr_vif_seq_stop,
2292 .show = ipmr_vif_seq_show,
2293};
2294
2295static int ipmr_vif_open(struct inode *inode, struct file *file)
2296{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002297 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2298 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
Arjan van de Ven9a321442007-02-12 00:55:35 -08002301static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 .owner = THIS_MODULE,
2303 .open = ipmr_vif_open,
2304 .read = seq_read,
2305 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002306 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307};
2308
2309struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002310 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002311 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002312 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 int ct;
2314};
2315
2316
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002317static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2318 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002320 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 struct mfc_cache *mfc;
2322
Eric Dumazeta8c94862010-10-01 16:15:08 +00002323 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002324 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002325 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002326 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002327 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002329 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002330 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002333 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002334 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002335 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return mfc;
2337 spin_unlock_bh(&mfc_unres_lock);
2338
2339 it->cache = NULL;
2340 return NULL;
2341}
2342
2343
2344static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2345{
2346 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002347 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002348 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002349
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002350 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2351 if (mrt == NULL)
2352 return ERR_PTR(-ENOENT);
2353
2354 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 it->cache = NULL;
2356 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002357 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 : SEQ_START_TOKEN;
2359}
2360
2361static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2362{
2363 struct mfc_cache *mfc = v;
2364 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002365 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002366 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 ++*pos;
2369
2370 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002371 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Patrick McHardy862465f2010-04-13 05:03:21 +00002373 if (mfc->list.next != it->cache)
2374 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002375
Patrick McHardy0c122952010-04-13 05:03:22 +00002376 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 goto end_of_list;
2378
Patrick McHardy0c122952010-04-13 05:03:22 +00002379 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002382 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002383 if (list_empty(it->cache))
2384 continue;
2385 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
2388 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002389 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002390 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002394 if (!list_empty(it->cache))
2395 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002397end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 spin_unlock_bh(&mfc_unres_lock);
2399 it->cache = NULL;
2400
2401 return NULL;
2402}
2403
2404static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2405{
2406 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002407 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Patrick McHardy0c122952010-04-13 05:03:22 +00002409 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002411 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002412 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
2415static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2416{
2417 int n;
2418
2419 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002420 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2422 } else {
2423 const struct mfc_cache *mfc = v;
2424 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002425 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002426
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002427 seq_printf(seq, "%08X %08X %-3hd",
2428 (__force u32) mfc->mfc_mcastgrp,
2429 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002430 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Patrick McHardy0c122952010-04-13 05:03:22 +00002432 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002433 seq_printf(seq, " %8lu %8lu %8lu",
2434 mfc->mfc_un.res.pkt,
2435 mfc->mfc_un.res.bytes,
2436 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002437 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002438 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002439 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002440 mfc->mfc_un.res.ttls[n] < 255)
2441 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002442 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 n, mfc->mfc_un.res.ttls[n]);
2444 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002445 } else {
2446 /* unresolved mfc_caches don't contain
2447 * pkt, bytes and wrong_if values
2448 */
2449 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451 seq_putc(seq, '\n');
2452 }
2453 return 0;
2454}
2455
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002456static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 .start = ipmr_mfc_seq_start,
2458 .next = ipmr_mfc_seq_next,
2459 .stop = ipmr_mfc_seq_stop,
2460 .show = ipmr_mfc_seq_show,
2461};
2462
2463static int ipmr_mfc_open(struct inode *inode, struct file *file)
2464{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002465 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2466 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Arjan van de Ven9a321442007-02-12 00:55:35 -08002469static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 .owner = THIS_MODULE,
2471 .open = ipmr_mfc_open,
2472 .read = seq_read,
2473 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002474 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002479static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002481 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482};
2483#endif
2484
2485
2486/*
2487 * Setup for IP multicast routing
2488 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002489static int __net_init ipmr_net_init(struct net *net)
2490{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002491 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002492
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002493 err = ipmr_rules_init(net);
2494 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002495 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002496
2497#ifdef CONFIG_PROC_FS
2498 err = -ENOMEM;
2499 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2500 goto proc_vif_fail;
2501 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2502 goto proc_cache_fail;
2503#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002504 return 0;
2505
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002506#ifdef CONFIG_PROC_FS
2507proc_cache_fail:
2508 proc_net_remove(net, "ip_mr_vif");
2509proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002510 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002511#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002512fail:
2513 return err;
2514}
2515
2516static void __net_exit ipmr_net_exit(struct net *net)
2517{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002518#ifdef CONFIG_PROC_FS
2519 proc_net_remove(net, "ip_mr_cache");
2520 proc_net_remove(net, "ip_mr_vif");
2521#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002522 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002523}
2524
2525static struct pernet_operations ipmr_net_ops = {
2526 .init = ipmr_net_init,
2527 .exit = ipmr_net_exit,
2528};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002529
Wang Chen03d2f892008-07-03 12:13:36 +08002530int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Wang Chen03d2f892008-07-03 12:13:36 +08002532 int err;
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2535 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002536 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002537 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002538 if (!mrt_cachep)
2539 return -ENOMEM;
2540
Benjamin Therycf958ae32009-01-22 04:56:16 +00002541 err = register_pernet_subsys(&ipmr_net_ops);
2542 if (err)
2543 goto reg_pernet_fail;
2544
Wang Chen03d2f892008-07-03 12:13:36 +08002545 err = register_netdevice_notifier(&ip_mr_notifier);
2546 if (err)
2547 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002548#ifdef CONFIG_IP_PIMSM_V2
2549 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002550 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002551 err = -EAGAIN;
2552 goto add_proto_fail;
2553 }
2554#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002555 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2556 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002557 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002558
Tom Goff403dbb92009-06-14 03:16:13 -07002559#ifdef CONFIG_IP_PIMSM_V2
2560add_proto_fail:
2561 unregister_netdevice_notifier(&ip_mr_notifier);
2562#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002563reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002564 unregister_pernet_subsys(&ipmr_net_ops);
2565reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002566 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002567 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}