blob: 5ad48ec7771007a0c01c8210001ecb17ab012863 [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>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010069#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardyf0ad0862010-04-13 05:03:23 +000071struct ipmr_rule {
72 struct fib_rule common;
73};
74
75struct ipmr_result {
76 struct mr_table *mrt;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000080 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static DEFINE_RWLOCK(mrt_lock);
84
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010085/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Special spinlock for queue of unresolved entries */
88static DEFINE_SPINLOCK(mfc_unres_lock);
89
90/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000091 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
94 *
95 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97
Christoph Lametere18b8902006-12-06 20:33:20 -080098static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000100static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000101static void ipmr_free_table(struct mr_table *mrt);
102
Rami Rosenc4854ec2013-07-20 15:09:28 +0300103static void ip_mr_forward(struct net *net, struct mr_table *mrt,
104 struct sk_buff *skb, struct mfc_cache *cache,
105 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000106static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000107 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200108static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
109 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000110static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
111 int cmd);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100112static void mroute_clean_tables(struct mr_table *mrt, bool all);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000113static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000115#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
116#define ipmr_for_each_table(mrt, net) \
117 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
118
119static struct mr_table *ipmr_get_table(struct net *net, u32 id)
120{
121 struct mr_table *mrt;
122
123 ipmr_for_each_table(mrt, net) {
124 if (mrt->id == id)
125 return mrt;
126 }
127 return NULL;
128}
129
David S. Millerda919812011-03-12 02:04:50 -0500130static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000131 struct mr_table **mrt)
132{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000133 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100134 struct ipmr_result res;
135 struct fib_lookup_arg arg = {
136 .result = &res,
137 .flags = FIB_LOOKUP_NOREF,
138 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000139
David S. Millerda919812011-03-12 02:04:50 -0500140 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
141 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000142 if (err < 0)
143 return err;
144 *mrt = res.mrt;
145 return 0;
146}
147
148static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
149 int flags, struct fib_lookup_arg *arg)
150{
151 struct ipmr_result *res = arg->result;
152 struct mr_table *mrt;
153
154 switch (rule->action) {
155 case FR_ACT_TO_TBL:
156 break;
157 case FR_ACT_UNREACHABLE:
158 return -ENETUNREACH;
159 case FR_ACT_PROHIBIT:
160 return -EACCES;
161 case FR_ACT_BLACKHOLE:
162 default:
163 return -EINVAL;
164 }
165
166 mrt = ipmr_get_table(rule->fr_net, rule->table);
Ian Morris51456b22015-04-03 09:17:26 +0100167 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000168 return -EAGAIN;
169 res->mrt = mrt;
170 return 0;
171}
172
173static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
174{
175 return 1;
176}
177
178static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
179 FRA_GENERIC_POLICY,
180};
181
182static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
183 struct fib_rule_hdr *frh, struct nlattr **tb)
184{
185 return 0;
186}
187
188static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
189 struct nlattr **tb)
190{
191 return 1;
192}
193
194static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
195 struct fib_rule_hdr *frh)
196{
197 frh->dst_len = 0;
198 frh->src_len = 0;
199 frh->tos = 0;
200 return 0;
201}
202
Andi Kleen04a6f822012-10-04 17:12:11 -0700203static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200204 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000205 .rule_size = sizeof(struct ipmr_rule),
206 .addr_size = sizeof(u32),
207 .action = ipmr_rule_action,
208 .match = ipmr_rule_match,
209 .configure = ipmr_rule_configure,
210 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000211 .fill = ipmr_rule_fill,
212 .nlgroup = RTNLGRP_IPV4_RULE,
213 .policy = ipmr_rule_policy,
214 .owner = THIS_MODULE,
215};
216
217static int __net_init ipmr_rules_init(struct net *net)
218{
219 struct fib_rules_ops *ops;
220 struct mr_table *mrt;
221 int err;
222
223 ops = fib_rules_register(&ipmr_rules_ops_template, net);
224 if (IS_ERR(ops))
225 return PTR_ERR(ops);
226
227 INIT_LIST_HEAD(&net->ipv4.mr_tables);
228
229 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100230 if (IS_ERR(mrt)) {
231 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000232 goto err1;
233 }
234
235 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
236 if (err < 0)
237 goto err2;
238
239 net->ipv4.mr_rules_ops = ops;
240 return 0;
241
242err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700243 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000244err1:
245 fib_rules_unregister(ops);
246 return err;
247}
248
249static void __net_exit ipmr_rules_exit(struct net *net)
250{
251 struct mr_table *mrt, *next;
252
WANG Conged785302015-03-31 11:01:45 -0700253 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000254 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
255 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000256 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000257 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000258 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700259 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000260}
261#else
262#define ipmr_for_each_table(mrt, net) \
263 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
264
265static struct mr_table *ipmr_get_table(struct net *net, u32 id)
266{
267 return net->ipv4.mrt;
268}
269
David S. Millerda919812011-03-12 02:04:50 -0500270static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000271 struct mr_table **mrt)
272{
273 *mrt = net->ipv4.mrt;
274 return 0;
275}
276
277static int __net_init ipmr_rules_init(struct net *net)
278{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100279 struct mr_table *mrt;
280
281 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
282 if (IS_ERR(mrt))
283 return PTR_ERR(mrt);
284 net->ipv4.mrt = mrt;
285 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000286}
287
288static void __net_exit ipmr_rules_exit(struct net *net)
289{
WANG Conged785302015-03-31 11:01:45 -0700290 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000291 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700292 net->ipv4.mrt = NULL;
293 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000294}
295#endif
296
297static struct mr_table *ipmr_new_table(struct net *net, u32 id)
298{
299 struct mr_table *mrt;
300 unsigned int i;
301
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100302 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
303 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
304 return ERR_PTR(-EINVAL);
305
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000306 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100307 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000308 return mrt;
309
310 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100311 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100312 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200313 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000314 mrt->id = id;
315
316 /* Forwarding cache */
317 for (i = 0; i < MFC_LINES; i++)
318 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
319
320 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
321
322 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
323 (unsigned long)mrt);
324
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000325 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000326#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
327 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
328#endif
329 return mrt;
330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000332static void ipmr_free_table(struct mr_table *mrt)
333{
334 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100335 mroute_clean_tables(mrt, true);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000336 kfree(mrt);
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
340
Wang Chend6070322008-07-14 20:55:26 -0700341static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
342{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000343 struct net *net = dev_net(dev);
344
Wang Chend6070322008-07-14 20:55:26 -0700345 dev_close(dev);
346
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000347 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700348 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800349 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700350 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700351 struct ip_tunnel_parm p;
352
353 memset(&p, 0, sizeof(p));
354 p.iph.daddr = v->vifc_rmt_addr.s_addr;
355 p.iph.saddr = v->vifc_lcl_addr.s_addr;
356 p.iph.version = 4;
357 p.iph.ihl = 5;
358 p.iph.protocol = IPPROTO_IPIP;
359 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
360 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
361
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800362 if (ops->ndo_do_ioctl) {
363 mm_segment_t oldfs = get_fs();
364
365 set_fs(KERNEL_DS);
366 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
367 set_fs(oldfs);
368 }
Wang Chend6070322008-07-14 20:55:26 -0700369 }
370}
371
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100372/* Initialize ipmr pimreg/tunnel in_device */
373static bool ipmr_init_vif_indev(const struct net_device *dev)
374{
375 struct in_device *in_dev;
376
377 ASSERT_RTNL();
378
379 in_dev = __in_dev_get_rtnl(dev);
380 if (!in_dev)
381 return false;
382 ipv4_devconf_setall(in_dev);
383 neigh_parms_data_state_setall(in_dev->arp_parms);
384 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
385
386 return true;
387}
388
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100389static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct net_device *dev;
392
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000393 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800396 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int err;
398 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 memset(&p, 0, sizeof(p));
402 p.iph.daddr = v->vifc_rmt_addr.s_addr;
403 p.iph.saddr = v->vifc_lcl_addr.s_addr;
404 p.iph.version = 4;
405 p.iph.ihl = 5;
406 p.iph.protocol = IPPROTO_IPIP;
407 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800408 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800410 if (ops->ndo_do_ioctl) {
411 mm_segment_t oldfs = get_fs();
412
413 set_fs(KERNEL_DS);
414 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
415 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000416 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800417 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dev = NULL;
420
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000421 if (err == 0 &&
422 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100424 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (dev_open(dev))
427 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700428 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 }
431 return dev;
432
433failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 unregister_netdevice(dev);
435 return NULL;
436}
437
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100438#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000439static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000441 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000442 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500443 struct flowi4 fl4 = {
444 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700445 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500446 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000447 };
448 int err;
449
David S. Millerda919812011-03-12 02:04:50 -0500450 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000451 if (err < 0) {
452 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000453 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000454 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700457 dev->stats.tx_bytes += skb->len;
458 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000459 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 read_unlock(&mrt_lock);
461 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000462 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200465static int reg_vif_get_iflink(const struct net_device *dev)
466{
467 return 0;
468}
469
Stephen Hemminger007c3832008-11-20 20:28:35 -0800470static const struct net_device_ops reg_vif_netdev_ops = {
471 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200472 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800473};
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static void reg_vif_setup(struct net_device *dev)
476{
477 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800478 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530480 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700482 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000485static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000488 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000490 if (mrt->id == RT_TABLE_DEFAULT)
491 sprintf(name, "pimreg");
492 else
493 sprintf(name, "pimreg%u", mrt->id);
494
Tom Gundersenc835a672014-07-14 16:37:24 +0200495 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Ian Morris51456b22015-04-03 09:17:26 +0100497 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return NULL;
499
Tom Goff403dbb92009-06-14 03:16:13 -0700500 dev_net_set(dev, net);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (register_netdevice(dev)) {
503 free_netdev(dev);
504 return NULL;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100507 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (dev_open(dev))
510 goto failure;
511
Wang Chen7dc00c82008-07-14 20:56:34 -0700512 dev_hold(dev);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return dev;
515
516failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unregister_netdevice(dev);
518 return NULL;
519}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100520
521/* called with rcu_read_lock() */
522static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
523 unsigned int pimlen)
524{
525 struct net_device *reg_dev = NULL;
526 struct iphdr *encap;
527
528 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100529 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100530 * a. packet is really sent to a multicast group
531 * b. packet is not a NULL-REGISTER
532 * c. packet is not truncated
533 */
534 if (!ipv4_is_multicast(encap->daddr) ||
535 encap->tot_len == 0 ||
536 ntohs(encap->tot_len) + pimlen > skb->len)
537 return 1;
538
539 read_lock(&mrt_lock);
540 if (mrt->mroute_reg_vif_num >= 0)
541 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
542 read_unlock(&mrt_lock);
543
544 if (!reg_dev)
545 return 1;
546
547 skb->mac_header = skb->network_header;
548 skb_pull(skb, (u8 *)encap - skb->data);
549 skb_reset_network_header(skb);
550 skb->protocol = htons(ETH_P_IP);
551 skb->ip_summed = CHECKSUM_NONE;
552
553 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
554
555 netif_rx(skb);
556
557 return NET_RX_SUCCESS;
558}
559#else
560static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
561{
562 return NULL;
563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#endif
565
Ben Hutchings2c530402012-07-10 10:55:09 +0000566/**
567 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700568 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000570static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000571 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct vif_device *v;
574 struct net_device *dev;
575 struct in_device *in_dev;
576
Patrick McHardy0c122952010-04-13 05:03:22 +0000577 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -EADDRNOTAVAIL;
579
Patrick McHardy0c122952010-04-13 05:03:22 +0000580 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 write_lock_bh(&mrt_lock);
583 dev = v->dev;
584 v->dev = NULL;
585
586 if (!dev) {
587 write_unlock_bh(&mrt_lock);
588 return -EADDRNOTAVAIL;
589 }
590
Patrick McHardy0c122952010-04-13 05:03:22 +0000591 if (vifi == mrt->mroute_reg_vif_num)
592 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000594 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000596
597 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000598 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 break;
600 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000601 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
604 write_unlock_bh(&mrt_lock);
605
606 dev_set_allmulti(dev, -1);
607
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000608 in_dev = __in_dev_get_rtnl(dev);
609 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700610 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000611 inet_netconf_notify_devconf(dev_net(dev),
612 NETCONFA_MC_FORWARDING,
613 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 ip_rt_multicast_event(in_dev);
615 }
616
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000617 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000618 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 dev_put(dev);
621 return 0;
622}
623
Eric Dumazeta8c94862010-10-01 16:15:08 +0000624static void ipmr_cache_free_rcu(struct rcu_head *head)
625{
626 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
627
628 kmem_cache_free(mrt_cachep, c);
629}
630
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000631static inline void ipmr_cache_free(struct mfc_cache *c)
632{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000633 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000637 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000639static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200641 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700643 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Patrick McHardy0c122952010-04-13 05:03:22 +0000645 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jianjun Kongc354e122008-11-03 00:28:02 -0800647 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700648 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
650 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000651 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000653 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700654 e->error = -ETIMEDOUT;
655 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700656
Eric W. Biederman15e47302012-09-07 20:12:54 +0000657 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000658 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000663 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Patrick McHardye258beb2010-04-13 05:03:19 +0000666/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000667static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Patrick McHardy0c122952010-04-13 05:03:22 +0000669 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned long now;
671 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000672 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000675 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return;
677 }
678
Patrick McHardy0c122952010-04-13 05:03:22 +0000679 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 goto out;
681
682 now = jiffies;
683 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Patrick McHardy0c122952010-04-13 05:03:22 +0000685 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (time_after(c->mfc_un.unres.expires, now)) {
687 unsigned long interval = c->mfc_un.unres.expires - now;
688 if (interval < expires)
689 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 continue;
691 }
692
Patrick McHardy862465f2010-04-13 05:03:21 +0000693 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000694 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000695 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Patrick McHardy0c122952010-04-13 05:03:22 +0000698 if (!list_empty(&mrt->mfc_unres_queue))
699 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701out:
702 spin_unlock(&mfc_unres_lock);
703}
704
705/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000706static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000707 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 int vifi;
710
711 cache->mfc_un.res.minvif = MAXVIFS;
712 cache->mfc_un.res.maxvif = 0;
713 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
714
Patrick McHardy0c122952010-04-13 05:03:22 +0000715 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
716 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000717 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
719 if (cache->mfc_un.res.minvif > vifi)
720 cache->mfc_un.res.minvif = vifi;
721 if (cache->mfc_un.res.maxvif <= vifi)
722 cache->mfc_un.res.maxvif = vifi + 1;
723 }
724 }
725}
726
Patrick McHardy0c122952010-04-13 05:03:22 +0000727static int vif_add(struct net *net, struct mr_table *mrt,
728 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000731 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct net_device *dev;
733 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700734 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000737 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EADDRINUSE;
739
740 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100742 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100743 return -EINVAL;
744 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 * All the packets will be sent to the daemon
746 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000747 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000749 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!dev)
751 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700752 err = dev_set_allmulti(dev, 1);
753 if (err) {
754 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700755 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700756 return err;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900759 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000760 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!dev)
762 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700763 err = dev_set_allmulti(dev, 1);
764 if (err) {
765 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700766 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700767 return err;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000770 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000772 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
773 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100774 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000775 dev_put(dev);
776 return -EADDRNOTAVAIL;
777 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000778 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000779 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (!dev)
782 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700783 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700784 if (err) {
785 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700786 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
789 default:
790 return -EINVAL;
791 }
792
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000793 in_dev = __in_dev_get_rtnl(dev);
794 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000795 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000797 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700798 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000799 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
800 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ip_rt_multicast_event(in_dev);
802
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000803 /* Fill in the VIF structures */
804
Jianjun Kongc354e122008-11-03 00:28:02 -0800805 v->rate_limit = vifc->vifc_rate_limit;
806 v->local = vifc->vifc_lcl_addr.s_addr;
807 v->remote = vifc->vifc_rmt_addr.s_addr;
808 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (!mrtsock)
810 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800811 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 v->bytes_in = 0;
813 v->bytes_out = 0;
814 v->pkt_in = 0;
815 v->pkt_out = 0;
816 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000817 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200818 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* And finish update writing critical data */
821 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800822 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000823 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000824 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000825 if (vifi+1 > mrt->maxvif)
826 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 write_unlock_bh(&mrt_lock);
828 return 0;
829}
830
Eric Dumazeta8c94862010-10-01 16:15:08 +0000831/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000832static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000833 __be32 origin,
834 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Jianjun Kongc354e122008-11-03 00:28:02 -0800836 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 struct mfc_cache *c;
838
Eric Dumazeta8c94862010-10-01 16:15:08 +0000839 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000840 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
841 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000843 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000846/* Look for a (*,*,oif) entry */
847static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
848 int vifi)
849{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100850 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000851 struct mfc_cache *c;
852
853 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100854 if (c->mfc_origin == htonl(INADDR_ANY) &&
855 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000856 c->mfc_un.res.ttls[vifi] < 255)
857 return c;
858
859 return NULL;
860}
861
862/* Look for a (*,G) entry */
863static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
864 __be32 mcastgrp, int vifi)
865{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100866 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000867 struct mfc_cache *c, *proxy;
868
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100869 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000870 goto skip;
871
872 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100873 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000874 c->mfc_mcastgrp == mcastgrp) {
875 if (c->mfc_un.res.ttls[vifi] < 255)
876 return c;
877
878 /* It's ok if the vifi is part of the static tree */
879 proxy = ipmr_cache_find_any_parent(mrt,
880 c->mfc_parent);
881 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
882 return c;
883 }
884
885skip:
886 return ipmr_cache_find_any_parent(mrt, vifi);
887}
888
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100889/* Allocate a multicast cache entry */
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
Tom Goff70a0dec2016-06-23 16:11:57 -0400894 if (c) {
895 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
Eric Dumazeta8c94862010-10-01 16:15:08 +0000896 c->mfc_un.res.minvif = MAXVIFS;
Tom Goff70a0dec2016-06-23 16:11:57 -0400897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return c;
899}
900
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000901static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Jianjun Kongc354e122008-11-03 00:28:02 -0800903 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000904
905 if (c) {
906 skb_queue_head_init(&c->mfc_un.unres.unresolved);
907 c->mfc_un.unres.expires = jiffies + 10*HZ;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return c;
910}
911
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100912/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000913static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
914 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700917 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000919 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800920 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700921 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
923
Hong zhi guo573ce262013-03-27 06:47:04 +0000924 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000925 nlh->nlmsg_len = skb_tail_pointer(skb) -
926 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 } else {
928 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000929 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000931 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700932 e->error = -EMSGSIZE;
933 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Thomas Graf2942e902006-08-15 00:30:25 -0700935
Eric W. Biederman15e47302012-09-07 20:12:54 +0000936 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000937 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000938 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941}
942
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100943/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
944 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100946 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000948static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000949 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300951 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100952 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 struct igmphdr *igmp;
954 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100955 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int ret;
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (assert == IGMPMSG_WHOLEPKT)
959 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
960 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 skb = alloc_skb(128, GFP_ATOMIC);
962
Stephen Hemminger132adf52007-03-08 20:44:43 -0800963 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -ENOBUFS;
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (assert == IGMPMSG_WHOLEPKT) {
967 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000968 * Duplicate old header, fix ihl, length etc.
969 * And all this only to mangle msg->im_msgtype and
970 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300972 skb_push(skb, sizeof(struct iphdr));
973 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300974 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300975 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700976 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 msg->im_msgtype = IGMPMSG_WHOLEPKT;
978 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000979 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700980 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
981 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
982 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100983 } else {
984 /* Copy the IP header */
985 skb_set_network_header(skb, skb->len);
986 skb_put(skb, ihl);
987 skb_copy_to_linear_data(skb, pkt->data, ihl);
988 /* Flag to the kernel this is a route add */
989 ip_hdr(skb)->protocol = 0;
990 msg = (struct igmpmsg *)skb_network_header(skb);
991 msg->im_vif = vifi;
992 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
993 /* Add our header */
994 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
995 igmp->type = assert;
996 msg->im_msgtype = assert;
997 igmp->code = 0;
998 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
999 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Eric Dumazet4c968702010-10-01 16:15:01 +00001002 rcu_read_lock();
1003 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001004 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001005 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 kfree_skb(skb);
1007 return -EINVAL;
1008 }
1009
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001010 /* Deliver to mrouted */
Eric Dumazet4c968702010-10-01 16:15:01 +00001011 ret = sock_queue_rcv_skb(mroute_sk, skb);
1012 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001013 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001014 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 kfree_skb(skb);
1016 }
1017
1018 return ret;
1019}
1020
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001021/* Queue a packet for resolution. It gets locked cache entry! */
1022static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1023 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Patrick McHardy862465f2010-04-13 05:03:21 +00001025 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int err;
1027 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001028 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001031 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001032 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001033 c->mfc_origin == iph->saddr) {
1034 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
Patrick McHardy862465f2010-04-13 05:03:21 +00001039 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001040 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001041 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001042 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 spin_unlock_bh(&mfc_unres_lock);
1044
1045 kfree_skb(skb);
1046 return -ENOBUFS;
1047 }
1048
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001049 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001050 c->mfc_parent = -1;
1051 c->mfc_origin = iph->saddr;
1052 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001054 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001055 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001056 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001057 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 out - Brad Parker
1059 */
1060 spin_unlock_bh(&mfc_unres_lock);
1061
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001062 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 kfree_skb(skb);
1064 return err;
1065 }
1066
Patrick McHardy0c122952010-04-13 05:03:22 +00001067 atomic_inc(&mrt->cache_resolve_queue_len);
1068 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001069 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
David S. Miller278554b2010-05-12 00:05:35 -07001071 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1072 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001075 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001076 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 kfree_skb(skb);
1078 err = -ENOBUFS;
1079 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001080 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 err = 0;
1082 }
1083
1084 spin_unlock_bh(&mfc_unres_lock);
1085 return err;
1086}
1087
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001088/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001090static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
1092 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001093 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Jianjun Kongc354e122008-11-03 00:28:02 -08001095 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Patrick McHardy0c122952010-04-13 05:03:22 +00001097 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001099 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1100 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001101 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001102 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001103 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return 0;
1105 }
1106 }
1107 return -ENOENT;
1108}
1109
Patrick McHardy0c122952010-04-13 05:03:22 +00001110static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001111 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Patrick McHardy862465f2010-04-13 05:03:21 +00001113 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001115 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Patrick McHardya50436f22010-03-17 06:04:14 +00001117 if (mfc->mfcc_parent >= MAXVIFS)
1118 return -ENFILE;
1119
Jianjun Kongc354e122008-11-03 00:28:02 -08001120 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Patrick McHardy0c122952010-04-13 05:03:22 +00001122 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001124 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1125 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001126 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130
Patrick McHardy862465f2010-04-13 05:03:21 +00001131 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 write_lock_bh(&mrt_lock);
1133 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001134 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (!mrtsock)
1136 c->mfc_flags |= MFC_STATIC;
1137 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001138 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return 0;
1140 }
1141
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001142 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001143 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return -EINVAL;
1145
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001146 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001147 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return -ENOMEM;
1149
Jianjun Kongc354e122008-11-03 00:28:02 -08001150 c->mfc_origin = mfc->mfcc_origin.s_addr;
1151 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1152 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001153 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (!mrtsock)
1155 c->mfc_flags |= MFC_STATIC;
1156
Eric Dumazeta8c94862010-10-01 16:15:08 +00001157 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001159 /* Check to see if we resolved a queued list. If so we
1160 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001162 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001164 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001165 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001167 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001168 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001169 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 break;
1171 }
1172 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001173 if (list_empty(&mrt->mfc_unres_queue))
1174 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 spin_unlock_bh(&mfc_unres_lock);
1176
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001177 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001178 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001179 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001181 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return 0;
1183}
1184
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001185/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001186static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001189 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001190 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001191
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001192 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001193 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001194 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1195 continue;
1196 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001198 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001200 /* Wipe the cache */
Patrick McHardy862465f2010-04-13 05:03:21 +00001201 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001202 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001203 if (!all && (c->mfc_flags & MFC_STATIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001205 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001206 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001207 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 }
1210
Patrick McHardy0c122952010-04-13 05:03:22 +00001211 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001213 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001214 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001215 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001216 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218 spin_unlock_bh(&mfc_unres_lock);
1219 }
1220}
1221
Eric Dumazet4c968702010-10-01 16:15:01 +00001222/* called from ip_ra_control(), before an RCU grace period,
1223 * we dont need to call synchronize_rcu() here
1224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225static void mrtsock_destruct(struct sock *sk)
1226{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001227 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001228 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001231 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001232 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001233 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001234 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1235 NETCONFA_IFINDEX_ALL,
1236 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001237 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001238 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241 rtnl_unlock();
1242}
1243
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001244/* Socket options and virtual interface manipulation. The whole
1245 * virtual interface system is a complete heap, but unfortunately
1246 * that's how BSD mrouted happens to think. Maybe one day with a proper
1247 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001249
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001250int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1251 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001253 struct net *net = sock_net(sk);
1254 int val, ret = 0, parent = 0;
1255 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 struct vifctl vif;
1257 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001258 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001259
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001260 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1261 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001262 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001263 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1264 ret = -EOPNOTSUPP;
1265 goto out_unlock;
1266 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001267
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001268 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001269 if (!mrt) {
1270 ret = -ENOENT;
1271 goto out_unlock;
1272 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001273 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001274 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001275 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1276 ret = -EACCES;
1277 goto out_unlock;
1278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
Stephen Hemminger132adf52007-03-08 20:44:43 -08001281 switch (optname) {
1282 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001283 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001284 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001285 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001286 }
1287 if (rtnl_dereference(mrt->mroute_sk)) {
1288 ret = -EADDRINUSE;
1289 break;
1290 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001291
1292 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1293 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001294 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001295 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001296 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1297 NETCONFA_IFINDEX_ALL,
1298 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001299 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001300 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001301 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001302 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1303 ret = -EACCES;
1304 } else {
1305 /* We need to unlock here because mrtsock_destruct takes
1306 * care of rtnl itself and we can't change that due to
1307 * the IP_ROUTER_ALERT setsockopt which runs without it.
1308 */
1309 rtnl_unlock();
1310 ret = ip_ra_control(sk, 0, NULL);
1311 goto out;
1312 }
1313 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001314 case MRT_ADD_VIF:
1315 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001316 if (optlen != sizeof(vif)) {
1317 ret = -EINVAL;
1318 break;
1319 }
1320 if (copy_from_user(&vif, optval, sizeof(vif))) {
1321 ret = -EFAULT;
1322 break;
1323 }
1324 if (vif.vifc_vifi >= MAXVIFS) {
1325 ret = -ENFILE;
1326 break;
1327 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001328 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001329 ret = vif_add(net, mrt, &vif,
1330 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001331 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001332 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001333 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001334 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001335 /* Manipulate the forwarding caches. These live
1336 * in a sort of kernel/user symbiosis.
1337 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001338 case MRT_ADD_MFC:
1339 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001340 parent = -1;
1341 case MRT_ADD_MFC_PROXY:
1342 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001343 if (optlen != sizeof(mfc)) {
1344 ret = -EINVAL;
1345 break;
1346 }
1347 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1348 ret = -EFAULT;
1349 break;
1350 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001351 if (parent == 0)
1352 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001353 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1354 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001355 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001356 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001357 sk == rtnl_dereference(mrt->mroute_sk),
1358 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001359 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001360 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001362 if (optlen != sizeof(val)) {
1363 ret = -EINVAL;
1364 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001366 if (get_user(val, (int __user *)optval)) {
1367 ret = -EFAULT;
1368 break;
1369 }
1370 mrt->mroute_do_assert = val;
1371 break;
1372 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001373 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001374 ret = -ENOPROTOOPT;
1375 break;
1376 }
1377 if (optlen != sizeof(val)) {
1378 ret = -EINVAL;
1379 break;
1380 }
1381 if (get_user(val, (int __user *)optval)) {
1382 ret = -EFAULT;
1383 break;
1384 }
1385
1386 val = !!val;
1387 if (val != mrt->mroute_do_pim) {
1388 mrt->mroute_do_pim = val;
1389 mrt->mroute_do_assert = val;
1390 }
1391 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001392 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001393 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1394 ret = -ENOPROTOOPT;
1395 break;
1396 }
1397 if (optlen != sizeof(uval)) {
1398 ret = -EINVAL;
1399 break;
1400 }
1401 if (get_user(uval, (u32 __user *)optval)) {
1402 ret = -EFAULT;
1403 break;
1404 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001405
Eric Dumazet4c968702010-10-01 16:15:01 +00001406 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1407 ret = -EBUSY;
1408 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001409 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001410 if (IS_ERR(mrt))
1411 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001412 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001413 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c968702010-10-01 16:15:01 +00001414 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001415 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001416 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001417 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001418 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001420out_unlock:
1421 rtnl_unlock();
1422out:
1423 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001426/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001427int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 int olr;
1430 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001431 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001432 struct mr_table *mrt;
1433
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001434 if (sk->sk_type != SOCK_RAW ||
1435 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1436 return -EOPNOTSUPP;
1437
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001438 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001439 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001440 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001442 switch (optname) {
1443 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001444 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001445 break;
1446 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001447 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001448 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001449 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001450 break;
1451 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001452 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001453 break;
1454 default:
1455 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001456 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001457
1458 if (get_user(olr, optlen))
1459 return -EFAULT;
1460 olr = min_t(unsigned int, olr, sizeof(int));
1461 if (olr < 0)
1462 return -EINVAL;
1463 if (put_user(olr, optlen))
1464 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001465 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return -EFAULT;
1467 return 0;
1468}
1469
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001470/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1472{
1473 struct sioc_sg_req sr;
1474 struct sioc_vif_req vr;
1475 struct vif_device *vif;
1476 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001477 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001478 struct mr_table *mrt;
1479
1480 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001481 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001482 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001483
Stephen Hemminger132adf52007-03-08 20:44:43 -08001484 switch (cmd) {
1485 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001486 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001487 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001488 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001489 return -EINVAL;
1490 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001491 vif = &mrt->vif_table[vr.vifi];
1492 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001493 vr.icount = vif->pkt_in;
1494 vr.ocount = vif->pkt_out;
1495 vr.ibytes = vif->bytes_in;
1496 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498
Jianjun Kongc354e122008-11-03 00:28:02 -08001499 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001501 return 0;
1502 }
1503 read_unlock(&mrt_lock);
1504 return -EADDRNOTAVAIL;
1505 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001506 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001507 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Eric Dumazeta8c94862010-10-01 16:15:08 +00001509 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001510 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001511 if (c) {
1512 sr.pktcnt = c->mfc_un.res.pkt;
1513 sr.bytecnt = c->mfc_un.res.bytes;
1514 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001515 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001516
Jianjun Kongc354e122008-11-03 00:28:02 -08001517 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001518 return -EFAULT;
1519 return 0;
1520 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001521 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522 return -EADDRNOTAVAIL;
1523 default:
1524 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526}
1527
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001528#ifdef CONFIG_COMPAT
1529struct compat_sioc_sg_req {
1530 struct in_addr src;
1531 struct in_addr grp;
1532 compat_ulong_t pktcnt;
1533 compat_ulong_t bytecnt;
1534 compat_ulong_t wrong_if;
1535};
1536
David S. Millerca6b8bb02011-02-03 17:24:28 -08001537struct compat_sioc_vif_req {
1538 vifi_t vifi; /* Which iface */
1539 compat_ulong_t icount;
1540 compat_ulong_t ocount;
1541 compat_ulong_t ibytes;
1542 compat_ulong_t obytes;
1543};
1544
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001545int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1546{
David S. Miller0033d5a2011-02-03 17:21:31 -08001547 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001548 struct compat_sioc_vif_req vr;
1549 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001550 struct mfc_cache *c;
1551 struct net *net = sock_net(sk);
1552 struct mr_table *mrt;
1553
1554 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001555 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001556 return -ENOENT;
1557
1558 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001559 case SIOCGETVIFCNT:
1560 if (copy_from_user(&vr, arg, sizeof(vr)))
1561 return -EFAULT;
1562 if (vr.vifi >= mrt->maxvif)
1563 return -EINVAL;
1564 read_lock(&mrt_lock);
1565 vif = &mrt->vif_table[vr.vifi];
1566 if (VIF_EXISTS(mrt, vr.vifi)) {
1567 vr.icount = vif->pkt_in;
1568 vr.ocount = vif->pkt_out;
1569 vr.ibytes = vif->bytes_in;
1570 vr.obytes = vif->bytes_out;
1571 read_unlock(&mrt_lock);
1572
1573 if (copy_to_user(arg, &vr, sizeof(vr)))
1574 return -EFAULT;
1575 return 0;
1576 }
1577 read_unlock(&mrt_lock);
1578 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001579 case SIOCGETSGCNT:
1580 if (copy_from_user(&sr, arg, sizeof(sr)))
1581 return -EFAULT;
1582
1583 rcu_read_lock();
1584 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1585 if (c) {
1586 sr.pktcnt = c->mfc_un.res.pkt;
1587 sr.bytecnt = c->mfc_un.res.bytes;
1588 sr.wrong_if = c->mfc_un.res.wrong_if;
1589 rcu_read_unlock();
1590
1591 if (copy_to_user(arg, &sr, sizeof(sr)))
1592 return -EFAULT;
1593 return 0;
1594 }
1595 rcu_read_unlock();
1596 return -EADDRNOTAVAIL;
1597 default:
1598 return -ENOIOCTLCMD;
1599 }
1600}
1601#endif
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1604{
Jiri Pirko351638e2013-05-28 01:30:21 +00001605 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001606 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001607 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 struct vif_device *v;
1609 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (event != NETDEV_UNREGISTER)
1612 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001613
1614 ipmr_for_each_table(mrt, net) {
1615 v = &mrt->vif_table[0];
1616 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1617 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001618 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621 return NOTIFY_DONE;
1622}
1623
Jianjun Kongc354e122008-11-03 00:28:02 -08001624static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 .notifier_call = ipmr_device_event,
1626};
1627
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001628/* Encapsulate a packet by attaching a valid IPIP header to it.
1629 * This avoids tunnel drivers and other mess and gives us the speed so
1630 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001632static void ip_encap(struct net *net, struct sk_buff *skb,
1633 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001635 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001636 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001637
1638 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001639 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001640 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001641 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001643 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001644 iph->tos = old_iph->tos;
1645 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 iph->frag_off = 0;
1647 iph->daddr = daddr;
1648 iph->saddr = saddr;
1649 iph->protocol = IPPROTO_IPIP;
1650 iph->ihl = 5;
1651 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001652 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 ip_send_check(iph);
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1656 nf_reset(skb);
1657}
1658
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001659static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1660 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001662 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
David S. Miller73186df2015-11-03 13:41:45 -05001664 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1665 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 if (unlikely(opt->optlen))
1668 ip_forward_options(skb);
1669
Eric W. Biederman13206b62015-10-07 16:48:35 -05001670 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001673/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Patrick McHardy0c122952010-04-13 05:03:22 +00001675static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1676 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001678 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001679 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct net_device *dev;
1681 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001682 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int encap = 0;
1684
Ian Morris51456b22015-04-03 09:17:26 +01001685 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 goto out_free;
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (vif->flags & VIFF_REGISTER) {
1689 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001690 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001691 vif->dev->stats.tx_bytes += skb->len;
1692 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001693 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001694 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001697 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001698 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001699 vif->remote, vif->local,
1700 0, 0,
1701 IPPROTO_IPIP,
1702 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001703 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 goto out_free;
1705 encap = sizeof(struct iphdr);
1706 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001707 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001708 0, 0,
1709 IPPROTO_IPIP,
1710 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001711 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 goto out_free;
1713 }
1714
Changli Gaod8d1f302010-06-10 23:31:35 -07001715 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Changli Gaod8d1f302010-06-10 23:31:35 -07001717 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001719 * allow to send ICMP, so that packets will disappear
1720 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
David S. Miller73186df2015-11-03 13:41:45 -05001722 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 ip_rt_put(rt);
1724 goto out_free;
1725 }
1726
Changli Gaod8d1f302010-06-10 23:31:35 -07001727 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001730 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto out_free;
1732 }
1733
1734 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001735 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Eric Dumazetadf30902009-06-02 05:19:30 +00001737 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001738 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001739 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001742 * What do we do with netfilter? -- RR
1743 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001745 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001747 vif->dev->stats.tx_packets++;
1748 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750
1751 IPCB(skb)->flags |= IPSKB_FORWARDED;
1752
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001753 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 * not only before forwarding, but after forwarding on all output
1755 * interfaces. It is clear, if mrouter runs a multicasting
1756 * program, it should receive packets not depending to what interface
1757 * program is joined.
1758 * If we will not make it, the program will have to join on all
1759 * interfaces. On the other hand, multihoming host (or router, but
1760 * not mrouter) cannot join to more than one interface - it will
1761 * result in receiving multiple packets.
1762 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001763 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1764 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 ipmr_forward_finish);
1766 return;
1767
1768out_free:
1769 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Patrick McHardy0c122952010-04-13 05:03:22 +00001772static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001775
1776 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1777 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 break;
1779 }
1780 return ct;
1781}
1782
1783/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001784static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1785 struct sk_buff *skb, struct mfc_cache *cache,
1786 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 int psend = -1;
1789 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001790 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 vif = cache->mfc_parent;
1793 cache->mfc_un.res.pkt++;
1794 cache->mfc_un.res.bytes += skb->len;
1795
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001796 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001797 struct mfc_cache *cache_proxy;
1798
1799 /* For an (*,G) entry, we only check that the incomming
1800 * interface is part of the static tree.
1801 */
1802 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1803 if (cache_proxy &&
1804 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1805 goto forward;
1806 }
1807
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001808 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001809 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001810 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001812 * Very complicated situation...
1813 *
1814 * The best workaround until routing daemons will be
1815 * fixed is not to redistribute packet, if it was
1816 * send through wrong interface. It means, that
1817 * multicast applications WILL NOT work for
1818 * (S,G), which have default multicast route pointing
1819 * to wrong oif. In any case, it is not a good
1820 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 */
1822 goto dont_forward;
1823 }
1824
1825 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Patrick McHardy0c122952010-04-13 05:03:22 +00001827 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001829 * so that we cannot check that packet arrived on an oif.
1830 * It is bad, but otherwise we would need to move pretty
1831 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001833 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001834 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001835 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1837 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001838 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840 goto dont_forward;
1841 }
1842
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001843forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001844 mrt->vif_table[vif].pkt_in++;
1845 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001847 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001848 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1849 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001850 if (true_vifi >= 0 &&
1851 true_vifi != cache->mfc_parent &&
1852 ip_hdr(skb)->ttl >
1853 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1854 /* It's an (*,*) entry and the packet is not coming from
1855 * the upstream: forward the packet to the upstream
1856 * only.
1857 */
1858 psend = cache->mfc_parent;
1859 goto last_forward;
1860 }
1861 goto dont_forward;
1862 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001863 for (ct = cache->mfc_un.res.maxvif - 1;
1864 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001865 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001866 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1867 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001868 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (psend != -1) {
1870 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001873 ipmr_queue_xmit(net, mrt, skb2, cache,
1874 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001876 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001879last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (psend != -1) {
1881 if (local) {
1882 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001885 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001887 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001888 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890 }
1891
1892dont_forward:
1893 if (!local)
1894 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
David S. Miller417da662011-05-03 19:42:43 -07001897static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001898{
David S. Miller417da662011-05-03 19:42:43 -07001899 struct rtable *rt = skb_rtable(skb);
1900 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001901 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001902 .daddr = iph->daddr,
1903 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001904 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001905 .flowi4_oif = (rt_is_output_route(rt) ?
1906 skb->dev->ifindex : 0),
1907 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001908 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001909 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001910 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001911 };
1912 struct mr_table *mrt;
1913 int err;
1914
David S. Millerda919812011-03-12 02:04:50 -05001915 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001916 if (err)
1917 return ERR_PTR(err);
1918 return mrt;
1919}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001921/* Multicast packets for forwarding arrive here
1922 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924int ip_mr_input(struct sk_buff *skb)
1925{
1926 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001927 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001928 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001929 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001932 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001934 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto dont_forward;
1936
David S. Miller417da662011-05-03 19:42:43 -07001937 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001938 if (IS_ERR(mrt)) {
1939 kfree_skb(skb);
1940 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001943 if (IPCB(skb)->opt.router_alert) {
1944 if (ip_call_ra_chain(skb))
1945 return 0;
1946 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1947 /* IGMPv1 (and broken IGMPv2 implementations sort of
1948 * Cisco IOS <= 11.2(8)) do not put router alert
1949 * option to IGMP packets destined to routable
1950 * groups. It is very bad, because it means
1951 * that we can forward NO IGMP messages.
1952 */
1953 struct sock *mroute_sk;
1954
1955 mroute_sk = rcu_dereference(mrt->mroute_sk);
1956 if (mroute_sk) {
1957 nf_reset(skb);
1958 raw_rcv(mroute_sk, skb);
1959 return 0;
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962 }
1963
Eric Dumazeta8c94862010-10-01 16:15:08 +00001964 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001965 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001966 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001967 int vif = ipmr_find_vif(mrt, skb->dev);
1968
1969 if (vif >= 0)
1970 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1971 vif);
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001974 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01001975 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 int vif;
1977
1978 if (local) {
1979 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1980 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01001981 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 skb = skb2;
1984 }
1985
Eric Dumazeta8c94862010-10-01 16:15:08 +00001986 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001987 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001989 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 read_unlock(&mrt_lock);
1991
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001992 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
1994 read_unlock(&mrt_lock);
1995 kfree_skb(skb);
1996 return -ENODEV;
1997 }
1998
Eric Dumazeta8c94862010-10-01 16:15:08 +00001999 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002000 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 read_unlock(&mrt_lock);
2002
2003 if (local)
2004 return ip_local_deliver(skb);
2005
2006 return 0;
2007
2008dont_forward:
2009 if (local)
2010 return ip_local_deliver(skb);
2011 kfree_skb(skb);
2012 return 0;
2013}
2014
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002015#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002016/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002017int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002018{
2019 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002020 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002021 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002022
2023 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2024 goto drop;
2025
2026 pim = igmp_hdr(skb);
2027
David S. Miller417da662011-05-03 19:42:43 -07002028 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002029 if (IS_ERR(mrt))
2030 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002031 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002032 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2033 goto drop;
2034
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002035 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002036drop:
2037 kfree_skb(skb);
2038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 return 0;
2040}
2041#endif
2042
2043#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002044static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002047 struct net *net = dev_net(skb->dev);
2048 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002050 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 goto drop;
2052
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002053 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002054 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2055 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002056 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002057 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 goto drop;
2059
David S. Miller417da662011-05-03 19:42:43 -07002060 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002061 if (IS_ERR(mrt))
2062 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002063 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002064drop:
2065 kfree_skb(skb);
2066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return 0;
2068}
2069#endif
2070
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002071static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2072 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 int ct;
2075 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002076 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002077 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Nicolas Dichtel74381892010-03-25 23:45:35 +00002079 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002080 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002081 return -ENOENT;
2082
Thomas Graf92a395e2012-06-26 23:36:13 +00002083 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2084 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2085 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Thomas Graf92a395e2012-06-26 23:36:13 +00002087 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2088 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002091 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002092 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2093 nla_nest_cancel(skb, mp_attr);
2094 return -EMSGSIZE;
2095 }
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 nhp->rtnh_flags = 0;
2098 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002099 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 nhp->rtnh_len = sizeof(*nhp);
2101 }
2102 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002103
2104 nla_nest_end(skb, mp_attr);
2105
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002106 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2107 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2108 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002109 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) < 0)
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002110 return -EMSGSIZE;
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 rtm->rtm_type = RTN_MULTICAST;
2113 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
David S. Miller9a1b9492011-05-04 12:18:54 -07002116int ipmr_get_route(struct net *net, struct sk_buff *skb,
2117 __be32 saddr, __be32 daddr,
2118 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002121 struct mr_table *mrt;
2122 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002124 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002125 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002126 return -ENOENT;
2127
Eric Dumazeta8c94862010-10-01 16:15:08 +00002128 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002129 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002130 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002131 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002133 if (vif >= 0)
2134 cache = ipmr_cache_find_any(mrt, daddr, vif);
2135 }
Ian Morris51456b22015-04-03 09:17:26 +01002136 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002137 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002138 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002140 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002143 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return -EAGAIN;
2145 }
2146
2147 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002148 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002149 if (dev)
2150 vif = ipmr_find_vif(mrt, dev);
2151 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002153 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return -ENODEV;
2155 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002156 skb2 = skb_clone(skb, GFP_ATOMIC);
2157 if (!skb2) {
2158 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002159 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002160 return -ENOMEM;
2161 }
2162
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002163 skb_push(skb2, sizeof(struct iphdr));
2164 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002165 iph = ip_hdr(skb2);
2166 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002167 iph->saddr = saddr;
2168 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002169 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002170 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002172 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return err;
2174 }
2175
Eric Dumazeta8c94862010-10-01 16:15:08 +00002176 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002177 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002179 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return err;
2181}
2182
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002183static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002184 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2185 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002186{
2187 struct nlmsghdr *nlh;
2188 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002189 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002190
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002191 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002192 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002193 return -EMSGSIZE;
2194
2195 rtm = nlmsg_data(nlh);
2196 rtm->rtm_family = RTNL_FAMILY_IPMR;
2197 rtm->rtm_dst_len = 32;
2198 rtm->rtm_src_len = 32;
2199 rtm->rtm_tos = 0;
2200 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002201 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2202 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002203 rtm->rtm_type = RTN_MULTICAST;
2204 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002205 if (c->mfc_flags & MFC_STATIC)
2206 rtm->rtm_protocol = RTPROT_STATIC;
2207 else
2208 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002209 rtm->rtm_flags = 0;
2210
Jiri Benc930345e2015-03-29 16:59:25 +02002211 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2212 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002213 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002214 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2215 /* do not break the dump if cache is unresolved */
2216 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002217 goto nla_put_failure;
2218
Johannes Berg053c0952015-01-16 22:09:00 +01002219 nlmsg_end(skb, nlh);
2220 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002221
2222nla_put_failure:
2223 nlmsg_cancel(skb, nlh);
2224 return -EMSGSIZE;
2225}
2226
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002227static size_t mroute_msgsize(bool unresolved, int maxvif)
2228{
2229 size_t len =
2230 NLMSG_ALIGN(sizeof(struct rtmsg))
2231 + nla_total_size(4) /* RTA_TABLE */
2232 + nla_total_size(4) /* RTA_SRC */
2233 + nla_total_size(4) /* RTA_DST */
2234 ;
2235
2236 if (!unresolved)
2237 len = len
2238 + nla_total_size(4) /* RTA_IIF */
2239 + nla_total_size(0) /* RTA_MULTIPATH */
2240 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2241 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002242 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002243 ;
2244
2245 return len;
2246}
2247
2248static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2249 int cmd)
2250{
2251 struct net *net = read_pnet(&mrt->net);
2252 struct sk_buff *skb;
2253 int err = -ENOBUFS;
2254
2255 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2256 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002257 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002258 goto errout;
2259
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002260 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002261 if (err < 0)
2262 goto errout;
2263
2264 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2265 return;
2266
2267errout:
2268 kfree_skb(skb);
2269 if (err < 0)
2270 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2271}
2272
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002273static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2274{
2275 struct net *net = sock_net(skb->sk);
2276 struct mr_table *mrt;
2277 struct mfc_cache *mfc;
2278 unsigned int t = 0, s_t;
2279 unsigned int h = 0, s_h;
2280 unsigned int e = 0, s_e;
2281
2282 s_t = cb->args[0];
2283 s_h = cb->args[1];
2284 s_e = cb->args[2];
2285
Eric Dumazeta8c94862010-10-01 16:15:08 +00002286 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002287 ipmr_for_each_table(mrt, net) {
2288 if (t < s_t)
2289 goto next_table;
2290 if (t > s_t)
2291 s_h = 0;
2292 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002293 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002294 if (e < s_e)
2295 goto next_entry;
2296 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002297 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002298 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002299 mfc, RTM_NEWROUTE,
2300 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002301 goto done;
2302next_entry:
2303 e++;
2304 }
2305 e = s_e = 0;
2306 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002307 spin_lock_bh(&mfc_unres_lock);
2308 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2309 if (e < s_e)
2310 goto next_entry2;
2311 if (ipmr_fill_mroute(mrt, skb,
2312 NETLINK_CB(cb->skb).portid,
2313 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002314 mfc, RTM_NEWROUTE,
2315 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002316 spin_unlock_bh(&mfc_unres_lock);
2317 goto done;
2318 }
2319next_entry2:
2320 e++;
2321 }
2322 spin_unlock_bh(&mfc_unres_lock);
2323 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002324 s_h = 0;
2325next_table:
2326 t++;
2327 }
2328done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002329 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002330
2331 cb->args[2] = e;
2332 cb->args[1] = h;
2333 cb->args[0] = t;
2334
2335 return skb->len;
2336}
2337
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002338static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2339 [RTA_SRC] = { .type = NLA_U32 },
2340 [RTA_DST] = { .type = NLA_U32 },
2341 [RTA_IIF] = { .type = NLA_U32 },
2342 [RTA_TABLE] = { .type = NLA_U32 },
2343 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2344};
2345
2346static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2347{
2348 switch (rtm_protocol) {
2349 case RTPROT_STATIC:
2350 case RTPROT_MROUTED:
2351 return true;
2352 }
2353 return false;
2354}
2355
2356static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2357{
2358 struct rtnexthop *rtnh = nla_data(nla);
2359 int remaining = nla_len(nla), vifi = 0;
2360
2361 while (rtnh_ok(rtnh, remaining)) {
2362 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2363 if (++vifi == MAXVIFS)
2364 break;
2365 rtnh = rtnh_next(rtnh, &remaining);
2366 }
2367
2368 return remaining > 0 ? -EINVAL : vifi;
2369}
2370
2371/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2372static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2373 struct mfcctl *mfcc, int *mrtsock,
2374 struct mr_table **mrtret)
2375{
2376 struct net_device *dev = NULL;
2377 u32 tblid = RT_TABLE_DEFAULT;
2378 struct mr_table *mrt;
2379 struct nlattr *attr;
2380 struct rtmsg *rtm;
2381 int ret, rem;
2382
2383 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy);
2384 if (ret < 0)
2385 goto out;
2386 rtm = nlmsg_data(nlh);
2387
2388 ret = -EINVAL;
2389 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2390 rtm->rtm_type != RTN_MULTICAST ||
2391 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2392 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2393 goto out;
2394
2395 memset(mfcc, 0, sizeof(*mfcc));
2396 mfcc->mfcc_parent = -1;
2397 ret = 0;
2398 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2399 switch (nla_type(attr)) {
2400 case RTA_SRC:
2401 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2402 break;
2403 case RTA_DST:
2404 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2405 break;
2406 case RTA_IIF:
2407 dev = __dev_get_by_index(net, nla_get_u32(attr));
2408 if (!dev) {
2409 ret = -ENODEV;
2410 goto out;
2411 }
2412 break;
2413 case RTA_MULTIPATH:
2414 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2415 ret = -EINVAL;
2416 goto out;
2417 }
2418 break;
2419 case RTA_PREFSRC:
2420 ret = 1;
2421 break;
2422 case RTA_TABLE:
2423 tblid = nla_get_u32(attr);
2424 break;
2425 }
2426 }
2427 mrt = ipmr_get_table(net, tblid);
2428 if (!mrt) {
2429 ret = -ENOENT;
2430 goto out;
2431 }
2432 *mrtret = mrt;
2433 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2434 if (dev)
2435 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2436
2437out:
2438 return ret;
2439}
2440
2441/* takes care of both newroute and delroute */
2442static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh)
2443{
2444 struct net *net = sock_net(skb->sk);
2445 int ret, mrtsock, parent;
2446 struct mr_table *tbl;
2447 struct mfcctl mfcc;
2448
2449 mrtsock = 0;
2450 tbl = NULL;
2451 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl);
2452 if (ret < 0)
2453 return ret;
2454
2455 parent = ret ? mfcc.mfcc_parent : -1;
2456 if (nlh->nlmsg_type == RTM_NEWROUTE)
2457 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2458 else
2459 return ipmr_mfc_delete(tbl, &mfcc, parent);
2460}
2461
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002462#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002463/* The /proc interfaces to multicast routing :
2464 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 */
2466struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002467 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002468 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 int ct;
2470};
2471
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002472static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2473 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 loff_t pos)
2475{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002476 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002477
2478 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2479 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002481 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002482 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484 return NULL;
2485}
2486
2487static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002488 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002490 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002491 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002492 struct mr_table *mrt;
2493
2494 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002495 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002496 return ERR_PTR(-ENOENT);
2497
2498 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002501 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 : SEQ_START_TOKEN;
2503}
2504
2505static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2506{
2507 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002508 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002509 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 ++*pos;
2512 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002513 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002514
Patrick McHardy0c122952010-04-13 05:03:22 +00002515 while (++iter->ct < mrt->maxvif) {
2516 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002518 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520 return NULL;
2521}
2522
2523static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002524 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 read_unlock(&mrt_lock);
2527}
2528
2529static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2530{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002531 struct ipmr_vif_iter *iter = seq->private;
2532 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002535 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2537 } else {
2538 const struct vif_device *vif = v;
2539 const char *name = vif->dev ? vif->dev->name : "none";
2540
2541 seq_printf(seq,
2542 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002543 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002544 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 vif->bytes_out, vif->pkt_out,
2546 vif->flags, vif->local, vif->remote);
2547 }
2548 return 0;
2549}
2550
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002551static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 .start = ipmr_vif_seq_start,
2553 .next = ipmr_vif_seq_next,
2554 .stop = ipmr_vif_seq_stop,
2555 .show = ipmr_vif_seq_show,
2556};
2557
2558static int ipmr_vif_open(struct inode *inode, struct file *file)
2559{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002560 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2561 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Arjan van de Ven9a321442007-02-12 00:55:35 -08002564static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 .owner = THIS_MODULE,
2566 .open = ipmr_vif_open,
2567 .read = seq_read,
2568 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002569 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570};
2571
2572struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002573 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002574 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002575 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 int ct;
2577};
2578
2579
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002580static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2581 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002583 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 struct mfc_cache *mfc;
2585
Eric Dumazeta8c94862010-10-01 16:15:08 +00002586 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002587 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002588 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002589 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002590 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002592 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002593 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002596 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002597 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002598 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 return mfc;
2600 spin_unlock_bh(&mfc_unres_lock);
2601
2602 it->cache = NULL;
2603 return NULL;
2604}
2605
2606
2607static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2608{
2609 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002610 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002611 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002612
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002613 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002614 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002615 return ERR_PTR(-ENOENT);
2616
2617 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 it->cache = NULL;
2619 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002620 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 : SEQ_START_TOKEN;
2622}
2623
2624static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2625{
2626 struct mfc_cache *mfc = v;
2627 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002628 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002629 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 ++*pos;
2632
2633 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002634 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Patrick McHardy862465f2010-04-13 05:03:21 +00002636 if (mfc->list.next != it->cache)
2637 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002638
Patrick McHardy0c122952010-04-13 05:03:22 +00002639 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 goto end_of_list;
2641
Patrick McHardy0c122952010-04-13 05:03:22 +00002642 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002645 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002646 if (list_empty(it->cache))
2647 continue;
2648 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
2650
2651 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002652 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002653 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002657 if (!list_empty(it->cache))
2658 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002660end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 spin_unlock_bh(&mfc_unres_lock);
2662 it->cache = NULL;
2663
2664 return NULL;
2665}
2666
2667static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2668{
2669 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002670 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Patrick McHardy0c122952010-04-13 05:03:22 +00002672 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002674 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002675 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
2678static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2679{
2680 int n;
2681
2682 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002683 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2685 } else {
2686 const struct mfc_cache *mfc = v;
2687 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002688 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002689
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002690 seq_printf(seq, "%08X %08X %-3hd",
2691 (__force u32) mfc->mfc_mcastgrp,
2692 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002693 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Patrick McHardy0c122952010-04-13 05:03:22 +00002695 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002696 seq_printf(seq, " %8lu %8lu %8lu",
2697 mfc->mfc_un.res.pkt,
2698 mfc->mfc_un.res.bytes,
2699 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002700 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002701 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002702 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002703 mfc->mfc_un.res.ttls[n] < 255)
2704 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002705 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 n, mfc->mfc_un.res.ttls[n]);
2707 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002708 } else {
2709 /* unresolved mfc_caches don't contain
2710 * pkt, bytes and wrong_if values
2711 */
2712 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
2714 seq_putc(seq, '\n');
2715 }
2716 return 0;
2717}
2718
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002719static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 .start = ipmr_mfc_seq_start,
2721 .next = ipmr_mfc_seq_next,
2722 .stop = ipmr_mfc_seq_stop,
2723 .show = ipmr_mfc_seq_show,
2724};
2725
2726static int ipmr_mfc_open(struct inode *inode, struct file *file)
2727{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002728 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2729 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Arjan van de Ven9a321442007-02-12 00:55:35 -08002732static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 .owner = THIS_MODULE,
2734 .open = ipmr_mfc_open,
2735 .read = seq_read,
2736 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002737 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002739#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002742static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002744 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745};
2746#endif
2747
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002748/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002749static int __net_init ipmr_net_init(struct net *net)
2750{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002751 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002752
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002753 err = ipmr_rules_init(net);
2754 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002755 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002756
2757#ifdef CONFIG_PROC_FS
2758 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002759 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002760 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002761 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002762 goto proc_cache_fail;
2763#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002764 return 0;
2765
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002766#ifdef CONFIG_PROC_FS
2767proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002768 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002769proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002770 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002771#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002772fail:
2773 return err;
2774}
2775
2776static void __net_exit ipmr_net_exit(struct net *net)
2777{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002778#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002779 remove_proc_entry("ip_mr_cache", net->proc_net);
2780 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002781#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002782 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002783}
2784
2785static struct pernet_operations ipmr_net_ops = {
2786 .init = ipmr_net_init,
2787 .exit = ipmr_net_exit,
2788};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002789
Wang Chen03d2f892008-07-03 12:13:36 +08002790int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Wang Chen03d2f892008-07-03 12:13:36 +08002792 int err;
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2795 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002796 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002797 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002798
Benjamin Therycf958ae32009-01-22 04:56:16 +00002799 err = register_pernet_subsys(&ipmr_net_ops);
2800 if (err)
2801 goto reg_pernet_fail;
2802
Wang Chen03d2f892008-07-03 12:13:36 +08002803 err = register_netdevice_notifier(&ip_mr_notifier);
2804 if (err)
2805 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002806#ifdef CONFIG_IP_PIMSM_V2
2807 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002808 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002809 err = -EAGAIN;
2810 goto add_proto_fail;
2811 }
2812#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002813 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2814 NULL, ipmr_rtm_dumproute, NULL);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002815 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
2816 ipmr_rtm_route, NULL, NULL);
2817 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
2818 ipmr_rtm_route, NULL, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002819 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002820
Tom Goff403dbb92009-06-14 03:16:13 -07002821#ifdef CONFIG_IP_PIMSM_V2
2822add_proto_fail:
2823 unregister_netdevice_notifier(&ip_mr_notifier);
2824#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002825reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002826 unregister_pernet_subsys(&ipmr_net_ops);
2827reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002828 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002829 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}