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