Linux 3.11-rc3
[cris-mirror.git] / net / ipv4 / ipmr.c
blob132a09664704ed73ed850e8961f11fa3e3641473
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/ip_tunnels.h>
65 #include <net/checksum.h>
66 #include <net/netlink.h>
67 #include <net/fib_rules.h>
68 #include <linux/netconf.h>
70 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71 #define CONFIG_IP_PIMSM 1
72 #endif
74 struct mr_table {
75 struct list_head list;
76 #ifdef CONFIG_NET_NS
77 struct net *net;
78 #endif
79 u32 id;
80 struct sock __rcu *mroute_sk;
81 struct timer_list ipmr_expire_timer;
82 struct list_head mfc_unres_queue;
83 struct list_head mfc_cache_array[MFC_LINES];
84 struct vif_device vif_table[MAXVIFS];
85 int maxvif;
86 atomic_t cache_resolve_queue_len;
87 bool mroute_do_assert;
88 bool mroute_do_pim;
89 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num;
91 #endif
94 struct ipmr_rule {
95 struct fib_rule common;
98 struct ipmr_result {
99 struct mr_table *mrt;
102 /* Big lock, protecting vif table, mrt cache and mroute socket state.
103 * Note that the changes are semaphored via rtnl_lock.
106 static DEFINE_RWLOCK(mrt_lock);
109 * Multicast router control variables
112 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
114 /* Special spinlock for queue of unresolved entries */
115 static DEFINE_SPINLOCK(mfc_unres_lock);
117 /* We return to original Alan's scheme. Hash table of resolved
118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
122 * In this case data path is free of exclusive locks at all.
125 static struct kmem_cache *mrt_cachep __read_mostly;
127 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
128 static void ipmr_free_table(struct mr_table *mrt);
130 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
131 struct sk_buff *skb, struct mfc_cache *cache,
132 int local);
133 static int ipmr_cache_report(struct mr_table *mrt,
134 struct sk_buff *pkt, vifi_t vifi, int assert);
135 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136 struct mfc_cache *c, struct rtmsg *rtm);
137 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
138 int cmd);
139 static void mroute_clean_tables(struct mr_table *mrt);
140 static void ipmr_expire_process(unsigned long arg);
142 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
143 #define ipmr_for_each_table(mrt, net) \
144 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
146 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
148 struct mr_table *mrt;
150 ipmr_for_each_table(mrt, net) {
151 if (mrt->id == id)
152 return mrt;
154 return NULL;
157 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
158 struct mr_table **mrt)
160 struct ipmr_result res;
161 struct fib_lookup_arg arg = { .result = &res, };
162 int err;
164 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
165 flowi4_to_flowi(flp4), 0, &arg);
166 if (err < 0)
167 return err;
168 *mrt = res.mrt;
169 return 0;
172 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
173 int flags, struct fib_lookup_arg *arg)
175 struct ipmr_result *res = arg->result;
176 struct mr_table *mrt;
178 switch (rule->action) {
179 case FR_ACT_TO_TBL:
180 break;
181 case FR_ACT_UNREACHABLE:
182 return -ENETUNREACH;
183 case FR_ACT_PROHIBIT:
184 return -EACCES;
185 case FR_ACT_BLACKHOLE:
186 default:
187 return -EINVAL;
190 mrt = ipmr_get_table(rule->fr_net, rule->table);
191 if (mrt == NULL)
192 return -EAGAIN;
193 res->mrt = mrt;
194 return 0;
197 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
199 return 1;
202 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
203 FRA_GENERIC_POLICY,
206 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
207 struct fib_rule_hdr *frh, struct nlattr **tb)
209 return 0;
212 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
213 struct nlattr **tb)
215 return 1;
218 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
219 struct fib_rule_hdr *frh)
221 frh->dst_len = 0;
222 frh->src_len = 0;
223 frh->tos = 0;
224 return 0;
227 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
228 .family = RTNL_FAMILY_IPMR,
229 .rule_size = sizeof(struct ipmr_rule),
230 .addr_size = sizeof(u32),
231 .action = ipmr_rule_action,
232 .match = ipmr_rule_match,
233 .configure = ipmr_rule_configure,
234 .compare = ipmr_rule_compare,
235 .default_pref = fib_default_rule_pref,
236 .fill = ipmr_rule_fill,
237 .nlgroup = RTNLGRP_IPV4_RULE,
238 .policy = ipmr_rule_policy,
239 .owner = THIS_MODULE,
242 static int __net_init ipmr_rules_init(struct net *net)
244 struct fib_rules_ops *ops;
245 struct mr_table *mrt;
246 int err;
248 ops = fib_rules_register(&ipmr_rules_ops_template, net);
249 if (IS_ERR(ops))
250 return PTR_ERR(ops);
252 INIT_LIST_HEAD(&net->ipv4.mr_tables);
254 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
255 if (mrt == NULL) {
256 err = -ENOMEM;
257 goto err1;
260 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
261 if (err < 0)
262 goto err2;
264 net->ipv4.mr_rules_ops = ops;
265 return 0;
267 err2:
268 kfree(mrt);
269 err1:
270 fib_rules_unregister(ops);
271 return err;
274 static void __net_exit ipmr_rules_exit(struct net *net)
276 struct mr_table *mrt, *next;
278 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
279 list_del(&mrt->list);
280 ipmr_free_table(mrt);
282 fib_rules_unregister(net->ipv4.mr_rules_ops);
284 #else
285 #define ipmr_for_each_table(mrt, net) \
286 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
288 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
290 return net->ipv4.mrt;
293 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
294 struct mr_table **mrt)
296 *mrt = net->ipv4.mrt;
297 return 0;
300 static int __net_init ipmr_rules_init(struct net *net)
302 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
303 return net->ipv4.mrt ? 0 : -ENOMEM;
306 static void __net_exit ipmr_rules_exit(struct net *net)
308 ipmr_free_table(net->ipv4.mrt);
310 #endif
312 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
314 struct mr_table *mrt;
315 unsigned int i;
317 mrt = ipmr_get_table(net, id);
318 if (mrt != NULL)
319 return mrt;
321 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
322 if (mrt == NULL)
323 return NULL;
324 write_pnet(&mrt->net, net);
325 mrt->id = id;
327 /* Forwarding cache */
328 for (i = 0; i < MFC_LINES; i++)
329 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
331 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
333 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
334 (unsigned long)mrt);
336 #ifdef CONFIG_IP_PIMSM
337 mrt->mroute_reg_vif_num = -1;
338 #endif
339 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
340 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
341 #endif
342 return mrt;
345 static void ipmr_free_table(struct mr_table *mrt)
347 del_timer_sync(&mrt->ipmr_expire_timer);
348 mroute_clean_tables(mrt);
349 kfree(mrt);
352 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
354 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
356 struct net *net = dev_net(dev);
358 dev_close(dev);
360 dev = __dev_get_by_name(net, "tunl0");
361 if (dev) {
362 const struct net_device_ops *ops = dev->netdev_ops;
363 struct ifreq ifr;
364 struct ip_tunnel_parm p;
366 memset(&p, 0, sizeof(p));
367 p.iph.daddr = v->vifc_rmt_addr.s_addr;
368 p.iph.saddr = v->vifc_lcl_addr.s_addr;
369 p.iph.version = 4;
370 p.iph.ihl = 5;
371 p.iph.protocol = IPPROTO_IPIP;
372 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
373 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
375 if (ops->ndo_do_ioctl) {
376 mm_segment_t oldfs = get_fs();
378 set_fs(KERNEL_DS);
379 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
380 set_fs(oldfs);
385 static
386 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
388 struct net_device *dev;
390 dev = __dev_get_by_name(net, "tunl0");
392 if (dev) {
393 const struct net_device_ops *ops = dev->netdev_ops;
394 int err;
395 struct ifreq ifr;
396 struct ip_tunnel_parm p;
397 struct in_device *in_dev;
399 memset(&p, 0, sizeof(p));
400 p.iph.daddr = v->vifc_rmt_addr.s_addr;
401 p.iph.saddr = v->vifc_lcl_addr.s_addr;
402 p.iph.version = 4;
403 p.iph.ihl = 5;
404 p.iph.protocol = IPPROTO_IPIP;
405 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
406 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
408 if (ops->ndo_do_ioctl) {
409 mm_segment_t oldfs = get_fs();
411 set_fs(KERNEL_DS);
412 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
413 set_fs(oldfs);
414 } else {
415 err = -EOPNOTSUPP;
417 dev = NULL;
419 if (err == 0 &&
420 (dev = __dev_get_by_name(net, p.name)) != NULL) {
421 dev->flags |= IFF_MULTICAST;
423 in_dev = __in_dev_get_rtnl(dev);
424 if (in_dev == NULL)
425 goto failure;
427 ipv4_devconf_setall(in_dev);
428 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
430 if (dev_open(dev))
431 goto failure;
432 dev_hold(dev);
435 return dev;
437 failure:
438 /* allow the register to be completed before unregistering. */
439 rtnl_unlock();
440 rtnl_lock();
442 unregister_netdevice(dev);
443 return NULL;
446 #ifdef CONFIG_IP_PIMSM
448 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
450 struct net *net = dev_net(dev);
451 struct mr_table *mrt;
452 struct flowi4 fl4 = {
453 .flowi4_oif = dev->ifindex,
454 .flowi4_iif = skb->skb_iif,
455 .flowi4_mark = skb->mark,
457 int err;
459 err = ipmr_fib_lookup(net, &fl4, &mrt);
460 if (err < 0) {
461 kfree_skb(skb);
462 return err;
465 read_lock(&mrt_lock);
466 dev->stats.tx_bytes += skb->len;
467 dev->stats.tx_packets++;
468 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
469 read_unlock(&mrt_lock);
470 kfree_skb(skb);
471 return NETDEV_TX_OK;
474 static const struct net_device_ops reg_vif_netdev_ops = {
475 .ndo_start_xmit = reg_vif_xmit,
478 static void reg_vif_setup(struct net_device *dev)
480 dev->type = ARPHRD_PIMREG;
481 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
482 dev->flags = IFF_NOARP;
483 dev->netdev_ops = &reg_vif_netdev_ops,
484 dev->destructor = free_netdev;
485 dev->features |= NETIF_F_NETNS_LOCAL;
488 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
490 struct net_device *dev;
491 struct in_device *in_dev;
492 char name[IFNAMSIZ];
494 if (mrt->id == RT_TABLE_DEFAULT)
495 sprintf(name, "pimreg");
496 else
497 sprintf(name, "pimreg%u", mrt->id);
499 dev = alloc_netdev(0, name, reg_vif_setup);
501 if (dev == NULL)
502 return NULL;
504 dev_net_set(dev, net);
506 if (register_netdevice(dev)) {
507 free_netdev(dev);
508 return NULL;
510 dev->iflink = 0;
512 rcu_read_lock();
513 in_dev = __in_dev_get_rcu(dev);
514 if (!in_dev) {
515 rcu_read_unlock();
516 goto failure;
519 ipv4_devconf_setall(in_dev);
520 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
521 rcu_read_unlock();
523 if (dev_open(dev))
524 goto failure;
526 dev_hold(dev);
528 return dev;
530 failure:
531 /* allow the register to be completed before unregistering. */
532 rtnl_unlock();
533 rtnl_lock();
535 unregister_netdevice(dev);
536 return NULL;
538 #endif
541 * vif_delete - Delete a VIF entry
542 * @notify: Set to 1, if the caller is a notifier_call
545 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
546 struct list_head *head)
548 struct vif_device *v;
549 struct net_device *dev;
550 struct in_device *in_dev;
552 if (vifi < 0 || vifi >= mrt->maxvif)
553 return -EADDRNOTAVAIL;
555 v = &mrt->vif_table[vifi];
557 write_lock_bh(&mrt_lock);
558 dev = v->dev;
559 v->dev = NULL;
561 if (!dev) {
562 write_unlock_bh(&mrt_lock);
563 return -EADDRNOTAVAIL;
566 #ifdef CONFIG_IP_PIMSM
567 if (vifi == mrt->mroute_reg_vif_num)
568 mrt->mroute_reg_vif_num = -1;
569 #endif
571 if (vifi + 1 == mrt->maxvif) {
572 int tmp;
574 for (tmp = vifi - 1; tmp >= 0; tmp--) {
575 if (VIF_EXISTS(mrt, tmp))
576 break;
578 mrt->maxvif = tmp+1;
581 write_unlock_bh(&mrt_lock);
583 dev_set_allmulti(dev, -1);
585 in_dev = __in_dev_get_rtnl(dev);
586 if (in_dev) {
587 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
588 inet_netconf_notify_devconf(dev_net(dev),
589 NETCONFA_MC_FORWARDING,
590 dev->ifindex, &in_dev->cnf);
591 ip_rt_multicast_event(in_dev);
594 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
595 unregister_netdevice_queue(dev, head);
597 dev_put(dev);
598 return 0;
601 static void ipmr_cache_free_rcu(struct rcu_head *head)
603 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
605 kmem_cache_free(mrt_cachep, c);
608 static inline void ipmr_cache_free(struct mfc_cache *c)
610 call_rcu(&c->rcu, ipmr_cache_free_rcu);
613 /* Destroy an unresolved cache entry, killing queued skbs
614 * and reporting error to netlink readers.
617 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
619 struct net *net = read_pnet(&mrt->net);
620 struct sk_buff *skb;
621 struct nlmsgerr *e;
623 atomic_dec(&mrt->cache_resolve_queue_len);
625 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
626 if (ip_hdr(skb)->version == 0) {
627 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
628 nlh->nlmsg_type = NLMSG_ERROR;
629 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
630 skb_trim(skb, nlh->nlmsg_len);
631 e = nlmsg_data(nlh);
632 e->error = -ETIMEDOUT;
633 memset(&e->msg, 0, sizeof(e->msg));
635 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
636 } else {
637 kfree_skb(skb);
641 ipmr_cache_free(c);
645 /* Timer process for the unresolved queue. */
647 static void ipmr_expire_process(unsigned long arg)
649 struct mr_table *mrt = (struct mr_table *)arg;
650 unsigned long now;
651 unsigned long expires;
652 struct mfc_cache *c, *next;
654 if (!spin_trylock(&mfc_unres_lock)) {
655 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
656 return;
659 if (list_empty(&mrt->mfc_unres_queue))
660 goto out;
662 now = jiffies;
663 expires = 10*HZ;
665 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
666 if (time_after(c->mfc_un.unres.expires, now)) {
667 unsigned long interval = c->mfc_un.unres.expires - now;
668 if (interval < expires)
669 expires = interval;
670 continue;
673 list_del(&c->list);
674 mroute_netlink_event(mrt, c, RTM_DELROUTE);
675 ipmr_destroy_unres(mrt, c);
678 if (!list_empty(&mrt->mfc_unres_queue))
679 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
681 out:
682 spin_unlock(&mfc_unres_lock);
685 /* Fill oifs list. It is called under write locked mrt_lock. */
687 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
688 unsigned char *ttls)
690 int vifi;
692 cache->mfc_un.res.minvif = MAXVIFS;
693 cache->mfc_un.res.maxvif = 0;
694 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
696 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
697 if (VIF_EXISTS(mrt, vifi) &&
698 ttls[vifi] && ttls[vifi] < 255) {
699 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
700 if (cache->mfc_un.res.minvif > vifi)
701 cache->mfc_un.res.minvif = vifi;
702 if (cache->mfc_un.res.maxvif <= vifi)
703 cache->mfc_un.res.maxvif = vifi + 1;
708 static int vif_add(struct net *net, struct mr_table *mrt,
709 struct vifctl *vifc, int mrtsock)
711 int vifi = vifc->vifc_vifi;
712 struct vif_device *v = &mrt->vif_table[vifi];
713 struct net_device *dev;
714 struct in_device *in_dev;
715 int err;
717 /* Is vif busy ? */
718 if (VIF_EXISTS(mrt, vifi))
719 return -EADDRINUSE;
721 switch (vifc->vifc_flags) {
722 #ifdef CONFIG_IP_PIMSM
723 case VIFF_REGISTER:
725 * Special Purpose VIF in PIM
726 * All the packets will be sent to the daemon
728 if (mrt->mroute_reg_vif_num >= 0)
729 return -EADDRINUSE;
730 dev = ipmr_reg_vif(net, mrt);
731 if (!dev)
732 return -ENOBUFS;
733 err = dev_set_allmulti(dev, 1);
734 if (err) {
735 unregister_netdevice(dev);
736 dev_put(dev);
737 return err;
739 break;
740 #endif
741 case VIFF_TUNNEL:
742 dev = ipmr_new_tunnel(net, vifc);
743 if (!dev)
744 return -ENOBUFS;
745 err = dev_set_allmulti(dev, 1);
746 if (err) {
747 ipmr_del_tunnel(dev, vifc);
748 dev_put(dev);
749 return err;
751 break;
753 case VIFF_USE_IFINDEX:
754 case 0:
755 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
756 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
757 if (dev && __in_dev_get_rtnl(dev) == NULL) {
758 dev_put(dev);
759 return -EADDRNOTAVAIL;
761 } else {
762 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
764 if (!dev)
765 return -EADDRNOTAVAIL;
766 err = dev_set_allmulti(dev, 1);
767 if (err) {
768 dev_put(dev);
769 return err;
771 break;
772 default:
773 return -EINVAL;
776 in_dev = __in_dev_get_rtnl(dev);
777 if (!in_dev) {
778 dev_put(dev);
779 return -EADDRNOTAVAIL;
781 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
782 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
783 &in_dev->cnf);
784 ip_rt_multicast_event(in_dev);
786 /* Fill in the VIF structures */
788 v->rate_limit = vifc->vifc_rate_limit;
789 v->local = vifc->vifc_lcl_addr.s_addr;
790 v->remote = vifc->vifc_rmt_addr.s_addr;
791 v->flags = vifc->vifc_flags;
792 if (!mrtsock)
793 v->flags |= VIFF_STATIC;
794 v->threshold = vifc->vifc_threshold;
795 v->bytes_in = 0;
796 v->bytes_out = 0;
797 v->pkt_in = 0;
798 v->pkt_out = 0;
799 v->link = dev->ifindex;
800 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
801 v->link = dev->iflink;
803 /* And finish update writing critical data */
804 write_lock_bh(&mrt_lock);
805 v->dev = dev;
806 #ifdef CONFIG_IP_PIMSM
807 if (v->flags & VIFF_REGISTER)
808 mrt->mroute_reg_vif_num = vifi;
809 #endif
810 if (vifi+1 > mrt->maxvif)
811 mrt->maxvif = vifi+1;
812 write_unlock_bh(&mrt_lock);
813 return 0;
816 /* called with rcu_read_lock() */
817 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
818 __be32 origin,
819 __be32 mcastgrp)
821 int line = MFC_HASH(mcastgrp, origin);
822 struct mfc_cache *c;
824 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
825 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
826 return c;
828 return NULL;
831 /* Look for a (*,*,oif) entry */
832 static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
833 int vifi)
835 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
836 struct mfc_cache *c;
838 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
839 if (c->mfc_origin == htonl(INADDR_ANY) &&
840 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
841 c->mfc_un.res.ttls[vifi] < 255)
842 return c;
844 return NULL;
847 /* Look for a (*,G) entry */
848 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
849 __be32 mcastgrp, int vifi)
851 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
852 struct mfc_cache *c, *proxy;
854 if (mcastgrp == htonl(INADDR_ANY))
855 goto skip;
857 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
858 if (c->mfc_origin == htonl(INADDR_ANY) &&
859 c->mfc_mcastgrp == mcastgrp) {
860 if (c->mfc_un.res.ttls[vifi] < 255)
861 return c;
863 /* It's ok if the vifi is part of the static tree */
864 proxy = ipmr_cache_find_any_parent(mrt,
865 c->mfc_parent);
866 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
867 return c;
870 skip:
871 return ipmr_cache_find_any_parent(mrt, vifi);
875 * Allocate a multicast cache entry
877 static struct mfc_cache *ipmr_cache_alloc(void)
879 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
881 if (c)
882 c->mfc_un.res.minvif = MAXVIFS;
883 return c;
886 static struct mfc_cache *ipmr_cache_alloc_unres(void)
888 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
890 if (c) {
891 skb_queue_head_init(&c->mfc_un.unres.unresolved);
892 c->mfc_un.unres.expires = jiffies + 10*HZ;
894 return c;
898 * A cache entry has gone into a resolved state from queued
901 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
902 struct mfc_cache *uc, struct mfc_cache *c)
904 struct sk_buff *skb;
905 struct nlmsgerr *e;
907 /* Play the pending entries through our router */
909 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
910 if (ip_hdr(skb)->version == 0) {
911 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
913 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
914 nlh->nlmsg_len = skb_tail_pointer(skb) -
915 (u8 *)nlh;
916 } else {
917 nlh->nlmsg_type = NLMSG_ERROR;
918 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
919 skb_trim(skb, nlh->nlmsg_len);
920 e = nlmsg_data(nlh);
921 e->error = -EMSGSIZE;
922 memset(&e->msg, 0, sizeof(e->msg));
925 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
926 } else {
927 ip_mr_forward(net, mrt, skb, c, 0);
933 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
934 * expects the following bizarre scheme.
936 * Called under mrt_lock.
939 static int ipmr_cache_report(struct mr_table *mrt,
940 struct sk_buff *pkt, vifi_t vifi, int assert)
942 struct sk_buff *skb;
943 const int ihl = ip_hdrlen(pkt);
944 struct igmphdr *igmp;
945 struct igmpmsg *msg;
946 struct sock *mroute_sk;
947 int ret;
949 #ifdef CONFIG_IP_PIMSM
950 if (assert == IGMPMSG_WHOLEPKT)
951 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
952 else
953 #endif
954 skb = alloc_skb(128, GFP_ATOMIC);
956 if (!skb)
957 return -ENOBUFS;
959 #ifdef CONFIG_IP_PIMSM
960 if (assert == IGMPMSG_WHOLEPKT) {
961 /* Ugly, but we have no choice with this interface.
962 * Duplicate old header, fix ihl, length etc.
963 * And all this only to mangle msg->im_msgtype and
964 * to set msg->im_mbz to "mbz" :-)
966 skb_push(skb, sizeof(struct iphdr));
967 skb_reset_network_header(skb);
968 skb_reset_transport_header(skb);
969 msg = (struct igmpmsg *)skb_network_header(skb);
970 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
971 msg->im_msgtype = IGMPMSG_WHOLEPKT;
972 msg->im_mbz = 0;
973 msg->im_vif = mrt->mroute_reg_vif_num;
974 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
975 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
976 sizeof(struct iphdr));
977 } else
978 #endif
981 /* Copy the IP header */
983 skb_set_network_header(skb, skb->len);
984 skb_put(skb, ihl);
985 skb_copy_to_linear_data(skb, pkt->data, ihl);
986 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
987 msg = (struct igmpmsg *)skb_network_header(skb);
988 msg->im_vif = vifi;
989 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
991 /* Add our header */
993 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
994 igmp->type =
995 msg->im_msgtype = assert;
996 igmp->code = 0;
997 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
998 skb->transport_header = skb->network_header;
1001 rcu_read_lock();
1002 mroute_sk = rcu_dereference(mrt->mroute_sk);
1003 if (mroute_sk == NULL) {
1004 rcu_read_unlock();
1005 kfree_skb(skb);
1006 return -EINVAL;
1009 /* Deliver to mrouted */
1011 ret = sock_queue_rcv_skb(mroute_sk, skb);
1012 rcu_read_unlock();
1013 if (ret < 0) {
1014 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1015 kfree_skb(skb);
1018 return ret;
1022 * Queue a packet for resolution. It gets locked cache entry!
1025 static int
1026 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
1028 bool found = false;
1029 int err;
1030 struct mfc_cache *c;
1031 const struct iphdr *iph = ip_hdr(skb);
1033 spin_lock_bh(&mfc_unres_lock);
1034 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
1035 if (c->mfc_mcastgrp == iph->daddr &&
1036 c->mfc_origin == iph->saddr) {
1037 found = true;
1038 break;
1042 if (!found) {
1043 /* Create a new entry if allowable */
1045 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1046 (c = ipmr_cache_alloc_unres()) == NULL) {
1047 spin_unlock_bh(&mfc_unres_lock);
1049 kfree_skb(skb);
1050 return -ENOBUFS;
1053 /* Fill in the new cache entry */
1055 c->mfc_parent = -1;
1056 c->mfc_origin = iph->saddr;
1057 c->mfc_mcastgrp = iph->daddr;
1059 /* Reflect first query at mrouted. */
1061 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1062 if (err < 0) {
1063 /* If the report failed throw the cache entry
1064 out - Brad Parker
1066 spin_unlock_bh(&mfc_unres_lock);
1068 ipmr_cache_free(c);
1069 kfree_skb(skb);
1070 return err;
1073 atomic_inc(&mrt->cache_resolve_queue_len);
1074 list_add(&c->list, &mrt->mfc_unres_queue);
1075 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1077 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1078 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1081 /* See if we can append the packet */
1083 if (c->mfc_un.unres.unresolved.qlen > 3) {
1084 kfree_skb(skb);
1085 err = -ENOBUFS;
1086 } else {
1087 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1088 err = 0;
1091 spin_unlock_bh(&mfc_unres_lock);
1092 return err;
1096 * MFC cache manipulation by user space mroute daemon
1099 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1101 int line;
1102 struct mfc_cache *c, *next;
1104 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1106 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1107 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1108 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1109 (parent == -1 || parent == c->mfc_parent)) {
1110 list_del_rcu(&c->list);
1111 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1112 ipmr_cache_free(c);
1113 return 0;
1116 return -ENOENT;
1119 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1120 struct mfcctl *mfc, int mrtsock, int parent)
1122 bool found = false;
1123 int line;
1124 struct mfc_cache *uc, *c;
1126 if (mfc->mfcc_parent >= MAXVIFS)
1127 return -ENFILE;
1129 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1131 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1132 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1133 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1134 (parent == -1 || parent == c->mfc_parent)) {
1135 found = true;
1136 break;
1140 if (found) {
1141 write_lock_bh(&mrt_lock);
1142 c->mfc_parent = mfc->mfcc_parent;
1143 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1144 if (!mrtsock)
1145 c->mfc_flags |= MFC_STATIC;
1146 write_unlock_bh(&mrt_lock);
1147 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1148 return 0;
1151 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1152 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1153 return -EINVAL;
1155 c = ipmr_cache_alloc();
1156 if (c == NULL)
1157 return -ENOMEM;
1159 c->mfc_origin = mfc->mfcc_origin.s_addr;
1160 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1161 c->mfc_parent = mfc->mfcc_parent;
1162 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1163 if (!mrtsock)
1164 c->mfc_flags |= MFC_STATIC;
1166 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1169 * Check to see if we resolved a queued list. If so we
1170 * need to send on the frames and tidy up.
1172 found = false;
1173 spin_lock_bh(&mfc_unres_lock);
1174 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1175 if (uc->mfc_origin == c->mfc_origin &&
1176 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1177 list_del(&uc->list);
1178 atomic_dec(&mrt->cache_resolve_queue_len);
1179 found = true;
1180 break;
1183 if (list_empty(&mrt->mfc_unres_queue))
1184 del_timer(&mrt->ipmr_expire_timer);
1185 spin_unlock_bh(&mfc_unres_lock);
1187 if (found) {
1188 ipmr_cache_resolve(net, mrt, uc, c);
1189 ipmr_cache_free(uc);
1191 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1192 return 0;
1196 * Close the multicast socket, and clear the vif tables etc
1199 static void mroute_clean_tables(struct mr_table *mrt)
1201 int i;
1202 LIST_HEAD(list);
1203 struct mfc_cache *c, *next;
1205 /* Shut down all active vif entries */
1207 for (i = 0; i < mrt->maxvif; i++) {
1208 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1209 vif_delete(mrt, i, 0, &list);
1211 unregister_netdevice_many(&list);
1213 /* Wipe the cache */
1215 for (i = 0; i < MFC_LINES; i++) {
1216 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1217 if (c->mfc_flags & MFC_STATIC)
1218 continue;
1219 list_del_rcu(&c->list);
1220 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1221 ipmr_cache_free(c);
1225 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1226 spin_lock_bh(&mfc_unres_lock);
1227 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1228 list_del(&c->list);
1229 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1230 ipmr_destroy_unres(mrt, c);
1232 spin_unlock_bh(&mfc_unres_lock);
1236 /* called from ip_ra_control(), before an RCU grace period,
1237 * we dont need to call synchronize_rcu() here
1239 static void mrtsock_destruct(struct sock *sk)
1241 struct net *net = sock_net(sk);
1242 struct mr_table *mrt;
1244 rtnl_lock();
1245 ipmr_for_each_table(mrt, net) {
1246 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1247 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1248 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1249 NETCONFA_IFINDEX_ALL,
1250 net->ipv4.devconf_all);
1251 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1252 mroute_clean_tables(mrt);
1255 rtnl_unlock();
1259 * Socket options and virtual interface manipulation. The whole
1260 * virtual interface system is a complete heap, but unfortunately
1261 * that's how BSD mrouted happens to think. Maybe one day with a proper
1262 * MOSPF/PIM router set up we can clean this up.
1265 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1267 int ret, parent = 0;
1268 struct vifctl vif;
1269 struct mfcctl mfc;
1270 struct net *net = sock_net(sk);
1271 struct mr_table *mrt;
1273 if (sk->sk_type != SOCK_RAW ||
1274 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1275 return -EOPNOTSUPP;
1277 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1278 if (mrt == NULL)
1279 return -ENOENT;
1281 if (optname != MRT_INIT) {
1282 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1283 !ns_capable(net->user_ns, CAP_NET_ADMIN))
1284 return -EACCES;
1287 switch (optname) {
1288 case MRT_INIT:
1289 if (optlen != sizeof(int))
1290 return -EINVAL;
1292 rtnl_lock();
1293 if (rtnl_dereference(mrt->mroute_sk)) {
1294 rtnl_unlock();
1295 return -EADDRINUSE;
1298 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1299 if (ret == 0) {
1300 rcu_assign_pointer(mrt->mroute_sk, sk);
1301 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1302 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1303 NETCONFA_IFINDEX_ALL,
1304 net->ipv4.devconf_all);
1306 rtnl_unlock();
1307 return ret;
1308 case MRT_DONE:
1309 if (sk != rcu_access_pointer(mrt->mroute_sk))
1310 return -EACCES;
1311 return ip_ra_control(sk, 0, NULL);
1312 case MRT_ADD_VIF:
1313 case MRT_DEL_VIF:
1314 if (optlen != sizeof(vif))
1315 return -EINVAL;
1316 if (copy_from_user(&vif, optval, sizeof(vif)))
1317 return -EFAULT;
1318 if (vif.vifc_vifi >= MAXVIFS)
1319 return -ENFILE;
1320 rtnl_lock();
1321 if (optname == MRT_ADD_VIF) {
1322 ret = vif_add(net, mrt, &vif,
1323 sk == rtnl_dereference(mrt->mroute_sk));
1324 } else {
1325 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1327 rtnl_unlock();
1328 return ret;
1331 * Manipulate the forwarding caches. These live
1332 * in a sort of kernel/user symbiosis.
1334 case MRT_ADD_MFC:
1335 case MRT_DEL_MFC:
1336 parent = -1;
1337 case MRT_ADD_MFC_PROXY:
1338 case MRT_DEL_MFC_PROXY:
1339 if (optlen != sizeof(mfc))
1340 return -EINVAL;
1341 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1342 return -EFAULT;
1343 if (parent == 0)
1344 parent = mfc.mfcc_parent;
1345 rtnl_lock();
1346 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1347 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1348 else
1349 ret = ipmr_mfc_add(net, mrt, &mfc,
1350 sk == rtnl_dereference(mrt->mroute_sk),
1351 parent);
1352 rtnl_unlock();
1353 return ret;
1355 * Control PIM assert.
1357 case MRT_ASSERT:
1359 int v;
1360 if (optlen != sizeof(v))
1361 return -EINVAL;
1362 if (get_user(v, (int __user *)optval))
1363 return -EFAULT;
1364 mrt->mroute_do_assert = v;
1365 return 0;
1367 #ifdef CONFIG_IP_PIMSM
1368 case MRT_PIM:
1370 int v;
1372 if (optlen != sizeof(v))
1373 return -EINVAL;
1374 if (get_user(v, (int __user *)optval))
1375 return -EFAULT;
1376 v = !!v;
1378 rtnl_lock();
1379 ret = 0;
1380 if (v != mrt->mroute_do_pim) {
1381 mrt->mroute_do_pim = v;
1382 mrt->mroute_do_assert = v;
1384 rtnl_unlock();
1385 return ret;
1387 #endif
1388 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1389 case MRT_TABLE:
1391 u32 v;
1393 if (optlen != sizeof(u32))
1394 return -EINVAL;
1395 if (get_user(v, (u32 __user *)optval))
1396 return -EFAULT;
1398 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1399 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1400 return -EINVAL;
1402 rtnl_lock();
1403 ret = 0;
1404 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1405 ret = -EBUSY;
1406 } else {
1407 if (!ipmr_new_table(net, v))
1408 ret = -ENOMEM;
1409 else
1410 raw_sk(sk)->ipmr_table = v;
1412 rtnl_unlock();
1413 return ret;
1415 #endif
1417 * Spurious command, or MRT_VERSION which you cannot
1418 * set.
1420 default:
1421 return -ENOPROTOOPT;
1426 * Getsock opt support for the multicast routing system.
1429 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1431 int olr;
1432 int val;
1433 struct net *net = sock_net(sk);
1434 struct mr_table *mrt;
1436 if (sk->sk_type != SOCK_RAW ||
1437 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1438 return -EOPNOTSUPP;
1440 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1441 if (mrt == NULL)
1442 return -ENOENT;
1444 if (optname != MRT_VERSION &&
1445 #ifdef CONFIG_IP_PIMSM
1446 optname != MRT_PIM &&
1447 #endif
1448 optname != MRT_ASSERT)
1449 return -ENOPROTOOPT;
1451 if (get_user(olr, optlen))
1452 return -EFAULT;
1454 olr = min_t(unsigned int, olr, sizeof(int));
1455 if (olr < 0)
1456 return -EINVAL;
1458 if (put_user(olr, optlen))
1459 return -EFAULT;
1460 if (optname == MRT_VERSION)
1461 val = 0x0305;
1462 #ifdef CONFIG_IP_PIMSM
1463 else if (optname == MRT_PIM)
1464 val = mrt->mroute_do_pim;
1465 #endif
1466 else
1467 val = mrt->mroute_do_assert;
1468 if (copy_to_user(optval, &val, olr))
1469 return -EFAULT;
1470 return 0;
1474 * The IP multicast ioctl support routines.
1477 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1479 struct sioc_sg_req sr;
1480 struct sioc_vif_req vr;
1481 struct vif_device *vif;
1482 struct mfc_cache *c;
1483 struct net *net = sock_net(sk);
1484 struct mr_table *mrt;
1486 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1487 if (mrt == NULL)
1488 return -ENOENT;
1490 switch (cmd) {
1491 case SIOCGETVIFCNT:
1492 if (copy_from_user(&vr, arg, sizeof(vr)))
1493 return -EFAULT;
1494 if (vr.vifi >= mrt->maxvif)
1495 return -EINVAL;
1496 read_lock(&mrt_lock);
1497 vif = &mrt->vif_table[vr.vifi];
1498 if (VIF_EXISTS(mrt, vr.vifi)) {
1499 vr.icount = vif->pkt_in;
1500 vr.ocount = vif->pkt_out;
1501 vr.ibytes = vif->bytes_in;
1502 vr.obytes = vif->bytes_out;
1503 read_unlock(&mrt_lock);
1505 if (copy_to_user(arg, &vr, sizeof(vr)))
1506 return -EFAULT;
1507 return 0;
1509 read_unlock(&mrt_lock);
1510 return -EADDRNOTAVAIL;
1511 case SIOCGETSGCNT:
1512 if (copy_from_user(&sr, arg, sizeof(sr)))
1513 return -EFAULT;
1515 rcu_read_lock();
1516 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1517 if (c) {
1518 sr.pktcnt = c->mfc_un.res.pkt;
1519 sr.bytecnt = c->mfc_un.res.bytes;
1520 sr.wrong_if = c->mfc_un.res.wrong_if;
1521 rcu_read_unlock();
1523 if (copy_to_user(arg, &sr, sizeof(sr)))
1524 return -EFAULT;
1525 return 0;
1527 rcu_read_unlock();
1528 return -EADDRNOTAVAIL;
1529 default:
1530 return -ENOIOCTLCMD;
1534 #ifdef CONFIG_COMPAT
1535 struct compat_sioc_sg_req {
1536 struct in_addr src;
1537 struct in_addr grp;
1538 compat_ulong_t pktcnt;
1539 compat_ulong_t bytecnt;
1540 compat_ulong_t wrong_if;
1543 struct compat_sioc_vif_req {
1544 vifi_t vifi; /* Which iface */
1545 compat_ulong_t icount;
1546 compat_ulong_t ocount;
1547 compat_ulong_t ibytes;
1548 compat_ulong_t obytes;
1551 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1553 struct compat_sioc_sg_req sr;
1554 struct compat_sioc_vif_req vr;
1555 struct vif_device *vif;
1556 struct mfc_cache *c;
1557 struct net *net = sock_net(sk);
1558 struct mr_table *mrt;
1560 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1561 if (mrt == NULL)
1562 return -ENOENT;
1564 switch (cmd) {
1565 case SIOCGETVIFCNT:
1566 if (copy_from_user(&vr, arg, sizeof(vr)))
1567 return -EFAULT;
1568 if (vr.vifi >= mrt->maxvif)
1569 return -EINVAL;
1570 read_lock(&mrt_lock);
1571 vif = &mrt->vif_table[vr.vifi];
1572 if (VIF_EXISTS(mrt, vr.vifi)) {
1573 vr.icount = vif->pkt_in;
1574 vr.ocount = vif->pkt_out;
1575 vr.ibytes = vif->bytes_in;
1576 vr.obytes = vif->bytes_out;
1577 read_unlock(&mrt_lock);
1579 if (copy_to_user(arg, &vr, sizeof(vr)))
1580 return -EFAULT;
1581 return 0;
1583 read_unlock(&mrt_lock);
1584 return -EADDRNOTAVAIL;
1585 case SIOCGETSGCNT:
1586 if (copy_from_user(&sr, arg, sizeof(sr)))
1587 return -EFAULT;
1589 rcu_read_lock();
1590 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1591 if (c) {
1592 sr.pktcnt = c->mfc_un.res.pkt;
1593 sr.bytecnt = c->mfc_un.res.bytes;
1594 sr.wrong_if = c->mfc_un.res.wrong_if;
1595 rcu_read_unlock();
1597 if (copy_to_user(arg, &sr, sizeof(sr)))
1598 return -EFAULT;
1599 return 0;
1601 rcu_read_unlock();
1602 return -EADDRNOTAVAIL;
1603 default:
1604 return -ENOIOCTLCMD;
1607 #endif
1610 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1612 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1613 struct net *net = dev_net(dev);
1614 struct mr_table *mrt;
1615 struct vif_device *v;
1616 int ct;
1618 if (event != NETDEV_UNREGISTER)
1619 return NOTIFY_DONE;
1621 ipmr_for_each_table(mrt, net) {
1622 v = &mrt->vif_table[0];
1623 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1624 if (v->dev == dev)
1625 vif_delete(mrt, ct, 1, NULL);
1628 return NOTIFY_DONE;
1632 static struct notifier_block ip_mr_notifier = {
1633 .notifier_call = ipmr_device_event,
1637 * Encapsulate a packet by attaching a valid IPIP header to it.
1638 * This avoids tunnel drivers and other mess and gives us the speed so
1639 * important for multicast video.
1642 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1644 struct iphdr *iph;
1645 const struct iphdr *old_iph = ip_hdr(skb);
1647 skb_push(skb, sizeof(struct iphdr));
1648 skb->transport_header = skb->network_header;
1649 skb_reset_network_header(skb);
1650 iph = ip_hdr(skb);
1652 iph->version = 4;
1653 iph->tos = old_iph->tos;
1654 iph->ttl = old_iph->ttl;
1655 iph->frag_off = 0;
1656 iph->daddr = daddr;
1657 iph->saddr = saddr;
1658 iph->protocol = IPPROTO_IPIP;
1659 iph->ihl = 5;
1660 iph->tot_len = htons(skb->len);
1661 ip_select_ident(iph, skb_dst(skb), NULL);
1662 ip_send_check(iph);
1664 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1665 nf_reset(skb);
1668 static inline int ipmr_forward_finish(struct sk_buff *skb)
1670 struct ip_options *opt = &(IPCB(skb)->opt);
1672 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1673 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
1675 if (unlikely(opt->optlen))
1676 ip_forward_options(skb);
1678 return dst_output(skb);
1682 * Processing handlers for ipmr_forward
1685 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1686 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1688 const struct iphdr *iph = ip_hdr(skb);
1689 struct vif_device *vif = &mrt->vif_table[vifi];
1690 struct net_device *dev;
1691 struct rtable *rt;
1692 struct flowi4 fl4;
1693 int encap = 0;
1695 if (vif->dev == NULL)
1696 goto out_free;
1698 #ifdef CONFIG_IP_PIMSM
1699 if (vif->flags & VIFF_REGISTER) {
1700 vif->pkt_out++;
1701 vif->bytes_out += skb->len;
1702 vif->dev->stats.tx_bytes += skb->len;
1703 vif->dev->stats.tx_packets++;
1704 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1705 goto out_free;
1707 #endif
1709 if (vif->flags & VIFF_TUNNEL) {
1710 rt = ip_route_output_ports(net, &fl4, NULL,
1711 vif->remote, vif->local,
1712 0, 0,
1713 IPPROTO_IPIP,
1714 RT_TOS(iph->tos), vif->link);
1715 if (IS_ERR(rt))
1716 goto out_free;
1717 encap = sizeof(struct iphdr);
1718 } else {
1719 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1720 0, 0,
1721 IPPROTO_IPIP,
1722 RT_TOS(iph->tos), vif->link);
1723 if (IS_ERR(rt))
1724 goto out_free;
1727 dev = rt->dst.dev;
1729 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1730 /* Do not fragment multicasts. Alas, IPv4 does not
1731 * allow to send ICMP, so that packets will disappear
1732 * to blackhole.
1735 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1736 ip_rt_put(rt);
1737 goto out_free;
1740 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1742 if (skb_cow(skb, encap)) {
1743 ip_rt_put(rt);
1744 goto out_free;
1747 vif->pkt_out++;
1748 vif->bytes_out += skb->len;
1750 skb_dst_drop(skb);
1751 skb_dst_set(skb, &rt->dst);
1752 ip_decrease_ttl(ip_hdr(skb));
1754 /* FIXME: forward and output firewalls used to be called here.
1755 * What do we do with netfilter? -- RR
1757 if (vif->flags & VIFF_TUNNEL) {
1758 ip_encap(skb, vif->local, vif->remote);
1759 /* FIXME: extra output firewall step used to be here. --RR */
1760 vif->dev->stats.tx_packets++;
1761 vif->dev->stats.tx_bytes += skb->len;
1764 IPCB(skb)->flags |= IPSKB_FORWARDED;
1767 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1768 * not only before forwarding, but after forwarding on all output
1769 * interfaces. It is clear, if mrouter runs a multicasting
1770 * program, it should receive packets not depending to what interface
1771 * program is joined.
1772 * If we will not make it, the program will have to join on all
1773 * interfaces. On the other hand, multihoming host (or router, but
1774 * not mrouter) cannot join to more than one interface - it will
1775 * result in receiving multiple packets.
1777 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1778 ipmr_forward_finish);
1779 return;
1781 out_free:
1782 kfree_skb(skb);
1785 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1787 int ct;
1789 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1790 if (mrt->vif_table[ct].dev == dev)
1791 break;
1793 return ct;
1796 /* "local" means that we should preserve one skb (for local delivery) */
1798 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1799 struct sk_buff *skb, struct mfc_cache *cache,
1800 int local)
1802 int psend = -1;
1803 int vif, ct;
1804 int true_vifi = ipmr_find_vif(mrt, skb->dev);
1806 vif = cache->mfc_parent;
1807 cache->mfc_un.res.pkt++;
1808 cache->mfc_un.res.bytes += skb->len;
1810 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1811 struct mfc_cache *cache_proxy;
1813 /* For an (*,G) entry, we only check that the incomming
1814 * interface is part of the static tree.
1816 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1817 if (cache_proxy &&
1818 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1819 goto forward;
1823 * Wrong interface: drop packet and (maybe) send PIM assert.
1825 if (mrt->vif_table[vif].dev != skb->dev) {
1826 if (rt_is_output_route(skb_rtable(skb))) {
1827 /* It is our own packet, looped back.
1828 * Very complicated situation...
1830 * The best workaround until routing daemons will be
1831 * fixed is not to redistribute packet, if it was
1832 * send through wrong interface. It means, that
1833 * multicast applications WILL NOT work for
1834 * (S,G), which have default multicast route pointing
1835 * to wrong oif. In any case, it is not a good
1836 * idea to use multicasting applications on router.
1838 goto dont_forward;
1841 cache->mfc_un.res.wrong_if++;
1843 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1844 /* pimsm uses asserts, when switching from RPT to SPT,
1845 * so that we cannot check that packet arrived on an oif.
1846 * It is bad, but otherwise we would need to move pretty
1847 * large chunk of pimd to kernel. Ough... --ANK
1849 (mrt->mroute_do_pim ||
1850 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1851 time_after(jiffies,
1852 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1853 cache->mfc_un.res.last_assert = jiffies;
1854 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1856 goto dont_forward;
1859 forward:
1860 mrt->vif_table[vif].pkt_in++;
1861 mrt->vif_table[vif].bytes_in += skb->len;
1864 * Forward the frame
1866 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1867 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
1868 if (true_vifi >= 0 &&
1869 true_vifi != cache->mfc_parent &&
1870 ip_hdr(skb)->ttl >
1871 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1872 /* It's an (*,*) entry and the packet is not coming from
1873 * the upstream: forward the packet to the upstream
1874 * only.
1876 psend = cache->mfc_parent;
1877 goto last_forward;
1879 goto dont_forward;
1881 for (ct = cache->mfc_un.res.maxvif - 1;
1882 ct >= cache->mfc_un.res.minvif; ct--) {
1883 /* For (*,G) entry, don't forward to the incoming interface */
1884 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1885 ct != true_vifi) &&
1886 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1887 if (psend != -1) {
1888 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1890 if (skb2)
1891 ipmr_queue_xmit(net, mrt, skb2, cache,
1892 psend);
1894 psend = ct;
1897 last_forward:
1898 if (psend != -1) {
1899 if (local) {
1900 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1902 if (skb2)
1903 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1904 } else {
1905 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1906 return 0;
1910 dont_forward:
1911 if (!local)
1912 kfree_skb(skb);
1913 return 0;
1916 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1918 struct rtable *rt = skb_rtable(skb);
1919 struct iphdr *iph = ip_hdr(skb);
1920 struct flowi4 fl4 = {
1921 .daddr = iph->daddr,
1922 .saddr = iph->saddr,
1923 .flowi4_tos = RT_TOS(iph->tos),
1924 .flowi4_oif = (rt_is_output_route(rt) ?
1925 skb->dev->ifindex : 0),
1926 .flowi4_iif = (rt_is_output_route(rt) ?
1927 LOOPBACK_IFINDEX :
1928 skb->dev->ifindex),
1929 .flowi4_mark = skb->mark,
1931 struct mr_table *mrt;
1932 int err;
1934 err = ipmr_fib_lookup(net, &fl4, &mrt);
1935 if (err)
1936 return ERR_PTR(err);
1937 return mrt;
1941 * Multicast packets for forwarding arrive here
1942 * Called with rcu_read_lock();
1945 int ip_mr_input(struct sk_buff *skb)
1947 struct mfc_cache *cache;
1948 struct net *net = dev_net(skb->dev);
1949 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1950 struct mr_table *mrt;
1952 /* Packet is looped back after forward, it should not be
1953 * forwarded second time, but still can be delivered locally.
1955 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1956 goto dont_forward;
1958 mrt = ipmr_rt_fib_lookup(net, skb);
1959 if (IS_ERR(mrt)) {
1960 kfree_skb(skb);
1961 return PTR_ERR(mrt);
1963 if (!local) {
1964 if (IPCB(skb)->opt.router_alert) {
1965 if (ip_call_ra_chain(skb))
1966 return 0;
1967 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1968 /* IGMPv1 (and broken IGMPv2 implementations sort of
1969 * Cisco IOS <= 11.2(8)) do not put router alert
1970 * option to IGMP packets destined to routable
1971 * groups. It is very bad, because it means
1972 * that we can forward NO IGMP messages.
1974 struct sock *mroute_sk;
1976 mroute_sk = rcu_dereference(mrt->mroute_sk);
1977 if (mroute_sk) {
1978 nf_reset(skb);
1979 raw_rcv(mroute_sk, skb);
1980 return 0;
1985 /* already under rcu_read_lock() */
1986 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1987 if (cache == NULL) {
1988 int vif = ipmr_find_vif(mrt, skb->dev);
1990 if (vif >= 0)
1991 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1992 vif);
1996 * No usable cache entry
1998 if (cache == NULL) {
1999 int vif;
2001 if (local) {
2002 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2003 ip_local_deliver(skb);
2004 if (skb2 == NULL)
2005 return -ENOBUFS;
2006 skb = skb2;
2009 read_lock(&mrt_lock);
2010 vif = ipmr_find_vif(mrt, skb->dev);
2011 if (vif >= 0) {
2012 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
2013 read_unlock(&mrt_lock);
2015 return err2;
2017 read_unlock(&mrt_lock);
2018 kfree_skb(skb);
2019 return -ENODEV;
2022 read_lock(&mrt_lock);
2023 ip_mr_forward(net, mrt, skb, cache, local);
2024 read_unlock(&mrt_lock);
2026 if (local)
2027 return ip_local_deliver(skb);
2029 return 0;
2031 dont_forward:
2032 if (local)
2033 return ip_local_deliver(skb);
2034 kfree_skb(skb);
2035 return 0;
2038 #ifdef CONFIG_IP_PIMSM
2039 /* called with rcu_read_lock() */
2040 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2041 unsigned int pimlen)
2043 struct net_device *reg_dev = NULL;
2044 struct iphdr *encap;
2046 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
2048 * Check that:
2049 * a. packet is really sent to a multicast group
2050 * b. packet is not a NULL-REGISTER
2051 * c. packet is not truncated
2053 if (!ipv4_is_multicast(encap->daddr) ||
2054 encap->tot_len == 0 ||
2055 ntohs(encap->tot_len) + pimlen > skb->len)
2056 return 1;
2058 read_lock(&mrt_lock);
2059 if (mrt->mroute_reg_vif_num >= 0)
2060 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
2061 read_unlock(&mrt_lock);
2063 if (reg_dev == NULL)
2064 return 1;
2066 skb->mac_header = skb->network_header;
2067 skb_pull(skb, (u8 *)encap - skb->data);
2068 skb_reset_network_header(skb);
2069 skb->protocol = htons(ETH_P_IP);
2070 skb->ip_summed = CHECKSUM_NONE;
2071 skb->pkt_type = PACKET_HOST;
2073 skb_tunnel_rx(skb, reg_dev);
2075 netif_rx(skb);
2077 return NET_RX_SUCCESS;
2079 #endif
2081 #ifdef CONFIG_IP_PIMSM_V1
2083 * Handle IGMP messages of PIMv1
2086 int pim_rcv_v1(struct sk_buff *skb)
2088 struct igmphdr *pim;
2089 struct net *net = dev_net(skb->dev);
2090 struct mr_table *mrt;
2092 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2093 goto drop;
2095 pim = igmp_hdr(skb);
2097 mrt = ipmr_rt_fib_lookup(net, skb);
2098 if (IS_ERR(mrt))
2099 goto drop;
2100 if (!mrt->mroute_do_pim ||
2101 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2102 goto drop;
2104 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2105 drop:
2106 kfree_skb(skb);
2108 return 0;
2110 #endif
2112 #ifdef CONFIG_IP_PIMSM_V2
2113 static int pim_rcv(struct sk_buff *skb)
2115 struct pimreghdr *pim;
2116 struct net *net = dev_net(skb->dev);
2117 struct mr_table *mrt;
2119 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2120 goto drop;
2122 pim = (struct pimreghdr *)skb_transport_header(skb);
2123 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2124 (pim->flags & PIM_NULL_REGISTER) ||
2125 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2126 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2127 goto drop;
2129 mrt = ipmr_rt_fib_lookup(net, skb);
2130 if (IS_ERR(mrt))
2131 goto drop;
2132 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2133 drop:
2134 kfree_skb(skb);
2136 return 0;
2138 #endif
2140 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2141 struct mfc_cache *c, struct rtmsg *rtm)
2143 int ct;
2144 struct rtnexthop *nhp;
2145 struct nlattr *mp_attr;
2146 struct rta_mfc_stats mfcs;
2148 /* If cache is unresolved, don't try to parse IIF and OIF */
2149 if (c->mfc_parent >= MAXVIFS)
2150 return -ENOENT;
2152 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2153 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2154 return -EMSGSIZE;
2156 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2157 return -EMSGSIZE;
2159 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2160 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2161 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2162 nla_nest_cancel(skb, mp_attr);
2163 return -EMSGSIZE;
2166 nhp->rtnh_flags = 0;
2167 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2168 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2169 nhp->rtnh_len = sizeof(*nhp);
2173 nla_nest_end(skb, mp_attr);
2175 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2176 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2177 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2178 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2179 return -EMSGSIZE;
2181 rtm->rtm_type = RTN_MULTICAST;
2182 return 1;
2185 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2186 __be32 saddr, __be32 daddr,
2187 struct rtmsg *rtm, int nowait)
2189 struct mfc_cache *cache;
2190 struct mr_table *mrt;
2191 int err;
2193 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2194 if (mrt == NULL)
2195 return -ENOENT;
2197 rcu_read_lock();
2198 cache = ipmr_cache_find(mrt, saddr, daddr);
2199 if (cache == NULL && skb->dev) {
2200 int vif = ipmr_find_vif(mrt, skb->dev);
2202 if (vif >= 0)
2203 cache = ipmr_cache_find_any(mrt, daddr, vif);
2205 if (cache == NULL) {
2206 struct sk_buff *skb2;
2207 struct iphdr *iph;
2208 struct net_device *dev;
2209 int vif = -1;
2211 if (nowait) {
2212 rcu_read_unlock();
2213 return -EAGAIN;
2216 dev = skb->dev;
2217 read_lock(&mrt_lock);
2218 if (dev)
2219 vif = ipmr_find_vif(mrt, dev);
2220 if (vif < 0) {
2221 read_unlock(&mrt_lock);
2222 rcu_read_unlock();
2223 return -ENODEV;
2225 skb2 = skb_clone(skb, GFP_ATOMIC);
2226 if (!skb2) {
2227 read_unlock(&mrt_lock);
2228 rcu_read_unlock();
2229 return -ENOMEM;
2232 skb_push(skb2, sizeof(struct iphdr));
2233 skb_reset_network_header(skb2);
2234 iph = ip_hdr(skb2);
2235 iph->ihl = sizeof(struct iphdr) >> 2;
2236 iph->saddr = saddr;
2237 iph->daddr = daddr;
2238 iph->version = 0;
2239 err = ipmr_cache_unresolved(mrt, vif, skb2);
2240 read_unlock(&mrt_lock);
2241 rcu_read_unlock();
2242 return err;
2245 read_lock(&mrt_lock);
2246 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2247 cache->mfc_flags |= MFC_NOTIFY;
2248 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2249 read_unlock(&mrt_lock);
2250 rcu_read_unlock();
2251 return err;
2254 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2255 u32 portid, u32 seq, struct mfc_cache *c, int cmd)
2257 struct nlmsghdr *nlh;
2258 struct rtmsg *rtm;
2259 int err;
2261 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), NLM_F_MULTI);
2262 if (nlh == NULL)
2263 return -EMSGSIZE;
2265 rtm = nlmsg_data(nlh);
2266 rtm->rtm_family = RTNL_FAMILY_IPMR;
2267 rtm->rtm_dst_len = 32;
2268 rtm->rtm_src_len = 32;
2269 rtm->rtm_tos = 0;
2270 rtm->rtm_table = mrt->id;
2271 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2272 goto nla_put_failure;
2273 rtm->rtm_type = RTN_MULTICAST;
2274 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2275 if (c->mfc_flags & MFC_STATIC)
2276 rtm->rtm_protocol = RTPROT_STATIC;
2277 else
2278 rtm->rtm_protocol = RTPROT_MROUTED;
2279 rtm->rtm_flags = 0;
2281 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2282 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2283 goto nla_put_failure;
2284 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2285 /* do not break the dump if cache is unresolved */
2286 if (err < 0 && err != -ENOENT)
2287 goto nla_put_failure;
2289 return nlmsg_end(skb, nlh);
2291 nla_put_failure:
2292 nlmsg_cancel(skb, nlh);
2293 return -EMSGSIZE;
2296 static size_t mroute_msgsize(bool unresolved, int maxvif)
2298 size_t len =
2299 NLMSG_ALIGN(sizeof(struct rtmsg))
2300 + nla_total_size(4) /* RTA_TABLE */
2301 + nla_total_size(4) /* RTA_SRC */
2302 + nla_total_size(4) /* RTA_DST */
2305 if (!unresolved)
2306 len = len
2307 + nla_total_size(4) /* RTA_IIF */
2308 + nla_total_size(0) /* RTA_MULTIPATH */
2309 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2310 /* RTA_MFC_STATS */
2311 + nla_total_size(sizeof(struct rta_mfc_stats))
2314 return len;
2317 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2318 int cmd)
2320 struct net *net = read_pnet(&mrt->net);
2321 struct sk_buff *skb;
2322 int err = -ENOBUFS;
2324 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2325 GFP_ATOMIC);
2326 if (skb == NULL)
2327 goto errout;
2329 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd);
2330 if (err < 0)
2331 goto errout;
2333 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2334 return;
2336 errout:
2337 kfree_skb(skb);
2338 if (err < 0)
2339 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2342 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2344 struct net *net = sock_net(skb->sk);
2345 struct mr_table *mrt;
2346 struct mfc_cache *mfc;
2347 unsigned int t = 0, s_t;
2348 unsigned int h = 0, s_h;
2349 unsigned int e = 0, s_e;
2351 s_t = cb->args[0];
2352 s_h = cb->args[1];
2353 s_e = cb->args[2];
2355 rcu_read_lock();
2356 ipmr_for_each_table(mrt, net) {
2357 if (t < s_t)
2358 goto next_table;
2359 if (t > s_t)
2360 s_h = 0;
2361 for (h = s_h; h < MFC_LINES; h++) {
2362 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2363 if (e < s_e)
2364 goto next_entry;
2365 if (ipmr_fill_mroute(mrt, skb,
2366 NETLINK_CB(cb->skb).portid,
2367 cb->nlh->nlmsg_seq,
2368 mfc, RTM_NEWROUTE) < 0)
2369 goto done;
2370 next_entry:
2371 e++;
2373 e = s_e = 0;
2375 spin_lock_bh(&mfc_unres_lock);
2376 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2377 if (e < s_e)
2378 goto next_entry2;
2379 if (ipmr_fill_mroute(mrt, skb,
2380 NETLINK_CB(cb->skb).portid,
2381 cb->nlh->nlmsg_seq,
2382 mfc, RTM_NEWROUTE) < 0) {
2383 spin_unlock_bh(&mfc_unres_lock);
2384 goto done;
2386 next_entry2:
2387 e++;
2389 spin_unlock_bh(&mfc_unres_lock);
2390 e = s_e = 0;
2391 s_h = 0;
2392 next_table:
2393 t++;
2395 done:
2396 rcu_read_unlock();
2398 cb->args[2] = e;
2399 cb->args[1] = h;
2400 cb->args[0] = t;
2402 return skb->len;
2405 #ifdef CONFIG_PROC_FS
2407 * The /proc interfaces to multicast routing :
2408 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2410 struct ipmr_vif_iter {
2411 struct seq_net_private p;
2412 struct mr_table *mrt;
2413 int ct;
2416 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2417 struct ipmr_vif_iter *iter,
2418 loff_t pos)
2420 struct mr_table *mrt = iter->mrt;
2422 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2423 if (!VIF_EXISTS(mrt, iter->ct))
2424 continue;
2425 if (pos-- == 0)
2426 return &mrt->vif_table[iter->ct];
2428 return NULL;
2431 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2432 __acquires(mrt_lock)
2434 struct ipmr_vif_iter *iter = seq->private;
2435 struct net *net = seq_file_net(seq);
2436 struct mr_table *mrt;
2438 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2439 if (mrt == NULL)
2440 return ERR_PTR(-ENOENT);
2442 iter->mrt = mrt;
2444 read_lock(&mrt_lock);
2445 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2446 : SEQ_START_TOKEN;
2449 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2451 struct ipmr_vif_iter *iter = seq->private;
2452 struct net *net = seq_file_net(seq);
2453 struct mr_table *mrt = iter->mrt;
2455 ++*pos;
2456 if (v == SEQ_START_TOKEN)
2457 return ipmr_vif_seq_idx(net, iter, 0);
2459 while (++iter->ct < mrt->maxvif) {
2460 if (!VIF_EXISTS(mrt, iter->ct))
2461 continue;
2462 return &mrt->vif_table[iter->ct];
2464 return NULL;
2467 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2468 __releases(mrt_lock)
2470 read_unlock(&mrt_lock);
2473 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2475 struct ipmr_vif_iter *iter = seq->private;
2476 struct mr_table *mrt = iter->mrt;
2478 if (v == SEQ_START_TOKEN) {
2479 seq_puts(seq,
2480 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2481 } else {
2482 const struct vif_device *vif = v;
2483 const char *name = vif->dev ? vif->dev->name : "none";
2485 seq_printf(seq,
2486 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2487 vif - mrt->vif_table,
2488 name, vif->bytes_in, vif->pkt_in,
2489 vif->bytes_out, vif->pkt_out,
2490 vif->flags, vif->local, vif->remote);
2492 return 0;
2495 static const struct seq_operations ipmr_vif_seq_ops = {
2496 .start = ipmr_vif_seq_start,
2497 .next = ipmr_vif_seq_next,
2498 .stop = ipmr_vif_seq_stop,
2499 .show = ipmr_vif_seq_show,
2502 static int ipmr_vif_open(struct inode *inode, struct file *file)
2504 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2505 sizeof(struct ipmr_vif_iter));
2508 static const struct file_operations ipmr_vif_fops = {
2509 .owner = THIS_MODULE,
2510 .open = ipmr_vif_open,
2511 .read = seq_read,
2512 .llseek = seq_lseek,
2513 .release = seq_release_net,
2516 struct ipmr_mfc_iter {
2517 struct seq_net_private p;
2518 struct mr_table *mrt;
2519 struct list_head *cache;
2520 int ct;
2524 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2525 struct ipmr_mfc_iter *it, loff_t pos)
2527 struct mr_table *mrt = it->mrt;
2528 struct mfc_cache *mfc;
2530 rcu_read_lock();
2531 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2532 it->cache = &mrt->mfc_cache_array[it->ct];
2533 list_for_each_entry_rcu(mfc, it->cache, list)
2534 if (pos-- == 0)
2535 return mfc;
2537 rcu_read_unlock();
2539 spin_lock_bh(&mfc_unres_lock);
2540 it->cache = &mrt->mfc_unres_queue;
2541 list_for_each_entry(mfc, it->cache, list)
2542 if (pos-- == 0)
2543 return mfc;
2544 spin_unlock_bh(&mfc_unres_lock);
2546 it->cache = NULL;
2547 return NULL;
2551 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2553 struct ipmr_mfc_iter *it = seq->private;
2554 struct net *net = seq_file_net(seq);
2555 struct mr_table *mrt;
2557 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2558 if (mrt == NULL)
2559 return ERR_PTR(-ENOENT);
2561 it->mrt = mrt;
2562 it->cache = NULL;
2563 it->ct = 0;
2564 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2565 : SEQ_START_TOKEN;
2568 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2570 struct mfc_cache *mfc = v;
2571 struct ipmr_mfc_iter *it = seq->private;
2572 struct net *net = seq_file_net(seq);
2573 struct mr_table *mrt = it->mrt;
2575 ++*pos;
2577 if (v == SEQ_START_TOKEN)
2578 return ipmr_mfc_seq_idx(net, seq->private, 0);
2580 if (mfc->list.next != it->cache)
2581 return list_entry(mfc->list.next, struct mfc_cache, list);
2583 if (it->cache == &mrt->mfc_unres_queue)
2584 goto end_of_list;
2586 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2588 while (++it->ct < MFC_LINES) {
2589 it->cache = &mrt->mfc_cache_array[it->ct];
2590 if (list_empty(it->cache))
2591 continue;
2592 return list_first_entry(it->cache, struct mfc_cache, list);
2595 /* exhausted cache_array, show unresolved */
2596 rcu_read_unlock();
2597 it->cache = &mrt->mfc_unres_queue;
2598 it->ct = 0;
2600 spin_lock_bh(&mfc_unres_lock);
2601 if (!list_empty(it->cache))
2602 return list_first_entry(it->cache, struct mfc_cache, list);
2604 end_of_list:
2605 spin_unlock_bh(&mfc_unres_lock);
2606 it->cache = NULL;
2608 return NULL;
2611 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2613 struct ipmr_mfc_iter *it = seq->private;
2614 struct mr_table *mrt = it->mrt;
2616 if (it->cache == &mrt->mfc_unres_queue)
2617 spin_unlock_bh(&mfc_unres_lock);
2618 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2619 rcu_read_unlock();
2622 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2624 int n;
2626 if (v == SEQ_START_TOKEN) {
2627 seq_puts(seq,
2628 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2629 } else {
2630 const struct mfc_cache *mfc = v;
2631 const struct ipmr_mfc_iter *it = seq->private;
2632 const struct mr_table *mrt = it->mrt;
2634 seq_printf(seq, "%08X %08X %-3hd",
2635 (__force u32) mfc->mfc_mcastgrp,
2636 (__force u32) mfc->mfc_origin,
2637 mfc->mfc_parent);
2639 if (it->cache != &mrt->mfc_unres_queue) {
2640 seq_printf(seq, " %8lu %8lu %8lu",
2641 mfc->mfc_un.res.pkt,
2642 mfc->mfc_un.res.bytes,
2643 mfc->mfc_un.res.wrong_if);
2644 for (n = mfc->mfc_un.res.minvif;
2645 n < mfc->mfc_un.res.maxvif; n++) {
2646 if (VIF_EXISTS(mrt, n) &&
2647 mfc->mfc_un.res.ttls[n] < 255)
2648 seq_printf(seq,
2649 " %2d:%-3d",
2650 n, mfc->mfc_un.res.ttls[n]);
2652 } else {
2653 /* unresolved mfc_caches don't contain
2654 * pkt, bytes and wrong_if values
2656 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2658 seq_putc(seq, '\n');
2660 return 0;
2663 static const struct seq_operations ipmr_mfc_seq_ops = {
2664 .start = ipmr_mfc_seq_start,
2665 .next = ipmr_mfc_seq_next,
2666 .stop = ipmr_mfc_seq_stop,
2667 .show = ipmr_mfc_seq_show,
2670 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2672 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2673 sizeof(struct ipmr_mfc_iter));
2676 static const struct file_operations ipmr_mfc_fops = {
2677 .owner = THIS_MODULE,
2678 .open = ipmr_mfc_open,
2679 .read = seq_read,
2680 .llseek = seq_lseek,
2681 .release = seq_release_net,
2683 #endif
2685 #ifdef CONFIG_IP_PIMSM_V2
2686 static const struct net_protocol pim_protocol = {
2687 .handler = pim_rcv,
2688 .netns_ok = 1,
2690 #endif
2694 * Setup for IP multicast routing
2696 static int __net_init ipmr_net_init(struct net *net)
2698 int err;
2700 err = ipmr_rules_init(net);
2701 if (err < 0)
2702 goto fail;
2704 #ifdef CONFIG_PROC_FS
2705 err = -ENOMEM;
2706 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
2707 goto proc_vif_fail;
2708 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
2709 goto proc_cache_fail;
2710 #endif
2711 return 0;
2713 #ifdef CONFIG_PROC_FS
2714 proc_cache_fail:
2715 remove_proc_entry("ip_mr_vif", net->proc_net);
2716 proc_vif_fail:
2717 ipmr_rules_exit(net);
2718 #endif
2719 fail:
2720 return err;
2723 static void __net_exit ipmr_net_exit(struct net *net)
2725 #ifdef CONFIG_PROC_FS
2726 remove_proc_entry("ip_mr_cache", net->proc_net);
2727 remove_proc_entry("ip_mr_vif", net->proc_net);
2728 #endif
2729 ipmr_rules_exit(net);
2732 static struct pernet_operations ipmr_net_ops = {
2733 .init = ipmr_net_init,
2734 .exit = ipmr_net_exit,
2737 int __init ip_mr_init(void)
2739 int err;
2741 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2742 sizeof(struct mfc_cache),
2743 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2744 NULL);
2745 if (!mrt_cachep)
2746 return -ENOMEM;
2748 err = register_pernet_subsys(&ipmr_net_ops);
2749 if (err)
2750 goto reg_pernet_fail;
2752 err = register_netdevice_notifier(&ip_mr_notifier);
2753 if (err)
2754 goto reg_notif_fail;
2755 #ifdef CONFIG_IP_PIMSM_V2
2756 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2757 pr_err("%s: can't add PIM protocol\n", __func__);
2758 err = -EAGAIN;
2759 goto add_proto_fail;
2761 #endif
2762 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2763 NULL, ipmr_rtm_dumproute, NULL);
2764 return 0;
2766 #ifdef CONFIG_IP_PIMSM_V2
2767 add_proto_fail:
2768 unregister_netdevice_notifier(&ip_mr_notifier);
2769 #endif
2770 reg_notif_fail:
2771 unregister_pernet_subsys(&ipmr_net_ops);
2772 reg_pernet_fail:
2773 kmem_cache_destroy(mrt_cachep);
2774 return err;