blob: 26ca2f2d37ced477650a6770d7767aa7d508f9fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/system.h>
30#include <asm/uaccess.h>
31#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
34#include <linux/timer.h>
35#include <linux/mm.h>
36#include <linux/kernel.h>
37#include <linux/fcntl.h>
38#include <linux/stat.h>
39#include <linux/socket.h>
40#include <linux/in.h>
41#include <linux/inet.h>
42#include <linux/netdevice.h>
43#include <linux/inetdevice.h>
44#include <linux/igmp.h>
45#include <linux/proc_fs.h>
46#include <linux/seq_file.h>
47#include <linux/mroute.h>
48#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080049#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020051#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/ip.h>
53#include <net/protocol.h>
54#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020055#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/sock.h>
57#include <net/icmp.h>
58#include <net/udp.h>
59#include <net/raw.h>
60#include <linux/notifier.h>
61#include <linux/if_arp.h>
62#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000063#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/ipip.h>
65#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
70#define CONFIG_IP_PIMSM 1
71#endif
72
Patrick McHardy0c122952010-04-13 05:03:22 +000073struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000074 struct list_head list;
Patrick McHardy8de53df2010-04-15 13:29:28 +020075#ifdef CONFIG_NET_NS
76 struct net *net;
77#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +000078 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000079 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000080 struct timer_list ipmr_expire_timer;
81 struct list_head mfc_unres_queue;
82 struct list_head mfc_cache_array[MFC_LINES];
83 struct vif_device vif_table[MAXVIFS];
84 int maxvif;
85 atomic_t cache_resolve_queue_len;
86 int mroute_do_assert;
87 int mroute_do_pim;
88#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
89 int mroute_reg_vif_num;
90#endif
91};
92
Patrick McHardyf0ad0862010-04-13 05:03:23 +000093struct ipmr_rule {
94 struct fib_rule common;
95};
96
97struct ipmr_result {
98 struct mr_table *mrt;
99};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000102 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
104
105static DEFINE_RWLOCK(mrt_lock);
106
107/*
108 * Multicast router control variables
109 */
110
Patrick McHardy0c122952010-04-13 05:03:22 +0000111#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* Special spinlock for queue of unresolved entries */
114static DEFINE_SPINLOCK(mfc_unres_lock);
115
116/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000117 * entries is changed only in process context and protected
118 * with weak lock mrt_lock. Queue of unresolved entries is protected
119 * with strong spinlock mfc_unres_lock.
120 *
121 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123
Christoph Lametere18b8902006-12-06 20:33:20 -0800124static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000126static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Patrick McHardy0c122952010-04-13 05:03:22 +0000127static int ip_mr_forward(struct net *net, struct mr_table *mrt,
128 struct sk_buff *skb, struct mfc_cache *cache,
129 int local);
130static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000131 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200132static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
133 struct mfc_cache *c, struct rtmsg *rtm);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000134static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000136#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
137#define ipmr_for_each_table(mrt, net) \
138 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
139
140static struct mr_table *ipmr_get_table(struct net *net, u32 id)
141{
142 struct mr_table *mrt;
143
144 ipmr_for_each_table(mrt, net) {
145 if (mrt->id == id)
146 return mrt;
147 }
148 return NULL;
149}
150
151static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
152 struct mr_table **mrt)
153{
154 struct ipmr_result res;
155 struct fib_lookup_arg arg = { .result = &res, };
156 int err;
157
158 err = fib_rules_lookup(net->ipv4.mr_rules_ops, flp, 0, &arg);
159 if (err < 0)
160 return err;
161 *mrt = res.mrt;
162 return 0;
163}
164
165static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
166 int flags, struct fib_lookup_arg *arg)
167{
168 struct ipmr_result *res = arg->result;
169 struct mr_table *mrt;
170
171 switch (rule->action) {
172 case FR_ACT_TO_TBL:
173 break;
174 case FR_ACT_UNREACHABLE:
175 return -ENETUNREACH;
176 case FR_ACT_PROHIBIT:
177 return -EACCES;
178 case FR_ACT_BLACKHOLE:
179 default:
180 return -EINVAL;
181 }
182
183 mrt = ipmr_get_table(rule->fr_net, rule->table);
184 if (mrt == NULL)
185 return -EAGAIN;
186 res->mrt = mrt;
187 return 0;
188}
189
190static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
191{
192 return 1;
193}
194
195static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
196 FRA_GENERIC_POLICY,
197};
198
199static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
200 struct fib_rule_hdr *frh, struct nlattr **tb)
201{
202 return 0;
203}
204
205static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
206 struct nlattr **tb)
207{
208 return 1;
209}
210
211static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
212 struct fib_rule_hdr *frh)
213{
214 frh->dst_len = 0;
215 frh->src_len = 0;
216 frh->tos = 0;
217 return 0;
218}
219
Patrick McHardy3d0c9c42010-04-26 16:02:04 +0200220static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200221 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000222 .rule_size = sizeof(struct ipmr_rule),
223 .addr_size = sizeof(u32),
224 .action = ipmr_rule_action,
225 .match = ipmr_rule_match,
226 .configure = ipmr_rule_configure,
227 .compare = ipmr_rule_compare,
228 .default_pref = fib_default_rule_pref,
229 .fill = ipmr_rule_fill,
230 .nlgroup = RTNLGRP_IPV4_RULE,
231 .policy = ipmr_rule_policy,
232 .owner = THIS_MODULE,
233};
234
235static int __net_init ipmr_rules_init(struct net *net)
236{
237 struct fib_rules_ops *ops;
238 struct mr_table *mrt;
239 int err;
240
241 ops = fib_rules_register(&ipmr_rules_ops_template, net);
242 if (IS_ERR(ops))
243 return PTR_ERR(ops);
244
245 INIT_LIST_HEAD(&net->ipv4.mr_tables);
246
247 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
248 if (mrt == NULL) {
249 err = -ENOMEM;
250 goto err1;
251 }
252
253 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
254 if (err < 0)
255 goto err2;
256
257 net->ipv4.mr_rules_ops = ops;
258 return 0;
259
260err2:
261 kfree(mrt);
262err1:
263 fib_rules_unregister(ops);
264 return err;
265}
266
267static void __net_exit ipmr_rules_exit(struct net *net)
268{
269 struct mr_table *mrt, *next;
270
Eric Dumazet035320d2010-06-06 23:48:40 +0000271 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
272 list_del(&mrt->list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000273 kfree(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000274 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000275 fib_rules_unregister(net->ipv4.mr_rules_ops);
276}
277#else
278#define ipmr_for_each_table(mrt, net) \
279 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
280
281static struct mr_table *ipmr_get_table(struct net *net, u32 id)
282{
283 return net->ipv4.mrt;
284}
285
286static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
287 struct mr_table **mrt)
288{
289 *mrt = net->ipv4.mrt;
290 return 0;
291}
292
293static int __net_init ipmr_rules_init(struct net *net)
294{
295 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
296 return net->ipv4.mrt ? 0 : -ENOMEM;
297}
298
299static void __net_exit ipmr_rules_exit(struct net *net)
300{
301 kfree(net->ipv4.mrt);
302}
303#endif
304
305static struct mr_table *ipmr_new_table(struct net *net, u32 id)
306{
307 struct mr_table *mrt;
308 unsigned int i;
309
310 mrt = ipmr_get_table(net, id);
311 if (mrt != NULL)
312 return mrt;
313
314 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
315 if (mrt == NULL)
316 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200317 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000318 mrt->id = id;
319
320 /* Forwarding cache */
321 for (i = 0; i < MFC_LINES; i++)
322 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
323
324 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
325
326 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
327 (unsigned long)mrt);
328
329#ifdef CONFIG_IP_PIMSM
330 mrt->mroute_reg_vif_num = -1;
331#endif
332#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
333 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
334#endif
335 return mrt;
336}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
339
Wang Chend6070322008-07-14 20:55:26 -0700340static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
341{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000342 struct net *net = dev_net(dev);
343
Wang Chend6070322008-07-14 20:55:26 -0700344 dev_close(dev);
345
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000346 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700347 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800348 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700349 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700350 struct ip_tunnel_parm p;
351
352 memset(&p, 0, sizeof(p));
353 p.iph.daddr = v->vifc_rmt_addr.s_addr;
354 p.iph.saddr = v->vifc_lcl_addr.s_addr;
355 p.iph.version = 4;
356 p.iph.ihl = 5;
357 p.iph.protocol = IPPROTO_IPIP;
358 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
359 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
360
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800361 if (ops->ndo_do_ioctl) {
362 mm_segment_t oldfs = get_fs();
363
364 set_fs(KERNEL_DS);
365 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
366 set_fs(oldfs);
367 }
Wang Chend6070322008-07-14 20:55:26 -0700368 }
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000372struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct net_device *dev;
375
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000376 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800379 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int err;
381 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct ip_tunnel_parm p;
383 struct in_device *in_dev;
384
385 memset(&p, 0, sizeof(p));
386 p.iph.daddr = v->vifc_rmt_addr.s_addr;
387 p.iph.saddr = v->vifc_lcl_addr.s_addr;
388 p.iph.version = 4;
389 p.iph.ihl = 5;
390 p.iph.protocol = IPPROTO_IPIP;
391 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800392 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800394 if (ops->ndo_do_ioctl) {
395 mm_segment_t oldfs = get_fs();
396
397 set_fs(KERNEL_DS);
398 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
399 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000400 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800401 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dev = NULL;
404
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000405 if (err == 0 &&
406 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 dev->flags |= IFF_MULTICAST;
408
Herbert Xue5ed6392005-10-03 14:35:55 -0700409 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700410 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700412
413 ipv4_devconf_setall(in_dev);
414 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if (dev_open(dev))
417 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700418 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 }
421 return dev;
422
423failure:
424 /* allow the register to be completed before unregistering. */
425 rtnl_unlock();
426 rtnl_lock();
427
428 unregister_netdevice(dev);
429 return NULL;
430}
431
432#ifdef CONFIG_IP_PIMSM
433
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000434static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000436 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000437 struct mr_table *mrt;
438 struct flowi fl = {
439 .oif = dev->ifindex,
440 .iif = skb->skb_iif,
441 .mark = skb->mark,
442 };
443 int err;
444
445 err = ipmr_fib_lookup(net, &fl, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000446 if (err < 0) {
447 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000448 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000449 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700452 dev->stats.tx_bytes += skb->len;
453 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000454 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 read_unlock(&mrt_lock);
456 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000457 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Stephen Hemminger007c3832008-11-20 20:28:35 -0800460static const struct net_device_ops reg_vif_netdev_ops = {
461 .ndo_start_xmit = reg_vif_xmit,
462};
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static void reg_vif_setup(struct net_device *dev)
465{
466 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800467 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800469 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700471 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000474static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct net_device *dev;
477 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000478 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000480 if (mrt->id == RT_TABLE_DEFAULT)
481 sprintf(name, "pimreg");
482 else
483 sprintf(name, "pimreg%u", mrt->id);
484
485 dev = alloc_netdev(0, name, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if (dev == NULL)
488 return NULL;
489
Tom Goff403dbb92009-06-14 03:16:13 -0700490 dev_net_set(dev, net);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (register_netdevice(dev)) {
493 free_netdev(dev);
494 return NULL;
495 }
496 dev->iflink = 0;
497
Herbert Xu71e27da2007-06-04 23:36:06 -0700498 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000499 in_dev = __in_dev_get_rcu(dev);
500 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700501 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Herbert Xu71e27da2007-06-04 23:36:06 -0700505 ipv4_devconf_setall(in_dev);
506 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
507 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (dev_open(dev))
510 goto failure;
511
Wang Chen7dc00c82008-07-14 20:56:34 -0700512 dev_hold(dev);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return dev;
515
516failure:
517 /* allow the register to be completed before unregistering. */
518 rtnl_unlock();
519 rtnl_lock();
520
521 unregister_netdevice(dev);
522 return NULL;
523}
524#endif
525
526/*
527 * Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700528 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900530
Patrick McHardy0c122952010-04-13 05:03:22 +0000531static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000532 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct vif_device *v;
535 struct net_device *dev;
536 struct in_device *in_dev;
537
Patrick McHardy0c122952010-04-13 05:03:22 +0000538 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EADDRNOTAVAIL;
540
Patrick McHardy0c122952010-04-13 05:03:22 +0000541 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 write_lock_bh(&mrt_lock);
544 dev = v->dev;
545 v->dev = NULL;
546
547 if (!dev) {
548 write_unlock_bh(&mrt_lock);
549 return -EADDRNOTAVAIL;
550 }
551
552#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000553 if (vifi == mrt->mroute_reg_vif_num)
554 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555#endif
556
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000557 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000559
560 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000561 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 break;
563 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000564 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
567 write_unlock_bh(&mrt_lock);
568
569 dev_set_allmulti(dev, -1);
570
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000571 in_dev = __in_dev_get_rtnl(dev);
572 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700573 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ip_rt_multicast_event(in_dev);
575 }
576
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000577 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000578 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 dev_put(dev);
581 return 0;
582}
583
Eric Dumazeta8c94862010-10-01 16:15:08 +0000584static void ipmr_cache_free_rcu(struct rcu_head *head)
585{
586 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
587
588 kmem_cache_free(mrt_cachep, c);
589}
590
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000591static inline void ipmr_cache_free(struct mfc_cache *c)
592{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000593 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000597 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
599
Patrick McHardy0c122952010-04-13 05:03:22 +0000600static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200602 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700604 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Patrick McHardy0c122952010-04-13 05:03:22 +0000606 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Jianjun Kongc354e122008-11-03 00:28:02 -0800608 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700609 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
611 nlh->nlmsg_type = NLMSG_ERROR;
612 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
613 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700614 e = NLMSG_DATA(nlh);
615 e->error = -ETIMEDOUT;
616 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700617
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000618 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000619 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000624 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627
Patrick McHardye258beb2010-04-13 05:03:19 +0000628/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Patrick McHardye258beb2010-04-13 05:03:19 +0000630static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Patrick McHardy0c122952010-04-13 05:03:22 +0000632 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 unsigned long now;
634 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000635 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000638 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return;
640 }
641
Patrick McHardy0c122952010-04-13 05:03:22 +0000642 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto out;
644
645 now = jiffies;
646 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Patrick McHardy0c122952010-04-13 05:03:22 +0000648 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (time_after(c->mfc_un.unres.expires, now)) {
650 unsigned long interval = c->mfc_un.unres.expires - now;
651 if (interval < expires)
652 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 continue;
654 }
655
Patrick McHardy862465f2010-04-13 05:03:21 +0000656 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000657 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Patrick McHardy0c122952010-04-13 05:03:22 +0000660 if (!list_empty(&mrt->mfc_unres_queue))
661 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663out:
664 spin_unlock(&mfc_unres_lock);
665}
666
667/* Fill oifs list. It is called under write locked mrt_lock. */
668
Patrick McHardy0c122952010-04-13 05:03:22 +0000669static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000670 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 int vifi;
673
674 cache->mfc_un.res.minvif = MAXVIFS;
675 cache->mfc_un.res.maxvif = 0;
676 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
677
Patrick McHardy0c122952010-04-13 05:03:22 +0000678 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
679 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000680 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
682 if (cache->mfc_un.res.minvif > vifi)
683 cache->mfc_un.res.minvif = vifi;
684 if (cache->mfc_un.res.maxvif <= vifi)
685 cache->mfc_un.res.maxvif = vifi + 1;
686 }
687 }
688}
689
Patrick McHardy0c122952010-04-13 05:03:22 +0000690static int vif_add(struct net *net, struct mr_table *mrt,
691 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000694 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct net_device *dev;
696 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700697 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000700 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -EADDRINUSE;
702
703 switch (vifc->vifc_flags) {
704#ifdef CONFIG_IP_PIMSM
705 case VIFF_REGISTER:
706 /*
707 * Special Purpose VIF in PIM
708 * All the packets will be sent to the daemon
709 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000710 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000712 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!dev)
714 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700715 err = dev_set_allmulti(dev, 1);
716 if (err) {
717 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700718 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700719 return err;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
722#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900723 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000724 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!dev)
726 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700727 err = dev_set_allmulti(dev, 1);
728 if (err) {
729 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700730 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700731 return err;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000734
735 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000737 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
738 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Eric Dumazet95ae6b22010-09-15 04:04:31 +0000739 if (dev && __in_dev_get_rtnl(dev) == NULL) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000740 dev_put(dev);
741 return -EADDRNOTAVAIL;
742 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000743 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000744 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!dev)
747 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700748 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700749 if (err) {
750 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700751 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754 default:
755 return -EINVAL;
756 }
757
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000758 in_dev = __in_dev_get_rtnl(dev);
759 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000760 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000762 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700763 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ip_rt_multicast_event(in_dev);
765
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000766 /* Fill in the VIF structures */
767
Jianjun Kongc354e122008-11-03 00:28:02 -0800768 v->rate_limit = vifc->vifc_rate_limit;
769 v->local = vifc->vifc_lcl_addr.s_addr;
770 v->remote = vifc->vifc_rmt_addr.s_addr;
771 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!mrtsock)
773 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800774 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 v->bytes_in = 0;
776 v->bytes_out = 0;
777 v->pkt_in = 0;
778 v->pkt_out = 0;
779 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000780 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 v->link = dev->iflink;
782
783 /* And finish update writing critical data */
784 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800785 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000787 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000788 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000790 if (vifi+1 > mrt->maxvif)
791 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 write_unlock_bh(&mrt_lock);
793 return 0;
794}
795
Eric Dumazeta8c94862010-10-01 16:15:08 +0000796/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000797static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000798 __be32 origin,
799 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Jianjun Kongc354e122008-11-03 00:28:02 -0800801 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct mfc_cache *c;
803
Eric Dumazeta8c94862010-10-01 16:15:08 +0000804 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000805 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
806 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000808 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/*
812 * Allocate a multicast cache entry
813 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000814static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Jianjun Kongc354e122008-11-03 00:28:02 -0800816 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000817
818 if (c)
819 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return c;
821}
822
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000823static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Jianjun Kongc354e122008-11-03 00:28:02 -0800825 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000826
827 if (c) {
828 skb_queue_head_init(&c->mfc_un.unres.unresolved);
829 c->mfc_un.unres.expires = jiffies + 10*HZ;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return c;
832}
833
834/*
835 * A cache entry has gone into a resolved state from queued
836 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900837
Patrick McHardy0c122952010-04-13 05:03:22 +0000838static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
839 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700842 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000844 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Jianjun Kongc354e122008-11-03 00:28:02 -0800846 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700847 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
849
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200850 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000851 nlh->nlmsg_len = skb_tail_pointer(skb) -
852 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } else {
854 nlh->nlmsg_type = NLMSG_ERROR;
855 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
856 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700857 e = NLMSG_DATA(nlh);
858 e->error = -EMSGSIZE;
859 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Thomas Graf2942e902006-08-15 00:30:25 -0700861
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000862 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000863 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000864 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867}
868
869/*
870 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
871 * expects the following bizarre scheme.
872 *
873 * Called under mrt_lock.
874 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900875
Patrick McHardy0c122952010-04-13 05:03:22 +0000876static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000877 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300880 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct igmphdr *igmp;
882 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000883 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 int ret;
885
886#ifdef CONFIG_IP_PIMSM
887 if (assert == IGMPMSG_WHOLEPKT)
888 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
889 else
890#endif
891 skb = alloc_skb(128, GFP_ATOMIC);
892
Stephen Hemminger132adf52007-03-08 20:44:43 -0800893 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -ENOBUFS;
895
896#ifdef CONFIG_IP_PIMSM
897 if (assert == IGMPMSG_WHOLEPKT) {
898 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000899 * Duplicate old header, fix ihl, length etc.
900 * And all this only to mangle msg->im_msgtype and
901 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300903 skb_push(skb, sizeof(struct iphdr));
904 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300905 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300906 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700907 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 msg->im_msgtype = IGMPMSG_WHOLEPKT;
909 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000910 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700911 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
912 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
913 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900914 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900916 {
917
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000918 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700920 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300921 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300922 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000923 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700924 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000926 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000928 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000930 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 igmp->type =
932 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000933 igmp->code = 0;
934 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700935 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Eric Dumazet4c968702010-10-01 16:15:01 +0000938 rcu_read_lock();
939 mroute_sk = rcu_dereference(mrt->mroute_sk);
940 if (mroute_sk == NULL) {
941 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 kfree_skb(skb);
943 return -EINVAL;
944 }
945
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000946 /* Deliver to mrouted */
947
Eric Dumazet4c968702010-10-01 16:15:01 +0000948 ret = sock_queue_rcv_skb(mroute_sk, skb);
949 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +0000950 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (net_ratelimit())
952 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
953 kfree_skb(skb);
954 }
955
956 return ret;
957}
958
959/*
960 * Queue a packet for resolution. It gets locked cache entry!
961 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963static int
Patrick McHardy0c122952010-04-13 05:03:22 +0000964ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Patrick McHardy862465f2010-04-13 05:03:21 +0000966 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int err;
968 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700969 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000972 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000973 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000974 c->mfc_origin == iph->saddr) {
975 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Patrick McHardy862465f2010-04-13 05:03:21 +0000980 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000981 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Patrick McHardy0c122952010-04-13 05:03:22 +0000983 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000984 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 spin_unlock_bh(&mfc_unres_lock);
986
987 kfree_skb(skb);
988 return -ENOBUFS;
989 }
990
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000991 /* Fill in the new cache entry */
992
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700993 c->mfc_parent = -1;
994 c->mfc_origin = iph->saddr;
995 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000997 /* Reflect first query at mrouted. */
998
Patrick McHardy0c122952010-04-13 05:03:22 +0000999 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001000 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001001 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 out - Brad Parker
1003 */
1004 spin_unlock_bh(&mfc_unres_lock);
1005
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001006 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 kfree_skb(skb);
1008 return err;
1009 }
1010
Patrick McHardy0c122952010-04-13 05:03:22 +00001011 atomic_inc(&mrt->cache_resolve_queue_len);
1012 list_add(&c->list, &mrt->mfc_unres_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
David S. Miller278554b2010-05-12 00:05:35 -07001014 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1015 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001018 /* See if we can append the packet */
1019
1020 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 kfree_skb(skb);
1022 err = -ENOBUFS;
1023 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001024 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 err = 0;
1026 }
1027
1028 spin_unlock_bh(&mfc_unres_lock);
1029 return err;
1030}
1031
1032/*
1033 * MFC cache manipulation by user space mroute daemon
1034 */
1035
Patrick McHardy0c122952010-04-13 05:03:22 +00001036static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001039 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jianjun Kongc354e122008-11-03 00:28:02 -08001041 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Patrick McHardy0c122952010-04-13 05:03:22 +00001043 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1045 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001046 list_del_rcu(&c->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001048 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 0;
1050 }
1051 }
1052 return -ENOENT;
1053}
1054
Patrick McHardy0c122952010-04-13 05:03:22 +00001055static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1056 struct mfcctl *mfc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Patrick McHardy862465f2010-04-13 05:03:21 +00001058 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001060 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Patrick McHardya50436f2010-03-17 06:04:14 +00001062 if (mfc->mfcc_parent >= MAXVIFS)
1063 return -ENFILE;
1064
Jianjun Kongc354e122008-11-03 00:28:02 -08001065 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Patrick McHardy0c122952010-04-13 05:03:22 +00001067 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001069 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1070 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074
Patrick McHardy862465f2010-04-13 05:03:21 +00001075 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 write_lock_bh(&mrt_lock);
1077 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001078 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (!mrtsock)
1080 c->mfc_flags |= MFC_STATIC;
1081 write_unlock_bh(&mrt_lock);
1082 return 0;
1083 }
1084
Joe Perchesf97c1e02007-12-16 13:45:43 -08001085 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return -EINVAL;
1087
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001088 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -08001089 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return -ENOMEM;
1091
Jianjun Kongc354e122008-11-03 00:28:02 -08001092 c->mfc_origin = mfc->mfcc_origin.s_addr;
1093 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1094 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001095 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!mrtsock)
1097 c->mfc_flags |= MFC_STATIC;
1098
Eric Dumazeta8c94862010-10-01 16:15:08 +00001099 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /*
1102 * Check to see if we resolved a queued list. If so we
1103 * need to send on the frames and tidy up.
1104 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001105 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001107 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001108 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001110 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001111 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001112 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 break;
1114 }
1115 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001116 if (list_empty(&mrt->mfc_unres_queue))
1117 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 spin_unlock_bh(&mfc_unres_lock);
1119
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001120 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001121 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001122 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124 return 0;
1125}
1126
1127/*
1128 * Close the multicast socket, and clear the vif tables etc
1129 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001130
Patrick McHardy0c122952010-04-13 05:03:22 +00001131static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001134 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001135 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001136
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001137 /* Shut down all active vif entries */
1138
Patrick McHardy0c122952010-04-13 05:03:22 +00001139 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001140 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001141 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001143 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001145 /* Wipe the cache */
1146
Patrick McHardy862465f2010-04-13 05:03:21 +00001147 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001148 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001149 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001151 list_del_rcu(&c->list);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001152 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 }
1155
Patrick McHardy0c122952010-04-13 05:03:22 +00001156 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001158 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001159 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001160 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162 spin_unlock_bh(&mfc_unres_lock);
1163 }
1164}
1165
Eric Dumazet4c968702010-10-01 16:15:01 +00001166/* called from ip_ra_control(), before an RCU grace period,
1167 * we dont need to call synchronize_rcu() here
1168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169static void mrtsock_destruct(struct sock *sk)
1170{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001171 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001172 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001175 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001176 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001177 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Eric Dumazet4c968702010-10-01 16:15:01 +00001178 rcu_assign_pointer(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001179 mroute_clean_tables(mrt);
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 rtnl_unlock();
1183}
1184
1185/*
1186 * Socket options and virtual interface manipulation. The whole
1187 * virtual interface system is a complete heap, but unfortunately
1188 * that's how BSD mrouted happens to think. Maybe one day with a proper
1189 * MOSPF/PIM router set up we can clean this up.
1190 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001191
David S. Millerb7058842009-09-30 16:12:20 -07001192int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 int ret;
1195 struct vifctl vif;
1196 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001197 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001198 struct mr_table *mrt;
1199
1200 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1201 if (mrt == NULL)
1202 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001203
Stephen Hemminger132adf52007-03-08 20:44:43 -08001204 if (optname != MRT_INIT) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001205 if (sk != rcu_dereference_raw(mrt->mroute_sk) &&
1206 !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return -EACCES;
1208 }
1209
Stephen Hemminger132adf52007-03-08 20:44:43 -08001210 switch (optname) {
1211 case MRT_INIT:
1212 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001213 inet_sk(sk)->inet_num != IPPROTO_IGMP)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001214 return -EOPNOTSUPP;
Jianjun Kongc354e122008-11-03 00:28:02 -08001215 if (optlen != sizeof(int))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001216 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Stephen Hemminger132adf52007-03-08 20:44:43 -08001218 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001219 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001221 return -EADDRINUSE;
1222 }
1223
1224 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1225 if (ret == 0) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001226 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001227 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001228 }
1229 rtnl_unlock();
1230 return ret;
1231 case MRT_DONE:
Eric Dumazet4c968702010-10-01 16:15:01 +00001232 if (sk != rcu_dereference_raw(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001233 return -EACCES;
1234 return ip_ra_control(sk, 0, NULL);
1235 case MRT_ADD_VIF:
1236 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001237 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001238 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001239 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001240 return -EFAULT;
1241 if (vif.vifc_vifi >= MAXVIFS)
1242 return -ENFILE;
1243 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001244 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001245 ret = vif_add(net, mrt, &vif,
1246 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001247 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001248 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001249 }
1250 rtnl_unlock();
1251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /*
1254 * Manipulate the forwarding caches. These live
1255 * in a sort of kernel/user symbiosis.
1256 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001257 case MRT_ADD_MFC:
1258 case MRT_DEL_MFC:
Jianjun Kongc354e122008-11-03 00:28:02 -08001259 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001260 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001261 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001262 return -EFAULT;
1263 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001264 if (optname == MRT_DEL_MFC)
Patrick McHardy0c122952010-04-13 05:03:22 +00001265 ret = ipmr_mfc_delete(mrt, &mfc);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001266 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001267 ret = ipmr_mfc_add(net, mrt, &mfc,
1268 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001269 rtnl_unlock();
1270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * Control PIM assert.
1273 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001274 case MRT_ASSERT:
1275 {
1276 int v;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001277 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001278 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001279 mrt->mroute_do_assert = (v) ? 1 : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001280 return 0;
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001283 case MRT_PIM:
1284 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001285 int v;
1286
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001287 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001288 return -EFAULT;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001289 v = (v) ? 1 : 0;
1290
Stephen Hemminger132adf52007-03-08 20:44:43 -08001291 rtnl_lock();
1292 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001293 if (v != mrt->mroute_do_pim) {
1294 mrt->mroute_do_pim = v;
1295 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 rtnl_unlock();
1298 return ret;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001301#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1302 case MRT_TABLE:
1303 {
1304 u32 v;
1305
1306 if (optlen != sizeof(u32))
1307 return -EINVAL;
1308 if (get_user(v, (u32 __user *)optval))
1309 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001310
1311 rtnl_lock();
1312 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001313 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1314 ret = -EBUSY;
1315 } else {
1316 if (!ipmr_new_table(net, v))
1317 ret = -ENOMEM;
1318 raw_sk(sk)->ipmr_table = v;
1319 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001320 rtnl_unlock();
1321 return ret;
1322 }
1323#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001324 /*
1325 * Spurious command, or MRT_VERSION which you cannot
1326 * set.
1327 */
1328 default:
1329 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331}
1332
1333/*
1334 * Getsock opt support for the multicast routing system.
1335 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001336
Jianjun Kongc354e122008-11-03 00:28:02 -08001337int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 int olr;
1340 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001341 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001342 struct mr_table *mrt;
1343
1344 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1345 if (mrt == NULL)
1346 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Jianjun Kongc354e122008-11-03 00:28:02 -08001348 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001350 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001352 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -ENOPROTOOPT;
1354
1355 if (get_user(olr, optlen))
1356 return -EFAULT;
1357
1358 olr = min_t(unsigned int, olr, sizeof(int));
1359 if (olr < 0)
1360 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001361
Jianjun Kongc354e122008-11-03 00:28:02 -08001362 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001364 if (optname == MRT_VERSION)
1365 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001367 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001368 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369#endif
1370 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001371 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001372 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return -EFAULT;
1374 return 0;
1375}
1376
1377/*
1378 * The IP multicast ioctl support routines.
1379 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1382{
1383 struct sioc_sg_req sr;
1384 struct sioc_vif_req vr;
1385 struct vif_device *vif;
1386 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001387 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001388 struct mr_table *mrt;
1389
1390 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1391 if (mrt == NULL)
1392 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001393
Stephen Hemminger132adf52007-03-08 20:44:43 -08001394 switch (cmd) {
1395 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001396 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001397 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001398 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001399 return -EINVAL;
1400 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001401 vif = &mrt->vif_table[vr.vifi];
1402 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001403 vr.icount = vif->pkt_in;
1404 vr.ocount = vif->pkt_out;
1405 vr.ibytes = vif->bytes_in;
1406 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001408
Jianjun Kongc354e122008-11-03 00:28:02 -08001409 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001411 return 0;
1412 }
1413 read_unlock(&mrt_lock);
1414 return -EADDRNOTAVAIL;
1415 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001416 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001417 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Eric Dumazeta8c94862010-10-01 16:15:08 +00001419 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001420 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001421 if (c) {
1422 sr.pktcnt = c->mfc_un.res.pkt;
1423 sr.bytecnt = c->mfc_un.res.bytes;
1424 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001425 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001426
Jianjun Kongc354e122008-11-03 00:28:02 -08001427 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001428 return -EFAULT;
1429 return 0;
1430 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001431 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001432 return -EADDRNOTAVAIL;
1433 default:
1434 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436}
1437
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001438#ifdef CONFIG_COMPAT
1439struct compat_sioc_sg_req {
1440 struct in_addr src;
1441 struct in_addr grp;
1442 compat_ulong_t pktcnt;
1443 compat_ulong_t bytecnt;
1444 compat_ulong_t wrong_if;
1445};
1446
David S. Millerca6b8bb2011-02-03 17:24:28 -08001447struct compat_sioc_vif_req {
1448 vifi_t vifi; /* Which iface */
1449 compat_ulong_t icount;
1450 compat_ulong_t ocount;
1451 compat_ulong_t ibytes;
1452 compat_ulong_t obytes;
1453};
1454
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001455int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1456{
David S. Miller0033d5a2011-02-03 17:21:31 -08001457 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb2011-02-03 17:24:28 -08001458 struct compat_sioc_vif_req vr;
1459 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001460 struct mfc_cache *c;
1461 struct net *net = sock_net(sk);
1462 struct mr_table *mrt;
1463
1464 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1465 if (mrt == NULL)
1466 return -ENOENT;
1467
1468 switch (cmd) {
David S. Millerca6b8bb2011-02-03 17:24:28 -08001469 case SIOCGETVIFCNT:
1470 if (copy_from_user(&vr, arg, sizeof(vr)))
1471 return -EFAULT;
1472 if (vr.vifi >= mrt->maxvif)
1473 return -EINVAL;
1474 read_lock(&mrt_lock);
1475 vif = &mrt->vif_table[vr.vifi];
1476 if (VIF_EXISTS(mrt, vr.vifi)) {
1477 vr.icount = vif->pkt_in;
1478 vr.ocount = vif->pkt_out;
1479 vr.ibytes = vif->bytes_in;
1480 vr.obytes = vif->bytes_out;
1481 read_unlock(&mrt_lock);
1482
1483 if (copy_to_user(arg, &vr, sizeof(vr)))
1484 return -EFAULT;
1485 return 0;
1486 }
1487 read_unlock(&mrt_lock);
1488 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001489 case SIOCGETSGCNT:
1490 if (copy_from_user(&sr, arg, sizeof(sr)))
1491 return -EFAULT;
1492
1493 rcu_read_lock();
1494 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1495 if (c) {
1496 sr.pktcnt = c->mfc_un.res.pkt;
1497 sr.bytecnt = c->mfc_un.res.bytes;
1498 sr.wrong_if = c->mfc_un.res.wrong_if;
1499 rcu_read_unlock();
1500
1501 if (copy_to_user(arg, &sr, sizeof(sr)))
1502 return -EFAULT;
1503 return 0;
1504 }
1505 rcu_read_unlock();
1506 return -EADDRNOTAVAIL;
1507 default:
1508 return -ENOIOCTLCMD;
1509 }
1510}
1511#endif
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1515{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001516 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001517 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001518 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct vif_device *v;
1520 int ct;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001521 LIST_HEAD(list);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (event != NETDEV_UNREGISTER)
1524 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001525
1526 ipmr_for_each_table(mrt, net) {
1527 v = &mrt->vif_table[0];
1528 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1529 if (v->dev == dev)
1530 vif_delete(mrt, ct, 1, &list);
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001533 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 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;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001551 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;
1597 int encap = 0;
1598
1599 if (vif->dev == NULL)
1600 goto out_free;
1601
1602#ifdef CONFIG_IP_PIMSM
1603 if (vif->flags & VIFF_REGISTER) {
1604 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001605 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001606 vif->dev->stats.tx_bytes += skb->len;
1607 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001608 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001609 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611#endif
1612
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001613 if (vif->flags & VIFF_TUNNEL) {
1614 struct flowi fl = {
1615 .oif = vif->link,
Changli Gao58116622010-11-12 18:43:55 +00001616 .fl4_dst = vif->remote,
1617 .fl4_src = vif->local,
1618 .fl4_tos = RT_TOS(iph->tos),
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001619 .proto = IPPROTO_IPIP
1620 };
David S. Millerb23dd4f2011-03-02 14:31:35 -08001621 rt = ip_route_output_key(net, &fl);
1622 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto out_free;
1624 encap = sizeof(struct iphdr);
1625 } else {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001626 struct flowi fl = {
1627 .oif = vif->link,
Changli Gao58116622010-11-12 18:43:55 +00001628 .fl4_dst = iph->daddr,
1629 .fl4_tos = RT_TOS(iph->tos),
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001630 .proto = IPPROTO_IPIP
1631 };
David S. Millerb23dd4f2011-03-02 14:31:35 -08001632 rt = ip_route_output_key(net, &fl);
1633 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 goto out_free;
1635 }
1636
Changli Gaod8d1f302010-06-10 23:31:35 -07001637 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Changli Gaod8d1f302010-06-10 23:31:35 -07001639 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001641 * allow to send ICMP, so that packets will disappear
1642 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 */
1644
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001645 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 ip_rt_put(rt);
1647 goto out_free;
1648 }
1649
Changli Gaod8d1f302010-06-10 23:31:35 -07001650 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001653 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 goto out_free;
1655 }
1656
1657 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001658 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Eric Dumazetadf30902009-06-02 05:19:30 +00001660 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001661 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001662 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001665 * What do we do with netfilter? -- RR
1666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (vif->flags & VIFF_TUNNEL) {
1668 ip_encap(skb, vif->local, vif->remote);
1669 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001670 vif->dev->stats.tx_packets++;
1671 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
1674 IPCB(skb)->flags |= IPSKB_FORWARDED;
1675
1676 /*
1677 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1678 * not only before forwarding, but after forwarding on all output
1679 * interfaces. It is clear, if mrouter runs a multicasting
1680 * program, it should receive packets not depending to what interface
1681 * program is joined.
1682 * If we will not make it, the program will have to join on all
1683 * interfaces. On the other hand, multihoming host (or router, but
1684 * not mrouter) cannot join to more than one interface - it will
1685 * result in receiving multiple packets.
1686 */
Jan Engelhardt9bbc7682010-03-23 04:07:29 +01001687 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 ipmr_forward_finish);
1689 return;
1690
1691out_free:
1692 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Patrick McHardy0c122952010-04-13 05:03:22 +00001695static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001698
1699 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1700 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 break;
1702 }
1703 return ct;
1704}
1705
1706/* "local" means that we should preserve one skb (for local delivery) */
1707
Patrick McHardy0c122952010-04-13 05:03:22 +00001708static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1709 struct sk_buff *skb, struct mfc_cache *cache,
1710 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 int psend = -1;
1713 int vif, ct;
1714
1715 vif = cache->mfc_parent;
1716 cache->mfc_un.res.pkt++;
1717 cache->mfc_un.res.bytes += skb->len;
1718
1719 /*
1720 * Wrong interface: drop packet and (maybe) send PIM assert.
1721 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001722 if (mrt->vif_table[vif].dev != skb->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int true_vifi;
1724
David S. Millerc7537962010-11-11 17:07:48 -08001725 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001727 * Very complicated situation...
1728 *
1729 * The best workaround until routing daemons will be
1730 * fixed is not to redistribute packet, if it was
1731 * send through wrong interface. It means, that
1732 * multicast applications WILL NOT work for
1733 * (S,G), which have default multicast route pointing
1734 * to wrong oif. In any case, it is not a good
1735 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
1737 goto dont_forward;
1738 }
1739
1740 cache->mfc_un.res.wrong_if++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001741 true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Patrick McHardy0c122952010-04-13 05:03:22 +00001743 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001745 * so that we cannot check that packet arrived on an oif.
1746 * It is bad, but otherwise we would need to move pretty
1747 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001749 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001750 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001751 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1753 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001754 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 goto dont_forward;
1757 }
1758
Patrick McHardy0c122952010-04-13 05:03:22 +00001759 mrt->vif_table[vif].pkt_in++;
1760 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /*
1763 * Forward the frame
1764 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001765 for (ct = cache->mfc_un.res.maxvif - 1;
1766 ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001767 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (psend != -1) {
1769 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001772 ipmr_queue_xmit(net, mrt, skb2, cache,
1773 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001775 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777 }
1778 if (psend != -1) {
1779 if (local) {
1780 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001783 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001785 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return 0;
1787 }
1788 }
1789
1790dont_forward:
1791 if (!local)
1792 kfree_skb(skb);
1793 return 0;
1794}
1795
1796
1797/*
1798 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001799 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
1801
1802int ip_mr_input(struct sk_buff *skb)
1803{
1804 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001805 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001806 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001807 struct mr_table *mrt;
1808 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001811 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001813 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto dont_forward;
1815
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001816 err = ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001817 if (err < 0) {
1818 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001819 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +00001820 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001823 if (IPCB(skb)->opt.router_alert) {
1824 if (ip_call_ra_chain(skb))
1825 return 0;
1826 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1827 /* IGMPv1 (and broken IGMPv2 implementations sort of
1828 * Cisco IOS <= 11.2(8)) do not put router alert
1829 * option to IGMP packets destined to routable
1830 * groups. It is very bad, because it means
1831 * that we can forward NO IGMP messages.
1832 */
1833 struct sock *mroute_sk;
1834
1835 mroute_sk = rcu_dereference(mrt->mroute_sk);
1836 if (mroute_sk) {
1837 nf_reset(skb);
1838 raw_rcv(mroute_sk, skb);
1839 return 0;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842 }
1843
Eric Dumazeta8c94862010-10-01 16:15:08 +00001844 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001845 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 /*
1848 * No usable cache entry
1849 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001850 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 int vif;
1852
1853 if (local) {
1854 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1855 ip_local_deliver(skb);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001856 if (skb2 == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 skb = skb2;
1859 }
1860
Eric Dumazeta8c94862010-10-01 16:15:08 +00001861 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001862 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001864 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 read_unlock(&mrt_lock);
1866
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001867 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869 read_unlock(&mrt_lock);
1870 kfree_skb(skb);
1871 return -ENODEV;
1872 }
1873
Eric Dumazeta8c94862010-10-01 16:15:08 +00001874 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001875 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 read_unlock(&mrt_lock);
1877
1878 if (local)
1879 return ip_local_deliver(skb);
1880
1881 return 0;
1882
1883dont_forward:
1884 if (local)
1885 return ip_local_deliver(skb);
1886 kfree_skb(skb);
1887 return 0;
1888}
1889
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001890#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00001891/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001892static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1893 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001895 struct net_device *reg_dev = NULL;
1896 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001898 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001900 * Check that:
1901 * a. packet is really sent to a multicast group
1902 * b. packet is not a NULL-REGISTER
1903 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001905 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001907 ntohs(encap->tot_len) + pimlen > skb->len)
1908 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001911 if (mrt->mroute_reg_vif_num >= 0)
1912 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 read_unlock(&mrt_lock);
1914
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001915 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001916 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001918 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00001919 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001920 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00001922 skb->ip_summed = CHECKSUM_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 skb->pkt_type = PACKET_HOST;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07001924
1925 skb_tunnel_rx(skb, reg_dev);
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001928
Eric Dumazet55747a02010-10-01 16:14:55 +00001929 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001930}
1931#endif
1932
1933#ifdef CONFIG_IP_PIMSM_V1
1934/*
1935 * Handle IGMP messages of PIMv1
1936 */
1937
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001938int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001939{
1940 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001941 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001942 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001943
1944 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1945 goto drop;
1946
1947 pim = igmp_hdr(skb);
1948
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001949 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1950 goto drop;
1951
Patrick McHardy0c122952010-04-13 05:03:22 +00001952 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001953 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1954 goto drop;
1955
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001956 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001957drop:
1958 kfree_skb(skb);
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return 0;
1961}
1962#endif
1963
1964#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001965static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001968 struct net *net = dev_net(skb->dev);
1969 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001971 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 goto drop;
1973
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001974 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001975 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1976 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001977 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08001978 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto drop;
1980
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001981 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1982 goto drop;
1983
1984 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001985drop:
1986 kfree_skb(skb);
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return 0;
1989}
1990#endif
1991
Patrick McHardycb6a4e42010-04-26 16:02:08 +02001992static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
1993 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 int ct;
1996 struct rtnexthop *nhp;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001997 u8 *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct rtattr *mp_head;
1999
Nicolas Dichtel74381892010-03-25 23:45:35 +00002000 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f1602010-05-26 00:38:56 -07002001 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002002 return -ENOENT;
2003
Patrick McHardy0c122952010-04-13 05:03:22 +00002004 if (VIF_EXISTS(mrt, c->mfc_parent))
2005 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Jianjun Kongc354e122008-11-03 00:28:02 -08002007 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002010 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2012 goto rtattr_failure;
Jianjun Kongc354e122008-11-03 00:28:02 -08002013 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 nhp->rtnh_flags = 0;
2015 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002016 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 nhp->rtnh_len = sizeof(*nhp);
2018 }
2019 }
2020 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07002021 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 rtm->rtm_type = RTN_MULTICAST;
2023 return 1;
2024
2025rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07002026 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 return -EMSGSIZE;
2028}
2029
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002030int ipmr_get_route(struct net *net,
2031 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 int err;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002034 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct mfc_cache *cache;
Eric Dumazet511c3f92009-06-02 05:14:27 +00002036 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002038 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2039 if (mrt == NULL)
2040 return -ENOENT;
2041
Eric Dumazeta8c94862010-10-01 16:15:08 +00002042 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002043 cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Jianjun Kongc354e122008-11-03 00:28:02 -08002045 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002046 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002047 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002049 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002052 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return -EAGAIN;
2054 }
2055
2056 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002057 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002058 if (dev)
2059 vif = ipmr_find_vif(mrt, dev);
2060 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002062 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -ENODEV;
2064 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002065 skb2 = skb_clone(skb, GFP_ATOMIC);
2066 if (!skb2) {
2067 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002068 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002069 return -ENOMEM;
2070 }
2071
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002072 skb_push(skb2, sizeof(struct iphdr));
2073 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002074 iph = ip_hdr(skb2);
2075 iph->ihl = sizeof(struct iphdr) >> 2;
2076 iph->saddr = rt->rt_src;
2077 iph->daddr = rt->rt_dst;
2078 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002079 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002081 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return err;
2083 }
2084
Eric Dumazeta8c94862010-10-01 16:15:08 +00002085 read_lock(&mrt_lock);
2086 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002088 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002090 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return err;
2092}
2093
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002094static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2095 u32 pid, u32 seq, struct mfc_cache *c)
2096{
2097 struct nlmsghdr *nlh;
2098 struct rtmsg *rtm;
2099
2100 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2101 if (nlh == NULL)
2102 return -EMSGSIZE;
2103
2104 rtm = nlmsg_data(nlh);
2105 rtm->rtm_family = RTNL_FAMILY_IPMR;
2106 rtm->rtm_dst_len = 32;
2107 rtm->rtm_src_len = 32;
2108 rtm->rtm_tos = 0;
2109 rtm->rtm_table = mrt->id;
2110 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2111 rtm->rtm_type = RTN_MULTICAST;
2112 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2113 rtm->rtm_protocol = RTPROT_UNSPEC;
2114 rtm->rtm_flags = 0;
2115
2116 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2117 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2118
2119 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2120 goto nla_put_failure;
2121
2122 return nlmsg_end(skb, nlh);
2123
2124nla_put_failure:
2125 nlmsg_cancel(skb, nlh);
2126 return -EMSGSIZE;
2127}
2128
2129static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2130{
2131 struct net *net = sock_net(skb->sk);
2132 struct mr_table *mrt;
2133 struct mfc_cache *mfc;
2134 unsigned int t = 0, s_t;
2135 unsigned int h = 0, s_h;
2136 unsigned int e = 0, s_e;
2137
2138 s_t = cb->args[0];
2139 s_h = cb->args[1];
2140 s_e = cb->args[2];
2141
Eric Dumazeta8c94862010-10-01 16:15:08 +00002142 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002143 ipmr_for_each_table(mrt, net) {
2144 if (t < s_t)
2145 goto next_table;
2146 if (t > s_t)
2147 s_h = 0;
2148 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002149 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002150 if (e < s_e)
2151 goto next_entry;
2152 if (ipmr_fill_mroute(mrt, skb,
2153 NETLINK_CB(cb->skb).pid,
2154 cb->nlh->nlmsg_seq,
2155 mfc) < 0)
2156 goto done;
2157next_entry:
2158 e++;
2159 }
2160 e = s_e = 0;
2161 }
2162 s_h = 0;
2163next_table:
2164 t++;
2165 }
2166done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002167 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002168
2169 cb->args[2] = e;
2170 cb->args[1] = h;
2171 cb->args[0] = t;
2172
2173 return skb->len;
2174}
2175
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002176#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002178 * The /proc interfaces to multicast routing :
2179 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 */
2181struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002182 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002183 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 int ct;
2185};
2186
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002187static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2188 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 loff_t pos)
2190{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002191 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002192
2193 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2194 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002196 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002197 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199 return NULL;
2200}
2201
2202static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002203 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002205 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002206 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002207 struct mr_table *mrt;
2208
2209 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2210 if (mrt == NULL)
2211 return ERR_PTR(-ENOENT);
2212
2213 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002216 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 : SEQ_START_TOKEN;
2218}
2219
2220static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2221{
2222 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002223 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002224 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 ++*pos;
2227 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002228 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002229
Patrick McHardy0c122952010-04-13 05:03:22 +00002230 while (++iter->ct < mrt->maxvif) {
2231 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002233 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
2235 return NULL;
2236}
2237
2238static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002239 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
2241 read_unlock(&mrt_lock);
2242}
2243
2244static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2245{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002246 struct ipmr_vif_iter *iter = seq->private;
2247 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002250 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2252 } else {
2253 const struct vif_device *vif = v;
2254 const char *name = vif->dev ? vif->dev->name : "none";
2255
2256 seq_printf(seq,
2257 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002258 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002259 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 vif->bytes_out, vif->pkt_out,
2261 vif->flags, vif->local, vif->remote);
2262 }
2263 return 0;
2264}
2265
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002266static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 .start = ipmr_vif_seq_start,
2268 .next = ipmr_vif_seq_next,
2269 .stop = ipmr_vif_seq_stop,
2270 .show = ipmr_vif_seq_show,
2271};
2272
2273static int ipmr_vif_open(struct inode *inode, struct file *file)
2274{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002275 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2276 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
Arjan van de Ven9a321442007-02-12 00:55:35 -08002279static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 .owner = THIS_MODULE,
2281 .open = ipmr_vif_open,
2282 .read = seq_read,
2283 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002284 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285};
2286
2287struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002288 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002289 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002290 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 int ct;
2292};
2293
2294
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002295static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2296 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002298 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 struct mfc_cache *mfc;
2300
Eric Dumazeta8c94862010-10-01 16:15:08 +00002301 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002302 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002303 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002304 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002305 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002307 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002308 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002311 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002312 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002313 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return mfc;
2315 spin_unlock_bh(&mfc_unres_lock);
2316
2317 it->cache = NULL;
2318 return NULL;
2319}
2320
2321
2322static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2323{
2324 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002325 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002326 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002327
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002328 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2329 if (mrt == NULL)
2330 return ERR_PTR(-ENOENT);
2331
2332 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 it->cache = NULL;
2334 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002335 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 : SEQ_START_TOKEN;
2337}
2338
2339static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2340{
2341 struct mfc_cache *mfc = v;
2342 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002343 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002344 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 ++*pos;
2347
2348 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002349 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Patrick McHardy862465f2010-04-13 05:03:21 +00002351 if (mfc->list.next != it->cache)
2352 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002353
Patrick McHardy0c122952010-04-13 05:03:22 +00002354 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 goto end_of_list;
2356
Patrick McHardy0c122952010-04-13 05:03:22 +00002357 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002360 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002361 if (list_empty(it->cache))
2362 continue;
2363 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
2365
2366 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002367 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002368 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002372 if (!list_empty(it->cache))
2373 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002375end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 spin_unlock_bh(&mfc_unres_lock);
2377 it->cache = NULL;
2378
2379 return NULL;
2380}
2381
2382static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2383{
2384 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002385 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Patrick McHardy0c122952010-04-13 05:03:22 +00002387 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002389 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002390 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
2393static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2394{
2395 int n;
2396
2397 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002398 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2400 } else {
2401 const struct mfc_cache *mfc = v;
2402 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002403 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002404
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002405 seq_printf(seq, "%08X %08X %-3hd",
2406 (__force u32) mfc->mfc_mcastgrp,
2407 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002408 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Patrick McHardy0c122952010-04-13 05:03:22 +00002410 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002411 seq_printf(seq, " %8lu %8lu %8lu",
2412 mfc->mfc_un.res.pkt,
2413 mfc->mfc_un.res.bytes,
2414 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002415 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002416 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002417 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002418 mfc->mfc_un.res.ttls[n] < 255)
2419 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002420 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 n, mfc->mfc_un.res.ttls[n]);
2422 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002423 } else {
2424 /* unresolved mfc_caches don't contain
2425 * pkt, bytes and wrong_if values
2426 */
2427 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429 seq_putc(seq, '\n');
2430 }
2431 return 0;
2432}
2433
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002434static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 .start = ipmr_mfc_seq_start,
2436 .next = ipmr_mfc_seq_next,
2437 .stop = ipmr_mfc_seq_stop,
2438 .show = ipmr_mfc_seq_show,
2439};
2440
2441static int ipmr_mfc_open(struct inode *inode, struct file *file)
2442{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002443 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2444 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Arjan van de Ven9a321442007-02-12 00:55:35 -08002447static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 .owner = THIS_MODULE,
2449 .open = ipmr_mfc_open,
2450 .read = seq_read,
2451 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002452 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002454#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002457static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002459 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460};
2461#endif
2462
2463
2464/*
2465 * Setup for IP multicast routing
2466 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002467static int __net_init ipmr_net_init(struct net *net)
2468{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002469 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002470
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002471 err = ipmr_rules_init(net);
2472 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002473 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002474
2475#ifdef CONFIG_PROC_FS
2476 err = -ENOMEM;
2477 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2478 goto proc_vif_fail;
2479 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2480 goto proc_cache_fail;
2481#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002482 return 0;
2483
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002484#ifdef CONFIG_PROC_FS
2485proc_cache_fail:
2486 proc_net_remove(net, "ip_mr_vif");
2487proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002488 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002489#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002490fail:
2491 return err;
2492}
2493
2494static void __net_exit ipmr_net_exit(struct net *net)
2495{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002496#ifdef CONFIG_PROC_FS
2497 proc_net_remove(net, "ip_mr_cache");
2498 proc_net_remove(net, "ip_mr_vif");
2499#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002500 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002501}
2502
2503static struct pernet_operations ipmr_net_ops = {
2504 .init = ipmr_net_init,
2505 .exit = ipmr_net_exit,
2506};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002507
Wang Chen03d2f892008-07-03 12:13:36 +08002508int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Wang Chen03d2f892008-07-03 12:13:36 +08002510 int err;
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2513 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002514 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002515 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002516 if (!mrt_cachep)
2517 return -ENOMEM;
2518
Benjamin Therycf958ae32009-01-22 04:56:16 +00002519 err = register_pernet_subsys(&ipmr_net_ops);
2520 if (err)
2521 goto reg_pernet_fail;
2522
Wang Chen03d2f892008-07-03 12:13:36 +08002523 err = register_netdevice_notifier(&ip_mr_notifier);
2524 if (err)
2525 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002526#ifdef CONFIG_IP_PIMSM_V2
2527 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2528 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2529 err = -EAGAIN;
2530 goto add_proto_fail;
2531 }
2532#endif
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002533 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, NULL, ipmr_rtm_dumproute);
Wang Chen03d2f892008-07-03 12:13:36 +08002534 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002535
Tom Goff403dbb92009-06-14 03:16:13 -07002536#ifdef CONFIG_IP_PIMSM_V2
2537add_proto_fail:
2538 unregister_netdevice_notifier(&ip_mr_notifier);
2539#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002540reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002541 unregister_pernet_subsys(&ipmr_net_ops);
2542reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002543 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002544 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}