blob: ef2b0089e0ea3ffad352f4ec1c3ea29ea7a42704 [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>
63#include <net/ipip.h>
64#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070065#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000066#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
69#define CONFIG_IP_PIMSM 1
70#endif
71
Patrick McHardy0c122952010-04-13 05:03:22 +000072struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000073 struct list_head list;
Patrick McHardy8de53df2010-04-15 13:29:28 +020074#ifdef CONFIG_NET_NS
75 struct net *net;
76#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +000077 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000078 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000079 struct timer_list ipmr_expire_timer;
80 struct list_head mfc_unres_queue;
81 struct list_head mfc_cache_array[MFC_LINES];
82 struct vif_device vif_table[MAXVIFS];
83 int maxvif;
84 atomic_t cache_resolve_queue_len;
85 int mroute_do_assert;
86 int mroute_do_pim;
87#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
88 int mroute_reg_vif_num;
89#endif
90};
91
Patrick McHardyf0ad0862010-04-13 05:03:23 +000092struct ipmr_rule {
93 struct fib_rule common;
94};
95
96struct ipmr_result {
97 struct mr_table *mrt;
98};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000101 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
103
104static DEFINE_RWLOCK(mrt_lock);
105
106/*
107 * Multicast router control variables
108 */
109
Patrick McHardy0c122952010-04-13 05:03:22 +0000110#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* Special spinlock for queue of unresolved entries */
113static DEFINE_SPINLOCK(mfc_unres_lock);
114
115/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000116 * entries is changed only in process context and protected
117 * with weak lock mrt_lock. Queue of unresolved entries is protected
118 * with strong spinlock mfc_unres_lock.
119 *
120 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122
Christoph Lametere18b8902006-12-06 20:33:20 -0800123static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000125static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Patrick McHardy0c122952010-04-13 05:03:22 +0000126static int ip_mr_forward(struct net *net, struct mr_table *mrt,
127 struct sk_buff *skb, struct mfc_cache *cache,
128 int local);
129static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000130 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200131static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
132 struct mfc_cache *c, struct rtmsg *rtm);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000133static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000135#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
136#define ipmr_for_each_table(mrt, net) \
137 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
138
139static struct mr_table *ipmr_get_table(struct net *net, u32 id)
140{
141 struct mr_table *mrt;
142
143 ipmr_for_each_table(mrt, net) {
144 if (mrt->id == id)
145 return mrt;
146 }
147 return NULL;
148}
149
150static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
151 struct mr_table **mrt)
152{
153 struct ipmr_result res;
154 struct fib_lookup_arg arg = { .result = &res, };
155 int err;
156
157 err = fib_rules_lookup(net->ipv4.mr_rules_ops, flp, 0, &arg);
158 if (err < 0)
159 return err;
160 *mrt = res.mrt;
161 return 0;
162}
163
164static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
165 int flags, struct fib_lookup_arg *arg)
166{
167 struct ipmr_result *res = arg->result;
168 struct mr_table *mrt;
169
170 switch (rule->action) {
171 case FR_ACT_TO_TBL:
172 break;
173 case FR_ACT_UNREACHABLE:
174 return -ENETUNREACH;
175 case FR_ACT_PROHIBIT:
176 return -EACCES;
177 case FR_ACT_BLACKHOLE:
178 default:
179 return -EINVAL;
180 }
181
182 mrt = ipmr_get_table(rule->fr_net, rule->table);
183 if (mrt == NULL)
184 return -EAGAIN;
185 res->mrt = mrt;
186 return 0;
187}
188
189static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
190{
191 return 1;
192}
193
194static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
195 FRA_GENERIC_POLICY,
196};
197
198static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
199 struct fib_rule_hdr *frh, struct nlattr **tb)
200{
201 return 0;
202}
203
204static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
205 struct nlattr **tb)
206{
207 return 1;
208}
209
210static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
211 struct fib_rule_hdr *frh)
212{
213 frh->dst_len = 0;
214 frh->src_len = 0;
215 frh->tos = 0;
216 return 0;
217}
218
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200219static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200220 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000221 .rule_size = sizeof(struct ipmr_rule),
222 .addr_size = sizeof(u32),
223 .action = ipmr_rule_action,
224 .match = ipmr_rule_match,
225 .configure = ipmr_rule_configure,
226 .compare = ipmr_rule_compare,
227 .default_pref = fib_default_rule_pref,
228 .fill = ipmr_rule_fill,
229 .nlgroup = RTNLGRP_IPV4_RULE,
230 .policy = ipmr_rule_policy,
231 .owner = THIS_MODULE,
232};
233
234static int __net_init ipmr_rules_init(struct net *net)
235{
236 struct fib_rules_ops *ops;
237 struct mr_table *mrt;
238 int err;
239
240 ops = fib_rules_register(&ipmr_rules_ops_template, net);
241 if (IS_ERR(ops))
242 return PTR_ERR(ops);
243
244 INIT_LIST_HEAD(&net->ipv4.mr_tables);
245
246 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
247 if (mrt == NULL) {
248 err = -ENOMEM;
249 goto err1;
250 }
251
252 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
253 if (err < 0)
254 goto err2;
255
256 net->ipv4.mr_rules_ops = ops;
257 return 0;
258
259err2:
260 kfree(mrt);
261err1:
262 fib_rules_unregister(ops);
263 return err;
264}
265
266static void __net_exit ipmr_rules_exit(struct net *net)
267{
268 struct mr_table *mrt, *next;
269
Eric Dumazet035320d2010-06-06 23:48:40 +0000270 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
271 list_del(&mrt->list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000272 kfree(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000273 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000274 fib_rules_unregister(net->ipv4.mr_rules_ops);
275}
276#else
277#define ipmr_for_each_table(mrt, net) \
278 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
279
280static struct mr_table *ipmr_get_table(struct net *net, u32 id)
281{
282 return net->ipv4.mrt;
283}
284
285static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
286 struct mr_table **mrt)
287{
288 *mrt = net->ipv4.mrt;
289 return 0;
290}
291
292static int __net_init ipmr_rules_init(struct net *net)
293{
294 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
295 return net->ipv4.mrt ? 0 : -ENOMEM;
296}
297
298static void __net_exit ipmr_rules_exit(struct net *net)
299{
300 kfree(net->ipv4.mrt);
301}
302#endif
303
304static struct mr_table *ipmr_new_table(struct net *net, u32 id)
305{
306 struct mr_table *mrt;
307 unsigned int i;
308
309 mrt = ipmr_get_table(net, id);
310 if (mrt != NULL)
311 return mrt;
312
313 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
314 if (mrt == NULL)
315 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200316 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000317 mrt->id = id;
318
319 /* Forwarding cache */
320 for (i = 0; i < MFC_LINES; i++)
321 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
322
323 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
324
325 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
326 (unsigned long)mrt);
327
328#ifdef CONFIG_IP_PIMSM
329 mrt->mroute_reg_vif_num = -1;
330#endif
331#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
332 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
333#endif
334 return mrt;
335}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
338
Wang Chend6070322008-07-14 20:55:26 -0700339static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
340{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000341 struct net *net = dev_net(dev);
342
Wang Chend6070322008-07-14 20:55:26 -0700343 dev_close(dev);
344
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000345 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700346 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800347 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700348 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700349 struct ip_tunnel_parm p;
350
351 memset(&p, 0, sizeof(p));
352 p.iph.daddr = v->vifc_rmt_addr.s_addr;
353 p.iph.saddr = v->vifc_lcl_addr.s_addr;
354 p.iph.version = 4;
355 p.iph.ihl = 5;
356 p.iph.protocol = IPPROTO_IPIP;
357 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
358 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
359
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800360 if (ops->ndo_do_ioctl) {
361 mm_segment_t oldfs = get_fs();
362
363 set_fs(KERNEL_DS);
364 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
365 set_fs(oldfs);
366 }
Wang Chend6070322008-07-14 20:55:26 -0700367 }
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000371struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct net_device *dev;
374
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000375 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800378 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int err;
380 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct ip_tunnel_parm p;
382 struct in_device *in_dev;
383
384 memset(&p, 0, sizeof(p));
385 p.iph.daddr = v->vifc_rmt_addr.s_addr;
386 p.iph.saddr = v->vifc_lcl_addr.s_addr;
387 p.iph.version = 4;
388 p.iph.ihl = 5;
389 p.iph.protocol = IPPROTO_IPIP;
390 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800391 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800393 if (ops->ndo_do_ioctl) {
394 mm_segment_t oldfs = get_fs();
395
396 set_fs(KERNEL_DS);
397 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
398 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000399 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800400 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 dev = NULL;
403
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000404 if (err == 0 &&
405 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 dev->flags |= IFF_MULTICAST;
407
Herbert Xue5ed6392005-10-03 14:35:55 -0700408 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700409 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700411
412 ipv4_devconf_setall(in_dev);
413 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 if (dev_open(dev))
416 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700417 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 }
420 return dev;
421
422failure:
423 /* allow the register to be completed before unregistering. */
424 rtnl_unlock();
425 rtnl_lock();
426
427 unregister_netdevice(dev);
428 return NULL;
429}
430
431#ifdef CONFIG_IP_PIMSM
432
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000433static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000435 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000436 struct mr_table *mrt;
437 struct flowi fl = {
438 .oif = dev->ifindex,
439 .iif = skb->skb_iif,
440 .mark = skb->mark,
441 };
442 int err;
443
444 err = ipmr_fib_lookup(net, &fl, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000445 if (err < 0) {
446 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000447 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000448 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700451 dev->stats.tx_bytes += skb->len;
452 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000453 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 read_unlock(&mrt_lock);
455 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000456 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Stephen Hemminger007c3832008-11-20 20:28:35 -0800459static const struct net_device_ops reg_vif_netdev_ops = {
460 .ndo_start_xmit = reg_vif_xmit,
461};
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static void reg_vif_setup(struct net_device *dev)
464{
465 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800466 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800468 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700470 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000473static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct net_device *dev;
476 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000477 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000479 if (mrt->id == RT_TABLE_DEFAULT)
480 sprintf(name, "pimreg");
481 else
482 sprintf(name, "pimreg%u", mrt->id);
483
484 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (dev == NULL)
487 return NULL;
488
Tom Goff403dbb92009-06-14 03:16:13 -0700489 dev_net_set(dev, net);
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (register_netdevice(dev)) {
492 free_netdev(dev);
493 return NULL;
494 }
495 dev->iflink = 0;
496
Herbert Xu71e27da2007-06-04 23:36:06 -0700497 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000498 in_dev = __in_dev_get_rcu(dev);
499 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700500 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Herbert Xu71e27da2007-06-04 23:36:06 -0700504 ipv4_devconf_setall(in_dev);
505 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
506 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (dev_open(dev))
509 goto failure;
510
Wang Chen7dc00c82008-07-14 20:56:34 -0700511 dev_hold(dev);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return dev;
514
515failure:
516 /* allow the register to be completed before unregistering. */
517 rtnl_unlock();
518 rtnl_lock();
519
520 unregister_netdevice(dev);
521 return NULL;
522}
523#endif
524
525/*
526 * Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700527 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900529
Patrick McHardy0c122952010-04-13 05:03:22 +0000530static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000531 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct vif_device *v;
534 struct net_device *dev;
535 struct in_device *in_dev;
536
Patrick McHardy0c122952010-04-13 05:03:22 +0000537 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -EADDRNOTAVAIL;
539
Patrick McHardy0c122952010-04-13 05:03:22 +0000540 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 write_lock_bh(&mrt_lock);
543 dev = v->dev;
544 v->dev = NULL;
545
546 if (!dev) {
547 write_unlock_bh(&mrt_lock);
548 return -EADDRNOTAVAIL;
549 }
550
551#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000552 if (vifi == mrt->mroute_reg_vif_num)
553 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
555
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000556 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000558
559 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000560 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
562 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000563 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 write_unlock_bh(&mrt_lock);
567
568 dev_set_allmulti(dev, -1);
569
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000570 in_dev = __in_dev_get_rtnl(dev);
571 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700572 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 ip_rt_multicast_event(in_dev);
574 }
575
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000576 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000577 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 dev_put(dev);
580 return 0;
581}
582
Eric Dumazeta8c94862010-10-01 16:15:08 +0000583static void ipmr_cache_free_rcu(struct rcu_head *head)
584{
585 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
586
587 kmem_cache_free(mrt_cachep, c);
588}
589
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000590static inline void ipmr_cache_free(struct mfc_cache *c)
591{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000592 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000596 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
598
Patrick McHardy0c122952010-04-13 05:03:22 +0000599static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200601 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700603 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Patrick McHardy0c122952010-04-13 05:03:22 +0000605 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Jianjun Kongc354e122008-11-03 00:28:02 -0800607 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700608 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
610 nlh->nlmsg_type = NLMSG_ERROR;
611 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
612 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700613 e = NLMSG_DATA(nlh);
614 e->error = -ETIMEDOUT;
615 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700616
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000617 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000618 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000623 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626
Patrick McHardye258beb2010-04-13 05:03:19 +0000627/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Patrick McHardye258beb2010-04-13 05:03:19 +0000629static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Patrick McHardy0c122952010-04-13 05:03:22 +0000631 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 unsigned long now;
633 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000634 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000637 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return;
639 }
640
Patrick McHardy0c122952010-04-13 05:03:22 +0000641 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto out;
643
644 now = jiffies;
645 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Patrick McHardy0c122952010-04-13 05:03:22 +0000647 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (time_after(c->mfc_un.unres.expires, now)) {
649 unsigned long interval = c->mfc_un.unres.expires - now;
650 if (interval < expires)
651 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 continue;
653 }
654
Patrick McHardy862465f2010-04-13 05:03:21 +0000655 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000656 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Patrick McHardy0c122952010-04-13 05:03:22 +0000659 if (!list_empty(&mrt->mfc_unres_queue))
660 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662out:
663 spin_unlock(&mfc_unres_lock);
664}
665
666/* Fill oifs list. It is called under write locked mrt_lock. */
667
Patrick McHardy0c122952010-04-13 05:03:22 +0000668static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000669 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 int vifi;
672
673 cache->mfc_un.res.minvif = MAXVIFS;
674 cache->mfc_un.res.maxvif = 0;
675 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
676
Patrick McHardy0c122952010-04-13 05:03:22 +0000677 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
678 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000679 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
681 if (cache->mfc_un.res.minvif > vifi)
682 cache->mfc_un.res.minvif = vifi;
683 if (cache->mfc_un.res.maxvif <= vifi)
684 cache->mfc_un.res.maxvif = vifi + 1;
685 }
686 }
687}
688
Patrick McHardy0c122952010-04-13 05:03:22 +0000689static int vif_add(struct net *net, struct mr_table *mrt,
690 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000693 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct net_device *dev;
695 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700696 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000699 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -EADDRINUSE;
701
702 switch (vifc->vifc_flags) {
703#ifdef CONFIG_IP_PIMSM
704 case VIFF_REGISTER:
705 /*
706 * Special Purpose VIF in PIM
707 * All the packets will be sent to the daemon
708 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000709 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000711 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!dev)
713 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700714 err = dev_set_allmulti(dev, 1);
715 if (err) {
716 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700717 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700718 return err;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
721#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900722 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000723 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!dev)
725 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700726 err = dev_set_allmulti(dev, 1);
727 if (err) {
728 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700729 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700730 return err;
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000733
734 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000736 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
737 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000738 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000739 dev_put(dev);
740 return -EADDRNOTAVAIL;
741 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000742 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000743 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (!dev)
746 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700747 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700748 if (err) {
749 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700750 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
753 default:
754 return -EINVAL;
755 }
756
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000757 in_dev = __in_dev_get_rtnl(dev);
758 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000759 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000761 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700762 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ip_rt_multicast_event(in_dev);
764
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000765 /* Fill in the VIF structures */
766
Jianjun Kongc354e122008-11-03 00:28:02 -0800767 v->rate_limit = vifc->vifc_rate_limit;
768 v->local = vifc->vifc_lcl_addr.s_addr;
769 v->remote = vifc->vifc_rmt_addr.s_addr;
770 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!mrtsock)
772 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800773 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 v->bytes_in = 0;
775 v->bytes_out = 0;
776 v->pkt_in = 0;
777 v->pkt_out = 0;
778 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000779 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 v->link = dev->iflink;
781
782 /* And finish update writing critical data */
783 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800784 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000786 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000787 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000789 if (vifi+1 > mrt->maxvif)
790 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 write_unlock_bh(&mrt_lock);
792 return 0;
793}
794
Eric Dumazeta8c94862010-10-01 16:15:08 +0000795/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000796static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000797 __be32 origin,
798 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Jianjun Kongc354e122008-11-03 00:28:02 -0800800 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct mfc_cache *c;
802
Eric Dumazeta8c94862010-10-01 16:15:08 +0000803 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000804 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
805 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000807 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810/*
811 * Allocate a multicast cache entry
812 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000813static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Jianjun Kongc354e122008-11-03 00:28:02 -0800815 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000816
817 if (c)
818 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return c;
820}
821
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000822static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Jianjun Kongc354e122008-11-03 00:28:02 -0800824 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000825
826 if (c) {
827 skb_queue_head_init(&c->mfc_un.unres.unresolved);
828 c->mfc_un.unres.expires = jiffies + 10*HZ;
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return c;
831}
832
833/*
834 * A cache entry has gone into a resolved state from queued
835 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900836
Patrick McHardy0c122952010-04-13 05:03:22 +0000837static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
838 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700841 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000843 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Jianjun Kongc354e122008-11-03 00:28:02 -0800845 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700846 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
848
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200849 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000850 nlh->nlmsg_len = skb_tail_pointer(skb) -
851 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 } else {
853 nlh->nlmsg_type = NLMSG_ERROR;
854 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
855 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700856 e = NLMSG_DATA(nlh);
857 e->error = -EMSGSIZE;
858 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Thomas Graf2942e902006-08-15 00:30:25 -0700860
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000861 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000862 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000863 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866}
867
868/*
869 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
870 * expects the following bizarre scheme.
871 *
872 * Called under mrt_lock.
873 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900874
Patrick McHardy0c122952010-04-13 05:03:22 +0000875static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000876 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300879 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct igmphdr *igmp;
881 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000882 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 int ret;
884
885#ifdef CONFIG_IP_PIMSM
886 if (assert == IGMPMSG_WHOLEPKT)
887 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
888 else
889#endif
890 skb = alloc_skb(128, GFP_ATOMIC);
891
Stephen Hemminger132adf52007-03-08 20:44:43 -0800892 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -ENOBUFS;
894
895#ifdef CONFIG_IP_PIMSM
896 if (assert == IGMPMSG_WHOLEPKT) {
897 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000898 * Duplicate old header, fix ihl, length etc.
899 * And all this only to mangle msg->im_msgtype and
900 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300902 skb_push(skb, sizeof(struct iphdr));
903 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300904 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300905 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700906 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 msg->im_msgtype = IGMPMSG_WHOLEPKT;
908 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000909 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700910 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
911 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
912 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900913 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900915 {
916
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000917 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700919 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300920 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300921 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000922 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700923 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000925 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000927 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000929 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 igmp->type =
931 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000932 igmp->code = 0;
933 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700934 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Eric Dumazet4c968702010-10-01 16:15:01 +0000937 rcu_read_lock();
938 mroute_sk = rcu_dereference(mrt->mroute_sk);
939 if (mroute_sk == NULL) {
940 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 kfree_skb(skb);
942 return -EINVAL;
943 }
944
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000945 /* Deliver to mrouted */
946
Eric Dumazet4c968702010-10-01 16:15:01 +0000947 ret = sock_queue_rcv_skb(mroute_sk, skb);
948 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +0000949 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (net_ratelimit())
951 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
952 kfree_skb(skb);
953 }
954
955 return ret;
956}
957
958/*
959 * Queue a packet for resolution. It gets locked cache entry!
960 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962static int
Patrick McHardy0c122952010-04-13 05:03:22 +0000963ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Patrick McHardy862465f2010-04-13 05:03:21 +0000965 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int err;
967 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700968 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000971 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000972 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000973 c->mfc_origin == iph->saddr) {
974 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978
Patrick McHardy862465f2010-04-13 05:03:21 +0000979 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000980 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Patrick McHardy0c122952010-04-13 05:03:22 +0000982 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000983 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 spin_unlock_bh(&mfc_unres_lock);
985
986 kfree_skb(skb);
987 return -ENOBUFS;
988 }
989
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000990 /* Fill in the new cache entry */
991
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700992 c->mfc_parent = -1;
993 c->mfc_origin = iph->saddr;
994 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000996 /* Reflect first query at mrouted. */
997
Patrick McHardy0c122952010-04-13 05:03:22 +0000998 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000999 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001000 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 out - Brad Parker
1002 */
1003 spin_unlock_bh(&mfc_unres_lock);
1004
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001005 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 kfree_skb(skb);
1007 return err;
1008 }
1009
Patrick McHardy0c122952010-04-13 05:03:22 +00001010 atomic_inc(&mrt->cache_resolve_queue_len);
1011 list_add(&c->list, &mrt->mfc_unres_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
David S. Miller278554b2010-05-12 00:05:35 -07001013 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1014 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001017 /* See if we can append the packet */
1018
1019 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 kfree_skb(skb);
1021 err = -ENOBUFS;
1022 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001023 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 err = 0;
1025 }
1026
1027 spin_unlock_bh(&mfc_unres_lock);
1028 return err;
1029}
1030
1031/*
1032 * MFC cache manipulation by user space mroute daemon
1033 */
1034
Patrick McHardy0c122952010-04-13 05:03:22 +00001035static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001038 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Jianjun Kongc354e122008-11-03 00:28:02 -08001040 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Patrick McHardy0c122952010-04-13 05:03:22 +00001042 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1044 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001045 list_del_rcu(&c->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001047 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return 0;
1049 }
1050 }
1051 return -ENOENT;
1052}
1053
Patrick McHardy0c122952010-04-13 05:03:22 +00001054static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1055 struct mfcctl *mfc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Patrick McHardy862465f2010-04-13 05:03:21 +00001057 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001059 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Patrick McHardya50436f22010-03-17 06:04:14 +00001061 if (mfc->mfcc_parent >= MAXVIFS)
1062 return -ENFILE;
1063
Jianjun Kongc354e122008-11-03 00:28:02 -08001064 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Patrick McHardy0c122952010-04-13 05:03:22 +00001066 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001068 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1069 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
Patrick McHardy862465f2010-04-13 05:03:21 +00001074 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 write_lock_bh(&mrt_lock);
1076 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001077 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!mrtsock)
1079 c->mfc_flags |= MFC_STATIC;
1080 write_unlock_bh(&mrt_lock);
1081 return 0;
1082 }
1083
Joe Perchesf97c1e02007-12-16 13:45:43 -08001084 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -EINVAL;
1086
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001087 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001088 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return -ENOMEM;
1090
Jianjun Kongc354e122008-11-03 00:28:02 -08001091 c->mfc_origin = mfc->mfcc_origin.s_addr;
1092 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1093 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001094 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!mrtsock)
1096 c->mfc_flags |= MFC_STATIC;
1097
Eric Dumazeta8c94862010-10-01 16:15:08 +00001098 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /*
1101 * Check to see if we resolved a queued list. If so we
1102 * need to send on the frames and tidy up.
1103 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001104 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001106 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001107 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001109 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001110 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001111 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 break;
1113 }
1114 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001115 if (list_empty(&mrt->mfc_unres_queue))
1116 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 spin_unlock_bh(&mfc_unres_lock);
1118
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001119 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001120 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001121 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123 return 0;
1124}
1125
1126/*
1127 * Close the multicast socket, and clear the vif tables etc
1128 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001129
Patrick McHardy0c122952010-04-13 05:03:22 +00001130static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001133 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001134 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001135
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001136 /* Shut down all active vif entries */
1137
Patrick McHardy0c122952010-04-13 05:03:22 +00001138 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001139 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001140 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001142 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001144 /* Wipe the cache */
1145
Patrick McHardy862465f2010-04-13 05:03:21 +00001146 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001147 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001148 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001150 list_del_rcu(&c->list);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001151 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153 }
1154
Patrick McHardy0c122952010-04-13 05:03:22 +00001155 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001157 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001158 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001159 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161 spin_unlock_bh(&mfc_unres_lock);
1162 }
1163}
1164
Eric Dumazet4c968702010-10-01 16:15:01 +00001165/* called from ip_ra_control(), before an RCU grace period,
1166 * we dont need to call synchronize_rcu() here
1167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168static void mrtsock_destruct(struct sock *sk)
1169{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001170 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001171 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001174 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001175 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001176 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Eric Dumazet4c968702010-10-01 16:15:01 +00001177 rcu_assign_pointer(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001178 mroute_clean_tables(mrt);
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 rtnl_unlock();
1182}
1183
1184/*
1185 * Socket options and virtual interface manipulation. The whole
1186 * virtual interface system is a complete heap, but unfortunately
1187 * that's how BSD mrouted happens to think. Maybe one day with a proper
1188 * MOSPF/PIM router set up we can clean this up.
1189 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001190
David S. Millerb7058842009-09-30 16:12:20 -07001191int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 int ret;
1194 struct vifctl vif;
1195 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001196 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001197 struct mr_table *mrt;
1198
1199 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1200 if (mrt == NULL)
1201 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001202
Stephen Hemminger132adf52007-03-08 20:44:43 -08001203 if (optname != MRT_INIT) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001204 if (sk != rcu_dereference_raw(mrt->mroute_sk) &&
1205 !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return -EACCES;
1207 }
1208
Stephen Hemminger132adf52007-03-08 20:44:43 -08001209 switch (optname) {
1210 case MRT_INIT:
1211 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001212 inet_sk(sk)->inet_num != IPPROTO_IGMP)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001213 return -EOPNOTSUPP;
Jianjun Kongc354e122008-11-03 00:28:02 -08001214 if (optlen != sizeof(int))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001215 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Stephen Hemminger132adf52007-03-08 20:44:43 -08001217 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001218 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001220 return -EADDRINUSE;
1221 }
1222
1223 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1224 if (ret == 0) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001225 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001226 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001227 }
1228 rtnl_unlock();
1229 return ret;
1230 case MRT_DONE:
Eric Dumazet4c968702010-10-01 16:15:01 +00001231 if (sk != rcu_dereference_raw(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001232 return -EACCES;
1233 return ip_ra_control(sk, 0, NULL);
1234 case MRT_ADD_VIF:
1235 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001236 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001237 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001238 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001239 return -EFAULT;
1240 if (vif.vifc_vifi >= MAXVIFS)
1241 return -ENFILE;
1242 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001243 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001244 ret = vif_add(net, mrt, &vif,
1245 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001246 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001247 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001248 }
1249 rtnl_unlock();
1250 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /*
1253 * Manipulate the forwarding caches. These live
1254 * in a sort of kernel/user symbiosis.
1255 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001256 case MRT_ADD_MFC:
1257 case MRT_DEL_MFC:
Jianjun Kongc354e122008-11-03 00:28:02 -08001258 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001259 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001260 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001261 return -EFAULT;
1262 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001263 if (optname == MRT_DEL_MFC)
Patrick McHardy0c122952010-04-13 05:03:22 +00001264 ret = ipmr_mfc_delete(mrt, &mfc);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001265 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001266 ret = ipmr_mfc_add(net, mrt, &mfc,
1267 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001268 rtnl_unlock();
1269 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Control PIM assert.
1272 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001273 case MRT_ASSERT:
1274 {
1275 int v;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001276 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001277 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001278 mrt->mroute_do_assert = (v) ? 1 : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001279 return 0;
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001282 case MRT_PIM:
1283 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001284 int v;
1285
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001286 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001287 return -EFAULT;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001288 v = (v) ? 1 : 0;
1289
Stephen Hemminger132adf52007-03-08 20:44:43 -08001290 rtnl_lock();
1291 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001292 if (v != mrt->mroute_do_pim) {
1293 mrt->mroute_do_pim = v;
1294 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001296 rtnl_unlock();
1297 return ret;
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001300#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1301 case MRT_TABLE:
1302 {
1303 u32 v;
1304
1305 if (optlen != sizeof(u32))
1306 return -EINVAL;
1307 if (get_user(v, (u32 __user *)optval))
1308 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001309
1310 rtnl_lock();
1311 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001312 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1313 ret = -EBUSY;
1314 } else {
1315 if (!ipmr_new_table(net, v))
1316 ret = -ENOMEM;
1317 raw_sk(sk)->ipmr_table = v;
1318 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001319 rtnl_unlock();
1320 return ret;
1321 }
1322#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001323 /*
1324 * Spurious command, or MRT_VERSION which you cannot
1325 * set.
1326 */
1327 default:
1328 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330}
1331
1332/*
1333 * Getsock opt support for the multicast routing system.
1334 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001335
Jianjun Kongc354e122008-11-03 00:28:02 -08001336int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 int olr;
1339 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001340 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001341 struct mr_table *mrt;
1342
1343 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1344 if (mrt == NULL)
1345 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Jianjun Kongc354e122008-11-03 00:28:02 -08001347 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001349 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001351 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return -ENOPROTOOPT;
1353
1354 if (get_user(olr, optlen))
1355 return -EFAULT;
1356
1357 olr = min_t(unsigned int, olr, sizeof(int));
1358 if (olr < 0)
1359 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001360
Jianjun Kongc354e122008-11-03 00:28:02 -08001361 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001363 if (optname == MRT_VERSION)
1364 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001366 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001367 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368#endif
1369 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001370 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001371 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return -EFAULT;
1373 return 0;
1374}
1375
1376/*
1377 * The IP multicast ioctl support routines.
1378 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1381{
1382 struct sioc_sg_req sr;
1383 struct sioc_vif_req vr;
1384 struct vif_device *vif;
1385 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001386 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001387 struct mr_table *mrt;
1388
1389 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1390 if (mrt == NULL)
1391 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001392
Stephen Hemminger132adf52007-03-08 20:44:43 -08001393 switch (cmd) {
1394 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001395 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001396 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001397 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001398 return -EINVAL;
1399 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001400 vif = &mrt->vif_table[vr.vifi];
1401 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001402 vr.icount = vif->pkt_in;
1403 vr.ocount = vif->pkt_out;
1404 vr.ibytes = vif->bytes_in;
1405 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001407
Jianjun Kongc354e122008-11-03 00:28:02 -08001408 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001410 return 0;
1411 }
1412 read_unlock(&mrt_lock);
1413 return -EADDRNOTAVAIL;
1414 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001415 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001416 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Eric Dumazeta8c94862010-10-01 16:15:08 +00001418 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001419 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001420 if (c) {
1421 sr.pktcnt = c->mfc_un.res.pkt;
1422 sr.bytecnt = c->mfc_un.res.bytes;
1423 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001424 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001425
Jianjun Kongc354e122008-11-03 00:28:02 -08001426 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001427 return -EFAULT;
1428 return 0;
1429 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001430 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001431 return -EADDRNOTAVAIL;
1432 default:
1433 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435}
1436
1437
1438static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1439{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001440 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001441 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001442 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct vif_device *v;
1444 int ct;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001445 LIST_HEAD(list);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (event != NETDEV_UNREGISTER)
1448 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001449
1450 ipmr_for_each_table(mrt, net) {
1451 v = &mrt->vif_table[0];
1452 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1453 if (v->dev == dev)
1454 vif_delete(mrt, ct, 1, &list);
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001457 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return NOTIFY_DONE;
1459}
1460
1461
Jianjun Kongc354e122008-11-03 00:28:02 -08001462static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 .notifier_call = ipmr_device_event,
1464};
1465
1466/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001467 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 * This avoids tunnel drivers and other mess and gives us the speed so
1469 * important for multicast video.
1470 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001471
Al Viro114c7842006-09-27 18:39:29 -07001472static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001474 struct iphdr *iph;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001475 struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001476
1477 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001478 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001479 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001480 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001482 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001483 iph->tos = old_iph->tos;
1484 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 iph->frag_off = 0;
1486 iph->daddr = daddr;
1487 iph->saddr = saddr;
1488 iph->protocol = IPPROTO_IPIP;
1489 iph->ihl = 5;
1490 iph->tot_len = htons(skb->len);
Eric Dumazetadf30902009-06-02 05:19:30 +00001491 ip_select_ident(iph, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 ip_send_check(iph);
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1495 nf_reset(skb);
1496}
1497
1498static inline int ipmr_forward_finish(struct sk_buff *skb)
1499{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001500 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Eric Dumazetadf30902009-06-02 05:19:30 +00001502 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 if (unlikely(opt->optlen))
1505 ip_forward_options(skb);
1506
1507 return dst_output(skb);
1508}
1509
1510/*
1511 * Processing handlers for ipmr_forward
1512 */
1513
Patrick McHardy0c122952010-04-13 05:03:22 +00001514static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1515 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001517 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001518 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct net_device *dev;
1520 struct rtable *rt;
1521 int encap = 0;
1522
1523 if (vif->dev == NULL)
1524 goto out_free;
1525
1526#ifdef CONFIG_IP_PIMSM
1527 if (vif->flags & VIFF_REGISTER) {
1528 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001529 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001530 vif->dev->stats.tx_bytes += skb->len;
1531 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001532 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001533 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535#endif
1536
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001537 if (vif->flags & VIFF_TUNNEL) {
1538 struct flowi fl = {
1539 .oif = vif->link,
1540 .nl_u = {
1541 .ip4_u = {
1542 .daddr = vif->remote,
1543 .saddr = vif->local,
1544 .tos = RT_TOS(iph->tos)
1545 }
1546 },
1547 .proto = IPPROTO_IPIP
1548 };
1549
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001550 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto out_free;
1552 encap = sizeof(struct iphdr);
1553 } else {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001554 struct flowi fl = {
1555 .oif = vif->link,
1556 .nl_u = {
1557 .ip4_u = {
1558 .daddr = iph->daddr,
1559 .tos = RT_TOS(iph->tos)
1560 }
1561 },
1562 .proto = IPPROTO_IPIP
1563 };
1564
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001565 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 goto out_free;
1567 }
1568
Changli Gaod8d1f302010-06-10 23:31:35 -07001569 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Changli Gaod8d1f302010-06-10 23:31:35 -07001571 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001573 * allow to send ICMP, so that packets will disappear
1574 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 */
1576
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001577 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ip_rt_put(rt);
1579 goto out_free;
1580 }
1581
Changli Gaod8d1f302010-06-10 23:31:35 -07001582 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001585 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 goto out_free;
1587 }
1588
1589 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001590 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Eric Dumazetadf30902009-06-02 05:19:30 +00001592 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001593 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001594 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001597 * What do we do with netfilter? -- RR
1598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (vif->flags & VIFF_TUNNEL) {
1600 ip_encap(skb, vif->local, vif->remote);
1601 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001602 vif->dev->stats.tx_packets++;
1603 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605
1606 IPCB(skb)->flags |= IPSKB_FORWARDED;
1607
1608 /*
1609 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1610 * not only before forwarding, but after forwarding on all output
1611 * interfaces. It is clear, if mrouter runs a multicasting
1612 * program, it should receive packets not depending to what interface
1613 * program is joined.
1614 * If we will not make it, the program will have to join on all
1615 * interfaces. On the other hand, multihoming host (or router, but
1616 * not mrouter) cannot join to more than one interface - it will
1617 * result in receiving multiple packets.
1618 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001619 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ipmr_forward_finish);
1621 return;
1622
1623out_free:
1624 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Patrick McHardy0c122952010-04-13 05:03:22 +00001627static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001630
1631 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1632 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 break;
1634 }
1635 return ct;
1636}
1637
1638/* "local" means that we should preserve one skb (for local delivery) */
1639
Patrick McHardy0c122952010-04-13 05:03:22 +00001640static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1641 struct sk_buff *skb, struct mfc_cache *cache,
1642 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 int psend = -1;
1645 int vif, ct;
1646
1647 vif = cache->mfc_parent;
1648 cache->mfc_un.res.pkt++;
1649 cache->mfc_un.res.bytes += skb->len;
1650
1651 /*
1652 * Wrong interface: drop packet and (maybe) send PIM assert.
1653 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001654 if (mrt->vif_table[vif].dev != skb->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 int true_vifi;
1656
David S. Millerc7537962010-11-11 17:07:48 -08001657 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001659 * Very complicated situation...
1660 *
1661 * The best workaround until routing daemons will be
1662 * fixed is not to redistribute packet, if it was
1663 * send through wrong interface. It means, that
1664 * multicast applications WILL NOT work for
1665 * (S,G), which have default multicast route pointing
1666 * to wrong oif. In any case, it is not a good
1667 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 */
1669 goto dont_forward;
1670 }
1671
1672 cache->mfc_un.res.wrong_if++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001673 true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Patrick McHardy0c122952010-04-13 05:03:22 +00001675 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001677 * so that we cannot check that packet arrived on an oif.
1678 * It is bad, but otherwise we would need to move pretty
1679 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001681 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001682 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001683 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1685 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001686 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688 goto dont_forward;
1689 }
1690
Patrick McHardy0c122952010-04-13 05:03:22 +00001691 mrt->vif_table[vif].pkt_in++;
1692 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 /*
1695 * Forward the frame
1696 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001697 for (ct = cache->mfc_un.res.maxvif - 1;
1698 ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001699 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (psend != -1) {
1701 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001704 ipmr_queue_xmit(net, mrt, skb2, cache,
1705 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001707 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709 }
1710 if (psend != -1) {
1711 if (local) {
1712 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001715 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001717 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return 0;
1719 }
1720 }
1721
1722dont_forward:
1723 if (!local)
1724 kfree_skb(skb);
1725 return 0;
1726}
1727
1728
1729/*
1730 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001731 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
1733
1734int ip_mr_input(struct sk_buff *skb)
1735{
1736 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001737 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001738 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001739 struct mr_table *mrt;
1740 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001743 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001745 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto dont_forward;
1747
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001748 err = ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001749 if (err < 0) {
1750 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001751 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +00001752 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001755 if (IPCB(skb)->opt.router_alert) {
1756 if (ip_call_ra_chain(skb))
1757 return 0;
1758 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1759 /* IGMPv1 (and broken IGMPv2 implementations sort of
1760 * Cisco IOS <= 11.2(8)) do not put router alert
1761 * option to IGMP packets destined to routable
1762 * groups. It is very bad, because it means
1763 * that we can forward NO IGMP messages.
1764 */
1765 struct sock *mroute_sk;
1766
1767 mroute_sk = rcu_dereference(mrt->mroute_sk);
1768 if (mroute_sk) {
1769 nf_reset(skb);
1770 raw_rcv(mroute_sk, skb);
1771 return 0;
1772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 }
1775
Eric Dumazeta8c94862010-10-01 16:15:08 +00001776 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001777 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 /*
1780 * No usable cache entry
1781 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001782 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int vif;
1784
1785 if (local) {
1786 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1787 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001788 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 skb = skb2;
1791 }
1792
Eric Dumazeta8c94862010-10-01 16:15:08 +00001793 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001794 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001796 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 read_unlock(&mrt_lock);
1798
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001799 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801 read_unlock(&mrt_lock);
1802 kfree_skb(skb);
1803 return -ENODEV;
1804 }
1805
Eric Dumazeta8c94862010-10-01 16:15:08 +00001806 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001807 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 read_unlock(&mrt_lock);
1809
1810 if (local)
1811 return ip_local_deliver(skb);
1812
1813 return 0;
1814
1815dont_forward:
1816 if (local)
1817 return ip_local_deliver(skb);
1818 kfree_skb(skb);
1819 return 0;
1820}
1821
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001822#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00001823/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001824static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1825 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001827 struct net_device *reg_dev = NULL;
1828 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001830 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001832 * Check that:
1833 * a. packet is really sent to a multicast group
1834 * b. packet is not a NULL-REGISTER
1835 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001837 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001839 ntohs(encap->tot_len) + pimlen > skb->len)
1840 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001843 if (mrt->mroute_reg_vif_num >= 0)
1844 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 read_unlock(&mrt_lock);
1846
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001847 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001848 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001850 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00001851 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001852 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00001854 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07001856
1857 skb_tunnel_rx(skb, reg_dev);
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001860
Eric Dumazet55747a02010-10-01 16:14:55 +00001861 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001862}
1863#endif
1864
1865#ifdef CONFIG_IP_PIMSM_V1
1866/*
1867 * Handle IGMP messages of PIMv1
1868 */
1869
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001870int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001871{
1872 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001873 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001874 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001875
1876 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1877 goto drop;
1878
1879 pim = igmp_hdr(skb);
1880
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001881 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1882 goto drop;
1883
Patrick McHardy0c122952010-04-13 05:03:22 +00001884 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001885 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1886 goto drop;
1887
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001888 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001889drop:
1890 kfree_skb(skb);
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893}
1894#endif
1895
1896#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001897static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001900 struct net *net = dev_net(skb->dev);
1901 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001903 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto drop;
1905
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001906 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001907 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1908 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001909 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08001910 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 goto drop;
1912
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001913 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1914 goto drop;
1915
1916 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001917drop:
1918 kfree_skb(skb);
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return 0;
1921}
1922#endif
1923
Patrick McHardycb6a4e42010-04-26 16:02:08 +02001924static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
1925 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 int ct;
1928 struct rtnexthop *nhp;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001929 u8 *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 struct rtattr *mp_head;
1931
Nicolas Dichtel74381892010-03-25 23:45:35 +00001932 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07001933 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00001934 return -ENOENT;
1935
Patrick McHardy0c122952010-04-13 05:03:22 +00001936 if (VIF_EXISTS(mrt, c->mfc_parent))
1937 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Jianjun Kongc354e122008-11-03 00:28:02 -08001939 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001942 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1944 goto rtattr_failure;
Jianjun Kongc354e122008-11-03 00:28:02 -08001945 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 nhp->rtnh_flags = 0;
1947 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00001948 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 nhp->rtnh_len = sizeof(*nhp);
1950 }
1951 }
1952 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001953 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 rtm->rtm_type = RTN_MULTICAST;
1955 return 1;
1956
1957rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001958 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return -EMSGSIZE;
1960}
1961
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001962int ipmr_get_route(struct net *net,
1963 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 int err;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001966 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 struct mfc_cache *cache;
Eric Dumazet511c3f92009-06-02 05:14:27 +00001968 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001970 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
1971 if (mrt == NULL)
1972 return -ENOENT;
1973
Eric Dumazeta8c94862010-10-01 16:15:08 +00001974 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001975 cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Jianjun Kongc354e122008-11-03 00:28:02 -08001977 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07001978 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001979 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001981 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001984 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return -EAGAIN;
1986 }
1987
1988 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001989 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001990 if (dev)
1991 vif = ipmr_find_vif(mrt, dev);
1992 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001994 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return -ENODEV;
1996 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07001997 skb2 = skb_clone(skb, GFP_ATOMIC);
1998 if (!skb2) {
1999 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002000 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002001 return -ENOMEM;
2002 }
2003
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002004 skb_push(skb2, sizeof(struct iphdr));
2005 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002006 iph = ip_hdr(skb2);
2007 iph->ihl = sizeof(struct iphdr) >> 2;
2008 iph->saddr = rt->rt_src;
2009 iph->daddr = rt->rt_dst;
2010 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002011 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002013 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return err;
2015 }
2016
Eric Dumazeta8c94862010-10-01 16:15:08 +00002017 read_lock(&mrt_lock);
2018 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002020 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002022 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return err;
2024}
2025
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002026static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2027 u32 pid, u32 seq, struct mfc_cache *c)
2028{
2029 struct nlmsghdr *nlh;
2030 struct rtmsg *rtm;
2031
2032 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2033 if (nlh == NULL)
2034 return -EMSGSIZE;
2035
2036 rtm = nlmsg_data(nlh);
2037 rtm->rtm_family = RTNL_FAMILY_IPMR;
2038 rtm->rtm_dst_len = 32;
2039 rtm->rtm_src_len = 32;
2040 rtm->rtm_tos = 0;
2041 rtm->rtm_table = mrt->id;
2042 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2043 rtm->rtm_type = RTN_MULTICAST;
2044 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2045 rtm->rtm_protocol = RTPROT_UNSPEC;
2046 rtm->rtm_flags = 0;
2047
2048 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2049 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2050
2051 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2052 goto nla_put_failure;
2053
2054 return nlmsg_end(skb, nlh);
2055
2056nla_put_failure:
2057 nlmsg_cancel(skb, nlh);
2058 return -EMSGSIZE;
2059}
2060
2061static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2062{
2063 struct net *net = sock_net(skb->sk);
2064 struct mr_table *mrt;
2065 struct mfc_cache *mfc;
2066 unsigned int t = 0, s_t;
2067 unsigned int h = 0, s_h;
2068 unsigned int e = 0, s_e;
2069
2070 s_t = cb->args[0];
2071 s_h = cb->args[1];
2072 s_e = cb->args[2];
2073
Eric Dumazeta8c94862010-10-01 16:15:08 +00002074 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002075 ipmr_for_each_table(mrt, net) {
2076 if (t < s_t)
2077 goto next_table;
2078 if (t > s_t)
2079 s_h = 0;
2080 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002081 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002082 if (e < s_e)
2083 goto next_entry;
2084 if (ipmr_fill_mroute(mrt, skb,
2085 NETLINK_CB(cb->skb).pid,
2086 cb->nlh->nlmsg_seq,
2087 mfc) < 0)
2088 goto done;
2089next_entry:
2090 e++;
2091 }
2092 e = s_e = 0;
2093 }
2094 s_h = 0;
2095next_table:
2096 t++;
2097 }
2098done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002099 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002100
2101 cb->args[2] = e;
2102 cb->args[1] = h;
2103 cb->args[0] = t;
2104
2105 return skb->len;
2106}
2107
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002108#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002110 * The /proc interfaces to multicast routing :
2111 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 */
2113struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002114 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002115 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 int ct;
2117};
2118
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002119static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2120 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 loff_t pos)
2122{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002123 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002124
2125 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2126 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002128 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002129 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
2131 return NULL;
2132}
2133
2134static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002135 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002137 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002138 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002139 struct mr_table *mrt;
2140
2141 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2142 if (mrt == NULL)
2143 return ERR_PTR(-ENOENT);
2144
2145 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002148 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 : SEQ_START_TOKEN;
2150}
2151
2152static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2153{
2154 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002155 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002156 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 ++*pos;
2159 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002160 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002161
Patrick McHardy0c122952010-04-13 05:03:22 +00002162 while (++iter->ct < mrt->maxvif) {
2163 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002165 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167 return NULL;
2168}
2169
2170static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002171 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 read_unlock(&mrt_lock);
2174}
2175
2176static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2177{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002178 struct ipmr_vif_iter *iter = seq->private;
2179 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002182 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2184 } else {
2185 const struct vif_device *vif = v;
2186 const char *name = vif->dev ? vif->dev->name : "none";
2187
2188 seq_printf(seq,
2189 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002190 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002191 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 vif->bytes_out, vif->pkt_out,
2193 vif->flags, vif->local, vif->remote);
2194 }
2195 return 0;
2196}
2197
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002198static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 .start = ipmr_vif_seq_start,
2200 .next = ipmr_vif_seq_next,
2201 .stop = ipmr_vif_seq_stop,
2202 .show = ipmr_vif_seq_show,
2203};
2204
2205static int ipmr_vif_open(struct inode *inode, struct file *file)
2206{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002207 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2208 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Arjan van de Ven9a321442007-02-12 00:55:35 -08002211static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 .owner = THIS_MODULE,
2213 .open = ipmr_vif_open,
2214 .read = seq_read,
2215 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002216 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217};
2218
2219struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002220 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002221 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002222 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 int ct;
2224};
2225
2226
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002227static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2228 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002230 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 struct mfc_cache *mfc;
2232
Eric Dumazeta8c94862010-10-01 16:15:08 +00002233 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002234 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002235 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002236 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002237 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002239 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002240 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002243 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002244 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002245 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return mfc;
2247 spin_unlock_bh(&mfc_unres_lock);
2248
2249 it->cache = NULL;
2250 return NULL;
2251}
2252
2253
2254static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2255{
2256 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002257 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002258 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002259
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002260 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2261 if (mrt == NULL)
2262 return ERR_PTR(-ENOENT);
2263
2264 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 it->cache = NULL;
2266 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002267 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 : SEQ_START_TOKEN;
2269}
2270
2271static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2272{
2273 struct mfc_cache *mfc = v;
2274 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002275 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002276 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 ++*pos;
2279
2280 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002281 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Patrick McHardy862465f2010-04-13 05:03:21 +00002283 if (mfc->list.next != it->cache)
2284 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002285
Patrick McHardy0c122952010-04-13 05:03:22 +00002286 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 goto end_of_list;
2288
Patrick McHardy0c122952010-04-13 05:03:22 +00002289 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002292 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002293 if (list_empty(it->cache))
2294 continue;
2295 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297
2298 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002299 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002300 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002304 if (!list_empty(it->cache))
2305 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002307end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 spin_unlock_bh(&mfc_unres_lock);
2309 it->cache = NULL;
2310
2311 return NULL;
2312}
2313
2314static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2315{
2316 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002317 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Patrick McHardy0c122952010-04-13 05:03:22 +00002319 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002321 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002322 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
2325static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2326{
2327 int n;
2328
2329 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002330 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2332 } else {
2333 const struct mfc_cache *mfc = v;
2334 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002335 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002336
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002337 seq_printf(seq, "%08X %08X %-3hd",
2338 (__force u32) mfc->mfc_mcastgrp,
2339 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002340 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Patrick McHardy0c122952010-04-13 05:03:22 +00002342 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002343 seq_printf(seq, " %8lu %8lu %8lu",
2344 mfc->mfc_un.res.pkt,
2345 mfc->mfc_un.res.bytes,
2346 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002347 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002348 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002349 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002350 mfc->mfc_un.res.ttls[n] < 255)
2351 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002352 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 n, mfc->mfc_un.res.ttls[n]);
2354 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002355 } else {
2356 /* unresolved mfc_caches don't contain
2357 * pkt, bytes and wrong_if values
2358 */
2359 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361 seq_putc(seq, '\n');
2362 }
2363 return 0;
2364}
2365
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002366static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 .start = ipmr_mfc_seq_start,
2368 .next = ipmr_mfc_seq_next,
2369 .stop = ipmr_mfc_seq_stop,
2370 .show = ipmr_mfc_seq_show,
2371};
2372
2373static int ipmr_mfc_open(struct inode *inode, struct file *file)
2374{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002375 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2376 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
2378
Arjan van de Ven9a321442007-02-12 00:55:35 -08002379static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 .owner = THIS_MODULE,
2381 .open = ipmr_mfc_open,
2382 .read = seq_read,
2383 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002384 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002386#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002389static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002391 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392};
2393#endif
2394
2395
2396/*
2397 * Setup for IP multicast routing
2398 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002399static int __net_init ipmr_net_init(struct net *net)
2400{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002401 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002402
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002403 err = ipmr_rules_init(net);
2404 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002405 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002406
2407#ifdef CONFIG_PROC_FS
2408 err = -ENOMEM;
2409 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2410 goto proc_vif_fail;
2411 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2412 goto proc_cache_fail;
2413#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002414 return 0;
2415
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002416#ifdef CONFIG_PROC_FS
2417proc_cache_fail:
2418 proc_net_remove(net, "ip_mr_vif");
2419proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002420 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002421#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002422fail:
2423 return err;
2424}
2425
2426static void __net_exit ipmr_net_exit(struct net *net)
2427{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002428#ifdef CONFIG_PROC_FS
2429 proc_net_remove(net, "ip_mr_cache");
2430 proc_net_remove(net, "ip_mr_vif");
2431#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002432 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002433}
2434
2435static struct pernet_operations ipmr_net_ops = {
2436 .init = ipmr_net_init,
2437 .exit = ipmr_net_exit,
2438};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002439
Wang Chen03d2f892008-07-03 12:13:36 +08002440int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Wang Chen03d2f892008-07-03 12:13:36 +08002442 int err;
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2445 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002446 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002447 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002448 if (!mrt_cachep)
2449 return -ENOMEM;
2450
Benjamin Therycf958ae32009-01-22 04:56:16 +00002451 err = register_pernet_subsys(&ipmr_net_ops);
2452 if (err)
2453 goto reg_pernet_fail;
2454
Wang Chen03d2f892008-07-03 12:13:36 +08002455 err = register_netdevice_notifier(&ip_mr_notifier);
2456 if (err)
2457 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002458#ifdef CONFIG_IP_PIMSM_V2
2459 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2460 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2461 err = -EAGAIN;
2462 goto add_proto_fail;
2463 }
2464#endif
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002465 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, NULL, ipmr_rtm_dumproute);
Wang Chen03d2f892008-07-03 12:13:36 +08002466 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002467
Tom Goff403dbb92009-06-14 03:16:13 -07002468#ifdef CONFIG_IP_PIMSM_V2
2469add_proto_fail:
2470 unregister_netdevice_notifier(&ip_mr_notifier);
2471#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002472reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002473 unregister_pernet_subsys(&ipmr_net_ops);
2474reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002475 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002476 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}