ipvs: timeout tables do not need GFP_ATOMIC allocation
[linux/fpc-iii.git] / net / ipv4 / ipmr.c
blob5bef604ac0fad82714f5dbf64ed3d1a56993e864
1 /*
2 * IP multicast routing support for mrouted 3.6/3.8
4 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
5 * Linux Consultancy and Custom Driver Development
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
25 * Relax this requirement to work with older peers.
29 #include <asm/uaccess.h>
30 #include <linux/types.h>
31 #include <linux/capability.h>
32 #include <linux/errno.h>
33 #include <linux/timer.h>
34 #include <linux/mm.h>
35 #include <linux/kernel.h>
36 #include <linux/fcntl.h>
37 #include <linux/stat.h>
38 #include <linux/socket.h>
39 #include <linux/in.h>
40 #include <linux/inet.h>
41 #include <linux/netdevice.h>
42 #include <linux/inetdevice.h>
43 #include <linux/igmp.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <linux/mroute.h>
47 #include <linux/init.h>
48 #include <linux/if_ether.h>
49 #include <linux/slab.h>
50 #include <net/net_namespace.h>
51 #include <net/ip.h>
52 #include <net/protocol.h>
53 #include <linux/skbuff.h>
54 #include <net/route.h>
55 #include <net/sock.h>
56 #include <net/icmp.h>
57 #include <net/udp.h>
58 #include <net/raw.h>
59 #include <linux/notifier.h>
60 #include <linux/if_arp.h>
61 #include <linux/netfilter_ipv4.h>
62 #include <linux/compat.h>
63 #include <linux/export.h>
64 #include <net/ipip.h>
65 #include <net/checksum.h>
66 #include <net/netlink.h>
67 #include <net/fib_rules.h>
69 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
70 #define CONFIG_IP_PIMSM 1
71 #endif
73 struct mr_table {
74 struct list_head list;
75 #ifdef CONFIG_NET_NS
76 struct net *net;
77 #endif
78 u32 id;
79 struct sock __rcu *mroute_sk;
80 struct timer_list ipmr_expire_timer;
81 struct list_head mfc_unres_queue;
82 struct list_head mfc_cache_array[MFC_LINES];
83 struct vif_device vif_table[MAXVIFS];
84 int maxvif;
85 atomic_t cache_resolve_queue_len;
86 int mroute_do_assert;
87 int mroute_do_pim;
88 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
89 int mroute_reg_vif_num;
90 #endif
93 struct ipmr_rule {
94 struct fib_rule common;
97 struct ipmr_result {
98 struct mr_table *mrt;
101 /* Big lock, protecting vif table, mrt cache and mroute socket state.
102 * Note that the changes are semaphored via rtnl_lock.
105 static DEFINE_RWLOCK(mrt_lock);
108 * Multicast router control variables
111 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
113 /* Special spinlock for queue of unresolved entries */
114 static DEFINE_SPINLOCK(mfc_unres_lock);
116 /* We return to original Alan's scheme. Hash table of resolved
117 * entries is changed only in process context and protected
118 * with weak lock mrt_lock. Queue of unresolved entries is protected
119 * with strong spinlock mfc_unres_lock.
121 * In this case data path is free of exclusive locks at all.
124 static struct kmem_cache *mrt_cachep __read_mostly;
126 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
127 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
128 struct sk_buff *skb, struct mfc_cache *cache,
129 int local);
130 static int ipmr_cache_report(struct mr_table *mrt,
131 struct sk_buff *pkt, vifi_t vifi, int assert);
132 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
133 struct mfc_cache *c, struct rtmsg *rtm);
134 static void ipmr_expire_process(unsigned long arg);
136 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
137 #define ipmr_for_each_table(mrt, net) \
138 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
140 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
142 struct mr_table *mrt;
144 ipmr_for_each_table(mrt, net) {
145 if (mrt->id == id)
146 return mrt;
148 return NULL;
151 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
152 struct mr_table **mrt)
154 struct ipmr_result res;
155 struct fib_lookup_arg arg = { .result = &res, };
156 int err;
158 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
159 flowi4_to_flowi(flp4), 0, &arg);
160 if (err < 0)
161 return err;
162 *mrt = res.mrt;
163 return 0;
166 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
167 int flags, struct fib_lookup_arg *arg)
169 struct ipmr_result *res = arg->result;
170 struct mr_table *mrt;
172 switch (rule->action) {
173 case FR_ACT_TO_TBL:
174 break;
175 case FR_ACT_UNREACHABLE:
176 return -ENETUNREACH;
177 case FR_ACT_PROHIBIT:
178 return -EACCES;
179 case FR_ACT_BLACKHOLE:
180 default:
181 return -EINVAL;
184 mrt = ipmr_get_table(rule->fr_net, rule->table);
185 if (mrt == NULL)
186 return -EAGAIN;
187 res->mrt = mrt;
188 return 0;
191 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
193 return 1;
196 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
197 FRA_GENERIC_POLICY,
200 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
201 struct fib_rule_hdr *frh, struct nlattr **tb)
203 return 0;
206 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
207 struct nlattr **tb)
209 return 1;
212 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
213 struct fib_rule_hdr *frh)
215 frh->dst_len = 0;
216 frh->src_len = 0;
217 frh->tos = 0;
218 return 0;
221 static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
222 .family = RTNL_FAMILY_IPMR,
223 .rule_size = sizeof(struct ipmr_rule),
224 .addr_size = sizeof(u32),
225 .action = ipmr_rule_action,
226 .match = ipmr_rule_match,
227 .configure = ipmr_rule_configure,
228 .compare = ipmr_rule_compare,
229 .default_pref = fib_default_rule_pref,
230 .fill = ipmr_rule_fill,
231 .nlgroup = RTNLGRP_IPV4_RULE,
232 .policy = ipmr_rule_policy,
233 .owner = THIS_MODULE,
236 static int __net_init ipmr_rules_init(struct net *net)
238 struct fib_rules_ops *ops;
239 struct mr_table *mrt;
240 int err;
242 ops = fib_rules_register(&ipmr_rules_ops_template, net);
243 if (IS_ERR(ops))
244 return PTR_ERR(ops);
246 INIT_LIST_HEAD(&net->ipv4.mr_tables);
248 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
249 if (mrt == NULL) {
250 err = -ENOMEM;
251 goto err1;
254 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
255 if (err < 0)
256 goto err2;
258 net->ipv4.mr_rules_ops = ops;
259 return 0;
261 err2:
262 kfree(mrt);
263 err1:
264 fib_rules_unregister(ops);
265 return err;
268 static void __net_exit ipmr_rules_exit(struct net *net)
270 struct mr_table *mrt, *next;
272 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
273 list_del(&mrt->list);
274 kfree(mrt);
276 fib_rules_unregister(net->ipv4.mr_rules_ops);
278 #else
279 #define ipmr_for_each_table(mrt, net) \
280 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
282 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
284 return net->ipv4.mrt;
287 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
288 struct mr_table **mrt)
290 *mrt = net->ipv4.mrt;
291 return 0;
294 static int __net_init ipmr_rules_init(struct net *net)
296 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
297 return net->ipv4.mrt ? 0 : -ENOMEM;
300 static void __net_exit ipmr_rules_exit(struct net *net)
302 kfree(net->ipv4.mrt);
304 #endif
306 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
308 struct mr_table *mrt;
309 unsigned int i;
311 mrt = ipmr_get_table(net, id);
312 if (mrt != NULL)
313 return mrt;
315 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
316 if (mrt == NULL)
317 return NULL;
318 write_pnet(&mrt->net, net);
319 mrt->id = id;
321 /* Forwarding cache */
322 for (i = 0; i < MFC_LINES; i++)
323 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
325 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
327 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
328 (unsigned long)mrt);
330 #ifdef CONFIG_IP_PIMSM
331 mrt->mroute_reg_vif_num = -1;
332 #endif
333 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
334 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
335 #endif
336 return mrt;
339 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
341 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
343 struct net *net = dev_net(dev);
345 dev_close(dev);
347 dev = __dev_get_by_name(net, "tunl0");
348 if (dev) {
349 const struct net_device_ops *ops = dev->netdev_ops;
350 struct ifreq ifr;
351 struct ip_tunnel_parm p;
353 memset(&p, 0, sizeof(p));
354 p.iph.daddr = v->vifc_rmt_addr.s_addr;
355 p.iph.saddr = v->vifc_lcl_addr.s_addr;
356 p.iph.version = 4;
357 p.iph.ihl = 5;
358 p.iph.protocol = IPPROTO_IPIP;
359 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
360 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
362 if (ops->ndo_do_ioctl) {
363 mm_segment_t oldfs = get_fs();
365 set_fs(KERNEL_DS);
366 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
367 set_fs(oldfs);
372 static
373 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
375 struct net_device *dev;
377 dev = __dev_get_by_name(net, "tunl0");
379 if (dev) {
380 const struct net_device_ops *ops = dev->netdev_ops;
381 int err;
382 struct ifreq ifr;
383 struct ip_tunnel_parm p;
384 struct in_device *in_dev;
386 memset(&p, 0, sizeof(p));
387 p.iph.daddr = v->vifc_rmt_addr.s_addr;
388 p.iph.saddr = v->vifc_lcl_addr.s_addr;
389 p.iph.version = 4;
390 p.iph.ihl = 5;
391 p.iph.protocol = IPPROTO_IPIP;
392 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
393 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
395 if (ops->ndo_do_ioctl) {
396 mm_segment_t oldfs = get_fs();
398 set_fs(KERNEL_DS);
399 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
400 set_fs(oldfs);
401 } else {
402 err = -EOPNOTSUPP;
404 dev = NULL;
406 if (err == 0 &&
407 (dev = __dev_get_by_name(net, p.name)) != NULL) {
408 dev->flags |= IFF_MULTICAST;
410 in_dev = __in_dev_get_rtnl(dev);
411 if (in_dev == NULL)
412 goto failure;
414 ipv4_devconf_setall(in_dev);
415 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
417 if (dev_open(dev))
418 goto failure;
419 dev_hold(dev);
422 return dev;
424 failure:
425 /* allow the register to be completed before unregistering. */
426 rtnl_unlock();
427 rtnl_lock();
429 unregister_netdevice(dev);
430 return NULL;
433 #ifdef CONFIG_IP_PIMSM
435 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
437 struct net *net = dev_net(dev);
438 struct mr_table *mrt;
439 struct flowi4 fl4 = {
440 .flowi4_oif = dev->ifindex,
441 .flowi4_iif = skb->skb_iif,
442 .flowi4_mark = skb->mark,
444 int err;
446 err = ipmr_fib_lookup(net, &fl4, &mrt);
447 if (err < 0) {
448 kfree_skb(skb);
449 return err;
452 read_lock(&mrt_lock);
453 dev->stats.tx_bytes += skb->len;
454 dev->stats.tx_packets++;
455 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
456 read_unlock(&mrt_lock);
457 kfree_skb(skb);
458 return NETDEV_TX_OK;
461 static const struct net_device_ops reg_vif_netdev_ops = {
462 .ndo_start_xmit = reg_vif_xmit,
465 static void reg_vif_setup(struct net_device *dev)
467 dev->type = ARPHRD_PIMREG;
468 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
469 dev->flags = IFF_NOARP;
470 dev->netdev_ops = &reg_vif_netdev_ops,
471 dev->destructor = free_netdev;
472 dev->features |= NETIF_F_NETNS_LOCAL;
475 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
477 struct net_device *dev;
478 struct in_device *in_dev;
479 char name[IFNAMSIZ];
481 if (mrt->id == RT_TABLE_DEFAULT)
482 sprintf(name, "pimreg");
483 else
484 sprintf(name, "pimreg%u", mrt->id);
486 dev = alloc_netdev(0, name, reg_vif_setup);
488 if (dev == NULL)
489 return NULL;
491 dev_net_set(dev, net);
493 if (register_netdevice(dev)) {
494 free_netdev(dev);
495 return NULL;
497 dev->iflink = 0;
499 rcu_read_lock();
500 in_dev = __in_dev_get_rcu(dev);
501 if (!in_dev) {
502 rcu_read_unlock();
503 goto failure;
506 ipv4_devconf_setall(in_dev);
507 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
508 rcu_read_unlock();
510 if (dev_open(dev))
511 goto failure;
513 dev_hold(dev);
515 return dev;
517 failure:
518 /* allow the register to be completed before unregistering. */
519 rtnl_unlock();
520 rtnl_lock();
522 unregister_netdevice(dev);
523 return NULL;
525 #endif
528 * Delete a VIF entry
529 * @notify: Set to 1, if the caller is a notifier_call
532 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
533 struct list_head *head)
535 struct vif_device *v;
536 struct net_device *dev;
537 struct in_device *in_dev;
539 if (vifi < 0 || vifi >= mrt->maxvif)
540 return -EADDRNOTAVAIL;
542 v = &mrt->vif_table[vifi];
544 write_lock_bh(&mrt_lock);
545 dev = v->dev;
546 v->dev = NULL;
548 if (!dev) {
549 write_unlock_bh(&mrt_lock);
550 return -EADDRNOTAVAIL;
553 #ifdef CONFIG_IP_PIMSM
554 if (vifi == mrt->mroute_reg_vif_num)
555 mrt->mroute_reg_vif_num = -1;
556 #endif
558 if (vifi + 1 == mrt->maxvif) {
559 int tmp;
561 for (tmp = vifi - 1; tmp >= 0; tmp--) {
562 if (VIF_EXISTS(mrt, tmp))
563 break;
565 mrt->maxvif = tmp+1;
568 write_unlock_bh(&mrt_lock);
570 dev_set_allmulti(dev, -1);
572 in_dev = __in_dev_get_rtnl(dev);
573 if (in_dev) {
574 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
575 ip_rt_multicast_event(in_dev);
578 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
579 unregister_netdevice_queue(dev, head);
581 dev_put(dev);
582 return 0;
585 static void ipmr_cache_free_rcu(struct rcu_head *head)
587 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
589 kmem_cache_free(mrt_cachep, c);
592 static inline void ipmr_cache_free(struct mfc_cache *c)
594 call_rcu(&c->rcu, ipmr_cache_free_rcu);
597 /* Destroy an unresolved cache entry, killing queued skbs
598 * and reporting error to netlink readers.
601 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
603 struct net *net = read_pnet(&mrt->net);
604 struct sk_buff *skb;
605 struct nlmsgerr *e;
607 atomic_dec(&mrt->cache_resolve_queue_len);
609 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
610 if (ip_hdr(skb)->version == 0) {
611 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
612 nlh->nlmsg_type = NLMSG_ERROR;
613 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
614 skb_trim(skb, nlh->nlmsg_len);
615 e = NLMSG_DATA(nlh);
616 e->error = -ETIMEDOUT;
617 memset(&e->msg, 0, sizeof(e->msg));
619 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
620 } else {
621 kfree_skb(skb);
625 ipmr_cache_free(c);
629 /* Timer process for the unresolved queue. */
631 static void ipmr_expire_process(unsigned long arg)
633 struct mr_table *mrt = (struct mr_table *)arg;
634 unsigned long now;
635 unsigned long expires;
636 struct mfc_cache *c, *next;
638 if (!spin_trylock(&mfc_unres_lock)) {
639 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
640 return;
643 if (list_empty(&mrt->mfc_unres_queue))
644 goto out;
646 now = jiffies;
647 expires = 10*HZ;
649 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
650 if (time_after(c->mfc_un.unres.expires, now)) {
651 unsigned long interval = c->mfc_un.unres.expires - now;
652 if (interval < expires)
653 expires = interval;
654 continue;
657 list_del(&c->list);
658 ipmr_destroy_unres(mrt, c);
661 if (!list_empty(&mrt->mfc_unres_queue))
662 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
664 out:
665 spin_unlock(&mfc_unres_lock);
668 /* Fill oifs list. It is called under write locked mrt_lock. */
670 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
671 unsigned char *ttls)
673 int vifi;
675 cache->mfc_un.res.minvif = MAXVIFS;
676 cache->mfc_un.res.maxvif = 0;
677 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
679 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
680 if (VIF_EXISTS(mrt, vifi) &&
681 ttls[vifi] && ttls[vifi] < 255) {
682 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
683 if (cache->mfc_un.res.minvif > vifi)
684 cache->mfc_un.res.minvif = vifi;
685 if (cache->mfc_un.res.maxvif <= vifi)
686 cache->mfc_un.res.maxvif = vifi + 1;
691 static int vif_add(struct net *net, struct mr_table *mrt,
692 struct vifctl *vifc, int mrtsock)
694 int vifi = vifc->vifc_vifi;
695 struct vif_device *v = &mrt->vif_table[vifi];
696 struct net_device *dev;
697 struct in_device *in_dev;
698 int err;
700 /* Is vif busy ? */
701 if (VIF_EXISTS(mrt, vifi))
702 return -EADDRINUSE;
704 switch (vifc->vifc_flags) {
705 #ifdef CONFIG_IP_PIMSM
706 case VIFF_REGISTER:
708 * Special Purpose VIF in PIM
709 * All the packets will be sent to the daemon
711 if (mrt->mroute_reg_vif_num >= 0)
712 return -EADDRINUSE;
713 dev = ipmr_reg_vif(net, mrt);
714 if (!dev)
715 return -ENOBUFS;
716 err = dev_set_allmulti(dev, 1);
717 if (err) {
718 unregister_netdevice(dev);
719 dev_put(dev);
720 return err;
722 break;
723 #endif
724 case VIFF_TUNNEL:
725 dev = ipmr_new_tunnel(net, vifc);
726 if (!dev)
727 return -ENOBUFS;
728 err = dev_set_allmulti(dev, 1);
729 if (err) {
730 ipmr_del_tunnel(dev, vifc);
731 dev_put(dev);
732 return err;
734 break;
736 case VIFF_USE_IFINDEX:
737 case 0:
738 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
739 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
740 if (dev && __in_dev_get_rtnl(dev) == NULL) {
741 dev_put(dev);
742 return -EADDRNOTAVAIL;
744 } else {
745 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
747 if (!dev)
748 return -EADDRNOTAVAIL;
749 err = dev_set_allmulti(dev, 1);
750 if (err) {
751 dev_put(dev);
752 return err;
754 break;
755 default:
756 return -EINVAL;
759 in_dev = __in_dev_get_rtnl(dev);
760 if (!in_dev) {
761 dev_put(dev);
762 return -EADDRNOTAVAIL;
764 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
765 ip_rt_multicast_event(in_dev);
767 /* Fill in the VIF structures */
769 v->rate_limit = vifc->vifc_rate_limit;
770 v->local = vifc->vifc_lcl_addr.s_addr;
771 v->remote = vifc->vifc_rmt_addr.s_addr;
772 v->flags = vifc->vifc_flags;
773 if (!mrtsock)
774 v->flags |= VIFF_STATIC;
775 v->threshold = vifc->vifc_threshold;
776 v->bytes_in = 0;
777 v->bytes_out = 0;
778 v->pkt_in = 0;
779 v->pkt_out = 0;
780 v->link = dev->ifindex;
781 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
782 v->link = dev->iflink;
784 /* And finish update writing critical data */
785 write_lock_bh(&mrt_lock);
786 v->dev = dev;
787 #ifdef CONFIG_IP_PIMSM
788 if (v->flags & VIFF_REGISTER)
789 mrt->mroute_reg_vif_num = vifi;
790 #endif
791 if (vifi+1 > mrt->maxvif)
792 mrt->maxvif = vifi+1;
793 write_unlock_bh(&mrt_lock);
794 return 0;
797 /* called with rcu_read_lock() */
798 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
799 __be32 origin,
800 __be32 mcastgrp)
802 int line = MFC_HASH(mcastgrp, origin);
803 struct mfc_cache *c;
805 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
806 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
807 return c;
809 return NULL;
813 * Allocate a multicast cache entry
815 static struct mfc_cache *ipmr_cache_alloc(void)
817 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
819 if (c)
820 c->mfc_un.res.minvif = MAXVIFS;
821 return c;
824 static struct mfc_cache *ipmr_cache_alloc_unres(void)
826 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
828 if (c) {
829 skb_queue_head_init(&c->mfc_un.unres.unresolved);
830 c->mfc_un.unres.expires = jiffies + 10*HZ;
832 return c;
836 * A cache entry has gone into a resolved state from queued
839 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
840 struct mfc_cache *uc, struct mfc_cache *c)
842 struct sk_buff *skb;
843 struct nlmsgerr *e;
845 /* Play the pending entries through our router */
847 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
848 if (ip_hdr(skb)->version == 0) {
849 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
851 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
852 nlh->nlmsg_len = skb_tail_pointer(skb) -
853 (u8 *)nlh;
854 } else {
855 nlh->nlmsg_type = NLMSG_ERROR;
856 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
857 skb_trim(skb, nlh->nlmsg_len);
858 e = NLMSG_DATA(nlh);
859 e->error = -EMSGSIZE;
860 memset(&e->msg, 0, sizeof(e->msg));
863 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
864 } else {
865 ip_mr_forward(net, mrt, skb, c, 0);
871 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
872 * expects the following bizarre scheme.
874 * Called under mrt_lock.
877 static int ipmr_cache_report(struct mr_table *mrt,
878 struct sk_buff *pkt, vifi_t vifi, int assert)
880 struct sk_buff *skb;
881 const int ihl = ip_hdrlen(pkt);
882 struct igmphdr *igmp;
883 struct igmpmsg *msg;
884 struct sock *mroute_sk;
885 int ret;
887 #ifdef CONFIG_IP_PIMSM
888 if (assert == IGMPMSG_WHOLEPKT)
889 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
890 else
891 #endif
892 skb = alloc_skb(128, GFP_ATOMIC);
894 if (!skb)
895 return -ENOBUFS;
897 #ifdef CONFIG_IP_PIMSM
898 if (assert == IGMPMSG_WHOLEPKT) {
899 /* Ugly, but we have no choice with this interface.
900 * Duplicate old header, fix ihl, length etc.
901 * And all this only to mangle msg->im_msgtype and
902 * to set msg->im_mbz to "mbz" :-)
904 skb_push(skb, sizeof(struct iphdr));
905 skb_reset_network_header(skb);
906 skb_reset_transport_header(skb);
907 msg = (struct igmpmsg *)skb_network_header(skb);
908 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
909 msg->im_msgtype = IGMPMSG_WHOLEPKT;
910 msg->im_mbz = 0;
911 msg->im_vif = mrt->mroute_reg_vif_num;
912 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
913 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
914 sizeof(struct iphdr));
915 } else
916 #endif
919 /* Copy the IP header */
921 skb->network_header = skb->tail;
922 skb_put(skb, ihl);
923 skb_copy_to_linear_data(skb, pkt->data, ihl);
924 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
925 msg = (struct igmpmsg *)skb_network_header(skb);
926 msg->im_vif = vifi;
927 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
929 /* Add our header */
931 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
932 igmp->type =
933 msg->im_msgtype = assert;
934 igmp->code = 0;
935 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
936 skb->transport_header = skb->network_header;
939 rcu_read_lock();
940 mroute_sk = rcu_dereference(mrt->mroute_sk);
941 if (mroute_sk == NULL) {
942 rcu_read_unlock();
943 kfree_skb(skb);
944 return -EINVAL;
947 /* Deliver to mrouted */
949 ret = sock_queue_rcv_skb(mroute_sk, skb);
950 rcu_read_unlock();
951 if (ret < 0) {
952 if (net_ratelimit())
953 pr_warn("mroute: pending queue full, dropping entries\n");
954 kfree_skb(skb);
957 return ret;
961 * Queue a packet for resolution. It gets locked cache entry!
964 static int
965 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
967 bool found = false;
968 int err;
969 struct mfc_cache *c;
970 const struct iphdr *iph = ip_hdr(skb);
972 spin_lock_bh(&mfc_unres_lock);
973 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
974 if (c->mfc_mcastgrp == iph->daddr &&
975 c->mfc_origin == iph->saddr) {
976 found = true;
977 break;
981 if (!found) {
982 /* Create a new entry if allowable */
984 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
985 (c = ipmr_cache_alloc_unres()) == NULL) {
986 spin_unlock_bh(&mfc_unres_lock);
988 kfree_skb(skb);
989 return -ENOBUFS;
992 /* Fill in the new cache entry */
994 c->mfc_parent = -1;
995 c->mfc_origin = iph->saddr;
996 c->mfc_mcastgrp = iph->daddr;
998 /* Reflect first query at mrouted. */
1000 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1001 if (err < 0) {
1002 /* If the report failed throw the cache entry
1003 out - Brad Parker
1005 spin_unlock_bh(&mfc_unres_lock);
1007 ipmr_cache_free(c);
1008 kfree_skb(skb);
1009 return err;
1012 atomic_inc(&mrt->cache_resolve_queue_len);
1013 list_add(&c->list, &mrt->mfc_unres_queue);
1015 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1016 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1019 /* See if we can append the packet */
1021 if (c->mfc_un.unres.unresolved.qlen > 3) {
1022 kfree_skb(skb);
1023 err = -ENOBUFS;
1024 } else {
1025 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1026 err = 0;
1029 spin_unlock_bh(&mfc_unres_lock);
1030 return err;
1034 * MFC cache manipulation by user space mroute daemon
1037 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1039 int line;
1040 struct mfc_cache *c, *next;
1042 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1044 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1045 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1046 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1047 list_del_rcu(&c->list);
1049 ipmr_cache_free(c);
1050 return 0;
1053 return -ENOENT;
1056 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1057 struct mfcctl *mfc, int mrtsock)
1059 bool found = false;
1060 int line;
1061 struct mfc_cache *uc, *c;
1063 if (mfc->mfcc_parent >= MAXVIFS)
1064 return -ENFILE;
1066 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1068 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1069 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1070 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1071 found = true;
1072 break;
1076 if (found) {
1077 write_lock_bh(&mrt_lock);
1078 c->mfc_parent = mfc->mfcc_parent;
1079 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1080 if (!mrtsock)
1081 c->mfc_flags |= MFC_STATIC;
1082 write_unlock_bh(&mrt_lock);
1083 return 0;
1086 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1087 return -EINVAL;
1089 c = ipmr_cache_alloc();
1090 if (c == NULL)
1091 return -ENOMEM;
1093 c->mfc_origin = mfc->mfcc_origin.s_addr;
1094 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1095 c->mfc_parent = mfc->mfcc_parent;
1096 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1097 if (!mrtsock)
1098 c->mfc_flags |= MFC_STATIC;
1100 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1103 * Check to see if we resolved a queued list. If so we
1104 * need to send on the frames and tidy up.
1106 found = false;
1107 spin_lock_bh(&mfc_unres_lock);
1108 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1109 if (uc->mfc_origin == c->mfc_origin &&
1110 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1111 list_del(&uc->list);
1112 atomic_dec(&mrt->cache_resolve_queue_len);
1113 found = true;
1114 break;
1117 if (list_empty(&mrt->mfc_unres_queue))
1118 del_timer(&mrt->ipmr_expire_timer);
1119 spin_unlock_bh(&mfc_unres_lock);
1121 if (found) {
1122 ipmr_cache_resolve(net, mrt, uc, c);
1123 ipmr_cache_free(uc);
1125 return 0;
1129 * Close the multicast socket, and clear the vif tables etc
1132 static void mroute_clean_tables(struct mr_table *mrt)
1134 int i;
1135 LIST_HEAD(list);
1136 struct mfc_cache *c, *next;
1138 /* Shut down all active vif entries */
1140 for (i = 0; i < mrt->maxvif; i++) {
1141 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1142 vif_delete(mrt, i, 0, &list);
1144 unregister_netdevice_many(&list);
1146 /* Wipe the cache */
1148 for (i = 0; i < MFC_LINES; i++) {
1149 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1150 if (c->mfc_flags & MFC_STATIC)
1151 continue;
1152 list_del_rcu(&c->list);
1153 ipmr_cache_free(c);
1157 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1158 spin_lock_bh(&mfc_unres_lock);
1159 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1160 list_del(&c->list);
1161 ipmr_destroy_unres(mrt, c);
1163 spin_unlock_bh(&mfc_unres_lock);
1167 /* called from ip_ra_control(), before an RCU grace period,
1168 * we dont need to call synchronize_rcu() here
1170 static void mrtsock_destruct(struct sock *sk)
1172 struct net *net = sock_net(sk);
1173 struct mr_table *mrt;
1175 rtnl_lock();
1176 ipmr_for_each_table(mrt, net) {
1177 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1178 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1179 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1180 mroute_clean_tables(mrt);
1183 rtnl_unlock();
1187 * Socket options and virtual interface manipulation. The whole
1188 * virtual interface system is a complete heap, but unfortunately
1189 * that's how BSD mrouted happens to think. Maybe one day with a proper
1190 * MOSPF/PIM router set up we can clean this up.
1193 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1195 int ret;
1196 struct vifctl vif;
1197 struct mfcctl mfc;
1198 struct net *net = sock_net(sk);
1199 struct mr_table *mrt;
1201 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1202 if (mrt == NULL)
1203 return -ENOENT;
1205 if (optname != MRT_INIT) {
1206 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1207 !capable(CAP_NET_ADMIN))
1208 return -EACCES;
1211 switch (optname) {
1212 case MRT_INIT:
1213 if (sk->sk_type != SOCK_RAW ||
1214 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1215 return -EOPNOTSUPP;
1216 if (optlen != sizeof(int))
1217 return -ENOPROTOOPT;
1219 rtnl_lock();
1220 if (rtnl_dereference(mrt->mroute_sk)) {
1221 rtnl_unlock();
1222 return -EADDRINUSE;
1225 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1226 if (ret == 0) {
1227 rcu_assign_pointer(mrt->mroute_sk, sk);
1228 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1230 rtnl_unlock();
1231 return ret;
1232 case MRT_DONE:
1233 if (sk != rcu_access_pointer(mrt->mroute_sk))
1234 return -EACCES;
1235 return ip_ra_control(sk, 0, NULL);
1236 case MRT_ADD_VIF:
1237 case MRT_DEL_VIF:
1238 if (optlen != sizeof(vif))
1239 return -EINVAL;
1240 if (copy_from_user(&vif, optval, sizeof(vif)))
1241 return -EFAULT;
1242 if (vif.vifc_vifi >= MAXVIFS)
1243 return -ENFILE;
1244 rtnl_lock();
1245 if (optname == MRT_ADD_VIF) {
1246 ret = vif_add(net, mrt, &vif,
1247 sk == rtnl_dereference(mrt->mroute_sk));
1248 } else {
1249 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1251 rtnl_unlock();
1252 return ret;
1255 * Manipulate the forwarding caches. These live
1256 * in a sort of kernel/user symbiosis.
1258 case MRT_ADD_MFC:
1259 case MRT_DEL_MFC:
1260 if (optlen != sizeof(mfc))
1261 return -EINVAL;
1262 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1263 return -EFAULT;
1264 rtnl_lock();
1265 if (optname == MRT_DEL_MFC)
1266 ret = ipmr_mfc_delete(mrt, &mfc);
1267 else
1268 ret = ipmr_mfc_add(net, mrt, &mfc,
1269 sk == rtnl_dereference(mrt->mroute_sk));
1270 rtnl_unlock();
1271 return ret;
1273 * Control PIM assert.
1275 case MRT_ASSERT:
1277 int v;
1278 if (get_user(v, (int __user *)optval))
1279 return -EFAULT;
1280 mrt->mroute_do_assert = (v) ? 1 : 0;
1281 return 0;
1283 #ifdef CONFIG_IP_PIMSM
1284 case MRT_PIM:
1286 int v;
1288 if (get_user(v, (int __user *)optval))
1289 return -EFAULT;
1290 v = (v) ? 1 : 0;
1292 rtnl_lock();
1293 ret = 0;
1294 if (v != mrt->mroute_do_pim) {
1295 mrt->mroute_do_pim = v;
1296 mrt->mroute_do_assert = v;
1298 rtnl_unlock();
1299 return ret;
1301 #endif
1302 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1303 case MRT_TABLE:
1305 u32 v;
1307 if (optlen != sizeof(u32))
1308 return -EINVAL;
1309 if (get_user(v, (u32 __user *)optval))
1310 return -EFAULT;
1312 rtnl_lock();
1313 ret = 0;
1314 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1315 ret = -EBUSY;
1316 } else {
1317 if (!ipmr_new_table(net, v))
1318 ret = -ENOMEM;
1319 raw_sk(sk)->ipmr_table = v;
1321 rtnl_unlock();
1322 return ret;
1324 #endif
1326 * Spurious command, or MRT_VERSION which you cannot
1327 * set.
1329 default:
1330 return -ENOPROTOOPT;
1335 * Getsock opt support for the multicast routing system.
1338 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1340 int olr;
1341 int val;
1342 struct net *net = sock_net(sk);
1343 struct mr_table *mrt;
1345 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1346 if (mrt == NULL)
1347 return -ENOENT;
1349 if (optname != MRT_VERSION &&
1350 #ifdef CONFIG_IP_PIMSM
1351 optname != MRT_PIM &&
1352 #endif
1353 optname != MRT_ASSERT)
1354 return -ENOPROTOOPT;
1356 if (get_user(olr, optlen))
1357 return -EFAULT;
1359 olr = min_t(unsigned int, olr, sizeof(int));
1360 if (olr < 0)
1361 return -EINVAL;
1363 if (put_user(olr, optlen))
1364 return -EFAULT;
1365 if (optname == MRT_VERSION)
1366 val = 0x0305;
1367 #ifdef CONFIG_IP_PIMSM
1368 else if (optname == MRT_PIM)
1369 val = mrt->mroute_do_pim;
1370 #endif
1371 else
1372 val = mrt->mroute_do_assert;
1373 if (copy_to_user(optval, &val, olr))
1374 return -EFAULT;
1375 return 0;
1379 * The IP multicast ioctl support routines.
1382 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1384 struct sioc_sg_req sr;
1385 struct sioc_vif_req vr;
1386 struct vif_device *vif;
1387 struct mfc_cache *c;
1388 struct net *net = sock_net(sk);
1389 struct mr_table *mrt;
1391 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1392 if (mrt == NULL)
1393 return -ENOENT;
1395 switch (cmd) {
1396 case SIOCGETVIFCNT:
1397 if (copy_from_user(&vr, arg, sizeof(vr)))
1398 return -EFAULT;
1399 if (vr.vifi >= mrt->maxvif)
1400 return -EINVAL;
1401 read_lock(&mrt_lock);
1402 vif = &mrt->vif_table[vr.vifi];
1403 if (VIF_EXISTS(mrt, vr.vifi)) {
1404 vr.icount = vif->pkt_in;
1405 vr.ocount = vif->pkt_out;
1406 vr.ibytes = vif->bytes_in;
1407 vr.obytes = vif->bytes_out;
1408 read_unlock(&mrt_lock);
1410 if (copy_to_user(arg, &vr, sizeof(vr)))
1411 return -EFAULT;
1412 return 0;
1414 read_unlock(&mrt_lock);
1415 return -EADDRNOTAVAIL;
1416 case SIOCGETSGCNT:
1417 if (copy_from_user(&sr, arg, sizeof(sr)))
1418 return -EFAULT;
1420 rcu_read_lock();
1421 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1422 if (c) {
1423 sr.pktcnt = c->mfc_un.res.pkt;
1424 sr.bytecnt = c->mfc_un.res.bytes;
1425 sr.wrong_if = c->mfc_un.res.wrong_if;
1426 rcu_read_unlock();
1428 if (copy_to_user(arg, &sr, sizeof(sr)))
1429 return -EFAULT;
1430 return 0;
1432 rcu_read_unlock();
1433 return -EADDRNOTAVAIL;
1434 default:
1435 return -ENOIOCTLCMD;
1439 #ifdef CONFIG_COMPAT
1440 struct compat_sioc_sg_req {
1441 struct in_addr src;
1442 struct in_addr grp;
1443 compat_ulong_t pktcnt;
1444 compat_ulong_t bytecnt;
1445 compat_ulong_t wrong_if;
1448 struct compat_sioc_vif_req {
1449 vifi_t vifi; /* Which iface */
1450 compat_ulong_t icount;
1451 compat_ulong_t ocount;
1452 compat_ulong_t ibytes;
1453 compat_ulong_t obytes;
1456 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1458 struct compat_sioc_sg_req sr;
1459 struct compat_sioc_vif_req vr;
1460 struct vif_device *vif;
1461 struct mfc_cache *c;
1462 struct net *net = sock_net(sk);
1463 struct mr_table *mrt;
1465 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1466 if (mrt == NULL)
1467 return -ENOENT;
1469 switch (cmd) {
1470 case SIOCGETVIFCNT:
1471 if (copy_from_user(&vr, arg, sizeof(vr)))
1472 return -EFAULT;
1473 if (vr.vifi >= mrt->maxvif)
1474 return -EINVAL;
1475 read_lock(&mrt_lock);
1476 vif = &mrt->vif_table[vr.vifi];
1477 if (VIF_EXISTS(mrt, vr.vifi)) {
1478 vr.icount = vif->pkt_in;
1479 vr.ocount = vif->pkt_out;
1480 vr.ibytes = vif->bytes_in;
1481 vr.obytes = vif->bytes_out;
1482 read_unlock(&mrt_lock);
1484 if (copy_to_user(arg, &vr, sizeof(vr)))
1485 return -EFAULT;
1486 return 0;
1488 read_unlock(&mrt_lock);
1489 return -EADDRNOTAVAIL;
1490 case SIOCGETSGCNT:
1491 if (copy_from_user(&sr, arg, sizeof(sr)))
1492 return -EFAULT;
1494 rcu_read_lock();
1495 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1496 if (c) {
1497 sr.pktcnt = c->mfc_un.res.pkt;
1498 sr.bytecnt = c->mfc_un.res.bytes;
1499 sr.wrong_if = c->mfc_un.res.wrong_if;
1500 rcu_read_unlock();
1502 if (copy_to_user(arg, &sr, sizeof(sr)))
1503 return -EFAULT;
1504 return 0;
1506 rcu_read_unlock();
1507 return -EADDRNOTAVAIL;
1508 default:
1509 return -ENOIOCTLCMD;
1512 #endif
1515 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1517 struct net_device *dev = ptr;
1518 struct net *net = dev_net(dev);
1519 struct mr_table *mrt;
1520 struct vif_device *v;
1521 int ct;
1523 if (event != NETDEV_UNREGISTER)
1524 return NOTIFY_DONE;
1526 ipmr_for_each_table(mrt, net) {
1527 v = &mrt->vif_table[0];
1528 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1529 if (v->dev == dev)
1530 vif_delete(mrt, ct, 1, NULL);
1533 return NOTIFY_DONE;
1537 static struct notifier_block ip_mr_notifier = {
1538 .notifier_call = ipmr_device_event,
1542 * Encapsulate a packet by attaching a valid IPIP header to it.
1543 * This avoids tunnel drivers and other mess and gives us the speed so
1544 * important for multicast video.
1547 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1549 struct iphdr *iph;
1550 const struct iphdr *old_iph = ip_hdr(skb);
1552 skb_push(skb, sizeof(struct iphdr));
1553 skb->transport_header = skb->network_header;
1554 skb_reset_network_header(skb);
1555 iph = ip_hdr(skb);
1557 iph->version = 4;
1558 iph->tos = old_iph->tos;
1559 iph->ttl = old_iph->ttl;
1560 iph->frag_off = 0;
1561 iph->daddr = daddr;
1562 iph->saddr = saddr;
1563 iph->protocol = IPPROTO_IPIP;
1564 iph->ihl = 5;
1565 iph->tot_len = htons(skb->len);
1566 ip_select_ident(iph, skb_dst(skb), NULL);
1567 ip_send_check(iph);
1569 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1570 nf_reset(skb);
1573 static inline int ipmr_forward_finish(struct sk_buff *skb)
1575 struct ip_options *opt = &(IPCB(skb)->opt);
1577 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1579 if (unlikely(opt->optlen))
1580 ip_forward_options(skb);
1582 return dst_output(skb);
1586 * Processing handlers for ipmr_forward
1589 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1590 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1592 const struct iphdr *iph = ip_hdr(skb);
1593 struct vif_device *vif = &mrt->vif_table[vifi];
1594 struct net_device *dev;
1595 struct rtable *rt;
1596 struct flowi4 fl4;
1597 int encap = 0;
1599 if (vif->dev == NULL)
1600 goto out_free;
1602 #ifdef CONFIG_IP_PIMSM
1603 if (vif->flags & VIFF_REGISTER) {
1604 vif->pkt_out++;
1605 vif->bytes_out += skb->len;
1606 vif->dev->stats.tx_bytes += skb->len;
1607 vif->dev->stats.tx_packets++;
1608 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1609 goto out_free;
1611 #endif
1613 if (vif->flags & VIFF_TUNNEL) {
1614 rt = ip_route_output_ports(net, &fl4, NULL,
1615 vif->remote, vif->local,
1616 0, 0,
1617 IPPROTO_IPIP,
1618 RT_TOS(iph->tos), vif->link);
1619 if (IS_ERR(rt))
1620 goto out_free;
1621 encap = sizeof(struct iphdr);
1622 } else {
1623 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1624 0, 0,
1625 IPPROTO_IPIP,
1626 RT_TOS(iph->tos), vif->link);
1627 if (IS_ERR(rt))
1628 goto out_free;
1631 dev = rt->dst.dev;
1633 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1634 /* Do not fragment multicasts. Alas, IPv4 does not
1635 * allow to send ICMP, so that packets will disappear
1636 * to blackhole.
1639 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1640 ip_rt_put(rt);
1641 goto out_free;
1644 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1646 if (skb_cow(skb, encap)) {
1647 ip_rt_put(rt);
1648 goto out_free;
1651 vif->pkt_out++;
1652 vif->bytes_out += skb->len;
1654 skb_dst_drop(skb);
1655 skb_dst_set(skb, &rt->dst);
1656 ip_decrease_ttl(ip_hdr(skb));
1658 /* FIXME: forward and output firewalls used to be called here.
1659 * What do we do with netfilter? -- RR
1661 if (vif->flags & VIFF_TUNNEL) {
1662 ip_encap(skb, vif->local, vif->remote);
1663 /* FIXME: extra output firewall step used to be here. --RR */
1664 vif->dev->stats.tx_packets++;
1665 vif->dev->stats.tx_bytes += skb->len;
1668 IPCB(skb)->flags |= IPSKB_FORWARDED;
1671 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1672 * not only before forwarding, but after forwarding on all output
1673 * interfaces. It is clear, if mrouter runs a multicasting
1674 * program, it should receive packets not depending to what interface
1675 * program is joined.
1676 * If we will not make it, the program will have to join on all
1677 * interfaces. On the other hand, multihoming host (or router, but
1678 * not mrouter) cannot join to more than one interface - it will
1679 * result in receiving multiple packets.
1681 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1682 ipmr_forward_finish);
1683 return;
1685 out_free:
1686 kfree_skb(skb);
1689 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1691 int ct;
1693 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1694 if (mrt->vif_table[ct].dev == dev)
1695 break;
1697 return ct;
1700 /* "local" means that we should preserve one skb (for local delivery) */
1702 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1703 struct sk_buff *skb, struct mfc_cache *cache,
1704 int local)
1706 int psend = -1;
1707 int vif, ct;
1709 vif = cache->mfc_parent;
1710 cache->mfc_un.res.pkt++;
1711 cache->mfc_un.res.bytes += skb->len;
1714 * Wrong interface: drop packet and (maybe) send PIM assert.
1716 if (mrt->vif_table[vif].dev != skb->dev) {
1717 int true_vifi;
1719 if (rt_is_output_route(skb_rtable(skb))) {
1720 /* It is our own packet, looped back.
1721 * Very complicated situation...
1723 * The best workaround until routing daemons will be
1724 * fixed is not to redistribute packet, if it was
1725 * send through wrong interface. It means, that
1726 * multicast applications WILL NOT work for
1727 * (S,G), which have default multicast route pointing
1728 * to wrong oif. In any case, it is not a good
1729 * idea to use multicasting applications on router.
1731 goto dont_forward;
1734 cache->mfc_un.res.wrong_if++;
1735 true_vifi = ipmr_find_vif(mrt, skb->dev);
1737 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1738 /* pimsm uses asserts, when switching from RPT to SPT,
1739 * so that we cannot check that packet arrived on an oif.
1740 * It is bad, but otherwise we would need to move pretty
1741 * large chunk of pimd to kernel. Ough... --ANK
1743 (mrt->mroute_do_pim ||
1744 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1745 time_after(jiffies,
1746 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1747 cache->mfc_un.res.last_assert = jiffies;
1748 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1750 goto dont_forward;
1753 mrt->vif_table[vif].pkt_in++;
1754 mrt->vif_table[vif].bytes_in += skb->len;
1757 * Forward the frame
1759 for (ct = cache->mfc_un.res.maxvif - 1;
1760 ct >= cache->mfc_un.res.minvif; ct--) {
1761 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1762 if (psend != -1) {
1763 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1765 if (skb2)
1766 ipmr_queue_xmit(net, mrt, skb2, cache,
1767 psend);
1769 psend = ct;
1772 if (psend != -1) {
1773 if (local) {
1774 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1776 if (skb2)
1777 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1778 } else {
1779 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1780 return 0;
1784 dont_forward:
1785 if (!local)
1786 kfree_skb(skb);
1787 return 0;
1790 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1792 struct rtable *rt = skb_rtable(skb);
1793 struct iphdr *iph = ip_hdr(skb);
1794 struct flowi4 fl4 = {
1795 .daddr = iph->daddr,
1796 .saddr = iph->saddr,
1797 .flowi4_tos = RT_TOS(iph->tos),
1798 .flowi4_oif = rt->rt_oif,
1799 .flowi4_iif = rt->rt_iif,
1800 .flowi4_mark = rt->rt_mark,
1802 struct mr_table *mrt;
1803 int err;
1805 err = ipmr_fib_lookup(net, &fl4, &mrt);
1806 if (err)
1807 return ERR_PTR(err);
1808 return mrt;
1812 * Multicast packets for forwarding arrive here
1813 * Called with rcu_read_lock();
1816 int ip_mr_input(struct sk_buff *skb)
1818 struct mfc_cache *cache;
1819 struct net *net = dev_net(skb->dev);
1820 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1821 struct mr_table *mrt;
1823 /* Packet is looped back after forward, it should not be
1824 * forwarded second time, but still can be delivered locally.
1826 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1827 goto dont_forward;
1829 mrt = ipmr_rt_fib_lookup(net, skb);
1830 if (IS_ERR(mrt)) {
1831 kfree_skb(skb);
1832 return PTR_ERR(mrt);
1834 if (!local) {
1835 if (IPCB(skb)->opt.router_alert) {
1836 if (ip_call_ra_chain(skb))
1837 return 0;
1838 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1839 /* IGMPv1 (and broken IGMPv2 implementations sort of
1840 * Cisco IOS <= 11.2(8)) do not put router alert
1841 * option to IGMP packets destined to routable
1842 * groups. It is very bad, because it means
1843 * that we can forward NO IGMP messages.
1845 struct sock *mroute_sk;
1847 mroute_sk = rcu_dereference(mrt->mroute_sk);
1848 if (mroute_sk) {
1849 nf_reset(skb);
1850 raw_rcv(mroute_sk, skb);
1851 return 0;
1856 /* already under rcu_read_lock() */
1857 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1860 * No usable cache entry
1862 if (cache == NULL) {
1863 int vif;
1865 if (local) {
1866 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1867 ip_local_deliver(skb);
1868 if (skb2 == NULL)
1869 return -ENOBUFS;
1870 skb = skb2;
1873 read_lock(&mrt_lock);
1874 vif = ipmr_find_vif(mrt, skb->dev);
1875 if (vif >= 0) {
1876 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1877 read_unlock(&mrt_lock);
1879 return err2;
1881 read_unlock(&mrt_lock);
1882 kfree_skb(skb);
1883 return -ENODEV;
1886 read_lock(&mrt_lock);
1887 ip_mr_forward(net, mrt, skb, cache, local);
1888 read_unlock(&mrt_lock);
1890 if (local)
1891 return ip_local_deliver(skb);
1893 return 0;
1895 dont_forward:
1896 if (local)
1897 return ip_local_deliver(skb);
1898 kfree_skb(skb);
1899 return 0;
1902 #ifdef CONFIG_IP_PIMSM
1903 /* called with rcu_read_lock() */
1904 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1905 unsigned int pimlen)
1907 struct net_device *reg_dev = NULL;
1908 struct iphdr *encap;
1910 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1912 * Check that:
1913 * a. packet is really sent to a multicast group
1914 * b. packet is not a NULL-REGISTER
1915 * c. packet is not truncated
1917 if (!ipv4_is_multicast(encap->daddr) ||
1918 encap->tot_len == 0 ||
1919 ntohs(encap->tot_len) + pimlen > skb->len)
1920 return 1;
1922 read_lock(&mrt_lock);
1923 if (mrt->mroute_reg_vif_num >= 0)
1924 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1925 read_unlock(&mrt_lock);
1927 if (reg_dev == NULL)
1928 return 1;
1930 skb->mac_header = skb->network_header;
1931 skb_pull(skb, (u8 *)encap - skb->data);
1932 skb_reset_network_header(skb);
1933 skb->protocol = htons(ETH_P_IP);
1934 skb->ip_summed = CHECKSUM_NONE;
1935 skb->pkt_type = PACKET_HOST;
1937 skb_tunnel_rx(skb, reg_dev);
1939 netif_rx(skb);
1941 return NET_RX_SUCCESS;
1943 #endif
1945 #ifdef CONFIG_IP_PIMSM_V1
1947 * Handle IGMP messages of PIMv1
1950 int pim_rcv_v1(struct sk_buff *skb)
1952 struct igmphdr *pim;
1953 struct net *net = dev_net(skb->dev);
1954 struct mr_table *mrt;
1956 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1957 goto drop;
1959 pim = igmp_hdr(skb);
1961 mrt = ipmr_rt_fib_lookup(net, skb);
1962 if (IS_ERR(mrt))
1963 goto drop;
1964 if (!mrt->mroute_do_pim ||
1965 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1966 goto drop;
1968 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1969 drop:
1970 kfree_skb(skb);
1972 return 0;
1974 #endif
1976 #ifdef CONFIG_IP_PIMSM_V2
1977 static int pim_rcv(struct sk_buff *skb)
1979 struct pimreghdr *pim;
1980 struct net *net = dev_net(skb->dev);
1981 struct mr_table *mrt;
1983 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1984 goto drop;
1986 pim = (struct pimreghdr *)skb_transport_header(skb);
1987 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1988 (pim->flags & PIM_NULL_REGISTER) ||
1989 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1990 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
1991 goto drop;
1993 mrt = ipmr_rt_fib_lookup(net, skb);
1994 if (IS_ERR(mrt))
1995 goto drop;
1996 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1997 drop:
1998 kfree_skb(skb);
2000 return 0;
2002 #endif
2004 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2005 struct mfc_cache *c, struct rtmsg *rtm)
2007 int ct;
2008 struct rtnexthop *nhp;
2009 u8 *b = skb_tail_pointer(skb);
2010 struct rtattr *mp_head;
2012 /* If cache is unresolved, don't try to parse IIF and OIF */
2013 if (c->mfc_parent >= MAXVIFS)
2014 return -ENOENT;
2016 if (VIF_EXISTS(mrt, c->mfc_parent))
2017 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
2019 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
2021 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2022 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2023 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2024 goto rtattr_failure;
2025 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
2026 nhp->rtnh_flags = 0;
2027 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2028 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2029 nhp->rtnh_len = sizeof(*nhp);
2032 mp_head->rta_type = RTA_MULTIPATH;
2033 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
2034 rtm->rtm_type = RTN_MULTICAST;
2035 return 1;
2037 rtattr_failure:
2038 nlmsg_trim(skb, b);
2039 return -EMSGSIZE;
2042 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2043 __be32 saddr, __be32 daddr,
2044 struct rtmsg *rtm, int nowait)
2046 struct mfc_cache *cache;
2047 struct mr_table *mrt;
2048 int err;
2050 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2051 if (mrt == NULL)
2052 return -ENOENT;
2054 rcu_read_lock();
2055 cache = ipmr_cache_find(mrt, saddr, daddr);
2057 if (cache == NULL) {
2058 struct sk_buff *skb2;
2059 struct iphdr *iph;
2060 struct net_device *dev;
2061 int vif = -1;
2063 if (nowait) {
2064 rcu_read_unlock();
2065 return -EAGAIN;
2068 dev = skb->dev;
2069 read_lock(&mrt_lock);
2070 if (dev)
2071 vif = ipmr_find_vif(mrt, dev);
2072 if (vif < 0) {
2073 read_unlock(&mrt_lock);
2074 rcu_read_unlock();
2075 return -ENODEV;
2077 skb2 = skb_clone(skb, GFP_ATOMIC);
2078 if (!skb2) {
2079 read_unlock(&mrt_lock);
2080 rcu_read_unlock();
2081 return -ENOMEM;
2084 skb_push(skb2, sizeof(struct iphdr));
2085 skb_reset_network_header(skb2);
2086 iph = ip_hdr(skb2);
2087 iph->ihl = sizeof(struct iphdr) >> 2;
2088 iph->saddr = saddr;
2089 iph->daddr = daddr;
2090 iph->version = 0;
2091 err = ipmr_cache_unresolved(mrt, vif, skb2);
2092 read_unlock(&mrt_lock);
2093 rcu_read_unlock();
2094 return err;
2097 read_lock(&mrt_lock);
2098 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2099 cache->mfc_flags |= MFC_NOTIFY;
2100 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2101 read_unlock(&mrt_lock);
2102 rcu_read_unlock();
2103 return err;
2106 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2107 u32 pid, u32 seq, struct mfc_cache *c)
2109 struct nlmsghdr *nlh;
2110 struct rtmsg *rtm;
2112 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2113 if (nlh == NULL)
2114 return -EMSGSIZE;
2116 rtm = nlmsg_data(nlh);
2117 rtm->rtm_family = RTNL_FAMILY_IPMR;
2118 rtm->rtm_dst_len = 32;
2119 rtm->rtm_src_len = 32;
2120 rtm->rtm_tos = 0;
2121 rtm->rtm_table = mrt->id;
2122 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2123 goto nla_put_failure;
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 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2130 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2131 goto nla_put_failure;
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 pr_err("%s: can't add PIM protocol\n", __func__);
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;