blob: 26253328d227bda8e3a6b23be3130a4ce4231b60 [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 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200725 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Patrick McHardy0c122952010-04-13 05:03:22 +0000728static int vif_add(struct net *net, struct mr_table *mrt,
729 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000732 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct net_device *dev;
734 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700735 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000738 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -EADDRINUSE;
740
741 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100743 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100744 return -EINVAL;
745 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * All the packets will be sent to the daemon
747 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000748 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000750 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!dev)
752 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700753 err = dev_set_allmulti(dev, 1);
754 if (err) {
755 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700756 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700757 return err;
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900760 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000761 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (!dev)
763 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700764 err = dev_set_allmulti(dev, 1);
765 if (err) {
766 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700767 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700768 return err;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000771 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000773 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
774 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100775 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000776 dev_put(dev);
777 return -EADDRNOTAVAIL;
778 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000779 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000780 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (!dev)
783 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700784 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700785 if (err) {
786 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700787 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 break;
790 default:
791 return -EINVAL;
792 }
793
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000794 in_dev = __in_dev_get_rtnl(dev);
795 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000796 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000798 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700799 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000800 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
801 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 ip_rt_multicast_event(in_dev);
803
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000804 /* Fill in the VIF structures */
805
Jianjun Kongc354e122008-11-03 00:28:02 -0800806 v->rate_limit = vifc->vifc_rate_limit;
807 v->local = vifc->vifc_lcl_addr.s_addr;
808 v->remote = vifc->vifc_rmt_addr.s_addr;
809 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (!mrtsock)
811 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800812 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 v->bytes_in = 0;
814 v->bytes_out = 0;
815 v->pkt_in = 0;
816 v->pkt_out = 0;
817 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000818 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200819 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* And finish update writing critical data */
822 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800823 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000824 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000825 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000826 if (vifi+1 > mrt->maxvif)
827 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 write_unlock_bh(&mrt_lock);
829 return 0;
830}
831
Eric Dumazeta8c94862010-10-01 16:15:08 +0000832/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000833static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000834 __be32 origin,
835 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Jianjun Kongc354e122008-11-03 00:28:02 -0800837 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct mfc_cache *c;
839
Eric Dumazeta8c94862010-10-01 16:15:08 +0000840 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000841 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
842 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000844 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000847/* Look for a (*,*,oif) entry */
848static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
849 int vifi)
850{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100851 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000852 struct mfc_cache *c;
853
854 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100855 if (c->mfc_origin == htonl(INADDR_ANY) &&
856 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000857 c->mfc_un.res.ttls[vifi] < 255)
858 return c;
859
860 return NULL;
861}
862
863/* Look for a (*,G) entry */
864static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
865 __be32 mcastgrp, int vifi)
866{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100867 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000868 struct mfc_cache *c, *proxy;
869
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100870 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000871 goto skip;
872
873 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100874 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000875 c->mfc_mcastgrp == mcastgrp) {
876 if (c->mfc_un.res.ttls[vifi] < 255)
877 return c;
878
879 /* It's ok if the vifi is part of the static tree */
880 proxy = ipmr_cache_find_any_parent(mrt,
881 c->mfc_parent);
882 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
883 return c;
884 }
885
886skip:
887 return ipmr_cache_find_any_parent(mrt, vifi);
888}
889
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100890/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000891static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Jianjun Kongc354e122008-11-03 00:28:02 -0800893 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000894
Tom Goff70a0dec2016-06-23 16:11:57 -0400895 if (c) {
896 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
Eric Dumazeta8c94862010-10-01 16:15:08 +0000897 c->mfc_un.res.minvif = MAXVIFS;
Tom Goff70a0dec2016-06-23 16:11:57 -0400898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return c;
900}
901
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000902static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Jianjun Kongc354e122008-11-03 00:28:02 -0800904 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000905
906 if (c) {
907 skb_queue_head_init(&c->mfc_un.unres.unresolved);
908 c->mfc_un.unres.expires = jiffies + 10*HZ;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return c;
911}
912
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100913/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000914static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
915 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700918 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000920 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800921 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700922 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
924
Hong zhi guo573ce262013-03-27 06:47:04 +0000925 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000926 nlh->nlmsg_len = skb_tail_pointer(skb) -
927 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 } else {
929 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000930 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000932 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700933 e->error = -EMSGSIZE;
934 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Thomas Graf2942e902006-08-15 00:30:25 -0700936
Eric W. Biederman15e47302012-09-07 20:12:54 +0000937 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000938 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000939 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942}
943
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100944/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
945 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100947 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000949static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000950 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300952 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100953 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct igmphdr *igmp;
955 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100956 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int ret;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (assert == IGMPMSG_WHOLEPKT)
960 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
961 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 skb = alloc_skb(128, GFP_ATOMIC);
963
Stephen Hemminger132adf52007-03-08 20:44:43 -0800964 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return -ENOBUFS;
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (assert == IGMPMSG_WHOLEPKT) {
968 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000969 * Duplicate old header, fix ihl, length etc.
970 * And all this only to mangle msg->im_msgtype and
971 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300973 skb_push(skb, sizeof(struct iphdr));
974 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300975 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300976 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700977 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 msg->im_msgtype = IGMPMSG_WHOLEPKT;
979 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000980 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700981 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
982 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
983 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100984 } else {
985 /* Copy the IP header */
986 skb_set_network_header(skb, skb->len);
987 skb_put(skb, ihl);
988 skb_copy_to_linear_data(skb, pkt->data, ihl);
989 /* Flag to the kernel this is a route add */
990 ip_hdr(skb)->protocol = 0;
991 msg = (struct igmpmsg *)skb_network_header(skb);
992 msg->im_vif = vifi;
993 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
994 /* Add our header */
995 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
996 igmp->type = assert;
997 msg->im_msgtype = assert;
998 igmp->code = 0;
999 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1000 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric Dumazet4c9687092010-10-01 16:15:01 +00001003 rcu_read_lock();
1004 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001005 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001006 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 kfree_skb(skb);
1008 return -EINVAL;
1009 }
1010
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001011 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001012 ret = sock_queue_rcv_skb(mroute_sk, skb);
1013 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001014 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001015 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 kfree_skb(skb);
1017 }
1018
1019 return ret;
1020}
1021
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001022/* Queue a packet for resolution. It gets locked cache entry! */
1023static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1024 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Patrick McHardy862465f2010-04-13 05:03:21 +00001026 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int err;
1028 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001029 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001032 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001033 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001034 c->mfc_origin == iph->saddr) {
1035 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
Patrick McHardy862465f2010-04-13 05:03:21 +00001040 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001041 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001042 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001043 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 spin_unlock_bh(&mfc_unres_lock);
1045
1046 kfree_skb(skb);
1047 return -ENOBUFS;
1048 }
1049
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001050 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001051 c->mfc_parent = -1;
1052 c->mfc_origin = iph->saddr;
1053 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001055 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001056 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001057 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001058 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 out - Brad Parker
1060 */
1061 spin_unlock_bh(&mfc_unres_lock);
1062
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001063 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 kfree_skb(skb);
1065 return err;
1066 }
1067
Patrick McHardy0c122952010-04-13 05:03:22 +00001068 atomic_inc(&mrt->cache_resolve_queue_len);
1069 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001070 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
David S. Miller278554b2010-05-12 00:05:35 -07001072 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1073 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001076 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001077 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 kfree_skb(skb);
1079 err = -ENOBUFS;
1080 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001081 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 err = 0;
1083 }
1084
1085 spin_unlock_bh(&mfc_unres_lock);
1086 return err;
1087}
1088
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001089/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001091static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001094 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jianjun Kongc354e122008-11-03 00:28:02 -08001096 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Patrick McHardy0c122952010-04-13 05:03:22 +00001098 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001100 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1101 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001102 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001103 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001104 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return 0;
1106 }
1107 }
1108 return -ENOENT;
1109}
1110
Patrick McHardy0c122952010-04-13 05:03:22 +00001111static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001112 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Patrick McHardy862465f2010-04-13 05:03:21 +00001114 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001116 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Patrick McHardya50436f22010-03-17 06:04:14 +00001118 if (mfc->mfcc_parent >= MAXVIFS)
1119 return -ENFILE;
1120
Jianjun Kongc354e122008-11-03 00:28:02 -08001121 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Patrick McHardy0c122952010-04-13 05:03:22 +00001123 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001125 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1126 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001127 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
Patrick McHardy862465f2010-04-13 05:03:21 +00001132 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 write_lock_bh(&mrt_lock);
1134 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001135 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (!mrtsock)
1137 c->mfc_flags |= MFC_STATIC;
1138 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001139 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return 0;
1141 }
1142
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001143 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001144 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return -EINVAL;
1146
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001147 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001148 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return -ENOMEM;
1150
Jianjun Kongc354e122008-11-03 00:28:02 -08001151 c->mfc_origin = mfc->mfcc_origin.s_addr;
1152 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1153 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001154 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (!mrtsock)
1156 c->mfc_flags |= MFC_STATIC;
1157
Eric Dumazeta8c94862010-10-01 16:15:08 +00001158 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001160 /* Check to see if we resolved a queued list. If so we
1161 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001163 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001165 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001166 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001168 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001169 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001170 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 break;
1172 }
1173 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001174 if (list_empty(&mrt->mfc_unres_queue))
1175 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 spin_unlock_bh(&mfc_unres_lock);
1177
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001178 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001179 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001180 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001182 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return 0;
1184}
1185
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001186/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001187static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001190 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001191 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001192
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001193 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001194 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001195 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1196 continue;
1197 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001199 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001201 /* Wipe the cache */
Patrick McHardy862465f2010-04-13 05:03:21 +00001202 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001203 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001204 if (!all && (c->mfc_flags & MFC_STATIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001206 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001207 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001208 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210 }
1211
Patrick McHardy0c122952010-04-13 05:03:22 +00001212 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001214 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001215 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001216 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001217 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 spin_unlock_bh(&mfc_unres_lock);
1220 }
1221}
1222
Eric Dumazet4c9687092010-10-01 16:15:01 +00001223/* called from ip_ra_control(), before an RCU grace period,
1224 * we dont need to call synchronize_rcu() here
1225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226static void mrtsock_destruct(struct sock *sk)
1227{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001228 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001229 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001232 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001233 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001234 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001235 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1236 NETCONFA_IFINDEX_ALL,
1237 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001238 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001239 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242 rtnl_unlock();
1243}
1244
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001245/* Socket options and virtual interface manipulation. The whole
1246 * virtual interface system is a complete heap, but unfortunately
1247 * that's how BSD mrouted happens to think. Maybe one day with a proper
1248 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001250
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001251int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1252 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001254 struct net *net = sock_net(sk);
1255 int val, ret = 0, parent = 0;
1256 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct vifctl vif;
1258 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001259 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001260
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001261 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1262 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001263 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001264 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1265 ret = -EOPNOTSUPP;
1266 goto out_unlock;
1267 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001268
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001269 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001270 if (!mrt) {
1271 ret = -ENOENT;
1272 goto out_unlock;
1273 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001274 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001275 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001276 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1277 ret = -EACCES;
1278 goto out_unlock;
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
Stephen Hemminger132adf52007-03-08 20:44:43 -08001282 switch (optname) {
1283 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001284 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001285 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001286 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001287 }
1288 if (rtnl_dereference(mrt->mroute_sk)) {
1289 ret = -EADDRINUSE;
1290 break;
1291 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001292
1293 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1294 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001295 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001296 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001297 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1298 NETCONFA_IFINDEX_ALL,
1299 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001300 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001301 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001302 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001303 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1304 ret = -EACCES;
1305 } else {
1306 /* We need to unlock here because mrtsock_destruct takes
1307 * care of rtnl itself and we can't change that due to
1308 * the IP_ROUTER_ALERT setsockopt which runs without it.
1309 */
1310 rtnl_unlock();
1311 ret = ip_ra_control(sk, 0, NULL);
1312 goto out;
1313 }
1314 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001315 case MRT_ADD_VIF:
1316 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001317 if (optlen != sizeof(vif)) {
1318 ret = -EINVAL;
1319 break;
1320 }
1321 if (copy_from_user(&vif, optval, sizeof(vif))) {
1322 ret = -EFAULT;
1323 break;
1324 }
1325 if (vif.vifc_vifi >= MAXVIFS) {
1326 ret = -ENFILE;
1327 break;
1328 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001329 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001330 ret = vif_add(net, mrt, &vif,
1331 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001332 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001333 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001334 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001335 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001336 /* Manipulate the forwarding caches. These live
1337 * in a sort of kernel/user symbiosis.
1338 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 case MRT_ADD_MFC:
1340 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001341 parent = -1;
1342 case MRT_ADD_MFC_PROXY:
1343 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001344 if (optlen != sizeof(mfc)) {
1345 ret = -EINVAL;
1346 break;
1347 }
1348 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1349 ret = -EFAULT;
1350 break;
1351 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001352 if (parent == 0)
1353 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001354 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1355 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001356 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001357 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001358 sk == rtnl_dereference(mrt->mroute_sk),
1359 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001360 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001361 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001362 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001363 if (optlen != sizeof(val)) {
1364 ret = -EINVAL;
1365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001367 if (get_user(val, (int __user *)optval)) {
1368 ret = -EFAULT;
1369 break;
1370 }
1371 mrt->mroute_do_assert = val;
1372 break;
1373 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001374 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001375 ret = -ENOPROTOOPT;
1376 break;
1377 }
1378 if (optlen != sizeof(val)) {
1379 ret = -EINVAL;
1380 break;
1381 }
1382 if (get_user(val, (int __user *)optval)) {
1383 ret = -EFAULT;
1384 break;
1385 }
1386
1387 val = !!val;
1388 if (val != mrt->mroute_do_pim) {
1389 mrt->mroute_do_pim = val;
1390 mrt->mroute_do_assert = val;
1391 }
1392 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001393 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001394 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1395 ret = -ENOPROTOOPT;
1396 break;
1397 }
1398 if (optlen != sizeof(uval)) {
1399 ret = -EINVAL;
1400 break;
1401 }
1402 if (get_user(uval, (u32 __user *)optval)) {
1403 ret = -EFAULT;
1404 break;
1405 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001406
Eric Dumazet4c9687092010-10-01 16:15:01 +00001407 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1408 ret = -EBUSY;
1409 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001410 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001411 if (IS_ERR(mrt))
1412 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001413 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001414 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001415 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001416 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001417 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001418 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001419 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001421out_unlock:
1422 rtnl_unlock();
1423out:
1424 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001427/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001428int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 int olr;
1431 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001432 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001433 struct mr_table *mrt;
1434
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001435 if (sk->sk_type != SOCK_RAW ||
1436 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1437 return -EOPNOTSUPP;
1438
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001439 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001440 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001441 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001443 switch (optname) {
1444 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001445 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001446 break;
1447 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001448 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001449 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001450 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001451 break;
1452 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001453 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001454 break;
1455 default:
1456 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001457 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001458
1459 if (get_user(olr, optlen))
1460 return -EFAULT;
1461 olr = min_t(unsigned int, olr, sizeof(int));
1462 if (olr < 0)
1463 return -EINVAL;
1464 if (put_user(olr, optlen))
1465 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001466 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return -EFAULT;
1468 return 0;
1469}
1470
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001471/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1473{
1474 struct sioc_sg_req sr;
1475 struct sioc_vif_req vr;
1476 struct vif_device *vif;
1477 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001478 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001479 struct mr_table *mrt;
1480
1481 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001482 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001483 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001484
Stephen Hemminger132adf52007-03-08 20:44:43 -08001485 switch (cmd) {
1486 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001487 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001488 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001489 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001490 return -EINVAL;
1491 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001492 vif = &mrt->vif_table[vr.vifi];
1493 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001494 vr.icount = vif->pkt_in;
1495 vr.ocount = vif->pkt_out;
1496 vr.ibytes = vif->bytes_in;
1497 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001499
Jianjun Kongc354e122008-11-03 00:28:02 -08001500 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001502 return 0;
1503 }
1504 read_unlock(&mrt_lock);
1505 return -EADDRNOTAVAIL;
1506 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001507 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001508 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Eric Dumazeta8c94862010-10-01 16:15:08 +00001510 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001511 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001512 if (c) {
1513 sr.pktcnt = c->mfc_un.res.pkt;
1514 sr.bytecnt = c->mfc_un.res.bytes;
1515 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001516 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001517
Jianjun Kongc354e122008-11-03 00:28:02 -08001518 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001519 return -EFAULT;
1520 return 0;
1521 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001522 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001523 return -EADDRNOTAVAIL;
1524 default:
1525 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527}
1528
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001529#ifdef CONFIG_COMPAT
1530struct compat_sioc_sg_req {
1531 struct in_addr src;
1532 struct in_addr grp;
1533 compat_ulong_t pktcnt;
1534 compat_ulong_t bytecnt;
1535 compat_ulong_t wrong_if;
1536};
1537
David S. Millerca6b8bb02011-02-03 17:24:28 -08001538struct compat_sioc_vif_req {
1539 vifi_t vifi; /* Which iface */
1540 compat_ulong_t icount;
1541 compat_ulong_t ocount;
1542 compat_ulong_t ibytes;
1543 compat_ulong_t obytes;
1544};
1545
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001546int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1547{
David S. Miller0033d5a2011-02-03 17:21:31 -08001548 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001549 struct compat_sioc_vif_req vr;
1550 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001551 struct mfc_cache *c;
1552 struct net *net = sock_net(sk);
1553 struct mr_table *mrt;
1554
1555 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001556 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001557 return -ENOENT;
1558
1559 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001560 case SIOCGETVIFCNT:
1561 if (copy_from_user(&vr, arg, sizeof(vr)))
1562 return -EFAULT;
1563 if (vr.vifi >= mrt->maxvif)
1564 return -EINVAL;
1565 read_lock(&mrt_lock);
1566 vif = &mrt->vif_table[vr.vifi];
1567 if (VIF_EXISTS(mrt, vr.vifi)) {
1568 vr.icount = vif->pkt_in;
1569 vr.ocount = vif->pkt_out;
1570 vr.ibytes = vif->bytes_in;
1571 vr.obytes = vif->bytes_out;
1572 read_unlock(&mrt_lock);
1573
1574 if (copy_to_user(arg, &vr, sizeof(vr)))
1575 return -EFAULT;
1576 return 0;
1577 }
1578 read_unlock(&mrt_lock);
1579 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001580 case SIOCGETSGCNT:
1581 if (copy_from_user(&sr, arg, sizeof(sr)))
1582 return -EFAULT;
1583
1584 rcu_read_lock();
1585 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1586 if (c) {
1587 sr.pktcnt = c->mfc_un.res.pkt;
1588 sr.bytecnt = c->mfc_un.res.bytes;
1589 sr.wrong_if = c->mfc_un.res.wrong_if;
1590 rcu_read_unlock();
1591
1592 if (copy_to_user(arg, &sr, sizeof(sr)))
1593 return -EFAULT;
1594 return 0;
1595 }
1596 rcu_read_unlock();
1597 return -EADDRNOTAVAIL;
1598 default:
1599 return -ENOIOCTLCMD;
1600 }
1601}
1602#endif
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1605{
Jiri Pirko351638e2013-05-28 01:30:21 +00001606 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001607 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001608 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 struct vif_device *v;
1610 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (event != NETDEV_UNREGISTER)
1613 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001614
1615 ipmr_for_each_table(mrt, net) {
1616 v = &mrt->vif_table[0];
1617 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1618 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001619 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622 return NOTIFY_DONE;
1623}
1624
Jianjun Kongc354e122008-11-03 00:28:02 -08001625static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 .notifier_call = ipmr_device_event,
1627};
1628
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001629/* Encapsulate a packet by attaching a valid IPIP header to it.
1630 * This avoids tunnel drivers and other mess and gives us the speed so
1631 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001633static void ip_encap(struct net *net, struct sk_buff *skb,
1634 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001636 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001637 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001638
1639 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001640 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001641 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001642 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001644 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001645 iph->tos = old_iph->tos;
1646 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 iph->frag_off = 0;
1648 iph->daddr = daddr;
1649 iph->saddr = saddr;
1650 iph->protocol = IPPROTO_IPIP;
1651 iph->ihl = 5;
1652 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001653 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 ip_send_check(iph);
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1657 nf_reset(skb);
1658}
1659
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001660static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1661 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001663 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
David S. Miller73186df2015-11-03 13:41:45 -05001665 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1666 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (unlikely(opt->optlen))
1669 ip_forward_options(skb);
1670
Eric W. Biederman13206b62015-10-07 16:48:35 -05001671 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001674/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Patrick McHardy0c122952010-04-13 05:03:22 +00001676static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1677 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001679 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001680 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 struct net_device *dev;
1682 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001683 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 int encap = 0;
1685
Ian Morris51456b22015-04-03 09:17:26 +01001686 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 goto out_free;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (vif->flags & VIFF_REGISTER) {
1690 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001691 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001692 vif->dev->stats.tx_bytes += skb->len;
1693 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001694 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001695 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001698 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001699 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001700 vif->remote, vif->local,
1701 0, 0,
1702 IPPROTO_IPIP,
1703 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001704 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 goto out_free;
1706 encap = sizeof(struct iphdr);
1707 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001708 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001709 0, 0,
1710 IPPROTO_IPIP,
1711 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001712 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 goto out_free;
1714 }
1715
Changli Gaod8d1f302010-06-10 23:31:35 -07001716 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Changli Gaod8d1f302010-06-10 23:31:35 -07001718 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001720 * allow to send ICMP, so that packets will disappear
1721 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 */
David S. Miller73186df2015-11-03 13:41:45 -05001723 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 ip_rt_put(rt);
1725 goto out_free;
1726 }
1727
Changli Gaod8d1f302010-06-10 23:31:35 -07001728 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001731 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto out_free;
1733 }
1734
1735 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001736 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Eric Dumazetadf30902009-06-02 05:19:30 +00001738 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001739 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001740 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001743 * What do we do with netfilter? -- RR
1744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001746 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001748 vif->dev->stats.tx_packets++;
1749 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751
Shmulik Ladkani359ebda2016-07-18 14:49:33 +03001752 IPCB(skb)->flags |= IPSKB_FORWARDED | IPSKB_FRAG_SEGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001754 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 * not only before forwarding, but after forwarding on all output
1756 * interfaces. It is clear, if mrouter runs a multicasting
1757 * program, it should receive packets not depending to what interface
1758 * program is joined.
1759 * If we will not make it, the program will have to join on all
1760 * interfaces. On the other hand, multihoming host (or router, but
1761 * not mrouter) cannot join to more than one interface - it will
1762 * result in receiving multiple packets.
1763 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001764 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1765 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 ipmr_forward_finish);
1767 return;
1768
1769out_free:
1770 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Patrick McHardy0c122952010-04-13 05:03:22 +00001773static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001776
1777 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1778 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 break;
1780 }
1781 return ct;
1782}
1783
1784/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001785static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1786 struct sk_buff *skb, struct mfc_cache *cache,
1787 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 int psend = -1;
1790 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001791 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 vif = cache->mfc_parent;
1794 cache->mfc_un.res.pkt++;
1795 cache->mfc_un.res.bytes += skb->len;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03001796 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001798 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001799 struct mfc_cache *cache_proxy;
1800
1801 /* For an (*,G) entry, we only check that the incomming
1802 * interface is part of the static tree.
1803 */
1804 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1805 if (cache_proxy &&
1806 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1807 goto forward;
1808 }
1809
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001810 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001811 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001812 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001814 * Very complicated situation...
1815 *
1816 * The best workaround until routing daemons will be
1817 * fixed is not to redistribute packet, if it was
1818 * send through wrong interface. It means, that
1819 * multicast applications WILL NOT work for
1820 * (S,G), which have default multicast route pointing
1821 * to wrong oif. In any case, it is not a good
1822 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
1824 goto dont_forward;
1825 }
1826
1827 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Patrick McHardy0c122952010-04-13 05:03:22 +00001829 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001831 * so that we cannot check that packet arrived on an oif.
1832 * It is bad, but otherwise we would need to move pretty
1833 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001835 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001836 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001837 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1839 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001840 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842 goto dont_forward;
1843 }
1844
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001845forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001846 mrt->vif_table[vif].pkt_in++;
1847 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001849 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001850 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1851 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001852 if (true_vifi >= 0 &&
1853 true_vifi != cache->mfc_parent &&
1854 ip_hdr(skb)->ttl >
1855 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1856 /* It's an (*,*) entry and the packet is not coming from
1857 * the upstream: forward the packet to the upstream
1858 * only.
1859 */
1860 psend = cache->mfc_parent;
1861 goto last_forward;
1862 }
1863 goto dont_forward;
1864 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001865 for (ct = cache->mfc_un.res.maxvif - 1;
1866 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001867 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001868 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1869 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001870 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (psend != -1) {
1872 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001875 ipmr_queue_xmit(net, mrt, skb2, cache,
1876 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001878 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001881last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (psend != -1) {
1883 if (local) {
1884 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001887 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001889 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001890 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892 }
1893
1894dont_forward:
1895 if (!local)
1896 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
David S. Miller417da662011-05-03 19:42:43 -07001899static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001900{
David S. Miller417da662011-05-03 19:42:43 -07001901 struct rtable *rt = skb_rtable(skb);
1902 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001903 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001904 .daddr = iph->daddr,
1905 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001906 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001907 .flowi4_oif = (rt_is_output_route(rt) ?
1908 skb->dev->ifindex : 0),
1909 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001910 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001911 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001912 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001913 };
1914 struct mr_table *mrt;
1915 int err;
1916
David S. Millerda919812011-03-12 02:04:50 -05001917 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001918 if (err)
1919 return ERR_PTR(err);
1920 return mrt;
1921}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001923/* Multicast packets for forwarding arrive here
1924 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926int ip_mr_input(struct sk_buff *skb)
1927{
1928 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001929 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001930 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001931 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001934 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001936 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 goto dont_forward;
1938
David S. Miller417da662011-05-03 19:42:43 -07001939 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001940 if (IS_ERR(mrt)) {
1941 kfree_skb(skb);
1942 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001945 if (IPCB(skb)->opt.router_alert) {
1946 if (ip_call_ra_chain(skb))
1947 return 0;
1948 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1949 /* IGMPv1 (and broken IGMPv2 implementations sort of
1950 * Cisco IOS <= 11.2(8)) do not put router alert
1951 * option to IGMP packets destined to routable
1952 * groups. It is very bad, because it means
1953 * that we can forward NO IGMP messages.
1954 */
1955 struct sock *mroute_sk;
1956
1957 mroute_sk = rcu_dereference(mrt->mroute_sk);
1958 if (mroute_sk) {
1959 nf_reset(skb);
1960 raw_rcv(mroute_sk, skb);
1961 return 0;
1962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964 }
1965
Eric Dumazeta8c94862010-10-01 16:15:08 +00001966 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001967 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001968 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001969 int vif = ipmr_find_vif(mrt, skb->dev);
1970
1971 if (vif >= 0)
1972 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1973 vif);
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001976 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01001977 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 int vif;
1979
1980 if (local) {
1981 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1982 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01001983 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 skb = skb2;
1986 }
1987
Eric Dumazeta8c94862010-10-01 16:15:08 +00001988 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001989 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001991 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 read_unlock(&mrt_lock);
1993
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001994 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 read_unlock(&mrt_lock);
1997 kfree_skb(skb);
1998 return -ENODEV;
1999 }
2000
Eric Dumazeta8c94862010-10-01 16:15:08 +00002001 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002002 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 read_unlock(&mrt_lock);
2004
2005 if (local)
2006 return ip_local_deliver(skb);
2007
2008 return 0;
2009
2010dont_forward:
2011 if (local)
2012 return ip_local_deliver(skb);
2013 kfree_skb(skb);
2014 return 0;
2015}
2016
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002017#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002018/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002019int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002020{
2021 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002022 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002023 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002024
2025 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2026 goto drop;
2027
2028 pim = igmp_hdr(skb);
2029
David S. Miller417da662011-05-03 19:42:43 -07002030 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002031 if (IS_ERR(mrt))
2032 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002033 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002034 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2035 goto drop;
2036
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002037 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002038drop:
2039 kfree_skb(skb);
2040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return 0;
2042}
2043#endif
2044
2045#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002046static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002049 struct net *net = dev_net(skb->dev);
2050 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002052 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 goto drop;
2054
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002055 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002056 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2057 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002058 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002059 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 goto drop;
2061
David S. Miller417da662011-05-03 19:42:43 -07002062 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002063 if (IS_ERR(mrt))
2064 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002065 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002066drop:
2067 kfree_skb(skb);
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return 0;
2070}
2071#endif
2072
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002073static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2074 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002076 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002077 struct nlattr *mp_attr;
2078 struct rtnexthop *nhp;
2079 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Nicolas Dichtel74381892010-03-25 23:45:35 +00002081 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002082 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002083 return -ENOENT;
2084
Thomas Graf92a395e2012-06-26 23:36:13 +00002085 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2086 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2087 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Thomas Graf92a395e2012-06-26 23:36:13 +00002089 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2090 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002093 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002094 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2095 nla_nest_cancel(skb, mp_attr);
2096 return -EMSGSIZE;
2097 }
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 nhp->rtnh_flags = 0;
2100 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002101 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 nhp->rtnh_len = sizeof(*nhp);
2103 }
2104 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002105
2106 nla_nest_end(skb, mp_attr);
2107
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002108 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2109 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2110 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002111 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
2112 nla_put_u64_64bit(skb, RTA_EXPIRES,
2113 jiffies_to_clock_t(c->mfc_un.res.lastuse),
2114 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002115 return -EMSGSIZE;
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 rtm->rtm_type = RTN_MULTICAST;
2118 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
David S. Miller9a1b9492011-05-04 12:18:54 -07002121int ipmr_get_route(struct net *net, struct sk_buff *skb,
2122 __be32 saddr, __be32 daddr,
2123 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002126 struct mr_table *mrt;
2127 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002129 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002130 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002131 return -ENOENT;
2132
Eric Dumazeta8c94862010-10-01 16:15:08 +00002133 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002134 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002135 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002136 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002138 if (vif >= 0)
2139 cache = ipmr_cache_find_any(mrt, daddr, vif);
2140 }
Ian Morris51456b22015-04-03 09:17:26 +01002141 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002142 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002143 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002145 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002148 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return -EAGAIN;
2150 }
2151
2152 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002153 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002154 if (dev)
2155 vif = ipmr_find_vif(mrt, dev);
2156 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002158 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return -ENODEV;
2160 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002161 skb2 = skb_clone(skb, GFP_ATOMIC);
2162 if (!skb2) {
2163 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002164 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002165 return -ENOMEM;
2166 }
2167
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002168 skb_push(skb2, sizeof(struct iphdr));
2169 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002170 iph = ip_hdr(skb2);
2171 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002172 iph->saddr = saddr;
2173 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002174 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002175 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002177 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return err;
2179 }
2180
Eric Dumazeta8c94862010-10-01 16:15:08 +00002181 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002182 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002184 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return err;
2186}
2187
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002188static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002189 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2190 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002191{
2192 struct nlmsghdr *nlh;
2193 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002194 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002195
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002196 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002197 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002198 return -EMSGSIZE;
2199
2200 rtm = nlmsg_data(nlh);
2201 rtm->rtm_family = RTNL_FAMILY_IPMR;
2202 rtm->rtm_dst_len = 32;
2203 rtm->rtm_src_len = 32;
2204 rtm->rtm_tos = 0;
2205 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002206 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2207 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002208 rtm->rtm_type = RTN_MULTICAST;
2209 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002210 if (c->mfc_flags & MFC_STATIC)
2211 rtm->rtm_protocol = RTPROT_STATIC;
2212 else
2213 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002214 rtm->rtm_flags = 0;
2215
Jiri Benc930345e2015-03-29 16:59:25 +02002216 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2217 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002218 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002219 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2220 /* do not break the dump if cache is unresolved */
2221 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002222 goto nla_put_failure;
2223
Johannes Berg053c0952015-01-16 22:09:00 +01002224 nlmsg_end(skb, nlh);
2225 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002226
2227nla_put_failure:
2228 nlmsg_cancel(skb, nlh);
2229 return -EMSGSIZE;
2230}
2231
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002232static size_t mroute_msgsize(bool unresolved, int maxvif)
2233{
2234 size_t len =
2235 NLMSG_ALIGN(sizeof(struct rtmsg))
2236 + nla_total_size(4) /* RTA_TABLE */
2237 + nla_total_size(4) /* RTA_SRC */
2238 + nla_total_size(4) /* RTA_DST */
2239 ;
2240
2241 if (!unresolved)
2242 len = len
2243 + nla_total_size(4) /* RTA_IIF */
2244 + nla_total_size(0) /* RTA_MULTIPATH */
2245 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2246 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002247 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002248 ;
2249
2250 return len;
2251}
2252
2253static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2254 int cmd)
2255{
2256 struct net *net = read_pnet(&mrt->net);
2257 struct sk_buff *skb;
2258 int err = -ENOBUFS;
2259
2260 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2261 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002262 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002263 goto errout;
2264
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002265 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002266 if (err < 0)
2267 goto errout;
2268
2269 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2270 return;
2271
2272errout:
2273 kfree_skb(skb);
2274 if (err < 0)
2275 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2276}
2277
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002278static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2279{
2280 struct net *net = sock_net(skb->sk);
2281 struct mr_table *mrt;
2282 struct mfc_cache *mfc;
2283 unsigned int t = 0, s_t;
2284 unsigned int h = 0, s_h;
2285 unsigned int e = 0, s_e;
2286
2287 s_t = cb->args[0];
2288 s_h = cb->args[1];
2289 s_e = cb->args[2];
2290
Eric Dumazeta8c94862010-10-01 16:15:08 +00002291 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002292 ipmr_for_each_table(mrt, net) {
2293 if (t < s_t)
2294 goto next_table;
2295 if (t > s_t)
2296 s_h = 0;
2297 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002298 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002299 if (e < s_e)
2300 goto next_entry;
2301 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002302 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002303 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002304 mfc, RTM_NEWROUTE,
2305 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002306 goto done;
2307next_entry:
2308 e++;
2309 }
2310 e = s_e = 0;
2311 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002312 spin_lock_bh(&mfc_unres_lock);
2313 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2314 if (e < s_e)
2315 goto next_entry2;
2316 if (ipmr_fill_mroute(mrt, skb,
2317 NETLINK_CB(cb->skb).portid,
2318 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002319 mfc, RTM_NEWROUTE,
2320 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002321 spin_unlock_bh(&mfc_unres_lock);
2322 goto done;
2323 }
2324next_entry2:
2325 e++;
2326 }
2327 spin_unlock_bh(&mfc_unres_lock);
2328 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002329 s_h = 0;
2330next_table:
2331 t++;
2332 }
2333done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002334 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002335
2336 cb->args[2] = e;
2337 cb->args[1] = h;
2338 cb->args[0] = t;
2339
2340 return skb->len;
2341}
2342
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002343static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2344 [RTA_SRC] = { .type = NLA_U32 },
2345 [RTA_DST] = { .type = NLA_U32 },
2346 [RTA_IIF] = { .type = NLA_U32 },
2347 [RTA_TABLE] = { .type = NLA_U32 },
2348 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2349};
2350
2351static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2352{
2353 switch (rtm_protocol) {
2354 case RTPROT_STATIC:
2355 case RTPROT_MROUTED:
2356 return true;
2357 }
2358 return false;
2359}
2360
2361static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2362{
2363 struct rtnexthop *rtnh = nla_data(nla);
2364 int remaining = nla_len(nla), vifi = 0;
2365
2366 while (rtnh_ok(rtnh, remaining)) {
2367 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2368 if (++vifi == MAXVIFS)
2369 break;
2370 rtnh = rtnh_next(rtnh, &remaining);
2371 }
2372
2373 return remaining > 0 ? -EINVAL : vifi;
2374}
2375
2376/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2377static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2378 struct mfcctl *mfcc, int *mrtsock,
2379 struct mr_table **mrtret)
2380{
2381 struct net_device *dev = NULL;
2382 u32 tblid = RT_TABLE_DEFAULT;
2383 struct mr_table *mrt;
2384 struct nlattr *attr;
2385 struct rtmsg *rtm;
2386 int ret, rem;
2387
2388 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy);
2389 if (ret < 0)
2390 goto out;
2391 rtm = nlmsg_data(nlh);
2392
2393 ret = -EINVAL;
2394 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2395 rtm->rtm_type != RTN_MULTICAST ||
2396 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2397 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2398 goto out;
2399
2400 memset(mfcc, 0, sizeof(*mfcc));
2401 mfcc->mfcc_parent = -1;
2402 ret = 0;
2403 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2404 switch (nla_type(attr)) {
2405 case RTA_SRC:
2406 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2407 break;
2408 case RTA_DST:
2409 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2410 break;
2411 case RTA_IIF:
2412 dev = __dev_get_by_index(net, nla_get_u32(attr));
2413 if (!dev) {
2414 ret = -ENODEV;
2415 goto out;
2416 }
2417 break;
2418 case RTA_MULTIPATH:
2419 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2420 ret = -EINVAL;
2421 goto out;
2422 }
2423 break;
2424 case RTA_PREFSRC:
2425 ret = 1;
2426 break;
2427 case RTA_TABLE:
2428 tblid = nla_get_u32(attr);
2429 break;
2430 }
2431 }
2432 mrt = ipmr_get_table(net, tblid);
2433 if (!mrt) {
2434 ret = -ENOENT;
2435 goto out;
2436 }
2437 *mrtret = mrt;
2438 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2439 if (dev)
2440 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2441
2442out:
2443 return ret;
2444}
2445
2446/* takes care of both newroute and delroute */
2447static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh)
2448{
2449 struct net *net = sock_net(skb->sk);
2450 int ret, mrtsock, parent;
2451 struct mr_table *tbl;
2452 struct mfcctl mfcc;
2453
2454 mrtsock = 0;
2455 tbl = NULL;
2456 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl);
2457 if (ret < 0)
2458 return ret;
2459
2460 parent = ret ? mfcc.mfcc_parent : -1;
2461 if (nlh->nlmsg_type == RTM_NEWROUTE)
2462 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2463 else
2464 return ipmr_mfc_delete(tbl, &mfcc, parent);
2465}
2466
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002467#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002468/* The /proc interfaces to multicast routing :
2469 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 */
2471struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002472 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002473 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 int ct;
2475};
2476
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002477static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2478 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 loff_t pos)
2480{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002481 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002482
2483 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2484 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002486 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002487 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489 return NULL;
2490}
2491
2492static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002493 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002495 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002496 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002497 struct mr_table *mrt;
2498
2499 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002500 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002501 return ERR_PTR(-ENOENT);
2502
2503 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002506 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 : SEQ_START_TOKEN;
2508}
2509
2510static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2511{
2512 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002513 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002514 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 ++*pos;
2517 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002518 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002519
Patrick McHardy0c122952010-04-13 05:03:22 +00002520 while (++iter->ct < mrt->maxvif) {
2521 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002523 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525 return NULL;
2526}
2527
2528static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002529 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 read_unlock(&mrt_lock);
2532}
2533
2534static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2535{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002536 struct ipmr_vif_iter *iter = seq->private;
2537 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002540 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2542 } else {
2543 const struct vif_device *vif = v;
2544 const char *name = vif->dev ? vif->dev->name : "none";
2545
2546 seq_printf(seq,
2547 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002548 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002549 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 vif->bytes_out, vif->pkt_out,
2551 vif->flags, vif->local, vif->remote);
2552 }
2553 return 0;
2554}
2555
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002556static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 .start = ipmr_vif_seq_start,
2558 .next = ipmr_vif_seq_next,
2559 .stop = ipmr_vif_seq_stop,
2560 .show = ipmr_vif_seq_show,
2561};
2562
2563static int ipmr_vif_open(struct inode *inode, struct file *file)
2564{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002565 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2566 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Arjan van de Ven9a321442007-02-12 00:55:35 -08002569static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 .owner = THIS_MODULE,
2571 .open = ipmr_vif_open,
2572 .read = seq_read,
2573 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002574 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575};
2576
2577struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002578 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002579 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002580 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 int ct;
2582};
2583
2584
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002585static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2586 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002588 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 struct mfc_cache *mfc;
2590
Eric Dumazeta8c94862010-10-01 16:15:08 +00002591 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002592 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002593 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002594 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002595 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002597 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002598 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002601 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002602 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002603 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return mfc;
2605 spin_unlock_bh(&mfc_unres_lock);
2606
2607 it->cache = NULL;
2608 return NULL;
2609}
2610
2611
2612static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2613{
2614 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002615 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002616 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002617
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002618 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002619 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002620 return ERR_PTR(-ENOENT);
2621
2622 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 it->cache = NULL;
2624 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002625 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 : SEQ_START_TOKEN;
2627}
2628
2629static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2630{
2631 struct mfc_cache *mfc = v;
2632 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002633 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002634 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 ++*pos;
2637
2638 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002639 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Patrick McHardy862465f2010-04-13 05:03:21 +00002641 if (mfc->list.next != it->cache)
2642 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002643
Patrick McHardy0c122952010-04-13 05:03:22 +00002644 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 goto end_of_list;
2646
Patrick McHardy0c122952010-04-13 05:03:22 +00002647 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002650 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002651 if (list_empty(it->cache))
2652 continue;
2653 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
2655
2656 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002657 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002658 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002662 if (!list_empty(it->cache))
2663 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002665end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 spin_unlock_bh(&mfc_unres_lock);
2667 it->cache = NULL;
2668
2669 return NULL;
2670}
2671
2672static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2673{
2674 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002675 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Patrick McHardy0c122952010-04-13 05:03:22 +00002677 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002679 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002680 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
2683static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2684{
2685 int n;
2686
2687 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002688 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2690 } else {
2691 const struct mfc_cache *mfc = v;
2692 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002693 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002694
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002695 seq_printf(seq, "%08X %08X %-3hd",
2696 (__force u32) mfc->mfc_mcastgrp,
2697 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002698 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Patrick McHardy0c122952010-04-13 05:03:22 +00002700 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002701 seq_printf(seq, " %8lu %8lu %8lu",
2702 mfc->mfc_un.res.pkt,
2703 mfc->mfc_un.res.bytes,
2704 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002705 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002706 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002707 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002708 mfc->mfc_un.res.ttls[n] < 255)
2709 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002710 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 n, mfc->mfc_un.res.ttls[n]);
2712 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002713 } else {
2714 /* unresolved mfc_caches don't contain
2715 * pkt, bytes and wrong_if values
2716 */
2717 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719 seq_putc(seq, '\n');
2720 }
2721 return 0;
2722}
2723
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002724static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 .start = ipmr_mfc_seq_start,
2726 .next = ipmr_mfc_seq_next,
2727 .stop = ipmr_mfc_seq_stop,
2728 .show = ipmr_mfc_seq_show,
2729};
2730
2731static int ipmr_mfc_open(struct inode *inode, struct file *file)
2732{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002733 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2734 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Arjan van de Ven9a321442007-02-12 00:55:35 -08002737static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 .owner = THIS_MODULE,
2739 .open = ipmr_mfc_open,
2740 .read = seq_read,
2741 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002742 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002744#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002747static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002749 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750};
2751#endif
2752
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002753/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002754static int __net_init ipmr_net_init(struct net *net)
2755{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002756 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002757
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002758 err = ipmr_rules_init(net);
2759 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002760 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002761
2762#ifdef CONFIG_PROC_FS
2763 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002764 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002765 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002766 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002767 goto proc_cache_fail;
2768#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002769 return 0;
2770
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002771#ifdef CONFIG_PROC_FS
2772proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002773 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002774proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002775 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002776#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002777fail:
2778 return err;
2779}
2780
2781static void __net_exit ipmr_net_exit(struct net *net)
2782{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002783#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002784 remove_proc_entry("ip_mr_cache", net->proc_net);
2785 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002786#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002787 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002788}
2789
2790static struct pernet_operations ipmr_net_ops = {
2791 .init = ipmr_net_init,
2792 .exit = ipmr_net_exit,
2793};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002794
Wang Chen03d2f892008-07-03 12:13:36 +08002795int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Wang Chen03d2f892008-07-03 12:13:36 +08002797 int err;
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2800 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002801 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002802 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002803
Benjamin Therycf958ae32009-01-22 04:56:16 +00002804 err = register_pernet_subsys(&ipmr_net_ops);
2805 if (err)
2806 goto reg_pernet_fail;
2807
Wang Chen03d2f892008-07-03 12:13:36 +08002808 err = register_netdevice_notifier(&ip_mr_notifier);
2809 if (err)
2810 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002811#ifdef CONFIG_IP_PIMSM_V2
2812 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002813 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002814 err = -EAGAIN;
2815 goto add_proto_fail;
2816 }
2817#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002818 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2819 NULL, ipmr_rtm_dumproute, NULL);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002820 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
2821 ipmr_rtm_route, NULL, NULL);
2822 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
2823 ipmr_rtm_route, NULL, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002824 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002825
Tom Goff403dbb92009-06-14 03:16:13 -07002826#ifdef CONFIG_IP_PIMSM_V2
2827add_proto_fail:
2828 unregister_netdevice_notifier(&ip_mr_notifier);
2829#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002830reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002831 unregister_pernet_subsys(&ipmr_net_ops);
2832reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002833 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002834 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}