blob: 5f006e13de567ad65ce856a47c118f5ea91ac671 [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 Dumazet4c968702010-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 Dumazet4c968702010-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 Dumazet4c968702010-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 Dumazet4c968702010-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 Dumazet4c968702010-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 Dumazet4c968702010-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 Dumazet4c968702010-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 Dumazet4c968702010-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 Dumazet4c968702010-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 Dumazet4c968702010-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 Dumazet4c968702010-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;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002079 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002080 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Nicolas Dichtel74381892010-03-25 23:45:35 +00002082 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002083 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002084 return -ENOENT;
2085
Thomas Graf92a395e2012-06-26 23:36:13 +00002086 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2087 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2088 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Thomas Graf92a395e2012-06-26 23:36:13 +00002090 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2091 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002094 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002095 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2096 nla_nest_cancel(skb, mp_attr);
2097 return -EMSGSIZE;
2098 }
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 nhp->rtnh_flags = 0;
2101 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002102 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 nhp->rtnh_len = sizeof(*nhp);
2104 }
2105 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002106
2107 nla_nest_end(skb, mp_attr);
2108
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002109 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2110 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2111
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002112 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2113 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2114 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002115 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002116 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002117 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002118 return -EMSGSIZE;
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 rtm->rtm_type = RTN_MULTICAST;
2121 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
David S. Miller9a1b9492011-05-04 12:18:54 -07002124int ipmr_get_route(struct net *net, struct sk_buff *skb,
2125 __be32 saddr, __be32 daddr,
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002126 struct rtmsg *rtm, int nowait, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002129 struct mr_table *mrt;
2130 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002132 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002133 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002134 return -ENOENT;
2135
Eric Dumazeta8c94862010-10-01 16:15:08 +00002136 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002137 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002138 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002139 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002141 if (vif >= 0)
2142 cache = ipmr_cache_find_any(mrt, daddr, vif);
2143 }
Ian Morris51456b22015-04-03 09:17:26 +01002144 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002145 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002146 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002148 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002151 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return -EAGAIN;
2153 }
2154
2155 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002156 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002157 if (dev)
2158 vif = ipmr_find_vif(mrt, dev);
2159 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002161 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return -ENODEV;
2163 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002164 skb2 = skb_clone(skb, GFP_ATOMIC);
2165 if (!skb2) {
2166 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002167 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002168 return -ENOMEM;
2169 }
2170
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002171 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002172 skb_push(skb2, sizeof(struct iphdr));
2173 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002174 iph = ip_hdr(skb2);
2175 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002176 iph->saddr = saddr;
2177 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002178 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002179 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002181 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return err;
2183 }
2184
Eric Dumazeta8c94862010-10-01 16:15:08 +00002185 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002186 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002188 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return err;
2190}
2191
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002192static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002193 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2194 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002195{
2196 struct nlmsghdr *nlh;
2197 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002198 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002199
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002200 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002201 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002202 return -EMSGSIZE;
2203
2204 rtm = nlmsg_data(nlh);
2205 rtm->rtm_family = RTNL_FAMILY_IPMR;
2206 rtm->rtm_dst_len = 32;
2207 rtm->rtm_src_len = 32;
2208 rtm->rtm_tos = 0;
2209 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002210 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2211 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002212 rtm->rtm_type = RTN_MULTICAST;
2213 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002214 if (c->mfc_flags & MFC_STATIC)
2215 rtm->rtm_protocol = RTPROT_STATIC;
2216 else
2217 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002218 rtm->rtm_flags = 0;
2219
Jiri Benc930345e2015-03-29 16:59:25 +02002220 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2221 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002222 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002223 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2224 /* do not break the dump if cache is unresolved */
2225 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002226 goto nla_put_failure;
2227
Johannes Berg053c0952015-01-16 22:09:00 +01002228 nlmsg_end(skb, nlh);
2229 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002230
2231nla_put_failure:
2232 nlmsg_cancel(skb, nlh);
2233 return -EMSGSIZE;
2234}
2235
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002236static size_t mroute_msgsize(bool unresolved, int maxvif)
2237{
2238 size_t len =
2239 NLMSG_ALIGN(sizeof(struct rtmsg))
2240 + nla_total_size(4) /* RTA_TABLE */
2241 + nla_total_size(4) /* RTA_SRC */
2242 + nla_total_size(4) /* RTA_DST */
2243 ;
2244
2245 if (!unresolved)
2246 len = len
2247 + nla_total_size(4) /* RTA_IIF */
2248 + nla_total_size(0) /* RTA_MULTIPATH */
2249 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2250 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002251 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002252 ;
2253
2254 return len;
2255}
2256
2257static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2258 int cmd)
2259{
2260 struct net *net = read_pnet(&mrt->net);
2261 struct sk_buff *skb;
2262 int err = -ENOBUFS;
2263
2264 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2265 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002266 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002267 goto errout;
2268
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002269 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002270 if (err < 0)
2271 goto errout;
2272
2273 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2274 return;
2275
2276errout:
2277 kfree_skb(skb);
2278 if (err < 0)
2279 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2280}
2281
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002282static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2283{
2284 struct net *net = sock_net(skb->sk);
2285 struct mr_table *mrt;
2286 struct mfc_cache *mfc;
2287 unsigned int t = 0, s_t;
2288 unsigned int h = 0, s_h;
2289 unsigned int e = 0, s_e;
2290
2291 s_t = cb->args[0];
2292 s_h = cb->args[1];
2293 s_e = cb->args[2];
2294
Eric Dumazeta8c94862010-10-01 16:15:08 +00002295 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002296 ipmr_for_each_table(mrt, net) {
2297 if (t < s_t)
2298 goto next_table;
2299 if (t > s_t)
2300 s_h = 0;
2301 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002302 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002303 if (e < s_e)
2304 goto next_entry;
2305 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002306 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002307 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002308 mfc, RTM_NEWROUTE,
2309 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002310 goto done;
2311next_entry:
2312 e++;
2313 }
2314 e = s_e = 0;
2315 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002316 spin_lock_bh(&mfc_unres_lock);
2317 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2318 if (e < s_e)
2319 goto next_entry2;
2320 if (ipmr_fill_mroute(mrt, skb,
2321 NETLINK_CB(cb->skb).portid,
2322 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002323 mfc, RTM_NEWROUTE,
2324 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002325 spin_unlock_bh(&mfc_unres_lock);
2326 goto done;
2327 }
2328next_entry2:
2329 e++;
2330 }
2331 spin_unlock_bh(&mfc_unres_lock);
2332 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002333 s_h = 0;
2334next_table:
2335 t++;
2336 }
2337done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002338 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002339
2340 cb->args[2] = e;
2341 cb->args[1] = h;
2342 cb->args[0] = t;
2343
2344 return skb->len;
2345}
2346
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002347static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2348 [RTA_SRC] = { .type = NLA_U32 },
2349 [RTA_DST] = { .type = NLA_U32 },
2350 [RTA_IIF] = { .type = NLA_U32 },
2351 [RTA_TABLE] = { .type = NLA_U32 },
2352 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2353};
2354
2355static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2356{
2357 switch (rtm_protocol) {
2358 case RTPROT_STATIC:
2359 case RTPROT_MROUTED:
2360 return true;
2361 }
2362 return false;
2363}
2364
2365static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2366{
2367 struct rtnexthop *rtnh = nla_data(nla);
2368 int remaining = nla_len(nla), vifi = 0;
2369
2370 while (rtnh_ok(rtnh, remaining)) {
2371 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2372 if (++vifi == MAXVIFS)
2373 break;
2374 rtnh = rtnh_next(rtnh, &remaining);
2375 }
2376
2377 return remaining > 0 ? -EINVAL : vifi;
2378}
2379
2380/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2381static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2382 struct mfcctl *mfcc, int *mrtsock,
2383 struct mr_table **mrtret)
2384{
2385 struct net_device *dev = NULL;
2386 u32 tblid = RT_TABLE_DEFAULT;
2387 struct mr_table *mrt;
2388 struct nlattr *attr;
2389 struct rtmsg *rtm;
2390 int ret, rem;
2391
2392 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy);
2393 if (ret < 0)
2394 goto out;
2395 rtm = nlmsg_data(nlh);
2396
2397 ret = -EINVAL;
2398 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2399 rtm->rtm_type != RTN_MULTICAST ||
2400 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2401 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2402 goto out;
2403
2404 memset(mfcc, 0, sizeof(*mfcc));
2405 mfcc->mfcc_parent = -1;
2406 ret = 0;
2407 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2408 switch (nla_type(attr)) {
2409 case RTA_SRC:
2410 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2411 break;
2412 case RTA_DST:
2413 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2414 break;
2415 case RTA_IIF:
2416 dev = __dev_get_by_index(net, nla_get_u32(attr));
2417 if (!dev) {
2418 ret = -ENODEV;
2419 goto out;
2420 }
2421 break;
2422 case RTA_MULTIPATH:
2423 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2424 ret = -EINVAL;
2425 goto out;
2426 }
2427 break;
2428 case RTA_PREFSRC:
2429 ret = 1;
2430 break;
2431 case RTA_TABLE:
2432 tblid = nla_get_u32(attr);
2433 break;
2434 }
2435 }
2436 mrt = ipmr_get_table(net, tblid);
2437 if (!mrt) {
2438 ret = -ENOENT;
2439 goto out;
2440 }
2441 *mrtret = mrt;
2442 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2443 if (dev)
2444 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2445
2446out:
2447 return ret;
2448}
2449
2450/* takes care of both newroute and delroute */
2451static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh)
2452{
2453 struct net *net = sock_net(skb->sk);
2454 int ret, mrtsock, parent;
2455 struct mr_table *tbl;
2456 struct mfcctl mfcc;
2457
2458 mrtsock = 0;
2459 tbl = NULL;
2460 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl);
2461 if (ret < 0)
2462 return ret;
2463
2464 parent = ret ? mfcc.mfcc_parent : -1;
2465 if (nlh->nlmsg_type == RTM_NEWROUTE)
2466 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2467 else
2468 return ipmr_mfc_delete(tbl, &mfcc, parent);
2469}
2470
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002471#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002472/* The /proc interfaces to multicast routing :
2473 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 */
2475struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002476 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002477 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 int ct;
2479};
2480
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002481static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2482 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 loff_t pos)
2484{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002485 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002486
2487 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2488 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002490 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002491 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
2493 return NULL;
2494}
2495
2496static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002497 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002499 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002500 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002501 struct mr_table *mrt;
2502
2503 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002504 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002505 return ERR_PTR(-ENOENT);
2506
2507 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002510 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 : SEQ_START_TOKEN;
2512}
2513
2514static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2515{
2516 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002517 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002518 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 ++*pos;
2521 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002522 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002523
Patrick McHardy0c122952010-04-13 05:03:22 +00002524 while (++iter->ct < mrt->maxvif) {
2525 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002527 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529 return NULL;
2530}
2531
2532static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002533 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
2535 read_unlock(&mrt_lock);
2536}
2537
2538static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2539{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002540 struct ipmr_vif_iter *iter = seq->private;
2541 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002544 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2546 } else {
2547 const struct vif_device *vif = v;
2548 const char *name = vif->dev ? vif->dev->name : "none";
2549
2550 seq_printf(seq,
2551 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002552 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002553 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 vif->bytes_out, vif->pkt_out,
2555 vif->flags, vif->local, vif->remote);
2556 }
2557 return 0;
2558}
2559
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002560static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 .start = ipmr_vif_seq_start,
2562 .next = ipmr_vif_seq_next,
2563 .stop = ipmr_vif_seq_stop,
2564 .show = ipmr_vif_seq_show,
2565};
2566
2567static int ipmr_vif_open(struct inode *inode, struct file *file)
2568{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002569 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2570 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Arjan van de Ven9a321442007-02-12 00:55:35 -08002573static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 .owner = THIS_MODULE,
2575 .open = ipmr_vif_open,
2576 .read = seq_read,
2577 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002578 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579};
2580
2581struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002582 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002583 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002584 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 int ct;
2586};
2587
2588
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002589static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2590 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002592 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct mfc_cache *mfc;
2594
Eric Dumazeta8c94862010-10-01 16:15:08 +00002595 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002596 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002597 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002598 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002599 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002601 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002602 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002605 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002606 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002607 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return mfc;
2609 spin_unlock_bh(&mfc_unres_lock);
2610
2611 it->cache = NULL;
2612 return NULL;
2613}
2614
2615
2616static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2617{
2618 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002619 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002620 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002621
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002622 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002623 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002624 return ERR_PTR(-ENOENT);
2625
2626 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 it->cache = NULL;
2628 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002629 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 : SEQ_START_TOKEN;
2631}
2632
2633static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2634{
2635 struct mfc_cache *mfc = v;
2636 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002637 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002638 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 ++*pos;
2641
2642 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002643 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Patrick McHardy862465f2010-04-13 05:03:21 +00002645 if (mfc->list.next != it->cache)
2646 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002647
Patrick McHardy0c122952010-04-13 05:03:22 +00002648 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 goto end_of_list;
2650
Patrick McHardy0c122952010-04-13 05:03:22 +00002651 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002654 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002655 if (list_empty(it->cache))
2656 continue;
2657 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
2659
2660 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002661 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002662 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002666 if (!list_empty(it->cache))
2667 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002669end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 spin_unlock_bh(&mfc_unres_lock);
2671 it->cache = NULL;
2672
2673 return NULL;
2674}
2675
2676static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2677{
2678 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002679 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Patrick McHardy0c122952010-04-13 05:03:22 +00002681 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002683 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002684 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
2687static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2688{
2689 int n;
2690
2691 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002692 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2694 } else {
2695 const struct mfc_cache *mfc = v;
2696 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002697 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002698
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002699 seq_printf(seq, "%08X %08X %-3hd",
2700 (__force u32) mfc->mfc_mcastgrp,
2701 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002702 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Patrick McHardy0c122952010-04-13 05:03:22 +00002704 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002705 seq_printf(seq, " %8lu %8lu %8lu",
2706 mfc->mfc_un.res.pkt,
2707 mfc->mfc_un.res.bytes,
2708 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002709 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002710 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002711 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002712 mfc->mfc_un.res.ttls[n] < 255)
2713 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002714 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 n, mfc->mfc_un.res.ttls[n]);
2716 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002717 } else {
2718 /* unresolved mfc_caches don't contain
2719 * pkt, bytes and wrong_if values
2720 */
2721 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723 seq_putc(seq, '\n');
2724 }
2725 return 0;
2726}
2727
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002728static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 .start = ipmr_mfc_seq_start,
2730 .next = ipmr_mfc_seq_next,
2731 .stop = ipmr_mfc_seq_stop,
2732 .show = ipmr_mfc_seq_show,
2733};
2734
2735static int ipmr_mfc_open(struct inode *inode, struct file *file)
2736{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002737 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2738 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
Arjan van de Ven9a321442007-02-12 00:55:35 -08002741static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 .owner = THIS_MODULE,
2743 .open = ipmr_mfc_open,
2744 .read = seq_read,
2745 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002746 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002748#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002751static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002753 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754};
2755#endif
2756
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002757/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002758static int __net_init ipmr_net_init(struct net *net)
2759{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002760 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002761
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002762 err = ipmr_rules_init(net);
2763 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002764 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002765
2766#ifdef CONFIG_PROC_FS
2767 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002768 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002769 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002770 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002771 goto proc_cache_fail;
2772#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002773 return 0;
2774
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002775#ifdef CONFIG_PROC_FS
2776proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002777 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002778proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002779 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002780#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002781fail:
2782 return err;
2783}
2784
2785static void __net_exit ipmr_net_exit(struct net *net)
2786{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002787#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002788 remove_proc_entry("ip_mr_cache", net->proc_net);
2789 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002790#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002791 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002792}
2793
2794static struct pernet_operations ipmr_net_ops = {
2795 .init = ipmr_net_init,
2796 .exit = ipmr_net_exit,
2797};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002798
Wang Chen03d2f892008-07-03 12:13:36 +08002799int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
Wang Chen03d2f892008-07-03 12:13:36 +08002801 int err;
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2804 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002805 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002806 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002807
Benjamin Therycf958ae32009-01-22 04:56:16 +00002808 err = register_pernet_subsys(&ipmr_net_ops);
2809 if (err)
2810 goto reg_pernet_fail;
2811
Wang Chen03d2f892008-07-03 12:13:36 +08002812 err = register_netdevice_notifier(&ip_mr_notifier);
2813 if (err)
2814 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002815#ifdef CONFIG_IP_PIMSM_V2
2816 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002817 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002818 err = -EAGAIN;
2819 goto add_proto_fail;
2820 }
2821#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002822 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2823 NULL, ipmr_rtm_dumproute, NULL);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002824 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
2825 ipmr_rtm_route, NULL, NULL);
2826 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
2827 ipmr_rtm_route, NULL, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002828 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002829
Tom Goff403dbb92009-06-14 03:16:13 -07002830#ifdef CONFIG_IP_PIMSM_V2
2831add_proto_fail:
2832 unregister_netdevice_notifier(&ip_mr_notifier);
2833#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002834reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002835 unregister_pernet_subsys(&ipmr_net_ops);
2836reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002837 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002838 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839}