gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / net / ipv4 / ipmr.c
blobc3bfebd501ed17c0c9257bf6ed1a5a35a7893596
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 possible_net_t net;
77 u32 id;
78 struct sock __rcu *mroute_sk;
79 struct timer_list ipmr_expire_timer;
80 struct list_head mfc_unres_queue;
81 struct list_head mfc_cache_array[MFC_LINES];
82 struct vif_device vif_table[MAXVIFS];
83 int maxvif;
84 atomic_t cache_resolve_queue_len;
85 bool mroute_do_assert;
86 bool mroute_do_pim;
87 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
88 int mroute_reg_vif_num;
89 #endif
92 struct ipmr_rule {
93 struct fib_rule common;
96 struct ipmr_result {
97 struct mr_table *mrt;
100 /* Big lock, protecting vif table, mrt cache and mroute socket state.
101 * Note that the changes are semaphored via rtnl_lock.
104 static DEFINE_RWLOCK(mrt_lock);
107 * Multicast router control variables
110 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
112 /* Special spinlock for queue of unresolved entries */
113 static DEFINE_SPINLOCK(mfc_unres_lock);
115 /* We return to original Alan's scheme. Hash table of resolved
116 * entries is changed only in process context and protected
117 * with weak lock mrt_lock. Queue of unresolved entries is protected
118 * with strong spinlock mfc_unres_lock.
120 * In this case data path is free of exclusive locks at all.
123 static struct kmem_cache *mrt_cachep __read_mostly;
125 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
126 static void ipmr_free_table(struct mr_table *mrt);
128 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
129 struct sk_buff *skb, struct mfc_cache *cache,
130 int local);
131 static int ipmr_cache_report(struct mr_table *mrt,
132 struct sk_buff *pkt, vifi_t vifi, int assert);
133 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
134 struct mfc_cache *c, struct rtmsg *rtm);
135 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
136 int cmd);
137 static void mroute_clean_tables(struct mr_table *mrt, bool all);
138 static void ipmr_expire_process(unsigned long arg);
140 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
141 #define ipmr_for_each_table(mrt, net) \
142 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
144 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
146 struct mr_table *mrt;
148 ipmr_for_each_table(mrt, net) {
149 if (mrt->id == id)
150 return mrt;
152 return NULL;
155 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
156 struct mr_table **mrt)
158 int err;
159 struct ipmr_result res;
160 struct fib_lookup_arg arg = {
161 .result = &res,
162 .flags = FIB_LOOKUP_NOREF,
165 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
166 flowi4_to_flowi(flp4), 0, &arg);
167 if (err < 0)
168 return err;
169 *mrt = res.mrt;
170 return 0;
173 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
174 int flags, struct fib_lookup_arg *arg)
176 struct ipmr_result *res = arg->result;
177 struct mr_table *mrt;
179 switch (rule->action) {
180 case FR_ACT_TO_TBL:
181 break;
182 case FR_ACT_UNREACHABLE:
183 return -ENETUNREACH;
184 case FR_ACT_PROHIBIT:
185 return -EACCES;
186 case FR_ACT_BLACKHOLE:
187 default:
188 return -EINVAL;
191 mrt = ipmr_get_table(rule->fr_net, rule->table);
192 if (!mrt)
193 return -EAGAIN;
194 res->mrt = mrt;
195 return 0;
198 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
200 return 1;
203 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
204 FRA_GENERIC_POLICY,
207 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
208 struct fib_rule_hdr *frh, struct nlattr **tb)
210 return 0;
213 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
214 struct nlattr **tb)
216 return 1;
219 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
220 struct fib_rule_hdr *frh)
222 frh->dst_len = 0;
223 frh->src_len = 0;
224 frh->tos = 0;
225 return 0;
228 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
229 .family = RTNL_FAMILY_IPMR,
230 .rule_size = sizeof(struct ipmr_rule),
231 .addr_size = sizeof(u32),
232 .action = ipmr_rule_action,
233 .match = ipmr_rule_match,
234 .configure = ipmr_rule_configure,
235 .compare = ipmr_rule_compare,
236 .default_pref = fib_default_rule_pref,
237 .fill = ipmr_rule_fill,
238 .nlgroup = RTNLGRP_IPV4_RULE,
239 .policy = ipmr_rule_policy,
240 .owner = THIS_MODULE,
243 static int __net_init ipmr_rules_init(struct net *net)
245 struct fib_rules_ops *ops;
246 struct mr_table *mrt;
247 int err;
249 ops = fib_rules_register(&ipmr_rules_ops_template, net);
250 if (IS_ERR(ops))
251 return PTR_ERR(ops);
253 INIT_LIST_HEAD(&net->ipv4.mr_tables);
255 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
256 if (!mrt) {
257 err = -ENOMEM;
258 goto err1;
261 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
262 if (err < 0)
263 goto err2;
265 net->ipv4.mr_rules_ops = ops;
266 return 0;
268 err2:
269 ipmr_free_table(mrt);
270 err1:
271 fib_rules_unregister(ops);
272 return err;
275 static void __net_exit ipmr_rules_exit(struct net *net)
277 struct mr_table *mrt, *next;
279 rtnl_lock();
280 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
281 list_del(&mrt->list);
282 ipmr_free_table(mrt);
284 fib_rules_unregister(net->ipv4.mr_rules_ops);
285 rtnl_unlock();
287 #else
288 #define ipmr_for_each_table(mrt, net) \
289 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
291 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
293 return net->ipv4.mrt;
296 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
297 struct mr_table **mrt)
299 *mrt = net->ipv4.mrt;
300 return 0;
303 static int __net_init ipmr_rules_init(struct net *net)
305 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
306 return net->ipv4.mrt ? 0 : -ENOMEM;
309 static void __net_exit ipmr_rules_exit(struct net *net)
311 rtnl_lock();
312 ipmr_free_table(net->ipv4.mrt);
313 net->ipv4.mrt = NULL;
314 rtnl_unlock();
316 #endif
318 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
320 struct mr_table *mrt;
321 unsigned int i;
323 mrt = ipmr_get_table(net, id);
324 if (mrt)
325 return mrt;
327 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
328 if (!mrt)
329 return NULL;
330 write_pnet(&mrt->net, net);
331 mrt->id = id;
333 /* Forwarding cache */
334 for (i = 0; i < MFC_LINES; i++)
335 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
337 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
339 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
340 (unsigned long)mrt);
342 #ifdef CONFIG_IP_PIMSM
343 mrt->mroute_reg_vif_num = -1;
344 #endif
345 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
346 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
347 #endif
348 return mrt;
351 static void ipmr_free_table(struct mr_table *mrt)
353 del_timer_sync(&mrt->ipmr_expire_timer);
354 mroute_clean_tables(mrt, true);
355 kfree(mrt);
358 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
360 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
362 struct net *net = dev_net(dev);
364 dev_close(dev);
366 dev = __dev_get_by_name(net, "tunl0");
367 if (dev) {
368 const struct net_device_ops *ops = dev->netdev_ops;
369 struct ifreq ifr;
370 struct ip_tunnel_parm p;
372 memset(&p, 0, sizeof(p));
373 p.iph.daddr = v->vifc_rmt_addr.s_addr;
374 p.iph.saddr = v->vifc_lcl_addr.s_addr;
375 p.iph.version = 4;
376 p.iph.ihl = 5;
377 p.iph.protocol = IPPROTO_IPIP;
378 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
379 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
381 if (ops->ndo_do_ioctl) {
382 mm_segment_t oldfs = get_fs();
384 set_fs(KERNEL_DS);
385 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
386 set_fs(oldfs);
391 static
392 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
394 struct net_device *dev;
396 dev = __dev_get_by_name(net, "tunl0");
398 if (dev) {
399 const struct net_device_ops *ops = dev->netdev_ops;
400 int err;
401 struct ifreq ifr;
402 struct ip_tunnel_parm p;
403 struct in_device *in_dev;
405 memset(&p, 0, sizeof(p));
406 p.iph.daddr = v->vifc_rmt_addr.s_addr;
407 p.iph.saddr = v->vifc_lcl_addr.s_addr;
408 p.iph.version = 4;
409 p.iph.ihl = 5;
410 p.iph.protocol = IPPROTO_IPIP;
411 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
412 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
414 if (ops->ndo_do_ioctl) {
415 mm_segment_t oldfs = get_fs();
417 set_fs(KERNEL_DS);
418 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
419 set_fs(oldfs);
420 } else {
421 err = -EOPNOTSUPP;
423 dev = NULL;
425 if (err == 0 &&
426 (dev = __dev_get_by_name(net, p.name)) != NULL) {
427 dev->flags |= IFF_MULTICAST;
429 in_dev = __in_dev_get_rtnl(dev);
430 if (!in_dev)
431 goto failure;
433 ipv4_devconf_setall(in_dev);
434 neigh_parms_data_state_setall(in_dev->arp_parms);
435 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
437 if (dev_open(dev))
438 goto failure;
439 dev_hold(dev);
442 return dev;
444 failure:
445 /* allow the register to be completed before unregistering. */
446 rtnl_unlock();
447 rtnl_lock();
449 unregister_netdevice(dev);
450 return NULL;
453 #ifdef CONFIG_IP_PIMSM
455 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
457 struct net *net = dev_net(dev);
458 struct mr_table *mrt;
459 struct flowi4 fl4 = {
460 .flowi4_oif = dev->ifindex,
461 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
462 .flowi4_mark = skb->mark,
464 int err;
466 err = ipmr_fib_lookup(net, &fl4, &mrt);
467 if (err < 0) {
468 kfree_skb(skb);
469 return err;
472 read_lock(&mrt_lock);
473 dev->stats.tx_bytes += skb->len;
474 dev->stats.tx_packets++;
475 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
476 read_unlock(&mrt_lock);
477 kfree_skb(skb);
478 return NETDEV_TX_OK;
481 static int reg_vif_get_iflink(const struct net_device *dev)
483 return 0;
486 static const struct net_device_ops reg_vif_netdev_ops = {
487 .ndo_start_xmit = reg_vif_xmit,
488 .ndo_get_iflink = reg_vif_get_iflink,
491 static void reg_vif_setup(struct net_device *dev)
493 dev->type = ARPHRD_PIMREG;
494 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
495 dev->flags = IFF_NOARP;
496 dev->netdev_ops = &reg_vif_netdev_ops;
497 dev->destructor = free_netdev;
498 dev->features |= NETIF_F_NETNS_LOCAL;
501 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
503 struct net_device *dev;
504 struct in_device *in_dev;
505 char name[IFNAMSIZ];
507 if (mrt->id == RT_TABLE_DEFAULT)
508 sprintf(name, "pimreg");
509 else
510 sprintf(name, "pimreg%u", mrt->id);
512 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
514 if (!dev)
515 return NULL;
517 dev_net_set(dev, net);
519 if (register_netdevice(dev)) {
520 free_netdev(dev);
521 return NULL;
524 rcu_read_lock();
525 in_dev = __in_dev_get_rcu(dev);
526 if (!in_dev) {
527 rcu_read_unlock();
528 goto failure;
531 ipv4_devconf_setall(in_dev);
532 neigh_parms_data_state_setall(in_dev->arp_parms);
533 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
534 rcu_read_unlock();
536 if (dev_open(dev))
537 goto failure;
539 dev_hold(dev);
541 return dev;
543 failure:
544 /* allow the register to be completed before unregistering. */
545 rtnl_unlock();
546 rtnl_lock();
548 unregister_netdevice(dev);
549 return NULL;
551 #endif
554 * vif_delete - Delete a VIF entry
555 * @notify: Set to 1, if the caller is a notifier_call
558 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
559 struct list_head *head)
561 struct vif_device *v;
562 struct net_device *dev;
563 struct in_device *in_dev;
565 if (vifi < 0 || vifi >= mrt->maxvif)
566 return -EADDRNOTAVAIL;
568 v = &mrt->vif_table[vifi];
570 write_lock_bh(&mrt_lock);
571 dev = v->dev;
572 v->dev = NULL;
574 if (!dev) {
575 write_unlock_bh(&mrt_lock);
576 return -EADDRNOTAVAIL;
579 #ifdef CONFIG_IP_PIMSM
580 if (vifi == mrt->mroute_reg_vif_num)
581 mrt->mroute_reg_vif_num = -1;
582 #endif
584 if (vifi + 1 == mrt->maxvif) {
585 int tmp;
587 for (tmp = vifi - 1; tmp >= 0; tmp--) {
588 if (VIF_EXISTS(mrt, tmp))
589 break;
591 mrt->maxvif = tmp+1;
594 write_unlock_bh(&mrt_lock);
596 dev_set_allmulti(dev, -1);
598 in_dev = __in_dev_get_rtnl(dev);
599 if (in_dev) {
600 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
601 inet_netconf_notify_devconf(dev_net(dev),
602 NETCONFA_MC_FORWARDING,
603 dev->ifindex, &in_dev->cnf);
604 ip_rt_multicast_event(in_dev);
607 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
608 unregister_netdevice_queue(dev, head);
610 dev_put(dev);
611 return 0;
614 static void ipmr_cache_free_rcu(struct rcu_head *head)
616 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
618 kmem_cache_free(mrt_cachep, c);
621 static inline void ipmr_cache_free(struct mfc_cache *c)
623 call_rcu(&c->rcu, ipmr_cache_free_rcu);
626 /* Destroy an unresolved cache entry, killing queued skbs
627 * and reporting error to netlink readers.
630 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
632 struct net *net = read_pnet(&mrt->net);
633 struct sk_buff *skb;
634 struct nlmsgerr *e;
636 atomic_dec(&mrt->cache_resolve_queue_len);
638 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
639 if (ip_hdr(skb)->version == 0) {
640 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
641 nlh->nlmsg_type = NLMSG_ERROR;
642 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
643 skb_trim(skb, nlh->nlmsg_len);
644 e = nlmsg_data(nlh);
645 e->error = -ETIMEDOUT;
646 memset(&e->msg, 0, sizeof(e->msg));
648 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
649 } else {
650 kfree_skb(skb);
654 ipmr_cache_free(c);
658 /* Timer process for the unresolved queue. */
660 static void ipmr_expire_process(unsigned long arg)
662 struct mr_table *mrt = (struct mr_table *)arg;
663 unsigned long now;
664 unsigned long expires;
665 struct mfc_cache *c, *next;
667 if (!spin_trylock(&mfc_unres_lock)) {
668 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
669 return;
672 if (list_empty(&mrt->mfc_unres_queue))
673 goto out;
675 now = jiffies;
676 expires = 10*HZ;
678 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
679 if (time_after(c->mfc_un.unres.expires, now)) {
680 unsigned long interval = c->mfc_un.unres.expires - now;
681 if (interval < expires)
682 expires = interval;
683 continue;
686 list_del(&c->list);
687 mroute_netlink_event(mrt, c, RTM_DELROUTE);
688 ipmr_destroy_unres(mrt, c);
691 if (!list_empty(&mrt->mfc_unres_queue))
692 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
694 out:
695 spin_unlock(&mfc_unres_lock);
698 /* Fill oifs list. It is called under write locked mrt_lock. */
700 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
701 unsigned char *ttls)
703 int vifi;
705 cache->mfc_un.res.minvif = MAXVIFS;
706 cache->mfc_un.res.maxvif = 0;
707 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
709 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
710 if (VIF_EXISTS(mrt, vifi) &&
711 ttls[vifi] && ttls[vifi] < 255) {
712 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
713 if (cache->mfc_un.res.minvif > vifi)
714 cache->mfc_un.res.minvif = vifi;
715 if (cache->mfc_un.res.maxvif <= vifi)
716 cache->mfc_un.res.maxvif = vifi + 1;
721 static int vif_add(struct net *net, struct mr_table *mrt,
722 struct vifctl *vifc, int mrtsock)
724 int vifi = vifc->vifc_vifi;
725 struct vif_device *v = &mrt->vif_table[vifi];
726 struct net_device *dev;
727 struct in_device *in_dev;
728 int err;
730 /* Is vif busy ? */
731 if (VIF_EXISTS(mrt, vifi))
732 return -EADDRINUSE;
734 switch (vifc->vifc_flags) {
735 #ifdef CONFIG_IP_PIMSM
736 case VIFF_REGISTER:
738 * Special Purpose VIF in PIM
739 * All the packets will be sent to the daemon
741 if (mrt->mroute_reg_vif_num >= 0)
742 return -EADDRINUSE;
743 dev = ipmr_reg_vif(net, mrt);
744 if (!dev)
745 return -ENOBUFS;
746 err = dev_set_allmulti(dev, 1);
747 if (err) {
748 unregister_netdevice(dev);
749 dev_put(dev);
750 return err;
752 break;
753 #endif
754 case VIFF_TUNNEL:
755 dev = ipmr_new_tunnel(net, vifc);
756 if (!dev)
757 return -ENOBUFS;
758 err = dev_set_allmulti(dev, 1);
759 if (err) {
760 ipmr_del_tunnel(dev, vifc);
761 dev_put(dev);
762 return err;
764 break;
766 case VIFF_USE_IFINDEX:
767 case 0:
768 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
769 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
770 if (dev && !__in_dev_get_rtnl(dev)) {
771 dev_put(dev);
772 return -EADDRNOTAVAIL;
774 } else {
775 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
777 if (!dev)
778 return -EADDRNOTAVAIL;
779 err = dev_set_allmulti(dev, 1);
780 if (err) {
781 dev_put(dev);
782 return err;
784 break;
785 default:
786 return -EINVAL;
789 in_dev = __in_dev_get_rtnl(dev);
790 if (!in_dev) {
791 dev_put(dev);
792 return -EADDRNOTAVAIL;
794 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
795 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
796 &in_dev->cnf);
797 ip_rt_multicast_event(in_dev);
799 /* Fill in the VIF structures */
801 v->rate_limit = vifc->vifc_rate_limit;
802 v->local = vifc->vifc_lcl_addr.s_addr;
803 v->remote = vifc->vifc_rmt_addr.s_addr;
804 v->flags = vifc->vifc_flags;
805 if (!mrtsock)
806 v->flags |= VIFF_STATIC;
807 v->threshold = vifc->vifc_threshold;
808 v->bytes_in = 0;
809 v->bytes_out = 0;
810 v->pkt_in = 0;
811 v->pkt_out = 0;
812 v->link = dev->ifindex;
813 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
814 v->link = dev_get_iflink(dev);
816 /* And finish update writing critical data */
817 write_lock_bh(&mrt_lock);
818 v->dev = dev;
819 #ifdef CONFIG_IP_PIMSM
820 if (v->flags & VIFF_REGISTER)
821 mrt->mroute_reg_vif_num = vifi;
822 #endif
823 if (vifi+1 > mrt->maxvif)
824 mrt->maxvif = vifi+1;
825 write_unlock_bh(&mrt_lock);
826 return 0;
829 /* called with rcu_read_lock() */
830 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
831 __be32 origin,
832 __be32 mcastgrp)
834 int line = MFC_HASH(mcastgrp, origin);
835 struct mfc_cache *c;
837 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
838 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
839 return c;
841 return NULL;
844 /* Look for a (*,*,oif) entry */
845 static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
846 int vifi)
848 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
849 struct mfc_cache *c;
851 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
852 if (c->mfc_origin == htonl(INADDR_ANY) &&
853 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
854 c->mfc_un.res.ttls[vifi] < 255)
855 return c;
857 return NULL;
860 /* Look for a (*,G) entry */
861 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
862 __be32 mcastgrp, int vifi)
864 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
865 struct mfc_cache *c, *proxy;
867 if (mcastgrp == htonl(INADDR_ANY))
868 goto skip;
870 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
871 if (c->mfc_origin == htonl(INADDR_ANY) &&
872 c->mfc_mcastgrp == mcastgrp) {
873 if (c->mfc_un.res.ttls[vifi] < 255)
874 return c;
876 /* It's ok if the vifi is part of the static tree */
877 proxy = ipmr_cache_find_any_parent(mrt,
878 c->mfc_parent);
879 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
880 return c;
883 skip:
884 return ipmr_cache_find_any_parent(mrt, vifi);
888 * Allocate a multicast cache entry
890 static struct mfc_cache *ipmr_cache_alloc(void)
892 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
894 if (c)
895 c->mfc_un.res.minvif = MAXVIFS;
896 return c;
899 static struct mfc_cache *ipmr_cache_alloc_unres(void)
901 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
903 if (c) {
904 skb_queue_head_init(&c->mfc_un.unres.unresolved);
905 c->mfc_un.unres.expires = jiffies + 10*HZ;
907 return c;
911 * A cache entry has gone into a resolved state from queued
914 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
915 struct mfc_cache *uc, struct mfc_cache *c)
917 struct sk_buff *skb;
918 struct nlmsgerr *e;
920 /* Play the pending entries through our router */
922 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
923 if (ip_hdr(skb)->version == 0) {
924 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
926 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
927 nlh->nlmsg_len = skb_tail_pointer(skb) -
928 (u8 *)nlh;
929 } else {
930 nlh->nlmsg_type = NLMSG_ERROR;
931 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
932 skb_trim(skb, nlh->nlmsg_len);
933 e = nlmsg_data(nlh);
934 e->error = -EMSGSIZE;
935 memset(&e->msg, 0, sizeof(e->msg));
938 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
939 } else {
940 ip_mr_forward(net, mrt, skb, c, 0);
946 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
947 * expects the following bizarre scheme.
949 * Called under mrt_lock.
952 static int ipmr_cache_report(struct mr_table *mrt,
953 struct sk_buff *pkt, vifi_t vifi, int assert)
955 struct sk_buff *skb;
956 const int ihl = ip_hdrlen(pkt);
957 struct igmphdr *igmp;
958 struct igmpmsg *msg;
959 struct sock *mroute_sk;
960 int ret;
962 #ifdef CONFIG_IP_PIMSM
963 if (assert == IGMPMSG_WHOLEPKT)
964 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
965 else
966 #endif
967 skb = alloc_skb(128, GFP_ATOMIC);
969 if (!skb)
970 return -ENOBUFS;
972 #ifdef CONFIG_IP_PIMSM
973 if (assert == IGMPMSG_WHOLEPKT) {
974 /* Ugly, but we have no choice with this interface.
975 * Duplicate old header, fix ihl, length etc.
976 * And all this only to mangle msg->im_msgtype and
977 * to set msg->im_mbz to "mbz" :-)
979 skb_push(skb, sizeof(struct iphdr));
980 skb_reset_network_header(skb);
981 skb_reset_transport_header(skb);
982 msg = (struct igmpmsg *)skb_network_header(skb);
983 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
984 msg->im_msgtype = IGMPMSG_WHOLEPKT;
985 msg->im_mbz = 0;
986 msg->im_vif = mrt->mroute_reg_vif_num;
987 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
988 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
989 sizeof(struct iphdr));
990 } else
991 #endif
994 /* Copy the IP header */
996 skb_set_network_header(skb, skb->len);
997 skb_put(skb, ihl);
998 skb_copy_to_linear_data(skb, pkt->data, ihl);
999 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
1000 msg = (struct igmpmsg *)skb_network_header(skb);
1001 msg->im_vif = vifi;
1002 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1004 /* Add our header */
1006 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1007 igmp->type =
1008 msg->im_msgtype = assert;
1009 igmp->code = 0;
1010 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1011 skb->transport_header = skb->network_header;
1014 rcu_read_lock();
1015 mroute_sk = rcu_dereference(mrt->mroute_sk);
1016 if (!mroute_sk) {
1017 rcu_read_unlock();
1018 kfree_skb(skb);
1019 return -EINVAL;
1022 /* Deliver to mrouted */
1024 ret = sock_queue_rcv_skb(mroute_sk, skb);
1025 rcu_read_unlock();
1026 if (ret < 0) {
1027 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1028 kfree_skb(skb);
1031 return ret;
1035 * Queue a packet for resolution. It gets locked cache entry!
1038 static int
1039 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
1041 bool found = false;
1042 int err;
1043 struct mfc_cache *c;
1044 const struct iphdr *iph = ip_hdr(skb);
1046 spin_lock_bh(&mfc_unres_lock);
1047 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
1048 if (c->mfc_mcastgrp == iph->daddr &&
1049 c->mfc_origin == iph->saddr) {
1050 found = true;
1051 break;
1055 if (!found) {
1056 /* Create a new entry if allowable */
1058 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1059 (c = ipmr_cache_alloc_unres()) == NULL) {
1060 spin_unlock_bh(&mfc_unres_lock);
1062 kfree_skb(skb);
1063 return -ENOBUFS;
1066 /* Fill in the new cache entry */
1068 c->mfc_parent = -1;
1069 c->mfc_origin = iph->saddr;
1070 c->mfc_mcastgrp = iph->daddr;
1072 /* Reflect first query at mrouted. */
1074 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1075 if (err < 0) {
1076 /* If the report failed throw the cache entry
1077 out - Brad Parker
1079 spin_unlock_bh(&mfc_unres_lock);
1081 ipmr_cache_free(c);
1082 kfree_skb(skb);
1083 return err;
1086 atomic_inc(&mrt->cache_resolve_queue_len);
1087 list_add(&c->list, &mrt->mfc_unres_queue);
1088 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1090 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1091 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1094 /* See if we can append the packet */
1096 if (c->mfc_un.unres.unresolved.qlen > 3) {
1097 kfree_skb(skb);
1098 err = -ENOBUFS;
1099 } else {
1100 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1101 err = 0;
1104 spin_unlock_bh(&mfc_unres_lock);
1105 return err;
1109 * MFC cache manipulation by user space mroute daemon
1112 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1114 int line;
1115 struct mfc_cache *c, *next;
1117 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1119 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1120 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1121 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1122 (parent == -1 || parent == c->mfc_parent)) {
1123 list_del_rcu(&c->list);
1124 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1125 ipmr_cache_free(c);
1126 return 0;
1129 return -ENOENT;
1132 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1133 struct mfcctl *mfc, int mrtsock, int parent)
1135 bool found = false;
1136 int line;
1137 struct mfc_cache *uc, *c;
1139 if (mfc->mfcc_parent >= MAXVIFS)
1140 return -ENFILE;
1142 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1144 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1145 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1146 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1147 (parent == -1 || parent == c->mfc_parent)) {
1148 found = true;
1149 break;
1153 if (found) {
1154 write_lock_bh(&mrt_lock);
1155 c->mfc_parent = mfc->mfcc_parent;
1156 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1157 if (!mrtsock)
1158 c->mfc_flags |= MFC_STATIC;
1159 write_unlock_bh(&mrt_lock);
1160 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1161 return 0;
1164 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1165 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1166 return -EINVAL;
1168 c = ipmr_cache_alloc();
1169 if (!c)
1170 return -ENOMEM;
1172 c->mfc_origin = mfc->mfcc_origin.s_addr;
1173 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1174 c->mfc_parent = mfc->mfcc_parent;
1175 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1176 if (!mrtsock)
1177 c->mfc_flags |= MFC_STATIC;
1179 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1182 * Check to see if we resolved a queued list. If so we
1183 * need to send on the frames and tidy up.
1185 found = false;
1186 spin_lock_bh(&mfc_unres_lock);
1187 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1188 if (uc->mfc_origin == c->mfc_origin &&
1189 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1190 list_del(&uc->list);
1191 atomic_dec(&mrt->cache_resolve_queue_len);
1192 found = true;
1193 break;
1196 if (list_empty(&mrt->mfc_unres_queue))
1197 del_timer(&mrt->ipmr_expire_timer);
1198 spin_unlock_bh(&mfc_unres_lock);
1200 if (found) {
1201 ipmr_cache_resolve(net, mrt, uc, c);
1202 ipmr_cache_free(uc);
1204 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1205 return 0;
1209 * Close the multicast socket, and clear the vif tables etc
1212 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1214 int i;
1215 LIST_HEAD(list);
1216 struct mfc_cache *c, *next;
1218 /* Shut down all active vif entries */
1220 for (i = 0; i < mrt->maxvif; i++) {
1221 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1222 continue;
1223 vif_delete(mrt, i, 0, &list);
1225 unregister_netdevice_many(&list);
1227 /* Wipe the cache */
1229 for (i = 0; i < MFC_LINES; i++) {
1230 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1231 if (!all && (c->mfc_flags & MFC_STATIC))
1232 continue;
1233 list_del_rcu(&c->list);
1234 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1235 ipmr_cache_free(c);
1239 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1240 spin_lock_bh(&mfc_unres_lock);
1241 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1242 list_del(&c->list);
1243 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1244 ipmr_destroy_unres(mrt, c);
1246 spin_unlock_bh(&mfc_unres_lock);
1250 /* called from ip_ra_control(), before an RCU grace period,
1251 * we dont need to call synchronize_rcu() here
1253 static void mrtsock_destruct(struct sock *sk)
1255 struct net *net = sock_net(sk);
1256 struct mr_table *mrt;
1258 rtnl_lock();
1259 ipmr_for_each_table(mrt, net) {
1260 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1261 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1262 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1263 NETCONFA_IFINDEX_ALL,
1264 net->ipv4.devconf_all);
1265 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1266 mroute_clean_tables(mrt, false);
1269 rtnl_unlock();
1273 * Socket options and virtual interface manipulation. The whole
1274 * virtual interface system is a complete heap, but unfortunately
1275 * that's how BSD mrouted happens to think. Maybe one day with a proper
1276 * MOSPF/PIM router set up we can clean this up.
1279 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1281 int ret, parent = 0;
1282 struct vifctl vif;
1283 struct mfcctl mfc;
1284 struct net *net = sock_net(sk);
1285 struct mr_table *mrt;
1287 if (sk->sk_type != SOCK_RAW ||
1288 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1289 return -EOPNOTSUPP;
1291 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1292 if (!mrt)
1293 return -ENOENT;
1295 if (optname != MRT_INIT) {
1296 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1297 !ns_capable(net->user_ns, CAP_NET_ADMIN))
1298 return -EACCES;
1301 switch (optname) {
1302 case MRT_INIT:
1303 if (optlen != sizeof(int))
1304 return -EINVAL;
1306 rtnl_lock();
1307 if (rtnl_dereference(mrt->mroute_sk)) {
1308 rtnl_unlock();
1309 return -EADDRINUSE;
1312 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1313 if (ret == 0) {
1314 rcu_assign_pointer(mrt->mroute_sk, sk);
1315 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1316 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1317 NETCONFA_IFINDEX_ALL,
1318 net->ipv4.devconf_all);
1320 rtnl_unlock();
1321 return ret;
1322 case MRT_DONE:
1323 if (sk != rcu_access_pointer(mrt->mroute_sk))
1324 return -EACCES;
1325 return ip_ra_control(sk, 0, NULL);
1326 case MRT_ADD_VIF:
1327 case MRT_DEL_VIF:
1328 if (optlen != sizeof(vif))
1329 return -EINVAL;
1330 if (copy_from_user(&vif, optval, sizeof(vif)))
1331 return -EFAULT;
1332 if (vif.vifc_vifi >= MAXVIFS)
1333 return -ENFILE;
1334 rtnl_lock();
1335 if (optname == MRT_ADD_VIF) {
1336 ret = vif_add(net, mrt, &vif,
1337 sk == rtnl_dereference(mrt->mroute_sk));
1338 } else {
1339 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1341 rtnl_unlock();
1342 return ret;
1345 * Manipulate the forwarding caches. These live
1346 * in a sort of kernel/user symbiosis.
1348 case MRT_ADD_MFC:
1349 case MRT_DEL_MFC:
1350 parent = -1;
1351 case MRT_ADD_MFC_PROXY:
1352 case MRT_DEL_MFC_PROXY:
1353 if (optlen != sizeof(mfc))
1354 return -EINVAL;
1355 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1356 return -EFAULT;
1357 if (parent == 0)
1358 parent = mfc.mfcc_parent;
1359 rtnl_lock();
1360 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1361 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1362 else
1363 ret = ipmr_mfc_add(net, mrt, &mfc,
1364 sk == rtnl_dereference(mrt->mroute_sk),
1365 parent);
1366 rtnl_unlock();
1367 return ret;
1369 * Control PIM assert.
1371 case MRT_ASSERT:
1373 int v;
1374 if (optlen != sizeof(v))
1375 return -EINVAL;
1376 if (get_user(v, (int __user *)optval))
1377 return -EFAULT;
1378 mrt->mroute_do_assert = v;
1379 return 0;
1381 #ifdef CONFIG_IP_PIMSM
1382 case MRT_PIM:
1384 int v;
1386 if (optlen != sizeof(v))
1387 return -EINVAL;
1388 if (get_user(v, (int __user *)optval))
1389 return -EFAULT;
1390 v = !!v;
1392 rtnl_lock();
1393 ret = 0;
1394 if (v != mrt->mroute_do_pim) {
1395 mrt->mroute_do_pim = v;
1396 mrt->mroute_do_assert = v;
1398 rtnl_unlock();
1399 return ret;
1401 #endif
1402 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1403 case MRT_TABLE:
1405 u32 v;
1407 if (optlen != sizeof(u32))
1408 return -EINVAL;
1409 if (get_user(v, (u32 __user *)optval))
1410 return -EFAULT;
1412 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1413 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1414 return -EINVAL;
1416 rtnl_lock();
1417 ret = 0;
1418 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1419 ret = -EBUSY;
1420 } else {
1421 if (!ipmr_new_table(net, v))
1422 ret = -ENOMEM;
1423 else
1424 raw_sk(sk)->ipmr_table = v;
1426 rtnl_unlock();
1427 return ret;
1429 #endif
1431 * Spurious command, or MRT_VERSION which you cannot
1432 * set.
1434 default:
1435 return -ENOPROTOOPT;
1440 * Getsock opt support for the multicast routing system.
1443 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1445 int olr;
1446 int val;
1447 struct net *net = sock_net(sk);
1448 struct mr_table *mrt;
1450 if (sk->sk_type != SOCK_RAW ||
1451 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1452 return -EOPNOTSUPP;
1454 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1455 if (!mrt)
1456 return -ENOENT;
1458 if (optname != MRT_VERSION &&
1459 #ifdef CONFIG_IP_PIMSM
1460 optname != MRT_PIM &&
1461 #endif
1462 optname != MRT_ASSERT)
1463 return -ENOPROTOOPT;
1465 if (get_user(olr, optlen))
1466 return -EFAULT;
1468 olr = min_t(unsigned int, olr, sizeof(int));
1469 if (olr < 0)
1470 return -EINVAL;
1472 if (put_user(olr, optlen))
1473 return -EFAULT;
1474 if (optname == MRT_VERSION)
1475 val = 0x0305;
1476 #ifdef CONFIG_IP_PIMSM
1477 else if (optname == MRT_PIM)
1478 val = mrt->mroute_do_pim;
1479 #endif
1480 else
1481 val = mrt->mroute_do_assert;
1482 if (copy_to_user(optval, &val, olr))
1483 return -EFAULT;
1484 return 0;
1488 * The IP multicast ioctl support routines.
1491 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1493 struct sioc_sg_req sr;
1494 struct sioc_vif_req vr;
1495 struct vif_device *vif;
1496 struct mfc_cache *c;
1497 struct net *net = sock_net(sk);
1498 struct mr_table *mrt;
1500 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1501 if (!mrt)
1502 return -ENOENT;
1504 switch (cmd) {
1505 case SIOCGETVIFCNT:
1506 if (copy_from_user(&vr, arg, sizeof(vr)))
1507 return -EFAULT;
1508 if (vr.vifi >= mrt->maxvif)
1509 return -EINVAL;
1510 read_lock(&mrt_lock);
1511 vif = &mrt->vif_table[vr.vifi];
1512 if (VIF_EXISTS(mrt, vr.vifi)) {
1513 vr.icount = vif->pkt_in;
1514 vr.ocount = vif->pkt_out;
1515 vr.ibytes = vif->bytes_in;
1516 vr.obytes = vif->bytes_out;
1517 read_unlock(&mrt_lock);
1519 if (copy_to_user(arg, &vr, sizeof(vr)))
1520 return -EFAULT;
1521 return 0;
1523 read_unlock(&mrt_lock);
1524 return -EADDRNOTAVAIL;
1525 case SIOCGETSGCNT:
1526 if (copy_from_user(&sr, arg, sizeof(sr)))
1527 return -EFAULT;
1529 rcu_read_lock();
1530 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1531 if (c) {
1532 sr.pktcnt = c->mfc_un.res.pkt;
1533 sr.bytecnt = c->mfc_un.res.bytes;
1534 sr.wrong_if = c->mfc_un.res.wrong_if;
1535 rcu_read_unlock();
1537 if (copy_to_user(arg, &sr, sizeof(sr)))
1538 return -EFAULT;
1539 return 0;
1541 rcu_read_unlock();
1542 return -EADDRNOTAVAIL;
1543 default:
1544 return -ENOIOCTLCMD;
1548 #ifdef CONFIG_COMPAT
1549 struct compat_sioc_sg_req {
1550 struct in_addr src;
1551 struct in_addr grp;
1552 compat_ulong_t pktcnt;
1553 compat_ulong_t bytecnt;
1554 compat_ulong_t wrong_if;
1557 struct compat_sioc_vif_req {
1558 vifi_t vifi; /* Which iface */
1559 compat_ulong_t icount;
1560 compat_ulong_t ocount;
1561 compat_ulong_t ibytes;
1562 compat_ulong_t obytes;
1565 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1567 struct compat_sioc_sg_req sr;
1568 struct compat_sioc_vif_req vr;
1569 struct vif_device *vif;
1570 struct mfc_cache *c;
1571 struct net *net = sock_net(sk);
1572 struct mr_table *mrt;
1574 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1575 if (!mrt)
1576 return -ENOENT;
1578 switch (cmd) {
1579 case SIOCGETVIFCNT:
1580 if (copy_from_user(&vr, arg, sizeof(vr)))
1581 return -EFAULT;
1582 if (vr.vifi >= mrt->maxvif)
1583 return -EINVAL;
1584 read_lock(&mrt_lock);
1585 vif = &mrt->vif_table[vr.vifi];
1586 if (VIF_EXISTS(mrt, vr.vifi)) {
1587 vr.icount = vif->pkt_in;
1588 vr.ocount = vif->pkt_out;
1589 vr.ibytes = vif->bytes_in;
1590 vr.obytes = vif->bytes_out;
1591 read_unlock(&mrt_lock);
1593 if (copy_to_user(arg, &vr, sizeof(vr)))
1594 return -EFAULT;
1595 return 0;
1597 read_unlock(&mrt_lock);
1598 return -EADDRNOTAVAIL;
1599 case SIOCGETSGCNT:
1600 if (copy_from_user(&sr, arg, sizeof(sr)))
1601 return -EFAULT;
1603 rcu_read_lock();
1604 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1605 if (c) {
1606 sr.pktcnt = c->mfc_un.res.pkt;
1607 sr.bytecnt = c->mfc_un.res.bytes;
1608 sr.wrong_if = c->mfc_un.res.wrong_if;
1609 rcu_read_unlock();
1611 if (copy_to_user(arg, &sr, sizeof(sr)))
1612 return -EFAULT;
1613 return 0;
1615 rcu_read_unlock();
1616 return -EADDRNOTAVAIL;
1617 default:
1618 return -ENOIOCTLCMD;
1621 #endif
1624 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1626 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1627 struct net *net = dev_net(dev);
1628 struct mr_table *mrt;
1629 struct vif_device *v;
1630 int ct;
1632 if (event != NETDEV_UNREGISTER)
1633 return NOTIFY_DONE;
1635 ipmr_for_each_table(mrt, net) {
1636 v = &mrt->vif_table[0];
1637 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1638 if (v->dev == dev)
1639 vif_delete(mrt, ct, 1, NULL);
1642 return NOTIFY_DONE;
1646 static struct notifier_block ip_mr_notifier = {
1647 .notifier_call = ipmr_device_event,
1651 * Encapsulate a packet by attaching a valid IPIP header to it.
1652 * This avoids tunnel drivers and other mess and gives us the speed so
1653 * important for multicast video.
1656 static void ip_encap(struct net *net, struct sk_buff *skb,
1657 __be32 saddr, __be32 daddr)
1659 struct iphdr *iph;
1660 const struct iphdr *old_iph = ip_hdr(skb);
1662 skb_push(skb, sizeof(struct iphdr));
1663 skb->transport_header = skb->network_header;
1664 skb_reset_network_header(skb);
1665 iph = ip_hdr(skb);
1667 iph->version = 4;
1668 iph->tos = old_iph->tos;
1669 iph->ttl = old_iph->ttl;
1670 iph->frag_off = 0;
1671 iph->daddr = daddr;
1672 iph->saddr = saddr;
1673 iph->protocol = IPPROTO_IPIP;
1674 iph->ihl = 5;
1675 iph->tot_len = htons(skb->len);
1676 ip_select_ident(net, skb, NULL);
1677 ip_send_check(iph);
1679 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1680 nf_reset(skb);
1683 static inline int ipmr_forward_finish(struct sock *sk, struct sk_buff *skb)
1685 struct ip_options *opt = &(IPCB(skb)->opt);
1687 IP_INC_STATS(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1688 IP_ADD_STATS(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
1690 if (unlikely(opt->optlen))
1691 ip_forward_options(skb);
1693 return dst_output_sk(sk, skb);
1697 * Processing handlers for ipmr_forward
1700 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1701 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1703 const struct iphdr *iph = ip_hdr(skb);
1704 struct vif_device *vif = &mrt->vif_table[vifi];
1705 struct net_device *dev;
1706 struct rtable *rt;
1707 struct flowi4 fl4;
1708 int encap = 0;
1710 if (!vif->dev)
1711 goto out_free;
1713 #ifdef CONFIG_IP_PIMSM
1714 if (vif->flags & VIFF_REGISTER) {
1715 vif->pkt_out++;
1716 vif->bytes_out += skb->len;
1717 vif->dev->stats.tx_bytes += skb->len;
1718 vif->dev->stats.tx_packets++;
1719 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1720 goto out_free;
1722 #endif
1724 if (vif->flags & VIFF_TUNNEL) {
1725 rt = ip_route_output_ports(net, &fl4, NULL,
1726 vif->remote, vif->local,
1727 0, 0,
1728 IPPROTO_IPIP,
1729 RT_TOS(iph->tos), vif->link);
1730 if (IS_ERR(rt))
1731 goto out_free;
1732 encap = sizeof(struct iphdr);
1733 } else {
1734 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1735 0, 0,
1736 IPPROTO_IPIP,
1737 RT_TOS(iph->tos), vif->link);
1738 if (IS_ERR(rt))
1739 goto out_free;
1742 dev = rt->dst.dev;
1744 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1745 /* Do not fragment multicasts. Alas, IPv4 does not
1746 * allow to send ICMP, so that packets will disappear
1747 * to blackhole.
1750 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1751 ip_rt_put(rt);
1752 goto out_free;
1755 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1757 if (skb_cow(skb, encap)) {
1758 ip_rt_put(rt);
1759 goto out_free;
1762 vif->pkt_out++;
1763 vif->bytes_out += skb->len;
1765 skb_dst_drop(skb);
1766 skb_dst_set(skb, &rt->dst);
1767 ip_decrease_ttl(ip_hdr(skb));
1769 /* FIXME: forward and output firewalls used to be called here.
1770 * What do we do with netfilter? -- RR
1772 if (vif->flags & VIFF_TUNNEL) {
1773 ip_encap(net, skb, vif->local, vif->remote);
1774 /* FIXME: extra output firewall step used to be here. --RR */
1775 vif->dev->stats.tx_packets++;
1776 vif->dev->stats.tx_bytes += skb->len;
1779 IPCB(skb)->flags |= IPSKB_FORWARDED;
1782 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1783 * not only before forwarding, but after forwarding on all output
1784 * interfaces. It is clear, if mrouter runs a multicasting
1785 * program, it should receive packets not depending to what interface
1786 * program is joined.
1787 * If we will not make it, the program will have to join on all
1788 * interfaces. On the other hand, multihoming host (or router, but
1789 * not mrouter) cannot join to more than one interface - it will
1790 * result in receiving multiple packets.
1792 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, NULL, skb,
1793 skb->dev, dev,
1794 ipmr_forward_finish);
1795 return;
1797 out_free:
1798 kfree_skb(skb);
1801 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1803 int ct;
1805 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1806 if (mrt->vif_table[ct].dev == dev)
1807 break;
1809 return ct;
1812 /* "local" means that we should preserve one skb (for local delivery) */
1814 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1815 struct sk_buff *skb, struct mfc_cache *cache,
1816 int local)
1818 int psend = -1;
1819 int vif, ct;
1820 int true_vifi = ipmr_find_vif(mrt, skb->dev);
1822 vif = cache->mfc_parent;
1823 cache->mfc_un.res.pkt++;
1824 cache->mfc_un.res.bytes += skb->len;
1826 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1827 struct mfc_cache *cache_proxy;
1829 /* For an (*,G) entry, we only check that the incomming
1830 * interface is part of the static tree.
1832 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1833 if (cache_proxy &&
1834 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1835 goto forward;
1839 * Wrong interface: drop packet and (maybe) send PIM assert.
1841 if (mrt->vif_table[vif].dev != skb->dev) {
1842 if (rt_is_output_route(skb_rtable(skb))) {
1843 /* It is our own packet, looped back.
1844 * Very complicated situation...
1846 * The best workaround until routing daemons will be
1847 * fixed is not to redistribute packet, if it was
1848 * send through wrong interface. It means, that
1849 * multicast applications WILL NOT work for
1850 * (S,G), which have default multicast route pointing
1851 * to wrong oif. In any case, it is not a good
1852 * idea to use multicasting applications on router.
1854 goto dont_forward;
1857 cache->mfc_un.res.wrong_if++;
1859 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1860 /* pimsm uses asserts, when switching from RPT to SPT,
1861 * so that we cannot check that packet arrived on an oif.
1862 * It is bad, but otherwise we would need to move pretty
1863 * large chunk of pimd to kernel. Ough... --ANK
1865 (mrt->mroute_do_pim ||
1866 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1867 time_after(jiffies,
1868 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1869 cache->mfc_un.res.last_assert = jiffies;
1870 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1872 goto dont_forward;
1875 forward:
1876 mrt->vif_table[vif].pkt_in++;
1877 mrt->vif_table[vif].bytes_in += skb->len;
1880 * Forward the frame
1882 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1883 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
1884 if (true_vifi >= 0 &&
1885 true_vifi != cache->mfc_parent &&
1886 ip_hdr(skb)->ttl >
1887 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1888 /* It's an (*,*) entry and the packet is not coming from
1889 * the upstream: forward the packet to the upstream
1890 * only.
1892 psend = cache->mfc_parent;
1893 goto last_forward;
1895 goto dont_forward;
1897 for (ct = cache->mfc_un.res.maxvif - 1;
1898 ct >= cache->mfc_un.res.minvif; ct--) {
1899 /* For (*,G) entry, don't forward to the incoming interface */
1900 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1901 ct != true_vifi) &&
1902 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1903 if (psend != -1) {
1904 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1906 if (skb2)
1907 ipmr_queue_xmit(net, mrt, skb2, cache,
1908 psend);
1910 psend = ct;
1913 last_forward:
1914 if (psend != -1) {
1915 if (local) {
1916 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1918 if (skb2)
1919 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1920 } else {
1921 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1922 return;
1926 dont_forward:
1927 if (!local)
1928 kfree_skb(skb);
1931 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1933 struct rtable *rt = skb_rtable(skb);
1934 struct iphdr *iph = ip_hdr(skb);
1935 struct flowi4 fl4 = {
1936 .daddr = iph->daddr,
1937 .saddr = iph->saddr,
1938 .flowi4_tos = RT_TOS(iph->tos),
1939 .flowi4_oif = (rt_is_output_route(rt) ?
1940 skb->dev->ifindex : 0),
1941 .flowi4_iif = (rt_is_output_route(rt) ?
1942 LOOPBACK_IFINDEX :
1943 skb->dev->ifindex),
1944 .flowi4_mark = skb->mark,
1946 struct mr_table *mrt;
1947 int err;
1949 err = ipmr_fib_lookup(net, &fl4, &mrt);
1950 if (err)
1951 return ERR_PTR(err);
1952 return mrt;
1956 * Multicast packets for forwarding arrive here
1957 * Called with rcu_read_lock();
1960 int ip_mr_input(struct sk_buff *skb)
1962 struct mfc_cache *cache;
1963 struct net *net = dev_net(skb->dev);
1964 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1965 struct mr_table *mrt;
1967 /* Packet is looped back after forward, it should not be
1968 * forwarded second time, but still can be delivered locally.
1970 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1971 goto dont_forward;
1973 mrt = ipmr_rt_fib_lookup(net, skb);
1974 if (IS_ERR(mrt)) {
1975 kfree_skb(skb);
1976 return PTR_ERR(mrt);
1978 if (!local) {
1979 if (IPCB(skb)->opt.router_alert) {
1980 if (ip_call_ra_chain(skb))
1981 return 0;
1982 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1983 /* IGMPv1 (and broken IGMPv2 implementations sort of
1984 * Cisco IOS <= 11.2(8)) do not put router alert
1985 * option to IGMP packets destined to routable
1986 * groups. It is very bad, because it means
1987 * that we can forward NO IGMP messages.
1989 struct sock *mroute_sk;
1991 mroute_sk = rcu_dereference(mrt->mroute_sk);
1992 if (mroute_sk) {
1993 nf_reset(skb);
1994 raw_rcv(mroute_sk, skb);
1995 return 0;
2000 /* already under rcu_read_lock() */
2001 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2002 if (!cache) {
2003 int vif = ipmr_find_vif(mrt, skb->dev);
2005 if (vif >= 0)
2006 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2007 vif);
2011 * No usable cache entry
2013 if (!cache) {
2014 int vif;
2016 if (local) {
2017 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2018 ip_local_deliver(skb);
2019 if (!skb2)
2020 return -ENOBUFS;
2021 skb = skb2;
2024 read_lock(&mrt_lock);
2025 vif = ipmr_find_vif(mrt, skb->dev);
2026 if (vif >= 0) {
2027 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
2028 read_unlock(&mrt_lock);
2030 return err2;
2032 read_unlock(&mrt_lock);
2033 kfree_skb(skb);
2034 return -ENODEV;
2037 read_lock(&mrt_lock);
2038 ip_mr_forward(net, mrt, skb, cache, local);
2039 read_unlock(&mrt_lock);
2041 if (local)
2042 return ip_local_deliver(skb);
2044 return 0;
2046 dont_forward:
2047 if (local)
2048 return ip_local_deliver(skb);
2049 kfree_skb(skb);
2050 return 0;
2053 #ifdef CONFIG_IP_PIMSM
2054 /* called with rcu_read_lock() */
2055 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2056 unsigned int pimlen)
2058 struct net_device *reg_dev = NULL;
2059 struct iphdr *encap;
2061 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
2063 * Check that:
2064 * a. packet is really sent to a multicast group
2065 * b. packet is not a NULL-REGISTER
2066 * c. packet is not truncated
2068 if (!ipv4_is_multicast(encap->daddr) ||
2069 encap->tot_len == 0 ||
2070 ntohs(encap->tot_len) + pimlen > skb->len)
2071 return 1;
2073 read_lock(&mrt_lock);
2074 if (mrt->mroute_reg_vif_num >= 0)
2075 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
2076 read_unlock(&mrt_lock);
2078 if (!reg_dev)
2079 return 1;
2081 skb->mac_header = skb->network_header;
2082 skb_pull(skb, (u8 *)encap - skb->data);
2083 skb_reset_network_header(skb);
2084 skb->protocol = htons(ETH_P_IP);
2085 skb->ip_summed = CHECKSUM_NONE;
2087 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
2089 netif_rx(skb);
2091 return NET_RX_SUCCESS;
2093 #endif
2095 #ifdef CONFIG_IP_PIMSM_V1
2097 * Handle IGMP messages of PIMv1
2100 int pim_rcv_v1(struct sk_buff *skb)
2102 struct igmphdr *pim;
2103 struct net *net = dev_net(skb->dev);
2104 struct mr_table *mrt;
2106 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2107 goto drop;
2109 pim = igmp_hdr(skb);
2111 mrt = ipmr_rt_fib_lookup(net, skb);
2112 if (IS_ERR(mrt))
2113 goto drop;
2114 if (!mrt->mroute_do_pim ||
2115 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2116 goto drop;
2118 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2119 drop:
2120 kfree_skb(skb);
2122 return 0;
2124 #endif
2126 #ifdef CONFIG_IP_PIMSM_V2
2127 static int pim_rcv(struct sk_buff *skb)
2129 struct pimreghdr *pim;
2130 struct net *net = dev_net(skb->dev);
2131 struct mr_table *mrt;
2133 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2134 goto drop;
2136 pim = (struct pimreghdr *)skb_transport_header(skb);
2137 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2138 (pim->flags & PIM_NULL_REGISTER) ||
2139 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2140 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2141 goto drop;
2143 mrt = ipmr_rt_fib_lookup(net, skb);
2144 if (IS_ERR(mrt))
2145 goto drop;
2146 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2147 drop:
2148 kfree_skb(skb);
2150 return 0;
2152 #endif
2154 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2155 struct mfc_cache *c, struct rtmsg *rtm)
2157 int ct;
2158 struct rtnexthop *nhp;
2159 struct nlattr *mp_attr;
2160 struct rta_mfc_stats mfcs;
2162 /* If cache is unresolved, don't try to parse IIF and OIF */
2163 if (c->mfc_parent >= MAXVIFS)
2164 return -ENOENT;
2166 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2167 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2168 return -EMSGSIZE;
2170 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2171 return -EMSGSIZE;
2173 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2174 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2175 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2176 nla_nest_cancel(skb, mp_attr);
2177 return -EMSGSIZE;
2180 nhp->rtnh_flags = 0;
2181 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2182 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2183 nhp->rtnh_len = sizeof(*nhp);
2187 nla_nest_end(skb, mp_attr);
2189 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2190 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2191 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2192 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2193 return -EMSGSIZE;
2195 rtm->rtm_type = RTN_MULTICAST;
2196 return 1;
2199 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2200 __be32 saddr, __be32 daddr,
2201 struct rtmsg *rtm, int nowait)
2203 struct mfc_cache *cache;
2204 struct mr_table *mrt;
2205 int err;
2207 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2208 if (!mrt)
2209 return -ENOENT;
2211 rcu_read_lock();
2212 cache = ipmr_cache_find(mrt, saddr, daddr);
2213 if (!cache && skb->dev) {
2214 int vif = ipmr_find_vif(mrt, skb->dev);
2216 if (vif >= 0)
2217 cache = ipmr_cache_find_any(mrt, daddr, vif);
2219 if (!cache) {
2220 struct sk_buff *skb2;
2221 struct iphdr *iph;
2222 struct net_device *dev;
2223 int vif = -1;
2225 if (nowait) {
2226 rcu_read_unlock();
2227 return -EAGAIN;
2230 dev = skb->dev;
2231 read_lock(&mrt_lock);
2232 if (dev)
2233 vif = ipmr_find_vif(mrt, dev);
2234 if (vif < 0) {
2235 read_unlock(&mrt_lock);
2236 rcu_read_unlock();
2237 return -ENODEV;
2239 skb2 = skb_clone(skb, GFP_ATOMIC);
2240 if (!skb2) {
2241 read_unlock(&mrt_lock);
2242 rcu_read_unlock();
2243 return -ENOMEM;
2246 skb_push(skb2, sizeof(struct iphdr));
2247 skb_reset_network_header(skb2);
2248 iph = ip_hdr(skb2);
2249 iph->ihl = sizeof(struct iphdr) >> 2;
2250 iph->saddr = saddr;
2251 iph->daddr = daddr;
2252 iph->version = 0;
2253 err = ipmr_cache_unresolved(mrt, vif, skb2);
2254 read_unlock(&mrt_lock);
2255 rcu_read_unlock();
2256 return err;
2259 read_lock(&mrt_lock);
2260 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2261 cache->mfc_flags |= MFC_NOTIFY;
2262 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2263 read_unlock(&mrt_lock);
2264 rcu_read_unlock();
2265 return err;
2268 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2269 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2270 int flags)
2272 struct nlmsghdr *nlh;
2273 struct rtmsg *rtm;
2274 int err;
2276 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2277 if (!nlh)
2278 return -EMSGSIZE;
2280 rtm = nlmsg_data(nlh);
2281 rtm->rtm_family = RTNL_FAMILY_IPMR;
2282 rtm->rtm_dst_len = 32;
2283 rtm->rtm_src_len = 32;
2284 rtm->rtm_tos = 0;
2285 rtm->rtm_table = mrt->id;
2286 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2287 goto nla_put_failure;
2288 rtm->rtm_type = RTN_MULTICAST;
2289 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2290 if (c->mfc_flags & MFC_STATIC)
2291 rtm->rtm_protocol = RTPROT_STATIC;
2292 else
2293 rtm->rtm_protocol = RTPROT_MROUTED;
2294 rtm->rtm_flags = 0;
2296 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2297 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2298 goto nla_put_failure;
2299 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2300 /* do not break the dump if cache is unresolved */
2301 if (err < 0 && err != -ENOENT)
2302 goto nla_put_failure;
2304 nlmsg_end(skb, nlh);
2305 return 0;
2307 nla_put_failure:
2308 nlmsg_cancel(skb, nlh);
2309 return -EMSGSIZE;
2312 static size_t mroute_msgsize(bool unresolved, int maxvif)
2314 size_t len =
2315 NLMSG_ALIGN(sizeof(struct rtmsg))
2316 + nla_total_size(4) /* RTA_TABLE */
2317 + nla_total_size(4) /* RTA_SRC */
2318 + nla_total_size(4) /* RTA_DST */
2321 if (!unresolved)
2322 len = len
2323 + nla_total_size(4) /* RTA_IIF */
2324 + nla_total_size(0) /* RTA_MULTIPATH */
2325 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2326 /* RTA_MFC_STATS */
2327 + nla_total_size(sizeof(struct rta_mfc_stats))
2330 return len;
2333 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2334 int cmd)
2336 struct net *net = read_pnet(&mrt->net);
2337 struct sk_buff *skb;
2338 int err = -ENOBUFS;
2340 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2341 GFP_ATOMIC);
2342 if (!skb)
2343 goto errout;
2345 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2346 if (err < 0)
2347 goto errout;
2349 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2350 return;
2352 errout:
2353 kfree_skb(skb);
2354 if (err < 0)
2355 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2358 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2360 struct net *net = sock_net(skb->sk);
2361 struct mr_table *mrt;
2362 struct mfc_cache *mfc;
2363 unsigned int t = 0, s_t;
2364 unsigned int h = 0, s_h;
2365 unsigned int e = 0, s_e;
2367 s_t = cb->args[0];
2368 s_h = cb->args[1];
2369 s_e = cb->args[2];
2371 rcu_read_lock();
2372 ipmr_for_each_table(mrt, net) {
2373 if (t < s_t)
2374 goto next_table;
2375 if (t > s_t)
2376 s_h = 0;
2377 for (h = s_h; h < MFC_LINES; h++) {
2378 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2379 if (e < s_e)
2380 goto next_entry;
2381 if (ipmr_fill_mroute(mrt, skb,
2382 NETLINK_CB(cb->skb).portid,
2383 cb->nlh->nlmsg_seq,
2384 mfc, RTM_NEWROUTE,
2385 NLM_F_MULTI) < 0)
2386 goto done;
2387 next_entry:
2388 e++;
2390 e = s_e = 0;
2392 spin_lock_bh(&mfc_unres_lock);
2393 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2394 if (e < s_e)
2395 goto next_entry2;
2396 if (ipmr_fill_mroute(mrt, skb,
2397 NETLINK_CB(cb->skb).portid,
2398 cb->nlh->nlmsg_seq,
2399 mfc, RTM_NEWROUTE,
2400 NLM_F_MULTI) < 0) {
2401 spin_unlock_bh(&mfc_unres_lock);
2402 goto done;
2404 next_entry2:
2405 e++;
2407 spin_unlock_bh(&mfc_unres_lock);
2408 e = s_e = 0;
2409 s_h = 0;
2410 next_table:
2411 t++;
2413 done:
2414 rcu_read_unlock();
2416 cb->args[2] = e;
2417 cb->args[1] = h;
2418 cb->args[0] = t;
2420 return skb->len;
2423 #ifdef CONFIG_PROC_FS
2425 * The /proc interfaces to multicast routing :
2426 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2428 struct ipmr_vif_iter {
2429 struct seq_net_private p;
2430 struct mr_table *mrt;
2431 int ct;
2434 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2435 struct ipmr_vif_iter *iter,
2436 loff_t pos)
2438 struct mr_table *mrt = iter->mrt;
2440 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2441 if (!VIF_EXISTS(mrt, iter->ct))
2442 continue;
2443 if (pos-- == 0)
2444 return &mrt->vif_table[iter->ct];
2446 return NULL;
2449 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2450 __acquires(mrt_lock)
2452 struct ipmr_vif_iter *iter = seq->private;
2453 struct net *net = seq_file_net(seq);
2454 struct mr_table *mrt;
2456 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2457 if (!mrt)
2458 return ERR_PTR(-ENOENT);
2460 iter->mrt = mrt;
2462 read_lock(&mrt_lock);
2463 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2464 : SEQ_START_TOKEN;
2467 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2469 struct ipmr_vif_iter *iter = seq->private;
2470 struct net *net = seq_file_net(seq);
2471 struct mr_table *mrt = iter->mrt;
2473 ++*pos;
2474 if (v == SEQ_START_TOKEN)
2475 return ipmr_vif_seq_idx(net, iter, 0);
2477 while (++iter->ct < mrt->maxvif) {
2478 if (!VIF_EXISTS(mrt, iter->ct))
2479 continue;
2480 return &mrt->vif_table[iter->ct];
2482 return NULL;
2485 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2486 __releases(mrt_lock)
2488 read_unlock(&mrt_lock);
2491 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2493 struct ipmr_vif_iter *iter = seq->private;
2494 struct mr_table *mrt = iter->mrt;
2496 if (v == SEQ_START_TOKEN) {
2497 seq_puts(seq,
2498 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2499 } else {
2500 const struct vif_device *vif = v;
2501 const char *name = vif->dev ? vif->dev->name : "none";
2503 seq_printf(seq,
2504 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2505 vif - mrt->vif_table,
2506 name, vif->bytes_in, vif->pkt_in,
2507 vif->bytes_out, vif->pkt_out,
2508 vif->flags, vif->local, vif->remote);
2510 return 0;
2513 static const struct seq_operations ipmr_vif_seq_ops = {
2514 .start = ipmr_vif_seq_start,
2515 .next = ipmr_vif_seq_next,
2516 .stop = ipmr_vif_seq_stop,
2517 .show = ipmr_vif_seq_show,
2520 static int ipmr_vif_open(struct inode *inode, struct file *file)
2522 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2523 sizeof(struct ipmr_vif_iter));
2526 static const struct file_operations ipmr_vif_fops = {
2527 .owner = THIS_MODULE,
2528 .open = ipmr_vif_open,
2529 .read = seq_read,
2530 .llseek = seq_lseek,
2531 .release = seq_release_net,
2534 struct ipmr_mfc_iter {
2535 struct seq_net_private p;
2536 struct mr_table *mrt;
2537 struct list_head *cache;
2538 int ct;
2542 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2543 struct ipmr_mfc_iter *it, loff_t pos)
2545 struct mr_table *mrt = it->mrt;
2546 struct mfc_cache *mfc;
2548 rcu_read_lock();
2549 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2550 it->cache = &mrt->mfc_cache_array[it->ct];
2551 list_for_each_entry_rcu(mfc, it->cache, list)
2552 if (pos-- == 0)
2553 return mfc;
2555 rcu_read_unlock();
2557 spin_lock_bh(&mfc_unres_lock);
2558 it->cache = &mrt->mfc_unres_queue;
2559 list_for_each_entry(mfc, it->cache, list)
2560 if (pos-- == 0)
2561 return mfc;
2562 spin_unlock_bh(&mfc_unres_lock);
2564 it->cache = NULL;
2565 return NULL;
2569 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2571 struct ipmr_mfc_iter *it = seq->private;
2572 struct net *net = seq_file_net(seq);
2573 struct mr_table *mrt;
2575 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2576 if (!mrt)
2577 return ERR_PTR(-ENOENT);
2579 it->mrt = mrt;
2580 it->cache = NULL;
2581 it->ct = 0;
2582 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2583 : SEQ_START_TOKEN;
2586 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2588 struct mfc_cache *mfc = v;
2589 struct ipmr_mfc_iter *it = seq->private;
2590 struct net *net = seq_file_net(seq);
2591 struct mr_table *mrt = it->mrt;
2593 ++*pos;
2595 if (v == SEQ_START_TOKEN)
2596 return ipmr_mfc_seq_idx(net, seq->private, 0);
2598 if (mfc->list.next != it->cache)
2599 return list_entry(mfc->list.next, struct mfc_cache, list);
2601 if (it->cache == &mrt->mfc_unres_queue)
2602 goto end_of_list;
2604 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2606 while (++it->ct < MFC_LINES) {
2607 it->cache = &mrt->mfc_cache_array[it->ct];
2608 if (list_empty(it->cache))
2609 continue;
2610 return list_first_entry(it->cache, struct mfc_cache, list);
2613 /* exhausted cache_array, show unresolved */
2614 rcu_read_unlock();
2615 it->cache = &mrt->mfc_unres_queue;
2616 it->ct = 0;
2618 spin_lock_bh(&mfc_unres_lock);
2619 if (!list_empty(it->cache))
2620 return list_first_entry(it->cache, struct mfc_cache, list);
2622 end_of_list:
2623 spin_unlock_bh(&mfc_unres_lock);
2624 it->cache = NULL;
2626 return NULL;
2629 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2631 struct ipmr_mfc_iter *it = seq->private;
2632 struct mr_table *mrt = it->mrt;
2634 if (it->cache == &mrt->mfc_unres_queue)
2635 spin_unlock_bh(&mfc_unres_lock);
2636 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2637 rcu_read_unlock();
2640 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2642 int n;
2644 if (v == SEQ_START_TOKEN) {
2645 seq_puts(seq,
2646 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2647 } else {
2648 const struct mfc_cache *mfc = v;
2649 const struct ipmr_mfc_iter *it = seq->private;
2650 const struct mr_table *mrt = it->mrt;
2652 seq_printf(seq, "%08X %08X %-3hd",
2653 (__force u32) mfc->mfc_mcastgrp,
2654 (__force u32) mfc->mfc_origin,
2655 mfc->mfc_parent);
2657 if (it->cache != &mrt->mfc_unres_queue) {
2658 seq_printf(seq, " %8lu %8lu %8lu",
2659 mfc->mfc_un.res.pkt,
2660 mfc->mfc_un.res.bytes,
2661 mfc->mfc_un.res.wrong_if);
2662 for (n = mfc->mfc_un.res.minvif;
2663 n < mfc->mfc_un.res.maxvif; n++) {
2664 if (VIF_EXISTS(mrt, n) &&
2665 mfc->mfc_un.res.ttls[n] < 255)
2666 seq_printf(seq,
2667 " %2d:%-3d",
2668 n, mfc->mfc_un.res.ttls[n]);
2670 } else {
2671 /* unresolved mfc_caches don't contain
2672 * pkt, bytes and wrong_if values
2674 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2676 seq_putc(seq, '\n');
2678 return 0;
2681 static const struct seq_operations ipmr_mfc_seq_ops = {
2682 .start = ipmr_mfc_seq_start,
2683 .next = ipmr_mfc_seq_next,
2684 .stop = ipmr_mfc_seq_stop,
2685 .show = ipmr_mfc_seq_show,
2688 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2690 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2691 sizeof(struct ipmr_mfc_iter));
2694 static const struct file_operations ipmr_mfc_fops = {
2695 .owner = THIS_MODULE,
2696 .open = ipmr_mfc_open,
2697 .read = seq_read,
2698 .llseek = seq_lseek,
2699 .release = seq_release_net,
2701 #endif
2703 #ifdef CONFIG_IP_PIMSM_V2
2704 static const struct net_protocol pim_protocol = {
2705 .handler = pim_rcv,
2706 .netns_ok = 1,
2708 #endif
2712 * Setup for IP multicast routing
2714 static int __net_init ipmr_net_init(struct net *net)
2716 int err;
2718 err = ipmr_rules_init(net);
2719 if (err < 0)
2720 goto fail;
2722 #ifdef CONFIG_PROC_FS
2723 err = -ENOMEM;
2724 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
2725 goto proc_vif_fail;
2726 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
2727 goto proc_cache_fail;
2728 #endif
2729 return 0;
2731 #ifdef CONFIG_PROC_FS
2732 proc_cache_fail:
2733 remove_proc_entry("ip_mr_vif", net->proc_net);
2734 proc_vif_fail:
2735 ipmr_rules_exit(net);
2736 #endif
2737 fail:
2738 return err;
2741 static void __net_exit ipmr_net_exit(struct net *net)
2743 #ifdef CONFIG_PROC_FS
2744 remove_proc_entry("ip_mr_cache", net->proc_net);
2745 remove_proc_entry("ip_mr_vif", net->proc_net);
2746 #endif
2747 ipmr_rules_exit(net);
2750 static struct pernet_operations ipmr_net_ops = {
2751 .init = ipmr_net_init,
2752 .exit = ipmr_net_exit,
2755 int __init ip_mr_init(void)
2757 int err;
2759 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2760 sizeof(struct mfc_cache),
2761 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2762 NULL);
2763 if (!mrt_cachep)
2764 return -ENOMEM;
2766 err = register_pernet_subsys(&ipmr_net_ops);
2767 if (err)
2768 goto reg_pernet_fail;
2770 err = register_netdevice_notifier(&ip_mr_notifier);
2771 if (err)
2772 goto reg_notif_fail;
2773 #ifdef CONFIG_IP_PIMSM_V2
2774 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2775 pr_err("%s: can't add PIM protocol\n", __func__);
2776 err = -EAGAIN;
2777 goto add_proto_fail;
2779 #endif
2780 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2781 NULL, ipmr_rtm_dumproute, NULL);
2782 return 0;
2784 #ifdef CONFIG_IP_PIMSM_V2
2785 add_proto_fail:
2786 unregister_netdevice_notifier(&ip_mr_notifier);
2787 #endif
2788 reg_notif_fail:
2789 unregister_pernet_subsys(&ipmr_net_ops);
2790 reg_pernet_fail:
2791 kmem_cache_destroy(mrt_cachep);
2792 return err;