blob: 7e41ac0b926035fa8c1894a00d3b1f68d658aa38 [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/system.h>
30#include <asm/uaccess.h>
31#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
34#include <linux/timer.h>
35#include <linux/mm.h>
36#include <linux/kernel.h>
37#include <linux/fcntl.h>
38#include <linux/stat.h>
39#include <linux/socket.h>
40#include <linux/in.h>
41#include <linux/inet.h>
42#include <linux/netdevice.h>
43#include <linux/inetdevice.h>
44#include <linux/igmp.h>
45#include <linux/proc_fs.h>
46#include <linux/seq_file.h>
47#include <linux/mroute.h>
48#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080049#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020051#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/ip.h>
53#include <net/protocol.h>
54#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020055#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/sock.h>
57#include <net/icmp.h>
58#include <net/udp.h>
59#include <net/raw.h>
60#include <linux/notifier.h>
61#include <linux/if_arp.h>
62#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000063#include <linux/compat.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
151static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
152 struct mr_table **mrt)
153{
154 struct ipmr_result res;
155 struct fib_lookup_arg arg = { .result = &res, };
156 int err;
157
158 err = fib_rules_lookup(net->ipv4.mr_rules_ops, flp, 0, &arg);
159 if (err < 0)
160 return err;
161 *mrt = res.mrt;
162 return 0;
163}
164
165static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
166 int flags, struct fib_lookup_arg *arg)
167{
168 struct ipmr_result *res = arg->result;
169 struct mr_table *mrt;
170
171 switch (rule->action) {
172 case FR_ACT_TO_TBL:
173 break;
174 case FR_ACT_UNREACHABLE:
175 return -ENETUNREACH;
176 case FR_ACT_PROHIBIT:
177 return -EACCES;
178 case FR_ACT_BLACKHOLE:
179 default:
180 return -EINVAL;
181 }
182
183 mrt = ipmr_get_table(rule->fr_net, rule->table);
184 if (mrt == NULL)
185 return -EAGAIN;
186 res->mrt = mrt;
187 return 0;
188}
189
190static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
191{
192 return 1;
193}
194
195static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
196 FRA_GENERIC_POLICY,
197};
198
199static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
200 struct fib_rule_hdr *frh, struct nlattr **tb)
201{
202 return 0;
203}
204
205static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
206 struct nlattr **tb)
207{
208 return 1;
209}
210
211static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
212 struct fib_rule_hdr *frh)
213{
214 frh->dst_len = 0;
215 frh->src_len = 0;
216 frh->tos = 0;
217 return 0;
218}
219
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200220static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200221 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000222 .rule_size = sizeof(struct ipmr_rule),
223 .addr_size = sizeof(u32),
224 .action = ipmr_rule_action,
225 .match = ipmr_rule_match,
226 .configure = ipmr_rule_configure,
227 .compare = ipmr_rule_compare,
228 .default_pref = fib_default_rule_pref,
229 .fill = ipmr_rule_fill,
230 .nlgroup = RTNLGRP_IPV4_RULE,
231 .policy = ipmr_rule_policy,
232 .owner = THIS_MODULE,
233};
234
235static int __net_init ipmr_rules_init(struct net *net)
236{
237 struct fib_rules_ops *ops;
238 struct mr_table *mrt;
239 int err;
240
241 ops = fib_rules_register(&ipmr_rules_ops_template, net);
242 if (IS_ERR(ops))
243 return PTR_ERR(ops);
244
245 INIT_LIST_HEAD(&net->ipv4.mr_tables);
246
247 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
248 if (mrt == NULL) {
249 err = -ENOMEM;
250 goto err1;
251 }
252
253 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
254 if (err < 0)
255 goto err2;
256
257 net->ipv4.mr_rules_ops = ops;
258 return 0;
259
260err2:
261 kfree(mrt);
262err1:
263 fib_rules_unregister(ops);
264 return err;
265}
266
267static void __net_exit ipmr_rules_exit(struct net *net)
268{
269 struct mr_table *mrt, *next;
270
Eric Dumazet035320d2010-06-06 23:48:40 +0000271 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
272 list_del(&mrt->list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000273 kfree(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000274 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000275 fib_rules_unregister(net->ipv4.mr_rules_ops);
276}
277#else
278#define ipmr_for_each_table(mrt, net) \
279 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
280
281static struct mr_table *ipmr_get_table(struct net *net, u32 id)
282{
283 return net->ipv4.mrt;
284}
285
286static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
287 struct mr_table **mrt)
288{
289 *mrt = net->ipv4.mrt;
290 return 0;
291}
292
293static int __net_init ipmr_rules_init(struct net *net)
294{
295 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
296 return net->ipv4.mrt ? 0 : -ENOMEM;
297}
298
299static void __net_exit ipmr_rules_exit(struct net *net)
300{
301 kfree(net->ipv4.mrt);
302}
303#endif
304
305static struct mr_table *ipmr_new_table(struct net *net, u32 id)
306{
307 struct mr_table *mrt;
308 unsigned int i;
309
310 mrt = ipmr_get_table(net, id);
311 if (mrt != NULL)
312 return mrt;
313
314 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
315 if (mrt == NULL)
316 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200317 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000318 mrt->id = id;
319
320 /* Forwarding cache */
321 for (i = 0; i < MFC_LINES; i++)
322 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
323
324 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
325
326 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
327 (unsigned long)mrt);
328
329#ifdef CONFIG_IP_PIMSM
330 mrt->mroute_reg_vif_num = -1;
331#endif
332#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
333 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
334#endif
335 return mrt;
336}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
339
Wang Chend6070322008-07-14 20:55:26 -0700340static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
341{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000342 struct net *net = dev_net(dev);
343
Wang Chend6070322008-07-14 20:55:26 -0700344 dev_close(dev);
345
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000346 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700347 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800348 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700349 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700350 struct ip_tunnel_parm p;
351
352 memset(&p, 0, sizeof(p));
353 p.iph.daddr = v->vifc_rmt_addr.s_addr;
354 p.iph.saddr = v->vifc_lcl_addr.s_addr;
355 p.iph.version = 4;
356 p.iph.ihl = 5;
357 p.iph.protocol = IPPROTO_IPIP;
358 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
359 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
360
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800361 if (ops->ndo_do_ioctl) {
362 mm_segment_t oldfs = get_fs();
363
364 set_fs(KERNEL_DS);
365 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
366 set_fs(oldfs);
367 }
Wang Chend6070322008-07-14 20:55:26 -0700368 }
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000372struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct net_device *dev;
375
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000376 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800379 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int err;
381 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct ip_tunnel_parm p;
383 struct in_device *in_dev;
384
385 memset(&p, 0, sizeof(p));
386 p.iph.daddr = v->vifc_rmt_addr.s_addr;
387 p.iph.saddr = v->vifc_lcl_addr.s_addr;
388 p.iph.version = 4;
389 p.iph.ihl = 5;
390 p.iph.protocol = IPPROTO_IPIP;
391 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800392 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800394 if (ops->ndo_do_ioctl) {
395 mm_segment_t oldfs = get_fs();
396
397 set_fs(KERNEL_DS);
398 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
399 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000400 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800401 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dev = NULL;
404
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000405 if (err == 0 &&
406 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dev->flags |= IFF_MULTICAST;
408
Herbert Xue5ed6392005-10-03 14:35:55 -0700409 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700410 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700412
413 ipv4_devconf_setall(in_dev);
414 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if (dev_open(dev))
417 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700418 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 }
421 return dev;
422
423failure:
424 /* allow the register to be completed before unregistering. */
425 rtnl_unlock();
426 rtnl_lock();
427
428 unregister_netdevice(dev);
429 return NULL;
430}
431
432#ifdef CONFIG_IP_PIMSM
433
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000434static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000436 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000437 struct mr_table *mrt;
438 struct flowi fl = {
439 .oif = dev->ifindex,
440 .iif = skb->skb_iif,
441 .mark = skb->mark,
442 };
443 int err;
444
445 err = ipmr_fib_lookup(net, &fl, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000446 if (err < 0) {
447 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000448 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000449 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700452 dev->stats.tx_bytes += skb->len;
453 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000454 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 read_unlock(&mrt_lock);
456 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000457 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Stephen Hemminger007c3832008-11-20 20:28:35 -0800460static const struct net_device_ops reg_vif_netdev_ops = {
461 .ndo_start_xmit = reg_vif_xmit,
462};
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static void reg_vif_setup(struct net_device *dev)
465{
466 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800467 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800469 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700471 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000474static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct net_device *dev;
477 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000478 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000480 if (mrt->id == RT_TABLE_DEFAULT)
481 sprintf(name, "pimreg");
482 else
483 sprintf(name, "pimreg%u", mrt->id);
484
485 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (dev == NULL)
488 return NULL;
489
Tom Goff403dbb92009-06-14 03:16:13 -0700490 dev_net_set(dev, net);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (register_netdevice(dev)) {
493 free_netdev(dev);
494 return NULL;
495 }
496 dev->iflink = 0;
497
Herbert Xu71e27da2007-06-04 23:36:06 -0700498 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000499 in_dev = __in_dev_get_rcu(dev);
500 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700501 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Herbert Xu71e27da2007-06-04 23:36:06 -0700505 ipv4_devconf_setall(in_dev);
506 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
507 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (dev_open(dev))
510 goto failure;
511
Wang Chen7dc00c82008-07-14 20:56:34 -0700512 dev_hold(dev);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return dev;
515
516failure:
517 /* allow the register to be completed before unregistering. */
518 rtnl_unlock();
519 rtnl_lock();
520
521 unregister_netdevice(dev);
522 return NULL;
523}
524#endif
525
526/*
527 * Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700528 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900530
Patrick McHardy0c122952010-04-13 05:03:22 +0000531static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000532 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct vif_device *v;
535 struct net_device *dev;
536 struct in_device *in_dev;
537
Patrick McHardy0c122952010-04-13 05:03:22 +0000538 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EADDRNOTAVAIL;
540
Patrick McHardy0c122952010-04-13 05:03:22 +0000541 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 write_lock_bh(&mrt_lock);
544 dev = v->dev;
545 v->dev = NULL;
546
547 if (!dev) {
548 write_unlock_bh(&mrt_lock);
549 return -EADDRNOTAVAIL;
550 }
551
552#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000553 if (vifi == mrt->mroute_reg_vif_num)
554 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#endif
556
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000557 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000559
560 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000561 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 break;
563 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000564 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
567 write_unlock_bh(&mrt_lock);
568
569 dev_set_allmulti(dev, -1);
570
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000571 in_dev = __in_dev_get_rtnl(dev);
572 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700573 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ip_rt_multicast_event(in_dev);
575 }
576
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000577 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000578 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 dev_put(dev);
581 return 0;
582}
583
Eric Dumazeta8c94862010-10-01 16:15:08 +0000584static void ipmr_cache_free_rcu(struct rcu_head *head)
585{
586 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
587
588 kmem_cache_free(mrt_cachep, c);
589}
590
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000591static inline void ipmr_cache_free(struct mfc_cache *c)
592{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000593 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000597 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
599
Patrick McHardy0c122952010-04-13 05:03:22 +0000600static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200602 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700604 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Patrick McHardy0c122952010-04-13 05:03:22 +0000606 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Jianjun Kongc354e122008-11-03 00:28:02 -0800608 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700609 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
611 nlh->nlmsg_type = NLMSG_ERROR;
612 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
613 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700614 e = NLMSG_DATA(nlh);
615 e->error = -ETIMEDOUT;
616 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700617
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000618 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000619 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000624 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627
Patrick McHardye258beb2010-04-13 05:03:19 +0000628/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Patrick McHardye258beb2010-04-13 05:03:19 +0000630static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Patrick McHardy0c122952010-04-13 05:03:22 +0000632 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 unsigned long now;
634 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000635 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000638 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return;
640 }
641
Patrick McHardy0c122952010-04-13 05:03:22 +0000642 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto out;
644
645 now = jiffies;
646 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Patrick McHardy0c122952010-04-13 05:03:22 +0000648 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (time_after(c->mfc_un.unres.expires, now)) {
650 unsigned long interval = c->mfc_un.unres.expires - now;
651 if (interval < expires)
652 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 continue;
654 }
655
Patrick McHardy862465f2010-04-13 05:03:21 +0000656 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000657 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Patrick McHardy0c122952010-04-13 05:03:22 +0000660 if (!list_empty(&mrt->mfc_unres_queue))
661 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663out:
664 spin_unlock(&mfc_unres_lock);
665}
666
667/* Fill oifs list. It is called under write locked mrt_lock. */
668
Patrick McHardy0c122952010-04-13 05:03:22 +0000669static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000670 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int vifi;
673
674 cache->mfc_un.res.minvif = MAXVIFS;
675 cache->mfc_un.res.maxvif = 0;
676 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
677
Patrick McHardy0c122952010-04-13 05:03:22 +0000678 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
679 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000680 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
682 if (cache->mfc_un.res.minvif > vifi)
683 cache->mfc_un.res.minvif = vifi;
684 if (cache->mfc_un.res.maxvif <= vifi)
685 cache->mfc_un.res.maxvif = vifi + 1;
686 }
687 }
688}
689
Patrick McHardy0c122952010-04-13 05:03:22 +0000690static int vif_add(struct net *net, struct mr_table *mrt,
691 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000694 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct net_device *dev;
696 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700697 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000700 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -EADDRINUSE;
702
703 switch (vifc->vifc_flags) {
704#ifdef CONFIG_IP_PIMSM
705 case VIFF_REGISTER:
706 /*
707 * Special Purpose VIF in PIM
708 * All the packets will be sent to the daemon
709 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000710 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000712 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!dev)
714 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700715 err = dev_set_allmulti(dev, 1);
716 if (err) {
717 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700718 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700719 return err;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
722#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900723 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000724 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!dev)
726 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700727 err = dev_set_allmulti(dev, 1);
728 if (err) {
729 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700730 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700731 return err;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000734
735 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000737 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
738 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000739 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000740 dev_put(dev);
741 return -EADDRNOTAVAIL;
742 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000743 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000744 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!dev)
747 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700748 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700749 if (err) {
750 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700751 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754 default:
755 return -EINVAL;
756 }
757
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000758 in_dev = __in_dev_get_rtnl(dev);
759 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000760 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000762 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700763 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ip_rt_multicast_event(in_dev);
765
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000766 /* Fill in the VIF structures */
767
Jianjun Kongc354e122008-11-03 00:28:02 -0800768 v->rate_limit = vifc->vifc_rate_limit;
769 v->local = vifc->vifc_lcl_addr.s_addr;
770 v->remote = vifc->vifc_rmt_addr.s_addr;
771 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!mrtsock)
773 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800774 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 v->bytes_in = 0;
776 v->bytes_out = 0;
777 v->pkt_in = 0;
778 v->pkt_out = 0;
779 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000780 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 v->link = dev->iflink;
782
783 /* And finish update writing critical data */
784 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800785 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000787 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000788 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000790 if (vifi+1 > mrt->maxvif)
791 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 write_unlock_bh(&mrt_lock);
793 return 0;
794}
795
Eric Dumazeta8c94862010-10-01 16:15:08 +0000796/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000797static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000798 __be32 origin,
799 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Jianjun Kongc354e122008-11-03 00:28:02 -0800801 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct mfc_cache *c;
803
Eric Dumazeta8c94862010-10-01 16:15:08 +0000804 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000805 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
806 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000808 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/*
812 * Allocate a multicast cache entry
813 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000814static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Jianjun Kongc354e122008-11-03 00:28:02 -0800816 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000817
818 if (c)
819 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return c;
821}
822
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000823static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Jianjun Kongc354e122008-11-03 00:28:02 -0800825 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000826
827 if (c) {
828 skb_queue_head_init(&c->mfc_un.unres.unresolved);
829 c->mfc_un.unres.expires = jiffies + 10*HZ;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return c;
832}
833
834/*
835 * A cache entry has gone into a resolved state from queued
836 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900837
Patrick McHardy0c122952010-04-13 05:03:22 +0000838static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
839 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700842 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000844 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Jianjun Kongc354e122008-11-03 00:28:02 -0800846 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700847 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
849
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200850 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000851 nlh->nlmsg_len = skb_tail_pointer(skb) -
852 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } else {
854 nlh->nlmsg_type = NLMSG_ERROR;
855 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
856 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700857 e = NLMSG_DATA(nlh);
858 e->error = -EMSGSIZE;
859 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Thomas Graf2942e902006-08-15 00:30:25 -0700861
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000862 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000863 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000864 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867}
868
869/*
870 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
871 * expects the following bizarre scheme.
872 *
873 * Called under mrt_lock.
874 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900875
Patrick McHardy0c122952010-04-13 05:03:22 +0000876static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000877 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300880 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct igmphdr *igmp;
882 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000883 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 int ret;
885
886#ifdef CONFIG_IP_PIMSM
887 if (assert == IGMPMSG_WHOLEPKT)
888 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
889 else
890#endif
891 skb = alloc_skb(128, GFP_ATOMIC);
892
Stephen Hemminger132adf52007-03-08 20:44:43 -0800893 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -ENOBUFS;
895
896#ifdef CONFIG_IP_PIMSM
897 if (assert == IGMPMSG_WHOLEPKT) {
898 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000899 * Duplicate old header, fix ihl, length etc.
900 * And all this only to mangle msg->im_msgtype and
901 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300903 skb_push(skb, sizeof(struct iphdr));
904 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300905 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300906 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700907 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 msg->im_msgtype = IGMPMSG_WHOLEPKT;
909 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000910 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700911 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
912 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
913 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900914 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900916 {
917
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000918 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700920 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300921 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300922 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000923 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700924 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000926 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000928 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000930 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 igmp->type =
932 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000933 igmp->code = 0;
934 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700935 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Eric Dumazet4c968702010-10-01 16:15:01 +0000938 rcu_read_lock();
939 mroute_sk = rcu_dereference(mrt->mroute_sk);
940 if (mroute_sk == NULL) {
941 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 kfree_skb(skb);
943 return -EINVAL;
944 }
945
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000946 /* Deliver to mrouted */
947
Eric Dumazet4c968702010-10-01 16:15:01 +0000948 ret = sock_queue_rcv_skb(mroute_sk, skb);
949 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +0000950 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (net_ratelimit())
952 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
953 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)--;
Eric Dumazet4c968702010-10-01 16:15:01 +00001178 rcu_assign_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 Dumazet4c968702010-10-01 16:15:01 +00001205 if (sk != rcu_dereference_raw(mrt->mroute_sk) &&
1206 !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 Dumazet4c968702010-10-01 16:15:01 +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 Dumazet4c968702010-10-01 16:15:01 +00001232 if (sk != rcu_dereference_raw(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
1447int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1448{
1449 struct sioc_sg_req sr;
1450 struct mfc_cache *c;
1451 struct net *net = sock_net(sk);
1452 struct mr_table *mrt;
1453
1454 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1455 if (mrt == NULL)
1456 return -ENOENT;
1457
1458 switch (cmd) {
1459 case SIOCGETSGCNT:
1460 if (copy_from_user(&sr, arg, sizeof(sr)))
1461 return -EFAULT;
1462
1463 rcu_read_lock();
1464 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1465 if (c) {
1466 sr.pktcnt = c->mfc_un.res.pkt;
1467 sr.bytecnt = c->mfc_un.res.bytes;
1468 sr.wrong_if = c->mfc_un.res.wrong_if;
1469 rcu_read_unlock();
1470
1471 if (copy_to_user(arg, &sr, sizeof(sr)))
1472 return -EFAULT;
1473 return 0;
1474 }
1475 rcu_read_unlock();
1476 return -EADDRNOTAVAIL;
1477 default:
1478 return -ENOIOCTLCMD;
1479 }
1480}
1481#endif
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1485{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001486 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001487 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001488 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct vif_device *v;
1490 int ct;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001491 LIST_HEAD(list);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (event != NETDEV_UNREGISTER)
1494 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001495
1496 ipmr_for_each_table(mrt, net) {
1497 v = &mrt->vif_table[0];
1498 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1499 if (v->dev == dev)
1500 vif_delete(mrt, ct, 1, &list);
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001503 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return NOTIFY_DONE;
1505}
1506
1507
Jianjun Kongc354e122008-11-03 00:28:02 -08001508static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 .notifier_call = ipmr_device_event,
1510};
1511
1512/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001513 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 * This avoids tunnel drivers and other mess and gives us the speed so
1515 * important for multicast video.
1516 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001517
Al Viro114c7842006-09-27 18:39:29 -07001518static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001520 struct iphdr *iph;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001521 struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001522
1523 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001524 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001525 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001526 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001528 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001529 iph->tos = old_iph->tos;
1530 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 iph->frag_off = 0;
1532 iph->daddr = daddr;
1533 iph->saddr = saddr;
1534 iph->protocol = IPPROTO_IPIP;
1535 iph->ihl = 5;
1536 iph->tot_len = htons(skb->len);
Eric Dumazetadf30902009-06-02 05:19:30 +00001537 ip_select_ident(iph, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 ip_send_check(iph);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1541 nf_reset(skb);
1542}
1543
1544static inline int ipmr_forward_finish(struct sk_buff *skb)
1545{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001546 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Eric Dumazetadf30902009-06-02 05:19:30 +00001548 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 if (unlikely(opt->optlen))
1551 ip_forward_options(skb);
1552
1553 return dst_output(skb);
1554}
1555
1556/*
1557 * Processing handlers for ipmr_forward
1558 */
1559
Patrick McHardy0c122952010-04-13 05:03:22 +00001560static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1561 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001563 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001564 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 struct net_device *dev;
1566 struct rtable *rt;
1567 int encap = 0;
1568
1569 if (vif->dev == NULL)
1570 goto out_free;
1571
1572#ifdef CONFIG_IP_PIMSM
1573 if (vif->flags & VIFF_REGISTER) {
1574 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001575 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001576 vif->dev->stats.tx_bytes += skb->len;
1577 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001578 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001579 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581#endif
1582
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001583 if (vif->flags & VIFF_TUNNEL) {
1584 struct flowi fl = {
1585 .oif = vif->link,
Changli Gao58116622010-11-12 18:43:55 +00001586 .fl4_dst = vif->remote,
1587 .fl4_src = vif->local,
1588 .fl4_tos = RT_TOS(iph->tos),
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001589 .proto = IPPROTO_IPIP
1590 };
1591
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001592 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 goto out_free;
1594 encap = sizeof(struct iphdr);
1595 } else {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001596 struct flowi fl = {
1597 .oif = vif->link,
Changli Gao58116622010-11-12 18:43:55 +00001598 .fl4_dst = iph->daddr,
1599 .fl4_tos = RT_TOS(iph->tos),
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001600 .proto = IPPROTO_IPIP
1601 };
1602
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001603 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 goto out_free;
1605 }
1606
Changli Gaod8d1f302010-06-10 23:31:35 -07001607 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Changli Gaod8d1f302010-06-10 23:31:35 -07001609 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001611 * allow to send ICMP, so that packets will disappear
1612 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
1614
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001615 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 ip_rt_put(rt);
1617 goto out_free;
1618 }
1619
Changli Gaod8d1f302010-06-10 23:31:35 -07001620 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001623 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto out_free;
1625 }
1626
1627 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001628 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Eric Dumazetadf30902009-06-02 05:19:30 +00001630 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001631 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001632 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001635 * What do we do with netfilter? -- RR
1636 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (vif->flags & VIFF_TUNNEL) {
1638 ip_encap(skb, vif->local, vif->remote);
1639 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001640 vif->dev->stats.tx_packets++;
1641 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643
1644 IPCB(skb)->flags |= IPSKB_FORWARDED;
1645
1646 /*
1647 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1648 * not only before forwarding, but after forwarding on all output
1649 * interfaces. It is clear, if mrouter runs a multicasting
1650 * program, it should receive packets not depending to what interface
1651 * program is joined.
1652 * If we will not make it, the program will have to join on all
1653 * interfaces. On the other hand, multihoming host (or router, but
1654 * not mrouter) cannot join to more than one interface - it will
1655 * result in receiving multiple packets.
1656 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001657 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 ipmr_forward_finish);
1659 return;
1660
1661out_free:
1662 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Patrick McHardy0c122952010-04-13 05:03:22 +00001665static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001668
1669 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1670 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 break;
1672 }
1673 return ct;
1674}
1675
1676/* "local" means that we should preserve one skb (for local delivery) */
1677
Patrick McHardy0c122952010-04-13 05:03:22 +00001678static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1679 struct sk_buff *skb, struct mfc_cache *cache,
1680 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 int psend = -1;
1683 int vif, ct;
1684
1685 vif = cache->mfc_parent;
1686 cache->mfc_un.res.pkt++;
1687 cache->mfc_un.res.bytes += skb->len;
1688
1689 /*
1690 * Wrong interface: drop packet and (maybe) send PIM assert.
1691 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001692 if (mrt->vif_table[vif].dev != skb->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 int true_vifi;
1694
David S. Millerc7537962010-11-11 17:07:48 -08001695 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001697 * Very complicated situation...
1698 *
1699 * The best workaround until routing daemons will be
1700 * fixed is not to redistribute packet, if it was
1701 * send through wrong interface. It means, that
1702 * multicast applications WILL NOT work for
1703 * (S,G), which have default multicast route pointing
1704 * to wrong oif. In any case, it is not a good
1705 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
1707 goto dont_forward;
1708 }
1709
1710 cache->mfc_un.res.wrong_if++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001711 true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Patrick McHardy0c122952010-04-13 05:03:22 +00001713 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001715 * so that we cannot check that packet arrived on an oif.
1716 * It is bad, but otherwise we would need to move pretty
1717 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001719 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001720 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001721 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1723 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001724 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726 goto dont_forward;
1727 }
1728
Patrick McHardy0c122952010-04-13 05:03:22 +00001729 mrt->vif_table[vif].pkt_in++;
1730 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /*
1733 * Forward the frame
1734 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001735 for (ct = cache->mfc_un.res.maxvif - 1;
1736 ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001737 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (psend != -1) {
1739 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001742 ipmr_queue_xmit(net, mrt, skb2, cache,
1743 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001745 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747 }
1748 if (psend != -1) {
1749 if (local) {
1750 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001753 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001755 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return 0;
1757 }
1758 }
1759
1760dont_forward:
1761 if (!local)
1762 kfree_skb(skb);
1763 return 0;
1764}
1765
1766
1767/*
1768 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001769 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 */
1771
1772int ip_mr_input(struct sk_buff *skb)
1773{
1774 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001775 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001776 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001777 struct mr_table *mrt;
1778 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001781 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001783 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto dont_forward;
1785
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001786 err = ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001787 if (err < 0) {
1788 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001789 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +00001790 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001793 if (IPCB(skb)->opt.router_alert) {
1794 if (ip_call_ra_chain(skb))
1795 return 0;
1796 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1797 /* IGMPv1 (and broken IGMPv2 implementations sort of
1798 * Cisco IOS <= 11.2(8)) do not put router alert
1799 * option to IGMP packets destined to routable
1800 * groups. It is very bad, because it means
1801 * that we can forward NO IGMP messages.
1802 */
1803 struct sock *mroute_sk;
1804
1805 mroute_sk = rcu_dereference(mrt->mroute_sk);
1806 if (mroute_sk) {
1807 nf_reset(skb);
1808 raw_rcv(mroute_sk, skb);
1809 return 0;
1810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 }
1813
Eric Dumazeta8c94862010-10-01 16:15:08 +00001814 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001815 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 /*
1818 * No usable cache entry
1819 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001820 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int vif;
1822
1823 if (local) {
1824 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1825 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001826 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 skb = skb2;
1829 }
1830
Eric Dumazeta8c94862010-10-01 16:15:08 +00001831 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001832 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001834 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 read_unlock(&mrt_lock);
1836
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001837 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839 read_unlock(&mrt_lock);
1840 kfree_skb(skb);
1841 return -ENODEV;
1842 }
1843
Eric Dumazeta8c94862010-10-01 16:15:08 +00001844 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001845 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 read_unlock(&mrt_lock);
1847
1848 if (local)
1849 return ip_local_deliver(skb);
1850
1851 return 0;
1852
1853dont_forward:
1854 if (local)
1855 return ip_local_deliver(skb);
1856 kfree_skb(skb);
1857 return 0;
1858}
1859
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001860#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00001861/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001862static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1863 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001865 struct net_device *reg_dev = NULL;
1866 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001868 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001870 * Check that:
1871 * a. packet is really sent to a multicast group
1872 * b. packet is not a NULL-REGISTER
1873 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001875 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001877 ntohs(encap->tot_len) + pimlen > skb->len)
1878 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001881 if (mrt->mroute_reg_vif_num >= 0)
1882 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 read_unlock(&mrt_lock);
1884
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001885 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001886 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001888 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00001889 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001890 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00001892 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07001894
1895 skb_tunnel_rx(skb, reg_dev);
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001898
Eric Dumazet55747a02010-10-01 16:14:55 +00001899 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001900}
1901#endif
1902
1903#ifdef CONFIG_IP_PIMSM_V1
1904/*
1905 * Handle IGMP messages of PIMv1
1906 */
1907
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001908int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001909{
1910 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001911 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001912 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001913
1914 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1915 goto drop;
1916
1917 pim = igmp_hdr(skb);
1918
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001919 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1920 goto drop;
1921
Patrick McHardy0c122952010-04-13 05:03:22 +00001922 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001923 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1924 goto drop;
1925
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001926 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001927drop:
1928 kfree_skb(skb);
1929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return 0;
1931}
1932#endif
1933
1934#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001935static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001938 struct net *net = dev_net(skb->dev);
1939 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001941 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 goto drop;
1943
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001944 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001945 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1946 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001947 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08001948 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 goto drop;
1950
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001951 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1952 goto drop;
1953
1954 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001955drop:
1956 kfree_skb(skb);
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return 0;
1959}
1960#endif
1961
Patrick McHardycb6a4e42010-04-26 16:02:08 +02001962static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
1963 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 int ct;
1966 struct rtnexthop *nhp;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001967 u8 *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 struct rtattr *mp_head;
1969
Nicolas Dichtel74381892010-03-25 23:45:35 +00001970 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07001971 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00001972 return -ENOENT;
1973
Patrick McHardy0c122952010-04-13 05:03:22 +00001974 if (VIF_EXISTS(mrt, c->mfc_parent))
1975 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Jianjun Kongc354e122008-11-03 00:28:02 -08001977 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001980 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1982 goto rtattr_failure;
Jianjun Kongc354e122008-11-03 00:28:02 -08001983 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 nhp->rtnh_flags = 0;
1985 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00001986 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 nhp->rtnh_len = sizeof(*nhp);
1988 }
1989 }
1990 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001991 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 rtm->rtm_type = RTN_MULTICAST;
1993 return 1;
1994
1995rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001996 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return -EMSGSIZE;
1998}
1999
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002000int ipmr_get_route(struct net *net,
2001 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 int err;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002004 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct mfc_cache *cache;
Eric Dumazet511c3f92009-06-02 05:14:27 +00002006 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002008 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2009 if (mrt == NULL)
2010 return -ENOENT;
2011
Eric Dumazeta8c94862010-10-01 16:15:08 +00002012 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002013 cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Jianjun Kongc354e122008-11-03 00:28:02 -08002015 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002016 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002017 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002019 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002022 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return -EAGAIN;
2024 }
2025
2026 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002027 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002028 if (dev)
2029 vif = ipmr_find_vif(mrt, dev);
2030 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002032 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return -ENODEV;
2034 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002035 skb2 = skb_clone(skb, GFP_ATOMIC);
2036 if (!skb2) {
2037 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002038 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002039 return -ENOMEM;
2040 }
2041
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002042 skb_push(skb2, sizeof(struct iphdr));
2043 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002044 iph = ip_hdr(skb2);
2045 iph->ihl = sizeof(struct iphdr) >> 2;
2046 iph->saddr = rt->rt_src;
2047 iph->daddr = rt->rt_dst;
2048 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002049 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002051 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return err;
2053 }
2054
Eric Dumazeta8c94862010-10-01 16:15:08 +00002055 read_lock(&mrt_lock);
2056 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002058 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002060 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return err;
2062}
2063
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002064static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2065 u32 pid, u32 seq, struct mfc_cache *c)
2066{
2067 struct nlmsghdr *nlh;
2068 struct rtmsg *rtm;
2069
2070 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2071 if (nlh == NULL)
2072 return -EMSGSIZE;
2073
2074 rtm = nlmsg_data(nlh);
2075 rtm->rtm_family = RTNL_FAMILY_IPMR;
2076 rtm->rtm_dst_len = 32;
2077 rtm->rtm_src_len = 32;
2078 rtm->rtm_tos = 0;
2079 rtm->rtm_table = mrt->id;
2080 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2081 rtm->rtm_type = RTN_MULTICAST;
2082 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2083 rtm->rtm_protocol = RTPROT_UNSPEC;
2084 rtm->rtm_flags = 0;
2085
2086 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2087 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2088
2089 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2090 goto nla_put_failure;
2091
2092 return nlmsg_end(skb, nlh);
2093
2094nla_put_failure:
2095 nlmsg_cancel(skb, nlh);
2096 return -EMSGSIZE;
2097}
2098
2099static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2100{
2101 struct net *net = sock_net(skb->sk);
2102 struct mr_table *mrt;
2103 struct mfc_cache *mfc;
2104 unsigned int t = 0, s_t;
2105 unsigned int h = 0, s_h;
2106 unsigned int e = 0, s_e;
2107
2108 s_t = cb->args[0];
2109 s_h = cb->args[1];
2110 s_e = cb->args[2];
2111
Eric Dumazeta8c94862010-10-01 16:15:08 +00002112 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002113 ipmr_for_each_table(mrt, net) {
2114 if (t < s_t)
2115 goto next_table;
2116 if (t > s_t)
2117 s_h = 0;
2118 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002119 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002120 if (e < s_e)
2121 goto next_entry;
2122 if (ipmr_fill_mroute(mrt, skb,
2123 NETLINK_CB(cb->skb).pid,
2124 cb->nlh->nlmsg_seq,
2125 mfc) < 0)
2126 goto done;
2127next_entry:
2128 e++;
2129 }
2130 e = s_e = 0;
2131 }
2132 s_h = 0;
2133next_table:
2134 t++;
2135 }
2136done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002137 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002138
2139 cb->args[2] = e;
2140 cb->args[1] = h;
2141 cb->args[0] = t;
2142
2143 return skb->len;
2144}
2145
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002146#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002148 * The /proc interfaces to multicast routing :
2149 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 */
2151struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002152 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002153 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int ct;
2155};
2156
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002157static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2158 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 loff_t pos)
2160{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002161 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002162
2163 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2164 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002166 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002167 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169 return NULL;
2170}
2171
2172static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002173 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002175 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002176 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002177 struct mr_table *mrt;
2178
2179 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2180 if (mrt == NULL)
2181 return ERR_PTR(-ENOENT);
2182
2183 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002186 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 : SEQ_START_TOKEN;
2188}
2189
2190static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2191{
2192 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002193 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002194 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 ++*pos;
2197 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002198 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002199
Patrick McHardy0c122952010-04-13 05:03:22 +00002200 while (++iter->ct < mrt->maxvif) {
2201 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002203 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205 return NULL;
2206}
2207
2208static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002209 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
2211 read_unlock(&mrt_lock);
2212}
2213
2214static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2215{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002216 struct ipmr_vif_iter *iter = seq->private;
2217 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002220 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2222 } else {
2223 const struct vif_device *vif = v;
2224 const char *name = vif->dev ? vif->dev->name : "none";
2225
2226 seq_printf(seq,
2227 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002228 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002229 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 vif->bytes_out, vif->pkt_out,
2231 vif->flags, vif->local, vif->remote);
2232 }
2233 return 0;
2234}
2235
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002236static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 .start = ipmr_vif_seq_start,
2238 .next = ipmr_vif_seq_next,
2239 .stop = ipmr_vif_seq_stop,
2240 .show = ipmr_vif_seq_show,
2241};
2242
2243static int ipmr_vif_open(struct inode *inode, struct file *file)
2244{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002245 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2246 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
Arjan van de Ven9a321442007-02-12 00:55:35 -08002249static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 .owner = THIS_MODULE,
2251 .open = ipmr_vif_open,
2252 .read = seq_read,
2253 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002254 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255};
2256
2257struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002258 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002259 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002260 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 int ct;
2262};
2263
2264
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002265static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2266 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002268 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 struct mfc_cache *mfc;
2270
Eric Dumazeta8c94862010-10-01 16:15:08 +00002271 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002272 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002273 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002274 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002275 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002277 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002278 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002281 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002282 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002283 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return mfc;
2285 spin_unlock_bh(&mfc_unres_lock);
2286
2287 it->cache = NULL;
2288 return NULL;
2289}
2290
2291
2292static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2293{
2294 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002295 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002296 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002297
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002298 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2299 if (mrt == NULL)
2300 return ERR_PTR(-ENOENT);
2301
2302 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 it->cache = NULL;
2304 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002305 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 : SEQ_START_TOKEN;
2307}
2308
2309static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2310{
2311 struct mfc_cache *mfc = v;
2312 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002313 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002314 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 ++*pos;
2317
2318 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002319 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Patrick McHardy862465f2010-04-13 05:03:21 +00002321 if (mfc->list.next != it->cache)
2322 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002323
Patrick McHardy0c122952010-04-13 05:03:22 +00002324 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 goto end_of_list;
2326
Patrick McHardy0c122952010-04-13 05:03:22 +00002327 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002330 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002331 if (list_empty(it->cache))
2332 continue;
2333 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
2336 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002337 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002338 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002342 if (!list_empty(it->cache))
2343 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002345end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 spin_unlock_bh(&mfc_unres_lock);
2347 it->cache = NULL;
2348
2349 return NULL;
2350}
2351
2352static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2353{
2354 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002355 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Patrick McHardy0c122952010-04-13 05:03:22 +00002357 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002359 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002360 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
2363static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2364{
2365 int n;
2366
2367 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002368 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2370 } else {
2371 const struct mfc_cache *mfc = v;
2372 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002373 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002374
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002375 seq_printf(seq, "%08X %08X %-3hd",
2376 (__force u32) mfc->mfc_mcastgrp,
2377 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002378 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Patrick McHardy0c122952010-04-13 05:03:22 +00002380 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002381 seq_printf(seq, " %8lu %8lu %8lu",
2382 mfc->mfc_un.res.pkt,
2383 mfc->mfc_un.res.bytes,
2384 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002385 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002386 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002387 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002388 mfc->mfc_un.res.ttls[n] < 255)
2389 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002390 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 n, mfc->mfc_un.res.ttls[n]);
2392 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002393 } else {
2394 /* unresolved mfc_caches don't contain
2395 * pkt, bytes and wrong_if values
2396 */
2397 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 seq_putc(seq, '\n');
2400 }
2401 return 0;
2402}
2403
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002404static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 .start = ipmr_mfc_seq_start,
2406 .next = ipmr_mfc_seq_next,
2407 .stop = ipmr_mfc_seq_stop,
2408 .show = ipmr_mfc_seq_show,
2409};
2410
2411static int ipmr_mfc_open(struct inode *inode, struct file *file)
2412{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002413 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2414 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Arjan van de Ven9a321442007-02-12 00:55:35 -08002417static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 .owner = THIS_MODULE,
2419 .open = ipmr_mfc_open,
2420 .read = seq_read,
2421 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002422 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002427static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002429 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430};
2431#endif
2432
2433
2434/*
2435 * Setup for IP multicast routing
2436 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002437static int __net_init ipmr_net_init(struct net *net)
2438{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002439 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002440
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002441 err = ipmr_rules_init(net);
2442 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002443 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002444
2445#ifdef CONFIG_PROC_FS
2446 err = -ENOMEM;
2447 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2448 goto proc_vif_fail;
2449 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2450 goto proc_cache_fail;
2451#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002452 return 0;
2453
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002454#ifdef CONFIG_PROC_FS
2455proc_cache_fail:
2456 proc_net_remove(net, "ip_mr_vif");
2457proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002458 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002459#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002460fail:
2461 return err;
2462}
2463
2464static void __net_exit ipmr_net_exit(struct net *net)
2465{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002466#ifdef CONFIG_PROC_FS
2467 proc_net_remove(net, "ip_mr_cache");
2468 proc_net_remove(net, "ip_mr_vif");
2469#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002470 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002471}
2472
2473static struct pernet_operations ipmr_net_ops = {
2474 .init = ipmr_net_init,
2475 .exit = ipmr_net_exit,
2476};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002477
Wang Chen03d2f892008-07-03 12:13:36 +08002478int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
Wang Chen03d2f892008-07-03 12:13:36 +08002480 int err;
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2483 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002484 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002485 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002486 if (!mrt_cachep)
2487 return -ENOMEM;
2488
Benjamin Therycf958ae32009-01-22 04:56:16 +00002489 err = register_pernet_subsys(&ipmr_net_ops);
2490 if (err)
2491 goto reg_pernet_fail;
2492
Wang Chen03d2f892008-07-03 12:13:36 +08002493 err = register_netdevice_notifier(&ip_mr_notifier);
2494 if (err)
2495 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002496#ifdef CONFIG_IP_PIMSM_V2
2497 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2498 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2499 err = -EAGAIN;
2500 goto add_proto_fail;
2501 }
2502#endif
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002503 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, NULL, ipmr_rtm_dumproute);
Wang Chen03d2f892008-07-03 12:13:36 +08002504 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002505
Tom Goff403dbb92009-06-14 03:16:13 -07002506#ifdef CONFIG_IP_PIMSM_V2
2507add_proto_fail:
2508 unregister_netdevice_notifier(&ip_mr_notifier);
2509#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002510reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002511 unregister_pernet_subsys(&ipmr_net_ops);
2512reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002513 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002514 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}