blob: 8eec8f4a05360d24897719495100ea100f0c69b2 [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);
Patrick McHardy0c122952010-04-13 05:03:22 +0000127static int ip_mr_forward(struct net *net, struct mr_table *mrt,
128 struct sk_buff *skb, struct mfc_cache *cache,
129 int local);
130static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000131 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200132static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
133 struct mfc_cache *c, struct rtmsg *rtm);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000134static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000136#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
137#define ipmr_for_each_table(mrt, net) \
138 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
139
140static struct mr_table *ipmr_get_table(struct net *net, u32 id)
141{
142 struct mr_table *mrt;
143
144 ipmr_for_each_table(mrt, net) {
145 if (mrt->id == id)
146 return mrt;
147 }
148 return NULL;
149}
150
David S. Millerda919812011-03-12 02:04:50 -0500151static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000152 struct mr_table **mrt)
153{
154 struct ipmr_result res;
155 struct fib_lookup_arg arg = { .result = &res, };
156 int err;
157
David S. Millerda919812011-03-12 02:04:50 -0500158 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
159 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000160 if (err < 0)
161 return err;
162 *mrt = res.mrt;
163 return 0;
164}
165
166static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
167 int flags, struct fib_lookup_arg *arg)
168{
169 struct ipmr_result *res = arg->result;
170 struct mr_table *mrt;
171
172 switch (rule->action) {
173 case FR_ACT_TO_TBL:
174 break;
175 case FR_ACT_UNREACHABLE:
176 return -ENETUNREACH;
177 case FR_ACT_PROHIBIT:
178 return -EACCES;
179 case FR_ACT_BLACKHOLE:
180 default:
181 return -EINVAL;
182 }
183
184 mrt = ipmr_get_table(rule->fr_net, rule->table);
185 if (mrt == NULL)
186 return -EAGAIN;
187 res->mrt = mrt;
188 return 0;
189}
190
191static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
192{
193 return 1;
194}
195
196static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
197 FRA_GENERIC_POLICY,
198};
199
200static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
201 struct fib_rule_hdr *frh, struct nlattr **tb)
202{
203 return 0;
204}
205
206static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
207 struct nlattr **tb)
208{
209 return 1;
210}
211
212static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
213 struct fib_rule_hdr *frh)
214{
215 frh->dst_len = 0;
216 frh->src_len = 0;
217 frh->tos = 0;
218 return 0;
219}
220
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200221static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200222 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000223 .rule_size = sizeof(struct ipmr_rule),
224 .addr_size = sizeof(u32),
225 .action = ipmr_rule_action,
226 .match = ipmr_rule_match,
227 .configure = ipmr_rule_configure,
228 .compare = ipmr_rule_compare,
229 .default_pref = fib_default_rule_pref,
230 .fill = ipmr_rule_fill,
231 .nlgroup = RTNLGRP_IPV4_RULE,
232 .policy = ipmr_rule_policy,
233 .owner = THIS_MODULE,
234};
235
236static int __net_init ipmr_rules_init(struct net *net)
237{
238 struct fib_rules_ops *ops;
239 struct mr_table *mrt;
240 int err;
241
242 ops = fib_rules_register(&ipmr_rules_ops_template, net);
243 if (IS_ERR(ops))
244 return PTR_ERR(ops);
245
246 INIT_LIST_HEAD(&net->ipv4.mr_tables);
247
248 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
249 if (mrt == NULL) {
250 err = -ENOMEM;
251 goto err1;
252 }
253
254 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
255 if (err < 0)
256 goto err2;
257
258 net->ipv4.mr_rules_ops = ops;
259 return 0;
260
261err2:
262 kfree(mrt);
263err1:
264 fib_rules_unregister(ops);
265 return err;
266}
267
268static void __net_exit ipmr_rules_exit(struct net *net)
269{
270 struct mr_table *mrt, *next;
271
Eric Dumazet035320d2010-06-06 23:48:40 +0000272 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
273 list_del(&mrt->list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000274 kfree(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000275 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000276 fib_rules_unregister(net->ipv4.mr_rules_ops);
277}
278#else
279#define ipmr_for_each_table(mrt, net) \
280 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
281
282static struct mr_table *ipmr_get_table(struct net *net, u32 id)
283{
284 return net->ipv4.mrt;
285}
286
David S. Millerda919812011-03-12 02:04:50 -0500287static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000288 struct mr_table **mrt)
289{
290 *mrt = net->ipv4.mrt;
291 return 0;
292}
293
294static int __net_init ipmr_rules_init(struct net *net)
295{
296 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
297 return net->ipv4.mrt ? 0 : -ENOMEM;
298}
299
300static void __net_exit ipmr_rules_exit(struct net *net)
301{
302 kfree(net->ipv4.mrt);
303}
304#endif
305
306static struct mr_table *ipmr_new_table(struct net *net, u32 id)
307{
308 struct mr_table *mrt;
309 unsigned int i;
310
311 mrt = ipmr_get_table(net, id);
312 if (mrt != NULL)
313 return mrt;
314
315 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
316 if (mrt == NULL)
317 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200318 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000319 mrt->id = id;
320
321 /* Forwarding cache */
322 for (i = 0; i < MFC_LINES; i++)
323 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
324
325 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
326
327 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
328 (unsigned long)mrt);
329
330#ifdef CONFIG_IP_PIMSM
331 mrt->mroute_reg_vif_num = -1;
332#endif
333#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
334 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
335#endif
336 return mrt;
337}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
340
Wang Chend6070322008-07-14 20:55:26 -0700341static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
342{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000343 struct net *net = dev_net(dev);
344
Wang Chend6070322008-07-14 20:55:26 -0700345 dev_close(dev);
346
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000347 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700348 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800349 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700350 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700351 struct ip_tunnel_parm p;
352
353 memset(&p, 0, sizeof(p));
354 p.iph.daddr = v->vifc_rmt_addr.s_addr;
355 p.iph.saddr = v->vifc_lcl_addr.s_addr;
356 p.iph.version = 4;
357 p.iph.ihl = 5;
358 p.iph.protocol = IPPROTO_IPIP;
359 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
360 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
361
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800362 if (ops->ndo_do_ioctl) {
363 mm_segment_t oldfs = get_fs();
364
365 set_fs(KERNEL_DS);
366 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
367 set_fs(oldfs);
368 }
Wang Chend6070322008-07-14 20:55:26 -0700369 }
370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000373struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct net_device *dev;
376
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000377 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800380 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int err;
382 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct ip_tunnel_parm p;
384 struct in_device *in_dev;
385
386 memset(&p, 0, sizeof(p));
387 p.iph.daddr = v->vifc_rmt_addr.s_addr;
388 p.iph.saddr = v->vifc_lcl_addr.s_addr;
389 p.iph.version = 4;
390 p.iph.ihl = 5;
391 p.iph.protocol = IPPROTO_IPIP;
392 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800393 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800395 if (ops->ndo_do_ioctl) {
396 mm_segment_t oldfs = get_fs();
397
398 set_fs(KERNEL_DS);
399 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
400 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000401 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800402 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dev = NULL;
405
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000406 if (err == 0 &&
407 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 dev->flags |= IFF_MULTICAST;
409
Herbert Xue5ed6392005-10-03 14:35:55 -0700410 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700411 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700413
414 ipv4_devconf_setall(in_dev);
415 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (dev_open(dev))
418 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700419 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 }
422 return dev;
423
424failure:
425 /* allow the register to be completed before unregistering. */
426 rtnl_unlock();
427 rtnl_lock();
428
429 unregister_netdevice(dev);
430 return NULL;
431}
432
433#ifdef CONFIG_IP_PIMSM
434
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000435static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000437 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000438 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500439 struct flowi4 fl4 = {
440 .flowi4_oif = dev->ifindex,
441 .flowi4_iif = skb->skb_iif,
442 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000443 };
444 int err;
445
David S. Millerda919812011-03-12 02:04:50 -0500446 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000447 if (err < 0) {
448 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000449 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000450 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700453 dev->stats.tx_bytes += skb->len;
454 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000455 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 read_unlock(&mrt_lock);
457 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000458 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Stephen Hemminger007c3832008-11-20 20:28:35 -0800461static const struct net_device_ops reg_vif_netdev_ops = {
462 .ndo_start_xmit = reg_vif_xmit,
463};
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static void reg_vif_setup(struct net_device *dev)
466{
467 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800468 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800470 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700472 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000475static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct net_device *dev;
478 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000479 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000481 if (mrt->id == RT_TABLE_DEFAULT)
482 sprintf(name, "pimreg");
483 else
484 sprintf(name, "pimreg%u", mrt->id);
485
486 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (dev == NULL)
489 return NULL;
490
Tom Goff403dbb92009-06-14 03:16:13 -0700491 dev_net_set(dev, net);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (register_netdevice(dev)) {
494 free_netdev(dev);
495 return NULL;
496 }
497 dev->iflink = 0;
498
Herbert Xu71e27da2007-06-04 23:36:06 -0700499 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000500 in_dev = __in_dev_get_rcu(dev);
501 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700502 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Herbert Xu71e27da2007-06-04 23:36:06 -0700506 ipv4_devconf_setall(in_dev);
507 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
508 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (dev_open(dev))
511 goto failure;
512
Wang Chen7dc00c82008-07-14 20:56:34 -0700513 dev_hold(dev);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return dev;
516
517failure:
518 /* allow the register to be completed before unregistering. */
519 rtnl_unlock();
520 rtnl_lock();
521
522 unregister_netdevice(dev);
523 return NULL;
524}
525#endif
526
Ben Hutchings2c530402012-07-10 10:55:09 +0000527/**
528 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700529 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900531
Patrick McHardy0c122952010-04-13 05:03:22 +0000532static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000533 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct vif_device *v;
536 struct net_device *dev;
537 struct in_device *in_dev;
538
Patrick McHardy0c122952010-04-13 05:03:22 +0000539 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EADDRNOTAVAIL;
541
Patrick McHardy0c122952010-04-13 05:03:22 +0000542 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 write_lock_bh(&mrt_lock);
545 dev = v->dev;
546 v->dev = NULL;
547
548 if (!dev) {
549 write_unlock_bh(&mrt_lock);
550 return -EADDRNOTAVAIL;
551 }
552
553#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000554 if (vifi == mrt->mroute_reg_vif_num)
555 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556#endif
557
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000558 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000560
561 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000562 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000565 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 write_unlock_bh(&mrt_lock);
569
570 dev_set_allmulti(dev, -1);
571
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000572 in_dev = __in_dev_get_rtnl(dev);
573 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700574 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ip_rt_multicast_event(in_dev);
576 }
577
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000578 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000579 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 dev_put(dev);
582 return 0;
583}
584
Eric Dumazeta8c94862010-10-01 16:15:08 +0000585static void ipmr_cache_free_rcu(struct rcu_head *head)
586{
587 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
588
589 kmem_cache_free(mrt_cachep, c);
590}
591
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000592static inline void ipmr_cache_free(struct mfc_cache *c)
593{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000594 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000598 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
600
Patrick McHardy0c122952010-04-13 05:03:22 +0000601static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200603 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700605 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Patrick McHardy0c122952010-04-13 05:03:22 +0000607 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jianjun Kongc354e122008-11-03 00:28:02 -0800609 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700610 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
612 nlh->nlmsg_type = NLMSG_ERROR;
613 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
614 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700615 e = NLMSG_DATA(nlh);
616 e->error = -ETIMEDOUT;
617 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700618
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000619 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000620 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000625 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628
Patrick McHardye258beb2010-04-13 05:03:19 +0000629/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Patrick McHardye258beb2010-04-13 05:03:19 +0000631static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Patrick McHardy0c122952010-04-13 05:03:22 +0000633 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 unsigned long now;
635 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000636 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000639 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return;
641 }
642
Patrick McHardy0c122952010-04-13 05:03:22 +0000643 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto out;
645
646 now = jiffies;
647 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Patrick McHardy0c122952010-04-13 05:03:22 +0000649 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (time_after(c->mfc_un.unres.expires, now)) {
651 unsigned long interval = c->mfc_un.unres.expires - now;
652 if (interval < expires)
653 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
655 }
656
Patrick McHardy862465f2010-04-13 05:03:21 +0000657 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000658 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Patrick McHardy0c122952010-04-13 05:03:22 +0000661 if (!list_empty(&mrt->mfc_unres_queue))
662 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664out:
665 spin_unlock(&mfc_unres_lock);
666}
667
668/* Fill oifs list. It is called under write locked mrt_lock. */
669
Patrick McHardy0c122952010-04-13 05:03:22 +0000670static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000671 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 int vifi;
674
675 cache->mfc_un.res.minvif = MAXVIFS;
676 cache->mfc_un.res.maxvif = 0;
677 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
678
Patrick McHardy0c122952010-04-13 05:03:22 +0000679 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
680 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000681 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
683 if (cache->mfc_un.res.minvif > vifi)
684 cache->mfc_un.res.minvif = vifi;
685 if (cache->mfc_un.res.maxvif <= vifi)
686 cache->mfc_un.res.maxvif = vifi + 1;
687 }
688 }
689}
690
Patrick McHardy0c122952010-04-13 05:03:22 +0000691static int vif_add(struct net *net, struct mr_table *mrt,
692 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000695 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct net_device *dev;
697 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700698 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000701 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EADDRINUSE;
703
704 switch (vifc->vifc_flags) {
705#ifdef CONFIG_IP_PIMSM
706 case VIFF_REGISTER:
707 /*
708 * Special Purpose VIF in PIM
709 * All the packets will be sent to the daemon
710 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000711 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000713 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (!dev)
715 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700716 err = dev_set_allmulti(dev, 1);
717 if (err) {
718 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700719 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700720 return err;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
723#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900724 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000725 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!dev)
727 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700728 err = dev_set_allmulti(dev, 1);
729 if (err) {
730 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700731 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700732 return err;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000735
736 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000738 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
739 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000740 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000741 dev_put(dev);
742 return -EADDRNOTAVAIL;
743 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000744 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000745 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!dev)
748 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700749 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700750 if (err) {
751 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700752 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755 default:
756 return -EINVAL;
757 }
758
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000759 in_dev = __in_dev_get_rtnl(dev);
760 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000761 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000763 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700764 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 ip_rt_multicast_event(in_dev);
766
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000767 /* Fill in the VIF structures */
768
Jianjun Kongc354e122008-11-03 00:28:02 -0800769 v->rate_limit = vifc->vifc_rate_limit;
770 v->local = vifc->vifc_lcl_addr.s_addr;
771 v->remote = vifc->vifc_rmt_addr.s_addr;
772 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!mrtsock)
774 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800775 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 v->bytes_in = 0;
777 v->bytes_out = 0;
778 v->pkt_in = 0;
779 v->pkt_out = 0;
780 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000781 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 v->link = dev->iflink;
783
784 /* And finish update writing critical data */
785 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800786 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000788 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000789 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000791 if (vifi+1 > mrt->maxvif)
792 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 write_unlock_bh(&mrt_lock);
794 return 0;
795}
796
Eric Dumazeta8c94862010-10-01 16:15:08 +0000797/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000798static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000799 __be32 origin,
800 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Jianjun Kongc354e122008-11-03 00:28:02 -0800802 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct mfc_cache *c;
804
Eric Dumazeta8c94862010-10-01 16:15:08 +0000805 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000806 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
807 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000809 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812/*
813 * Allocate a multicast cache entry
814 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000815static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Jianjun Kongc354e122008-11-03 00:28:02 -0800817 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000818
819 if (c)
820 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return c;
822}
823
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000824static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Jianjun Kongc354e122008-11-03 00:28:02 -0800826 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000827
828 if (c) {
829 skb_queue_head_init(&c->mfc_un.unres.unresolved);
830 c->mfc_un.unres.expires = jiffies + 10*HZ;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return c;
833}
834
835/*
836 * A cache entry has gone into a resolved state from queued
837 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900838
Patrick McHardy0c122952010-04-13 05:03:22 +0000839static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
840 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700843 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000845 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Jianjun Kongc354e122008-11-03 00:28:02 -0800847 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700848 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
850
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200851 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000852 nlh->nlmsg_len = skb_tail_pointer(skb) -
853 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 } else {
855 nlh->nlmsg_type = NLMSG_ERROR;
856 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
857 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700858 e = NLMSG_DATA(nlh);
859 e->error = -EMSGSIZE;
860 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Thomas Graf2942e902006-08-15 00:30:25 -0700862
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000863 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000864 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000865 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868}
869
870/*
871 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
872 * expects the following bizarre scheme.
873 *
874 * Called under mrt_lock.
875 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900876
Patrick McHardy0c122952010-04-13 05:03:22 +0000877static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000878 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300881 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct igmphdr *igmp;
883 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000884 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 int ret;
886
887#ifdef CONFIG_IP_PIMSM
888 if (assert == IGMPMSG_WHOLEPKT)
889 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
890 else
891#endif
892 skb = alloc_skb(128, GFP_ATOMIC);
893
Stephen Hemminger132adf52007-03-08 20:44:43 -0800894 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -ENOBUFS;
896
897#ifdef CONFIG_IP_PIMSM
898 if (assert == IGMPMSG_WHOLEPKT) {
899 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000900 * Duplicate old header, fix ihl, length etc.
901 * And all this only to mangle msg->im_msgtype and
902 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300904 skb_push(skb, sizeof(struct iphdr));
905 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300906 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300907 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700908 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 msg->im_msgtype = IGMPMSG_WHOLEPKT;
910 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000911 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700912 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
913 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
914 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900915 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900917 {
918
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000919 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700921 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300922 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300923 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000924 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700925 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000927 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000929 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000931 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 igmp->type =
933 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000934 igmp->code = 0;
935 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700936 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Eric Dumazet4c968702010-10-01 16:15:01 +0000939 rcu_read_lock();
940 mroute_sk = rcu_dereference(mrt->mroute_sk);
941 if (mroute_sk == NULL) {
942 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 kfree_skb(skb);
944 return -EINVAL;
945 }
946
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000947 /* Deliver to mrouted */
948
Eric Dumazet4c968702010-10-01 16:15:01 +0000949 ret = sock_queue_rcv_skb(mroute_sk, skb);
950 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +0000951 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000952 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 kfree_skb(skb);
954 }
955
956 return ret;
957}
958
959/*
960 * Queue a packet for resolution. It gets locked cache entry!
961 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963static int
Patrick McHardy0c122952010-04-13 05:03:22 +0000964ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Patrick McHardy862465f2010-04-13 05:03:21 +0000966 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int err;
968 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700969 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000972 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000973 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000974 c->mfc_origin == iph->saddr) {
975 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Patrick McHardy862465f2010-04-13 05:03:21 +0000980 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000981 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Patrick McHardy0c122952010-04-13 05:03:22 +0000983 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000984 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 spin_unlock_bh(&mfc_unres_lock);
986
987 kfree_skb(skb);
988 return -ENOBUFS;
989 }
990
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000991 /* Fill in the new cache entry */
992
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700993 c->mfc_parent = -1;
994 c->mfc_origin = iph->saddr;
995 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000997 /* Reflect first query at mrouted. */
998
Patrick McHardy0c122952010-04-13 05:03:22 +0000999 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001000 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001001 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 out - Brad Parker
1003 */
1004 spin_unlock_bh(&mfc_unres_lock);
1005
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001006 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 kfree_skb(skb);
1008 return err;
1009 }
1010
Patrick McHardy0c122952010-04-13 05:03:22 +00001011 atomic_inc(&mrt->cache_resolve_queue_len);
1012 list_add(&c->list, &mrt->mfc_unres_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
David S. Miller278554b2010-05-12 00:05:35 -07001014 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1015 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001018 /* See if we can append the packet */
1019
1020 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 kfree_skb(skb);
1022 err = -ENOBUFS;
1023 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001024 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 err = 0;
1026 }
1027
1028 spin_unlock_bh(&mfc_unres_lock);
1029 return err;
1030}
1031
1032/*
1033 * MFC cache manipulation by user space mroute daemon
1034 */
1035
Patrick McHardy0c122952010-04-13 05:03:22 +00001036static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001039 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jianjun Kongc354e122008-11-03 00:28:02 -08001041 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Patrick McHardy0c122952010-04-13 05:03:22 +00001043 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1045 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001046 list_del_rcu(&c->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001048 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050 }
1051 }
1052 return -ENOENT;
1053}
1054
Patrick McHardy0c122952010-04-13 05:03:22 +00001055static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1056 struct mfcctl *mfc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Patrick McHardy862465f2010-04-13 05:03:21 +00001058 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001060 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Patrick McHardya50436f22010-03-17 06:04:14 +00001062 if (mfc->mfcc_parent >= MAXVIFS)
1063 return -ENFILE;
1064
Jianjun Kongc354e122008-11-03 00:28:02 -08001065 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Patrick McHardy0c122952010-04-13 05:03:22 +00001067 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001069 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1070 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074
Patrick McHardy862465f2010-04-13 05:03:21 +00001075 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 write_lock_bh(&mrt_lock);
1077 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001078 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (!mrtsock)
1080 c->mfc_flags |= MFC_STATIC;
1081 write_unlock_bh(&mrt_lock);
1082 return 0;
1083 }
1084
Joe Perchesf97c1e02007-12-16 13:45:43 -08001085 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -EINVAL;
1087
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001088 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001089 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -ENOMEM;
1091
Jianjun Kongc354e122008-11-03 00:28:02 -08001092 c->mfc_origin = mfc->mfcc_origin.s_addr;
1093 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1094 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001095 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!mrtsock)
1097 c->mfc_flags |= MFC_STATIC;
1098
Eric Dumazeta8c94862010-10-01 16:15:08 +00001099 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /*
1102 * Check to see if we resolved a queued list. If so we
1103 * need to send on the frames and tidy up.
1104 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001105 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001107 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001108 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001110 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001111 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001112 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
1114 }
1115 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001116 if (list_empty(&mrt->mfc_unres_queue))
1117 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 spin_unlock_bh(&mfc_unres_lock);
1119
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001120 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001121 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001122 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 return 0;
1125}
1126
1127/*
1128 * Close the multicast socket, and clear the vif tables etc
1129 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001130
Patrick McHardy0c122952010-04-13 05:03:22 +00001131static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001134 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001135 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001136
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001137 /* Shut down all active vif entries */
1138
Patrick McHardy0c122952010-04-13 05:03:22 +00001139 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001140 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001141 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001143 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001145 /* Wipe the cache */
1146
Patrick McHardy862465f2010-04-13 05:03:21 +00001147 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001148 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001149 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001151 list_del_rcu(&c->list);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001152 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 }
1155
Patrick McHardy0c122952010-04-13 05:03:22 +00001156 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001158 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001159 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001160 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162 spin_unlock_bh(&mfc_unres_lock);
1163 }
1164}
1165
Eric Dumazet4c968702010-10-01 16:15:01 +00001166/* called from ip_ra_control(), before an RCU grace period,
1167 * we dont need to call synchronize_rcu() here
1168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169static void mrtsock_destruct(struct sock *sk)
1170{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001171 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001172 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001175 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001176 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001177 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001178 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001179 mroute_clean_tables(mrt);
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 rtnl_unlock();
1183}
1184
1185/*
1186 * Socket options and virtual interface manipulation. The whole
1187 * virtual interface system is a complete heap, but unfortunately
1188 * that's how BSD mrouted happens to think. Maybe one day with a proper
1189 * MOSPF/PIM router set up we can clean this up.
1190 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001191
David S. Millerb7058842009-09-30 16:12:20 -07001192int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 int ret;
1195 struct vifctl vif;
1196 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001197 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001198 struct mr_table *mrt;
1199
1200 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1201 if (mrt == NULL)
1202 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001203
Stephen Hemminger132adf52007-03-08 20:44:43 -08001204 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001205 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric Dumazet4c968702010-10-01 16:15:01 +00001206 !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return -EACCES;
1208 }
1209
Stephen Hemminger132adf52007-03-08 20:44:43 -08001210 switch (optname) {
1211 case MRT_INIT:
1212 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001213 inet_sk(sk)->inet_num != IPPROTO_IGMP)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001214 return -EOPNOTSUPP;
Jianjun Kongc354e122008-11-03 00:28:02 -08001215 if (optlen != sizeof(int))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001216 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Stephen Hemminger132adf52007-03-08 20:44:43 -08001218 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001219 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001221 return -EADDRINUSE;
1222 }
1223
1224 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1225 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001226 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001227 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001228 }
1229 rtnl_unlock();
1230 return ret;
1231 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001232 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001233 return -EACCES;
1234 return ip_ra_control(sk, 0, NULL);
1235 case MRT_ADD_VIF:
1236 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001237 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001238 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001239 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001240 return -EFAULT;
1241 if (vif.vifc_vifi >= MAXVIFS)
1242 return -ENFILE;
1243 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001244 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001245 ret = vif_add(net, mrt, &vif,
1246 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001247 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001248 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001249 }
1250 rtnl_unlock();
1251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /*
1254 * Manipulate the forwarding caches. These live
1255 * in a sort of kernel/user symbiosis.
1256 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001257 case MRT_ADD_MFC:
1258 case MRT_DEL_MFC:
Jianjun Kongc354e122008-11-03 00:28:02 -08001259 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001260 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001261 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001262 return -EFAULT;
1263 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001264 if (optname == MRT_DEL_MFC)
Patrick McHardy0c122952010-04-13 05:03:22 +00001265 ret = ipmr_mfc_delete(mrt, &mfc);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001266 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001267 ret = ipmr_mfc_add(net, mrt, &mfc,
1268 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001269 rtnl_unlock();
1270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * Control PIM assert.
1273 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001274 case MRT_ASSERT:
1275 {
1276 int v;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001277 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001278 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001279 mrt->mroute_do_assert = (v) ? 1 : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001280 return 0;
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001283 case MRT_PIM:
1284 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001285 int v;
1286
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001287 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001288 return -EFAULT;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001289 v = (v) ? 1 : 0;
1290
Stephen Hemminger132adf52007-03-08 20:44:43 -08001291 rtnl_lock();
1292 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001293 if (v != mrt->mroute_do_pim) {
1294 mrt->mroute_do_pim = v;
1295 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 rtnl_unlock();
1298 return ret;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001301#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1302 case MRT_TABLE:
1303 {
1304 u32 v;
1305
1306 if (optlen != sizeof(u32))
1307 return -EINVAL;
1308 if (get_user(v, (u32 __user *)optval))
1309 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001310
1311 rtnl_lock();
1312 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001313 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1314 ret = -EBUSY;
1315 } else {
1316 if (!ipmr_new_table(net, v))
1317 ret = -ENOMEM;
1318 raw_sk(sk)->ipmr_table = v;
1319 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001320 rtnl_unlock();
1321 return ret;
1322 }
1323#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001324 /*
1325 * Spurious command, or MRT_VERSION which you cannot
1326 * set.
1327 */
1328 default:
1329 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331}
1332
1333/*
1334 * Getsock opt support for the multicast routing system.
1335 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001336
Jianjun Kongc354e122008-11-03 00:28:02 -08001337int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 int olr;
1340 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001341 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001342 struct mr_table *mrt;
1343
1344 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1345 if (mrt == NULL)
1346 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Jianjun Kongc354e122008-11-03 00:28:02 -08001348 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001350 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001352 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -ENOPROTOOPT;
1354
1355 if (get_user(olr, optlen))
1356 return -EFAULT;
1357
1358 olr = min_t(unsigned int, olr, sizeof(int));
1359 if (olr < 0)
1360 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001361
Jianjun Kongc354e122008-11-03 00:28:02 -08001362 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001364 if (optname == MRT_VERSION)
1365 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001367 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001368 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369#endif
1370 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001371 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001372 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return -EFAULT;
1374 return 0;
1375}
1376
1377/*
1378 * The IP multicast ioctl support routines.
1379 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1382{
1383 struct sioc_sg_req sr;
1384 struct sioc_vif_req vr;
1385 struct vif_device *vif;
1386 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001387 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001388 struct mr_table *mrt;
1389
1390 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1391 if (mrt == NULL)
1392 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001393
Stephen Hemminger132adf52007-03-08 20:44:43 -08001394 switch (cmd) {
1395 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001396 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001397 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001398 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001399 return -EINVAL;
1400 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001401 vif = &mrt->vif_table[vr.vifi];
1402 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001403 vr.icount = vif->pkt_in;
1404 vr.ocount = vif->pkt_out;
1405 vr.ibytes = vif->bytes_in;
1406 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001408
Jianjun Kongc354e122008-11-03 00:28:02 -08001409 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001411 return 0;
1412 }
1413 read_unlock(&mrt_lock);
1414 return -EADDRNOTAVAIL;
1415 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001416 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001417 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Eric Dumazeta8c94862010-10-01 16:15:08 +00001419 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001420 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001421 if (c) {
1422 sr.pktcnt = c->mfc_un.res.pkt;
1423 sr.bytecnt = c->mfc_un.res.bytes;
1424 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001425 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001426
Jianjun Kongc354e122008-11-03 00:28:02 -08001427 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001428 return -EFAULT;
1429 return 0;
1430 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001431 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001432 return -EADDRNOTAVAIL;
1433 default:
1434 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436}
1437
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001438#ifdef CONFIG_COMPAT
1439struct compat_sioc_sg_req {
1440 struct in_addr src;
1441 struct in_addr grp;
1442 compat_ulong_t pktcnt;
1443 compat_ulong_t bytecnt;
1444 compat_ulong_t wrong_if;
1445};
1446
David S. Millerca6b8bb02011-02-03 17:24:28 -08001447struct compat_sioc_vif_req {
1448 vifi_t vifi; /* Which iface */
1449 compat_ulong_t icount;
1450 compat_ulong_t ocount;
1451 compat_ulong_t ibytes;
1452 compat_ulong_t obytes;
1453};
1454
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001455int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1456{
David S. Miller0033d5a2011-02-03 17:21:31 -08001457 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001458 struct compat_sioc_vif_req vr;
1459 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001460 struct mfc_cache *c;
1461 struct net *net = sock_net(sk);
1462 struct mr_table *mrt;
1463
1464 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1465 if (mrt == NULL)
1466 return -ENOENT;
1467
1468 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001469 case SIOCGETVIFCNT:
1470 if (copy_from_user(&vr, arg, sizeof(vr)))
1471 return -EFAULT;
1472 if (vr.vifi >= mrt->maxvif)
1473 return -EINVAL;
1474 read_lock(&mrt_lock);
1475 vif = &mrt->vif_table[vr.vifi];
1476 if (VIF_EXISTS(mrt, vr.vifi)) {
1477 vr.icount = vif->pkt_in;
1478 vr.ocount = vif->pkt_out;
1479 vr.ibytes = vif->bytes_in;
1480 vr.obytes = vif->bytes_out;
1481 read_unlock(&mrt_lock);
1482
1483 if (copy_to_user(arg, &vr, sizeof(vr)))
1484 return -EFAULT;
1485 return 0;
1486 }
1487 read_unlock(&mrt_lock);
1488 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001489 case SIOCGETSGCNT:
1490 if (copy_from_user(&sr, arg, sizeof(sr)))
1491 return -EFAULT;
1492
1493 rcu_read_lock();
1494 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1495 if (c) {
1496 sr.pktcnt = c->mfc_un.res.pkt;
1497 sr.bytecnt = c->mfc_un.res.bytes;
1498 sr.wrong_if = c->mfc_un.res.wrong_if;
1499 rcu_read_unlock();
1500
1501 if (copy_to_user(arg, &sr, sizeof(sr)))
1502 return -EFAULT;
1503 return 0;
1504 }
1505 rcu_read_unlock();
1506 return -EADDRNOTAVAIL;
1507 default:
1508 return -ENOIOCTLCMD;
1509 }
1510}
1511#endif
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1515{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001516 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001517 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001518 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct vif_device *v;
1520 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (event != NETDEV_UNREGISTER)
1523 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001524
1525 ipmr_for_each_table(mrt, net) {
1526 v = &mrt->vif_table[0];
1527 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1528 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001529 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 return NOTIFY_DONE;
1533}
1534
1535
Jianjun Kongc354e122008-11-03 00:28:02 -08001536static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 .notifier_call = ipmr_device_event,
1538};
1539
1540/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001541 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * This avoids tunnel drivers and other mess and gives us the speed so
1543 * important for multicast video.
1544 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001545
Al Viro114c7842006-09-27 18:39:29 -07001546static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001548 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001549 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001550
1551 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001552 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001553 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001554 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001556 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001557 iph->tos = old_iph->tos;
1558 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 iph->frag_off = 0;
1560 iph->daddr = daddr;
1561 iph->saddr = saddr;
1562 iph->protocol = IPPROTO_IPIP;
1563 iph->ihl = 5;
1564 iph->tot_len = htons(skb->len);
Eric Dumazetadf30902009-06-02 05:19:30 +00001565 ip_select_ident(iph, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 ip_send_check(iph);
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1569 nf_reset(skb);
1570}
1571
1572static inline int ipmr_forward_finish(struct sk_buff *skb)
1573{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001574 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Eric Dumazetadf30902009-06-02 05:19:30 +00001576 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Vincent Bernat2d8dbb02012-06-05 03:41:42 +00001577 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 if (unlikely(opt->optlen))
1580 ip_forward_options(skb);
1581
1582 return dst_output(skb);
1583}
1584
1585/*
1586 * Processing handlers for ipmr_forward
1587 */
1588
Patrick McHardy0c122952010-04-13 05:03:22 +00001589static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1590 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001592 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001593 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct net_device *dev;
1595 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001596 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 int encap = 0;
1598
1599 if (vif->dev == NULL)
1600 goto out_free;
1601
1602#ifdef CONFIG_IP_PIMSM
1603 if (vif->flags & VIFF_REGISTER) {
1604 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001605 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001606 vif->dev->stats.tx_bytes += skb->len;
1607 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001608 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001609 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611#endif
1612
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001613 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001614 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001615 vif->remote, vif->local,
1616 0, 0,
1617 IPPROTO_IPIP,
1618 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001619 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto out_free;
1621 encap = sizeof(struct iphdr);
1622 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001623 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001624 0, 0,
1625 IPPROTO_IPIP,
1626 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001627 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto out_free;
1629 }
1630
Changli Gaod8d1f302010-06-10 23:31:35 -07001631 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Changli Gaod8d1f302010-06-10 23:31:35 -07001633 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001635 * allow to send ICMP, so that packets will disappear
1636 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 */
1638
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001639 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 ip_rt_put(rt);
1641 goto out_free;
1642 }
1643
Changli Gaod8d1f302010-06-10 23:31:35 -07001644 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001647 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto out_free;
1649 }
1650
1651 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001652 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Eric Dumazetadf30902009-06-02 05:19:30 +00001654 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001655 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001656 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001659 * What do we do with netfilter? -- RR
1660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (vif->flags & VIFF_TUNNEL) {
1662 ip_encap(skb, vif->local, vif->remote);
1663 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001664 vif->dev->stats.tx_packets++;
1665 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
1668 IPCB(skb)->flags |= IPSKB_FORWARDED;
1669
1670 /*
1671 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1672 * not only before forwarding, but after forwarding on all output
1673 * interfaces. It is clear, if mrouter runs a multicasting
1674 * program, it should receive packets not depending to what interface
1675 * program is joined.
1676 * If we will not make it, the program will have to join on all
1677 * interfaces. On the other hand, multihoming host (or router, but
1678 * not mrouter) cannot join to more than one interface - it will
1679 * result in receiving multiple packets.
1680 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001681 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 ipmr_forward_finish);
1683 return;
1684
1685out_free:
1686 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Patrick McHardy0c122952010-04-13 05:03:22 +00001689static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001692
1693 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1694 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 break;
1696 }
1697 return ct;
1698}
1699
1700/* "local" means that we should preserve one skb (for local delivery) */
1701
Patrick McHardy0c122952010-04-13 05:03:22 +00001702static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1703 struct sk_buff *skb, struct mfc_cache *cache,
1704 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 int psend = -1;
1707 int vif, ct;
1708
1709 vif = cache->mfc_parent;
1710 cache->mfc_un.res.pkt++;
1711 cache->mfc_un.res.bytes += skb->len;
1712
1713 /*
1714 * Wrong interface: drop packet and (maybe) send PIM assert.
1715 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001716 if (mrt->vif_table[vif].dev != skb->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 int true_vifi;
1718
David S. Millerc7537962010-11-11 17:07:48 -08001719 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001721 * Very complicated situation...
1722 *
1723 * The best workaround until routing daemons will be
1724 * fixed is not to redistribute packet, if it was
1725 * send through wrong interface. It means, that
1726 * multicast applications WILL NOT work for
1727 * (S,G), which have default multicast route pointing
1728 * to wrong oif. In any case, it is not a good
1729 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
1731 goto dont_forward;
1732 }
1733
1734 cache->mfc_un.res.wrong_if++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001735 true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Patrick McHardy0c122952010-04-13 05:03:22 +00001737 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001739 * so that we cannot check that packet arrived on an oif.
1740 * It is bad, but otherwise we would need to move pretty
1741 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001743 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001744 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001745 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1747 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001748 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750 goto dont_forward;
1751 }
1752
Patrick McHardy0c122952010-04-13 05:03:22 +00001753 mrt->vif_table[vif].pkt_in++;
1754 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 /*
1757 * Forward the frame
1758 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001759 for (ct = cache->mfc_un.res.maxvif - 1;
1760 ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001761 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (psend != -1) {
1763 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001766 ipmr_queue_xmit(net, mrt, skb2, cache,
1767 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001769 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771 }
1772 if (psend != -1) {
1773 if (local) {
1774 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001777 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001779 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781 }
1782 }
1783
1784dont_forward:
1785 if (!local)
1786 kfree_skb(skb);
1787 return 0;
1788}
1789
David S. Miller417da662011-05-03 19:42:43 -07001790static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001791{
David S. Miller417da662011-05-03 19:42:43 -07001792 struct rtable *rt = skb_rtable(skb);
1793 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001794 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001795 .daddr = iph->daddr,
1796 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001797 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001798 .flowi4_oif = (rt_is_output_route(rt) ?
1799 skb->dev->ifindex : 0),
1800 .flowi4_iif = (rt_is_output_route(rt) ?
1801 net->loopback_dev->ifindex :
1802 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001803 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001804 };
1805 struct mr_table *mrt;
1806 int err;
1807
David S. Millerda919812011-03-12 02:04:50 -05001808 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001809 if (err)
1810 return ERR_PTR(err);
1811 return mrt;
1812}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814/*
1815 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001816 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
1818
1819int ip_mr_input(struct sk_buff *skb)
1820{
1821 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001822 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001823 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001824 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001827 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001829 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 goto dont_forward;
1831
David S. Miller417da662011-05-03 19:42:43 -07001832 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001833 if (IS_ERR(mrt)) {
1834 kfree_skb(skb);
1835 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001838 if (IPCB(skb)->opt.router_alert) {
1839 if (ip_call_ra_chain(skb))
1840 return 0;
1841 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1842 /* IGMPv1 (and broken IGMPv2 implementations sort of
1843 * Cisco IOS <= 11.2(8)) do not put router alert
1844 * option to IGMP packets destined to routable
1845 * groups. It is very bad, because it means
1846 * that we can forward NO IGMP messages.
1847 */
1848 struct sock *mroute_sk;
1849
1850 mroute_sk = rcu_dereference(mrt->mroute_sk);
1851 if (mroute_sk) {
1852 nf_reset(skb);
1853 raw_rcv(mroute_sk, skb);
1854 return 0;
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857 }
1858
Eric Dumazeta8c94862010-10-01 16:15:08 +00001859 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001860 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /*
1863 * No usable cache entry
1864 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001865 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 int vif;
1867
1868 if (local) {
1869 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1870 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001871 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 skb = skb2;
1874 }
1875
Eric Dumazeta8c94862010-10-01 16:15:08 +00001876 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001877 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001879 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 read_unlock(&mrt_lock);
1881
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001882 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884 read_unlock(&mrt_lock);
1885 kfree_skb(skb);
1886 return -ENODEV;
1887 }
1888
Eric Dumazeta8c94862010-10-01 16:15:08 +00001889 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001890 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 read_unlock(&mrt_lock);
1892
1893 if (local)
1894 return ip_local_deliver(skb);
1895
1896 return 0;
1897
1898dont_forward:
1899 if (local)
1900 return ip_local_deliver(skb);
1901 kfree_skb(skb);
1902 return 0;
1903}
1904
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001905#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00001906/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001907static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1908 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001910 struct net_device *reg_dev = NULL;
1911 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001913 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001915 * Check that:
1916 * a. packet is really sent to a multicast group
1917 * b. packet is not a NULL-REGISTER
1918 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001920 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001922 ntohs(encap->tot_len) + pimlen > skb->len)
1923 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001926 if (mrt->mroute_reg_vif_num >= 0)
1927 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 read_unlock(&mrt_lock);
1929
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001930 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001931 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001933 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00001934 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001935 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00001937 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07001939
1940 skb_tunnel_rx(skb, reg_dev);
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001943
Eric Dumazet55747a02010-10-01 16:14:55 +00001944 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001945}
1946#endif
1947
1948#ifdef CONFIG_IP_PIMSM_V1
1949/*
1950 * Handle IGMP messages of PIMv1
1951 */
1952
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001953int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001954{
1955 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001956 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001957 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001958
1959 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1960 goto drop;
1961
1962 pim = igmp_hdr(skb);
1963
David S. Miller417da662011-05-03 19:42:43 -07001964 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001965 if (IS_ERR(mrt))
1966 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00001967 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001968 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1969 goto drop;
1970
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001971 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001972drop:
1973 kfree_skb(skb);
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return 0;
1976}
1977#endif
1978
1979#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001980static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001983 struct net *net = dev_net(skb->dev);
1984 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001986 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 goto drop;
1988
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001989 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001990 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1991 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001992 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08001993 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto drop;
1995
David S. Miller417da662011-05-03 19:42:43 -07001996 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001997 if (IS_ERR(mrt))
1998 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001999 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002000drop:
2001 kfree_skb(skb);
2002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return 0;
2004}
2005#endif
2006
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002007static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2008 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 int ct;
2011 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002012 struct nlattr *mp_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Nicolas Dichtel74381892010-03-25 23:45:35 +00002014 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002015 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002016 return -ENOENT;
2017
Thomas Graf92a395e2012-06-26 23:36:13 +00002018 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2019 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2020 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Thomas Graf92a395e2012-06-26 23:36:13 +00002022 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2023 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002026 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002027 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2028 nla_nest_cancel(skb, mp_attr);
2029 return -EMSGSIZE;
2030 }
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 nhp->rtnh_flags = 0;
2033 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002034 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 nhp->rtnh_len = sizeof(*nhp);
2036 }
2037 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002038
2039 nla_nest_end(skb, mp_attr);
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 rtm->rtm_type = RTN_MULTICAST;
2042 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
David S. Miller9a1b9492011-05-04 12:18:54 -07002045int ipmr_get_route(struct net *net, struct sk_buff *skb,
2046 __be32 saddr, __be32 daddr,
2047 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002050 struct mr_table *mrt;
2051 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002053 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2054 if (mrt == NULL)
2055 return -ENOENT;
2056
Eric Dumazeta8c94862010-10-01 16:15:08 +00002057 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002058 cache = ipmr_cache_find(mrt, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Jianjun Kongc354e122008-11-03 00:28:02 -08002060 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002061 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002062 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002064 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002067 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return -EAGAIN;
2069 }
2070
2071 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002072 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002073 if (dev)
2074 vif = ipmr_find_vif(mrt, dev);
2075 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002077 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return -ENODEV;
2079 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002080 skb2 = skb_clone(skb, GFP_ATOMIC);
2081 if (!skb2) {
2082 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002083 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002084 return -ENOMEM;
2085 }
2086
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002087 skb_push(skb2, sizeof(struct iphdr));
2088 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002089 iph = ip_hdr(skb2);
2090 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002091 iph->saddr = saddr;
2092 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002093 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002094 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002096 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return err;
2098 }
2099
Eric Dumazeta8c94862010-10-01 16:15:08 +00002100 read_lock(&mrt_lock);
2101 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002103 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002105 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return err;
2107}
2108
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002109static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2110 u32 pid, u32 seq, struct mfc_cache *c)
2111{
2112 struct nlmsghdr *nlh;
2113 struct rtmsg *rtm;
2114
2115 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2116 if (nlh == NULL)
2117 return -EMSGSIZE;
2118
2119 rtm = nlmsg_data(nlh);
2120 rtm->rtm_family = RTNL_FAMILY_IPMR;
2121 rtm->rtm_dst_len = 32;
2122 rtm->rtm_src_len = 32;
2123 rtm->rtm_tos = 0;
2124 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002125 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2126 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002127 rtm->rtm_type = RTN_MULTICAST;
2128 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2129 rtm->rtm_protocol = RTPROT_UNSPEC;
2130 rtm->rtm_flags = 0;
2131
David S. Millerf3756b72012-04-01 20:39:02 -04002132 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2133 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2134 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002135 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2136 goto nla_put_failure;
2137
2138 return nlmsg_end(skb, nlh);
2139
2140nla_put_failure:
2141 nlmsg_cancel(skb, nlh);
2142 return -EMSGSIZE;
2143}
2144
2145static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2146{
2147 struct net *net = sock_net(skb->sk);
2148 struct mr_table *mrt;
2149 struct mfc_cache *mfc;
2150 unsigned int t = 0, s_t;
2151 unsigned int h = 0, s_h;
2152 unsigned int e = 0, s_e;
2153
2154 s_t = cb->args[0];
2155 s_h = cb->args[1];
2156 s_e = cb->args[2];
2157
Eric Dumazeta8c94862010-10-01 16:15:08 +00002158 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002159 ipmr_for_each_table(mrt, net) {
2160 if (t < s_t)
2161 goto next_table;
2162 if (t > s_t)
2163 s_h = 0;
2164 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002165 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002166 if (e < s_e)
2167 goto next_entry;
2168 if (ipmr_fill_mroute(mrt, skb,
2169 NETLINK_CB(cb->skb).pid,
2170 cb->nlh->nlmsg_seq,
2171 mfc) < 0)
2172 goto done;
2173next_entry:
2174 e++;
2175 }
2176 e = s_e = 0;
2177 }
2178 s_h = 0;
2179next_table:
2180 t++;
2181 }
2182done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002183 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002184
2185 cb->args[2] = e;
2186 cb->args[1] = h;
2187 cb->args[0] = t;
2188
2189 return skb->len;
2190}
2191
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002192#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002194 * The /proc interfaces to multicast routing :
2195 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 */
2197struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002198 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002199 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 int ct;
2201};
2202
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002203static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2204 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 loff_t pos)
2206{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002207 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002208
2209 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2210 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002212 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002213 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 return NULL;
2216}
2217
2218static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002219 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002221 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002222 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002223 struct mr_table *mrt;
2224
2225 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2226 if (mrt == NULL)
2227 return ERR_PTR(-ENOENT);
2228
2229 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002232 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 : SEQ_START_TOKEN;
2234}
2235
2236static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2237{
2238 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002239 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002240 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 ++*pos;
2243 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002244 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002245
Patrick McHardy0c122952010-04-13 05:03:22 +00002246 while (++iter->ct < mrt->maxvif) {
2247 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002249 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
2251 return NULL;
2252}
2253
2254static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002255 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 read_unlock(&mrt_lock);
2258}
2259
2260static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2261{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002262 struct ipmr_vif_iter *iter = seq->private;
2263 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002266 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2268 } else {
2269 const struct vif_device *vif = v;
2270 const char *name = vif->dev ? vif->dev->name : "none";
2271
2272 seq_printf(seq,
2273 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002274 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002275 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 vif->bytes_out, vif->pkt_out,
2277 vif->flags, vif->local, vif->remote);
2278 }
2279 return 0;
2280}
2281
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002282static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 .start = ipmr_vif_seq_start,
2284 .next = ipmr_vif_seq_next,
2285 .stop = ipmr_vif_seq_stop,
2286 .show = ipmr_vif_seq_show,
2287};
2288
2289static int ipmr_vif_open(struct inode *inode, struct file *file)
2290{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002291 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2292 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Arjan van de Ven9a321442007-02-12 00:55:35 -08002295static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 .owner = THIS_MODULE,
2297 .open = ipmr_vif_open,
2298 .read = seq_read,
2299 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002300 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301};
2302
2303struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002304 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002305 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002306 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 int ct;
2308};
2309
2310
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002311static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2312 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002314 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 struct mfc_cache *mfc;
2316
Eric Dumazeta8c94862010-10-01 16:15:08 +00002317 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002318 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002319 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002320 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002321 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002323 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002324 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002327 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002328 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002329 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return mfc;
2331 spin_unlock_bh(&mfc_unres_lock);
2332
2333 it->cache = NULL;
2334 return NULL;
2335}
2336
2337
2338static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2339{
2340 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002341 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002342 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002343
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002344 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2345 if (mrt == NULL)
2346 return ERR_PTR(-ENOENT);
2347
2348 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 it->cache = NULL;
2350 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002351 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 : SEQ_START_TOKEN;
2353}
2354
2355static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2356{
2357 struct mfc_cache *mfc = v;
2358 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002359 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002360 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 ++*pos;
2363
2364 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002365 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Patrick McHardy862465f2010-04-13 05:03:21 +00002367 if (mfc->list.next != it->cache)
2368 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002369
Patrick McHardy0c122952010-04-13 05:03:22 +00002370 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto end_of_list;
2372
Patrick McHardy0c122952010-04-13 05:03:22 +00002373 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002376 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002377 if (list_empty(it->cache))
2378 continue;
2379 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
2381
2382 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002383 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002384 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002388 if (!list_empty(it->cache))
2389 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002391end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 spin_unlock_bh(&mfc_unres_lock);
2393 it->cache = NULL;
2394
2395 return NULL;
2396}
2397
2398static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2399{
2400 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002401 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Patrick McHardy0c122952010-04-13 05:03:22 +00002403 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002405 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002406 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2410{
2411 int n;
2412
2413 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002414 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2416 } else {
2417 const struct mfc_cache *mfc = v;
2418 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002419 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002420
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002421 seq_printf(seq, "%08X %08X %-3hd",
2422 (__force u32) mfc->mfc_mcastgrp,
2423 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002424 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Patrick McHardy0c122952010-04-13 05:03:22 +00002426 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002427 seq_printf(seq, " %8lu %8lu %8lu",
2428 mfc->mfc_un.res.pkt,
2429 mfc->mfc_un.res.bytes,
2430 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002431 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002432 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002433 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002434 mfc->mfc_un.res.ttls[n] < 255)
2435 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002436 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 n, mfc->mfc_un.res.ttls[n]);
2438 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002439 } else {
2440 /* unresolved mfc_caches don't contain
2441 * pkt, bytes and wrong_if values
2442 */
2443 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445 seq_putc(seq, '\n');
2446 }
2447 return 0;
2448}
2449
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002450static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 .start = ipmr_mfc_seq_start,
2452 .next = ipmr_mfc_seq_next,
2453 .stop = ipmr_mfc_seq_stop,
2454 .show = ipmr_mfc_seq_show,
2455};
2456
2457static int ipmr_mfc_open(struct inode *inode, struct file *file)
2458{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002459 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2460 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Arjan van de Ven9a321442007-02-12 00:55:35 -08002463static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 .owner = THIS_MODULE,
2465 .open = ipmr_mfc_open,
2466 .read = seq_read,
2467 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002468 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002470#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002473static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002475 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476};
2477#endif
2478
2479
2480/*
2481 * Setup for IP multicast routing
2482 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002483static int __net_init ipmr_net_init(struct net *net)
2484{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002485 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002486
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002487 err = ipmr_rules_init(net);
2488 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002489 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002490
2491#ifdef CONFIG_PROC_FS
2492 err = -ENOMEM;
2493 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2494 goto proc_vif_fail;
2495 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2496 goto proc_cache_fail;
2497#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002498 return 0;
2499
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002500#ifdef CONFIG_PROC_FS
2501proc_cache_fail:
2502 proc_net_remove(net, "ip_mr_vif");
2503proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002504 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002505#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002506fail:
2507 return err;
2508}
2509
2510static void __net_exit ipmr_net_exit(struct net *net)
2511{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002512#ifdef CONFIG_PROC_FS
2513 proc_net_remove(net, "ip_mr_cache");
2514 proc_net_remove(net, "ip_mr_vif");
2515#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002516 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002517}
2518
2519static struct pernet_operations ipmr_net_ops = {
2520 .init = ipmr_net_init,
2521 .exit = ipmr_net_exit,
2522};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002523
Wang Chen03d2f892008-07-03 12:13:36 +08002524int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Wang Chen03d2f892008-07-03 12:13:36 +08002526 int err;
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2529 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002530 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002531 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002532 if (!mrt_cachep)
2533 return -ENOMEM;
2534
Benjamin Therycf958ae32009-01-22 04:56:16 +00002535 err = register_pernet_subsys(&ipmr_net_ops);
2536 if (err)
2537 goto reg_pernet_fail;
2538
Wang Chen03d2f892008-07-03 12:13:36 +08002539 err = register_netdevice_notifier(&ip_mr_notifier);
2540 if (err)
2541 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002542#ifdef CONFIG_IP_PIMSM_V2
2543 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002544 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002545 err = -EAGAIN;
2546 goto add_proto_fail;
2547 }
2548#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002549 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2550 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002551 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002552
Tom Goff403dbb92009-06-14 03:16:13 -07002553#ifdef CONFIG_IP_PIMSM_V2
2554add_proto_fail:
2555 unregister_netdevice_notifier(&ip_mr_notifier);
2556#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002557reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002558 unregister_pernet_subsys(&ipmr_net_ops);
2559reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002560 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002561 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}