blob: 395e2814a46d9995b63b6bc703e28433ecc26c2b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010069#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardyf0ad0862010-04-13 05:03:23 +000071struct ipmr_rule {
72 struct fib_rule common;
73};
74
75struct ipmr_result {
76 struct mr_table *mrt;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000080 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static DEFINE_RWLOCK(mrt_lock);
84
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010085/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Special spinlock for queue of unresolved entries */
88static DEFINE_SPINLOCK(mfc_unres_lock);
89
90/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000091 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
94 *
95 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97
Christoph Lametere18b8902006-12-06 20:33:20 -080098static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000100static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000101static void ipmr_free_table(struct mr_table *mrt);
102
Rami Rosenc4854ec2013-07-20 15:09:28 +0300103static void ip_mr_forward(struct net *net, struct mr_table *mrt,
104 struct sk_buff *skb, struct mfc_cache *cache,
105 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000106static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000107 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200108static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
109 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000110static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
111 int cmd);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100112static void mroute_clean_tables(struct mr_table *mrt, bool all);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000113static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000115#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
116#define ipmr_for_each_table(mrt, net) \
117 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
118
119static struct mr_table *ipmr_get_table(struct net *net, u32 id)
120{
121 struct mr_table *mrt;
122
123 ipmr_for_each_table(mrt, net) {
124 if (mrt->id == id)
125 return mrt;
126 }
127 return NULL;
128}
129
David S. Millerda919812011-03-12 02:04:50 -0500130static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000131 struct mr_table **mrt)
132{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000133 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100134 struct ipmr_result res;
135 struct fib_lookup_arg arg = {
136 .result = &res,
137 .flags = FIB_LOOKUP_NOREF,
138 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000139
David S. Millerda919812011-03-12 02:04:50 -0500140 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
141 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000142 if (err < 0)
143 return err;
144 *mrt = res.mrt;
145 return 0;
146}
147
148static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
149 int flags, struct fib_lookup_arg *arg)
150{
151 struct ipmr_result *res = arg->result;
152 struct mr_table *mrt;
153
154 switch (rule->action) {
155 case FR_ACT_TO_TBL:
156 break;
157 case FR_ACT_UNREACHABLE:
158 return -ENETUNREACH;
159 case FR_ACT_PROHIBIT:
160 return -EACCES;
161 case FR_ACT_BLACKHOLE:
162 default:
163 return -EINVAL;
164 }
165
166 mrt = ipmr_get_table(rule->fr_net, rule->table);
Ian Morris51456b22015-04-03 09:17:26 +0100167 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000168 return -EAGAIN;
169 res->mrt = mrt;
170 return 0;
171}
172
173static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
174{
175 return 1;
176}
177
178static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
179 FRA_GENERIC_POLICY,
180};
181
182static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
183 struct fib_rule_hdr *frh, struct nlattr **tb)
184{
185 return 0;
186}
187
188static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
189 struct nlattr **tb)
190{
191 return 1;
192}
193
194static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
195 struct fib_rule_hdr *frh)
196{
197 frh->dst_len = 0;
198 frh->src_len = 0;
199 frh->tos = 0;
200 return 0;
201}
202
Andi Kleen04a6f822012-10-04 17:12:11 -0700203static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200204 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000205 .rule_size = sizeof(struct ipmr_rule),
206 .addr_size = sizeof(u32),
207 .action = ipmr_rule_action,
208 .match = ipmr_rule_match,
209 .configure = ipmr_rule_configure,
210 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000211 .fill = ipmr_rule_fill,
212 .nlgroup = RTNLGRP_IPV4_RULE,
213 .policy = ipmr_rule_policy,
214 .owner = THIS_MODULE,
215};
216
217static int __net_init ipmr_rules_init(struct net *net)
218{
219 struct fib_rules_ops *ops;
220 struct mr_table *mrt;
221 int err;
222
223 ops = fib_rules_register(&ipmr_rules_ops_template, net);
224 if (IS_ERR(ops))
225 return PTR_ERR(ops);
226
227 INIT_LIST_HEAD(&net->ipv4.mr_tables);
228
229 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100230 if (IS_ERR(mrt)) {
231 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000232 goto err1;
233 }
234
235 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
236 if (err < 0)
237 goto err2;
238
239 net->ipv4.mr_rules_ops = ops;
240 return 0;
241
242err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700243 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000244err1:
245 fib_rules_unregister(ops);
246 return err;
247}
248
249static void __net_exit ipmr_rules_exit(struct net *net)
250{
251 struct mr_table *mrt, *next;
252
WANG Conged785302015-03-31 11:01:45 -0700253 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000254 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
255 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000256 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000257 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000258 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700259 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000260}
261#else
262#define ipmr_for_each_table(mrt, net) \
263 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
264
265static struct mr_table *ipmr_get_table(struct net *net, u32 id)
266{
267 return net->ipv4.mrt;
268}
269
David S. Millerda919812011-03-12 02:04:50 -0500270static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000271 struct mr_table **mrt)
272{
273 *mrt = net->ipv4.mrt;
274 return 0;
275}
276
277static int __net_init ipmr_rules_init(struct net *net)
278{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100279 struct mr_table *mrt;
280
281 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
282 if (IS_ERR(mrt))
283 return PTR_ERR(mrt);
284 net->ipv4.mrt = mrt;
285 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000286}
287
288static void __net_exit ipmr_rules_exit(struct net *net)
289{
WANG Conged785302015-03-31 11:01:45 -0700290 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000291 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700292 net->ipv4.mrt = NULL;
293 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000294}
295#endif
296
297static struct mr_table *ipmr_new_table(struct net *net, u32 id)
298{
299 struct mr_table *mrt;
300 unsigned int i;
301
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100302 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
303 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
304 return ERR_PTR(-EINVAL);
305
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000306 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100307 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000308 return mrt;
309
310 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100311 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100312 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200313 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000314 mrt->id = id;
315
316 /* Forwarding cache */
317 for (i = 0; i < MFC_LINES; i++)
318 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
319
320 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
321
322 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
323 (unsigned long)mrt);
324
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000325 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000326#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
327 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
328#endif
329 return mrt;
330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000332static void ipmr_free_table(struct mr_table *mrt)
333{
334 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100335 mroute_clean_tables(mrt, true);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000336 kfree(mrt);
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
340
Wang Chend6070322008-07-14 20:55:26 -0700341static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
342{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000343 struct net *net = dev_net(dev);
344
Wang Chend6070322008-07-14 20:55:26 -0700345 dev_close(dev);
346
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000347 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700348 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800349 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700350 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700351 struct ip_tunnel_parm p;
352
353 memset(&p, 0, sizeof(p));
354 p.iph.daddr = v->vifc_rmt_addr.s_addr;
355 p.iph.saddr = v->vifc_lcl_addr.s_addr;
356 p.iph.version = 4;
357 p.iph.ihl = 5;
358 p.iph.protocol = IPPROTO_IPIP;
359 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
360 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
361
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800362 if (ops->ndo_do_ioctl) {
363 mm_segment_t oldfs = get_fs();
364
365 set_fs(KERNEL_DS);
366 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
367 set_fs(oldfs);
368 }
Wang Chend6070322008-07-14 20:55:26 -0700369 }
370}
371
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100372/* Initialize ipmr pimreg/tunnel in_device */
373static bool ipmr_init_vif_indev(const struct net_device *dev)
374{
375 struct in_device *in_dev;
376
377 ASSERT_RTNL();
378
379 in_dev = __in_dev_get_rtnl(dev);
380 if (!in_dev)
381 return false;
382 ipv4_devconf_setall(in_dev);
383 neigh_parms_data_state_setall(in_dev->arp_parms);
384 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
385
386 return true;
387}
388
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100389static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct net_device *dev;
392
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000393 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800396 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int err;
398 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 memset(&p, 0, sizeof(p));
402 p.iph.daddr = v->vifc_rmt_addr.s_addr;
403 p.iph.saddr = v->vifc_lcl_addr.s_addr;
404 p.iph.version = 4;
405 p.iph.ihl = 5;
406 p.iph.protocol = IPPROTO_IPIP;
407 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800408 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800410 if (ops->ndo_do_ioctl) {
411 mm_segment_t oldfs = get_fs();
412
413 set_fs(KERNEL_DS);
414 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
415 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000416 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800417 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dev = NULL;
420
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000421 if (err == 0 &&
422 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100424 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (dev_open(dev))
427 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700428 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 }
431 return dev;
432
433failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 unregister_netdevice(dev);
435 return NULL;
436}
437
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100438#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000439static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000441 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000442 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500443 struct flowi4 fl4 = {
444 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700445 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500446 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000447 };
448 int err;
449
David S. Millerda919812011-03-12 02:04:50 -0500450 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000451 if (err < 0) {
452 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000453 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000454 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700457 dev->stats.tx_bytes += skb->len;
458 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000459 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 read_unlock(&mrt_lock);
461 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000462 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200465static int reg_vif_get_iflink(const struct net_device *dev)
466{
467 return 0;
468}
469
Stephen Hemminger007c3832008-11-20 20:28:35 -0800470static const struct net_device_ops reg_vif_netdev_ops = {
471 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200472 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800473};
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static void reg_vif_setup(struct net_device *dev)
476{
477 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800478 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530480 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700482 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000485static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000488 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000490 if (mrt->id == RT_TABLE_DEFAULT)
491 sprintf(name, "pimreg");
492 else
493 sprintf(name, "pimreg%u", mrt->id);
494
Tom Gundersenc835a672014-07-14 16:37:24 +0200495 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Ian Morris51456b22015-04-03 09:17:26 +0100497 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return NULL;
499
Tom Goff403dbb92009-06-14 03:16:13 -0700500 dev_net_set(dev, net);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (register_netdevice(dev)) {
503 free_netdev(dev);
504 return NULL;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100507 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (dev_open(dev))
510 goto failure;
511
Wang Chen7dc00c82008-07-14 20:56:34 -0700512 dev_hold(dev);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return dev;
515
516failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unregister_netdevice(dev);
518 return NULL;
519}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100520
521/* called with rcu_read_lock() */
522static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
523 unsigned int pimlen)
524{
525 struct net_device *reg_dev = NULL;
526 struct iphdr *encap;
527
528 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100529 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100530 * a. packet is really sent to a multicast group
531 * b. packet is not a NULL-REGISTER
532 * c. packet is not truncated
533 */
534 if (!ipv4_is_multicast(encap->daddr) ||
535 encap->tot_len == 0 ||
536 ntohs(encap->tot_len) + pimlen > skb->len)
537 return 1;
538
539 read_lock(&mrt_lock);
540 if (mrt->mroute_reg_vif_num >= 0)
541 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
542 read_unlock(&mrt_lock);
543
544 if (!reg_dev)
545 return 1;
546
547 skb->mac_header = skb->network_header;
548 skb_pull(skb, (u8 *)encap - skb->data);
549 skb_reset_network_header(skb);
550 skb->protocol = htons(ETH_P_IP);
551 skb->ip_summed = CHECKSUM_NONE;
552
553 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
554
555 netif_rx(skb);
556
557 return NET_RX_SUCCESS;
558}
559#else
560static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
561{
562 return NULL;
563}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#endif
565
Ben Hutchings2c530402012-07-10 10:55:09 +0000566/**
567 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700568 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000570static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000571 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct vif_device *v;
574 struct net_device *dev;
575 struct in_device *in_dev;
576
Patrick McHardy0c122952010-04-13 05:03:22 +0000577 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -EADDRNOTAVAIL;
579
Patrick McHardy0c122952010-04-13 05:03:22 +0000580 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 write_lock_bh(&mrt_lock);
583 dev = v->dev;
584 v->dev = NULL;
585
586 if (!dev) {
587 write_unlock_bh(&mrt_lock);
588 return -EADDRNOTAVAIL;
589 }
590
Patrick McHardy0c122952010-04-13 05:03:22 +0000591 if (vifi == mrt->mroute_reg_vif_num)
592 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000594 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000596
597 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000598 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 break;
600 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000601 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
604 write_unlock_bh(&mrt_lock);
605
606 dev_set_allmulti(dev, -1);
607
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000608 in_dev = __in_dev_get_rtnl(dev);
609 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700610 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000611 inet_netconf_notify_devconf(dev_net(dev),
612 NETCONFA_MC_FORWARDING,
613 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 ip_rt_multicast_event(in_dev);
615 }
616
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000617 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000618 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 dev_put(dev);
621 return 0;
622}
623
Eric Dumazeta8c94862010-10-01 16:15:08 +0000624static void ipmr_cache_free_rcu(struct rcu_head *head)
625{
626 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
627
628 kmem_cache_free(mrt_cachep, c);
629}
630
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000631static inline void ipmr_cache_free(struct mfc_cache *c)
632{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000633 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000637 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000639static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200641 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700643 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Patrick McHardy0c122952010-04-13 05:03:22 +0000645 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jianjun Kongc354e122008-11-03 00:28:02 -0800647 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700648 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
650 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000651 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000653 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700654 e->error = -ETIMEDOUT;
655 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700656
Eric W. Biederman15e47302012-09-07 20:12:54 +0000657 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000658 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000663 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Patrick McHardye258beb2010-04-13 05:03:19 +0000666/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000667static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Patrick McHardy0c122952010-04-13 05:03:22 +0000669 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned long now;
671 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000672 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000675 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return;
677 }
678
Patrick McHardy0c122952010-04-13 05:03:22 +0000679 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 goto out;
681
682 now = jiffies;
683 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Patrick McHardy0c122952010-04-13 05:03:22 +0000685 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (time_after(c->mfc_un.unres.expires, now)) {
687 unsigned long interval = c->mfc_un.unres.expires - now;
688 if (interval < expires)
689 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 continue;
691 }
692
Patrick McHardy862465f2010-04-13 05:03:21 +0000693 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000694 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000695 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Patrick McHardy0c122952010-04-13 05:03:22 +0000698 if (!list_empty(&mrt->mfc_unres_queue))
699 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701out:
702 spin_unlock(&mfc_unres_lock);
703}
704
705/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000706static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000707 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 int vifi;
710
711 cache->mfc_un.res.minvif = MAXVIFS;
712 cache->mfc_un.res.maxvif = 0;
713 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
714
Patrick McHardy0c122952010-04-13 05:03:22 +0000715 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
716 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000717 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
719 if (cache->mfc_un.res.minvif > vifi)
720 cache->mfc_un.res.minvif = vifi;
721 if (cache->mfc_un.res.maxvif <= vifi)
722 cache->mfc_un.res.maxvif = vifi + 1;
723 }
724 }
725}
726
Patrick McHardy0c122952010-04-13 05:03:22 +0000727static int vif_add(struct net *net, struct mr_table *mrt,
728 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000731 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct net_device *dev;
733 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700734 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000737 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -EADDRINUSE;
739
740 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100742 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100743 return -EINVAL;
744 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 * All the packets will be sent to the daemon
746 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000747 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000749 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!dev)
751 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700752 err = dev_set_allmulti(dev, 1);
753 if (err) {
754 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700755 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700756 return err;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900759 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000760 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!dev)
762 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700763 err = dev_set_allmulti(dev, 1);
764 if (err) {
765 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700766 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700767 return err;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000770 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000772 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
773 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100774 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000775 dev_put(dev);
776 return -EADDRNOTAVAIL;
777 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000778 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000779 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (!dev)
782 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700783 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700784 if (err) {
785 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700786 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
789 default:
790 return -EINVAL;
791 }
792
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000793 in_dev = __in_dev_get_rtnl(dev);
794 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000795 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000797 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700798 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000799 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
800 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ip_rt_multicast_event(in_dev);
802
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000803 /* Fill in the VIF structures */
804
Jianjun Kongc354e122008-11-03 00:28:02 -0800805 v->rate_limit = vifc->vifc_rate_limit;
806 v->local = vifc->vifc_lcl_addr.s_addr;
807 v->remote = vifc->vifc_rmt_addr.s_addr;
808 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (!mrtsock)
810 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800811 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 v->bytes_in = 0;
813 v->bytes_out = 0;
814 v->pkt_in = 0;
815 v->pkt_out = 0;
816 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000817 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200818 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* And finish update writing critical data */
821 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800822 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000823 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000824 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000825 if (vifi+1 > mrt->maxvif)
826 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 write_unlock_bh(&mrt_lock);
828 return 0;
829}
830
Eric Dumazeta8c94862010-10-01 16:15:08 +0000831/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000832static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000833 __be32 origin,
834 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Jianjun Kongc354e122008-11-03 00:28:02 -0800836 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 struct mfc_cache *c;
838
Eric Dumazeta8c94862010-10-01 16:15:08 +0000839 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000840 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
841 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000843 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000846/* Look for a (*,*,oif) entry */
847static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
848 int vifi)
849{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100850 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000851 struct mfc_cache *c;
852
853 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100854 if (c->mfc_origin == htonl(INADDR_ANY) &&
855 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000856 c->mfc_un.res.ttls[vifi] < 255)
857 return c;
858
859 return NULL;
860}
861
862/* Look for a (*,G) entry */
863static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
864 __be32 mcastgrp, int vifi)
865{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100866 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000867 struct mfc_cache *c, *proxy;
868
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100869 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000870 goto skip;
871
872 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100873 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000874 c->mfc_mcastgrp == mcastgrp) {
875 if (c->mfc_un.res.ttls[vifi] < 255)
876 return c;
877
878 /* It's ok if the vifi is part of the static tree */
879 proxy = ipmr_cache_find_any_parent(mrt,
880 c->mfc_parent);
881 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
882 return c;
883 }
884
885skip:
886 return ipmr_cache_find_any_parent(mrt, vifi);
887}
888
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100889/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000890static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Jianjun Kongc354e122008-11-03 00:28:02 -0800892 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000893
894 if (c)
895 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return c;
897}
898
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000899static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Jianjun Kongc354e122008-11-03 00:28:02 -0800901 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000902
903 if (c) {
904 skb_queue_head_init(&c->mfc_un.unres.unresolved);
905 c->mfc_un.unres.expires = jiffies + 10*HZ;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return c;
908}
909
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100910/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000911static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
912 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700915 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000917 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800918 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700919 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
921
Hong zhi guo573ce262013-03-27 06:47:04 +0000922 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000923 nlh->nlmsg_len = skb_tail_pointer(skb) -
924 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 } else {
926 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000927 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000929 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700930 e->error = -EMSGSIZE;
931 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Thomas Graf2942e902006-08-15 00:30:25 -0700933
Eric W. Biederman15e47302012-09-07 20:12:54 +0000934 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000935 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000936 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939}
940
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100941/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
942 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100944 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000946static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000947 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300949 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100950 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct igmphdr *igmp;
952 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100953 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int ret;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (assert == IGMPMSG_WHOLEPKT)
957 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
958 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 skb = alloc_skb(128, GFP_ATOMIC);
960
Stephen Hemminger132adf52007-03-08 20:44:43 -0800961 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return -ENOBUFS;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (assert == IGMPMSG_WHOLEPKT) {
965 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000966 * Duplicate old header, fix ihl, length etc.
967 * And all this only to mangle msg->im_msgtype and
968 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300970 skb_push(skb, sizeof(struct iphdr));
971 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300972 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300973 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700974 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 msg->im_msgtype = IGMPMSG_WHOLEPKT;
976 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000977 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700978 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
979 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
980 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100981 } else {
982 /* Copy the IP header */
983 skb_set_network_header(skb, skb->len);
984 skb_put(skb, ihl);
985 skb_copy_to_linear_data(skb, pkt->data, ihl);
986 /* Flag to the kernel this is a route add */
987 ip_hdr(skb)->protocol = 0;
988 msg = (struct igmpmsg *)skb_network_header(skb);
989 msg->im_vif = vifi;
990 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
991 /* Add our header */
992 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
993 igmp->type = assert;
994 msg->im_msgtype = assert;
995 igmp->code = 0;
996 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
997 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Eric Dumazet4c968702010-10-01 16:15:01 +00001000 rcu_read_lock();
1001 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001002 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001003 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 kfree_skb(skb);
1005 return -EINVAL;
1006 }
1007
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001008 /* Deliver to mrouted */
Eric Dumazet4c968702010-10-01 16:15:01 +00001009 ret = sock_queue_rcv_skb(mroute_sk, skb);
1010 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001011 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001012 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 kfree_skb(skb);
1014 }
1015
1016 return ret;
1017}
1018
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001019/* Queue a packet for resolution. It gets locked cache entry! */
1020static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1021 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Patrick McHardy862465f2010-04-13 05:03:21 +00001023 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int err;
1025 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001026 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001029 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001030 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001031 c->mfc_origin == iph->saddr) {
1032 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
Patrick McHardy862465f2010-04-13 05:03:21 +00001037 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001038 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001039 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001040 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 spin_unlock_bh(&mfc_unres_lock);
1042
1043 kfree_skb(skb);
1044 return -ENOBUFS;
1045 }
1046
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001047 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001048 c->mfc_parent = -1;
1049 c->mfc_origin = iph->saddr;
1050 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001052 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001053 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001054 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001055 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 out - Brad Parker
1057 */
1058 spin_unlock_bh(&mfc_unres_lock);
1059
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001060 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 kfree_skb(skb);
1062 return err;
1063 }
1064
Patrick McHardy0c122952010-04-13 05:03:22 +00001065 atomic_inc(&mrt->cache_resolve_queue_len);
1066 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001067 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
David S. Miller278554b2010-05-12 00:05:35 -07001069 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1070 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001073 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001074 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 kfree_skb(skb);
1076 err = -ENOBUFS;
1077 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001078 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 err = 0;
1080 }
1081
1082 spin_unlock_bh(&mfc_unres_lock);
1083 return err;
1084}
1085
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001086/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001088static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001091 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Jianjun Kongc354e122008-11-03 00:28:02 -08001093 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Patrick McHardy0c122952010-04-13 05:03:22 +00001095 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001097 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1098 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001099 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001100 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001101 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return 0;
1103 }
1104 }
1105 return -ENOENT;
1106}
1107
Patrick McHardy0c122952010-04-13 05:03:22 +00001108static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001109 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Patrick McHardy862465f2010-04-13 05:03:21 +00001111 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001113 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Patrick McHardya50436f22010-03-17 06:04:14 +00001115 if (mfc->mfcc_parent >= MAXVIFS)
1116 return -ENFILE;
1117
Jianjun Kongc354e122008-11-03 00:28:02 -08001118 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Patrick McHardy0c122952010-04-13 05:03:22 +00001120 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001122 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1123 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001124 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
Patrick McHardy862465f2010-04-13 05:03:21 +00001129 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 write_lock_bh(&mrt_lock);
1131 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001132 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (!mrtsock)
1134 c->mfc_flags |= MFC_STATIC;
1135 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001136 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return 0;
1138 }
1139
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001140 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001141 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return -EINVAL;
1143
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001144 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001145 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return -ENOMEM;
1147
Jianjun Kongc354e122008-11-03 00:28:02 -08001148 c->mfc_origin = mfc->mfcc_origin.s_addr;
1149 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1150 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001151 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (!mrtsock)
1153 c->mfc_flags |= MFC_STATIC;
1154
Eric Dumazeta8c94862010-10-01 16:15:08 +00001155 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001157 /* Check to see if we resolved a queued list. If so we
1158 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001160 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001162 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001163 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001165 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001166 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001167 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 break;
1169 }
1170 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001171 if (list_empty(&mrt->mfc_unres_queue))
1172 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 spin_unlock_bh(&mfc_unres_lock);
1174
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001175 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001176 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001177 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001179 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return 0;
1181}
1182
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001183/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001184static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001187 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001188 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001189
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001190 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001191 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001192 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1193 continue;
1194 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001196 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001198 /* Wipe the cache */
Patrick McHardy862465f2010-04-13 05:03:21 +00001199 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001200 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001201 if (!all && (c->mfc_flags & MFC_STATIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001203 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001204 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001205 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207 }
1208
Patrick McHardy0c122952010-04-13 05:03:22 +00001209 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001211 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001212 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001213 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001214 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216 spin_unlock_bh(&mfc_unres_lock);
1217 }
1218}
1219
Eric Dumazet4c968702010-10-01 16:15:01 +00001220/* called from ip_ra_control(), before an RCU grace period,
1221 * we dont need to call synchronize_rcu() here
1222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223static void mrtsock_destruct(struct sock *sk)
1224{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001225 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001226 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001229 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001230 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001231 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001232 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1233 NETCONFA_IFINDEX_ALL,
1234 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001235 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001236 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239 rtnl_unlock();
1240}
1241
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001242/* Socket options and virtual interface manipulation. The whole
1243 * virtual interface system is a complete heap, but unfortunately
1244 * that's how BSD mrouted happens to think. Maybe one day with a proper
1245 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001247
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001248int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1249 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001251 struct net *net = sock_net(sk);
1252 int val, ret = 0, parent = 0;
1253 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 struct vifctl vif;
1255 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001256 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001257
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001258 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1259 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001260 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001261 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1262 ret = -EOPNOTSUPP;
1263 goto out_unlock;
1264 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001265
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001266 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001267 if (!mrt) {
1268 ret = -ENOENT;
1269 goto out_unlock;
1270 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001271 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001272 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001273 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1274 ret = -EACCES;
1275 goto out_unlock;
1276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Stephen Hemminger132adf52007-03-08 20:44:43 -08001279 switch (optname) {
1280 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001281 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001282 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001283 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001284 }
1285 if (rtnl_dereference(mrt->mroute_sk)) {
1286 ret = -EADDRINUSE;
1287 break;
1288 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289
1290 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1291 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001292 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001293 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001294 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1295 NETCONFA_IFINDEX_ALL,
1296 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001298 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001299 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001300 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1301 ret = -EACCES;
1302 } else {
1303 /* We need to unlock here because mrtsock_destruct takes
1304 * care of rtnl itself and we can't change that due to
1305 * the IP_ROUTER_ALERT setsockopt which runs without it.
1306 */
1307 rtnl_unlock();
1308 ret = ip_ra_control(sk, 0, NULL);
1309 goto out;
1310 }
1311 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001312 case MRT_ADD_VIF:
1313 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001314 if (optlen != sizeof(vif)) {
1315 ret = -EINVAL;
1316 break;
1317 }
1318 if (copy_from_user(&vif, optval, sizeof(vif))) {
1319 ret = -EFAULT;
1320 break;
1321 }
1322 if (vif.vifc_vifi >= MAXVIFS) {
1323 ret = -ENFILE;
1324 break;
1325 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001326 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001327 ret = vif_add(net, mrt, &vif,
1328 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001329 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001330 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001331 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001332 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001333 /* Manipulate the forwarding caches. These live
1334 * in a sort of kernel/user symbiosis.
1335 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001336 case MRT_ADD_MFC:
1337 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001338 parent = -1;
1339 case MRT_ADD_MFC_PROXY:
1340 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001341 if (optlen != sizeof(mfc)) {
1342 ret = -EINVAL;
1343 break;
1344 }
1345 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1346 ret = -EFAULT;
1347 break;
1348 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001349 if (parent == 0)
1350 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001351 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1352 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001353 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001354 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001355 sk == rtnl_dereference(mrt->mroute_sk),
1356 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001357 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001358 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001359 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001360 if (optlen != sizeof(val)) {
1361 ret = -EINVAL;
1362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001364 if (get_user(val, (int __user *)optval)) {
1365 ret = -EFAULT;
1366 break;
1367 }
1368 mrt->mroute_do_assert = val;
1369 break;
1370 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001371 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001372 ret = -ENOPROTOOPT;
1373 break;
1374 }
1375 if (optlen != sizeof(val)) {
1376 ret = -EINVAL;
1377 break;
1378 }
1379 if (get_user(val, (int __user *)optval)) {
1380 ret = -EFAULT;
1381 break;
1382 }
1383
1384 val = !!val;
1385 if (val != mrt->mroute_do_pim) {
1386 mrt->mroute_do_pim = val;
1387 mrt->mroute_do_assert = val;
1388 }
1389 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001390 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001391 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1392 ret = -ENOPROTOOPT;
1393 break;
1394 }
1395 if (optlen != sizeof(uval)) {
1396 ret = -EINVAL;
1397 break;
1398 }
1399 if (get_user(uval, (u32 __user *)optval)) {
1400 ret = -EFAULT;
1401 break;
1402 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001403
Eric Dumazet4c968702010-10-01 16:15:01 +00001404 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1405 ret = -EBUSY;
1406 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001407 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001408 if (IS_ERR(mrt))
1409 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001410 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001411 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c968702010-10-01 16:15:01 +00001412 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001413 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001414 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001415 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001416 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001418out_unlock:
1419 rtnl_unlock();
1420out:
1421 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001424/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001425int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 int olr;
1428 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001429 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001430 struct mr_table *mrt;
1431
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001432 if (sk->sk_type != SOCK_RAW ||
1433 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1434 return -EOPNOTSUPP;
1435
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001436 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001437 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001438 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001440 switch (optname) {
1441 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001442 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001443 break;
1444 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001445 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001446 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001447 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001448 break;
1449 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001450 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001451 break;
1452 default:
1453 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001454 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001455
1456 if (get_user(olr, optlen))
1457 return -EFAULT;
1458 olr = min_t(unsigned int, olr, sizeof(int));
1459 if (olr < 0)
1460 return -EINVAL;
1461 if (put_user(olr, optlen))
1462 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001463 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EFAULT;
1465 return 0;
1466}
1467
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001468/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1470{
1471 struct sioc_sg_req sr;
1472 struct sioc_vif_req vr;
1473 struct vif_device *vif;
1474 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001475 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001476 struct mr_table *mrt;
1477
1478 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001479 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001480 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001481
Stephen Hemminger132adf52007-03-08 20:44:43 -08001482 switch (cmd) {
1483 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001484 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001485 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001486 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001487 return -EINVAL;
1488 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001489 vif = &mrt->vif_table[vr.vifi];
1490 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001491 vr.icount = vif->pkt_in;
1492 vr.ocount = vif->pkt_out;
1493 vr.ibytes = vif->bytes_in;
1494 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001496
Jianjun Kongc354e122008-11-03 00:28:02 -08001497 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001499 return 0;
1500 }
1501 read_unlock(&mrt_lock);
1502 return -EADDRNOTAVAIL;
1503 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001504 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001505 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Eric Dumazeta8c94862010-10-01 16:15:08 +00001507 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001508 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001509 if (c) {
1510 sr.pktcnt = c->mfc_un.res.pkt;
1511 sr.bytecnt = c->mfc_un.res.bytes;
1512 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001513 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001514
Jianjun Kongc354e122008-11-03 00:28:02 -08001515 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001516 return -EFAULT;
1517 return 0;
1518 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001519 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520 return -EADDRNOTAVAIL;
1521 default:
1522 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524}
1525
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001526#ifdef CONFIG_COMPAT
1527struct compat_sioc_sg_req {
1528 struct in_addr src;
1529 struct in_addr grp;
1530 compat_ulong_t pktcnt;
1531 compat_ulong_t bytecnt;
1532 compat_ulong_t wrong_if;
1533};
1534
David S. Millerca6b8bb02011-02-03 17:24:28 -08001535struct compat_sioc_vif_req {
1536 vifi_t vifi; /* Which iface */
1537 compat_ulong_t icount;
1538 compat_ulong_t ocount;
1539 compat_ulong_t ibytes;
1540 compat_ulong_t obytes;
1541};
1542
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001543int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1544{
David S. Miller0033d5a2011-02-03 17:21:31 -08001545 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001546 struct compat_sioc_vif_req vr;
1547 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001548 struct mfc_cache *c;
1549 struct net *net = sock_net(sk);
1550 struct mr_table *mrt;
1551
1552 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001553 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001554 return -ENOENT;
1555
1556 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001557 case SIOCGETVIFCNT:
1558 if (copy_from_user(&vr, arg, sizeof(vr)))
1559 return -EFAULT;
1560 if (vr.vifi >= mrt->maxvif)
1561 return -EINVAL;
1562 read_lock(&mrt_lock);
1563 vif = &mrt->vif_table[vr.vifi];
1564 if (VIF_EXISTS(mrt, vr.vifi)) {
1565 vr.icount = vif->pkt_in;
1566 vr.ocount = vif->pkt_out;
1567 vr.ibytes = vif->bytes_in;
1568 vr.obytes = vif->bytes_out;
1569 read_unlock(&mrt_lock);
1570
1571 if (copy_to_user(arg, &vr, sizeof(vr)))
1572 return -EFAULT;
1573 return 0;
1574 }
1575 read_unlock(&mrt_lock);
1576 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001577 case SIOCGETSGCNT:
1578 if (copy_from_user(&sr, arg, sizeof(sr)))
1579 return -EFAULT;
1580
1581 rcu_read_lock();
1582 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1583 if (c) {
1584 sr.pktcnt = c->mfc_un.res.pkt;
1585 sr.bytecnt = c->mfc_un.res.bytes;
1586 sr.wrong_if = c->mfc_un.res.wrong_if;
1587 rcu_read_unlock();
1588
1589 if (copy_to_user(arg, &sr, sizeof(sr)))
1590 return -EFAULT;
1591 return 0;
1592 }
1593 rcu_read_unlock();
1594 return -EADDRNOTAVAIL;
1595 default:
1596 return -ENOIOCTLCMD;
1597 }
1598}
1599#endif
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1602{
Jiri Pirko351638e2013-05-28 01:30:21 +00001603 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001604 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001605 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct vif_device *v;
1607 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (event != NETDEV_UNREGISTER)
1610 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001611
1612 ipmr_for_each_table(mrt, net) {
1613 v = &mrt->vif_table[0];
1614 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1615 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001616 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619 return NOTIFY_DONE;
1620}
1621
Jianjun Kongc354e122008-11-03 00:28:02 -08001622static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 .notifier_call = ipmr_device_event,
1624};
1625
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001626/* Encapsulate a packet by attaching a valid IPIP header to it.
1627 * This avoids tunnel drivers and other mess and gives us the speed so
1628 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001630static void ip_encap(struct net *net, struct sk_buff *skb,
1631 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001633 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001634 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001635
1636 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001637 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001638 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001639 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001641 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001642 iph->tos = old_iph->tos;
1643 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 iph->frag_off = 0;
1645 iph->daddr = daddr;
1646 iph->saddr = saddr;
1647 iph->protocol = IPPROTO_IPIP;
1648 iph->ihl = 5;
1649 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001650 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 ip_send_check(iph);
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1654 nf_reset(skb);
1655}
1656
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001657static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1658 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001660 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
David S. Miller73186df2015-11-03 13:41:45 -05001662 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1663 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 if (unlikely(opt->optlen))
1666 ip_forward_options(skb);
1667
Eric W. Biederman13206b62015-10-07 16:48:35 -05001668 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001671/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Patrick McHardy0c122952010-04-13 05:03:22 +00001673static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1674 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001676 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001677 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 struct net_device *dev;
1679 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001680 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 int encap = 0;
1682
Ian Morris51456b22015-04-03 09:17:26 +01001683 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 goto out_free;
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (vif->flags & VIFF_REGISTER) {
1687 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001688 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001689 vif->dev->stats.tx_bytes += skb->len;
1690 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001691 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001692 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001695 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001696 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001697 vif->remote, vif->local,
1698 0, 0,
1699 IPPROTO_IPIP,
1700 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001701 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 goto out_free;
1703 encap = sizeof(struct iphdr);
1704 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001705 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001706 0, 0,
1707 IPPROTO_IPIP,
1708 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001709 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 goto out_free;
1711 }
1712
Changli Gaod8d1f302010-06-10 23:31:35 -07001713 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Changli Gaod8d1f302010-06-10 23:31:35 -07001715 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001717 * allow to send ICMP, so that packets will disappear
1718 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 */
David S. Miller73186df2015-11-03 13:41:45 -05001720 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 ip_rt_put(rt);
1722 goto out_free;
1723 }
1724
Changli Gaod8d1f302010-06-10 23:31:35 -07001725 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001728 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto out_free;
1730 }
1731
1732 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001733 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Eric Dumazetadf30902009-06-02 05:19:30 +00001735 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001736 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001737 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001740 * What do we do with netfilter? -- RR
1741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001743 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001745 vif->dev->stats.tx_packets++;
1746 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748
1749 IPCB(skb)->flags |= IPSKB_FORWARDED;
1750
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001751 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 * not only before forwarding, but after forwarding on all output
1753 * interfaces. It is clear, if mrouter runs a multicasting
1754 * program, it should receive packets not depending to what interface
1755 * program is joined.
1756 * If we will not make it, the program will have to join on all
1757 * interfaces. On the other hand, multihoming host (or router, but
1758 * not mrouter) cannot join to more than one interface - it will
1759 * result in receiving multiple packets.
1760 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001761 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1762 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 ipmr_forward_finish);
1764 return;
1765
1766out_free:
1767 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Patrick McHardy0c122952010-04-13 05:03:22 +00001770static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
1772 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001773
1774 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1775 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
1777 }
1778 return ct;
1779}
1780
1781/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001782static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1783 struct sk_buff *skb, struct mfc_cache *cache,
1784 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 int psend = -1;
1787 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001788 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 vif = cache->mfc_parent;
1791 cache->mfc_un.res.pkt++;
1792 cache->mfc_un.res.bytes += skb->len;
1793
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001794 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001795 struct mfc_cache *cache_proxy;
1796
1797 /* For an (*,G) entry, we only check that the incomming
1798 * interface is part of the static tree.
1799 */
1800 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1801 if (cache_proxy &&
1802 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1803 goto forward;
1804 }
1805
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001806 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001807 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001808 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001810 * Very complicated situation...
1811 *
1812 * The best workaround until routing daemons will be
1813 * fixed is not to redistribute packet, if it was
1814 * send through wrong interface. It means, that
1815 * multicast applications WILL NOT work for
1816 * (S,G), which have default multicast route pointing
1817 * to wrong oif. In any case, it is not a good
1818 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 */
1820 goto dont_forward;
1821 }
1822
1823 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Patrick McHardy0c122952010-04-13 05:03:22 +00001825 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001827 * so that we cannot check that packet arrived on an oif.
1828 * It is bad, but otherwise we would need to move pretty
1829 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001831 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001832 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001833 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1835 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001836 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838 goto dont_forward;
1839 }
1840
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001841forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001842 mrt->vif_table[vif].pkt_in++;
1843 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001845 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001846 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1847 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001848 if (true_vifi >= 0 &&
1849 true_vifi != cache->mfc_parent &&
1850 ip_hdr(skb)->ttl >
1851 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1852 /* It's an (*,*) entry and the packet is not coming from
1853 * the upstream: forward the packet to the upstream
1854 * only.
1855 */
1856 psend = cache->mfc_parent;
1857 goto last_forward;
1858 }
1859 goto dont_forward;
1860 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001861 for (ct = cache->mfc_un.res.maxvif - 1;
1862 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001863 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001864 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1865 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001866 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (psend != -1) {
1868 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001871 ipmr_queue_xmit(net, mrt, skb2, cache,
1872 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001874 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
1876 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001877last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (psend != -1) {
1879 if (local) {
1880 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001883 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001885 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001886 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 }
1888 }
1889
1890dont_forward:
1891 if (!local)
1892 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
David S. Miller417da662011-05-03 19:42:43 -07001895static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001896{
David S. Miller417da662011-05-03 19:42:43 -07001897 struct rtable *rt = skb_rtable(skb);
1898 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001899 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001900 .daddr = iph->daddr,
1901 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001902 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001903 .flowi4_oif = (rt_is_output_route(rt) ?
1904 skb->dev->ifindex : 0),
1905 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001906 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001907 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001908 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001909 };
1910 struct mr_table *mrt;
1911 int err;
1912
David S. Millerda919812011-03-12 02:04:50 -05001913 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001914 if (err)
1915 return ERR_PTR(err);
1916 return mrt;
1917}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001919/* Multicast packets for forwarding arrive here
1920 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922int ip_mr_input(struct sk_buff *skb)
1923{
1924 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001925 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001926 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001927 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001930 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001932 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 goto dont_forward;
1934
David S. Miller417da662011-05-03 19:42:43 -07001935 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001936 if (IS_ERR(mrt)) {
1937 kfree_skb(skb);
1938 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001941 if (IPCB(skb)->opt.router_alert) {
1942 if (ip_call_ra_chain(skb))
1943 return 0;
1944 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1945 /* IGMPv1 (and broken IGMPv2 implementations sort of
1946 * Cisco IOS <= 11.2(8)) do not put router alert
1947 * option to IGMP packets destined to routable
1948 * groups. It is very bad, because it means
1949 * that we can forward NO IGMP messages.
1950 */
1951 struct sock *mroute_sk;
1952
1953 mroute_sk = rcu_dereference(mrt->mroute_sk);
1954 if (mroute_sk) {
1955 nf_reset(skb);
1956 raw_rcv(mroute_sk, skb);
1957 return 0;
1958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960 }
1961
Eric Dumazeta8c94862010-10-01 16:15:08 +00001962 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001963 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001964 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001965 int vif = ipmr_find_vif(mrt, skb->dev);
1966
1967 if (vif >= 0)
1968 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1969 vif);
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001972 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01001973 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 int vif;
1975
1976 if (local) {
1977 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1978 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01001979 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 skb = skb2;
1982 }
1983
Eric Dumazeta8c94862010-10-01 16:15:08 +00001984 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001985 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001987 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 read_unlock(&mrt_lock);
1989
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001990 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
1992 read_unlock(&mrt_lock);
1993 kfree_skb(skb);
1994 return -ENODEV;
1995 }
1996
Eric Dumazeta8c94862010-10-01 16:15:08 +00001997 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001998 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 read_unlock(&mrt_lock);
2000
2001 if (local)
2002 return ip_local_deliver(skb);
2003
2004 return 0;
2005
2006dont_forward:
2007 if (local)
2008 return ip_local_deliver(skb);
2009 kfree_skb(skb);
2010 return 0;
2011}
2012
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002013#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002014/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002015int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002016{
2017 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002018 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002019 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002020
2021 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2022 goto drop;
2023
2024 pim = igmp_hdr(skb);
2025
David S. Miller417da662011-05-03 19:42:43 -07002026 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002027 if (IS_ERR(mrt))
2028 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002029 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002030 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2031 goto drop;
2032
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002033 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002034drop:
2035 kfree_skb(skb);
2036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return 0;
2038}
2039#endif
2040
2041#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002042static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002045 struct net *net = dev_net(skb->dev);
2046 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002048 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 goto drop;
2050
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002051 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002052 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2053 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002054 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002055 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 goto drop;
2057
David S. Miller417da662011-05-03 19:42:43 -07002058 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002059 if (IS_ERR(mrt))
2060 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002061 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002062drop:
2063 kfree_skb(skb);
2064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return 0;
2066}
2067#endif
2068
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002069static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2070 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
2072 int ct;
2073 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002074 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002075 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Nicolas Dichtel74381892010-03-25 23:45:35 +00002077 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002078 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002079 return -ENOENT;
2080
Thomas Graf92a395e2012-06-26 23:36:13 +00002081 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2082 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2083 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Thomas Graf92a395e2012-06-26 23:36:13 +00002085 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2086 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002089 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002090 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2091 nla_nest_cancel(skb, mp_attr);
2092 return -EMSGSIZE;
2093 }
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 nhp->rtnh_flags = 0;
2096 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002097 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 nhp->rtnh_len = sizeof(*nhp);
2099 }
2100 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002101
2102 nla_nest_end(skb, mp_attr);
2103
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002104 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2105 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2106 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2107 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2108 return -EMSGSIZE;
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 rtm->rtm_type = RTN_MULTICAST;
2111 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
David S. Miller9a1b9492011-05-04 12:18:54 -07002114int ipmr_get_route(struct net *net, struct sk_buff *skb,
2115 __be32 saddr, __be32 daddr,
2116 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002119 struct mr_table *mrt;
2120 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002122 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002123 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002124 return -ENOENT;
2125
Eric Dumazeta8c94862010-10-01 16:15:08 +00002126 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002127 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002128 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002129 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002131 if (vif >= 0)
2132 cache = ipmr_cache_find_any(mrt, daddr, vif);
2133 }
Ian Morris51456b22015-04-03 09:17:26 +01002134 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002135 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002136 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002138 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002141 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return -EAGAIN;
2143 }
2144
2145 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002146 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002147 if (dev)
2148 vif = ipmr_find_vif(mrt, dev);
2149 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002151 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return -ENODEV;
2153 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002154 skb2 = skb_clone(skb, GFP_ATOMIC);
2155 if (!skb2) {
2156 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002157 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002158 return -ENOMEM;
2159 }
2160
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002161 skb_push(skb2, sizeof(struct iphdr));
2162 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002163 iph = ip_hdr(skb2);
2164 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002165 iph->saddr = saddr;
2166 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002167 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002168 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002170 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return err;
2172 }
2173
Eric Dumazeta8c94862010-10-01 16:15:08 +00002174 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002175 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002177 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return err;
2179}
2180
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002181static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002182 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2183 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002184{
2185 struct nlmsghdr *nlh;
2186 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002187 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002188
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002189 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002190 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002191 return -EMSGSIZE;
2192
2193 rtm = nlmsg_data(nlh);
2194 rtm->rtm_family = RTNL_FAMILY_IPMR;
2195 rtm->rtm_dst_len = 32;
2196 rtm->rtm_src_len = 32;
2197 rtm->rtm_tos = 0;
2198 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002199 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2200 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002201 rtm->rtm_type = RTN_MULTICAST;
2202 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002203 if (c->mfc_flags & MFC_STATIC)
2204 rtm->rtm_protocol = RTPROT_STATIC;
2205 else
2206 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002207 rtm->rtm_flags = 0;
2208
Jiri Benc930345e2015-03-29 16:59:25 +02002209 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2210 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002211 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002212 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2213 /* do not break the dump if cache is unresolved */
2214 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002215 goto nla_put_failure;
2216
Johannes Berg053c0952015-01-16 22:09:00 +01002217 nlmsg_end(skb, nlh);
2218 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002219
2220nla_put_failure:
2221 nlmsg_cancel(skb, nlh);
2222 return -EMSGSIZE;
2223}
2224
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002225static size_t mroute_msgsize(bool unresolved, int maxvif)
2226{
2227 size_t len =
2228 NLMSG_ALIGN(sizeof(struct rtmsg))
2229 + nla_total_size(4) /* RTA_TABLE */
2230 + nla_total_size(4) /* RTA_SRC */
2231 + nla_total_size(4) /* RTA_DST */
2232 ;
2233
2234 if (!unresolved)
2235 len = len
2236 + nla_total_size(4) /* RTA_IIF */
2237 + nla_total_size(0) /* RTA_MULTIPATH */
2238 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2239 /* RTA_MFC_STATS */
2240 + nla_total_size(sizeof(struct rta_mfc_stats))
2241 ;
2242
2243 return len;
2244}
2245
2246static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2247 int cmd)
2248{
2249 struct net *net = read_pnet(&mrt->net);
2250 struct sk_buff *skb;
2251 int err = -ENOBUFS;
2252
2253 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2254 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002255 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002256 goto errout;
2257
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002258 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002259 if (err < 0)
2260 goto errout;
2261
2262 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2263 return;
2264
2265errout:
2266 kfree_skb(skb);
2267 if (err < 0)
2268 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2269}
2270
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002271static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2272{
2273 struct net *net = sock_net(skb->sk);
2274 struct mr_table *mrt;
2275 struct mfc_cache *mfc;
2276 unsigned int t = 0, s_t;
2277 unsigned int h = 0, s_h;
2278 unsigned int e = 0, s_e;
2279
2280 s_t = cb->args[0];
2281 s_h = cb->args[1];
2282 s_e = cb->args[2];
2283
Eric Dumazeta8c94862010-10-01 16:15:08 +00002284 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002285 ipmr_for_each_table(mrt, net) {
2286 if (t < s_t)
2287 goto next_table;
2288 if (t > s_t)
2289 s_h = 0;
2290 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002291 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002292 if (e < s_e)
2293 goto next_entry;
2294 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002295 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002296 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002297 mfc, RTM_NEWROUTE,
2298 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002299 goto done;
2300next_entry:
2301 e++;
2302 }
2303 e = s_e = 0;
2304 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002305 spin_lock_bh(&mfc_unres_lock);
2306 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2307 if (e < s_e)
2308 goto next_entry2;
2309 if (ipmr_fill_mroute(mrt, skb,
2310 NETLINK_CB(cb->skb).portid,
2311 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002312 mfc, RTM_NEWROUTE,
2313 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002314 spin_unlock_bh(&mfc_unres_lock);
2315 goto done;
2316 }
2317next_entry2:
2318 e++;
2319 }
2320 spin_unlock_bh(&mfc_unres_lock);
2321 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002322 s_h = 0;
2323next_table:
2324 t++;
2325 }
2326done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002327 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002328
2329 cb->args[2] = e;
2330 cb->args[1] = h;
2331 cb->args[0] = t;
2332
2333 return skb->len;
2334}
2335
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002336static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2337 [RTA_SRC] = { .type = NLA_U32 },
2338 [RTA_DST] = { .type = NLA_U32 },
2339 [RTA_IIF] = { .type = NLA_U32 },
2340 [RTA_TABLE] = { .type = NLA_U32 },
2341 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2342};
2343
2344static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2345{
2346 switch (rtm_protocol) {
2347 case RTPROT_STATIC:
2348 case RTPROT_MROUTED:
2349 return true;
2350 }
2351 return false;
2352}
2353
2354static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2355{
2356 struct rtnexthop *rtnh = nla_data(nla);
2357 int remaining = nla_len(nla), vifi = 0;
2358
2359 while (rtnh_ok(rtnh, remaining)) {
2360 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2361 if (++vifi == MAXVIFS)
2362 break;
2363 rtnh = rtnh_next(rtnh, &remaining);
2364 }
2365
2366 return remaining > 0 ? -EINVAL : vifi;
2367}
2368
2369/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2370static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2371 struct mfcctl *mfcc, int *mrtsock,
2372 struct mr_table **mrtret)
2373{
2374 struct net_device *dev = NULL;
2375 u32 tblid = RT_TABLE_DEFAULT;
2376 struct mr_table *mrt;
2377 struct nlattr *attr;
2378 struct rtmsg *rtm;
2379 int ret, rem;
2380
2381 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy);
2382 if (ret < 0)
2383 goto out;
2384 rtm = nlmsg_data(nlh);
2385
2386 ret = -EINVAL;
2387 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2388 rtm->rtm_type != RTN_MULTICAST ||
2389 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2390 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2391 goto out;
2392
2393 memset(mfcc, 0, sizeof(*mfcc));
2394 mfcc->mfcc_parent = -1;
2395 ret = 0;
2396 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2397 switch (nla_type(attr)) {
2398 case RTA_SRC:
2399 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2400 break;
2401 case RTA_DST:
2402 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2403 break;
2404 case RTA_IIF:
2405 dev = __dev_get_by_index(net, nla_get_u32(attr));
2406 if (!dev) {
2407 ret = -ENODEV;
2408 goto out;
2409 }
2410 break;
2411 case RTA_MULTIPATH:
2412 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2413 ret = -EINVAL;
2414 goto out;
2415 }
2416 break;
2417 case RTA_PREFSRC:
2418 ret = 1;
2419 break;
2420 case RTA_TABLE:
2421 tblid = nla_get_u32(attr);
2422 break;
2423 }
2424 }
2425 mrt = ipmr_get_table(net, tblid);
2426 if (!mrt) {
2427 ret = -ENOENT;
2428 goto out;
2429 }
2430 *mrtret = mrt;
2431 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2432 if (dev)
2433 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2434
2435out:
2436 return ret;
2437}
2438
2439/* takes care of both newroute and delroute */
2440static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh)
2441{
2442 struct net *net = sock_net(skb->sk);
2443 int ret, mrtsock, parent;
2444 struct mr_table *tbl;
2445 struct mfcctl mfcc;
2446
2447 mrtsock = 0;
2448 tbl = NULL;
2449 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl);
2450 if (ret < 0)
2451 return ret;
2452
2453 parent = ret ? mfcc.mfcc_parent : -1;
2454 if (nlh->nlmsg_type == RTM_NEWROUTE)
2455 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2456 else
2457 return ipmr_mfc_delete(tbl, &mfcc, parent);
2458}
2459
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002460#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002461/* The /proc interfaces to multicast routing :
2462 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 */
2464struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002465 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002466 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 int ct;
2468};
2469
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002470static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2471 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 loff_t pos)
2473{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002474 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002475
2476 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2477 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002479 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002480 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482 return NULL;
2483}
2484
2485static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002486 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002488 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002489 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002490 struct mr_table *mrt;
2491
2492 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002493 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002494 return ERR_PTR(-ENOENT);
2495
2496 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002499 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 : SEQ_START_TOKEN;
2501}
2502
2503static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2504{
2505 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002506 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002507 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 ++*pos;
2510 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002511 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002512
Patrick McHardy0c122952010-04-13 05:03:22 +00002513 while (++iter->ct < mrt->maxvif) {
2514 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002516 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
2518 return NULL;
2519}
2520
2521static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002522 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
2524 read_unlock(&mrt_lock);
2525}
2526
2527static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2528{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002529 struct ipmr_vif_iter *iter = seq->private;
2530 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002533 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2535 } else {
2536 const struct vif_device *vif = v;
2537 const char *name = vif->dev ? vif->dev->name : "none";
2538
2539 seq_printf(seq,
2540 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002541 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002542 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 vif->bytes_out, vif->pkt_out,
2544 vif->flags, vif->local, vif->remote);
2545 }
2546 return 0;
2547}
2548
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002549static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 .start = ipmr_vif_seq_start,
2551 .next = ipmr_vif_seq_next,
2552 .stop = ipmr_vif_seq_stop,
2553 .show = ipmr_vif_seq_show,
2554};
2555
2556static int ipmr_vif_open(struct inode *inode, struct file *file)
2557{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002558 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2559 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Arjan van de Ven9a321442007-02-12 00:55:35 -08002562static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 .owner = THIS_MODULE,
2564 .open = ipmr_vif_open,
2565 .read = seq_read,
2566 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002567 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568};
2569
2570struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002571 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002572 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002573 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 int ct;
2575};
2576
2577
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002578static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2579 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002581 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 struct mfc_cache *mfc;
2583
Eric Dumazeta8c94862010-10-01 16:15:08 +00002584 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002585 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002586 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002587 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002588 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002590 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002591 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002594 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002595 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002596 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return mfc;
2598 spin_unlock_bh(&mfc_unres_lock);
2599
2600 it->cache = NULL;
2601 return NULL;
2602}
2603
2604
2605static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2606{
2607 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002608 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002609 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002610
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002611 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002612 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002613 return ERR_PTR(-ENOENT);
2614
2615 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 it->cache = NULL;
2617 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002618 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 : SEQ_START_TOKEN;
2620}
2621
2622static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2623{
2624 struct mfc_cache *mfc = v;
2625 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002626 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002627 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 ++*pos;
2630
2631 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002632 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Patrick McHardy862465f2010-04-13 05:03:21 +00002634 if (mfc->list.next != it->cache)
2635 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002636
Patrick McHardy0c122952010-04-13 05:03:22 +00002637 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 goto end_of_list;
2639
Patrick McHardy0c122952010-04-13 05:03:22 +00002640 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002643 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002644 if (list_empty(it->cache))
2645 continue;
2646 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648
2649 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002650 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002651 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002655 if (!list_empty(it->cache))
2656 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002658end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 spin_unlock_bh(&mfc_unres_lock);
2660 it->cache = NULL;
2661
2662 return NULL;
2663}
2664
2665static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2666{
2667 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002668 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Patrick McHardy0c122952010-04-13 05:03:22 +00002670 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002672 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002673 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
2676static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2677{
2678 int n;
2679
2680 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002681 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2683 } else {
2684 const struct mfc_cache *mfc = v;
2685 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002686 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002687
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002688 seq_printf(seq, "%08X %08X %-3hd",
2689 (__force u32) mfc->mfc_mcastgrp,
2690 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002691 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Patrick McHardy0c122952010-04-13 05:03:22 +00002693 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002694 seq_printf(seq, " %8lu %8lu %8lu",
2695 mfc->mfc_un.res.pkt,
2696 mfc->mfc_un.res.bytes,
2697 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002698 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002699 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002700 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002701 mfc->mfc_un.res.ttls[n] < 255)
2702 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002703 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 n, mfc->mfc_un.res.ttls[n]);
2705 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002706 } else {
2707 /* unresolved mfc_caches don't contain
2708 * pkt, bytes and wrong_if values
2709 */
2710 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
2712 seq_putc(seq, '\n');
2713 }
2714 return 0;
2715}
2716
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002717static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 .start = ipmr_mfc_seq_start,
2719 .next = ipmr_mfc_seq_next,
2720 .stop = ipmr_mfc_seq_stop,
2721 .show = ipmr_mfc_seq_show,
2722};
2723
2724static int ipmr_mfc_open(struct inode *inode, struct file *file)
2725{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002726 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2727 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
Arjan van de Ven9a321442007-02-12 00:55:35 -08002730static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 .owner = THIS_MODULE,
2732 .open = ipmr_mfc_open,
2733 .read = seq_read,
2734 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002735 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002737#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002740static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002742 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743};
2744#endif
2745
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002746/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002747static int __net_init ipmr_net_init(struct net *net)
2748{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002749 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002750
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002751 err = ipmr_rules_init(net);
2752 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002753 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002754
2755#ifdef CONFIG_PROC_FS
2756 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002757 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002758 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002759 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002760 goto proc_cache_fail;
2761#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002762 return 0;
2763
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002764#ifdef CONFIG_PROC_FS
2765proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002766 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002767proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002768 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002769#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002770fail:
2771 return err;
2772}
2773
2774static void __net_exit ipmr_net_exit(struct net *net)
2775{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002776#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002777 remove_proc_entry("ip_mr_cache", net->proc_net);
2778 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002779#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002780 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002781}
2782
2783static struct pernet_operations ipmr_net_ops = {
2784 .init = ipmr_net_init,
2785 .exit = ipmr_net_exit,
2786};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002787
Wang Chen03d2f892008-07-03 12:13:36 +08002788int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
Wang Chen03d2f892008-07-03 12:13:36 +08002790 int err;
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2793 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002794 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002795 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002796
Benjamin Therycf958ae32009-01-22 04:56:16 +00002797 err = register_pernet_subsys(&ipmr_net_ops);
2798 if (err)
2799 goto reg_pernet_fail;
2800
Wang Chen03d2f892008-07-03 12:13:36 +08002801 err = register_netdevice_notifier(&ip_mr_notifier);
2802 if (err)
2803 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002804#ifdef CONFIG_IP_PIMSM_V2
2805 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002806 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002807 err = -EAGAIN;
2808 goto add_proto_fail;
2809 }
2810#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002811 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2812 NULL, ipmr_rtm_dumproute, NULL);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002813 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
2814 ipmr_rtm_route, NULL, NULL);
2815 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
2816 ipmr_rtm_route, NULL, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002817 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002818
Tom Goff403dbb92009-06-14 03:16:13 -07002819#ifdef CONFIG_IP_PIMSM_V2
2820add_proto_fail:
2821 unregister_netdevice_notifier(&ip_mr_notifier);
2822#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002823reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002824 unregister_pernet_subsys(&ipmr_net_ops);
2825reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002826 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002827 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}