blob: 9374b99c7c172435ebe0c442be244b489395241d [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 Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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 Aherne58e4152016-10-31 15:54:00 -0700140 /* update flow if oif or iif point to device enslaved to l3mdev */
141 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
142
David S. Millerda919812011-03-12 02:04:50 -0500143 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
144 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000145 if (err < 0)
146 return err;
147 *mrt = res.mrt;
148 return 0;
149}
150
151static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
152 int flags, struct fib_lookup_arg *arg)
153{
154 struct ipmr_result *res = arg->result;
155 struct mr_table *mrt;
156
157 switch (rule->action) {
158 case FR_ACT_TO_TBL:
159 break;
160 case FR_ACT_UNREACHABLE:
161 return -ENETUNREACH;
162 case FR_ACT_PROHIBIT:
163 return -EACCES;
164 case FR_ACT_BLACKHOLE:
165 default:
166 return -EINVAL;
167 }
168
David Aherne58e4152016-10-31 15:54:00 -0700169 arg->table = fib_rule_get_table(rule, arg);
170
171 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100172 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000173 return -EAGAIN;
174 res->mrt = mrt;
175 return 0;
176}
177
178static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
179{
180 return 1;
181}
182
183static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
184 FRA_GENERIC_POLICY,
185};
186
187static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
188 struct fib_rule_hdr *frh, struct nlattr **tb)
189{
190 return 0;
191}
192
193static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
194 struct nlattr **tb)
195{
196 return 1;
197}
198
199static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
200 struct fib_rule_hdr *frh)
201{
202 frh->dst_len = 0;
203 frh->src_len = 0;
204 frh->tos = 0;
205 return 0;
206}
207
Andi Kleen04a6f822012-10-04 17:12:11 -0700208static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200209 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000210 .rule_size = sizeof(struct ipmr_rule),
211 .addr_size = sizeof(u32),
212 .action = ipmr_rule_action,
213 .match = ipmr_rule_match,
214 .configure = ipmr_rule_configure,
215 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000216 .fill = ipmr_rule_fill,
217 .nlgroup = RTNLGRP_IPV4_RULE,
218 .policy = ipmr_rule_policy,
219 .owner = THIS_MODULE,
220};
221
222static int __net_init ipmr_rules_init(struct net *net)
223{
224 struct fib_rules_ops *ops;
225 struct mr_table *mrt;
226 int err;
227
228 ops = fib_rules_register(&ipmr_rules_ops_template, net);
229 if (IS_ERR(ops))
230 return PTR_ERR(ops);
231
232 INIT_LIST_HEAD(&net->ipv4.mr_tables);
233
234 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100235 if (IS_ERR(mrt)) {
236 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000237 goto err1;
238 }
239
240 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
241 if (err < 0)
242 goto err2;
243
244 net->ipv4.mr_rules_ops = ops;
245 return 0;
246
247err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700248 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000249err1:
250 fib_rules_unregister(ops);
251 return err;
252}
253
254static void __net_exit ipmr_rules_exit(struct net *net)
255{
256 struct mr_table *mrt, *next;
257
WANG Conged785302015-03-31 11:01:45 -0700258 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000259 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
260 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000261 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000262 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000263 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700264 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000265}
266#else
267#define ipmr_for_each_table(mrt, net) \
268 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
269
270static struct mr_table *ipmr_get_table(struct net *net, u32 id)
271{
272 return net->ipv4.mrt;
273}
274
David S. Millerda919812011-03-12 02:04:50 -0500275static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000276 struct mr_table **mrt)
277{
278 *mrt = net->ipv4.mrt;
279 return 0;
280}
281
282static int __net_init ipmr_rules_init(struct net *net)
283{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100284 struct mr_table *mrt;
285
286 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
287 if (IS_ERR(mrt))
288 return PTR_ERR(mrt);
289 net->ipv4.mrt = mrt;
290 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000291}
292
293static void __net_exit ipmr_rules_exit(struct net *net)
294{
WANG Conged785302015-03-31 11:01:45 -0700295 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000296 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700297 net->ipv4.mrt = NULL;
298 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000299}
300#endif
301
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100302static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
303 const void *ptr)
304{
305 const struct mfc_cache_cmp_arg *cmparg = arg->key;
306 struct mfc_cache *c = (struct mfc_cache *)ptr;
307
308 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
309 cmparg->mfc_origin != c->mfc_origin;
310}
311
312static const struct rhashtable_params ipmr_rht_params = {
313 .head_offset = offsetof(struct mfc_cache, mnode),
314 .key_offset = offsetof(struct mfc_cache, cmparg),
315 .key_len = sizeof(struct mfc_cache_cmp_arg),
316 .nelem_hint = 3,
317 .locks_mul = 1,
318 .obj_cmpfn = ipmr_hash_cmp,
319 .automatic_shrinking = true,
320};
321
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000322static struct mr_table *ipmr_new_table(struct net *net, u32 id)
323{
324 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000325
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100326 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
327 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
328 return ERR_PTR(-EINVAL);
329
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000330 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100331 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000332 return mrt;
333
334 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100335 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100336 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200337 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000338 mrt->id = id;
339
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100340 rhltable_init(&mrt->mfc_hash, &ipmr_rht_params);
341 INIT_LIST_HEAD(&mrt->mfc_cache_list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000342 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
343
344 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
345 (unsigned long)mrt);
346
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000347 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000348#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
349 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
350#endif
351 return mrt;
352}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000354static void ipmr_free_table(struct mr_table *mrt)
355{
356 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100357 mroute_clean_tables(mrt, true);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100358 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000359 kfree(mrt);
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
363
Wang Chend6070322008-07-14 20:55:26 -0700364static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
365{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000366 struct net *net = dev_net(dev);
367
Wang Chend6070322008-07-14 20:55:26 -0700368 dev_close(dev);
369
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000370 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700371 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800372 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700373 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700374 struct ip_tunnel_parm p;
375
376 memset(&p, 0, sizeof(p));
377 p.iph.daddr = v->vifc_rmt_addr.s_addr;
378 p.iph.saddr = v->vifc_lcl_addr.s_addr;
379 p.iph.version = 4;
380 p.iph.ihl = 5;
381 p.iph.protocol = IPPROTO_IPIP;
382 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
383 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
384
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800385 if (ops->ndo_do_ioctl) {
386 mm_segment_t oldfs = get_fs();
387
388 set_fs(KERNEL_DS);
389 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
390 set_fs(oldfs);
391 }
Wang Chend6070322008-07-14 20:55:26 -0700392 }
393}
394
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100395/* Initialize ipmr pimreg/tunnel in_device */
396static bool ipmr_init_vif_indev(const struct net_device *dev)
397{
398 struct in_device *in_dev;
399
400 ASSERT_RTNL();
401
402 in_dev = __in_dev_get_rtnl(dev);
403 if (!in_dev)
404 return false;
405 ipv4_devconf_setall(in_dev);
406 neigh_parms_data_state_setall(in_dev->arp_parms);
407 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
408
409 return true;
410}
411
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100412static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct net_device *dev;
415
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000416 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800419 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int err;
421 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 memset(&p, 0, sizeof(p));
425 p.iph.daddr = v->vifc_rmt_addr.s_addr;
426 p.iph.saddr = v->vifc_lcl_addr.s_addr;
427 p.iph.version = 4;
428 p.iph.ihl = 5;
429 p.iph.protocol = IPPROTO_IPIP;
430 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800431 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800433 if (ops->ndo_do_ioctl) {
434 mm_segment_t oldfs = get_fs();
435
436 set_fs(KERNEL_DS);
437 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
438 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000439 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800440 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 dev = NULL;
443
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000444 if (err == 0 &&
445 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100447 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (dev_open(dev))
450 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700451 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 }
454 return dev;
455
456failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 unregister_netdevice(dev);
458 return NULL;
459}
460
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100461#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000462static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000464 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000465 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500466 struct flowi4 fl4 = {
467 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700468 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500469 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000470 };
471 int err;
472
David S. Millerda919812011-03-12 02:04:50 -0500473 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000474 if (err < 0) {
475 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000476 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000477 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700480 dev->stats.tx_bytes += skb->len;
481 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000482 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 read_unlock(&mrt_lock);
484 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000485 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200488static int reg_vif_get_iflink(const struct net_device *dev)
489{
490 return 0;
491}
492
Stephen Hemminger007c3832008-11-20 20:28:35 -0800493static const struct net_device_ops reg_vif_netdev_ops = {
494 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200495 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800496};
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static void reg_vif_setup(struct net_device *dev)
499{
500 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800501 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530503 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700505 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000508static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000511 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000513 if (mrt->id == RT_TABLE_DEFAULT)
514 sprintf(name, "pimreg");
515 else
516 sprintf(name, "pimreg%u", mrt->id);
517
Tom Gundersenc835a672014-07-14 16:37:24 +0200518 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Ian Morris51456b22015-04-03 09:17:26 +0100520 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return NULL;
522
Tom Goff403dbb92009-06-14 03:16:13 -0700523 dev_net_set(dev, net);
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (register_netdevice(dev)) {
526 free_netdev(dev);
527 return NULL;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100530 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (dev_open(dev))
533 goto failure;
534
Wang Chen7dc00c82008-07-14 20:56:34 -0700535 dev_hold(dev);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return dev;
538
539failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 unregister_netdevice(dev);
541 return NULL;
542}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100543
544/* called with rcu_read_lock() */
545static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
546 unsigned int pimlen)
547{
548 struct net_device *reg_dev = NULL;
549 struct iphdr *encap;
550
551 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100552 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100553 * a. packet is really sent to a multicast group
554 * b. packet is not a NULL-REGISTER
555 * c. packet is not truncated
556 */
557 if (!ipv4_is_multicast(encap->daddr) ||
558 encap->tot_len == 0 ||
559 ntohs(encap->tot_len) + pimlen > skb->len)
560 return 1;
561
562 read_lock(&mrt_lock);
563 if (mrt->mroute_reg_vif_num >= 0)
564 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
565 read_unlock(&mrt_lock);
566
567 if (!reg_dev)
568 return 1;
569
570 skb->mac_header = skb->network_header;
571 skb_pull(skb, (u8 *)encap - skb->data);
572 skb_reset_network_header(skb);
573 skb->protocol = htons(ETH_P_IP);
574 skb->ip_summed = CHECKSUM_NONE;
575
576 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
577
578 netif_rx(skb);
579
580 return NET_RX_SUCCESS;
581}
582#else
583static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
584{
585 return NULL;
586}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#endif
588
Ben Hutchings2c530402012-07-10 10:55:09 +0000589/**
590 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700591 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000593static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000594 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct vif_device *v;
597 struct net_device *dev;
598 struct in_device *in_dev;
599
Patrick McHardy0c122952010-04-13 05:03:22 +0000600 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return -EADDRNOTAVAIL;
602
Patrick McHardy0c122952010-04-13 05:03:22 +0000603 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 write_lock_bh(&mrt_lock);
606 dev = v->dev;
607 v->dev = NULL;
608
609 if (!dev) {
610 write_unlock_bh(&mrt_lock);
611 return -EADDRNOTAVAIL;
612 }
613
Patrick McHardy0c122952010-04-13 05:03:22 +0000614 if (vifi == mrt->mroute_reg_vif_num)
615 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000617 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000619
620 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000621 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 break;
623 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000624 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 write_unlock_bh(&mrt_lock);
628
629 dev_set_allmulti(dev, -1);
630
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000631 in_dev = __in_dev_get_rtnl(dev);
632 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700633 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700634 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000635 NETCONFA_MC_FORWARDING,
636 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ip_rt_multicast_event(in_dev);
638 }
639
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000640 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000641 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 dev_put(dev);
644 return 0;
645}
646
Eric Dumazeta8c94862010-10-01 16:15:08 +0000647static void ipmr_cache_free_rcu(struct rcu_head *head)
648{
649 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
650
651 kmem_cache_free(mrt_cachep, c);
652}
653
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000654static inline void ipmr_cache_free(struct mfc_cache *c)
655{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000656 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000660 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000662static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200664 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700666 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Patrick McHardy0c122952010-04-13 05:03:22 +0000668 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Jianjun Kongc354e122008-11-03 00:28:02 -0800670 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700671 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
673 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000674 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000676 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700677 e->error = -ETIMEDOUT;
678 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700679
Eric W. Biederman15e47302012-09-07 20:12:54 +0000680 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000681 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000686 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Patrick McHardye258beb2010-04-13 05:03:19 +0000689/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000690static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Patrick McHardy0c122952010-04-13 05:03:22 +0000692 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 unsigned long now;
694 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000695 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000698 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return;
700 }
701
Patrick McHardy0c122952010-04-13 05:03:22 +0000702 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto out;
704
705 now = jiffies;
706 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Patrick McHardy0c122952010-04-13 05:03:22 +0000708 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (time_after(c->mfc_un.unres.expires, now)) {
710 unsigned long interval = c->mfc_un.unres.expires - now;
711 if (interval < expires)
712 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 continue;
714 }
715
Patrick McHardy862465f2010-04-13 05:03:21 +0000716 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000717 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000718 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Patrick McHardy0c122952010-04-13 05:03:22 +0000721 if (!list_empty(&mrt->mfc_unres_queue))
722 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724out:
725 spin_unlock(&mfc_unres_lock);
726}
727
728/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000729static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000730 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 int vifi;
733
734 cache->mfc_un.res.minvif = MAXVIFS;
735 cache->mfc_un.res.maxvif = 0;
736 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
737
Patrick McHardy0c122952010-04-13 05:03:22 +0000738 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
739 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000740 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
742 if (cache->mfc_un.res.minvif > vifi)
743 cache->mfc_un.res.minvif = vifi;
744 if (cache->mfc_un.res.maxvif <= vifi)
745 cache->mfc_un.res.maxvif = vifi + 1;
746 }
747 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200748 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Patrick McHardy0c122952010-04-13 05:03:22 +0000751static int vif_add(struct net *net, struct mr_table *mrt,
752 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000755 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct net_device *dev;
757 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700758 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000761 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return -EADDRINUSE;
763
764 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100766 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100767 return -EINVAL;
768 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 * All the packets will be sent to the daemon
770 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000771 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000773 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!dev)
775 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700776 err = dev_set_allmulti(dev, 1);
777 if (err) {
778 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700779 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700780 return err;
781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900783 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000784 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!dev)
786 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700787 err = dev_set_allmulti(dev, 1);
788 if (err) {
789 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700790 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700791 return err;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000794 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000796 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
797 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100798 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000799 dev_put(dev);
800 return -EADDRNOTAVAIL;
801 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000802 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000803 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!dev)
806 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700807 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700808 if (err) {
809 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700810 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813 default:
814 return -EINVAL;
815 }
816
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000817 in_dev = __in_dev_get_rtnl(dev);
818 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000819 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000821 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700822 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700823 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
824 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 ip_rt_multicast_event(in_dev);
826
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000827 /* Fill in the VIF structures */
828
Jianjun Kongc354e122008-11-03 00:28:02 -0800829 v->rate_limit = vifc->vifc_rate_limit;
830 v->local = vifc->vifc_lcl_addr.s_addr;
831 v->remote = vifc->vifc_rmt_addr.s_addr;
832 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (!mrtsock)
834 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800835 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 v->bytes_in = 0;
837 v->bytes_out = 0;
838 v->pkt_in = 0;
839 v->pkt_out = 0;
840 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000841 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200842 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 /* And finish update writing critical data */
845 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800846 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000847 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000848 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000849 if (vifi+1 > mrt->maxvif)
850 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 write_unlock_bh(&mrt_lock);
852 return 0;
853}
854
Eric Dumazeta8c94862010-10-01 16:15:08 +0000855/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000856static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000857 __be32 origin,
858 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100860 struct mfc_cache_cmp_arg arg = {
861 .mfc_mcastgrp = mcastgrp,
862 .mfc_origin = origin
863 };
864 struct rhlist_head *tmp, *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 struct mfc_cache *c;
866
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100867 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
868 rhl_for_each_entry_rcu(c, tmp, list, mnode)
869 return c;
870
Patrick McHardy862465f2010-04-13 05:03:21 +0000871 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000874/* Look for a (*,*,oif) entry */
875static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
876 int vifi)
877{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100878 struct mfc_cache_cmp_arg arg = {
879 .mfc_mcastgrp = htonl(INADDR_ANY),
880 .mfc_origin = htonl(INADDR_ANY)
881 };
882 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000883 struct mfc_cache *c;
884
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100885 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
886 rhl_for_each_entry_rcu(c, tmp, list, mnode)
887 if (c->mfc_un.res.ttls[vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000888 return c;
889
890 return NULL;
891}
892
893/* Look for a (*,G) entry */
894static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
895 __be32 mcastgrp, int vifi)
896{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100897 struct mfc_cache_cmp_arg arg = {
898 .mfc_mcastgrp = mcastgrp,
899 .mfc_origin = htonl(INADDR_ANY)
900 };
901 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000902 struct mfc_cache *c, *proxy;
903
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100904 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000905 goto skip;
906
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100907 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
908 rhl_for_each_entry_rcu(c, tmp, list, mnode) {
909 if (c->mfc_un.res.ttls[vifi] < 255)
910 return c;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000911
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100912 /* It's ok if the vifi is part of the static tree */
913 proxy = ipmr_cache_find_any_parent(mrt, c->mfc_parent);
914 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
915 return c;
916 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000917
918skip:
919 return ipmr_cache_find_any_parent(mrt, vifi);
920}
921
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100922/* Look for a (S,G,iif) entry if parent != -1 */
923static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
924 __be32 origin, __be32 mcastgrp,
925 int parent)
926{
927 struct mfc_cache_cmp_arg arg = {
928 .mfc_mcastgrp = mcastgrp,
929 .mfc_origin = origin,
930 };
931 struct rhlist_head *tmp, *list;
932 struct mfc_cache *c;
933
934 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
935 rhl_for_each_entry_rcu(c, tmp, list, mnode)
936 if (parent == -1 || parent == c->mfc_parent)
937 return c;
938
939 return NULL;
940}
941
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100942/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000943static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Jianjun Kongc354e122008-11-03 00:28:02 -0800945 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000946
Tom Goff70a0dec2016-06-23 16:11:57 -0400947 if (c) {
948 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
Eric Dumazeta8c94862010-10-01 16:15:08 +0000949 c->mfc_un.res.minvif = MAXVIFS;
Tom Goff70a0dec2016-06-23 16:11:57 -0400950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return c;
952}
953
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000954static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Jianjun Kongc354e122008-11-03 00:28:02 -0800956 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000957
958 if (c) {
959 skb_queue_head_init(&c->mfc_un.unres.unresolved);
960 c->mfc_un.unres.expires = jiffies + 10*HZ;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return c;
963}
964
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100965/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000966static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
967 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700970 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000972 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800973 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700974 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
976
Hong zhi guo573ce262013-03-27 06:47:04 +0000977 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000978 nlh->nlmsg_len = skb_tail_pointer(skb) -
979 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 } else {
981 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000982 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000984 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700985 e->error = -EMSGSIZE;
986 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Thomas Graf2942e902006-08-15 00:30:25 -0700988
Eric W. Biederman15e47302012-09-07 20:12:54 +0000989 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000990 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000991 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994}
995
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100996/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
997 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100999 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001001static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001002 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001004 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001005 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct igmphdr *igmp;
1007 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001008 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int ret;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (assert == IGMPMSG_WHOLEPKT)
1012 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1013 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 skb = alloc_skb(128, GFP_ATOMIC);
1015
Stephen Hemminger132adf52007-03-08 20:44:43 -08001016 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -ENOBUFS;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (assert == IGMPMSG_WHOLEPKT) {
1020 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001021 * Duplicate old header, fix ihl, length etc.
1022 * And all this only to mangle msg->im_msgtype and
1023 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001025 skb_push(skb, sizeof(struct iphdr));
1026 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001027 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001028 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001029 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1031 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001032 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001033 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1034 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1035 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001036 } else {
1037 /* Copy the IP header */
1038 skb_set_network_header(skb, skb->len);
1039 skb_put(skb, ihl);
1040 skb_copy_to_linear_data(skb, pkt->data, ihl);
1041 /* Flag to the kernel this is a route add */
1042 ip_hdr(skb)->protocol = 0;
1043 msg = (struct igmpmsg *)skb_network_header(skb);
1044 msg->im_vif = vifi;
1045 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1046 /* Add our header */
1047 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1048 igmp->type = assert;
1049 msg->im_msgtype = assert;
1050 igmp->code = 0;
1051 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1052 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Eric Dumazet4c9687092010-10-01 16:15:01 +00001055 rcu_read_lock();
1056 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001057 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001058 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 kfree_skb(skb);
1060 return -EINVAL;
1061 }
1062
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001063 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001064 ret = sock_queue_rcv_skb(mroute_sk, skb);
1065 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001066 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001067 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 kfree_skb(skb);
1069 }
1070
1071 return ret;
1072}
1073
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001074/* Queue a packet for resolution. It gets locked cache entry! */
1075static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1076 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001078 const struct iphdr *iph = ip_hdr(skb);
1079 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001080 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001084 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001085 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001086 c->mfc_origin == iph->saddr) {
1087 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
Patrick McHardy862465f2010-04-13 05:03:21 +00001092 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001093 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001094 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001095 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 spin_unlock_bh(&mfc_unres_lock);
1097
1098 kfree_skb(skb);
1099 return -ENOBUFS;
1100 }
1101
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001102 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001103 c->mfc_parent = -1;
1104 c->mfc_origin = iph->saddr;
1105 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001107 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001108 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001109 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001110 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 out - Brad Parker
1112 */
1113 spin_unlock_bh(&mfc_unres_lock);
1114
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001115 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 kfree_skb(skb);
1117 return err;
1118 }
1119
Patrick McHardy0c122952010-04-13 05:03:22 +00001120 atomic_inc(&mrt->cache_resolve_queue_len);
1121 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001122 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
David S. Miller278554b2010-05-12 00:05:35 -07001124 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1125 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001128 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001129 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 kfree_skb(skb);
1131 err = -ENOBUFS;
1132 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001133 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 err = 0;
1135 }
1136
1137 spin_unlock_bh(&mfc_unres_lock);
1138 return err;
1139}
1140
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001141/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001143static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001145 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001147 /* The entries are added/deleted only under RTNL */
1148 rcu_read_lock();
1149 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1150 mfc->mfcc_mcastgrp.s_addr, parent);
1151 rcu_read_unlock();
1152 if (!c)
1153 return -ENOENT;
1154 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1155 list_del_rcu(&c->list);
1156 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1157 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Patrick McHardy0c122952010-04-13 05:03:22 +00001162static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001163 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Patrick McHardy862465f2010-04-13 05:03:21 +00001165 struct mfc_cache *uc, *c;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001166 bool found;
1167 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Patrick McHardya50436f22010-03-17 06:04:14 +00001169 if (mfc->mfcc_parent >= MAXVIFS)
1170 return -ENFILE;
1171
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001172 /* The entries are added/deleted only under RTNL */
1173 rcu_read_lock();
1174 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1175 mfc->mfcc_mcastgrp.s_addr, parent);
1176 rcu_read_unlock();
1177 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 write_lock_bh(&mrt_lock);
1179 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001180 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!mrtsock)
1182 c->mfc_flags |= MFC_STATIC;
1183 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001184 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return 0;
1186 }
1187
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001188 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001189 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return -EINVAL;
1191
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001192 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001193 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return -ENOMEM;
1195
Jianjun Kongc354e122008-11-03 00:28:02 -08001196 c->mfc_origin = mfc->mfcc_origin.s_addr;
1197 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1198 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001199 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (!mrtsock)
1201 c->mfc_flags |= MFC_STATIC;
1202
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001203 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->mnode,
1204 ipmr_rht_params);
1205 if (ret) {
1206 pr_err("ipmr: rhtable insert error %d\n", ret);
1207 ipmr_cache_free(c);
1208 return ret;
1209 }
1210 list_add_tail_rcu(&c->list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001211 /* Check to see if we resolved a queued list. If so we
1212 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001214 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001216 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001217 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001219 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001220 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001221 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 break;
1223 }
1224 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001225 if (list_empty(&mrt->mfc_unres_queue))
1226 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 spin_unlock_bh(&mfc_unres_lock);
1228
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001229 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001230 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001231 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001233 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return 0;
1235}
1236
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001237/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001238static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001240 struct mfc_cache *c, *tmp;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001241 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001242 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001243
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001244 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001245 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001246 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1247 continue;
1248 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001250 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001252 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001253 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1254 if (!all && (c->mfc_flags & MFC_STATIC))
1255 continue;
1256 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1257 list_del_rcu(&c->list);
1258 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1259 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261
Patrick McHardy0c122952010-04-13 05:03:22 +00001262 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001264 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001265 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001266 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001267 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 spin_unlock_bh(&mfc_unres_lock);
1270 }
1271}
1272
Eric Dumazet4c9687092010-10-01 16:15:01 +00001273/* called from ip_ra_control(), before an RCU grace period,
1274 * we dont need to call synchronize_rcu() here
1275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static void mrtsock_destruct(struct sock *sk)
1277{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001278 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001279 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001280
WANG Cong1215e512017-04-12 12:32:13 -07001281 ASSERT_RTNL();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001282 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001283 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001284 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001285 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1286 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001287 NETCONFA_IFINDEX_ALL,
1288 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001289 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001290 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001295/* Socket options and virtual interface manipulation. The whole
1296 * virtual interface system is a complete heap, but unfortunately
1297 * that's how BSD mrouted happens to think. Maybe one day with a proper
1298 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001300
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001301int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1302 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001304 struct net *net = sock_net(sk);
1305 int val, ret = 0, parent = 0;
1306 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct vifctl vif;
1308 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001309 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001310
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001311 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1312 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001313 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001314 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1315 ret = -EOPNOTSUPP;
1316 goto out_unlock;
1317 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001318
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001319 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001320 if (!mrt) {
1321 ret = -ENOENT;
1322 goto out_unlock;
1323 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001324 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001325 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001326 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1327 ret = -EACCES;
1328 goto out_unlock;
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
Stephen Hemminger132adf52007-03-08 20:44:43 -08001332 switch (optname) {
1333 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001334 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001335 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001336 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001337 }
1338 if (rtnl_dereference(mrt->mroute_sk)) {
1339 ret = -EADDRINUSE;
1340 break;
1341 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001342
1343 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1344 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001345 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001346 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001347 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1348 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001349 NETCONFA_IFINDEX_ALL,
1350 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001351 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001352 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001353 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001354 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1355 ret = -EACCES;
1356 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001357 ret = ip_ra_control(sk, 0, NULL);
WANG Cong1215e512017-04-12 12:32:13 -07001358 goto out_unlock;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001359 }
1360 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 case MRT_ADD_VIF:
1362 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001363 if (optlen != sizeof(vif)) {
1364 ret = -EINVAL;
1365 break;
1366 }
1367 if (copy_from_user(&vif, optval, sizeof(vif))) {
1368 ret = -EFAULT;
1369 break;
1370 }
1371 if (vif.vifc_vifi >= MAXVIFS) {
1372 ret = -ENFILE;
1373 break;
1374 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001375 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001376 ret = vif_add(net, mrt, &vif,
1377 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001378 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001379 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001380 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001381 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001382 /* Manipulate the forwarding caches. These live
1383 * in a sort of kernel/user symbiosis.
1384 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001385 case MRT_ADD_MFC:
1386 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001387 parent = -1;
1388 case MRT_ADD_MFC_PROXY:
1389 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001390 if (optlen != sizeof(mfc)) {
1391 ret = -EINVAL;
1392 break;
1393 }
1394 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1395 ret = -EFAULT;
1396 break;
1397 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001398 if (parent == 0)
1399 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001400 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1401 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001402 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001403 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001404 sk == rtnl_dereference(mrt->mroute_sk),
1405 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001406 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001407 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001408 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001409 if (optlen != sizeof(val)) {
1410 ret = -EINVAL;
1411 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001413 if (get_user(val, (int __user *)optval)) {
1414 ret = -EFAULT;
1415 break;
1416 }
1417 mrt->mroute_do_assert = val;
1418 break;
1419 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001420 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001421 ret = -ENOPROTOOPT;
1422 break;
1423 }
1424 if (optlen != sizeof(val)) {
1425 ret = -EINVAL;
1426 break;
1427 }
1428 if (get_user(val, (int __user *)optval)) {
1429 ret = -EFAULT;
1430 break;
1431 }
1432
1433 val = !!val;
1434 if (val != mrt->mroute_do_pim) {
1435 mrt->mroute_do_pim = val;
1436 mrt->mroute_do_assert = val;
1437 }
1438 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001439 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001440 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1441 ret = -ENOPROTOOPT;
1442 break;
1443 }
1444 if (optlen != sizeof(uval)) {
1445 ret = -EINVAL;
1446 break;
1447 }
1448 if (get_user(uval, (u32 __user *)optval)) {
1449 ret = -EFAULT;
1450 break;
1451 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001452
Eric Dumazet4c9687092010-10-01 16:15:01 +00001453 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1454 ret = -EBUSY;
1455 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001456 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001457 if (IS_ERR(mrt))
1458 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001459 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001460 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001461 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001462 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001463 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001464 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001465 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001467out_unlock:
1468 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001472/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001473int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 int olr;
1476 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001477 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001478 struct mr_table *mrt;
1479
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001480 if (sk->sk_type != SOCK_RAW ||
1481 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1482 return -EOPNOTSUPP;
1483
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001484 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001485 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001486 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001488 switch (optname) {
1489 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001490 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001491 break;
1492 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001493 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001494 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001495 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001496 break;
1497 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001498 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001499 break;
1500 default:
1501 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001502 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001503
1504 if (get_user(olr, optlen))
1505 return -EFAULT;
1506 olr = min_t(unsigned int, olr, sizeof(int));
1507 if (olr < 0)
1508 return -EINVAL;
1509 if (put_user(olr, optlen))
1510 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001511 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EFAULT;
1513 return 0;
1514}
1515
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001516/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1518{
1519 struct sioc_sg_req sr;
1520 struct sioc_vif_req vr;
1521 struct vif_device *vif;
1522 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001523 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001524 struct mr_table *mrt;
1525
1526 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001527 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001528 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001529
Stephen Hemminger132adf52007-03-08 20:44:43 -08001530 switch (cmd) {
1531 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001532 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001533 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001534 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001535 return -EINVAL;
1536 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001537 vif = &mrt->vif_table[vr.vifi];
1538 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001539 vr.icount = vif->pkt_in;
1540 vr.ocount = vif->pkt_out;
1541 vr.ibytes = vif->bytes_in;
1542 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001544
Jianjun Kongc354e122008-11-03 00:28:02 -08001545 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001547 return 0;
1548 }
1549 read_unlock(&mrt_lock);
1550 return -EADDRNOTAVAIL;
1551 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001552 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001553 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Eric Dumazeta8c94862010-10-01 16:15:08 +00001555 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001556 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001557 if (c) {
1558 sr.pktcnt = c->mfc_un.res.pkt;
1559 sr.bytecnt = c->mfc_un.res.bytes;
1560 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001561 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001562
Jianjun Kongc354e122008-11-03 00:28:02 -08001563 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001564 return -EFAULT;
1565 return 0;
1566 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001567 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001568 return -EADDRNOTAVAIL;
1569 default:
1570 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572}
1573
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001574#ifdef CONFIG_COMPAT
1575struct compat_sioc_sg_req {
1576 struct in_addr src;
1577 struct in_addr grp;
1578 compat_ulong_t pktcnt;
1579 compat_ulong_t bytecnt;
1580 compat_ulong_t wrong_if;
1581};
1582
David S. Millerca6b8bb02011-02-03 17:24:28 -08001583struct compat_sioc_vif_req {
1584 vifi_t vifi; /* Which iface */
1585 compat_ulong_t icount;
1586 compat_ulong_t ocount;
1587 compat_ulong_t ibytes;
1588 compat_ulong_t obytes;
1589};
1590
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001591int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1592{
David S. Miller0033d5a2011-02-03 17:21:31 -08001593 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001594 struct compat_sioc_vif_req vr;
1595 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001596 struct mfc_cache *c;
1597 struct net *net = sock_net(sk);
1598 struct mr_table *mrt;
1599
1600 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001601 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001602 return -ENOENT;
1603
1604 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001605 case SIOCGETVIFCNT:
1606 if (copy_from_user(&vr, arg, sizeof(vr)))
1607 return -EFAULT;
1608 if (vr.vifi >= mrt->maxvif)
1609 return -EINVAL;
1610 read_lock(&mrt_lock);
1611 vif = &mrt->vif_table[vr.vifi];
1612 if (VIF_EXISTS(mrt, vr.vifi)) {
1613 vr.icount = vif->pkt_in;
1614 vr.ocount = vif->pkt_out;
1615 vr.ibytes = vif->bytes_in;
1616 vr.obytes = vif->bytes_out;
1617 read_unlock(&mrt_lock);
1618
1619 if (copy_to_user(arg, &vr, sizeof(vr)))
1620 return -EFAULT;
1621 return 0;
1622 }
1623 read_unlock(&mrt_lock);
1624 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001625 case SIOCGETSGCNT:
1626 if (copy_from_user(&sr, arg, sizeof(sr)))
1627 return -EFAULT;
1628
1629 rcu_read_lock();
1630 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1631 if (c) {
1632 sr.pktcnt = c->mfc_un.res.pkt;
1633 sr.bytecnt = c->mfc_un.res.bytes;
1634 sr.wrong_if = c->mfc_un.res.wrong_if;
1635 rcu_read_unlock();
1636
1637 if (copy_to_user(arg, &sr, sizeof(sr)))
1638 return -EFAULT;
1639 return 0;
1640 }
1641 rcu_read_unlock();
1642 return -EADDRNOTAVAIL;
1643 default:
1644 return -ENOIOCTLCMD;
1645 }
1646}
1647#endif
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1650{
Jiri Pirko351638e2013-05-28 01:30:21 +00001651 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001652 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001653 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct vif_device *v;
1655 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (event != NETDEV_UNREGISTER)
1658 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001659
1660 ipmr_for_each_table(mrt, net) {
1661 v = &mrt->vif_table[0];
1662 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1663 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001664 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 return NOTIFY_DONE;
1668}
1669
Jianjun Kongc354e122008-11-03 00:28:02 -08001670static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 .notifier_call = ipmr_device_event,
1672};
1673
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001674/* Encapsulate a packet by attaching a valid IPIP header to it.
1675 * This avoids tunnel drivers and other mess and gives us the speed so
1676 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001678static void ip_encap(struct net *net, struct sk_buff *skb,
1679 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001681 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001682 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001683
1684 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001685 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001686 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001687 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001689 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001690 iph->tos = old_iph->tos;
1691 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 iph->frag_off = 0;
1693 iph->daddr = daddr;
1694 iph->saddr = saddr;
1695 iph->protocol = IPPROTO_IPIP;
1696 iph->ihl = 5;
1697 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001698 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 ip_send_check(iph);
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1702 nf_reset(skb);
1703}
1704
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001705static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1706 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001708 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
David S. Miller73186df2015-11-03 13:41:45 -05001710 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1711 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 if (unlikely(opt->optlen))
1714 ip_forward_options(skb);
1715
Eric W. Biederman13206b62015-10-07 16:48:35 -05001716 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001719/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Patrick McHardy0c122952010-04-13 05:03:22 +00001721static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1722 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001724 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001725 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 struct net_device *dev;
1727 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001728 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 int encap = 0;
1730
Ian Morris51456b22015-04-03 09:17:26 +01001731 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto out_free;
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (vif->flags & VIFF_REGISTER) {
1735 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001736 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001737 vif->dev->stats.tx_bytes += skb->len;
1738 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001739 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001740 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001743 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001744 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001745 vif->remote, vif->local,
1746 0, 0,
1747 IPPROTO_IPIP,
1748 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001749 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 goto out_free;
1751 encap = sizeof(struct iphdr);
1752 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001753 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001754 0, 0,
1755 IPPROTO_IPIP,
1756 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001757 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 goto out_free;
1759 }
1760
Changli Gaod8d1f302010-06-10 23:31:35 -07001761 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Changli Gaod8d1f302010-06-10 23:31:35 -07001763 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001765 * allow to send ICMP, so that packets will disappear
1766 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
David S. Miller73186df2015-11-03 13:41:45 -05001768 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 ip_rt_put(rt);
1770 goto out_free;
1771 }
1772
Changli Gaod8d1f302010-06-10 23:31:35 -07001773 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001776 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 goto out_free;
1778 }
1779
1780 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001781 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Eric Dumazetadf30902009-06-02 05:19:30 +00001783 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001784 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001785 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001788 * What do we do with netfilter? -- RR
1789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001791 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001793 vif->dev->stats.tx_packets++;
1794 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001797 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001799 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 * not only before forwarding, but after forwarding on all output
1801 * interfaces. It is clear, if mrouter runs a multicasting
1802 * program, it should receive packets not depending to what interface
1803 * program is joined.
1804 * If we will not make it, the program will have to join on all
1805 * interfaces. On the other hand, multihoming host (or router, but
1806 * not mrouter) cannot join to more than one interface - it will
1807 * result in receiving multiple packets.
1808 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001809 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1810 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 ipmr_forward_finish);
1812 return;
1813
1814out_free:
1815 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Patrick McHardy0c122952010-04-13 05:03:22 +00001818static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001821
1822 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1823 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 break;
1825 }
1826 return ct;
1827}
1828
1829/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001830static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1831 struct sk_buff *skb, struct mfc_cache *cache,
1832 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001834 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 int psend = -1;
1836 int vif, ct;
1837
1838 vif = cache->mfc_parent;
1839 cache->mfc_un.res.pkt++;
1840 cache->mfc_un.res.bytes += skb->len;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03001841 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001843 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001844 struct mfc_cache *cache_proxy;
1845
1846 /* For an (*,G) entry, we only check that the incomming
1847 * interface is part of the static tree.
1848 */
1849 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1850 if (cache_proxy &&
1851 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1852 goto forward;
1853 }
1854
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001855 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001856 if (mrt->vif_table[vif].dev != skb->dev) {
David Aherne58e4152016-10-31 15:54:00 -07001857 struct net_device *mdev;
1858
1859 mdev = l3mdev_master_dev_rcu(mrt->vif_table[vif].dev);
1860 if (mdev == skb->dev)
1861 goto forward;
1862
David S. Millerc7537962010-11-11 17:07:48 -08001863 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001865 * Very complicated situation...
1866 *
1867 * The best workaround until routing daemons will be
1868 * fixed is not to redistribute packet, if it was
1869 * send through wrong interface. It means, that
1870 * multicast applications WILL NOT work for
1871 * (S,G), which have default multicast route pointing
1872 * to wrong oif. In any case, it is not a good
1873 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
1875 goto dont_forward;
1876 }
1877
1878 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Patrick McHardy0c122952010-04-13 05:03:22 +00001880 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001882 * so that we cannot check that packet arrived on an oif.
1883 * It is bad, but otherwise we would need to move pretty
1884 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001886 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001887 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001888 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1890 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001891 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
1893 goto dont_forward;
1894 }
1895
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001896forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001897 mrt->vif_table[vif].pkt_in++;
1898 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001900 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001901 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1902 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001903 if (true_vifi >= 0 &&
1904 true_vifi != cache->mfc_parent &&
1905 ip_hdr(skb)->ttl >
1906 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1907 /* It's an (*,*) entry and the packet is not coming from
1908 * the upstream: forward the packet to the upstream
1909 * only.
1910 */
1911 psend = cache->mfc_parent;
1912 goto last_forward;
1913 }
1914 goto dont_forward;
1915 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001916 for (ct = cache->mfc_un.res.maxvif - 1;
1917 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001918 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001919 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1920 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001921 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (psend != -1) {
1923 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001926 ipmr_queue_xmit(net, mrt, skb2, cache,
1927 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001929 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
1931 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001932last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (psend != -1) {
1934 if (local) {
1935 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001938 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001940 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 }
1944
1945dont_forward:
1946 if (!local)
1947 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
David S. Miller417da662011-05-03 19:42:43 -07001950static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001951{
David S. Miller417da662011-05-03 19:42:43 -07001952 struct rtable *rt = skb_rtable(skb);
1953 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001954 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001955 .daddr = iph->daddr,
1956 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001957 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001958 .flowi4_oif = (rt_is_output_route(rt) ?
1959 skb->dev->ifindex : 0),
1960 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001961 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001962 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001963 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001964 };
1965 struct mr_table *mrt;
1966 int err;
1967
David S. Millerda919812011-03-12 02:04:50 -05001968 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001969 if (err)
1970 return ERR_PTR(err);
1971 return mrt;
1972}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001974/* Multicast packets for forwarding arrive here
1975 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977int ip_mr_input(struct sk_buff *skb)
1978{
1979 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001980 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001981 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001982 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12001983 struct net_device *dev;
1984
1985 /* skb->dev passed in is the loX master dev for vrfs.
1986 * As there are no vifs associated with loopback devices,
1987 * get the proper interface that does have a vif associated with it.
1988 */
1989 dev = skb->dev;
1990 if (netif_is_l3_master(skb->dev)) {
1991 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
1992 if (!dev) {
1993 kfree_skb(skb);
1994 return -ENODEV;
1995 }
1996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001999 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002001 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 goto dont_forward;
2003
David S. Miller417da662011-05-03 19:42:43 -07002004 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002005 if (IS_ERR(mrt)) {
2006 kfree_skb(skb);
2007 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002010 if (IPCB(skb)->opt.router_alert) {
2011 if (ip_call_ra_chain(skb))
2012 return 0;
2013 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2014 /* IGMPv1 (and broken IGMPv2 implementations sort of
2015 * Cisco IOS <= 11.2(8)) do not put router alert
2016 * option to IGMP packets destined to routable
2017 * groups. It is very bad, because it means
2018 * that we can forward NO IGMP messages.
2019 */
2020 struct sock *mroute_sk;
2021
2022 mroute_sk = rcu_dereference(mrt->mroute_sk);
2023 if (mroute_sk) {
2024 nf_reset(skb);
2025 raw_rcv(mroute_sk, skb);
2026 return 0;
2027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029 }
2030
Eric Dumazeta8c94862010-10-01 16:15:08 +00002031 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002032 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002033 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002034 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002035
2036 if (vif >= 0)
2037 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2038 vif);
2039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002041 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002042 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 int vif;
2044
2045 if (local) {
2046 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2047 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002048 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 skb = skb2;
2051 }
2052
Eric Dumazeta8c94862010-10-01 16:15:08 +00002053 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002054 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002056 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 read_unlock(&mrt_lock);
2058
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002059 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061 read_unlock(&mrt_lock);
2062 kfree_skb(skb);
2063 return -ENODEV;
2064 }
2065
Eric Dumazeta8c94862010-10-01 16:15:08 +00002066 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002067 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 read_unlock(&mrt_lock);
2069
2070 if (local)
2071 return ip_local_deliver(skb);
2072
2073 return 0;
2074
2075dont_forward:
2076 if (local)
2077 return ip_local_deliver(skb);
2078 kfree_skb(skb);
2079 return 0;
2080}
2081
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002082#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002083/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002084int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002085{
2086 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002087 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002088 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002089
2090 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2091 goto drop;
2092
2093 pim = igmp_hdr(skb);
2094
David S. Miller417da662011-05-03 19:42:43 -07002095 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002096 if (IS_ERR(mrt))
2097 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002098 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002099 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2100 goto drop;
2101
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002102 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002103drop:
2104 kfree_skb(skb);
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return 0;
2107}
2108#endif
2109
2110#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002111static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002114 struct net *net = dev_net(skb->dev);
2115 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002117 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 goto drop;
2119
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002120 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002121 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002122 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002123 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002124 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 goto drop;
2126
David S. Miller417da662011-05-03 19:42:43 -07002127 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002128 if (IS_ERR(mrt))
2129 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002130 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002131drop:
2132 kfree_skb(skb);
2133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return 0;
2135}
2136#endif
2137
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002138static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2139 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002141 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002142 struct nlattr *mp_attr;
2143 struct rtnexthop *nhp;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002144 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002145 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Nicolas Dichtel74381892010-03-25 23:45:35 +00002147 /* If cache is unresolved, don't try to parse IIF and OIF */
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002148 if (c->mfc_parent >= MAXVIFS) {
2149 rtm->rtm_flags |= RTNH_F_UNRESOLVED;
Nicolas Dichtel74381892010-03-25 23:45:35 +00002150 return -ENOENT;
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002151 }
Nicolas Dichtel74381892010-03-25 23:45:35 +00002152
Thomas Graf92a395e2012-06-26 23:36:13 +00002153 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2154 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2155 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Thomas Graf92a395e2012-06-26 23:36:13 +00002157 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2158 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002161 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002162 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2163 nla_nest_cancel(skb, mp_attr);
2164 return -EMSGSIZE;
2165 }
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 nhp->rtnh_flags = 0;
2168 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002169 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 nhp->rtnh_len = sizeof(*nhp);
2171 }
2172 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002173
2174 nla_nest_end(skb, mp_attr);
2175
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002176 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2177 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2178
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002179 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2180 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2181 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002182 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002183 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002184 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002185 return -EMSGSIZE;
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 rtm->rtm_type = RTN_MULTICAST;
2188 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
David S. Miller9a1b9492011-05-04 12:18:54 -07002191int ipmr_get_route(struct net *net, struct sk_buff *skb,
2192 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002193 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002196 struct mr_table *mrt;
2197 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002199 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002200 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002201 return -ENOENT;
2202
Eric Dumazeta8c94862010-10-01 16:15:08 +00002203 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002204 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002205 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002206 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002208 if (vif >= 0)
2209 cache = ipmr_cache_find_any(mrt, daddr, vif);
2210 }
Ian Morris51456b22015-04-03 09:17:26 +01002211 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002212 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002213 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002215 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002218 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002219 if (dev)
2220 vif = ipmr_find_vif(mrt, dev);
2221 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002223 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return -ENODEV;
2225 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002226 skb2 = skb_clone(skb, GFP_ATOMIC);
2227 if (!skb2) {
2228 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002229 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002230 return -ENOMEM;
2231 }
2232
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002233 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002234 skb_push(skb2, sizeof(struct iphdr));
2235 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002236 iph = ip_hdr(skb2);
2237 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002238 iph->saddr = saddr;
2239 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002240 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002241 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002243 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return err;
2245 }
2246
Eric Dumazeta8c94862010-10-01 16:15:08 +00002247 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002248 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002250 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return err;
2252}
2253
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002254static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002255 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2256 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002257{
2258 struct nlmsghdr *nlh;
2259 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002260 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002261
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002262 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002263 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002264 return -EMSGSIZE;
2265
2266 rtm = nlmsg_data(nlh);
2267 rtm->rtm_family = RTNL_FAMILY_IPMR;
2268 rtm->rtm_dst_len = 32;
2269 rtm->rtm_src_len = 32;
2270 rtm->rtm_tos = 0;
2271 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002272 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2273 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002274 rtm->rtm_type = RTN_MULTICAST;
2275 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002276 if (c->mfc_flags & MFC_STATIC)
2277 rtm->rtm_protocol = RTPROT_STATIC;
2278 else
2279 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002280 rtm->rtm_flags = 0;
2281
Jiri Benc930345e2015-03-29 16:59:25 +02002282 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2283 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002284 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002285 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2286 /* do not break the dump if cache is unresolved */
2287 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002288 goto nla_put_failure;
2289
Johannes Berg053c0952015-01-16 22:09:00 +01002290 nlmsg_end(skb, nlh);
2291 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002292
2293nla_put_failure:
2294 nlmsg_cancel(skb, nlh);
2295 return -EMSGSIZE;
2296}
2297
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002298static size_t mroute_msgsize(bool unresolved, int maxvif)
2299{
2300 size_t len =
2301 NLMSG_ALIGN(sizeof(struct rtmsg))
2302 + nla_total_size(4) /* RTA_TABLE */
2303 + nla_total_size(4) /* RTA_SRC */
2304 + nla_total_size(4) /* RTA_DST */
2305 ;
2306
2307 if (!unresolved)
2308 len = len
2309 + nla_total_size(4) /* RTA_IIF */
2310 + nla_total_size(0) /* RTA_MULTIPATH */
2311 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2312 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002313 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002314 ;
2315
2316 return len;
2317}
2318
2319static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2320 int cmd)
2321{
2322 struct net *net = read_pnet(&mrt->net);
2323 struct sk_buff *skb;
2324 int err = -ENOBUFS;
2325
2326 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2327 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002328 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002329 goto errout;
2330
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002331 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002332 if (err < 0)
2333 goto errout;
2334
2335 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2336 return;
2337
2338errout:
2339 kfree_skb(skb);
2340 if (err < 0)
2341 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2342}
2343
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002344static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2345{
2346 struct net *net = sock_net(skb->sk);
2347 struct mr_table *mrt;
2348 struct mfc_cache *mfc;
2349 unsigned int t = 0, s_t;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002350 unsigned int e = 0, s_e;
2351
2352 s_t = cb->args[0];
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002353 s_e = cb->args[1];
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002354
Eric Dumazeta8c94862010-10-01 16:15:08 +00002355 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002356 ipmr_for_each_table(mrt, net) {
2357 if (t < s_t)
2358 goto next_table;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002359 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
2360 if (e < s_e)
2361 goto next_entry;
2362 if (ipmr_fill_mroute(mrt, skb,
2363 NETLINK_CB(cb->skb).portid,
2364 cb->nlh->nlmsg_seq,
2365 mfc, RTM_NEWROUTE,
2366 NLM_F_MULTI) < 0)
2367 goto done;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002368next_entry:
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002369 e++;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002370 }
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002371 e = 0;
2372 s_e = 0;
2373
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002374 spin_lock_bh(&mfc_unres_lock);
2375 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2376 if (e < s_e)
2377 goto next_entry2;
2378 if (ipmr_fill_mroute(mrt, skb,
2379 NETLINK_CB(cb->skb).portid,
2380 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002381 mfc, RTM_NEWROUTE,
2382 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002383 spin_unlock_bh(&mfc_unres_lock);
2384 goto done;
2385 }
2386next_entry2:
2387 e++;
2388 }
2389 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002390 e = 0;
2391 s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002392next_table:
2393 t++;
2394 }
2395done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002396 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002397
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002398 cb->args[1] = e;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002399 cb->args[0] = t;
2400
2401 return skb->len;
2402}
2403
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002404static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2405 [RTA_SRC] = { .type = NLA_U32 },
2406 [RTA_DST] = { .type = NLA_U32 },
2407 [RTA_IIF] = { .type = NLA_U32 },
2408 [RTA_TABLE] = { .type = NLA_U32 },
2409 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2410};
2411
2412static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2413{
2414 switch (rtm_protocol) {
2415 case RTPROT_STATIC:
2416 case RTPROT_MROUTED:
2417 return true;
2418 }
2419 return false;
2420}
2421
2422static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2423{
2424 struct rtnexthop *rtnh = nla_data(nla);
2425 int remaining = nla_len(nla), vifi = 0;
2426
2427 while (rtnh_ok(rtnh, remaining)) {
2428 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2429 if (++vifi == MAXVIFS)
2430 break;
2431 rtnh = rtnh_next(rtnh, &remaining);
2432 }
2433
2434 return remaining > 0 ? -EINVAL : vifi;
2435}
2436
2437/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2438static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2439 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002440 struct mr_table **mrtret,
2441 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002442{
2443 struct net_device *dev = NULL;
2444 u32 tblid = RT_TABLE_DEFAULT;
2445 struct mr_table *mrt;
2446 struct nlattr *attr;
2447 struct rtmsg *rtm;
2448 int ret, rem;
2449
Johannes Bergfceb6432017-04-12 14:34:07 +02002450 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002451 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002452 if (ret < 0)
2453 goto out;
2454 rtm = nlmsg_data(nlh);
2455
2456 ret = -EINVAL;
2457 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2458 rtm->rtm_type != RTN_MULTICAST ||
2459 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2460 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2461 goto out;
2462
2463 memset(mfcc, 0, sizeof(*mfcc));
2464 mfcc->mfcc_parent = -1;
2465 ret = 0;
2466 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2467 switch (nla_type(attr)) {
2468 case RTA_SRC:
2469 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2470 break;
2471 case RTA_DST:
2472 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2473 break;
2474 case RTA_IIF:
2475 dev = __dev_get_by_index(net, nla_get_u32(attr));
2476 if (!dev) {
2477 ret = -ENODEV;
2478 goto out;
2479 }
2480 break;
2481 case RTA_MULTIPATH:
2482 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2483 ret = -EINVAL;
2484 goto out;
2485 }
2486 break;
2487 case RTA_PREFSRC:
2488 ret = 1;
2489 break;
2490 case RTA_TABLE:
2491 tblid = nla_get_u32(attr);
2492 break;
2493 }
2494 }
2495 mrt = ipmr_get_table(net, tblid);
2496 if (!mrt) {
2497 ret = -ENOENT;
2498 goto out;
2499 }
2500 *mrtret = mrt;
2501 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2502 if (dev)
2503 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2504
2505out:
2506 return ret;
2507}
2508
2509/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002510static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2511 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002512{
2513 struct net *net = sock_net(skb->sk);
2514 int ret, mrtsock, parent;
2515 struct mr_table *tbl;
2516 struct mfcctl mfcc;
2517
2518 mrtsock = 0;
2519 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002520 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002521 if (ret < 0)
2522 return ret;
2523
2524 parent = ret ? mfcc.mfcc_parent : -1;
2525 if (nlh->nlmsg_type == RTM_NEWROUTE)
2526 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2527 else
2528 return ipmr_mfc_delete(tbl, &mfcc, parent);
2529}
2530
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002531static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2532{
2533 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2534
2535 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2536 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2537 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2538 mrt->mroute_reg_vif_num) ||
2539 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2540 mrt->mroute_do_assert) ||
2541 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2542 return false;
2543
2544 return true;
2545}
2546
2547static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2548{
2549 struct nlattr *vif_nest;
2550 struct vif_device *vif;
2551
2552 /* if the VIF doesn't exist just continue */
2553 if (!VIF_EXISTS(mrt, vifid))
2554 return true;
2555
2556 vif = &mrt->vif_table[vifid];
2557 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2558 if (!vif_nest)
2559 return false;
2560 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2561 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2562 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2563 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2564 IPMRA_VIFA_PAD) ||
2565 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2566 IPMRA_VIFA_PAD) ||
2567 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2568 IPMRA_VIFA_PAD) ||
2569 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2570 IPMRA_VIFA_PAD) ||
2571 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2572 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2573 nla_nest_cancel(skb, vif_nest);
2574 return false;
2575 }
2576 nla_nest_end(skb, vif_nest);
2577
2578 return true;
2579}
2580
2581static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2582{
2583 struct net *net = sock_net(skb->sk);
2584 struct nlmsghdr *nlh = NULL;
2585 unsigned int t = 0, s_t;
2586 unsigned int e = 0, s_e;
2587 struct mr_table *mrt;
2588
2589 s_t = cb->args[0];
2590 s_e = cb->args[1];
2591
2592 ipmr_for_each_table(mrt, net) {
2593 struct nlattr *vifs, *af;
2594 struct ifinfomsg *hdr;
2595 u32 i;
2596
2597 if (t < s_t)
2598 goto skip_table;
2599 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2600 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2601 sizeof(*hdr), NLM_F_MULTI);
2602 if (!nlh)
2603 break;
2604
2605 hdr = nlmsg_data(nlh);
2606 memset(hdr, 0, sizeof(*hdr));
2607 hdr->ifi_family = RTNL_FAMILY_IPMR;
2608
2609 af = nla_nest_start(skb, IFLA_AF_SPEC);
2610 if (!af) {
2611 nlmsg_cancel(skb, nlh);
2612 goto out;
2613 }
2614
2615 if (!ipmr_fill_table(mrt, skb)) {
2616 nlmsg_cancel(skb, nlh);
2617 goto out;
2618 }
2619
2620 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2621 if (!vifs) {
2622 nla_nest_end(skb, af);
2623 nlmsg_end(skb, nlh);
2624 goto out;
2625 }
2626 for (i = 0; i < mrt->maxvif; i++) {
2627 if (e < s_e)
2628 goto skip_entry;
2629 if (!ipmr_fill_vif(mrt, i, skb)) {
2630 nla_nest_end(skb, vifs);
2631 nla_nest_end(skb, af);
2632 nlmsg_end(skb, nlh);
2633 goto out;
2634 }
2635skip_entry:
2636 e++;
2637 }
2638 s_e = 0;
2639 e = 0;
2640 nla_nest_end(skb, vifs);
2641 nla_nest_end(skb, af);
2642 nlmsg_end(skb, nlh);
2643skip_table:
2644 t++;
2645 }
2646
2647out:
2648 cb->args[1] = e;
2649 cb->args[0] = t;
2650
2651 return skb->len;
2652}
2653
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002654#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002655/* The /proc interfaces to multicast routing :
2656 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 */
2658struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002659 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002660 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 int ct;
2662};
2663
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002664static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2665 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 loff_t pos)
2667{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002668 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002669
2670 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2671 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002673 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002674 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676 return NULL;
2677}
2678
2679static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002680 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002682 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002683 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002684 struct mr_table *mrt;
2685
2686 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002687 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002688 return ERR_PTR(-ENOENT);
2689
2690 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002693 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 : SEQ_START_TOKEN;
2695}
2696
2697static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2698{
2699 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002700 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002701 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 ++*pos;
2704 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002705 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002706
Patrick McHardy0c122952010-04-13 05:03:22 +00002707 while (++iter->ct < mrt->maxvif) {
2708 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002710 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
2712 return NULL;
2713}
2714
2715static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002716 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 read_unlock(&mrt_lock);
2719}
2720
2721static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2722{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002723 struct ipmr_vif_iter *iter = seq->private;
2724 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002727 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2729 } else {
2730 const struct vif_device *vif = v;
2731 const char *name = vif->dev ? vif->dev->name : "none";
2732
2733 seq_printf(seq,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002734 "%2zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002735 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002736 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 vif->bytes_out, vif->pkt_out,
2738 vif->flags, vif->local, vif->remote);
2739 }
2740 return 0;
2741}
2742
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002743static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 .start = ipmr_vif_seq_start,
2745 .next = ipmr_vif_seq_next,
2746 .stop = ipmr_vif_seq_stop,
2747 .show = ipmr_vif_seq_show,
2748};
2749
2750static int ipmr_vif_open(struct inode *inode, struct file *file)
2751{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002752 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2753 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
Arjan van de Ven9a321442007-02-12 00:55:35 -08002756static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 .owner = THIS_MODULE,
2758 .open = ipmr_vif_open,
2759 .read = seq_read,
2760 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002761 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762};
2763
2764struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002765 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002766 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002767 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768};
2769
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002770static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2771 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002773 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 struct mfc_cache *mfc;
2775
Eric Dumazeta8c94862010-10-01 16:15:08 +00002776 rcu_read_lock();
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002777 it->cache = &mrt->mfc_cache_list;
2778 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
2779 if (pos-- == 0)
2780 return mfc;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002781 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002784 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002785 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002786 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return mfc;
2788 spin_unlock_bh(&mfc_unres_lock);
2789
2790 it->cache = NULL;
2791 return NULL;
2792}
2793
2794
2795static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2796{
2797 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002798 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002799 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002800
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002801 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002802 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002803 return ERR_PTR(-ENOENT);
2804
2805 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 it->cache = NULL;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002807 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 : SEQ_START_TOKEN;
2809}
2810
2811static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2812{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002814 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002815 struct mr_table *mrt = it->mrt;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002816 struct mfc_cache *mfc = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 ++*pos;
2819
2820 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002821 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Patrick McHardy862465f2010-04-13 05:03:21 +00002823 if (mfc->list.next != it->cache)
2824 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002825
Patrick McHardy0c122952010-04-13 05:03:22 +00002826 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 goto end_of_list;
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002830 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002831 it->cache = &mrt->mfc_unres_queue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002834 if (!list_empty(it->cache))
2835 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002837end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 spin_unlock_bh(&mfc_unres_lock);
2839 it->cache = NULL;
2840
2841 return NULL;
2842}
2843
2844static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2845{
2846 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002847 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Patrick McHardy0c122952010-04-13 05:03:22 +00002849 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002851 else if (it->cache == &mrt->mfc_cache_list)
Eric Dumazeta8c94862010-10-01 16:15:08 +00002852 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
2855static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2856{
2857 int n;
2858
2859 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002860 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2862 } else {
2863 const struct mfc_cache *mfc = v;
2864 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002865 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002866
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002867 seq_printf(seq, "%08X %08X %-3hd",
2868 (__force u32) mfc->mfc_mcastgrp,
2869 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002870 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Patrick McHardy0c122952010-04-13 05:03:22 +00002872 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002873 seq_printf(seq, " %8lu %8lu %8lu",
2874 mfc->mfc_un.res.pkt,
2875 mfc->mfc_un.res.bytes,
2876 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002877 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002878 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002879 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002880 mfc->mfc_un.res.ttls[n] < 255)
2881 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002882 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 n, mfc->mfc_un.res.ttls[n]);
2884 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002885 } else {
2886 /* unresolved mfc_caches don't contain
2887 * pkt, bytes and wrong_if values
2888 */
2889 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891 seq_putc(seq, '\n');
2892 }
2893 return 0;
2894}
2895
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002896static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 .start = ipmr_mfc_seq_start,
2898 .next = ipmr_mfc_seq_next,
2899 .stop = ipmr_mfc_seq_stop,
2900 .show = ipmr_mfc_seq_show,
2901};
2902
2903static int ipmr_mfc_open(struct inode *inode, struct file *file)
2904{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002905 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2906 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907}
2908
Arjan van de Ven9a321442007-02-12 00:55:35 -08002909static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 .owner = THIS_MODULE,
2911 .open = ipmr_mfc_open,
2912 .read = seq_read,
2913 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002914 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002916#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002919static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002921 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922};
2923#endif
2924
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002925/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002926static int __net_init ipmr_net_init(struct net *net)
2927{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002928 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002929
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002930 err = ipmr_rules_init(net);
2931 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002932 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002933
2934#ifdef CONFIG_PROC_FS
2935 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002936 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002937 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002938 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002939 goto proc_cache_fail;
2940#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002941 return 0;
2942
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002943#ifdef CONFIG_PROC_FS
2944proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002945 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002946proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002947 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002948#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002949fail:
2950 return err;
2951}
2952
2953static void __net_exit ipmr_net_exit(struct net *net)
2954{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002955#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002956 remove_proc_entry("ip_mr_cache", net->proc_net);
2957 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002958#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002959 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002960}
2961
2962static struct pernet_operations ipmr_net_ops = {
2963 .init = ipmr_net_init,
2964 .exit = ipmr_net_exit,
2965};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002966
Wang Chen03d2f892008-07-03 12:13:36 +08002967int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
Wang Chen03d2f892008-07-03 12:13:36 +08002969 int err;
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2972 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002973 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002974 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002975
Benjamin Therycf958ae32009-01-22 04:56:16 +00002976 err = register_pernet_subsys(&ipmr_net_ops);
2977 if (err)
2978 goto reg_pernet_fail;
2979
Wang Chen03d2f892008-07-03 12:13:36 +08002980 err = register_netdevice_notifier(&ip_mr_notifier);
2981 if (err)
2982 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002983#ifdef CONFIG_IP_PIMSM_V2
2984 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002985 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002986 err = -EAGAIN;
2987 goto add_proto_fail;
2988 }
2989#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002990 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2991 NULL, ipmr_rtm_dumproute, NULL);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002992 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
2993 ipmr_rtm_route, NULL, NULL);
2994 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
2995 ipmr_rtm_route, NULL, NULL);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002996
2997 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
2998 NULL, ipmr_rtm_dumplink, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002999 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003000
Tom Goff403dbb92009-06-14 03:16:13 -07003001#ifdef CONFIG_IP_PIMSM_V2
3002add_proto_fail:
3003 unregister_netdevice_notifier(&ip_mr_notifier);
3004#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003005reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003006 unregister_pernet_subsys(&ipmr_net_ops);
3007reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003008 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003009 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}