blob: 7bc2db6db8d407987bbd06f59a25cf1b0f488989 [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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040064#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/ipip.h>
66#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070067#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000068#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71#define CONFIG_IP_PIMSM 1
72#endif
73
Patrick McHardy0c122952010-04-13 05:03:22 +000074struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000075 struct list_head list;
Patrick McHardy8de53df2010-04-15 13:29:28 +020076#ifdef CONFIG_NET_NS
77 struct net *net;
78#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +000079 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000080 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000081 struct timer_list ipmr_expire_timer;
82 struct list_head mfc_unres_queue;
83 struct list_head mfc_cache_array[MFC_LINES];
84 struct vif_device vif_table[MAXVIFS];
85 int maxvif;
86 atomic_t cache_resolve_queue_len;
87 int mroute_do_assert;
88 int mroute_do_pim;
89#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num;
91#endif
92};
93
Patrick McHardyf0ad0862010-04-13 05:03:23 +000094struct ipmr_rule {
95 struct fib_rule common;
96};
97
98struct ipmr_result {
99 struct mr_table *mrt;
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000103 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
105
106static DEFINE_RWLOCK(mrt_lock);
107
108/*
109 * Multicast router control variables
110 */
111
Patrick McHardy0c122952010-04-13 05:03:22 +0000112#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* Special spinlock for queue of unresolved entries */
115static DEFINE_SPINLOCK(mfc_unres_lock);
116
117/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
121 *
122 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124
Christoph Lametere18b8902006-12-06 20:33:20 -0800125static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000127static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Patrick McHardy0c122952010-04-13 05:03:22 +0000128static int ip_mr_forward(struct net *net, struct mr_table *mrt,
129 struct sk_buff *skb, struct mfc_cache *cache,
130 int local);
131static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000132 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200133static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
134 struct mfc_cache *c, struct rtmsg *rtm);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000135static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000137#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
138#define ipmr_for_each_table(mrt, net) \
139 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
140
141static struct mr_table *ipmr_get_table(struct net *net, u32 id)
142{
143 struct mr_table *mrt;
144
145 ipmr_for_each_table(mrt, net) {
146 if (mrt->id == id)
147 return mrt;
148 }
149 return NULL;
150}
151
David S. Millerda919812011-03-12 02:04:50 -0500152static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000153 struct mr_table **mrt)
154{
155 struct ipmr_result res;
156 struct fib_lookup_arg arg = { .result = &res, };
157 int err;
158
David S. Millerda919812011-03-12 02:04:50 -0500159 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
160 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000161 if (err < 0)
162 return err;
163 *mrt = res.mrt;
164 return 0;
165}
166
167static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
168 int flags, struct fib_lookup_arg *arg)
169{
170 struct ipmr_result *res = arg->result;
171 struct mr_table *mrt;
172
173 switch (rule->action) {
174 case FR_ACT_TO_TBL:
175 break;
176 case FR_ACT_UNREACHABLE:
177 return -ENETUNREACH;
178 case FR_ACT_PROHIBIT:
179 return -EACCES;
180 case FR_ACT_BLACKHOLE:
181 default:
182 return -EINVAL;
183 }
184
185 mrt = ipmr_get_table(rule->fr_net, rule->table);
186 if (mrt == NULL)
187 return -EAGAIN;
188 res->mrt = mrt;
189 return 0;
190}
191
192static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
193{
194 return 1;
195}
196
197static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
198 FRA_GENERIC_POLICY,
199};
200
201static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
202 struct fib_rule_hdr *frh, struct nlattr **tb)
203{
204 return 0;
205}
206
207static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
208 struct nlattr **tb)
209{
210 return 1;
211}
212
213static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
214 struct fib_rule_hdr *frh)
215{
216 frh->dst_len = 0;
217 frh->src_len = 0;
218 frh->tos = 0;
219 return 0;
220}
221
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200222static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200223 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000224 .rule_size = sizeof(struct ipmr_rule),
225 .addr_size = sizeof(u32),
226 .action = ipmr_rule_action,
227 .match = ipmr_rule_match,
228 .configure = ipmr_rule_configure,
229 .compare = ipmr_rule_compare,
230 .default_pref = fib_default_rule_pref,
231 .fill = ipmr_rule_fill,
232 .nlgroup = RTNLGRP_IPV4_RULE,
233 .policy = ipmr_rule_policy,
234 .owner = THIS_MODULE,
235};
236
237static int __net_init ipmr_rules_init(struct net *net)
238{
239 struct fib_rules_ops *ops;
240 struct mr_table *mrt;
241 int err;
242
243 ops = fib_rules_register(&ipmr_rules_ops_template, net);
244 if (IS_ERR(ops))
245 return PTR_ERR(ops);
246
247 INIT_LIST_HEAD(&net->ipv4.mr_tables);
248
249 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
250 if (mrt == NULL) {
251 err = -ENOMEM;
252 goto err1;
253 }
254
255 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
256 if (err < 0)
257 goto err2;
258
259 net->ipv4.mr_rules_ops = ops;
260 return 0;
261
262err2:
263 kfree(mrt);
264err1:
265 fib_rules_unregister(ops);
266 return err;
267}
268
269static void __net_exit ipmr_rules_exit(struct net *net)
270{
271 struct mr_table *mrt, *next;
272
Eric Dumazet035320d2010-06-06 23:48:40 +0000273 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
274 list_del(&mrt->list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000275 kfree(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000276 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000277 fib_rules_unregister(net->ipv4.mr_rules_ops);
278}
279#else
280#define ipmr_for_each_table(mrt, net) \
281 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
282
283static struct mr_table *ipmr_get_table(struct net *net, u32 id)
284{
285 return net->ipv4.mrt;
286}
287
David S. Millerda919812011-03-12 02:04:50 -0500288static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000289 struct mr_table **mrt)
290{
291 *mrt = net->ipv4.mrt;
292 return 0;
293}
294
295static int __net_init ipmr_rules_init(struct net *net)
296{
297 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
298 return net->ipv4.mrt ? 0 : -ENOMEM;
299}
300
301static void __net_exit ipmr_rules_exit(struct net *net)
302{
303 kfree(net->ipv4.mrt);
304}
305#endif
306
307static struct mr_table *ipmr_new_table(struct net *net, u32 id)
308{
309 struct mr_table *mrt;
310 unsigned int i;
311
312 mrt = ipmr_get_table(net, id);
313 if (mrt != NULL)
314 return mrt;
315
316 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
317 if (mrt == NULL)
318 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200319 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000320 mrt->id = id;
321
322 /* Forwarding cache */
323 for (i = 0; i < MFC_LINES; i++)
324 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
325
326 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
327
328 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
329 (unsigned long)mrt);
330
331#ifdef CONFIG_IP_PIMSM
332 mrt->mroute_reg_vif_num = -1;
333#endif
334#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
335 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
336#endif
337 return mrt;
338}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
341
Wang Chend6070322008-07-14 20:55:26 -0700342static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
343{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000344 struct net *net = dev_net(dev);
345
Wang Chend6070322008-07-14 20:55:26 -0700346 dev_close(dev);
347
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000348 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700349 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800350 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700351 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700352 struct ip_tunnel_parm p;
353
354 memset(&p, 0, sizeof(p));
355 p.iph.daddr = v->vifc_rmt_addr.s_addr;
356 p.iph.saddr = v->vifc_lcl_addr.s_addr;
357 p.iph.version = 4;
358 p.iph.ihl = 5;
359 p.iph.protocol = IPPROTO_IPIP;
360 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
361 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
362
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800363 if (ops->ndo_do_ioctl) {
364 mm_segment_t oldfs = get_fs();
365
366 set_fs(KERNEL_DS);
367 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
368 set_fs(oldfs);
369 }
Wang Chend6070322008-07-14 20:55:26 -0700370 }
371}
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000374struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct net_device *dev;
377
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000378 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800381 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int err;
383 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct ip_tunnel_parm p;
385 struct in_device *in_dev;
386
387 memset(&p, 0, sizeof(p));
388 p.iph.daddr = v->vifc_rmt_addr.s_addr;
389 p.iph.saddr = v->vifc_lcl_addr.s_addr;
390 p.iph.version = 4;
391 p.iph.ihl = 5;
392 p.iph.protocol = IPPROTO_IPIP;
393 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800394 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800396 if (ops->ndo_do_ioctl) {
397 mm_segment_t oldfs = get_fs();
398
399 set_fs(KERNEL_DS);
400 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
401 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000402 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800403 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 dev = NULL;
406
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000407 if (err == 0 &&
408 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 dev->flags |= IFF_MULTICAST;
410
Herbert Xue5ed6392005-10-03 14:35:55 -0700411 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700412 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700414
415 ipv4_devconf_setall(in_dev);
416 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (dev_open(dev))
419 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700420 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 }
423 return dev;
424
425failure:
426 /* allow the register to be completed before unregistering. */
427 rtnl_unlock();
428 rtnl_lock();
429
430 unregister_netdevice(dev);
431 return NULL;
432}
433
434#ifdef CONFIG_IP_PIMSM
435
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000436static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000438 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000439 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500440 struct flowi4 fl4 = {
441 .flowi4_oif = dev->ifindex,
442 .flowi4_iif = skb->skb_iif,
443 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000444 };
445 int err;
446
David S. Millerda919812011-03-12 02:04:50 -0500447 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000448 if (err < 0) {
449 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000450 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000451 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700454 dev->stats.tx_bytes += skb->len;
455 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000456 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 read_unlock(&mrt_lock);
458 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000459 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Stephen Hemminger007c3832008-11-20 20:28:35 -0800462static const struct net_device_ops reg_vif_netdev_ops = {
463 .ndo_start_xmit = reg_vif_xmit,
464};
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static void reg_vif_setup(struct net_device *dev)
467{
468 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800469 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800471 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700473 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000476static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct net_device *dev;
479 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000480 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000482 if (mrt->id == RT_TABLE_DEFAULT)
483 sprintf(name, "pimreg");
484 else
485 sprintf(name, "pimreg%u", mrt->id);
486
487 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (dev == NULL)
490 return NULL;
491
Tom Goff403dbb92009-06-14 03:16:13 -0700492 dev_net_set(dev, net);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (register_netdevice(dev)) {
495 free_netdev(dev);
496 return NULL;
497 }
498 dev->iflink = 0;
499
Herbert Xu71e27da2007-06-04 23:36:06 -0700500 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000501 in_dev = __in_dev_get_rcu(dev);
502 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700503 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Herbert Xu71e27da2007-06-04 23:36:06 -0700507 ipv4_devconf_setall(in_dev);
508 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
509 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (dev_open(dev))
512 goto failure;
513
Wang Chen7dc00c82008-07-14 20:56:34 -0700514 dev_hold(dev);
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return dev;
517
518failure:
519 /* allow the register to be completed before unregistering. */
520 rtnl_unlock();
521 rtnl_lock();
522
523 unregister_netdevice(dev);
524 return NULL;
525}
526#endif
527
528/*
529 * Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700530 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900532
Patrick McHardy0c122952010-04-13 05:03:22 +0000533static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000534 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct vif_device *v;
537 struct net_device *dev;
538 struct in_device *in_dev;
539
Patrick McHardy0c122952010-04-13 05:03:22 +0000540 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -EADDRNOTAVAIL;
542
Patrick McHardy0c122952010-04-13 05:03:22 +0000543 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 write_lock_bh(&mrt_lock);
546 dev = v->dev;
547 v->dev = NULL;
548
549 if (!dev) {
550 write_unlock_bh(&mrt_lock);
551 return -EADDRNOTAVAIL;
552 }
553
554#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000555 if (vifi == mrt->mroute_reg_vif_num)
556 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#endif
558
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000559 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000561
562 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000563 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000566 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569 write_unlock_bh(&mrt_lock);
570
571 dev_set_allmulti(dev, -1);
572
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000573 in_dev = __in_dev_get_rtnl(dev);
574 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700575 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 ip_rt_multicast_event(in_dev);
577 }
578
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000579 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000580 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 dev_put(dev);
583 return 0;
584}
585
Eric Dumazeta8c94862010-10-01 16:15:08 +0000586static void ipmr_cache_free_rcu(struct rcu_head *head)
587{
588 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
589
590 kmem_cache_free(mrt_cachep, c);
591}
592
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000593static inline void ipmr_cache_free(struct mfc_cache *c)
594{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000595 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000599 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601
Patrick McHardy0c122952010-04-13 05:03:22 +0000602static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200604 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700606 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Patrick McHardy0c122952010-04-13 05:03:22 +0000608 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Jianjun Kongc354e122008-11-03 00:28:02 -0800610 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700611 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
613 nlh->nlmsg_type = NLMSG_ERROR;
614 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
615 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700616 e = NLMSG_DATA(nlh);
617 e->error = -ETIMEDOUT;
618 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700619
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000620 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000621 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000626 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629
Patrick McHardye258beb2010-04-13 05:03:19 +0000630/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Patrick McHardye258beb2010-04-13 05:03:19 +0000632static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Patrick McHardy0c122952010-04-13 05:03:22 +0000634 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 unsigned long now;
636 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000637 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000640 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return;
642 }
643
Patrick McHardy0c122952010-04-13 05:03:22 +0000644 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto out;
646
647 now = jiffies;
648 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Patrick McHardy0c122952010-04-13 05:03:22 +0000650 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (time_after(c->mfc_un.unres.expires, now)) {
652 unsigned long interval = c->mfc_un.unres.expires - now;
653 if (interval < expires)
654 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 continue;
656 }
657
Patrick McHardy862465f2010-04-13 05:03:21 +0000658 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000659 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
Patrick McHardy0c122952010-04-13 05:03:22 +0000662 if (!list_empty(&mrt->mfc_unres_queue))
663 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665out:
666 spin_unlock(&mfc_unres_lock);
667}
668
669/* Fill oifs list. It is called under write locked mrt_lock. */
670
Patrick McHardy0c122952010-04-13 05:03:22 +0000671static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000672 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 int vifi;
675
676 cache->mfc_un.res.minvif = MAXVIFS;
677 cache->mfc_un.res.maxvif = 0;
678 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
679
Patrick McHardy0c122952010-04-13 05:03:22 +0000680 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
681 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000682 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
684 if (cache->mfc_un.res.minvif > vifi)
685 cache->mfc_un.res.minvif = vifi;
686 if (cache->mfc_un.res.maxvif <= vifi)
687 cache->mfc_un.res.maxvif = vifi + 1;
688 }
689 }
690}
691
Patrick McHardy0c122952010-04-13 05:03:22 +0000692static int vif_add(struct net *net, struct mr_table *mrt,
693 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000696 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct net_device *dev;
698 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700699 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000702 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -EADDRINUSE;
704
705 switch (vifc->vifc_flags) {
706#ifdef CONFIG_IP_PIMSM
707 case VIFF_REGISTER:
708 /*
709 * Special Purpose VIF in PIM
710 * All the packets will be sent to the daemon
711 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000712 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000714 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (!dev)
716 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700717 err = dev_set_allmulti(dev, 1);
718 if (err) {
719 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700720 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700721 return err;
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
724#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900725 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000726 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (!dev)
728 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700729 err = dev_set_allmulti(dev, 1);
730 if (err) {
731 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700732 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700733 return err;
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000736
737 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000739 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
740 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000741 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000742 dev_put(dev);
743 return -EADDRNOTAVAIL;
744 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000745 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000746 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (!dev)
749 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700750 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700751 if (err) {
752 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700753 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756 default:
757 return -EINVAL;
758 }
759
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000760 in_dev = __in_dev_get_rtnl(dev);
761 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000762 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000764 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700765 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 ip_rt_multicast_event(in_dev);
767
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000768 /* Fill in the VIF structures */
769
Jianjun Kongc354e122008-11-03 00:28:02 -0800770 v->rate_limit = vifc->vifc_rate_limit;
771 v->local = vifc->vifc_lcl_addr.s_addr;
772 v->remote = vifc->vifc_rmt_addr.s_addr;
773 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!mrtsock)
775 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800776 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 v->bytes_in = 0;
778 v->bytes_out = 0;
779 v->pkt_in = 0;
780 v->pkt_out = 0;
781 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000782 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 v->link = dev->iflink;
784
785 /* And finish update writing critical data */
786 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800787 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000789 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000790 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000792 if (vifi+1 > mrt->maxvif)
793 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 write_unlock_bh(&mrt_lock);
795 return 0;
796}
797
Eric Dumazeta8c94862010-10-01 16:15:08 +0000798/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000799static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000800 __be32 origin,
801 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Jianjun Kongc354e122008-11-03 00:28:02 -0800803 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct mfc_cache *c;
805
Eric Dumazeta8c94862010-10-01 16:15:08 +0000806 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000807 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
808 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000810 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/*
814 * Allocate a multicast cache entry
815 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000816static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Jianjun Kongc354e122008-11-03 00:28:02 -0800818 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000819
820 if (c)
821 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return c;
823}
824
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000825static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Jianjun Kongc354e122008-11-03 00:28:02 -0800827 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000828
829 if (c) {
830 skb_queue_head_init(&c->mfc_un.unres.unresolved);
831 c->mfc_un.unres.expires = jiffies + 10*HZ;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return c;
834}
835
836/*
837 * A cache entry has gone into a resolved state from queued
838 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900839
Patrick McHardy0c122952010-04-13 05:03:22 +0000840static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
841 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700844 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000846 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Jianjun Kongc354e122008-11-03 00:28:02 -0800848 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700849 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
851
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200852 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000853 nlh->nlmsg_len = skb_tail_pointer(skb) -
854 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 } else {
856 nlh->nlmsg_type = NLMSG_ERROR;
857 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
858 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700859 e = NLMSG_DATA(nlh);
860 e->error = -EMSGSIZE;
861 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Thomas Graf2942e902006-08-15 00:30:25 -0700863
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000864 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000865 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000866 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869}
870
871/*
872 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
873 * expects the following bizarre scheme.
874 *
875 * Called under mrt_lock.
876 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900877
Patrick McHardy0c122952010-04-13 05:03:22 +0000878static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000879 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300882 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct igmphdr *igmp;
884 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000885 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 int ret;
887
888#ifdef CONFIG_IP_PIMSM
889 if (assert == IGMPMSG_WHOLEPKT)
890 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
891 else
892#endif
893 skb = alloc_skb(128, GFP_ATOMIC);
894
Stephen Hemminger132adf52007-03-08 20:44:43 -0800895 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return -ENOBUFS;
897
898#ifdef CONFIG_IP_PIMSM
899 if (assert == IGMPMSG_WHOLEPKT) {
900 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000901 * Duplicate old header, fix ihl, length etc.
902 * And all this only to mangle msg->im_msgtype and
903 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300905 skb_push(skb, sizeof(struct iphdr));
906 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300907 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300908 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700909 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 msg->im_msgtype = IGMPMSG_WHOLEPKT;
911 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000912 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700913 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
914 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
915 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900916 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900918 {
919
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000920 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700922 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300923 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300924 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000925 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700926 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000928 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000930 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000932 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 igmp->type =
934 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000935 igmp->code = 0;
936 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700937 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Eric Dumazet4c968702010-10-01 16:15:01 +0000940 rcu_read_lock();
941 mroute_sk = rcu_dereference(mrt->mroute_sk);
942 if (mroute_sk == NULL) {
943 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 kfree_skb(skb);
945 return -EINVAL;
946 }
947
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000948 /* Deliver to mrouted */
949
Eric Dumazet4c968702010-10-01 16:15:01 +0000950 ret = sock_queue_rcv_skb(mroute_sk, skb);
951 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +0000952 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (net_ratelimit())
954 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
955 kfree_skb(skb);
956 }
957
958 return ret;
959}
960
961/*
962 * Queue a packet for resolution. It gets locked cache entry!
963 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static int
Patrick McHardy0c122952010-04-13 05:03:22 +0000966ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Patrick McHardy862465f2010-04-13 05:03:21 +0000968 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int err;
970 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700971 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000974 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000975 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000976 c->mfc_origin == iph->saddr) {
977 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981
Patrick McHardy862465f2010-04-13 05:03:21 +0000982 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000983 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Patrick McHardy0c122952010-04-13 05:03:22 +0000985 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000986 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 spin_unlock_bh(&mfc_unres_lock);
988
989 kfree_skb(skb);
990 return -ENOBUFS;
991 }
992
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000993 /* Fill in the new cache entry */
994
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700995 c->mfc_parent = -1;
996 c->mfc_origin = iph->saddr;
997 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000999 /* Reflect first query at mrouted. */
1000
Patrick McHardy0c122952010-04-13 05:03:22 +00001001 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001002 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001003 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 out - Brad Parker
1005 */
1006 spin_unlock_bh(&mfc_unres_lock);
1007
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001008 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 kfree_skb(skb);
1010 return err;
1011 }
1012
Patrick McHardy0c122952010-04-13 05:03:22 +00001013 atomic_inc(&mrt->cache_resolve_queue_len);
1014 list_add(&c->list, &mrt->mfc_unres_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
David S. Miller278554b2010-05-12 00:05:35 -07001016 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1017 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001020 /* See if we can append the packet */
1021
1022 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 kfree_skb(skb);
1024 err = -ENOBUFS;
1025 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001026 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 err = 0;
1028 }
1029
1030 spin_unlock_bh(&mfc_unres_lock);
1031 return err;
1032}
1033
1034/*
1035 * MFC cache manipulation by user space mroute daemon
1036 */
1037
Patrick McHardy0c122952010-04-13 05:03:22 +00001038static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001041 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Jianjun Kongc354e122008-11-03 00:28:02 -08001043 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Patrick McHardy0c122952010-04-13 05:03:22 +00001045 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1047 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001048 list_del_rcu(&c->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001050 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return 0;
1052 }
1053 }
1054 return -ENOENT;
1055}
1056
Patrick McHardy0c122952010-04-13 05:03:22 +00001057static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1058 struct mfcctl *mfc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Patrick McHardy862465f2010-04-13 05:03:21 +00001060 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001062 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Patrick McHardya50436f2010-03-17 06:04:14 +00001064 if (mfc->mfcc_parent >= MAXVIFS)
1065 return -ENFILE;
1066
Jianjun Kongc354e122008-11-03 00:28:02 -08001067 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Patrick McHardy0c122952010-04-13 05:03:22 +00001069 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001071 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1072 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
Patrick McHardy862465f2010-04-13 05:03:21 +00001077 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 write_lock_bh(&mrt_lock);
1079 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001080 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (!mrtsock)
1082 c->mfc_flags |= MFC_STATIC;
1083 write_unlock_bh(&mrt_lock);
1084 return 0;
1085 }
1086
Joe Perchesf97c1e02007-12-16 13:45:43 -08001087 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -EINVAL;
1089
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001090 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001091 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -ENOMEM;
1093
Jianjun Kongc354e122008-11-03 00:28:02 -08001094 c->mfc_origin = mfc->mfcc_origin.s_addr;
1095 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1096 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001097 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!mrtsock)
1099 c->mfc_flags |= MFC_STATIC;
1100
Eric Dumazeta8c94862010-10-01 16:15:08 +00001101 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /*
1104 * Check to see if we resolved a queued list. If so we
1105 * need to send on the frames and tidy up.
1106 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001107 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001109 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001110 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001112 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001113 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001114 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 break;
1116 }
1117 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001118 if (list_empty(&mrt->mfc_unres_queue))
1119 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 spin_unlock_bh(&mfc_unres_lock);
1121
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001122 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001123 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001124 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126 return 0;
1127}
1128
1129/*
1130 * Close the multicast socket, and clear the vif tables etc
1131 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001132
Patrick McHardy0c122952010-04-13 05:03:22 +00001133static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001136 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001137 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001138
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001139 /* Shut down all active vif entries */
1140
Patrick McHardy0c122952010-04-13 05:03:22 +00001141 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001142 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001143 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001145 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001147 /* Wipe the cache */
1148
Patrick McHardy862465f2010-04-13 05:03:21 +00001149 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001150 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001151 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001153 list_del_rcu(&c->list);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001154 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 }
1157
Patrick McHardy0c122952010-04-13 05:03:22 +00001158 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001160 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001161 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001162 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164 spin_unlock_bh(&mfc_unres_lock);
1165 }
1166}
1167
Eric Dumazet4c968702010-10-01 16:15:01 +00001168/* called from ip_ra_control(), before an RCU grace period,
1169 * we dont need to call synchronize_rcu() here
1170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171static void mrtsock_destruct(struct sock *sk)
1172{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001173 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001174 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001177 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001178 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001179 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001180 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001181 mroute_clean_tables(mrt);
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184 rtnl_unlock();
1185}
1186
1187/*
1188 * Socket options and virtual interface manipulation. The whole
1189 * virtual interface system is a complete heap, but unfortunately
1190 * that's how BSD mrouted happens to think. Maybe one day with a proper
1191 * MOSPF/PIM router set up we can clean this up.
1192 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001193
David S. Millerb7058842009-09-30 16:12:20 -07001194int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 int ret;
1197 struct vifctl vif;
1198 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001199 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001200 struct mr_table *mrt;
1201
1202 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1203 if (mrt == NULL)
1204 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001205
Stephen Hemminger132adf52007-03-08 20:44:43 -08001206 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001207 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric Dumazet4c968702010-10-01 16:15:01 +00001208 !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -EACCES;
1210 }
1211
Stephen Hemminger132adf52007-03-08 20:44:43 -08001212 switch (optname) {
1213 case MRT_INIT:
1214 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001215 inet_sk(sk)->inet_num != IPPROTO_IGMP)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001216 return -EOPNOTSUPP;
Jianjun Kongc354e122008-11-03 00:28:02 -08001217 if (optlen != sizeof(int))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001218 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Stephen Hemminger132adf52007-03-08 20:44:43 -08001220 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001221 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001223 return -EADDRINUSE;
1224 }
1225
1226 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1227 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001228 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001229 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001230 }
1231 rtnl_unlock();
1232 return ret;
1233 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001234 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001235 return -EACCES;
1236 return ip_ra_control(sk, 0, NULL);
1237 case MRT_ADD_VIF:
1238 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001239 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001240 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001241 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001242 return -EFAULT;
1243 if (vif.vifc_vifi >= MAXVIFS)
1244 return -ENFILE;
1245 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001246 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001247 ret = vif_add(net, mrt, &vif,
1248 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001249 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001250 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001251 }
1252 rtnl_unlock();
1253 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 /*
1256 * Manipulate the forwarding caches. These live
1257 * in a sort of kernel/user symbiosis.
1258 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001259 case MRT_ADD_MFC:
1260 case MRT_DEL_MFC:
Jianjun Kongc354e122008-11-03 00:28:02 -08001261 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001262 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001263 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001264 return -EFAULT;
1265 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001266 if (optname == MRT_DEL_MFC)
Patrick McHardy0c122952010-04-13 05:03:22 +00001267 ret = ipmr_mfc_delete(mrt, &mfc);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001268 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001269 ret = ipmr_mfc_add(net, mrt, &mfc,
1270 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001271 rtnl_unlock();
1272 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /*
1274 * Control PIM assert.
1275 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001276 case MRT_ASSERT:
1277 {
1278 int v;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001279 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001280 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001281 mrt->mroute_do_assert = (v) ? 1 : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001282 return 0;
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001285 case MRT_PIM:
1286 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001287 int v;
1288
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001289 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001290 return -EFAULT;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001291 v = (v) ? 1 : 0;
1292
Stephen Hemminger132adf52007-03-08 20:44:43 -08001293 rtnl_lock();
1294 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001295 if (v != mrt->mroute_do_pim) {
1296 mrt->mroute_do_pim = v;
1297 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001299 rtnl_unlock();
1300 return ret;
1301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001303#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1304 case MRT_TABLE:
1305 {
1306 u32 v;
1307
1308 if (optlen != sizeof(u32))
1309 return -EINVAL;
1310 if (get_user(v, (u32 __user *)optval))
1311 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001312
1313 rtnl_lock();
1314 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001315 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1316 ret = -EBUSY;
1317 } else {
1318 if (!ipmr_new_table(net, v))
1319 ret = -ENOMEM;
1320 raw_sk(sk)->ipmr_table = v;
1321 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001322 rtnl_unlock();
1323 return ret;
1324 }
1325#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001326 /*
1327 * Spurious command, or MRT_VERSION which you cannot
1328 * set.
1329 */
1330 default:
1331 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333}
1334
1335/*
1336 * Getsock opt support for the multicast routing system.
1337 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001338
Jianjun Kongc354e122008-11-03 00:28:02 -08001339int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 int olr;
1342 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001343 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001344 struct mr_table *mrt;
1345
1346 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1347 if (mrt == NULL)
1348 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jianjun Kongc354e122008-11-03 00:28:02 -08001350 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001352 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001354 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -ENOPROTOOPT;
1356
1357 if (get_user(olr, optlen))
1358 return -EFAULT;
1359
1360 olr = min_t(unsigned int, olr, sizeof(int));
1361 if (olr < 0)
1362 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001363
Jianjun Kongc354e122008-11-03 00:28:02 -08001364 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001366 if (optname == MRT_VERSION)
1367 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001369 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001370 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371#endif
1372 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001373 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001374 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return -EFAULT;
1376 return 0;
1377}
1378
1379/*
1380 * The IP multicast ioctl support routines.
1381 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1384{
1385 struct sioc_sg_req sr;
1386 struct sioc_vif_req vr;
1387 struct vif_device *vif;
1388 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001389 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001390 struct mr_table *mrt;
1391
1392 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1393 if (mrt == NULL)
1394 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001395
Stephen Hemminger132adf52007-03-08 20:44:43 -08001396 switch (cmd) {
1397 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001398 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001399 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001400 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001401 return -EINVAL;
1402 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001403 vif = &mrt->vif_table[vr.vifi];
1404 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001405 vr.icount = vif->pkt_in;
1406 vr.ocount = vif->pkt_out;
1407 vr.ibytes = vif->bytes_in;
1408 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001410
Jianjun Kongc354e122008-11-03 00:28:02 -08001411 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001413 return 0;
1414 }
1415 read_unlock(&mrt_lock);
1416 return -EADDRNOTAVAIL;
1417 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001418 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001419 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Eric Dumazeta8c94862010-10-01 16:15:08 +00001421 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001422 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001423 if (c) {
1424 sr.pktcnt = c->mfc_un.res.pkt;
1425 sr.bytecnt = c->mfc_un.res.bytes;
1426 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001427 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001428
Jianjun Kongc354e122008-11-03 00:28:02 -08001429 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001430 return -EFAULT;
1431 return 0;
1432 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001433 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001434 return -EADDRNOTAVAIL;
1435 default:
1436 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438}
1439
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001440#ifdef CONFIG_COMPAT
1441struct compat_sioc_sg_req {
1442 struct in_addr src;
1443 struct in_addr grp;
1444 compat_ulong_t pktcnt;
1445 compat_ulong_t bytecnt;
1446 compat_ulong_t wrong_if;
1447};
1448
David S. Millerca6b8bb2011-02-03 17:24:28 -08001449struct compat_sioc_vif_req {
1450 vifi_t vifi; /* Which iface */
1451 compat_ulong_t icount;
1452 compat_ulong_t ocount;
1453 compat_ulong_t ibytes;
1454 compat_ulong_t obytes;
1455};
1456
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001457int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1458{
David S. Miller0033d5a2011-02-03 17:21:31 -08001459 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb2011-02-03 17:24:28 -08001460 struct compat_sioc_vif_req vr;
1461 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001462 struct mfc_cache *c;
1463 struct net *net = sock_net(sk);
1464 struct mr_table *mrt;
1465
1466 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1467 if (mrt == NULL)
1468 return -ENOENT;
1469
1470 switch (cmd) {
David S. Millerca6b8bb2011-02-03 17:24:28 -08001471 case SIOCGETVIFCNT:
1472 if (copy_from_user(&vr, arg, sizeof(vr)))
1473 return -EFAULT;
1474 if (vr.vifi >= mrt->maxvif)
1475 return -EINVAL;
1476 read_lock(&mrt_lock);
1477 vif = &mrt->vif_table[vr.vifi];
1478 if (VIF_EXISTS(mrt, vr.vifi)) {
1479 vr.icount = vif->pkt_in;
1480 vr.ocount = vif->pkt_out;
1481 vr.ibytes = vif->bytes_in;
1482 vr.obytes = vif->bytes_out;
1483 read_unlock(&mrt_lock);
1484
1485 if (copy_to_user(arg, &vr, sizeof(vr)))
1486 return -EFAULT;
1487 return 0;
1488 }
1489 read_unlock(&mrt_lock);
1490 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001491 case SIOCGETSGCNT:
1492 if (copy_from_user(&sr, arg, sizeof(sr)))
1493 return -EFAULT;
1494
1495 rcu_read_lock();
1496 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1497 if (c) {
1498 sr.pktcnt = c->mfc_un.res.pkt;
1499 sr.bytecnt = c->mfc_un.res.bytes;
1500 sr.wrong_if = c->mfc_un.res.wrong_if;
1501 rcu_read_unlock();
1502
1503 if (copy_to_user(arg, &sr, sizeof(sr)))
1504 return -EFAULT;
1505 return 0;
1506 }
1507 rcu_read_unlock();
1508 return -EADDRNOTAVAIL;
1509 default:
1510 return -ENOIOCTLCMD;
1511 }
1512}
1513#endif
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1517{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001518 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001519 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001520 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 struct vif_device *v;
1522 int ct;
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)
RongQing.Lie92036a2011-11-23 23:10:52 +00001531 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 return NOTIFY_DONE;
1535}
1536
1537
Jianjun Kongc354e122008-11-03 00:28:02 -08001538static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 .notifier_call = ipmr_device_event,
1540};
1541
1542/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001543 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 * This avoids tunnel drivers and other mess and gives us the speed so
1545 * important for multicast video.
1546 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001547
Al Viro114c7842006-09-27 18:39:29 -07001548static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001550 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001551 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001552
1553 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001554 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001555 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001556 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001558 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001559 iph->tos = old_iph->tos;
1560 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 iph->frag_off = 0;
1562 iph->daddr = daddr;
1563 iph->saddr = saddr;
1564 iph->protocol = IPPROTO_IPIP;
1565 iph->ihl = 5;
1566 iph->tot_len = htons(skb->len);
Eric Dumazetadf30902009-06-02 05:19:30 +00001567 ip_select_ident(iph, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 ip_send_check(iph);
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1571 nf_reset(skb);
1572}
1573
1574static inline int ipmr_forward_finish(struct sk_buff *skb)
1575{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001576 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Eric Dumazetadf30902009-06-02 05:19:30 +00001578 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 if (unlikely(opt->optlen))
1581 ip_forward_options(skb);
1582
1583 return dst_output(skb);
1584}
1585
1586/*
1587 * Processing handlers for ipmr_forward
1588 */
1589
Patrick McHardy0c122952010-04-13 05:03:22 +00001590static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1591 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001593 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001594 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 struct net_device *dev;
1596 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001597 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 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. Miller31e45432011-05-03 20:25:42 -07001615 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001616 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. Miller31e45432011-05-03 20:25:42 -07001624 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001625 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. Miller417da662011-05-03 19:42:43 -07001791static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001792{
David S. Miller417da662011-05-03 19:42:43 -07001793 struct rtable *rt = skb_rtable(skb);
1794 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001795 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001796 .daddr = iph->daddr,
1797 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001798 .flowi4_tos = RT_TOS(iph->tos),
David S. Millerda919812011-03-12 02:04:50 -05001799 .flowi4_oif = rt->rt_oif,
1800 .flowi4_iif = rt->rt_iif,
1801 .flowi4_mark = rt->rt_mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001802 };
1803 struct mr_table *mrt;
1804 int err;
1805
David S. Millerda919812011-03-12 02:04:50 -05001806 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001807 if (err)
1808 return ERR_PTR(err);
1809 return mrt;
1810}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812/*
1813 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001814 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 */
1816
1817int ip_mr_input(struct sk_buff *skb)
1818{
1819 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001820 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001821 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001822 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001825 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001827 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 goto dont_forward;
1829
David S. Miller417da662011-05-03 19:42:43 -07001830 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001831 if (IS_ERR(mrt)) {
1832 kfree_skb(skb);
1833 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001836 if (IPCB(skb)->opt.router_alert) {
1837 if (ip_call_ra_chain(skb))
1838 return 0;
1839 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1840 /* IGMPv1 (and broken IGMPv2 implementations sort of
1841 * Cisco IOS <= 11.2(8)) do not put router alert
1842 * option to IGMP packets destined to routable
1843 * groups. It is very bad, because it means
1844 * that we can forward NO IGMP messages.
1845 */
1846 struct sock *mroute_sk;
1847
1848 mroute_sk = rcu_dereference(mrt->mroute_sk);
1849 if (mroute_sk) {
1850 nf_reset(skb);
1851 raw_rcv(mroute_sk, skb);
1852 return 0;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855 }
1856
Eric Dumazeta8c94862010-10-01 16:15:08 +00001857 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001858 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 /*
1861 * No usable cache entry
1862 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001863 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 int vif;
1865
1866 if (local) {
1867 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1868 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001869 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 skb = skb2;
1872 }
1873
Eric Dumazeta8c94862010-10-01 16:15:08 +00001874 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001875 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001877 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 read_unlock(&mrt_lock);
1879
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001880 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882 read_unlock(&mrt_lock);
1883 kfree_skb(skb);
1884 return -ENODEV;
1885 }
1886
Eric Dumazeta8c94862010-10-01 16:15:08 +00001887 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001888 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 read_unlock(&mrt_lock);
1890
1891 if (local)
1892 return ip_local_deliver(skb);
1893
1894 return 0;
1895
1896dont_forward:
1897 if (local)
1898 return ip_local_deliver(skb);
1899 kfree_skb(skb);
1900 return 0;
1901}
1902
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001903#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00001904/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001905static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1906 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001908 struct net_device *reg_dev = NULL;
1909 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001911 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001913 * Check that:
1914 * a. packet is really sent to a multicast group
1915 * b. packet is not a NULL-REGISTER
1916 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001918 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001920 ntohs(encap->tot_len) + pimlen > skb->len)
1921 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001924 if (mrt->mroute_reg_vif_num >= 0)
1925 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 read_unlock(&mrt_lock);
1927
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001928 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001929 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001931 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00001932 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001933 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00001935 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07001937
1938 skb_tunnel_rx(skb, reg_dev);
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001941
Eric Dumazet55747a02010-10-01 16:14:55 +00001942 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001943}
1944#endif
1945
1946#ifdef CONFIG_IP_PIMSM_V1
1947/*
1948 * Handle IGMP messages of PIMv1
1949 */
1950
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001951int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001952{
1953 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001954 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001955 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001956
1957 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1958 goto drop;
1959
1960 pim = igmp_hdr(skb);
1961
David S. Miller417da662011-05-03 19:42:43 -07001962 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001963 if (IS_ERR(mrt))
1964 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00001965 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001966 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1967 goto drop;
1968
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001969 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001970drop:
1971 kfree_skb(skb);
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return 0;
1974}
1975#endif
1976
1977#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001978static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001981 struct net *net = dev_net(skb->dev);
1982 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001984 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 goto drop;
1986
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001987 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001988 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1989 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001990 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08001991 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 goto drop;
1993
David S. Miller417da662011-05-03 19:42:43 -07001994 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001995 if (IS_ERR(mrt))
1996 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001997 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001998drop:
1999 kfree_skb(skb);
2000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return 0;
2002}
2003#endif
2004
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002005static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2006 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 int ct;
2009 struct rtnexthop *nhp;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002010 u8 *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 struct rtattr *mp_head;
2012
Nicolas Dichtel74381892010-03-25 23:45:35 +00002013 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f1602010-05-26 00:38:56 -07002014 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002015 return -ENOENT;
2016
Patrick McHardy0c122952010-04-13 05:03:22 +00002017 if (VIF_EXISTS(mrt, c->mfc_parent))
2018 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Jianjun Kongc354e122008-11-03 00:28:02 -08002020 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002023 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2025 goto rtattr_failure;
Jianjun Kongc354e122008-11-03 00:28:02 -08002026 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 nhp->rtnh_flags = 0;
2028 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002029 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 nhp->rtnh_len = sizeof(*nhp);
2031 }
2032 }
2033 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002034 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 rtm->rtm_type = RTN_MULTICAST;
2036 return 1;
2037
2038rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002039 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return -EMSGSIZE;
2041}
2042
David S. Miller9a1b9492011-05-04 12:18:54 -07002043int ipmr_get_route(struct net *net, struct sk_buff *skb,
2044 __be32 saddr, __be32 daddr,
2045 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002048 struct mr_table *mrt;
2049 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002051 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2052 if (mrt == NULL)
2053 return -ENOENT;
2054
Eric Dumazeta8c94862010-10-01 16:15:08 +00002055 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002056 cache = ipmr_cache_find(mrt, saddr, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Jianjun Kongc354e122008-11-03 00:28:02 -08002058 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002059 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002060 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002062 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002065 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return -EAGAIN;
2067 }
2068
2069 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002070 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002071 if (dev)
2072 vif = ipmr_find_vif(mrt, dev);
2073 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002075 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return -ENODEV;
2077 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002078 skb2 = skb_clone(skb, GFP_ATOMIC);
2079 if (!skb2) {
2080 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002081 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002082 return -ENOMEM;
2083 }
2084
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002085 skb_push(skb2, sizeof(struct iphdr));
2086 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002087 iph = ip_hdr(skb2);
2088 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002089 iph->saddr = saddr;
2090 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002091 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002092 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002094 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 return err;
2096 }
2097
Eric Dumazeta8c94862010-10-01 16:15:08 +00002098 read_lock(&mrt_lock);
2099 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002101 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002103 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return err;
2105}
2106
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002107static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2108 u32 pid, u32 seq, struct mfc_cache *c)
2109{
2110 struct nlmsghdr *nlh;
2111 struct rtmsg *rtm;
2112
2113 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2114 if (nlh == NULL)
2115 return -EMSGSIZE;
2116
2117 rtm = nlmsg_data(nlh);
2118 rtm->rtm_family = RTNL_FAMILY_IPMR;
2119 rtm->rtm_dst_len = 32;
2120 rtm->rtm_src_len = 32;
2121 rtm->rtm_tos = 0;
2122 rtm->rtm_table = mrt->id;
2123 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2124 rtm->rtm_type = RTN_MULTICAST;
2125 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2126 rtm->rtm_protocol = RTPROT_UNSPEC;
2127 rtm->rtm_flags = 0;
2128
2129 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2130 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2131
2132 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2133 goto nla_put_failure;
2134
2135 return nlmsg_end(skb, nlh);
2136
2137nla_put_failure:
2138 nlmsg_cancel(skb, nlh);
2139 return -EMSGSIZE;
2140}
2141
2142static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2143{
2144 struct net *net = sock_net(skb->sk);
2145 struct mr_table *mrt;
2146 struct mfc_cache *mfc;
2147 unsigned int t = 0, s_t;
2148 unsigned int h = 0, s_h;
2149 unsigned int e = 0, s_e;
2150
2151 s_t = cb->args[0];
2152 s_h = cb->args[1];
2153 s_e = cb->args[2];
2154
Eric Dumazeta8c94862010-10-01 16:15:08 +00002155 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002156 ipmr_for_each_table(mrt, net) {
2157 if (t < s_t)
2158 goto next_table;
2159 if (t > s_t)
2160 s_h = 0;
2161 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002162 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002163 if (e < s_e)
2164 goto next_entry;
2165 if (ipmr_fill_mroute(mrt, skb,
2166 NETLINK_CB(cb->skb).pid,
2167 cb->nlh->nlmsg_seq,
2168 mfc) < 0)
2169 goto done;
2170next_entry:
2171 e++;
2172 }
2173 e = s_e = 0;
2174 }
2175 s_h = 0;
2176next_table:
2177 t++;
2178 }
2179done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002180 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002181
2182 cb->args[2] = e;
2183 cb->args[1] = h;
2184 cb->args[0] = t;
2185
2186 return skb->len;
2187}
2188
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002189#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002191 * The /proc interfaces to multicast routing :
2192 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 */
2194struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002195 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002196 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 int ct;
2198};
2199
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002200static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2201 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 loff_t pos)
2203{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002204 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002205
2206 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2207 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002209 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002210 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212 return NULL;
2213}
2214
2215static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002216 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002218 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002219 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002220 struct mr_table *mrt;
2221
2222 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2223 if (mrt == NULL)
2224 return ERR_PTR(-ENOENT);
2225
2226 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002229 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 : SEQ_START_TOKEN;
2231}
2232
2233static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2234{
2235 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002236 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002237 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 ++*pos;
2240 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002241 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002242
Patrick McHardy0c122952010-04-13 05:03:22 +00002243 while (++iter->ct < mrt->maxvif) {
2244 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002246 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248 return NULL;
2249}
2250
2251static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002252 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 read_unlock(&mrt_lock);
2255}
2256
2257static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2258{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002259 struct ipmr_vif_iter *iter = seq->private;
2260 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002263 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2265 } else {
2266 const struct vif_device *vif = v;
2267 const char *name = vif->dev ? vif->dev->name : "none";
2268
2269 seq_printf(seq,
2270 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002271 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002272 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 vif->bytes_out, vif->pkt_out,
2274 vif->flags, vif->local, vif->remote);
2275 }
2276 return 0;
2277}
2278
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002279static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 .start = ipmr_vif_seq_start,
2281 .next = ipmr_vif_seq_next,
2282 .stop = ipmr_vif_seq_stop,
2283 .show = ipmr_vif_seq_show,
2284};
2285
2286static int ipmr_vif_open(struct inode *inode, struct file *file)
2287{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002288 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2289 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
Arjan van de Ven9a321442007-02-12 00:55:35 -08002292static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 .owner = THIS_MODULE,
2294 .open = ipmr_vif_open,
2295 .read = seq_read,
2296 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002297 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298};
2299
2300struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002301 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002302 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002303 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 int ct;
2305};
2306
2307
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002308static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2309 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002311 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 struct mfc_cache *mfc;
2313
Eric Dumazeta8c94862010-10-01 16:15:08 +00002314 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002315 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002316 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002317 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002318 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002320 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002321 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002324 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002325 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002326 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return mfc;
2328 spin_unlock_bh(&mfc_unres_lock);
2329
2330 it->cache = NULL;
2331 return NULL;
2332}
2333
2334
2335static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2336{
2337 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002338 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002339 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002340
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002341 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2342 if (mrt == NULL)
2343 return ERR_PTR(-ENOENT);
2344
2345 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 it->cache = NULL;
2347 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002348 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 : SEQ_START_TOKEN;
2350}
2351
2352static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2353{
2354 struct mfc_cache *mfc = v;
2355 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002356 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002357 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 ++*pos;
2360
2361 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002362 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Patrick McHardy862465f2010-04-13 05:03:21 +00002364 if (mfc->list.next != it->cache)
2365 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002366
Patrick McHardy0c122952010-04-13 05:03:22 +00002367 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 goto end_of_list;
2369
Patrick McHardy0c122952010-04-13 05:03:22 +00002370 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002373 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002374 if (list_empty(it->cache))
2375 continue;
2376 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
2378
2379 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002380 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002381 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002385 if (!list_empty(it->cache))
2386 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002388end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 spin_unlock_bh(&mfc_unres_lock);
2390 it->cache = NULL;
2391
2392 return NULL;
2393}
2394
2395static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2396{
2397 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002398 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Patrick McHardy0c122952010-04-13 05:03:22 +00002400 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002402 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002403 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2407{
2408 int n;
2409
2410 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002411 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2413 } else {
2414 const struct mfc_cache *mfc = v;
2415 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002416 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002417
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002418 seq_printf(seq, "%08X %08X %-3hd",
2419 (__force u32) mfc->mfc_mcastgrp,
2420 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002421 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Patrick McHardy0c122952010-04-13 05:03:22 +00002423 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002424 seq_printf(seq, " %8lu %8lu %8lu",
2425 mfc->mfc_un.res.pkt,
2426 mfc->mfc_un.res.bytes,
2427 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002428 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002429 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002430 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002431 mfc->mfc_un.res.ttls[n] < 255)
2432 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002433 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 n, mfc->mfc_un.res.ttls[n]);
2435 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002436 } else {
2437 /* unresolved mfc_caches don't contain
2438 * pkt, bytes and wrong_if values
2439 */
2440 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
2442 seq_putc(seq, '\n');
2443 }
2444 return 0;
2445}
2446
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002447static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 .start = ipmr_mfc_seq_start,
2449 .next = ipmr_mfc_seq_next,
2450 .stop = ipmr_mfc_seq_stop,
2451 .show = ipmr_mfc_seq_show,
2452};
2453
2454static int ipmr_mfc_open(struct inode *inode, struct file *file)
2455{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002456 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2457 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Arjan van de Ven9a321442007-02-12 00:55:35 -08002460static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 .owner = THIS_MODULE,
2462 .open = ipmr_mfc_open,
2463 .read = seq_read,
2464 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002465 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002467#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002470static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002472 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473};
2474#endif
2475
2476
2477/*
2478 * Setup for IP multicast routing
2479 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002480static int __net_init ipmr_net_init(struct net *net)
2481{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002482 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002483
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002484 err = ipmr_rules_init(net);
2485 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002486 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002487
2488#ifdef CONFIG_PROC_FS
2489 err = -ENOMEM;
2490 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2491 goto proc_vif_fail;
2492 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2493 goto proc_cache_fail;
2494#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002495 return 0;
2496
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002497#ifdef CONFIG_PROC_FS
2498proc_cache_fail:
2499 proc_net_remove(net, "ip_mr_vif");
2500proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002501 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002502#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002503fail:
2504 return err;
2505}
2506
2507static void __net_exit ipmr_net_exit(struct net *net)
2508{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002509#ifdef CONFIG_PROC_FS
2510 proc_net_remove(net, "ip_mr_cache");
2511 proc_net_remove(net, "ip_mr_vif");
2512#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002513 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002514}
2515
2516static struct pernet_operations ipmr_net_ops = {
2517 .init = ipmr_net_init,
2518 .exit = ipmr_net_exit,
2519};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002520
Wang Chen03d2f892008-07-03 12:13:36 +08002521int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Wang Chen03d2f892008-07-03 12:13:36 +08002523 int err;
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2526 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002527 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002528 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002529 if (!mrt_cachep)
2530 return -ENOMEM;
2531
Benjamin Therycf958ae32009-01-22 04:56:16 +00002532 err = register_pernet_subsys(&ipmr_net_ops);
2533 if (err)
2534 goto reg_pernet_fail;
2535
Wang Chen03d2f892008-07-03 12:13:36 +08002536 err = register_netdevice_notifier(&ip_mr_notifier);
2537 if (err)
2538 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002539#ifdef CONFIG_IP_PIMSM_V2
2540 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2541 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2542 err = -EAGAIN;
2543 goto add_proto_fail;
2544 }
2545#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002546 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2547 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002548 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002549
Tom Goff403dbb92009-06-14 03:16:13 -07002550#ifdef CONFIG_IP_PIMSM_V2
2551add_proto_fail:
2552 unregister_netdevice_notifier(&ip_mr_notifier);
2553#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002554reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002555 unregister_pernet_subsys(&ipmr_net_ops);
2556reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002557 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002558 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}