blob: 322fdc6ac75b3fb354e5a3b48e663fa351e080dc [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Patrick McHardyf0ad0862010-04-13 05:03:23 +000070struct ipmr_rule {
71 struct fib_rule common;
72};
73
74struct ipmr_result {
75 struct mr_table *mrt;
76};
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000079 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81
82static DEFINE_RWLOCK(mrt_lock);
83
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010084/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* Special spinlock for queue of unresolved entries */
87static DEFINE_SPINLOCK(mfc_unres_lock);
88
89/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000090 * entries is changed only in process context and protected
91 * with weak lock mrt_lock. Queue of unresolved entries is protected
92 * with strong spinlock mfc_unres_lock.
93 *
94 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
96
Christoph Lametere18b8902006-12-06 20:33:20 -080097static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Patrick McHardyf0ad0862010-04-13 05:03:23 +000099static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000100static void ipmr_free_table(struct mr_table *mrt);
101
Rami Rosenc4854ec2013-07-20 15:09:28 +0300102static void ip_mr_forward(struct net *net, struct mr_table *mrt,
103 struct sk_buff *skb, struct mfc_cache *cache,
104 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000105static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000106 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200107static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
108 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000109static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
110 int cmd);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000111static void mroute_clean_tables(struct mr_table *mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000112static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000114#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
115#define ipmr_for_each_table(mrt, net) \
116 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
117
118static struct mr_table *ipmr_get_table(struct net *net, u32 id)
119{
120 struct mr_table *mrt;
121
122 ipmr_for_each_table(mrt, net) {
123 if (mrt->id == id)
124 return mrt;
125 }
126 return NULL;
127}
128
David S. Millerda919812011-03-12 02:04:50 -0500129static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000130 struct mr_table **mrt)
131{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000132 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100133 struct ipmr_result res;
134 struct fib_lookup_arg arg = {
135 .result = &res,
136 .flags = FIB_LOOKUP_NOREF,
137 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000138
David S. Millerda919812011-03-12 02:04:50 -0500139 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
140 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000141 if (err < 0)
142 return err;
143 *mrt = res.mrt;
144 return 0;
145}
146
147static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
148 int flags, struct fib_lookup_arg *arg)
149{
150 struct ipmr_result *res = arg->result;
151 struct mr_table *mrt;
152
153 switch (rule->action) {
154 case FR_ACT_TO_TBL:
155 break;
156 case FR_ACT_UNREACHABLE:
157 return -ENETUNREACH;
158 case FR_ACT_PROHIBIT:
159 return -EACCES;
160 case FR_ACT_BLACKHOLE:
161 default:
162 return -EINVAL;
163 }
164
165 mrt = ipmr_get_table(rule->fr_net, rule->table);
Ian Morris51456b22015-04-03 09:17:26 +0100166 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000167 return -EAGAIN;
168 res->mrt = mrt;
169 return 0;
170}
171
172static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
173{
174 return 1;
175}
176
177static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
178 FRA_GENERIC_POLICY,
179};
180
181static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
182 struct fib_rule_hdr *frh, struct nlattr **tb)
183{
184 return 0;
185}
186
187static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
188 struct nlattr **tb)
189{
190 return 1;
191}
192
193static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
194 struct fib_rule_hdr *frh)
195{
196 frh->dst_len = 0;
197 frh->src_len = 0;
198 frh->tos = 0;
199 return 0;
200}
201
Andi Kleen04a6f822012-10-04 17:12:11 -0700202static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200203 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000204 .rule_size = sizeof(struct ipmr_rule),
205 .addr_size = sizeof(u32),
206 .action = ipmr_rule_action,
207 .match = ipmr_rule_match,
208 .configure = ipmr_rule_configure,
209 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000210 .fill = ipmr_rule_fill,
211 .nlgroup = RTNLGRP_IPV4_RULE,
212 .policy = ipmr_rule_policy,
213 .owner = THIS_MODULE,
214};
215
216static int __net_init ipmr_rules_init(struct net *net)
217{
218 struct fib_rules_ops *ops;
219 struct mr_table *mrt;
220 int err;
221
222 ops = fib_rules_register(&ipmr_rules_ops_template, net);
223 if (IS_ERR(ops))
224 return PTR_ERR(ops);
225
226 INIT_LIST_HEAD(&net->ipv4.mr_tables);
227
228 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100229 if (IS_ERR(mrt)) {
230 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000231 goto err1;
232 }
233
234 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
235 if (err < 0)
236 goto err2;
237
238 net->ipv4.mr_rules_ops = ops;
239 return 0;
240
241err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700242 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000243err1:
244 fib_rules_unregister(ops);
245 return err;
246}
247
248static void __net_exit ipmr_rules_exit(struct net *net)
249{
250 struct mr_table *mrt, *next;
251
WANG Conged785302015-03-31 11:01:45 -0700252 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000253 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
254 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000255 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000256 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000257 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700258 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000259}
260#else
261#define ipmr_for_each_table(mrt, net) \
262 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
263
264static struct mr_table *ipmr_get_table(struct net *net, u32 id)
265{
266 return net->ipv4.mrt;
267}
268
David S. Millerda919812011-03-12 02:04:50 -0500269static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000270 struct mr_table **mrt)
271{
272 *mrt = net->ipv4.mrt;
273 return 0;
274}
275
276static int __net_init ipmr_rules_init(struct net *net)
277{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100278 struct mr_table *mrt;
279
280 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
281 if (IS_ERR(mrt))
282 return PTR_ERR(mrt);
283 net->ipv4.mrt = mrt;
284 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000285}
286
287static void __net_exit ipmr_rules_exit(struct net *net)
288{
WANG Conged785302015-03-31 11:01:45 -0700289 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000290 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700291 net->ipv4.mrt = NULL;
292 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000293}
294#endif
295
296static struct mr_table *ipmr_new_table(struct net *net, u32 id)
297{
298 struct mr_table *mrt;
299 unsigned int i;
300
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100301 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
302 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
303 return ERR_PTR(-EINVAL);
304
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000305 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100306 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000307 return mrt;
308
309 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100310 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100311 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200312 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000313 mrt->id = id;
314
315 /* Forwarding cache */
316 for (i = 0; i < MFC_LINES; i++)
317 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
318
319 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
320
321 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
322 (unsigned long)mrt);
323
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000324 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000325#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
326 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
327#endif
328 return mrt;
329}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000331static void ipmr_free_table(struct mr_table *mrt)
332{
333 del_timer_sync(&mrt->ipmr_expire_timer);
334 mroute_clean_tables(mrt);
335 kfree(mrt);
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
339
Wang Chend6070322008-07-14 20:55:26 -0700340static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
341{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000342 struct net *net = dev_net(dev);
343
Wang Chend6070322008-07-14 20:55:26 -0700344 dev_close(dev);
345
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000346 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700347 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800348 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700349 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700350 struct ip_tunnel_parm p;
351
352 memset(&p, 0, sizeof(p));
353 p.iph.daddr = v->vifc_rmt_addr.s_addr;
354 p.iph.saddr = v->vifc_lcl_addr.s_addr;
355 p.iph.version = 4;
356 p.iph.ihl = 5;
357 p.iph.protocol = IPPROTO_IPIP;
358 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
359 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
360
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800361 if (ops->ndo_do_ioctl) {
362 mm_segment_t oldfs = get_fs();
363
364 set_fs(KERNEL_DS);
365 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
366 set_fs(oldfs);
367 }
Wang Chend6070322008-07-14 20:55:26 -0700368 }
369}
370
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100371/* Initialize ipmr pimreg/tunnel in_device */
372static bool ipmr_init_vif_indev(const struct net_device *dev)
373{
374 struct in_device *in_dev;
375
376 ASSERT_RTNL();
377
378 in_dev = __in_dev_get_rtnl(dev);
379 if (!in_dev)
380 return false;
381 ipv4_devconf_setall(in_dev);
382 neigh_parms_data_state_setall(in_dev->arp_parms);
383 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
384
385 return true;
386}
387
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100388static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct net_device *dev;
391
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000392 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800395 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int err;
397 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 memset(&p, 0, sizeof(p));
401 p.iph.daddr = v->vifc_rmt_addr.s_addr;
402 p.iph.saddr = v->vifc_lcl_addr.s_addr;
403 p.iph.version = 4;
404 p.iph.ihl = 5;
405 p.iph.protocol = IPPROTO_IPIP;
406 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800407 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800409 if (ops->ndo_do_ioctl) {
410 mm_segment_t oldfs = get_fs();
411
412 set_fs(KERNEL_DS);
413 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
414 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000415 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800416 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 dev = NULL;
419
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000420 if (err == 0 &&
421 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100423 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (dev_open(dev))
426 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700427 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 }
430 return dev;
431
432failure:
433 /* allow the register to be completed before unregistering. */
434 rtnl_unlock();
435 rtnl_lock();
436
437 unregister_netdevice(dev);
438 return NULL;
439}
440
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100441#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000442static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000444 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000445 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500446 struct flowi4 fl4 = {
447 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700448 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500449 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000450 };
451 int err;
452
David S. Millerda919812011-03-12 02:04:50 -0500453 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000454 if (err < 0) {
455 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000456 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000457 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700460 dev->stats.tx_bytes += skb->len;
461 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000462 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 read_unlock(&mrt_lock);
464 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000465 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200468static int reg_vif_get_iflink(const struct net_device *dev)
469{
470 return 0;
471}
472
Stephen Hemminger007c3832008-11-20 20:28:35 -0800473static const struct net_device_ops reg_vif_netdev_ops = {
474 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200475 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800476};
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static void reg_vif_setup(struct net_device *dev)
479{
480 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800481 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530483 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700485 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000488static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000491 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000493 if (mrt->id == RT_TABLE_DEFAULT)
494 sprintf(name, "pimreg");
495 else
496 sprintf(name, "pimreg%u", mrt->id);
497
Tom Gundersenc835a672014-07-14 16:37:24 +0200498 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Ian Morris51456b22015-04-03 09:17:26 +0100500 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return NULL;
502
Tom Goff403dbb92009-06-14 03:16:13 -0700503 dev_net_set(dev, net);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (register_netdevice(dev)) {
506 free_netdev(dev);
507 return NULL;
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100510 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (dev_open(dev))
513 goto failure;
514
Wang Chen7dc00c82008-07-14 20:56:34 -0700515 dev_hold(dev);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return dev;
518
519failure:
520 /* allow the register to be completed before unregistering. */
521 rtnl_unlock();
522 rtnl_lock();
523
524 unregister_netdevice(dev);
525 return NULL;
526}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100527
528/* called with rcu_read_lock() */
529static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
530 unsigned int pimlen)
531{
532 struct net_device *reg_dev = NULL;
533 struct iphdr *encap;
534
535 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100536 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100537 * a. packet is really sent to a multicast group
538 * b. packet is not a NULL-REGISTER
539 * c. packet is not truncated
540 */
541 if (!ipv4_is_multicast(encap->daddr) ||
542 encap->tot_len == 0 ||
543 ntohs(encap->tot_len) + pimlen > skb->len)
544 return 1;
545
546 read_lock(&mrt_lock);
547 if (mrt->mroute_reg_vif_num >= 0)
548 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
549 read_unlock(&mrt_lock);
550
551 if (!reg_dev)
552 return 1;
553
554 skb->mac_header = skb->network_header;
555 skb_pull(skb, (u8 *)encap - skb->data);
556 skb_reset_network_header(skb);
557 skb->protocol = htons(ETH_P_IP);
558 skb->ip_summed = CHECKSUM_NONE;
559
560 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
561
562 netif_rx(skb);
563
564 return NET_RX_SUCCESS;
565}
566#else
567static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
568{
569 return NULL;
570}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571#endif
572
Ben Hutchings2c530402012-07-10 10:55:09 +0000573/**
574 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700575 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000577static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000578 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct vif_device *v;
581 struct net_device *dev;
582 struct in_device *in_dev;
583
Patrick McHardy0c122952010-04-13 05:03:22 +0000584 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -EADDRNOTAVAIL;
586
Patrick McHardy0c122952010-04-13 05:03:22 +0000587 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 write_lock_bh(&mrt_lock);
590 dev = v->dev;
591 v->dev = NULL;
592
593 if (!dev) {
594 write_unlock_bh(&mrt_lock);
595 return -EADDRNOTAVAIL;
596 }
597
Patrick McHardy0c122952010-04-13 05:03:22 +0000598 if (vifi == mrt->mroute_reg_vif_num)
599 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000601 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000603
604 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000605 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 break;
607 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000608 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
611 write_unlock_bh(&mrt_lock);
612
613 dev_set_allmulti(dev, -1);
614
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000615 in_dev = __in_dev_get_rtnl(dev);
616 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700617 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000618 inet_netconf_notify_devconf(dev_net(dev),
619 NETCONFA_MC_FORWARDING,
620 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 ip_rt_multicast_event(in_dev);
622 }
623
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000624 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000625 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 dev_put(dev);
628 return 0;
629}
630
Eric Dumazeta8c94862010-10-01 16:15:08 +0000631static void ipmr_cache_free_rcu(struct rcu_head *head)
632{
633 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
634
635 kmem_cache_free(mrt_cachep, c);
636}
637
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000638static inline void ipmr_cache_free(struct mfc_cache *c)
639{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000640 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000641}
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000644 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000646static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200648 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700650 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Patrick McHardy0c122952010-04-13 05:03:22 +0000652 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Jianjun Kongc354e122008-11-03 00:28:02 -0800654 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700655 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
657 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000658 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000660 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700661 e->error = -ETIMEDOUT;
662 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700663
Eric W. Biederman15e47302012-09-07 20:12:54 +0000664 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000665 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000670 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Patrick McHardye258beb2010-04-13 05:03:19 +0000673/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000674static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Patrick McHardy0c122952010-04-13 05:03:22 +0000676 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned long now;
678 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000679 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000682 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return;
684 }
685
Patrick McHardy0c122952010-04-13 05:03:22 +0000686 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto out;
688
689 now = jiffies;
690 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Patrick McHardy0c122952010-04-13 05:03:22 +0000692 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (time_after(c->mfc_un.unres.expires, now)) {
694 unsigned long interval = c->mfc_un.unres.expires - now;
695 if (interval < expires)
696 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 continue;
698 }
699
Patrick McHardy862465f2010-04-13 05:03:21 +0000700 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000701 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000702 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Patrick McHardy0c122952010-04-13 05:03:22 +0000705 if (!list_empty(&mrt->mfc_unres_queue))
706 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708out:
709 spin_unlock(&mfc_unres_lock);
710}
711
712/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000713static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000714 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 int vifi;
717
718 cache->mfc_un.res.minvif = MAXVIFS;
719 cache->mfc_un.res.maxvif = 0;
720 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
721
Patrick McHardy0c122952010-04-13 05:03:22 +0000722 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
723 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000724 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
726 if (cache->mfc_un.res.minvif > vifi)
727 cache->mfc_un.res.minvif = vifi;
728 if (cache->mfc_un.res.maxvif <= vifi)
729 cache->mfc_un.res.maxvif = vifi + 1;
730 }
731 }
732}
733
Patrick McHardy0c122952010-04-13 05:03:22 +0000734static int vif_add(struct net *net, struct mr_table *mrt,
735 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000738 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct net_device *dev;
740 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700741 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000744 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -EADDRINUSE;
746
747 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100749 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100750 return -EINVAL;
751 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * All the packets will be sent to the daemon
753 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000754 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000756 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!dev)
758 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700759 err = dev_set_allmulti(dev, 1);
760 if (err) {
761 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700762 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700763 return err;
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900766 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000767 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (!dev)
769 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700770 err = dev_set_allmulti(dev, 1);
771 if (err) {
772 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700773 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700774 return err;
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000777 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000779 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
780 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100781 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000782 dev_put(dev);
783 return -EADDRNOTAVAIL;
784 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000785 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000786 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (!dev)
789 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700790 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700791 if (err) {
792 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700793 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 break;
796 default:
797 return -EINVAL;
798 }
799
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000800 in_dev = __in_dev_get_rtnl(dev);
801 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000802 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000804 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700805 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000806 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
807 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ip_rt_multicast_event(in_dev);
809
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000810 /* Fill in the VIF structures */
811
Jianjun Kongc354e122008-11-03 00:28:02 -0800812 v->rate_limit = vifc->vifc_rate_limit;
813 v->local = vifc->vifc_lcl_addr.s_addr;
814 v->remote = vifc->vifc_rmt_addr.s_addr;
815 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!mrtsock)
817 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800818 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 v->bytes_in = 0;
820 v->bytes_out = 0;
821 v->pkt_in = 0;
822 v->pkt_out = 0;
823 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000824 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200825 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* And finish update writing critical data */
828 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800829 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000830 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000831 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000832 if (vifi+1 > mrt->maxvif)
833 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 write_unlock_bh(&mrt_lock);
835 return 0;
836}
837
Eric Dumazeta8c94862010-10-01 16:15:08 +0000838/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000839static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000840 __be32 origin,
841 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Jianjun Kongc354e122008-11-03 00:28:02 -0800843 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct mfc_cache *c;
845
Eric Dumazeta8c94862010-10-01 16:15:08 +0000846 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000847 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
848 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000850 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000853/* Look for a (*,*,oif) entry */
854static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
855 int vifi)
856{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100857 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000858 struct mfc_cache *c;
859
860 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100861 if (c->mfc_origin == htonl(INADDR_ANY) &&
862 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000863 c->mfc_un.res.ttls[vifi] < 255)
864 return c;
865
866 return NULL;
867}
868
869/* Look for a (*,G) entry */
870static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
871 __be32 mcastgrp, int vifi)
872{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100873 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000874 struct mfc_cache *c, *proxy;
875
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100876 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000877 goto skip;
878
879 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100880 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000881 c->mfc_mcastgrp == mcastgrp) {
882 if (c->mfc_un.res.ttls[vifi] < 255)
883 return c;
884
885 /* It's ok if the vifi is part of the static tree */
886 proxy = ipmr_cache_find_any_parent(mrt,
887 c->mfc_parent);
888 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
889 return c;
890 }
891
892skip:
893 return ipmr_cache_find_any_parent(mrt, vifi);
894}
895
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100896/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000897static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Jianjun Kongc354e122008-11-03 00:28:02 -0800899 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000900
901 if (c)
902 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return c;
904}
905
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000906static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Jianjun Kongc354e122008-11-03 00:28:02 -0800908 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000909
910 if (c) {
911 skb_queue_head_init(&c->mfc_un.unres.unresolved);
912 c->mfc_un.unres.expires = jiffies + 10*HZ;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return c;
915}
916
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100917/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000918static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
919 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700922 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000924 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800925 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700926 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
928
Hong zhi guo573ce262013-03-27 06:47:04 +0000929 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000930 nlh->nlmsg_len = skb_tail_pointer(skb) -
931 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 } else {
933 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000934 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000936 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700937 e->error = -EMSGSIZE;
938 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Thomas Graf2942e902006-08-15 00:30:25 -0700940
Eric W. Biederman15e47302012-09-07 20:12:54 +0000941 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000942 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000943 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946}
947
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100948/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
949 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100951 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000953static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000954 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300956 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100957 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct igmphdr *igmp;
959 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100960 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int ret;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (assert == IGMPMSG_WHOLEPKT)
964 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
965 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 skb = alloc_skb(128, GFP_ATOMIC);
967
Stephen Hemminger132adf52007-03-08 20:44:43 -0800968 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return -ENOBUFS;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (assert == IGMPMSG_WHOLEPKT) {
972 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000973 * Duplicate old header, fix ihl, length etc.
974 * And all this only to mangle msg->im_msgtype and
975 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300977 skb_push(skb, sizeof(struct iphdr));
978 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300979 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300980 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700981 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 msg->im_msgtype = IGMPMSG_WHOLEPKT;
983 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000984 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700985 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
986 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
987 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100988 } else {
989 /* Copy the IP header */
990 skb_set_network_header(skb, skb->len);
991 skb_put(skb, ihl);
992 skb_copy_to_linear_data(skb, pkt->data, ihl);
993 /* Flag to the kernel this is a route add */
994 ip_hdr(skb)->protocol = 0;
995 msg = (struct igmpmsg *)skb_network_header(skb);
996 msg->im_vif = vifi;
997 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
998 /* Add our header */
999 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1000 igmp->type = assert;
1001 msg->im_msgtype = assert;
1002 igmp->code = 0;
1003 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1004 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Eric Dumazet4c968702010-10-01 16:15:01 +00001007 rcu_read_lock();
1008 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001009 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001010 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 kfree_skb(skb);
1012 return -EINVAL;
1013 }
1014
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001015 /* Deliver to mrouted */
Eric Dumazet4c968702010-10-01 16:15:01 +00001016 ret = sock_queue_rcv_skb(mroute_sk, skb);
1017 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001018 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001019 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 kfree_skb(skb);
1021 }
1022
1023 return ret;
1024}
1025
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001026/* Queue a packet for resolution. It gets locked cache entry! */
1027static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1028 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Patrick McHardy862465f2010-04-13 05:03:21 +00001030 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 int err;
1032 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001033 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001036 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001037 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001038 c->mfc_origin == iph->saddr) {
1039 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Patrick McHardy862465f2010-04-13 05:03:21 +00001044 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001045 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001046 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001047 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 spin_unlock_bh(&mfc_unres_lock);
1049
1050 kfree_skb(skb);
1051 return -ENOBUFS;
1052 }
1053
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001054 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001055 c->mfc_parent = -1;
1056 c->mfc_origin = iph->saddr;
1057 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001059 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001060 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001061 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001062 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 out - Brad Parker
1064 */
1065 spin_unlock_bh(&mfc_unres_lock);
1066
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001067 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 kfree_skb(skb);
1069 return err;
1070 }
1071
Patrick McHardy0c122952010-04-13 05:03:22 +00001072 atomic_inc(&mrt->cache_resolve_queue_len);
1073 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001074 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
David S. Miller278554b2010-05-12 00:05:35 -07001076 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1077 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001080 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001081 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 kfree_skb(skb);
1083 err = -ENOBUFS;
1084 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001085 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 err = 0;
1087 }
1088
1089 spin_unlock_bh(&mfc_unres_lock);
1090 return err;
1091}
1092
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001093/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001095static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001098 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Jianjun Kongc354e122008-11-03 00:28:02 -08001100 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Patrick McHardy0c122952010-04-13 05:03:22 +00001102 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001104 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1105 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001106 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001107 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001108 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 0;
1110 }
1111 }
1112 return -ENOENT;
1113}
1114
Patrick McHardy0c122952010-04-13 05:03:22 +00001115static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001116 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Patrick McHardy862465f2010-04-13 05:03:21 +00001118 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001120 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Patrick McHardya50436f22010-03-17 06:04:14 +00001122 if (mfc->mfcc_parent >= MAXVIFS)
1123 return -ENFILE;
1124
Jianjun Kongc354e122008-11-03 00:28:02 -08001125 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Patrick McHardy0c122952010-04-13 05:03:22 +00001127 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001129 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1130 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001131 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
Patrick McHardy862465f2010-04-13 05:03:21 +00001136 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 write_lock_bh(&mrt_lock);
1138 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001139 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!mrtsock)
1141 c->mfc_flags |= MFC_STATIC;
1142 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001143 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return 0;
1145 }
1146
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001147 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001148 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return -EINVAL;
1150
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001151 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001152 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return -ENOMEM;
1154
Jianjun Kongc354e122008-11-03 00:28:02 -08001155 c->mfc_origin = mfc->mfcc_origin.s_addr;
1156 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1157 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001158 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (!mrtsock)
1160 c->mfc_flags |= MFC_STATIC;
1161
Eric Dumazeta8c94862010-10-01 16:15:08 +00001162 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001164 /* Check to see if we resolved a queued list. If so we
1165 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001167 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001169 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001170 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001172 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001173 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001174 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
1176 }
1177 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001178 if (list_empty(&mrt->mfc_unres_queue))
1179 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 spin_unlock_bh(&mfc_unres_lock);
1181
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001182 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001183 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001184 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001186 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
1188}
1189
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001190/* Close the multicast socket, and clear the vif tables etc */
Patrick McHardy0c122952010-04-13 05:03:22 +00001191static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001194 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001195 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001196
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001197 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001198 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001199 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001200 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001202 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001204 /* Wipe the cache */
Patrick McHardy862465f2010-04-13 05:03:21 +00001205 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001206 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001207 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001209 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001210 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001211 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213 }
1214
Patrick McHardy0c122952010-04-13 05:03:22 +00001215 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001217 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001218 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001219 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001220 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222 spin_unlock_bh(&mfc_unres_lock);
1223 }
1224}
1225
Eric Dumazet4c968702010-10-01 16:15:01 +00001226/* called from ip_ra_control(), before an RCU grace period,
1227 * we dont need to call synchronize_rcu() here
1228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static void mrtsock_destruct(struct sock *sk)
1230{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001231 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001232 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001235 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001236 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001237 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001238 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1239 NETCONFA_IFINDEX_ALL,
1240 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001241 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001242 mroute_clean_tables(mrt);
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245 rtnl_unlock();
1246}
1247
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001248/* Socket options and virtual interface manipulation. The whole
1249 * virtual interface system is a complete heap, but unfortunately
1250 * that's how BSD mrouted happens to think. Maybe one day with a proper
1251 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001253
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001254int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1255 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001257 struct net *net = sock_net(sk);
1258 int val, ret = 0, parent = 0;
1259 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct vifctl vif;
1261 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001262 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001263
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001264 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1265 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001266 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001267 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1268 ret = -EOPNOTSUPP;
1269 goto out_unlock;
1270 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001271
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001272 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001273 if (!mrt) {
1274 ret = -ENOENT;
1275 goto out_unlock;
1276 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001277 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001278 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001279 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1280 ret = -EACCES;
1281 goto out_unlock;
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
Stephen Hemminger132adf52007-03-08 20:44:43 -08001285 switch (optname) {
1286 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001287 if (optlen != sizeof(int))
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001288 ret = -EINVAL;
1289 if (rtnl_dereference(mrt->mroute_sk))
1290 ret = -EADDRINUSE;
1291 if (ret)
1292 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001293
1294 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1295 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001296 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001297 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001298 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1299 NETCONFA_IFINDEX_ALL,
1300 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001301 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001302 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001303 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001304 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1305 ret = -EACCES;
1306 } else {
1307 /* We need to unlock here because mrtsock_destruct takes
1308 * care of rtnl itself and we can't change that due to
1309 * the IP_ROUTER_ALERT setsockopt which runs without it.
1310 */
1311 rtnl_unlock();
1312 ret = ip_ra_control(sk, 0, NULL);
1313 goto out;
1314 }
1315 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001316 case MRT_ADD_VIF:
1317 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001318 if (optlen != sizeof(vif)) {
1319 ret = -EINVAL;
1320 break;
1321 }
1322 if (copy_from_user(&vif, optval, sizeof(vif))) {
1323 ret = -EFAULT;
1324 break;
1325 }
1326 if (vif.vifc_vifi >= MAXVIFS) {
1327 ret = -ENFILE;
1328 break;
1329 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001330 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001331 ret = vif_add(net, mrt, &vif,
1332 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001333 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001334 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001335 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001336 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001337 /* Manipulate the forwarding caches. These live
1338 * in a sort of kernel/user symbiosis.
1339 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001340 case MRT_ADD_MFC:
1341 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001342 parent = -1;
1343 case MRT_ADD_MFC_PROXY:
1344 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001345 if (optlen != sizeof(mfc)) {
1346 ret = -EINVAL;
1347 break;
1348 }
1349 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1350 ret = -EFAULT;
1351 break;
1352 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001353 if (parent == 0)
1354 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001355 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1356 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001357 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001358 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001359 sk == rtnl_dereference(mrt->mroute_sk),
1360 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001361 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001362 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001363 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001364 if (optlen != sizeof(val)) {
1365 ret = -EINVAL;
1366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001368 if (get_user(val, (int __user *)optval)) {
1369 ret = -EFAULT;
1370 break;
1371 }
1372 mrt->mroute_do_assert = val;
1373 break;
1374 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001375 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001376 ret = -ENOPROTOOPT;
1377 break;
1378 }
1379 if (optlen != sizeof(val)) {
1380 ret = -EINVAL;
1381 break;
1382 }
1383 if (get_user(val, (int __user *)optval)) {
1384 ret = -EFAULT;
1385 break;
1386 }
1387
1388 val = !!val;
1389 if (val != mrt->mroute_do_pim) {
1390 mrt->mroute_do_pim = val;
1391 mrt->mroute_do_assert = val;
1392 }
1393 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001394 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001395 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1396 ret = -ENOPROTOOPT;
1397 break;
1398 }
1399 if (optlen != sizeof(uval)) {
1400 ret = -EINVAL;
1401 break;
1402 }
1403 if (get_user(uval, (u32 __user *)optval)) {
1404 ret = -EFAULT;
1405 break;
1406 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001407
Eric Dumazet4c968702010-10-01 16:15:01 +00001408 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1409 ret = -EBUSY;
1410 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001411 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001412 if (IS_ERR(mrt))
1413 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001414 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001415 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c968702010-10-01 16:15:01 +00001416 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001417 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001418 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001419 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001420 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001422out_unlock:
1423 rtnl_unlock();
1424out:
1425 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001428/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001429int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431 int olr;
1432 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001433 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001434 struct mr_table *mrt;
1435
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001436 if (sk->sk_type != SOCK_RAW ||
1437 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1438 return -EOPNOTSUPP;
1439
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001440 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001441 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001442 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001444 switch (optname) {
1445 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001446 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001447 break;
1448 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001449 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001450 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001451 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001452 break;
1453 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001454 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001455 break;
1456 default:
1457 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001458 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001459
1460 if (get_user(olr, optlen))
1461 return -EFAULT;
1462 olr = min_t(unsigned int, olr, sizeof(int));
1463 if (olr < 0)
1464 return -EINVAL;
1465 if (put_user(olr, optlen))
1466 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001467 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return -EFAULT;
1469 return 0;
1470}
1471
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001472/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1474{
1475 struct sioc_sg_req sr;
1476 struct sioc_vif_req vr;
1477 struct vif_device *vif;
1478 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001479 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001480 struct mr_table *mrt;
1481
1482 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001483 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001484 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001485
Stephen Hemminger132adf52007-03-08 20:44:43 -08001486 switch (cmd) {
1487 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001488 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001489 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001490 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001491 return -EINVAL;
1492 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001493 vif = &mrt->vif_table[vr.vifi];
1494 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001495 vr.icount = vif->pkt_in;
1496 vr.ocount = vif->pkt_out;
1497 vr.ibytes = vif->bytes_in;
1498 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001500
Jianjun Kongc354e122008-11-03 00:28:02 -08001501 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001503 return 0;
1504 }
1505 read_unlock(&mrt_lock);
1506 return -EADDRNOTAVAIL;
1507 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001508 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001509 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Eric Dumazeta8c94862010-10-01 16:15:08 +00001511 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001512 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001513 if (c) {
1514 sr.pktcnt = c->mfc_un.res.pkt;
1515 sr.bytecnt = c->mfc_un.res.bytes;
1516 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001517 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001518
Jianjun Kongc354e122008-11-03 00:28:02 -08001519 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520 return -EFAULT;
1521 return 0;
1522 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001523 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001524 return -EADDRNOTAVAIL;
1525 default:
1526 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528}
1529
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001530#ifdef CONFIG_COMPAT
1531struct compat_sioc_sg_req {
1532 struct in_addr src;
1533 struct in_addr grp;
1534 compat_ulong_t pktcnt;
1535 compat_ulong_t bytecnt;
1536 compat_ulong_t wrong_if;
1537};
1538
David S. Millerca6b8bb02011-02-03 17:24:28 -08001539struct compat_sioc_vif_req {
1540 vifi_t vifi; /* Which iface */
1541 compat_ulong_t icount;
1542 compat_ulong_t ocount;
1543 compat_ulong_t ibytes;
1544 compat_ulong_t obytes;
1545};
1546
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001547int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1548{
David S. Miller0033d5a2011-02-03 17:21:31 -08001549 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001550 struct compat_sioc_vif_req vr;
1551 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001552 struct mfc_cache *c;
1553 struct net *net = sock_net(sk);
1554 struct mr_table *mrt;
1555
1556 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001557 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001558 return -ENOENT;
1559
1560 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001561 case SIOCGETVIFCNT:
1562 if (copy_from_user(&vr, arg, sizeof(vr)))
1563 return -EFAULT;
1564 if (vr.vifi >= mrt->maxvif)
1565 return -EINVAL;
1566 read_lock(&mrt_lock);
1567 vif = &mrt->vif_table[vr.vifi];
1568 if (VIF_EXISTS(mrt, vr.vifi)) {
1569 vr.icount = vif->pkt_in;
1570 vr.ocount = vif->pkt_out;
1571 vr.ibytes = vif->bytes_in;
1572 vr.obytes = vif->bytes_out;
1573 read_unlock(&mrt_lock);
1574
1575 if (copy_to_user(arg, &vr, sizeof(vr)))
1576 return -EFAULT;
1577 return 0;
1578 }
1579 read_unlock(&mrt_lock);
1580 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001581 case SIOCGETSGCNT:
1582 if (copy_from_user(&sr, arg, sizeof(sr)))
1583 return -EFAULT;
1584
1585 rcu_read_lock();
1586 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1587 if (c) {
1588 sr.pktcnt = c->mfc_un.res.pkt;
1589 sr.bytecnt = c->mfc_un.res.bytes;
1590 sr.wrong_if = c->mfc_un.res.wrong_if;
1591 rcu_read_unlock();
1592
1593 if (copy_to_user(arg, &sr, sizeof(sr)))
1594 return -EFAULT;
1595 return 0;
1596 }
1597 rcu_read_unlock();
1598 return -EADDRNOTAVAIL;
1599 default:
1600 return -ENOIOCTLCMD;
1601 }
1602}
1603#endif
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1606{
Jiri Pirko351638e2013-05-28 01:30:21 +00001607 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001608 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001609 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct vif_device *v;
1611 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (event != NETDEV_UNREGISTER)
1614 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001615
1616 ipmr_for_each_table(mrt, net) {
1617 v = &mrt->vif_table[0];
1618 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1619 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001620 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623 return NOTIFY_DONE;
1624}
1625
Jianjun Kongc354e122008-11-03 00:28:02 -08001626static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 .notifier_call = ipmr_device_event,
1628};
1629
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001630/* Encapsulate a packet by attaching a valid IPIP header to it.
1631 * This avoids tunnel drivers and other mess and gives us the speed so
1632 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001634static void ip_encap(struct net *net, struct sk_buff *skb,
1635 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001637 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001638 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001639
1640 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001641 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001642 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001643 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001645 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001646 iph->tos = old_iph->tos;
1647 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 iph->frag_off = 0;
1649 iph->daddr = daddr;
1650 iph->saddr = saddr;
1651 iph->protocol = IPPROTO_IPIP;
1652 iph->ihl = 5;
1653 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001654 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 ip_send_check(iph);
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1658 nf_reset(skb);
1659}
1660
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001661static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1662 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001664 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
David S. Miller73186df2015-11-03 13:41:45 -05001666 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1667 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 if (unlikely(opt->optlen))
1670 ip_forward_options(skb);
1671
Eric W. Biederman13206b62015-10-07 16:48:35 -05001672 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001675/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Patrick McHardy0c122952010-04-13 05:03:22 +00001677static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1678 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001680 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001681 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 struct net_device *dev;
1683 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001684 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 int encap = 0;
1686
Ian Morris51456b22015-04-03 09:17:26 +01001687 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 goto out_free;
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (vif->flags & VIFF_REGISTER) {
1691 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001692 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001693 vif->dev->stats.tx_bytes += skb->len;
1694 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001695 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001696 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001699 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001700 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001701 vif->remote, vif->local,
1702 0, 0,
1703 IPPROTO_IPIP,
1704 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001705 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 goto out_free;
1707 encap = sizeof(struct iphdr);
1708 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001709 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001710 0, 0,
1711 IPPROTO_IPIP,
1712 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001713 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 goto out_free;
1715 }
1716
Changli Gaod8d1f302010-06-10 23:31:35 -07001717 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Changli Gaod8d1f302010-06-10 23:31:35 -07001719 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001721 * allow to send ICMP, so that packets will disappear
1722 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 */
David S. Miller73186df2015-11-03 13:41:45 -05001724 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ip_rt_put(rt);
1726 goto out_free;
1727 }
1728
Changli Gaod8d1f302010-06-10 23:31:35 -07001729 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001732 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 goto out_free;
1734 }
1735
1736 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001737 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Eric Dumazetadf30902009-06-02 05:19:30 +00001739 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001740 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001741 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001744 * What do we do with netfilter? -- RR
1745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001747 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001749 vif->dev->stats.tx_packets++;
1750 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752
1753 IPCB(skb)->flags |= IPSKB_FORWARDED;
1754
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001755 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 * not only before forwarding, but after forwarding on all output
1757 * interfaces. It is clear, if mrouter runs a multicasting
1758 * program, it should receive packets not depending to what interface
1759 * program is joined.
1760 * If we will not make it, the program will have to join on all
1761 * interfaces. On the other hand, multihoming host (or router, but
1762 * not mrouter) cannot join to more than one interface - it will
1763 * result in receiving multiple packets.
1764 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001765 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1766 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 ipmr_forward_finish);
1768 return;
1769
1770out_free:
1771 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
Patrick McHardy0c122952010-04-13 05:03:22 +00001774static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001777
1778 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1779 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 break;
1781 }
1782 return ct;
1783}
1784
1785/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001786static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1787 struct sk_buff *skb, struct mfc_cache *cache,
1788 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 int psend = -1;
1791 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001792 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 vif = cache->mfc_parent;
1795 cache->mfc_un.res.pkt++;
1796 cache->mfc_un.res.bytes += skb->len;
1797
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001798 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001799 struct mfc_cache *cache_proxy;
1800
1801 /* For an (*,G) entry, we only check that the incomming
1802 * interface is part of the static tree.
1803 */
1804 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1805 if (cache_proxy &&
1806 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1807 goto forward;
1808 }
1809
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001810 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001811 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001812 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001814 * Very complicated situation...
1815 *
1816 * The best workaround until routing daemons will be
1817 * fixed is not to redistribute packet, if it was
1818 * send through wrong interface. It means, that
1819 * multicast applications WILL NOT work for
1820 * (S,G), which have default multicast route pointing
1821 * to wrong oif. In any case, it is not a good
1822 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 */
1824 goto dont_forward;
1825 }
1826
1827 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Patrick McHardy0c122952010-04-13 05:03:22 +00001829 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001831 * so that we cannot check that packet arrived on an oif.
1832 * It is bad, but otherwise we would need to move pretty
1833 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001835 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001836 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001837 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1839 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001840 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842 goto dont_forward;
1843 }
1844
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001845forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001846 mrt->vif_table[vif].pkt_in++;
1847 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001849 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001850 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1851 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001852 if (true_vifi >= 0 &&
1853 true_vifi != cache->mfc_parent &&
1854 ip_hdr(skb)->ttl >
1855 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1856 /* It's an (*,*) entry and the packet is not coming from
1857 * the upstream: forward the packet to the upstream
1858 * only.
1859 */
1860 psend = cache->mfc_parent;
1861 goto last_forward;
1862 }
1863 goto dont_forward;
1864 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001865 for (ct = cache->mfc_un.res.maxvif - 1;
1866 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001867 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001868 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1869 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001870 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (psend != -1) {
1872 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001875 ipmr_queue_xmit(net, mrt, skb2, cache,
1876 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001878 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001881last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (psend != -1) {
1883 if (local) {
1884 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001887 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001889 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001890 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892 }
1893
1894dont_forward:
1895 if (!local)
1896 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
David S. Miller417da662011-05-03 19:42:43 -07001899static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001900{
David S. Miller417da662011-05-03 19:42:43 -07001901 struct rtable *rt = skb_rtable(skb);
1902 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001903 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001904 .daddr = iph->daddr,
1905 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001906 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001907 .flowi4_oif = (rt_is_output_route(rt) ?
1908 skb->dev->ifindex : 0),
1909 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001910 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001911 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001912 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001913 };
1914 struct mr_table *mrt;
1915 int err;
1916
David S. Millerda919812011-03-12 02:04:50 -05001917 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001918 if (err)
1919 return ERR_PTR(err);
1920 return mrt;
1921}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001923/* Multicast packets for forwarding arrive here
1924 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926int ip_mr_input(struct sk_buff *skb)
1927{
1928 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001929 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001930 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001931 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001934 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001936 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 goto dont_forward;
1938
David S. Miller417da662011-05-03 19:42:43 -07001939 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001940 if (IS_ERR(mrt)) {
1941 kfree_skb(skb);
1942 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001945 if (IPCB(skb)->opt.router_alert) {
1946 if (ip_call_ra_chain(skb))
1947 return 0;
1948 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1949 /* IGMPv1 (and broken IGMPv2 implementations sort of
1950 * Cisco IOS <= 11.2(8)) do not put router alert
1951 * option to IGMP packets destined to routable
1952 * groups. It is very bad, because it means
1953 * that we can forward NO IGMP messages.
1954 */
1955 struct sock *mroute_sk;
1956
1957 mroute_sk = rcu_dereference(mrt->mroute_sk);
1958 if (mroute_sk) {
1959 nf_reset(skb);
1960 raw_rcv(mroute_sk, skb);
1961 return 0;
1962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964 }
1965
Eric Dumazeta8c94862010-10-01 16:15:08 +00001966 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001967 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001968 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001969 int vif = ipmr_find_vif(mrt, skb->dev);
1970
1971 if (vif >= 0)
1972 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1973 vif);
1974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001976 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01001977 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 int vif;
1979
1980 if (local) {
1981 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1982 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01001983 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 skb = skb2;
1986 }
1987
Eric Dumazeta8c94862010-10-01 16:15:08 +00001988 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001989 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001991 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 read_unlock(&mrt_lock);
1993
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001994 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 read_unlock(&mrt_lock);
1997 kfree_skb(skb);
1998 return -ENODEV;
1999 }
2000
Eric Dumazeta8c94862010-10-01 16:15:08 +00002001 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002002 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 read_unlock(&mrt_lock);
2004
2005 if (local)
2006 return ip_local_deliver(skb);
2007
2008 return 0;
2009
2010dont_forward:
2011 if (local)
2012 return ip_local_deliver(skb);
2013 kfree_skb(skb);
2014 return 0;
2015}
2016
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002017#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002018/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002019int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002020{
2021 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002022 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002023 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002024
2025 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2026 goto drop;
2027
2028 pim = igmp_hdr(skb);
2029
David S. Miller417da662011-05-03 19:42:43 -07002030 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002031 if (IS_ERR(mrt))
2032 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002033 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002034 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2035 goto drop;
2036
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002037 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002038drop:
2039 kfree_skb(skb);
2040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return 0;
2042}
2043#endif
2044
2045#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002046static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002049 struct net *net = dev_net(skb->dev);
2050 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002052 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 goto drop;
2054
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002055 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002056 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2057 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002058 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002059 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 goto drop;
2061
David S. Miller417da662011-05-03 19:42:43 -07002062 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002063 if (IS_ERR(mrt))
2064 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002065 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002066drop:
2067 kfree_skb(skb);
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return 0;
2070}
2071#endif
2072
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002073static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2074 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 int ct;
2077 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002078 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002079 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Nicolas Dichtel74381892010-03-25 23:45:35 +00002081 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002082 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002083 return -ENOENT;
2084
Thomas Graf92a395e2012-06-26 23:36:13 +00002085 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2086 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2087 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Thomas Graf92a395e2012-06-26 23:36:13 +00002089 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2090 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002093 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002094 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2095 nla_nest_cancel(skb, mp_attr);
2096 return -EMSGSIZE;
2097 }
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 nhp->rtnh_flags = 0;
2100 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002101 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 nhp->rtnh_len = sizeof(*nhp);
2103 }
2104 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002105
2106 nla_nest_end(skb, mp_attr);
2107
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002108 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2109 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2110 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2111 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2112 return -EMSGSIZE;
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 rtm->rtm_type = RTN_MULTICAST;
2115 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
David S. Miller9a1b9492011-05-04 12:18:54 -07002118int ipmr_get_route(struct net *net, struct sk_buff *skb,
2119 __be32 saddr, __be32 daddr,
2120 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002123 struct mr_table *mrt;
2124 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002126 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002127 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002128 return -ENOENT;
2129
Eric Dumazeta8c94862010-10-01 16:15:08 +00002130 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002131 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002132 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002133 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002135 if (vif >= 0)
2136 cache = ipmr_cache_find_any(mrt, daddr, vif);
2137 }
Ian Morris51456b22015-04-03 09:17:26 +01002138 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002139 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002140 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002142 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002145 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return -EAGAIN;
2147 }
2148
2149 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002150 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002151 if (dev)
2152 vif = ipmr_find_vif(mrt, dev);
2153 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002155 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return -ENODEV;
2157 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002158 skb2 = skb_clone(skb, GFP_ATOMIC);
2159 if (!skb2) {
2160 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002161 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002162 return -ENOMEM;
2163 }
2164
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002165 skb_push(skb2, sizeof(struct iphdr));
2166 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002167 iph = ip_hdr(skb2);
2168 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002169 iph->saddr = saddr;
2170 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002171 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002172 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002174 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return err;
2176 }
2177
Eric Dumazeta8c94862010-10-01 16:15:08 +00002178 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002179 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002181 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return err;
2183}
2184
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002185static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002186 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2187 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002188{
2189 struct nlmsghdr *nlh;
2190 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002191 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002192
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002193 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002194 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002195 return -EMSGSIZE;
2196
2197 rtm = nlmsg_data(nlh);
2198 rtm->rtm_family = RTNL_FAMILY_IPMR;
2199 rtm->rtm_dst_len = 32;
2200 rtm->rtm_src_len = 32;
2201 rtm->rtm_tos = 0;
2202 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002203 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2204 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002205 rtm->rtm_type = RTN_MULTICAST;
2206 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002207 if (c->mfc_flags & MFC_STATIC)
2208 rtm->rtm_protocol = RTPROT_STATIC;
2209 else
2210 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002211 rtm->rtm_flags = 0;
2212
Jiri Benc930345e2015-03-29 16:59:25 +02002213 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2214 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002215 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002216 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2217 /* do not break the dump if cache is unresolved */
2218 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002219 goto nla_put_failure;
2220
Johannes Berg053c0952015-01-16 22:09:00 +01002221 nlmsg_end(skb, nlh);
2222 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002223
2224nla_put_failure:
2225 nlmsg_cancel(skb, nlh);
2226 return -EMSGSIZE;
2227}
2228
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002229static size_t mroute_msgsize(bool unresolved, int maxvif)
2230{
2231 size_t len =
2232 NLMSG_ALIGN(sizeof(struct rtmsg))
2233 + nla_total_size(4) /* RTA_TABLE */
2234 + nla_total_size(4) /* RTA_SRC */
2235 + nla_total_size(4) /* RTA_DST */
2236 ;
2237
2238 if (!unresolved)
2239 len = len
2240 + nla_total_size(4) /* RTA_IIF */
2241 + nla_total_size(0) /* RTA_MULTIPATH */
2242 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2243 /* RTA_MFC_STATS */
2244 + nla_total_size(sizeof(struct rta_mfc_stats))
2245 ;
2246
2247 return len;
2248}
2249
2250static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2251 int cmd)
2252{
2253 struct net *net = read_pnet(&mrt->net);
2254 struct sk_buff *skb;
2255 int err = -ENOBUFS;
2256
2257 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2258 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002259 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002260 goto errout;
2261
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002262 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002263 if (err < 0)
2264 goto errout;
2265
2266 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2267 return;
2268
2269errout:
2270 kfree_skb(skb);
2271 if (err < 0)
2272 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2273}
2274
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002275static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2276{
2277 struct net *net = sock_net(skb->sk);
2278 struct mr_table *mrt;
2279 struct mfc_cache *mfc;
2280 unsigned int t = 0, s_t;
2281 unsigned int h = 0, s_h;
2282 unsigned int e = 0, s_e;
2283
2284 s_t = cb->args[0];
2285 s_h = cb->args[1];
2286 s_e = cb->args[2];
2287
Eric Dumazeta8c94862010-10-01 16:15:08 +00002288 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002289 ipmr_for_each_table(mrt, net) {
2290 if (t < s_t)
2291 goto next_table;
2292 if (t > s_t)
2293 s_h = 0;
2294 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002295 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002296 if (e < s_e)
2297 goto next_entry;
2298 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002299 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002300 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002301 mfc, RTM_NEWROUTE,
2302 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002303 goto done;
2304next_entry:
2305 e++;
2306 }
2307 e = s_e = 0;
2308 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002309 spin_lock_bh(&mfc_unres_lock);
2310 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2311 if (e < s_e)
2312 goto next_entry2;
2313 if (ipmr_fill_mroute(mrt, skb,
2314 NETLINK_CB(cb->skb).portid,
2315 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002316 mfc, RTM_NEWROUTE,
2317 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002318 spin_unlock_bh(&mfc_unres_lock);
2319 goto done;
2320 }
2321next_entry2:
2322 e++;
2323 }
2324 spin_unlock_bh(&mfc_unres_lock);
2325 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002326 s_h = 0;
2327next_table:
2328 t++;
2329 }
2330done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002331 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002332
2333 cb->args[2] = e;
2334 cb->args[1] = h;
2335 cb->args[0] = t;
2336
2337 return skb->len;
2338}
2339
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002340#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002341/* The /proc interfaces to multicast routing :
2342 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 */
2344struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002345 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002346 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 int ct;
2348};
2349
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002350static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2351 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 loff_t pos)
2353{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002354 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002355
2356 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2357 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002359 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002360 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362 return NULL;
2363}
2364
2365static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002366 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002368 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002369 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002370 struct mr_table *mrt;
2371
2372 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002373 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002374 return ERR_PTR(-ENOENT);
2375
2376 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002379 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 : SEQ_START_TOKEN;
2381}
2382
2383static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2384{
2385 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002386 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002387 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 ++*pos;
2390 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002391 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002392
Patrick McHardy0c122952010-04-13 05:03:22 +00002393 while (++iter->ct < mrt->maxvif) {
2394 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002396 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 return NULL;
2399}
2400
2401static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002402 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
2404 read_unlock(&mrt_lock);
2405}
2406
2407static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2408{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002409 struct ipmr_vif_iter *iter = seq->private;
2410 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002413 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2415 } else {
2416 const struct vif_device *vif = v;
2417 const char *name = vif->dev ? vif->dev->name : "none";
2418
2419 seq_printf(seq,
2420 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002421 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002422 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 vif->bytes_out, vif->pkt_out,
2424 vif->flags, vif->local, vif->remote);
2425 }
2426 return 0;
2427}
2428
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002429static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 .start = ipmr_vif_seq_start,
2431 .next = ipmr_vif_seq_next,
2432 .stop = ipmr_vif_seq_stop,
2433 .show = ipmr_vif_seq_show,
2434};
2435
2436static int ipmr_vif_open(struct inode *inode, struct file *file)
2437{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002438 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2439 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Arjan van de Ven9a321442007-02-12 00:55:35 -08002442static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 .owner = THIS_MODULE,
2444 .open = ipmr_vif_open,
2445 .read = seq_read,
2446 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002447 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448};
2449
2450struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002451 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002452 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002453 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 int ct;
2455};
2456
2457
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002458static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2459 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002461 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 struct mfc_cache *mfc;
2463
Eric Dumazeta8c94862010-10-01 16:15:08 +00002464 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002465 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002466 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002467 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002468 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002470 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002471 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002474 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002475 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002476 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return mfc;
2478 spin_unlock_bh(&mfc_unres_lock);
2479
2480 it->cache = NULL;
2481 return NULL;
2482}
2483
2484
2485static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2486{
2487 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002488 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002489 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002490
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002491 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002492 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002493 return ERR_PTR(-ENOENT);
2494
2495 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 it->cache = NULL;
2497 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002498 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 : SEQ_START_TOKEN;
2500}
2501
2502static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2503{
2504 struct mfc_cache *mfc = v;
2505 struct ipmr_mfc_iter *it = 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 = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 ++*pos;
2510
2511 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002512 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Patrick McHardy862465f2010-04-13 05:03:21 +00002514 if (mfc->list.next != it->cache)
2515 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002516
Patrick McHardy0c122952010-04-13 05:03:22 +00002517 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 goto end_of_list;
2519
Patrick McHardy0c122952010-04-13 05:03:22 +00002520 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002523 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002524 if (list_empty(it->cache))
2525 continue;
2526 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528
2529 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002530 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002531 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002535 if (!list_empty(it->cache))
2536 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002538end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 spin_unlock_bh(&mfc_unres_lock);
2540 it->cache = NULL;
2541
2542 return NULL;
2543}
2544
2545static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2546{
2547 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002548 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Patrick McHardy0c122952010-04-13 05:03:22 +00002550 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002552 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002553 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
2556static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2557{
2558 int n;
2559
2560 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002561 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2563 } else {
2564 const struct mfc_cache *mfc = v;
2565 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002566 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002567
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002568 seq_printf(seq, "%08X %08X %-3hd",
2569 (__force u32) mfc->mfc_mcastgrp,
2570 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002571 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Patrick McHardy0c122952010-04-13 05:03:22 +00002573 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002574 seq_printf(seq, " %8lu %8lu %8lu",
2575 mfc->mfc_un.res.pkt,
2576 mfc->mfc_un.res.bytes,
2577 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002578 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002579 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002580 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002581 mfc->mfc_un.res.ttls[n] < 255)
2582 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002583 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 n, mfc->mfc_un.res.ttls[n]);
2585 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002586 } else {
2587 /* unresolved mfc_caches don't contain
2588 * pkt, bytes and wrong_if values
2589 */
2590 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
2592 seq_putc(seq, '\n');
2593 }
2594 return 0;
2595}
2596
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002597static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 .start = ipmr_mfc_seq_start,
2599 .next = ipmr_mfc_seq_next,
2600 .stop = ipmr_mfc_seq_stop,
2601 .show = ipmr_mfc_seq_show,
2602};
2603
2604static int ipmr_mfc_open(struct inode *inode, struct file *file)
2605{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002606 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2607 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
Arjan van de Ven9a321442007-02-12 00:55:35 -08002610static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 .owner = THIS_MODULE,
2612 .open = ipmr_mfc_open,
2613 .read = seq_read,
2614 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002615 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002617#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002620static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002622 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623};
2624#endif
2625
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002626/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002627static int __net_init ipmr_net_init(struct net *net)
2628{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002629 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002630
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002631 err = ipmr_rules_init(net);
2632 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002633 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002634
2635#ifdef CONFIG_PROC_FS
2636 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002637 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002638 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002639 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002640 goto proc_cache_fail;
2641#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002642 return 0;
2643
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002644#ifdef CONFIG_PROC_FS
2645proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002646 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002647proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002648 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002649#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002650fail:
2651 return err;
2652}
2653
2654static void __net_exit ipmr_net_exit(struct net *net)
2655{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002656#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002657 remove_proc_entry("ip_mr_cache", net->proc_net);
2658 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002659#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002660 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002661}
2662
2663static struct pernet_operations ipmr_net_ops = {
2664 .init = ipmr_net_init,
2665 .exit = ipmr_net_exit,
2666};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002667
Wang Chen03d2f892008-07-03 12:13:36 +08002668int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Wang Chen03d2f892008-07-03 12:13:36 +08002670 int err;
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2673 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002674 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002675 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002676
Benjamin Therycf958ae32009-01-22 04:56:16 +00002677 err = register_pernet_subsys(&ipmr_net_ops);
2678 if (err)
2679 goto reg_pernet_fail;
2680
Wang Chen03d2f892008-07-03 12:13:36 +08002681 err = register_netdevice_notifier(&ip_mr_notifier);
2682 if (err)
2683 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002684#ifdef CONFIG_IP_PIMSM_V2
2685 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002686 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002687 err = -EAGAIN;
2688 goto add_proto_fail;
2689 }
2690#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002691 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2692 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002693 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002694
Tom Goff403dbb92009-06-14 03:16:13 -07002695#ifdef CONFIG_IP_PIMSM_V2
2696add_proto_fail:
2697 unregister_netdevice_notifier(&ip_mr_notifier);
2698#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002699reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002700 unregister_pernet_subsys(&ipmr_net_ops);
2701reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002702 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}