blob: 94cd1ef1e319187ff7718862b8344e848cef0d4d [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{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000157 int err;
Hannes Frederic Sowadf647932014-01-13 02:45:22 +0100158 struct ipmr_result res;
159 struct fib_lookup_arg arg = {
160 .result = &res,
161 .flags = FIB_LOOKUP_NOREF,
162 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000163
David S. Millerda919812011-03-12 02:04:50 -0500164 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
165 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000166 if (err < 0)
167 return err;
168 *mrt = res.mrt;
169 return 0;
170}
171
172static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
173 int flags, struct fib_lookup_arg *arg)
174{
175 struct ipmr_result *res = arg->result;
176 struct mr_table *mrt;
177
178 switch (rule->action) {
179 case FR_ACT_TO_TBL:
180 break;
181 case FR_ACT_UNREACHABLE:
182 return -ENETUNREACH;
183 case FR_ACT_PROHIBIT:
184 return -EACCES;
185 case FR_ACT_BLACKHOLE:
186 default:
187 return -EINVAL;
188 }
189
190 mrt = ipmr_get_table(rule->fr_net, rule->table);
191 if (mrt == NULL)
192 return -EAGAIN;
193 res->mrt = mrt;
194 return 0;
195}
196
197static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
198{
199 return 1;
200}
201
202static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
203 FRA_GENERIC_POLICY,
204};
205
206static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
207 struct fib_rule_hdr *frh, struct nlattr **tb)
208{
209 return 0;
210}
211
212static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
213 struct nlattr **tb)
214{
215 return 1;
216}
217
218static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
219 struct fib_rule_hdr *frh)
220{
221 frh->dst_len = 0;
222 frh->src_len = 0;
223 frh->tos = 0;
224 return 0;
225}
226
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200227static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200228 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000229 .rule_size = sizeof(struct ipmr_rule),
230 .addr_size = sizeof(u32),
231 .action = ipmr_rule_action,
232 .match = ipmr_rule_match,
233 .configure = ipmr_rule_configure,
234 .compare = ipmr_rule_compare,
235 .default_pref = fib_default_rule_pref,
236 .fill = ipmr_rule_fill,
237 .nlgroup = RTNLGRP_IPV4_RULE,
238 .policy = ipmr_rule_policy,
239 .owner = THIS_MODULE,
240};
241
242static int __net_init ipmr_rules_init(struct net *net)
243{
244 struct fib_rules_ops *ops;
245 struct mr_table *mrt;
246 int err;
247
248 ops = fib_rules_register(&ipmr_rules_ops_template, net);
249 if (IS_ERR(ops))
250 return PTR_ERR(ops);
251
252 INIT_LIST_HEAD(&net->ipv4.mr_tables);
253
254 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
255 if (mrt == NULL) {
256 err = -ENOMEM;
257 goto err1;
258 }
259
260 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
261 if (err < 0)
262 goto err2;
263
264 net->ipv4.mr_rules_ops = ops;
265 return 0;
266
267err2:
268 kfree(mrt);
269err1:
270 fib_rules_unregister(ops);
271 return err;
272}
273
274static void __net_exit ipmr_rules_exit(struct net *net)
275{
276 struct mr_table *mrt, *next;
277
Eric Dumazet035320d2010-06-06 23:48:40 +0000278 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
279 list_del(&mrt->list);
Francesco Ruggerib9d798a2012-08-24 07:38:35 +0000280 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000281 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000282 fib_rules_unregister(net->ipv4.mr_rules_ops);
283}
284#else
285#define ipmr_for_each_table(mrt, net) \
286 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
287
288static struct mr_table *ipmr_get_table(struct net *net, u32 id)
289{
290 return net->ipv4.mrt;
291}
292
David S. Millerda919812011-03-12 02:04:50 -0500293static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000294 struct mr_table **mrt)
295{
296 *mrt = net->ipv4.mrt;
297 return 0;
298}
299
300static int __net_init ipmr_rules_init(struct net *net)
301{
302 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
303 return net->ipv4.mrt ? 0 : -ENOMEM;
304}
305
306static void __net_exit ipmr_rules_exit(struct net *net)
307{
Francesco Ruggerib9d798a2012-08-24 07:38:35 +0000308 ipmr_free_table(net->ipv4.mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000309}
310#endif
311
312static struct mr_table *ipmr_new_table(struct net *net, u32 id)
313{
314 struct mr_table *mrt;
315 unsigned int i;
316
317 mrt = ipmr_get_table(net, id);
318 if (mrt != NULL)
319 return mrt;
320
321 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
322 if (mrt == NULL)
323 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200324 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000325 mrt->id = id;
326
327 /* Forwarding cache */
328 for (i = 0; i < MFC_LINES; i++)
329 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
330
331 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
332
333 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
334 (unsigned long)mrt);
335
336#ifdef CONFIG_IP_PIMSM
337 mrt->mroute_reg_vif_num = -1;
338#endif
339#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
340 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
341#endif
342 return mrt;
343}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Francesco Ruggerib9d798a2012-08-24 07:38:35 +0000345static void ipmr_free_table(struct mr_table *mrt)
346{
347 del_timer_sync(&mrt->ipmr_expire_timer);
348 mroute_clean_tables(mrt);
349 kfree(mrt);
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
353
Wang Chend6070322008-07-14 20:55:26 -0700354static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
355{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000356 struct net *net = dev_net(dev);
357
Wang Chend6070322008-07-14 20:55:26 -0700358 dev_close(dev);
359
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000360 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700361 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800362 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700363 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700364 struct ip_tunnel_parm p;
365
366 memset(&p, 0, sizeof(p));
367 p.iph.daddr = v->vifc_rmt_addr.s_addr;
368 p.iph.saddr = v->vifc_lcl_addr.s_addr;
369 p.iph.version = 4;
370 p.iph.ihl = 5;
371 p.iph.protocol = IPPROTO_IPIP;
372 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
373 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
374
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800375 if (ops->ndo_do_ioctl) {
376 mm_segment_t oldfs = get_fs();
377
378 set_fs(KERNEL_DS);
379 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
380 set_fs(oldfs);
381 }
Wang Chend6070322008-07-14 20:55:26 -0700382 }
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000386struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct net_device *dev;
389
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000390 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800393 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int err;
395 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct ip_tunnel_parm p;
397 struct in_device *in_dev;
398
399 memset(&p, 0, sizeof(p));
400 p.iph.daddr = v->vifc_rmt_addr.s_addr;
401 p.iph.saddr = v->vifc_lcl_addr.s_addr;
402 p.iph.version = 4;
403 p.iph.ihl = 5;
404 p.iph.protocol = IPPROTO_IPIP;
405 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800406 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800408 if (ops->ndo_do_ioctl) {
409 mm_segment_t oldfs = get_fs();
410
411 set_fs(KERNEL_DS);
412 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
413 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000414 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800415 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 dev = NULL;
418
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000419 if (err == 0 &&
420 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dev->flags |= IFF_MULTICAST;
422
Herbert Xue5ed6392005-10-03 14:35:55 -0700423 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700424 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700426
427 ipv4_devconf_setall(in_dev);
428 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (dev_open(dev))
431 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700432 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 }
435 return dev;
436
437failure:
438 /* allow the register to be completed before unregistering. */
439 rtnl_unlock();
440 rtnl_lock();
441
442 unregister_netdevice(dev);
443 return NULL;
444}
445
446#ifdef CONFIG_IP_PIMSM
447
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000448static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000450 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000451 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500452 struct flowi4 fl4 = {
453 .flowi4_oif = dev->ifindex,
454 .flowi4_iif = skb->skb_iif,
455 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000456 };
457 int err;
458
David S. Millerda919812011-03-12 02:04:50 -0500459 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000460 if (err < 0) {
461 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000462 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000463 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700466 dev->stats.tx_bytes += skb->len;
467 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000468 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 read_unlock(&mrt_lock);
470 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000471 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Stephen Hemminger007c3832008-11-20 20:28:35 -0800474static const struct net_device_ops reg_vif_netdev_ops = {
475 .ndo_start_xmit = reg_vif_xmit,
476};
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static void reg_vif_setup(struct net_device *dev)
479{
480 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800481 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800483 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700485 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000488static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct net_device *dev;
491 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000492 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000494 if (mrt->id == RT_TABLE_DEFAULT)
495 sprintf(name, "pimreg");
496 else
497 sprintf(name, "pimreg%u", mrt->id);
498
499 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (dev == NULL)
502 return NULL;
503
Tom Goff403dbb92009-06-14 03:16:13 -0700504 dev_net_set(dev, net);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (register_netdevice(dev)) {
507 free_netdev(dev);
508 return NULL;
509 }
510 dev->iflink = 0;
511
Herbert Xu71e27da2007-06-04 23:36:06 -0700512 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000513 in_dev = __in_dev_get_rcu(dev);
514 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700515 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Herbert Xu71e27da2007-06-04 23:36:06 -0700519 ipv4_devconf_setall(in_dev);
520 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
521 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 if (dev_open(dev))
524 goto failure;
525
Wang Chen7dc00c82008-07-14 20:56:34 -0700526 dev_hold(dev);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return dev;
529
530failure:
531 /* allow the register to be completed before unregistering. */
532 rtnl_unlock();
533 rtnl_lock();
534
535 unregister_netdevice(dev);
536 return NULL;
537}
538#endif
539
540/*
541 * Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700542 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900544
Patrick McHardy0c122952010-04-13 05:03:22 +0000545static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000546 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct vif_device *v;
549 struct net_device *dev;
550 struct in_device *in_dev;
551
Patrick McHardy0c122952010-04-13 05:03:22 +0000552 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -EADDRNOTAVAIL;
554
Patrick McHardy0c122952010-04-13 05:03:22 +0000555 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 write_lock_bh(&mrt_lock);
558 dev = v->dev;
559 v->dev = NULL;
560
561 if (!dev) {
562 write_unlock_bh(&mrt_lock);
563 return -EADDRNOTAVAIL;
564 }
565
566#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000567 if (vifi == mrt->mroute_reg_vif_num)
568 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#endif
570
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000571 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000573
574 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000575 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
577 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000578 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
581 write_unlock_bh(&mrt_lock);
582
583 dev_set_allmulti(dev, -1);
584
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000585 in_dev = __in_dev_get_rtnl(dev);
586 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700587 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ip_rt_multicast_event(in_dev);
589 }
590
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000591 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000592 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 dev_put(dev);
595 return 0;
596}
597
Eric Dumazeta8c94862010-10-01 16:15:08 +0000598static void ipmr_cache_free_rcu(struct rcu_head *head)
599{
600 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
601
602 kmem_cache_free(mrt_cachep, c);
603}
604
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000605static inline void ipmr_cache_free(struct mfc_cache *c)
606{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000607 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000611 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
613
Patrick McHardy0c122952010-04-13 05:03:22 +0000614static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200616 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700618 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Patrick McHardy0c122952010-04-13 05:03:22 +0000620 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Jianjun Kongc354e122008-11-03 00:28:02 -0800622 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700623 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
625 nlh->nlmsg_type = NLMSG_ERROR;
626 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
627 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700628 e = NLMSG_DATA(nlh);
629 e->error = -ETIMEDOUT;
630 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700631
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000632 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000633 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000638 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641
Patrick McHardye258beb2010-04-13 05:03:19 +0000642/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Patrick McHardye258beb2010-04-13 05:03:19 +0000644static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Patrick McHardy0c122952010-04-13 05:03:22 +0000646 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned long now;
648 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000649 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000652 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return;
654 }
655
Patrick McHardy0c122952010-04-13 05:03:22 +0000656 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto out;
658
659 now = jiffies;
660 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Patrick McHardy0c122952010-04-13 05:03:22 +0000662 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (time_after(c->mfc_un.unres.expires, now)) {
664 unsigned long interval = c->mfc_un.unres.expires - now;
665 if (interval < expires)
666 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 continue;
668 }
669
Patrick McHardy862465f2010-04-13 05:03:21 +0000670 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000671 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Patrick McHardy0c122952010-04-13 05:03:22 +0000674 if (!list_empty(&mrt->mfc_unres_queue))
675 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677out:
678 spin_unlock(&mfc_unres_lock);
679}
680
681/* Fill oifs list. It is called under write locked mrt_lock. */
682
Patrick McHardy0c122952010-04-13 05:03:22 +0000683static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000684 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 int vifi;
687
688 cache->mfc_un.res.minvif = MAXVIFS;
689 cache->mfc_un.res.maxvif = 0;
690 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
691
Patrick McHardy0c122952010-04-13 05:03:22 +0000692 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
693 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000694 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
696 if (cache->mfc_un.res.minvif > vifi)
697 cache->mfc_un.res.minvif = vifi;
698 if (cache->mfc_un.res.maxvif <= vifi)
699 cache->mfc_un.res.maxvif = vifi + 1;
700 }
701 }
702}
703
Patrick McHardy0c122952010-04-13 05:03:22 +0000704static int vif_add(struct net *net, struct mr_table *mrt,
705 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000708 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct net_device *dev;
710 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700711 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000714 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EADDRINUSE;
716
717 switch (vifc->vifc_flags) {
718#ifdef CONFIG_IP_PIMSM
719 case VIFF_REGISTER:
720 /*
721 * Special Purpose VIF in PIM
722 * All the packets will be sent to the daemon
723 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000724 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000726 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!dev)
728 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700729 err = dev_set_allmulti(dev, 1);
730 if (err) {
731 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700732 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700733 return err;
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
736#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900737 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000738 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!dev)
740 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700741 err = dev_set_allmulti(dev, 1);
742 if (err) {
743 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700744 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700745 return err;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000748
749 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000751 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
752 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000753 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000754 dev_put(dev);
755 return -EADDRNOTAVAIL;
756 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000757 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000758 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (!dev)
761 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700762 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700763 if (err) {
764 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700765 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 break;
768 default:
769 return -EINVAL;
770 }
771
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000772 in_dev = __in_dev_get_rtnl(dev);
773 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000774 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000776 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700777 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 ip_rt_multicast_event(in_dev);
779
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000780 /* Fill in the VIF structures */
781
Jianjun Kongc354e122008-11-03 00:28:02 -0800782 v->rate_limit = vifc->vifc_rate_limit;
783 v->local = vifc->vifc_lcl_addr.s_addr;
784 v->remote = vifc->vifc_rmt_addr.s_addr;
785 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (!mrtsock)
787 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800788 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 v->bytes_in = 0;
790 v->bytes_out = 0;
791 v->pkt_in = 0;
792 v->pkt_out = 0;
793 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000794 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 v->link = dev->iflink;
796
797 /* And finish update writing critical data */
798 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800799 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000801 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000802 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000804 if (vifi+1 > mrt->maxvif)
805 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 write_unlock_bh(&mrt_lock);
807 return 0;
808}
809
Eric Dumazeta8c94862010-10-01 16:15:08 +0000810/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000811static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000812 __be32 origin,
813 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Jianjun Kongc354e122008-11-03 00:28:02 -0800815 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct mfc_cache *c;
817
Eric Dumazeta8c94862010-10-01 16:15:08 +0000818 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000819 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
820 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000822 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825/*
826 * Allocate a multicast cache entry
827 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000828static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Jianjun Kongc354e122008-11-03 00:28:02 -0800830 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000831
832 if (c)
833 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return c;
835}
836
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000837static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Jianjun Kongc354e122008-11-03 00:28:02 -0800839 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000840
841 if (c) {
842 skb_queue_head_init(&c->mfc_un.unres.unresolved);
843 c->mfc_un.unres.expires = jiffies + 10*HZ;
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return c;
846}
847
848/*
849 * A cache entry has gone into a resolved state from queued
850 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900851
Patrick McHardy0c122952010-04-13 05:03:22 +0000852static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
853 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700856 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000858 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Jianjun Kongc354e122008-11-03 00:28:02 -0800860 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700861 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
863
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200864 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000865 nlh->nlmsg_len = skb_tail_pointer(skb) -
866 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 } else {
868 nlh->nlmsg_type = NLMSG_ERROR;
869 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
870 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700871 e = NLMSG_DATA(nlh);
872 e->error = -EMSGSIZE;
873 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Thomas Graf2942e902006-08-15 00:30:25 -0700875
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000876 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000877 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000878 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881}
882
883/*
884 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
885 * expects the following bizarre scheme.
886 *
887 * Called under mrt_lock.
888 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900889
Patrick McHardy0c122952010-04-13 05:03:22 +0000890static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000891 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300894 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 struct igmphdr *igmp;
896 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000897 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int ret;
899
900#ifdef CONFIG_IP_PIMSM
901 if (assert == IGMPMSG_WHOLEPKT)
902 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
903 else
904#endif
905 skb = alloc_skb(128, GFP_ATOMIC);
906
Stephen Hemminger132adf52007-03-08 20:44:43 -0800907 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return -ENOBUFS;
909
910#ifdef CONFIG_IP_PIMSM
911 if (assert == IGMPMSG_WHOLEPKT) {
912 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000913 * Duplicate old header, fix ihl, length etc.
914 * And all this only to mangle msg->im_msgtype and
915 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300917 skb_push(skb, sizeof(struct iphdr));
918 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300919 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300920 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700921 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 msg->im_msgtype = IGMPMSG_WHOLEPKT;
923 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000924 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700925 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
926 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
927 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900928 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900930 {
931
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000932 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700934 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300935 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300936 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000937 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700938 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000940 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000942 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000944 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 igmp->type =
946 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000947 igmp->code = 0;
948 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700949 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Eric Dumazet4c968702010-10-01 16:15:01 +0000952 rcu_read_lock();
953 mroute_sk = rcu_dereference(mrt->mroute_sk);
954 if (mroute_sk == NULL) {
955 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 kfree_skb(skb);
957 return -EINVAL;
958 }
959
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000960 /* Deliver to mrouted */
961
Eric Dumazet4c968702010-10-01 16:15:01 +0000962 ret = sock_queue_rcv_skb(mroute_sk, skb);
963 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +0000964 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +0000966 pr_warn("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 kfree_skb(skb);
968 }
969
970 return ret;
971}
972
973/*
974 * Queue a packet for resolution. It gets locked cache entry!
975 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977static int
Patrick McHardy0c122952010-04-13 05:03:22 +0000978ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Patrick McHardy862465f2010-04-13 05:03:21 +0000980 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 int err;
982 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700983 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000986 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000987 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000988 c->mfc_origin == iph->saddr) {
989 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
Patrick McHardy862465f2010-04-13 05:03:21 +0000994 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000995 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Patrick McHardy0c122952010-04-13 05:03:22 +0000997 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000998 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 spin_unlock_bh(&mfc_unres_lock);
1000
1001 kfree_skb(skb);
1002 return -ENOBUFS;
1003 }
1004
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001005 /* Fill in the new cache entry */
1006
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001007 c->mfc_parent = -1;
1008 c->mfc_origin = iph->saddr;
1009 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001011 /* Reflect first query at mrouted. */
1012
Patrick McHardy0c122952010-04-13 05:03:22 +00001013 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001014 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001015 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 out - Brad Parker
1017 */
1018 spin_unlock_bh(&mfc_unres_lock);
1019
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001020 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 kfree_skb(skb);
1022 return err;
1023 }
1024
Patrick McHardy0c122952010-04-13 05:03:22 +00001025 atomic_inc(&mrt->cache_resolve_queue_len);
1026 list_add(&c->list, &mrt->mfc_unres_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
David S. Miller278554b2010-05-12 00:05:35 -07001028 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1029 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001032 /* See if we can append the packet */
1033
1034 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 kfree_skb(skb);
1036 err = -ENOBUFS;
1037 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001038 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 err = 0;
1040 }
1041
1042 spin_unlock_bh(&mfc_unres_lock);
1043 return err;
1044}
1045
1046/*
1047 * MFC cache manipulation by user space mroute daemon
1048 */
1049
Patrick McHardy0c122952010-04-13 05:03:22 +00001050static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001053 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Jianjun Kongc354e122008-11-03 00:28:02 -08001055 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Patrick McHardy0c122952010-04-13 05:03:22 +00001057 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1059 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001060 list_del_rcu(&c->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001062 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064 }
1065 }
1066 return -ENOENT;
1067}
1068
Patrick McHardy0c122952010-04-13 05:03:22 +00001069static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1070 struct mfcctl *mfc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Patrick McHardy862465f2010-04-13 05:03:21 +00001072 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001074 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Patrick McHardya50436f2010-03-17 06:04:14 +00001076 if (mfc->mfcc_parent >= MAXVIFS)
1077 return -ENFILE;
1078
Jianjun Kongc354e122008-11-03 00:28:02 -08001079 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Patrick McHardy0c122952010-04-13 05:03:22 +00001081 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001083 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1084 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
Patrick McHardy862465f2010-04-13 05:03:21 +00001089 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 write_lock_bh(&mrt_lock);
1091 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001092 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!mrtsock)
1094 c->mfc_flags |= MFC_STATIC;
1095 write_unlock_bh(&mrt_lock);
1096 return 0;
1097 }
1098
Joe Perchesf97c1e02007-12-16 13:45:43 -08001099 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -EINVAL;
1101
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001102 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001103 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -ENOMEM;
1105
Jianjun Kongc354e122008-11-03 00:28:02 -08001106 c->mfc_origin = mfc->mfcc_origin.s_addr;
1107 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1108 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001109 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (!mrtsock)
1111 c->mfc_flags |= MFC_STATIC;
1112
Eric Dumazeta8c94862010-10-01 16:15:08 +00001113 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 /*
1116 * Check to see if we resolved a queued list. If so we
1117 * need to send on the frames and tidy up.
1118 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001119 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001121 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001122 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001124 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001125 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001126 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 break;
1128 }
1129 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001130 if (list_empty(&mrt->mfc_unres_queue))
1131 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 spin_unlock_bh(&mfc_unres_lock);
1133
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001134 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001135 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001136 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 return 0;
1139}
1140
1141/*
1142 * Close the multicast socket, and clear the vif tables etc
1143 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001144
Patrick McHardy0c122952010-04-13 05:03:22 +00001145static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001148 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001149 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001150
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001151 /* Shut down all active vif entries */
1152
Patrick McHardy0c122952010-04-13 05:03:22 +00001153 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001154 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001155 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001157 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001159 /* Wipe the cache */
1160
Patrick McHardy862465f2010-04-13 05:03:21 +00001161 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001162 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001163 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001165 list_del_rcu(&c->list);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001166 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 }
1169
Patrick McHardy0c122952010-04-13 05:03:22 +00001170 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001172 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001173 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001174 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 spin_unlock_bh(&mfc_unres_lock);
1177 }
1178}
1179
Eric Dumazet4c968702010-10-01 16:15:01 +00001180/* called from ip_ra_control(), before an RCU grace period,
1181 * we dont need to call synchronize_rcu() here
1182 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183static void mrtsock_destruct(struct sock *sk)
1184{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001185 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001186 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001189 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001190 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001191 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001192 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001193 mroute_clean_tables(mrt);
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196 rtnl_unlock();
1197}
1198
1199/*
1200 * Socket options and virtual interface manipulation. The whole
1201 * virtual interface system is a complete heap, but unfortunately
1202 * that's how BSD mrouted happens to think. Maybe one day with a proper
1203 * MOSPF/PIM router set up we can clean this up.
1204 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001205
David S. Millerb7058842009-09-30 16:12:20 -07001206int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 int ret;
1209 struct vifctl vif;
1210 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001211 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001212 struct mr_table *mrt;
1213
1214 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1215 if (mrt == NULL)
1216 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001217
Stephen Hemminger132adf52007-03-08 20:44:43 -08001218 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001219 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric Dumazet4c968702010-10-01 16:15:01 +00001220 !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return -EACCES;
1222 }
1223
Stephen Hemminger132adf52007-03-08 20:44:43 -08001224 switch (optname) {
1225 case MRT_INIT:
1226 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001227 inet_sk(sk)->inet_num != IPPROTO_IGMP)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001228 return -EOPNOTSUPP;
Jianjun Kongc354e122008-11-03 00:28:02 -08001229 if (optlen != sizeof(int))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001230 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Stephen Hemminger132adf52007-03-08 20:44:43 -08001232 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001233 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001235 return -EADDRINUSE;
1236 }
1237
1238 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1239 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001240 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001241 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001242 }
1243 rtnl_unlock();
1244 return ret;
1245 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001246 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001247 return -EACCES;
1248 return ip_ra_control(sk, 0, NULL);
1249 case MRT_ADD_VIF:
1250 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001251 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001252 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001253 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001254 return -EFAULT;
1255 if (vif.vifc_vifi >= MAXVIFS)
1256 return -ENFILE;
1257 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001258 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001259 ret = vif_add(net, mrt, &vif,
1260 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001261 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001262 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001263 }
1264 rtnl_unlock();
1265 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
1268 * Manipulate the forwarding caches. These live
1269 * in a sort of kernel/user symbiosis.
1270 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001271 case MRT_ADD_MFC:
1272 case MRT_DEL_MFC:
Jianjun Kongc354e122008-11-03 00:28:02 -08001273 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001274 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001275 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001276 return -EFAULT;
1277 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001278 if (optname == MRT_DEL_MFC)
Patrick McHardy0c122952010-04-13 05:03:22 +00001279 ret = ipmr_mfc_delete(mrt, &mfc);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001280 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001281 ret = ipmr_mfc_add(net, mrt, &mfc,
1282 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001283 rtnl_unlock();
1284 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /*
1286 * Control PIM assert.
1287 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001288 case MRT_ASSERT:
1289 {
1290 int v;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001291 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001292 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001293 mrt->mroute_do_assert = (v) ? 1 : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001294 return 0;
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 case MRT_PIM:
1298 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001299 int v;
1300
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001301 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001302 return -EFAULT;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001303 v = (v) ? 1 : 0;
1304
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 rtnl_lock();
1306 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001307 if (v != mrt->mroute_do_pim) {
1308 mrt->mroute_do_pim = v;
1309 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001311 rtnl_unlock();
1312 return ret;
1313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001315#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1316 case MRT_TABLE:
1317 {
1318 u32 v;
1319
1320 if (optlen != sizeof(u32))
1321 return -EINVAL;
1322 if (get_user(v, (u32 __user *)optval))
1323 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001324
1325 rtnl_lock();
1326 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001327 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1328 ret = -EBUSY;
1329 } else {
1330 if (!ipmr_new_table(net, v))
1331 ret = -ENOMEM;
1332 raw_sk(sk)->ipmr_table = v;
1333 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001334 rtnl_unlock();
1335 return ret;
1336 }
1337#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001338 /*
1339 * Spurious command, or MRT_VERSION which you cannot
1340 * set.
1341 */
1342 default:
1343 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345}
1346
1347/*
1348 * Getsock opt support for the multicast routing system.
1349 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001350
Jianjun Kongc354e122008-11-03 00:28:02 -08001351int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 int olr;
1354 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001355 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001356 struct mr_table *mrt;
1357
1358 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1359 if (mrt == NULL)
1360 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Jianjun Kongc354e122008-11-03 00:28:02 -08001362 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001364 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001366 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -ENOPROTOOPT;
1368
1369 if (get_user(olr, optlen))
1370 return -EFAULT;
1371
1372 olr = min_t(unsigned int, olr, sizeof(int));
1373 if (olr < 0)
1374 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001375
Jianjun Kongc354e122008-11-03 00:28:02 -08001376 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001378 if (optname == MRT_VERSION)
1379 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001381 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001382 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383#endif
1384 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001385 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001386 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return -EFAULT;
1388 return 0;
1389}
1390
1391/*
1392 * The IP multicast ioctl support routines.
1393 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1396{
1397 struct sioc_sg_req sr;
1398 struct sioc_vif_req vr;
1399 struct vif_device *vif;
1400 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001401 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001402 struct mr_table *mrt;
1403
1404 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1405 if (mrt == NULL)
1406 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001407
Stephen Hemminger132adf52007-03-08 20:44:43 -08001408 switch (cmd) {
1409 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001410 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001411 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001412 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001413 return -EINVAL;
1414 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001415 vif = &mrt->vif_table[vr.vifi];
1416 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001417 vr.icount = vif->pkt_in;
1418 vr.ocount = vif->pkt_out;
1419 vr.ibytes = vif->bytes_in;
1420 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001422
Jianjun Kongc354e122008-11-03 00:28:02 -08001423 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001425 return 0;
1426 }
1427 read_unlock(&mrt_lock);
1428 return -EADDRNOTAVAIL;
1429 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001430 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001431 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Eric Dumazeta8c94862010-10-01 16:15:08 +00001433 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001434 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001435 if (c) {
1436 sr.pktcnt = c->mfc_un.res.pkt;
1437 sr.bytecnt = c->mfc_un.res.bytes;
1438 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001439 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001440
Jianjun Kongc354e122008-11-03 00:28:02 -08001441 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001442 return -EFAULT;
1443 return 0;
1444 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001445 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001446 return -EADDRNOTAVAIL;
1447 default:
1448 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450}
1451
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001452#ifdef CONFIG_COMPAT
1453struct compat_sioc_sg_req {
1454 struct in_addr src;
1455 struct in_addr grp;
1456 compat_ulong_t pktcnt;
1457 compat_ulong_t bytecnt;
1458 compat_ulong_t wrong_if;
1459};
1460
David S. Millerca6b8bb2011-02-03 17:24:28 -08001461struct compat_sioc_vif_req {
1462 vifi_t vifi; /* Which iface */
1463 compat_ulong_t icount;
1464 compat_ulong_t ocount;
1465 compat_ulong_t ibytes;
1466 compat_ulong_t obytes;
1467};
1468
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001469int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1470{
David S. Miller0033d5a2011-02-03 17:21:31 -08001471 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb2011-02-03 17:24:28 -08001472 struct compat_sioc_vif_req vr;
1473 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001474 struct mfc_cache *c;
1475 struct net *net = sock_net(sk);
1476 struct mr_table *mrt;
1477
1478 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1479 if (mrt == NULL)
1480 return -ENOENT;
1481
1482 switch (cmd) {
David S. Millerca6b8bb2011-02-03 17:24:28 -08001483 case SIOCGETVIFCNT:
1484 if (copy_from_user(&vr, arg, sizeof(vr)))
1485 return -EFAULT;
1486 if (vr.vifi >= mrt->maxvif)
1487 return -EINVAL;
1488 read_lock(&mrt_lock);
1489 vif = &mrt->vif_table[vr.vifi];
1490 if (VIF_EXISTS(mrt, vr.vifi)) {
1491 vr.icount = vif->pkt_in;
1492 vr.ocount = vif->pkt_out;
1493 vr.ibytes = vif->bytes_in;
1494 vr.obytes = vif->bytes_out;
1495 read_unlock(&mrt_lock);
1496
1497 if (copy_to_user(arg, &vr, sizeof(vr)))
1498 return -EFAULT;
1499 return 0;
1500 }
1501 read_unlock(&mrt_lock);
1502 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001503 case SIOCGETSGCNT:
1504 if (copy_from_user(&sr, arg, sizeof(sr)))
1505 return -EFAULT;
1506
1507 rcu_read_lock();
1508 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1509 if (c) {
1510 sr.pktcnt = c->mfc_un.res.pkt;
1511 sr.bytecnt = c->mfc_un.res.bytes;
1512 sr.wrong_if = c->mfc_un.res.wrong_if;
1513 rcu_read_unlock();
1514
1515 if (copy_to_user(arg, &sr, sizeof(sr)))
1516 return -EFAULT;
1517 return 0;
1518 }
1519 rcu_read_unlock();
1520 return -EADDRNOTAVAIL;
1521 default:
1522 return -ENOIOCTLCMD;
1523 }
1524}
1525#endif
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1529{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001530 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001531 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001532 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct vif_device *v;
1534 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (event != NETDEV_UNREGISTER)
1537 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001538
1539 ipmr_for_each_table(mrt, net) {
1540 v = &mrt->vif_table[0];
1541 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1542 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001543 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 return NOTIFY_DONE;
1547}
1548
1549
Jianjun Kongc354e122008-11-03 00:28:02 -08001550static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 .notifier_call = ipmr_device_event,
1552};
1553
1554/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001555 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 * This avoids tunnel drivers and other mess and gives us the speed so
1557 * important for multicast video.
1558 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001559
Al Viro114c7842006-09-27 18:39:29 -07001560static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001562 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001563 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001564
1565 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001566 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001567 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001568 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001570 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001571 iph->tos = old_iph->tos;
1572 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 iph->frag_off = 0;
1574 iph->daddr = daddr;
1575 iph->saddr = saddr;
1576 iph->protocol = IPPROTO_IPIP;
1577 iph->ihl = 5;
1578 iph->tot_len = htons(skb->len);
Ansis Attekaf72299d2013-09-18 15:29:53 -07001579 ip_select_ident(skb, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 ip_send_check(iph);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1583 nf_reset(skb);
1584}
1585
1586static inline int ipmr_forward_finish(struct sk_buff *skb)
1587{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001588 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Eric Dumazetadf30902009-06-02 05:19:30 +00001590 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 if (unlikely(opt->optlen))
1593 ip_forward_options(skb);
1594
1595 return dst_output(skb);
1596}
1597
1598/*
1599 * Processing handlers for ipmr_forward
1600 */
1601
Patrick McHardy0c122952010-04-13 05:03:22 +00001602static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1603 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001605 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001606 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 struct net_device *dev;
1608 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001609 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 int encap = 0;
1611
1612 if (vif->dev == NULL)
1613 goto out_free;
1614
1615#ifdef CONFIG_IP_PIMSM
1616 if (vif->flags & VIFF_REGISTER) {
1617 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001618 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001619 vif->dev->stats.tx_bytes += skb->len;
1620 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001621 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001622 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624#endif
1625
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001626 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001627 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001628 vif->remote, vif->local,
1629 0, 0,
1630 IPPROTO_IPIP,
1631 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001632 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 goto out_free;
1634 encap = sizeof(struct iphdr);
1635 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001636 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001637 0, 0,
1638 IPPROTO_IPIP,
1639 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001640 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 goto out_free;
1642 }
1643
Changli Gaod8d1f302010-06-10 23:31:35 -07001644 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Changli Gaod8d1f302010-06-10 23:31:35 -07001646 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001648 * allow to send ICMP, so that packets will disappear
1649 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
1651
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001652 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 ip_rt_put(rt);
1654 goto out_free;
1655 }
1656
Changli Gaod8d1f302010-06-10 23:31:35 -07001657 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001660 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto out_free;
1662 }
1663
1664 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001665 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Eric Dumazetadf30902009-06-02 05:19:30 +00001667 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001668 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001669 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001672 * What do we do with netfilter? -- RR
1673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (vif->flags & VIFF_TUNNEL) {
1675 ip_encap(skb, vif->local, vif->remote);
1676 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001677 vif->dev->stats.tx_packets++;
1678 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
1681 IPCB(skb)->flags |= IPSKB_FORWARDED;
1682
1683 /*
1684 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1685 * not only before forwarding, but after forwarding on all output
1686 * interfaces. It is clear, if mrouter runs a multicasting
1687 * program, it should receive packets not depending to what interface
1688 * program is joined.
1689 * If we will not make it, the program will have to join on all
1690 * interfaces. On the other hand, multihoming host (or router, but
1691 * not mrouter) cannot join to more than one interface - it will
1692 * result in receiving multiple packets.
1693 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001694 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 ipmr_forward_finish);
1696 return;
1697
1698out_free:
1699 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Patrick McHardy0c122952010-04-13 05:03:22 +00001702static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001705
1706 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1707 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709 }
1710 return ct;
1711}
1712
1713/* "local" means that we should preserve one skb (for local delivery) */
1714
Patrick McHardy0c122952010-04-13 05:03:22 +00001715static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1716 struct sk_buff *skb, struct mfc_cache *cache,
1717 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 int psend = -1;
1720 int vif, ct;
1721
1722 vif = cache->mfc_parent;
1723 cache->mfc_un.res.pkt++;
1724 cache->mfc_un.res.bytes += skb->len;
1725
1726 /*
1727 * Wrong interface: drop packet and (maybe) send PIM assert.
1728 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001729 if (mrt->vif_table[vif].dev != skb->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 int true_vifi;
1731
David S. Millerc7537962010-11-11 17:07:48 -08001732 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001734 * Very complicated situation...
1735 *
1736 * The best workaround until routing daemons will be
1737 * fixed is not to redistribute packet, if it was
1738 * send through wrong interface. It means, that
1739 * multicast applications WILL NOT work for
1740 * (S,G), which have default multicast route pointing
1741 * to wrong oif. In any case, it is not a good
1742 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
1744 goto dont_forward;
1745 }
1746
1747 cache->mfc_un.res.wrong_if++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001748 true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Patrick McHardy0c122952010-04-13 05:03:22 +00001750 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001752 * so that we cannot check that packet arrived on an oif.
1753 * It is bad, but otherwise we would need to move pretty
1754 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001756 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001757 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001758 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1760 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001761 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763 goto dont_forward;
1764 }
1765
Patrick McHardy0c122952010-04-13 05:03:22 +00001766 mrt->vif_table[vif].pkt_in++;
1767 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 /*
1770 * Forward the frame
1771 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001772 for (ct = cache->mfc_un.res.maxvif - 1;
1773 ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001774 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (psend != -1) {
1776 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001779 ipmr_queue_xmit(net, mrt, skb2, cache,
1780 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001782 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 }
1785 if (psend != -1) {
1786 if (local) {
1787 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001790 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001792 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return 0;
1794 }
1795 }
1796
1797dont_forward:
1798 if (!local)
1799 kfree_skb(skb);
1800 return 0;
1801}
1802
David S. Miller417da662011-05-03 19:42:43 -07001803static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001804{
David S. Miller417da662011-05-03 19:42:43 -07001805 struct rtable *rt = skb_rtable(skb);
1806 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001807 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001808 .daddr = iph->daddr,
1809 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001810 .flowi4_tos = RT_TOS(iph->tos),
David S. Millerda919812011-03-12 02:04:50 -05001811 .flowi4_oif = rt->rt_oif,
1812 .flowi4_iif = rt->rt_iif,
1813 .flowi4_mark = rt->rt_mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001814 };
1815 struct mr_table *mrt;
1816 int err;
1817
David S. Millerda919812011-03-12 02:04:50 -05001818 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001819 if (err)
1820 return ERR_PTR(err);
1821 return mrt;
1822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824/*
1825 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001826 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 */
1828
1829int ip_mr_input(struct sk_buff *skb)
1830{
1831 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001832 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001833 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001834 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001837 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001839 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto dont_forward;
1841
David S. Miller417da662011-05-03 19:42:43 -07001842 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001843 if (IS_ERR(mrt)) {
1844 kfree_skb(skb);
1845 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001848 if (IPCB(skb)->opt.router_alert) {
1849 if (ip_call_ra_chain(skb))
1850 return 0;
1851 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1852 /* IGMPv1 (and broken IGMPv2 implementations sort of
1853 * Cisco IOS <= 11.2(8)) do not put router alert
1854 * option to IGMP packets destined to routable
1855 * groups. It is very bad, because it means
1856 * that we can forward NO IGMP messages.
1857 */
1858 struct sock *mroute_sk;
1859
1860 mroute_sk = rcu_dereference(mrt->mroute_sk);
1861 if (mroute_sk) {
1862 nf_reset(skb);
1863 raw_rcv(mroute_sk, skb);
1864 return 0;
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867 }
1868
Eric Dumazeta8c94862010-10-01 16:15:08 +00001869 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001870 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 /*
1873 * No usable cache entry
1874 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001875 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 int vif;
1877
1878 if (local) {
1879 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1880 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001881 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 skb = skb2;
1884 }
1885
Eric Dumazeta8c94862010-10-01 16:15:08 +00001886 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001887 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001889 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 read_unlock(&mrt_lock);
1891
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001892 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 read_unlock(&mrt_lock);
1895 kfree_skb(skb);
1896 return -ENODEV;
1897 }
1898
Eric Dumazeta8c94862010-10-01 16:15:08 +00001899 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001900 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 read_unlock(&mrt_lock);
1902
1903 if (local)
1904 return ip_local_deliver(skb);
1905
1906 return 0;
1907
1908dont_forward:
1909 if (local)
1910 return ip_local_deliver(skb);
1911 kfree_skb(skb);
1912 return 0;
1913}
1914
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001915#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00001916/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001917static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1918 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001920 struct net_device *reg_dev = NULL;
1921 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001923 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001925 * Check that:
1926 * a. packet is really sent to a multicast group
1927 * b. packet is not a NULL-REGISTER
1928 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001930 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001932 ntohs(encap->tot_len) + pimlen > skb->len)
1933 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001936 if (mrt->mroute_reg_vif_num >= 0)
1937 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 read_unlock(&mrt_lock);
1939
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001940 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001941 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001943 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00001944 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001945 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00001947 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07001949
1950 skb_tunnel_rx(skb, reg_dev);
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001953
Eric Dumazet55747a02010-10-01 16:14:55 +00001954 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001955}
1956#endif
1957
1958#ifdef CONFIG_IP_PIMSM_V1
1959/*
1960 * Handle IGMP messages of PIMv1
1961 */
1962
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001963int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001964{
1965 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001966 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001967 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001968
1969 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1970 goto drop;
1971
1972 pim = igmp_hdr(skb);
1973
David S. Miller417da662011-05-03 19:42:43 -07001974 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001975 if (IS_ERR(mrt))
1976 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00001977 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001978 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1979 goto drop;
1980
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001981 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001982drop:
1983 kfree_skb(skb);
1984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return 0;
1986}
1987#endif
1988
1989#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001990static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001993 struct net *net = dev_net(skb->dev);
1994 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001996 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 goto drop;
1998
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001999 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002000 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2001 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002002 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002003 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 goto drop;
2005
David S. Miller417da662011-05-03 19:42:43 -07002006 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002007 if (IS_ERR(mrt))
2008 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002009 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002010drop:
2011 kfree_skb(skb);
2012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return 0;
2014}
2015#endif
2016
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002017static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2018 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 int ct;
2021 struct rtnexthop *nhp;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002022 u8 *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 struct rtattr *mp_head;
2024
Nicolas Dichtel74381892010-03-25 23:45:35 +00002025 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f1602010-05-26 00:38:56 -07002026 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002027 return -ENOENT;
2028
Patrick McHardy0c122952010-04-13 05:03:22 +00002029 if (VIF_EXISTS(mrt, c->mfc_parent))
2030 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Jianjun Kongc354e122008-11-03 00:28:02 -08002032 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002035 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2037 goto rtattr_failure;
Jianjun Kongc354e122008-11-03 00:28:02 -08002038 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 nhp->rtnh_flags = 0;
2040 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002041 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 nhp->rtnh_len = sizeof(*nhp);
2043 }
2044 }
2045 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002046 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 rtm->rtm_type = RTN_MULTICAST;
2048 return 1;
2049
2050rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002051 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return -EMSGSIZE;
2053}
2054
David S. Miller9a1b9492011-05-04 12:18:54 -07002055int ipmr_get_route(struct net *net, struct sk_buff *skb,
2056 __be32 saddr, __be32 daddr,
2057 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002060 struct mr_table *mrt;
2061 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002063 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2064 if (mrt == NULL)
2065 return -ENOENT;
2066
Eric Dumazeta8c94862010-10-01 16:15:08 +00002067 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002068 cache = ipmr_cache_find(mrt, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Jianjun Kongc354e122008-11-03 00:28:02 -08002070 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002071 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002072 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002074 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002077 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return -EAGAIN;
2079 }
2080
2081 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002082 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002083 if (dev)
2084 vif = ipmr_find_vif(mrt, dev);
2085 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002087 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return -ENODEV;
2089 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002090 skb2 = skb_clone(skb, GFP_ATOMIC);
2091 if (!skb2) {
2092 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002093 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002094 return -ENOMEM;
2095 }
2096
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002097 skb_push(skb2, sizeof(struct iphdr));
2098 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002099 iph = ip_hdr(skb2);
2100 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002101 iph->saddr = saddr;
2102 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002103 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002104 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002106 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return err;
2108 }
2109
Eric Dumazeta8c94862010-10-01 16:15:08 +00002110 read_lock(&mrt_lock);
2111 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002113 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002115 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return err;
2117}
2118
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002119static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2120 u32 pid, u32 seq, struct mfc_cache *c)
2121{
2122 struct nlmsghdr *nlh;
2123 struct rtmsg *rtm;
2124
2125 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2126 if (nlh == NULL)
2127 return -EMSGSIZE;
2128
2129 rtm = nlmsg_data(nlh);
2130 rtm->rtm_family = RTNL_FAMILY_IPMR;
2131 rtm->rtm_dst_len = 32;
2132 rtm->rtm_src_len = 32;
2133 rtm->rtm_tos = 0;
2134 rtm->rtm_table = mrt->id;
2135 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2136 rtm->rtm_type = RTN_MULTICAST;
2137 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2138 rtm->rtm_protocol = RTPROT_UNSPEC;
2139 rtm->rtm_flags = 0;
2140
2141 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2142 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2143
2144 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2145 goto nla_put_failure;
2146
2147 return nlmsg_end(skb, nlh);
2148
2149nla_put_failure:
2150 nlmsg_cancel(skb, nlh);
2151 return -EMSGSIZE;
2152}
2153
2154static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2155{
2156 struct net *net = sock_net(skb->sk);
2157 struct mr_table *mrt;
2158 struct mfc_cache *mfc;
2159 unsigned int t = 0, s_t;
2160 unsigned int h = 0, s_h;
2161 unsigned int e = 0, s_e;
2162
2163 s_t = cb->args[0];
2164 s_h = cb->args[1];
2165 s_e = cb->args[2];
2166
Eric Dumazeta8c94862010-10-01 16:15:08 +00002167 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002168 ipmr_for_each_table(mrt, net) {
2169 if (t < s_t)
2170 goto next_table;
2171 if (t > s_t)
2172 s_h = 0;
2173 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002174 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002175 if (e < s_e)
2176 goto next_entry;
2177 if (ipmr_fill_mroute(mrt, skb,
2178 NETLINK_CB(cb->skb).pid,
2179 cb->nlh->nlmsg_seq,
2180 mfc) < 0)
2181 goto done;
2182next_entry:
2183 e++;
2184 }
2185 e = s_e = 0;
2186 }
2187 s_h = 0;
2188next_table:
2189 t++;
2190 }
2191done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002192 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002193
2194 cb->args[2] = e;
2195 cb->args[1] = h;
2196 cb->args[0] = t;
2197
2198 return skb->len;
2199}
2200
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002201#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002203 * The /proc interfaces to multicast routing :
2204 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 */
2206struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002207 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002208 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 int ct;
2210};
2211
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002212static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2213 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 loff_t pos)
2215{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002216 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002217
2218 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2219 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002221 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002222 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224 return NULL;
2225}
2226
2227static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002228 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002230 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002231 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002232 struct mr_table *mrt;
2233
2234 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2235 if (mrt == NULL)
2236 return ERR_PTR(-ENOENT);
2237
2238 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002241 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 : SEQ_START_TOKEN;
2243}
2244
2245static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2246{
2247 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002248 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002249 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 ++*pos;
2252 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002253 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002254
Patrick McHardy0c122952010-04-13 05:03:22 +00002255 while (++iter->ct < mrt->maxvif) {
2256 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002258 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260 return NULL;
2261}
2262
2263static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002264 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 read_unlock(&mrt_lock);
2267}
2268
2269static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2270{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002271 struct ipmr_vif_iter *iter = seq->private;
2272 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002275 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2277 } else {
2278 const struct vif_device *vif = v;
2279 const char *name = vif->dev ? vif->dev->name : "none";
2280
2281 seq_printf(seq,
2282 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002283 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002284 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 vif->bytes_out, vif->pkt_out,
2286 vif->flags, vif->local, vif->remote);
2287 }
2288 return 0;
2289}
2290
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002291static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 .start = ipmr_vif_seq_start,
2293 .next = ipmr_vif_seq_next,
2294 .stop = ipmr_vif_seq_stop,
2295 .show = ipmr_vif_seq_show,
2296};
2297
2298static int ipmr_vif_open(struct inode *inode, struct file *file)
2299{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002300 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2301 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
Arjan van de Ven9a321442007-02-12 00:55:35 -08002304static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 .owner = THIS_MODULE,
2306 .open = ipmr_vif_open,
2307 .read = seq_read,
2308 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002309 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310};
2311
2312struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002313 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002314 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002315 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 int ct;
2317};
2318
2319
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002320static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2321 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002323 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 struct mfc_cache *mfc;
2325
Eric Dumazeta8c94862010-10-01 16:15:08 +00002326 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002327 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002328 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002329 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002330 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002332 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002333 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002336 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002337 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002338 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return mfc;
2340 spin_unlock_bh(&mfc_unres_lock);
2341
2342 it->cache = NULL;
2343 return NULL;
2344}
2345
2346
2347static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2348{
2349 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002350 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002351 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002352
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002353 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2354 if (mrt == NULL)
2355 return ERR_PTR(-ENOENT);
2356
2357 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 it->cache = NULL;
2359 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002360 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 : SEQ_START_TOKEN;
2362}
2363
2364static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2365{
2366 struct mfc_cache *mfc = v;
2367 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002368 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002369 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 ++*pos;
2372
2373 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002374 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Patrick McHardy862465f2010-04-13 05:03:21 +00002376 if (mfc->list.next != it->cache)
2377 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002378
Patrick McHardy0c122952010-04-13 05:03:22 +00002379 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 goto end_of_list;
2381
Patrick McHardy0c122952010-04-13 05:03:22 +00002382 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002385 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002386 if (list_empty(it->cache))
2387 continue;
2388 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390
2391 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002392 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002393 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002397 if (!list_empty(it->cache))
2398 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002400end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 spin_unlock_bh(&mfc_unres_lock);
2402 it->cache = NULL;
2403
2404 return NULL;
2405}
2406
2407static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2408{
2409 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002410 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Patrick McHardy0c122952010-04-13 05:03:22 +00002412 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002414 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002415 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
2418static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2419{
2420 int n;
2421
2422 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002423 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2425 } else {
2426 const struct mfc_cache *mfc = v;
2427 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002428 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002429
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002430 seq_printf(seq, "%08X %08X %-3hd",
2431 (__force u32) mfc->mfc_mcastgrp,
2432 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002433 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Patrick McHardy0c122952010-04-13 05:03:22 +00002435 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002436 seq_printf(seq, " %8lu %8lu %8lu",
2437 mfc->mfc_un.res.pkt,
2438 mfc->mfc_un.res.bytes,
2439 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002440 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002441 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002442 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002443 mfc->mfc_un.res.ttls[n] < 255)
2444 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002445 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 n, mfc->mfc_un.res.ttls[n]);
2447 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002448 } else {
2449 /* unresolved mfc_caches don't contain
2450 * pkt, bytes and wrong_if values
2451 */
2452 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454 seq_putc(seq, '\n');
2455 }
2456 return 0;
2457}
2458
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002459static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 .start = ipmr_mfc_seq_start,
2461 .next = ipmr_mfc_seq_next,
2462 .stop = ipmr_mfc_seq_stop,
2463 .show = ipmr_mfc_seq_show,
2464};
2465
2466static int ipmr_mfc_open(struct inode *inode, struct file *file)
2467{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002468 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2469 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
Arjan van de Ven9a321442007-02-12 00:55:35 -08002472static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 .owner = THIS_MODULE,
2474 .open = ipmr_mfc_open,
2475 .read = seq_read,
2476 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002477 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002479#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002482static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002484 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485};
2486#endif
2487
2488
2489/*
2490 * Setup for IP multicast routing
2491 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002492static int __net_init ipmr_net_init(struct net *net)
2493{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002494 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002495
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002496 err = ipmr_rules_init(net);
2497 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002498 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002499
2500#ifdef CONFIG_PROC_FS
2501 err = -ENOMEM;
2502 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2503 goto proc_vif_fail;
2504 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2505 goto proc_cache_fail;
2506#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002507 return 0;
2508
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002509#ifdef CONFIG_PROC_FS
2510proc_cache_fail:
2511 proc_net_remove(net, "ip_mr_vif");
2512proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002513 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002514#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002515fail:
2516 return err;
2517}
2518
2519static void __net_exit ipmr_net_exit(struct net *net)
2520{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002521#ifdef CONFIG_PROC_FS
2522 proc_net_remove(net, "ip_mr_cache");
2523 proc_net_remove(net, "ip_mr_vif");
2524#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002525 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002526}
2527
2528static struct pernet_operations ipmr_net_ops = {
2529 .init = ipmr_net_init,
2530 .exit = ipmr_net_exit,
2531};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002532
Wang Chen03d2f892008-07-03 12:13:36 +08002533int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Wang Chen03d2f892008-07-03 12:13:36 +08002535 int err;
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2538 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002539 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002540 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002541 if (!mrt_cachep)
2542 return -ENOMEM;
2543
Benjamin Therycf958ae32009-01-22 04:56:16 +00002544 err = register_pernet_subsys(&ipmr_net_ops);
2545 if (err)
2546 goto reg_pernet_fail;
2547
Wang Chen03d2f892008-07-03 12:13:36 +08002548 err = register_netdevice_notifier(&ip_mr_notifier);
2549 if (err)
2550 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002551#ifdef CONFIG_IP_PIMSM_V2
2552 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002553 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002554 err = -EAGAIN;
2555 goto add_proto_fail;
2556 }
2557#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002558 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2559 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002560 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002561
Tom Goff403dbb92009-06-14 03:16:13 -07002562#ifdef CONFIG_IP_PIMSM_V2
2563add_proto_fail:
2564 unregister_netdevice_notifier(&ip_mr_notifier);
2565#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002566reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002567 unregister_pernet_subsys(&ipmr_net_ops);
2568reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002569 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}