blob: 06863ea3fc5b8c5582a70b14a4da9ecc67851822 [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,
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400104 struct net_device *dev, struct sk_buff *skb,
105 struct mfc_cache *cache, 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);
Julien Gomes5a645dd2017-06-20 13:54:17 -0700112static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100113static void mroute_clean_tables(struct mr_table *mrt, bool all);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000114static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000116#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
117#define ipmr_for_each_table(mrt, net) \
118 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
119
120static struct mr_table *ipmr_get_table(struct net *net, u32 id)
121{
122 struct mr_table *mrt;
123
124 ipmr_for_each_table(mrt, net) {
125 if (mrt->id == id)
126 return mrt;
127 }
128 return NULL;
129}
130
David S. Millerda919812011-03-12 02:04:50 -0500131static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000132 struct mr_table **mrt)
133{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000134 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100135 struct ipmr_result res;
136 struct fib_lookup_arg arg = {
137 .result = &res,
138 .flags = FIB_LOOKUP_NOREF,
139 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000140
David Aherne58e4152016-10-31 15:54:00 -0700141 /* update flow if oif or iif point to device enslaved to l3mdev */
142 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
143
David S. Millerda919812011-03-12 02:04:50 -0500144 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
145 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000146 if (err < 0)
147 return err;
148 *mrt = res.mrt;
149 return 0;
150}
151
152static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
153 int flags, struct fib_lookup_arg *arg)
154{
155 struct ipmr_result *res = arg->result;
156 struct mr_table *mrt;
157
158 switch (rule->action) {
159 case FR_ACT_TO_TBL:
160 break;
161 case FR_ACT_UNREACHABLE:
162 return -ENETUNREACH;
163 case FR_ACT_PROHIBIT:
164 return -EACCES;
165 case FR_ACT_BLACKHOLE:
166 default:
167 return -EINVAL;
168 }
169
David Aherne58e4152016-10-31 15:54:00 -0700170 arg->table = fib_rule_get_table(rule, arg);
171
172 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100173 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000174 return -EAGAIN;
175 res->mrt = mrt;
176 return 0;
177}
178
179static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
180{
181 return 1;
182}
183
184static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
185 FRA_GENERIC_POLICY,
186};
187
188static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
189 struct fib_rule_hdr *frh, struct nlattr **tb)
190{
191 return 0;
192}
193
194static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
195 struct nlattr **tb)
196{
197 return 1;
198}
199
200static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
201 struct fib_rule_hdr *frh)
202{
203 frh->dst_len = 0;
204 frh->src_len = 0;
205 frh->tos = 0;
206 return 0;
207}
208
Andi Kleen04a6f822012-10-04 17:12:11 -0700209static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200210 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000211 .rule_size = sizeof(struct ipmr_rule),
212 .addr_size = sizeof(u32),
213 .action = ipmr_rule_action,
214 .match = ipmr_rule_match,
215 .configure = ipmr_rule_configure,
216 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000217 .fill = ipmr_rule_fill,
218 .nlgroup = RTNLGRP_IPV4_RULE,
219 .policy = ipmr_rule_policy,
220 .owner = THIS_MODULE,
221};
222
223static int __net_init ipmr_rules_init(struct net *net)
224{
225 struct fib_rules_ops *ops;
226 struct mr_table *mrt;
227 int err;
228
229 ops = fib_rules_register(&ipmr_rules_ops_template, net);
230 if (IS_ERR(ops))
231 return PTR_ERR(ops);
232
233 INIT_LIST_HEAD(&net->ipv4.mr_tables);
234
235 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100236 if (IS_ERR(mrt)) {
237 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000238 goto err1;
239 }
240
241 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
242 if (err < 0)
243 goto err2;
244
245 net->ipv4.mr_rules_ops = ops;
246 return 0;
247
248err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700249 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000250err1:
251 fib_rules_unregister(ops);
252 return err;
253}
254
255static void __net_exit ipmr_rules_exit(struct net *net)
256{
257 struct mr_table *mrt, *next;
258
WANG Conged785302015-03-31 11:01:45 -0700259 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000260 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
261 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000262 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000263 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000264 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700265 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000266}
267#else
268#define ipmr_for_each_table(mrt, net) \
269 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
270
271static struct mr_table *ipmr_get_table(struct net *net, u32 id)
272{
273 return net->ipv4.mrt;
274}
275
David S. Millerda919812011-03-12 02:04:50 -0500276static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000277 struct mr_table **mrt)
278{
279 *mrt = net->ipv4.mrt;
280 return 0;
281}
282
283static int __net_init ipmr_rules_init(struct net *net)
284{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100285 struct mr_table *mrt;
286
287 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
288 if (IS_ERR(mrt))
289 return PTR_ERR(mrt);
290 net->ipv4.mrt = mrt;
291 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000292}
293
294static void __net_exit ipmr_rules_exit(struct net *net)
295{
WANG Conged785302015-03-31 11:01:45 -0700296 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000297 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700298 net->ipv4.mrt = NULL;
299 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000300}
301#endif
302
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100303static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
304 const void *ptr)
305{
306 const struct mfc_cache_cmp_arg *cmparg = arg->key;
307 struct mfc_cache *c = (struct mfc_cache *)ptr;
308
309 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
310 cmparg->mfc_origin != c->mfc_origin;
311}
312
313static const struct rhashtable_params ipmr_rht_params = {
314 .head_offset = offsetof(struct mfc_cache, mnode),
315 .key_offset = offsetof(struct mfc_cache, cmparg),
316 .key_len = sizeof(struct mfc_cache_cmp_arg),
317 .nelem_hint = 3,
318 .locks_mul = 1,
319 .obj_cmpfn = ipmr_hash_cmp,
320 .automatic_shrinking = true,
321};
322
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000323static struct mr_table *ipmr_new_table(struct net *net, u32 id)
324{
325 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000326
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100327 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
328 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
329 return ERR_PTR(-EINVAL);
330
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000331 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100332 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000333 return mrt;
334
335 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100336 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100337 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200338 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000339 mrt->id = id;
340
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100341 rhltable_init(&mrt->mfc_hash, &ipmr_rht_params);
342 INIT_LIST_HEAD(&mrt->mfc_cache_list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000343 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
344
345 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
346 (unsigned long)mrt);
347
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000348 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000349#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
350 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
351#endif
352 return mrt;
353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000355static void ipmr_free_table(struct mr_table *mrt)
356{
357 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100358 mroute_clean_tables(mrt, true);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100359 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000360 kfree(mrt);
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
364
Wang Chend6070322008-07-14 20:55:26 -0700365static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
366{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000367 struct net *net = dev_net(dev);
368
Wang Chend6070322008-07-14 20:55:26 -0700369 dev_close(dev);
370
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000371 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700372 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800373 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700374 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700375 struct ip_tunnel_parm p;
376
377 memset(&p, 0, sizeof(p));
378 p.iph.daddr = v->vifc_rmt_addr.s_addr;
379 p.iph.saddr = v->vifc_lcl_addr.s_addr;
380 p.iph.version = 4;
381 p.iph.ihl = 5;
382 p.iph.protocol = IPPROTO_IPIP;
383 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
384 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
385
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800386 if (ops->ndo_do_ioctl) {
387 mm_segment_t oldfs = get_fs();
388
389 set_fs(KERNEL_DS);
390 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
391 set_fs(oldfs);
392 }
Wang Chend6070322008-07-14 20:55:26 -0700393 }
394}
395
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100396/* Initialize ipmr pimreg/tunnel in_device */
397static bool ipmr_init_vif_indev(const struct net_device *dev)
398{
399 struct in_device *in_dev;
400
401 ASSERT_RTNL();
402
403 in_dev = __in_dev_get_rtnl(dev);
404 if (!in_dev)
405 return false;
406 ipv4_devconf_setall(in_dev);
407 neigh_parms_data_state_setall(in_dev->arp_parms);
408 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
409
410 return true;
411}
412
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100413static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct net_device *dev;
416
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000417 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800420 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 int err;
422 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 memset(&p, 0, sizeof(p));
426 p.iph.daddr = v->vifc_rmt_addr.s_addr;
427 p.iph.saddr = v->vifc_lcl_addr.s_addr;
428 p.iph.version = 4;
429 p.iph.ihl = 5;
430 p.iph.protocol = IPPROTO_IPIP;
431 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800432 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800434 if (ops->ndo_do_ioctl) {
435 mm_segment_t oldfs = get_fs();
436
437 set_fs(KERNEL_DS);
438 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
439 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000440 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800441 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 dev = NULL;
444
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000445 if (err == 0 &&
446 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100448 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (dev_open(dev))
451 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700452 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 }
455 return dev;
456
457failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unregister_netdevice(dev);
459 return NULL;
460}
461
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100462#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000463static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000465 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000466 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500467 struct flowi4 fl4 = {
468 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700469 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500470 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000471 };
472 int err;
473
David S. Millerda919812011-03-12 02:04:50 -0500474 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000475 if (err < 0) {
476 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000477 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000478 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700481 dev->stats.tx_bytes += skb->len;
482 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000483 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 read_unlock(&mrt_lock);
485 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000486 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200489static int reg_vif_get_iflink(const struct net_device *dev)
490{
491 return 0;
492}
493
Stephen Hemminger007c3832008-11-20 20:28:35 -0800494static const struct net_device_ops reg_vif_netdev_ops = {
495 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200496 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800497};
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static void reg_vif_setup(struct net_device *dev)
500{
501 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800502 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530504 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400505 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700506 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000509static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000512 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000514 if (mrt->id == RT_TABLE_DEFAULT)
515 sprintf(name, "pimreg");
516 else
517 sprintf(name, "pimreg%u", mrt->id);
518
Tom Gundersenc835a672014-07-14 16:37:24 +0200519 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Ian Morris51456b22015-04-03 09:17:26 +0100521 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return NULL;
523
Tom Goff403dbb92009-06-14 03:16:13 -0700524 dev_net_set(dev, net);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (register_netdevice(dev)) {
527 free_netdev(dev);
528 return NULL;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100531 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (dev_open(dev))
534 goto failure;
535
Wang Chen7dc00c82008-07-14 20:56:34 -0700536 dev_hold(dev);
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return dev;
539
540failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 unregister_netdevice(dev);
542 return NULL;
543}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100544
545/* called with rcu_read_lock() */
546static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
547 unsigned int pimlen)
548{
549 struct net_device *reg_dev = NULL;
550 struct iphdr *encap;
551
552 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100553 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100554 * a. packet is really sent to a multicast group
555 * b. packet is not a NULL-REGISTER
556 * c. packet is not truncated
557 */
558 if (!ipv4_is_multicast(encap->daddr) ||
559 encap->tot_len == 0 ||
560 ntohs(encap->tot_len) + pimlen > skb->len)
561 return 1;
562
563 read_lock(&mrt_lock);
564 if (mrt->mroute_reg_vif_num >= 0)
565 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
566 read_unlock(&mrt_lock);
567
568 if (!reg_dev)
569 return 1;
570
571 skb->mac_header = skb->network_header;
572 skb_pull(skb, (u8 *)encap - skb->data);
573 skb_reset_network_header(skb);
574 skb->protocol = htons(ETH_P_IP);
575 skb->ip_summed = CHECKSUM_NONE;
576
577 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
578
579 netif_rx(skb);
580
581 return NET_RX_SUCCESS;
582}
583#else
584static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
585{
586 return NULL;
587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588#endif
589
Ben Hutchings2c530402012-07-10 10:55:09 +0000590/**
591 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700592 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000594static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000595 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct vif_device *v;
598 struct net_device *dev;
599 struct in_device *in_dev;
600
Patrick McHardy0c122952010-04-13 05:03:22 +0000601 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EADDRNOTAVAIL;
603
Patrick McHardy0c122952010-04-13 05:03:22 +0000604 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 write_lock_bh(&mrt_lock);
607 dev = v->dev;
608 v->dev = NULL;
609
610 if (!dev) {
611 write_unlock_bh(&mrt_lock);
612 return -EADDRNOTAVAIL;
613 }
614
Patrick McHardy0c122952010-04-13 05:03:22 +0000615 if (vifi == mrt->mroute_reg_vif_num)
616 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000618 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000620
621 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000622 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000625 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627
628 write_unlock_bh(&mrt_lock);
629
630 dev_set_allmulti(dev, -1);
631
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000632 in_dev = __in_dev_get_rtnl(dev);
633 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700634 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700635 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000636 NETCONFA_MC_FORWARDING,
637 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 ip_rt_multicast_event(in_dev);
639 }
640
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000641 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000642 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 dev_put(dev);
645 return 0;
646}
647
Eric Dumazeta8c94862010-10-01 16:15:08 +0000648static void ipmr_cache_free_rcu(struct rcu_head *head)
649{
650 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
651
652 kmem_cache_free(mrt_cachep, c);
653}
654
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000655static inline void ipmr_cache_free(struct mfc_cache *c)
656{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000657 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000661 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000663static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200665 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700667 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Patrick McHardy0c122952010-04-13 05:03:22 +0000669 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jianjun Kongc354e122008-11-03 00:28:02 -0800671 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700672 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200673 struct nlmsghdr *nlh = skb_pull(skb,
674 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000676 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000678 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700679 e->error = -ETIMEDOUT;
680 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700681
Eric W. Biederman15e47302012-09-07 20:12:54 +0000682 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000683 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000688 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Patrick McHardye258beb2010-04-13 05:03:19 +0000691/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000692static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Patrick McHardy0c122952010-04-13 05:03:22 +0000694 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 unsigned long now;
696 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000697 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000700 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return;
702 }
703
Patrick McHardy0c122952010-04-13 05:03:22 +0000704 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 goto out;
706
707 now = jiffies;
708 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Patrick McHardy0c122952010-04-13 05:03:22 +0000710 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (time_after(c->mfc_un.unres.expires, now)) {
712 unsigned long interval = c->mfc_un.unres.expires - now;
713 if (interval < expires)
714 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 continue;
716 }
717
Patrick McHardy862465f2010-04-13 05:03:21 +0000718 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000719 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000720 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722
Patrick McHardy0c122952010-04-13 05:03:22 +0000723 if (!list_empty(&mrt->mfc_unres_queue))
724 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726out:
727 spin_unlock(&mfc_unres_lock);
728}
729
730/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000731static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000732 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 int vifi;
735
736 cache->mfc_un.res.minvif = MAXVIFS;
737 cache->mfc_un.res.maxvif = 0;
738 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
739
Patrick McHardy0c122952010-04-13 05:03:22 +0000740 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
741 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000742 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
744 if (cache->mfc_un.res.minvif > vifi)
745 cache->mfc_un.res.minvif = vifi;
746 if (cache->mfc_un.res.maxvif <= vifi)
747 cache->mfc_un.res.maxvif = vifi + 1;
748 }
749 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200750 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Patrick McHardy0c122952010-04-13 05:03:22 +0000753static int vif_add(struct net *net, struct mr_table *mrt,
754 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000757 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 struct net_device *dev;
759 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700760 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000763 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return -EADDRINUSE;
765
766 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100768 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100769 return -EINVAL;
770 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * All the packets will be sent to the daemon
772 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000773 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000775 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (!dev)
777 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700778 err = dev_set_allmulti(dev, 1);
779 if (err) {
780 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700781 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700782 return err;
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900785 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000786 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!dev)
788 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700789 err = dev_set_allmulti(dev, 1);
790 if (err) {
791 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700792 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700793 return err;
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000796 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000798 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
799 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100800 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000801 dev_put(dev);
802 return -EADDRNOTAVAIL;
803 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000804 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000805 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!dev)
808 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700809 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700810 if (err) {
811 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700812 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 break;
815 default:
816 return -EINVAL;
817 }
818
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000819 in_dev = __in_dev_get_rtnl(dev);
820 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000821 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000823 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700824 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700825 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
826 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 ip_rt_multicast_event(in_dev);
828
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000829 /* Fill in the VIF structures */
830
Jianjun Kongc354e122008-11-03 00:28:02 -0800831 v->rate_limit = vifc->vifc_rate_limit;
832 v->local = vifc->vifc_lcl_addr.s_addr;
833 v->remote = vifc->vifc_rmt_addr.s_addr;
834 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (!mrtsock)
836 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800837 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 v->bytes_in = 0;
839 v->bytes_out = 0;
840 v->pkt_in = 0;
841 v->pkt_out = 0;
842 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000843 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200844 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* And finish update writing critical data */
847 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800848 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000849 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000850 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000851 if (vifi+1 > mrt->maxvif)
852 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 write_unlock_bh(&mrt_lock);
854 return 0;
855}
856
Eric Dumazeta8c94862010-10-01 16:15:08 +0000857/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000858static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000859 __be32 origin,
860 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100862 struct mfc_cache_cmp_arg arg = {
863 .mfc_mcastgrp = mcastgrp,
864 .mfc_origin = origin
865 };
866 struct rhlist_head *tmp, *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct mfc_cache *c;
868
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100869 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
870 rhl_for_each_entry_rcu(c, tmp, list, mnode)
871 return c;
872
Patrick McHardy862465f2010-04-13 05:03:21 +0000873 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000876/* Look for a (*,*,oif) entry */
877static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
878 int vifi)
879{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100880 struct mfc_cache_cmp_arg arg = {
881 .mfc_mcastgrp = htonl(INADDR_ANY),
882 .mfc_origin = htonl(INADDR_ANY)
883 };
884 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000885 struct mfc_cache *c;
886
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100887 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
888 rhl_for_each_entry_rcu(c, tmp, list, mnode)
889 if (c->mfc_un.res.ttls[vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000890 return c;
891
892 return NULL;
893}
894
895/* Look for a (*,G) entry */
896static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
897 __be32 mcastgrp, int vifi)
898{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100899 struct mfc_cache_cmp_arg arg = {
900 .mfc_mcastgrp = mcastgrp,
901 .mfc_origin = htonl(INADDR_ANY)
902 };
903 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000904 struct mfc_cache *c, *proxy;
905
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100906 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000907 goto skip;
908
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100909 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
910 rhl_for_each_entry_rcu(c, tmp, list, mnode) {
911 if (c->mfc_un.res.ttls[vifi] < 255)
912 return c;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000913
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100914 /* It's ok if the vifi is part of the static tree */
915 proxy = ipmr_cache_find_any_parent(mrt, c->mfc_parent);
916 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
917 return c;
918 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000919
920skip:
921 return ipmr_cache_find_any_parent(mrt, vifi);
922}
923
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100924/* Look for a (S,G,iif) entry if parent != -1 */
925static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
926 __be32 origin, __be32 mcastgrp,
927 int parent)
928{
929 struct mfc_cache_cmp_arg arg = {
930 .mfc_mcastgrp = mcastgrp,
931 .mfc_origin = origin,
932 };
933 struct rhlist_head *tmp, *list;
934 struct mfc_cache *c;
935
936 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
937 rhl_for_each_entry_rcu(c, tmp, list, mnode)
938 if (parent == -1 || parent == c->mfc_parent)
939 return c;
940
941 return NULL;
942}
943
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100944/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000945static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Jianjun Kongc354e122008-11-03 00:28:02 -0800947 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000948
Tom Goff70a0dec2016-06-23 16:11:57 -0400949 if (c) {
950 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
Eric Dumazeta8c94862010-10-01 16:15:08 +0000951 c->mfc_un.res.minvif = MAXVIFS;
Tom Goff70a0dec2016-06-23 16:11:57 -0400952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return c;
954}
955
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000956static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Jianjun Kongc354e122008-11-03 00:28:02 -0800958 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000959
960 if (c) {
961 skb_queue_head_init(&c->mfc_un.unres.unresolved);
962 c->mfc_un.unres.expires = jiffies + 10*HZ;
963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return c;
965}
966
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100967/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000968static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
969 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700972 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000974 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800975 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700976 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200977 struct nlmsghdr *nlh = skb_pull(skb,
978 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Hong zhi guo573ce262013-03-27 06:47:04 +0000980 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000981 nlh->nlmsg_len = skb_tail_pointer(skb) -
982 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 } else {
984 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000985 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000987 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700988 e->error = -EMSGSIZE;
989 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Thomas Graf2942e902006-08-15 00:30:25 -0700991
Eric W. Biederman15e47302012-09-07 20:12:54 +0000992 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000993 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400994 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997}
998
Julien Gomes5a645dd2017-06-20 13:54:17 -0700999/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001001 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001003static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001004 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001006 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001007 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct igmphdr *igmp;
1009 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001010 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int ret;
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (assert == IGMPMSG_WHOLEPKT)
1014 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1015 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 skb = alloc_skb(128, GFP_ATOMIC);
1017
Stephen Hemminger132adf52007-03-08 20:44:43 -08001018 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return -ENOBUFS;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (assert == IGMPMSG_WHOLEPKT) {
1022 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001023 * Duplicate old header, fix ihl, length etc.
1024 * And all this only to mangle msg->im_msgtype and
1025 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001027 skb_push(skb, sizeof(struct iphdr));
1028 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001029 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001030 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001031 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1033 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001034 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001035 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1036 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1037 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001038 } else {
1039 /* Copy the IP header */
1040 skb_set_network_header(skb, skb->len);
1041 skb_put(skb, ihl);
1042 skb_copy_to_linear_data(skb, pkt->data, ihl);
1043 /* Flag to the kernel this is a route add */
1044 ip_hdr(skb)->protocol = 0;
1045 msg = (struct igmpmsg *)skb_network_header(skb);
1046 msg->im_vif = vifi;
1047 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1048 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001049 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001050 igmp->type = assert;
1051 msg->im_msgtype = assert;
1052 igmp->code = 0;
1053 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1054 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Eric Dumazet4c968702010-10-01 16:15:01 +00001057 rcu_read_lock();
1058 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001059 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001060 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 kfree_skb(skb);
1062 return -EINVAL;
1063 }
1064
Julien Gomes5a645dd2017-06-20 13:54:17 -07001065 igmpmsg_netlink_event(mrt, skb);
1066
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001067 /* Deliver to mrouted */
Eric Dumazet4c968702010-10-01 16:15:01 +00001068 ret = sock_queue_rcv_skb(mroute_sk, skb);
1069 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001070 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001071 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 kfree_skb(skb);
1073 }
1074
1075 return ret;
1076}
1077
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001078/* Queue a packet for resolution. It gets locked cache entry! */
1079static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001080 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001082 const struct iphdr *iph = ip_hdr(skb);
1083 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001084 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001088 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001089 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001090 c->mfc_origin == iph->saddr) {
1091 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
Patrick McHardy862465f2010-04-13 05:03:21 +00001096 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001097 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001098 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001099 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 spin_unlock_bh(&mfc_unres_lock);
1101
1102 kfree_skb(skb);
1103 return -ENOBUFS;
1104 }
1105
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001106 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001107 c->mfc_parent = -1;
1108 c->mfc_origin = iph->saddr;
1109 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001111 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001112 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001113 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001114 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 out - Brad Parker
1116 */
1117 spin_unlock_bh(&mfc_unres_lock);
1118
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001119 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 kfree_skb(skb);
1121 return err;
1122 }
1123
Patrick McHardy0c122952010-04-13 05:03:22 +00001124 atomic_inc(&mrt->cache_resolve_queue_len);
1125 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001126 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
David S. Miller278554b2010-05-12 00:05:35 -07001128 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1129 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001132 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001133 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 kfree_skb(skb);
1135 err = -ENOBUFS;
1136 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001137 if (dev) {
1138 skb->dev = dev;
1139 skb->skb_iif = dev->ifindex;
1140 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001141 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 err = 0;
1143 }
1144
1145 spin_unlock_bh(&mfc_unres_lock);
1146 return err;
1147}
1148
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001149/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001151static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001153 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001155 /* The entries are added/deleted only under RTNL */
1156 rcu_read_lock();
1157 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1158 mfc->mfcc_mcastgrp.s_addr, parent);
1159 rcu_read_unlock();
1160 if (!c)
1161 return -ENOENT;
1162 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1163 list_del_rcu(&c->list);
1164 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1165 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Patrick McHardy0c122952010-04-13 05:03:22 +00001170static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001171 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Patrick McHardy862465f2010-04-13 05:03:21 +00001173 struct mfc_cache *uc, *c;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001174 bool found;
1175 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Patrick McHardya50436f22010-03-17 06:04:14 +00001177 if (mfc->mfcc_parent >= MAXVIFS)
1178 return -ENFILE;
1179
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001180 /* The entries are added/deleted only under RTNL */
1181 rcu_read_lock();
1182 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1183 mfc->mfcc_mcastgrp.s_addr, parent);
1184 rcu_read_unlock();
1185 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 write_lock_bh(&mrt_lock);
1187 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001188 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (!mrtsock)
1190 c->mfc_flags |= MFC_STATIC;
1191 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001192 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return 0;
1194 }
1195
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001196 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001197 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return -EINVAL;
1199
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001200 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001201 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return -ENOMEM;
1203
Jianjun Kongc354e122008-11-03 00:28:02 -08001204 c->mfc_origin = mfc->mfcc_origin.s_addr;
1205 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1206 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001207 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (!mrtsock)
1209 c->mfc_flags |= MFC_STATIC;
1210
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001211 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->mnode,
1212 ipmr_rht_params);
1213 if (ret) {
1214 pr_err("ipmr: rhtable insert error %d\n", ret);
1215 ipmr_cache_free(c);
1216 return ret;
1217 }
1218 list_add_tail_rcu(&c->list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001219 /* Check to see if we resolved a queued list. If so we
1220 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001222 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001224 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001225 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001227 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001228 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001229 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 break;
1231 }
1232 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001233 if (list_empty(&mrt->mfc_unres_queue))
1234 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 spin_unlock_bh(&mfc_unres_lock);
1236
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001237 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001238 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001239 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001241 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return 0;
1243}
1244
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001245/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001246static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001248 struct mfc_cache *c, *tmp;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001249 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001250 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001251
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001252 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001253 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001254 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1255 continue;
1256 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001258 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001260 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001261 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1262 if (!all && (c->mfc_flags & MFC_STATIC))
1263 continue;
1264 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1265 list_del_rcu(&c->list);
1266 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1267 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
Patrick McHardy0c122952010-04-13 05:03:22 +00001270 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001272 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001273 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001274 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001275 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277 spin_unlock_bh(&mfc_unres_lock);
1278 }
1279}
1280
Eric Dumazet4c968702010-10-01 16:15:01 +00001281/* called from ip_ra_control(), before an RCU grace period,
1282 * we dont need to call synchronize_rcu() here
1283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284static void mrtsock_destruct(struct sock *sk)
1285{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001286 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001287 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001288
WANG Cong1215e512017-04-12 12:32:13 -07001289 ASSERT_RTNL();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001290 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001291 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001292 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001293 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1294 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001295 NETCONFA_IFINDEX_ALL,
1296 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001297 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001298 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001303/* Socket options and virtual interface manipulation. The whole
1304 * virtual interface system is a complete heap, but unfortunately
1305 * that's how BSD mrouted happens to think. Maybe one day with a proper
1306 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001308
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001309int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1310 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001312 struct net *net = sock_net(sk);
1313 int val, ret = 0, parent = 0;
1314 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct vifctl vif;
1316 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001317 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001318
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001319 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1320 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001321 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001322 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1323 ret = -EOPNOTSUPP;
1324 goto out_unlock;
1325 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001326
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001327 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001328 if (!mrt) {
1329 ret = -ENOENT;
1330 goto out_unlock;
1331 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001332 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001333 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001334 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1335 ret = -EACCES;
1336 goto out_unlock;
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
Stephen Hemminger132adf52007-03-08 20:44:43 -08001340 switch (optname) {
1341 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001342 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001343 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001344 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001345 }
1346 if (rtnl_dereference(mrt->mroute_sk)) {
1347 ret = -EADDRINUSE;
1348 break;
1349 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001350
1351 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1352 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001353 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001354 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001355 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1356 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001357 NETCONFA_IFINDEX_ALL,
1358 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001359 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001360 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001362 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1363 ret = -EACCES;
1364 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001365 ret = ip_ra_control(sk, 0, NULL);
WANG Cong1215e512017-04-12 12:32:13 -07001366 goto out_unlock;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001367 }
1368 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001369 case MRT_ADD_VIF:
1370 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001371 if (optlen != sizeof(vif)) {
1372 ret = -EINVAL;
1373 break;
1374 }
1375 if (copy_from_user(&vif, optval, sizeof(vif))) {
1376 ret = -EFAULT;
1377 break;
1378 }
1379 if (vif.vifc_vifi >= MAXVIFS) {
1380 ret = -ENFILE;
1381 break;
1382 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001383 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001384 ret = vif_add(net, mrt, &vif,
1385 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001386 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001387 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001389 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001390 /* Manipulate the forwarding caches. These live
1391 * in a sort of kernel/user symbiosis.
1392 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001393 case MRT_ADD_MFC:
1394 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001395 parent = -1;
1396 case MRT_ADD_MFC_PROXY:
1397 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001398 if (optlen != sizeof(mfc)) {
1399 ret = -EINVAL;
1400 break;
1401 }
1402 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1403 ret = -EFAULT;
1404 break;
1405 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001406 if (parent == 0)
1407 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001408 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1409 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001410 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001411 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001412 sk == rtnl_dereference(mrt->mroute_sk),
1413 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001414 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001415 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001416 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001417 if (optlen != sizeof(val)) {
1418 ret = -EINVAL;
1419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001421 if (get_user(val, (int __user *)optval)) {
1422 ret = -EFAULT;
1423 break;
1424 }
1425 mrt->mroute_do_assert = val;
1426 break;
1427 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001428 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001429 ret = -ENOPROTOOPT;
1430 break;
1431 }
1432 if (optlen != sizeof(val)) {
1433 ret = -EINVAL;
1434 break;
1435 }
1436 if (get_user(val, (int __user *)optval)) {
1437 ret = -EFAULT;
1438 break;
1439 }
1440
1441 val = !!val;
1442 if (val != mrt->mroute_do_pim) {
1443 mrt->mroute_do_pim = val;
1444 mrt->mroute_do_assert = val;
1445 }
1446 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001447 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001448 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1449 ret = -ENOPROTOOPT;
1450 break;
1451 }
1452 if (optlen != sizeof(uval)) {
1453 ret = -EINVAL;
1454 break;
1455 }
1456 if (get_user(uval, (u32 __user *)optval)) {
1457 ret = -EFAULT;
1458 break;
1459 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001460
Eric Dumazet4c968702010-10-01 16:15:01 +00001461 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1462 ret = -EBUSY;
1463 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001464 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001465 if (IS_ERR(mrt))
1466 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001467 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001468 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c968702010-10-01 16:15:01 +00001469 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001470 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001471 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001472 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001473 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001475out_unlock:
1476 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001477 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001480/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001481int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 int olr;
1484 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001485 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001486 struct mr_table *mrt;
1487
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001488 if (sk->sk_type != SOCK_RAW ||
1489 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1490 return -EOPNOTSUPP;
1491
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001492 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001493 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001494 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001496 switch (optname) {
1497 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001498 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001499 break;
1500 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001501 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001502 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001503 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001504 break;
1505 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001506 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001507 break;
1508 default:
1509 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001510 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001511
1512 if (get_user(olr, optlen))
1513 return -EFAULT;
1514 olr = min_t(unsigned int, olr, sizeof(int));
1515 if (olr < 0)
1516 return -EINVAL;
1517 if (put_user(olr, optlen))
1518 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001519 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return -EFAULT;
1521 return 0;
1522}
1523
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001524/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1526{
1527 struct sioc_sg_req sr;
1528 struct sioc_vif_req vr;
1529 struct vif_device *vif;
1530 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001531 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001532 struct mr_table *mrt;
1533
1534 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001535 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001536 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001537
Stephen Hemminger132adf52007-03-08 20:44:43 -08001538 switch (cmd) {
1539 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001540 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001541 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001542 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001543 return -EINVAL;
1544 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001545 vif = &mrt->vif_table[vr.vifi];
1546 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001547 vr.icount = vif->pkt_in;
1548 vr.ocount = vif->pkt_out;
1549 vr.ibytes = vif->bytes_in;
1550 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001552
Jianjun Kongc354e122008-11-03 00:28:02 -08001553 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001555 return 0;
1556 }
1557 read_unlock(&mrt_lock);
1558 return -EADDRNOTAVAIL;
1559 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001560 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001561 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Eric Dumazeta8c94862010-10-01 16:15:08 +00001563 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001564 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001565 if (c) {
1566 sr.pktcnt = c->mfc_un.res.pkt;
1567 sr.bytecnt = c->mfc_un.res.bytes;
1568 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001569 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001570
Jianjun Kongc354e122008-11-03 00:28:02 -08001571 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001572 return -EFAULT;
1573 return 0;
1574 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001575 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001576 return -EADDRNOTAVAIL;
1577 default:
1578 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580}
1581
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001582#ifdef CONFIG_COMPAT
1583struct compat_sioc_sg_req {
1584 struct in_addr src;
1585 struct in_addr grp;
1586 compat_ulong_t pktcnt;
1587 compat_ulong_t bytecnt;
1588 compat_ulong_t wrong_if;
1589};
1590
David S. Millerca6b8bb02011-02-03 17:24:28 -08001591struct compat_sioc_vif_req {
1592 vifi_t vifi; /* Which iface */
1593 compat_ulong_t icount;
1594 compat_ulong_t ocount;
1595 compat_ulong_t ibytes;
1596 compat_ulong_t obytes;
1597};
1598
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001599int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1600{
David S. Miller0033d5a2011-02-03 17:21:31 -08001601 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001602 struct compat_sioc_vif_req vr;
1603 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001604 struct mfc_cache *c;
1605 struct net *net = sock_net(sk);
1606 struct mr_table *mrt;
1607
1608 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001609 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001610 return -ENOENT;
1611
1612 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001613 case SIOCGETVIFCNT:
1614 if (copy_from_user(&vr, arg, sizeof(vr)))
1615 return -EFAULT;
1616 if (vr.vifi >= mrt->maxvif)
1617 return -EINVAL;
1618 read_lock(&mrt_lock);
1619 vif = &mrt->vif_table[vr.vifi];
1620 if (VIF_EXISTS(mrt, vr.vifi)) {
1621 vr.icount = vif->pkt_in;
1622 vr.ocount = vif->pkt_out;
1623 vr.ibytes = vif->bytes_in;
1624 vr.obytes = vif->bytes_out;
1625 read_unlock(&mrt_lock);
1626
1627 if (copy_to_user(arg, &vr, sizeof(vr)))
1628 return -EFAULT;
1629 return 0;
1630 }
1631 read_unlock(&mrt_lock);
1632 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001633 case SIOCGETSGCNT:
1634 if (copy_from_user(&sr, arg, sizeof(sr)))
1635 return -EFAULT;
1636
1637 rcu_read_lock();
1638 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1639 if (c) {
1640 sr.pktcnt = c->mfc_un.res.pkt;
1641 sr.bytecnt = c->mfc_un.res.bytes;
1642 sr.wrong_if = c->mfc_un.res.wrong_if;
1643 rcu_read_unlock();
1644
1645 if (copy_to_user(arg, &sr, sizeof(sr)))
1646 return -EFAULT;
1647 return 0;
1648 }
1649 rcu_read_unlock();
1650 return -EADDRNOTAVAIL;
1651 default:
1652 return -ENOIOCTLCMD;
1653 }
1654}
1655#endif
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1658{
Jiri Pirko351638e2013-05-28 01:30:21 +00001659 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001660 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001661 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 struct vif_device *v;
1663 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (event != NETDEV_UNREGISTER)
1666 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001667
1668 ipmr_for_each_table(mrt, net) {
1669 v = &mrt->vif_table[0];
1670 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1671 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001672 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675 return NOTIFY_DONE;
1676}
1677
Jianjun Kongc354e122008-11-03 00:28:02 -08001678static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 .notifier_call = ipmr_device_event,
1680};
1681
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001682/* Encapsulate a packet by attaching a valid IPIP header to it.
1683 * This avoids tunnel drivers and other mess and gives us the speed so
1684 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001686static void ip_encap(struct net *net, struct sk_buff *skb,
1687 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001689 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001690 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001691
1692 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001693 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001694 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001695 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001697 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001698 iph->tos = old_iph->tos;
1699 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 iph->frag_off = 0;
1701 iph->daddr = daddr;
1702 iph->saddr = saddr;
1703 iph->protocol = IPPROTO_IPIP;
1704 iph->ihl = 5;
1705 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001706 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 ip_send_check(iph);
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1710 nf_reset(skb);
1711}
1712
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001713static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1714 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001716 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
David S. Miller73186df2015-11-03 13:41:45 -05001718 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1719 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 if (unlikely(opt->optlen))
1722 ip_forward_options(skb);
1723
Eric W. Biederman13206b62015-10-07 16:48:35 -05001724 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001727/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Patrick McHardy0c122952010-04-13 05:03:22 +00001729static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1730 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001732 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001733 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct net_device *dev;
1735 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001736 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int encap = 0;
1738
Ian Morris51456b22015-04-03 09:17:26 +01001739 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 goto out_free;
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (vif->flags & VIFF_REGISTER) {
1743 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001744 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001745 vif->dev->stats.tx_bytes += skb->len;
1746 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001747 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001748 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001751 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001752 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001753 vif->remote, vif->local,
1754 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 encap = sizeof(struct iphdr);
1760 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001761 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001762 0, 0,
1763 IPPROTO_IPIP,
1764 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001765 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto out_free;
1767 }
1768
Changli Gaod8d1f302010-06-10 23:31:35 -07001769 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Changli Gaod8d1f302010-06-10 23:31:35 -07001771 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001773 * allow to send ICMP, so that packets will disappear
1774 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 */
David S. Miller73186df2015-11-03 13:41:45 -05001776 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 ip_rt_put(rt);
1778 goto out_free;
1779 }
1780
Changli Gaod8d1f302010-06-10 23:31:35 -07001781 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001784 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto out_free;
1786 }
1787
1788 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001789 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Eric Dumazetadf30902009-06-02 05:19:30 +00001791 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001792 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001793 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001796 * What do we do with netfilter? -- RR
1797 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001799 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001801 vif->dev->stats.tx_packets++;
1802 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001805 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001807 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * not only before forwarding, but after forwarding on all output
1809 * interfaces. It is clear, if mrouter runs a multicasting
1810 * program, it should receive packets not depending to what interface
1811 * program is joined.
1812 * If we will not make it, the program will have to join on all
1813 * interfaces. On the other hand, multihoming host (or router, but
1814 * not mrouter) cannot join to more than one interface - it will
1815 * result in receiving multiple packets.
1816 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001817 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1818 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 ipmr_forward_finish);
1820 return;
1821
1822out_free:
1823 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
Patrick McHardy0c122952010-04-13 05:03:22 +00001826static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001829
1830 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1831 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 break;
1833 }
1834 return ct;
1835}
1836
1837/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001838static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001839 struct net_device *dev, struct sk_buff *skb,
1840 struct mfc_cache *cache, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001842 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 int psend = -1;
1844 int vif, ct;
1845
1846 vif = cache->mfc_parent;
1847 cache->mfc_un.res.pkt++;
1848 cache->mfc_un.res.bytes += skb->len;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03001849 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001851 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001852 struct mfc_cache *cache_proxy;
1853
1854 /* For an (*,G) entry, we only check that the incomming
1855 * interface is part of the static tree.
1856 */
1857 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1858 if (cache_proxy &&
1859 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1860 goto forward;
1861 }
1862
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001863 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001864 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001865 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001867 * Very complicated situation...
1868 *
1869 * The best workaround until routing daemons will be
1870 * fixed is not to redistribute packet, if it was
1871 * send through wrong interface. It means, that
1872 * multicast applications WILL NOT work for
1873 * (S,G), which have default multicast route pointing
1874 * to wrong oif. In any case, it is not a good
1875 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 */
1877 goto dont_forward;
1878 }
1879
1880 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Patrick McHardy0c122952010-04-13 05:03:22 +00001882 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001884 * so that we cannot check that packet arrived on an oif.
1885 * It is bad, but otherwise we would need to move pretty
1886 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001888 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001889 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001890 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1892 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001893 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895 goto dont_forward;
1896 }
1897
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001898forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001899 mrt->vif_table[vif].pkt_in++;
1900 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001902 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001903 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1904 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001905 if (true_vifi >= 0 &&
1906 true_vifi != cache->mfc_parent &&
1907 ip_hdr(skb)->ttl >
1908 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1909 /* It's an (*,*) entry and the packet is not coming from
1910 * the upstream: forward the packet to the upstream
1911 * only.
1912 */
1913 psend = cache->mfc_parent;
1914 goto last_forward;
1915 }
1916 goto dont_forward;
1917 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001918 for (ct = cache->mfc_un.res.maxvif - 1;
1919 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001920 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001921 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1922 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001923 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (psend != -1) {
1925 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001928 ipmr_queue_xmit(net, mrt, skb2, cache,
1929 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001931 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001934last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (psend != -1) {
1936 if (local) {
1937 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001940 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001942 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001943 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945 }
1946
1947dont_forward:
1948 if (!local)
1949 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
David S. Miller417da662011-05-03 19:42:43 -07001952static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001953{
David S. Miller417da662011-05-03 19:42:43 -07001954 struct rtable *rt = skb_rtable(skb);
1955 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001956 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001957 .daddr = iph->daddr,
1958 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001959 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001960 .flowi4_oif = (rt_is_output_route(rt) ?
1961 skb->dev->ifindex : 0),
1962 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001963 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001964 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001965 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001966 };
1967 struct mr_table *mrt;
1968 int err;
1969
David S. Millerda919812011-03-12 02:04:50 -05001970 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001971 if (err)
1972 return ERR_PTR(err);
1973 return mrt;
1974}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001976/* Multicast packets for forwarding arrive here
1977 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979int ip_mr_input(struct sk_buff *skb)
1980{
1981 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001982 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001983 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001984 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12001985 struct net_device *dev;
1986
1987 /* skb->dev passed in is the loX master dev for vrfs.
1988 * As there are no vifs associated with loopback devices,
1989 * get the proper interface that does have a vif associated with it.
1990 */
1991 dev = skb->dev;
1992 if (netif_is_l3_master(skb->dev)) {
1993 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
1994 if (!dev) {
1995 kfree_skb(skb);
1996 return -ENODEV;
1997 }
1998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002001 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 */
Eric Dumazet4c968702010-10-01 16:15:01 +00002003 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 goto dont_forward;
2005
David S. Miller417da662011-05-03 19:42:43 -07002006 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002007 if (IS_ERR(mrt)) {
2008 kfree_skb(skb);
2009 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00002012 if (IPCB(skb)->opt.router_alert) {
2013 if (ip_call_ra_chain(skb))
2014 return 0;
2015 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2016 /* IGMPv1 (and broken IGMPv2 implementations sort of
2017 * Cisco IOS <= 11.2(8)) do not put router alert
2018 * option to IGMP packets destined to routable
2019 * groups. It is very bad, because it means
2020 * that we can forward NO IGMP messages.
2021 */
2022 struct sock *mroute_sk;
2023
2024 mroute_sk = rcu_dereference(mrt->mroute_sk);
2025 if (mroute_sk) {
2026 nf_reset(skb);
2027 raw_rcv(mroute_sk, skb);
2028 return 0;
2029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031 }
2032
Eric Dumazeta8c94862010-10-01 16:15:08 +00002033 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002034 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002035 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002036 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002037
2038 if (vif >= 0)
2039 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2040 vif);
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002043 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002044 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int vif;
2046
2047 if (local) {
2048 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2049 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002050 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 skb = skb2;
2053 }
2054
Eric Dumazeta8c94862010-10-01 16:15:08 +00002055 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002056 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002058 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 read_unlock(&mrt_lock);
2060
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002061 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063 read_unlock(&mrt_lock);
2064 kfree_skb(skb);
2065 return -ENODEV;
2066 }
2067
Eric Dumazeta8c94862010-10-01 16:15:08 +00002068 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002069 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 read_unlock(&mrt_lock);
2071
2072 if (local)
2073 return ip_local_deliver(skb);
2074
2075 return 0;
2076
2077dont_forward:
2078 if (local)
2079 return ip_local_deliver(skb);
2080 kfree_skb(skb);
2081 return 0;
2082}
2083
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002084#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002085/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002086int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002087{
2088 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002089 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002090 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002091
2092 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2093 goto drop;
2094
2095 pim = igmp_hdr(skb);
2096
David S. Miller417da662011-05-03 19:42:43 -07002097 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002098 if (IS_ERR(mrt))
2099 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002100 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002101 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2102 goto drop;
2103
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002104 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002105drop:
2106 kfree_skb(skb);
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return 0;
2109}
2110#endif
2111
2112#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002113static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002116 struct net *net = dev_net(skb->dev);
2117 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002119 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 goto drop;
2121
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002122 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002123 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002124 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002125 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002126 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 goto drop;
2128
David S. Miller417da662011-05-03 19:42:43 -07002129 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002130 if (IS_ERR(mrt))
2131 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002132 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002133drop:
2134 kfree_skb(skb);
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return 0;
2137}
2138#endif
2139
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002140static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2141 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002143 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002144 struct nlattr *mp_attr;
2145 struct rtnexthop *nhp;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002146 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002147 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Nicolas Dichtel74381892010-03-25 23:45:35 +00002149 /* If cache is unresolved, don't try to parse IIF and OIF */
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002150 if (c->mfc_parent >= MAXVIFS) {
2151 rtm->rtm_flags |= RTNH_F_UNRESOLVED;
Nicolas Dichtel74381892010-03-25 23:45:35 +00002152 return -ENOENT;
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002153 }
Nicolas Dichtel74381892010-03-25 23:45:35 +00002154
Thomas Graf92a395e2012-06-26 23:36:13 +00002155 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2156 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2157 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Thomas Graf92a395e2012-06-26 23:36:13 +00002159 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2160 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002163 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002164 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2165 nla_nest_cancel(skb, mp_attr);
2166 return -EMSGSIZE;
2167 }
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 nhp->rtnh_flags = 0;
2170 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002171 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 nhp->rtnh_len = sizeof(*nhp);
2173 }
2174 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002175
2176 nla_nest_end(skb, mp_attr);
2177
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002178 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2179 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2180
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002181 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2182 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2183 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002184 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002185 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002186 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002187 return -EMSGSIZE;
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 rtm->rtm_type = RTN_MULTICAST;
2190 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
David S. Miller9a1b9492011-05-04 12:18:54 -07002193int ipmr_get_route(struct net *net, struct sk_buff *skb,
2194 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002195 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002198 struct mr_table *mrt;
2199 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002201 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002202 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002203 return -ENOENT;
2204
Eric Dumazeta8c94862010-10-01 16:15:08 +00002205 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002206 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002207 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002208 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002210 if (vif >= 0)
2211 cache = ipmr_cache_find_any(mrt, daddr, vif);
2212 }
Ian Morris51456b22015-04-03 09:17:26 +01002213 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002214 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002215 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002217 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002220 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002221 if (dev)
2222 vif = ipmr_find_vif(mrt, dev);
2223 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002225 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return -ENODEV;
2227 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002228 skb2 = skb_clone(skb, GFP_ATOMIC);
2229 if (!skb2) {
2230 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002231 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002232 return -ENOMEM;
2233 }
2234
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002235 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002236 skb_push(skb2, sizeof(struct iphdr));
2237 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002238 iph = ip_hdr(skb2);
2239 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002240 iph->saddr = saddr;
2241 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002242 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002243 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002245 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return err;
2247 }
2248
Eric Dumazeta8c94862010-10-01 16:15:08 +00002249 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002250 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002252 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return err;
2254}
2255
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002256static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002257 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2258 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002259{
2260 struct nlmsghdr *nlh;
2261 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002262 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002263
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002264 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002265 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002266 return -EMSGSIZE;
2267
2268 rtm = nlmsg_data(nlh);
2269 rtm->rtm_family = RTNL_FAMILY_IPMR;
2270 rtm->rtm_dst_len = 32;
2271 rtm->rtm_src_len = 32;
2272 rtm->rtm_tos = 0;
2273 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002274 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2275 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002276 rtm->rtm_type = RTN_MULTICAST;
2277 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002278 if (c->mfc_flags & MFC_STATIC)
2279 rtm->rtm_protocol = RTPROT_STATIC;
2280 else
2281 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002282 rtm->rtm_flags = 0;
2283
Jiri Benc930345e2015-03-29 16:59:25 +02002284 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2285 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002286 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002287 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2288 /* do not break the dump if cache is unresolved */
2289 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002290 goto nla_put_failure;
2291
Johannes Berg053c0952015-01-16 22:09:00 +01002292 nlmsg_end(skb, nlh);
2293 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002294
2295nla_put_failure:
2296 nlmsg_cancel(skb, nlh);
2297 return -EMSGSIZE;
2298}
2299
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002300static size_t mroute_msgsize(bool unresolved, int maxvif)
2301{
2302 size_t len =
2303 NLMSG_ALIGN(sizeof(struct rtmsg))
2304 + nla_total_size(4) /* RTA_TABLE */
2305 + nla_total_size(4) /* RTA_SRC */
2306 + nla_total_size(4) /* RTA_DST */
2307 ;
2308
2309 if (!unresolved)
2310 len = len
2311 + nla_total_size(4) /* RTA_IIF */
2312 + nla_total_size(0) /* RTA_MULTIPATH */
2313 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2314 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002315 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002316 ;
2317
2318 return len;
2319}
2320
2321static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2322 int cmd)
2323{
2324 struct net *net = read_pnet(&mrt->net);
2325 struct sk_buff *skb;
2326 int err = -ENOBUFS;
2327
2328 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2329 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002330 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002331 goto errout;
2332
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002333 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002334 if (err < 0)
2335 goto errout;
2336
2337 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2338 return;
2339
2340errout:
2341 kfree_skb(skb);
2342 if (err < 0)
2343 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2344}
2345
Julien Gomes5a645dd2017-06-20 13:54:17 -07002346static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2347{
2348 size_t len =
2349 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2350 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2351 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2352 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2353 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2354 /* IPMRA_CREPORT_PKT */
2355 + nla_total_size(payloadlen)
2356 ;
2357
2358 return len;
2359}
2360
2361static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2362{
2363 struct net *net = read_pnet(&mrt->net);
2364 struct nlmsghdr *nlh;
2365 struct rtgenmsg *rtgenm;
2366 struct igmpmsg *msg;
2367 struct sk_buff *skb;
2368 struct nlattr *nla;
2369 int payloadlen;
2370
2371 payloadlen = pkt->len - sizeof(struct igmpmsg);
2372 msg = (struct igmpmsg *)skb_network_header(pkt);
2373
2374 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2375 if (!skb)
2376 goto errout;
2377
2378 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2379 sizeof(struct rtgenmsg), 0);
2380 if (!nlh)
2381 goto errout;
2382 rtgenm = nlmsg_data(nlh);
2383 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2384 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2385 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2386 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2387 msg->im_src.s_addr) ||
2388 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2389 msg->im_dst.s_addr))
2390 goto nla_put_failure;
2391
2392 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2393 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2394 nla_data(nla), payloadlen))
2395 goto nla_put_failure;
2396
2397 nlmsg_end(skb, nlh);
2398
2399 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2400 return;
2401
2402nla_put_failure:
2403 nlmsg_cancel(skb, nlh);
2404errout:
2405 kfree_skb(skb);
2406 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2407}
2408
Donald Sharp4f75ba62017-06-28 13:58:57 -04002409static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2410 struct netlink_ext_ack *extack)
2411{
2412 struct net *net = sock_net(in_skb->sk);
2413 struct nlattr *tb[RTA_MAX + 1];
2414 struct sk_buff *skb = NULL;
2415 struct mfc_cache *cache;
2416 struct mr_table *mrt;
2417 struct rtmsg *rtm;
2418 __be32 src, grp;
2419 u32 tableid;
2420 int err;
2421
2422 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2423 rtm_ipv4_policy, extack);
2424 if (err < 0)
2425 goto errout;
2426
2427 rtm = nlmsg_data(nlh);
2428
2429 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2430 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2431 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2432
2433 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002434 if (!mrt) {
2435 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002436 goto errout_free;
2437 }
2438
2439 /* entries are added/deleted only under RTNL */
2440 rcu_read_lock();
2441 cache = ipmr_cache_find(mrt, src, grp);
2442 rcu_read_unlock();
2443 if (!cache) {
2444 err = -ENOENT;
2445 goto errout_free;
2446 }
2447
2448 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2449 if (!skb) {
2450 err = -ENOBUFS;
2451 goto errout_free;
2452 }
2453
2454 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2455 nlh->nlmsg_seq, cache,
2456 RTM_NEWROUTE, 0);
2457 if (err < 0)
2458 goto errout_free;
2459
2460 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2461
2462errout:
2463 return err;
2464
2465errout_free:
2466 kfree_skb(skb);
2467 goto errout;
2468}
2469
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002470static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2471{
2472 struct net *net = sock_net(skb->sk);
2473 struct mr_table *mrt;
2474 struct mfc_cache *mfc;
2475 unsigned int t = 0, s_t;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002476 unsigned int e = 0, s_e;
2477
2478 s_t = cb->args[0];
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002479 s_e = cb->args[1];
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002480
Eric Dumazeta8c94862010-10-01 16:15:08 +00002481 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002482 ipmr_for_each_table(mrt, net) {
2483 if (t < s_t)
2484 goto next_table;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002485 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
2486 if (e < s_e)
2487 goto next_entry;
2488 if (ipmr_fill_mroute(mrt, skb,
2489 NETLINK_CB(cb->skb).portid,
2490 cb->nlh->nlmsg_seq,
2491 mfc, RTM_NEWROUTE,
2492 NLM_F_MULTI) < 0)
2493 goto done;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002494next_entry:
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002495 e++;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002496 }
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002497 e = 0;
2498 s_e = 0;
2499
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002500 spin_lock_bh(&mfc_unres_lock);
2501 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2502 if (e < s_e)
2503 goto next_entry2;
2504 if (ipmr_fill_mroute(mrt, skb,
2505 NETLINK_CB(cb->skb).portid,
2506 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002507 mfc, RTM_NEWROUTE,
2508 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002509 spin_unlock_bh(&mfc_unres_lock);
2510 goto done;
2511 }
2512next_entry2:
2513 e++;
2514 }
2515 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002516 e = 0;
2517 s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002518next_table:
2519 t++;
2520 }
2521done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002522 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002523
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002524 cb->args[1] = e;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002525 cb->args[0] = t;
2526
2527 return skb->len;
2528}
2529
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002530static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2531 [RTA_SRC] = { .type = NLA_U32 },
2532 [RTA_DST] = { .type = NLA_U32 },
2533 [RTA_IIF] = { .type = NLA_U32 },
2534 [RTA_TABLE] = { .type = NLA_U32 },
2535 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2536};
2537
2538static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2539{
2540 switch (rtm_protocol) {
2541 case RTPROT_STATIC:
2542 case RTPROT_MROUTED:
2543 return true;
2544 }
2545 return false;
2546}
2547
2548static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2549{
2550 struct rtnexthop *rtnh = nla_data(nla);
2551 int remaining = nla_len(nla), vifi = 0;
2552
2553 while (rtnh_ok(rtnh, remaining)) {
2554 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2555 if (++vifi == MAXVIFS)
2556 break;
2557 rtnh = rtnh_next(rtnh, &remaining);
2558 }
2559
2560 return remaining > 0 ? -EINVAL : vifi;
2561}
2562
2563/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2564static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2565 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002566 struct mr_table **mrtret,
2567 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002568{
2569 struct net_device *dev = NULL;
2570 u32 tblid = RT_TABLE_DEFAULT;
2571 struct mr_table *mrt;
2572 struct nlattr *attr;
2573 struct rtmsg *rtm;
2574 int ret, rem;
2575
Johannes Bergfceb6432017-04-12 14:34:07 +02002576 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002577 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002578 if (ret < 0)
2579 goto out;
2580 rtm = nlmsg_data(nlh);
2581
2582 ret = -EINVAL;
2583 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2584 rtm->rtm_type != RTN_MULTICAST ||
2585 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2586 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2587 goto out;
2588
2589 memset(mfcc, 0, sizeof(*mfcc));
2590 mfcc->mfcc_parent = -1;
2591 ret = 0;
2592 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2593 switch (nla_type(attr)) {
2594 case RTA_SRC:
2595 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2596 break;
2597 case RTA_DST:
2598 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2599 break;
2600 case RTA_IIF:
2601 dev = __dev_get_by_index(net, nla_get_u32(attr));
2602 if (!dev) {
2603 ret = -ENODEV;
2604 goto out;
2605 }
2606 break;
2607 case RTA_MULTIPATH:
2608 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2609 ret = -EINVAL;
2610 goto out;
2611 }
2612 break;
2613 case RTA_PREFSRC:
2614 ret = 1;
2615 break;
2616 case RTA_TABLE:
2617 tblid = nla_get_u32(attr);
2618 break;
2619 }
2620 }
2621 mrt = ipmr_get_table(net, tblid);
2622 if (!mrt) {
2623 ret = -ENOENT;
2624 goto out;
2625 }
2626 *mrtret = mrt;
2627 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2628 if (dev)
2629 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2630
2631out:
2632 return ret;
2633}
2634
2635/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002636static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2637 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002638{
2639 struct net *net = sock_net(skb->sk);
2640 int ret, mrtsock, parent;
2641 struct mr_table *tbl;
2642 struct mfcctl mfcc;
2643
2644 mrtsock = 0;
2645 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002646 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002647 if (ret < 0)
2648 return ret;
2649
2650 parent = ret ? mfcc.mfcc_parent : -1;
2651 if (nlh->nlmsg_type == RTM_NEWROUTE)
2652 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2653 else
2654 return ipmr_mfc_delete(tbl, &mfcc, parent);
2655}
2656
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002657static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2658{
2659 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2660
2661 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2662 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2663 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2664 mrt->mroute_reg_vif_num) ||
2665 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2666 mrt->mroute_do_assert) ||
2667 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2668 return false;
2669
2670 return true;
2671}
2672
2673static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2674{
2675 struct nlattr *vif_nest;
2676 struct vif_device *vif;
2677
2678 /* if the VIF doesn't exist just continue */
2679 if (!VIF_EXISTS(mrt, vifid))
2680 return true;
2681
2682 vif = &mrt->vif_table[vifid];
2683 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2684 if (!vif_nest)
2685 return false;
2686 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2687 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2688 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2689 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2690 IPMRA_VIFA_PAD) ||
2691 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2692 IPMRA_VIFA_PAD) ||
2693 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2694 IPMRA_VIFA_PAD) ||
2695 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2696 IPMRA_VIFA_PAD) ||
2697 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2698 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2699 nla_nest_cancel(skb, vif_nest);
2700 return false;
2701 }
2702 nla_nest_end(skb, vif_nest);
2703
2704 return true;
2705}
2706
2707static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2708{
2709 struct net *net = sock_net(skb->sk);
2710 struct nlmsghdr *nlh = NULL;
2711 unsigned int t = 0, s_t;
2712 unsigned int e = 0, s_e;
2713 struct mr_table *mrt;
2714
2715 s_t = cb->args[0];
2716 s_e = cb->args[1];
2717
2718 ipmr_for_each_table(mrt, net) {
2719 struct nlattr *vifs, *af;
2720 struct ifinfomsg *hdr;
2721 u32 i;
2722
2723 if (t < s_t)
2724 goto skip_table;
2725 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2726 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2727 sizeof(*hdr), NLM_F_MULTI);
2728 if (!nlh)
2729 break;
2730
2731 hdr = nlmsg_data(nlh);
2732 memset(hdr, 0, sizeof(*hdr));
2733 hdr->ifi_family = RTNL_FAMILY_IPMR;
2734
2735 af = nla_nest_start(skb, IFLA_AF_SPEC);
2736 if (!af) {
2737 nlmsg_cancel(skb, nlh);
2738 goto out;
2739 }
2740
2741 if (!ipmr_fill_table(mrt, skb)) {
2742 nlmsg_cancel(skb, nlh);
2743 goto out;
2744 }
2745
2746 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2747 if (!vifs) {
2748 nla_nest_end(skb, af);
2749 nlmsg_end(skb, nlh);
2750 goto out;
2751 }
2752 for (i = 0; i < mrt->maxvif; i++) {
2753 if (e < s_e)
2754 goto skip_entry;
2755 if (!ipmr_fill_vif(mrt, i, skb)) {
2756 nla_nest_end(skb, vifs);
2757 nla_nest_end(skb, af);
2758 nlmsg_end(skb, nlh);
2759 goto out;
2760 }
2761skip_entry:
2762 e++;
2763 }
2764 s_e = 0;
2765 e = 0;
2766 nla_nest_end(skb, vifs);
2767 nla_nest_end(skb, af);
2768 nlmsg_end(skb, nlh);
2769skip_table:
2770 t++;
2771 }
2772
2773out:
2774 cb->args[1] = e;
2775 cb->args[0] = t;
2776
2777 return skb->len;
2778}
2779
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002780#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002781/* The /proc interfaces to multicast routing :
2782 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 */
2784struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002785 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002786 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 int ct;
2788};
2789
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002790static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2791 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 loff_t pos)
2793{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002794 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002795
2796 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2797 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002799 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002800 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802 return NULL;
2803}
2804
2805static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002806 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002808 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002809 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002810 struct mr_table *mrt;
2811
2812 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002813 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002814 return ERR_PTR(-ENOENT);
2815
2816 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002819 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 : SEQ_START_TOKEN;
2821}
2822
2823static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2824{
2825 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002826 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002827 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 ++*pos;
2830 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002831 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002832
Patrick McHardy0c122952010-04-13 05:03:22 +00002833 while (++iter->ct < mrt->maxvif) {
2834 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002836 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838 return NULL;
2839}
2840
2841static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002842 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 read_unlock(&mrt_lock);
2845}
2846
2847static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2848{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002849 struct ipmr_vif_iter *iter = seq->private;
2850 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002853 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2855 } else {
2856 const struct vif_device *vif = v;
2857 const char *name = vif->dev ? vif->dev->name : "none";
2858
2859 seq_printf(seq,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002860 "%2zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002861 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002862 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 vif->bytes_out, vif->pkt_out,
2864 vif->flags, vif->local, vif->remote);
2865 }
2866 return 0;
2867}
2868
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002869static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 .start = ipmr_vif_seq_start,
2871 .next = ipmr_vif_seq_next,
2872 .stop = ipmr_vif_seq_stop,
2873 .show = ipmr_vif_seq_show,
2874};
2875
2876static int ipmr_vif_open(struct inode *inode, struct file *file)
2877{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002878 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2879 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
Arjan van de Ven9a321442007-02-12 00:55:35 -08002882static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 .owner = THIS_MODULE,
2884 .open = ipmr_vif_open,
2885 .read = seq_read,
2886 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002887 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888};
2889
2890struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002891 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002892 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002893 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894};
2895
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002896static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2897 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002899 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 struct mfc_cache *mfc;
2901
Eric Dumazeta8c94862010-10-01 16:15:08 +00002902 rcu_read_lock();
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002903 it->cache = &mrt->mfc_cache_list;
2904 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
2905 if (pos-- == 0)
2906 return mfc;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002907 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002910 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002911 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002912 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return mfc;
2914 spin_unlock_bh(&mfc_unres_lock);
2915
2916 it->cache = NULL;
2917 return NULL;
2918}
2919
2920
2921static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2922{
2923 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002924 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002925 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002926
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002927 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002928 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002929 return ERR_PTR(-ENOENT);
2930
2931 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 it->cache = NULL;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002933 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 : SEQ_START_TOKEN;
2935}
2936
2937static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2938{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002940 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002941 struct mr_table *mrt = it->mrt;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002942 struct mfc_cache *mfc = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 ++*pos;
2945
2946 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002947 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Patrick McHardy862465f2010-04-13 05:03:21 +00002949 if (mfc->list.next != it->cache)
2950 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002951
Patrick McHardy0c122952010-04-13 05:03:22 +00002952 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 goto end_of_list;
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002956 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002957 it->cache = &mrt->mfc_unres_queue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002960 if (!list_empty(it->cache))
2961 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002963end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 spin_unlock_bh(&mfc_unres_lock);
2965 it->cache = NULL;
2966
2967 return NULL;
2968}
2969
2970static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2971{
2972 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002973 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Patrick McHardy0c122952010-04-13 05:03:22 +00002975 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002977 else if (it->cache == &mrt->mfc_cache_list)
Eric Dumazeta8c94862010-10-01 16:15:08 +00002978 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980
2981static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2982{
2983 int n;
2984
2985 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002986 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2988 } else {
2989 const struct mfc_cache *mfc = v;
2990 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002991 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002992
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002993 seq_printf(seq, "%08X %08X %-3hd",
2994 (__force u32) mfc->mfc_mcastgrp,
2995 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002996 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Patrick McHardy0c122952010-04-13 05:03:22 +00002998 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002999 seq_printf(seq, " %8lu %8lu %8lu",
3000 mfc->mfc_un.res.pkt,
3001 mfc->mfc_un.res.bytes,
3002 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08003003 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00003004 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00003005 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00003006 mfc->mfc_un.res.ttls[n] < 255)
3007 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003008 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 n, mfc->mfc_un.res.ttls[n]);
3010 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003011 } else {
3012 /* unresolved mfc_caches don't contain
3013 * pkt, bytes and wrong_if values
3014 */
3015 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 }
3017 seq_putc(seq, '\n');
3018 }
3019 return 0;
3020}
3021
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003022static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 .start = ipmr_mfc_seq_start,
3024 .next = ipmr_mfc_seq_next,
3025 .stop = ipmr_mfc_seq_stop,
3026 .show = ipmr_mfc_seq_show,
3027};
3028
3029static int ipmr_mfc_open(struct inode *inode, struct file *file)
3030{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003031 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
3032 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Arjan van de Ven9a321442007-02-12 00:55:35 -08003035static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 .owner = THIS_MODULE,
3037 .open = ipmr_mfc_open,
3038 .read = seq_read,
3039 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003040 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003042#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003045static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07003047 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048};
3049#endif
3050
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003051/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003052static int __net_init ipmr_net_init(struct net *net)
3053{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003054 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003055
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003056 err = ipmr_rules_init(net);
3057 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00003058 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003059
3060#ifdef CONFIG_PROC_FS
3061 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00003062 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003063 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00003064 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003065 goto proc_cache_fail;
3066#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003067 return 0;
3068
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003069#ifdef CONFIG_PROC_FS
3070proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003071 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003072proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003073 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003074#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00003075fail:
3076 return err;
3077}
3078
3079static void __net_exit ipmr_net_exit(struct net *net)
3080{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003081#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003082 remove_proc_entry("ip_mr_cache", net->proc_net);
3083 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003084#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003085 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003086}
3087
3088static struct pernet_operations ipmr_net_ops = {
3089 .init = ipmr_net_init,
3090 .exit = ipmr_net_exit,
3091};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003092
Wang Chen03d2f892008-07-03 12:13:36 +08003093int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Wang Chen03d2f892008-07-03 12:13:36 +08003095 int err;
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3098 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003099 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003100 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003101
Benjamin Therycf958ae32009-01-22 04:56:16 +00003102 err = register_pernet_subsys(&ipmr_net_ops);
3103 if (err)
3104 goto reg_pernet_fail;
3105
Wang Chen03d2f892008-07-03 12:13:36 +08003106 err = register_netdevice_notifier(&ip_mr_notifier);
3107 if (err)
3108 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003109#ifdef CONFIG_IP_PIMSM_V2
3110 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003111 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003112 err = -EAGAIN;
3113 goto add_proto_fail;
3114 }
3115#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003116 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Donald Sharp4f75ba62017-06-28 13:58:57 -04003117 ipmr_rtm_getroute, ipmr_rtm_dumproute, NULL);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003118 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3119 ipmr_rtm_route, NULL, NULL);
3120 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3121 ipmr_rtm_route, NULL, NULL);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003122
3123 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3124 NULL, ipmr_rtm_dumplink, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003125 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003126
Tom Goff403dbb92009-06-14 03:16:13 -07003127#ifdef CONFIG_IP_PIMSM_V2
3128add_proto_fail:
3129 unregister_netdevice_notifier(&ip_mr_notifier);
3130#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003131reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003132 unregister_pernet_subsys(&ipmr_net_ops);
3133reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003134 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003135 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}