Staging: hv: remove OnChildDeviceRemove vmbus_driver callback
[zen-stable.git] / net / ipv4 / ipmr.c
blob86dd5691af46dfc4d87631127e9fa06b7329dc3c
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/system.h>
30 #include <asm/uaccess.h>
31 #include <linux/types.h>
32 #include <linux/capability.h>
33 #include <linux/errno.h>
34 #include <linux/timer.h>
35 #include <linux/mm.h>
36 #include <linux/kernel.h>
37 #include <linux/fcntl.h>
38 #include <linux/stat.h>
39 #include <linux/socket.h>
40 #include <linux/in.h>
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/inetdevice.h>
44 #include <linux/igmp.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <linux/mroute.h>
48 #include <linux/init.h>
49 #include <linux/if_ether.h>
50 #include <linux/slab.h>
51 #include <net/net_namespace.h>
52 #include <net/ip.h>
53 #include <net/protocol.h>
54 #include <linux/skbuff.h>
55 #include <net/route.h>
56 #include <net/sock.h>
57 #include <net/icmp.h>
58 #include <net/udp.h>
59 #include <net/raw.h>
60 #include <linux/notifier.h>
61 #include <linux/if_arp.h>
62 #include <linux/netfilter_ipv4.h>
63 #include <net/ipip.h>
64 #include <net/checksum.h>
65 #include <net/netlink.h>
66 #include <net/fib_rules.h>
68 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
69 #define CONFIG_IP_PIMSM 1
70 #endif
72 struct mr_table {
73 struct list_head list;
74 #ifdef CONFIG_NET_NS
75 struct net *net;
76 #endif
77 u32 id;
78 struct sock __rcu *mroute_sk;
79 struct timer_list ipmr_expire_timer;
80 struct list_head mfc_unres_queue;
81 struct list_head mfc_cache_array[MFC_LINES];
82 struct vif_device vif_table[MAXVIFS];
83 int maxvif;
84 atomic_t cache_resolve_queue_len;
85 int mroute_do_assert;
86 int mroute_do_pim;
87 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
88 int mroute_reg_vif_num;
89 #endif
92 struct ipmr_rule {
93 struct fib_rule common;
96 struct ipmr_result {
97 struct mr_table *mrt;
100 /* Big lock, protecting vif table, mrt cache and mroute socket state.
101 * Note that the changes are semaphored via rtnl_lock.
104 static DEFINE_RWLOCK(mrt_lock);
107 * Multicast router control variables
110 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
112 /* Special spinlock for queue of unresolved entries */
113 static DEFINE_SPINLOCK(mfc_unres_lock);
115 /* We return to original Alan's scheme. Hash table of resolved
116 * entries is changed only in process context and protected
117 * with weak lock mrt_lock. Queue of unresolved entries is protected
118 * with strong spinlock mfc_unres_lock.
120 * In this case data path is free of exclusive locks at all.
123 static struct kmem_cache *mrt_cachep __read_mostly;
125 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
126 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
127 struct sk_buff *skb, struct mfc_cache *cache,
128 int local);
129 static int ipmr_cache_report(struct mr_table *mrt,
130 struct sk_buff *pkt, vifi_t vifi, int assert);
131 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
132 struct mfc_cache *c, struct rtmsg *rtm);
133 static void ipmr_expire_process(unsigned long arg);
135 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
136 #define ipmr_for_each_table(mrt, net) \
137 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
139 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
141 struct mr_table *mrt;
143 ipmr_for_each_table(mrt, net) {
144 if (mrt->id == id)
145 return mrt;
147 return NULL;
150 static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
151 struct mr_table **mrt)
153 struct ipmr_result res;
154 struct fib_lookup_arg arg = { .result = &res, };
155 int err;
157 err = fib_rules_lookup(net->ipv4.mr_rules_ops, flp, 0, &arg);
158 if (err < 0)
159 return err;
160 *mrt = res.mrt;
161 return 0;
164 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
165 int flags, struct fib_lookup_arg *arg)
167 struct ipmr_result *res = arg->result;
168 struct mr_table *mrt;
170 switch (rule->action) {
171 case FR_ACT_TO_TBL:
172 break;
173 case FR_ACT_UNREACHABLE:
174 return -ENETUNREACH;
175 case FR_ACT_PROHIBIT:
176 return -EACCES;
177 case FR_ACT_BLACKHOLE:
178 default:
179 return -EINVAL;
182 mrt = ipmr_get_table(rule->fr_net, rule->table);
183 if (mrt == NULL)
184 return -EAGAIN;
185 res->mrt = mrt;
186 return 0;
189 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
191 return 1;
194 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
195 FRA_GENERIC_POLICY,
198 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
199 struct fib_rule_hdr *frh, struct nlattr **tb)
201 return 0;
204 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
205 struct nlattr **tb)
207 return 1;
210 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
211 struct fib_rule_hdr *frh)
213 frh->dst_len = 0;
214 frh->src_len = 0;
215 frh->tos = 0;
216 return 0;
219 static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
220 .family = RTNL_FAMILY_IPMR,
221 .rule_size = sizeof(struct ipmr_rule),
222 .addr_size = sizeof(u32),
223 .action = ipmr_rule_action,
224 .match = ipmr_rule_match,
225 .configure = ipmr_rule_configure,
226 .compare = ipmr_rule_compare,
227 .default_pref = fib_default_rule_pref,
228 .fill = ipmr_rule_fill,
229 .nlgroup = RTNLGRP_IPV4_RULE,
230 .policy = ipmr_rule_policy,
231 .owner = THIS_MODULE,
234 static int __net_init ipmr_rules_init(struct net *net)
236 struct fib_rules_ops *ops;
237 struct mr_table *mrt;
238 int err;
240 ops = fib_rules_register(&ipmr_rules_ops_template, net);
241 if (IS_ERR(ops))
242 return PTR_ERR(ops);
244 INIT_LIST_HEAD(&net->ipv4.mr_tables);
246 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
247 if (mrt == NULL) {
248 err = -ENOMEM;
249 goto err1;
252 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
253 if (err < 0)
254 goto err2;
256 net->ipv4.mr_rules_ops = ops;
257 return 0;
259 err2:
260 kfree(mrt);
261 err1:
262 fib_rules_unregister(ops);
263 return err;
266 static void __net_exit ipmr_rules_exit(struct net *net)
268 struct mr_table *mrt, *next;
270 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
271 list_del(&mrt->list);
272 kfree(mrt);
274 fib_rules_unregister(net->ipv4.mr_rules_ops);
276 #else
277 #define ipmr_for_each_table(mrt, net) \
278 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
280 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
282 return net->ipv4.mrt;
285 static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
286 struct mr_table **mrt)
288 *mrt = net->ipv4.mrt;
289 return 0;
292 static int __net_init ipmr_rules_init(struct net *net)
294 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
295 return net->ipv4.mrt ? 0 : -ENOMEM;
298 static void __net_exit ipmr_rules_exit(struct net *net)
300 kfree(net->ipv4.mrt);
302 #endif
304 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
306 struct mr_table *mrt;
307 unsigned int i;
309 mrt = ipmr_get_table(net, id);
310 if (mrt != NULL)
311 return mrt;
313 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
314 if (mrt == NULL)
315 return NULL;
316 write_pnet(&mrt->net, net);
317 mrt->id = id;
319 /* Forwarding cache */
320 for (i = 0; i < MFC_LINES; i++)
321 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
323 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
325 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
326 (unsigned long)mrt);
328 #ifdef CONFIG_IP_PIMSM
329 mrt->mroute_reg_vif_num = -1;
330 #endif
331 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
332 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
333 #endif
334 return mrt;
337 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
339 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
341 struct net *net = dev_net(dev);
343 dev_close(dev);
345 dev = __dev_get_by_name(net, "tunl0");
346 if (dev) {
347 const struct net_device_ops *ops = dev->netdev_ops;
348 struct ifreq ifr;
349 struct ip_tunnel_parm p;
351 memset(&p, 0, sizeof(p));
352 p.iph.daddr = v->vifc_rmt_addr.s_addr;
353 p.iph.saddr = v->vifc_lcl_addr.s_addr;
354 p.iph.version = 4;
355 p.iph.ihl = 5;
356 p.iph.protocol = IPPROTO_IPIP;
357 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
358 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
360 if (ops->ndo_do_ioctl) {
361 mm_segment_t oldfs = get_fs();
363 set_fs(KERNEL_DS);
364 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
365 set_fs(oldfs);
370 static
371 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
373 struct net_device *dev;
375 dev = __dev_get_by_name(net, "tunl0");
377 if (dev) {
378 const struct net_device_ops *ops = dev->netdev_ops;
379 int err;
380 struct ifreq ifr;
381 struct ip_tunnel_parm p;
382 struct in_device *in_dev;
384 memset(&p, 0, sizeof(p));
385 p.iph.daddr = v->vifc_rmt_addr.s_addr;
386 p.iph.saddr = v->vifc_lcl_addr.s_addr;
387 p.iph.version = 4;
388 p.iph.ihl = 5;
389 p.iph.protocol = IPPROTO_IPIP;
390 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
391 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
393 if (ops->ndo_do_ioctl) {
394 mm_segment_t oldfs = get_fs();
396 set_fs(KERNEL_DS);
397 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
398 set_fs(oldfs);
399 } else {
400 err = -EOPNOTSUPP;
402 dev = NULL;
404 if (err == 0 &&
405 (dev = __dev_get_by_name(net, p.name)) != NULL) {
406 dev->flags |= IFF_MULTICAST;
408 in_dev = __in_dev_get_rtnl(dev);
409 if (in_dev == NULL)
410 goto failure;
412 ipv4_devconf_setall(in_dev);
413 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
415 if (dev_open(dev))
416 goto failure;
417 dev_hold(dev);
420 return dev;
422 failure:
423 /* allow the register to be completed before unregistering. */
424 rtnl_unlock();
425 rtnl_lock();
427 unregister_netdevice(dev);
428 return NULL;
431 #ifdef CONFIG_IP_PIMSM
433 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
435 struct net *net = dev_net(dev);
436 struct mr_table *mrt;
437 struct flowi fl = {
438 .oif = dev->ifindex,
439 .iif = skb->skb_iif,
440 .mark = skb->mark,
442 int err;
444 err = ipmr_fib_lookup(net, &fl, &mrt);
445 if (err < 0) {
446 kfree_skb(skb);
447 return err;
450 read_lock(&mrt_lock);
451 dev->stats.tx_bytes += skb->len;
452 dev->stats.tx_packets++;
453 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
454 read_unlock(&mrt_lock);
455 kfree_skb(skb);
456 return NETDEV_TX_OK;
459 static const struct net_device_ops reg_vif_netdev_ops = {
460 .ndo_start_xmit = reg_vif_xmit,
463 static void reg_vif_setup(struct net_device *dev)
465 dev->type = ARPHRD_PIMREG;
466 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
467 dev->flags = IFF_NOARP;
468 dev->netdev_ops = &reg_vif_netdev_ops,
469 dev->destructor = free_netdev;
470 dev->features |= NETIF_F_NETNS_LOCAL;
473 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
475 struct net_device *dev;
476 struct in_device *in_dev;
477 char name[IFNAMSIZ];
479 if (mrt->id == RT_TABLE_DEFAULT)
480 sprintf(name, "pimreg");
481 else
482 sprintf(name, "pimreg%u", mrt->id);
484 dev = alloc_netdev(0, name, reg_vif_setup);
486 if (dev == NULL)
487 return NULL;
489 dev_net_set(dev, net);
491 if (register_netdevice(dev)) {
492 free_netdev(dev);
493 return NULL;
495 dev->iflink = 0;
497 rcu_read_lock();
498 in_dev = __in_dev_get_rcu(dev);
499 if (!in_dev) {
500 rcu_read_unlock();
501 goto failure;
504 ipv4_devconf_setall(in_dev);
505 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
506 rcu_read_unlock();
508 if (dev_open(dev))
509 goto failure;
511 dev_hold(dev);
513 return dev;
515 failure:
516 /* allow the register to be completed before unregistering. */
517 rtnl_unlock();
518 rtnl_lock();
520 unregister_netdevice(dev);
521 return NULL;
523 #endif
526 * Delete a VIF entry
527 * @notify: Set to 1, if the caller is a notifier_call
530 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
531 struct list_head *head)
533 struct vif_device *v;
534 struct net_device *dev;
535 struct in_device *in_dev;
537 if (vifi < 0 || vifi >= mrt->maxvif)
538 return -EADDRNOTAVAIL;
540 v = &mrt->vif_table[vifi];
542 write_lock_bh(&mrt_lock);
543 dev = v->dev;
544 v->dev = NULL;
546 if (!dev) {
547 write_unlock_bh(&mrt_lock);
548 return -EADDRNOTAVAIL;
551 #ifdef CONFIG_IP_PIMSM
552 if (vifi == mrt->mroute_reg_vif_num)
553 mrt->mroute_reg_vif_num = -1;
554 #endif
556 if (vifi + 1 == mrt->maxvif) {
557 int tmp;
559 for (tmp = vifi - 1; tmp >= 0; tmp--) {
560 if (VIF_EXISTS(mrt, tmp))
561 break;
563 mrt->maxvif = tmp+1;
566 write_unlock_bh(&mrt_lock);
568 dev_set_allmulti(dev, -1);
570 in_dev = __in_dev_get_rtnl(dev);
571 if (in_dev) {
572 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
573 ip_rt_multicast_event(in_dev);
576 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
577 unregister_netdevice_queue(dev, head);
579 dev_put(dev);
580 return 0;
583 static void ipmr_cache_free_rcu(struct rcu_head *head)
585 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
587 kmem_cache_free(mrt_cachep, c);
590 static inline void ipmr_cache_free(struct mfc_cache *c)
592 call_rcu(&c->rcu, ipmr_cache_free_rcu);
595 /* Destroy an unresolved cache entry, killing queued skbs
596 * and reporting error to netlink readers.
599 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
601 struct net *net = read_pnet(&mrt->net);
602 struct sk_buff *skb;
603 struct nlmsgerr *e;
605 atomic_dec(&mrt->cache_resolve_queue_len);
607 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
608 if (ip_hdr(skb)->version == 0) {
609 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
610 nlh->nlmsg_type = NLMSG_ERROR;
611 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
612 skb_trim(skb, nlh->nlmsg_len);
613 e = NLMSG_DATA(nlh);
614 e->error = -ETIMEDOUT;
615 memset(&e->msg, 0, sizeof(e->msg));
617 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
618 } else {
619 kfree_skb(skb);
623 ipmr_cache_free(c);
627 /* Timer process for the unresolved queue. */
629 static void ipmr_expire_process(unsigned long arg)
631 struct mr_table *mrt = (struct mr_table *)arg;
632 unsigned long now;
633 unsigned long expires;
634 struct mfc_cache *c, *next;
636 if (!spin_trylock(&mfc_unres_lock)) {
637 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
638 return;
641 if (list_empty(&mrt->mfc_unres_queue))
642 goto out;
644 now = jiffies;
645 expires = 10*HZ;
647 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
648 if (time_after(c->mfc_un.unres.expires, now)) {
649 unsigned long interval = c->mfc_un.unres.expires - now;
650 if (interval < expires)
651 expires = interval;
652 continue;
655 list_del(&c->list);
656 ipmr_destroy_unres(mrt, c);
659 if (!list_empty(&mrt->mfc_unres_queue))
660 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
662 out:
663 spin_unlock(&mfc_unres_lock);
666 /* Fill oifs list. It is called under write locked mrt_lock. */
668 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
669 unsigned char *ttls)
671 int vifi;
673 cache->mfc_un.res.minvif = MAXVIFS;
674 cache->mfc_un.res.maxvif = 0;
675 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
677 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
678 if (VIF_EXISTS(mrt, vifi) &&
679 ttls[vifi] && ttls[vifi] < 255) {
680 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
681 if (cache->mfc_un.res.minvif > vifi)
682 cache->mfc_un.res.minvif = vifi;
683 if (cache->mfc_un.res.maxvif <= vifi)
684 cache->mfc_un.res.maxvif = vifi + 1;
689 static int vif_add(struct net *net, struct mr_table *mrt,
690 struct vifctl *vifc, int mrtsock)
692 int vifi = vifc->vifc_vifi;
693 struct vif_device *v = &mrt->vif_table[vifi];
694 struct net_device *dev;
695 struct in_device *in_dev;
696 int err;
698 /* Is vif busy ? */
699 if (VIF_EXISTS(mrt, vifi))
700 return -EADDRINUSE;
702 switch (vifc->vifc_flags) {
703 #ifdef CONFIG_IP_PIMSM
704 case VIFF_REGISTER:
706 * Special Purpose VIF in PIM
707 * All the packets will be sent to the daemon
709 if (mrt->mroute_reg_vif_num >= 0)
710 return -EADDRINUSE;
711 dev = ipmr_reg_vif(net, mrt);
712 if (!dev)
713 return -ENOBUFS;
714 err = dev_set_allmulti(dev, 1);
715 if (err) {
716 unregister_netdevice(dev);
717 dev_put(dev);
718 return err;
720 break;
721 #endif
722 case VIFF_TUNNEL:
723 dev = ipmr_new_tunnel(net, vifc);
724 if (!dev)
725 return -ENOBUFS;
726 err = dev_set_allmulti(dev, 1);
727 if (err) {
728 ipmr_del_tunnel(dev, vifc);
729 dev_put(dev);
730 return err;
732 break;
734 case VIFF_USE_IFINDEX:
735 case 0:
736 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
737 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
738 if (dev && __in_dev_get_rtnl(dev) == NULL) {
739 dev_put(dev);
740 return -EADDRNOTAVAIL;
742 } else {
743 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
745 if (!dev)
746 return -EADDRNOTAVAIL;
747 err = dev_set_allmulti(dev, 1);
748 if (err) {
749 dev_put(dev);
750 return err;
752 break;
753 default:
754 return -EINVAL;
757 in_dev = __in_dev_get_rtnl(dev);
758 if (!in_dev) {
759 dev_put(dev);
760 return -EADDRNOTAVAIL;
762 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
763 ip_rt_multicast_event(in_dev);
765 /* Fill in the VIF structures */
767 v->rate_limit = vifc->vifc_rate_limit;
768 v->local = vifc->vifc_lcl_addr.s_addr;
769 v->remote = vifc->vifc_rmt_addr.s_addr;
770 v->flags = vifc->vifc_flags;
771 if (!mrtsock)
772 v->flags |= VIFF_STATIC;
773 v->threshold = vifc->vifc_threshold;
774 v->bytes_in = 0;
775 v->bytes_out = 0;
776 v->pkt_in = 0;
777 v->pkt_out = 0;
778 v->link = dev->ifindex;
779 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
780 v->link = dev->iflink;
782 /* And finish update writing critical data */
783 write_lock_bh(&mrt_lock);
784 v->dev = dev;
785 #ifdef CONFIG_IP_PIMSM
786 if (v->flags & VIFF_REGISTER)
787 mrt->mroute_reg_vif_num = vifi;
788 #endif
789 if (vifi+1 > mrt->maxvif)
790 mrt->maxvif = vifi+1;
791 write_unlock_bh(&mrt_lock);
792 return 0;
795 /* called with rcu_read_lock() */
796 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
797 __be32 origin,
798 __be32 mcastgrp)
800 int line = MFC_HASH(mcastgrp, origin);
801 struct mfc_cache *c;
803 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
804 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
805 return c;
807 return NULL;
811 * Allocate a multicast cache entry
813 static struct mfc_cache *ipmr_cache_alloc(void)
815 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
817 if (c)
818 c->mfc_un.res.minvif = MAXVIFS;
819 return c;
822 static struct mfc_cache *ipmr_cache_alloc_unres(void)
824 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
826 if (c) {
827 skb_queue_head_init(&c->mfc_un.unres.unresolved);
828 c->mfc_un.unres.expires = jiffies + 10*HZ;
830 return c;
834 * A cache entry has gone into a resolved state from queued
837 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
838 struct mfc_cache *uc, struct mfc_cache *c)
840 struct sk_buff *skb;
841 struct nlmsgerr *e;
843 /* Play the pending entries through our router */
845 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
846 if (ip_hdr(skb)->version == 0) {
847 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
849 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
850 nlh->nlmsg_len = skb_tail_pointer(skb) -
851 (u8 *)nlh;
852 } else {
853 nlh->nlmsg_type = NLMSG_ERROR;
854 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
855 skb_trim(skb, nlh->nlmsg_len);
856 e = NLMSG_DATA(nlh);
857 e->error = -EMSGSIZE;
858 memset(&e->msg, 0, sizeof(e->msg));
861 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
862 } else {
863 ip_mr_forward(net, mrt, skb, c, 0);
869 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
870 * expects the following bizarre scheme.
872 * Called under mrt_lock.
875 static int ipmr_cache_report(struct mr_table *mrt,
876 struct sk_buff *pkt, vifi_t vifi, int assert)
878 struct sk_buff *skb;
879 const int ihl = ip_hdrlen(pkt);
880 struct igmphdr *igmp;
881 struct igmpmsg *msg;
882 struct sock *mroute_sk;
883 int ret;
885 #ifdef CONFIG_IP_PIMSM
886 if (assert == IGMPMSG_WHOLEPKT)
887 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
888 else
889 #endif
890 skb = alloc_skb(128, GFP_ATOMIC);
892 if (!skb)
893 return -ENOBUFS;
895 #ifdef CONFIG_IP_PIMSM
896 if (assert == IGMPMSG_WHOLEPKT) {
897 /* Ugly, but we have no choice with this interface.
898 * Duplicate old header, fix ihl, length etc.
899 * And all this only to mangle msg->im_msgtype and
900 * to set msg->im_mbz to "mbz" :-)
902 skb_push(skb, sizeof(struct iphdr));
903 skb_reset_network_header(skb);
904 skb_reset_transport_header(skb);
905 msg = (struct igmpmsg *)skb_network_header(skb);
906 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
907 msg->im_msgtype = IGMPMSG_WHOLEPKT;
908 msg->im_mbz = 0;
909 msg->im_vif = mrt->mroute_reg_vif_num;
910 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
911 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
912 sizeof(struct iphdr));
913 } else
914 #endif
917 /* Copy the IP header */
919 skb->network_header = skb->tail;
920 skb_put(skb, ihl);
921 skb_copy_to_linear_data(skb, pkt->data, ihl);
922 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
923 msg = (struct igmpmsg *)skb_network_header(skb);
924 msg->im_vif = vifi;
925 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
927 /* Add our header */
929 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
930 igmp->type =
931 msg->im_msgtype = assert;
932 igmp->code = 0;
933 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
934 skb->transport_header = skb->network_header;
937 rcu_read_lock();
938 mroute_sk = rcu_dereference(mrt->mroute_sk);
939 if (mroute_sk == NULL) {
940 rcu_read_unlock();
941 kfree_skb(skb);
942 return -EINVAL;
945 /* Deliver to mrouted */
947 ret = sock_queue_rcv_skb(mroute_sk, skb);
948 rcu_read_unlock();
949 if (ret < 0) {
950 if (net_ratelimit())
951 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
952 kfree_skb(skb);
955 return ret;
959 * Queue a packet for resolution. It gets locked cache entry!
962 static int
963 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
965 bool found = false;
966 int err;
967 struct mfc_cache *c;
968 const struct iphdr *iph = ip_hdr(skb);
970 spin_lock_bh(&mfc_unres_lock);
971 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
972 if (c->mfc_mcastgrp == iph->daddr &&
973 c->mfc_origin == iph->saddr) {
974 found = true;
975 break;
979 if (!found) {
980 /* Create a new entry if allowable */
982 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
983 (c = ipmr_cache_alloc_unres()) == NULL) {
984 spin_unlock_bh(&mfc_unres_lock);
986 kfree_skb(skb);
987 return -ENOBUFS;
990 /* Fill in the new cache entry */
992 c->mfc_parent = -1;
993 c->mfc_origin = iph->saddr;
994 c->mfc_mcastgrp = iph->daddr;
996 /* Reflect first query at mrouted. */
998 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
999 if (err < 0) {
1000 /* If the report failed throw the cache entry
1001 out - Brad Parker
1003 spin_unlock_bh(&mfc_unres_lock);
1005 ipmr_cache_free(c);
1006 kfree_skb(skb);
1007 return err;
1010 atomic_inc(&mrt->cache_resolve_queue_len);
1011 list_add(&c->list, &mrt->mfc_unres_queue);
1013 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1014 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1017 /* See if we can append the packet */
1019 if (c->mfc_un.unres.unresolved.qlen > 3) {
1020 kfree_skb(skb);
1021 err = -ENOBUFS;
1022 } else {
1023 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1024 err = 0;
1027 spin_unlock_bh(&mfc_unres_lock);
1028 return err;
1032 * MFC cache manipulation by user space mroute daemon
1035 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1037 int line;
1038 struct mfc_cache *c, *next;
1040 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1042 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1043 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1044 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1045 list_del_rcu(&c->list);
1047 ipmr_cache_free(c);
1048 return 0;
1051 return -ENOENT;
1054 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1055 struct mfcctl *mfc, int mrtsock)
1057 bool found = false;
1058 int line;
1059 struct mfc_cache *uc, *c;
1061 if (mfc->mfcc_parent >= MAXVIFS)
1062 return -ENFILE;
1064 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1066 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1067 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1068 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1069 found = true;
1070 break;
1074 if (found) {
1075 write_lock_bh(&mrt_lock);
1076 c->mfc_parent = mfc->mfcc_parent;
1077 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1078 if (!mrtsock)
1079 c->mfc_flags |= MFC_STATIC;
1080 write_unlock_bh(&mrt_lock);
1081 return 0;
1084 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1085 return -EINVAL;
1087 c = ipmr_cache_alloc();
1088 if (c == NULL)
1089 return -ENOMEM;
1091 c->mfc_origin = mfc->mfcc_origin.s_addr;
1092 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
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;
1098 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1101 * Check to see if we resolved a queued list. If so we
1102 * need to send on the frames and tidy up.
1104 found = false;
1105 spin_lock_bh(&mfc_unres_lock);
1106 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1107 if (uc->mfc_origin == c->mfc_origin &&
1108 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1109 list_del(&uc->list);
1110 atomic_dec(&mrt->cache_resolve_queue_len);
1111 found = true;
1112 break;
1115 if (list_empty(&mrt->mfc_unres_queue))
1116 del_timer(&mrt->ipmr_expire_timer);
1117 spin_unlock_bh(&mfc_unres_lock);
1119 if (found) {
1120 ipmr_cache_resolve(net, mrt, uc, c);
1121 ipmr_cache_free(uc);
1123 return 0;
1127 * Close the multicast socket, and clear the vif tables etc
1130 static void mroute_clean_tables(struct mr_table *mrt)
1132 int i;
1133 LIST_HEAD(list);
1134 struct mfc_cache *c, *next;
1136 /* Shut down all active vif entries */
1138 for (i = 0; i < mrt->maxvif; i++) {
1139 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1140 vif_delete(mrt, i, 0, &list);
1142 unregister_netdevice_many(&list);
1144 /* Wipe the cache */
1146 for (i = 0; i < MFC_LINES; i++) {
1147 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1148 if (c->mfc_flags & MFC_STATIC)
1149 continue;
1150 list_del_rcu(&c->list);
1151 ipmr_cache_free(c);
1155 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1156 spin_lock_bh(&mfc_unres_lock);
1157 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1158 list_del(&c->list);
1159 ipmr_destroy_unres(mrt, c);
1161 spin_unlock_bh(&mfc_unres_lock);
1165 /* called from ip_ra_control(), before an RCU grace period,
1166 * we dont need to call synchronize_rcu() here
1168 static void mrtsock_destruct(struct sock *sk)
1170 struct net *net = sock_net(sk);
1171 struct mr_table *mrt;
1173 rtnl_lock();
1174 ipmr_for_each_table(mrt, net) {
1175 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1176 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1177 rcu_assign_pointer(mrt->mroute_sk, NULL);
1178 mroute_clean_tables(mrt);
1181 rtnl_unlock();
1185 * Socket options and virtual interface manipulation. The whole
1186 * virtual interface system is a complete heap, but unfortunately
1187 * that's how BSD mrouted happens to think. Maybe one day with a proper
1188 * MOSPF/PIM router set up we can clean this up.
1191 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1193 int ret;
1194 struct vifctl vif;
1195 struct mfcctl mfc;
1196 struct net *net = sock_net(sk);
1197 struct mr_table *mrt;
1199 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1200 if (mrt == NULL)
1201 return -ENOENT;
1203 if (optname != MRT_INIT) {
1204 if (sk != rcu_dereference_raw(mrt->mroute_sk) &&
1205 !capable(CAP_NET_ADMIN))
1206 return -EACCES;
1209 switch (optname) {
1210 case MRT_INIT:
1211 if (sk->sk_type != SOCK_RAW ||
1212 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1213 return -EOPNOTSUPP;
1214 if (optlen != sizeof(int))
1215 return -ENOPROTOOPT;
1217 rtnl_lock();
1218 if (rtnl_dereference(mrt->mroute_sk)) {
1219 rtnl_unlock();
1220 return -EADDRINUSE;
1223 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1224 if (ret == 0) {
1225 rcu_assign_pointer(mrt->mroute_sk, sk);
1226 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1228 rtnl_unlock();
1229 return ret;
1230 case MRT_DONE:
1231 if (sk != rcu_dereference_raw(mrt->mroute_sk))
1232 return -EACCES;
1233 return ip_ra_control(sk, 0, NULL);
1234 case MRT_ADD_VIF:
1235 case MRT_DEL_VIF:
1236 if (optlen != sizeof(vif))
1237 return -EINVAL;
1238 if (copy_from_user(&vif, optval, sizeof(vif)))
1239 return -EFAULT;
1240 if (vif.vifc_vifi >= MAXVIFS)
1241 return -ENFILE;
1242 rtnl_lock();
1243 if (optname == MRT_ADD_VIF) {
1244 ret = vif_add(net, mrt, &vif,
1245 sk == rtnl_dereference(mrt->mroute_sk));
1246 } else {
1247 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1249 rtnl_unlock();
1250 return ret;
1253 * Manipulate the forwarding caches. These live
1254 * in a sort of kernel/user symbiosis.
1256 case MRT_ADD_MFC:
1257 case MRT_DEL_MFC:
1258 if (optlen != sizeof(mfc))
1259 return -EINVAL;
1260 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1261 return -EFAULT;
1262 rtnl_lock();
1263 if (optname == MRT_DEL_MFC)
1264 ret = ipmr_mfc_delete(mrt, &mfc);
1265 else
1266 ret = ipmr_mfc_add(net, mrt, &mfc,
1267 sk == rtnl_dereference(mrt->mroute_sk));
1268 rtnl_unlock();
1269 return ret;
1271 * Control PIM assert.
1273 case MRT_ASSERT:
1275 int v;
1276 if (get_user(v, (int __user *)optval))
1277 return -EFAULT;
1278 mrt->mroute_do_assert = (v) ? 1 : 0;
1279 return 0;
1281 #ifdef CONFIG_IP_PIMSM
1282 case MRT_PIM:
1284 int v;
1286 if (get_user(v, (int __user *)optval))
1287 return -EFAULT;
1288 v = (v) ? 1 : 0;
1290 rtnl_lock();
1291 ret = 0;
1292 if (v != mrt->mroute_do_pim) {
1293 mrt->mroute_do_pim = v;
1294 mrt->mroute_do_assert = v;
1296 rtnl_unlock();
1297 return ret;
1299 #endif
1300 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1301 case MRT_TABLE:
1303 u32 v;
1305 if (optlen != sizeof(u32))
1306 return -EINVAL;
1307 if (get_user(v, (u32 __user *)optval))
1308 return -EFAULT;
1310 rtnl_lock();
1311 ret = 0;
1312 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1313 ret = -EBUSY;
1314 } else {
1315 if (!ipmr_new_table(net, v))
1316 ret = -ENOMEM;
1317 raw_sk(sk)->ipmr_table = v;
1319 rtnl_unlock();
1320 return ret;
1322 #endif
1324 * Spurious command, or MRT_VERSION which you cannot
1325 * set.
1327 default:
1328 return -ENOPROTOOPT;
1333 * Getsock opt support for the multicast routing system.
1336 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1338 int olr;
1339 int val;
1340 struct net *net = sock_net(sk);
1341 struct mr_table *mrt;
1343 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1344 if (mrt == NULL)
1345 return -ENOENT;
1347 if (optname != MRT_VERSION &&
1348 #ifdef CONFIG_IP_PIMSM
1349 optname != MRT_PIM &&
1350 #endif
1351 optname != MRT_ASSERT)
1352 return -ENOPROTOOPT;
1354 if (get_user(olr, optlen))
1355 return -EFAULT;
1357 olr = min_t(unsigned int, olr, sizeof(int));
1358 if (olr < 0)
1359 return -EINVAL;
1361 if (put_user(olr, optlen))
1362 return -EFAULT;
1363 if (optname == MRT_VERSION)
1364 val = 0x0305;
1365 #ifdef CONFIG_IP_PIMSM
1366 else if (optname == MRT_PIM)
1367 val = mrt->mroute_do_pim;
1368 #endif
1369 else
1370 val = mrt->mroute_do_assert;
1371 if (copy_to_user(optval, &val, olr))
1372 return -EFAULT;
1373 return 0;
1377 * The IP multicast ioctl support routines.
1380 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1382 struct sioc_sg_req sr;
1383 struct sioc_vif_req vr;
1384 struct vif_device *vif;
1385 struct mfc_cache *c;
1386 struct net *net = sock_net(sk);
1387 struct mr_table *mrt;
1389 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1390 if (mrt == NULL)
1391 return -ENOENT;
1393 switch (cmd) {
1394 case SIOCGETVIFCNT:
1395 if (copy_from_user(&vr, arg, sizeof(vr)))
1396 return -EFAULT;
1397 if (vr.vifi >= mrt->maxvif)
1398 return -EINVAL;
1399 read_lock(&mrt_lock);
1400 vif = &mrt->vif_table[vr.vifi];
1401 if (VIF_EXISTS(mrt, vr.vifi)) {
1402 vr.icount = vif->pkt_in;
1403 vr.ocount = vif->pkt_out;
1404 vr.ibytes = vif->bytes_in;
1405 vr.obytes = vif->bytes_out;
1406 read_unlock(&mrt_lock);
1408 if (copy_to_user(arg, &vr, sizeof(vr)))
1409 return -EFAULT;
1410 return 0;
1412 read_unlock(&mrt_lock);
1413 return -EADDRNOTAVAIL;
1414 case SIOCGETSGCNT:
1415 if (copy_from_user(&sr, arg, sizeof(sr)))
1416 return -EFAULT;
1418 rcu_read_lock();
1419 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1420 if (c) {
1421 sr.pktcnt = c->mfc_un.res.pkt;
1422 sr.bytecnt = c->mfc_un.res.bytes;
1423 sr.wrong_if = c->mfc_un.res.wrong_if;
1424 rcu_read_unlock();
1426 if (copy_to_user(arg, &sr, sizeof(sr)))
1427 return -EFAULT;
1428 return 0;
1430 rcu_read_unlock();
1431 return -EADDRNOTAVAIL;
1432 default:
1433 return -ENOIOCTLCMD;
1438 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1440 struct net_device *dev = ptr;
1441 struct net *net = dev_net(dev);
1442 struct mr_table *mrt;
1443 struct vif_device *v;
1444 int ct;
1445 LIST_HEAD(list);
1447 if (event != NETDEV_UNREGISTER)
1448 return NOTIFY_DONE;
1450 ipmr_for_each_table(mrt, net) {
1451 v = &mrt->vif_table[0];
1452 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1453 if (v->dev == dev)
1454 vif_delete(mrt, ct, 1, &list);
1457 unregister_netdevice_many(&list);
1458 return NOTIFY_DONE;
1462 static struct notifier_block ip_mr_notifier = {
1463 .notifier_call = ipmr_device_event,
1467 * Encapsulate a packet by attaching a valid IPIP header to it.
1468 * This avoids tunnel drivers and other mess and gives us the speed so
1469 * important for multicast video.
1472 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1474 struct iphdr *iph;
1475 struct iphdr *old_iph = ip_hdr(skb);
1477 skb_push(skb, sizeof(struct iphdr));
1478 skb->transport_header = skb->network_header;
1479 skb_reset_network_header(skb);
1480 iph = ip_hdr(skb);
1482 iph->version = 4;
1483 iph->tos = old_iph->tos;
1484 iph->ttl = old_iph->ttl;
1485 iph->frag_off = 0;
1486 iph->daddr = daddr;
1487 iph->saddr = saddr;
1488 iph->protocol = IPPROTO_IPIP;
1489 iph->ihl = 5;
1490 iph->tot_len = htons(skb->len);
1491 ip_select_ident(iph, skb_dst(skb), NULL);
1492 ip_send_check(iph);
1494 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1495 nf_reset(skb);
1498 static inline int ipmr_forward_finish(struct sk_buff *skb)
1500 struct ip_options *opt = &(IPCB(skb)->opt);
1502 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1504 if (unlikely(opt->optlen))
1505 ip_forward_options(skb);
1507 return dst_output(skb);
1511 * Processing handlers for ipmr_forward
1514 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1515 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1517 const struct iphdr *iph = ip_hdr(skb);
1518 struct vif_device *vif = &mrt->vif_table[vifi];
1519 struct net_device *dev;
1520 struct rtable *rt;
1521 int encap = 0;
1523 if (vif->dev == NULL)
1524 goto out_free;
1526 #ifdef CONFIG_IP_PIMSM
1527 if (vif->flags & VIFF_REGISTER) {
1528 vif->pkt_out++;
1529 vif->bytes_out += skb->len;
1530 vif->dev->stats.tx_bytes += skb->len;
1531 vif->dev->stats.tx_packets++;
1532 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1533 goto out_free;
1535 #endif
1537 if (vif->flags & VIFF_TUNNEL) {
1538 struct flowi fl = {
1539 .oif = vif->link,
1540 .nl_u = {
1541 .ip4_u = {
1542 .daddr = vif->remote,
1543 .saddr = vif->local,
1544 .tos = RT_TOS(iph->tos)
1547 .proto = IPPROTO_IPIP
1550 if (ip_route_output_key(net, &rt, &fl))
1551 goto out_free;
1552 encap = sizeof(struct iphdr);
1553 } else {
1554 struct flowi fl = {
1555 .oif = vif->link,
1556 .nl_u = {
1557 .ip4_u = {
1558 .daddr = iph->daddr,
1559 .tos = RT_TOS(iph->tos)
1562 .proto = IPPROTO_IPIP
1565 if (ip_route_output_key(net, &rt, &fl))
1566 goto out_free;
1569 dev = rt->dst.dev;
1571 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1572 /* Do not fragment multicasts. Alas, IPv4 does not
1573 * allow to send ICMP, so that packets will disappear
1574 * to blackhole.
1577 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1578 ip_rt_put(rt);
1579 goto out_free;
1582 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1584 if (skb_cow(skb, encap)) {
1585 ip_rt_put(rt);
1586 goto out_free;
1589 vif->pkt_out++;
1590 vif->bytes_out += skb->len;
1592 skb_dst_drop(skb);
1593 skb_dst_set(skb, &rt->dst);
1594 ip_decrease_ttl(ip_hdr(skb));
1596 /* FIXME: forward and output firewalls used to be called here.
1597 * What do we do with netfilter? -- RR
1599 if (vif->flags & VIFF_TUNNEL) {
1600 ip_encap(skb, vif->local, vif->remote);
1601 /* FIXME: extra output firewall step used to be here. --RR */
1602 vif->dev->stats.tx_packets++;
1603 vif->dev->stats.tx_bytes += skb->len;
1606 IPCB(skb)->flags |= IPSKB_FORWARDED;
1609 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1610 * not only before forwarding, but after forwarding on all output
1611 * interfaces. It is clear, if mrouter runs a multicasting
1612 * program, it should receive packets not depending to what interface
1613 * program is joined.
1614 * If we will not make it, the program will have to join on all
1615 * interfaces. On the other hand, multihoming host (or router, but
1616 * not mrouter) cannot join to more than one interface - it will
1617 * result in receiving multiple packets.
1619 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1620 ipmr_forward_finish);
1621 return;
1623 out_free:
1624 kfree_skb(skb);
1627 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1629 int ct;
1631 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1632 if (mrt->vif_table[ct].dev == dev)
1633 break;
1635 return ct;
1638 /* "local" means that we should preserve one skb (for local delivery) */
1640 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1641 struct sk_buff *skb, struct mfc_cache *cache,
1642 int local)
1644 int psend = -1;
1645 int vif, ct;
1647 vif = cache->mfc_parent;
1648 cache->mfc_un.res.pkt++;
1649 cache->mfc_un.res.bytes += skb->len;
1652 * Wrong interface: drop packet and (maybe) send PIM assert.
1654 if (mrt->vif_table[vif].dev != skb->dev) {
1655 int true_vifi;
1657 if (skb_rtable(skb)->fl.iif == 0) {
1658 /* It is our own packet, looped back.
1659 * Very complicated situation...
1661 * The best workaround until routing daemons will be
1662 * fixed is not to redistribute packet, if it was
1663 * send through wrong interface. It means, that
1664 * multicast applications WILL NOT work for
1665 * (S,G), which have default multicast route pointing
1666 * to wrong oif. In any case, it is not a good
1667 * idea to use multicasting applications on router.
1669 goto dont_forward;
1672 cache->mfc_un.res.wrong_if++;
1673 true_vifi = ipmr_find_vif(mrt, skb->dev);
1675 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1676 /* pimsm uses asserts, when switching from RPT to SPT,
1677 * so that we cannot check that packet arrived on an oif.
1678 * It is bad, but otherwise we would need to move pretty
1679 * large chunk of pimd to kernel. Ough... --ANK
1681 (mrt->mroute_do_pim ||
1682 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1683 time_after(jiffies,
1684 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1685 cache->mfc_un.res.last_assert = jiffies;
1686 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1688 goto dont_forward;
1691 mrt->vif_table[vif].pkt_in++;
1692 mrt->vif_table[vif].bytes_in += skb->len;
1695 * Forward the frame
1697 for (ct = cache->mfc_un.res.maxvif - 1;
1698 ct >= cache->mfc_un.res.minvif; ct--) {
1699 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1700 if (psend != -1) {
1701 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1703 if (skb2)
1704 ipmr_queue_xmit(net, mrt, skb2, cache,
1705 psend);
1707 psend = ct;
1710 if (psend != -1) {
1711 if (local) {
1712 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1714 if (skb2)
1715 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1716 } else {
1717 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1718 return 0;
1722 dont_forward:
1723 if (!local)
1724 kfree_skb(skb);
1725 return 0;
1730 * Multicast packets for forwarding arrive here
1731 * Called with rcu_read_lock();
1734 int ip_mr_input(struct sk_buff *skb)
1736 struct mfc_cache *cache;
1737 struct net *net = dev_net(skb->dev);
1738 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1739 struct mr_table *mrt;
1740 int err;
1742 /* Packet is looped back after forward, it should not be
1743 * forwarded second time, but still can be delivered locally.
1745 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1746 goto dont_forward;
1748 err = ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt);
1749 if (err < 0) {
1750 kfree_skb(skb);
1751 return err;
1754 if (!local) {
1755 if (IPCB(skb)->opt.router_alert) {
1756 if (ip_call_ra_chain(skb))
1757 return 0;
1758 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1759 /* IGMPv1 (and broken IGMPv2 implementations sort of
1760 * Cisco IOS <= 11.2(8)) do not put router alert
1761 * option to IGMP packets destined to routable
1762 * groups. It is very bad, because it means
1763 * that we can forward NO IGMP messages.
1765 struct sock *mroute_sk;
1767 mroute_sk = rcu_dereference(mrt->mroute_sk);
1768 if (mroute_sk) {
1769 nf_reset(skb);
1770 raw_rcv(mroute_sk, skb);
1771 return 0;
1776 /* already under rcu_read_lock() */
1777 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1780 * No usable cache entry
1782 if (cache == NULL) {
1783 int vif;
1785 if (local) {
1786 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1787 ip_local_deliver(skb);
1788 if (skb2 == NULL)
1789 return -ENOBUFS;
1790 skb = skb2;
1793 read_lock(&mrt_lock);
1794 vif = ipmr_find_vif(mrt, skb->dev);
1795 if (vif >= 0) {
1796 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1797 read_unlock(&mrt_lock);
1799 return err2;
1801 read_unlock(&mrt_lock);
1802 kfree_skb(skb);
1803 return -ENODEV;
1806 read_lock(&mrt_lock);
1807 ip_mr_forward(net, mrt, skb, cache, local);
1808 read_unlock(&mrt_lock);
1810 if (local)
1811 return ip_local_deliver(skb);
1813 return 0;
1815 dont_forward:
1816 if (local)
1817 return ip_local_deliver(skb);
1818 kfree_skb(skb);
1819 return 0;
1822 #ifdef CONFIG_IP_PIMSM
1823 /* called with rcu_read_lock() */
1824 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1825 unsigned int pimlen)
1827 struct net_device *reg_dev = NULL;
1828 struct iphdr *encap;
1830 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1832 * Check that:
1833 * a. packet is really sent to a multicast group
1834 * b. packet is not a NULL-REGISTER
1835 * c. packet is not truncated
1837 if (!ipv4_is_multicast(encap->daddr) ||
1838 encap->tot_len == 0 ||
1839 ntohs(encap->tot_len) + pimlen > skb->len)
1840 return 1;
1842 read_lock(&mrt_lock);
1843 if (mrt->mroute_reg_vif_num >= 0)
1844 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1845 read_unlock(&mrt_lock);
1847 if (reg_dev == NULL)
1848 return 1;
1850 skb->mac_header = skb->network_header;
1851 skb_pull(skb, (u8 *)encap - skb->data);
1852 skb_reset_network_header(skb);
1853 skb->protocol = htons(ETH_P_IP);
1854 skb->ip_summed = CHECKSUM_NONE;
1855 skb->pkt_type = PACKET_HOST;
1857 skb_tunnel_rx(skb, reg_dev);
1859 netif_rx(skb);
1861 return NET_RX_SUCCESS;
1863 #endif
1865 #ifdef CONFIG_IP_PIMSM_V1
1867 * Handle IGMP messages of PIMv1
1870 int pim_rcv_v1(struct sk_buff *skb)
1872 struct igmphdr *pim;
1873 struct net *net = dev_net(skb->dev);
1874 struct mr_table *mrt;
1876 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1877 goto drop;
1879 pim = igmp_hdr(skb);
1881 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1882 goto drop;
1884 if (!mrt->mroute_do_pim ||
1885 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1886 goto drop;
1888 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1889 drop:
1890 kfree_skb(skb);
1892 return 0;
1894 #endif
1896 #ifdef CONFIG_IP_PIMSM_V2
1897 static int pim_rcv(struct sk_buff *skb)
1899 struct pimreghdr *pim;
1900 struct net *net = dev_net(skb->dev);
1901 struct mr_table *mrt;
1903 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1904 goto drop;
1906 pim = (struct pimreghdr *)skb_transport_header(skb);
1907 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1908 (pim->flags & PIM_NULL_REGISTER) ||
1909 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1910 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
1911 goto drop;
1913 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1914 goto drop;
1916 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1917 drop:
1918 kfree_skb(skb);
1920 return 0;
1922 #endif
1924 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
1925 struct mfc_cache *c, struct rtmsg *rtm)
1927 int ct;
1928 struct rtnexthop *nhp;
1929 u8 *b = skb_tail_pointer(skb);
1930 struct rtattr *mp_head;
1932 /* If cache is unresolved, don't try to parse IIF and OIF */
1933 if (c->mfc_parent >= MAXVIFS)
1934 return -ENOENT;
1936 if (VIF_EXISTS(mrt, c->mfc_parent))
1937 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
1939 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1941 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1942 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
1943 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1944 goto rtattr_failure;
1945 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1946 nhp->rtnh_flags = 0;
1947 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1948 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
1949 nhp->rtnh_len = sizeof(*nhp);
1952 mp_head->rta_type = RTA_MULTIPATH;
1953 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
1954 rtm->rtm_type = RTN_MULTICAST;
1955 return 1;
1957 rtattr_failure:
1958 nlmsg_trim(skb, b);
1959 return -EMSGSIZE;
1962 int ipmr_get_route(struct net *net,
1963 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1965 int err;
1966 struct mr_table *mrt;
1967 struct mfc_cache *cache;
1968 struct rtable *rt = skb_rtable(skb);
1970 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
1971 if (mrt == NULL)
1972 return -ENOENT;
1974 rcu_read_lock();
1975 cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst);
1977 if (cache == NULL) {
1978 struct sk_buff *skb2;
1979 struct iphdr *iph;
1980 struct net_device *dev;
1981 int vif = -1;
1983 if (nowait) {
1984 rcu_read_unlock();
1985 return -EAGAIN;
1988 dev = skb->dev;
1989 read_lock(&mrt_lock);
1990 if (dev)
1991 vif = ipmr_find_vif(mrt, dev);
1992 if (vif < 0) {
1993 read_unlock(&mrt_lock);
1994 rcu_read_unlock();
1995 return -ENODEV;
1997 skb2 = skb_clone(skb, GFP_ATOMIC);
1998 if (!skb2) {
1999 read_unlock(&mrt_lock);
2000 rcu_read_unlock();
2001 return -ENOMEM;
2004 skb_push(skb2, sizeof(struct iphdr));
2005 skb_reset_network_header(skb2);
2006 iph = ip_hdr(skb2);
2007 iph->ihl = sizeof(struct iphdr) >> 2;
2008 iph->saddr = rt->rt_src;
2009 iph->daddr = rt->rt_dst;
2010 iph->version = 0;
2011 err = ipmr_cache_unresolved(mrt, vif, skb2);
2012 read_unlock(&mrt_lock);
2013 rcu_read_unlock();
2014 return err;
2017 read_lock(&mrt_lock);
2018 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2019 cache->mfc_flags |= MFC_NOTIFY;
2020 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2021 read_unlock(&mrt_lock);
2022 rcu_read_unlock();
2023 return err;
2026 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2027 u32 pid, u32 seq, struct mfc_cache *c)
2029 struct nlmsghdr *nlh;
2030 struct rtmsg *rtm;
2032 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2033 if (nlh == NULL)
2034 return -EMSGSIZE;
2036 rtm = nlmsg_data(nlh);
2037 rtm->rtm_family = RTNL_FAMILY_IPMR;
2038 rtm->rtm_dst_len = 32;
2039 rtm->rtm_src_len = 32;
2040 rtm->rtm_tos = 0;
2041 rtm->rtm_table = mrt->id;
2042 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2043 rtm->rtm_type = RTN_MULTICAST;
2044 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2045 rtm->rtm_protocol = RTPROT_UNSPEC;
2046 rtm->rtm_flags = 0;
2048 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2049 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2051 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2052 goto nla_put_failure;
2054 return nlmsg_end(skb, nlh);
2056 nla_put_failure:
2057 nlmsg_cancel(skb, nlh);
2058 return -EMSGSIZE;
2061 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2063 struct net *net = sock_net(skb->sk);
2064 struct mr_table *mrt;
2065 struct mfc_cache *mfc;
2066 unsigned int t = 0, s_t;
2067 unsigned int h = 0, s_h;
2068 unsigned int e = 0, s_e;
2070 s_t = cb->args[0];
2071 s_h = cb->args[1];
2072 s_e = cb->args[2];
2074 rcu_read_lock();
2075 ipmr_for_each_table(mrt, net) {
2076 if (t < s_t)
2077 goto next_table;
2078 if (t > s_t)
2079 s_h = 0;
2080 for (h = s_h; h < MFC_LINES; h++) {
2081 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2082 if (e < s_e)
2083 goto next_entry;
2084 if (ipmr_fill_mroute(mrt, skb,
2085 NETLINK_CB(cb->skb).pid,
2086 cb->nlh->nlmsg_seq,
2087 mfc) < 0)
2088 goto done;
2089 next_entry:
2090 e++;
2092 e = s_e = 0;
2094 s_h = 0;
2095 next_table:
2096 t++;
2098 done:
2099 rcu_read_unlock();
2101 cb->args[2] = e;
2102 cb->args[1] = h;
2103 cb->args[0] = t;
2105 return skb->len;
2108 #ifdef CONFIG_PROC_FS
2110 * The /proc interfaces to multicast routing :
2111 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2113 struct ipmr_vif_iter {
2114 struct seq_net_private p;
2115 struct mr_table *mrt;
2116 int ct;
2119 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2120 struct ipmr_vif_iter *iter,
2121 loff_t pos)
2123 struct mr_table *mrt = iter->mrt;
2125 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2126 if (!VIF_EXISTS(mrt, iter->ct))
2127 continue;
2128 if (pos-- == 0)
2129 return &mrt->vif_table[iter->ct];
2131 return NULL;
2134 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2135 __acquires(mrt_lock)
2137 struct ipmr_vif_iter *iter = seq->private;
2138 struct net *net = seq_file_net(seq);
2139 struct mr_table *mrt;
2141 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2142 if (mrt == NULL)
2143 return ERR_PTR(-ENOENT);
2145 iter->mrt = mrt;
2147 read_lock(&mrt_lock);
2148 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2149 : SEQ_START_TOKEN;
2152 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2154 struct ipmr_vif_iter *iter = seq->private;
2155 struct net *net = seq_file_net(seq);
2156 struct mr_table *mrt = iter->mrt;
2158 ++*pos;
2159 if (v == SEQ_START_TOKEN)
2160 return ipmr_vif_seq_idx(net, iter, 0);
2162 while (++iter->ct < mrt->maxvif) {
2163 if (!VIF_EXISTS(mrt, iter->ct))
2164 continue;
2165 return &mrt->vif_table[iter->ct];
2167 return NULL;
2170 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2171 __releases(mrt_lock)
2173 read_unlock(&mrt_lock);
2176 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2178 struct ipmr_vif_iter *iter = seq->private;
2179 struct mr_table *mrt = iter->mrt;
2181 if (v == SEQ_START_TOKEN) {
2182 seq_puts(seq,
2183 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2184 } else {
2185 const struct vif_device *vif = v;
2186 const char *name = vif->dev ? vif->dev->name : "none";
2188 seq_printf(seq,
2189 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2190 vif - mrt->vif_table,
2191 name, vif->bytes_in, vif->pkt_in,
2192 vif->bytes_out, vif->pkt_out,
2193 vif->flags, vif->local, vif->remote);
2195 return 0;
2198 static const struct seq_operations ipmr_vif_seq_ops = {
2199 .start = ipmr_vif_seq_start,
2200 .next = ipmr_vif_seq_next,
2201 .stop = ipmr_vif_seq_stop,
2202 .show = ipmr_vif_seq_show,
2205 static int ipmr_vif_open(struct inode *inode, struct file *file)
2207 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2208 sizeof(struct ipmr_vif_iter));
2211 static const struct file_operations ipmr_vif_fops = {
2212 .owner = THIS_MODULE,
2213 .open = ipmr_vif_open,
2214 .read = seq_read,
2215 .llseek = seq_lseek,
2216 .release = seq_release_net,
2219 struct ipmr_mfc_iter {
2220 struct seq_net_private p;
2221 struct mr_table *mrt;
2222 struct list_head *cache;
2223 int ct;
2227 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2228 struct ipmr_mfc_iter *it, loff_t pos)
2230 struct mr_table *mrt = it->mrt;
2231 struct mfc_cache *mfc;
2233 rcu_read_lock();
2234 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2235 it->cache = &mrt->mfc_cache_array[it->ct];
2236 list_for_each_entry_rcu(mfc, it->cache, list)
2237 if (pos-- == 0)
2238 return mfc;
2240 rcu_read_unlock();
2242 spin_lock_bh(&mfc_unres_lock);
2243 it->cache = &mrt->mfc_unres_queue;
2244 list_for_each_entry(mfc, it->cache, list)
2245 if (pos-- == 0)
2246 return mfc;
2247 spin_unlock_bh(&mfc_unres_lock);
2249 it->cache = NULL;
2250 return NULL;
2254 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2256 struct ipmr_mfc_iter *it = seq->private;
2257 struct net *net = seq_file_net(seq);
2258 struct mr_table *mrt;
2260 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2261 if (mrt == NULL)
2262 return ERR_PTR(-ENOENT);
2264 it->mrt = mrt;
2265 it->cache = NULL;
2266 it->ct = 0;
2267 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2268 : SEQ_START_TOKEN;
2271 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2273 struct mfc_cache *mfc = v;
2274 struct ipmr_mfc_iter *it = seq->private;
2275 struct net *net = seq_file_net(seq);
2276 struct mr_table *mrt = it->mrt;
2278 ++*pos;
2280 if (v == SEQ_START_TOKEN)
2281 return ipmr_mfc_seq_idx(net, seq->private, 0);
2283 if (mfc->list.next != it->cache)
2284 return list_entry(mfc->list.next, struct mfc_cache, list);
2286 if (it->cache == &mrt->mfc_unres_queue)
2287 goto end_of_list;
2289 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2291 while (++it->ct < MFC_LINES) {
2292 it->cache = &mrt->mfc_cache_array[it->ct];
2293 if (list_empty(it->cache))
2294 continue;
2295 return list_first_entry(it->cache, struct mfc_cache, list);
2298 /* exhausted cache_array, show unresolved */
2299 rcu_read_unlock();
2300 it->cache = &mrt->mfc_unres_queue;
2301 it->ct = 0;
2303 spin_lock_bh(&mfc_unres_lock);
2304 if (!list_empty(it->cache))
2305 return list_first_entry(it->cache, struct mfc_cache, list);
2307 end_of_list:
2308 spin_unlock_bh(&mfc_unres_lock);
2309 it->cache = NULL;
2311 return NULL;
2314 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2316 struct ipmr_mfc_iter *it = seq->private;
2317 struct mr_table *mrt = it->mrt;
2319 if (it->cache == &mrt->mfc_unres_queue)
2320 spin_unlock_bh(&mfc_unres_lock);
2321 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2322 rcu_read_unlock();
2325 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2327 int n;
2329 if (v == SEQ_START_TOKEN) {
2330 seq_puts(seq,
2331 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2332 } else {
2333 const struct mfc_cache *mfc = v;
2334 const struct ipmr_mfc_iter *it = seq->private;
2335 const struct mr_table *mrt = it->mrt;
2337 seq_printf(seq, "%08X %08X %-3hd",
2338 (__force u32) mfc->mfc_mcastgrp,
2339 (__force u32) mfc->mfc_origin,
2340 mfc->mfc_parent);
2342 if (it->cache != &mrt->mfc_unres_queue) {
2343 seq_printf(seq, " %8lu %8lu %8lu",
2344 mfc->mfc_un.res.pkt,
2345 mfc->mfc_un.res.bytes,
2346 mfc->mfc_un.res.wrong_if);
2347 for (n = mfc->mfc_un.res.minvif;
2348 n < mfc->mfc_un.res.maxvif; n++) {
2349 if (VIF_EXISTS(mrt, n) &&
2350 mfc->mfc_un.res.ttls[n] < 255)
2351 seq_printf(seq,
2352 " %2d:%-3d",
2353 n, mfc->mfc_un.res.ttls[n]);
2355 } else {
2356 /* unresolved mfc_caches don't contain
2357 * pkt, bytes and wrong_if values
2359 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2361 seq_putc(seq, '\n');
2363 return 0;
2366 static const struct seq_operations ipmr_mfc_seq_ops = {
2367 .start = ipmr_mfc_seq_start,
2368 .next = ipmr_mfc_seq_next,
2369 .stop = ipmr_mfc_seq_stop,
2370 .show = ipmr_mfc_seq_show,
2373 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2375 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2376 sizeof(struct ipmr_mfc_iter));
2379 static const struct file_operations ipmr_mfc_fops = {
2380 .owner = THIS_MODULE,
2381 .open = ipmr_mfc_open,
2382 .read = seq_read,
2383 .llseek = seq_lseek,
2384 .release = seq_release_net,
2386 #endif
2388 #ifdef CONFIG_IP_PIMSM_V2
2389 static const struct net_protocol pim_protocol = {
2390 .handler = pim_rcv,
2391 .netns_ok = 1,
2393 #endif
2397 * Setup for IP multicast routing
2399 static int __net_init ipmr_net_init(struct net *net)
2401 int err;
2403 err = ipmr_rules_init(net);
2404 if (err < 0)
2405 goto fail;
2407 #ifdef CONFIG_PROC_FS
2408 err = -ENOMEM;
2409 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2410 goto proc_vif_fail;
2411 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2412 goto proc_cache_fail;
2413 #endif
2414 return 0;
2416 #ifdef CONFIG_PROC_FS
2417 proc_cache_fail:
2418 proc_net_remove(net, "ip_mr_vif");
2419 proc_vif_fail:
2420 ipmr_rules_exit(net);
2421 #endif
2422 fail:
2423 return err;
2426 static void __net_exit ipmr_net_exit(struct net *net)
2428 #ifdef CONFIG_PROC_FS
2429 proc_net_remove(net, "ip_mr_cache");
2430 proc_net_remove(net, "ip_mr_vif");
2431 #endif
2432 ipmr_rules_exit(net);
2435 static struct pernet_operations ipmr_net_ops = {
2436 .init = ipmr_net_init,
2437 .exit = ipmr_net_exit,
2440 int __init ip_mr_init(void)
2442 int err;
2444 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2445 sizeof(struct mfc_cache),
2446 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2447 NULL);
2448 if (!mrt_cachep)
2449 return -ENOMEM;
2451 err = register_pernet_subsys(&ipmr_net_ops);
2452 if (err)
2453 goto reg_pernet_fail;
2455 err = register_netdevice_notifier(&ip_mr_notifier);
2456 if (err)
2457 goto reg_notif_fail;
2458 #ifdef CONFIG_IP_PIMSM_V2
2459 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2460 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2461 err = -EAGAIN;
2462 goto add_proto_fail;
2464 #endif
2465 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, NULL, ipmr_rtm_dumproute);
2466 return 0;
2468 #ifdef CONFIG_IP_PIMSM_V2
2469 add_proto_fail:
2470 unregister_netdevice_notifier(&ip_mr_notifier);
2471 #endif
2472 reg_notif_fail:
2473 unregister_pernet_subsys(&ipmr_net_ops);
2474 reg_pernet_fail:
2475 kmem_cache_destroy(mrt_cachep);
2476 return err;