]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/ipv4/ipmr.c
Merge remote-tracking branches 'asoc/topic/inntel', 'asoc/topic/input', 'asoc/topic...
[mirror_ubuntu-bionic-kernel.git] / net / ipv4 / ipmr.c
CommitLineData
1da177e4
LT
1/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
113aa838 4 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
1da177e4
LT
5 * 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 *
1da177e4
LT
12 * 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
f77f13e2 25 * Relax this requirement to work with older peers.
1da177e4
LT
26 *
27 */
28
1da177e4
LT
29#include <asm/uaccess.h>
30#include <linux/types.h>
4fc268d2 31#include <linux/capability.h>
1da177e4
LT
32#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>
46f25dff 48#include <linux/if_ether.h>
5a0e3ad6 49#include <linux/slab.h>
457c4cbc 50#include <net/net_namespace.h>
1da177e4
LT
51#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
14c85021 54#include <net/route.h>
1da177e4
LT
55#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>
709b46e8 62#include <linux/compat.h>
bc3b2d7f 63#include <linux/export.h>
c5441932 64#include <net/ip_tunnels.h>
1da177e4 65#include <net/checksum.h>
dc5fc579 66#include <net/netlink.h>
f0ad0860 67#include <net/fib_rules.h>
d67b8c61 68#include <linux/netconf.h>
ccbb0aa6 69#include <net/nexthop.h>
1da177e4 70
f0ad0860
PM
71struct ipmr_rule {
72 struct fib_rule common;
73};
74
75struct ipmr_result {
76 struct mr_table *mrt;
77};
78
1da177e4 79/* Big lock, protecting vif table, mrt cache and mroute socket state.
a8cb16dd 80 * Note that the changes are semaphored via rtnl_lock.
1da177e4
LT
81 */
82
83static DEFINE_RWLOCK(mrt_lock);
84
7ef8f65d 85/* Multicast router control variables */
1da177e4 86
1da177e4
LT
87/* Special spinlock for queue of unresolved entries */
88static DEFINE_SPINLOCK(mfc_unres_lock);
89
90/* We return to original Alan's scheme. Hash table of resolved
a8cb16dd
ED
91 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
94 *
95 * In this case data path is free of exclusive locks at all.
1da177e4
LT
96 */
97
e18b890b 98static struct kmem_cache *mrt_cachep __read_mostly;
1da177e4 99
f0ad0860 100static struct mr_table *ipmr_new_table(struct net *net, u32 id);
acbb219d
FR
101static void ipmr_free_table(struct mr_table *mrt);
102
c4854ec8
RR
103static void ip_mr_forward(struct net *net, struct mr_table *mrt,
104 struct sk_buff *skb, struct mfc_cache *cache,
105 int local);
0c12295a 106static int ipmr_cache_report(struct mr_table *mrt,
4feb88e5 107 struct sk_buff *pkt, vifi_t vifi, int assert);
cb6a4e46
PM
108static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
109 struct mfc_cache *c, struct rtmsg *rtm);
8cd3ac9f
ND
110static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
111 int cmd);
0e615e96 112static void mroute_clean_tables(struct mr_table *mrt, bool all);
f0ad0860
PM
113static void ipmr_expire_process(unsigned long arg);
114
115#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
116#define ipmr_for_each_table(mrt, net) \
117 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
118
119static struct mr_table *ipmr_get_table(struct net *net, u32 id)
120{
121 struct mr_table *mrt;
122
123 ipmr_for_each_table(mrt, net) {
124 if (mrt->id == id)
125 return mrt;
126 }
127 return NULL;
128}
129
da91981b 130static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
f0ad0860
PM
131 struct mr_table **mrt)
132{
f0ad0860 133 int err;
95f4a45d
HFS
134 struct ipmr_result res;
135 struct fib_lookup_arg arg = {
136 .result = &res,
137 .flags = FIB_LOOKUP_NOREF,
138 };
f0ad0860 139
da91981b
DM
140 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
141 flowi4_to_flowi(flp4), 0, &arg);
f0ad0860
PM
142 if (err < 0)
143 return err;
144 *mrt = res.mrt;
145 return 0;
146}
147
148static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
149 int flags, struct fib_lookup_arg *arg)
150{
151 struct ipmr_result *res = arg->result;
152 struct mr_table *mrt;
1da177e4 153
f0ad0860
PM
154 switch (rule->action) {
155 case FR_ACT_TO_TBL:
156 break;
157 case FR_ACT_UNREACHABLE:
158 return -ENETUNREACH;
159 case FR_ACT_PROHIBIT:
160 return -EACCES;
161 case FR_ACT_BLACKHOLE:
162 default:
163 return -EINVAL;
164 }
165
166 mrt = ipmr_get_table(rule->fr_net, rule->table);
51456b29 167 if (!mrt)
f0ad0860
PM
168 return -EAGAIN;
169 res->mrt = mrt;
170 return 0;
171}
172
173static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
174{
175 return 1;
176}
177
178static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
179 FRA_GENERIC_POLICY,
180};
181
182static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
183 struct fib_rule_hdr *frh, struct nlattr **tb)
184{
185 return 0;
186}
187
188static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
189 struct nlattr **tb)
190{
191 return 1;
192}
193
194static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
195 struct fib_rule_hdr *frh)
196{
197 frh->dst_len = 0;
198 frh->src_len = 0;
199 frh->tos = 0;
200 return 0;
201}
202
04a6f82c 203static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
25239cee 204 .family = RTNL_FAMILY_IPMR,
f0ad0860
PM
205 .rule_size = sizeof(struct ipmr_rule),
206 .addr_size = sizeof(u32),
207 .action = ipmr_rule_action,
208 .match = ipmr_rule_match,
209 .configure = ipmr_rule_configure,
210 .compare = ipmr_rule_compare,
f0ad0860
PM
211 .fill = ipmr_rule_fill,
212 .nlgroup = RTNLGRP_IPV4_RULE,
213 .policy = ipmr_rule_policy,
214 .owner = THIS_MODULE,
215};
216
217static int __net_init ipmr_rules_init(struct net *net)
218{
219 struct fib_rules_ops *ops;
220 struct mr_table *mrt;
221 int err;
222
223 ops = fib_rules_register(&ipmr_rules_ops_template, net);
224 if (IS_ERR(ops))
225 return PTR_ERR(ops);
226
227 INIT_LIST_HEAD(&net->ipv4.mr_tables);
228
229 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
1113ebbc
NA
230 if (IS_ERR(mrt)) {
231 err = PTR_ERR(mrt);
f0ad0860
PM
232 goto err1;
233 }
234
235 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
236 if (err < 0)
237 goto err2;
238
239 net->ipv4.mr_rules_ops = ops;
240 return 0;
241
242err2:
f243e5a7 243 ipmr_free_table(mrt);
f0ad0860
PM
244err1:
245 fib_rules_unregister(ops);
246 return err;
247}
248
249static void __net_exit ipmr_rules_exit(struct net *net)
250{
251 struct mr_table *mrt, *next;
252
ed785309 253 rtnl_lock();
035320d5
ED
254 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
255 list_del(&mrt->list);
acbb219d 256 ipmr_free_table(mrt);
035320d5 257 }
f0ad0860 258 fib_rules_unregister(net->ipv4.mr_rules_ops);
419df12f 259 rtnl_unlock();
f0ad0860
PM
260}
261#else
262#define ipmr_for_each_table(mrt, net) \
263 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
264
265static struct mr_table *ipmr_get_table(struct net *net, u32 id)
266{
267 return net->ipv4.mrt;
268}
269
da91981b 270static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
f0ad0860
PM
271 struct mr_table **mrt)
272{
273 *mrt = net->ipv4.mrt;
274 return 0;
275}
276
277static int __net_init ipmr_rules_init(struct net *net)
278{
1113ebbc
NA
279 struct mr_table *mrt;
280
281 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
282 if (IS_ERR(mrt))
283 return PTR_ERR(mrt);
284 net->ipv4.mrt = mrt;
285 return 0;
f0ad0860
PM
286}
287
288static void __net_exit ipmr_rules_exit(struct net *net)
289{
ed785309 290 rtnl_lock();
acbb219d 291 ipmr_free_table(net->ipv4.mrt);
ed785309
WC
292 net->ipv4.mrt = NULL;
293 rtnl_unlock();
f0ad0860
PM
294}
295#endif
296
297static struct mr_table *ipmr_new_table(struct net *net, u32 id)
298{
299 struct mr_table *mrt;
300 unsigned int i;
1da177e4 301
1113ebbc
NA
302 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
303 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
304 return ERR_PTR(-EINVAL);
305
f0ad0860 306 mrt = ipmr_get_table(net, id);
00db4124 307 if (mrt)
f0ad0860
PM
308 return mrt;
309
310 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
51456b29 311 if (!mrt)
1113ebbc 312 return ERR_PTR(-ENOMEM);
8de53dfb 313 write_pnet(&mrt->net, net);
f0ad0860
PM
314 mrt->id = id;
315
316 /* Forwarding cache */
317 for (i = 0; i < MFC_LINES; i++)
318 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
319
320 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
321
322 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
323 (unsigned long)mrt);
324
f0ad0860 325 mrt->mroute_reg_vif_num = -1;
f0ad0860
PM
326#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
327 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
328#endif
329 return mrt;
330}
1da177e4 331
acbb219d
FR
332static void ipmr_free_table(struct mr_table *mrt)
333{
334 del_timer_sync(&mrt->ipmr_expire_timer);
0e615e96 335 mroute_clean_tables(mrt, true);
acbb219d
FR
336 kfree(mrt);
337}
338
1da177e4
LT
339/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
340
d607032d
WC
341static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
342{
4feb88e5
BT
343 struct net *net = dev_net(dev);
344
d607032d
WC
345 dev_close(dev);
346
4feb88e5 347 dev = __dev_get_by_name(net, "tunl0");
d607032d 348 if (dev) {
5bc3eb7e 349 const struct net_device_ops *ops = dev->netdev_ops;
d607032d 350 struct ifreq ifr;
d607032d
WC
351 struct ip_tunnel_parm p;
352
353 memset(&p, 0, sizeof(p));
354 p.iph.daddr = v->vifc_rmt_addr.s_addr;
355 p.iph.saddr = v->vifc_lcl_addr.s_addr;
356 p.iph.version = 4;
357 p.iph.ihl = 5;
358 p.iph.protocol = IPPROTO_IPIP;
359 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
360 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
361
5bc3eb7e
SH
362 if (ops->ndo_do_ioctl) {
363 mm_segment_t oldfs = get_fs();
364
365 set_fs(KERNEL_DS);
366 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
367 set_fs(oldfs);
368 }
d607032d
WC
369 }
370}
371
a0b47736
NA
372/* Initialize ipmr pimreg/tunnel in_device */
373static bool ipmr_init_vif_indev(const struct net_device *dev)
374{
375 struct in_device *in_dev;
376
377 ASSERT_RTNL();
378
379 in_dev = __in_dev_get_rtnl(dev);
380 if (!in_dev)
381 return false;
382 ipv4_devconf_setall(in_dev);
383 neigh_parms_data_state_setall(in_dev->arp_parms);
384 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
385
386 return true;
387}
388
7ef8f65d 389static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
1da177e4
LT
390{
391 struct net_device *dev;
392
4feb88e5 393 dev = __dev_get_by_name(net, "tunl0");
1da177e4
LT
394
395 if (dev) {
5bc3eb7e 396 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4
LT
397 int err;
398 struct ifreq ifr;
1da177e4 399 struct ip_tunnel_parm p;
1da177e4
LT
400
401 memset(&p, 0, sizeof(p));
402 p.iph.daddr = v->vifc_rmt_addr.s_addr;
403 p.iph.saddr = v->vifc_lcl_addr.s_addr;
404 p.iph.version = 4;
405 p.iph.ihl = 5;
406 p.iph.protocol = IPPROTO_IPIP;
407 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
ba93ef74 408 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
1da177e4 409
5bc3eb7e
SH
410 if (ops->ndo_do_ioctl) {
411 mm_segment_t oldfs = get_fs();
412
413 set_fs(KERNEL_DS);
414 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
415 set_fs(oldfs);
a8cb16dd 416 } else {
5bc3eb7e 417 err = -EOPNOTSUPP;
a8cb16dd 418 }
1da177e4
LT
419 dev = NULL;
420
4feb88e5
BT
421 if (err == 0 &&
422 (dev = __dev_get_by_name(net, p.name)) != NULL) {
1da177e4 423 dev->flags |= IFF_MULTICAST;
a0b47736 424 if (!ipmr_init_vif_indev(dev))
1da177e4 425 goto failure;
1da177e4
LT
426 if (dev_open(dev))
427 goto failure;
7dc00c82 428 dev_hold(dev);
1da177e4
LT
429 }
430 }
431 return dev;
432
433failure:
1da177e4
LT
434 unregister_netdevice(dev);
435 return NULL;
436}
437
c316c629 438#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
6fef4c0c 439static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4 440{
4feb88e5 441 struct net *net = dev_net(dev);
f0ad0860 442 struct mr_table *mrt;
da91981b
DM
443 struct flowi4 fl4 = {
444 .flowi4_oif = dev->ifindex,
6a662719 445 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
da91981b 446 .flowi4_mark = skb->mark,
f0ad0860
PM
447 };
448 int err;
449
da91981b 450 err = ipmr_fib_lookup(net, &fl4, &mrt);
e40dbc51
BG
451 if (err < 0) {
452 kfree_skb(skb);
f0ad0860 453 return err;
e40dbc51 454 }
4feb88e5 455
1da177e4 456 read_lock(&mrt_lock);
cf3677ae
PE
457 dev->stats.tx_bytes += skb->len;
458 dev->stats.tx_packets++;
0c12295a 459 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
1da177e4
LT
460 read_unlock(&mrt_lock);
461 kfree_skb(skb);
6ed10654 462 return NETDEV_TX_OK;
1da177e4
LT
463}
464
ee9b9596
ND
465static int reg_vif_get_iflink(const struct net_device *dev)
466{
467 return 0;
468}
469
007c3838
SH
470static const struct net_device_ops reg_vif_netdev_ops = {
471 .ndo_start_xmit = reg_vif_xmit,
ee9b9596 472 .ndo_get_iflink = reg_vif_get_iflink,
007c3838
SH
473};
474
1da177e4
LT
475static void reg_vif_setup(struct net_device *dev)
476{
477 dev->type = ARPHRD_PIMREG;
46f25dff 478 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
1da177e4 479 dev->flags = IFF_NOARP;
70cb4a45 480 dev->netdev_ops = &reg_vif_netdev_ops;
1da177e4 481 dev->destructor = free_netdev;
403dbb97 482 dev->features |= NETIF_F_NETNS_LOCAL;
1da177e4
LT
483}
484
f0ad0860 485static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
1da177e4
LT
486{
487 struct net_device *dev;
f0ad0860 488 char name[IFNAMSIZ];
1da177e4 489
f0ad0860
PM
490 if (mrt->id == RT_TABLE_DEFAULT)
491 sprintf(name, "pimreg");
492 else
493 sprintf(name, "pimreg%u", mrt->id);
1da177e4 494
c835a677 495 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
1da177e4 496
51456b29 497 if (!dev)
1da177e4
LT
498 return NULL;
499
403dbb97
TG
500 dev_net_set(dev, net);
501
1da177e4
LT
502 if (register_netdevice(dev)) {
503 free_netdev(dev);
504 return NULL;
505 }
1da177e4 506
a0b47736 507 if (!ipmr_init_vif_indev(dev))
1da177e4 508 goto failure;
1da177e4
LT
509 if (dev_open(dev))
510 goto failure;
511
7dc00c82
WC
512 dev_hold(dev);
513
1da177e4
LT
514 return dev;
515
516failure:
1da177e4
LT
517 unregister_netdevice(dev);
518 return NULL;
519}
c316c629
NA
520
521/* called with rcu_read_lock() */
522static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
523 unsigned int pimlen)
524{
525 struct net_device *reg_dev = NULL;
526 struct iphdr *encap;
527
528 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
7ef8f65d 529 /* Check that:
c316c629
NA
530 * a. packet is really sent to a multicast group
531 * b. packet is not a NULL-REGISTER
532 * c. packet is not truncated
533 */
534 if (!ipv4_is_multicast(encap->daddr) ||
535 encap->tot_len == 0 ||
536 ntohs(encap->tot_len) + pimlen > skb->len)
537 return 1;
538
539 read_lock(&mrt_lock);
540 if (mrt->mroute_reg_vif_num >= 0)
541 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
542 read_unlock(&mrt_lock);
543
544 if (!reg_dev)
545 return 1;
546
547 skb->mac_header = skb->network_header;
548 skb_pull(skb, (u8 *)encap - skb->data);
549 skb_reset_network_header(skb);
550 skb->protocol = htons(ETH_P_IP);
551 skb->ip_summed = CHECKSUM_NONE;
552
553 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
554
555 netif_rx(skb);
556
557 return NET_RX_SUCCESS;
558}
559#else
560static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
561{
562 return NULL;
563}
1da177e4
LT
564#endif
565
2c53040f
BH
566/**
567 * vif_delete - Delete a VIF entry
7dc00c82 568 * @notify: Set to 1, if the caller is a notifier_call
1da177e4 569 */
0c12295a 570static int vif_delete(struct mr_table *mrt, int vifi, int notify,
d17fa6fa 571 struct list_head *head)
1da177e4
LT
572{
573 struct vif_device *v;
574 struct net_device *dev;
575 struct in_device *in_dev;
576
0c12295a 577 if (vifi < 0 || vifi >= mrt->maxvif)
1da177e4
LT
578 return -EADDRNOTAVAIL;
579
0c12295a 580 v = &mrt->vif_table[vifi];
1da177e4
LT
581
582 write_lock_bh(&mrt_lock);
583 dev = v->dev;
584 v->dev = NULL;
585
586 if (!dev) {
587 write_unlock_bh(&mrt_lock);
588 return -EADDRNOTAVAIL;
589 }
590
0c12295a
PM
591 if (vifi == mrt->mroute_reg_vif_num)
592 mrt->mroute_reg_vif_num = -1;
1da177e4 593
a8cb16dd 594 if (vifi + 1 == mrt->maxvif) {
1da177e4 595 int tmp;
a8cb16dd
ED
596
597 for (tmp = vifi - 1; tmp >= 0; tmp--) {
0c12295a 598 if (VIF_EXISTS(mrt, tmp))
1da177e4
LT
599 break;
600 }
0c12295a 601 mrt->maxvif = tmp+1;
1da177e4
LT
602 }
603
604 write_unlock_bh(&mrt_lock);
605
606 dev_set_allmulti(dev, -1);
607
a8cb16dd
ED
608 in_dev = __in_dev_get_rtnl(dev);
609 if (in_dev) {
42f811b8 610 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
d67b8c61
ND
611 inet_netconf_notify_devconf(dev_net(dev),
612 NETCONFA_MC_FORWARDING,
613 dev->ifindex, &in_dev->cnf);
1da177e4
LT
614 ip_rt_multicast_event(in_dev);
615 }
616
a8cb16dd 617 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
d17fa6fa 618 unregister_netdevice_queue(dev, head);
1da177e4
LT
619
620 dev_put(dev);
621 return 0;
622}
623
a8c9486b 624static void ipmr_cache_free_rcu(struct rcu_head *head)
5c0a66f5 625{
a8c9486b
ED
626 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
627
5c0a66f5
BT
628 kmem_cache_free(mrt_cachep, c);
629}
630
a8c9486b
ED
631static inline void ipmr_cache_free(struct mfc_cache *c)
632{
633 call_rcu(&c->rcu, ipmr_cache_free_rcu);
634}
635
1da177e4 636/* Destroy an unresolved cache entry, killing queued skbs
a8cb16dd 637 * and reporting error to netlink readers.
1da177e4 638 */
0c12295a 639static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
1da177e4 640{
8de53dfb 641 struct net *net = read_pnet(&mrt->net);
1da177e4 642 struct sk_buff *skb;
9ef1d4c7 643 struct nlmsgerr *e;
1da177e4 644
0c12295a 645 atomic_dec(&mrt->cache_resolve_queue_len);
1da177e4 646
c354e124 647 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
eddc9ec5 648 if (ip_hdr(skb)->version == 0) {
1da177e4
LT
649 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
650 nlh->nlmsg_type = NLMSG_ERROR;
573ce260 651 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1da177e4 652 skb_trim(skb, nlh->nlmsg_len);
573ce260 653 e = nlmsg_data(nlh);
9ef1d4c7
PM
654 e->error = -ETIMEDOUT;
655 memset(&e->msg, 0, sizeof(e->msg));
2942e900 656
15e47304 657 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
a8cb16dd 658 } else {
1da177e4 659 kfree_skb(skb);
a8cb16dd 660 }
1da177e4
LT
661 }
662
5c0a66f5 663 ipmr_cache_free(c);
1da177e4
LT
664}
665
e258beb2 666/* Timer process for the unresolved queue. */
e258beb2 667static void ipmr_expire_process(unsigned long arg)
1da177e4 668{
0c12295a 669 struct mr_table *mrt = (struct mr_table *)arg;
1da177e4
LT
670 unsigned long now;
671 unsigned long expires;
862465f2 672 struct mfc_cache *c, *next;
1da177e4
LT
673
674 if (!spin_trylock(&mfc_unres_lock)) {
0c12295a 675 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
1da177e4
LT
676 return;
677 }
678
0c12295a 679 if (list_empty(&mrt->mfc_unres_queue))
1da177e4
LT
680 goto out;
681
682 now = jiffies;
683 expires = 10*HZ;
1da177e4 684
0c12295a 685 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1da177e4
LT
686 if (time_after(c->mfc_un.unres.expires, now)) {
687 unsigned long interval = c->mfc_un.unres.expires - now;
688 if (interval < expires)
689 expires = interval;
1da177e4
LT
690 continue;
691 }
692
862465f2 693 list_del(&c->list);
8cd3ac9f 694 mroute_netlink_event(mrt, c, RTM_DELROUTE);
0c12295a 695 ipmr_destroy_unres(mrt, c);
1da177e4
LT
696 }
697
0c12295a
PM
698 if (!list_empty(&mrt->mfc_unres_queue))
699 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
1da177e4
LT
700
701out:
702 spin_unlock(&mfc_unres_lock);
703}
704
705/* Fill oifs list. It is called under write locked mrt_lock. */
0c12295a 706static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
d658f8a0 707 unsigned char *ttls)
1da177e4
LT
708{
709 int vifi;
710
711 cache->mfc_un.res.minvif = MAXVIFS;
712 cache->mfc_un.res.maxvif = 0;
713 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
714
0c12295a
PM
715 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
716 if (VIF_EXISTS(mrt, vifi) &&
cf958ae3 717 ttls[vifi] && ttls[vifi] < 255) {
1da177e4
LT
718 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
719 if (cache->mfc_un.res.minvif > vifi)
720 cache->mfc_un.res.minvif = vifi;
721 if (cache->mfc_un.res.maxvif <= vifi)
722 cache->mfc_un.res.maxvif = vifi + 1;
723 }
724 }
90b5ca17 725 cache->mfc_un.res.lastuse = jiffies;
1da177e4
LT
726}
727
0c12295a
PM
728static int vif_add(struct net *net, struct mr_table *mrt,
729 struct vifctl *vifc, int mrtsock)
1da177e4
LT
730{
731 int vifi = vifc->vifc_vifi;
0c12295a 732 struct vif_device *v = &mrt->vif_table[vifi];
1da177e4
LT
733 struct net_device *dev;
734 struct in_device *in_dev;
d607032d 735 int err;
1da177e4
LT
736
737 /* Is vif busy ? */
0c12295a 738 if (VIF_EXISTS(mrt, vifi))
1da177e4
LT
739 return -EADDRINUSE;
740
741 switch (vifc->vifc_flags) {
1da177e4 742 case VIFF_REGISTER:
1973a4ea 743 if (!ipmr_pimsm_enabled())
c316c629
NA
744 return -EINVAL;
745 /* Special Purpose VIF in PIM
1da177e4
LT
746 * All the packets will be sent to the daemon
747 */
0c12295a 748 if (mrt->mroute_reg_vif_num >= 0)
1da177e4 749 return -EADDRINUSE;
f0ad0860 750 dev = ipmr_reg_vif(net, mrt);
1da177e4
LT
751 if (!dev)
752 return -ENOBUFS;
d607032d
WC
753 err = dev_set_allmulti(dev, 1);
754 if (err) {
755 unregister_netdevice(dev);
7dc00c82 756 dev_put(dev);
d607032d
WC
757 return err;
758 }
1da177e4 759 break;
e905a9ed 760 case VIFF_TUNNEL:
4feb88e5 761 dev = ipmr_new_tunnel(net, vifc);
1da177e4
LT
762 if (!dev)
763 return -ENOBUFS;
d607032d
WC
764 err = dev_set_allmulti(dev, 1);
765 if (err) {
766 ipmr_del_tunnel(dev, vifc);
7dc00c82 767 dev_put(dev);
d607032d
WC
768 return err;
769 }
1da177e4 770 break;
ee5e81f0 771 case VIFF_USE_IFINDEX:
1da177e4 772 case 0:
ee5e81f0
I
773 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
774 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
51456b29 775 if (dev && !__in_dev_get_rtnl(dev)) {
ee5e81f0
I
776 dev_put(dev);
777 return -EADDRNOTAVAIL;
778 }
a8cb16dd 779 } else {
ee5e81f0 780 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
a8cb16dd 781 }
1da177e4
LT
782 if (!dev)
783 return -EADDRNOTAVAIL;
d607032d 784 err = dev_set_allmulti(dev, 1);
7dc00c82
WC
785 if (err) {
786 dev_put(dev);
d607032d 787 return err;
7dc00c82 788 }
1da177e4
LT
789 break;
790 default:
791 return -EINVAL;
792 }
793
a8cb16dd
ED
794 in_dev = __in_dev_get_rtnl(dev);
795 if (!in_dev) {
d0490cfd 796 dev_put(dev);
1da177e4 797 return -EADDRNOTAVAIL;
d0490cfd 798 }
42f811b8 799 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
d67b8c61
ND
800 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
801 &in_dev->cnf);
1da177e4
LT
802 ip_rt_multicast_event(in_dev);
803
a8cb16dd
ED
804 /* Fill in the VIF structures */
805
c354e124
JK
806 v->rate_limit = vifc->vifc_rate_limit;
807 v->local = vifc->vifc_lcl_addr.s_addr;
808 v->remote = vifc->vifc_rmt_addr.s_addr;
809 v->flags = vifc->vifc_flags;
1da177e4
LT
810 if (!mrtsock)
811 v->flags |= VIFF_STATIC;
c354e124 812 v->threshold = vifc->vifc_threshold;
1da177e4
LT
813 v->bytes_in = 0;
814 v->bytes_out = 0;
815 v->pkt_in = 0;
816 v->pkt_out = 0;
817 v->link = dev->ifindex;
a8cb16dd 818 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
a54acb3a 819 v->link = dev_get_iflink(dev);
1da177e4
LT
820
821 /* And finish update writing critical data */
822 write_lock_bh(&mrt_lock);
c354e124 823 v->dev = dev;
a8cb16dd 824 if (v->flags & VIFF_REGISTER)
0c12295a 825 mrt->mroute_reg_vif_num = vifi;
0c12295a
PM
826 if (vifi+1 > mrt->maxvif)
827 mrt->maxvif = vifi+1;
1da177e4
LT
828 write_unlock_bh(&mrt_lock);
829 return 0;
830}
831
a8c9486b 832/* called with rcu_read_lock() */
0c12295a 833static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
4feb88e5
BT
834 __be32 origin,
835 __be32 mcastgrp)
1da177e4 836{
c354e124 837 int line = MFC_HASH(mcastgrp, origin);
1da177e4
LT
838 struct mfc_cache *c;
839
a8c9486b 840 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
862465f2
PM
841 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
842 return c;
1da177e4 843 }
862465f2 844 return NULL;
1da177e4
LT
845}
846
660b26dc
ND
847/* Look for a (*,*,oif) entry */
848static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
849 int vifi)
850{
360eb5da 851 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
660b26dc
ND
852 struct mfc_cache *c;
853
854 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
360eb5da
ND
855 if (c->mfc_origin == htonl(INADDR_ANY) &&
856 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
660b26dc
ND
857 c->mfc_un.res.ttls[vifi] < 255)
858 return c;
859
860 return NULL;
861}
862
863/* Look for a (*,G) entry */
864static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
865 __be32 mcastgrp, int vifi)
866{
360eb5da 867 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
660b26dc
ND
868 struct mfc_cache *c, *proxy;
869
360eb5da 870 if (mcastgrp == htonl(INADDR_ANY))
660b26dc
ND
871 goto skip;
872
873 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
360eb5da 874 if (c->mfc_origin == htonl(INADDR_ANY) &&
660b26dc
ND
875 c->mfc_mcastgrp == mcastgrp) {
876 if (c->mfc_un.res.ttls[vifi] < 255)
877 return c;
878
879 /* It's ok if the vifi is part of the static tree */
880 proxy = ipmr_cache_find_any_parent(mrt,
881 c->mfc_parent);
882 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
883 return c;
884 }
885
886skip:
887 return ipmr_cache_find_any_parent(mrt, vifi);
888}
889
7ef8f65d 890/* Allocate a multicast cache entry */
d658f8a0 891static struct mfc_cache *ipmr_cache_alloc(void)
1da177e4 892{
c354e124 893 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
a8c9486b 894
70a0dec4
TG
895 if (c) {
896 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
a8c9486b 897 c->mfc_un.res.minvif = MAXVIFS;
70a0dec4 898 }
1da177e4
LT
899 return c;
900}
901
d658f8a0 902static struct mfc_cache *ipmr_cache_alloc_unres(void)
1da177e4 903{
c354e124 904 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
a8c9486b
ED
905
906 if (c) {
907 skb_queue_head_init(&c->mfc_un.unres.unresolved);
908 c->mfc_un.unres.expires = jiffies + 10*HZ;
909 }
1da177e4
LT
910 return c;
911}
912
7ef8f65d 913/* A cache entry has gone into a resolved state from queued */
0c12295a
PM
914static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
915 struct mfc_cache *uc, struct mfc_cache *c)
1da177e4
LT
916{
917 struct sk_buff *skb;
9ef1d4c7 918 struct nlmsgerr *e;
1da177e4 919
a8cb16dd 920 /* Play the pending entries through our router */
c354e124 921 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
eddc9ec5 922 if (ip_hdr(skb)->version == 0) {
1da177e4
LT
923 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
924
573ce260 925 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
a8cb16dd
ED
926 nlh->nlmsg_len = skb_tail_pointer(skb) -
927 (u8 *)nlh;
1da177e4
LT
928 } else {
929 nlh->nlmsg_type = NLMSG_ERROR;
573ce260 930 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1da177e4 931 skb_trim(skb, nlh->nlmsg_len);
573ce260 932 e = nlmsg_data(nlh);
9ef1d4c7
PM
933 e->error = -EMSGSIZE;
934 memset(&e->msg, 0, sizeof(e->msg));
1da177e4 935 }
2942e900 936
15e47304 937 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
a8cb16dd 938 } else {
0c12295a 939 ip_mr_forward(net, mrt, skb, c, 0);
a8cb16dd 940 }
1da177e4
LT
941 }
942}
943
c316c629
NA
944/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
945 * expects the following bizarre scheme.
1da177e4 946 *
c316c629 947 * Called under mrt_lock.
1da177e4 948 */
0c12295a 949static int ipmr_cache_report(struct mr_table *mrt,
4feb88e5 950 struct sk_buff *pkt, vifi_t vifi, int assert)
1da177e4 951{
c9bdd4b5 952 const int ihl = ip_hdrlen(pkt);
c316c629 953 struct sock *mroute_sk;
1da177e4
LT
954 struct igmphdr *igmp;
955 struct igmpmsg *msg;
c316c629 956 struct sk_buff *skb;
1da177e4
LT
957 int ret;
958
1da177e4
LT
959 if (assert == IGMPMSG_WHOLEPKT)
960 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
961 else
1da177e4
LT
962 skb = alloc_skb(128, GFP_ATOMIC);
963
132adf54 964 if (!skb)
1da177e4
LT
965 return -ENOBUFS;
966
1da177e4
LT
967 if (assert == IGMPMSG_WHOLEPKT) {
968 /* Ugly, but we have no choice with this interface.
a8cb16dd
ED
969 * Duplicate old header, fix ihl, length etc.
970 * And all this only to mangle msg->im_msgtype and
971 * to set msg->im_mbz to "mbz" :-)
1da177e4 972 */
878c8145
ACM
973 skb_push(skb, sizeof(struct iphdr));
974 skb_reset_network_header(skb);
badff6d0 975 skb_reset_transport_header(skb);
0272ffc4 976 msg = (struct igmpmsg *)skb_network_header(skb);
d56f90a7 977 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1da177e4
LT
978 msg->im_msgtype = IGMPMSG_WHOLEPKT;
979 msg->im_mbz = 0;
0c12295a 980 msg->im_vif = mrt->mroute_reg_vif_num;
eddc9ec5
ACM
981 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
982 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
983 sizeof(struct iphdr));
c316c629
NA
984 } else {
985 /* Copy the IP header */
986 skb_set_network_header(skb, skb->len);
987 skb_put(skb, ihl);
988 skb_copy_to_linear_data(skb, pkt->data, ihl);
989 /* Flag to the kernel this is a route add */
990 ip_hdr(skb)->protocol = 0;
991 msg = (struct igmpmsg *)skb_network_header(skb);
992 msg->im_vif = vifi;
993 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
994 /* Add our header */
995 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
996 igmp->type = assert;
997 msg->im_msgtype = assert;
998 igmp->code = 0;
999 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1000 skb->transport_header = skb->network_header;
e905a9ed 1001 }
1da177e4 1002
4c968709
ED
1003 rcu_read_lock();
1004 mroute_sk = rcu_dereference(mrt->mroute_sk);
51456b29 1005 if (!mroute_sk) {
4c968709 1006 rcu_read_unlock();
1da177e4
LT
1007 kfree_skb(skb);
1008 return -EINVAL;
1009 }
1010
a8cb16dd 1011 /* Deliver to mrouted */
4c968709
ED
1012 ret = sock_queue_rcv_skb(mroute_sk, skb);
1013 rcu_read_unlock();
70a269e6 1014 if (ret < 0) {
e87cc472 1015 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1da177e4
LT
1016 kfree_skb(skb);
1017 }
1018
1019 return ret;
1020}
1021
7ef8f65d
NA
1022/* Queue a packet for resolution. It gets locked cache entry! */
1023static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1024 struct sk_buff *skb)
1da177e4 1025{
862465f2 1026 bool found = false;
1da177e4
LT
1027 int err;
1028 struct mfc_cache *c;
eddc9ec5 1029 const struct iphdr *iph = ip_hdr(skb);
1da177e4
LT
1030
1031 spin_lock_bh(&mfc_unres_lock);
0c12295a 1032 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
e258beb2 1033 if (c->mfc_mcastgrp == iph->daddr &&
862465f2
PM
1034 c->mfc_origin == iph->saddr) {
1035 found = true;
1da177e4 1036 break;
862465f2 1037 }
1da177e4
LT
1038 }
1039
862465f2 1040 if (!found) {
a8cb16dd 1041 /* Create a new entry if allowable */
0c12295a 1042 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
d658f8a0 1043 (c = ipmr_cache_alloc_unres()) == NULL) {
1da177e4
LT
1044 spin_unlock_bh(&mfc_unres_lock);
1045
1046 kfree_skb(skb);
1047 return -ENOBUFS;
1048 }
1049
a8cb16dd 1050 /* Fill in the new cache entry */
eddc9ec5
ACM
1051 c->mfc_parent = -1;
1052 c->mfc_origin = iph->saddr;
1053 c->mfc_mcastgrp = iph->daddr;
1da177e4 1054
a8cb16dd 1055 /* Reflect first query at mrouted. */
0c12295a 1056 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
4feb88e5 1057 if (err < 0) {
e905a9ed 1058 /* If the report failed throw the cache entry
1da177e4
LT
1059 out - Brad Parker
1060 */
1061 spin_unlock_bh(&mfc_unres_lock);
1062
5c0a66f5 1063 ipmr_cache_free(c);
1da177e4
LT
1064 kfree_skb(skb);
1065 return err;
1066 }
1067
0c12295a
PM
1068 atomic_inc(&mrt->cache_resolve_queue_len);
1069 list_add(&c->list, &mrt->mfc_unres_queue);
8cd3ac9f 1070 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1da177e4 1071
278554bd
DM
1072 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1073 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1da177e4
LT
1074 }
1075
a8cb16dd 1076 /* See if we can append the packet */
a8cb16dd 1077 if (c->mfc_un.unres.unresolved.qlen > 3) {
1da177e4
LT
1078 kfree_skb(skb);
1079 err = -ENOBUFS;
1080 } else {
c354e124 1081 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1da177e4
LT
1082 err = 0;
1083 }
1084
1085 spin_unlock_bh(&mfc_unres_lock);
1086 return err;
1087}
1088
7ef8f65d 1089/* MFC cache manipulation by user space mroute daemon */
1da177e4 1090
660b26dc 1091static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1da177e4
LT
1092{
1093 int line;
862465f2 1094 struct mfc_cache *c, *next;
1da177e4 1095
c354e124 1096 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1da177e4 1097
0c12295a 1098 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1da177e4 1099 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
660b26dc
ND
1100 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1101 (parent == -1 || parent == c->mfc_parent)) {
a8c9486b 1102 list_del_rcu(&c->list);
8cd3ac9f 1103 mroute_netlink_event(mrt, c, RTM_DELROUTE);
5c0a66f5 1104 ipmr_cache_free(c);
1da177e4
LT
1105 return 0;
1106 }
1107 }
1108 return -ENOENT;
1109}
1110
0c12295a 1111static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
660b26dc 1112 struct mfcctl *mfc, int mrtsock, int parent)
1da177e4 1113{
862465f2 1114 bool found = false;
1da177e4 1115 int line;
862465f2 1116 struct mfc_cache *uc, *c;
1da177e4 1117
a50436f2
PM
1118 if (mfc->mfcc_parent >= MAXVIFS)
1119 return -ENFILE;
1120
c354e124 1121 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1da177e4 1122
0c12295a 1123 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1da177e4 1124 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
660b26dc
ND
1125 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1126 (parent == -1 || parent == c->mfc_parent)) {
862465f2 1127 found = true;
1da177e4 1128 break;
862465f2 1129 }
1da177e4
LT
1130 }
1131
862465f2 1132 if (found) {
1da177e4
LT
1133 write_lock_bh(&mrt_lock);
1134 c->mfc_parent = mfc->mfcc_parent;
0c12295a 1135 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1da177e4
LT
1136 if (!mrtsock)
1137 c->mfc_flags |= MFC_STATIC;
1138 write_unlock_bh(&mrt_lock);
8cd3ac9f 1139 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1da177e4
LT
1140 return 0;
1141 }
1142
360eb5da 1143 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
660b26dc 1144 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1da177e4
LT
1145 return -EINVAL;
1146
d658f8a0 1147 c = ipmr_cache_alloc();
51456b29 1148 if (!c)
1da177e4
LT
1149 return -ENOMEM;
1150
c354e124
JK
1151 c->mfc_origin = mfc->mfcc_origin.s_addr;
1152 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1153 c->mfc_parent = mfc->mfcc_parent;
0c12295a 1154 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1da177e4
LT
1155 if (!mrtsock)
1156 c->mfc_flags |= MFC_STATIC;
1157
a8c9486b 1158 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1da177e4 1159
7ef8f65d
NA
1160 /* Check to see if we resolved a queued list. If so we
1161 * need to send on the frames and tidy up.
1da177e4 1162 */
b0ebb739 1163 found = false;
1da177e4 1164 spin_lock_bh(&mfc_unres_lock);
0c12295a 1165 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
e258beb2 1166 if (uc->mfc_origin == c->mfc_origin &&
1da177e4 1167 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
862465f2 1168 list_del(&uc->list);
0c12295a 1169 atomic_dec(&mrt->cache_resolve_queue_len);
b0ebb739 1170 found = true;
1da177e4
LT
1171 break;
1172 }
1173 }
0c12295a
PM
1174 if (list_empty(&mrt->mfc_unres_queue))
1175 del_timer(&mrt->ipmr_expire_timer);
1da177e4
LT
1176 spin_unlock_bh(&mfc_unres_lock);
1177
b0ebb739 1178 if (found) {
0c12295a 1179 ipmr_cache_resolve(net, mrt, uc, c);
5c0a66f5 1180 ipmr_cache_free(uc);
1da177e4 1181 }
8cd3ac9f 1182 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1da177e4
LT
1183 return 0;
1184}
1185
7ef8f65d 1186/* Close the multicast socket, and clear the vif tables etc */
0e615e96 1187static void mroute_clean_tables(struct mr_table *mrt, bool all)
1da177e4
LT
1188{
1189 int i;
d17fa6fa 1190 LIST_HEAD(list);
862465f2 1191 struct mfc_cache *c, *next;
e905a9ed 1192
a8cb16dd 1193 /* Shut down all active vif entries */
0c12295a 1194 for (i = 0; i < mrt->maxvif; i++) {
0e615e96
NA
1195 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1196 continue;
1197 vif_delete(mrt, i, 0, &list);
1da177e4 1198 }
d17fa6fa 1199 unregister_netdevice_many(&list);
1da177e4 1200
a8cb16dd 1201 /* Wipe the cache */
862465f2 1202 for (i = 0; i < MFC_LINES; i++) {
0c12295a 1203 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
0e615e96 1204 if (!all && (c->mfc_flags & MFC_STATIC))
1da177e4 1205 continue;
a8c9486b 1206 list_del_rcu(&c->list);
8cd3ac9f 1207 mroute_netlink_event(mrt, c, RTM_DELROUTE);
5c0a66f5 1208 ipmr_cache_free(c);
1da177e4
LT
1209 }
1210 }
1211
0c12295a 1212 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1da177e4 1213 spin_lock_bh(&mfc_unres_lock);
0c12295a 1214 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
862465f2 1215 list_del(&c->list);
8cd3ac9f 1216 mroute_netlink_event(mrt, c, RTM_DELROUTE);
0c12295a 1217 ipmr_destroy_unres(mrt, c);
1da177e4
LT
1218 }
1219 spin_unlock_bh(&mfc_unres_lock);
1220 }
1221}
1222
4c968709
ED
1223/* called from ip_ra_control(), before an RCU grace period,
1224 * we dont need to call synchronize_rcu() here
1225 */
1da177e4
LT
1226static void mrtsock_destruct(struct sock *sk)
1227{
4feb88e5 1228 struct net *net = sock_net(sk);
f0ad0860 1229 struct mr_table *mrt;
4feb88e5 1230
1da177e4 1231 rtnl_lock();
f0ad0860 1232 ipmr_for_each_table(mrt, net) {
4c968709 1233 if (sk == rtnl_dereference(mrt->mroute_sk)) {
f0ad0860 1234 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
d67b8c61
ND
1235 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1236 NETCONFA_IFINDEX_ALL,
1237 net->ipv4.devconf_all);
a9b3cd7f 1238 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
0e615e96 1239 mroute_clean_tables(mrt, false);
f0ad0860 1240 }
1da177e4
LT
1241 }
1242 rtnl_unlock();
1243}
1244
7ef8f65d
NA
1245/* Socket options and virtual interface manipulation. The whole
1246 * virtual interface system is a complete heap, but unfortunately
1247 * that's how BSD mrouted happens to think. Maybe one day with a proper
1248 * MOSPF/PIM router set up we can clean this up.
1da177e4 1249 */
e905a9ed 1250
29e97d21
NA
1251int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1252 unsigned int optlen)
1da177e4 1253{
4feb88e5 1254 struct net *net = sock_net(sk);
29e97d21 1255 int val, ret = 0, parent = 0;
f0ad0860 1256 struct mr_table *mrt;
29e97d21
NA
1257 struct vifctl vif;
1258 struct mfcctl mfc;
1259 u32 uval;
f0ad0860 1260
29e97d21
NA
1261 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1262 rtnl_lock();
5e1859fb 1263 if (sk->sk_type != SOCK_RAW ||
29e97d21
NA
1264 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1265 ret = -EOPNOTSUPP;
1266 goto out_unlock;
1267 }
5e1859fb 1268
f0ad0860 1269 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
29e97d21
NA
1270 if (!mrt) {
1271 ret = -ENOENT;
1272 goto out_unlock;
1273 }
132adf54 1274 if (optname != MRT_INIT) {
33d480ce 1275 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
29e97d21
NA
1276 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1277 ret = -EACCES;
1278 goto out_unlock;
1279 }
1da177e4
LT
1280 }
1281
132adf54
SH
1282 switch (optname) {
1283 case MRT_INIT:
42e6b89c 1284 if (optlen != sizeof(int)) {
29e97d21 1285 ret = -EINVAL;
42e6b89c
NA
1286 break;
1287 }
1288 if (rtnl_dereference(mrt->mroute_sk)) {
29e97d21 1289 ret = -EADDRINUSE;
29e97d21 1290 break;
42e6b89c 1291 }
132adf54
SH
1292
1293 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1294 if (ret == 0) {
cf778b00 1295 rcu_assign_pointer(mrt->mroute_sk, sk);
4feb88e5 1296 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
d67b8c61
ND
1297 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1298 NETCONFA_IFINDEX_ALL,
1299 net->ipv4.devconf_all);
132adf54 1300 }
29e97d21 1301 break;
132adf54 1302 case MRT_DONE:
29e97d21
NA
1303 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1304 ret = -EACCES;
1305 } else {
1306 /* We need to unlock here because mrtsock_destruct takes
1307 * care of rtnl itself and we can't change that due to
1308 * the IP_ROUTER_ALERT setsockopt which runs without it.
1309 */
1310 rtnl_unlock();
1311 ret = ip_ra_control(sk, 0, NULL);
1312 goto out;
1313 }
1314 break;
132adf54
SH
1315 case MRT_ADD_VIF:
1316 case MRT_DEL_VIF:
29e97d21
NA
1317 if (optlen != sizeof(vif)) {
1318 ret = -EINVAL;
1319 break;
1320 }
1321 if (copy_from_user(&vif, optval, sizeof(vif))) {
1322 ret = -EFAULT;
1323 break;
1324 }
1325 if (vif.vifc_vifi >= MAXVIFS) {
1326 ret = -ENFILE;
1327 break;
1328 }
c354e124 1329 if (optname == MRT_ADD_VIF) {
4c968709
ED
1330 ret = vif_add(net, mrt, &vif,
1331 sk == rtnl_dereference(mrt->mroute_sk));
132adf54 1332 } else {
0c12295a 1333 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
132adf54 1334 }
29e97d21 1335 break;
7ef8f65d
NA
1336 /* Manipulate the forwarding caches. These live
1337 * in a sort of kernel/user symbiosis.
1338 */
132adf54
SH
1339 case MRT_ADD_MFC:
1340 case MRT_DEL_MFC:
660b26dc
ND
1341 parent = -1;
1342 case MRT_ADD_MFC_PROXY:
1343 case MRT_DEL_MFC_PROXY:
29e97d21
NA
1344 if (optlen != sizeof(mfc)) {
1345 ret = -EINVAL;
1346 break;
1347 }
1348 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1349 ret = -EFAULT;
1350 break;
1351 }
660b26dc
ND
1352 if (parent == 0)
1353 parent = mfc.mfcc_parent;
660b26dc
ND
1354 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1355 ret = ipmr_mfc_delete(mrt, &mfc, parent);
132adf54 1356 else
4c968709 1357 ret = ipmr_mfc_add(net, mrt, &mfc,
660b26dc
ND
1358 sk == rtnl_dereference(mrt->mroute_sk),
1359 parent);
29e97d21 1360 break;
7ef8f65d 1361 /* Control PIM assert. */
132adf54 1362 case MRT_ASSERT:
29e97d21
NA
1363 if (optlen != sizeof(val)) {
1364 ret = -EINVAL;
1365 break;
1366 }
1367 if (get_user(val, (int __user *)optval)) {
1368 ret = -EFAULT;
1369 break;
1370 }
1371 mrt->mroute_do_assert = val;
1372 break;
132adf54 1373 case MRT_PIM:
1973a4ea 1374 if (!ipmr_pimsm_enabled()) {
29e97d21
NA
1375 ret = -ENOPROTOOPT;
1376 break;
1377 }
1378 if (optlen != sizeof(val)) {
1379 ret = -EINVAL;
1380 break;
1381 }
1382 if (get_user(val, (int __user *)optval)) {
1383 ret = -EFAULT;
1384 break;
1385 }
ba93ef74 1386
29e97d21
NA
1387 val = !!val;
1388 if (val != mrt->mroute_do_pim) {
1389 mrt->mroute_do_pim = val;
1390 mrt->mroute_do_assert = val;
1da177e4 1391 }
29e97d21 1392 break;
f0ad0860 1393 case MRT_TABLE:
29e97d21
NA
1394 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1395 ret = -ENOPROTOOPT;
1396 break;
1397 }
1398 if (optlen != sizeof(uval)) {
1399 ret = -EINVAL;
1400 break;
1401 }
1402 if (get_user(uval, (u32 __user *)optval)) {
1403 ret = -EFAULT;
1404 break;
1405 }
f0ad0860 1406
4c968709
ED
1407 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1408 ret = -EBUSY;
1409 } else {
29e97d21 1410 mrt = ipmr_new_table(net, uval);
1113ebbc
NA
1411 if (IS_ERR(mrt))
1412 ret = PTR_ERR(mrt);
5e1859fb 1413 else
29e97d21 1414 raw_sk(sk)->ipmr_table = uval;
4c968709 1415 }
29e97d21 1416 break;
7ef8f65d 1417 /* Spurious command, or MRT_VERSION which you cannot set. */
132adf54 1418 default:
29e97d21 1419 ret = -ENOPROTOOPT;
1da177e4 1420 }
29e97d21
NA
1421out_unlock:
1422 rtnl_unlock();
1423out:
1424 return ret;
1da177e4
LT
1425}
1426
7ef8f65d 1427/* Getsock opt support for the multicast routing system. */
c354e124 1428int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1da177e4
LT
1429{
1430 int olr;
1431 int val;
4feb88e5 1432 struct net *net = sock_net(sk);
f0ad0860
PM
1433 struct mr_table *mrt;
1434
5e1859fb
ED
1435 if (sk->sk_type != SOCK_RAW ||
1436 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1437 return -EOPNOTSUPP;
1438
f0ad0860 1439 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
51456b29 1440 if (!mrt)
f0ad0860 1441 return -ENOENT;
1da177e4 1442
fe9ef3ce
NA
1443 switch (optname) {
1444 case MRT_VERSION:
1445 val = 0x0305;
1446 break;
1447 case MRT_PIM:
1973a4ea 1448 if (!ipmr_pimsm_enabled())
fe9ef3ce
NA
1449 return -ENOPROTOOPT;
1450 val = mrt->mroute_do_pim;
1451 break;
1452 case MRT_ASSERT:
1453 val = mrt->mroute_do_assert;
1454 break;
1455 default:
1da177e4 1456 return -ENOPROTOOPT;
fe9ef3ce 1457 }
1da177e4
LT
1458
1459 if (get_user(olr, optlen))
1460 return -EFAULT;
1da177e4
LT
1461 olr = min_t(unsigned int, olr, sizeof(int));
1462 if (olr < 0)
1463 return -EINVAL;
c354e124 1464 if (put_user(olr, optlen))
1da177e4 1465 return -EFAULT;
c354e124 1466 if (copy_to_user(optval, &val, olr))
1da177e4
LT
1467 return -EFAULT;
1468 return 0;
1469}
1470
7ef8f65d 1471/* The IP multicast ioctl support routines. */
1da177e4
LT
1472int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1473{
1474 struct sioc_sg_req sr;
1475 struct sioc_vif_req vr;
1476 struct vif_device *vif;
1477 struct mfc_cache *c;
4feb88e5 1478 struct net *net = sock_net(sk);
f0ad0860
PM
1479 struct mr_table *mrt;
1480
1481 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
51456b29 1482 if (!mrt)
f0ad0860 1483 return -ENOENT;
e905a9ed 1484
132adf54
SH
1485 switch (cmd) {
1486 case SIOCGETVIFCNT:
c354e124 1487 if (copy_from_user(&vr, arg, sizeof(vr)))
132adf54 1488 return -EFAULT;
0c12295a 1489 if (vr.vifi >= mrt->maxvif)
132adf54
SH
1490 return -EINVAL;
1491 read_lock(&mrt_lock);
0c12295a
PM
1492 vif = &mrt->vif_table[vr.vifi];
1493 if (VIF_EXISTS(mrt, vr.vifi)) {
c354e124
JK
1494 vr.icount = vif->pkt_in;
1495 vr.ocount = vif->pkt_out;
1496 vr.ibytes = vif->bytes_in;
1497 vr.obytes = vif->bytes_out;
1da177e4 1498 read_unlock(&mrt_lock);
1da177e4 1499
c354e124 1500 if (copy_to_user(arg, &vr, sizeof(vr)))
132adf54
SH
1501 return -EFAULT;
1502 return 0;
1503 }
1504 read_unlock(&mrt_lock);
1505 return -EADDRNOTAVAIL;
1506 case SIOCGETSGCNT:
c354e124 1507 if (copy_from_user(&sr, arg, sizeof(sr)))
132adf54
SH
1508 return -EFAULT;
1509
a8c9486b 1510 rcu_read_lock();
0c12295a 1511 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
132adf54
SH
1512 if (c) {
1513 sr.pktcnt = c->mfc_un.res.pkt;
1514 sr.bytecnt = c->mfc_un.res.bytes;
1515 sr.wrong_if = c->mfc_un.res.wrong_if;
a8c9486b 1516 rcu_read_unlock();
132adf54 1517
c354e124 1518 if (copy_to_user(arg, &sr, sizeof(sr)))
132adf54
SH
1519 return -EFAULT;
1520 return 0;
1521 }
a8c9486b 1522 rcu_read_unlock();
132adf54
SH
1523 return -EADDRNOTAVAIL;
1524 default:
1525 return -ENOIOCTLCMD;
1da177e4
LT
1526 }
1527}
1528
709b46e8
EB
1529#ifdef CONFIG_COMPAT
1530struct compat_sioc_sg_req {
1531 struct in_addr src;
1532 struct in_addr grp;
1533 compat_ulong_t pktcnt;
1534 compat_ulong_t bytecnt;
1535 compat_ulong_t wrong_if;
1536};
1537
ca6b8bb0
DM
1538struct compat_sioc_vif_req {
1539 vifi_t vifi; /* Which iface */
1540 compat_ulong_t icount;
1541 compat_ulong_t ocount;
1542 compat_ulong_t ibytes;
1543 compat_ulong_t obytes;
1544};
1545
709b46e8
EB
1546int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1547{
0033d5ad 1548 struct compat_sioc_sg_req sr;
ca6b8bb0
DM
1549 struct compat_sioc_vif_req vr;
1550 struct vif_device *vif;
709b46e8
EB
1551 struct mfc_cache *c;
1552 struct net *net = sock_net(sk);
1553 struct mr_table *mrt;
1554
1555 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
51456b29 1556 if (!mrt)
709b46e8
EB
1557 return -ENOENT;
1558
1559 switch (cmd) {
ca6b8bb0
DM
1560 case SIOCGETVIFCNT:
1561 if (copy_from_user(&vr, arg, sizeof(vr)))
1562 return -EFAULT;
1563 if (vr.vifi >= mrt->maxvif)
1564 return -EINVAL;
1565 read_lock(&mrt_lock);
1566 vif = &mrt->vif_table[vr.vifi];
1567 if (VIF_EXISTS(mrt, vr.vifi)) {
1568 vr.icount = vif->pkt_in;
1569 vr.ocount = vif->pkt_out;
1570 vr.ibytes = vif->bytes_in;
1571 vr.obytes = vif->bytes_out;
1572 read_unlock(&mrt_lock);
1573
1574 if (copy_to_user(arg, &vr, sizeof(vr)))
1575 return -EFAULT;
1576 return 0;
1577 }
1578 read_unlock(&mrt_lock);
1579 return -EADDRNOTAVAIL;
709b46e8
EB
1580 case SIOCGETSGCNT:
1581 if (copy_from_user(&sr, arg, sizeof(sr)))
1582 return -EFAULT;
1583
1584 rcu_read_lock();
1585 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1586 if (c) {
1587 sr.pktcnt = c->mfc_un.res.pkt;
1588 sr.bytecnt = c->mfc_un.res.bytes;
1589 sr.wrong_if = c->mfc_un.res.wrong_if;
1590 rcu_read_unlock();
1591
1592 if (copy_to_user(arg, &sr, sizeof(sr)))
1593 return -EFAULT;
1594 return 0;
1595 }
1596 rcu_read_unlock();
1597 return -EADDRNOTAVAIL;
1598 default:
1599 return -ENOIOCTLCMD;
1600 }
1601}
1602#endif
1603
1da177e4
LT
1604static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1605{
351638e7 1606 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4feb88e5 1607 struct net *net = dev_net(dev);
f0ad0860 1608 struct mr_table *mrt;
1da177e4
LT
1609 struct vif_device *v;
1610 int ct;
e9dc8653 1611
1da177e4
LT
1612 if (event != NETDEV_UNREGISTER)
1613 return NOTIFY_DONE;
f0ad0860
PM
1614
1615 ipmr_for_each_table(mrt, net) {
1616 v = &mrt->vif_table[0];
1617 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1618 if (v->dev == dev)
e92036a6 1619 vif_delete(mrt, ct, 1, NULL);
f0ad0860 1620 }
1da177e4
LT
1621 }
1622 return NOTIFY_DONE;
1623}
1624
c354e124 1625static struct notifier_block ip_mr_notifier = {
1da177e4
LT
1626 .notifier_call = ipmr_device_event,
1627};
1628
7ef8f65d
NA
1629/* Encapsulate a packet by attaching a valid IPIP header to it.
1630 * This avoids tunnel drivers and other mess and gives us the speed so
1631 * important for multicast video.
1da177e4 1632 */
b6a7719a
HFS
1633static void ip_encap(struct net *net, struct sk_buff *skb,
1634 __be32 saddr, __be32 daddr)
1da177e4 1635{
8856dfa3 1636 struct iphdr *iph;
b71d1d42 1637 const struct iphdr *old_iph = ip_hdr(skb);
8856dfa3
ACM
1638
1639 skb_push(skb, sizeof(struct iphdr));
b0e380b1 1640 skb->transport_header = skb->network_header;
8856dfa3 1641 skb_reset_network_header(skb);
eddc9ec5 1642 iph = ip_hdr(skb);
1da177e4 1643
a8cb16dd 1644 iph->version = 4;
e023dd64
ACM
1645 iph->tos = old_iph->tos;
1646 iph->ttl = old_iph->ttl;
1da177e4
LT
1647 iph->frag_off = 0;
1648 iph->daddr = daddr;
1649 iph->saddr = saddr;
1650 iph->protocol = IPPROTO_IPIP;
1651 iph->ihl = 5;
1652 iph->tot_len = htons(skb->len);
b6a7719a 1653 ip_select_ident(net, skb, NULL);
1da177e4
LT
1654 ip_send_check(iph);
1655
1da177e4
LT
1656 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1657 nf_reset(skb);
1658}
1659
0c4b51f0
EB
1660static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1661 struct sk_buff *skb)
1da177e4 1662{
a8cb16dd 1663 struct ip_options *opt = &(IPCB(skb)->opt);
1da177e4 1664
73186df8
DM
1665 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1666 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1da177e4
LT
1667
1668 if (unlikely(opt->optlen))
1669 ip_forward_options(skb);
1670
13206b6b 1671 return dst_output(net, sk, skb);
1da177e4
LT
1672}
1673
7ef8f65d 1674/* Processing handlers for ipmr_forward */
1da177e4 1675
0c12295a
PM
1676static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1677 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1da177e4 1678{
eddc9ec5 1679 const struct iphdr *iph = ip_hdr(skb);
0c12295a 1680 struct vif_device *vif = &mrt->vif_table[vifi];
1da177e4
LT
1681 struct net_device *dev;
1682 struct rtable *rt;
31e4543d 1683 struct flowi4 fl4;
1da177e4
LT
1684 int encap = 0;
1685
51456b29 1686 if (!vif->dev)
1da177e4
LT
1687 goto out_free;
1688
1da177e4
LT
1689 if (vif->flags & VIFF_REGISTER) {
1690 vif->pkt_out++;
c354e124 1691 vif->bytes_out += skb->len;
cf3677ae
PE
1692 vif->dev->stats.tx_bytes += skb->len;
1693 vif->dev->stats.tx_packets++;
0c12295a 1694 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
69ebbf58 1695 goto out_free;
1da177e4 1696 }
1da177e4 1697
a8cb16dd 1698 if (vif->flags & VIFF_TUNNEL) {
31e4543d 1699 rt = ip_route_output_ports(net, &fl4, NULL,
78fbfd8a
DM
1700 vif->remote, vif->local,
1701 0, 0,
1702 IPPROTO_IPIP,
1703 RT_TOS(iph->tos), vif->link);
b23dd4fe 1704 if (IS_ERR(rt))
1da177e4
LT
1705 goto out_free;
1706 encap = sizeof(struct iphdr);
1707 } else {
31e4543d 1708 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
78fbfd8a
DM
1709 0, 0,
1710 IPPROTO_IPIP,
1711 RT_TOS(iph->tos), vif->link);
b23dd4fe 1712 if (IS_ERR(rt))
1da177e4
LT
1713 goto out_free;
1714 }
1715
d8d1f30b 1716 dev = rt->dst.dev;
1da177e4 1717
d8d1f30b 1718 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1da177e4 1719 /* Do not fragment multicasts. Alas, IPv4 does not
a8cb16dd
ED
1720 * allow to send ICMP, so that packets will disappear
1721 * to blackhole.
1da177e4 1722 */
73186df8 1723 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1da177e4
LT
1724 ip_rt_put(rt);
1725 goto out_free;
1726 }
1727
d8d1f30b 1728 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1da177e4
LT
1729
1730 if (skb_cow(skb, encap)) {
e905a9ed 1731 ip_rt_put(rt);
1da177e4
LT
1732 goto out_free;
1733 }
1734
1735 vif->pkt_out++;
c354e124 1736 vif->bytes_out += skb->len;
1da177e4 1737
adf30907 1738 skb_dst_drop(skb);
d8d1f30b 1739 skb_dst_set(skb, &rt->dst);
eddc9ec5 1740 ip_decrease_ttl(ip_hdr(skb));
1da177e4
LT
1741
1742 /* FIXME: forward and output firewalls used to be called here.
a8cb16dd
ED
1743 * What do we do with netfilter? -- RR
1744 */
1da177e4 1745 if (vif->flags & VIFF_TUNNEL) {
b6a7719a 1746 ip_encap(net, skb, vif->local, vif->remote);
1da177e4 1747 /* FIXME: extra output firewall step used to be here. --RR */
2f4c02d4
PE
1748 vif->dev->stats.tx_packets++;
1749 vif->dev->stats.tx_bytes += skb->len;
1da177e4
LT
1750 }
1751
9ee6c5dc 1752 IPCB(skb)->flags |= IPSKB_FORWARDED;
1da177e4 1753
7ef8f65d 1754 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1da177e4
LT
1755 * not only before forwarding, but after forwarding on all output
1756 * interfaces. It is clear, if mrouter runs a multicasting
1757 * program, it should receive packets not depending to what interface
1758 * program is joined.
1759 * If we will not make it, the program will have to join on all
1760 * interfaces. On the other hand, multihoming host (or router, but
1761 * not mrouter) cannot join to more than one interface - it will
1762 * result in receiving multiple packets.
1763 */
29a26a56
EB
1764 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1765 net, NULL, skb, skb->dev, dev,
1da177e4
LT
1766 ipmr_forward_finish);
1767 return;
1768
1769out_free:
1770 kfree_skb(skb);
1da177e4
LT
1771}
1772
0c12295a 1773static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1da177e4
LT
1774{
1775 int ct;
0c12295a
PM
1776
1777 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1778 if (mrt->vif_table[ct].dev == dev)
1da177e4
LT
1779 break;
1780 }
1781 return ct;
1782}
1783
1784/* "local" means that we should preserve one skb (for local delivery) */
c4854ec8
RR
1785static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1786 struct sk_buff *skb, struct mfc_cache *cache,
1787 int local)
1da177e4
LT
1788{
1789 int psend = -1;
1790 int vif, ct;
660b26dc 1791 int true_vifi = ipmr_find_vif(mrt, skb->dev);
1da177e4
LT
1792
1793 vif = cache->mfc_parent;
1794 cache->mfc_un.res.pkt++;
1795 cache->mfc_un.res.bytes += skb->len;
43b9e127 1796 cache->mfc_un.res.lastuse = jiffies;
1da177e4 1797
360eb5da 1798 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
660b26dc
ND
1799 struct mfc_cache *cache_proxy;
1800
1801 /* For an (*,G) entry, we only check that the incomming
1802 * interface is part of the static tree.
1803 */
1804 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1805 if (cache_proxy &&
1806 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1807 goto forward;
1808 }
1809
7ef8f65d 1810 /* Wrong interface: drop packet and (maybe) send PIM assert. */
0c12295a 1811 if (mrt->vif_table[vif].dev != skb->dev) {
c7537967 1812 if (rt_is_output_route(skb_rtable(skb))) {
1da177e4 1813 /* It is our own packet, looped back.
a8cb16dd
ED
1814 * Very complicated situation...
1815 *
1816 * The best workaround until routing daemons will be
1817 * fixed is not to redistribute packet, if it was
1818 * send through wrong interface. It means, that
1819 * multicast applications WILL NOT work for
1820 * (S,G), which have default multicast route pointing
1821 * to wrong oif. In any case, it is not a good
1822 * idea to use multicasting applications on router.
1da177e4
LT
1823 */
1824 goto dont_forward;
1825 }
1826
1827 cache->mfc_un.res.wrong_if++;
1da177e4 1828
0c12295a 1829 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1da177e4 1830 /* pimsm uses asserts, when switching from RPT to SPT,
a8cb16dd
ED
1831 * so that we cannot check that packet arrived on an oif.
1832 * It is bad, but otherwise we would need to move pretty
1833 * large chunk of pimd to kernel. Ough... --ANK
1da177e4 1834 */
0c12295a 1835 (mrt->mroute_do_pim ||
6f9374a9 1836 cache->mfc_un.res.ttls[true_vifi] < 255) &&
e905a9ed 1837 time_after(jiffies,
1da177e4
LT
1838 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1839 cache->mfc_un.res.last_assert = jiffies;
0c12295a 1840 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1da177e4
LT
1841 }
1842 goto dont_forward;
1843 }
1844
660b26dc 1845forward:
0c12295a
PM
1846 mrt->vif_table[vif].pkt_in++;
1847 mrt->vif_table[vif].bytes_in += skb->len;
1da177e4 1848
7ef8f65d 1849 /* Forward the frame */
360eb5da
ND
1850 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1851 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
660b26dc
ND
1852 if (true_vifi >= 0 &&
1853 true_vifi != cache->mfc_parent &&
1854 ip_hdr(skb)->ttl >
1855 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1856 /* It's an (*,*) entry and the packet is not coming from
1857 * the upstream: forward the packet to the upstream
1858 * only.
1859 */
1860 psend = cache->mfc_parent;
1861 goto last_forward;
1862 }
1863 goto dont_forward;
1864 }
a8cb16dd
ED
1865 for (ct = cache->mfc_un.res.maxvif - 1;
1866 ct >= cache->mfc_un.res.minvif; ct--) {
660b26dc 1867 /* For (*,G) entry, don't forward to the incoming interface */
360eb5da
ND
1868 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1869 ct != true_vifi) &&
660b26dc 1870 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1da177e4
LT
1871 if (psend != -1) {
1872 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
a8cb16dd 1873
1da177e4 1874 if (skb2)
0c12295a
PM
1875 ipmr_queue_xmit(net, mrt, skb2, cache,
1876 psend);
1da177e4 1877 }
c354e124 1878 psend = ct;
1da177e4
LT
1879 }
1880 }
660b26dc 1881last_forward:
1da177e4
LT
1882 if (psend != -1) {
1883 if (local) {
1884 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
a8cb16dd 1885
1da177e4 1886 if (skb2)
0c12295a 1887 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1da177e4 1888 } else {
0c12295a 1889 ipmr_queue_xmit(net, mrt, skb, cache, psend);
c4854ec8 1890 return;
1da177e4
LT
1891 }
1892 }
1893
1894dont_forward:
1895 if (!local)
1896 kfree_skb(skb);
1da177e4
LT
1897}
1898
417da66f 1899static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
ee3f1aaf 1900{
417da66f
DM
1901 struct rtable *rt = skb_rtable(skb);
1902 struct iphdr *iph = ip_hdr(skb);
da91981b 1903 struct flowi4 fl4 = {
417da66f
DM
1904 .daddr = iph->daddr,
1905 .saddr = iph->saddr,
b0fe4a31 1906 .flowi4_tos = RT_TOS(iph->tos),
4fd551d7
DM
1907 .flowi4_oif = (rt_is_output_route(rt) ?
1908 skb->dev->ifindex : 0),
1909 .flowi4_iif = (rt_is_output_route(rt) ?
1fb9489b 1910 LOOPBACK_IFINDEX :
4fd551d7 1911 skb->dev->ifindex),
b4869889 1912 .flowi4_mark = skb->mark,
ee3f1aaf
DM
1913 };
1914 struct mr_table *mrt;
1915 int err;
1916
da91981b 1917 err = ipmr_fib_lookup(net, &fl4, &mrt);
ee3f1aaf
DM
1918 if (err)
1919 return ERR_PTR(err);
1920 return mrt;
1921}
1da177e4 1922
7ef8f65d
NA
1923/* Multicast packets for forwarding arrive here
1924 * Called with rcu_read_lock();
1da177e4 1925 */
1da177e4
LT
1926int ip_mr_input(struct sk_buff *skb)
1927{
1928 struct mfc_cache *cache;
4feb88e5 1929 struct net *net = dev_net(skb->dev);
511c3f92 1930 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
f0ad0860 1931 struct mr_table *mrt;
1da177e4
LT
1932
1933 /* Packet is looped back after forward, it should not be
a8cb16dd 1934 * forwarded second time, but still can be delivered locally.
1da177e4 1935 */
4c968709 1936 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1da177e4
LT
1937 goto dont_forward;
1938
417da66f 1939 mrt = ipmr_rt_fib_lookup(net, skb);
ee3f1aaf
DM
1940 if (IS_ERR(mrt)) {
1941 kfree_skb(skb);
1942 return PTR_ERR(mrt);
e40dbc51 1943 }
1da177e4 1944 if (!local) {
4c968709
ED
1945 if (IPCB(skb)->opt.router_alert) {
1946 if (ip_call_ra_chain(skb))
1947 return 0;
1948 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1949 /* IGMPv1 (and broken IGMPv2 implementations sort of
1950 * Cisco IOS <= 11.2(8)) do not put router alert
1951 * option to IGMP packets destined to routable
1952 * groups. It is very bad, because it means
1953 * that we can forward NO IGMP messages.
1954 */
1955 struct sock *mroute_sk;
1956
1957 mroute_sk = rcu_dereference(mrt->mroute_sk);
1958 if (mroute_sk) {
1959 nf_reset(skb);
1960 raw_rcv(mroute_sk, skb);
1961 return 0;
1962 }
1da177e4
LT
1963 }
1964 }
1965
a8c9486b 1966 /* already under rcu_read_lock() */
0c12295a 1967 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
51456b29 1968 if (!cache) {
660b26dc
ND
1969 int vif = ipmr_find_vif(mrt, skb->dev);
1970
1971 if (vif >= 0)
1972 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1973 vif);
1974 }
1da177e4 1975
7ef8f65d 1976 /* No usable cache entry */
51456b29 1977 if (!cache) {
1da177e4
LT
1978 int vif;
1979
1980 if (local) {
1981 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1982 ip_local_deliver(skb);
51456b29 1983 if (!skb2)
1da177e4 1984 return -ENOBUFS;
1da177e4
LT
1985 skb = skb2;
1986 }
1987
a8c9486b 1988 read_lock(&mrt_lock);
0c12295a 1989 vif = ipmr_find_vif(mrt, skb->dev);
1da177e4 1990 if (vif >= 0) {
0eae88f3 1991 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1da177e4
LT
1992 read_unlock(&mrt_lock);
1993
0eae88f3 1994 return err2;
1da177e4
LT
1995 }
1996 read_unlock(&mrt_lock);
1997 kfree_skb(skb);
1998 return -ENODEV;
1999 }
2000
a8c9486b 2001 read_lock(&mrt_lock);
0c12295a 2002 ip_mr_forward(net, mrt, skb, cache, local);
1da177e4
LT
2003 read_unlock(&mrt_lock);
2004
2005 if (local)
2006 return ip_local_deliver(skb);
2007
2008 return 0;
2009
2010dont_forward:
2011 if (local)
2012 return ip_local_deliver(skb);
2013 kfree_skb(skb);
2014 return 0;
2015}
2016
b1879204 2017#ifdef CONFIG_IP_PIMSM_V1
7ef8f65d 2018/* Handle IGMP messages of PIMv1 */
a8cb16dd 2019int pim_rcv_v1(struct sk_buff *skb)
b1879204
IJ
2020{
2021 struct igmphdr *pim;
4feb88e5 2022 struct net *net = dev_net(skb->dev);
f0ad0860 2023 struct mr_table *mrt;
b1879204
IJ
2024
2025 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2026 goto drop;
2027
2028 pim = igmp_hdr(skb);
2029
417da66f 2030 mrt = ipmr_rt_fib_lookup(net, skb);
ee3f1aaf
DM
2031 if (IS_ERR(mrt))
2032 goto drop;
0c12295a 2033 if (!mrt->mroute_do_pim ||
b1879204
IJ
2034 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2035 goto drop;
2036
f0ad0860 2037 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
b1879204
IJ
2038drop:
2039 kfree_skb(skb);
2040 }
1da177e4
LT
2041 return 0;
2042}
2043#endif
2044
2045#ifdef CONFIG_IP_PIMSM_V2
a8cb16dd 2046static int pim_rcv(struct sk_buff *skb)
1da177e4
LT
2047{
2048 struct pimreghdr *pim;
f0ad0860
PM
2049 struct net *net = dev_net(skb->dev);
2050 struct mr_table *mrt;
1da177e4 2051
b1879204 2052 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1da177e4
LT
2053 goto drop;
2054
9c70220b 2055 pim = (struct pimreghdr *)skb_transport_header(skb);
a8cb16dd
ED
2056 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2057 (pim->flags & PIM_NULL_REGISTER) ||
e905a9ed 2058 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
d3bc23e7 2059 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
1da177e4
LT
2060 goto drop;
2061
417da66f 2062 mrt = ipmr_rt_fib_lookup(net, skb);
ee3f1aaf
DM
2063 if (IS_ERR(mrt))
2064 goto drop;
f0ad0860 2065 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
b1879204
IJ
2066drop:
2067 kfree_skb(skb);
2068 }
1da177e4
LT
2069 return 0;
2070}
2071#endif
2072
cb6a4e46
PM
2073static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2074 struct mfc_cache *c, struct rtmsg *rtm)
1da177e4 2075{
adfa85e4 2076 struct rta_mfc_stats mfcs;
43b9e127
NA
2077 struct nlattr *mp_attr;
2078 struct rtnexthop *nhp;
b5036cd4 2079 unsigned long lastuse;
43b9e127 2080 int ct;
1da177e4 2081
7438189b 2082 /* If cache is unresolved, don't try to parse IIF and OIF */
ed0f160a 2083 if (c->mfc_parent >= MAXVIFS)
7438189b
ND
2084 return -ENOENT;
2085
92a395e5
TG
2086 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2087 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2088 return -EMSGSIZE;
1da177e4 2089
92a395e5
TG
2090 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2091 return -EMSGSIZE;
1da177e4
LT
2092
2093 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
0c12295a 2094 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
92a395e5
TG
2095 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2096 nla_nest_cancel(skb, mp_attr);
2097 return -EMSGSIZE;
2098 }
2099
1da177e4
LT
2100 nhp->rtnh_flags = 0;
2101 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
0c12295a 2102 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
1da177e4
LT
2103 nhp->rtnh_len = sizeof(*nhp);
2104 }
2105 }
92a395e5
TG
2106
2107 nla_nest_end(skb, mp_attr);
2108
b5036cd4
NA
2109 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2110 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2111
adfa85e4
ND
2112 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2113 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2114 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
43b9e127 2115 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
b5036cd4 2116 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
43b9e127 2117 RTA_PAD))
adfa85e4
ND
2118 return -EMSGSIZE;
2119
1da177e4
LT
2120 rtm->rtm_type = RTN_MULTICAST;
2121 return 1;
1da177e4
LT
2122}
2123
9a1b9496
DM
2124int ipmr_get_route(struct net *net, struct sk_buff *skb,
2125 __be32 saddr, __be32 daddr,
2cf75070 2126 struct rtmsg *rtm, int nowait, u32 portid)
1da177e4 2127{
1da177e4 2128 struct mfc_cache *cache;
9a1b9496
DM
2129 struct mr_table *mrt;
2130 int err;
1da177e4 2131
f0ad0860 2132 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
51456b29 2133 if (!mrt)
f0ad0860
PM
2134 return -ENOENT;
2135
a8c9486b 2136 rcu_read_lock();
9a1b9496 2137 cache = ipmr_cache_find(mrt, saddr, daddr);
51456b29 2138 if (!cache && skb->dev) {
660b26dc 2139 int vif = ipmr_find_vif(mrt, skb->dev);
1da177e4 2140
660b26dc
ND
2141 if (vif >= 0)
2142 cache = ipmr_cache_find_any(mrt, daddr, vif);
2143 }
51456b29 2144 if (!cache) {
72287490 2145 struct sk_buff *skb2;
eddc9ec5 2146 struct iphdr *iph;
1da177e4 2147 struct net_device *dev;
a8cb16dd 2148 int vif = -1;
1da177e4
LT
2149
2150 if (nowait) {
a8c9486b 2151 rcu_read_unlock();
1da177e4
LT
2152 return -EAGAIN;
2153 }
2154
2155 dev = skb->dev;
a8c9486b 2156 read_lock(&mrt_lock);
a8cb16dd
ED
2157 if (dev)
2158 vif = ipmr_find_vif(mrt, dev);
2159 if (vif < 0) {
1da177e4 2160 read_unlock(&mrt_lock);
a8c9486b 2161 rcu_read_unlock();
1da177e4
LT
2162 return -ENODEV;
2163 }
72287490
AK
2164 skb2 = skb_clone(skb, GFP_ATOMIC);
2165 if (!skb2) {
2166 read_unlock(&mrt_lock);
a8c9486b 2167 rcu_read_unlock();
72287490
AK
2168 return -ENOMEM;
2169 }
2170
2cf75070 2171 NETLINK_CB(skb2).portid = portid;
e2d1bca7
ACM
2172 skb_push(skb2, sizeof(struct iphdr));
2173 skb_reset_network_header(skb2);
eddc9ec5
ACM
2174 iph = ip_hdr(skb2);
2175 iph->ihl = sizeof(struct iphdr) >> 2;
9a1b9496
DM
2176 iph->saddr = saddr;
2177 iph->daddr = daddr;
eddc9ec5 2178 iph->version = 0;
0c12295a 2179 err = ipmr_cache_unresolved(mrt, vif, skb2);
1da177e4 2180 read_unlock(&mrt_lock);
a8c9486b 2181 rcu_read_unlock();
1da177e4
LT
2182 return err;
2183 }
2184
a8c9486b 2185 read_lock(&mrt_lock);
cb6a4e46 2186 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
1da177e4 2187 read_unlock(&mrt_lock);
a8c9486b 2188 rcu_read_unlock();
1da177e4
LT
2189 return err;
2190}
2191
cb6a4e46 2192static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
65886f43
ND
2193 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2194 int flags)
cb6a4e46
PM
2195{
2196 struct nlmsghdr *nlh;
2197 struct rtmsg *rtm;
1eb99af5 2198 int err;
cb6a4e46 2199
65886f43 2200 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
51456b29 2201 if (!nlh)
cb6a4e46
PM
2202 return -EMSGSIZE;
2203
2204 rtm = nlmsg_data(nlh);
2205 rtm->rtm_family = RTNL_FAMILY_IPMR;
2206 rtm->rtm_dst_len = 32;
2207 rtm->rtm_src_len = 32;
2208 rtm->rtm_tos = 0;
2209 rtm->rtm_table = mrt->id;
f3756b79
DM
2210 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2211 goto nla_put_failure;
cb6a4e46
PM
2212 rtm->rtm_type = RTN_MULTICAST;
2213 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
9a68ac72
ND
2214 if (c->mfc_flags & MFC_STATIC)
2215 rtm->rtm_protocol = RTPROT_STATIC;
2216 else
2217 rtm->rtm_protocol = RTPROT_MROUTED;
cb6a4e46
PM
2218 rtm->rtm_flags = 0;
2219
930345ea
JB
2220 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2221 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
f3756b79 2222 goto nla_put_failure;
1eb99af5
ND
2223 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2224 /* do not break the dump if cache is unresolved */
2225 if (err < 0 && err != -ENOENT)
cb6a4e46
PM
2226 goto nla_put_failure;
2227
053c095a
JB
2228 nlmsg_end(skb, nlh);
2229 return 0;
cb6a4e46
PM
2230
2231nla_put_failure:
2232 nlmsg_cancel(skb, nlh);
2233 return -EMSGSIZE;
2234}
2235
8cd3ac9f
ND
2236static size_t mroute_msgsize(bool unresolved, int maxvif)
2237{
2238 size_t len =
2239 NLMSG_ALIGN(sizeof(struct rtmsg))
2240 + nla_total_size(4) /* RTA_TABLE */
2241 + nla_total_size(4) /* RTA_SRC */
2242 + nla_total_size(4) /* RTA_DST */
2243 ;
2244
2245 if (!unresolved)
2246 len = len
2247 + nla_total_size(4) /* RTA_IIF */
2248 + nla_total_size(0) /* RTA_MULTIPATH */
2249 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2250 /* RTA_MFC_STATS */
a9a08042 2251 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
8cd3ac9f
ND
2252 ;
2253
2254 return len;
2255}
2256
2257static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2258 int cmd)
2259{
2260 struct net *net = read_pnet(&mrt->net);
2261 struct sk_buff *skb;
2262 int err = -ENOBUFS;
2263
2264 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2265 GFP_ATOMIC);
51456b29 2266 if (!skb)
8cd3ac9f
ND
2267 goto errout;
2268
65886f43 2269 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
8cd3ac9f
ND
2270 if (err < 0)
2271 goto errout;
2272
2273 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2274 return;
2275
2276errout:
2277 kfree_skb(skb);
2278 if (err < 0)
2279 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2280}
2281
cb6a4e46
PM
2282static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2283{
2284 struct net *net = sock_net(skb->sk);
2285 struct mr_table *mrt;
2286 struct mfc_cache *mfc;
2287 unsigned int t = 0, s_t;
2288 unsigned int h = 0, s_h;
2289 unsigned int e = 0, s_e;
2290
2291 s_t = cb->args[0];
2292 s_h = cb->args[1];
2293 s_e = cb->args[2];
2294
a8c9486b 2295 rcu_read_lock();
cb6a4e46
PM
2296 ipmr_for_each_table(mrt, net) {
2297 if (t < s_t)
2298 goto next_table;
2299 if (t > s_t)
2300 s_h = 0;
2301 for (h = s_h; h < MFC_LINES; h++) {
a8c9486b 2302 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
cb6a4e46
PM
2303 if (e < s_e)
2304 goto next_entry;
2305 if (ipmr_fill_mroute(mrt, skb,
15e47304 2306 NETLINK_CB(cb->skb).portid,
cb6a4e46 2307 cb->nlh->nlmsg_seq,
65886f43
ND
2308 mfc, RTM_NEWROUTE,
2309 NLM_F_MULTI) < 0)
cb6a4e46
PM
2310 goto done;
2311next_entry:
2312 e++;
2313 }
2314 e = s_e = 0;
2315 }
1eb99af5
ND
2316 spin_lock_bh(&mfc_unres_lock);
2317 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2318 if (e < s_e)
2319 goto next_entry2;
2320 if (ipmr_fill_mroute(mrt, skb,
2321 NETLINK_CB(cb->skb).portid,
2322 cb->nlh->nlmsg_seq,
65886f43
ND
2323 mfc, RTM_NEWROUTE,
2324 NLM_F_MULTI) < 0) {
1eb99af5
ND
2325 spin_unlock_bh(&mfc_unres_lock);
2326 goto done;
2327 }
2328next_entry2:
2329 e++;
2330 }
2331 spin_unlock_bh(&mfc_unres_lock);
2332 e = s_e = 0;
cb6a4e46
PM
2333 s_h = 0;
2334next_table:
2335 t++;
2336 }
2337done:
a8c9486b 2338 rcu_read_unlock();
cb6a4e46
PM
2339
2340 cb->args[2] = e;
2341 cb->args[1] = h;
2342 cb->args[0] = t;
2343
2344 return skb->len;
2345}
2346
ccbb0aa6
NA
2347static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2348 [RTA_SRC] = { .type = NLA_U32 },
2349 [RTA_DST] = { .type = NLA_U32 },
2350 [RTA_IIF] = { .type = NLA_U32 },
2351 [RTA_TABLE] = { .type = NLA_U32 },
2352 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2353};
2354
2355static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2356{
2357 switch (rtm_protocol) {
2358 case RTPROT_STATIC:
2359 case RTPROT_MROUTED:
2360 return true;
2361 }
2362 return false;
2363}
2364
2365static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2366{
2367 struct rtnexthop *rtnh = nla_data(nla);
2368 int remaining = nla_len(nla), vifi = 0;
2369
2370 while (rtnh_ok(rtnh, remaining)) {
2371 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2372 if (++vifi == MAXVIFS)
2373 break;
2374 rtnh = rtnh_next(rtnh, &remaining);
2375 }
2376
2377 return remaining > 0 ? -EINVAL : vifi;
2378}
2379
2380/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2381static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2382 struct mfcctl *mfcc, int *mrtsock,
2383 struct mr_table **mrtret)
2384{
2385 struct net_device *dev = NULL;
2386 u32 tblid = RT_TABLE_DEFAULT;
2387 struct mr_table *mrt;
2388 struct nlattr *attr;
2389 struct rtmsg *rtm;
2390 int ret, rem;
2391
2392 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy);
2393 if (ret < 0)
2394 goto out;
2395 rtm = nlmsg_data(nlh);
2396
2397 ret = -EINVAL;
2398 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2399 rtm->rtm_type != RTN_MULTICAST ||
2400 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2401 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2402 goto out;
2403
2404 memset(mfcc, 0, sizeof(*mfcc));
2405 mfcc->mfcc_parent = -1;
2406 ret = 0;
2407 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2408 switch (nla_type(attr)) {
2409 case RTA_SRC:
2410 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2411 break;
2412 case RTA_DST:
2413 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2414 break;
2415 case RTA_IIF:
2416 dev = __dev_get_by_index(net, nla_get_u32(attr));
2417 if (!dev) {
2418 ret = -ENODEV;
2419 goto out;
2420 }
2421 break;
2422 case RTA_MULTIPATH:
2423 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2424 ret = -EINVAL;
2425 goto out;
2426 }
2427 break;
2428 case RTA_PREFSRC:
2429 ret = 1;
2430 break;
2431 case RTA_TABLE:
2432 tblid = nla_get_u32(attr);
2433 break;
2434 }
2435 }
2436 mrt = ipmr_get_table(net, tblid);
2437 if (!mrt) {
2438 ret = -ENOENT;
2439 goto out;
2440 }
2441 *mrtret = mrt;
2442 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2443 if (dev)
2444 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2445
2446out:
2447 return ret;
2448}
2449
2450/* takes care of both newroute and delroute */
2451static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh)
2452{
2453 struct net *net = sock_net(skb->sk);
2454 int ret, mrtsock, parent;
2455 struct mr_table *tbl;
2456 struct mfcctl mfcc;
2457
2458 mrtsock = 0;
2459 tbl = NULL;
2460 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl);
2461 if (ret < 0)
2462 return ret;
2463
2464 parent = ret ? mfcc.mfcc_parent : -1;
2465 if (nlh->nlmsg_type == RTM_NEWROUTE)
2466 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2467 else
2468 return ipmr_mfc_delete(tbl, &mfcc, parent);
2469}
2470
e905a9ed 2471#ifdef CONFIG_PROC_FS
7ef8f65d
NA
2472/* The /proc interfaces to multicast routing :
2473 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
1da177e4
LT
2474 */
2475struct ipmr_vif_iter {
f6bb4514 2476 struct seq_net_private p;
f0ad0860 2477 struct mr_table *mrt;
1da177e4
LT
2478 int ct;
2479};
2480
f6bb4514
BT
2481static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2482 struct ipmr_vif_iter *iter,
1da177e4
LT
2483 loff_t pos)
2484{
f0ad0860 2485 struct mr_table *mrt = iter->mrt;
0c12295a
PM
2486
2487 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2488 if (!VIF_EXISTS(mrt, iter->ct))
1da177e4 2489 continue;
e905a9ed 2490 if (pos-- == 0)
0c12295a 2491 return &mrt->vif_table[iter->ct];
1da177e4
LT
2492 }
2493 return NULL;
2494}
2495
2496static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
ba93ef74 2497 __acquires(mrt_lock)
1da177e4 2498{
f0ad0860 2499 struct ipmr_vif_iter *iter = seq->private;
f6bb4514 2500 struct net *net = seq_file_net(seq);
f0ad0860
PM
2501 struct mr_table *mrt;
2502
2503 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
51456b29 2504 if (!mrt)
f0ad0860
PM
2505 return ERR_PTR(-ENOENT);
2506
2507 iter->mrt = mrt;
f6bb4514 2508
1da177e4 2509 read_lock(&mrt_lock);
f6bb4514 2510 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
1da177e4
LT
2511 : SEQ_START_TOKEN;
2512}
2513
2514static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2515{
2516 struct ipmr_vif_iter *iter = seq->private;
f6bb4514 2517 struct net *net = seq_file_net(seq);
f0ad0860 2518 struct mr_table *mrt = iter->mrt;
1da177e4
LT
2519
2520 ++*pos;
2521 if (v == SEQ_START_TOKEN)
f6bb4514 2522 return ipmr_vif_seq_idx(net, iter, 0);
e905a9ed 2523
0c12295a
PM
2524 while (++iter->ct < mrt->maxvif) {
2525 if (!VIF_EXISTS(mrt, iter->ct))
1da177e4 2526 continue;
0c12295a 2527 return &mrt->vif_table[iter->ct];
1da177e4
LT
2528 }
2529 return NULL;
2530}
2531
2532static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
ba93ef74 2533 __releases(mrt_lock)
1da177e4
LT
2534{
2535 read_unlock(&mrt_lock);
2536}
2537
2538static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2539{
f0ad0860
PM
2540 struct ipmr_vif_iter *iter = seq->private;
2541 struct mr_table *mrt = iter->mrt;
f6bb4514 2542
1da177e4 2543 if (v == SEQ_START_TOKEN) {
e905a9ed 2544 seq_puts(seq,
1da177e4
LT
2545 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2546 } else {
2547 const struct vif_device *vif = v;
2548 const char *name = vif->dev ? vif->dev->name : "none";
2549
2550 seq_printf(seq,
2551 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
0c12295a 2552 vif - mrt->vif_table,
e905a9ed 2553 name, vif->bytes_in, vif->pkt_in,
1da177e4
LT
2554 vif->bytes_out, vif->pkt_out,
2555 vif->flags, vif->local, vif->remote);
2556 }
2557 return 0;
2558}
2559
f690808e 2560static const struct seq_operations ipmr_vif_seq_ops = {
1da177e4
LT
2561 .start = ipmr_vif_seq_start,
2562 .next = ipmr_vif_seq_next,
2563 .stop = ipmr_vif_seq_stop,
2564 .show = ipmr_vif_seq_show,
2565};
2566
2567static int ipmr_vif_open(struct inode *inode, struct file *file)
2568{
f6bb4514
BT
2569 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2570 sizeof(struct ipmr_vif_iter));
1da177e4
LT
2571}
2572
9a32144e 2573static const struct file_operations ipmr_vif_fops = {
1da177e4
LT
2574 .owner = THIS_MODULE,
2575 .open = ipmr_vif_open,
2576 .read = seq_read,
2577 .llseek = seq_lseek,
f6bb4514 2578 .release = seq_release_net,
1da177e4
LT
2579};
2580
2581struct ipmr_mfc_iter {
f6bb4514 2582 struct seq_net_private p;
f0ad0860 2583 struct mr_table *mrt;
862465f2 2584 struct list_head *cache;
1da177e4
LT
2585 int ct;
2586};
2587
2588
f6bb4514
BT
2589static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2590 struct ipmr_mfc_iter *it, loff_t pos)
1da177e4 2591{
f0ad0860 2592 struct mr_table *mrt = it->mrt;
1da177e4
LT
2593 struct mfc_cache *mfc;
2594
a8c9486b 2595 rcu_read_lock();
862465f2 2596 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
0c12295a 2597 it->cache = &mrt->mfc_cache_array[it->ct];
a8c9486b 2598 list_for_each_entry_rcu(mfc, it->cache, list)
e905a9ed 2599 if (pos-- == 0)
1da177e4 2600 return mfc;
862465f2 2601 }
a8c9486b 2602 rcu_read_unlock();
1da177e4 2603
1da177e4 2604 spin_lock_bh(&mfc_unres_lock);
0c12295a 2605 it->cache = &mrt->mfc_unres_queue;
862465f2 2606 list_for_each_entry(mfc, it->cache, list)
e258beb2 2607 if (pos-- == 0)
1da177e4
LT
2608 return mfc;
2609 spin_unlock_bh(&mfc_unres_lock);
2610
2611 it->cache = NULL;
2612 return NULL;
2613}
2614
2615
2616static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2617{
2618 struct ipmr_mfc_iter *it = seq->private;
f6bb4514 2619 struct net *net = seq_file_net(seq);
f0ad0860 2620 struct mr_table *mrt;
f6bb4514 2621
f0ad0860 2622 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
51456b29 2623 if (!mrt)
f0ad0860 2624 return ERR_PTR(-ENOENT);
f6bb4514 2625
f0ad0860 2626 it->mrt = mrt;
1da177e4
LT
2627 it->cache = NULL;
2628 it->ct = 0;
f6bb4514 2629 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
1da177e4
LT
2630 : SEQ_START_TOKEN;
2631}
2632
2633static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2634{
2635 struct mfc_cache *mfc = v;
2636 struct ipmr_mfc_iter *it = seq->private;
f6bb4514 2637 struct net *net = seq_file_net(seq);
f0ad0860 2638 struct mr_table *mrt = it->mrt;
1da177e4
LT
2639
2640 ++*pos;
2641
2642 if (v == SEQ_START_TOKEN)
f6bb4514 2643 return ipmr_mfc_seq_idx(net, seq->private, 0);
1da177e4 2644
862465f2
PM
2645 if (mfc->list.next != it->cache)
2646 return list_entry(mfc->list.next, struct mfc_cache, list);
e905a9ed 2647
0c12295a 2648 if (it->cache == &mrt->mfc_unres_queue)
1da177e4
LT
2649 goto end_of_list;
2650
0c12295a 2651 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
1da177e4
LT
2652
2653 while (++it->ct < MFC_LINES) {
0c12295a 2654 it->cache = &mrt->mfc_cache_array[it->ct];
862465f2
PM
2655 if (list_empty(it->cache))
2656 continue;
2657 return list_first_entry(it->cache, struct mfc_cache, list);
1da177e4
LT
2658 }
2659
2660 /* exhausted cache_array, show unresolved */
a8c9486b 2661 rcu_read_unlock();
0c12295a 2662 it->cache = &mrt->mfc_unres_queue;
1da177e4 2663 it->ct = 0;
e905a9ed 2664
1da177e4 2665 spin_lock_bh(&mfc_unres_lock);
862465f2
PM
2666 if (!list_empty(it->cache))
2667 return list_first_entry(it->cache, struct mfc_cache, list);
1da177e4 2668
a8cb16dd 2669end_of_list:
1da177e4
LT
2670 spin_unlock_bh(&mfc_unres_lock);
2671 it->cache = NULL;
2672
2673 return NULL;
2674}
2675
2676static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2677{
2678 struct ipmr_mfc_iter *it = seq->private;
f0ad0860 2679 struct mr_table *mrt = it->mrt;
1da177e4 2680
0c12295a 2681 if (it->cache == &mrt->mfc_unres_queue)
1da177e4 2682 spin_unlock_bh(&mfc_unres_lock);
0c12295a 2683 else if (it->cache == &mrt->mfc_cache_array[it->ct])
a8c9486b 2684 rcu_read_unlock();
1da177e4
LT
2685}
2686
2687static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2688{
2689 int n;
2690
2691 if (v == SEQ_START_TOKEN) {
e905a9ed 2692 seq_puts(seq,
1da177e4
LT
2693 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2694 } else {
2695 const struct mfc_cache *mfc = v;
2696 const struct ipmr_mfc_iter *it = seq->private;
f0ad0860 2697 const struct mr_table *mrt = it->mrt;
e905a9ed 2698
0eae88f3
ED
2699 seq_printf(seq, "%08X %08X %-3hd",
2700 (__force u32) mfc->mfc_mcastgrp,
2701 (__force u32) mfc->mfc_origin,
1ea472e2 2702 mfc->mfc_parent);
1da177e4 2703
0c12295a 2704 if (it->cache != &mrt->mfc_unres_queue) {
1ea472e2
BT
2705 seq_printf(seq, " %8lu %8lu %8lu",
2706 mfc->mfc_un.res.pkt,
2707 mfc->mfc_un.res.bytes,
2708 mfc->mfc_un.res.wrong_if);
132adf54 2709 for (n = mfc->mfc_un.res.minvif;
a8cb16dd 2710 n < mfc->mfc_un.res.maxvif; n++) {
0c12295a 2711 if (VIF_EXISTS(mrt, n) &&
cf958ae3
BT
2712 mfc->mfc_un.res.ttls[n] < 255)
2713 seq_printf(seq,
e905a9ed 2714 " %2d:%-3d",
1da177e4
LT
2715 n, mfc->mfc_un.res.ttls[n]);
2716 }
1ea472e2
BT
2717 } else {
2718 /* unresolved mfc_caches don't contain
2719 * pkt, bytes and wrong_if values
2720 */
2721 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
1da177e4
LT
2722 }
2723 seq_putc(seq, '\n');
2724 }
2725 return 0;
2726}
2727
f690808e 2728static const struct seq_operations ipmr_mfc_seq_ops = {
1da177e4
LT
2729 .start = ipmr_mfc_seq_start,
2730 .next = ipmr_mfc_seq_next,
2731 .stop = ipmr_mfc_seq_stop,
2732 .show = ipmr_mfc_seq_show,
2733};
2734
2735static int ipmr_mfc_open(struct inode *inode, struct file *file)
2736{
f6bb4514
BT
2737 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2738 sizeof(struct ipmr_mfc_iter));
1da177e4
LT
2739}
2740
9a32144e 2741static const struct file_operations ipmr_mfc_fops = {
1da177e4
LT
2742 .owner = THIS_MODULE,
2743 .open = ipmr_mfc_open,
2744 .read = seq_read,
2745 .llseek = seq_lseek,
f6bb4514 2746 .release = seq_release_net,
1da177e4 2747};
e905a9ed 2748#endif
1da177e4
LT
2749
2750#ifdef CONFIG_IP_PIMSM_V2
32613090 2751static const struct net_protocol pim_protocol = {
1da177e4 2752 .handler = pim_rcv,
403dbb97 2753 .netns_ok = 1,
1da177e4
LT
2754};
2755#endif
2756
7ef8f65d 2757/* Setup for IP multicast routing */
cf958ae3
BT
2758static int __net_init ipmr_net_init(struct net *net)
2759{
f0ad0860 2760 int err;
cf958ae3 2761
f0ad0860
PM
2762 err = ipmr_rules_init(net);
2763 if (err < 0)
cf958ae3 2764 goto fail;
f6bb4514
BT
2765
2766#ifdef CONFIG_PROC_FS
2767 err = -ENOMEM;
d4beaa66 2768 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
f6bb4514 2769 goto proc_vif_fail;
d4beaa66 2770 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
f6bb4514
BT
2771 goto proc_cache_fail;
2772#endif
2bb8b26c
BT
2773 return 0;
2774
f6bb4514
BT
2775#ifdef CONFIG_PROC_FS
2776proc_cache_fail:
ece31ffd 2777 remove_proc_entry("ip_mr_vif", net->proc_net);
f6bb4514 2778proc_vif_fail:
f0ad0860 2779 ipmr_rules_exit(net);
f6bb4514 2780#endif
cf958ae3
BT
2781fail:
2782 return err;
2783}
2784
2785static void __net_exit ipmr_net_exit(struct net *net)
2786{
f6bb4514 2787#ifdef CONFIG_PROC_FS
ece31ffd
G
2788 remove_proc_entry("ip_mr_cache", net->proc_net);
2789 remove_proc_entry("ip_mr_vif", net->proc_net);
f6bb4514 2790#endif
f0ad0860 2791 ipmr_rules_exit(net);
cf958ae3
BT
2792}
2793
2794static struct pernet_operations ipmr_net_ops = {
2795 .init = ipmr_net_init,
2796 .exit = ipmr_net_exit,
2797};
e905a9ed 2798
03d2f897 2799int __init ip_mr_init(void)
1da177e4 2800{
03d2f897
WC
2801 int err;
2802
1da177e4
LT
2803 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2804 sizeof(struct mfc_cache),
a8c9486b 2805 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
20c2df83 2806 NULL);
03d2f897 2807
cf958ae3
BT
2808 err = register_pernet_subsys(&ipmr_net_ops);
2809 if (err)
2810 goto reg_pernet_fail;
2811
03d2f897
WC
2812 err = register_netdevice_notifier(&ip_mr_notifier);
2813 if (err)
2814 goto reg_notif_fail;
403dbb97
TG
2815#ifdef CONFIG_IP_PIMSM_V2
2816 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
058bd4d2 2817 pr_err("%s: can't add PIM protocol\n", __func__);
403dbb97
TG
2818 err = -EAGAIN;
2819 goto add_proto_fail;
2820 }
2821#endif
c7ac8679
GR
2822 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2823 NULL, ipmr_rtm_dumproute, NULL);
ccbb0aa6
NA
2824 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
2825 ipmr_rtm_route, NULL, NULL);
2826 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
2827 ipmr_rtm_route, NULL, NULL);
03d2f897 2828 return 0;
f6bb4514 2829
403dbb97
TG
2830#ifdef CONFIG_IP_PIMSM_V2
2831add_proto_fail:
2832 unregister_netdevice_notifier(&ip_mr_notifier);
2833#endif
c3e38896 2834reg_notif_fail:
cf958ae3
BT
2835 unregister_pernet_subsys(&ipmr_net_ops);
2836reg_pernet_fail:
c3e38896 2837 kmem_cache_destroy(mrt_cachep);
03d2f897 2838 return err;
1da177e4 2839}