blob: 3a2c0162c3badeed716599e538ed06426ddc7199 [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
70#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71#define CONFIG_IP_PIMSM 1
72#endif
73
Patrick McHardy0c122952010-04-13 05:03:22 +000074struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000075 struct list_head list;
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -050076 possible_net_t net;
Patrick McHardyf0ad0862010-04-13 05:03:23 +000077 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000078 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000079 struct timer_list ipmr_expire_timer;
80 struct list_head mfc_unres_queue;
81 struct list_head mfc_cache_array[MFC_LINES];
82 struct vif_device vif_table[MAXVIFS];
83 int maxvif;
84 atomic_t cache_resolve_queue_len;
Joe Perches53d68412012-11-25 09:35:30 +000085 bool mroute_do_assert;
86 bool mroute_do_pim;
Patrick McHardy0c122952010-04-13 05:03:22 +000087#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
88 int mroute_reg_vif_num;
89#endif
90};
91
Patrick McHardyf0ad0862010-04-13 05:03:23 +000092struct ipmr_rule {
93 struct fib_rule common;
94};
95
96struct ipmr_result {
97 struct mr_table *mrt;
98};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000101 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
103
104static DEFINE_RWLOCK(mrt_lock);
105
106/*
107 * Multicast router control variables
108 */
109
Patrick McHardy0c122952010-04-13 05:03:22 +0000110#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* Special spinlock for queue of unresolved entries */
113static DEFINE_SPINLOCK(mfc_unres_lock);
114
115/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000116 * entries is changed only in process context and protected
117 * with weak lock mrt_lock. Queue of unresolved entries is protected
118 * with strong spinlock mfc_unres_lock.
119 *
120 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122
Christoph Lametere18b8902006-12-06 20:33:20 -0800123static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000125static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000126static void ipmr_free_table(struct mr_table *mrt);
127
Rami Rosenc4854ec2013-07-20 15:09:28 +0300128static void ip_mr_forward(struct net *net, struct mr_table *mrt,
129 struct sk_buff *skb, struct mfc_cache *cache,
130 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000131static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000132 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200133static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
134 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000135static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
136 int cmd);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000137static void mroute_clean_tables(struct mr_table *mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000138static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000140#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
141#define ipmr_for_each_table(mrt, net) \
142 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
143
144static struct mr_table *ipmr_get_table(struct net *net, u32 id)
145{
146 struct mr_table *mrt;
147
148 ipmr_for_each_table(mrt, net) {
149 if (mrt->id == id)
150 return mrt;
151 }
152 return NULL;
153}
154
David S. Millerda919812011-03-12 02:04:50 -0500155static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000156 struct mr_table **mrt)
157{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000158 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100159 struct ipmr_result res;
160 struct fib_lookup_arg arg = {
161 .result = &res,
162 .flags = FIB_LOOKUP_NOREF,
163 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000164
David S. Millerda919812011-03-12 02:04:50 -0500165 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
166 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000167 if (err < 0)
168 return err;
169 *mrt = res.mrt;
170 return 0;
171}
172
173static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
174 int flags, struct fib_lookup_arg *arg)
175{
176 struct ipmr_result *res = arg->result;
177 struct mr_table *mrt;
178
179 switch (rule->action) {
180 case FR_ACT_TO_TBL:
181 break;
182 case FR_ACT_UNREACHABLE:
183 return -ENETUNREACH;
184 case FR_ACT_PROHIBIT:
185 return -EACCES;
186 case FR_ACT_BLACKHOLE:
187 default:
188 return -EINVAL;
189 }
190
191 mrt = ipmr_get_table(rule->fr_net, rule->table);
Ian Morris51456b22015-04-03 09:17:26 +0100192 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000193 return -EAGAIN;
194 res->mrt = mrt;
195 return 0;
196}
197
198static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
199{
200 return 1;
201}
202
203static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
204 FRA_GENERIC_POLICY,
205};
206
207static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
208 struct fib_rule_hdr *frh, struct nlattr **tb)
209{
210 return 0;
211}
212
213static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
214 struct nlattr **tb)
215{
216 return 1;
217}
218
219static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
220 struct fib_rule_hdr *frh)
221{
222 frh->dst_len = 0;
223 frh->src_len = 0;
224 frh->tos = 0;
225 return 0;
226}
227
Andi Kleen04a6f822012-10-04 17:12:11 -0700228static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200229 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000230 .rule_size = sizeof(struct ipmr_rule),
231 .addr_size = sizeof(u32),
232 .action = ipmr_rule_action,
233 .match = ipmr_rule_match,
234 .configure = ipmr_rule_configure,
235 .compare = ipmr_rule_compare,
236 .default_pref = fib_default_rule_pref,
237 .fill = ipmr_rule_fill,
238 .nlgroup = RTNLGRP_IPV4_RULE,
239 .policy = ipmr_rule_policy,
240 .owner = THIS_MODULE,
241};
242
243static int __net_init ipmr_rules_init(struct net *net)
244{
245 struct fib_rules_ops *ops;
246 struct mr_table *mrt;
247 int err;
248
249 ops = fib_rules_register(&ipmr_rules_ops_template, net);
250 if (IS_ERR(ops))
251 return PTR_ERR(ops);
252
253 INIT_LIST_HEAD(&net->ipv4.mr_tables);
254
255 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +0100256 if (!mrt) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000257 err = -ENOMEM;
258 goto err1;
259 }
260
261 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
262 if (err < 0)
263 goto err2;
264
265 net->ipv4.mr_rules_ops = ops;
266 return 0;
267
268err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700269 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000270err1:
271 fib_rules_unregister(ops);
272 return err;
273}
274
275static void __net_exit ipmr_rules_exit(struct net *net)
276{
277 struct mr_table *mrt, *next;
278
WANG Conged785302015-03-31 11:01:45 -0700279 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000280 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
281 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000282 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000283 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000284 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700285 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000286}
287#else
288#define ipmr_for_each_table(mrt, net) \
289 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
290
291static struct mr_table *ipmr_get_table(struct net *net, u32 id)
292{
293 return net->ipv4.mrt;
294}
295
David S. Millerda919812011-03-12 02:04:50 -0500296static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000297 struct mr_table **mrt)
298{
299 *mrt = net->ipv4.mrt;
300 return 0;
301}
302
303static int __net_init ipmr_rules_init(struct net *net)
304{
305 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
306 return net->ipv4.mrt ? 0 : -ENOMEM;
307}
308
309static void __net_exit ipmr_rules_exit(struct net *net)
310{
WANG Conged785302015-03-31 11:01:45 -0700311 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000312 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700313 net->ipv4.mrt = NULL;
314 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000315}
316#endif
317
318static struct mr_table *ipmr_new_table(struct net *net, u32 id)
319{
320 struct mr_table *mrt;
321 unsigned int i;
322
323 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100324 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000325 return mrt;
326
327 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100328 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000329 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200330 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000331 mrt->id = id;
332
333 /* Forwarding cache */
334 for (i = 0; i < MFC_LINES; i++)
335 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
336
337 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
338
339 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
340 (unsigned long)mrt);
341
342#ifdef CONFIG_IP_PIMSM
343 mrt->mroute_reg_vif_num = -1;
344#endif
345#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
346 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
347#endif
348 return mrt;
349}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000351static void ipmr_free_table(struct mr_table *mrt)
352{
353 del_timer_sync(&mrt->ipmr_expire_timer);
354 mroute_clean_tables(mrt);
355 kfree(mrt);
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
359
Wang Chend6070322008-07-14 20:55:26 -0700360static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
361{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000362 struct net *net = dev_net(dev);
363
Wang Chend6070322008-07-14 20:55:26 -0700364 dev_close(dev);
365
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000366 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700367 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800368 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700369 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700370 struct ip_tunnel_parm p;
371
372 memset(&p, 0, sizeof(p));
373 p.iph.daddr = v->vifc_rmt_addr.s_addr;
374 p.iph.saddr = v->vifc_lcl_addr.s_addr;
375 p.iph.version = 4;
376 p.iph.ihl = 5;
377 p.iph.protocol = IPPROTO_IPIP;
378 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
379 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
380
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800381 if (ops->ndo_do_ioctl) {
382 mm_segment_t oldfs = get_fs();
383
384 set_fs(KERNEL_DS);
385 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
386 set_fs(oldfs);
387 }
Wang Chend6070322008-07-14 20:55:26 -0700388 }
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000392struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 struct net_device *dev;
395
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000396 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800399 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int err;
401 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct ip_tunnel_parm p;
403 struct in_device *in_dev;
404
405 memset(&p, 0, sizeof(p));
406 p.iph.daddr = v->vifc_rmt_addr.s_addr;
407 p.iph.saddr = v->vifc_lcl_addr.s_addr;
408 p.iph.version = 4;
409 p.iph.ihl = 5;
410 p.iph.protocol = IPPROTO_IPIP;
411 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800412 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800414 if (ops->ndo_do_ioctl) {
415 mm_segment_t oldfs = get_fs();
416
417 set_fs(KERNEL_DS);
418 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
419 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000420 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800421 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dev = NULL;
424
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000425 if (err == 0 &&
426 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 dev->flags |= IFF_MULTICAST;
428
Herbert Xue5ed6392005-10-03 14:35:55 -0700429 in_dev = __in_dev_get_rtnl(dev);
Ian Morris51456b22015-04-03 09:17:26 +0100430 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700432
433 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100434 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700435 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (dev_open(dev))
438 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700439 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 }
442 return dev;
443
444failure:
445 /* allow the register to be completed before unregistering. */
446 rtnl_unlock();
447 rtnl_lock();
448
449 unregister_netdevice(dev);
450 return NULL;
451}
452
453#ifdef CONFIG_IP_PIMSM
454
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000455static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000457 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000458 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500459 struct flowi4 fl4 = {
460 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700461 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500462 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000463 };
464 int err;
465
David S. Millerda919812011-03-12 02:04:50 -0500466 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000467 if (err < 0) {
468 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000469 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000470 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700473 dev->stats.tx_bytes += skb->len;
474 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000475 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 read_unlock(&mrt_lock);
477 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000478 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200481static int reg_vif_get_iflink(const struct net_device *dev)
482{
483 return 0;
484}
485
Stephen Hemminger007c3832008-11-20 20:28:35 -0800486static const struct net_device_ops reg_vif_netdev_ops = {
487 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200488 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800489};
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static void reg_vif_setup(struct net_device *dev)
492{
493 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800494 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530496 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700498 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000501static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct net_device *dev;
504 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000505 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000507 if (mrt->id == RT_TABLE_DEFAULT)
508 sprintf(name, "pimreg");
509 else
510 sprintf(name, "pimreg%u", mrt->id);
511
Tom Gundersenc835a672014-07-14 16:37:24 +0200512 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Ian Morris51456b22015-04-03 09:17:26 +0100514 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return NULL;
516
Tom Goff403dbb92009-06-14 03:16:13 -0700517 dev_net_set(dev, net);
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (register_netdevice(dev)) {
520 free_netdev(dev);
521 return NULL;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Herbert Xu71e27da2007-06-04 23:36:06 -0700524 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000525 in_dev = __in_dev_get_rcu(dev);
526 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700527 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Herbert Xu71e27da2007-06-04 23:36:06 -0700531 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100532 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700533 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
534 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 if (dev_open(dev))
537 goto failure;
538
Wang Chen7dc00c82008-07-14 20:56:34 -0700539 dev_hold(dev);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return dev;
542
543failure:
544 /* allow the register to be completed before unregistering. */
545 rtnl_unlock();
546 rtnl_lock();
547
548 unregister_netdevice(dev);
549 return NULL;
550}
551#endif
552
Ben Hutchings2c530402012-07-10 10:55:09 +0000553/**
554 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700555 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900557
Patrick McHardy0c122952010-04-13 05:03:22 +0000558static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000559 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct vif_device *v;
562 struct net_device *dev;
563 struct in_device *in_dev;
564
Patrick McHardy0c122952010-04-13 05:03:22 +0000565 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -EADDRNOTAVAIL;
567
Patrick McHardy0c122952010-04-13 05:03:22 +0000568 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 write_lock_bh(&mrt_lock);
571 dev = v->dev;
572 v->dev = NULL;
573
574 if (!dev) {
575 write_unlock_bh(&mrt_lock);
576 return -EADDRNOTAVAIL;
577 }
578
579#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000580 if (vifi == mrt->mroute_reg_vif_num)
581 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582#endif
583
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000584 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000586
587 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000588 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 break;
590 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000591 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 write_unlock_bh(&mrt_lock);
595
596 dev_set_allmulti(dev, -1);
597
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000598 in_dev = __in_dev_get_rtnl(dev);
599 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700600 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000601 inet_netconf_notify_devconf(dev_net(dev),
602 NETCONFA_MC_FORWARDING,
603 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ip_rt_multicast_event(in_dev);
605 }
606
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000607 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000608 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 dev_put(dev);
611 return 0;
612}
613
Eric Dumazeta8c94862010-10-01 16:15:08 +0000614static void ipmr_cache_free_rcu(struct rcu_head *head)
615{
616 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
617
618 kmem_cache_free(mrt_cachep, c);
619}
620
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000621static inline void ipmr_cache_free(struct mfc_cache *c)
622{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000623 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000627 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
629
Patrick McHardy0c122952010-04-13 05:03:22 +0000630static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200632 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700634 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Patrick McHardy0c122952010-04-13 05:03:22 +0000636 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Jianjun Kongc354e122008-11-03 00:28:02 -0800638 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700639 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
641 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000642 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000644 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700645 e->error = -ETIMEDOUT;
646 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700647
Eric W. Biederman15e47302012-09-07 20:12:54 +0000648 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000649 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000654 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657
Patrick McHardye258beb2010-04-13 05:03:19 +0000658/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Patrick McHardye258beb2010-04-13 05:03:19 +0000660static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Patrick McHardy0c122952010-04-13 05:03:22 +0000662 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 unsigned long now;
664 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000665 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000668 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return;
670 }
671
Patrick McHardy0c122952010-04-13 05:03:22 +0000672 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto out;
674
675 now = jiffies;
676 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Patrick McHardy0c122952010-04-13 05:03:22 +0000678 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (time_after(c->mfc_un.unres.expires, now)) {
680 unsigned long interval = c->mfc_un.unres.expires - now;
681 if (interval < expires)
682 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 continue;
684 }
685
Patrick McHardy862465f2010-04-13 05:03:21 +0000686 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000687 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000688 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
Patrick McHardy0c122952010-04-13 05:03:22 +0000691 if (!list_empty(&mrt->mfc_unres_queue))
692 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694out:
695 spin_unlock(&mfc_unres_lock);
696}
697
698/* Fill oifs list. It is called under write locked mrt_lock. */
699
Patrick McHardy0c122952010-04-13 05:03:22 +0000700static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000701 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 int vifi;
704
705 cache->mfc_un.res.minvif = MAXVIFS;
706 cache->mfc_un.res.maxvif = 0;
707 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
708
Patrick McHardy0c122952010-04-13 05:03:22 +0000709 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
710 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000711 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
713 if (cache->mfc_un.res.minvif > vifi)
714 cache->mfc_un.res.minvif = vifi;
715 if (cache->mfc_un.res.maxvif <= vifi)
716 cache->mfc_un.res.maxvif = vifi + 1;
717 }
718 }
719}
720
Patrick McHardy0c122952010-04-13 05:03:22 +0000721static int vif_add(struct net *net, struct mr_table *mrt,
722 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000725 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct net_device *dev;
727 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700728 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000731 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return -EADDRINUSE;
733
734 switch (vifc->vifc_flags) {
735#ifdef CONFIG_IP_PIMSM
736 case VIFF_REGISTER:
737 /*
738 * Special Purpose VIF in PIM
739 * All the packets will be sent to the daemon
740 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000741 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000743 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!dev)
745 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700746 err = dev_set_allmulti(dev, 1);
747 if (err) {
748 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700749 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700750 return err;
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
753#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900754 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000755 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (!dev)
757 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700758 err = dev_set_allmulti(dev, 1);
759 if (err) {
760 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700761 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700762 return err;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000765
766 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000768 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
769 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100770 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000771 dev_put(dev);
772 return -EADDRNOTAVAIL;
773 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000774 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000775 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!dev)
778 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700779 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700780 if (err) {
781 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700782 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
785 default:
786 return -EINVAL;
787 }
788
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000789 in_dev = __in_dev_get_rtnl(dev);
790 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000791 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000793 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700794 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000795 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
796 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 ip_rt_multicast_event(in_dev);
798
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000799 /* Fill in the VIF structures */
800
Jianjun Kongc354e122008-11-03 00:28:02 -0800801 v->rate_limit = vifc->vifc_rate_limit;
802 v->local = vifc->vifc_lcl_addr.s_addr;
803 v->remote = vifc->vifc_rmt_addr.s_addr;
804 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!mrtsock)
806 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800807 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 v->bytes_in = 0;
809 v->bytes_out = 0;
810 v->pkt_in = 0;
811 v->pkt_out = 0;
812 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000813 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200814 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 /* And finish update writing critical data */
817 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800818 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000820 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000821 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000823 if (vifi+1 > mrt->maxvif)
824 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 write_unlock_bh(&mrt_lock);
826 return 0;
827}
828
Eric Dumazeta8c94862010-10-01 16:15:08 +0000829/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000830static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000831 __be32 origin,
832 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Jianjun Kongc354e122008-11-03 00:28:02 -0800834 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct mfc_cache *c;
836
Eric Dumazeta8c94862010-10-01 16:15:08 +0000837 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000838 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
839 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000841 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000844/* Look for a (*,*,oif) entry */
845static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
846 int vifi)
847{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100848 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000849 struct mfc_cache *c;
850
851 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100852 if (c->mfc_origin == htonl(INADDR_ANY) &&
853 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000854 c->mfc_un.res.ttls[vifi] < 255)
855 return c;
856
857 return NULL;
858}
859
860/* Look for a (*,G) entry */
861static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
862 __be32 mcastgrp, int vifi)
863{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100864 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000865 struct mfc_cache *c, *proxy;
866
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100867 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000868 goto skip;
869
870 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100871 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000872 c->mfc_mcastgrp == mcastgrp) {
873 if (c->mfc_un.res.ttls[vifi] < 255)
874 return c;
875
876 /* It's ok if the vifi is part of the static tree */
877 proxy = ipmr_cache_find_any_parent(mrt,
878 c->mfc_parent);
879 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
880 return c;
881 }
882
883skip:
884 return ipmr_cache_find_any_parent(mrt, vifi);
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/*
888 * Allocate a multicast cache entry
889 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000890static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Jianjun Kongc354e122008-11-03 00:28:02 -0800892 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000893
894 if (c)
895 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return c;
897}
898
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000899static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Jianjun Kongc354e122008-11-03 00:28:02 -0800901 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000902
903 if (c) {
904 skb_queue_head_init(&c->mfc_un.unres.unresolved);
905 c->mfc_un.unres.expires = jiffies + 10*HZ;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return c;
908}
909
910/*
911 * A cache entry has gone into a resolved state from queued
912 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900913
Patrick McHardy0c122952010-04-13 05:03:22 +0000914static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
915 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700918 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000920 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Jianjun Kongc354e122008-11-03 00:28:02 -0800922 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700923 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
925
Hong zhi guo573ce262013-03-27 06:47:04 +0000926 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000927 nlh->nlmsg_len = skb_tail_pointer(skb) -
928 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 } else {
930 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000931 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000933 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700934 e->error = -EMSGSIZE;
935 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Thomas Graf2942e902006-08-15 00:30:25 -0700937
Eric W. Biederman15e47302012-09-07 20:12:54 +0000938 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000939 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000940 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943}
944
945/*
946 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
947 * expects the following bizarre scheme.
948 *
949 * Called under mrt_lock.
950 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900951
Patrick McHardy0c122952010-04-13 05:03:22 +0000952static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000953 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300956 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 struct igmphdr *igmp;
958 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000959 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int ret;
961
962#ifdef CONFIG_IP_PIMSM
963 if (assert == IGMPMSG_WHOLEPKT)
964 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
965 else
966#endif
967 skb = alloc_skb(128, GFP_ATOMIC);
968
Stephen Hemminger132adf52007-03-08 20:44:43 -0800969 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return -ENOBUFS;
971
972#ifdef CONFIG_IP_PIMSM
973 if (assert == IGMPMSG_WHOLEPKT) {
974 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000975 * Duplicate old header, fix ihl, length etc.
976 * And all this only to mangle msg->im_msgtype and
977 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300979 skb_push(skb, sizeof(struct iphdr));
980 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300981 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300982 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700983 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 msg->im_msgtype = IGMPMSG_WHOLEPKT;
985 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000986 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700987 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
988 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
989 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900990 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900992 {
993
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000994 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Cong Wang30f3a402013-06-05 20:14:10 +0800996 skb_set_network_header(skb, skb->len);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300997 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300998 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000999 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001000 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +00001002 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001004 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001006 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 igmp->type =
1008 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001009 igmp->code = 0;
1010 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001011 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Eric Dumazet4c968702010-10-01 16:15:01 +00001014 rcu_read_lock();
1015 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001016 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001017 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 kfree_skb(skb);
1019 return -EINVAL;
1020 }
1021
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001022 /* Deliver to mrouted */
1023
Eric Dumazet4c968702010-10-01 16:15:01 +00001024 ret = sock_queue_rcv_skb(mroute_sk, skb);
1025 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001026 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001027 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 kfree_skb(skb);
1029 }
1030
1031 return ret;
1032}
1033
1034/*
1035 * Queue a packet for resolution. It gets locked cache entry!
1036 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001039ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Patrick McHardy862465f2010-04-13 05:03:21 +00001041 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 int err;
1043 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001044 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001047 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001048 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001049 c->mfc_origin == iph->saddr) {
1050 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Patrick McHardy862465f2010-04-13 05:03:21 +00001055 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001056 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Patrick McHardy0c122952010-04-13 05:03:22 +00001058 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001059 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 spin_unlock_bh(&mfc_unres_lock);
1061
1062 kfree_skb(skb);
1063 return -ENOBUFS;
1064 }
1065
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001066 /* Fill in the new cache entry */
1067
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001068 c->mfc_parent = -1;
1069 c->mfc_origin = iph->saddr;
1070 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001072 /* Reflect first query at mrouted. */
1073
Patrick McHardy0c122952010-04-13 05:03:22 +00001074 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001075 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001076 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 out - Brad Parker
1078 */
1079 spin_unlock_bh(&mfc_unres_lock);
1080
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001081 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 kfree_skb(skb);
1083 return err;
1084 }
1085
Patrick McHardy0c122952010-04-13 05:03:22 +00001086 atomic_inc(&mrt->cache_resolve_queue_len);
1087 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001088 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
David S. Miller278554b2010-05-12 00:05:35 -07001090 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1091 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001094 /* See if we can append the packet */
1095
1096 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 kfree_skb(skb);
1098 err = -ENOBUFS;
1099 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001100 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 err = 0;
1102 }
1103
1104 spin_unlock_bh(&mfc_unres_lock);
1105 return err;
1106}
1107
1108/*
1109 * MFC cache manipulation by user space mroute daemon
1110 */
1111
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001112static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001115 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Jianjun Kongc354e122008-11-03 00:28:02 -08001117 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Patrick McHardy0c122952010-04-13 05:03:22 +00001119 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001121 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1122 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001123 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001124 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001125 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return 0;
1127 }
1128 }
1129 return -ENOENT;
1130}
1131
Patrick McHardy0c122952010-04-13 05:03:22 +00001132static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001133 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Patrick McHardy862465f2010-04-13 05:03:21 +00001135 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001137 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Patrick McHardya50436f22010-03-17 06:04:14 +00001139 if (mfc->mfcc_parent >= MAXVIFS)
1140 return -ENFILE;
1141
Jianjun Kongc354e122008-11-03 00:28:02 -08001142 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Patrick McHardy0c122952010-04-13 05:03:22 +00001144 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001146 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1147 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001148 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
Patrick McHardy862465f2010-04-13 05:03:21 +00001153 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 write_lock_bh(&mrt_lock);
1155 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001156 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!mrtsock)
1158 c->mfc_flags |= MFC_STATIC;
1159 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001160 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162 }
1163
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001164 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001165 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return -EINVAL;
1167
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001168 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001169 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return -ENOMEM;
1171
Jianjun Kongc354e122008-11-03 00:28:02 -08001172 c->mfc_origin = mfc->mfcc_origin.s_addr;
1173 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1174 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001175 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (!mrtsock)
1177 c->mfc_flags |= MFC_STATIC;
1178
Eric Dumazeta8c94862010-10-01 16:15:08 +00001179 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /*
1182 * Check to see if we resolved a queued list. If so we
1183 * need to send on the frames and tidy up.
1184 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001185 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001187 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001188 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001190 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001191 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001192 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 break;
1194 }
1195 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001196 if (list_empty(&mrt->mfc_unres_queue))
1197 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 spin_unlock_bh(&mfc_unres_lock);
1199
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001200 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001201 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001202 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001204 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
1206}
1207
1208/*
1209 * Close the multicast socket, and clear the vif tables etc
1210 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001211
Patrick McHardy0c122952010-04-13 05:03:22 +00001212static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001215 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001216 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001217
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001218 /* Shut down all active vif entries */
1219
Patrick McHardy0c122952010-04-13 05:03:22 +00001220 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001221 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001222 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001224 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001226 /* Wipe the cache */
1227
Patrick McHardy862465f2010-04-13 05:03:21 +00001228 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001229 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001230 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001232 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001233 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001234 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236 }
1237
Patrick McHardy0c122952010-04-13 05:03:22 +00001238 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001240 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001241 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001242 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001243 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245 spin_unlock_bh(&mfc_unres_lock);
1246 }
1247}
1248
Eric Dumazet4c968702010-10-01 16:15:01 +00001249/* called from ip_ra_control(), before an RCU grace period,
1250 * we dont need to call synchronize_rcu() here
1251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static void mrtsock_destruct(struct sock *sk)
1253{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001254 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001255 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001258 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001259 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001260 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001261 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1262 NETCONFA_IFINDEX_ALL,
1263 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001264 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001265 mroute_clean_tables(mrt);
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 rtnl_unlock();
1269}
1270
1271/*
1272 * Socket options and virtual interface manipulation. The whole
1273 * virtual interface system is a complete heap, but unfortunately
1274 * that's how BSD mrouted happens to think. Maybe one day with a proper
1275 * MOSPF/PIM router set up we can clean this up.
1276 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001277
David S. Millerb7058842009-09-30 16:12:20 -07001278int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001280 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 struct vifctl vif;
1282 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001283 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001284 struct mr_table *mrt;
1285
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001286 if (sk->sk_type != SOCK_RAW ||
1287 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1288 return -EOPNOTSUPP;
1289
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001290 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001291 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001292 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001293
Stephen Hemminger132adf52007-03-08 20:44:43 -08001294 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001295 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001296 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EACCES;
1298 }
1299
Stephen Hemminger132adf52007-03-08 20:44:43 -08001300 switch (optname) {
1301 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001302 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001306 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001308 return -EADDRINUSE;
1309 }
1310
1311 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1312 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001313 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001314 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001315 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1316 NETCONFA_IFINDEX_ALL,
1317 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 }
1319 rtnl_unlock();
1320 return ret;
1321 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001322 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001323 return -EACCES;
1324 return ip_ra_control(sk, 0, NULL);
1325 case MRT_ADD_VIF:
1326 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001327 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001328 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001329 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001330 return -EFAULT;
1331 if (vif.vifc_vifi >= MAXVIFS)
1332 return -ENFILE;
1333 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001334 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001335 ret = vif_add(net, mrt, &vif,
1336 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001337 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001338 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 }
1340 rtnl_unlock();
1341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /*
1344 * Manipulate the forwarding caches. These live
1345 * in a sort of kernel/user symbiosis.
1346 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001347 case MRT_ADD_MFC:
1348 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001349 parent = -1;
1350 case MRT_ADD_MFC_PROXY:
1351 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001352 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001353 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001354 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001355 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001356 if (parent == 0)
1357 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001358 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001359 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1360 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001362 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001363 sk == rtnl_dereference(mrt->mroute_sk),
1364 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001365 rtnl_unlock();
1366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /*
1368 * Control PIM assert.
1369 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001370 case MRT_ASSERT:
1371 {
1372 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001373 if (optlen != sizeof(v))
1374 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001375 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001376 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001377 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001378 return 0;
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001381 case MRT_PIM:
1382 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001383 int v;
1384
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001385 if (optlen != sizeof(v))
1386 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001387 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001389 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001390
Stephen Hemminger132adf52007-03-08 20:44:43 -08001391 rtnl_lock();
1392 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001393 if (v != mrt->mroute_do_pim) {
1394 mrt->mroute_do_pim = v;
1395 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001397 rtnl_unlock();
1398 return ret;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001401#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1402 case MRT_TABLE:
1403 {
1404 u32 v;
1405
1406 if (optlen != sizeof(u32))
1407 return -EINVAL;
1408 if (get_user(v, (u32 __user *)optval))
1409 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001410
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001411 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1412 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1413 return -EINVAL;
1414
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001415 rtnl_lock();
1416 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001417 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1418 ret = -EBUSY;
1419 } else {
1420 if (!ipmr_new_table(net, v))
1421 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001422 else
1423 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001424 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001425 rtnl_unlock();
1426 return ret;
1427 }
1428#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001429 /*
1430 * Spurious command, or MRT_VERSION which you cannot
1431 * set.
1432 */
1433 default:
1434 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436}
1437
1438/*
1439 * Getsock opt support for the multicast routing system.
1440 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001441
Jianjun Kongc354e122008-11-03 00:28:02 -08001442int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 int olr;
1445 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001446 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001447 struct mr_table *mrt;
1448
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001449 if (sk->sk_type != SOCK_RAW ||
1450 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1451 return -EOPNOTSUPP;
1452
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001453 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001454 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001455 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Jianjun Kongc354e122008-11-03 00:28:02 -08001457 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001459 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001461 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -ENOPROTOOPT;
1463
1464 if (get_user(olr, optlen))
1465 return -EFAULT;
1466
1467 olr = min_t(unsigned int, olr, sizeof(int));
1468 if (olr < 0)
1469 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001470
Jianjun Kongc354e122008-11-03 00:28:02 -08001471 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001473 if (optname == MRT_VERSION)
1474 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001476 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001477 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478#endif
1479 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001480 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001481 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EFAULT;
1483 return 0;
1484}
1485
1486/*
1487 * The IP multicast ioctl support routines.
1488 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1491{
1492 struct sioc_sg_req sr;
1493 struct sioc_vif_req vr;
1494 struct vif_device *vif;
1495 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001496 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001497 struct mr_table *mrt;
1498
1499 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001500 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001501 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001502
Stephen Hemminger132adf52007-03-08 20:44:43 -08001503 switch (cmd) {
1504 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001505 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001506 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001507 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001508 return -EINVAL;
1509 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001510 vif = &mrt->vif_table[vr.vifi];
1511 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001512 vr.icount = vif->pkt_in;
1513 vr.ocount = vif->pkt_out;
1514 vr.ibytes = vif->bytes_in;
1515 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001517
Jianjun Kongc354e122008-11-03 00:28:02 -08001518 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520 return 0;
1521 }
1522 read_unlock(&mrt_lock);
1523 return -EADDRNOTAVAIL;
1524 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001525 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001526 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Eric Dumazeta8c94862010-10-01 16:15:08 +00001528 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001529 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001530 if (c) {
1531 sr.pktcnt = c->mfc_un.res.pkt;
1532 sr.bytecnt = c->mfc_un.res.bytes;
1533 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001534 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001535
Jianjun Kongc354e122008-11-03 00:28:02 -08001536 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001537 return -EFAULT;
1538 return 0;
1539 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001540 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001541 return -EADDRNOTAVAIL;
1542 default:
1543 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545}
1546
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001547#ifdef CONFIG_COMPAT
1548struct compat_sioc_sg_req {
1549 struct in_addr src;
1550 struct in_addr grp;
1551 compat_ulong_t pktcnt;
1552 compat_ulong_t bytecnt;
1553 compat_ulong_t wrong_if;
1554};
1555
David S. Millerca6b8bb02011-02-03 17:24:28 -08001556struct compat_sioc_vif_req {
1557 vifi_t vifi; /* Which iface */
1558 compat_ulong_t icount;
1559 compat_ulong_t ocount;
1560 compat_ulong_t ibytes;
1561 compat_ulong_t obytes;
1562};
1563
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001564int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1565{
David S. Miller0033d5a2011-02-03 17:21:31 -08001566 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001567 struct compat_sioc_vif_req vr;
1568 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001569 struct mfc_cache *c;
1570 struct net *net = sock_net(sk);
1571 struct mr_table *mrt;
1572
1573 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001574 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001575 return -ENOENT;
1576
1577 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001578 case SIOCGETVIFCNT:
1579 if (copy_from_user(&vr, arg, sizeof(vr)))
1580 return -EFAULT;
1581 if (vr.vifi >= mrt->maxvif)
1582 return -EINVAL;
1583 read_lock(&mrt_lock);
1584 vif = &mrt->vif_table[vr.vifi];
1585 if (VIF_EXISTS(mrt, vr.vifi)) {
1586 vr.icount = vif->pkt_in;
1587 vr.ocount = vif->pkt_out;
1588 vr.ibytes = vif->bytes_in;
1589 vr.obytes = vif->bytes_out;
1590 read_unlock(&mrt_lock);
1591
1592 if (copy_to_user(arg, &vr, sizeof(vr)))
1593 return -EFAULT;
1594 return 0;
1595 }
1596 read_unlock(&mrt_lock);
1597 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001598 case SIOCGETSGCNT:
1599 if (copy_from_user(&sr, arg, sizeof(sr)))
1600 return -EFAULT;
1601
1602 rcu_read_lock();
1603 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1604 if (c) {
1605 sr.pktcnt = c->mfc_un.res.pkt;
1606 sr.bytecnt = c->mfc_un.res.bytes;
1607 sr.wrong_if = c->mfc_un.res.wrong_if;
1608 rcu_read_unlock();
1609
1610 if (copy_to_user(arg, &sr, sizeof(sr)))
1611 return -EFAULT;
1612 return 0;
1613 }
1614 rcu_read_unlock();
1615 return -EADDRNOTAVAIL;
1616 default:
1617 return -ENOIOCTLCMD;
1618 }
1619}
1620#endif
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1624{
Jiri Pirko351638e2013-05-28 01:30:21 +00001625 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001626 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001627 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct vif_device *v;
1629 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (event != NETDEV_UNREGISTER)
1632 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001633
1634 ipmr_for_each_table(mrt, net) {
1635 v = &mrt->vif_table[0];
1636 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1637 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001638 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641 return NOTIFY_DONE;
1642}
1643
1644
Jianjun Kongc354e122008-11-03 00:28:02 -08001645static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 .notifier_call = ipmr_device_event,
1647};
1648
1649/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001650 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * This avoids tunnel drivers and other mess and gives us the speed so
1652 * important for multicast video.
1653 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001654
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001655static void ip_encap(struct net *net, struct sk_buff *skb,
1656 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001658 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001659 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001660
1661 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001662 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001663 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001664 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001666 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001667 iph->tos = old_iph->tos;
1668 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 iph->frag_off = 0;
1670 iph->daddr = daddr;
1671 iph->saddr = saddr;
1672 iph->protocol = IPPROTO_IPIP;
1673 iph->ihl = 5;
1674 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001675 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 ip_send_check(iph);
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1679 nf_reset(skb);
1680}
1681
David Miller7026b1d2015-04-05 22:19:04 -04001682static inline int ipmr_forward_finish(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001684 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Eric Dumazetadf30902009-06-02 05:19:30 +00001686 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Vincent Bernat2d8dbb02012-06-05 03:41:42 +00001687 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (unlikely(opt->optlen))
1690 ip_forward_options(skb);
1691
David Miller7026b1d2015-04-05 22:19:04 -04001692 return dst_output_sk(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695/*
1696 * Processing handlers for ipmr_forward
1697 */
1698
Patrick McHardy0c122952010-04-13 05:03:22 +00001699static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1700 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001702 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001703 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 struct net_device *dev;
1705 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001706 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 int encap = 0;
1708
Ian Morris51456b22015-04-03 09:17:26 +01001709 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 goto out_free;
1711
1712#ifdef CONFIG_IP_PIMSM
1713 if (vif->flags & VIFF_REGISTER) {
1714 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001715 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001716 vif->dev->stats.tx_bytes += skb->len;
1717 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001718 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001719 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721#endif
1722
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001723 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001724 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001725 vif->remote, vif->local,
1726 0, 0,
1727 IPPROTO_IPIP,
1728 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001729 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 goto out_free;
1731 encap = sizeof(struct iphdr);
1732 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001733 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001734 0, 0,
1735 IPPROTO_IPIP,
1736 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001737 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto out_free;
1739 }
1740
Changli Gaod8d1f302010-06-10 23:31:35 -07001741 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Changli Gaod8d1f302010-06-10 23:31:35 -07001743 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001745 * allow to send ICMP, so that packets will disappear
1746 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 */
1748
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001749 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 ip_rt_put(rt);
1751 goto out_free;
1752 }
1753
Changli Gaod8d1f302010-06-10 23:31:35 -07001754 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001757 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 goto out_free;
1759 }
1760
1761 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001762 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Eric Dumazetadf30902009-06-02 05:19:30 +00001764 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001765 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001766 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001769 * What do we do with netfilter? -- RR
1770 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001772 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001774 vif->dev->stats.tx_packets++;
1775 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
1778 IPCB(skb)->flags |= IPSKB_FORWARDED;
1779
1780 /*
1781 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1782 * not only before forwarding, but after forwarding on all output
1783 * interfaces. It is clear, if mrouter runs a multicasting
1784 * program, it should receive packets not depending to what interface
1785 * program is joined.
1786 * If we will not make it, the program will have to join on all
1787 * interfaces. On the other hand, multihoming host (or router, but
1788 * not mrouter) cannot join to more than one interface - it will
1789 * result in receiving multiple packets.
1790 */
David Miller7026b1d2015-04-05 22:19:04 -04001791 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, NULL, skb,
1792 skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 ipmr_forward_finish);
1794 return;
1795
1796out_free:
1797 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
Patrick McHardy0c122952010-04-13 05:03:22 +00001800static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001803
1804 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1805 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
1807 }
1808 return ct;
1809}
1810
1811/* "local" means that we should preserve one skb (for local delivery) */
1812
Rami Rosenc4854ec2013-07-20 15:09:28 +03001813static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1814 struct sk_buff *skb, struct mfc_cache *cache,
1815 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 int psend = -1;
1818 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001819 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 vif = cache->mfc_parent;
1822 cache->mfc_un.res.pkt++;
1823 cache->mfc_un.res.bytes += skb->len;
1824
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001825 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001826 struct mfc_cache *cache_proxy;
1827
1828 /* For an (*,G) entry, we only check that the incomming
1829 * interface is part of the static tree.
1830 */
1831 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1832 if (cache_proxy &&
1833 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1834 goto forward;
1835 }
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /*
1838 * Wrong interface: drop packet and (maybe) send PIM assert.
1839 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001840 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001841 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001843 * Very complicated situation...
1844 *
1845 * The best workaround until routing daemons will be
1846 * fixed is not to redistribute packet, if it was
1847 * send through wrong interface. It means, that
1848 * multicast applications WILL NOT work for
1849 * (S,G), which have default multicast route pointing
1850 * to wrong oif. In any case, it is not a good
1851 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 */
1853 goto dont_forward;
1854 }
1855
1856 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Patrick McHardy0c122952010-04-13 05:03:22 +00001858 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001860 * so that we cannot check that packet arrived on an oif.
1861 * It is bad, but otherwise we would need to move pretty
1862 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001864 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001865 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001866 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1868 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001869 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871 goto dont_forward;
1872 }
1873
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001874forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001875 mrt->vif_table[vif].pkt_in++;
1876 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 /*
1879 * Forward the frame
1880 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001881 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1882 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001883 if (true_vifi >= 0 &&
1884 true_vifi != cache->mfc_parent &&
1885 ip_hdr(skb)->ttl >
1886 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1887 /* It's an (*,*) entry and the packet is not coming from
1888 * the upstream: forward the packet to the upstream
1889 * only.
1890 */
1891 psend = cache->mfc_parent;
1892 goto last_forward;
1893 }
1894 goto dont_forward;
1895 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001896 for (ct = cache->mfc_un.res.maxvif - 1;
1897 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001898 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001899 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1900 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001901 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (psend != -1) {
1903 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001906 ipmr_queue_xmit(net, mrt, skb2, cache,
1907 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001909 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001912last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 if (psend != -1) {
1914 if (local) {
1915 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001918 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001920 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001921 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923 }
1924
1925dont_forward:
1926 if (!local)
1927 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
David S. Miller417da662011-05-03 19:42:43 -07001930static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001931{
David S. Miller417da662011-05-03 19:42:43 -07001932 struct rtable *rt = skb_rtable(skb);
1933 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001934 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001935 .daddr = iph->daddr,
1936 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001937 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001938 .flowi4_oif = (rt_is_output_route(rt) ?
1939 skb->dev->ifindex : 0),
1940 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001941 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001942 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001943 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001944 };
1945 struct mr_table *mrt;
1946 int err;
1947
David S. Millerda919812011-03-12 02:04:50 -05001948 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001949 if (err)
1950 return ERR_PTR(err);
1951 return mrt;
1952}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954/*
1955 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001956 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 */
1958
1959int ip_mr_input(struct sk_buff *skb)
1960{
1961 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001962 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001963 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001964 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001967 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001969 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 goto dont_forward;
1971
David S. Miller417da662011-05-03 19:42:43 -07001972 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001973 if (IS_ERR(mrt)) {
1974 kfree_skb(skb);
1975 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001978 if (IPCB(skb)->opt.router_alert) {
1979 if (ip_call_ra_chain(skb))
1980 return 0;
1981 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1982 /* IGMPv1 (and broken IGMPv2 implementations sort of
1983 * Cisco IOS <= 11.2(8)) do not put router alert
1984 * option to IGMP packets destined to routable
1985 * groups. It is very bad, because it means
1986 * that we can forward NO IGMP messages.
1987 */
1988 struct sock *mroute_sk;
1989
1990 mroute_sk = rcu_dereference(mrt->mroute_sk);
1991 if (mroute_sk) {
1992 nf_reset(skb);
1993 raw_rcv(mroute_sk, skb);
1994 return 0;
1995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 }
1998
Eric Dumazeta8c94862010-10-01 16:15:08 +00001999 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002000 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002001 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002002 int vif = ipmr_find_vif(mrt, skb->dev);
2003
2004 if (vif >= 0)
2005 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2006 vif);
2007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 /*
2010 * No usable cache entry
2011 */
Ian Morris51456b22015-04-03 09:17:26 +01002012 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int vif;
2014
2015 if (local) {
2016 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2017 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002018 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 skb = skb2;
2021 }
2022
Eric Dumazeta8c94862010-10-01 16:15:08 +00002023 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002024 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002026 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 read_unlock(&mrt_lock);
2028
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002029 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031 read_unlock(&mrt_lock);
2032 kfree_skb(skb);
2033 return -ENODEV;
2034 }
2035
Eric Dumazeta8c94862010-10-01 16:15:08 +00002036 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002037 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 read_unlock(&mrt_lock);
2039
2040 if (local)
2041 return ip_local_deliver(skb);
2042
2043 return 0;
2044
2045dont_forward:
2046 if (local)
2047 return ip_local_deliver(skb);
2048 kfree_skb(skb);
2049 return 0;
2050}
2051
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002052#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00002053/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002054static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2055 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002057 struct net_device *reg_dev = NULL;
2058 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002060 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002062 * Check that:
2063 * a. packet is really sent to a multicast group
2064 * b. packet is not a NULL-REGISTER
2065 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002067 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002069 ntohs(encap->tot_len) + pimlen > skb->len)
2070 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002073 if (mrt->mroute_reg_vif_num >= 0)
2074 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 read_unlock(&mrt_lock);
2076
Ian Morris51456b22015-04-03 09:17:26 +01002077 if (!reg_dev)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002078 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002080 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00002081 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03002082 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00002084 skb->ip_summed = CHECKSUM_NONE;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002085
Nicolas Dichtelea231922013-09-02 15:34:58 +02002086 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002089
Eric Dumazet55747a02010-10-01 16:14:55 +00002090 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002091}
2092#endif
2093
2094#ifdef CONFIG_IP_PIMSM_V1
2095/*
2096 * Handle IGMP messages of PIMv1
2097 */
2098
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002099int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002100{
2101 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002102 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002103 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002104
2105 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2106 goto drop;
2107
2108 pim = igmp_hdr(skb);
2109
David S. Miller417da662011-05-03 19:42:43 -07002110 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002111 if (IS_ERR(mrt))
2112 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002113 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002114 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2115 goto drop;
2116
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002117 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002118drop:
2119 kfree_skb(skb);
2120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return 0;
2122}
2123#endif
2124
2125#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002126static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002129 struct net *net = dev_net(skb->dev);
2130 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002132 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 goto drop;
2134
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002135 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002136 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2137 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002138 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002139 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 goto drop;
2141
David S. Miller417da662011-05-03 19:42:43 -07002142 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002143 if (IS_ERR(mrt))
2144 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002145 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002146drop:
2147 kfree_skb(skb);
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return 0;
2150}
2151#endif
2152
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002153static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2154 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 int ct;
2157 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002158 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002159 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Nicolas Dichtel74381892010-03-25 23:45:35 +00002161 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002162 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002163 return -ENOENT;
2164
Thomas Graf92a395e2012-06-26 23:36:13 +00002165 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2166 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2167 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Thomas Graf92a395e2012-06-26 23:36:13 +00002169 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2170 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002173 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002174 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2175 nla_nest_cancel(skb, mp_attr);
2176 return -EMSGSIZE;
2177 }
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 nhp->rtnh_flags = 0;
2180 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002181 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 nhp->rtnh_len = sizeof(*nhp);
2183 }
2184 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002185
2186 nla_nest_end(skb, mp_attr);
2187
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002188 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2189 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2190 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2191 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2192 return -EMSGSIZE;
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 rtm->rtm_type = RTN_MULTICAST;
2195 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
David S. Miller9a1b9492011-05-04 12:18:54 -07002198int ipmr_get_route(struct net *net, struct sk_buff *skb,
2199 __be32 saddr, __be32 daddr,
2200 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002203 struct mr_table *mrt;
2204 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002206 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002207 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002208 return -ENOENT;
2209
Eric Dumazeta8c94862010-10-01 16:15:08 +00002210 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002211 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002212 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002213 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002215 if (vif >= 0)
2216 cache = ipmr_cache_find_any(mrt, daddr, vif);
2217 }
Ian Morris51456b22015-04-03 09:17:26 +01002218 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002219 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002220 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002222 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002225 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return -EAGAIN;
2227 }
2228
2229 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002230 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002231 if (dev)
2232 vif = ipmr_find_vif(mrt, dev);
2233 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002235 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 return -ENODEV;
2237 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002238 skb2 = skb_clone(skb, GFP_ATOMIC);
2239 if (!skb2) {
2240 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002241 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002242 return -ENOMEM;
2243 }
2244
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002245 skb_push(skb2, sizeof(struct iphdr));
2246 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002247 iph = ip_hdr(skb2);
2248 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002249 iph->saddr = saddr;
2250 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002251 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002252 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002254 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return err;
2256 }
2257
Eric Dumazeta8c94862010-10-01 16:15:08 +00002258 read_lock(&mrt_lock);
2259 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002261 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002263 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return err;
2265}
2266
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002267static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002268 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2269 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002270{
2271 struct nlmsghdr *nlh;
2272 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002273 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002274
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002275 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002276 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002277 return -EMSGSIZE;
2278
2279 rtm = nlmsg_data(nlh);
2280 rtm->rtm_family = RTNL_FAMILY_IPMR;
2281 rtm->rtm_dst_len = 32;
2282 rtm->rtm_src_len = 32;
2283 rtm->rtm_tos = 0;
2284 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002285 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2286 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002287 rtm->rtm_type = RTN_MULTICAST;
2288 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002289 if (c->mfc_flags & MFC_STATIC)
2290 rtm->rtm_protocol = RTPROT_STATIC;
2291 else
2292 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002293 rtm->rtm_flags = 0;
2294
Jiri Benc930345e2015-03-29 16:59:25 +02002295 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2296 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002297 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002298 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2299 /* do not break the dump if cache is unresolved */
2300 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002301 goto nla_put_failure;
2302
Johannes Berg053c0952015-01-16 22:09:00 +01002303 nlmsg_end(skb, nlh);
2304 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002305
2306nla_put_failure:
2307 nlmsg_cancel(skb, nlh);
2308 return -EMSGSIZE;
2309}
2310
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002311static size_t mroute_msgsize(bool unresolved, int maxvif)
2312{
2313 size_t len =
2314 NLMSG_ALIGN(sizeof(struct rtmsg))
2315 + nla_total_size(4) /* RTA_TABLE */
2316 + nla_total_size(4) /* RTA_SRC */
2317 + nla_total_size(4) /* RTA_DST */
2318 ;
2319
2320 if (!unresolved)
2321 len = len
2322 + nla_total_size(4) /* RTA_IIF */
2323 + nla_total_size(0) /* RTA_MULTIPATH */
2324 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2325 /* RTA_MFC_STATS */
2326 + nla_total_size(sizeof(struct rta_mfc_stats))
2327 ;
2328
2329 return len;
2330}
2331
2332static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2333 int cmd)
2334{
2335 struct net *net = read_pnet(&mrt->net);
2336 struct sk_buff *skb;
2337 int err = -ENOBUFS;
2338
2339 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2340 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002341 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002342 goto errout;
2343
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002344 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002345 if (err < 0)
2346 goto errout;
2347
2348 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2349 return;
2350
2351errout:
2352 kfree_skb(skb);
2353 if (err < 0)
2354 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2355}
2356
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002357static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2358{
2359 struct net *net = sock_net(skb->sk);
2360 struct mr_table *mrt;
2361 struct mfc_cache *mfc;
2362 unsigned int t = 0, s_t;
2363 unsigned int h = 0, s_h;
2364 unsigned int e = 0, s_e;
2365
2366 s_t = cb->args[0];
2367 s_h = cb->args[1];
2368 s_e = cb->args[2];
2369
Eric Dumazeta8c94862010-10-01 16:15:08 +00002370 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002371 ipmr_for_each_table(mrt, net) {
2372 if (t < s_t)
2373 goto next_table;
2374 if (t > s_t)
2375 s_h = 0;
2376 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002377 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002378 if (e < s_e)
2379 goto next_entry;
2380 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002381 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002382 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002383 mfc, RTM_NEWROUTE,
2384 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002385 goto done;
2386next_entry:
2387 e++;
2388 }
2389 e = s_e = 0;
2390 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002391 spin_lock_bh(&mfc_unres_lock);
2392 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2393 if (e < s_e)
2394 goto next_entry2;
2395 if (ipmr_fill_mroute(mrt, skb,
2396 NETLINK_CB(cb->skb).portid,
2397 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002398 mfc, RTM_NEWROUTE,
2399 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002400 spin_unlock_bh(&mfc_unres_lock);
2401 goto done;
2402 }
2403next_entry2:
2404 e++;
2405 }
2406 spin_unlock_bh(&mfc_unres_lock);
2407 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002408 s_h = 0;
2409next_table:
2410 t++;
2411 }
2412done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002413 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002414
2415 cb->args[2] = e;
2416 cb->args[1] = h;
2417 cb->args[0] = t;
2418
2419 return skb->len;
2420}
2421
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002422#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002424 * The /proc interfaces to multicast routing :
2425 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 */
2427struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002428 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002429 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 int ct;
2431};
2432
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002433static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2434 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 loff_t pos)
2436{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002437 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002438
2439 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2440 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002442 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002443 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445 return NULL;
2446}
2447
2448static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002449 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002451 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002452 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002453 struct mr_table *mrt;
2454
2455 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002456 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002457 return ERR_PTR(-ENOENT);
2458
2459 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002462 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 : SEQ_START_TOKEN;
2464}
2465
2466static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2467{
2468 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002469 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002470 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 ++*pos;
2473 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002474 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002475
Patrick McHardy0c122952010-04-13 05:03:22 +00002476 while (++iter->ct < mrt->maxvif) {
2477 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002479 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481 return NULL;
2482}
2483
2484static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002485 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
2487 read_unlock(&mrt_lock);
2488}
2489
2490static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2491{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002492 struct ipmr_vif_iter *iter = seq->private;
2493 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002496 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2498 } else {
2499 const struct vif_device *vif = v;
2500 const char *name = vif->dev ? vif->dev->name : "none";
2501
2502 seq_printf(seq,
2503 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002504 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002505 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 vif->bytes_out, vif->pkt_out,
2507 vif->flags, vif->local, vif->remote);
2508 }
2509 return 0;
2510}
2511
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002512static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 .start = ipmr_vif_seq_start,
2514 .next = ipmr_vif_seq_next,
2515 .stop = ipmr_vif_seq_stop,
2516 .show = ipmr_vif_seq_show,
2517};
2518
2519static int ipmr_vif_open(struct inode *inode, struct file *file)
2520{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002521 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2522 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
Arjan van de Ven9a321442007-02-12 00:55:35 -08002525static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 .owner = THIS_MODULE,
2527 .open = ipmr_vif_open,
2528 .read = seq_read,
2529 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002530 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531};
2532
2533struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002534 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002535 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002536 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 int ct;
2538};
2539
2540
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002541static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2542 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002544 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct mfc_cache *mfc;
2546
Eric Dumazeta8c94862010-10-01 16:15:08 +00002547 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002548 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002549 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002550 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002551 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002553 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002554 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002557 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002558 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002559 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 return mfc;
2561 spin_unlock_bh(&mfc_unres_lock);
2562
2563 it->cache = NULL;
2564 return NULL;
2565}
2566
2567
2568static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2569{
2570 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002571 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002572 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002573
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002574 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002575 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002576 return ERR_PTR(-ENOENT);
2577
2578 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 it->cache = NULL;
2580 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002581 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 : SEQ_START_TOKEN;
2583}
2584
2585static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2586{
2587 struct mfc_cache *mfc = v;
2588 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002589 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002590 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 ++*pos;
2593
2594 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002595 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Patrick McHardy862465f2010-04-13 05:03:21 +00002597 if (mfc->list.next != it->cache)
2598 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002599
Patrick McHardy0c122952010-04-13 05:03:22 +00002600 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 goto end_of_list;
2602
Patrick McHardy0c122952010-04-13 05:03:22 +00002603 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002606 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002607 if (list_empty(it->cache))
2608 continue;
2609 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
2611
2612 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002613 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002614 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002618 if (!list_empty(it->cache))
2619 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002621end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 spin_unlock_bh(&mfc_unres_lock);
2623 it->cache = NULL;
2624
2625 return NULL;
2626}
2627
2628static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2629{
2630 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002631 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Patrick McHardy0c122952010-04-13 05:03:22 +00002633 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002635 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002636 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
2639static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2640{
2641 int n;
2642
2643 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002644 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2646 } else {
2647 const struct mfc_cache *mfc = v;
2648 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002649 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002650
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002651 seq_printf(seq, "%08X %08X %-3hd",
2652 (__force u32) mfc->mfc_mcastgrp,
2653 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002654 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Patrick McHardy0c122952010-04-13 05:03:22 +00002656 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002657 seq_printf(seq, " %8lu %8lu %8lu",
2658 mfc->mfc_un.res.pkt,
2659 mfc->mfc_un.res.bytes,
2660 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002661 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002662 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002663 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002664 mfc->mfc_un.res.ttls[n] < 255)
2665 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002666 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 n, mfc->mfc_un.res.ttls[n]);
2668 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002669 } else {
2670 /* unresolved mfc_caches don't contain
2671 * pkt, bytes and wrong_if values
2672 */
2673 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675 seq_putc(seq, '\n');
2676 }
2677 return 0;
2678}
2679
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002680static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 .start = ipmr_mfc_seq_start,
2682 .next = ipmr_mfc_seq_next,
2683 .stop = ipmr_mfc_seq_stop,
2684 .show = ipmr_mfc_seq_show,
2685};
2686
2687static int ipmr_mfc_open(struct inode *inode, struct file *file)
2688{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002689 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2690 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
Arjan van de Ven9a321442007-02-12 00:55:35 -08002693static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 .owner = THIS_MODULE,
2695 .open = ipmr_mfc_open,
2696 .read = seq_read,
2697 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002698 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002700#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002703static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002705 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706};
2707#endif
2708
2709
2710/*
2711 * Setup for IP multicast routing
2712 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002713static int __net_init ipmr_net_init(struct net *net)
2714{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002715 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002716
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002717 err = ipmr_rules_init(net);
2718 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002719 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002720
2721#ifdef CONFIG_PROC_FS
2722 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002723 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002724 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002725 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002726 goto proc_cache_fail;
2727#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002728 return 0;
2729
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002730#ifdef CONFIG_PROC_FS
2731proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002732 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002733proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002734 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002735#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002736fail:
2737 return err;
2738}
2739
2740static void __net_exit ipmr_net_exit(struct net *net)
2741{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002742#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002743 remove_proc_entry("ip_mr_cache", net->proc_net);
2744 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002745#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002746 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002747}
2748
2749static struct pernet_operations ipmr_net_ops = {
2750 .init = ipmr_net_init,
2751 .exit = ipmr_net_exit,
2752};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002753
Wang Chen03d2f892008-07-03 12:13:36 +08002754int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Wang Chen03d2f892008-07-03 12:13:36 +08002756 int err;
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2759 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002760 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002761 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002762 if (!mrt_cachep)
2763 return -ENOMEM;
2764
Benjamin Therycf958ae32009-01-22 04:56:16 +00002765 err = register_pernet_subsys(&ipmr_net_ops);
2766 if (err)
2767 goto reg_pernet_fail;
2768
Wang Chen03d2f892008-07-03 12:13:36 +08002769 err = register_netdevice_notifier(&ip_mr_notifier);
2770 if (err)
2771 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002772#ifdef CONFIG_IP_PIMSM_V2
2773 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002774 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002775 err = -EAGAIN;
2776 goto add_proto_fail;
2777 }
2778#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002779 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2780 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002781 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002782
Tom Goff403dbb92009-06-14 03:16:13 -07002783#ifdef CONFIG_IP_PIMSM_V2
2784add_proto_fail:
2785 unregister_netdevice_notifier(&ip_mr_notifier);
2786#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002787reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002788 unregister_pernet_subsys(&ipmr_net_ops);
2789reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002790 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}