blob: a2d248d9c35c872e332b868a1f3ad46b2a2fddd8 [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 McHardy0c122952010-04-13 05:03:22 +000070struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000071 struct list_head list;
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -050072 possible_net_t net;
Patrick McHardyf0ad0862010-04-13 05:03:23 +000073 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000074 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000075 struct timer_list ipmr_expire_timer;
76 struct list_head mfc_unres_queue;
77 struct list_head mfc_cache_array[MFC_LINES];
78 struct vif_device vif_table[MAXVIFS];
79 int maxvif;
80 atomic_t cache_resolve_queue_len;
Joe Perches53d68412012-11-25 09:35:30 +000081 bool mroute_do_assert;
82 bool mroute_do_pim;
Patrick McHardy0c122952010-04-13 05:03:22 +000083 int mroute_reg_vif_num;
Patrick McHardy0c122952010-04-13 05:03:22 +000084};
85
Patrick McHardyf0ad0862010-04-13 05:03:23 +000086struct ipmr_rule {
87 struct fib_rule common;
88};
89
90struct ipmr_result {
91 struct mr_table *mrt;
92};
93
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +010094static inline bool pimsm_enabled(void)
95{
96 return IS_BUILTIN(CONFIG_IP_PIMSM_V1) || IS_BUILTIN(CONFIG_IP_PIMSM_V2);
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000100 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
102
103static DEFINE_RWLOCK(mrt_lock);
104
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100105/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Patrick McHardy0c122952010-04-13 05:03:22 +0000107#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* Special spinlock for queue of unresolved entries */
110static DEFINE_SPINLOCK(mfc_unres_lock);
111
112/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000113 * entries is changed only in process context and protected
114 * with weak lock mrt_lock. Queue of unresolved entries is protected
115 * with strong spinlock mfc_unres_lock.
116 *
117 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119
Christoph Lametere18b8902006-12-06 20:33:20 -0800120static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000122static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000123static void ipmr_free_table(struct mr_table *mrt);
124
Rami Rosenc4854ec2013-07-20 15:09:28 +0300125static void ip_mr_forward(struct net *net, struct mr_table *mrt,
126 struct sk_buff *skb, struct mfc_cache *cache,
127 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000128static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000129 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200130static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
131 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000132static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
133 int cmd);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000134static void mroute_clean_tables(struct mr_table *mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000135static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000137#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
138#define ipmr_for_each_table(mrt, net) \
139 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
140
141static struct mr_table *ipmr_get_table(struct net *net, u32 id)
142{
143 struct mr_table *mrt;
144
145 ipmr_for_each_table(mrt, net) {
146 if (mrt->id == id)
147 return mrt;
148 }
149 return NULL;
150}
151
David S. Millerda919812011-03-12 02:04:50 -0500152static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000153 struct mr_table **mrt)
154{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000155 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100156 struct ipmr_result res;
157 struct fib_lookup_arg arg = {
158 .result = &res,
159 .flags = FIB_LOOKUP_NOREF,
160 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000161
David S. Millerda919812011-03-12 02:04:50 -0500162 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
163 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000164 if (err < 0)
165 return err;
166 *mrt = res.mrt;
167 return 0;
168}
169
170static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
171 int flags, struct fib_lookup_arg *arg)
172{
173 struct ipmr_result *res = arg->result;
174 struct mr_table *mrt;
175
176 switch (rule->action) {
177 case FR_ACT_TO_TBL:
178 break;
179 case FR_ACT_UNREACHABLE:
180 return -ENETUNREACH;
181 case FR_ACT_PROHIBIT:
182 return -EACCES;
183 case FR_ACT_BLACKHOLE:
184 default:
185 return -EINVAL;
186 }
187
188 mrt = ipmr_get_table(rule->fr_net, rule->table);
Ian Morris51456b22015-04-03 09:17:26 +0100189 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000190 return -EAGAIN;
191 res->mrt = mrt;
192 return 0;
193}
194
195static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
196{
197 return 1;
198}
199
200static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
201 FRA_GENERIC_POLICY,
202};
203
204static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
205 struct fib_rule_hdr *frh, struct nlattr **tb)
206{
207 return 0;
208}
209
210static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
211 struct nlattr **tb)
212{
213 return 1;
214}
215
216static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
217 struct fib_rule_hdr *frh)
218{
219 frh->dst_len = 0;
220 frh->src_len = 0;
221 frh->tos = 0;
222 return 0;
223}
224
Andi Kleen04a6f822012-10-04 17:12:11 -0700225static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200226 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000227 .rule_size = sizeof(struct ipmr_rule),
228 .addr_size = sizeof(u32),
229 .action = ipmr_rule_action,
230 .match = ipmr_rule_match,
231 .configure = ipmr_rule_configure,
232 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000233 .fill = ipmr_rule_fill,
234 .nlgroup = RTNLGRP_IPV4_RULE,
235 .policy = ipmr_rule_policy,
236 .owner = THIS_MODULE,
237};
238
239static int __net_init ipmr_rules_init(struct net *net)
240{
241 struct fib_rules_ops *ops;
242 struct mr_table *mrt;
243 int err;
244
245 ops = fib_rules_register(&ipmr_rules_ops_template, net);
246 if (IS_ERR(ops))
247 return PTR_ERR(ops);
248
249 INIT_LIST_HEAD(&net->ipv4.mr_tables);
250
251 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100252 if (IS_ERR(mrt)) {
253 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000254 goto err1;
255 }
256
257 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
258 if (err < 0)
259 goto err2;
260
261 net->ipv4.mr_rules_ops = ops;
262 return 0;
263
264err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700265 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000266err1:
267 fib_rules_unregister(ops);
268 return err;
269}
270
271static void __net_exit ipmr_rules_exit(struct net *net)
272{
273 struct mr_table *mrt, *next;
274
WANG Conged785302015-03-31 11:01:45 -0700275 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000276 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
277 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000278 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000279 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000280 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700281 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000282}
283#else
284#define ipmr_for_each_table(mrt, net) \
285 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
286
287static struct mr_table *ipmr_get_table(struct net *net, u32 id)
288{
289 return net->ipv4.mrt;
290}
291
David S. Millerda919812011-03-12 02:04:50 -0500292static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000293 struct mr_table **mrt)
294{
295 *mrt = net->ipv4.mrt;
296 return 0;
297}
298
299static int __net_init ipmr_rules_init(struct net *net)
300{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100301 struct mr_table *mrt;
302
303 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
304 if (IS_ERR(mrt))
305 return PTR_ERR(mrt);
306 net->ipv4.mrt = mrt;
307 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000308}
309
310static void __net_exit ipmr_rules_exit(struct net *net)
311{
WANG Conged785302015-03-31 11:01:45 -0700312 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000313 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700314 net->ipv4.mrt = NULL;
315 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000316}
317#endif
318
319static struct mr_table *ipmr_new_table(struct net *net, u32 id)
320{
321 struct mr_table *mrt;
322 unsigned int i;
323
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100324 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
325 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
326 return ERR_PTR(-EINVAL);
327
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000328 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100329 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000330 return mrt;
331
332 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100333 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100334 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200335 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000336 mrt->id = id;
337
338 /* Forwarding cache */
339 for (i = 0; i < MFC_LINES; i++)
340 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
341
342 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
343
344 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
345 (unsigned long)mrt);
346
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000347 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000348#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
349 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
350#endif
351 return mrt;
352}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000354static void ipmr_free_table(struct mr_table *mrt)
355{
356 del_timer_sync(&mrt->ipmr_expire_timer);
357 mroute_clean_tables(mrt);
358 kfree(mrt);
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
362
Wang Chend6070322008-07-14 20:55:26 -0700363static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
364{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000365 struct net *net = dev_net(dev);
366
Wang Chend6070322008-07-14 20:55:26 -0700367 dev_close(dev);
368
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000369 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700370 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800371 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700372 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700373 struct ip_tunnel_parm p;
374
375 memset(&p, 0, sizeof(p));
376 p.iph.daddr = v->vifc_rmt_addr.s_addr;
377 p.iph.saddr = v->vifc_lcl_addr.s_addr;
378 p.iph.version = 4;
379 p.iph.ihl = 5;
380 p.iph.protocol = IPPROTO_IPIP;
381 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
382 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
383
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800384 if (ops->ndo_do_ioctl) {
385 mm_segment_t oldfs = get_fs();
386
387 set_fs(KERNEL_DS);
388 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
389 set_fs(oldfs);
390 }
Wang Chend6070322008-07-14 20:55:26 -0700391 }
392}
393
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100394/* Initialize ipmr pimreg/tunnel in_device */
395static bool ipmr_init_vif_indev(const struct net_device *dev)
396{
397 struct in_device *in_dev;
398
399 ASSERT_RTNL();
400
401 in_dev = __in_dev_get_rtnl(dev);
402 if (!in_dev)
403 return false;
404 ipv4_devconf_setall(in_dev);
405 neigh_parms_data_state_setall(in_dev->arp_parms);
406 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
407
408 return true;
409}
410
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100411static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct net_device *dev;
414
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000415 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800418 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int err;
420 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 memset(&p, 0, sizeof(p));
424 p.iph.daddr = v->vifc_rmt_addr.s_addr;
425 p.iph.saddr = v->vifc_lcl_addr.s_addr;
426 p.iph.version = 4;
427 p.iph.ihl = 5;
428 p.iph.protocol = IPPROTO_IPIP;
429 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800430 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800432 if (ops->ndo_do_ioctl) {
433 mm_segment_t oldfs = get_fs();
434
435 set_fs(KERNEL_DS);
436 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
437 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000438 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800439 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 dev = NULL;
442
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000443 if (err == 0 &&
444 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100446 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (dev_open(dev))
449 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700450 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 }
453 return dev;
454
455failure:
456 /* allow the register to be completed before unregistering. */
457 rtnl_unlock();
458 rtnl_lock();
459
460 unregister_netdevice(dev);
461 return NULL;
462}
463
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100464#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000465static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000467 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000468 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500469 struct flowi4 fl4 = {
470 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700471 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500472 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000473 };
474 int err;
475
David S. Millerda919812011-03-12 02:04:50 -0500476 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000477 if (err < 0) {
478 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000479 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000480 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700483 dev->stats.tx_bytes += skb->len;
484 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000485 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 read_unlock(&mrt_lock);
487 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000488 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200491static int reg_vif_get_iflink(const struct net_device *dev)
492{
493 return 0;
494}
495
Stephen Hemminger007c3832008-11-20 20:28:35 -0800496static const struct net_device_ops reg_vif_netdev_ops = {
497 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200498 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800499};
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static void reg_vif_setup(struct net_device *dev)
502{
503 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800504 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530506 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700508 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000511static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000514 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000516 if (mrt->id == RT_TABLE_DEFAULT)
517 sprintf(name, "pimreg");
518 else
519 sprintf(name, "pimreg%u", mrt->id);
520
Tom Gundersenc835a672014-07-14 16:37:24 +0200521 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Ian Morris51456b22015-04-03 09:17:26 +0100523 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return NULL;
525
Tom Goff403dbb92009-06-14 03:16:13 -0700526 dev_net_set(dev, net);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (register_netdevice(dev)) {
529 free_netdev(dev);
530 return NULL;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100533 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (dev_open(dev))
536 goto failure;
537
Wang Chen7dc00c82008-07-14 20:56:34 -0700538 dev_hold(dev);
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return dev;
541
542failure:
543 /* allow the register to be completed before unregistering. */
544 rtnl_unlock();
545 rtnl_lock();
546
547 unregister_netdevice(dev);
548 return NULL;
549}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100550
551/* called with rcu_read_lock() */
552static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
553 unsigned int pimlen)
554{
555 struct net_device *reg_dev = NULL;
556 struct iphdr *encap;
557
558 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100559 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100560 * a. packet is really sent to a multicast group
561 * b. packet is not a NULL-REGISTER
562 * c. packet is not truncated
563 */
564 if (!ipv4_is_multicast(encap->daddr) ||
565 encap->tot_len == 0 ||
566 ntohs(encap->tot_len) + pimlen > skb->len)
567 return 1;
568
569 read_lock(&mrt_lock);
570 if (mrt->mroute_reg_vif_num >= 0)
571 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
572 read_unlock(&mrt_lock);
573
574 if (!reg_dev)
575 return 1;
576
577 skb->mac_header = skb->network_header;
578 skb_pull(skb, (u8 *)encap - skb->data);
579 skb_reset_network_header(skb);
580 skb->protocol = htons(ETH_P_IP);
581 skb->ip_summed = CHECKSUM_NONE;
582
583 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
584
585 netif_rx(skb);
586
587 return NET_RX_SUCCESS;
588}
589#else
590static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
591{
592 return NULL;
593}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594#endif
595
Ben Hutchings2c530402012-07-10 10:55:09 +0000596/**
597 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700598 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000600static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000601 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct vif_device *v;
604 struct net_device *dev;
605 struct in_device *in_dev;
606
Patrick McHardy0c122952010-04-13 05:03:22 +0000607 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return -EADDRNOTAVAIL;
609
Patrick McHardy0c122952010-04-13 05:03:22 +0000610 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 write_lock_bh(&mrt_lock);
613 dev = v->dev;
614 v->dev = NULL;
615
616 if (!dev) {
617 write_unlock_bh(&mrt_lock);
618 return -EADDRNOTAVAIL;
619 }
620
Patrick McHardy0c122952010-04-13 05:03:22 +0000621 if (vifi == mrt->mroute_reg_vif_num)
622 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000624 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000626
627 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000628 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
630 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000631 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 write_unlock_bh(&mrt_lock);
635
636 dev_set_allmulti(dev, -1);
637
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000638 in_dev = __in_dev_get_rtnl(dev);
639 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700640 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000641 inet_netconf_notify_devconf(dev_net(dev),
642 NETCONFA_MC_FORWARDING,
643 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ip_rt_multicast_event(in_dev);
645 }
646
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000647 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000648 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 dev_put(dev);
651 return 0;
652}
653
Eric Dumazeta8c94862010-10-01 16:15:08 +0000654static void ipmr_cache_free_rcu(struct rcu_head *head)
655{
656 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
657
658 kmem_cache_free(mrt_cachep, c);
659}
660
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000661static inline void ipmr_cache_free(struct mfc_cache *c)
662{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000663 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000667 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000669static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200671 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700673 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Patrick McHardy0c122952010-04-13 05:03:22 +0000675 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Jianjun Kongc354e122008-11-03 00:28:02 -0800677 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700678 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
680 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000681 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000683 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700684 e->error = -ETIMEDOUT;
685 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700686
Eric W. Biederman15e47302012-09-07 20:12:54 +0000687 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000688 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000693 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Patrick McHardye258beb2010-04-13 05:03:19 +0000696/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000697static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Patrick McHardy0c122952010-04-13 05:03:22 +0000699 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 unsigned long now;
701 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000702 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000705 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return;
707 }
708
Patrick McHardy0c122952010-04-13 05:03:22 +0000709 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto out;
711
712 now = jiffies;
713 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Patrick McHardy0c122952010-04-13 05:03:22 +0000715 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (time_after(c->mfc_un.unres.expires, now)) {
717 unsigned long interval = c->mfc_un.unres.expires - now;
718 if (interval < expires)
719 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 continue;
721 }
722
Patrick McHardy862465f2010-04-13 05:03:21 +0000723 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000724 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000725 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
Patrick McHardy0c122952010-04-13 05:03:22 +0000728 if (!list_empty(&mrt->mfc_unres_queue))
729 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731out:
732 spin_unlock(&mfc_unres_lock);
733}
734
735/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000736static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000737 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
739 int vifi;
740
741 cache->mfc_un.res.minvif = MAXVIFS;
742 cache->mfc_un.res.maxvif = 0;
743 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
744
Patrick McHardy0c122952010-04-13 05:03:22 +0000745 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
746 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000747 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
749 if (cache->mfc_un.res.minvif > vifi)
750 cache->mfc_un.res.minvif = vifi;
751 if (cache->mfc_un.res.maxvif <= vifi)
752 cache->mfc_un.res.maxvif = vifi + 1;
753 }
754 }
755}
756
Patrick McHardy0c122952010-04-13 05:03:22 +0000757static int vif_add(struct net *net, struct mr_table *mrt,
758 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000761 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct net_device *dev;
763 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700764 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000767 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -EADDRINUSE;
769
770 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 case VIFF_REGISTER:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100772 if (!pimsm_enabled())
773 return -EINVAL;
774 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * All the packets will be sent to the daemon
776 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000777 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000779 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (!dev)
781 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700782 err = dev_set_allmulti(dev, 1);
783 if (err) {
784 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700785 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700786 return err;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900789 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000790 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (!dev)
792 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700793 err = dev_set_allmulti(dev, 1);
794 if (err) {
795 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700796 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700797 return err;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000800 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000802 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
803 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100804 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000805 dev_put(dev);
806 return -EADDRNOTAVAIL;
807 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000808 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000809 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!dev)
812 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700813 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700814 if (err) {
815 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700816 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 break;
819 default:
820 return -EINVAL;
821 }
822
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000823 in_dev = __in_dev_get_rtnl(dev);
824 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000825 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000827 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700828 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000829 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
830 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ip_rt_multicast_event(in_dev);
832
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000833 /* Fill in the VIF structures */
834
Jianjun Kongc354e122008-11-03 00:28:02 -0800835 v->rate_limit = vifc->vifc_rate_limit;
836 v->local = vifc->vifc_lcl_addr.s_addr;
837 v->remote = vifc->vifc_rmt_addr.s_addr;
838 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (!mrtsock)
840 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800841 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 v->bytes_in = 0;
843 v->bytes_out = 0;
844 v->pkt_in = 0;
845 v->pkt_out = 0;
846 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000847 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200848 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* And finish update writing critical data */
851 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800852 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000853 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000854 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000855 if (vifi+1 > mrt->maxvif)
856 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 write_unlock_bh(&mrt_lock);
858 return 0;
859}
860
Eric Dumazeta8c94862010-10-01 16:15:08 +0000861/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000862static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000863 __be32 origin,
864 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Jianjun Kongc354e122008-11-03 00:28:02 -0800866 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct mfc_cache *c;
868
Eric Dumazeta8c94862010-10-01 16:15:08 +0000869 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000870 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
871 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000873 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000876/* Look for a (*,*,oif) entry */
877static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
878 int vifi)
879{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100880 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000881 struct mfc_cache *c;
882
883 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100884 if (c->mfc_origin == htonl(INADDR_ANY) &&
885 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000886 c->mfc_un.res.ttls[vifi] < 255)
887 return c;
888
889 return NULL;
890}
891
892/* Look for a (*,G) entry */
893static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
894 __be32 mcastgrp, int vifi)
895{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100896 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000897 struct mfc_cache *c, *proxy;
898
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100899 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000900 goto skip;
901
902 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100903 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000904 c->mfc_mcastgrp == mcastgrp) {
905 if (c->mfc_un.res.ttls[vifi] < 255)
906 return c;
907
908 /* It's ok if the vifi is part of the static tree */
909 proxy = ipmr_cache_find_any_parent(mrt,
910 c->mfc_parent);
911 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
912 return c;
913 }
914
915skip:
916 return ipmr_cache_find_any_parent(mrt, vifi);
917}
918
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100919/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000920static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Jianjun Kongc354e122008-11-03 00:28:02 -0800922 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000923
924 if (c)
925 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return c;
927}
928
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000929static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Jianjun Kongc354e122008-11-03 00:28:02 -0800931 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000932
933 if (c) {
934 skb_queue_head_init(&c->mfc_un.unres.unresolved);
935 c->mfc_un.unres.expires = jiffies + 10*HZ;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return c;
938}
939
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100940/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000941static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
942 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700945 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000947 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800948 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700949 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
951
Hong zhi guo573ce262013-03-27 06:47:04 +0000952 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000953 nlh->nlmsg_len = skb_tail_pointer(skb) -
954 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 } else {
956 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000957 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000959 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700960 e->error = -EMSGSIZE;
961 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
Thomas Graf2942e902006-08-15 00:30:25 -0700963
Eric W. Biederman15e47302012-09-07 20:12:54 +0000964 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000965 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000966 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969}
970
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100971/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
972 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100974 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000976static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000977 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300979 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100980 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct igmphdr *igmp;
982 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100983 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 int ret;
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (assert == IGMPMSG_WHOLEPKT)
987 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
988 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 skb = alloc_skb(128, GFP_ATOMIC);
990
Stephen Hemminger132adf52007-03-08 20:44:43 -0800991 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return -ENOBUFS;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (assert == IGMPMSG_WHOLEPKT) {
995 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000996 * Duplicate old header, fix ihl, length etc.
997 * And all this only to mangle msg->im_msgtype and
998 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001000 skb_push(skb, sizeof(struct iphdr));
1001 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001002 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001003 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001004 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1006 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001007 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001008 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1009 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1010 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001011 } else {
1012 /* Copy the IP header */
1013 skb_set_network_header(skb, skb->len);
1014 skb_put(skb, ihl);
1015 skb_copy_to_linear_data(skb, pkt->data, ihl);
1016 /* Flag to the kernel this is a route add */
1017 ip_hdr(skb)->protocol = 0;
1018 msg = (struct igmpmsg *)skb_network_header(skb);
1019 msg->im_vif = vifi;
1020 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1021 /* Add our header */
1022 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1023 igmp->type = assert;
1024 msg->im_msgtype = assert;
1025 igmp->code = 0;
1026 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1027 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Eric Dumazet4c968702010-10-01 16:15:01 +00001030 rcu_read_lock();
1031 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001032 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001033 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 kfree_skb(skb);
1035 return -EINVAL;
1036 }
1037
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001038 /* Deliver to mrouted */
Eric Dumazet4c968702010-10-01 16:15:01 +00001039 ret = sock_queue_rcv_skb(mroute_sk, skb);
1040 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001041 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001042 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 kfree_skb(skb);
1044 }
1045
1046 return ret;
1047}
1048
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001049/* Queue a packet for resolution. It gets locked cache entry! */
1050static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1051 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Patrick McHardy862465f2010-04-13 05:03:21 +00001053 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 int err;
1055 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001056 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001059 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001060 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001061 c->mfc_origin == iph->saddr) {
1062 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
Patrick McHardy862465f2010-04-13 05:03:21 +00001067 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001068 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001069 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001070 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 spin_unlock_bh(&mfc_unres_lock);
1072
1073 kfree_skb(skb);
1074 return -ENOBUFS;
1075 }
1076
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001077 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001078 c->mfc_parent = -1;
1079 c->mfc_origin = iph->saddr;
1080 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001082 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001083 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001084 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001085 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 out - Brad Parker
1087 */
1088 spin_unlock_bh(&mfc_unres_lock);
1089
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001090 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 kfree_skb(skb);
1092 return err;
1093 }
1094
Patrick McHardy0c122952010-04-13 05:03:22 +00001095 atomic_inc(&mrt->cache_resolve_queue_len);
1096 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001097 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
David S. Miller278554b2010-05-12 00:05:35 -07001099 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1100 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001103 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001104 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 kfree_skb(skb);
1106 err = -ENOBUFS;
1107 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001108 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 err = 0;
1110 }
1111
1112 spin_unlock_bh(&mfc_unres_lock);
1113 return err;
1114}
1115
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001116/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001118static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001121 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Jianjun Kongc354e122008-11-03 00:28:02 -08001123 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Patrick McHardy0c122952010-04-13 05:03:22 +00001125 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001127 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1128 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001129 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001130 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001131 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return 0;
1133 }
1134 }
1135 return -ENOENT;
1136}
1137
Patrick McHardy0c122952010-04-13 05:03:22 +00001138static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001139 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Patrick McHardy862465f2010-04-13 05:03:21 +00001141 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001143 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Patrick McHardya50436f22010-03-17 06:04:14 +00001145 if (mfc->mfcc_parent >= MAXVIFS)
1146 return -ENFILE;
1147
Jianjun Kongc354e122008-11-03 00:28:02 -08001148 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Patrick McHardy0c122952010-04-13 05:03:22 +00001150 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001152 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1153 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001154 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
Patrick McHardy862465f2010-04-13 05:03:21 +00001159 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 write_lock_bh(&mrt_lock);
1161 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001162 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (!mrtsock)
1164 c->mfc_flags |= MFC_STATIC;
1165 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001166 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return 0;
1168 }
1169
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001170 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001171 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return -EINVAL;
1173
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001174 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001175 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return -ENOMEM;
1177
Jianjun Kongc354e122008-11-03 00:28:02 -08001178 c->mfc_origin = mfc->mfcc_origin.s_addr;
1179 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1180 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001181 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (!mrtsock)
1183 c->mfc_flags |= MFC_STATIC;
1184
Eric Dumazeta8c94862010-10-01 16:15:08 +00001185 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001187 /* Check to see if we resolved a queued list. If so we
1188 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001190 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001192 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001193 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001195 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001196 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001197 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 break;
1199 }
1200 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001201 if (list_empty(&mrt->mfc_unres_queue))
1202 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 spin_unlock_bh(&mfc_unres_lock);
1204
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001205 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001206 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001207 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001209 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return 0;
1211}
1212
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001213/* Close the multicast socket, and clear the vif tables etc */
Patrick McHardy0c122952010-04-13 05:03:22 +00001214static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001217 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001218 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001219
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001220 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001221 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001222 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001223 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001225 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001227 /* Wipe the cache */
Patrick McHardy862465f2010-04-13 05:03:21 +00001228 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001229 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001230 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001232 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001233 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001234 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236 }
1237
Patrick McHardy0c122952010-04-13 05:03:22 +00001238 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001240 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001241 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001242 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001243 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245 spin_unlock_bh(&mfc_unres_lock);
1246 }
1247}
1248
Eric Dumazet4c968702010-10-01 16:15:01 +00001249/* called from ip_ra_control(), before an RCU grace period,
1250 * we dont need to call synchronize_rcu() here
1251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static void mrtsock_destruct(struct sock *sk)
1253{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001254 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001255 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001258 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001259 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001260 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001261 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1262 NETCONFA_IFINDEX_ALL,
1263 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001264 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001265 mroute_clean_tables(mrt);
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 rtnl_unlock();
1269}
1270
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001271/* Socket options and virtual interface manipulation. The whole
1272 * virtual interface system is a complete heap, but unfortunately
1273 * that's how BSD mrouted happens to think. Maybe one day with a proper
1274 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001276
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001277int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1278 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001280 struct net *net = sock_net(sk);
1281 int val, ret = 0, parent = 0;
1282 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct vifctl vif;
1284 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001285 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001286
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001287 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1288 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001289 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001290 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1291 ret = -EOPNOTSUPP;
1292 goto out_unlock;
1293 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001294
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001295 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001296 if (!mrt) {
1297 ret = -ENOENT;
1298 goto out_unlock;
1299 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001300 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001301 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001302 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1303 ret = -EACCES;
1304 goto out_unlock;
1305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Stephen Hemminger132adf52007-03-08 20:44:43 -08001308 switch (optname) {
1309 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001310 if (optlen != sizeof(int))
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001311 ret = -EINVAL;
1312 if (rtnl_dereference(mrt->mroute_sk))
1313 ret = -EADDRINUSE;
1314 if (ret)
1315 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001316
1317 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1318 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001319 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001320 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001321 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1322 NETCONFA_IFINDEX_ALL,
1323 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001324 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001325 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001326 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001327 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1328 ret = -EACCES;
1329 } else {
1330 /* We need to unlock here because mrtsock_destruct takes
1331 * care of rtnl itself and we can't change that due to
1332 * the IP_ROUTER_ALERT setsockopt which runs without it.
1333 */
1334 rtnl_unlock();
1335 ret = ip_ra_control(sk, 0, NULL);
1336 goto out;
1337 }
1338 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 case MRT_ADD_VIF:
1340 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001341 if (optlen != sizeof(vif)) {
1342 ret = -EINVAL;
1343 break;
1344 }
1345 if (copy_from_user(&vif, optval, sizeof(vif))) {
1346 ret = -EFAULT;
1347 break;
1348 }
1349 if (vif.vifc_vifi >= MAXVIFS) {
1350 ret = -ENFILE;
1351 break;
1352 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001353 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001354 ret = vif_add(net, mrt, &vif,
1355 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001356 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001357 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001358 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001359 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001360 /* Manipulate the forwarding caches. These live
1361 * in a sort of kernel/user symbiosis.
1362 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001363 case MRT_ADD_MFC:
1364 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001365 parent = -1;
1366 case MRT_ADD_MFC_PROXY:
1367 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001368 if (optlen != sizeof(mfc)) {
1369 ret = -EINVAL;
1370 break;
1371 }
1372 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1373 ret = -EFAULT;
1374 break;
1375 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001376 if (parent == 0)
1377 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001378 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1379 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001380 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001381 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001382 sk == rtnl_dereference(mrt->mroute_sk),
1383 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001384 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001385 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001386 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001387 if (optlen != sizeof(val)) {
1388 ret = -EINVAL;
1389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001391 if (get_user(val, (int __user *)optval)) {
1392 ret = -EFAULT;
1393 break;
1394 }
1395 mrt->mroute_do_assert = val;
1396 break;
1397 case MRT_PIM:
1398 if (!pimsm_enabled()) {
1399 ret = -ENOPROTOOPT;
1400 break;
1401 }
1402 if (optlen != sizeof(val)) {
1403 ret = -EINVAL;
1404 break;
1405 }
1406 if (get_user(val, (int __user *)optval)) {
1407 ret = -EFAULT;
1408 break;
1409 }
1410
1411 val = !!val;
1412 if (val != mrt->mroute_do_pim) {
1413 mrt->mroute_do_pim = val;
1414 mrt->mroute_do_assert = val;
1415 }
1416 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001417 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001418 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1419 ret = -ENOPROTOOPT;
1420 break;
1421 }
1422 if (optlen != sizeof(uval)) {
1423 ret = -EINVAL;
1424 break;
1425 }
1426 if (get_user(uval, (u32 __user *)optval)) {
1427 ret = -EFAULT;
1428 break;
1429 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001430
Eric Dumazet4c968702010-10-01 16:15:01 +00001431 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1432 ret = -EBUSY;
1433 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001434 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001435 if (IS_ERR(mrt))
1436 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001437 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001438 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c968702010-10-01 16:15:01 +00001439 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001440 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001441 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001442 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001443 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001445out_unlock:
1446 rtnl_unlock();
1447out:
1448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001451/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001452int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 int olr;
1455 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001456 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001457 struct mr_table *mrt;
1458
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001459 if (sk->sk_type != SOCK_RAW ||
1460 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1461 return -EOPNOTSUPP;
1462
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001463 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001464 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001465 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001467 switch (optname) {
1468 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001469 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001470 break;
1471 case MRT_PIM:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001472 if (!pimsm_enabled())
1473 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001474 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001475 break;
1476 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001477 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001478 break;
1479 default:
1480 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001481 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001482
1483 if (get_user(olr, optlen))
1484 return -EFAULT;
1485 olr = min_t(unsigned int, olr, sizeof(int));
1486 if (olr < 0)
1487 return -EINVAL;
1488 if (put_user(olr, optlen))
1489 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001490 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -EFAULT;
1492 return 0;
1493}
1494
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001495/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1497{
1498 struct sioc_sg_req sr;
1499 struct sioc_vif_req vr;
1500 struct vif_device *vif;
1501 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001502 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001503 struct mr_table *mrt;
1504
1505 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001506 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001507 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001508
Stephen Hemminger132adf52007-03-08 20:44:43 -08001509 switch (cmd) {
1510 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001511 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001512 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001513 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001514 return -EINVAL;
1515 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001516 vif = &mrt->vif_table[vr.vifi];
1517 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001518 vr.icount = vif->pkt_in;
1519 vr.ocount = vif->pkt_out;
1520 vr.ibytes = vif->bytes_in;
1521 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001523
Jianjun Kongc354e122008-11-03 00:28:02 -08001524 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001526 return 0;
1527 }
1528 read_unlock(&mrt_lock);
1529 return -EADDRNOTAVAIL;
1530 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001531 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001532 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Eric Dumazeta8c94862010-10-01 16:15:08 +00001534 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001535 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001536 if (c) {
1537 sr.pktcnt = c->mfc_un.res.pkt;
1538 sr.bytecnt = c->mfc_un.res.bytes;
1539 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001540 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001541
Jianjun Kongc354e122008-11-03 00:28:02 -08001542 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001543 return -EFAULT;
1544 return 0;
1545 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001546 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001547 return -EADDRNOTAVAIL;
1548 default:
1549 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551}
1552
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001553#ifdef CONFIG_COMPAT
1554struct compat_sioc_sg_req {
1555 struct in_addr src;
1556 struct in_addr grp;
1557 compat_ulong_t pktcnt;
1558 compat_ulong_t bytecnt;
1559 compat_ulong_t wrong_if;
1560};
1561
David S. Millerca6b8bb02011-02-03 17:24:28 -08001562struct compat_sioc_vif_req {
1563 vifi_t vifi; /* Which iface */
1564 compat_ulong_t icount;
1565 compat_ulong_t ocount;
1566 compat_ulong_t ibytes;
1567 compat_ulong_t obytes;
1568};
1569
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001570int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1571{
David S. Miller0033d5a2011-02-03 17:21:31 -08001572 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001573 struct compat_sioc_vif_req vr;
1574 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001575 struct mfc_cache *c;
1576 struct net *net = sock_net(sk);
1577 struct mr_table *mrt;
1578
1579 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001580 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001581 return -ENOENT;
1582
1583 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001584 case SIOCGETVIFCNT:
1585 if (copy_from_user(&vr, arg, sizeof(vr)))
1586 return -EFAULT;
1587 if (vr.vifi >= mrt->maxvif)
1588 return -EINVAL;
1589 read_lock(&mrt_lock);
1590 vif = &mrt->vif_table[vr.vifi];
1591 if (VIF_EXISTS(mrt, vr.vifi)) {
1592 vr.icount = vif->pkt_in;
1593 vr.ocount = vif->pkt_out;
1594 vr.ibytes = vif->bytes_in;
1595 vr.obytes = vif->bytes_out;
1596 read_unlock(&mrt_lock);
1597
1598 if (copy_to_user(arg, &vr, sizeof(vr)))
1599 return -EFAULT;
1600 return 0;
1601 }
1602 read_unlock(&mrt_lock);
1603 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001604 case SIOCGETSGCNT:
1605 if (copy_from_user(&sr, arg, sizeof(sr)))
1606 return -EFAULT;
1607
1608 rcu_read_lock();
1609 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1610 if (c) {
1611 sr.pktcnt = c->mfc_un.res.pkt;
1612 sr.bytecnt = c->mfc_un.res.bytes;
1613 sr.wrong_if = c->mfc_un.res.wrong_if;
1614 rcu_read_unlock();
1615
1616 if (copy_to_user(arg, &sr, sizeof(sr)))
1617 return -EFAULT;
1618 return 0;
1619 }
1620 rcu_read_unlock();
1621 return -EADDRNOTAVAIL;
1622 default:
1623 return -ENOIOCTLCMD;
1624 }
1625}
1626#endif
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1629{
Jiri Pirko351638e2013-05-28 01:30:21 +00001630 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001631 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001632 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 struct vif_device *v;
1634 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (event != NETDEV_UNREGISTER)
1637 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001638
1639 ipmr_for_each_table(mrt, net) {
1640 v = &mrt->vif_table[0];
1641 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1642 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001643 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646 return NOTIFY_DONE;
1647}
1648
Jianjun Kongc354e122008-11-03 00:28:02 -08001649static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 .notifier_call = ipmr_device_event,
1651};
1652
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001653/* Encapsulate a packet by attaching a valid IPIP header to it.
1654 * This avoids tunnel drivers and other mess and gives us the speed so
1655 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001657static void ip_encap(struct net *net, struct sk_buff *skb,
1658 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001660 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001661 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001662
1663 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001664 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001665 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001666 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001668 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001669 iph->tos = old_iph->tos;
1670 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 iph->frag_off = 0;
1672 iph->daddr = daddr;
1673 iph->saddr = saddr;
1674 iph->protocol = IPPROTO_IPIP;
1675 iph->ihl = 5;
1676 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001677 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 ip_send_check(iph);
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1681 nf_reset(skb);
1682}
1683
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001684static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1685 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001687 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
David S. Miller73186df2015-11-03 13:41:45 -05001689 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1690 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 if (unlikely(opt->optlen))
1693 ip_forward_options(skb);
1694
Eric W. Biederman13206b62015-10-07 16:48:35 -05001695 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001698/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Patrick McHardy0c122952010-04-13 05:03:22 +00001700static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1701 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001703 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001704 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 struct net_device *dev;
1706 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001707 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int encap = 0;
1709
Ian Morris51456b22015-04-03 09:17:26 +01001710 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto out_free;
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (vif->flags & VIFF_REGISTER) {
1714 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001715 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001716 vif->dev->stats.tx_bytes += skb->len;
1717 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001718 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001719 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001722 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001723 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001724 vif->remote, vif->local,
1725 0, 0,
1726 IPPROTO_IPIP,
1727 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001728 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto out_free;
1730 encap = sizeof(struct iphdr);
1731 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001732 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001733 0, 0,
1734 IPPROTO_IPIP,
1735 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001736 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 goto out_free;
1738 }
1739
Changli Gaod8d1f302010-06-10 23:31:35 -07001740 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Changli Gaod8d1f302010-06-10 23:31:35 -07001742 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001744 * allow to send ICMP, so that packets will disappear
1745 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 */
David S. Miller73186df2015-11-03 13:41:45 -05001747 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 ip_rt_put(rt);
1749 goto out_free;
1750 }
1751
Changli Gaod8d1f302010-06-10 23:31:35 -07001752 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001755 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 goto out_free;
1757 }
1758
1759 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001760 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Eric Dumazetadf30902009-06-02 05:19:30 +00001762 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001763 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001764 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001767 * What do we do with netfilter? -- RR
1768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001770 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001772 vif->dev->stats.tx_packets++;
1773 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
1776 IPCB(skb)->flags |= IPSKB_FORWARDED;
1777
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001778 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 * not only before forwarding, but after forwarding on all output
1780 * interfaces. It is clear, if mrouter runs a multicasting
1781 * program, it should receive packets not depending to what interface
1782 * program is joined.
1783 * If we will not make it, the program will have to join on all
1784 * interfaces. On the other hand, multihoming host (or router, but
1785 * not mrouter) cannot join to more than one interface - it will
1786 * result in receiving multiple packets.
1787 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001788 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1789 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 ipmr_forward_finish);
1791 return;
1792
1793out_free:
1794 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
Patrick McHardy0c122952010-04-13 05:03:22 +00001797static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
1799 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001800
1801 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1802 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 break;
1804 }
1805 return ct;
1806}
1807
1808/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001809static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1810 struct sk_buff *skb, struct mfc_cache *cache,
1811 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 int psend = -1;
1814 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001815 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 vif = cache->mfc_parent;
1818 cache->mfc_un.res.pkt++;
1819 cache->mfc_un.res.bytes += skb->len;
1820
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001821 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001822 struct mfc_cache *cache_proxy;
1823
1824 /* For an (*,G) entry, we only check that the incomming
1825 * interface is part of the static tree.
1826 */
1827 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1828 if (cache_proxy &&
1829 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1830 goto forward;
1831 }
1832
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001833 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001834 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001835 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001837 * Very complicated situation...
1838 *
1839 * The best workaround until routing daemons will be
1840 * fixed is not to redistribute packet, if it was
1841 * send through wrong interface. It means, that
1842 * multicast applications WILL NOT work for
1843 * (S,G), which have default multicast route pointing
1844 * to wrong oif. In any case, it is not a good
1845 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 */
1847 goto dont_forward;
1848 }
1849
1850 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Patrick McHardy0c122952010-04-13 05:03:22 +00001852 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001854 * so that we cannot check that packet arrived on an oif.
1855 * It is bad, but otherwise we would need to move pretty
1856 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001858 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001859 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001860 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1862 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001863 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865 goto dont_forward;
1866 }
1867
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001868forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001869 mrt->vif_table[vif].pkt_in++;
1870 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001872 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001873 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1874 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001875 if (true_vifi >= 0 &&
1876 true_vifi != cache->mfc_parent &&
1877 ip_hdr(skb)->ttl >
1878 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1879 /* It's an (*,*) entry and the packet is not coming from
1880 * the upstream: forward the packet to the upstream
1881 * only.
1882 */
1883 psend = cache->mfc_parent;
1884 goto last_forward;
1885 }
1886 goto dont_forward;
1887 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001888 for (ct = cache->mfc_un.res.maxvif - 1;
1889 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001890 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001891 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1892 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001893 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (psend != -1) {
1895 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001898 ipmr_queue_xmit(net, mrt, skb2, cache,
1899 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001901 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001904last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (psend != -1) {
1906 if (local) {
1907 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001910 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001912 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001913 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915 }
1916
1917dont_forward:
1918 if (!local)
1919 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
David S. Miller417da662011-05-03 19:42:43 -07001922static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001923{
David S. Miller417da662011-05-03 19:42:43 -07001924 struct rtable *rt = skb_rtable(skb);
1925 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001926 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001927 .daddr = iph->daddr,
1928 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001929 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001930 .flowi4_oif = (rt_is_output_route(rt) ?
1931 skb->dev->ifindex : 0),
1932 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001933 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001934 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001935 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001936 };
1937 struct mr_table *mrt;
1938 int err;
1939
David S. Millerda919812011-03-12 02:04:50 -05001940 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001941 if (err)
1942 return ERR_PTR(err);
1943 return mrt;
1944}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001946/* Multicast packets for forwarding arrive here
1947 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949int ip_mr_input(struct sk_buff *skb)
1950{
1951 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001952 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001953 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001954 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001957 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001959 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto dont_forward;
1961
David S. Miller417da662011-05-03 19:42:43 -07001962 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001963 if (IS_ERR(mrt)) {
1964 kfree_skb(skb);
1965 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001968 if (IPCB(skb)->opt.router_alert) {
1969 if (ip_call_ra_chain(skb))
1970 return 0;
1971 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1972 /* IGMPv1 (and broken IGMPv2 implementations sort of
1973 * Cisco IOS <= 11.2(8)) do not put router alert
1974 * option to IGMP packets destined to routable
1975 * groups. It is very bad, because it means
1976 * that we can forward NO IGMP messages.
1977 */
1978 struct sock *mroute_sk;
1979
1980 mroute_sk = rcu_dereference(mrt->mroute_sk);
1981 if (mroute_sk) {
1982 nf_reset(skb);
1983 raw_rcv(mroute_sk, skb);
1984 return 0;
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 }
1988
Eric Dumazeta8c94862010-10-01 16:15:08 +00001989 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001990 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001991 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001992 int vif = ipmr_find_vif(mrt, skb->dev);
1993
1994 if (vif >= 0)
1995 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1996 vif);
1997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001999 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002000 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 int vif;
2002
2003 if (local) {
2004 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2005 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002006 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 skb = skb2;
2009 }
2010
Eric Dumazeta8c94862010-10-01 16:15:08 +00002011 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002012 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002014 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 read_unlock(&mrt_lock);
2016
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002017 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019 read_unlock(&mrt_lock);
2020 kfree_skb(skb);
2021 return -ENODEV;
2022 }
2023
Eric Dumazeta8c94862010-10-01 16:15:08 +00002024 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002025 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 read_unlock(&mrt_lock);
2027
2028 if (local)
2029 return ip_local_deliver(skb);
2030
2031 return 0;
2032
2033dont_forward:
2034 if (local)
2035 return ip_local_deliver(skb);
2036 kfree_skb(skb);
2037 return 0;
2038}
2039
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002040#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002041/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002042int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002043{
2044 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002045 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002046 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002047
2048 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2049 goto drop;
2050
2051 pim = igmp_hdr(skb);
2052
David S. Miller417da662011-05-03 19:42:43 -07002053 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002054 if (IS_ERR(mrt))
2055 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002056 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002057 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2058 goto drop;
2059
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002060 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002061drop:
2062 kfree_skb(skb);
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return 0;
2065}
2066#endif
2067
2068#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002069static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002072 struct net *net = dev_net(skb->dev);
2073 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002075 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 goto drop;
2077
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002078 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002079 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2080 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002081 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002082 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 goto drop;
2084
David S. Miller417da662011-05-03 19:42:43 -07002085 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002086 if (IS_ERR(mrt))
2087 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002088 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002089drop:
2090 kfree_skb(skb);
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return 0;
2093}
2094#endif
2095
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002096static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2097 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 int ct;
2100 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002101 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002102 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Nicolas Dichtel74381892010-03-25 23:45:35 +00002104 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002105 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002106 return -ENOENT;
2107
Thomas Graf92a395e2012-06-26 23:36:13 +00002108 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2109 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2110 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Thomas Graf92a395e2012-06-26 23:36:13 +00002112 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2113 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002116 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002117 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2118 nla_nest_cancel(skb, mp_attr);
2119 return -EMSGSIZE;
2120 }
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 nhp->rtnh_flags = 0;
2123 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002124 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 nhp->rtnh_len = sizeof(*nhp);
2126 }
2127 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002128
2129 nla_nest_end(skb, mp_attr);
2130
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002131 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2132 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2133 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2134 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2135 return -EMSGSIZE;
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 rtm->rtm_type = RTN_MULTICAST;
2138 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
David S. Miller9a1b9492011-05-04 12:18:54 -07002141int ipmr_get_route(struct net *net, struct sk_buff *skb,
2142 __be32 saddr, __be32 daddr,
2143 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002146 struct mr_table *mrt;
2147 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002149 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002150 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002151 return -ENOENT;
2152
Eric Dumazeta8c94862010-10-01 16:15:08 +00002153 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002154 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002155 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002156 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002158 if (vif >= 0)
2159 cache = ipmr_cache_find_any(mrt, daddr, vif);
2160 }
Ian Morris51456b22015-04-03 09:17:26 +01002161 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002162 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002163 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002165 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002168 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return -EAGAIN;
2170 }
2171
2172 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002173 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002174 if (dev)
2175 vif = ipmr_find_vif(mrt, dev);
2176 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002178 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return -ENODEV;
2180 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002181 skb2 = skb_clone(skb, GFP_ATOMIC);
2182 if (!skb2) {
2183 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002184 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002185 return -ENOMEM;
2186 }
2187
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002188 skb_push(skb2, sizeof(struct iphdr));
2189 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002190 iph = ip_hdr(skb2);
2191 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002192 iph->saddr = saddr;
2193 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002194 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002195 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002197 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return err;
2199 }
2200
Eric Dumazeta8c94862010-10-01 16:15:08 +00002201 read_lock(&mrt_lock);
2202 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002204 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002206 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return err;
2208}
2209
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002210static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002211 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2212 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002213{
2214 struct nlmsghdr *nlh;
2215 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002216 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002217
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002218 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002219 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002220 return -EMSGSIZE;
2221
2222 rtm = nlmsg_data(nlh);
2223 rtm->rtm_family = RTNL_FAMILY_IPMR;
2224 rtm->rtm_dst_len = 32;
2225 rtm->rtm_src_len = 32;
2226 rtm->rtm_tos = 0;
2227 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002228 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2229 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002230 rtm->rtm_type = RTN_MULTICAST;
2231 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002232 if (c->mfc_flags & MFC_STATIC)
2233 rtm->rtm_protocol = RTPROT_STATIC;
2234 else
2235 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002236 rtm->rtm_flags = 0;
2237
Jiri Benc930345e2015-03-29 16:59:25 +02002238 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2239 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002240 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002241 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2242 /* do not break the dump if cache is unresolved */
2243 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002244 goto nla_put_failure;
2245
Johannes Berg053c0952015-01-16 22:09:00 +01002246 nlmsg_end(skb, nlh);
2247 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002248
2249nla_put_failure:
2250 nlmsg_cancel(skb, nlh);
2251 return -EMSGSIZE;
2252}
2253
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002254static size_t mroute_msgsize(bool unresolved, int maxvif)
2255{
2256 size_t len =
2257 NLMSG_ALIGN(sizeof(struct rtmsg))
2258 + nla_total_size(4) /* RTA_TABLE */
2259 + nla_total_size(4) /* RTA_SRC */
2260 + nla_total_size(4) /* RTA_DST */
2261 ;
2262
2263 if (!unresolved)
2264 len = len
2265 + nla_total_size(4) /* RTA_IIF */
2266 + nla_total_size(0) /* RTA_MULTIPATH */
2267 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2268 /* RTA_MFC_STATS */
2269 + nla_total_size(sizeof(struct rta_mfc_stats))
2270 ;
2271
2272 return len;
2273}
2274
2275static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2276 int cmd)
2277{
2278 struct net *net = read_pnet(&mrt->net);
2279 struct sk_buff *skb;
2280 int err = -ENOBUFS;
2281
2282 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2283 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002284 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002285 goto errout;
2286
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002287 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002288 if (err < 0)
2289 goto errout;
2290
2291 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2292 return;
2293
2294errout:
2295 kfree_skb(skb);
2296 if (err < 0)
2297 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2298}
2299
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002300static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2301{
2302 struct net *net = sock_net(skb->sk);
2303 struct mr_table *mrt;
2304 struct mfc_cache *mfc;
2305 unsigned int t = 0, s_t;
2306 unsigned int h = 0, s_h;
2307 unsigned int e = 0, s_e;
2308
2309 s_t = cb->args[0];
2310 s_h = cb->args[1];
2311 s_e = cb->args[2];
2312
Eric Dumazeta8c94862010-10-01 16:15:08 +00002313 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002314 ipmr_for_each_table(mrt, net) {
2315 if (t < s_t)
2316 goto next_table;
2317 if (t > s_t)
2318 s_h = 0;
2319 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002320 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002321 if (e < s_e)
2322 goto next_entry;
2323 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002324 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002325 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002326 mfc, RTM_NEWROUTE,
2327 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002328 goto done;
2329next_entry:
2330 e++;
2331 }
2332 e = s_e = 0;
2333 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002334 spin_lock_bh(&mfc_unres_lock);
2335 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2336 if (e < s_e)
2337 goto next_entry2;
2338 if (ipmr_fill_mroute(mrt, skb,
2339 NETLINK_CB(cb->skb).portid,
2340 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002341 mfc, RTM_NEWROUTE,
2342 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002343 spin_unlock_bh(&mfc_unres_lock);
2344 goto done;
2345 }
2346next_entry2:
2347 e++;
2348 }
2349 spin_unlock_bh(&mfc_unres_lock);
2350 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002351 s_h = 0;
2352next_table:
2353 t++;
2354 }
2355done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002356 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002357
2358 cb->args[2] = e;
2359 cb->args[1] = h;
2360 cb->args[0] = t;
2361
2362 return skb->len;
2363}
2364
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002365#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002366/* The /proc interfaces to multicast routing :
2367 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 */
2369struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002370 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002371 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 int ct;
2373};
2374
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002375static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2376 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 loff_t pos)
2378{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002379 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002380
2381 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2382 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002384 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002385 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387 return NULL;
2388}
2389
2390static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002391 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002393 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002394 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002395 struct mr_table *mrt;
2396
2397 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002398 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002399 return ERR_PTR(-ENOENT);
2400
2401 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002404 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 : SEQ_START_TOKEN;
2406}
2407
2408static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2409{
2410 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002411 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002412 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 ++*pos;
2415 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002416 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002417
Patrick McHardy0c122952010-04-13 05:03:22 +00002418 while (++iter->ct < mrt->maxvif) {
2419 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002421 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423 return NULL;
2424}
2425
2426static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002427 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
2429 read_unlock(&mrt_lock);
2430}
2431
2432static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2433{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002434 struct ipmr_vif_iter *iter = seq->private;
2435 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002438 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2440 } else {
2441 const struct vif_device *vif = v;
2442 const char *name = vif->dev ? vif->dev->name : "none";
2443
2444 seq_printf(seq,
2445 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002446 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002447 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 vif->bytes_out, vif->pkt_out,
2449 vif->flags, vif->local, vif->remote);
2450 }
2451 return 0;
2452}
2453
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002454static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 .start = ipmr_vif_seq_start,
2456 .next = ipmr_vif_seq_next,
2457 .stop = ipmr_vif_seq_stop,
2458 .show = ipmr_vif_seq_show,
2459};
2460
2461static int ipmr_vif_open(struct inode *inode, struct file *file)
2462{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002463 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2464 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Arjan van de Ven9a321442007-02-12 00:55:35 -08002467static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 .owner = THIS_MODULE,
2469 .open = ipmr_vif_open,
2470 .read = seq_read,
2471 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002472 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473};
2474
2475struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002476 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002477 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002478 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 int ct;
2480};
2481
2482
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002483static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2484 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002486 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 struct mfc_cache *mfc;
2488
Eric Dumazeta8c94862010-10-01 16:15:08 +00002489 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002490 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002491 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002492 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002493 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002495 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002496 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002499 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002500 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002501 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return mfc;
2503 spin_unlock_bh(&mfc_unres_lock);
2504
2505 it->cache = NULL;
2506 return NULL;
2507}
2508
2509
2510static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2511{
2512 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002513 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002514 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002515
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002516 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002517 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002518 return ERR_PTR(-ENOENT);
2519
2520 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 it->cache = NULL;
2522 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002523 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 : SEQ_START_TOKEN;
2525}
2526
2527static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2528{
2529 struct mfc_cache *mfc = v;
2530 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002531 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002532 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 ++*pos;
2535
2536 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002537 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Patrick McHardy862465f2010-04-13 05:03:21 +00002539 if (mfc->list.next != it->cache)
2540 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002541
Patrick McHardy0c122952010-04-13 05:03:22 +00002542 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 goto end_of_list;
2544
Patrick McHardy0c122952010-04-13 05:03:22 +00002545 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002548 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002549 if (list_empty(it->cache))
2550 continue;
2551 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
2553
2554 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002555 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002556 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002560 if (!list_empty(it->cache))
2561 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002563end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 spin_unlock_bh(&mfc_unres_lock);
2565 it->cache = NULL;
2566
2567 return NULL;
2568}
2569
2570static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2571{
2572 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002573 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Patrick McHardy0c122952010-04-13 05:03:22 +00002575 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002577 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002578 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
2581static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2582{
2583 int n;
2584
2585 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002586 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2588 } else {
2589 const struct mfc_cache *mfc = v;
2590 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002591 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002592
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002593 seq_printf(seq, "%08X %08X %-3hd",
2594 (__force u32) mfc->mfc_mcastgrp,
2595 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002596 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Patrick McHardy0c122952010-04-13 05:03:22 +00002598 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002599 seq_printf(seq, " %8lu %8lu %8lu",
2600 mfc->mfc_un.res.pkt,
2601 mfc->mfc_un.res.bytes,
2602 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002603 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002604 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002605 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002606 mfc->mfc_un.res.ttls[n] < 255)
2607 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002608 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 n, mfc->mfc_un.res.ttls[n]);
2610 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002611 } else {
2612 /* unresolved mfc_caches don't contain
2613 * pkt, bytes and wrong_if values
2614 */
2615 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617 seq_putc(seq, '\n');
2618 }
2619 return 0;
2620}
2621
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002622static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 .start = ipmr_mfc_seq_start,
2624 .next = ipmr_mfc_seq_next,
2625 .stop = ipmr_mfc_seq_stop,
2626 .show = ipmr_mfc_seq_show,
2627};
2628
2629static int ipmr_mfc_open(struct inode *inode, struct file *file)
2630{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002631 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2632 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
Arjan van de Ven9a321442007-02-12 00:55:35 -08002635static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 .owner = THIS_MODULE,
2637 .open = ipmr_mfc_open,
2638 .read = seq_read,
2639 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002640 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002642#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002645static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002647 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648};
2649#endif
2650
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002651/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002652static int __net_init ipmr_net_init(struct net *net)
2653{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002654 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002655
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002656 err = ipmr_rules_init(net);
2657 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002658 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002659
2660#ifdef CONFIG_PROC_FS
2661 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002662 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002663 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002664 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002665 goto proc_cache_fail;
2666#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002667 return 0;
2668
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002669#ifdef CONFIG_PROC_FS
2670proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002671 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002672proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002673 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002674#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002675fail:
2676 return err;
2677}
2678
2679static void __net_exit ipmr_net_exit(struct net *net)
2680{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002681#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002682 remove_proc_entry("ip_mr_cache", net->proc_net);
2683 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002684#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002685 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002686}
2687
2688static struct pernet_operations ipmr_net_ops = {
2689 .init = ipmr_net_init,
2690 .exit = ipmr_net_exit,
2691};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002692
Wang Chen03d2f892008-07-03 12:13:36 +08002693int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Wang Chen03d2f892008-07-03 12:13:36 +08002695 int err;
2696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2698 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002699 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002700 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002701
Benjamin Therycf958ae32009-01-22 04:56:16 +00002702 err = register_pernet_subsys(&ipmr_net_ops);
2703 if (err)
2704 goto reg_pernet_fail;
2705
Wang Chen03d2f892008-07-03 12:13:36 +08002706 err = register_netdevice_notifier(&ip_mr_notifier);
2707 if (err)
2708 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002709#ifdef CONFIG_IP_PIMSM_V2
2710 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002711 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002712 err = -EAGAIN;
2713 goto add_proto_fail;
2714 }
2715#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002716 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2717 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002718 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002719
Tom Goff403dbb92009-06-14 03:16:13 -07002720#ifdef CONFIG_IP_PIMSM_V2
2721add_proto_fail:
2722 unregister_netdevice_notifier(&ip_mr_notifier);
2723#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002724reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002725 unregister_pernet_subsys(&ipmr_net_ops);
2726reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002727 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}