blob: e153ab7b17a143a3869bc5aa7091ff478bb31435 [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/uaccess.h>
30#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#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>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Patrick McHardy0c122952010-04-13 05:03:22 +000070struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000071 struct list_head list;
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -050072 possible_net_t net;
Patrick McHardyf0ad0862010-04-13 05:03:23 +000073 u32 id;
Eric Dumazet4c9687092010-10-01 16:15:01 +000074 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000075 struct timer_list ipmr_expire_timer;
76 struct list_head mfc_unres_queue;
77 struct list_head mfc_cache_array[MFC_LINES];
78 struct vif_device vif_table[MAXVIFS];
79 int maxvif;
80 atomic_t cache_resolve_queue_len;
Joe Perches53d68412012-11-25 09:35:30 +000081 bool mroute_do_assert;
82 bool mroute_do_pim;
Patrick McHardy0c122952010-04-13 05:03:22 +000083 int mroute_reg_vif_num;
Patrick McHardy0c122952010-04-13 05:03:22 +000084};
85
Patrick McHardyf0ad0862010-04-13 05:03:23 +000086struct ipmr_rule {
87 struct fib_rule common;
88};
89
90struct ipmr_result {
91 struct mr_table *mrt;
92};
93
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +010094static inline bool pimsm_enabled(void)
95{
96 return IS_BUILTIN(CONFIG_IP_PIMSM_V1) || IS_BUILTIN(CONFIG_IP_PIMSM_V2);
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000100 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102
103static DEFINE_RWLOCK(mrt_lock);
104
105/*
106 * Multicast router control variables
107 */
108
Patrick McHardy0c122952010-04-13 05:03:22 +0000109#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/* Special spinlock for queue of unresolved entries */
112static DEFINE_SPINLOCK(mfc_unres_lock);
113
114/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000115 * entries is changed only in process context and protected
116 * with weak lock mrt_lock. Queue of unresolved entries is protected
117 * with strong spinlock mfc_unres_lock.
118 *
119 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
121
Christoph Lametere18b8902006-12-06 20:33:20 -0800122static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000124static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000125static void ipmr_free_table(struct mr_table *mrt);
126
Rami Rosenc4854ec2013-07-20 15:09:28 +0300127static void ip_mr_forward(struct net *net, struct mr_table *mrt,
128 struct sk_buff *skb, struct mfc_cache *cache,
129 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000130static 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);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000134static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
135 int cmd);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000136static void mroute_clean_tables(struct mr_table *mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000137static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000139#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
140#define ipmr_for_each_table(mrt, net) \
141 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
142
143static struct mr_table *ipmr_get_table(struct net *net, u32 id)
144{
145 struct mr_table *mrt;
146
147 ipmr_for_each_table(mrt, net) {
148 if (mrt->id == id)
149 return mrt;
150 }
151 return NULL;
152}
153
David S. Millerda919812011-03-12 02:04:50 -0500154static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000155 struct mr_table **mrt)
156{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000157 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100158 struct ipmr_result res;
159 struct fib_lookup_arg arg = {
160 .result = &res,
161 .flags = FIB_LOOKUP_NOREF,
162 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000163
David S. Millerda919812011-03-12 02:04:50 -0500164 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
165 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000166 if (err < 0)
167 return err;
168 *mrt = res.mrt;
169 return 0;
170}
171
172static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
173 int flags, struct fib_lookup_arg *arg)
174{
175 struct ipmr_result *res = arg->result;
176 struct mr_table *mrt;
177
178 switch (rule->action) {
179 case FR_ACT_TO_TBL:
180 break;
181 case FR_ACT_UNREACHABLE:
182 return -ENETUNREACH;
183 case FR_ACT_PROHIBIT:
184 return -EACCES;
185 case FR_ACT_BLACKHOLE:
186 default:
187 return -EINVAL;
188 }
189
190 mrt = ipmr_get_table(rule->fr_net, rule->table);
Ian Morris51456b22015-04-03 09:17:26 +0100191 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000192 return -EAGAIN;
193 res->mrt = mrt;
194 return 0;
195}
196
197static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
198{
199 return 1;
200}
201
202static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
203 FRA_GENERIC_POLICY,
204};
205
206static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
207 struct fib_rule_hdr *frh, struct nlattr **tb)
208{
209 return 0;
210}
211
212static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
213 struct nlattr **tb)
214{
215 return 1;
216}
217
218static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
219 struct fib_rule_hdr *frh)
220{
221 frh->dst_len = 0;
222 frh->src_len = 0;
223 frh->tos = 0;
224 return 0;
225}
226
Andi Kleen04a6f822012-10-04 17:12:11 -0700227static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200228 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000229 .rule_size = sizeof(struct ipmr_rule),
230 .addr_size = sizeof(u32),
231 .action = ipmr_rule_action,
232 .match = ipmr_rule_match,
233 .configure = ipmr_rule_configure,
234 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000235 .fill = ipmr_rule_fill,
236 .nlgroup = RTNLGRP_IPV4_RULE,
237 .policy = ipmr_rule_policy,
238 .owner = THIS_MODULE,
239};
240
241static int __net_init ipmr_rules_init(struct net *net)
242{
243 struct fib_rules_ops *ops;
244 struct mr_table *mrt;
245 int err;
246
247 ops = fib_rules_register(&ipmr_rules_ops_template, net);
248 if (IS_ERR(ops))
249 return PTR_ERR(ops);
250
251 INIT_LIST_HEAD(&net->ipv4.mr_tables);
252
253 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100254 if (IS_ERR(mrt)) {
255 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000256 goto err1;
257 }
258
259 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
260 if (err < 0)
261 goto err2;
262
263 net->ipv4.mr_rules_ops = ops;
264 return 0;
265
266err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700267 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000268err1:
269 fib_rules_unregister(ops);
270 return err;
271}
272
273static void __net_exit ipmr_rules_exit(struct net *net)
274{
275 struct mr_table *mrt, *next;
276
WANG Conged785302015-03-31 11:01:45 -0700277 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000278 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
279 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000280 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000281 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000282 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700283 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000284}
285#else
286#define ipmr_for_each_table(mrt, net) \
287 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
288
289static struct mr_table *ipmr_get_table(struct net *net, u32 id)
290{
291 return net->ipv4.mrt;
292}
293
David S. Millerda919812011-03-12 02:04:50 -0500294static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000295 struct mr_table **mrt)
296{
297 *mrt = net->ipv4.mrt;
298 return 0;
299}
300
301static int __net_init ipmr_rules_init(struct net *net)
302{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100303 struct mr_table *mrt;
304
305 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
306 if (IS_ERR(mrt))
307 return PTR_ERR(mrt);
308 net->ipv4.mrt = mrt;
309 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000310}
311
312static void __net_exit ipmr_rules_exit(struct net *net)
313{
WANG Conged785302015-03-31 11:01:45 -0700314 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000315 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700316 net->ipv4.mrt = NULL;
317 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000318}
319#endif
320
321static struct mr_table *ipmr_new_table(struct net *net, u32 id)
322{
323 struct mr_table *mrt;
324 unsigned int i;
325
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100326 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
327 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
328 return ERR_PTR(-EINVAL);
329
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000330 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100331 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000332 return mrt;
333
334 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100335 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100336 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200337 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000338 mrt->id = id;
339
340 /* Forwarding cache */
341 for (i = 0; i < MFC_LINES; i++)
342 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
343
344 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
345
346 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
347 (unsigned long)mrt);
348
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000349 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000350#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
351 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
352#endif
353 return mrt;
354}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000356static void ipmr_free_table(struct mr_table *mrt)
357{
358 del_timer_sync(&mrt->ipmr_expire_timer);
359 mroute_clean_tables(mrt);
360 kfree(mrt);
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
364
Wang Chend6070322008-07-14 20:55:26 -0700365static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
366{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000367 struct net *net = dev_net(dev);
368
Wang Chend6070322008-07-14 20:55:26 -0700369 dev_close(dev);
370
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000371 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700372 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800373 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700374 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700375 struct ip_tunnel_parm p;
376
377 memset(&p, 0, sizeof(p));
378 p.iph.daddr = v->vifc_rmt_addr.s_addr;
379 p.iph.saddr = v->vifc_lcl_addr.s_addr;
380 p.iph.version = 4;
381 p.iph.ihl = 5;
382 p.iph.protocol = IPPROTO_IPIP;
383 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
384 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
385
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800386 if (ops->ndo_do_ioctl) {
387 mm_segment_t oldfs = get_fs();
388
389 set_fs(KERNEL_DS);
390 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
391 set_fs(oldfs);
392 }
Wang Chend6070322008-07-14 20:55:26 -0700393 }
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000397struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct net_device *dev;
400
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000401 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800404 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int err;
406 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct ip_tunnel_parm p;
408 struct in_device *in_dev;
409
410 memset(&p, 0, sizeof(p));
411 p.iph.daddr = v->vifc_rmt_addr.s_addr;
412 p.iph.saddr = v->vifc_lcl_addr.s_addr;
413 p.iph.version = 4;
414 p.iph.ihl = 5;
415 p.iph.protocol = IPPROTO_IPIP;
416 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800417 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800419 if (ops->ndo_do_ioctl) {
420 mm_segment_t oldfs = get_fs();
421
422 set_fs(KERNEL_DS);
423 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
424 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000425 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800426 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 dev = NULL;
429
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000430 if (err == 0 &&
431 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 dev->flags |= IFF_MULTICAST;
433
Herbert Xue5ed6392005-10-03 14:35:55 -0700434 in_dev = __in_dev_get_rtnl(dev);
Ian Morris51456b22015-04-03 09:17:26 +0100435 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700437
438 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100439 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700440 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 if (dev_open(dev))
443 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700444 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 }
447 return dev;
448
449failure:
450 /* allow the register to be completed before unregistering. */
451 rtnl_unlock();
452 rtnl_lock();
453
454 unregister_netdevice(dev);
455 return NULL;
456}
457
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100458#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000459static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000461 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000462 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500463 struct flowi4 fl4 = {
464 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700465 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500466 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000467 };
468 int err;
469
David S. Millerda919812011-03-12 02:04:50 -0500470 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000471 if (err < 0) {
472 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000473 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000474 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700477 dev->stats.tx_bytes += skb->len;
478 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000479 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 read_unlock(&mrt_lock);
481 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000482 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200485static int reg_vif_get_iflink(const struct net_device *dev)
486{
487 return 0;
488}
489
Stephen Hemminger007c3832008-11-20 20:28:35 -0800490static const struct net_device_ops reg_vif_netdev_ops = {
491 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200492 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800493};
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static void reg_vif_setup(struct net_device *dev)
496{
497 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800498 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530500 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700502 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000505static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct net_device *dev;
508 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000509 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000511 if (mrt->id == RT_TABLE_DEFAULT)
512 sprintf(name, "pimreg");
513 else
514 sprintf(name, "pimreg%u", mrt->id);
515
Tom Gundersenc835a672014-07-14 16:37:24 +0200516 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Ian Morris51456b22015-04-03 09:17:26 +0100518 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return NULL;
520
Tom Goff403dbb92009-06-14 03:16:13 -0700521 dev_net_set(dev, net);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (register_netdevice(dev)) {
524 free_netdev(dev);
525 return NULL;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Herbert Xu71e27da2007-06-04 23:36:06 -0700528 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000529 in_dev = __in_dev_get_rcu(dev);
530 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700531 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Herbert Xu71e27da2007-06-04 23:36:06 -0700535 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100536 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700537 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
538 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (dev_open(dev))
541 goto failure;
542
Wang Chen7dc00c82008-07-14 20:56:34 -0700543 dev_hold(dev);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return dev;
546
547failure:
548 /* allow the register to be completed before unregistering. */
549 rtnl_unlock();
550 rtnl_lock();
551
552 unregister_netdevice(dev);
553 return NULL;
554}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100555
556/* called with rcu_read_lock() */
557static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
558 unsigned int pimlen)
559{
560 struct net_device *reg_dev = NULL;
561 struct iphdr *encap;
562
563 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
564 /*
565 * Check that:
566 * a. packet is really sent to a multicast group
567 * b. packet is not a NULL-REGISTER
568 * c. packet is not truncated
569 */
570 if (!ipv4_is_multicast(encap->daddr) ||
571 encap->tot_len == 0 ||
572 ntohs(encap->tot_len) + pimlen > skb->len)
573 return 1;
574
575 read_lock(&mrt_lock);
576 if (mrt->mroute_reg_vif_num >= 0)
577 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
578 read_unlock(&mrt_lock);
579
580 if (!reg_dev)
581 return 1;
582
583 skb->mac_header = skb->network_header;
584 skb_pull(skb, (u8 *)encap - skb->data);
585 skb_reset_network_header(skb);
586 skb->protocol = htons(ETH_P_IP);
587 skb->ip_summed = CHECKSUM_NONE;
588
589 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
590
591 netif_rx(skb);
592
593 return NET_RX_SUCCESS;
594}
595#else
596static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
597{
598 return NULL;
599}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600#endif
601
Ben Hutchings2c530402012-07-10 10:55:09 +0000602/**
603 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700604 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900606
Patrick McHardy0c122952010-04-13 05:03:22 +0000607static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000608 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct vif_device *v;
611 struct net_device *dev;
612 struct in_device *in_dev;
613
Patrick McHardy0c122952010-04-13 05:03:22 +0000614 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return -EADDRNOTAVAIL;
616
Patrick McHardy0c122952010-04-13 05:03:22 +0000617 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 write_lock_bh(&mrt_lock);
620 dev = v->dev;
621 v->dev = NULL;
622
623 if (!dev) {
624 write_unlock_bh(&mrt_lock);
625 return -EADDRNOTAVAIL;
626 }
627
Patrick McHardy0c122952010-04-13 05:03:22 +0000628 if (vifi == mrt->mroute_reg_vif_num)
629 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000631 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000633
634 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000635 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
637 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000638 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640
641 write_unlock_bh(&mrt_lock);
642
643 dev_set_allmulti(dev, -1);
644
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000645 in_dev = __in_dev_get_rtnl(dev);
646 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700647 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000648 inet_netconf_notify_devconf(dev_net(dev),
649 NETCONFA_MC_FORWARDING,
650 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ip_rt_multicast_event(in_dev);
652 }
653
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000654 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000655 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 dev_put(dev);
658 return 0;
659}
660
Eric Dumazeta8c94862010-10-01 16:15:08 +0000661static void ipmr_cache_free_rcu(struct rcu_head *head)
662{
663 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
664
665 kmem_cache_free(mrt_cachep, c);
666}
667
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000668static inline void ipmr_cache_free(struct mfc_cache *c)
669{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000670 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000671}
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000674 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
676
Patrick McHardy0c122952010-04-13 05:03:22 +0000677static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200679 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700681 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Patrick McHardy0c122952010-04-13 05:03:22 +0000683 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Jianjun Kongc354e122008-11-03 00:28:02 -0800685 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700686 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
688 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000689 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000691 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700692 e->error = -ETIMEDOUT;
693 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700694
Eric W. Biederman15e47302012-09-07 20:12:54 +0000695 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000696 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000701 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704
Patrick McHardye258beb2010-04-13 05:03:19 +0000705/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Patrick McHardye258beb2010-04-13 05:03:19 +0000707static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Patrick McHardy0c122952010-04-13 05:03:22 +0000709 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 unsigned long now;
711 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000712 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000715 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return;
717 }
718
Patrick McHardy0c122952010-04-13 05:03:22 +0000719 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 goto out;
721
722 now = jiffies;
723 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Patrick McHardy0c122952010-04-13 05:03:22 +0000725 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (time_after(c->mfc_un.unres.expires, now)) {
727 unsigned long interval = c->mfc_un.unres.expires - now;
728 if (interval < expires)
729 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 continue;
731 }
732
Patrick McHardy862465f2010-04-13 05:03:21 +0000733 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000734 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000735 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
Patrick McHardy0c122952010-04-13 05:03:22 +0000738 if (!list_empty(&mrt->mfc_unres_queue))
739 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741out:
742 spin_unlock(&mfc_unres_lock);
743}
744
745/* Fill oifs list. It is called under write locked mrt_lock. */
746
Patrick McHardy0c122952010-04-13 05:03:22 +0000747static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000748 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 int vifi;
751
752 cache->mfc_un.res.minvif = MAXVIFS;
753 cache->mfc_un.res.maxvif = 0;
754 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
755
Patrick McHardy0c122952010-04-13 05:03:22 +0000756 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
757 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000758 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
760 if (cache->mfc_un.res.minvif > vifi)
761 cache->mfc_un.res.minvif = vifi;
762 if (cache->mfc_un.res.maxvif <= vifi)
763 cache->mfc_un.res.maxvif = vifi + 1;
764 }
765 }
766}
767
Patrick McHardy0c122952010-04-13 05:03:22 +0000768static int vif_add(struct net *net, struct mr_table *mrt,
769 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000772 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct net_device *dev;
774 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700775 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000778 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EADDRINUSE;
780
781 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 case VIFF_REGISTER:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100783 if (!pimsm_enabled())
784 return -EINVAL;
785 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * All the packets will be sent to the daemon
787 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000788 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000790 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (!dev)
792 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700793 err = dev_set_allmulti(dev, 1);
794 if (err) {
795 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700796 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700797 return err;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900800 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000801 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!dev)
803 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700804 err = dev_set_allmulti(dev, 1);
805 if (err) {
806 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700807 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700808 return err;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000811
812 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000814 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
815 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100816 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000817 dev_put(dev);
818 return -EADDRNOTAVAIL;
819 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000820 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000821 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (!dev)
824 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700825 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700826 if (err) {
827 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700828 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
831 default:
832 return -EINVAL;
833 }
834
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000835 in_dev = __in_dev_get_rtnl(dev);
836 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000837 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000839 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700840 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000841 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
842 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 ip_rt_multicast_event(in_dev);
844
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000845 /* Fill in the VIF structures */
846
Jianjun Kongc354e122008-11-03 00:28:02 -0800847 v->rate_limit = vifc->vifc_rate_limit;
848 v->local = vifc->vifc_lcl_addr.s_addr;
849 v->remote = vifc->vifc_rmt_addr.s_addr;
850 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (!mrtsock)
852 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800853 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 v->bytes_in = 0;
855 v->bytes_out = 0;
856 v->pkt_in = 0;
857 v->pkt_out = 0;
858 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000859 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200860 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* And finish update writing critical data */
863 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800864 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000865 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000866 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000867 if (vifi+1 > mrt->maxvif)
868 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 write_unlock_bh(&mrt_lock);
870 return 0;
871}
872
Eric Dumazeta8c94862010-10-01 16:15:08 +0000873/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000874static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000875 __be32 origin,
876 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Jianjun Kongc354e122008-11-03 00:28:02 -0800878 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct mfc_cache *c;
880
Eric Dumazeta8c94862010-10-01 16:15:08 +0000881 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000882 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
883 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000885 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000888/* Look for a (*,*,oif) entry */
889static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
890 int vifi)
891{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100892 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000893 struct mfc_cache *c;
894
895 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100896 if (c->mfc_origin == htonl(INADDR_ANY) &&
897 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000898 c->mfc_un.res.ttls[vifi] < 255)
899 return c;
900
901 return NULL;
902}
903
904/* Look for a (*,G) entry */
905static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
906 __be32 mcastgrp, int vifi)
907{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100908 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000909 struct mfc_cache *c, *proxy;
910
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100911 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000912 goto skip;
913
914 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100915 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000916 c->mfc_mcastgrp == mcastgrp) {
917 if (c->mfc_un.res.ttls[vifi] < 255)
918 return c;
919
920 /* It's ok if the vifi is part of the static tree */
921 proxy = ipmr_cache_find_any_parent(mrt,
922 c->mfc_parent);
923 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
924 return c;
925 }
926
927skip:
928 return ipmr_cache_find_any_parent(mrt, vifi);
929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/*
932 * Allocate a multicast cache entry
933 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000934static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Jianjun Kongc354e122008-11-03 00:28:02 -0800936 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000937
938 if (c)
939 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return c;
941}
942
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000943static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Jianjun Kongc354e122008-11-03 00:28:02 -0800945 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000946
947 if (c) {
948 skb_queue_head_init(&c->mfc_un.unres.unresolved);
949 c->mfc_un.unres.expires = jiffies + 10*HZ;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return c;
952}
953
954/*
955 * A cache entry has gone into a resolved state from queued
956 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900957
Patrick McHardy0c122952010-04-13 05:03:22 +0000958static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
959 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700962 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000964 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Jianjun Kongc354e122008-11-03 00:28:02 -0800966 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700967 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
969
Hong zhi guo573ce262013-03-27 06:47:04 +0000970 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000971 nlh->nlmsg_len = skb_tail_pointer(skb) -
972 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 } else {
974 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000975 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000977 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700978 e->error = -EMSGSIZE;
979 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
Thomas Graf2942e902006-08-15 00:30:25 -0700981
Eric W. Biederman15e47302012-09-07 20:12:54 +0000982 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000983 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000984 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987}
988
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100989/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
990 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100992 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000994static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000995 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300997 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100998 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 struct igmphdr *igmp;
1000 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001001 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int ret;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (assert == IGMPMSG_WHOLEPKT)
1005 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1006 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 skb = alloc_skb(128, GFP_ATOMIC);
1008
Stephen Hemminger132adf52007-03-08 20:44:43 -08001009 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -ENOBUFS;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (assert == IGMPMSG_WHOLEPKT) {
1013 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001014 * Duplicate old header, fix ihl, length etc.
1015 * And all this only to mangle msg->im_msgtype and
1016 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001018 skb_push(skb, sizeof(struct iphdr));
1019 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001020 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001021 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001022 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1024 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001025 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001026 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1027 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1028 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001029 } else {
1030 /* Copy the IP header */
1031 skb_set_network_header(skb, skb->len);
1032 skb_put(skb, ihl);
1033 skb_copy_to_linear_data(skb, pkt->data, ihl);
1034 /* Flag to the kernel this is a route add */
1035 ip_hdr(skb)->protocol = 0;
1036 msg = (struct igmpmsg *)skb_network_header(skb);
1037 msg->im_vif = vifi;
1038 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1039 /* Add our header */
1040 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1041 igmp->type = assert;
1042 msg->im_msgtype = assert;
1043 igmp->code = 0;
1044 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1045 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Eric Dumazet4c9687092010-10-01 16:15:01 +00001048 rcu_read_lock();
1049 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001050 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001051 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 kfree_skb(skb);
1053 return -EINVAL;
1054 }
1055
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001056 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001057 ret = sock_queue_rcv_skb(mroute_sk, skb);
1058 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001059 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001060 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 kfree_skb(skb);
1062 }
1063
1064 return ret;
1065}
1066
1067/*
1068 * Queue a packet for resolution. It gets locked cache entry!
1069 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001072ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Patrick McHardy862465f2010-04-13 05:03:21 +00001074 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int err;
1076 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001077 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001080 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001081 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001082 c->mfc_origin == iph->saddr) {
1083 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087
Patrick McHardy862465f2010-04-13 05:03:21 +00001088 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001089 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Patrick McHardy0c122952010-04-13 05:03:22 +00001091 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001092 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 spin_unlock_bh(&mfc_unres_lock);
1094
1095 kfree_skb(skb);
1096 return -ENOBUFS;
1097 }
1098
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001099 /* Fill in the new cache entry */
1100
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001101 c->mfc_parent = -1;
1102 c->mfc_origin = iph->saddr;
1103 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001105 /* Reflect first query at mrouted. */
1106
Patrick McHardy0c122952010-04-13 05:03:22 +00001107 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001108 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001109 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 out - Brad Parker
1111 */
1112 spin_unlock_bh(&mfc_unres_lock);
1113
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001114 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 kfree_skb(skb);
1116 return err;
1117 }
1118
Patrick McHardy0c122952010-04-13 05:03:22 +00001119 atomic_inc(&mrt->cache_resolve_queue_len);
1120 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001121 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
David S. Miller278554b2010-05-12 00:05:35 -07001123 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1124 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001127 /* See if we can append the packet */
1128
1129 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 kfree_skb(skb);
1131 err = -ENOBUFS;
1132 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001133 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 err = 0;
1135 }
1136
1137 spin_unlock_bh(&mfc_unres_lock);
1138 return err;
1139}
1140
1141/*
1142 * MFC cache manipulation by user space mroute daemon
1143 */
1144
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001145static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001148 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Jianjun Kongc354e122008-11-03 00:28:02 -08001150 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Patrick McHardy0c122952010-04-13 05:03:22 +00001152 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001154 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1155 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001156 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001157 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001158 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return 0;
1160 }
1161 }
1162 return -ENOENT;
1163}
1164
Patrick McHardy0c122952010-04-13 05:03:22 +00001165static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001166 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Patrick McHardy862465f2010-04-13 05:03:21 +00001168 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001170 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Patrick McHardya50436f22010-03-17 06:04:14 +00001172 if (mfc->mfcc_parent >= MAXVIFS)
1173 return -ENFILE;
1174
Jianjun Kongc354e122008-11-03 00:28:02 -08001175 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Patrick McHardy0c122952010-04-13 05:03:22 +00001177 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001179 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1180 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001181 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
Patrick McHardy862465f2010-04-13 05:03:21 +00001186 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 write_lock_bh(&mrt_lock);
1188 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001189 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (!mrtsock)
1191 c->mfc_flags |= MFC_STATIC;
1192 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001193 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return 0;
1195 }
1196
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001197 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001198 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return -EINVAL;
1200
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001201 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001202 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -ENOMEM;
1204
Jianjun Kongc354e122008-11-03 00:28:02 -08001205 c->mfc_origin = mfc->mfcc_origin.s_addr;
1206 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1207 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001208 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (!mrtsock)
1210 c->mfc_flags |= MFC_STATIC;
1211
Eric Dumazeta8c94862010-10-01 16:15:08 +00001212 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /*
1215 * Check to see if we resolved a queued list. If so we
1216 * need to send on the frames and tidy up.
1217 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001218 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001220 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001221 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001223 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001224 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001225 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 break;
1227 }
1228 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001229 if (list_empty(&mrt->mfc_unres_queue))
1230 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 spin_unlock_bh(&mfc_unres_lock);
1232
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001233 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001234 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001235 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001237 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return 0;
1239}
1240
1241/*
1242 * Close the multicast socket, and clear the vif tables etc
1243 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001244
Patrick McHardy0c122952010-04-13 05:03:22 +00001245static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001248 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001249 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001250
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001251 /* Shut down all active vif entries */
1252
Patrick McHardy0c122952010-04-13 05:03:22 +00001253 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001254 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001255 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001257 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001259 /* Wipe the cache */
1260
Patrick McHardy862465f2010-04-13 05:03:21 +00001261 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001262 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001263 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001265 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001266 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001267 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 }
1270
Patrick McHardy0c122952010-04-13 05:03:22 +00001271 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001273 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001274 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001275 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001276 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 spin_unlock_bh(&mfc_unres_lock);
1279 }
1280}
1281
Eric Dumazet4c9687092010-10-01 16:15:01 +00001282/* called from ip_ra_control(), before an RCU grace period,
1283 * we dont need to call synchronize_rcu() here
1284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285static void mrtsock_destruct(struct sock *sk)
1286{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001287 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001288 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001291 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001292 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001293 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001294 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1295 NETCONFA_IFINDEX_ALL,
1296 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001297 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001298 mroute_clean_tables(mrt);
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 rtnl_unlock();
1302}
1303
1304/*
1305 * Socket options and virtual interface manipulation. The whole
1306 * virtual interface system is a complete heap, but unfortunately
1307 * that's how BSD mrouted happens to think. Maybe one day with a proper
1308 * MOSPF/PIM router set up we can clean this up.
1309 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001310
David S. Millerb7058842009-09-30 16:12:20 -07001311int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001313 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 struct vifctl vif;
1315 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001316 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001317 struct mr_table *mrt;
1318
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001319 if (sk->sk_type != SOCK_RAW ||
1320 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1321 return -EOPNOTSUPP;
1322
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001323 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001324 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001325 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001326
Stephen Hemminger132adf52007-03-08 20:44:43 -08001327 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001328 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001329 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return -EACCES;
1331 }
1332
Stephen Hemminger132adf52007-03-08 20:44:43 -08001333 switch (optname) {
1334 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001335 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001336 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Stephen Hemminger132adf52007-03-08 20:44:43 -08001338 rtnl_lock();
Eric Dumazet4c9687092010-10-01 16:15:01 +00001339 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001341 return -EADDRINUSE;
1342 }
1343
1344 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1345 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001346 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001347 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001348 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1349 NETCONFA_IFINDEX_ALL,
1350 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001351 }
1352 rtnl_unlock();
1353 return ret;
1354 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001355 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001356 return -EACCES;
1357 return ip_ra_control(sk, 0, NULL);
1358 case MRT_ADD_VIF:
1359 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001360 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001362 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001363 return -EFAULT;
1364 if (vif.vifc_vifi >= MAXVIFS)
1365 return -ENFILE;
1366 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001367 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001368 ret = vif_add(net, mrt, &vif,
1369 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001370 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001371 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001372 }
1373 rtnl_unlock();
1374 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /*
1377 * Manipulate the forwarding caches. These live
1378 * in a sort of kernel/user symbiosis.
1379 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001380 case MRT_ADD_MFC:
1381 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001382 parent = -1;
1383 case MRT_ADD_MFC_PROXY:
1384 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001385 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001386 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001387 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001389 if (parent == 0)
1390 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001391 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001392 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1393 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001394 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001395 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001396 sk == rtnl_dereference(mrt->mroute_sk),
1397 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001398 rtnl_unlock();
1399 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /*
1401 * Control PIM assert.
1402 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001403 case MRT_ASSERT:
1404 {
1405 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001406 if (optlen != sizeof(v))
1407 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001408 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001409 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001410 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001411 return 0;
1412 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001413 case MRT_PIM:
1414 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001415 int v;
1416
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001417 if (!pimsm_enabled())
1418 return -ENOPROTOOPT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001419 if (optlen != sizeof(v))
1420 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001421 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001422 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001423 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001424
Stephen Hemminger132adf52007-03-08 20:44:43 -08001425 rtnl_lock();
1426 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001427 if (v != mrt->mroute_do_pim) {
1428 mrt->mroute_do_pim = v;
1429 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001431 rtnl_unlock();
1432 return ret;
1433 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001434#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1435 case MRT_TABLE:
1436 {
1437 u32 v;
1438
1439 if (optlen != sizeof(u32))
1440 return -EINVAL;
1441 if (get_user(v, (u32 __user *)optval))
1442 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001443
1444 rtnl_lock();
1445 ret = 0;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001446 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1447 ret = -EBUSY;
1448 } else {
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001449 mrt = ipmr_new_table(net, v);
1450 if (IS_ERR(mrt))
1451 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001452 else
1453 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001454 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001455 rtnl_unlock();
1456 return ret;
1457 }
1458#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001459 /*
1460 * Spurious command, or MRT_VERSION which you cannot
1461 * set.
1462 */
1463 default:
1464 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466}
1467
1468/*
1469 * Getsock opt support for the multicast routing system.
1470 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001471
Jianjun Kongc354e122008-11-03 00:28:02 -08001472int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 int olr;
1475 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001476 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001477 struct mr_table *mrt;
1478
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001479 if (sk->sk_type != SOCK_RAW ||
1480 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1481 return -EOPNOTSUPP;
1482
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001483 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001484 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001485 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Jianjun Kongc354e122008-11-03 00:28:02 -08001487 if (optname != MRT_VERSION &&
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001488 optname != MRT_PIM &&
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001489 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return -ENOPROTOOPT;
1491
1492 if (get_user(olr, optlen))
1493 return -EFAULT;
1494
1495 olr = min_t(unsigned int, olr, sizeof(int));
1496 if (olr < 0)
1497 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001498
Jianjun Kongc354e122008-11-03 00:28:02 -08001499 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return -EFAULT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001501 if (optname == MRT_VERSION) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001502 val = 0x0305;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001503 } else if (optname == MRT_PIM) {
1504 if (!pimsm_enabled())
1505 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001506 val = mrt->mroute_do_pim;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001507 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001508 val = mrt->mroute_do_assert;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001509 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001510 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EFAULT;
1512 return 0;
1513}
1514
1515/*
1516 * The IP multicast ioctl support routines.
1517 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1520{
1521 struct sioc_sg_req sr;
1522 struct sioc_vif_req vr;
1523 struct vif_device *vif;
1524 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001525 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001526 struct mr_table *mrt;
1527
1528 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001529 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001530 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001531
Stephen Hemminger132adf52007-03-08 20:44:43 -08001532 switch (cmd) {
1533 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001534 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001535 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001536 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001537 return -EINVAL;
1538 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001539 vif = &mrt->vif_table[vr.vifi];
1540 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001541 vr.icount = vif->pkt_in;
1542 vr.ocount = vif->pkt_out;
1543 vr.ibytes = vif->bytes_in;
1544 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001546
Jianjun Kongc354e122008-11-03 00:28:02 -08001547 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001549 return 0;
1550 }
1551 read_unlock(&mrt_lock);
1552 return -EADDRNOTAVAIL;
1553 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001554 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001555 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Eric Dumazeta8c94862010-10-01 16:15:08 +00001557 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001558 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001559 if (c) {
1560 sr.pktcnt = c->mfc_un.res.pkt;
1561 sr.bytecnt = c->mfc_un.res.bytes;
1562 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001563 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001564
Jianjun Kongc354e122008-11-03 00:28:02 -08001565 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001566 return -EFAULT;
1567 return 0;
1568 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001569 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001570 return -EADDRNOTAVAIL;
1571 default:
1572 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574}
1575
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001576#ifdef CONFIG_COMPAT
1577struct compat_sioc_sg_req {
1578 struct in_addr src;
1579 struct in_addr grp;
1580 compat_ulong_t pktcnt;
1581 compat_ulong_t bytecnt;
1582 compat_ulong_t wrong_if;
1583};
1584
David S. Millerca6b8bb02011-02-03 17:24:28 -08001585struct compat_sioc_vif_req {
1586 vifi_t vifi; /* Which iface */
1587 compat_ulong_t icount;
1588 compat_ulong_t ocount;
1589 compat_ulong_t ibytes;
1590 compat_ulong_t obytes;
1591};
1592
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001593int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1594{
David S. Miller0033d5a2011-02-03 17:21:31 -08001595 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001596 struct compat_sioc_vif_req vr;
1597 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001598 struct mfc_cache *c;
1599 struct net *net = sock_net(sk);
1600 struct mr_table *mrt;
1601
1602 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001603 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001604 return -ENOENT;
1605
1606 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001607 case SIOCGETVIFCNT:
1608 if (copy_from_user(&vr, arg, sizeof(vr)))
1609 return -EFAULT;
1610 if (vr.vifi >= mrt->maxvif)
1611 return -EINVAL;
1612 read_lock(&mrt_lock);
1613 vif = &mrt->vif_table[vr.vifi];
1614 if (VIF_EXISTS(mrt, vr.vifi)) {
1615 vr.icount = vif->pkt_in;
1616 vr.ocount = vif->pkt_out;
1617 vr.ibytes = vif->bytes_in;
1618 vr.obytes = vif->bytes_out;
1619 read_unlock(&mrt_lock);
1620
1621 if (copy_to_user(arg, &vr, sizeof(vr)))
1622 return -EFAULT;
1623 return 0;
1624 }
1625 read_unlock(&mrt_lock);
1626 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001627 case SIOCGETSGCNT:
1628 if (copy_from_user(&sr, arg, sizeof(sr)))
1629 return -EFAULT;
1630
1631 rcu_read_lock();
1632 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1633 if (c) {
1634 sr.pktcnt = c->mfc_un.res.pkt;
1635 sr.bytecnt = c->mfc_un.res.bytes;
1636 sr.wrong_if = c->mfc_un.res.wrong_if;
1637 rcu_read_unlock();
1638
1639 if (copy_to_user(arg, &sr, sizeof(sr)))
1640 return -EFAULT;
1641 return 0;
1642 }
1643 rcu_read_unlock();
1644 return -EADDRNOTAVAIL;
1645 default:
1646 return -ENOIOCTLCMD;
1647 }
1648}
1649#endif
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1653{
Jiri Pirko351638e2013-05-28 01:30:21 +00001654 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001655 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001656 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 struct vif_device *v;
1658 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (event != NETDEV_UNREGISTER)
1661 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001662
1663 ipmr_for_each_table(mrt, net) {
1664 v = &mrt->vif_table[0];
1665 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1666 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001667 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670 return NOTIFY_DONE;
1671}
1672
1673
Jianjun Kongc354e122008-11-03 00:28:02 -08001674static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 .notifier_call = ipmr_device_event,
1676};
1677
1678/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001679 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * This avoids tunnel drivers and other mess and gives us the speed so
1681 * important for multicast video.
1682 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001683
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001684static void ip_encap(struct net *net, struct sk_buff *skb,
1685 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001687 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001688 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001689
1690 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001691 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001692 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001693 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001695 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001696 iph->tos = old_iph->tos;
1697 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 iph->frag_off = 0;
1699 iph->daddr = daddr;
1700 iph->saddr = saddr;
1701 iph->protocol = IPPROTO_IPIP;
1702 iph->ihl = 5;
1703 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001704 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 ip_send_check(iph);
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1708 nf_reset(skb);
1709}
1710
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001711static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1712 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001714 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
David S. Miller73186df2015-11-03 13:41:45 -05001716 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1717 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 if (unlikely(opt->optlen))
1720 ip_forward_options(skb);
1721
Eric W. Biederman13206b62015-10-07 16:48:35 -05001722 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
1725/*
1726 * Processing handlers for ipmr_forward
1727 */
1728
Patrick McHardy0c122952010-04-13 05:03:22 +00001729static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1730 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001732 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001733 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct net_device *dev;
1735 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001736 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int encap = 0;
1738
Ian Morris51456b22015-04-03 09:17:26 +01001739 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 goto out_free;
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (vif->flags & VIFF_REGISTER) {
1743 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001744 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001745 vif->dev->stats.tx_bytes += skb->len;
1746 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001747 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001748 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001751 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001752 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001753 vif->remote, vif->local,
1754 0, 0,
1755 IPPROTO_IPIP,
1756 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001757 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 goto out_free;
1759 encap = sizeof(struct iphdr);
1760 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001761 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001762 0, 0,
1763 IPPROTO_IPIP,
1764 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001765 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto out_free;
1767 }
1768
Changli Gaod8d1f302010-06-10 23:31:35 -07001769 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Changli Gaod8d1f302010-06-10 23:31:35 -07001771 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001773 * allow to send ICMP, so that packets will disappear
1774 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 */
1776
David S. Miller73186df2015-11-03 13:41:45 -05001777 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 ip_rt_put(rt);
1779 goto out_free;
1780 }
1781
Changli Gaod8d1f302010-06-10 23:31:35 -07001782 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001785 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto out_free;
1787 }
1788
1789 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001790 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Eric Dumazetadf30902009-06-02 05:19:30 +00001792 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001793 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001794 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001797 * What do we do with netfilter? -- RR
1798 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001800 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001802 vif->dev->stats.tx_packets++;
1803 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805
1806 IPCB(skb)->flags |= IPSKB_FORWARDED;
1807
1808 /*
1809 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1810 * not only before forwarding, but after forwarding on all output
1811 * interfaces. It is clear, if mrouter runs a multicasting
1812 * program, it should receive packets not depending to what interface
1813 * program is joined.
1814 * If we will not make it, the program will have to join on all
1815 * interfaces. On the other hand, multihoming host (or router, but
1816 * not mrouter) cannot join to more than one interface - it will
1817 * result in receiving multiple packets.
1818 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001819 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1820 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 ipmr_forward_finish);
1822 return;
1823
1824out_free:
1825 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
Patrick McHardy0c122952010-04-13 05:03:22 +00001828static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001831
1832 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1833 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 break;
1835 }
1836 return ct;
1837}
1838
1839/* "local" means that we should preserve one skb (for local delivery) */
1840
Rami Rosenc4854ec2013-07-20 15:09:28 +03001841static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1842 struct sk_buff *skb, struct mfc_cache *cache,
1843 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
1845 int psend = -1;
1846 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001847 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 vif = cache->mfc_parent;
1850 cache->mfc_un.res.pkt++;
1851 cache->mfc_un.res.bytes += skb->len;
1852
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001853 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001854 struct mfc_cache *cache_proxy;
1855
1856 /* For an (*,G) entry, we only check that the incomming
1857 * interface is part of the static tree.
1858 */
1859 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1860 if (cache_proxy &&
1861 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1862 goto forward;
1863 }
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
1866 * Wrong interface: drop packet and (maybe) send PIM assert.
1867 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001868 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001869 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001871 * Very complicated situation...
1872 *
1873 * The best workaround until routing daemons will be
1874 * fixed is not to redistribute packet, if it was
1875 * send through wrong interface. It means, that
1876 * multicast applications WILL NOT work for
1877 * (S,G), which have default multicast route pointing
1878 * to wrong oif. In any case, it is not a good
1879 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 */
1881 goto dont_forward;
1882 }
1883
1884 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Patrick McHardy0c122952010-04-13 05:03:22 +00001886 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001888 * so that we cannot check that packet arrived on an oif.
1889 * It is bad, but otherwise we would need to move pretty
1890 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001892 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001893 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001894 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1896 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001897 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899 goto dont_forward;
1900 }
1901
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001902forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001903 mrt->vif_table[vif].pkt_in++;
1904 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /*
1907 * Forward the frame
1908 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001909 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1910 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001911 if (true_vifi >= 0 &&
1912 true_vifi != cache->mfc_parent &&
1913 ip_hdr(skb)->ttl >
1914 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1915 /* It's an (*,*) entry and the packet is not coming from
1916 * the upstream: forward the packet to the upstream
1917 * only.
1918 */
1919 psend = cache->mfc_parent;
1920 goto last_forward;
1921 }
1922 goto dont_forward;
1923 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001924 for (ct = cache->mfc_un.res.maxvif - 1;
1925 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001926 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001927 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1928 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001929 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (psend != -1) {
1931 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001934 ipmr_queue_xmit(net, mrt, skb2, cache,
1935 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001937 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001940last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (psend != -1) {
1942 if (local) {
1943 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001946 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001948 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001949 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951 }
1952
1953dont_forward:
1954 if (!local)
1955 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
David S. Miller417da662011-05-03 19:42:43 -07001958static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001959{
David S. Miller417da662011-05-03 19:42:43 -07001960 struct rtable *rt = skb_rtable(skb);
1961 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001962 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001963 .daddr = iph->daddr,
1964 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001965 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001966 .flowi4_oif = (rt_is_output_route(rt) ?
1967 skb->dev->ifindex : 0),
1968 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001969 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001970 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001971 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001972 };
1973 struct mr_table *mrt;
1974 int err;
1975
David S. Millerda919812011-03-12 02:04:50 -05001976 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001977 if (err)
1978 return ERR_PTR(err);
1979 return mrt;
1980}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982/*
1983 * Multicast packets for forwarding arrive here
Eric Dumazet4c9687092010-10-01 16:15:01 +00001984 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 */
1986
1987int ip_mr_input(struct sk_buff *skb)
1988{
1989 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001990 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001991 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001992 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001995 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001997 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 goto dont_forward;
1999
David S. Miller417da662011-05-03 19:42:43 -07002000 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002001 if (IS_ERR(mrt)) {
2002 kfree_skb(skb);
2003 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002006 if (IPCB(skb)->opt.router_alert) {
2007 if (ip_call_ra_chain(skb))
2008 return 0;
2009 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2010 /* IGMPv1 (and broken IGMPv2 implementations sort of
2011 * Cisco IOS <= 11.2(8)) do not put router alert
2012 * option to IGMP packets destined to routable
2013 * groups. It is very bad, because it means
2014 * that we can forward NO IGMP messages.
2015 */
2016 struct sock *mroute_sk;
2017
2018 mroute_sk = rcu_dereference(mrt->mroute_sk);
2019 if (mroute_sk) {
2020 nf_reset(skb);
2021 raw_rcv(mroute_sk, skb);
2022 return 0;
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025 }
2026
Eric Dumazeta8c94862010-10-01 16:15:08 +00002027 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002028 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002029 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002030 int vif = ipmr_find_vif(mrt, skb->dev);
2031
2032 if (vif >= 0)
2033 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2034 vif);
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 /*
2038 * No usable cache entry
2039 */
Ian Morris51456b22015-04-03 09:17:26 +01002040 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 int vif;
2042
2043 if (local) {
2044 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2045 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002046 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 skb = skb2;
2049 }
2050
Eric Dumazeta8c94862010-10-01 16:15:08 +00002051 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002052 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002054 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 read_unlock(&mrt_lock);
2056
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002057 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059 read_unlock(&mrt_lock);
2060 kfree_skb(skb);
2061 return -ENODEV;
2062 }
2063
Eric Dumazeta8c94862010-10-01 16:15:08 +00002064 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002065 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 read_unlock(&mrt_lock);
2067
2068 if (local)
2069 return ip_local_deliver(skb);
2070
2071 return 0;
2072
2073dont_forward:
2074 if (local)
2075 return ip_local_deliver(skb);
2076 kfree_skb(skb);
2077 return 0;
2078}
2079
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002080#ifdef CONFIG_IP_PIMSM_V1
2081/*
2082 * Handle IGMP messages of PIMv1
2083 */
2084
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002085int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002086{
2087 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002088 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002089 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002090
2091 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2092 goto drop;
2093
2094 pim = igmp_hdr(skb);
2095
David S. Miller417da662011-05-03 19:42:43 -07002096 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002097 if (IS_ERR(mrt))
2098 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002099 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002100 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2101 goto drop;
2102
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002103 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002104drop:
2105 kfree_skb(skb);
2106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return 0;
2108}
2109#endif
2110
2111#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002112static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002115 struct net *net = dev_net(skb->dev);
2116 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002118 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 goto drop;
2120
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002121 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002122 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2123 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002124 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002125 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 goto drop;
2127
David S. Miller417da662011-05-03 19:42:43 -07002128 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002129 if (IS_ERR(mrt))
2130 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002131 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002132drop:
2133 kfree_skb(skb);
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return 0;
2136}
2137#endif
2138
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002139static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2140 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 int ct;
2143 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002144 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002145 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Nicolas Dichtel74381892010-03-25 23:45:35 +00002147 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002148 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002149 return -ENOENT;
2150
Thomas Graf92a395e2012-06-26 23:36:13 +00002151 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2152 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2153 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Thomas Graf92a395e2012-06-26 23:36:13 +00002155 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2156 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002159 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002160 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2161 nla_nest_cancel(skb, mp_attr);
2162 return -EMSGSIZE;
2163 }
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 nhp->rtnh_flags = 0;
2166 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002167 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 nhp->rtnh_len = sizeof(*nhp);
2169 }
2170 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002171
2172 nla_nest_end(skb, mp_attr);
2173
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002174 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2175 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2176 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2177 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2178 return -EMSGSIZE;
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 rtm->rtm_type = RTN_MULTICAST;
2181 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
David S. Miller9a1b9492011-05-04 12:18:54 -07002184int ipmr_get_route(struct net *net, struct sk_buff *skb,
2185 __be32 saddr, __be32 daddr,
2186 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002189 struct mr_table *mrt;
2190 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002192 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002193 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002194 return -ENOENT;
2195
Eric Dumazeta8c94862010-10-01 16:15:08 +00002196 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002197 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002198 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002199 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002201 if (vif >= 0)
2202 cache = ipmr_cache_find_any(mrt, daddr, vif);
2203 }
Ian Morris51456b22015-04-03 09:17:26 +01002204 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002205 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002206 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002208 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002211 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return -EAGAIN;
2213 }
2214
2215 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002216 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002217 if (dev)
2218 vif = ipmr_find_vif(mrt, dev);
2219 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002221 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return -ENODEV;
2223 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002224 skb2 = skb_clone(skb, GFP_ATOMIC);
2225 if (!skb2) {
2226 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002227 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002228 return -ENOMEM;
2229 }
2230
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002231 skb_push(skb2, sizeof(struct iphdr));
2232 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002233 iph = ip_hdr(skb2);
2234 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002235 iph->saddr = saddr;
2236 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002237 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002238 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002240 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return err;
2242 }
2243
Eric Dumazeta8c94862010-10-01 16:15:08 +00002244 read_lock(&mrt_lock);
2245 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002247 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002249 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return err;
2251}
2252
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002253static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002254 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2255 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002256{
2257 struct nlmsghdr *nlh;
2258 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002259 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002260
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002261 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002262 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002263 return -EMSGSIZE;
2264
2265 rtm = nlmsg_data(nlh);
2266 rtm->rtm_family = RTNL_FAMILY_IPMR;
2267 rtm->rtm_dst_len = 32;
2268 rtm->rtm_src_len = 32;
2269 rtm->rtm_tos = 0;
2270 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002271 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2272 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002273 rtm->rtm_type = RTN_MULTICAST;
2274 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002275 if (c->mfc_flags & MFC_STATIC)
2276 rtm->rtm_protocol = RTPROT_STATIC;
2277 else
2278 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002279 rtm->rtm_flags = 0;
2280
Jiri Benc930345e2015-03-29 16:59:25 +02002281 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2282 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002283 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002284 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2285 /* do not break the dump if cache is unresolved */
2286 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002287 goto nla_put_failure;
2288
Johannes Berg053c0952015-01-16 22:09:00 +01002289 nlmsg_end(skb, nlh);
2290 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002291
2292nla_put_failure:
2293 nlmsg_cancel(skb, nlh);
2294 return -EMSGSIZE;
2295}
2296
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002297static size_t mroute_msgsize(bool unresolved, int maxvif)
2298{
2299 size_t len =
2300 NLMSG_ALIGN(sizeof(struct rtmsg))
2301 + nla_total_size(4) /* RTA_TABLE */
2302 + nla_total_size(4) /* RTA_SRC */
2303 + nla_total_size(4) /* RTA_DST */
2304 ;
2305
2306 if (!unresolved)
2307 len = len
2308 + nla_total_size(4) /* RTA_IIF */
2309 + nla_total_size(0) /* RTA_MULTIPATH */
2310 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2311 /* RTA_MFC_STATS */
2312 + nla_total_size(sizeof(struct rta_mfc_stats))
2313 ;
2314
2315 return len;
2316}
2317
2318static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2319 int cmd)
2320{
2321 struct net *net = read_pnet(&mrt->net);
2322 struct sk_buff *skb;
2323 int err = -ENOBUFS;
2324
2325 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2326 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002327 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002328 goto errout;
2329
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002330 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002331 if (err < 0)
2332 goto errout;
2333
2334 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2335 return;
2336
2337errout:
2338 kfree_skb(skb);
2339 if (err < 0)
2340 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2341}
2342
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002343static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2344{
2345 struct net *net = sock_net(skb->sk);
2346 struct mr_table *mrt;
2347 struct mfc_cache *mfc;
2348 unsigned int t = 0, s_t;
2349 unsigned int h = 0, s_h;
2350 unsigned int e = 0, s_e;
2351
2352 s_t = cb->args[0];
2353 s_h = cb->args[1];
2354 s_e = cb->args[2];
2355
Eric Dumazeta8c94862010-10-01 16:15:08 +00002356 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002357 ipmr_for_each_table(mrt, net) {
2358 if (t < s_t)
2359 goto next_table;
2360 if (t > s_t)
2361 s_h = 0;
2362 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002363 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002364 if (e < s_e)
2365 goto next_entry;
2366 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002367 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002368 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002369 mfc, RTM_NEWROUTE,
2370 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002371 goto done;
2372next_entry:
2373 e++;
2374 }
2375 e = s_e = 0;
2376 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002377 spin_lock_bh(&mfc_unres_lock);
2378 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2379 if (e < s_e)
2380 goto next_entry2;
2381 if (ipmr_fill_mroute(mrt, skb,
2382 NETLINK_CB(cb->skb).portid,
2383 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002384 mfc, RTM_NEWROUTE,
2385 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002386 spin_unlock_bh(&mfc_unres_lock);
2387 goto done;
2388 }
2389next_entry2:
2390 e++;
2391 }
2392 spin_unlock_bh(&mfc_unres_lock);
2393 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002394 s_h = 0;
2395next_table:
2396 t++;
2397 }
2398done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002399 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002400
2401 cb->args[2] = e;
2402 cb->args[1] = h;
2403 cb->args[0] = t;
2404
2405 return skb->len;
2406}
2407
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002408#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002410 * The /proc interfaces to multicast routing :
2411 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 */
2413struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002414 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002415 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 int ct;
2417};
2418
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002419static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2420 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 loff_t pos)
2422{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002423 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002424
2425 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2426 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002428 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002429 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431 return NULL;
2432}
2433
2434static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002435 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002437 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002438 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002439 struct mr_table *mrt;
2440
2441 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002442 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002443 return ERR_PTR(-ENOENT);
2444
2445 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002448 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 : SEQ_START_TOKEN;
2450}
2451
2452static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2453{
2454 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002455 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002456 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 ++*pos;
2459 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002460 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002461
Patrick McHardy0c122952010-04-13 05:03:22 +00002462 while (++iter->ct < mrt->maxvif) {
2463 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002465 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
2467 return NULL;
2468}
2469
2470static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002471 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
2473 read_unlock(&mrt_lock);
2474}
2475
2476static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2477{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002478 struct ipmr_vif_iter *iter = seq->private;
2479 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002482 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2484 } else {
2485 const struct vif_device *vif = v;
2486 const char *name = vif->dev ? vif->dev->name : "none";
2487
2488 seq_printf(seq,
2489 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002490 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002491 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 vif->bytes_out, vif->pkt_out,
2493 vif->flags, vif->local, vif->remote);
2494 }
2495 return 0;
2496}
2497
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002498static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 .start = ipmr_vif_seq_start,
2500 .next = ipmr_vif_seq_next,
2501 .stop = ipmr_vif_seq_stop,
2502 .show = ipmr_vif_seq_show,
2503};
2504
2505static int ipmr_vif_open(struct inode *inode, struct file *file)
2506{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002507 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2508 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Arjan van de Ven9a321442007-02-12 00:55:35 -08002511static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 .owner = THIS_MODULE,
2513 .open = ipmr_vif_open,
2514 .read = seq_read,
2515 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002516 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517};
2518
2519struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002520 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002521 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002522 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 int ct;
2524};
2525
2526
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002527static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2528 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002530 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct mfc_cache *mfc;
2532
Eric Dumazeta8c94862010-10-01 16:15:08 +00002533 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002534 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002535 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002536 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002537 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002539 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002540 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002543 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002544 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002545 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return mfc;
2547 spin_unlock_bh(&mfc_unres_lock);
2548
2549 it->cache = NULL;
2550 return NULL;
2551}
2552
2553
2554static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2555{
2556 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002557 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002558 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002559
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002560 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002561 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002562 return ERR_PTR(-ENOENT);
2563
2564 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 it->cache = NULL;
2566 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002567 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 : SEQ_START_TOKEN;
2569}
2570
2571static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2572{
2573 struct mfc_cache *mfc = v;
2574 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002575 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002576 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 ++*pos;
2579
2580 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002581 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Patrick McHardy862465f2010-04-13 05:03:21 +00002583 if (mfc->list.next != it->cache)
2584 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002585
Patrick McHardy0c122952010-04-13 05:03:22 +00002586 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 goto end_of_list;
2588
Patrick McHardy0c122952010-04-13 05:03:22 +00002589 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002592 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002593 if (list_empty(it->cache))
2594 continue;
2595 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
2597
2598 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002599 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002600 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002604 if (!list_empty(it->cache))
2605 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002607end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 spin_unlock_bh(&mfc_unres_lock);
2609 it->cache = NULL;
2610
2611 return NULL;
2612}
2613
2614static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2615{
2616 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002617 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Patrick McHardy0c122952010-04-13 05:03:22 +00002619 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002621 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002622 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
2625static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2626{
2627 int n;
2628
2629 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002630 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2632 } else {
2633 const struct mfc_cache *mfc = v;
2634 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002635 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002636
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002637 seq_printf(seq, "%08X %08X %-3hd",
2638 (__force u32) mfc->mfc_mcastgrp,
2639 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002640 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Patrick McHardy0c122952010-04-13 05:03:22 +00002642 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002643 seq_printf(seq, " %8lu %8lu %8lu",
2644 mfc->mfc_un.res.pkt,
2645 mfc->mfc_un.res.bytes,
2646 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002647 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002648 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002649 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002650 mfc->mfc_un.res.ttls[n] < 255)
2651 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002652 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 n, mfc->mfc_un.res.ttls[n]);
2654 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002655 } else {
2656 /* unresolved mfc_caches don't contain
2657 * pkt, bytes and wrong_if values
2658 */
2659 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
2661 seq_putc(seq, '\n');
2662 }
2663 return 0;
2664}
2665
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002666static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 .start = ipmr_mfc_seq_start,
2668 .next = ipmr_mfc_seq_next,
2669 .stop = ipmr_mfc_seq_stop,
2670 .show = ipmr_mfc_seq_show,
2671};
2672
2673static int ipmr_mfc_open(struct inode *inode, struct file *file)
2674{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002675 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2676 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Arjan van de Ven9a321442007-02-12 00:55:35 -08002679static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 .owner = THIS_MODULE,
2681 .open = ipmr_mfc_open,
2682 .read = seq_read,
2683 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002684 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002686#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002689static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002691 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692};
2693#endif
2694
2695
2696/*
2697 * Setup for IP multicast routing
2698 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002699static int __net_init ipmr_net_init(struct net *net)
2700{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002701 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002702
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002703 err = ipmr_rules_init(net);
2704 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002705 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002706
2707#ifdef CONFIG_PROC_FS
2708 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002709 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002710 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002711 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002712 goto proc_cache_fail;
2713#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002714 return 0;
2715
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002716#ifdef CONFIG_PROC_FS
2717proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002718 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002719proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002720 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002721#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002722fail:
2723 return err;
2724}
2725
2726static void __net_exit ipmr_net_exit(struct net *net)
2727{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002728#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002729 remove_proc_entry("ip_mr_cache", net->proc_net);
2730 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002731#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002732 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002733}
2734
2735static struct pernet_operations ipmr_net_ops = {
2736 .init = ipmr_net_init,
2737 .exit = ipmr_net_exit,
2738};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002739
Wang Chen03d2f892008-07-03 12:13:36 +08002740int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Wang Chen03d2f892008-07-03 12:13:36 +08002742 int err;
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2745 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002746 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002747 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002748 if (!mrt_cachep)
2749 return -ENOMEM;
2750
Benjamin Therycf958ae32009-01-22 04:56:16 +00002751 err = register_pernet_subsys(&ipmr_net_ops);
2752 if (err)
2753 goto reg_pernet_fail;
2754
Wang Chen03d2f892008-07-03 12:13:36 +08002755 err = register_netdevice_notifier(&ip_mr_notifier);
2756 if (err)
2757 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002758#ifdef CONFIG_IP_PIMSM_V2
2759 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002760 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002761 err = -EAGAIN;
2762 goto add_proto_fail;
2763 }
2764#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002765 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2766 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002767 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002768
Tom Goff403dbb92009-06-14 03:16:13 -07002769#ifdef CONFIG_IP_PIMSM_V2
2770add_proto_fail:
2771 unregister_netdevice_notifier(&ip_mr_notifier);
2772#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002773reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002774 unregister_pernet_subsys(&ipmr_net_ops);
2775reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002776 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}