blob: 286ede3716ee0644db75d57742d0c148af807fa8 [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 Aleksandrov7ef8f652015-11-21 15:57:27 +0100394static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct net_device *dev;
397
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000398 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800401 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int err;
403 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct ip_tunnel_parm p;
405 struct in_device *in_dev;
406
407 memset(&p, 0, sizeof(p));
408 p.iph.daddr = v->vifc_rmt_addr.s_addr;
409 p.iph.saddr = v->vifc_lcl_addr.s_addr;
410 p.iph.version = 4;
411 p.iph.ihl = 5;
412 p.iph.protocol = IPPROTO_IPIP;
413 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800414 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800416 if (ops->ndo_do_ioctl) {
417 mm_segment_t oldfs = get_fs();
418
419 set_fs(KERNEL_DS);
420 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
421 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000422 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800423 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 dev = NULL;
426
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000427 if (err == 0 &&
428 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dev->flags |= IFF_MULTICAST;
430
Herbert Xue5ed6392005-10-03 14:35:55 -0700431 in_dev = __in_dev_get_rtnl(dev);
Ian Morris51456b22015-04-03 09:17:26 +0100432 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700434
435 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100436 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700437 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (dev_open(dev))
440 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700441 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 }
444 return dev;
445
446failure:
447 /* allow the register to be completed before unregistering. */
448 rtnl_unlock();
449 rtnl_lock();
450
451 unregister_netdevice(dev);
452 return NULL;
453}
454
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100455#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000456static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000458 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000459 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500460 struct flowi4 fl4 = {
461 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700462 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500463 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000464 };
465 int err;
466
David S. Millerda919812011-03-12 02:04:50 -0500467 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000468 if (err < 0) {
469 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000470 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000471 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700474 dev->stats.tx_bytes += skb->len;
475 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000476 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 read_unlock(&mrt_lock);
478 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000479 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200482static int reg_vif_get_iflink(const struct net_device *dev)
483{
484 return 0;
485}
486
Stephen Hemminger007c3832008-11-20 20:28:35 -0800487static const struct net_device_ops reg_vif_netdev_ops = {
488 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200489 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800490};
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static void reg_vif_setup(struct net_device *dev)
493{
494 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800495 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530497 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700499 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000502static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 struct net_device *dev;
505 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000506 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000508 if (mrt->id == RT_TABLE_DEFAULT)
509 sprintf(name, "pimreg");
510 else
511 sprintf(name, "pimreg%u", mrt->id);
512
Tom Gundersenc835a672014-07-14 16:37:24 +0200513 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Ian Morris51456b22015-04-03 09:17:26 +0100515 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return NULL;
517
Tom Goff403dbb92009-06-14 03:16:13 -0700518 dev_net_set(dev, net);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (register_netdevice(dev)) {
521 free_netdev(dev);
522 return NULL;
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Herbert Xu71e27da2007-06-04 23:36:06 -0700525 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000526 in_dev = __in_dev_get_rcu(dev);
527 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700528 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Herbert Xu71e27da2007-06-04 23:36:06 -0700532 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100533 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700534 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
535 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (dev_open(dev))
538 goto failure;
539
Wang Chen7dc00c82008-07-14 20:56:34 -0700540 dev_hold(dev);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return dev;
543
544failure:
545 /* allow the register to be completed before unregistering. */
546 rtnl_unlock();
547 rtnl_lock();
548
549 unregister_netdevice(dev);
550 return NULL;
551}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100552
553/* called with rcu_read_lock() */
554static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
555 unsigned int pimlen)
556{
557 struct net_device *reg_dev = NULL;
558 struct iphdr *encap;
559
560 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100561 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100562 * a. packet is really sent to a multicast group
563 * b. packet is not a NULL-REGISTER
564 * c. packet is not truncated
565 */
566 if (!ipv4_is_multicast(encap->daddr) ||
567 encap->tot_len == 0 ||
568 ntohs(encap->tot_len) + pimlen > skb->len)
569 return 1;
570
571 read_lock(&mrt_lock);
572 if (mrt->mroute_reg_vif_num >= 0)
573 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
574 read_unlock(&mrt_lock);
575
576 if (!reg_dev)
577 return 1;
578
579 skb->mac_header = skb->network_header;
580 skb_pull(skb, (u8 *)encap - skb->data);
581 skb_reset_network_header(skb);
582 skb->protocol = htons(ETH_P_IP);
583 skb->ip_summed = CHECKSUM_NONE;
584
585 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
586
587 netif_rx(skb);
588
589 return NET_RX_SUCCESS;
590}
591#else
592static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
593{
594 return NULL;
595}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#endif
597
Ben Hutchings2c530402012-07-10 10:55:09 +0000598/**
599 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700600 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000602static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000603 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct vif_device *v;
606 struct net_device *dev;
607 struct in_device *in_dev;
608
Patrick McHardy0c122952010-04-13 05:03:22 +0000609 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return -EADDRNOTAVAIL;
611
Patrick McHardy0c122952010-04-13 05:03:22 +0000612 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 write_lock_bh(&mrt_lock);
615 dev = v->dev;
616 v->dev = NULL;
617
618 if (!dev) {
619 write_unlock_bh(&mrt_lock);
620 return -EADDRNOTAVAIL;
621 }
622
Patrick McHardy0c122952010-04-13 05:03:22 +0000623 if (vifi == mrt->mroute_reg_vif_num)
624 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000626 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000628
629 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000630 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 break;
632 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000633 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 write_unlock_bh(&mrt_lock);
637
638 dev_set_allmulti(dev, -1);
639
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000640 in_dev = __in_dev_get_rtnl(dev);
641 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700642 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000643 inet_netconf_notify_devconf(dev_net(dev),
644 NETCONFA_MC_FORWARDING,
645 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ip_rt_multicast_event(in_dev);
647 }
648
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000649 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000650 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 dev_put(dev);
653 return 0;
654}
655
Eric Dumazeta8c94862010-10-01 16:15:08 +0000656static void ipmr_cache_free_rcu(struct rcu_head *head)
657{
658 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
659
660 kmem_cache_free(mrt_cachep, c);
661}
662
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000663static inline void ipmr_cache_free(struct mfc_cache *c)
664{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000665 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000669 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000671static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200673 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700675 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Patrick McHardy0c122952010-04-13 05:03:22 +0000677 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Jianjun Kongc354e122008-11-03 00:28:02 -0800679 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700680 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
682 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000683 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000685 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700686 e->error = -ETIMEDOUT;
687 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700688
Eric W. Biederman15e47302012-09-07 20:12:54 +0000689 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000690 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000695 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Patrick McHardye258beb2010-04-13 05:03:19 +0000698/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000699static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Patrick McHardy0c122952010-04-13 05:03:22 +0000701 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 unsigned long now;
703 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000704 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000707 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return;
709 }
710
Patrick McHardy0c122952010-04-13 05:03:22 +0000711 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto out;
713
714 now = jiffies;
715 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Patrick McHardy0c122952010-04-13 05:03:22 +0000717 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (time_after(c->mfc_un.unres.expires, now)) {
719 unsigned long interval = c->mfc_un.unres.expires - now;
720 if (interval < expires)
721 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 continue;
723 }
724
Patrick McHardy862465f2010-04-13 05:03:21 +0000725 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000726 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000727 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
Patrick McHardy0c122952010-04-13 05:03:22 +0000730 if (!list_empty(&mrt->mfc_unres_queue))
731 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733out:
734 spin_unlock(&mfc_unres_lock);
735}
736
737/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000738static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000739 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int vifi;
742
743 cache->mfc_un.res.minvif = MAXVIFS;
744 cache->mfc_un.res.maxvif = 0;
745 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
746
Patrick McHardy0c122952010-04-13 05:03:22 +0000747 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
748 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000749 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
751 if (cache->mfc_un.res.minvif > vifi)
752 cache->mfc_un.res.minvif = vifi;
753 if (cache->mfc_un.res.maxvif <= vifi)
754 cache->mfc_un.res.maxvif = vifi + 1;
755 }
756 }
757}
758
Patrick McHardy0c122952010-04-13 05:03:22 +0000759static int vif_add(struct net *net, struct mr_table *mrt,
760 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000763 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct net_device *dev;
765 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700766 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000769 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -EADDRINUSE;
771
772 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 case VIFF_REGISTER:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100774 if (!pimsm_enabled())
775 return -EINVAL;
776 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * All the packets will be sent to the daemon
778 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000779 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000781 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (!dev)
783 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700784 err = dev_set_allmulti(dev, 1);
785 if (err) {
786 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700787 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700788 return err;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900791 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000792 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!dev)
794 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700795 err = dev_set_allmulti(dev, 1);
796 if (err) {
797 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700798 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700799 return err;
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000802 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000804 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
805 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100806 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000807 dev_put(dev);
808 return -EADDRNOTAVAIL;
809 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000810 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000811 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (!dev)
814 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700815 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700816 if (err) {
817 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700818 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 break;
821 default:
822 return -EINVAL;
823 }
824
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000825 in_dev = __in_dev_get_rtnl(dev);
826 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000827 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000829 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700830 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000831 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
832 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ip_rt_multicast_event(in_dev);
834
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000835 /* Fill in the VIF structures */
836
Jianjun Kongc354e122008-11-03 00:28:02 -0800837 v->rate_limit = vifc->vifc_rate_limit;
838 v->local = vifc->vifc_lcl_addr.s_addr;
839 v->remote = vifc->vifc_rmt_addr.s_addr;
840 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (!mrtsock)
842 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800843 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 v->bytes_in = 0;
845 v->bytes_out = 0;
846 v->pkt_in = 0;
847 v->pkt_out = 0;
848 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000849 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200850 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* And finish update writing critical data */
853 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800854 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000855 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000856 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000857 if (vifi+1 > mrt->maxvif)
858 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 write_unlock_bh(&mrt_lock);
860 return 0;
861}
862
Eric Dumazeta8c94862010-10-01 16:15:08 +0000863/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000864static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000865 __be32 origin,
866 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Jianjun Kongc354e122008-11-03 00:28:02 -0800868 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct mfc_cache *c;
870
Eric Dumazeta8c94862010-10-01 16:15:08 +0000871 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000872 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
873 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000875 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000878/* Look for a (*,*,oif) entry */
879static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
880 int vifi)
881{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100882 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000883 struct mfc_cache *c;
884
885 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100886 if (c->mfc_origin == htonl(INADDR_ANY) &&
887 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000888 c->mfc_un.res.ttls[vifi] < 255)
889 return c;
890
891 return NULL;
892}
893
894/* Look for a (*,G) entry */
895static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
896 __be32 mcastgrp, int vifi)
897{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100898 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000899 struct mfc_cache *c, *proxy;
900
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100901 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000902 goto skip;
903
904 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100905 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000906 c->mfc_mcastgrp == mcastgrp) {
907 if (c->mfc_un.res.ttls[vifi] < 255)
908 return c;
909
910 /* It's ok if the vifi is part of the static tree */
911 proxy = ipmr_cache_find_any_parent(mrt,
912 c->mfc_parent);
913 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
914 return c;
915 }
916
917skip:
918 return ipmr_cache_find_any_parent(mrt, vifi);
919}
920
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100921/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000922static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Jianjun Kongc354e122008-11-03 00:28:02 -0800924 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000925
926 if (c)
927 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return c;
929}
930
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000931static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Jianjun Kongc354e122008-11-03 00:28:02 -0800933 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000934
935 if (c) {
936 skb_queue_head_init(&c->mfc_un.unres.unresolved);
937 c->mfc_un.unres.expires = jiffies + 10*HZ;
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return c;
940}
941
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100942/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000943static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
944 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700947 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000949 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800950 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700951 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
953
Hong zhi guo573ce262013-03-27 06:47:04 +0000954 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000955 nlh->nlmsg_len = skb_tail_pointer(skb) -
956 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 } else {
958 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000959 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000961 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700962 e->error = -EMSGSIZE;
963 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Thomas Graf2942e902006-08-15 00:30:25 -0700965
Eric W. Biederman15e47302012-09-07 20:12:54 +0000966 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000967 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000968 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971}
972
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100973/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
974 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100976 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000978static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000979 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300981 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100982 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 struct igmphdr *igmp;
984 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100985 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 int ret;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (assert == IGMPMSG_WHOLEPKT)
989 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
990 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 skb = alloc_skb(128, GFP_ATOMIC);
992
Stephen Hemminger132adf52007-03-08 20:44:43 -0800993 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return -ENOBUFS;
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (assert == IGMPMSG_WHOLEPKT) {
997 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000998 * Duplicate old header, fix ihl, length etc.
999 * And all this only to mangle msg->im_msgtype and
1000 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001002 skb_push(skb, sizeof(struct iphdr));
1003 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001004 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001005 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001006 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1008 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001009 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001010 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1011 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1012 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001013 } else {
1014 /* Copy the IP header */
1015 skb_set_network_header(skb, skb->len);
1016 skb_put(skb, ihl);
1017 skb_copy_to_linear_data(skb, pkt->data, ihl);
1018 /* Flag to the kernel this is a route add */
1019 ip_hdr(skb)->protocol = 0;
1020 msg = (struct igmpmsg *)skb_network_header(skb);
1021 msg->im_vif = vifi;
1022 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1023 /* Add our header */
1024 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1025 igmp->type = assert;
1026 msg->im_msgtype = assert;
1027 igmp->code = 0;
1028 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1029 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Eric Dumazet4c968702010-10-01 16:15:01 +00001032 rcu_read_lock();
1033 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001034 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001035 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 kfree_skb(skb);
1037 return -EINVAL;
1038 }
1039
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001040 /* Deliver to mrouted */
Eric Dumazet4c968702010-10-01 16:15:01 +00001041 ret = sock_queue_rcv_skb(mroute_sk, skb);
1042 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001043 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001044 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 kfree_skb(skb);
1046 }
1047
1048 return ret;
1049}
1050
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001051/* Queue a packet for resolution. It gets locked cache entry! */
1052static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1053 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Patrick McHardy862465f2010-04-13 05:03:21 +00001055 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int err;
1057 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001058 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001061 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001062 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001063 c->mfc_origin == iph->saddr) {
1064 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Patrick McHardy862465f2010-04-13 05:03:21 +00001069 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001070 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001071 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001072 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 spin_unlock_bh(&mfc_unres_lock);
1074
1075 kfree_skb(skb);
1076 return -ENOBUFS;
1077 }
1078
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001079 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001080 c->mfc_parent = -1;
1081 c->mfc_origin = iph->saddr;
1082 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001084 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001085 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001086 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001087 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 out - Brad Parker
1089 */
1090 spin_unlock_bh(&mfc_unres_lock);
1091
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001092 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 kfree_skb(skb);
1094 return err;
1095 }
1096
Patrick McHardy0c122952010-04-13 05:03:22 +00001097 atomic_inc(&mrt->cache_resolve_queue_len);
1098 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001099 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
David S. Miller278554b2010-05-12 00:05:35 -07001101 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1102 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001105 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001106 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 kfree_skb(skb);
1108 err = -ENOBUFS;
1109 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001110 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 err = 0;
1112 }
1113
1114 spin_unlock_bh(&mfc_unres_lock);
1115 return err;
1116}
1117
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001118/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001120static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001123 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Jianjun Kongc354e122008-11-03 00:28:02 -08001125 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Patrick McHardy0c122952010-04-13 05:03:22 +00001127 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001129 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1130 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001131 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001132 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001133 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return 0;
1135 }
1136 }
1137 return -ENOENT;
1138}
1139
Patrick McHardy0c122952010-04-13 05:03:22 +00001140static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001141 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Patrick McHardy862465f2010-04-13 05:03:21 +00001143 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001145 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Patrick McHardya50436f22010-03-17 06:04:14 +00001147 if (mfc->mfcc_parent >= MAXVIFS)
1148 return -ENFILE;
1149
Jianjun Kongc354e122008-11-03 00:28:02 -08001150 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Patrick McHardy0c122952010-04-13 05:03:22 +00001152 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001154 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1155 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001156 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
Patrick McHardy862465f2010-04-13 05:03:21 +00001161 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 write_lock_bh(&mrt_lock);
1163 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001164 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!mrtsock)
1166 c->mfc_flags |= MFC_STATIC;
1167 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001168 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
1170 }
1171
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001172 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001173 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return -EINVAL;
1175
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001176 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001177 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return -ENOMEM;
1179
Jianjun Kongc354e122008-11-03 00:28:02 -08001180 c->mfc_origin = mfc->mfcc_origin.s_addr;
1181 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1182 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001183 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (!mrtsock)
1185 c->mfc_flags |= MFC_STATIC;
1186
Eric Dumazeta8c94862010-10-01 16:15:08 +00001187 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001189 /* Check to see if we resolved a queued list. If so we
1190 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001192 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001194 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001195 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001197 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001198 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001199 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
1201 }
1202 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001203 if (list_empty(&mrt->mfc_unres_queue))
1204 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 spin_unlock_bh(&mfc_unres_lock);
1206
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001207 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001208 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001209 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001211 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return 0;
1213}
1214
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001215/* Close the multicast socket, and clear the vif tables etc */
Patrick McHardy0c122952010-04-13 05:03:22 +00001216static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001219 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001220 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001221
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001222 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001223 for (i = 0; i < mrt->maxvif; i++) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001224 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
Patrick McHardy0c122952010-04-13 05:03:22 +00001225 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001227 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001229 /* Wipe the cache */
Patrick McHardy862465f2010-04-13 05:03:21 +00001230 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001231 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001232 if (c->mfc_flags & MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001234 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001235 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001236 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238 }
1239
Patrick McHardy0c122952010-04-13 05:03:22 +00001240 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001242 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001243 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001244 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001245 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247 spin_unlock_bh(&mfc_unres_lock);
1248 }
1249}
1250
Eric Dumazet4c968702010-10-01 16:15:01 +00001251/* called from ip_ra_control(), before an RCU grace period,
1252 * we dont need to call synchronize_rcu() here
1253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254static void mrtsock_destruct(struct sock *sk)
1255{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001256 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001257 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001260 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001261 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001262 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001263 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1264 NETCONFA_IFINDEX_ALL,
1265 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001266 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001267 mroute_clean_tables(mrt);
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 rtnl_unlock();
1271}
1272
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001273/* Socket options and virtual interface manipulation. The whole
1274 * virtual interface system is a complete heap, but unfortunately
1275 * that's how BSD mrouted happens to think. Maybe one day with a proper
1276 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001278
David S. Millerb7058842009-09-30 16:12:20 -07001279int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001281 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 struct vifctl vif;
1283 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001284 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001285 struct mr_table *mrt;
1286
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001287 if (sk->sk_type != SOCK_RAW ||
1288 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1289 return -EOPNOTSUPP;
1290
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001291 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001292 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001293 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001294
Stephen Hemminger132adf52007-03-08 20:44:43 -08001295 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001296 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001297 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EACCES;
1299 }
1300
Stephen Hemminger132adf52007-03-08 20:44:43 -08001301 switch (optname) {
1302 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001303 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001304 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Stephen Hemminger132adf52007-03-08 20:44:43 -08001306 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001307 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001309 return -EADDRINUSE;
1310 }
1311
1312 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1313 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001314 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001315 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001316 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1317 NETCONFA_IFINDEX_ALL,
1318 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001319 }
1320 rtnl_unlock();
1321 return ret;
1322 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001323 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001324 return -EACCES;
1325 return ip_ra_control(sk, 0, NULL);
1326 case MRT_ADD_VIF:
1327 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001328 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001329 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001330 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001331 return -EFAULT;
1332 if (vif.vifc_vifi >= MAXVIFS)
1333 return -ENFILE;
1334 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001335 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001336 ret = vif_add(net, mrt, &vif,
1337 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001338 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001339 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001340 }
1341 rtnl_unlock();
1342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001344 /* Manipulate the forwarding caches. These live
1345 * in a sort of kernel/user symbiosis.
1346 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001347 case MRT_ADD_MFC:
1348 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001349 parent = -1;
1350 case MRT_ADD_MFC_PROXY:
1351 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001352 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001353 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001354 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001355 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001356 if (parent == 0)
1357 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001358 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001359 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1360 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001362 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001363 sk == rtnl_dereference(mrt->mroute_sk),
1364 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001365 rtnl_unlock();
1366 return ret;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001367 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001368 case MRT_ASSERT:
1369 {
1370 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001371 if (optlen != sizeof(v))
1372 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001373 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001374 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001375 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001376 return 0;
1377 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001378 case MRT_PIM:
1379 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001380 int v;
1381
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001382 if (!pimsm_enabled())
1383 return -ENOPROTOOPT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001384 if (optlen != sizeof(v))
1385 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001386 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001387 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001388 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001389
Stephen Hemminger132adf52007-03-08 20:44:43 -08001390 rtnl_lock();
1391 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001392 if (v != mrt->mroute_do_pim) {
1393 mrt->mroute_do_pim = v;
1394 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001396 rtnl_unlock();
1397 return ret;
1398 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001399#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1400 case MRT_TABLE:
1401 {
1402 u32 v;
1403
1404 if (optlen != sizeof(u32))
1405 return -EINVAL;
1406 if (get_user(v, (u32 __user *)optval))
1407 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001408
1409 rtnl_lock();
1410 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001411 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1412 ret = -EBUSY;
1413 } else {
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001414 mrt = ipmr_new_table(net, v);
1415 if (IS_ERR(mrt))
1416 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001417 else
1418 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001419 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001420 rtnl_unlock();
1421 return ret;
1422 }
1423#endif
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001424 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001425 default:
1426 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428}
1429
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001430/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001431int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 int olr;
1434 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001435 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001436 struct mr_table *mrt;
1437
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001438 if (sk->sk_type != SOCK_RAW ||
1439 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1440 return -EOPNOTSUPP;
1441
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001442 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001443 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001444 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Jianjun Kongc354e122008-11-03 00:28:02 -08001446 if (optname != MRT_VERSION &&
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001447 optname != MRT_PIM &&
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001448 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -ENOPROTOOPT;
1450
1451 if (get_user(olr, optlen))
1452 return -EFAULT;
1453
1454 olr = min_t(unsigned int, olr, sizeof(int));
1455 if (olr < 0)
1456 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001457
Jianjun Kongc354e122008-11-03 00:28:02 -08001458 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -EFAULT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001460 if (optname == MRT_VERSION) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001461 val = 0x0305;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001462 } else if (optname == MRT_PIM) {
1463 if (!pimsm_enabled())
1464 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001465 val = mrt->mroute_do_pim;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001466 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001467 val = mrt->mroute_do_assert;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001468 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001469 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return -EFAULT;
1471 return 0;
1472}
1473
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001474/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1476{
1477 struct sioc_sg_req sr;
1478 struct sioc_vif_req vr;
1479 struct vif_device *vif;
1480 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001481 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001482 struct mr_table *mrt;
1483
1484 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001485 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001486 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001487
Stephen Hemminger132adf52007-03-08 20:44:43 -08001488 switch (cmd) {
1489 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001490 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001491 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001492 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001493 return -EINVAL;
1494 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001495 vif = &mrt->vif_table[vr.vifi];
1496 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001497 vr.icount = vif->pkt_in;
1498 vr.ocount = vif->pkt_out;
1499 vr.ibytes = vif->bytes_in;
1500 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001502
Jianjun Kongc354e122008-11-03 00:28:02 -08001503 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001505 return 0;
1506 }
1507 read_unlock(&mrt_lock);
1508 return -EADDRNOTAVAIL;
1509 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001510 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001511 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Eric Dumazeta8c94862010-10-01 16:15:08 +00001513 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001514 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001515 if (c) {
1516 sr.pktcnt = c->mfc_un.res.pkt;
1517 sr.bytecnt = c->mfc_un.res.bytes;
1518 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001519 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520
Jianjun Kongc354e122008-11-03 00:28:02 -08001521 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522 return -EFAULT;
1523 return 0;
1524 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001525 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001526 return -EADDRNOTAVAIL;
1527 default:
1528 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530}
1531
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001532#ifdef CONFIG_COMPAT
1533struct compat_sioc_sg_req {
1534 struct in_addr src;
1535 struct in_addr grp;
1536 compat_ulong_t pktcnt;
1537 compat_ulong_t bytecnt;
1538 compat_ulong_t wrong_if;
1539};
1540
David S. Millerca6b8bb02011-02-03 17:24:28 -08001541struct compat_sioc_vif_req {
1542 vifi_t vifi; /* Which iface */
1543 compat_ulong_t icount;
1544 compat_ulong_t ocount;
1545 compat_ulong_t ibytes;
1546 compat_ulong_t obytes;
1547};
1548
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001549int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1550{
David S. Miller0033d5a2011-02-03 17:21:31 -08001551 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001552 struct compat_sioc_vif_req vr;
1553 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001554 struct mfc_cache *c;
1555 struct net *net = sock_net(sk);
1556 struct mr_table *mrt;
1557
1558 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001559 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001560 return -ENOENT;
1561
1562 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001563 case SIOCGETVIFCNT:
1564 if (copy_from_user(&vr, arg, sizeof(vr)))
1565 return -EFAULT;
1566 if (vr.vifi >= mrt->maxvif)
1567 return -EINVAL;
1568 read_lock(&mrt_lock);
1569 vif = &mrt->vif_table[vr.vifi];
1570 if (VIF_EXISTS(mrt, vr.vifi)) {
1571 vr.icount = vif->pkt_in;
1572 vr.ocount = vif->pkt_out;
1573 vr.ibytes = vif->bytes_in;
1574 vr.obytes = vif->bytes_out;
1575 read_unlock(&mrt_lock);
1576
1577 if (copy_to_user(arg, &vr, sizeof(vr)))
1578 return -EFAULT;
1579 return 0;
1580 }
1581 read_unlock(&mrt_lock);
1582 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001583 case SIOCGETSGCNT:
1584 if (copy_from_user(&sr, arg, sizeof(sr)))
1585 return -EFAULT;
1586
1587 rcu_read_lock();
1588 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1589 if (c) {
1590 sr.pktcnt = c->mfc_un.res.pkt;
1591 sr.bytecnt = c->mfc_un.res.bytes;
1592 sr.wrong_if = c->mfc_un.res.wrong_if;
1593 rcu_read_unlock();
1594
1595 if (copy_to_user(arg, &sr, sizeof(sr)))
1596 return -EFAULT;
1597 return 0;
1598 }
1599 rcu_read_unlock();
1600 return -EADDRNOTAVAIL;
1601 default:
1602 return -ENOIOCTLCMD;
1603 }
1604}
1605#endif
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1608{
Jiri Pirko351638e2013-05-28 01:30:21 +00001609 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001610 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001611 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 struct vif_device *v;
1613 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (event != NETDEV_UNREGISTER)
1616 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001617
1618 ipmr_for_each_table(mrt, net) {
1619 v = &mrt->vif_table[0];
1620 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1621 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001622 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625 return NOTIFY_DONE;
1626}
1627
Jianjun Kongc354e122008-11-03 00:28:02 -08001628static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 .notifier_call = ipmr_device_event,
1630};
1631
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001632/* Encapsulate a packet by attaching a valid IPIP header to it.
1633 * This avoids tunnel drivers and other mess and gives us the speed so
1634 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001636static void ip_encap(struct net *net, struct sk_buff *skb,
1637 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001639 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001640 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001641
1642 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001643 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001644 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001645 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001647 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001648 iph->tos = old_iph->tos;
1649 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 iph->frag_off = 0;
1651 iph->daddr = daddr;
1652 iph->saddr = saddr;
1653 iph->protocol = IPPROTO_IPIP;
1654 iph->ihl = 5;
1655 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001656 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ip_send_check(iph);
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1660 nf_reset(skb);
1661}
1662
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001663static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1664 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001666 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
David S. Miller73186df2015-11-03 13:41:45 -05001668 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1669 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 if (unlikely(opt->optlen))
1672 ip_forward_options(skb);
1673
Eric W. Biederman13206b62015-10-07 16:48:35 -05001674 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001677/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Patrick McHardy0c122952010-04-13 05:03:22 +00001679static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1680 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001682 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001683 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct net_device *dev;
1685 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001686 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int encap = 0;
1688
Ian Morris51456b22015-04-03 09:17:26 +01001689 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto out_free;
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (vif->flags & VIFF_REGISTER) {
1693 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001694 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001695 vif->dev->stats.tx_bytes += skb->len;
1696 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001697 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001698 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001701 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001702 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001703 vif->remote, vif->local,
1704 0, 0,
1705 IPPROTO_IPIP,
1706 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001707 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 goto out_free;
1709 encap = sizeof(struct iphdr);
1710 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001711 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001712 0, 0,
1713 IPPROTO_IPIP,
1714 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001715 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto out_free;
1717 }
1718
Changli Gaod8d1f302010-06-10 23:31:35 -07001719 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Changli Gaod8d1f302010-06-10 23:31:35 -07001721 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001723 * allow to send ICMP, so that packets will disappear
1724 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
David S. Miller73186df2015-11-03 13:41:45 -05001726 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ip_rt_put(rt);
1728 goto out_free;
1729 }
1730
Changli Gaod8d1f302010-06-10 23:31:35 -07001731 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001734 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 goto out_free;
1736 }
1737
1738 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001739 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Eric Dumazetadf30902009-06-02 05:19:30 +00001741 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001742 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001743 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001746 * What do we do with netfilter? -- RR
1747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001749 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001751 vif->dev->stats.tx_packets++;
1752 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
1755 IPCB(skb)->flags |= IPSKB_FORWARDED;
1756
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001757 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * not only before forwarding, but after forwarding on all output
1759 * interfaces. It is clear, if mrouter runs a multicasting
1760 * program, it should receive packets not depending to what interface
1761 * program is joined.
1762 * If we will not make it, the program will have to join on all
1763 * interfaces. On the other hand, multihoming host (or router, but
1764 * not mrouter) cannot join to more than one interface - it will
1765 * result in receiving multiple packets.
1766 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001767 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1768 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 ipmr_forward_finish);
1770 return;
1771
1772out_free:
1773 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Patrick McHardy0c122952010-04-13 05:03:22 +00001776static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001779
1780 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1781 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
1783 }
1784 return ct;
1785}
1786
1787/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001788static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1789 struct sk_buff *skb, struct mfc_cache *cache,
1790 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 int psend = -1;
1793 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001794 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 vif = cache->mfc_parent;
1797 cache->mfc_un.res.pkt++;
1798 cache->mfc_un.res.bytes += skb->len;
1799
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001800 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001801 struct mfc_cache *cache_proxy;
1802
1803 /* For an (*,G) entry, we only check that the incomming
1804 * interface is part of the static tree.
1805 */
1806 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1807 if (cache_proxy &&
1808 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1809 goto forward;
1810 }
1811
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001812 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001813 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001814 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001816 * Very complicated situation...
1817 *
1818 * The best workaround until routing daemons will be
1819 * fixed is not to redistribute packet, if it was
1820 * send through wrong interface. It means, that
1821 * multicast applications WILL NOT work for
1822 * (S,G), which have default multicast route pointing
1823 * to wrong oif. In any case, it is not a good
1824 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
1826 goto dont_forward;
1827 }
1828
1829 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Patrick McHardy0c122952010-04-13 05:03:22 +00001831 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001833 * so that we cannot check that packet arrived on an oif.
1834 * It is bad, but otherwise we would need to move pretty
1835 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001837 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001838 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001839 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1841 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001842 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844 goto dont_forward;
1845 }
1846
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001847forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001848 mrt->vif_table[vif].pkt_in++;
1849 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001851 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001852 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1853 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001854 if (true_vifi >= 0 &&
1855 true_vifi != cache->mfc_parent &&
1856 ip_hdr(skb)->ttl >
1857 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1858 /* It's an (*,*) entry and the packet is not coming from
1859 * the upstream: forward the packet to the upstream
1860 * only.
1861 */
1862 psend = cache->mfc_parent;
1863 goto last_forward;
1864 }
1865 goto dont_forward;
1866 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001867 for (ct = cache->mfc_un.res.maxvif - 1;
1868 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001869 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001870 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1871 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001872 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (psend != -1) {
1874 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001877 ipmr_queue_xmit(net, mrt, skb2, cache,
1878 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001880 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001883last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (psend != -1) {
1885 if (local) {
1886 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001889 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001891 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001892 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894 }
1895
1896dont_forward:
1897 if (!local)
1898 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
David S. Miller417da662011-05-03 19:42:43 -07001901static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001902{
David S. Miller417da662011-05-03 19:42:43 -07001903 struct rtable *rt = skb_rtable(skb);
1904 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001905 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001906 .daddr = iph->daddr,
1907 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001908 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001909 .flowi4_oif = (rt_is_output_route(rt) ?
1910 skb->dev->ifindex : 0),
1911 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001912 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001913 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001914 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001915 };
1916 struct mr_table *mrt;
1917 int err;
1918
David S. Millerda919812011-03-12 02:04:50 -05001919 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001920 if (err)
1921 return ERR_PTR(err);
1922 return mrt;
1923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001925/* Multicast packets for forwarding arrive here
1926 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928int ip_mr_input(struct sk_buff *skb)
1929{
1930 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001931 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001932 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001933 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001936 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001938 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 goto dont_forward;
1940
David S. Miller417da662011-05-03 19:42:43 -07001941 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001942 if (IS_ERR(mrt)) {
1943 kfree_skb(skb);
1944 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001947 if (IPCB(skb)->opt.router_alert) {
1948 if (ip_call_ra_chain(skb))
1949 return 0;
1950 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1951 /* IGMPv1 (and broken IGMPv2 implementations sort of
1952 * Cisco IOS <= 11.2(8)) do not put router alert
1953 * option to IGMP packets destined to routable
1954 * groups. It is very bad, because it means
1955 * that we can forward NO IGMP messages.
1956 */
1957 struct sock *mroute_sk;
1958
1959 mroute_sk = rcu_dereference(mrt->mroute_sk);
1960 if (mroute_sk) {
1961 nf_reset(skb);
1962 raw_rcv(mroute_sk, skb);
1963 return 0;
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966 }
1967
Eric Dumazeta8c94862010-10-01 16:15:08 +00001968 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001969 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001970 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001971 int vif = ipmr_find_vif(mrt, skb->dev);
1972
1973 if (vif >= 0)
1974 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1975 vif);
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001978 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01001979 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 int vif;
1981
1982 if (local) {
1983 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1984 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01001985 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 skb = skb2;
1988 }
1989
Eric Dumazeta8c94862010-10-01 16:15:08 +00001990 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001991 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001993 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 read_unlock(&mrt_lock);
1995
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001996 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998 read_unlock(&mrt_lock);
1999 kfree_skb(skb);
2000 return -ENODEV;
2001 }
2002
Eric Dumazeta8c94862010-10-01 16:15:08 +00002003 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002004 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 read_unlock(&mrt_lock);
2006
2007 if (local)
2008 return ip_local_deliver(skb);
2009
2010 return 0;
2011
2012dont_forward:
2013 if (local)
2014 return ip_local_deliver(skb);
2015 kfree_skb(skb);
2016 return 0;
2017}
2018
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002019#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002020/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002021int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002022{
2023 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002024 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002025 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002026
2027 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2028 goto drop;
2029
2030 pim = igmp_hdr(skb);
2031
David S. Miller417da662011-05-03 19:42:43 -07002032 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002033 if (IS_ERR(mrt))
2034 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002035 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002036 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2037 goto drop;
2038
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002039 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002040drop:
2041 kfree_skb(skb);
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return 0;
2044}
2045#endif
2046
2047#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002048static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002051 struct net *net = dev_net(skb->dev);
2052 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002054 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 goto drop;
2056
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002057 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002058 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2059 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002060 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002061 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 goto drop;
2063
David S. Miller417da662011-05-03 19:42:43 -07002064 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002065 if (IS_ERR(mrt))
2066 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002067 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002068drop:
2069 kfree_skb(skb);
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072}
2073#endif
2074
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002075static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2076 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078 int ct;
2079 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002080 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002081 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Nicolas Dichtel74381892010-03-25 23:45:35 +00002083 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002084 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002085 return -ENOENT;
2086
Thomas Graf92a395e2012-06-26 23:36:13 +00002087 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2088 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2089 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Thomas Graf92a395e2012-06-26 23:36:13 +00002091 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2092 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002095 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002096 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2097 nla_nest_cancel(skb, mp_attr);
2098 return -EMSGSIZE;
2099 }
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 nhp->rtnh_flags = 0;
2102 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002103 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 nhp->rtnh_len = sizeof(*nhp);
2105 }
2106 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002107
2108 nla_nest_end(skb, mp_attr);
2109
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002110 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2111 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2112 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2113 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2114 return -EMSGSIZE;
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 rtm->rtm_type = RTN_MULTICAST;
2117 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
David S. Miller9a1b9492011-05-04 12:18:54 -07002120int ipmr_get_route(struct net *net, struct sk_buff *skb,
2121 __be32 saddr, __be32 daddr,
2122 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002125 struct mr_table *mrt;
2126 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002128 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002129 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002130 return -ENOENT;
2131
Eric Dumazeta8c94862010-10-01 16:15:08 +00002132 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002133 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002134 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002135 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002137 if (vif >= 0)
2138 cache = ipmr_cache_find_any(mrt, daddr, vif);
2139 }
Ian Morris51456b22015-04-03 09:17:26 +01002140 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002141 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002142 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002144 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002147 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return -EAGAIN;
2149 }
2150
2151 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002152 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002153 if (dev)
2154 vif = ipmr_find_vif(mrt, dev);
2155 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002157 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return -ENODEV;
2159 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002160 skb2 = skb_clone(skb, GFP_ATOMIC);
2161 if (!skb2) {
2162 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002163 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002164 return -ENOMEM;
2165 }
2166
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002167 skb_push(skb2, sizeof(struct iphdr));
2168 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002169 iph = ip_hdr(skb2);
2170 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002171 iph->saddr = saddr;
2172 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002173 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002174 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002176 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return err;
2178 }
2179
Eric Dumazeta8c94862010-10-01 16:15:08 +00002180 read_lock(&mrt_lock);
2181 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002183 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002185 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return err;
2187}
2188
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002189static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002190 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2191 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002192{
2193 struct nlmsghdr *nlh;
2194 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002195 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002196
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002197 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002198 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002199 return -EMSGSIZE;
2200
2201 rtm = nlmsg_data(nlh);
2202 rtm->rtm_family = RTNL_FAMILY_IPMR;
2203 rtm->rtm_dst_len = 32;
2204 rtm->rtm_src_len = 32;
2205 rtm->rtm_tos = 0;
2206 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002207 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2208 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002209 rtm->rtm_type = RTN_MULTICAST;
2210 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002211 if (c->mfc_flags & MFC_STATIC)
2212 rtm->rtm_protocol = RTPROT_STATIC;
2213 else
2214 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002215 rtm->rtm_flags = 0;
2216
Jiri Benc930345e2015-03-29 16:59:25 +02002217 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2218 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002219 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002220 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2221 /* do not break the dump if cache is unresolved */
2222 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002223 goto nla_put_failure;
2224
Johannes Berg053c0952015-01-16 22:09:00 +01002225 nlmsg_end(skb, nlh);
2226 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002227
2228nla_put_failure:
2229 nlmsg_cancel(skb, nlh);
2230 return -EMSGSIZE;
2231}
2232
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002233static size_t mroute_msgsize(bool unresolved, int maxvif)
2234{
2235 size_t len =
2236 NLMSG_ALIGN(sizeof(struct rtmsg))
2237 + nla_total_size(4) /* RTA_TABLE */
2238 + nla_total_size(4) /* RTA_SRC */
2239 + nla_total_size(4) /* RTA_DST */
2240 ;
2241
2242 if (!unresolved)
2243 len = len
2244 + nla_total_size(4) /* RTA_IIF */
2245 + nla_total_size(0) /* RTA_MULTIPATH */
2246 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2247 /* RTA_MFC_STATS */
2248 + nla_total_size(sizeof(struct rta_mfc_stats))
2249 ;
2250
2251 return len;
2252}
2253
2254static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2255 int cmd)
2256{
2257 struct net *net = read_pnet(&mrt->net);
2258 struct sk_buff *skb;
2259 int err = -ENOBUFS;
2260
2261 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2262 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002263 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002264 goto errout;
2265
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002266 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002267 if (err < 0)
2268 goto errout;
2269
2270 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2271 return;
2272
2273errout:
2274 kfree_skb(skb);
2275 if (err < 0)
2276 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2277}
2278
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002279static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2280{
2281 struct net *net = sock_net(skb->sk);
2282 struct mr_table *mrt;
2283 struct mfc_cache *mfc;
2284 unsigned int t = 0, s_t;
2285 unsigned int h = 0, s_h;
2286 unsigned int e = 0, s_e;
2287
2288 s_t = cb->args[0];
2289 s_h = cb->args[1];
2290 s_e = cb->args[2];
2291
Eric Dumazeta8c94862010-10-01 16:15:08 +00002292 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002293 ipmr_for_each_table(mrt, net) {
2294 if (t < s_t)
2295 goto next_table;
2296 if (t > s_t)
2297 s_h = 0;
2298 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002299 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002300 if (e < s_e)
2301 goto next_entry;
2302 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002303 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002304 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002305 mfc, RTM_NEWROUTE,
2306 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002307 goto done;
2308next_entry:
2309 e++;
2310 }
2311 e = s_e = 0;
2312 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002313 spin_lock_bh(&mfc_unres_lock);
2314 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2315 if (e < s_e)
2316 goto next_entry2;
2317 if (ipmr_fill_mroute(mrt, skb,
2318 NETLINK_CB(cb->skb).portid,
2319 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002320 mfc, RTM_NEWROUTE,
2321 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002322 spin_unlock_bh(&mfc_unres_lock);
2323 goto done;
2324 }
2325next_entry2:
2326 e++;
2327 }
2328 spin_unlock_bh(&mfc_unres_lock);
2329 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002330 s_h = 0;
2331next_table:
2332 t++;
2333 }
2334done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002335 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002336
2337 cb->args[2] = e;
2338 cb->args[1] = h;
2339 cb->args[0] = t;
2340
2341 return skb->len;
2342}
2343
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002344#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002345/* The /proc interfaces to multicast routing :
2346 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 */
2348struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002349 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002350 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int ct;
2352};
2353
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002354static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2355 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 loff_t pos)
2357{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002358 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002359
2360 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2361 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002363 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002364 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366 return NULL;
2367}
2368
2369static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002370 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002372 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002373 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002374 struct mr_table *mrt;
2375
2376 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002377 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002378 return ERR_PTR(-ENOENT);
2379
2380 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002383 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 : SEQ_START_TOKEN;
2385}
2386
2387static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2388{
2389 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002390 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002391 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 ++*pos;
2394 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002395 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002396
Patrick McHardy0c122952010-04-13 05:03:22 +00002397 while (++iter->ct < mrt->maxvif) {
2398 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002400 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 return NULL;
2403}
2404
2405static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002406 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
2408 read_unlock(&mrt_lock);
2409}
2410
2411static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2412{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002413 struct ipmr_vif_iter *iter = seq->private;
2414 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002417 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2419 } else {
2420 const struct vif_device *vif = v;
2421 const char *name = vif->dev ? vif->dev->name : "none";
2422
2423 seq_printf(seq,
2424 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002425 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002426 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 vif->bytes_out, vif->pkt_out,
2428 vif->flags, vif->local, vif->remote);
2429 }
2430 return 0;
2431}
2432
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002433static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 .start = ipmr_vif_seq_start,
2435 .next = ipmr_vif_seq_next,
2436 .stop = ipmr_vif_seq_stop,
2437 .show = ipmr_vif_seq_show,
2438};
2439
2440static int ipmr_vif_open(struct inode *inode, struct file *file)
2441{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002442 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2443 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
Arjan van de Ven9a321442007-02-12 00:55:35 -08002446static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 .owner = THIS_MODULE,
2448 .open = ipmr_vif_open,
2449 .read = seq_read,
2450 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002451 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452};
2453
2454struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002455 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002456 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002457 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 int ct;
2459};
2460
2461
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002462static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2463 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002465 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 struct mfc_cache *mfc;
2467
Eric Dumazeta8c94862010-10-01 16:15:08 +00002468 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002469 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002470 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002471 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002472 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002474 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002475 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002478 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002479 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002480 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return mfc;
2482 spin_unlock_bh(&mfc_unres_lock);
2483
2484 it->cache = NULL;
2485 return NULL;
2486}
2487
2488
2489static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2490{
2491 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002492 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002493 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002494
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002495 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002496 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002497 return ERR_PTR(-ENOENT);
2498
2499 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 it->cache = NULL;
2501 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002502 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 : SEQ_START_TOKEN;
2504}
2505
2506static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2507{
2508 struct mfc_cache *mfc = v;
2509 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002510 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002511 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 ++*pos;
2514
2515 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002516 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Patrick McHardy862465f2010-04-13 05:03:21 +00002518 if (mfc->list.next != it->cache)
2519 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002520
Patrick McHardy0c122952010-04-13 05:03:22 +00002521 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 goto end_of_list;
2523
Patrick McHardy0c122952010-04-13 05:03:22 +00002524 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002527 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002528 if (list_empty(it->cache))
2529 continue;
2530 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
2532
2533 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002534 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002535 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002539 if (!list_empty(it->cache))
2540 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002542end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 spin_unlock_bh(&mfc_unres_lock);
2544 it->cache = NULL;
2545
2546 return NULL;
2547}
2548
2549static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2550{
2551 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002552 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Patrick McHardy0c122952010-04-13 05:03:22 +00002554 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002556 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002557 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
2560static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2561{
2562 int n;
2563
2564 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002565 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2567 } else {
2568 const struct mfc_cache *mfc = v;
2569 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002570 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002571
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002572 seq_printf(seq, "%08X %08X %-3hd",
2573 (__force u32) mfc->mfc_mcastgrp,
2574 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002575 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Patrick McHardy0c122952010-04-13 05:03:22 +00002577 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002578 seq_printf(seq, " %8lu %8lu %8lu",
2579 mfc->mfc_un.res.pkt,
2580 mfc->mfc_un.res.bytes,
2581 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002582 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002583 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002584 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002585 mfc->mfc_un.res.ttls[n] < 255)
2586 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002587 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 n, mfc->mfc_un.res.ttls[n]);
2589 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002590 } else {
2591 /* unresolved mfc_caches don't contain
2592 * pkt, bytes and wrong_if values
2593 */
2594 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596 seq_putc(seq, '\n');
2597 }
2598 return 0;
2599}
2600
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002601static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 .start = ipmr_mfc_seq_start,
2603 .next = ipmr_mfc_seq_next,
2604 .stop = ipmr_mfc_seq_stop,
2605 .show = ipmr_mfc_seq_show,
2606};
2607
2608static int ipmr_mfc_open(struct inode *inode, struct file *file)
2609{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002610 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2611 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Arjan van de Ven9a321442007-02-12 00:55:35 -08002614static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 .owner = THIS_MODULE,
2616 .open = ipmr_mfc_open,
2617 .read = seq_read,
2618 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002619 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002621#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002624static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002626 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627};
2628#endif
2629
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002630/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002631static int __net_init ipmr_net_init(struct net *net)
2632{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002633 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002634
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002635 err = ipmr_rules_init(net);
2636 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002637 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002638
2639#ifdef CONFIG_PROC_FS
2640 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002641 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002642 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002643 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002644 goto proc_cache_fail;
2645#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002646 return 0;
2647
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002648#ifdef CONFIG_PROC_FS
2649proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002650 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002651proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002652 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002653#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002654fail:
2655 return err;
2656}
2657
2658static void __net_exit ipmr_net_exit(struct net *net)
2659{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002660#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002661 remove_proc_entry("ip_mr_cache", net->proc_net);
2662 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002663#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002664 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002665}
2666
2667static struct pernet_operations ipmr_net_ops = {
2668 .init = ipmr_net_init,
2669 .exit = ipmr_net_exit,
2670};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002671
Wang Chen03d2f892008-07-03 12:13:36 +08002672int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Wang Chen03d2f892008-07-03 12:13:36 +08002674 int err;
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2677 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002678 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002679 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002680 if (!mrt_cachep)
2681 return -ENOMEM;
2682
Benjamin Therycf958ae32009-01-22 04:56:16 +00002683 err = register_pernet_subsys(&ipmr_net_ops);
2684 if (err)
2685 goto reg_pernet_fail;
2686
Wang Chen03d2f892008-07-03 12:13:36 +08002687 err = register_netdevice_notifier(&ip_mr_notifier);
2688 if (err)
2689 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002690#ifdef CONFIG_IP_PIMSM_V2
2691 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002692 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002693 err = -EAGAIN;
2694 goto add_proto_fail;
2695 }
2696#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002697 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2698 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002699 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002700
Tom Goff403dbb92009-06-14 03:16:13 -07002701#ifdef CONFIG_IP_PIMSM_V2
2702add_proto_fail:
2703 unregister_netdevice_notifier(&ip_mr_notifier);
2704#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002705reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002706 unregister_pernet_subsys(&ipmr_net_ops);
2707reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002708 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002709 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}