ipmr/ip6mr: report origin of mfc entry into rtnl msg
[linux-2.6.git] / net / ipv4 / ipmr.c
blob91782a7634c2e95edd7eae496e188b3c8cf233a9
1 /*
2 * IP multicast routing support for mrouted 3.6/3.8
4 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
5 * Linux Consultancy and Custom Driver Development
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.
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.
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/ipip.h>
65 #include <net/checksum.h>
66 #include <net/netlink.h>
67 #include <net/fib_rules.h>
68 #include <linux/netconf.h>
70 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71 #define CONFIG_IP_PIMSM 1
72 #endif
74 struct mr_table {
75 struct list_head list;
76 #ifdef CONFIG_NET_NS
77 struct net *net;
78 #endif
79 u32 id;
80 struct sock __rcu *mroute_sk;
81 struct timer_list ipmr_expire_timer;
82 struct list_head mfc_unres_queue;
83 struct list_head mfc_cache_array[MFC_LINES];
84 struct vif_device vif_table[MAXVIFS];
85 int maxvif;
86 atomic_t cache_resolve_queue_len;
87 bool mroute_do_assert;
88 bool mroute_do_pim;
89 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num;
91 #endif
94 struct ipmr_rule {
95 struct fib_rule common;
98 struct ipmr_result {
99 struct mr_table *mrt;
102 /* Big lock, protecting vif table, mrt cache and mroute socket state.
103 * Note that the changes are semaphored via rtnl_lock.
106 static DEFINE_RWLOCK(mrt_lock);
109 * Multicast router control variables
112 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
114 /* Special spinlock for queue of unresolved entries */
115 static DEFINE_SPINLOCK(mfc_unres_lock);
117 /* We return to original Alan's scheme. Hash table of resolved
118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
122 * In this case data path is free of exclusive locks at all.
125 static struct kmem_cache *mrt_cachep __read_mostly;
127 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
128 static void ipmr_free_table(struct mr_table *mrt);
130 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
131 struct sk_buff *skb, struct mfc_cache *cache,
132 int local);
133 static int ipmr_cache_report(struct mr_table *mrt,
134 struct sk_buff *pkt, vifi_t vifi, int assert);
135 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136 struct mfc_cache *c, struct rtmsg *rtm);
137 static void mroute_clean_tables(struct mr_table *mrt);
138 static void ipmr_expire_process(unsigned long arg);
140 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
141 #define ipmr_for_each_table(mrt, net) \
142 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
144 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
146 struct mr_table *mrt;
148 ipmr_for_each_table(mrt, net) {
149 if (mrt->id == id)
150 return mrt;
152 return NULL;
155 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
156 struct mr_table **mrt)
158 struct ipmr_result res;
159 struct fib_lookup_arg arg = { .result = &res, };
160 int err;
162 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
163 flowi4_to_flowi(flp4), 0, &arg);
164 if (err < 0)
165 return err;
166 *mrt = res.mrt;
167 return 0;
170 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
171 int flags, struct fib_lookup_arg *arg)
173 struct ipmr_result *res = arg->result;
174 struct mr_table *mrt;
176 switch (rule->action) {
177 case FR_ACT_TO_TBL:
178 break;
179 case FR_ACT_UNREACHABLE:
180 return -ENETUNREACH;
181 case FR_ACT_PROHIBIT:
182 return -EACCES;
183 case FR_ACT_BLACKHOLE:
184 default:
185 return -EINVAL;
188 mrt = ipmr_get_table(rule->fr_net, rule->table);
189 if (mrt == NULL)
190 return -EAGAIN;
191 res->mrt = mrt;
192 return 0;
195 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
197 return 1;
200 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
201 FRA_GENERIC_POLICY,
204 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
205 struct fib_rule_hdr *frh, struct nlattr **tb)
207 return 0;
210 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
211 struct nlattr **tb)
213 return 1;
216 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
217 struct fib_rule_hdr *frh)
219 frh->dst_len = 0;
220 frh->src_len = 0;
221 frh->tos = 0;
222 return 0;
225 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
226 .family = RTNL_FAMILY_IPMR,
227 .rule_size = sizeof(struct ipmr_rule),
228 .addr_size = sizeof(u32),
229 .action = ipmr_rule_action,
230 .match = ipmr_rule_match,
231 .configure = ipmr_rule_configure,
232 .compare = ipmr_rule_compare,
233 .default_pref = fib_default_rule_pref,
234 .fill = ipmr_rule_fill,
235 .nlgroup = RTNLGRP_IPV4_RULE,
236 .policy = ipmr_rule_policy,
237 .owner = THIS_MODULE,
240 static int __net_init ipmr_rules_init(struct net *net)
242 struct fib_rules_ops *ops;
243 struct mr_table *mrt;
244 int err;
246 ops = fib_rules_register(&ipmr_rules_ops_template, net);
247 if (IS_ERR(ops))
248 return PTR_ERR(ops);
250 INIT_LIST_HEAD(&net->ipv4.mr_tables);
252 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
253 if (mrt == NULL) {
254 err = -ENOMEM;
255 goto err1;
258 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
259 if (err < 0)
260 goto err2;
262 net->ipv4.mr_rules_ops = ops;
263 return 0;
265 err2:
266 kfree(mrt);
267 err1:
268 fib_rules_unregister(ops);
269 return err;
272 static void __net_exit ipmr_rules_exit(struct net *net)
274 struct mr_table *mrt, *next;
276 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
277 list_del(&mrt->list);
278 ipmr_free_table(mrt);
280 fib_rules_unregister(net->ipv4.mr_rules_ops);
282 #else
283 #define ipmr_for_each_table(mrt, net) \
284 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
286 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
288 return net->ipv4.mrt;
291 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
292 struct mr_table **mrt)
294 *mrt = net->ipv4.mrt;
295 return 0;
298 static int __net_init ipmr_rules_init(struct net *net)
300 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
301 return net->ipv4.mrt ? 0 : -ENOMEM;
304 static void __net_exit ipmr_rules_exit(struct net *net)
306 ipmr_free_table(net->ipv4.mrt);
308 #endif
310 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
312 struct mr_table *mrt;
313 unsigned int i;
315 mrt = ipmr_get_table(net, id);
316 if (mrt != NULL)
317 return mrt;
319 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
320 if (mrt == NULL)
321 return NULL;
322 write_pnet(&mrt->net, net);
323 mrt->id = id;
325 /* Forwarding cache */
326 for (i = 0; i < MFC_LINES; i++)
327 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
329 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
331 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
332 (unsigned long)mrt);
334 #ifdef CONFIG_IP_PIMSM
335 mrt->mroute_reg_vif_num = -1;
336 #endif
337 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
338 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
339 #endif
340 return mrt;
343 static void ipmr_free_table(struct mr_table *mrt)
345 del_timer_sync(&mrt->ipmr_expire_timer);
346 mroute_clean_tables(mrt);
347 kfree(mrt);
350 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
352 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
354 struct net *net = dev_net(dev);
356 dev_close(dev);
358 dev = __dev_get_by_name(net, "tunl0");
359 if (dev) {
360 const struct net_device_ops *ops = dev->netdev_ops;
361 struct ifreq ifr;
362 struct ip_tunnel_parm p;
364 memset(&p, 0, sizeof(p));
365 p.iph.daddr = v->vifc_rmt_addr.s_addr;
366 p.iph.saddr = v->vifc_lcl_addr.s_addr;
367 p.iph.version = 4;
368 p.iph.ihl = 5;
369 p.iph.protocol = IPPROTO_IPIP;
370 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
371 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
373 if (ops->ndo_do_ioctl) {
374 mm_segment_t oldfs = get_fs();
376 set_fs(KERNEL_DS);
377 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
378 set_fs(oldfs);
383 static
384 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
386 struct net_device *dev;
388 dev = __dev_get_by_name(net, "tunl0");
390 if (dev) {
391 const struct net_device_ops *ops = dev->netdev_ops;
392 int err;
393 struct ifreq ifr;
394 struct ip_tunnel_parm p;
395 struct in_device *in_dev;
397 memset(&p, 0, sizeof(p));
398 p.iph.daddr = v->vifc_rmt_addr.s_addr;
399 p.iph.saddr = v->vifc_lcl_addr.s_addr;
400 p.iph.version = 4;
401 p.iph.ihl = 5;
402 p.iph.protocol = IPPROTO_IPIP;
403 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
404 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
406 if (ops->ndo_do_ioctl) {
407 mm_segment_t oldfs = get_fs();
409 set_fs(KERNEL_DS);
410 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
411 set_fs(oldfs);
412 } else {
413 err = -EOPNOTSUPP;
415 dev = NULL;
417 if (err == 0 &&
418 (dev = __dev_get_by_name(net, p.name)) != NULL) {
419 dev->flags |= IFF_MULTICAST;
421 in_dev = __in_dev_get_rtnl(dev);
422 if (in_dev == NULL)
423 goto failure;
425 ipv4_devconf_setall(in_dev);
426 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
428 if (dev_open(dev))
429 goto failure;
430 dev_hold(dev);
433 return dev;
435 failure:
436 /* allow the register to be completed before unregistering. */
437 rtnl_unlock();
438 rtnl_lock();
440 unregister_netdevice(dev);
441 return NULL;
444 #ifdef CONFIG_IP_PIMSM
446 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
448 struct net *net = dev_net(dev);
449 struct mr_table *mrt;
450 struct flowi4 fl4 = {
451 .flowi4_oif = dev->ifindex,
452 .flowi4_iif = skb->skb_iif,
453 .flowi4_mark = skb->mark,
455 int err;
457 err = ipmr_fib_lookup(net, &fl4, &mrt);
458 if (err < 0) {
459 kfree_skb(skb);
460 return err;
463 read_lock(&mrt_lock);
464 dev->stats.tx_bytes += skb->len;
465 dev->stats.tx_packets++;
466 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
467 read_unlock(&mrt_lock);
468 kfree_skb(skb);
469 return NETDEV_TX_OK;
472 static const struct net_device_ops reg_vif_netdev_ops = {
473 .ndo_start_xmit = reg_vif_xmit,
476 static void reg_vif_setup(struct net_device *dev)
478 dev->type = ARPHRD_PIMREG;
479 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
480 dev->flags = IFF_NOARP;
481 dev->netdev_ops = &reg_vif_netdev_ops,
482 dev->destructor = free_netdev;
483 dev->features |= NETIF_F_NETNS_LOCAL;
486 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
488 struct net_device *dev;
489 struct in_device *in_dev;
490 char name[IFNAMSIZ];
492 if (mrt->id == RT_TABLE_DEFAULT)
493 sprintf(name, "pimreg");
494 else
495 sprintf(name, "pimreg%u", mrt->id);
497 dev = alloc_netdev(0, name, reg_vif_setup);
499 if (dev == NULL)
500 return NULL;
502 dev_net_set(dev, net);
504 if (register_netdevice(dev)) {
505 free_netdev(dev);
506 return NULL;
508 dev->iflink = 0;
510 rcu_read_lock();
511 in_dev = __in_dev_get_rcu(dev);
512 if (!in_dev) {
513 rcu_read_unlock();
514 goto failure;
517 ipv4_devconf_setall(in_dev);
518 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
519 rcu_read_unlock();
521 if (dev_open(dev))
522 goto failure;
524 dev_hold(dev);
526 return dev;
528 failure:
529 /* allow the register to be completed before unregistering. */
530 rtnl_unlock();
531 rtnl_lock();
533 unregister_netdevice(dev);
534 return NULL;
536 #endif
539 * vif_delete - Delete a VIF entry
540 * @notify: Set to 1, if the caller is a notifier_call
543 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
544 struct list_head *head)
546 struct vif_device *v;
547 struct net_device *dev;
548 struct in_device *in_dev;
550 if (vifi < 0 || vifi >= mrt->maxvif)
551 return -EADDRNOTAVAIL;
553 v = &mrt->vif_table[vifi];
555 write_lock_bh(&mrt_lock);
556 dev = v->dev;
557 v->dev = NULL;
559 if (!dev) {
560 write_unlock_bh(&mrt_lock);
561 return -EADDRNOTAVAIL;
564 #ifdef CONFIG_IP_PIMSM
565 if (vifi == mrt->mroute_reg_vif_num)
566 mrt->mroute_reg_vif_num = -1;
567 #endif
569 if (vifi + 1 == mrt->maxvif) {
570 int tmp;
572 for (tmp = vifi - 1; tmp >= 0; tmp--) {
573 if (VIF_EXISTS(mrt, tmp))
574 break;
576 mrt->maxvif = tmp+1;
579 write_unlock_bh(&mrt_lock);
581 dev_set_allmulti(dev, -1);
583 in_dev = __in_dev_get_rtnl(dev);
584 if (in_dev) {
585 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
586 inet_netconf_notify_devconf(dev_net(dev),
587 NETCONFA_MC_FORWARDING,
588 dev->ifindex, &in_dev->cnf);
589 ip_rt_multicast_event(in_dev);
592 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
593 unregister_netdevice_queue(dev, head);
595 dev_put(dev);
596 return 0;
599 static void ipmr_cache_free_rcu(struct rcu_head *head)
601 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
603 kmem_cache_free(mrt_cachep, c);
606 static inline void ipmr_cache_free(struct mfc_cache *c)
608 call_rcu(&c->rcu, ipmr_cache_free_rcu);
611 /* Destroy an unresolved cache entry, killing queued skbs
612 * and reporting error to netlink readers.
615 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
617 struct net *net = read_pnet(&mrt->net);
618 struct sk_buff *skb;
619 struct nlmsgerr *e;
621 atomic_dec(&mrt->cache_resolve_queue_len);
623 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
624 if (ip_hdr(skb)->version == 0) {
625 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
626 nlh->nlmsg_type = NLMSG_ERROR;
627 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
628 skb_trim(skb, nlh->nlmsg_len);
629 e = NLMSG_DATA(nlh);
630 e->error = -ETIMEDOUT;
631 memset(&e->msg, 0, sizeof(e->msg));
633 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
634 } else {
635 kfree_skb(skb);
639 ipmr_cache_free(c);
643 /* Timer process for the unresolved queue. */
645 static void ipmr_expire_process(unsigned long arg)
647 struct mr_table *mrt = (struct mr_table *)arg;
648 unsigned long now;
649 unsigned long expires;
650 struct mfc_cache *c, *next;
652 if (!spin_trylock(&mfc_unres_lock)) {
653 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
654 return;
657 if (list_empty(&mrt->mfc_unres_queue))
658 goto out;
660 now = jiffies;
661 expires = 10*HZ;
663 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
664 if (time_after(c->mfc_un.unres.expires, now)) {
665 unsigned long interval = c->mfc_un.unres.expires - now;
666 if (interval < expires)
667 expires = interval;
668 continue;
671 list_del(&c->list);
672 ipmr_destroy_unres(mrt, c);
675 if (!list_empty(&mrt->mfc_unres_queue))
676 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
678 out:
679 spin_unlock(&mfc_unres_lock);
682 /* Fill oifs list. It is called under write locked mrt_lock. */
684 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
685 unsigned char *ttls)
687 int vifi;
689 cache->mfc_un.res.minvif = MAXVIFS;
690 cache->mfc_un.res.maxvif = 0;
691 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
693 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
694 if (VIF_EXISTS(mrt, vifi) &&
695 ttls[vifi] && ttls[vifi] < 255) {
696 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
697 if (cache->mfc_un.res.minvif > vifi)
698 cache->mfc_un.res.minvif = vifi;
699 if (cache->mfc_un.res.maxvif <= vifi)
700 cache->mfc_un.res.maxvif = vifi + 1;
705 static int vif_add(struct net *net, struct mr_table *mrt,
706 struct vifctl *vifc, int mrtsock)
708 int vifi = vifc->vifc_vifi;
709 struct vif_device *v = &mrt->vif_table[vifi];
710 struct net_device *dev;
711 struct in_device *in_dev;
712 int err;
714 /* Is vif busy ? */
715 if (VIF_EXISTS(mrt, vifi))
716 return -EADDRINUSE;
718 switch (vifc->vifc_flags) {
719 #ifdef CONFIG_IP_PIMSM
720 case VIFF_REGISTER:
722 * Special Purpose VIF in PIM
723 * All the packets will be sent to the daemon
725 if (mrt->mroute_reg_vif_num >= 0)
726 return -EADDRINUSE;
727 dev = ipmr_reg_vif(net, mrt);
728 if (!dev)
729 return -ENOBUFS;
730 err = dev_set_allmulti(dev, 1);
731 if (err) {
732 unregister_netdevice(dev);
733 dev_put(dev);
734 return err;
736 break;
737 #endif
738 case VIFF_TUNNEL:
739 dev = ipmr_new_tunnel(net, vifc);
740 if (!dev)
741 return -ENOBUFS;
742 err = dev_set_allmulti(dev, 1);
743 if (err) {
744 ipmr_del_tunnel(dev, vifc);
745 dev_put(dev);
746 return err;
748 break;
750 case VIFF_USE_IFINDEX:
751 case 0:
752 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
753 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
754 if (dev && __in_dev_get_rtnl(dev) == NULL) {
755 dev_put(dev);
756 return -EADDRNOTAVAIL;
758 } else {
759 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
761 if (!dev)
762 return -EADDRNOTAVAIL;
763 err = dev_set_allmulti(dev, 1);
764 if (err) {
765 dev_put(dev);
766 return err;
768 break;
769 default:
770 return -EINVAL;
773 in_dev = __in_dev_get_rtnl(dev);
774 if (!in_dev) {
775 dev_put(dev);
776 return -EADDRNOTAVAIL;
778 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
779 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
780 &in_dev->cnf);
781 ip_rt_multicast_event(in_dev);
783 /* Fill in the VIF structures */
785 v->rate_limit = vifc->vifc_rate_limit;
786 v->local = vifc->vifc_lcl_addr.s_addr;
787 v->remote = vifc->vifc_rmt_addr.s_addr;
788 v->flags = vifc->vifc_flags;
789 if (!mrtsock)
790 v->flags |= VIFF_STATIC;
791 v->threshold = vifc->vifc_threshold;
792 v->bytes_in = 0;
793 v->bytes_out = 0;
794 v->pkt_in = 0;
795 v->pkt_out = 0;
796 v->link = dev->ifindex;
797 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
798 v->link = dev->iflink;
800 /* And finish update writing critical data */
801 write_lock_bh(&mrt_lock);
802 v->dev = dev;
803 #ifdef CONFIG_IP_PIMSM
804 if (v->flags & VIFF_REGISTER)
805 mrt->mroute_reg_vif_num = vifi;
806 #endif
807 if (vifi+1 > mrt->maxvif)
808 mrt->maxvif = vifi+1;
809 write_unlock_bh(&mrt_lock);
810 return 0;
813 /* called with rcu_read_lock() */
814 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
815 __be32 origin,
816 __be32 mcastgrp)
818 int line = MFC_HASH(mcastgrp, origin);
819 struct mfc_cache *c;
821 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
822 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
823 return c;
825 return NULL;
829 * Allocate a multicast cache entry
831 static struct mfc_cache *ipmr_cache_alloc(void)
833 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
835 if (c)
836 c->mfc_un.res.minvif = MAXVIFS;
837 return c;
840 static struct mfc_cache *ipmr_cache_alloc_unres(void)
842 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
844 if (c) {
845 skb_queue_head_init(&c->mfc_un.unres.unresolved);
846 c->mfc_un.unres.expires = jiffies + 10*HZ;
848 return c;
852 * A cache entry has gone into a resolved state from queued
855 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
856 struct mfc_cache *uc, struct mfc_cache *c)
858 struct sk_buff *skb;
859 struct nlmsgerr *e;
861 /* Play the pending entries through our router */
863 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
864 if (ip_hdr(skb)->version == 0) {
865 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
867 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
868 nlh->nlmsg_len = skb_tail_pointer(skb) -
869 (u8 *)nlh;
870 } else {
871 nlh->nlmsg_type = NLMSG_ERROR;
872 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
873 skb_trim(skb, nlh->nlmsg_len);
874 e = NLMSG_DATA(nlh);
875 e->error = -EMSGSIZE;
876 memset(&e->msg, 0, sizeof(e->msg));
879 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
880 } else {
881 ip_mr_forward(net, mrt, skb, c, 0);
887 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
888 * expects the following bizarre scheme.
890 * Called under mrt_lock.
893 static int ipmr_cache_report(struct mr_table *mrt,
894 struct sk_buff *pkt, vifi_t vifi, int assert)
896 struct sk_buff *skb;
897 const int ihl = ip_hdrlen(pkt);
898 struct igmphdr *igmp;
899 struct igmpmsg *msg;
900 struct sock *mroute_sk;
901 int ret;
903 #ifdef CONFIG_IP_PIMSM
904 if (assert == IGMPMSG_WHOLEPKT)
905 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
906 else
907 #endif
908 skb = alloc_skb(128, GFP_ATOMIC);
910 if (!skb)
911 return -ENOBUFS;
913 #ifdef CONFIG_IP_PIMSM
914 if (assert == IGMPMSG_WHOLEPKT) {
915 /* Ugly, but we have no choice with this interface.
916 * Duplicate old header, fix ihl, length etc.
917 * And all this only to mangle msg->im_msgtype and
918 * to set msg->im_mbz to "mbz" :-)
920 skb_push(skb, sizeof(struct iphdr));
921 skb_reset_network_header(skb);
922 skb_reset_transport_header(skb);
923 msg = (struct igmpmsg *)skb_network_header(skb);
924 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
925 msg->im_msgtype = IGMPMSG_WHOLEPKT;
926 msg->im_mbz = 0;
927 msg->im_vif = mrt->mroute_reg_vif_num;
928 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
929 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
930 sizeof(struct iphdr));
931 } else
932 #endif
935 /* Copy the IP header */
937 skb->network_header = skb->tail;
938 skb_put(skb, ihl);
939 skb_copy_to_linear_data(skb, pkt->data, ihl);
940 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
941 msg = (struct igmpmsg *)skb_network_header(skb);
942 msg->im_vif = vifi;
943 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
945 /* Add our header */
947 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
948 igmp->type =
949 msg->im_msgtype = assert;
950 igmp->code = 0;
951 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
952 skb->transport_header = skb->network_header;
955 rcu_read_lock();
956 mroute_sk = rcu_dereference(mrt->mroute_sk);
957 if (mroute_sk == NULL) {
958 rcu_read_unlock();
959 kfree_skb(skb);
960 return -EINVAL;
963 /* Deliver to mrouted */
965 ret = sock_queue_rcv_skb(mroute_sk, skb);
966 rcu_read_unlock();
967 if (ret < 0) {
968 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
969 kfree_skb(skb);
972 return ret;
976 * Queue a packet for resolution. It gets locked cache entry!
979 static int
980 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
982 bool found = false;
983 int err;
984 struct mfc_cache *c;
985 const struct iphdr *iph = ip_hdr(skb);
987 spin_lock_bh(&mfc_unres_lock);
988 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
989 if (c->mfc_mcastgrp == iph->daddr &&
990 c->mfc_origin == iph->saddr) {
991 found = true;
992 break;
996 if (!found) {
997 /* Create a new entry if allowable */
999 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1000 (c = ipmr_cache_alloc_unres()) == NULL) {
1001 spin_unlock_bh(&mfc_unres_lock);
1003 kfree_skb(skb);
1004 return -ENOBUFS;
1007 /* Fill in the new cache entry */
1009 c->mfc_parent = -1;
1010 c->mfc_origin = iph->saddr;
1011 c->mfc_mcastgrp = iph->daddr;
1013 /* Reflect first query at mrouted. */
1015 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1016 if (err < 0) {
1017 /* If the report failed throw the cache entry
1018 out - Brad Parker
1020 spin_unlock_bh(&mfc_unres_lock);
1022 ipmr_cache_free(c);
1023 kfree_skb(skb);
1024 return err;
1027 atomic_inc(&mrt->cache_resolve_queue_len);
1028 list_add(&c->list, &mrt->mfc_unres_queue);
1030 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1031 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1034 /* See if we can append the packet */
1036 if (c->mfc_un.unres.unresolved.qlen > 3) {
1037 kfree_skb(skb);
1038 err = -ENOBUFS;
1039 } else {
1040 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1041 err = 0;
1044 spin_unlock_bh(&mfc_unres_lock);
1045 return err;
1049 * MFC cache manipulation by user space mroute daemon
1052 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1054 int line;
1055 struct mfc_cache *c, *next;
1057 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1059 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1060 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1061 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1062 list_del_rcu(&c->list);
1064 ipmr_cache_free(c);
1065 return 0;
1068 return -ENOENT;
1071 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1072 struct mfcctl *mfc, int mrtsock)
1074 bool found = false;
1075 int line;
1076 struct mfc_cache *uc, *c;
1078 if (mfc->mfcc_parent >= MAXVIFS)
1079 return -ENFILE;
1081 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1083 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1084 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1085 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1086 found = true;
1087 break;
1091 if (found) {
1092 write_lock_bh(&mrt_lock);
1093 c->mfc_parent = mfc->mfcc_parent;
1094 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1095 if (!mrtsock)
1096 c->mfc_flags |= MFC_STATIC;
1097 write_unlock_bh(&mrt_lock);
1098 return 0;
1101 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1102 return -EINVAL;
1104 c = ipmr_cache_alloc();
1105 if (c == NULL)
1106 return -ENOMEM;
1108 c->mfc_origin = mfc->mfcc_origin.s_addr;
1109 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1110 c->mfc_parent = mfc->mfcc_parent;
1111 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1112 if (!mrtsock)
1113 c->mfc_flags |= MFC_STATIC;
1115 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1118 * Check to see if we resolved a queued list. If so we
1119 * need to send on the frames and tidy up.
1121 found = false;
1122 spin_lock_bh(&mfc_unres_lock);
1123 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1124 if (uc->mfc_origin == c->mfc_origin &&
1125 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1126 list_del(&uc->list);
1127 atomic_dec(&mrt->cache_resolve_queue_len);
1128 found = true;
1129 break;
1132 if (list_empty(&mrt->mfc_unres_queue))
1133 del_timer(&mrt->ipmr_expire_timer);
1134 spin_unlock_bh(&mfc_unres_lock);
1136 if (found) {
1137 ipmr_cache_resolve(net, mrt, uc, c);
1138 ipmr_cache_free(uc);
1140 return 0;
1144 * Close the multicast socket, and clear the vif tables etc
1147 static void mroute_clean_tables(struct mr_table *mrt)
1149 int i;
1150 LIST_HEAD(list);
1151 struct mfc_cache *c, *next;
1153 /* Shut down all active vif entries */
1155 for (i = 0; i < mrt->maxvif; i++) {
1156 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1157 vif_delete(mrt, i, 0, &list);
1159 unregister_netdevice_many(&list);
1161 /* Wipe the cache */
1163 for (i = 0; i < MFC_LINES; i++) {
1164 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1165 if (c->mfc_flags & MFC_STATIC)
1166 continue;
1167 list_del_rcu(&c->list);
1168 ipmr_cache_free(c);
1172 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1173 spin_lock_bh(&mfc_unres_lock);
1174 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1175 list_del(&c->list);
1176 ipmr_destroy_unres(mrt, c);
1178 spin_unlock_bh(&mfc_unres_lock);
1182 /* called from ip_ra_control(), before an RCU grace period,
1183 * we dont need to call synchronize_rcu() here
1185 static void mrtsock_destruct(struct sock *sk)
1187 struct net *net = sock_net(sk);
1188 struct mr_table *mrt;
1190 rtnl_lock();
1191 ipmr_for_each_table(mrt, net) {
1192 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1193 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1194 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1195 NETCONFA_IFINDEX_ALL,
1196 net->ipv4.devconf_all);
1197 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1198 mroute_clean_tables(mrt);
1201 rtnl_unlock();
1205 * Socket options and virtual interface manipulation. The whole
1206 * virtual interface system is a complete heap, but unfortunately
1207 * that's how BSD mrouted happens to think. Maybe one day with a proper
1208 * MOSPF/PIM router set up we can clean this up.
1211 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1213 int ret;
1214 struct vifctl vif;
1215 struct mfcctl mfc;
1216 struct net *net = sock_net(sk);
1217 struct mr_table *mrt;
1219 if (sk->sk_type != SOCK_RAW ||
1220 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1221 return -EOPNOTSUPP;
1223 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1224 if (mrt == NULL)
1225 return -ENOENT;
1227 if (optname != MRT_INIT) {
1228 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1229 !ns_capable(net->user_ns, CAP_NET_ADMIN))
1230 return -EACCES;
1233 switch (optname) {
1234 case MRT_INIT:
1235 if (optlen != sizeof(int))
1236 return -EINVAL;
1238 rtnl_lock();
1239 if (rtnl_dereference(mrt->mroute_sk)) {
1240 rtnl_unlock();
1241 return -EADDRINUSE;
1244 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1245 if (ret == 0) {
1246 rcu_assign_pointer(mrt->mroute_sk, sk);
1247 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1248 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1249 NETCONFA_IFINDEX_ALL,
1250 net->ipv4.devconf_all);
1252 rtnl_unlock();
1253 return ret;
1254 case MRT_DONE:
1255 if (sk != rcu_access_pointer(mrt->mroute_sk))
1256 return -EACCES;
1257 return ip_ra_control(sk, 0, NULL);
1258 case MRT_ADD_VIF:
1259 case MRT_DEL_VIF:
1260 if (optlen != sizeof(vif))
1261 return -EINVAL;
1262 if (copy_from_user(&vif, optval, sizeof(vif)))
1263 return -EFAULT;
1264 if (vif.vifc_vifi >= MAXVIFS)
1265 return -ENFILE;
1266 rtnl_lock();
1267 if (optname == MRT_ADD_VIF) {
1268 ret = vif_add(net, mrt, &vif,
1269 sk == rtnl_dereference(mrt->mroute_sk));
1270 } else {
1271 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1273 rtnl_unlock();
1274 return ret;
1277 * Manipulate the forwarding caches. These live
1278 * in a sort of kernel/user symbiosis.
1280 case MRT_ADD_MFC:
1281 case MRT_DEL_MFC:
1282 if (optlen != sizeof(mfc))
1283 return -EINVAL;
1284 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1285 return -EFAULT;
1286 rtnl_lock();
1287 if (optname == MRT_DEL_MFC)
1288 ret = ipmr_mfc_delete(mrt, &mfc);
1289 else
1290 ret = ipmr_mfc_add(net, mrt, &mfc,
1291 sk == rtnl_dereference(mrt->mroute_sk));
1292 rtnl_unlock();
1293 return ret;
1295 * Control PIM assert.
1297 case MRT_ASSERT:
1299 int v;
1300 if (optlen != sizeof(v))
1301 return -EINVAL;
1302 if (get_user(v, (int __user *)optval))
1303 return -EFAULT;
1304 mrt->mroute_do_assert = v;
1305 return 0;
1307 #ifdef CONFIG_IP_PIMSM
1308 case MRT_PIM:
1310 int v;
1312 if (optlen != sizeof(v))
1313 return -EINVAL;
1314 if (get_user(v, (int __user *)optval))
1315 return -EFAULT;
1316 v = !!v;
1318 rtnl_lock();
1319 ret = 0;
1320 if (v != mrt->mroute_do_pim) {
1321 mrt->mroute_do_pim = v;
1322 mrt->mroute_do_assert = v;
1324 rtnl_unlock();
1325 return ret;
1327 #endif
1328 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1329 case MRT_TABLE:
1331 u32 v;
1333 if (optlen != sizeof(u32))
1334 return -EINVAL;
1335 if (get_user(v, (u32 __user *)optval))
1336 return -EFAULT;
1338 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1339 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1340 return -EINVAL;
1342 rtnl_lock();
1343 ret = 0;
1344 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1345 ret = -EBUSY;
1346 } else {
1347 if (!ipmr_new_table(net, v))
1348 ret = -ENOMEM;
1349 else
1350 raw_sk(sk)->ipmr_table = v;
1352 rtnl_unlock();
1353 return ret;
1355 #endif
1357 * Spurious command, or MRT_VERSION which you cannot
1358 * set.
1360 default:
1361 return -ENOPROTOOPT;
1366 * Getsock opt support for the multicast routing system.
1369 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1371 int olr;
1372 int val;
1373 struct net *net = sock_net(sk);
1374 struct mr_table *mrt;
1376 if (sk->sk_type != SOCK_RAW ||
1377 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1378 return -EOPNOTSUPP;
1380 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1381 if (mrt == NULL)
1382 return -ENOENT;
1384 if (optname != MRT_VERSION &&
1385 #ifdef CONFIG_IP_PIMSM
1386 optname != MRT_PIM &&
1387 #endif
1388 optname != MRT_ASSERT)
1389 return -ENOPROTOOPT;
1391 if (get_user(olr, optlen))
1392 return -EFAULT;
1394 olr = min_t(unsigned int, olr, sizeof(int));
1395 if (olr < 0)
1396 return -EINVAL;
1398 if (put_user(olr, optlen))
1399 return -EFAULT;
1400 if (optname == MRT_VERSION)
1401 val = 0x0305;
1402 #ifdef CONFIG_IP_PIMSM
1403 else if (optname == MRT_PIM)
1404 val = mrt->mroute_do_pim;
1405 #endif
1406 else
1407 val = mrt->mroute_do_assert;
1408 if (copy_to_user(optval, &val, olr))
1409 return -EFAULT;
1410 return 0;
1414 * The IP multicast ioctl support routines.
1417 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1419 struct sioc_sg_req sr;
1420 struct sioc_vif_req vr;
1421 struct vif_device *vif;
1422 struct mfc_cache *c;
1423 struct net *net = sock_net(sk);
1424 struct mr_table *mrt;
1426 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1427 if (mrt == NULL)
1428 return -ENOENT;
1430 switch (cmd) {
1431 case SIOCGETVIFCNT:
1432 if (copy_from_user(&vr, arg, sizeof(vr)))
1433 return -EFAULT;
1434 if (vr.vifi >= mrt->maxvif)
1435 return -EINVAL;
1436 read_lock(&mrt_lock);
1437 vif = &mrt->vif_table[vr.vifi];
1438 if (VIF_EXISTS(mrt, vr.vifi)) {
1439 vr.icount = vif->pkt_in;
1440 vr.ocount = vif->pkt_out;
1441 vr.ibytes = vif->bytes_in;
1442 vr.obytes = vif->bytes_out;
1443 read_unlock(&mrt_lock);
1445 if (copy_to_user(arg, &vr, sizeof(vr)))
1446 return -EFAULT;
1447 return 0;
1449 read_unlock(&mrt_lock);
1450 return -EADDRNOTAVAIL;
1451 case SIOCGETSGCNT:
1452 if (copy_from_user(&sr, arg, sizeof(sr)))
1453 return -EFAULT;
1455 rcu_read_lock();
1456 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1457 if (c) {
1458 sr.pktcnt = c->mfc_un.res.pkt;
1459 sr.bytecnt = c->mfc_un.res.bytes;
1460 sr.wrong_if = c->mfc_un.res.wrong_if;
1461 rcu_read_unlock();
1463 if (copy_to_user(arg, &sr, sizeof(sr)))
1464 return -EFAULT;
1465 return 0;
1467 rcu_read_unlock();
1468 return -EADDRNOTAVAIL;
1469 default:
1470 return -ENOIOCTLCMD;
1474 #ifdef CONFIG_COMPAT
1475 struct compat_sioc_sg_req {
1476 struct in_addr src;
1477 struct in_addr grp;
1478 compat_ulong_t pktcnt;
1479 compat_ulong_t bytecnt;
1480 compat_ulong_t wrong_if;
1483 struct compat_sioc_vif_req {
1484 vifi_t vifi; /* Which iface */
1485 compat_ulong_t icount;
1486 compat_ulong_t ocount;
1487 compat_ulong_t ibytes;
1488 compat_ulong_t obytes;
1491 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1493 struct compat_sioc_sg_req sr;
1494 struct compat_sioc_vif_req vr;
1495 struct vif_device *vif;
1496 struct mfc_cache *c;
1497 struct net *net = sock_net(sk);
1498 struct mr_table *mrt;
1500 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1501 if (mrt == NULL)
1502 return -ENOENT;
1504 switch (cmd) {
1505 case SIOCGETVIFCNT:
1506 if (copy_from_user(&vr, arg, sizeof(vr)))
1507 return -EFAULT;
1508 if (vr.vifi >= mrt->maxvif)
1509 return -EINVAL;
1510 read_lock(&mrt_lock);
1511 vif = &mrt->vif_table[vr.vifi];
1512 if (VIF_EXISTS(mrt, vr.vifi)) {
1513 vr.icount = vif->pkt_in;
1514 vr.ocount = vif->pkt_out;
1515 vr.ibytes = vif->bytes_in;
1516 vr.obytes = vif->bytes_out;
1517 read_unlock(&mrt_lock);
1519 if (copy_to_user(arg, &vr, sizeof(vr)))
1520 return -EFAULT;
1521 return 0;
1523 read_unlock(&mrt_lock);
1524 return -EADDRNOTAVAIL;
1525 case SIOCGETSGCNT:
1526 if (copy_from_user(&sr, arg, sizeof(sr)))
1527 return -EFAULT;
1529 rcu_read_lock();
1530 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1531 if (c) {
1532 sr.pktcnt = c->mfc_un.res.pkt;
1533 sr.bytecnt = c->mfc_un.res.bytes;
1534 sr.wrong_if = c->mfc_un.res.wrong_if;
1535 rcu_read_unlock();
1537 if (copy_to_user(arg, &sr, sizeof(sr)))
1538 return -EFAULT;
1539 return 0;
1541 rcu_read_unlock();
1542 return -EADDRNOTAVAIL;
1543 default:
1544 return -ENOIOCTLCMD;
1547 #endif
1550 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1552 struct net_device *dev = ptr;
1553 struct net *net = dev_net(dev);
1554 struct mr_table *mrt;
1555 struct vif_device *v;
1556 int ct;
1558 if (event != NETDEV_UNREGISTER)
1559 return NOTIFY_DONE;
1561 ipmr_for_each_table(mrt, net) {
1562 v = &mrt->vif_table[0];
1563 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1564 if (v->dev == dev)
1565 vif_delete(mrt, ct, 1, NULL);
1568 return NOTIFY_DONE;
1572 static struct notifier_block ip_mr_notifier = {
1573 .notifier_call = ipmr_device_event,
1577 * Encapsulate a packet by attaching a valid IPIP header to it.
1578 * This avoids tunnel drivers and other mess and gives us the speed so
1579 * important for multicast video.
1582 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1584 struct iphdr *iph;
1585 const struct iphdr *old_iph = ip_hdr(skb);
1587 skb_push(skb, sizeof(struct iphdr));
1588 skb->transport_header = skb->network_header;
1589 skb_reset_network_header(skb);
1590 iph = ip_hdr(skb);
1592 iph->version = 4;
1593 iph->tos = old_iph->tos;
1594 iph->ttl = old_iph->ttl;
1595 iph->frag_off = 0;
1596 iph->daddr = daddr;
1597 iph->saddr = saddr;
1598 iph->protocol = IPPROTO_IPIP;
1599 iph->ihl = 5;
1600 iph->tot_len = htons(skb->len);
1601 ip_select_ident(iph, skb_dst(skb), NULL);
1602 ip_send_check(iph);
1604 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1605 nf_reset(skb);
1608 static inline int ipmr_forward_finish(struct sk_buff *skb)
1610 struct ip_options *opt = &(IPCB(skb)->opt);
1612 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1613 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
1615 if (unlikely(opt->optlen))
1616 ip_forward_options(skb);
1618 return dst_output(skb);
1622 * Processing handlers for ipmr_forward
1625 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1626 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1628 const struct iphdr *iph = ip_hdr(skb);
1629 struct vif_device *vif = &mrt->vif_table[vifi];
1630 struct net_device *dev;
1631 struct rtable *rt;
1632 struct flowi4 fl4;
1633 int encap = 0;
1635 if (vif->dev == NULL)
1636 goto out_free;
1638 #ifdef CONFIG_IP_PIMSM
1639 if (vif->flags & VIFF_REGISTER) {
1640 vif->pkt_out++;
1641 vif->bytes_out += skb->len;
1642 vif->dev->stats.tx_bytes += skb->len;
1643 vif->dev->stats.tx_packets++;
1644 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1645 goto out_free;
1647 #endif
1649 if (vif->flags & VIFF_TUNNEL) {
1650 rt = ip_route_output_ports(net, &fl4, NULL,
1651 vif->remote, vif->local,
1652 0, 0,
1653 IPPROTO_IPIP,
1654 RT_TOS(iph->tos), vif->link);
1655 if (IS_ERR(rt))
1656 goto out_free;
1657 encap = sizeof(struct iphdr);
1658 } else {
1659 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1660 0, 0,
1661 IPPROTO_IPIP,
1662 RT_TOS(iph->tos), vif->link);
1663 if (IS_ERR(rt))
1664 goto out_free;
1667 dev = rt->dst.dev;
1669 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1670 /* Do not fragment multicasts. Alas, IPv4 does not
1671 * allow to send ICMP, so that packets will disappear
1672 * to blackhole.
1675 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1676 ip_rt_put(rt);
1677 goto out_free;
1680 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1682 if (skb_cow(skb, encap)) {
1683 ip_rt_put(rt);
1684 goto out_free;
1687 vif->pkt_out++;
1688 vif->bytes_out += skb->len;
1690 skb_dst_drop(skb);
1691 skb_dst_set(skb, &rt->dst);
1692 ip_decrease_ttl(ip_hdr(skb));
1694 /* FIXME: forward and output firewalls used to be called here.
1695 * What do we do with netfilter? -- RR
1697 if (vif->flags & VIFF_TUNNEL) {
1698 ip_encap(skb, vif->local, vif->remote);
1699 /* FIXME: extra output firewall step used to be here. --RR */
1700 vif->dev->stats.tx_packets++;
1701 vif->dev->stats.tx_bytes += skb->len;
1704 IPCB(skb)->flags |= IPSKB_FORWARDED;
1707 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1708 * not only before forwarding, but after forwarding on all output
1709 * interfaces. It is clear, if mrouter runs a multicasting
1710 * program, it should receive packets not depending to what interface
1711 * program is joined.
1712 * If we will not make it, the program will have to join on all
1713 * interfaces. On the other hand, multihoming host (or router, but
1714 * not mrouter) cannot join to more than one interface - it will
1715 * result in receiving multiple packets.
1717 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1718 ipmr_forward_finish);
1719 return;
1721 out_free:
1722 kfree_skb(skb);
1725 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1727 int ct;
1729 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1730 if (mrt->vif_table[ct].dev == dev)
1731 break;
1733 return ct;
1736 /* "local" means that we should preserve one skb (for local delivery) */
1738 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1739 struct sk_buff *skb, struct mfc_cache *cache,
1740 int local)
1742 int psend = -1;
1743 int vif, ct;
1745 vif = cache->mfc_parent;
1746 cache->mfc_un.res.pkt++;
1747 cache->mfc_un.res.bytes += skb->len;
1750 * Wrong interface: drop packet and (maybe) send PIM assert.
1752 if (mrt->vif_table[vif].dev != skb->dev) {
1753 int true_vifi;
1755 if (rt_is_output_route(skb_rtable(skb))) {
1756 /* It is our own packet, looped back.
1757 * Very complicated situation...
1759 * The best workaround until routing daemons will be
1760 * fixed is not to redistribute packet, if it was
1761 * send through wrong interface. It means, that
1762 * multicast applications WILL NOT work for
1763 * (S,G), which have default multicast route pointing
1764 * to wrong oif. In any case, it is not a good
1765 * idea to use multicasting applications on router.
1767 goto dont_forward;
1770 cache->mfc_un.res.wrong_if++;
1771 true_vifi = ipmr_find_vif(mrt, skb->dev);
1773 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1774 /* pimsm uses asserts, when switching from RPT to SPT,
1775 * so that we cannot check that packet arrived on an oif.
1776 * It is bad, but otherwise we would need to move pretty
1777 * large chunk of pimd to kernel. Ough... --ANK
1779 (mrt->mroute_do_pim ||
1780 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1781 time_after(jiffies,
1782 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1783 cache->mfc_un.res.last_assert = jiffies;
1784 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1786 goto dont_forward;
1789 mrt->vif_table[vif].pkt_in++;
1790 mrt->vif_table[vif].bytes_in += skb->len;
1793 * Forward the frame
1795 for (ct = cache->mfc_un.res.maxvif - 1;
1796 ct >= cache->mfc_un.res.minvif; ct--) {
1797 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1798 if (psend != -1) {
1799 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1801 if (skb2)
1802 ipmr_queue_xmit(net, mrt, skb2, cache,
1803 psend);
1805 psend = ct;
1808 if (psend != -1) {
1809 if (local) {
1810 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1812 if (skb2)
1813 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1814 } else {
1815 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1816 return 0;
1820 dont_forward:
1821 if (!local)
1822 kfree_skb(skb);
1823 return 0;
1826 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1828 struct rtable *rt = skb_rtable(skb);
1829 struct iphdr *iph = ip_hdr(skb);
1830 struct flowi4 fl4 = {
1831 .daddr = iph->daddr,
1832 .saddr = iph->saddr,
1833 .flowi4_tos = RT_TOS(iph->tos),
1834 .flowi4_oif = (rt_is_output_route(rt) ?
1835 skb->dev->ifindex : 0),
1836 .flowi4_iif = (rt_is_output_route(rt) ?
1837 LOOPBACK_IFINDEX :
1838 skb->dev->ifindex),
1839 .flowi4_mark = skb->mark,
1841 struct mr_table *mrt;
1842 int err;
1844 err = ipmr_fib_lookup(net, &fl4, &mrt);
1845 if (err)
1846 return ERR_PTR(err);
1847 return mrt;
1851 * Multicast packets for forwarding arrive here
1852 * Called with rcu_read_lock();
1855 int ip_mr_input(struct sk_buff *skb)
1857 struct mfc_cache *cache;
1858 struct net *net = dev_net(skb->dev);
1859 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1860 struct mr_table *mrt;
1862 /* Packet is looped back after forward, it should not be
1863 * forwarded second time, but still can be delivered locally.
1865 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1866 goto dont_forward;
1868 mrt = ipmr_rt_fib_lookup(net, skb);
1869 if (IS_ERR(mrt)) {
1870 kfree_skb(skb);
1871 return PTR_ERR(mrt);
1873 if (!local) {
1874 if (IPCB(skb)->opt.router_alert) {
1875 if (ip_call_ra_chain(skb))
1876 return 0;
1877 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1878 /* IGMPv1 (and broken IGMPv2 implementations sort of
1879 * Cisco IOS <= 11.2(8)) do not put router alert
1880 * option to IGMP packets destined to routable
1881 * groups. It is very bad, because it means
1882 * that we can forward NO IGMP messages.
1884 struct sock *mroute_sk;
1886 mroute_sk = rcu_dereference(mrt->mroute_sk);
1887 if (mroute_sk) {
1888 nf_reset(skb);
1889 raw_rcv(mroute_sk, skb);
1890 return 0;
1895 /* already under rcu_read_lock() */
1896 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1899 * No usable cache entry
1901 if (cache == NULL) {
1902 int vif;
1904 if (local) {
1905 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1906 ip_local_deliver(skb);
1907 if (skb2 == NULL)
1908 return -ENOBUFS;
1909 skb = skb2;
1912 read_lock(&mrt_lock);
1913 vif = ipmr_find_vif(mrt, skb->dev);
1914 if (vif >= 0) {
1915 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1916 read_unlock(&mrt_lock);
1918 return err2;
1920 read_unlock(&mrt_lock);
1921 kfree_skb(skb);
1922 return -ENODEV;
1925 read_lock(&mrt_lock);
1926 ip_mr_forward(net, mrt, skb, cache, local);
1927 read_unlock(&mrt_lock);
1929 if (local)
1930 return ip_local_deliver(skb);
1932 return 0;
1934 dont_forward:
1935 if (local)
1936 return ip_local_deliver(skb);
1937 kfree_skb(skb);
1938 return 0;
1941 #ifdef CONFIG_IP_PIMSM
1942 /* called with rcu_read_lock() */
1943 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1944 unsigned int pimlen)
1946 struct net_device *reg_dev = NULL;
1947 struct iphdr *encap;
1949 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1951 * Check that:
1952 * a. packet is really sent to a multicast group
1953 * b. packet is not a NULL-REGISTER
1954 * c. packet is not truncated
1956 if (!ipv4_is_multicast(encap->daddr) ||
1957 encap->tot_len == 0 ||
1958 ntohs(encap->tot_len) + pimlen > skb->len)
1959 return 1;
1961 read_lock(&mrt_lock);
1962 if (mrt->mroute_reg_vif_num >= 0)
1963 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1964 read_unlock(&mrt_lock);
1966 if (reg_dev == NULL)
1967 return 1;
1969 skb->mac_header = skb->network_header;
1970 skb_pull(skb, (u8 *)encap - skb->data);
1971 skb_reset_network_header(skb);
1972 skb->protocol = htons(ETH_P_IP);
1973 skb->ip_summed = CHECKSUM_NONE;
1974 skb->pkt_type = PACKET_HOST;
1976 skb_tunnel_rx(skb, reg_dev);
1978 netif_rx(skb);
1980 return NET_RX_SUCCESS;
1982 #endif
1984 #ifdef CONFIG_IP_PIMSM_V1
1986 * Handle IGMP messages of PIMv1
1989 int pim_rcv_v1(struct sk_buff *skb)
1991 struct igmphdr *pim;
1992 struct net *net = dev_net(skb->dev);
1993 struct mr_table *mrt;
1995 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1996 goto drop;
1998 pim = igmp_hdr(skb);
2000 mrt = ipmr_rt_fib_lookup(net, skb);
2001 if (IS_ERR(mrt))
2002 goto drop;
2003 if (!mrt->mroute_do_pim ||
2004 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2005 goto drop;
2007 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2008 drop:
2009 kfree_skb(skb);
2011 return 0;
2013 #endif
2015 #ifdef CONFIG_IP_PIMSM_V2
2016 static int pim_rcv(struct sk_buff *skb)
2018 struct pimreghdr *pim;
2019 struct net *net = dev_net(skb->dev);
2020 struct mr_table *mrt;
2022 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2023 goto drop;
2025 pim = (struct pimreghdr *)skb_transport_header(skb);
2026 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2027 (pim->flags & PIM_NULL_REGISTER) ||
2028 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2029 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2030 goto drop;
2032 mrt = ipmr_rt_fib_lookup(net, skb);
2033 if (IS_ERR(mrt))
2034 goto drop;
2035 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2036 drop:
2037 kfree_skb(skb);
2039 return 0;
2041 #endif
2043 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2044 struct mfc_cache *c, struct rtmsg *rtm)
2046 int ct;
2047 struct rtnexthop *nhp;
2048 struct nlattr *mp_attr;
2049 struct rta_mfc_stats mfcs;
2051 /* If cache is unresolved, don't try to parse IIF and OIF */
2052 if (c->mfc_parent >= MAXVIFS)
2053 return -ENOENT;
2055 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2056 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2057 return -EMSGSIZE;
2059 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2060 return -EMSGSIZE;
2062 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2063 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2064 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2065 nla_nest_cancel(skb, mp_attr);
2066 return -EMSGSIZE;
2069 nhp->rtnh_flags = 0;
2070 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2071 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2072 nhp->rtnh_len = sizeof(*nhp);
2076 nla_nest_end(skb, mp_attr);
2078 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2079 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2080 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2081 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2082 return -EMSGSIZE;
2084 rtm->rtm_type = RTN_MULTICAST;
2085 return 1;
2088 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2089 __be32 saddr, __be32 daddr,
2090 struct rtmsg *rtm, int nowait)
2092 struct mfc_cache *cache;
2093 struct mr_table *mrt;
2094 int err;
2096 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2097 if (mrt == NULL)
2098 return -ENOENT;
2100 rcu_read_lock();
2101 cache = ipmr_cache_find(mrt, saddr, daddr);
2103 if (cache == NULL) {
2104 struct sk_buff *skb2;
2105 struct iphdr *iph;
2106 struct net_device *dev;
2107 int vif = -1;
2109 if (nowait) {
2110 rcu_read_unlock();
2111 return -EAGAIN;
2114 dev = skb->dev;
2115 read_lock(&mrt_lock);
2116 if (dev)
2117 vif = ipmr_find_vif(mrt, dev);
2118 if (vif < 0) {
2119 read_unlock(&mrt_lock);
2120 rcu_read_unlock();
2121 return -ENODEV;
2123 skb2 = skb_clone(skb, GFP_ATOMIC);
2124 if (!skb2) {
2125 read_unlock(&mrt_lock);
2126 rcu_read_unlock();
2127 return -ENOMEM;
2130 skb_push(skb2, sizeof(struct iphdr));
2131 skb_reset_network_header(skb2);
2132 iph = ip_hdr(skb2);
2133 iph->ihl = sizeof(struct iphdr) >> 2;
2134 iph->saddr = saddr;
2135 iph->daddr = daddr;
2136 iph->version = 0;
2137 err = ipmr_cache_unresolved(mrt, vif, skb2);
2138 read_unlock(&mrt_lock);
2139 rcu_read_unlock();
2140 return err;
2143 read_lock(&mrt_lock);
2144 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2145 cache->mfc_flags |= MFC_NOTIFY;
2146 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2147 read_unlock(&mrt_lock);
2148 rcu_read_unlock();
2149 return err;
2152 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2153 u32 portid, u32 seq, struct mfc_cache *c)
2155 struct nlmsghdr *nlh;
2156 struct rtmsg *rtm;
2158 nlh = nlmsg_put(skb, portid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2159 if (nlh == NULL)
2160 return -EMSGSIZE;
2162 rtm = nlmsg_data(nlh);
2163 rtm->rtm_family = RTNL_FAMILY_IPMR;
2164 rtm->rtm_dst_len = 32;
2165 rtm->rtm_src_len = 32;
2166 rtm->rtm_tos = 0;
2167 rtm->rtm_table = mrt->id;
2168 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2169 goto nla_put_failure;
2170 rtm->rtm_type = RTN_MULTICAST;
2171 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2172 if (c->mfc_flags & MFC_STATIC)
2173 rtm->rtm_protocol = RTPROT_STATIC;
2174 else
2175 rtm->rtm_protocol = RTPROT_MROUTED;
2176 rtm->rtm_flags = 0;
2178 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2179 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2180 goto nla_put_failure;
2181 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2182 goto nla_put_failure;
2184 return nlmsg_end(skb, nlh);
2186 nla_put_failure:
2187 nlmsg_cancel(skb, nlh);
2188 return -EMSGSIZE;
2191 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2193 struct net *net = sock_net(skb->sk);
2194 struct mr_table *mrt;
2195 struct mfc_cache *mfc;
2196 unsigned int t = 0, s_t;
2197 unsigned int h = 0, s_h;
2198 unsigned int e = 0, s_e;
2200 s_t = cb->args[0];
2201 s_h = cb->args[1];
2202 s_e = cb->args[2];
2204 rcu_read_lock();
2205 ipmr_for_each_table(mrt, net) {
2206 if (t < s_t)
2207 goto next_table;
2208 if (t > s_t)
2209 s_h = 0;
2210 for (h = s_h; h < MFC_LINES; h++) {
2211 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2212 if (e < s_e)
2213 goto next_entry;
2214 if (ipmr_fill_mroute(mrt, skb,
2215 NETLINK_CB(cb->skb).portid,
2216 cb->nlh->nlmsg_seq,
2217 mfc) < 0)
2218 goto done;
2219 next_entry:
2220 e++;
2222 e = s_e = 0;
2224 s_h = 0;
2225 next_table:
2226 t++;
2228 done:
2229 rcu_read_unlock();
2231 cb->args[2] = e;
2232 cb->args[1] = h;
2233 cb->args[0] = t;
2235 return skb->len;
2238 #ifdef CONFIG_PROC_FS
2240 * The /proc interfaces to multicast routing :
2241 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2243 struct ipmr_vif_iter {
2244 struct seq_net_private p;
2245 struct mr_table *mrt;
2246 int ct;
2249 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2250 struct ipmr_vif_iter *iter,
2251 loff_t pos)
2253 struct mr_table *mrt = iter->mrt;
2255 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2256 if (!VIF_EXISTS(mrt, iter->ct))
2257 continue;
2258 if (pos-- == 0)
2259 return &mrt->vif_table[iter->ct];
2261 return NULL;
2264 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2265 __acquires(mrt_lock)
2267 struct ipmr_vif_iter *iter = seq->private;
2268 struct net *net = seq_file_net(seq);
2269 struct mr_table *mrt;
2271 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2272 if (mrt == NULL)
2273 return ERR_PTR(-ENOENT);
2275 iter->mrt = mrt;
2277 read_lock(&mrt_lock);
2278 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2279 : SEQ_START_TOKEN;
2282 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2284 struct ipmr_vif_iter *iter = seq->private;
2285 struct net *net = seq_file_net(seq);
2286 struct mr_table *mrt = iter->mrt;
2288 ++*pos;
2289 if (v == SEQ_START_TOKEN)
2290 return ipmr_vif_seq_idx(net, iter, 0);
2292 while (++iter->ct < mrt->maxvif) {
2293 if (!VIF_EXISTS(mrt, iter->ct))
2294 continue;
2295 return &mrt->vif_table[iter->ct];
2297 return NULL;
2300 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2301 __releases(mrt_lock)
2303 read_unlock(&mrt_lock);
2306 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2308 struct ipmr_vif_iter *iter = seq->private;
2309 struct mr_table *mrt = iter->mrt;
2311 if (v == SEQ_START_TOKEN) {
2312 seq_puts(seq,
2313 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2314 } else {
2315 const struct vif_device *vif = v;
2316 const char *name = vif->dev ? vif->dev->name : "none";
2318 seq_printf(seq,
2319 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2320 vif - mrt->vif_table,
2321 name, vif->bytes_in, vif->pkt_in,
2322 vif->bytes_out, vif->pkt_out,
2323 vif->flags, vif->local, vif->remote);
2325 return 0;
2328 static const struct seq_operations ipmr_vif_seq_ops = {
2329 .start = ipmr_vif_seq_start,
2330 .next = ipmr_vif_seq_next,
2331 .stop = ipmr_vif_seq_stop,
2332 .show = ipmr_vif_seq_show,
2335 static int ipmr_vif_open(struct inode *inode, struct file *file)
2337 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2338 sizeof(struct ipmr_vif_iter));
2341 static const struct file_operations ipmr_vif_fops = {
2342 .owner = THIS_MODULE,
2343 .open = ipmr_vif_open,
2344 .read = seq_read,
2345 .llseek = seq_lseek,
2346 .release = seq_release_net,
2349 struct ipmr_mfc_iter {
2350 struct seq_net_private p;
2351 struct mr_table *mrt;
2352 struct list_head *cache;
2353 int ct;
2357 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2358 struct ipmr_mfc_iter *it, loff_t pos)
2360 struct mr_table *mrt = it->mrt;
2361 struct mfc_cache *mfc;
2363 rcu_read_lock();
2364 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2365 it->cache = &mrt->mfc_cache_array[it->ct];
2366 list_for_each_entry_rcu(mfc, it->cache, list)
2367 if (pos-- == 0)
2368 return mfc;
2370 rcu_read_unlock();
2372 spin_lock_bh(&mfc_unres_lock);
2373 it->cache = &mrt->mfc_unres_queue;
2374 list_for_each_entry(mfc, it->cache, list)
2375 if (pos-- == 0)
2376 return mfc;
2377 spin_unlock_bh(&mfc_unres_lock);
2379 it->cache = NULL;
2380 return NULL;
2384 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2386 struct ipmr_mfc_iter *it = seq->private;
2387 struct net *net = seq_file_net(seq);
2388 struct mr_table *mrt;
2390 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2391 if (mrt == NULL)
2392 return ERR_PTR(-ENOENT);
2394 it->mrt = mrt;
2395 it->cache = NULL;
2396 it->ct = 0;
2397 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2398 : SEQ_START_TOKEN;
2401 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2403 struct mfc_cache *mfc = v;
2404 struct ipmr_mfc_iter *it = seq->private;
2405 struct net *net = seq_file_net(seq);
2406 struct mr_table *mrt = it->mrt;
2408 ++*pos;
2410 if (v == SEQ_START_TOKEN)
2411 return ipmr_mfc_seq_idx(net, seq->private, 0);
2413 if (mfc->list.next != it->cache)
2414 return list_entry(mfc->list.next, struct mfc_cache, list);
2416 if (it->cache == &mrt->mfc_unres_queue)
2417 goto end_of_list;
2419 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2421 while (++it->ct < MFC_LINES) {
2422 it->cache = &mrt->mfc_cache_array[it->ct];
2423 if (list_empty(it->cache))
2424 continue;
2425 return list_first_entry(it->cache, struct mfc_cache, list);
2428 /* exhausted cache_array, show unresolved */
2429 rcu_read_unlock();
2430 it->cache = &mrt->mfc_unres_queue;
2431 it->ct = 0;
2433 spin_lock_bh(&mfc_unres_lock);
2434 if (!list_empty(it->cache))
2435 return list_first_entry(it->cache, struct mfc_cache, list);
2437 end_of_list:
2438 spin_unlock_bh(&mfc_unres_lock);
2439 it->cache = NULL;
2441 return NULL;
2444 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2446 struct ipmr_mfc_iter *it = seq->private;
2447 struct mr_table *mrt = it->mrt;
2449 if (it->cache == &mrt->mfc_unres_queue)
2450 spin_unlock_bh(&mfc_unres_lock);
2451 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2452 rcu_read_unlock();
2455 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2457 int n;
2459 if (v == SEQ_START_TOKEN) {
2460 seq_puts(seq,
2461 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2462 } else {
2463 const struct mfc_cache *mfc = v;
2464 const struct ipmr_mfc_iter *it = seq->private;
2465 const struct mr_table *mrt = it->mrt;
2467 seq_printf(seq, "%08X %08X %-3hd",
2468 (__force u32) mfc->mfc_mcastgrp,
2469 (__force u32) mfc->mfc_origin,
2470 mfc->mfc_parent);
2472 if (it->cache != &mrt->mfc_unres_queue) {
2473 seq_printf(seq, " %8lu %8lu %8lu",
2474 mfc->mfc_un.res.pkt,
2475 mfc->mfc_un.res.bytes,
2476 mfc->mfc_un.res.wrong_if);
2477 for (n = mfc->mfc_un.res.minvif;
2478 n < mfc->mfc_un.res.maxvif; n++) {
2479 if (VIF_EXISTS(mrt, n) &&
2480 mfc->mfc_un.res.ttls[n] < 255)
2481 seq_printf(seq,
2482 " %2d:%-3d",
2483 n, mfc->mfc_un.res.ttls[n]);
2485 } else {
2486 /* unresolved mfc_caches don't contain
2487 * pkt, bytes and wrong_if values
2489 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2491 seq_putc(seq, '\n');
2493 return 0;
2496 static const struct seq_operations ipmr_mfc_seq_ops = {
2497 .start = ipmr_mfc_seq_start,
2498 .next = ipmr_mfc_seq_next,
2499 .stop = ipmr_mfc_seq_stop,
2500 .show = ipmr_mfc_seq_show,
2503 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2505 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2506 sizeof(struct ipmr_mfc_iter));
2509 static const struct file_operations ipmr_mfc_fops = {
2510 .owner = THIS_MODULE,
2511 .open = ipmr_mfc_open,
2512 .read = seq_read,
2513 .llseek = seq_lseek,
2514 .release = seq_release_net,
2516 #endif
2518 #ifdef CONFIG_IP_PIMSM_V2
2519 static const struct net_protocol pim_protocol = {
2520 .handler = pim_rcv,
2521 .netns_ok = 1,
2523 #endif
2527 * Setup for IP multicast routing
2529 static int __net_init ipmr_net_init(struct net *net)
2531 int err;
2533 err = ipmr_rules_init(net);
2534 if (err < 0)
2535 goto fail;
2537 #ifdef CONFIG_PROC_FS
2538 err = -ENOMEM;
2539 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2540 goto proc_vif_fail;
2541 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2542 goto proc_cache_fail;
2543 #endif
2544 return 0;
2546 #ifdef CONFIG_PROC_FS
2547 proc_cache_fail:
2548 proc_net_remove(net, "ip_mr_vif");
2549 proc_vif_fail:
2550 ipmr_rules_exit(net);
2551 #endif
2552 fail:
2553 return err;
2556 static void __net_exit ipmr_net_exit(struct net *net)
2558 #ifdef CONFIG_PROC_FS
2559 proc_net_remove(net, "ip_mr_cache");
2560 proc_net_remove(net, "ip_mr_vif");
2561 #endif
2562 ipmr_rules_exit(net);
2565 static struct pernet_operations ipmr_net_ops = {
2566 .init = ipmr_net_init,
2567 .exit = ipmr_net_exit,
2570 int __init ip_mr_init(void)
2572 int err;
2574 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2575 sizeof(struct mfc_cache),
2576 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2577 NULL);
2578 if (!mrt_cachep)
2579 return -ENOMEM;
2581 err = register_pernet_subsys(&ipmr_net_ops);
2582 if (err)
2583 goto reg_pernet_fail;
2585 err = register_netdevice_notifier(&ip_mr_notifier);
2586 if (err)
2587 goto reg_notif_fail;
2588 #ifdef CONFIG_IP_PIMSM_V2
2589 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2590 pr_err("%s: can't add PIM protocol\n", __func__);
2591 err = -EAGAIN;
2592 goto add_proto_fail;
2594 #endif
2595 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2596 NULL, ipmr_rtm_dumproute, NULL);
2597 return 0;
2599 #ifdef CONFIG_IP_PIMSM_V2
2600 add_proto_fail:
2601 unregister_netdevice_notifier(&ip_mr_notifier);
2602 #endif
2603 reg_notif_fail:
2604 unregister_pernet_subsys(&ipmr_net_ops);
2605 reg_pernet_fail:
2606 kmem_cache_destroy(mrt_cachep);
2607 return err;