blob: 1f62eaeb6de42845f2181f4e6e78177cab59ad03 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/system.h>
30#include <asm/uaccess.h>
31#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
34#include <linux/timer.h>
35#include <linux/mm.h>
36#include <linux/kernel.h>
37#include <linux/fcntl.h>
38#include <linux/stat.h>
39#include <linux/socket.h>
40#include <linux/in.h>
41#include <linux/inet.h>
42#include <linux/netdevice.h>
43#include <linux/inetdevice.h>
44#include <linux/igmp.h>
45#include <linux/proc_fs.h>
46#include <linux/seq_file.h>
47#include <linux/mroute.h>
48#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080049#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020051#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/ip.h>
53#include <net/protocol.h>
54#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020055#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/sock.h>
57#include <net/icmp.h>
58#include <net/udp.h>
59#include <net/raw.h>
60#include <linux/notifier.h>
61#include <linux/if_arp.h>
62#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000063#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/ipip.h>
65#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
70#define CONFIG_IP_PIMSM 1
71#endif
72
Patrick McHardy0c122952010-04-13 05:03:22 +000073struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000074 struct list_head list;
Patrick McHardy8de53df2010-04-15 13:29:28 +020075#ifdef CONFIG_NET_NS
76 struct net *net;
77#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +000078 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000079 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000080 struct timer_list ipmr_expire_timer;
81 struct list_head mfc_unres_queue;
82 struct list_head mfc_cache_array[MFC_LINES];
83 struct vif_device vif_table[MAXVIFS];
84 int maxvif;
85 atomic_t cache_resolve_queue_len;
86 int mroute_do_assert;
87 int mroute_do_pim;
88#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
89 int mroute_reg_vif_num;
90#endif
91};
92
Patrick McHardyf0ad0862010-04-13 05:03:23 +000093struct ipmr_rule {
94 struct fib_rule common;
95};
96
97struct ipmr_result {
98 struct mr_table *mrt;
99};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000102 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104
105static DEFINE_RWLOCK(mrt_lock);
106
107/*
108 * Multicast router control variables
109 */
110
Patrick McHardy0c122952010-04-13 05:03:22 +0000111#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* Special spinlock for queue of unresolved entries */
114static DEFINE_SPINLOCK(mfc_unres_lock);
115
116/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000117 * entries is changed only in process context and protected
118 * with weak lock mrt_lock. Queue of unresolved entries is protected
119 * with strong spinlock mfc_unres_lock.
120 *
121 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123
Christoph Lametere18b8902006-12-06 20:33:20 -0800124static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000126static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Patrick McHardy0c122952010-04-13 05:03:22 +0000127static int ip_mr_forward(struct net *net, struct mr_table *mrt,
128 struct sk_buff *skb, struct mfc_cache *cache,
129 int local);
130static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000131 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200132static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
133 struct mfc_cache *c, struct rtmsg *rtm);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000134static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000136#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
137#define ipmr_for_each_table(mrt, net) \
138 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
139
140static struct mr_table *ipmr_get_table(struct net *net, u32 id)
141{
142 struct mr_table *mrt;
143
144 ipmr_for_each_table(mrt, net) {
145 if (mrt->id == id)
146 return mrt;
147 }
148 return NULL;
149}
150
David S. Millerda919812011-03-12 02:04:50 -0500151static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000152 struct mr_table **mrt)
153{
154 struct ipmr_result res;
155 struct fib_lookup_arg arg = { .result = &res, };
156 int err;
157
David S. Millerda919812011-03-12 02:04:50 -0500158 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
159 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000160 if (err < 0)
161 return err;
162 *mrt = res.mrt;
163 return 0;
164}
165
166static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
167 int flags, struct fib_lookup_arg *arg)
168{
169 struct ipmr_result *res = arg->result;
170 struct mr_table *mrt;
171
172 switch (rule->action) {
173 case FR_ACT_TO_TBL:
174 break;
175 case FR_ACT_UNREACHABLE:
176 return -ENETUNREACH;
177 case FR_ACT_PROHIBIT:
178 return -EACCES;
179 case FR_ACT_BLACKHOLE:
180 default:
181 return -EINVAL;
182 }
183
184 mrt = ipmr_get_table(rule->fr_net, rule->table);
185 if (mrt == NULL)
186 return -EAGAIN;
187 res->mrt = mrt;
188 return 0;
189}
190
191static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
192{
193 return 1;
194}
195
196static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
197 FRA_GENERIC_POLICY,
198};
199
200static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
201 struct fib_rule_hdr *frh, struct nlattr **tb)
202{
203 return 0;
204}
205
206static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
207 struct nlattr **tb)
208{
209 return 1;
210}
211
212static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
213 struct fib_rule_hdr *frh)
214{
215 frh->dst_len = 0;
216 frh->src_len = 0;
217 frh->tos = 0;
218 return 0;
219}
220
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200221static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200222 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000223 .rule_size = sizeof(struct ipmr_rule),
224 .addr_size = sizeof(u32),
225 .action = ipmr_rule_action,
226 .match = ipmr_rule_match,
227 .configure = ipmr_rule_configure,
228 .compare = ipmr_rule_compare,
229 .default_pref = fib_default_rule_pref,
230 .fill = ipmr_rule_fill,
231 .nlgroup = RTNLGRP_IPV4_RULE,
232 .policy = ipmr_rule_policy,
233 .owner = THIS_MODULE,
234};
235
236static int __net_init ipmr_rules_init(struct net *net)
237{
238 struct fib_rules_ops *ops;
239 struct mr_table *mrt;
240 int err;
241
242 ops = fib_rules_register(&ipmr_rules_ops_template, net);
243 if (IS_ERR(ops))
244 return PTR_ERR(ops);
245
246 INIT_LIST_HEAD(&net->ipv4.mr_tables);
247
248 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
249 if (mrt == NULL) {
250 err = -ENOMEM;
251 goto err1;
252 }
253
254 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
255 if (err < 0)
256 goto err2;
257
258 net->ipv4.mr_rules_ops = ops;
259 return 0;
260
261err2:
262 kfree(mrt);
263err1:
264 fib_rules_unregister(ops);
265 return err;
266}
267
268static void __net_exit ipmr_rules_exit(struct net *net)
269{
270 struct mr_table *mrt, *next;
271
Eric Dumazet035320d2010-06-06 23:48:40 +0000272 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
273 list_del(&mrt->list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000274 kfree(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000275 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000276 fib_rules_unregister(net->ipv4.mr_rules_ops);
277}
278#else
279#define ipmr_for_each_table(mrt, net) \
280 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
281
282static struct mr_table *ipmr_get_table(struct net *net, u32 id)
283{
284 return net->ipv4.mrt;
285}
286
David S. Millerda919812011-03-12 02:04:50 -0500287static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000288 struct mr_table **mrt)
289{
290 *mrt = net->ipv4.mrt;
291 return 0;
292}
293
294static int __net_init ipmr_rules_init(struct net *net)
295{
296 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
297 return net->ipv4.mrt ? 0 : -ENOMEM;
298}
299
300static void __net_exit ipmr_rules_exit(struct net *net)
301{
302 kfree(net->ipv4.mrt);
303}
304#endif
305
306static struct mr_table *ipmr_new_table(struct net *net, u32 id)
307{
308 struct mr_table *mrt;
309 unsigned int i;
310
311 mrt = ipmr_get_table(net, id);
312 if (mrt != NULL)
313 return mrt;
314
315 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
316 if (mrt == NULL)
317 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200318 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000319 mrt->id = id;
320
321 /* Forwarding cache */
322 for (i = 0; i < MFC_LINES; i++)
323 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
324
325 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
326
327 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
328 (unsigned long)mrt);
329
330#ifdef CONFIG_IP_PIMSM
331 mrt->mroute_reg_vif_num = -1;
332#endif
333#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
334 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
335#endif
336 return mrt;
337}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
340
Wang Chend6070322008-07-14 20:55:26 -0700341static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
342{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000343 struct net *net = dev_net(dev);
344
Wang Chend6070322008-07-14 20:55:26 -0700345 dev_close(dev);
346
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000347 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700348 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800349 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700350 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700351 struct ip_tunnel_parm p;
352
353 memset(&p, 0, sizeof(p));
354 p.iph.daddr = v->vifc_rmt_addr.s_addr;
355 p.iph.saddr = v->vifc_lcl_addr.s_addr;
356 p.iph.version = 4;
357 p.iph.ihl = 5;
358 p.iph.protocol = IPPROTO_IPIP;
359 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
360 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
361
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800362 if (ops->ndo_do_ioctl) {
363 mm_segment_t oldfs = get_fs();
364
365 set_fs(KERNEL_DS);
366 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
367 set_fs(oldfs);
368 }
Wang Chend6070322008-07-14 20:55:26 -0700369 }
370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000373struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct net_device *dev;
376
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000377 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800380 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int err;
382 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct ip_tunnel_parm p;
384 struct in_device *in_dev;
385
386 memset(&p, 0, sizeof(p));
387 p.iph.daddr = v->vifc_rmt_addr.s_addr;
388 p.iph.saddr = v->vifc_lcl_addr.s_addr;
389 p.iph.version = 4;
390 p.iph.ihl = 5;
391 p.iph.protocol = IPPROTO_IPIP;
392 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800393 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800395 if (ops->ndo_do_ioctl) {
396 mm_segment_t oldfs = get_fs();
397
398 set_fs(KERNEL_DS);
399 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
400 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000401 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800402 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 dev = NULL;
405
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000406 if (err == 0 &&
407 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 dev->flags |= IFF_MULTICAST;
409
Herbert Xue5ed6392005-10-03 14:35:55 -0700410 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700411 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700413
414 ipv4_devconf_setall(in_dev);
415 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (dev_open(dev))
418 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700419 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 }
422 return dev;
423
424failure:
425 /* allow the register to be completed before unregistering. */
426 rtnl_unlock();
427 rtnl_lock();
428
429 unregister_netdevice(dev);
430 return NULL;
431}
432
433#ifdef CONFIG_IP_PIMSM
434
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000435static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000437 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000438 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500439 struct flowi4 fl4 = {
440 .flowi4_oif = dev->ifindex,
441 .flowi4_iif = skb->skb_iif,
442 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000443 };
444 int err;
445
David S. Millerda919812011-03-12 02:04:50 -0500446 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000447 if (err < 0) {
448 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000449 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000450 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700453 dev->stats.tx_bytes += skb->len;
454 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000455 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 read_unlock(&mrt_lock);
457 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000458 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Stephen Hemminger007c3832008-11-20 20:28:35 -0800461static const struct net_device_ops reg_vif_netdev_ops = {
462 .ndo_start_xmit = reg_vif_xmit,
463};
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static void reg_vif_setup(struct net_device *dev)
466{
467 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800468 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800470 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700472 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000475static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct net_device *dev;
478 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000479 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000481 if (mrt->id == RT_TABLE_DEFAULT)
482 sprintf(name, "pimreg");
483 else
484 sprintf(name, "pimreg%u", mrt->id);
485
486 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (dev == NULL)
489 return NULL;
490
Tom Goff403dbb92009-06-14 03:16:13 -0700491 dev_net_set(dev, net);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (register_netdevice(dev)) {
494 free_netdev(dev);
495 return NULL;
496 }
497 dev->iflink = 0;
498
Herbert Xu71e27da2007-06-04 23:36:06 -0700499 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000500 in_dev = __in_dev_get_rcu(dev);
501 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700502 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Herbert Xu71e27da2007-06-04 23:36:06 -0700506 ipv4_devconf_setall(in_dev);
507 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
508 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (dev_open(dev))
511 goto failure;
512
Wang Chen7dc00c82008-07-14 20:56:34 -0700513 dev_hold(dev);
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return dev;
516
517failure:
518 /* allow the register to be completed before unregistering. */
519 rtnl_unlock();
520 rtnl_lock();
521
522 unregister_netdevice(dev);
523 return NULL;
524}
525#endif
526
527/*
528 * Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700529 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900531
Patrick McHardy0c122952010-04-13 05:03:22 +0000532static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000533 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct vif_device *v;
536 struct net_device *dev;
537 struct in_device *in_dev;
538
Patrick McHardy0c122952010-04-13 05:03:22 +0000539 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EADDRNOTAVAIL;
541
Patrick McHardy0c122952010-04-13 05:03:22 +0000542 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 write_lock_bh(&mrt_lock);
545 dev = v->dev;
546 v->dev = NULL;
547
548 if (!dev) {
549 write_unlock_bh(&mrt_lock);
550 return -EADDRNOTAVAIL;
551 }
552
553#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000554 if (vifi == mrt->mroute_reg_vif_num)
555 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556#endif
557
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000558 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000560
561 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000562 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000565 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 write_unlock_bh(&mrt_lock);
569
570 dev_set_allmulti(dev, -1);
571
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000572 in_dev = __in_dev_get_rtnl(dev);
573 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700574 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ip_rt_multicast_event(in_dev);
576 }
577
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000578 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000579 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 dev_put(dev);
582 return 0;
583}
584
Eric Dumazeta8c94862010-10-01 16:15:08 +0000585static void ipmr_cache_free_rcu(struct rcu_head *head)
586{
587 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
588
589 kmem_cache_free(mrt_cachep, c);
590}
591
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000592static inline void ipmr_cache_free(struct mfc_cache *c)
593{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000594 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000598 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
600
Patrick McHardy0c122952010-04-13 05:03:22 +0000601static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200603 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700605 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Patrick McHardy0c122952010-04-13 05:03:22 +0000607 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jianjun Kongc354e122008-11-03 00:28:02 -0800609 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700610 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
612 nlh->nlmsg_type = NLMSG_ERROR;
613 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
614 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700615 e = NLMSG_DATA(nlh);
616 e->error = -ETIMEDOUT;
617 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700618
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000619 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000620 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000625 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628
Patrick McHardye258beb2010-04-13 05:03:19 +0000629/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Patrick McHardye258beb2010-04-13 05:03:19 +0000631static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Patrick McHardy0c122952010-04-13 05:03:22 +0000633 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 unsigned long now;
635 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000636 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000639 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return;
641 }
642
Patrick McHardy0c122952010-04-13 05:03:22 +0000643 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto out;
645
646 now = jiffies;
647 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Patrick McHardy0c122952010-04-13 05:03:22 +0000649 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (time_after(c->mfc_un.unres.expires, now)) {
651 unsigned long interval = c->mfc_un.unres.expires - now;
652 if (interval < expires)
653 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 continue;
655 }
656
Patrick McHardy862465f2010-04-13 05:03:21 +0000657 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000658 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Patrick McHardy0c122952010-04-13 05:03:22 +0000661 if (!list_empty(&mrt->mfc_unres_queue))
662 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664out:
665 spin_unlock(&mfc_unres_lock);
666}
667
668/* Fill oifs list. It is called under write locked mrt_lock. */
669
Patrick McHardy0c122952010-04-13 05:03:22 +0000670static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000671 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 int vifi;
674
675 cache->mfc_un.res.minvif = MAXVIFS;
676 cache->mfc_un.res.maxvif = 0;
677 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
678
Patrick McHardy0c122952010-04-13 05:03:22 +0000679 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
680 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000681 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
683 if (cache->mfc_un.res.minvif > vifi)
684 cache->mfc_un.res.minvif = vifi;
685 if (cache->mfc_un.res.maxvif <= vifi)
686 cache->mfc_un.res.maxvif = vifi + 1;
687 }
688 }
689}
690
Patrick McHardy0c122952010-04-13 05:03:22 +0000691static int vif_add(struct net *net, struct mr_table *mrt,
692 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000695 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct net_device *dev;
697 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700698 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000701 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EADDRINUSE;
703
704 switch (vifc->vifc_flags) {
705#ifdef CONFIG_IP_PIMSM
706 case VIFF_REGISTER:
707 /*
708 * Special Purpose VIF in PIM
709 * All the packets will be sent to the daemon
710 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000711 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000713 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (!dev)
715 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700716 err = dev_set_allmulti(dev, 1);
717 if (err) {
718 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700719 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700720 return err;
721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
723#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900724 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000725 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!dev)
727 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700728 err = dev_set_allmulti(dev, 1);
729 if (err) {
730 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700731 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700732 return err;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000735
736 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000738 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
739 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000740 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000741 dev_put(dev);
742 return -EADDRNOTAVAIL;
743 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000744 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000745 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!dev)
748 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700749 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700750 if (err) {
751 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700752 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755 default:
756 return -EINVAL;
757 }
758
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000759 in_dev = __in_dev_get_rtnl(dev);
760 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000761 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000763 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700764 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 ip_rt_multicast_event(in_dev);
766
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000767 /* Fill in the VIF structures */
768
Jianjun Kongc354e122008-11-03 00:28:02 -0800769 v->rate_limit = vifc->vifc_rate_limit;
770 v->local = vifc->vifc_lcl_addr.s_addr;
771 v->remote = vifc->vifc_rmt_addr.s_addr;
772 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!mrtsock)
774 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800775 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 v->bytes_in = 0;
777 v->bytes_out = 0;
778 v->pkt_in = 0;
779 v->pkt_out = 0;
780 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000781 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 v->link = dev->iflink;
783
784 /* And finish update writing critical data */
785 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800786 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000788 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000789 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000791 if (vifi+1 > mrt->maxvif)
792 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 write_unlock_bh(&mrt_lock);
794 return 0;
795}
796
Eric Dumazeta8c94862010-10-01 16:15:08 +0000797/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000798static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000799 __be32 origin,
800 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Jianjun Kongc354e122008-11-03 00:28:02 -0800802 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct mfc_cache *c;
804
Eric Dumazeta8c94862010-10-01 16:15:08 +0000805 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000806 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
807 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000809 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812/*
813 * Allocate a multicast cache entry
814 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000815static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Jianjun Kongc354e122008-11-03 00:28:02 -0800817 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000818
819 if (c)
820 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return c;
822}
823
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000824static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Jianjun Kongc354e122008-11-03 00:28:02 -0800826 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000827
828 if (c) {
829 skb_queue_head_init(&c->mfc_un.unres.unresolved);
830 c->mfc_un.unres.expires = jiffies + 10*HZ;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return c;
833}
834
835/*
836 * A cache entry has gone into a resolved state from queued
837 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900838
Patrick McHardy0c122952010-04-13 05:03:22 +0000839static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
840 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700843 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000845 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Jianjun Kongc354e122008-11-03 00:28:02 -0800847 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700848 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
850
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200851 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000852 nlh->nlmsg_len = skb_tail_pointer(skb) -
853 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 } else {
855 nlh->nlmsg_type = NLMSG_ERROR;
856 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
857 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700858 e = NLMSG_DATA(nlh);
859 e->error = -EMSGSIZE;
860 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Thomas Graf2942e902006-08-15 00:30:25 -0700862
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000863 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000864 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000865 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868}
869
870/*
871 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
872 * expects the following bizarre scheme.
873 *
874 * Called under mrt_lock.
875 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900876
Patrick McHardy0c122952010-04-13 05:03:22 +0000877static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000878 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300881 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct igmphdr *igmp;
883 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000884 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 int ret;
886
887#ifdef CONFIG_IP_PIMSM
888 if (assert == IGMPMSG_WHOLEPKT)
889 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
890 else
891#endif
892 skb = alloc_skb(128, GFP_ATOMIC);
893
Stephen Hemminger132adf52007-03-08 20:44:43 -0800894 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -ENOBUFS;
896
897#ifdef CONFIG_IP_PIMSM
898 if (assert == IGMPMSG_WHOLEPKT) {
899 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000900 * Duplicate old header, fix ihl, length etc.
901 * And all this only to mangle msg->im_msgtype and
902 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300904 skb_push(skb, sizeof(struct iphdr));
905 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300906 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300907 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700908 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 msg->im_msgtype = IGMPMSG_WHOLEPKT;
910 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000911 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700912 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
913 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
914 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900915 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900917 {
918
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000919 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700921 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300922 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300923 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000924 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700925 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000927 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000929 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000931 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 igmp->type =
933 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000934 igmp->code = 0;
935 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700936 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Eric Dumazet4c968702010-10-01 16:15:01 +0000939 rcu_read_lock();
940 mroute_sk = rcu_dereference(mrt->mroute_sk);
941 if (mroute_sk == NULL) {
942 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 kfree_skb(skb);
944 return -EINVAL;
945 }
946
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000947 /* Deliver to mrouted */
948
Eric Dumazet4c968702010-10-01 16:15:01 +0000949 ret = sock_queue_rcv_skb(mroute_sk, skb);
950 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +0000951 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (net_ratelimit())
953 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
954 kfree_skb(skb);
955 }
956
957 return ret;
958}
959
960/*
961 * Queue a packet for resolution. It gets locked cache entry!
962 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964static int
Patrick McHardy0c122952010-04-13 05:03:22 +0000965ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Patrick McHardy862465f2010-04-13 05:03:21 +0000967 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int err;
969 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700970 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000973 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000974 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000975 c->mfc_origin == iph->saddr) {
976 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
Patrick McHardy862465f2010-04-13 05:03:21 +0000981 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000982 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Patrick McHardy0c122952010-04-13 05:03:22 +0000984 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000985 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 spin_unlock_bh(&mfc_unres_lock);
987
988 kfree_skb(skb);
989 return -ENOBUFS;
990 }
991
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000992 /* Fill in the new cache entry */
993
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700994 c->mfc_parent = -1;
995 c->mfc_origin = iph->saddr;
996 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000998 /* Reflect first query at mrouted. */
999
Patrick McHardy0c122952010-04-13 05:03:22 +00001000 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001001 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001002 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 out - Brad Parker
1004 */
1005 spin_unlock_bh(&mfc_unres_lock);
1006
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001007 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 kfree_skb(skb);
1009 return err;
1010 }
1011
Patrick McHardy0c122952010-04-13 05:03:22 +00001012 atomic_inc(&mrt->cache_resolve_queue_len);
1013 list_add(&c->list, &mrt->mfc_unres_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
David S. Miller278554b2010-05-12 00:05:35 -07001015 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1016 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001019 /* See if we can append the packet */
1020
1021 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 kfree_skb(skb);
1023 err = -ENOBUFS;
1024 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001025 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 err = 0;
1027 }
1028
1029 spin_unlock_bh(&mfc_unres_lock);
1030 return err;
1031}
1032
1033/*
1034 * MFC cache manipulation by user space mroute daemon
1035 */
1036
Patrick McHardy0c122952010-04-13 05:03:22 +00001037static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001040 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Jianjun Kongc354e122008-11-03 00:28:02 -08001042 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Patrick McHardy0c122952010-04-13 05:03:22 +00001044 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1046 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001047 list_del_rcu(&c->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001049 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return 0;
1051 }
1052 }
1053 return -ENOENT;
1054}
1055
Patrick McHardy0c122952010-04-13 05:03:22 +00001056static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1057 struct mfcctl *mfc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Patrick McHardy862465f2010-04-13 05:03:21 +00001059 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001061 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Patrick McHardya50436f2010-03-17 06:04:14 +00001063 if (mfc->mfcc_parent >= MAXVIFS)
1064 return -ENFILE;
1065
Jianjun Kongc354e122008-11-03 00:28:02 -08001066 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Patrick McHardy0c122952010-04-13 05:03:22 +00001068 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001070 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1071 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
Patrick McHardy862465f2010-04-13 05:03:21 +00001076 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 write_lock_bh(&mrt_lock);
1078 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001079 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (!mrtsock)
1081 c->mfc_flags |= MFC_STATIC;
1082 write_unlock_bh(&mrt_lock);
1083 return 0;
1084 }
1085
Joe Perchesf97c1e02007-12-16 13:45:43 -08001086 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return -EINVAL;
1088
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001089 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001090 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return -ENOMEM;
1092
Jianjun Kongc354e122008-11-03 00:28:02 -08001093 c->mfc_origin = mfc->mfcc_origin.s_addr;
1094 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1095 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001096 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!mrtsock)
1098 c->mfc_flags |= MFC_STATIC;
1099
Eric Dumazeta8c94862010-10-01 16:15:08 +00001100 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /*
1103 * Check to see if we resolved a queued list. If so we
1104 * need to send on the frames and tidy up.
1105 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001106 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001108 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001109 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001111 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001112 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001113 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
1115 }
1116 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001117 if (list_empty(&mrt->mfc_unres_queue))
1118 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 spin_unlock_bh(&mfc_unres_lock);
1120
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001121 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001122 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001123 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125 return 0;
1126}
1127
1128/*
1129 * Close the multicast socket, and clear the vif tables etc
1130 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001131
Patrick McHardy0c122952010-04-13 05:03:22 +00001132static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001135 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001136 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001137
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001138 /* Shut down all active vif entries */
1139
Patrick McHardy0c122952010-04-13 05:03:22 +00001140 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001141 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001142 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001144 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001146 /* Wipe the cache */
1147
Patrick McHardy862465f2010-04-13 05:03:21 +00001148 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001149 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001150 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001152 list_del_rcu(&c->list);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001153 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 }
1156
Patrick McHardy0c122952010-04-13 05:03:22 +00001157 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001159 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001160 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001161 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 spin_unlock_bh(&mfc_unres_lock);
1164 }
1165}
1166
Eric Dumazet4c968702010-10-01 16:15:01 +00001167/* called from ip_ra_control(), before an RCU grace period,
1168 * we dont need to call synchronize_rcu() here
1169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170static void mrtsock_destruct(struct sock *sk)
1171{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001172 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001173 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001176 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001177 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001178 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Eric Dumazet4c968702010-10-01 16:15:01 +00001179 rcu_assign_pointer(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001180 mroute_clean_tables(mrt);
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183 rtnl_unlock();
1184}
1185
1186/*
1187 * Socket options and virtual interface manipulation. The whole
1188 * virtual interface system is a complete heap, but unfortunately
1189 * that's how BSD mrouted happens to think. Maybe one day with a proper
1190 * MOSPF/PIM router set up we can clean this up.
1191 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001192
David S. Millerb7058842009-09-30 16:12:20 -07001193int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 int ret;
1196 struct vifctl vif;
1197 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001198 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001199 struct mr_table *mrt;
1200
1201 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1202 if (mrt == NULL)
1203 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001204
Stephen Hemminger132adf52007-03-08 20:44:43 -08001205 if (optname != MRT_INIT) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001206 if (sk != rcu_dereference_raw(mrt->mroute_sk) &&
1207 !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -EACCES;
1209 }
1210
Stephen Hemminger132adf52007-03-08 20:44:43 -08001211 switch (optname) {
1212 case MRT_INIT:
1213 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001214 inet_sk(sk)->inet_num != IPPROTO_IGMP)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001215 return -EOPNOTSUPP;
Jianjun Kongc354e122008-11-03 00:28:02 -08001216 if (optlen != sizeof(int))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001217 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Stephen Hemminger132adf52007-03-08 20:44:43 -08001219 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001220 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001222 return -EADDRINUSE;
1223 }
1224
1225 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1226 if (ret == 0) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001227 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001228 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001229 }
1230 rtnl_unlock();
1231 return ret;
1232 case MRT_DONE:
Eric Dumazet4c968702010-10-01 16:15:01 +00001233 if (sk != rcu_dereference_raw(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001234 return -EACCES;
1235 return ip_ra_control(sk, 0, NULL);
1236 case MRT_ADD_VIF:
1237 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001238 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001239 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001240 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001241 return -EFAULT;
1242 if (vif.vifc_vifi >= MAXVIFS)
1243 return -ENFILE;
1244 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001245 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001246 ret = vif_add(net, mrt, &vif,
1247 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001248 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001249 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001250 }
1251 rtnl_unlock();
1252 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /*
1255 * Manipulate the forwarding caches. These live
1256 * in a sort of kernel/user symbiosis.
1257 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001258 case MRT_ADD_MFC:
1259 case MRT_DEL_MFC:
Jianjun Kongc354e122008-11-03 00:28:02 -08001260 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001261 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001262 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001263 return -EFAULT;
1264 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001265 if (optname == MRT_DEL_MFC)
Patrick McHardy0c122952010-04-13 05:03:22 +00001266 ret = ipmr_mfc_delete(mrt, &mfc);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001267 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001268 ret = ipmr_mfc_add(net, mrt, &mfc,
1269 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001270 rtnl_unlock();
1271 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /*
1273 * Control PIM assert.
1274 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001275 case MRT_ASSERT:
1276 {
1277 int v;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001278 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001279 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001280 mrt->mroute_do_assert = (v) ? 1 : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001281 return 0;
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001284 case MRT_PIM:
1285 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001286 int v;
1287
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001288 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289 return -EFAULT;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001290 v = (v) ? 1 : 0;
1291
Stephen Hemminger132adf52007-03-08 20:44:43 -08001292 rtnl_lock();
1293 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001294 if (v != mrt->mroute_do_pim) {
1295 mrt->mroute_do_pim = v;
1296 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001298 rtnl_unlock();
1299 return ret;
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001302#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1303 case MRT_TABLE:
1304 {
1305 u32 v;
1306
1307 if (optlen != sizeof(u32))
1308 return -EINVAL;
1309 if (get_user(v, (u32 __user *)optval))
1310 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001311
1312 rtnl_lock();
1313 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001314 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1315 ret = -EBUSY;
1316 } else {
1317 if (!ipmr_new_table(net, v))
1318 ret = -ENOMEM;
1319 raw_sk(sk)->ipmr_table = v;
1320 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001321 rtnl_unlock();
1322 return ret;
1323 }
1324#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001325 /*
1326 * Spurious command, or MRT_VERSION which you cannot
1327 * set.
1328 */
1329 default:
1330 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332}
1333
1334/*
1335 * Getsock opt support for the multicast routing system.
1336 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001337
Jianjun Kongc354e122008-11-03 00:28:02 -08001338int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 int olr;
1341 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001342 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001343 struct mr_table *mrt;
1344
1345 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1346 if (mrt == NULL)
1347 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Jianjun Kongc354e122008-11-03 00:28:02 -08001349 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001351 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001353 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return -ENOPROTOOPT;
1355
1356 if (get_user(olr, optlen))
1357 return -EFAULT;
1358
1359 olr = min_t(unsigned int, olr, sizeof(int));
1360 if (olr < 0)
1361 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001362
Jianjun Kongc354e122008-11-03 00:28:02 -08001363 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001365 if (optname == MRT_VERSION)
1366 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001368 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001369 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370#endif
1371 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001372 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001373 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -EFAULT;
1375 return 0;
1376}
1377
1378/*
1379 * The IP multicast ioctl support routines.
1380 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1383{
1384 struct sioc_sg_req sr;
1385 struct sioc_vif_req vr;
1386 struct vif_device *vif;
1387 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001388 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001389 struct mr_table *mrt;
1390
1391 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1392 if (mrt == NULL)
1393 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001394
Stephen Hemminger132adf52007-03-08 20:44:43 -08001395 switch (cmd) {
1396 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001397 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001398 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001399 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001400 return -EINVAL;
1401 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001402 vif = &mrt->vif_table[vr.vifi];
1403 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001404 vr.icount = vif->pkt_in;
1405 vr.ocount = vif->pkt_out;
1406 vr.ibytes = vif->bytes_in;
1407 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001409
Jianjun Kongc354e122008-11-03 00:28:02 -08001410 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001412 return 0;
1413 }
1414 read_unlock(&mrt_lock);
1415 return -EADDRNOTAVAIL;
1416 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001417 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001418 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Eric Dumazeta8c94862010-10-01 16:15:08 +00001420 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001421 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001422 if (c) {
1423 sr.pktcnt = c->mfc_un.res.pkt;
1424 sr.bytecnt = c->mfc_un.res.bytes;
1425 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001426 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001427
Jianjun Kongc354e122008-11-03 00:28:02 -08001428 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001429 return -EFAULT;
1430 return 0;
1431 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001432 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001433 return -EADDRNOTAVAIL;
1434 default:
1435 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437}
1438
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001439#ifdef CONFIG_COMPAT
1440struct compat_sioc_sg_req {
1441 struct in_addr src;
1442 struct in_addr grp;
1443 compat_ulong_t pktcnt;
1444 compat_ulong_t bytecnt;
1445 compat_ulong_t wrong_if;
1446};
1447
David S. Millerca6b8bb2011-02-03 17:24:28 -08001448struct compat_sioc_vif_req {
1449 vifi_t vifi; /* Which iface */
1450 compat_ulong_t icount;
1451 compat_ulong_t ocount;
1452 compat_ulong_t ibytes;
1453 compat_ulong_t obytes;
1454};
1455
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001456int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1457{
David S. Miller0033d5a2011-02-03 17:21:31 -08001458 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb2011-02-03 17:24:28 -08001459 struct compat_sioc_vif_req vr;
1460 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001461 struct mfc_cache *c;
1462 struct net *net = sock_net(sk);
1463 struct mr_table *mrt;
1464
1465 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1466 if (mrt == NULL)
1467 return -ENOENT;
1468
1469 switch (cmd) {
David S. Millerca6b8bb2011-02-03 17:24:28 -08001470 case SIOCGETVIFCNT:
1471 if (copy_from_user(&vr, arg, sizeof(vr)))
1472 return -EFAULT;
1473 if (vr.vifi >= mrt->maxvif)
1474 return -EINVAL;
1475 read_lock(&mrt_lock);
1476 vif = &mrt->vif_table[vr.vifi];
1477 if (VIF_EXISTS(mrt, vr.vifi)) {
1478 vr.icount = vif->pkt_in;
1479 vr.ocount = vif->pkt_out;
1480 vr.ibytes = vif->bytes_in;
1481 vr.obytes = vif->bytes_out;
1482 read_unlock(&mrt_lock);
1483
1484 if (copy_to_user(arg, &vr, sizeof(vr)))
1485 return -EFAULT;
1486 return 0;
1487 }
1488 read_unlock(&mrt_lock);
1489 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001490 case SIOCGETSGCNT:
1491 if (copy_from_user(&sr, arg, sizeof(sr)))
1492 return -EFAULT;
1493
1494 rcu_read_lock();
1495 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1496 if (c) {
1497 sr.pktcnt = c->mfc_un.res.pkt;
1498 sr.bytecnt = c->mfc_un.res.bytes;
1499 sr.wrong_if = c->mfc_un.res.wrong_if;
1500 rcu_read_unlock();
1501
1502 if (copy_to_user(arg, &sr, sizeof(sr)))
1503 return -EFAULT;
1504 return 0;
1505 }
1506 rcu_read_unlock();
1507 return -EADDRNOTAVAIL;
1508 default:
1509 return -ENOIOCTLCMD;
1510 }
1511}
1512#endif
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1516{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001517 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001518 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001519 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 struct vif_device *v;
1521 int ct;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001522 LIST_HEAD(list);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (event != NETDEV_UNREGISTER)
1525 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001526
1527 ipmr_for_each_table(mrt, net) {
1528 v = &mrt->vif_table[0];
1529 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1530 if (v->dev == dev)
1531 vif_delete(mrt, ct, 1, &list);
1532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001534 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return NOTIFY_DONE;
1536}
1537
1538
Jianjun Kongc354e122008-11-03 00:28:02 -08001539static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 .notifier_call = ipmr_device_event,
1541};
1542
1543/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001544 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 * This avoids tunnel drivers and other mess and gives us the speed so
1546 * important for multicast video.
1547 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001548
Al Viro114c7842006-09-27 18:39:29 -07001549static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001551 struct iphdr *iph;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001552 struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001553
1554 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001555 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001556 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001557 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001559 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001560 iph->tos = old_iph->tos;
1561 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 iph->frag_off = 0;
1563 iph->daddr = daddr;
1564 iph->saddr = saddr;
1565 iph->protocol = IPPROTO_IPIP;
1566 iph->ihl = 5;
1567 iph->tot_len = htons(skb->len);
Eric Dumazetadf30902009-06-02 05:19:30 +00001568 ip_select_ident(iph, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 ip_send_check(iph);
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1572 nf_reset(skb);
1573}
1574
1575static inline int ipmr_forward_finish(struct sk_buff *skb)
1576{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001577 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Eric Dumazetadf30902009-06-02 05:19:30 +00001579 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 if (unlikely(opt->optlen))
1582 ip_forward_options(skb);
1583
1584 return dst_output(skb);
1585}
1586
1587/*
1588 * Processing handlers for ipmr_forward
1589 */
1590
Patrick McHardy0c122952010-04-13 05:03:22 +00001591static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1592 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001594 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001595 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 struct net_device *dev;
1597 struct rtable *rt;
1598 int encap = 0;
1599
1600 if (vif->dev == NULL)
1601 goto out_free;
1602
1603#ifdef CONFIG_IP_PIMSM
1604 if (vif->flags & VIFF_REGISTER) {
1605 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001606 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001607 vif->dev->stats.tx_bytes += skb->len;
1608 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001609 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001610 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612#endif
1613
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001614 if (vif->flags & VIFF_TUNNEL) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001615 rt = ip_route_output_ports(net, NULL,
1616 vif->remote, vif->local,
1617 0, 0,
1618 IPPROTO_IPIP,
1619 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001620 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 goto out_free;
1622 encap = sizeof(struct iphdr);
1623 } else {
David S. Miller78fbfd82011-03-12 00:00:52 -05001624 rt = ip_route_output_ports(net, NULL, iph->daddr, 0,
1625 0, 0,
1626 IPPROTO_IPIP,
1627 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001628 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 goto out_free;
1630 }
1631
Changli Gaod8d1f302010-06-10 23:31:35 -07001632 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Changli Gaod8d1f302010-06-10 23:31:35 -07001634 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001636 * allow to send ICMP, so that packets will disappear
1637 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 */
1639
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001640 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 ip_rt_put(rt);
1642 goto out_free;
1643 }
1644
Changli Gaod8d1f302010-06-10 23:31:35 -07001645 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001648 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 goto out_free;
1650 }
1651
1652 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001653 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Eric Dumazetadf30902009-06-02 05:19:30 +00001655 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001656 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001657 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001660 * What do we do with netfilter? -- RR
1661 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (vif->flags & VIFF_TUNNEL) {
1663 ip_encap(skb, vif->local, vif->remote);
1664 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001665 vif->dev->stats.tx_packets++;
1666 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
1669 IPCB(skb)->flags |= IPSKB_FORWARDED;
1670
1671 /*
1672 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1673 * not only before forwarding, but after forwarding on all output
1674 * interfaces. It is clear, if mrouter runs a multicasting
1675 * program, it should receive packets not depending to what interface
1676 * program is joined.
1677 * If we will not make it, the program will have to join on all
1678 * interfaces. On the other hand, multihoming host (or router, but
1679 * not mrouter) cannot join to more than one interface - it will
1680 * result in receiving multiple packets.
1681 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001682 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 ipmr_forward_finish);
1684 return;
1685
1686out_free:
1687 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Patrick McHardy0c122952010-04-13 05:03:22 +00001690static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
1692 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001693
1694 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1695 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 break;
1697 }
1698 return ct;
1699}
1700
1701/* "local" means that we should preserve one skb (for local delivery) */
1702
Patrick McHardy0c122952010-04-13 05:03:22 +00001703static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1704 struct sk_buff *skb, struct mfc_cache *cache,
1705 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 int psend = -1;
1708 int vif, ct;
1709
1710 vif = cache->mfc_parent;
1711 cache->mfc_un.res.pkt++;
1712 cache->mfc_un.res.bytes += skb->len;
1713
1714 /*
1715 * Wrong interface: drop packet and (maybe) send PIM assert.
1716 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001717 if (mrt->vif_table[vif].dev != skb->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 int true_vifi;
1719
David S. Millerc7537962010-11-11 17:07:48 -08001720 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001722 * Very complicated situation...
1723 *
1724 * The best workaround until routing daemons will be
1725 * fixed is not to redistribute packet, if it was
1726 * send through wrong interface. It means, that
1727 * multicast applications WILL NOT work for
1728 * (S,G), which have default multicast route pointing
1729 * to wrong oif. In any case, it is not a good
1730 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 */
1732 goto dont_forward;
1733 }
1734
1735 cache->mfc_un.res.wrong_if++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001736 true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Patrick McHardy0c122952010-04-13 05:03:22 +00001738 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001740 * so that we cannot check that packet arrived on an oif.
1741 * It is bad, but otherwise we would need to move pretty
1742 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001744 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001745 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001746 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1748 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001749 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751 goto dont_forward;
1752 }
1753
Patrick McHardy0c122952010-04-13 05:03:22 +00001754 mrt->vif_table[vif].pkt_in++;
1755 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /*
1758 * Forward the frame
1759 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001760 for (ct = cache->mfc_un.res.maxvif - 1;
1761 ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001762 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (psend != -1) {
1764 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001767 ipmr_queue_xmit(net, mrt, skb2, cache,
1768 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001770 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772 }
1773 if (psend != -1) {
1774 if (local) {
1775 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001778 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001780 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 return 0;
1782 }
1783 }
1784
1785dont_forward:
1786 if (!local)
1787 kfree_skb(skb);
1788 return 0;
1789}
1790
David S. Milleree3f1aa2011-03-09 14:06:20 -08001791static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct rtable *rt)
1792{
David S. Millerda919812011-03-12 02:04:50 -05001793 struct flowi4 fl4 = {
1794 .daddr = rt->rt_key_dst,
1795 .saddr = rt->rt_key_src,
1796 .flowi4_tos = rt->rt_tos,
1797 .flowi4_oif = rt->rt_oif,
1798 .flowi4_iif = rt->rt_iif,
1799 .flowi4_mark = rt->rt_mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001800 };
1801 struct mr_table *mrt;
1802 int err;
1803
David S. Millerda919812011-03-12 02:04:50 -05001804 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001805 if (err)
1806 return ERR_PTR(err);
1807 return mrt;
1808}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810/*
1811 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001812 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
1814
1815int ip_mr_input(struct sk_buff *skb)
1816{
1817 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001818 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001819 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001820 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001823 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001825 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto dont_forward;
1827
David S. Milleree3f1aa2011-03-09 14:06:20 -08001828 mrt = ipmr_rt_fib_lookup(net, skb_rtable(skb));
1829 if (IS_ERR(mrt)) {
1830 kfree_skb(skb);
1831 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001834 if (IPCB(skb)->opt.router_alert) {
1835 if (ip_call_ra_chain(skb))
1836 return 0;
1837 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1838 /* IGMPv1 (and broken IGMPv2 implementations sort of
1839 * Cisco IOS <= 11.2(8)) do not put router alert
1840 * option to IGMP packets destined to routable
1841 * groups. It is very bad, because it means
1842 * that we can forward NO IGMP messages.
1843 */
1844 struct sock *mroute_sk;
1845
1846 mroute_sk = rcu_dereference(mrt->mroute_sk);
1847 if (mroute_sk) {
1848 nf_reset(skb);
1849 raw_rcv(mroute_sk, skb);
1850 return 0;
1851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
1853 }
1854
Eric Dumazeta8c94862010-10-01 16:15:08 +00001855 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001856 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /*
1859 * No usable cache entry
1860 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001861 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 int vif;
1863
1864 if (local) {
1865 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1866 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001867 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 skb = skb2;
1870 }
1871
Eric Dumazeta8c94862010-10-01 16:15:08 +00001872 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001873 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001875 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 read_unlock(&mrt_lock);
1877
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001878 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880 read_unlock(&mrt_lock);
1881 kfree_skb(skb);
1882 return -ENODEV;
1883 }
1884
Eric Dumazeta8c94862010-10-01 16:15:08 +00001885 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001886 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 read_unlock(&mrt_lock);
1888
1889 if (local)
1890 return ip_local_deliver(skb);
1891
1892 return 0;
1893
1894dont_forward:
1895 if (local)
1896 return ip_local_deliver(skb);
1897 kfree_skb(skb);
1898 return 0;
1899}
1900
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001901#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00001902/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001903static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1904 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001906 struct net_device *reg_dev = NULL;
1907 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001909 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001911 * Check that:
1912 * a. packet is really sent to a multicast group
1913 * b. packet is not a NULL-REGISTER
1914 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001916 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001918 ntohs(encap->tot_len) + pimlen > skb->len)
1919 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001922 if (mrt->mroute_reg_vif_num >= 0)
1923 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 read_unlock(&mrt_lock);
1925
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001926 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001927 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001929 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00001930 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001931 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00001933 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07001935
1936 skb_tunnel_rx(skb, reg_dev);
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001939
Eric Dumazet55747a02010-10-01 16:14:55 +00001940 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001941}
1942#endif
1943
1944#ifdef CONFIG_IP_PIMSM_V1
1945/*
1946 * Handle IGMP messages of PIMv1
1947 */
1948
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001949int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001950{
1951 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001952 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001953 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001954
1955 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1956 goto drop;
1957
1958 pim = igmp_hdr(skb);
1959
David S. Milleree3f1aa2011-03-09 14:06:20 -08001960 mrt = ipmr_rt_fib_lookup(net, skb_rtable(skb));
1961 if (IS_ERR(mrt))
1962 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00001963 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001964 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1965 goto drop;
1966
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001967 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001968drop:
1969 kfree_skb(skb);
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 return 0;
1972}
1973#endif
1974
1975#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001976static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001979 struct net *net = dev_net(skb->dev);
1980 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001982 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto drop;
1984
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001985 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001986 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1987 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001988 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08001989 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 goto drop;
1991
David S. Milleree3f1aa2011-03-09 14:06:20 -08001992 mrt = ipmr_rt_fib_lookup(net, skb_rtable(skb));
1993 if (IS_ERR(mrt))
1994 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001995 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001996drop:
1997 kfree_skb(skb);
1998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return 0;
2000}
2001#endif
2002
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002003static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2004 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 int ct;
2007 struct rtnexthop *nhp;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002008 u8 *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct rtattr *mp_head;
2010
Nicolas Dichtel74381892010-03-25 23:45:35 +00002011 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f1602010-05-26 00:38:56 -07002012 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002013 return -ENOENT;
2014
Patrick McHardy0c122952010-04-13 05:03:22 +00002015 if (VIF_EXISTS(mrt, c->mfc_parent))
2016 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Jianjun Kongc354e122008-11-03 00:28:02 -08002018 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002021 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2023 goto rtattr_failure;
Jianjun Kongc354e122008-11-03 00:28:02 -08002024 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 nhp->rtnh_flags = 0;
2026 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002027 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 nhp->rtnh_len = sizeof(*nhp);
2029 }
2030 }
2031 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002032 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 rtm->rtm_type = RTN_MULTICAST;
2034 return 1;
2035
2036rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002037 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return -EMSGSIZE;
2039}
2040
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002041int ipmr_get_route(struct net *net,
2042 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 int err;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002045 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 struct mfc_cache *cache;
Eric Dumazet511c3f92009-06-02 05:14:27 +00002047 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002049 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2050 if (mrt == NULL)
2051 return -ENOENT;
2052
Eric Dumazeta8c94862010-10-01 16:15:08 +00002053 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002054 cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Jianjun Kongc354e122008-11-03 00:28:02 -08002056 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002057 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002058 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002060 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002063 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return -EAGAIN;
2065 }
2066
2067 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002068 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002069 if (dev)
2070 vif = ipmr_find_vif(mrt, dev);
2071 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002073 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return -ENODEV;
2075 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002076 skb2 = skb_clone(skb, GFP_ATOMIC);
2077 if (!skb2) {
2078 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002079 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002080 return -ENOMEM;
2081 }
2082
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002083 skb_push(skb2, sizeof(struct iphdr));
2084 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002085 iph = ip_hdr(skb2);
2086 iph->ihl = sizeof(struct iphdr) >> 2;
2087 iph->saddr = rt->rt_src;
2088 iph->daddr = rt->rt_dst;
2089 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002090 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002092 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return err;
2094 }
2095
Eric Dumazeta8c94862010-10-01 16:15:08 +00002096 read_lock(&mrt_lock);
2097 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002099 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002101 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return err;
2103}
2104
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002105static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2106 u32 pid, u32 seq, struct mfc_cache *c)
2107{
2108 struct nlmsghdr *nlh;
2109 struct rtmsg *rtm;
2110
2111 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2112 if (nlh == NULL)
2113 return -EMSGSIZE;
2114
2115 rtm = nlmsg_data(nlh);
2116 rtm->rtm_family = RTNL_FAMILY_IPMR;
2117 rtm->rtm_dst_len = 32;
2118 rtm->rtm_src_len = 32;
2119 rtm->rtm_tos = 0;
2120 rtm->rtm_table = mrt->id;
2121 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2122 rtm->rtm_type = RTN_MULTICAST;
2123 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2124 rtm->rtm_protocol = RTPROT_UNSPEC;
2125 rtm->rtm_flags = 0;
2126
2127 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2128 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2129
2130 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2131 goto nla_put_failure;
2132
2133 return nlmsg_end(skb, nlh);
2134
2135nla_put_failure:
2136 nlmsg_cancel(skb, nlh);
2137 return -EMSGSIZE;
2138}
2139
2140static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2141{
2142 struct net *net = sock_net(skb->sk);
2143 struct mr_table *mrt;
2144 struct mfc_cache *mfc;
2145 unsigned int t = 0, s_t;
2146 unsigned int h = 0, s_h;
2147 unsigned int e = 0, s_e;
2148
2149 s_t = cb->args[0];
2150 s_h = cb->args[1];
2151 s_e = cb->args[2];
2152
Eric Dumazeta8c94862010-10-01 16:15:08 +00002153 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002154 ipmr_for_each_table(mrt, net) {
2155 if (t < s_t)
2156 goto next_table;
2157 if (t > s_t)
2158 s_h = 0;
2159 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002160 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002161 if (e < s_e)
2162 goto next_entry;
2163 if (ipmr_fill_mroute(mrt, skb,
2164 NETLINK_CB(cb->skb).pid,
2165 cb->nlh->nlmsg_seq,
2166 mfc) < 0)
2167 goto done;
2168next_entry:
2169 e++;
2170 }
2171 e = s_e = 0;
2172 }
2173 s_h = 0;
2174next_table:
2175 t++;
2176 }
2177done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002178 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002179
2180 cb->args[2] = e;
2181 cb->args[1] = h;
2182 cb->args[0] = t;
2183
2184 return skb->len;
2185}
2186
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002187#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002189 * The /proc interfaces to multicast routing :
2190 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 */
2192struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002193 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002194 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 int ct;
2196};
2197
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002198static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2199 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 loff_t pos)
2201{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002202 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002203
2204 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2205 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002207 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002208 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
2210 return NULL;
2211}
2212
2213static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002214 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002216 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002217 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002218 struct mr_table *mrt;
2219
2220 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2221 if (mrt == NULL)
2222 return ERR_PTR(-ENOENT);
2223
2224 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002227 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 : SEQ_START_TOKEN;
2229}
2230
2231static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2232{
2233 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002234 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002235 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 ++*pos;
2238 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002239 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002240
Patrick McHardy0c122952010-04-13 05:03:22 +00002241 while (++iter->ct < mrt->maxvif) {
2242 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002244 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246 return NULL;
2247}
2248
2249static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002250 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 read_unlock(&mrt_lock);
2253}
2254
2255static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2256{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002257 struct ipmr_vif_iter *iter = seq->private;
2258 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002261 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2263 } else {
2264 const struct vif_device *vif = v;
2265 const char *name = vif->dev ? vif->dev->name : "none";
2266
2267 seq_printf(seq,
2268 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002269 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002270 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 vif->bytes_out, vif->pkt_out,
2272 vif->flags, vif->local, vif->remote);
2273 }
2274 return 0;
2275}
2276
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002277static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 .start = ipmr_vif_seq_start,
2279 .next = ipmr_vif_seq_next,
2280 .stop = ipmr_vif_seq_stop,
2281 .show = ipmr_vif_seq_show,
2282};
2283
2284static int ipmr_vif_open(struct inode *inode, struct file *file)
2285{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002286 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2287 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Arjan van de Ven9a321442007-02-12 00:55:35 -08002290static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 .owner = THIS_MODULE,
2292 .open = ipmr_vif_open,
2293 .read = seq_read,
2294 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002295 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296};
2297
2298struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002299 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002300 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002301 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 int ct;
2303};
2304
2305
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002306static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2307 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002309 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 struct mfc_cache *mfc;
2311
Eric Dumazeta8c94862010-10-01 16:15:08 +00002312 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002313 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002314 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002315 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002316 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002318 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002319 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002322 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002323 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002324 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return mfc;
2326 spin_unlock_bh(&mfc_unres_lock);
2327
2328 it->cache = NULL;
2329 return NULL;
2330}
2331
2332
2333static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2334{
2335 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002336 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002337 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002338
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002339 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2340 if (mrt == NULL)
2341 return ERR_PTR(-ENOENT);
2342
2343 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 it->cache = NULL;
2345 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002346 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 : SEQ_START_TOKEN;
2348}
2349
2350static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2351{
2352 struct mfc_cache *mfc = v;
2353 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002354 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002355 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 ++*pos;
2358
2359 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002360 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Patrick McHardy862465f2010-04-13 05:03:21 +00002362 if (mfc->list.next != it->cache)
2363 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002364
Patrick McHardy0c122952010-04-13 05:03:22 +00002365 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 goto end_of_list;
2367
Patrick McHardy0c122952010-04-13 05:03:22 +00002368 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002371 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002372 if (list_empty(it->cache))
2373 continue;
2374 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376
2377 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002378 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002379 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002383 if (!list_empty(it->cache))
2384 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002386end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 spin_unlock_bh(&mfc_unres_lock);
2388 it->cache = NULL;
2389
2390 return NULL;
2391}
2392
2393static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2394{
2395 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002396 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Patrick McHardy0c122952010-04-13 05:03:22 +00002398 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002400 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002401 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
2404static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2405{
2406 int n;
2407
2408 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002409 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2411 } else {
2412 const struct mfc_cache *mfc = v;
2413 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002414 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002415
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002416 seq_printf(seq, "%08X %08X %-3hd",
2417 (__force u32) mfc->mfc_mcastgrp,
2418 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002419 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Patrick McHardy0c122952010-04-13 05:03:22 +00002421 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002422 seq_printf(seq, " %8lu %8lu %8lu",
2423 mfc->mfc_un.res.pkt,
2424 mfc->mfc_un.res.bytes,
2425 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002426 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002427 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002428 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002429 mfc->mfc_un.res.ttls[n] < 255)
2430 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002431 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 n, mfc->mfc_un.res.ttls[n]);
2433 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002434 } else {
2435 /* unresolved mfc_caches don't contain
2436 * pkt, bytes and wrong_if values
2437 */
2438 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440 seq_putc(seq, '\n');
2441 }
2442 return 0;
2443}
2444
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002445static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 .start = ipmr_mfc_seq_start,
2447 .next = ipmr_mfc_seq_next,
2448 .stop = ipmr_mfc_seq_stop,
2449 .show = ipmr_mfc_seq_show,
2450};
2451
2452static int ipmr_mfc_open(struct inode *inode, struct file *file)
2453{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002454 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2455 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Arjan van de Ven9a321442007-02-12 00:55:35 -08002458static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 .owner = THIS_MODULE,
2460 .open = ipmr_mfc_open,
2461 .read = seq_read,
2462 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002463 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002468static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002470 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471};
2472#endif
2473
2474
2475/*
2476 * Setup for IP multicast routing
2477 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002478static int __net_init ipmr_net_init(struct net *net)
2479{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002480 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002481
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002482 err = ipmr_rules_init(net);
2483 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002484 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002485
2486#ifdef CONFIG_PROC_FS
2487 err = -ENOMEM;
2488 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2489 goto proc_vif_fail;
2490 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2491 goto proc_cache_fail;
2492#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002493 return 0;
2494
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002495#ifdef CONFIG_PROC_FS
2496proc_cache_fail:
2497 proc_net_remove(net, "ip_mr_vif");
2498proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002499 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002500#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002501fail:
2502 return err;
2503}
2504
2505static void __net_exit ipmr_net_exit(struct net *net)
2506{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002507#ifdef CONFIG_PROC_FS
2508 proc_net_remove(net, "ip_mr_cache");
2509 proc_net_remove(net, "ip_mr_vif");
2510#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002511 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002512}
2513
2514static struct pernet_operations ipmr_net_ops = {
2515 .init = ipmr_net_init,
2516 .exit = ipmr_net_exit,
2517};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002518
Wang Chen03d2f892008-07-03 12:13:36 +08002519int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Wang Chen03d2f892008-07-03 12:13:36 +08002521 int err;
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2524 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002525 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002526 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002527 if (!mrt_cachep)
2528 return -ENOMEM;
2529
Benjamin Therycf958ae32009-01-22 04:56:16 +00002530 err = register_pernet_subsys(&ipmr_net_ops);
2531 if (err)
2532 goto reg_pernet_fail;
2533
Wang Chen03d2f892008-07-03 12:13:36 +08002534 err = register_netdevice_notifier(&ip_mr_notifier);
2535 if (err)
2536 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002537#ifdef CONFIG_IP_PIMSM_V2
2538 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2539 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2540 err = -EAGAIN;
2541 goto add_proto_fail;
2542 }
2543#endif
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002544 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, NULL, ipmr_rtm_dumproute);
Wang Chen03d2f892008-07-03 12:13:36 +08002545 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002546
Tom Goff403dbb92009-06-14 03:16:13 -07002547#ifdef CONFIG_IP_PIMSM_V2
2548add_proto_fail:
2549 unregister_netdevice_notifier(&ip_mr_notifier);
2550#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002551reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002552 unregister_pernet_subsys(&ipmr_net_ops);
2553reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002554 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002555 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}