blob: 6c24a16299c73c3b5b2151f1047a87edf119bde3 [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 Dumazet4c9687092010-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 Dumazet4c9687092010-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 Dumazet4c9687092010-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 Dumazet4c9687092010-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 Dumazet4c9687092010-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:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001287 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001288 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001289 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001290 }
1291 if (rtnl_dereference(mrt->mroute_sk)) {
1292 ret = -EADDRINUSE;
1293 break;
1294 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001295
1296 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1297 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001298 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001299 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001300 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1301 NETCONFA_IFINDEX_ALL,
1302 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001303 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001304 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001306 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1307 ret = -EACCES;
1308 } else {
1309 /* We need to unlock here because mrtsock_destruct takes
1310 * care of rtnl itself and we can't change that due to
1311 * the IP_ROUTER_ALERT setsockopt which runs without it.
1312 */
1313 rtnl_unlock();
1314 ret = ip_ra_control(sk, 0, NULL);
1315 goto out;
1316 }
1317 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001318 case MRT_ADD_VIF:
1319 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001320 if (optlen != sizeof(vif)) {
1321 ret = -EINVAL;
1322 break;
1323 }
1324 if (copy_from_user(&vif, optval, sizeof(vif))) {
1325 ret = -EFAULT;
1326 break;
1327 }
1328 if (vif.vifc_vifi >= MAXVIFS) {
1329 ret = -ENFILE;
1330 break;
1331 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001332 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001333 ret = vif_add(net, mrt, &vif,
1334 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001335 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001336 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001337 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001338 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001339 /* Manipulate the forwarding caches. These live
1340 * in a sort of kernel/user symbiosis.
1341 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001342 case MRT_ADD_MFC:
1343 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001344 parent = -1;
1345 case MRT_ADD_MFC_PROXY:
1346 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001347 if (optlen != sizeof(mfc)) {
1348 ret = -EINVAL;
1349 break;
1350 }
1351 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1352 ret = -EFAULT;
1353 break;
1354 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001355 if (parent == 0)
1356 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001357 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1358 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001359 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001360 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001361 sk == rtnl_dereference(mrt->mroute_sk),
1362 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001363 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001364 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001365 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001366 if (optlen != sizeof(val)) {
1367 ret = -EINVAL;
1368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001370 if (get_user(val, (int __user *)optval)) {
1371 ret = -EFAULT;
1372 break;
1373 }
1374 mrt->mroute_do_assert = val;
1375 break;
1376 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001377 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001378 ret = -ENOPROTOOPT;
1379 break;
1380 }
1381 if (optlen != sizeof(val)) {
1382 ret = -EINVAL;
1383 break;
1384 }
1385 if (get_user(val, (int __user *)optval)) {
1386 ret = -EFAULT;
1387 break;
1388 }
1389
1390 val = !!val;
1391 if (val != mrt->mroute_do_pim) {
1392 mrt->mroute_do_pim = val;
1393 mrt->mroute_do_assert = val;
1394 }
1395 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001396 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001397 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1398 ret = -ENOPROTOOPT;
1399 break;
1400 }
1401 if (optlen != sizeof(uval)) {
1402 ret = -EINVAL;
1403 break;
1404 }
1405 if (get_user(uval, (u32 __user *)optval)) {
1406 ret = -EFAULT;
1407 break;
1408 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001409
Eric Dumazet4c9687092010-10-01 16:15:01 +00001410 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1411 ret = -EBUSY;
1412 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001413 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001414 if (IS_ERR(mrt))
1415 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001416 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001417 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001418 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001419 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001420 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001421 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001422 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001424out_unlock:
1425 rtnl_unlock();
1426out:
1427 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001430/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001431int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 int olr;
1434 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001435 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001436 struct mr_table *mrt;
1437
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001438 if (sk->sk_type != SOCK_RAW ||
1439 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1440 return -EOPNOTSUPP;
1441
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001442 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001443 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001444 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001446 switch (optname) {
1447 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001448 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001449 break;
1450 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001451 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001452 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001453 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001454 break;
1455 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001456 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001457 break;
1458 default:
1459 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001460 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001461
1462 if (get_user(olr, optlen))
1463 return -EFAULT;
1464 olr = min_t(unsigned int, olr, sizeof(int));
1465 if (olr < 0)
1466 return -EINVAL;
1467 if (put_user(olr, optlen))
1468 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001469 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -EFAULT;
1471 return 0;
1472}
1473
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001474/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1476{
1477 struct sioc_sg_req sr;
1478 struct sioc_vif_req vr;
1479 struct vif_device *vif;
1480 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001481 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001482 struct mr_table *mrt;
1483
1484 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001485 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001486 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001487
Stephen Hemminger132adf52007-03-08 20:44:43 -08001488 switch (cmd) {
1489 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001490 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001491 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001492 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001493 return -EINVAL;
1494 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001495 vif = &mrt->vif_table[vr.vifi];
1496 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001497 vr.icount = vif->pkt_in;
1498 vr.ocount = vif->pkt_out;
1499 vr.ibytes = vif->bytes_in;
1500 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001502
Jianjun Kongc354e122008-11-03 00:28:02 -08001503 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001505 return 0;
1506 }
1507 read_unlock(&mrt_lock);
1508 return -EADDRNOTAVAIL;
1509 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001510 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001511 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Eric Dumazeta8c94862010-10-01 16:15:08 +00001513 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001514 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001515 if (c) {
1516 sr.pktcnt = c->mfc_un.res.pkt;
1517 sr.bytecnt = c->mfc_un.res.bytes;
1518 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001519 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520
Jianjun Kongc354e122008-11-03 00:28:02 -08001521 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522 return -EFAULT;
1523 return 0;
1524 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001525 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001526 return -EADDRNOTAVAIL;
1527 default:
1528 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530}
1531
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001532#ifdef CONFIG_COMPAT
1533struct compat_sioc_sg_req {
1534 struct in_addr src;
1535 struct in_addr grp;
1536 compat_ulong_t pktcnt;
1537 compat_ulong_t bytecnt;
1538 compat_ulong_t wrong_if;
1539};
1540
David S. Millerca6b8bb02011-02-03 17:24:28 -08001541struct compat_sioc_vif_req {
1542 vifi_t vifi; /* Which iface */
1543 compat_ulong_t icount;
1544 compat_ulong_t ocount;
1545 compat_ulong_t ibytes;
1546 compat_ulong_t obytes;
1547};
1548
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001549int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1550{
David S. Miller0033d5a2011-02-03 17:21:31 -08001551 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001552 struct compat_sioc_vif_req vr;
1553 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001554 struct mfc_cache *c;
1555 struct net *net = sock_net(sk);
1556 struct mr_table *mrt;
1557
1558 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001559 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001560 return -ENOENT;
1561
1562 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001563 case SIOCGETVIFCNT:
1564 if (copy_from_user(&vr, arg, sizeof(vr)))
1565 return -EFAULT;
1566 if (vr.vifi >= mrt->maxvif)
1567 return -EINVAL;
1568 read_lock(&mrt_lock);
1569 vif = &mrt->vif_table[vr.vifi];
1570 if (VIF_EXISTS(mrt, vr.vifi)) {
1571 vr.icount = vif->pkt_in;
1572 vr.ocount = vif->pkt_out;
1573 vr.ibytes = vif->bytes_in;
1574 vr.obytes = vif->bytes_out;
1575 read_unlock(&mrt_lock);
1576
1577 if (copy_to_user(arg, &vr, sizeof(vr)))
1578 return -EFAULT;
1579 return 0;
1580 }
1581 read_unlock(&mrt_lock);
1582 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001583 case SIOCGETSGCNT:
1584 if (copy_from_user(&sr, arg, sizeof(sr)))
1585 return -EFAULT;
1586
1587 rcu_read_lock();
1588 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1589 if (c) {
1590 sr.pktcnt = c->mfc_un.res.pkt;
1591 sr.bytecnt = c->mfc_un.res.bytes;
1592 sr.wrong_if = c->mfc_un.res.wrong_if;
1593 rcu_read_unlock();
1594
1595 if (copy_to_user(arg, &sr, sizeof(sr)))
1596 return -EFAULT;
1597 return 0;
1598 }
1599 rcu_read_unlock();
1600 return -EADDRNOTAVAIL;
1601 default:
1602 return -ENOIOCTLCMD;
1603 }
1604}
1605#endif
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1608{
Jiri Pirko351638e2013-05-28 01:30:21 +00001609 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001610 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001611 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 struct vif_device *v;
1613 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (event != NETDEV_UNREGISTER)
1616 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001617
1618 ipmr_for_each_table(mrt, net) {
1619 v = &mrt->vif_table[0];
1620 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1621 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001622 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625 return NOTIFY_DONE;
1626}
1627
Jianjun Kongc354e122008-11-03 00:28:02 -08001628static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 .notifier_call = ipmr_device_event,
1630};
1631
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001632/* Encapsulate a packet by attaching a valid IPIP header to it.
1633 * This avoids tunnel drivers and other mess and gives us the speed so
1634 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001636static void ip_encap(struct net *net, struct sk_buff *skb,
1637 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001639 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001640 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001641
1642 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001643 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001644 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001645 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001647 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001648 iph->tos = old_iph->tos;
1649 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 iph->frag_off = 0;
1651 iph->daddr = daddr;
1652 iph->saddr = saddr;
1653 iph->protocol = IPPROTO_IPIP;
1654 iph->ihl = 5;
1655 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001656 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ip_send_check(iph);
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1660 nf_reset(skb);
1661}
1662
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001663static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1664 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001666 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
David S. Miller73186df2015-11-03 13:41:45 -05001668 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1669 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 if (unlikely(opt->optlen))
1672 ip_forward_options(skb);
1673
Eric W. Biederman13206b62015-10-07 16:48:35 -05001674 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001677/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Patrick McHardy0c122952010-04-13 05:03:22 +00001679static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1680 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001682 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001683 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct net_device *dev;
1685 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001686 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int encap = 0;
1688
Ian Morris51456b22015-04-03 09:17:26 +01001689 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto out_free;
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (vif->flags & VIFF_REGISTER) {
1693 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001694 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001695 vif->dev->stats.tx_bytes += skb->len;
1696 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001697 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001698 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001701 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001702 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001703 vif->remote, vif->local,
1704 0, 0,
1705 IPPROTO_IPIP,
1706 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001707 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 goto out_free;
1709 encap = sizeof(struct iphdr);
1710 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001711 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001712 0, 0,
1713 IPPROTO_IPIP,
1714 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001715 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto out_free;
1717 }
1718
Changli Gaod8d1f302010-06-10 23:31:35 -07001719 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Changli Gaod8d1f302010-06-10 23:31:35 -07001721 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001723 * allow to send ICMP, so that packets will disappear
1724 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
David S. Miller73186df2015-11-03 13:41:45 -05001726 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ip_rt_put(rt);
1728 goto out_free;
1729 }
1730
Changli Gaod8d1f302010-06-10 23:31:35 -07001731 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001734 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 goto out_free;
1736 }
1737
1738 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001739 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Eric Dumazetadf30902009-06-02 05:19:30 +00001741 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001742 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001743 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001746 * What do we do with netfilter? -- RR
1747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001749 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001751 vif->dev->stats.tx_packets++;
1752 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
1755 IPCB(skb)->flags |= IPSKB_FORWARDED;
1756
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001757 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * not only before forwarding, but after forwarding on all output
1759 * interfaces. It is clear, if mrouter runs a multicasting
1760 * program, it should receive packets not depending to what interface
1761 * program is joined.
1762 * If we will not make it, the program will have to join on all
1763 * interfaces. On the other hand, multihoming host (or router, but
1764 * not mrouter) cannot join to more than one interface - it will
1765 * result in receiving multiple packets.
1766 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001767 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1768 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 ipmr_forward_finish);
1770 return;
1771
1772out_free:
1773 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Patrick McHardy0c122952010-04-13 05:03:22 +00001776static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001779
1780 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1781 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
1783 }
1784 return ct;
1785}
1786
1787/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001788static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1789 struct sk_buff *skb, struct mfc_cache *cache,
1790 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 int psend = -1;
1793 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001794 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 vif = cache->mfc_parent;
1797 cache->mfc_un.res.pkt++;
1798 cache->mfc_un.res.bytes += skb->len;
1799
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001800 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001801 struct mfc_cache *cache_proxy;
1802
1803 /* For an (*,G) entry, we only check that the incomming
1804 * interface is part of the static tree.
1805 */
1806 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1807 if (cache_proxy &&
1808 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1809 goto forward;
1810 }
1811
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001812 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001813 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001814 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001816 * Very complicated situation...
1817 *
1818 * The best workaround until routing daemons will be
1819 * fixed is not to redistribute packet, if it was
1820 * send through wrong interface. It means, that
1821 * multicast applications WILL NOT work for
1822 * (S,G), which have default multicast route pointing
1823 * to wrong oif. In any case, it is not a good
1824 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
1826 goto dont_forward;
1827 }
1828
1829 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Patrick McHardy0c122952010-04-13 05:03:22 +00001831 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001833 * so that we cannot check that packet arrived on an oif.
1834 * It is bad, but otherwise we would need to move pretty
1835 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001837 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001838 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001839 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1841 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001842 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844 goto dont_forward;
1845 }
1846
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001847forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001848 mrt->vif_table[vif].pkt_in++;
1849 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001851 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001852 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1853 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001854 if (true_vifi >= 0 &&
1855 true_vifi != cache->mfc_parent &&
1856 ip_hdr(skb)->ttl >
1857 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1858 /* It's an (*,*) entry and the packet is not coming from
1859 * the upstream: forward the packet to the upstream
1860 * only.
1861 */
1862 psend = cache->mfc_parent;
1863 goto last_forward;
1864 }
1865 goto dont_forward;
1866 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001867 for (ct = cache->mfc_un.res.maxvif - 1;
1868 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001869 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001870 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1871 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001872 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (psend != -1) {
1874 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001877 ipmr_queue_xmit(net, mrt, skb2, cache,
1878 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001880 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001883last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (psend != -1) {
1885 if (local) {
1886 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001889 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001891 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001892 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 }
1895
1896dont_forward:
1897 if (!local)
1898 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
David S. Miller417da662011-05-03 19:42:43 -07001901static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001902{
David S. Miller417da662011-05-03 19:42:43 -07001903 struct rtable *rt = skb_rtable(skb);
1904 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001905 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001906 .daddr = iph->daddr,
1907 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001908 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001909 .flowi4_oif = (rt_is_output_route(rt) ?
1910 skb->dev->ifindex : 0),
1911 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001912 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001913 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001914 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001915 };
1916 struct mr_table *mrt;
1917 int err;
1918
David S. Millerda919812011-03-12 02:04:50 -05001919 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001920 if (err)
1921 return ERR_PTR(err);
1922 return mrt;
1923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001925/* Multicast packets for forwarding arrive here
1926 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928int ip_mr_input(struct sk_buff *skb)
1929{
1930 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001931 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001932 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001933 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001936 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001938 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 goto dont_forward;
1940
David S. Miller417da662011-05-03 19:42:43 -07001941 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001942 if (IS_ERR(mrt)) {
1943 kfree_skb(skb);
1944 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001947 if (IPCB(skb)->opt.router_alert) {
1948 if (ip_call_ra_chain(skb))
1949 return 0;
1950 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1951 /* IGMPv1 (and broken IGMPv2 implementations sort of
1952 * Cisco IOS <= 11.2(8)) do not put router alert
1953 * option to IGMP packets destined to routable
1954 * groups. It is very bad, because it means
1955 * that we can forward NO IGMP messages.
1956 */
1957 struct sock *mroute_sk;
1958
1959 mroute_sk = rcu_dereference(mrt->mroute_sk);
1960 if (mroute_sk) {
1961 nf_reset(skb);
1962 raw_rcv(mroute_sk, skb);
1963 return 0;
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966 }
1967
Eric Dumazeta8c94862010-10-01 16:15:08 +00001968 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001969 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001970 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001971 int vif = ipmr_find_vif(mrt, skb->dev);
1972
1973 if (vif >= 0)
1974 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1975 vif);
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001978 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01001979 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 int vif;
1981
1982 if (local) {
1983 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1984 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01001985 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 skb = skb2;
1988 }
1989
Eric Dumazeta8c94862010-10-01 16:15:08 +00001990 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001991 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001993 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 read_unlock(&mrt_lock);
1995
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001996 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998 read_unlock(&mrt_lock);
1999 kfree_skb(skb);
2000 return -ENODEV;
2001 }
2002
Eric Dumazeta8c94862010-10-01 16:15:08 +00002003 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002004 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 read_unlock(&mrt_lock);
2006
2007 if (local)
2008 return ip_local_deliver(skb);
2009
2010 return 0;
2011
2012dont_forward:
2013 if (local)
2014 return ip_local_deliver(skb);
2015 kfree_skb(skb);
2016 return 0;
2017}
2018
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002019#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002020/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002021int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002022{
2023 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002024 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002025 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002026
2027 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2028 goto drop;
2029
2030 pim = igmp_hdr(skb);
2031
David S. Miller417da662011-05-03 19:42:43 -07002032 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002033 if (IS_ERR(mrt))
2034 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002035 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002036 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2037 goto drop;
2038
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002039 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002040drop:
2041 kfree_skb(skb);
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return 0;
2044}
2045#endif
2046
2047#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002048static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002051 struct net *net = dev_net(skb->dev);
2052 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002054 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 goto drop;
2056
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002057 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002058 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2059 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002060 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002061 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 goto drop;
2063
David S. Miller417da662011-05-03 19:42:43 -07002064 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002065 if (IS_ERR(mrt))
2066 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002067 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002068drop:
2069 kfree_skb(skb);
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072}
2073#endif
2074
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002075static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2076 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078 int ct;
2079 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002080 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002081 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Nicolas Dichtel74381892010-03-25 23:45:35 +00002083 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002084 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002085 return -ENOENT;
2086
Thomas Graf92a395e2012-06-26 23:36:13 +00002087 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2088 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2089 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Thomas Graf92a395e2012-06-26 23:36:13 +00002091 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2092 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002095 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002096 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2097 nla_nest_cancel(skb, mp_attr);
2098 return -EMSGSIZE;
2099 }
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 nhp->rtnh_flags = 0;
2102 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002103 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 nhp->rtnh_len = sizeof(*nhp);
2105 }
2106 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002107
2108 nla_nest_end(skb, mp_attr);
2109
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002110 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2111 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2112 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2113 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2114 return -EMSGSIZE;
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 rtm->rtm_type = RTN_MULTICAST;
2117 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
David S. Miller9a1b9492011-05-04 12:18:54 -07002120int ipmr_get_route(struct net *net, struct sk_buff *skb,
2121 __be32 saddr, __be32 daddr,
2122 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002125 struct mr_table *mrt;
2126 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002128 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002129 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002130 return -ENOENT;
2131
Eric Dumazeta8c94862010-10-01 16:15:08 +00002132 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002133 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002134 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002135 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002137 if (vif >= 0)
2138 cache = ipmr_cache_find_any(mrt, daddr, vif);
2139 }
Ian Morris51456b22015-04-03 09:17:26 +01002140 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002141 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002142 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002144 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002147 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return -EAGAIN;
2149 }
2150
2151 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002152 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002153 if (dev)
2154 vif = ipmr_find_vif(mrt, dev);
2155 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002157 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return -ENODEV;
2159 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002160 skb2 = skb_clone(skb, GFP_ATOMIC);
2161 if (!skb2) {
2162 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002163 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002164 return -ENOMEM;
2165 }
2166
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002167 skb_push(skb2, sizeof(struct iphdr));
2168 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002169 iph = ip_hdr(skb2);
2170 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002171 iph->saddr = saddr;
2172 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002173 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002174 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002176 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return err;
2178 }
2179
Eric Dumazeta8c94862010-10-01 16:15:08 +00002180 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002181 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002183 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return err;
2185}
2186
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002187static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002188 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2189 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002190{
2191 struct nlmsghdr *nlh;
2192 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002193 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002194
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002195 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002196 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002197 return -EMSGSIZE;
2198
2199 rtm = nlmsg_data(nlh);
2200 rtm->rtm_family = RTNL_FAMILY_IPMR;
2201 rtm->rtm_dst_len = 32;
2202 rtm->rtm_src_len = 32;
2203 rtm->rtm_tos = 0;
2204 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002205 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2206 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002207 rtm->rtm_type = RTN_MULTICAST;
2208 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002209 if (c->mfc_flags & MFC_STATIC)
2210 rtm->rtm_protocol = RTPROT_STATIC;
2211 else
2212 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002213 rtm->rtm_flags = 0;
2214
Jiri Benc930345e2015-03-29 16:59:25 +02002215 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2216 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002217 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002218 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2219 /* do not break the dump if cache is unresolved */
2220 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002221 goto nla_put_failure;
2222
Johannes Berg053c0952015-01-16 22:09:00 +01002223 nlmsg_end(skb, nlh);
2224 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002225
2226nla_put_failure:
2227 nlmsg_cancel(skb, nlh);
2228 return -EMSGSIZE;
2229}
2230
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002231static size_t mroute_msgsize(bool unresolved, int maxvif)
2232{
2233 size_t len =
2234 NLMSG_ALIGN(sizeof(struct rtmsg))
2235 + nla_total_size(4) /* RTA_TABLE */
2236 + nla_total_size(4) /* RTA_SRC */
2237 + nla_total_size(4) /* RTA_DST */
2238 ;
2239
2240 if (!unresolved)
2241 len = len
2242 + nla_total_size(4) /* RTA_IIF */
2243 + nla_total_size(0) /* RTA_MULTIPATH */
2244 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2245 /* RTA_MFC_STATS */
2246 + nla_total_size(sizeof(struct rta_mfc_stats))
2247 ;
2248
2249 return len;
2250}
2251
2252static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2253 int cmd)
2254{
2255 struct net *net = read_pnet(&mrt->net);
2256 struct sk_buff *skb;
2257 int err = -ENOBUFS;
2258
2259 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2260 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002261 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002262 goto errout;
2263
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002264 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002265 if (err < 0)
2266 goto errout;
2267
2268 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2269 return;
2270
2271errout:
2272 kfree_skb(skb);
2273 if (err < 0)
2274 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2275}
2276
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002277static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2278{
2279 struct net *net = sock_net(skb->sk);
2280 struct mr_table *mrt;
2281 struct mfc_cache *mfc;
2282 unsigned int t = 0, s_t;
2283 unsigned int h = 0, s_h;
2284 unsigned int e = 0, s_e;
2285
2286 s_t = cb->args[0];
2287 s_h = cb->args[1];
2288 s_e = cb->args[2];
2289
Eric Dumazeta8c94862010-10-01 16:15:08 +00002290 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002291 ipmr_for_each_table(mrt, net) {
2292 if (t < s_t)
2293 goto next_table;
2294 if (t > s_t)
2295 s_h = 0;
2296 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002297 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002298 if (e < s_e)
2299 goto next_entry;
2300 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002301 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002302 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002303 mfc, RTM_NEWROUTE,
2304 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002305 goto done;
2306next_entry:
2307 e++;
2308 }
2309 e = s_e = 0;
2310 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002311 spin_lock_bh(&mfc_unres_lock);
2312 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2313 if (e < s_e)
2314 goto next_entry2;
2315 if (ipmr_fill_mroute(mrt, skb,
2316 NETLINK_CB(cb->skb).portid,
2317 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002318 mfc, RTM_NEWROUTE,
2319 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002320 spin_unlock_bh(&mfc_unres_lock);
2321 goto done;
2322 }
2323next_entry2:
2324 e++;
2325 }
2326 spin_unlock_bh(&mfc_unres_lock);
2327 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002328 s_h = 0;
2329next_table:
2330 t++;
2331 }
2332done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002333 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002334
2335 cb->args[2] = e;
2336 cb->args[1] = h;
2337 cb->args[0] = t;
2338
2339 return skb->len;
2340}
2341
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002342#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002343/* The /proc interfaces to multicast routing :
2344 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 */
2346struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002347 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002348 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 int ct;
2350};
2351
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002352static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2353 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 loff_t pos)
2355{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002356 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002357
2358 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2359 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002361 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002362 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
2364 return NULL;
2365}
2366
2367static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002368 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002370 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002371 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002372 struct mr_table *mrt;
2373
2374 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002375 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002376 return ERR_PTR(-ENOENT);
2377
2378 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002381 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 : SEQ_START_TOKEN;
2383}
2384
2385static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2386{
2387 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002388 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002389 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 ++*pos;
2392 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002393 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002394
Patrick McHardy0c122952010-04-13 05:03:22 +00002395 while (++iter->ct < mrt->maxvif) {
2396 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002398 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400 return NULL;
2401}
2402
2403static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002404 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406 read_unlock(&mrt_lock);
2407}
2408
2409static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2410{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002411 struct ipmr_vif_iter *iter = seq->private;
2412 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002415 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2417 } else {
2418 const struct vif_device *vif = v;
2419 const char *name = vif->dev ? vif->dev->name : "none";
2420
2421 seq_printf(seq,
2422 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002423 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002424 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 vif->bytes_out, vif->pkt_out,
2426 vif->flags, vif->local, vif->remote);
2427 }
2428 return 0;
2429}
2430
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002431static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 .start = ipmr_vif_seq_start,
2433 .next = ipmr_vif_seq_next,
2434 .stop = ipmr_vif_seq_stop,
2435 .show = ipmr_vif_seq_show,
2436};
2437
2438static int ipmr_vif_open(struct inode *inode, struct file *file)
2439{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002440 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2441 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
Arjan van de Ven9a321442007-02-12 00:55:35 -08002444static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 .owner = THIS_MODULE,
2446 .open = ipmr_vif_open,
2447 .read = seq_read,
2448 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002449 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450};
2451
2452struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002453 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002454 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002455 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 int ct;
2457};
2458
2459
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002460static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2461 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002463 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct mfc_cache *mfc;
2465
Eric Dumazeta8c94862010-10-01 16:15:08 +00002466 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002467 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002468 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002469 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002470 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002472 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002473 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002476 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002477 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002478 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return mfc;
2480 spin_unlock_bh(&mfc_unres_lock);
2481
2482 it->cache = NULL;
2483 return NULL;
2484}
2485
2486
2487static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2488{
2489 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002490 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002491 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002492
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002493 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002494 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002495 return ERR_PTR(-ENOENT);
2496
2497 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 it->cache = NULL;
2499 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002500 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 : SEQ_START_TOKEN;
2502}
2503
2504static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2505{
2506 struct mfc_cache *mfc = v;
2507 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002508 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002509 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 ++*pos;
2512
2513 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002514 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Patrick McHardy862465f2010-04-13 05:03:21 +00002516 if (mfc->list.next != it->cache)
2517 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002518
Patrick McHardy0c122952010-04-13 05:03:22 +00002519 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 goto end_of_list;
2521
Patrick McHardy0c122952010-04-13 05:03:22 +00002522 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002525 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002526 if (list_empty(it->cache))
2527 continue;
2528 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530
2531 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002532 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002533 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002537 if (!list_empty(it->cache))
2538 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002540end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 spin_unlock_bh(&mfc_unres_lock);
2542 it->cache = NULL;
2543
2544 return NULL;
2545}
2546
2547static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2548{
2549 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002550 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Patrick McHardy0c122952010-04-13 05:03:22 +00002552 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002554 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002555 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
2558static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2559{
2560 int n;
2561
2562 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002563 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2565 } else {
2566 const struct mfc_cache *mfc = v;
2567 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002568 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002569
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002570 seq_printf(seq, "%08X %08X %-3hd",
2571 (__force u32) mfc->mfc_mcastgrp,
2572 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002573 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Patrick McHardy0c122952010-04-13 05:03:22 +00002575 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002576 seq_printf(seq, " %8lu %8lu %8lu",
2577 mfc->mfc_un.res.pkt,
2578 mfc->mfc_un.res.bytes,
2579 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002580 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002581 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002582 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002583 mfc->mfc_un.res.ttls[n] < 255)
2584 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002585 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 n, mfc->mfc_un.res.ttls[n]);
2587 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002588 } else {
2589 /* unresolved mfc_caches don't contain
2590 * pkt, bytes and wrong_if values
2591 */
2592 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594 seq_putc(seq, '\n');
2595 }
2596 return 0;
2597}
2598
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002599static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 .start = ipmr_mfc_seq_start,
2601 .next = ipmr_mfc_seq_next,
2602 .stop = ipmr_mfc_seq_stop,
2603 .show = ipmr_mfc_seq_show,
2604};
2605
2606static int ipmr_mfc_open(struct inode *inode, struct file *file)
2607{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002608 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2609 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
Arjan van de Ven9a321442007-02-12 00:55:35 -08002612static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 .owner = THIS_MODULE,
2614 .open = ipmr_mfc_open,
2615 .read = seq_read,
2616 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002617 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002622static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002624 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625};
2626#endif
2627
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002628/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002629static int __net_init ipmr_net_init(struct net *net)
2630{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002631 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002632
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002633 err = ipmr_rules_init(net);
2634 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002635 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002636
2637#ifdef CONFIG_PROC_FS
2638 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002639 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002640 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002641 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002642 goto proc_cache_fail;
2643#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002644 return 0;
2645
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002646#ifdef CONFIG_PROC_FS
2647proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002648 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002649proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002650 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002651#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002652fail:
2653 return err;
2654}
2655
2656static void __net_exit ipmr_net_exit(struct net *net)
2657{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002658#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002659 remove_proc_entry("ip_mr_cache", net->proc_net);
2660 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002661#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002662 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002663}
2664
2665static struct pernet_operations ipmr_net_ops = {
2666 .init = ipmr_net_init,
2667 .exit = ipmr_net_exit,
2668};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002669
Wang Chen03d2f892008-07-03 12:13:36 +08002670int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
Wang Chen03d2f892008-07-03 12:13:36 +08002672 int err;
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2675 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002676 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002677 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002678
Benjamin Therycf958ae32009-01-22 04:56:16 +00002679 err = register_pernet_subsys(&ipmr_net_ops);
2680 if (err)
2681 goto reg_pernet_fail;
2682
Wang Chen03d2f892008-07-03 12:13:36 +08002683 err = register_netdevice_notifier(&ip_mr_notifier);
2684 if (err)
2685 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002686#ifdef CONFIG_IP_PIMSM_V2
2687 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002688 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002689 err = -EAGAIN;
2690 goto add_proto_fail;
2691 }
2692#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002693 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2694 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002695 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002696
Tom Goff403dbb92009-06-14 03:16:13 -07002697#ifdef CONFIG_IP_PIMSM_V2
2698add_proto_fail:
2699 unregister_netdevice_notifier(&ip_mr_notifier);
2700#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002701reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002702 unregister_pernet_subsys(&ipmr_net_ops);
2703reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002704 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002705 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}