spi-topcliff-pch: Fix issue for transmitting over 4KByte
[zen-stable.git] / net / ipv4 / ipmr.c
blob7bc2db6db8d407987bbd06f59a25cf1b0f488989
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 <linux/compat.h>
64 #include <linux/export.h>
65 #include <net/ipip.h>
66 #include <net/checksum.h>
67 #include <net/netlink.h>
68 #include <net/fib_rules.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 int mroute_do_assert;
88 int 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 int ip_mr_forward(struct net *net, struct mr_table *mrt,
129 struct sk_buff *skb, struct mfc_cache *cache,
130 int local);
131 static int ipmr_cache_report(struct mr_table *mrt,
132 struct sk_buff *pkt, vifi_t vifi, int assert);
133 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
134 struct mfc_cache *c, struct rtmsg *rtm);
135 static void ipmr_expire_process(unsigned long arg);
137 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
138 #define ipmr_for_each_table(mrt, net) \
139 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
141 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
143 struct mr_table *mrt;
145 ipmr_for_each_table(mrt, net) {
146 if (mrt->id == id)
147 return mrt;
149 return NULL;
152 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
153 struct mr_table **mrt)
155 struct ipmr_result res;
156 struct fib_lookup_arg arg = { .result = &res, };
157 int err;
159 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
160 flowi4_to_flowi(flp4), 0, &arg);
161 if (err < 0)
162 return err;
163 *mrt = res.mrt;
164 return 0;
167 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
168 int flags, struct fib_lookup_arg *arg)
170 struct ipmr_result *res = arg->result;
171 struct mr_table *mrt;
173 switch (rule->action) {
174 case FR_ACT_TO_TBL:
175 break;
176 case FR_ACT_UNREACHABLE:
177 return -ENETUNREACH;
178 case FR_ACT_PROHIBIT:
179 return -EACCES;
180 case FR_ACT_BLACKHOLE:
181 default:
182 return -EINVAL;
185 mrt = ipmr_get_table(rule->fr_net, rule->table);
186 if (mrt == NULL)
187 return -EAGAIN;
188 res->mrt = mrt;
189 return 0;
192 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
194 return 1;
197 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
198 FRA_GENERIC_POLICY,
201 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
202 struct fib_rule_hdr *frh, struct nlattr **tb)
204 return 0;
207 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
208 struct nlattr **tb)
210 return 1;
213 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
214 struct fib_rule_hdr *frh)
216 frh->dst_len = 0;
217 frh->src_len = 0;
218 frh->tos = 0;
219 return 0;
222 static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
223 .family = RTNL_FAMILY_IPMR,
224 .rule_size = sizeof(struct ipmr_rule),
225 .addr_size = sizeof(u32),
226 .action = ipmr_rule_action,
227 .match = ipmr_rule_match,
228 .configure = ipmr_rule_configure,
229 .compare = ipmr_rule_compare,
230 .default_pref = fib_default_rule_pref,
231 .fill = ipmr_rule_fill,
232 .nlgroup = RTNLGRP_IPV4_RULE,
233 .policy = ipmr_rule_policy,
234 .owner = THIS_MODULE,
237 static int __net_init ipmr_rules_init(struct net *net)
239 struct fib_rules_ops *ops;
240 struct mr_table *mrt;
241 int err;
243 ops = fib_rules_register(&ipmr_rules_ops_template, net);
244 if (IS_ERR(ops))
245 return PTR_ERR(ops);
247 INIT_LIST_HEAD(&net->ipv4.mr_tables);
249 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
250 if (mrt == NULL) {
251 err = -ENOMEM;
252 goto err1;
255 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
256 if (err < 0)
257 goto err2;
259 net->ipv4.mr_rules_ops = ops;
260 return 0;
262 err2:
263 kfree(mrt);
264 err1:
265 fib_rules_unregister(ops);
266 return err;
269 static void __net_exit ipmr_rules_exit(struct net *net)
271 struct mr_table *mrt, *next;
273 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
274 list_del(&mrt->list);
275 kfree(mrt);
277 fib_rules_unregister(net->ipv4.mr_rules_ops);
279 #else
280 #define ipmr_for_each_table(mrt, net) \
281 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
283 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
285 return net->ipv4.mrt;
288 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
289 struct mr_table **mrt)
291 *mrt = net->ipv4.mrt;
292 return 0;
295 static int __net_init ipmr_rules_init(struct net *net)
297 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
298 return net->ipv4.mrt ? 0 : -ENOMEM;
301 static void __net_exit ipmr_rules_exit(struct net *net)
303 kfree(net->ipv4.mrt);
305 #endif
307 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
309 struct mr_table *mrt;
310 unsigned int i;
312 mrt = ipmr_get_table(net, id);
313 if (mrt != NULL)
314 return mrt;
316 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
317 if (mrt == NULL)
318 return NULL;
319 write_pnet(&mrt->net, net);
320 mrt->id = id;
322 /* Forwarding cache */
323 for (i = 0; i < MFC_LINES; i++)
324 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
326 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
328 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
329 (unsigned long)mrt);
331 #ifdef CONFIG_IP_PIMSM
332 mrt->mroute_reg_vif_num = -1;
333 #endif
334 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
335 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
336 #endif
337 return mrt;
340 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
342 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
344 struct net *net = dev_net(dev);
346 dev_close(dev);
348 dev = __dev_get_by_name(net, "tunl0");
349 if (dev) {
350 const struct net_device_ops *ops = dev->netdev_ops;
351 struct ifreq ifr;
352 struct ip_tunnel_parm p;
354 memset(&p, 0, sizeof(p));
355 p.iph.daddr = v->vifc_rmt_addr.s_addr;
356 p.iph.saddr = v->vifc_lcl_addr.s_addr;
357 p.iph.version = 4;
358 p.iph.ihl = 5;
359 p.iph.protocol = IPPROTO_IPIP;
360 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
361 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
363 if (ops->ndo_do_ioctl) {
364 mm_segment_t oldfs = get_fs();
366 set_fs(KERNEL_DS);
367 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
368 set_fs(oldfs);
373 static
374 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
376 struct net_device *dev;
378 dev = __dev_get_by_name(net, "tunl0");
380 if (dev) {
381 const struct net_device_ops *ops = dev->netdev_ops;
382 int err;
383 struct ifreq ifr;
384 struct ip_tunnel_parm p;
385 struct in_device *in_dev;
387 memset(&p, 0, sizeof(p));
388 p.iph.daddr = v->vifc_rmt_addr.s_addr;
389 p.iph.saddr = v->vifc_lcl_addr.s_addr;
390 p.iph.version = 4;
391 p.iph.ihl = 5;
392 p.iph.protocol = IPPROTO_IPIP;
393 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
394 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
396 if (ops->ndo_do_ioctl) {
397 mm_segment_t oldfs = get_fs();
399 set_fs(KERNEL_DS);
400 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
401 set_fs(oldfs);
402 } else {
403 err = -EOPNOTSUPP;
405 dev = NULL;
407 if (err == 0 &&
408 (dev = __dev_get_by_name(net, p.name)) != NULL) {
409 dev->flags |= IFF_MULTICAST;
411 in_dev = __in_dev_get_rtnl(dev);
412 if (in_dev == NULL)
413 goto failure;
415 ipv4_devconf_setall(in_dev);
416 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
418 if (dev_open(dev))
419 goto failure;
420 dev_hold(dev);
423 return dev;
425 failure:
426 /* allow the register to be completed before unregistering. */
427 rtnl_unlock();
428 rtnl_lock();
430 unregister_netdevice(dev);
431 return NULL;
434 #ifdef CONFIG_IP_PIMSM
436 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
438 struct net *net = dev_net(dev);
439 struct mr_table *mrt;
440 struct flowi4 fl4 = {
441 .flowi4_oif = dev->ifindex,
442 .flowi4_iif = skb->skb_iif,
443 .flowi4_mark = skb->mark,
445 int err;
447 err = ipmr_fib_lookup(net, &fl4, &mrt);
448 if (err < 0) {
449 kfree_skb(skb);
450 return err;
453 read_lock(&mrt_lock);
454 dev->stats.tx_bytes += skb->len;
455 dev->stats.tx_packets++;
456 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
457 read_unlock(&mrt_lock);
458 kfree_skb(skb);
459 return NETDEV_TX_OK;
462 static const struct net_device_ops reg_vif_netdev_ops = {
463 .ndo_start_xmit = reg_vif_xmit,
466 static void reg_vif_setup(struct net_device *dev)
468 dev->type = ARPHRD_PIMREG;
469 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
470 dev->flags = IFF_NOARP;
471 dev->netdev_ops = &reg_vif_netdev_ops,
472 dev->destructor = free_netdev;
473 dev->features |= NETIF_F_NETNS_LOCAL;
476 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
478 struct net_device *dev;
479 struct in_device *in_dev;
480 char name[IFNAMSIZ];
482 if (mrt->id == RT_TABLE_DEFAULT)
483 sprintf(name, "pimreg");
484 else
485 sprintf(name, "pimreg%u", mrt->id);
487 dev = alloc_netdev(0, name, reg_vif_setup);
489 if (dev == NULL)
490 return NULL;
492 dev_net_set(dev, net);
494 if (register_netdevice(dev)) {
495 free_netdev(dev);
496 return NULL;
498 dev->iflink = 0;
500 rcu_read_lock();
501 in_dev = __in_dev_get_rcu(dev);
502 if (!in_dev) {
503 rcu_read_unlock();
504 goto failure;
507 ipv4_devconf_setall(in_dev);
508 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
509 rcu_read_unlock();
511 if (dev_open(dev))
512 goto failure;
514 dev_hold(dev);
516 return dev;
518 failure:
519 /* allow the register to be completed before unregistering. */
520 rtnl_unlock();
521 rtnl_lock();
523 unregister_netdevice(dev);
524 return NULL;
526 #endif
529 * Delete a VIF entry
530 * @notify: Set to 1, if the caller is a notifier_call
533 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
534 struct list_head *head)
536 struct vif_device *v;
537 struct net_device *dev;
538 struct in_device *in_dev;
540 if (vifi < 0 || vifi >= mrt->maxvif)
541 return -EADDRNOTAVAIL;
543 v = &mrt->vif_table[vifi];
545 write_lock_bh(&mrt_lock);
546 dev = v->dev;
547 v->dev = NULL;
549 if (!dev) {
550 write_unlock_bh(&mrt_lock);
551 return -EADDRNOTAVAIL;
554 #ifdef CONFIG_IP_PIMSM
555 if (vifi == mrt->mroute_reg_vif_num)
556 mrt->mroute_reg_vif_num = -1;
557 #endif
559 if (vifi + 1 == mrt->maxvif) {
560 int tmp;
562 for (tmp = vifi - 1; tmp >= 0; tmp--) {
563 if (VIF_EXISTS(mrt, tmp))
564 break;
566 mrt->maxvif = tmp+1;
569 write_unlock_bh(&mrt_lock);
571 dev_set_allmulti(dev, -1);
573 in_dev = __in_dev_get_rtnl(dev);
574 if (in_dev) {
575 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
576 ip_rt_multicast_event(in_dev);
579 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
580 unregister_netdevice_queue(dev, head);
582 dev_put(dev);
583 return 0;
586 static void ipmr_cache_free_rcu(struct rcu_head *head)
588 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
590 kmem_cache_free(mrt_cachep, c);
593 static inline void ipmr_cache_free(struct mfc_cache *c)
595 call_rcu(&c->rcu, ipmr_cache_free_rcu);
598 /* Destroy an unresolved cache entry, killing queued skbs
599 * and reporting error to netlink readers.
602 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
604 struct net *net = read_pnet(&mrt->net);
605 struct sk_buff *skb;
606 struct nlmsgerr *e;
608 atomic_dec(&mrt->cache_resolve_queue_len);
610 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
611 if (ip_hdr(skb)->version == 0) {
612 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
613 nlh->nlmsg_type = NLMSG_ERROR;
614 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
615 skb_trim(skb, nlh->nlmsg_len);
616 e = NLMSG_DATA(nlh);
617 e->error = -ETIMEDOUT;
618 memset(&e->msg, 0, sizeof(e->msg));
620 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
621 } else {
622 kfree_skb(skb);
626 ipmr_cache_free(c);
630 /* Timer process for the unresolved queue. */
632 static void ipmr_expire_process(unsigned long arg)
634 struct mr_table *mrt = (struct mr_table *)arg;
635 unsigned long now;
636 unsigned long expires;
637 struct mfc_cache *c, *next;
639 if (!spin_trylock(&mfc_unres_lock)) {
640 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
641 return;
644 if (list_empty(&mrt->mfc_unres_queue))
645 goto out;
647 now = jiffies;
648 expires = 10*HZ;
650 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
651 if (time_after(c->mfc_un.unres.expires, now)) {
652 unsigned long interval = c->mfc_un.unres.expires - now;
653 if (interval < expires)
654 expires = interval;
655 continue;
658 list_del(&c->list);
659 ipmr_destroy_unres(mrt, c);
662 if (!list_empty(&mrt->mfc_unres_queue))
663 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
665 out:
666 spin_unlock(&mfc_unres_lock);
669 /* Fill oifs list. It is called under write locked mrt_lock. */
671 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
672 unsigned char *ttls)
674 int vifi;
676 cache->mfc_un.res.minvif = MAXVIFS;
677 cache->mfc_un.res.maxvif = 0;
678 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
680 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
681 if (VIF_EXISTS(mrt, vifi) &&
682 ttls[vifi] && ttls[vifi] < 255) {
683 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
684 if (cache->mfc_un.res.minvif > vifi)
685 cache->mfc_un.res.minvif = vifi;
686 if (cache->mfc_un.res.maxvif <= vifi)
687 cache->mfc_un.res.maxvif = vifi + 1;
692 static int vif_add(struct net *net, struct mr_table *mrt,
693 struct vifctl *vifc, int mrtsock)
695 int vifi = vifc->vifc_vifi;
696 struct vif_device *v = &mrt->vif_table[vifi];
697 struct net_device *dev;
698 struct in_device *in_dev;
699 int err;
701 /* Is vif busy ? */
702 if (VIF_EXISTS(mrt, vifi))
703 return -EADDRINUSE;
705 switch (vifc->vifc_flags) {
706 #ifdef CONFIG_IP_PIMSM
707 case VIFF_REGISTER:
709 * Special Purpose VIF in PIM
710 * All the packets will be sent to the daemon
712 if (mrt->mroute_reg_vif_num >= 0)
713 return -EADDRINUSE;
714 dev = ipmr_reg_vif(net, mrt);
715 if (!dev)
716 return -ENOBUFS;
717 err = dev_set_allmulti(dev, 1);
718 if (err) {
719 unregister_netdevice(dev);
720 dev_put(dev);
721 return err;
723 break;
724 #endif
725 case VIFF_TUNNEL:
726 dev = ipmr_new_tunnel(net, vifc);
727 if (!dev)
728 return -ENOBUFS;
729 err = dev_set_allmulti(dev, 1);
730 if (err) {
731 ipmr_del_tunnel(dev, vifc);
732 dev_put(dev);
733 return err;
735 break;
737 case VIFF_USE_IFINDEX:
738 case 0:
739 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
740 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
741 if (dev && __in_dev_get_rtnl(dev) == NULL) {
742 dev_put(dev);
743 return -EADDRNOTAVAIL;
745 } else {
746 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
748 if (!dev)
749 return -EADDRNOTAVAIL;
750 err = dev_set_allmulti(dev, 1);
751 if (err) {
752 dev_put(dev);
753 return err;
755 break;
756 default:
757 return -EINVAL;
760 in_dev = __in_dev_get_rtnl(dev);
761 if (!in_dev) {
762 dev_put(dev);
763 return -EADDRNOTAVAIL;
765 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
766 ip_rt_multicast_event(in_dev);
768 /* Fill in the VIF structures */
770 v->rate_limit = vifc->vifc_rate_limit;
771 v->local = vifc->vifc_lcl_addr.s_addr;
772 v->remote = vifc->vifc_rmt_addr.s_addr;
773 v->flags = vifc->vifc_flags;
774 if (!mrtsock)
775 v->flags |= VIFF_STATIC;
776 v->threshold = vifc->vifc_threshold;
777 v->bytes_in = 0;
778 v->bytes_out = 0;
779 v->pkt_in = 0;
780 v->pkt_out = 0;
781 v->link = dev->ifindex;
782 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
783 v->link = dev->iflink;
785 /* And finish update writing critical data */
786 write_lock_bh(&mrt_lock);
787 v->dev = dev;
788 #ifdef CONFIG_IP_PIMSM
789 if (v->flags & VIFF_REGISTER)
790 mrt->mroute_reg_vif_num = vifi;
791 #endif
792 if (vifi+1 > mrt->maxvif)
793 mrt->maxvif = vifi+1;
794 write_unlock_bh(&mrt_lock);
795 return 0;
798 /* called with rcu_read_lock() */
799 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
800 __be32 origin,
801 __be32 mcastgrp)
803 int line = MFC_HASH(mcastgrp, origin);
804 struct mfc_cache *c;
806 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
807 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
808 return c;
810 return NULL;
814 * Allocate a multicast cache entry
816 static struct mfc_cache *ipmr_cache_alloc(void)
818 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
820 if (c)
821 c->mfc_un.res.minvif = MAXVIFS;
822 return c;
825 static struct mfc_cache *ipmr_cache_alloc_unres(void)
827 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
829 if (c) {
830 skb_queue_head_init(&c->mfc_un.unres.unresolved);
831 c->mfc_un.unres.expires = jiffies + 10*HZ;
833 return c;
837 * A cache entry has gone into a resolved state from queued
840 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
841 struct mfc_cache *uc, struct mfc_cache *c)
843 struct sk_buff *skb;
844 struct nlmsgerr *e;
846 /* Play the pending entries through our router */
848 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
849 if (ip_hdr(skb)->version == 0) {
850 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
852 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
853 nlh->nlmsg_len = skb_tail_pointer(skb) -
854 (u8 *)nlh;
855 } else {
856 nlh->nlmsg_type = NLMSG_ERROR;
857 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
858 skb_trim(skb, nlh->nlmsg_len);
859 e = NLMSG_DATA(nlh);
860 e->error = -EMSGSIZE;
861 memset(&e->msg, 0, sizeof(e->msg));
864 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
865 } else {
866 ip_mr_forward(net, mrt, skb, c, 0);
872 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
873 * expects the following bizarre scheme.
875 * Called under mrt_lock.
878 static int ipmr_cache_report(struct mr_table *mrt,
879 struct sk_buff *pkt, vifi_t vifi, int assert)
881 struct sk_buff *skb;
882 const int ihl = ip_hdrlen(pkt);
883 struct igmphdr *igmp;
884 struct igmpmsg *msg;
885 struct sock *mroute_sk;
886 int ret;
888 #ifdef CONFIG_IP_PIMSM
889 if (assert == IGMPMSG_WHOLEPKT)
890 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
891 else
892 #endif
893 skb = alloc_skb(128, GFP_ATOMIC);
895 if (!skb)
896 return -ENOBUFS;
898 #ifdef CONFIG_IP_PIMSM
899 if (assert == IGMPMSG_WHOLEPKT) {
900 /* Ugly, but we have no choice with this interface.
901 * Duplicate old header, fix ihl, length etc.
902 * And all this only to mangle msg->im_msgtype and
903 * to set msg->im_mbz to "mbz" :-)
905 skb_push(skb, sizeof(struct iphdr));
906 skb_reset_network_header(skb);
907 skb_reset_transport_header(skb);
908 msg = (struct igmpmsg *)skb_network_header(skb);
909 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
910 msg->im_msgtype = IGMPMSG_WHOLEPKT;
911 msg->im_mbz = 0;
912 msg->im_vif = mrt->mroute_reg_vif_num;
913 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
914 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
915 sizeof(struct iphdr));
916 } else
917 #endif
920 /* Copy the IP header */
922 skb->network_header = skb->tail;
923 skb_put(skb, ihl);
924 skb_copy_to_linear_data(skb, pkt->data, ihl);
925 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
926 msg = (struct igmpmsg *)skb_network_header(skb);
927 msg->im_vif = vifi;
928 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
930 /* Add our header */
932 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
933 igmp->type =
934 msg->im_msgtype = assert;
935 igmp->code = 0;
936 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
937 skb->transport_header = skb->network_header;
940 rcu_read_lock();
941 mroute_sk = rcu_dereference(mrt->mroute_sk);
942 if (mroute_sk == NULL) {
943 rcu_read_unlock();
944 kfree_skb(skb);
945 return -EINVAL;
948 /* Deliver to mrouted */
950 ret = sock_queue_rcv_skb(mroute_sk, skb);
951 rcu_read_unlock();
952 if (ret < 0) {
953 if (net_ratelimit())
954 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
955 kfree_skb(skb);
958 return ret;
962 * Queue a packet for resolution. It gets locked cache entry!
965 static int
966 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
968 bool found = false;
969 int err;
970 struct mfc_cache *c;
971 const struct iphdr *iph = ip_hdr(skb);
973 spin_lock_bh(&mfc_unres_lock);
974 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
975 if (c->mfc_mcastgrp == iph->daddr &&
976 c->mfc_origin == iph->saddr) {
977 found = true;
978 break;
982 if (!found) {
983 /* Create a new entry if allowable */
985 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
986 (c = ipmr_cache_alloc_unres()) == NULL) {
987 spin_unlock_bh(&mfc_unres_lock);
989 kfree_skb(skb);
990 return -ENOBUFS;
993 /* Fill in the new cache entry */
995 c->mfc_parent = -1;
996 c->mfc_origin = iph->saddr;
997 c->mfc_mcastgrp = iph->daddr;
999 /* Reflect first query at mrouted. */
1001 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1002 if (err < 0) {
1003 /* If the report failed throw the cache entry
1004 out - Brad Parker
1006 spin_unlock_bh(&mfc_unres_lock);
1008 ipmr_cache_free(c);
1009 kfree_skb(skb);
1010 return err;
1013 atomic_inc(&mrt->cache_resolve_queue_len);
1014 list_add(&c->list, &mrt->mfc_unres_queue);
1016 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1017 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1020 /* See if we can append the packet */
1022 if (c->mfc_un.unres.unresolved.qlen > 3) {
1023 kfree_skb(skb);
1024 err = -ENOBUFS;
1025 } else {
1026 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1027 err = 0;
1030 spin_unlock_bh(&mfc_unres_lock);
1031 return err;
1035 * MFC cache manipulation by user space mroute daemon
1038 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1040 int line;
1041 struct mfc_cache *c, *next;
1043 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1045 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1046 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1047 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1048 list_del_rcu(&c->list);
1050 ipmr_cache_free(c);
1051 return 0;
1054 return -ENOENT;
1057 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1058 struct mfcctl *mfc, int mrtsock)
1060 bool found = false;
1061 int line;
1062 struct mfc_cache *uc, *c;
1064 if (mfc->mfcc_parent >= MAXVIFS)
1065 return -ENFILE;
1067 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1069 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1070 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1071 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1072 found = true;
1073 break;
1077 if (found) {
1078 write_lock_bh(&mrt_lock);
1079 c->mfc_parent = mfc->mfcc_parent;
1080 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1081 if (!mrtsock)
1082 c->mfc_flags |= MFC_STATIC;
1083 write_unlock_bh(&mrt_lock);
1084 return 0;
1087 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1088 return -EINVAL;
1090 c = ipmr_cache_alloc();
1091 if (c == NULL)
1092 return -ENOMEM;
1094 c->mfc_origin = mfc->mfcc_origin.s_addr;
1095 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1096 c->mfc_parent = mfc->mfcc_parent;
1097 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1098 if (!mrtsock)
1099 c->mfc_flags |= MFC_STATIC;
1101 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1104 * Check to see if we resolved a queued list. If so we
1105 * need to send on the frames and tidy up.
1107 found = false;
1108 spin_lock_bh(&mfc_unres_lock);
1109 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1110 if (uc->mfc_origin == c->mfc_origin &&
1111 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1112 list_del(&uc->list);
1113 atomic_dec(&mrt->cache_resolve_queue_len);
1114 found = true;
1115 break;
1118 if (list_empty(&mrt->mfc_unres_queue))
1119 del_timer(&mrt->ipmr_expire_timer);
1120 spin_unlock_bh(&mfc_unres_lock);
1122 if (found) {
1123 ipmr_cache_resolve(net, mrt, uc, c);
1124 ipmr_cache_free(uc);
1126 return 0;
1130 * Close the multicast socket, and clear the vif tables etc
1133 static void mroute_clean_tables(struct mr_table *mrt)
1135 int i;
1136 LIST_HEAD(list);
1137 struct mfc_cache *c, *next;
1139 /* Shut down all active vif entries */
1141 for (i = 0; i < mrt->maxvif; i++) {
1142 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1143 vif_delete(mrt, i, 0, &list);
1145 unregister_netdevice_many(&list);
1147 /* Wipe the cache */
1149 for (i = 0; i < MFC_LINES; i++) {
1150 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1151 if (c->mfc_flags & MFC_STATIC)
1152 continue;
1153 list_del_rcu(&c->list);
1154 ipmr_cache_free(c);
1158 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1159 spin_lock_bh(&mfc_unres_lock);
1160 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1161 list_del(&c->list);
1162 ipmr_destroy_unres(mrt, c);
1164 spin_unlock_bh(&mfc_unres_lock);
1168 /* called from ip_ra_control(), before an RCU grace period,
1169 * we dont need to call synchronize_rcu() here
1171 static void mrtsock_destruct(struct sock *sk)
1173 struct net *net = sock_net(sk);
1174 struct mr_table *mrt;
1176 rtnl_lock();
1177 ipmr_for_each_table(mrt, net) {
1178 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1179 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1180 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1181 mroute_clean_tables(mrt);
1184 rtnl_unlock();
1188 * Socket options and virtual interface manipulation. The whole
1189 * virtual interface system is a complete heap, but unfortunately
1190 * that's how BSD mrouted happens to think. Maybe one day with a proper
1191 * MOSPF/PIM router set up we can clean this up.
1194 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1196 int ret;
1197 struct vifctl vif;
1198 struct mfcctl mfc;
1199 struct net *net = sock_net(sk);
1200 struct mr_table *mrt;
1202 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1203 if (mrt == NULL)
1204 return -ENOENT;
1206 if (optname != MRT_INIT) {
1207 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1208 !capable(CAP_NET_ADMIN))
1209 return -EACCES;
1212 switch (optname) {
1213 case MRT_INIT:
1214 if (sk->sk_type != SOCK_RAW ||
1215 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1216 return -EOPNOTSUPP;
1217 if (optlen != sizeof(int))
1218 return -ENOPROTOOPT;
1220 rtnl_lock();
1221 if (rtnl_dereference(mrt->mroute_sk)) {
1222 rtnl_unlock();
1223 return -EADDRINUSE;
1226 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1227 if (ret == 0) {
1228 rcu_assign_pointer(mrt->mroute_sk, sk);
1229 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1231 rtnl_unlock();
1232 return ret;
1233 case MRT_DONE:
1234 if (sk != rcu_access_pointer(mrt->mroute_sk))
1235 return -EACCES;
1236 return ip_ra_control(sk, 0, NULL);
1237 case MRT_ADD_VIF:
1238 case MRT_DEL_VIF:
1239 if (optlen != sizeof(vif))
1240 return -EINVAL;
1241 if (copy_from_user(&vif, optval, sizeof(vif)))
1242 return -EFAULT;
1243 if (vif.vifc_vifi >= MAXVIFS)
1244 return -ENFILE;
1245 rtnl_lock();
1246 if (optname == MRT_ADD_VIF) {
1247 ret = vif_add(net, mrt, &vif,
1248 sk == rtnl_dereference(mrt->mroute_sk));
1249 } else {
1250 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1252 rtnl_unlock();
1253 return ret;
1256 * Manipulate the forwarding caches. These live
1257 * in a sort of kernel/user symbiosis.
1259 case MRT_ADD_MFC:
1260 case MRT_DEL_MFC:
1261 if (optlen != sizeof(mfc))
1262 return -EINVAL;
1263 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1264 return -EFAULT;
1265 rtnl_lock();
1266 if (optname == MRT_DEL_MFC)
1267 ret = ipmr_mfc_delete(mrt, &mfc);
1268 else
1269 ret = ipmr_mfc_add(net, mrt, &mfc,
1270 sk == rtnl_dereference(mrt->mroute_sk));
1271 rtnl_unlock();
1272 return ret;
1274 * Control PIM assert.
1276 case MRT_ASSERT:
1278 int v;
1279 if (get_user(v, (int __user *)optval))
1280 return -EFAULT;
1281 mrt->mroute_do_assert = (v) ? 1 : 0;
1282 return 0;
1284 #ifdef CONFIG_IP_PIMSM
1285 case MRT_PIM:
1287 int v;
1289 if (get_user(v, (int __user *)optval))
1290 return -EFAULT;
1291 v = (v) ? 1 : 0;
1293 rtnl_lock();
1294 ret = 0;
1295 if (v != mrt->mroute_do_pim) {
1296 mrt->mroute_do_pim = v;
1297 mrt->mroute_do_assert = v;
1299 rtnl_unlock();
1300 return ret;
1302 #endif
1303 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1304 case MRT_TABLE:
1306 u32 v;
1308 if (optlen != sizeof(u32))
1309 return -EINVAL;
1310 if (get_user(v, (u32 __user *)optval))
1311 return -EFAULT;
1313 rtnl_lock();
1314 ret = 0;
1315 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1316 ret = -EBUSY;
1317 } else {
1318 if (!ipmr_new_table(net, v))
1319 ret = -ENOMEM;
1320 raw_sk(sk)->ipmr_table = v;
1322 rtnl_unlock();
1323 return ret;
1325 #endif
1327 * Spurious command, or MRT_VERSION which you cannot
1328 * set.
1330 default:
1331 return -ENOPROTOOPT;
1336 * Getsock opt support for the multicast routing system.
1339 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1341 int olr;
1342 int val;
1343 struct net *net = sock_net(sk);
1344 struct mr_table *mrt;
1346 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1347 if (mrt == NULL)
1348 return -ENOENT;
1350 if (optname != MRT_VERSION &&
1351 #ifdef CONFIG_IP_PIMSM
1352 optname != MRT_PIM &&
1353 #endif
1354 optname != MRT_ASSERT)
1355 return -ENOPROTOOPT;
1357 if (get_user(olr, optlen))
1358 return -EFAULT;
1360 olr = min_t(unsigned int, olr, sizeof(int));
1361 if (olr < 0)
1362 return -EINVAL;
1364 if (put_user(olr, optlen))
1365 return -EFAULT;
1366 if (optname == MRT_VERSION)
1367 val = 0x0305;
1368 #ifdef CONFIG_IP_PIMSM
1369 else if (optname == MRT_PIM)
1370 val = mrt->mroute_do_pim;
1371 #endif
1372 else
1373 val = mrt->mroute_do_assert;
1374 if (copy_to_user(optval, &val, olr))
1375 return -EFAULT;
1376 return 0;
1380 * The IP multicast ioctl support routines.
1383 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1385 struct sioc_sg_req sr;
1386 struct sioc_vif_req vr;
1387 struct vif_device *vif;
1388 struct mfc_cache *c;
1389 struct net *net = sock_net(sk);
1390 struct mr_table *mrt;
1392 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1393 if (mrt == NULL)
1394 return -ENOENT;
1396 switch (cmd) {
1397 case SIOCGETVIFCNT:
1398 if (copy_from_user(&vr, arg, sizeof(vr)))
1399 return -EFAULT;
1400 if (vr.vifi >= mrt->maxvif)
1401 return -EINVAL;
1402 read_lock(&mrt_lock);
1403 vif = &mrt->vif_table[vr.vifi];
1404 if (VIF_EXISTS(mrt, vr.vifi)) {
1405 vr.icount = vif->pkt_in;
1406 vr.ocount = vif->pkt_out;
1407 vr.ibytes = vif->bytes_in;
1408 vr.obytes = vif->bytes_out;
1409 read_unlock(&mrt_lock);
1411 if (copy_to_user(arg, &vr, sizeof(vr)))
1412 return -EFAULT;
1413 return 0;
1415 read_unlock(&mrt_lock);
1416 return -EADDRNOTAVAIL;
1417 case SIOCGETSGCNT:
1418 if (copy_from_user(&sr, arg, sizeof(sr)))
1419 return -EFAULT;
1421 rcu_read_lock();
1422 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1423 if (c) {
1424 sr.pktcnt = c->mfc_un.res.pkt;
1425 sr.bytecnt = c->mfc_un.res.bytes;
1426 sr.wrong_if = c->mfc_un.res.wrong_if;
1427 rcu_read_unlock();
1429 if (copy_to_user(arg, &sr, sizeof(sr)))
1430 return -EFAULT;
1431 return 0;
1433 rcu_read_unlock();
1434 return -EADDRNOTAVAIL;
1435 default:
1436 return -ENOIOCTLCMD;
1440 #ifdef CONFIG_COMPAT
1441 struct compat_sioc_sg_req {
1442 struct in_addr src;
1443 struct in_addr grp;
1444 compat_ulong_t pktcnt;
1445 compat_ulong_t bytecnt;
1446 compat_ulong_t wrong_if;
1449 struct compat_sioc_vif_req {
1450 vifi_t vifi; /* Which iface */
1451 compat_ulong_t icount;
1452 compat_ulong_t ocount;
1453 compat_ulong_t ibytes;
1454 compat_ulong_t obytes;
1457 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1459 struct compat_sioc_sg_req sr;
1460 struct compat_sioc_vif_req vr;
1461 struct vif_device *vif;
1462 struct mfc_cache *c;
1463 struct net *net = sock_net(sk);
1464 struct mr_table *mrt;
1466 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1467 if (mrt == NULL)
1468 return -ENOENT;
1470 switch (cmd) {
1471 case SIOCGETVIFCNT:
1472 if (copy_from_user(&vr, arg, sizeof(vr)))
1473 return -EFAULT;
1474 if (vr.vifi >= mrt->maxvif)
1475 return -EINVAL;
1476 read_lock(&mrt_lock);
1477 vif = &mrt->vif_table[vr.vifi];
1478 if (VIF_EXISTS(mrt, vr.vifi)) {
1479 vr.icount = vif->pkt_in;
1480 vr.ocount = vif->pkt_out;
1481 vr.ibytes = vif->bytes_in;
1482 vr.obytes = vif->bytes_out;
1483 read_unlock(&mrt_lock);
1485 if (copy_to_user(arg, &vr, sizeof(vr)))
1486 return -EFAULT;
1487 return 0;
1489 read_unlock(&mrt_lock);
1490 return -EADDRNOTAVAIL;
1491 case SIOCGETSGCNT:
1492 if (copy_from_user(&sr, arg, sizeof(sr)))
1493 return -EFAULT;
1495 rcu_read_lock();
1496 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1497 if (c) {
1498 sr.pktcnt = c->mfc_un.res.pkt;
1499 sr.bytecnt = c->mfc_un.res.bytes;
1500 sr.wrong_if = c->mfc_un.res.wrong_if;
1501 rcu_read_unlock();
1503 if (copy_to_user(arg, &sr, sizeof(sr)))
1504 return -EFAULT;
1505 return 0;
1507 rcu_read_unlock();
1508 return -EADDRNOTAVAIL;
1509 default:
1510 return -ENOIOCTLCMD;
1513 #endif
1516 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1518 struct net_device *dev = ptr;
1519 struct net *net = dev_net(dev);
1520 struct mr_table *mrt;
1521 struct vif_device *v;
1522 int ct;
1524 if (event != NETDEV_UNREGISTER)
1525 return NOTIFY_DONE;
1527 ipmr_for_each_table(mrt, net) {
1528 v = &mrt->vif_table[0];
1529 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1530 if (v->dev == dev)
1531 vif_delete(mrt, ct, 1, NULL);
1534 return NOTIFY_DONE;
1538 static struct notifier_block ip_mr_notifier = {
1539 .notifier_call = ipmr_device_event,
1543 * Encapsulate a packet by attaching a valid IPIP header to it.
1544 * This avoids tunnel drivers and other mess and gives us the speed so
1545 * important for multicast video.
1548 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1550 struct iphdr *iph;
1551 const struct iphdr *old_iph = ip_hdr(skb);
1553 skb_push(skb, sizeof(struct iphdr));
1554 skb->transport_header = skb->network_header;
1555 skb_reset_network_header(skb);
1556 iph = ip_hdr(skb);
1558 iph->version = 4;
1559 iph->tos = old_iph->tos;
1560 iph->ttl = old_iph->ttl;
1561 iph->frag_off = 0;
1562 iph->daddr = daddr;
1563 iph->saddr = saddr;
1564 iph->protocol = IPPROTO_IPIP;
1565 iph->ihl = 5;
1566 iph->tot_len = htons(skb->len);
1567 ip_select_ident(iph, skb_dst(skb), NULL);
1568 ip_send_check(iph);
1570 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1571 nf_reset(skb);
1574 static inline int ipmr_forward_finish(struct sk_buff *skb)
1576 struct ip_options *opt = &(IPCB(skb)->opt);
1578 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1580 if (unlikely(opt->optlen))
1581 ip_forward_options(skb);
1583 return dst_output(skb);
1587 * Processing handlers for ipmr_forward
1590 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1591 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1593 const struct iphdr *iph = ip_hdr(skb);
1594 struct vif_device *vif = &mrt->vif_table[vifi];
1595 struct net_device *dev;
1596 struct rtable *rt;
1597 struct flowi4 fl4;
1598 int encap = 0;
1600 if (vif->dev == NULL)
1601 goto out_free;
1603 #ifdef CONFIG_IP_PIMSM
1604 if (vif->flags & VIFF_REGISTER) {
1605 vif->pkt_out++;
1606 vif->bytes_out += skb->len;
1607 vif->dev->stats.tx_bytes += skb->len;
1608 vif->dev->stats.tx_packets++;
1609 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1610 goto out_free;
1612 #endif
1614 if (vif->flags & VIFF_TUNNEL) {
1615 rt = ip_route_output_ports(net, &fl4, NULL,
1616 vif->remote, vif->local,
1617 0, 0,
1618 IPPROTO_IPIP,
1619 RT_TOS(iph->tos), vif->link);
1620 if (IS_ERR(rt))
1621 goto out_free;
1622 encap = sizeof(struct iphdr);
1623 } else {
1624 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1625 0, 0,
1626 IPPROTO_IPIP,
1627 RT_TOS(iph->tos), vif->link);
1628 if (IS_ERR(rt))
1629 goto out_free;
1632 dev = rt->dst.dev;
1634 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1635 /* Do not fragment multicasts. Alas, IPv4 does not
1636 * allow to send ICMP, so that packets will disappear
1637 * to blackhole.
1640 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1641 ip_rt_put(rt);
1642 goto out_free;
1645 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1647 if (skb_cow(skb, encap)) {
1648 ip_rt_put(rt);
1649 goto out_free;
1652 vif->pkt_out++;
1653 vif->bytes_out += skb->len;
1655 skb_dst_drop(skb);
1656 skb_dst_set(skb, &rt->dst);
1657 ip_decrease_ttl(ip_hdr(skb));
1659 /* FIXME: forward and output firewalls used to be called here.
1660 * What do we do with netfilter? -- RR
1662 if (vif->flags & VIFF_TUNNEL) {
1663 ip_encap(skb, vif->local, vif->remote);
1664 /* FIXME: extra output firewall step used to be here. --RR */
1665 vif->dev->stats.tx_packets++;
1666 vif->dev->stats.tx_bytes += skb->len;
1669 IPCB(skb)->flags |= IPSKB_FORWARDED;
1672 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1673 * not only before forwarding, but after forwarding on all output
1674 * interfaces. It is clear, if mrouter runs a multicasting
1675 * program, it should receive packets not depending to what interface
1676 * program is joined.
1677 * If we will not make it, the program will have to join on all
1678 * interfaces. On the other hand, multihoming host (or router, but
1679 * not mrouter) cannot join to more than one interface - it will
1680 * result in receiving multiple packets.
1682 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1683 ipmr_forward_finish);
1684 return;
1686 out_free:
1687 kfree_skb(skb);
1690 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1692 int ct;
1694 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1695 if (mrt->vif_table[ct].dev == dev)
1696 break;
1698 return ct;
1701 /* "local" means that we should preserve one skb (for local delivery) */
1703 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1704 struct sk_buff *skb, struct mfc_cache *cache,
1705 int local)
1707 int psend = -1;
1708 int vif, ct;
1710 vif = cache->mfc_parent;
1711 cache->mfc_un.res.pkt++;
1712 cache->mfc_un.res.bytes += skb->len;
1715 * Wrong interface: drop packet and (maybe) send PIM assert.
1717 if (mrt->vif_table[vif].dev != skb->dev) {
1718 int true_vifi;
1720 if (rt_is_output_route(skb_rtable(skb))) {
1721 /* It is our own packet, looped back.
1722 * Very complicated situation...
1724 * The best workaround until routing daemons will be
1725 * fixed is not to redistribute packet, if it was
1726 * send through wrong interface. It means, that
1727 * multicast applications WILL NOT work for
1728 * (S,G), which have default multicast route pointing
1729 * to wrong oif. In any case, it is not a good
1730 * idea to use multicasting applications on router.
1732 goto dont_forward;
1735 cache->mfc_un.res.wrong_if++;
1736 true_vifi = ipmr_find_vif(mrt, skb->dev);
1738 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1739 /* pimsm uses asserts, when switching from RPT to SPT,
1740 * so that we cannot check that packet arrived on an oif.
1741 * It is bad, but otherwise we would need to move pretty
1742 * large chunk of pimd to kernel. Ough... --ANK
1744 (mrt->mroute_do_pim ||
1745 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1746 time_after(jiffies,
1747 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1748 cache->mfc_un.res.last_assert = jiffies;
1749 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1751 goto dont_forward;
1754 mrt->vif_table[vif].pkt_in++;
1755 mrt->vif_table[vif].bytes_in += skb->len;
1758 * Forward the frame
1760 for (ct = cache->mfc_un.res.maxvif - 1;
1761 ct >= cache->mfc_un.res.minvif; ct--) {
1762 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1763 if (psend != -1) {
1764 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1766 if (skb2)
1767 ipmr_queue_xmit(net, mrt, skb2, cache,
1768 psend);
1770 psend = ct;
1773 if (psend != -1) {
1774 if (local) {
1775 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1777 if (skb2)
1778 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1779 } else {
1780 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1781 return 0;
1785 dont_forward:
1786 if (!local)
1787 kfree_skb(skb);
1788 return 0;
1791 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1793 struct rtable *rt = skb_rtable(skb);
1794 struct iphdr *iph = ip_hdr(skb);
1795 struct flowi4 fl4 = {
1796 .daddr = iph->daddr,
1797 .saddr = iph->saddr,
1798 .flowi4_tos = RT_TOS(iph->tos),
1799 .flowi4_oif = rt->rt_oif,
1800 .flowi4_iif = rt->rt_iif,
1801 .flowi4_mark = rt->rt_mark,
1803 struct mr_table *mrt;
1804 int err;
1806 err = ipmr_fib_lookup(net, &fl4, &mrt);
1807 if (err)
1808 return ERR_PTR(err);
1809 return mrt;
1813 * Multicast packets for forwarding arrive here
1814 * Called with rcu_read_lock();
1817 int ip_mr_input(struct sk_buff *skb)
1819 struct mfc_cache *cache;
1820 struct net *net = dev_net(skb->dev);
1821 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1822 struct mr_table *mrt;
1824 /* Packet is looped back after forward, it should not be
1825 * forwarded second time, but still can be delivered locally.
1827 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1828 goto dont_forward;
1830 mrt = ipmr_rt_fib_lookup(net, skb);
1831 if (IS_ERR(mrt)) {
1832 kfree_skb(skb);
1833 return PTR_ERR(mrt);
1835 if (!local) {
1836 if (IPCB(skb)->opt.router_alert) {
1837 if (ip_call_ra_chain(skb))
1838 return 0;
1839 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1840 /* IGMPv1 (and broken IGMPv2 implementations sort of
1841 * Cisco IOS <= 11.2(8)) do not put router alert
1842 * option to IGMP packets destined to routable
1843 * groups. It is very bad, because it means
1844 * that we can forward NO IGMP messages.
1846 struct sock *mroute_sk;
1848 mroute_sk = rcu_dereference(mrt->mroute_sk);
1849 if (mroute_sk) {
1850 nf_reset(skb);
1851 raw_rcv(mroute_sk, skb);
1852 return 0;
1857 /* already under rcu_read_lock() */
1858 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1861 * No usable cache entry
1863 if (cache == NULL) {
1864 int vif;
1866 if (local) {
1867 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1868 ip_local_deliver(skb);
1869 if (skb2 == NULL)
1870 return -ENOBUFS;
1871 skb = skb2;
1874 read_lock(&mrt_lock);
1875 vif = ipmr_find_vif(mrt, skb->dev);
1876 if (vif >= 0) {
1877 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1878 read_unlock(&mrt_lock);
1880 return err2;
1882 read_unlock(&mrt_lock);
1883 kfree_skb(skb);
1884 return -ENODEV;
1887 read_lock(&mrt_lock);
1888 ip_mr_forward(net, mrt, skb, cache, local);
1889 read_unlock(&mrt_lock);
1891 if (local)
1892 return ip_local_deliver(skb);
1894 return 0;
1896 dont_forward:
1897 if (local)
1898 return ip_local_deliver(skb);
1899 kfree_skb(skb);
1900 return 0;
1903 #ifdef CONFIG_IP_PIMSM
1904 /* called with rcu_read_lock() */
1905 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1906 unsigned int pimlen)
1908 struct net_device *reg_dev = NULL;
1909 struct iphdr *encap;
1911 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1913 * Check that:
1914 * a. packet is really sent to a multicast group
1915 * b. packet is not a NULL-REGISTER
1916 * c. packet is not truncated
1918 if (!ipv4_is_multicast(encap->daddr) ||
1919 encap->tot_len == 0 ||
1920 ntohs(encap->tot_len) + pimlen > skb->len)
1921 return 1;
1923 read_lock(&mrt_lock);
1924 if (mrt->mroute_reg_vif_num >= 0)
1925 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1926 read_unlock(&mrt_lock);
1928 if (reg_dev == NULL)
1929 return 1;
1931 skb->mac_header = skb->network_header;
1932 skb_pull(skb, (u8 *)encap - skb->data);
1933 skb_reset_network_header(skb);
1934 skb->protocol = htons(ETH_P_IP);
1935 skb->ip_summed = CHECKSUM_NONE;
1936 skb->pkt_type = PACKET_HOST;
1938 skb_tunnel_rx(skb, reg_dev);
1940 netif_rx(skb);
1942 return NET_RX_SUCCESS;
1944 #endif
1946 #ifdef CONFIG_IP_PIMSM_V1
1948 * Handle IGMP messages of PIMv1
1951 int pim_rcv_v1(struct sk_buff *skb)
1953 struct igmphdr *pim;
1954 struct net *net = dev_net(skb->dev);
1955 struct mr_table *mrt;
1957 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1958 goto drop;
1960 pim = igmp_hdr(skb);
1962 mrt = ipmr_rt_fib_lookup(net, skb);
1963 if (IS_ERR(mrt))
1964 goto drop;
1965 if (!mrt->mroute_do_pim ||
1966 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1967 goto drop;
1969 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1970 drop:
1971 kfree_skb(skb);
1973 return 0;
1975 #endif
1977 #ifdef CONFIG_IP_PIMSM_V2
1978 static int pim_rcv(struct sk_buff *skb)
1980 struct pimreghdr *pim;
1981 struct net *net = dev_net(skb->dev);
1982 struct mr_table *mrt;
1984 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1985 goto drop;
1987 pim = (struct pimreghdr *)skb_transport_header(skb);
1988 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1989 (pim->flags & PIM_NULL_REGISTER) ||
1990 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1991 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
1992 goto drop;
1994 mrt = ipmr_rt_fib_lookup(net, skb);
1995 if (IS_ERR(mrt))
1996 goto drop;
1997 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1998 drop:
1999 kfree_skb(skb);
2001 return 0;
2003 #endif
2005 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2006 struct mfc_cache *c, struct rtmsg *rtm)
2008 int ct;
2009 struct rtnexthop *nhp;
2010 u8 *b = skb_tail_pointer(skb);
2011 struct rtattr *mp_head;
2013 /* If cache is unresolved, don't try to parse IIF and OIF */
2014 if (c->mfc_parent >= MAXVIFS)
2015 return -ENOENT;
2017 if (VIF_EXISTS(mrt, c->mfc_parent))
2018 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
2020 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
2022 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2023 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2024 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2025 goto rtattr_failure;
2026 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
2027 nhp->rtnh_flags = 0;
2028 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2029 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2030 nhp->rtnh_len = sizeof(*nhp);
2033 mp_head->rta_type = RTA_MULTIPATH;
2034 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
2035 rtm->rtm_type = RTN_MULTICAST;
2036 return 1;
2038 rtattr_failure:
2039 nlmsg_trim(skb, b);
2040 return -EMSGSIZE;
2043 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2044 __be32 saddr, __be32 daddr,
2045 struct rtmsg *rtm, int nowait)
2047 struct mfc_cache *cache;
2048 struct mr_table *mrt;
2049 int err;
2051 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2052 if (mrt == NULL)
2053 return -ENOENT;
2055 rcu_read_lock();
2056 cache = ipmr_cache_find(mrt, saddr, daddr);
2058 if (cache == NULL) {
2059 struct sk_buff *skb2;
2060 struct iphdr *iph;
2061 struct net_device *dev;
2062 int vif = -1;
2064 if (nowait) {
2065 rcu_read_unlock();
2066 return -EAGAIN;
2069 dev = skb->dev;
2070 read_lock(&mrt_lock);
2071 if (dev)
2072 vif = ipmr_find_vif(mrt, dev);
2073 if (vif < 0) {
2074 read_unlock(&mrt_lock);
2075 rcu_read_unlock();
2076 return -ENODEV;
2078 skb2 = skb_clone(skb, GFP_ATOMIC);
2079 if (!skb2) {
2080 read_unlock(&mrt_lock);
2081 rcu_read_unlock();
2082 return -ENOMEM;
2085 skb_push(skb2, sizeof(struct iphdr));
2086 skb_reset_network_header(skb2);
2087 iph = ip_hdr(skb2);
2088 iph->ihl = sizeof(struct iphdr) >> 2;
2089 iph->saddr = saddr;
2090 iph->daddr = daddr;
2091 iph->version = 0;
2092 err = ipmr_cache_unresolved(mrt, vif, skb2);
2093 read_unlock(&mrt_lock);
2094 rcu_read_unlock();
2095 return err;
2098 read_lock(&mrt_lock);
2099 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2100 cache->mfc_flags |= MFC_NOTIFY;
2101 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2102 read_unlock(&mrt_lock);
2103 rcu_read_unlock();
2104 return err;
2107 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2108 u32 pid, u32 seq, struct mfc_cache *c)
2110 struct nlmsghdr *nlh;
2111 struct rtmsg *rtm;
2113 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2114 if (nlh == NULL)
2115 return -EMSGSIZE;
2117 rtm = nlmsg_data(nlh);
2118 rtm->rtm_family = RTNL_FAMILY_IPMR;
2119 rtm->rtm_dst_len = 32;
2120 rtm->rtm_src_len = 32;
2121 rtm->rtm_tos = 0;
2122 rtm->rtm_table = mrt->id;
2123 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2124 rtm->rtm_type = RTN_MULTICAST;
2125 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2126 rtm->rtm_protocol = RTPROT_UNSPEC;
2127 rtm->rtm_flags = 0;
2129 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2130 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2132 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2133 goto nla_put_failure;
2135 return nlmsg_end(skb, nlh);
2137 nla_put_failure:
2138 nlmsg_cancel(skb, nlh);
2139 return -EMSGSIZE;
2142 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2144 struct net *net = sock_net(skb->sk);
2145 struct mr_table *mrt;
2146 struct mfc_cache *mfc;
2147 unsigned int t = 0, s_t;
2148 unsigned int h = 0, s_h;
2149 unsigned int e = 0, s_e;
2151 s_t = cb->args[0];
2152 s_h = cb->args[1];
2153 s_e = cb->args[2];
2155 rcu_read_lock();
2156 ipmr_for_each_table(mrt, net) {
2157 if (t < s_t)
2158 goto next_table;
2159 if (t > s_t)
2160 s_h = 0;
2161 for (h = s_h; h < MFC_LINES; h++) {
2162 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2163 if (e < s_e)
2164 goto next_entry;
2165 if (ipmr_fill_mroute(mrt, skb,
2166 NETLINK_CB(cb->skb).pid,
2167 cb->nlh->nlmsg_seq,
2168 mfc) < 0)
2169 goto done;
2170 next_entry:
2171 e++;
2173 e = s_e = 0;
2175 s_h = 0;
2176 next_table:
2177 t++;
2179 done:
2180 rcu_read_unlock();
2182 cb->args[2] = e;
2183 cb->args[1] = h;
2184 cb->args[0] = t;
2186 return skb->len;
2189 #ifdef CONFIG_PROC_FS
2191 * The /proc interfaces to multicast routing :
2192 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2194 struct ipmr_vif_iter {
2195 struct seq_net_private p;
2196 struct mr_table *mrt;
2197 int ct;
2200 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2201 struct ipmr_vif_iter *iter,
2202 loff_t pos)
2204 struct mr_table *mrt = iter->mrt;
2206 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2207 if (!VIF_EXISTS(mrt, iter->ct))
2208 continue;
2209 if (pos-- == 0)
2210 return &mrt->vif_table[iter->ct];
2212 return NULL;
2215 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2216 __acquires(mrt_lock)
2218 struct ipmr_vif_iter *iter = seq->private;
2219 struct net *net = seq_file_net(seq);
2220 struct mr_table *mrt;
2222 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2223 if (mrt == NULL)
2224 return ERR_PTR(-ENOENT);
2226 iter->mrt = mrt;
2228 read_lock(&mrt_lock);
2229 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2230 : SEQ_START_TOKEN;
2233 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2235 struct ipmr_vif_iter *iter = seq->private;
2236 struct net *net = seq_file_net(seq);
2237 struct mr_table *mrt = iter->mrt;
2239 ++*pos;
2240 if (v == SEQ_START_TOKEN)
2241 return ipmr_vif_seq_idx(net, iter, 0);
2243 while (++iter->ct < mrt->maxvif) {
2244 if (!VIF_EXISTS(mrt, iter->ct))
2245 continue;
2246 return &mrt->vif_table[iter->ct];
2248 return NULL;
2251 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2252 __releases(mrt_lock)
2254 read_unlock(&mrt_lock);
2257 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2259 struct ipmr_vif_iter *iter = seq->private;
2260 struct mr_table *mrt = iter->mrt;
2262 if (v == SEQ_START_TOKEN) {
2263 seq_puts(seq,
2264 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2265 } else {
2266 const struct vif_device *vif = v;
2267 const char *name = vif->dev ? vif->dev->name : "none";
2269 seq_printf(seq,
2270 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2271 vif - mrt->vif_table,
2272 name, vif->bytes_in, vif->pkt_in,
2273 vif->bytes_out, vif->pkt_out,
2274 vif->flags, vif->local, vif->remote);
2276 return 0;
2279 static const struct seq_operations ipmr_vif_seq_ops = {
2280 .start = ipmr_vif_seq_start,
2281 .next = ipmr_vif_seq_next,
2282 .stop = ipmr_vif_seq_stop,
2283 .show = ipmr_vif_seq_show,
2286 static int ipmr_vif_open(struct inode *inode, struct file *file)
2288 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2289 sizeof(struct ipmr_vif_iter));
2292 static const struct file_operations ipmr_vif_fops = {
2293 .owner = THIS_MODULE,
2294 .open = ipmr_vif_open,
2295 .read = seq_read,
2296 .llseek = seq_lseek,
2297 .release = seq_release_net,
2300 struct ipmr_mfc_iter {
2301 struct seq_net_private p;
2302 struct mr_table *mrt;
2303 struct list_head *cache;
2304 int ct;
2308 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2309 struct ipmr_mfc_iter *it, loff_t pos)
2311 struct mr_table *mrt = it->mrt;
2312 struct mfc_cache *mfc;
2314 rcu_read_lock();
2315 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2316 it->cache = &mrt->mfc_cache_array[it->ct];
2317 list_for_each_entry_rcu(mfc, it->cache, list)
2318 if (pos-- == 0)
2319 return mfc;
2321 rcu_read_unlock();
2323 spin_lock_bh(&mfc_unres_lock);
2324 it->cache = &mrt->mfc_unres_queue;
2325 list_for_each_entry(mfc, it->cache, list)
2326 if (pos-- == 0)
2327 return mfc;
2328 spin_unlock_bh(&mfc_unres_lock);
2330 it->cache = NULL;
2331 return NULL;
2335 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2337 struct ipmr_mfc_iter *it = seq->private;
2338 struct net *net = seq_file_net(seq);
2339 struct mr_table *mrt;
2341 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2342 if (mrt == NULL)
2343 return ERR_PTR(-ENOENT);
2345 it->mrt = mrt;
2346 it->cache = NULL;
2347 it->ct = 0;
2348 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2349 : SEQ_START_TOKEN;
2352 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2354 struct mfc_cache *mfc = v;
2355 struct ipmr_mfc_iter *it = seq->private;
2356 struct net *net = seq_file_net(seq);
2357 struct mr_table *mrt = it->mrt;
2359 ++*pos;
2361 if (v == SEQ_START_TOKEN)
2362 return ipmr_mfc_seq_idx(net, seq->private, 0);
2364 if (mfc->list.next != it->cache)
2365 return list_entry(mfc->list.next, struct mfc_cache, list);
2367 if (it->cache == &mrt->mfc_unres_queue)
2368 goto end_of_list;
2370 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2372 while (++it->ct < MFC_LINES) {
2373 it->cache = &mrt->mfc_cache_array[it->ct];
2374 if (list_empty(it->cache))
2375 continue;
2376 return list_first_entry(it->cache, struct mfc_cache, list);
2379 /* exhausted cache_array, show unresolved */
2380 rcu_read_unlock();
2381 it->cache = &mrt->mfc_unres_queue;
2382 it->ct = 0;
2384 spin_lock_bh(&mfc_unres_lock);
2385 if (!list_empty(it->cache))
2386 return list_first_entry(it->cache, struct mfc_cache, list);
2388 end_of_list:
2389 spin_unlock_bh(&mfc_unres_lock);
2390 it->cache = NULL;
2392 return NULL;
2395 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2397 struct ipmr_mfc_iter *it = seq->private;
2398 struct mr_table *mrt = it->mrt;
2400 if (it->cache == &mrt->mfc_unres_queue)
2401 spin_unlock_bh(&mfc_unres_lock);
2402 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2403 rcu_read_unlock();
2406 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2408 int n;
2410 if (v == SEQ_START_TOKEN) {
2411 seq_puts(seq,
2412 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2413 } else {
2414 const struct mfc_cache *mfc = v;
2415 const struct ipmr_mfc_iter *it = seq->private;
2416 const struct mr_table *mrt = it->mrt;
2418 seq_printf(seq, "%08X %08X %-3hd",
2419 (__force u32) mfc->mfc_mcastgrp,
2420 (__force u32) mfc->mfc_origin,
2421 mfc->mfc_parent);
2423 if (it->cache != &mrt->mfc_unres_queue) {
2424 seq_printf(seq, " %8lu %8lu %8lu",
2425 mfc->mfc_un.res.pkt,
2426 mfc->mfc_un.res.bytes,
2427 mfc->mfc_un.res.wrong_if);
2428 for (n = mfc->mfc_un.res.minvif;
2429 n < mfc->mfc_un.res.maxvif; n++) {
2430 if (VIF_EXISTS(mrt, n) &&
2431 mfc->mfc_un.res.ttls[n] < 255)
2432 seq_printf(seq,
2433 " %2d:%-3d",
2434 n, mfc->mfc_un.res.ttls[n]);
2436 } else {
2437 /* unresolved mfc_caches don't contain
2438 * pkt, bytes and wrong_if values
2440 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2442 seq_putc(seq, '\n');
2444 return 0;
2447 static const struct seq_operations ipmr_mfc_seq_ops = {
2448 .start = ipmr_mfc_seq_start,
2449 .next = ipmr_mfc_seq_next,
2450 .stop = ipmr_mfc_seq_stop,
2451 .show = ipmr_mfc_seq_show,
2454 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2456 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2457 sizeof(struct ipmr_mfc_iter));
2460 static const struct file_operations ipmr_mfc_fops = {
2461 .owner = THIS_MODULE,
2462 .open = ipmr_mfc_open,
2463 .read = seq_read,
2464 .llseek = seq_lseek,
2465 .release = seq_release_net,
2467 #endif
2469 #ifdef CONFIG_IP_PIMSM_V2
2470 static const struct net_protocol pim_protocol = {
2471 .handler = pim_rcv,
2472 .netns_ok = 1,
2474 #endif
2478 * Setup for IP multicast routing
2480 static int __net_init ipmr_net_init(struct net *net)
2482 int err;
2484 err = ipmr_rules_init(net);
2485 if (err < 0)
2486 goto fail;
2488 #ifdef CONFIG_PROC_FS
2489 err = -ENOMEM;
2490 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2491 goto proc_vif_fail;
2492 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2493 goto proc_cache_fail;
2494 #endif
2495 return 0;
2497 #ifdef CONFIG_PROC_FS
2498 proc_cache_fail:
2499 proc_net_remove(net, "ip_mr_vif");
2500 proc_vif_fail:
2501 ipmr_rules_exit(net);
2502 #endif
2503 fail:
2504 return err;
2507 static void __net_exit ipmr_net_exit(struct net *net)
2509 #ifdef CONFIG_PROC_FS
2510 proc_net_remove(net, "ip_mr_cache");
2511 proc_net_remove(net, "ip_mr_vif");
2512 #endif
2513 ipmr_rules_exit(net);
2516 static struct pernet_operations ipmr_net_ops = {
2517 .init = ipmr_net_init,
2518 .exit = ipmr_net_exit,
2521 int __init ip_mr_init(void)
2523 int err;
2525 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2526 sizeof(struct mfc_cache),
2527 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2528 NULL);
2529 if (!mrt_cachep)
2530 return -ENOMEM;
2532 err = register_pernet_subsys(&ipmr_net_ops);
2533 if (err)
2534 goto reg_pernet_fail;
2536 err = register_netdevice_notifier(&ip_mr_notifier);
2537 if (err)
2538 goto reg_notif_fail;
2539 #ifdef CONFIG_IP_PIMSM_V2
2540 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2541 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2542 err = -EAGAIN;
2543 goto add_proto_fail;
2545 #endif
2546 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2547 NULL, ipmr_rtm_dumproute, NULL);
2548 return 0;
2550 #ifdef CONFIG_IP_PIMSM_V2
2551 add_proto_fail:
2552 unregister_netdevice_notifier(&ip_mr_notifier);
2553 #endif
2554 reg_notif_fail:
2555 unregister_pernet_subsys(&ipmr_net_ops);
2556 reg_pernet_fail:
2557 kmem_cache_destroy(mrt_cachep);
2558 return err;