ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / ipv4 / ipmr.c
blob1cb865fcc91b49fd1926e59021d7fd7a7c1c084f
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>
69 #include <linux/nospec.h>
71 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
72 #define CONFIG_IP_PIMSM 1
73 #endif
75 struct mr_table {
76 struct list_head list;
77 possible_net_t net;
78 u32 id;
79 struct sock __rcu *mroute_sk;
80 struct timer_list ipmr_expire_timer;
81 struct list_head mfc_unres_queue;
82 struct list_head mfc_cache_array[MFC_LINES];
83 struct vif_device vif_table[MAXVIFS];
84 int maxvif;
85 atomic_t cache_resolve_queue_len;
86 bool mroute_do_assert;
87 bool mroute_do_pim;
88 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
89 int mroute_reg_vif_num;
90 #endif
93 struct ipmr_rule {
94 struct fib_rule common;
97 struct ipmr_result {
98 struct mr_table *mrt;
101 /* Big lock, protecting vif table, mrt cache and mroute socket state.
102 * Note that the changes are semaphored via rtnl_lock.
105 static DEFINE_RWLOCK(mrt_lock);
108 * Multicast router control variables
111 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
113 /* Special spinlock for queue of unresolved entries */
114 static DEFINE_SPINLOCK(mfc_unres_lock);
116 /* We return to original Alan's scheme. Hash table of resolved
117 * entries is changed only in process context and protected
118 * with weak lock mrt_lock. Queue of unresolved entries is protected
119 * with strong spinlock mfc_unres_lock.
121 * In this case data path is free of exclusive locks at all.
124 static struct kmem_cache *mrt_cachep __read_mostly;
126 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
127 static void ipmr_free_table(struct mr_table *mrt);
129 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
130 struct sk_buff *skb, struct mfc_cache *cache,
131 int local);
132 static int ipmr_cache_report(struct mr_table *mrt,
133 struct sk_buff *pkt, vifi_t vifi, int assert);
134 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
135 struct mfc_cache *c, struct rtmsg *rtm);
136 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
137 int cmd);
138 static void mroute_clean_tables(struct mr_table *mrt, bool all);
139 static void ipmr_expire_process(unsigned long arg);
141 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
142 #define ipmr_for_each_table(mrt, net) \
143 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
145 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
147 struct mr_table *mrt;
149 ipmr_for_each_table(mrt, net) {
150 if (mrt->id == id)
151 return mrt;
153 return NULL;
156 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
157 struct mr_table **mrt)
159 int err;
160 struct ipmr_result res;
161 struct fib_lookup_arg arg = {
162 .result = &res,
163 .flags = FIB_LOOKUP_NOREF,
166 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
167 flowi4_to_flowi(flp4), 0, &arg);
168 if (err < 0)
169 return err;
170 *mrt = res.mrt;
171 return 0;
174 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
175 int flags, struct fib_lookup_arg *arg)
177 struct ipmr_result *res = arg->result;
178 struct mr_table *mrt;
180 switch (rule->action) {
181 case FR_ACT_TO_TBL:
182 break;
183 case FR_ACT_UNREACHABLE:
184 return -ENETUNREACH;
185 case FR_ACT_PROHIBIT:
186 return -EACCES;
187 case FR_ACT_BLACKHOLE:
188 default:
189 return -EINVAL;
192 mrt = ipmr_get_table(rule->fr_net, rule->table);
193 if (!mrt)
194 return -EAGAIN;
195 res->mrt = mrt;
196 return 0;
199 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
201 return 1;
204 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
205 FRA_GENERIC_POLICY,
208 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
209 struct fib_rule_hdr *frh, struct nlattr **tb)
211 return 0;
214 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
215 struct nlattr **tb)
217 return 1;
220 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
221 struct fib_rule_hdr *frh)
223 frh->dst_len = 0;
224 frh->src_len = 0;
225 frh->tos = 0;
226 return 0;
229 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
230 .family = RTNL_FAMILY_IPMR,
231 .rule_size = sizeof(struct ipmr_rule),
232 .addr_size = sizeof(u32),
233 .action = ipmr_rule_action,
234 .match = ipmr_rule_match,
235 .configure = ipmr_rule_configure,
236 .compare = ipmr_rule_compare,
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 unregister_netdevice(dev);
446 return NULL;
449 #ifdef CONFIG_IP_PIMSM
451 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
453 struct net *net = dev_net(dev);
454 struct mr_table *mrt;
455 struct flowi4 fl4 = {
456 .flowi4_oif = dev->ifindex,
457 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
458 .flowi4_mark = skb->mark,
460 int err;
462 err = ipmr_fib_lookup(net, &fl4, &mrt);
463 if (err < 0) {
464 kfree_skb(skb);
465 return err;
468 read_lock(&mrt_lock);
469 dev->stats.tx_bytes += skb->len;
470 dev->stats.tx_packets++;
471 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
472 read_unlock(&mrt_lock);
473 kfree_skb(skb);
474 return NETDEV_TX_OK;
477 static int reg_vif_get_iflink(const struct net_device *dev)
479 return 0;
482 static const struct net_device_ops reg_vif_netdev_ops = {
483 .ndo_start_xmit = reg_vif_xmit,
484 .ndo_get_iflink = reg_vif_get_iflink,
487 static void reg_vif_setup(struct net_device *dev)
489 dev->type = ARPHRD_PIMREG;
490 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
491 dev->flags = IFF_NOARP;
492 dev->netdev_ops = &reg_vif_netdev_ops;
493 dev->destructor = free_netdev;
494 dev->features |= NETIF_F_NETNS_LOCAL;
497 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
499 struct net_device *dev;
500 struct in_device *in_dev;
501 char name[IFNAMSIZ];
503 if (mrt->id == RT_TABLE_DEFAULT)
504 sprintf(name, "pimreg");
505 else
506 sprintf(name, "pimreg%u", mrt->id);
508 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
510 if (!dev)
511 return NULL;
513 dev_net_set(dev, net);
515 if (register_netdevice(dev)) {
516 free_netdev(dev);
517 return NULL;
520 rcu_read_lock();
521 in_dev = __in_dev_get_rcu(dev);
522 if (!in_dev) {
523 rcu_read_unlock();
524 goto failure;
527 ipv4_devconf_setall(in_dev);
528 neigh_parms_data_state_setall(in_dev->arp_parms);
529 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
530 rcu_read_unlock();
532 if (dev_open(dev))
533 goto failure;
535 dev_hold(dev);
537 return dev;
539 failure:
540 unregister_netdevice(dev);
541 return NULL;
543 #endif
546 * vif_delete - Delete a VIF entry
547 * @notify: Set to 1, if the caller is a notifier_call
550 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
551 struct list_head *head)
553 struct vif_device *v;
554 struct net_device *dev;
555 struct in_device *in_dev;
557 if (vifi < 0 || vifi >= mrt->maxvif)
558 return -EADDRNOTAVAIL;
560 v = &mrt->vif_table[vifi];
562 write_lock_bh(&mrt_lock);
563 dev = v->dev;
564 v->dev = NULL;
566 if (!dev) {
567 write_unlock_bh(&mrt_lock);
568 return -EADDRNOTAVAIL;
571 #ifdef CONFIG_IP_PIMSM
572 if (vifi == mrt->mroute_reg_vif_num)
573 mrt->mroute_reg_vif_num = -1;
574 #endif
576 if (vifi + 1 == mrt->maxvif) {
577 int tmp;
579 for (tmp = vifi - 1; tmp >= 0; tmp--) {
580 if (VIF_EXISTS(mrt, tmp))
581 break;
583 mrt->maxvif = tmp+1;
586 write_unlock_bh(&mrt_lock);
588 dev_set_allmulti(dev, -1);
590 in_dev = __in_dev_get_rtnl(dev);
591 if (in_dev) {
592 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
593 inet_netconf_notify_devconf(dev_net(dev),
594 NETCONFA_MC_FORWARDING,
595 dev->ifindex, &in_dev->cnf);
596 ip_rt_multicast_event(in_dev);
599 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
600 unregister_netdevice_queue(dev, head);
602 dev_put(dev);
603 return 0;
606 static void ipmr_cache_free_rcu(struct rcu_head *head)
608 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
610 kmem_cache_free(mrt_cachep, c);
613 static inline void ipmr_cache_free(struct mfc_cache *c)
615 call_rcu(&c->rcu, ipmr_cache_free_rcu);
618 /* Destroy an unresolved cache entry, killing queued skbs
619 * and reporting error to netlink readers.
622 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
624 struct net *net = read_pnet(&mrt->net);
625 struct sk_buff *skb;
626 struct nlmsgerr *e;
628 atomic_dec(&mrt->cache_resolve_queue_len);
630 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
631 if (ip_hdr(skb)->version == 0) {
632 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
633 nlh->nlmsg_type = NLMSG_ERROR;
634 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
635 skb_trim(skb, nlh->nlmsg_len);
636 e = nlmsg_data(nlh);
637 e->error = -ETIMEDOUT;
638 memset(&e->msg, 0, sizeof(e->msg));
640 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
641 } else {
642 kfree_skb(skb);
646 ipmr_cache_free(c);
650 /* Timer process for the unresolved queue. */
652 static void ipmr_expire_process(unsigned long arg)
654 struct mr_table *mrt = (struct mr_table *)arg;
655 unsigned long now;
656 unsigned long expires;
657 struct mfc_cache *c, *next;
659 if (!spin_trylock(&mfc_unres_lock)) {
660 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
661 return;
664 if (list_empty(&mrt->mfc_unres_queue))
665 goto out;
667 now = jiffies;
668 expires = 10*HZ;
670 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
671 if (time_after(c->mfc_un.unres.expires, now)) {
672 unsigned long interval = c->mfc_un.unres.expires - now;
673 if (interval < expires)
674 expires = interval;
675 continue;
678 list_del(&c->list);
679 mroute_netlink_event(mrt, c, RTM_DELROUTE);
680 ipmr_destroy_unres(mrt, c);
683 if (!list_empty(&mrt->mfc_unres_queue))
684 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
686 out:
687 spin_unlock(&mfc_unres_lock);
690 /* Fill oifs list. It is called under write locked mrt_lock. */
692 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
693 unsigned char *ttls)
695 int vifi;
697 cache->mfc_un.res.minvif = MAXVIFS;
698 cache->mfc_un.res.maxvif = 0;
699 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
701 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
702 if (VIF_EXISTS(mrt, vifi) &&
703 ttls[vifi] && ttls[vifi] < 255) {
704 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
705 if (cache->mfc_un.res.minvif > vifi)
706 cache->mfc_un.res.minvif = vifi;
707 if (cache->mfc_un.res.maxvif <= vifi)
708 cache->mfc_un.res.maxvif = vifi + 1;
713 static int vif_add(struct net *net, struct mr_table *mrt,
714 struct vifctl *vifc, int mrtsock)
716 int vifi = vifc->vifc_vifi;
717 struct vif_device *v = &mrt->vif_table[vifi];
718 struct net_device *dev;
719 struct in_device *in_dev;
720 int err;
722 /* Is vif busy ? */
723 if (VIF_EXISTS(mrt, vifi))
724 return -EADDRINUSE;
726 switch (vifc->vifc_flags) {
727 #ifdef CONFIG_IP_PIMSM
728 case VIFF_REGISTER:
730 * Special Purpose VIF in PIM
731 * All the packets will be sent to the daemon
733 if (mrt->mroute_reg_vif_num >= 0)
734 return -EADDRINUSE;
735 dev = ipmr_reg_vif(net, mrt);
736 if (!dev)
737 return -ENOBUFS;
738 err = dev_set_allmulti(dev, 1);
739 if (err) {
740 unregister_netdevice(dev);
741 dev_put(dev);
742 return err;
744 break;
745 #endif
746 case VIFF_TUNNEL:
747 dev = ipmr_new_tunnel(net, vifc);
748 if (!dev)
749 return -ENOBUFS;
750 err = dev_set_allmulti(dev, 1);
751 if (err) {
752 ipmr_del_tunnel(dev, vifc);
753 dev_put(dev);
754 return err;
756 break;
758 case VIFF_USE_IFINDEX:
759 case 0:
760 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
761 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
762 if (dev && !__in_dev_get_rtnl(dev)) {
763 dev_put(dev);
764 return -EADDRNOTAVAIL;
766 } else {
767 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
769 if (!dev)
770 return -EADDRNOTAVAIL;
771 err = dev_set_allmulti(dev, 1);
772 if (err) {
773 dev_put(dev);
774 return err;
776 break;
777 default:
778 return -EINVAL;
781 in_dev = __in_dev_get_rtnl(dev);
782 if (!in_dev) {
783 dev_put(dev);
784 return -EADDRNOTAVAIL;
786 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
787 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
788 &in_dev->cnf);
789 ip_rt_multicast_event(in_dev);
791 /* Fill in the VIF structures */
793 v->rate_limit = vifc->vifc_rate_limit;
794 v->local = vifc->vifc_lcl_addr.s_addr;
795 v->remote = vifc->vifc_rmt_addr.s_addr;
796 v->flags = vifc->vifc_flags;
797 if (!mrtsock)
798 v->flags |= VIFF_STATIC;
799 v->threshold = vifc->vifc_threshold;
800 v->bytes_in = 0;
801 v->bytes_out = 0;
802 v->pkt_in = 0;
803 v->pkt_out = 0;
804 v->link = dev->ifindex;
805 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
806 v->link = dev_get_iflink(dev);
808 /* And finish update writing critical data */
809 write_lock_bh(&mrt_lock);
810 v->dev = dev;
811 #ifdef CONFIG_IP_PIMSM
812 if (v->flags & VIFF_REGISTER)
813 mrt->mroute_reg_vif_num = vifi;
814 #endif
815 if (vifi+1 > mrt->maxvif)
816 mrt->maxvif = vifi+1;
817 write_unlock_bh(&mrt_lock);
818 return 0;
821 /* called with rcu_read_lock() */
822 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
823 __be32 origin,
824 __be32 mcastgrp)
826 int line = MFC_HASH(mcastgrp, origin);
827 struct mfc_cache *c;
829 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
830 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
831 return c;
833 return NULL;
836 /* Look for a (*,*,oif) entry */
837 static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
838 int vifi)
840 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
841 struct mfc_cache *c;
843 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
844 if (c->mfc_origin == htonl(INADDR_ANY) &&
845 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
846 c->mfc_un.res.ttls[vifi] < 255)
847 return c;
849 return NULL;
852 /* Look for a (*,G) entry */
853 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
854 __be32 mcastgrp, int vifi)
856 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
857 struct mfc_cache *c, *proxy;
859 if (mcastgrp == htonl(INADDR_ANY))
860 goto skip;
862 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
863 if (c->mfc_origin == htonl(INADDR_ANY) &&
864 c->mfc_mcastgrp == mcastgrp) {
865 if (c->mfc_un.res.ttls[vifi] < 255)
866 return c;
868 /* It's ok if the vifi is part of the static tree */
869 proxy = ipmr_cache_find_any_parent(mrt,
870 c->mfc_parent);
871 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
872 return c;
875 skip:
876 return ipmr_cache_find_any_parent(mrt, vifi);
880 * Allocate a multicast cache entry
882 static struct mfc_cache *ipmr_cache_alloc(void)
884 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
886 if (c) {
887 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
888 c->mfc_un.res.minvif = MAXVIFS;
890 return c;
893 static struct mfc_cache *ipmr_cache_alloc_unres(void)
895 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
897 if (c) {
898 skb_queue_head_init(&c->mfc_un.unres.unresolved);
899 c->mfc_un.unres.expires = jiffies + 10*HZ;
901 return c;
905 * A cache entry has gone into a resolved state from queued
908 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
909 struct mfc_cache *uc, struct mfc_cache *c)
911 struct sk_buff *skb;
912 struct nlmsgerr *e;
914 /* Play the pending entries through our router */
916 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
917 if (ip_hdr(skb)->version == 0) {
918 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
920 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
921 nlh->nlmsg_len = skb_tail_pointer(skb) -
922 (u8 *)nlh;
923 } else {
924 nlh->nlmsg_type = NLMSG_ERROR;
925 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
926 skb_trim(skb, nlh->nlmsg_len);
927 e = nlmsg_data(nlh);
928 e->error = -EMSGSIZE;
929 memset(&e->msg, 0, sizeof(e->msg));
932 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
933 } else {
934 ip_mr_forward(net, mrt, skb, c, 0);
940 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
941 * expects the following bizarre scheme.
943 * Called under mrt_lock.
946 static int ipmr_cache_report(struct mr_table *mrt,
947 struct sk_buff *pkt, vifi_t vifi, int assert)
949 struct sk_buff *skb;
950 const int ihl = ip_hdrlen(pkt);
951 struct igmphdr *igmp;
952 struct igmpmsg *msg;
953 struct sock *mroute_sk;
954 int ret;
956 #ifdef CONFIG_IP_PIMSM
957 if (assert == IGMPMSG_WHOLEPKT)
958 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
959 else
960 #endif
961 skb = alloc_skb(128, GFP_ATOMIC);
963 if (!skb)
964 return -ENOBUFS;
966 #ifdef CONFIG_IP_PIMSM
967 if (assert == IGMPMSG_WHOLEPKT) {
968 /* Ugly, but we have no choice with this interface.
969 * Duplicate old header, fix ihl, length etc.
970 * And all this only to mangle msg->im_msgtype and
971 * to set msg->im_mbz to "mbz" :-)
973 skb_push(skb, sizeof(struct iphdr));
974 skb_reset_network_header(skb);
975 skb_reset_transport_header(skb);
976 msg = (struct igmpmsg *)skb_network_header(skb);
977 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
978 msg->im_msgtype = IGMPMSG_WHOLEPKT;
979 msg->im_mbz = 0;
980 msg->im_vif = mrt->mroute_reg_vif_num;
981 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
982 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
983 sizeof(struct iphdr));
984 } else
985 #endif
988 /* Copy the IP header */
990 skb_set_network_header(skb, skb->len);
991 skb_put(skb, ihl);
992 skb_copy_to_linear_data(skb, pkt->data, ihl);
993 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
994 msg = (struct igmpmsg *)skb_network_header(skb);
995 msg->im_vif = vifi;
996 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
998 /* Add our header */
1000 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1001 igmp->type =
1002 msg->im_msgtype = assert;
1003 igmp->code = 0;
1004 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1005 skb->transport_header = skb->network_header;
1008 rcu_read_lock();
1009 mroute_sk = rcu_dereference(mrt->mroute_sk);
1010 if (!mroute_sk) {
1011 rcu_read_unlock();
1012 kfree_skb(skb);
1013 return -EINVAL;
1016 /* Deliver to mrouted */
1018 ret = sock_queue_rcv_skb(mroute_sk, skb);
1019 rcu_read_unlock();
1020 if (ret < 0) {
1021 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1022 kfree_skb(skb);
1025 return ret;
1029 * Queue a packet for resolution. It gets locked cache entry!
1032 static int
1033 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
1035 bool found = false;
1036 int err;
1037 struct mfc_cache *c;
1038 const struct iphdr *iph = ip_hdr(skb);
1040 spin_lock_bh(&mfc_unres_lock);
1041 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
1042 if (c->mfc_mcastgrp == iph->daddr &&
1043 c->mfc_origin == iph->saddr) {
1044 found = true;
1045 break;
1049 if (!found) {
1050 /* Create a new entry if allowable */
1052 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1053 (c = ipmr_cache_alloc_unres()) == NULL) {
1054 spin_unlock_bh(&mfc_unres_lock);
1056 kfree_skb(skb);
1057 return -ENOBUFS;
1060 /* Fill in the new cache entry */
1062 c->mfc_parent = -1;
1063 c->mfc_origin = iph->saddr;
1064 c->mfc_mcastgrp = iph->daddr;
1066 /* Reflect first query at mrouted. */
1068 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1069 if (err < 0) {
1070 /* If the report failed throw the cache entry
1071 out - Brad Parker
1073 spin_unlock_bh(&mfc_unres_lock);
1075 ipmr_cache_free(c);
1076 kfree_skb(skb);
1077 return err;
1080 atomic_inc(&mrt->cache_resolve_queue_len);
1081 list_add(&c->list, &mrt->mfc_unres_queue);
1082 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1084 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1085 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1088 /* See if we can append the packet */
1090 if (c->mfc_un.unres.unresolved.qlen > 3) {
1091 kfree_skb(skb);
1092 err = -ENOBUFS;
1093 } else {
1094 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1095 err = 0;
1098 spin_unlock_bh(&mfc_unres_lock);
1099 return err;
1103 * MFC cache manipulation by user space mroute daemon
1106 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1108 int line;
1109 struct mfc_cache *c, *next;
1111 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1113 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1114 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1115 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1116 (parent == -1 || parent == c->mfc_parent)) {
1117 list_del_rcu(&c->list);
1118 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1119 ipmr_cache_free(c);
1120 return 0;
1123 return -ENOENT;
1126 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1127 struct mfcctl *mfc, int mrtsock, int parent)
1129 bool found = false;
1130 int line;
1131 struct mfc_cache *uc, *c;
1133 if (mfc->mfcc_parent >= MAXVIFS)
1134 return -ENFILE;
1136 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1138 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1139 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1140 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1141 (parent == -1 || parent == c->mfc_parent)) {
1142 found = true;
1143 break;
1147 if (found) {
1148 write_lock_bh(&mrt_lock);
1149 c->mfc_parent = mfc->mfcc_parent;
1150 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1151 if (!mrtsock)
1152 c->mfc_flags |= MFC_STATIC;
1153 write_unlock_bh(&mrt_lock);
1154 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1155 return 0;
1158 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1159 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1160 return -EINVAL;
1162 c = ipmr_cache_alloc();
1163 if (!c)
1164 return -ENOMEM;
1166 c->mfc_origin = mfc->mfcc_origin.s_addr;
1167 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1168 c->mfc_parent = mfc->mfcc_parent;
1169 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1170 if (!mrtsock)
1171 c->mfc_flags |= MFC_STATIC;
1173 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1176 * Check to see if we resolved a queued list. If so we
1177 * need to send on the frames and tidy up.
1179 found = false;
1180 spin_lock_bh(&mfc_unres_lock);
1181 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1182 if (uc->mfc_origin == c->mfc_origin &&
1183 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1184 list_del(&uc->list);
1185 atomic_dec(&mrt->cache_resolve_queue_len);
1186 found = true;
1187 break;
1190 if (list_empty(&mrt->mfc_unres_queue))
1191 del_timer(&mrt->ipmr_expire_timer);
1192 spin_unlock_bh(&mfc_unres_lock);
1194 if (found) {
1195 ipmr_cache_resolve(net, mrt, uc, c);
1196 ipmr_cache_free(uc);
1198 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1199 return 0;
1203 * Close the multicast socket, and clear the vif tables etc
1206 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1208 int i;
1209 LIST_HEAD(list);
1210 struct mfc_cache *c, *next;
1212 /* Shut down all active vif entries */
1214 for (i = 0; i < mrt->maxvif; i++) {
1215 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1216 continue;
1217 vif_delete(mrt, i, 0, &list);
1219 unregister_netdevice_many(&list);
1221 /* Wipe the cache */
1223 for (i = 0; i < MFC_LINES; i++) {
1224 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1225 if (!all && (c->mfc_flags & MFC_STATIC))
1226 continue;
1227 list_del_rcu(&c->list);
1228 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1229 ipmr_cache_free(c);
1233 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1234 spin_lock_bh(&mfc_unres_lock);
1235 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1236 list_del(&c->list);
1237 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1238 ipmr_destroy_unres(mrt, c);
1240 spin_unlock_bh(&mfc_unres_lock);
1244 /* called from ip_ra_control(), before an RCU grace period,
1245 * we dont need to call synchronize_rcu() here
1247 static void mrtsock_destruct(struct sock *sk)
1249 struct net *net = sock_net(sk);
1250 struct mr_table *mrt;
1252 rtnl_lock();
1253 ipmr_for_each_table(mrt, net) {
1254 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1255 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1256 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1257 NETCONFA_IFINDEX_ALL,
1258 net->ipv4.devconf_all);
1259 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1260 mroute_clean_tables(mrt, false);
1263 rtnl_unlock();
1267 * Socket options and virtual interface manipulation. The whole
1268 * virtual interface system is a complete heap, but unfortunately
1269 * that's how BSD mrouted happens to think. Maybe one day with a proper
1270 * MOSPF/PIM router set up we can clean this up.
1273 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1275 int ret, parent = 0;
1276 struct vifctl vif;
1277 struct mfcctl mfc;
1278 struct net *net = sock_net(sk);
1279 struct mr_table *mrt;
1281 if (sk->sk_type != SOCK_RAW ||
1282 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1283 return -EOPNOTSUPP;
1285 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1286 if (!mrt)
1287 return -ENOENT;
1289 if (optname != MRT_INIT) {
1290 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1291 !ns_capable(net->user_ns, CAP_NET_ADMIN))
1292 return -EACCES;
1295 switch (optname) {
1296 case MRT_INIT:
1297 if (optlen != sizeof(int))
1298 return -EINVAL;
1300 rtnl_lock();
1301 if (rtnl_dereference(mrt->mroute_sk)) {
1302 rtnl_unlock();
1303 return -EADDRINUSE;
1306 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1307 if (ret == 0) {
1308 rcu_assign_pointer(mrt->mroute_sk, sk);
1309 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1310 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1311 NETCONFA_IFINDEX_ALL,
1312 net->ipv4.devconf_all);
1314 rtnl_unlock();
1315 return ret;
1316 case MRT_DONE:
1317 if (sk != rcu_access_pointer(mrt->mroute_sk))
1318 return -EACCES;
1319 return ip_ra_control(sk, 0, NULL);
1320 case MRT_ADD_VIF:
1321 case MRT_DEL_VIF:
1322 if (optlen != sizeof(vif))
1323 return -EINVAL;
1324 if (copy_from_user(&vif, optval, sizeof(vif)))
1325 return -EFAULT;
1326 if (vif.vifc_vifi >= MAXVIFS)
1327 return -ENFILE;
1328 rtnl_lock();
1329 if (optname == MRT_ADD_VIF) {
1330 ret = vif_add(net, mrt, &vif,
1331 sk == rtnl_dereference(mrt->mroute_sk));
1332 } else {
1333 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1335 rtnl_unlock();
1336 return ret;
1339 * Manipulate the forwarding caches. These live
1340 * in a sort of kernel/user symbiosis.
1342 case MRT_ADD_MFC:
1343 case MRT_DEL_MFC:
1344 parent = -1;
1345 case MRT_ADD_MFC_PROXY:
1346 case MRT_DEL_MFC_PROXY:
1347 if (optlen != sizeof(mfc))
1348 return -EINVAL;
1349 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1350 return -EFAULT;
1351 if (parent == 0)
1352 parent = mfc.mfcc_parent;
1353 rtnl_lock();
1354 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1355 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1356 else
1357 ret = ipmr_mfc_add(net, mrt, &mfc,
1358 sk == rtnl_dereference(mrt->mroute_sk),
1359 parent);
1360 rtnl_unlock();
1361 return ret;
1363 * Control PIM assert.
1365 case MRT_ASSERT:
1367 int v;
1368 if (optlen != sizeof(v))
1369 return -EINVAL;
1370 if (get_user(v, (int __user *)optval))
1371 return -EFAULT;
1372 mrt->mroute_do_assert = v;
1373 return 0;
1375 #ifdef CONFIG_IP_PIMSM
1376 case MRT_PIM:
1378 int v;
1380 if (optlen != sizeof(v))
1381 return -EINVAL;
1382 if (get_user(v, (int __user *)optval))
1383 return -EFAULT;
1384 v = !!v;
1386 rtnl_lock();
1387 ret = 0;
1388 if (v != mrt->mroute_do_pim) {
1389 mrt->mroute_do_pim = v;
1390 mrt->mroute_do_assert = v;
1392 rtnl_unlock();
1393 return ret;
1395 #endif
1396 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1397 case MRT_TABLE:
1399 u32 v;
1401 if (optlen != sizeof(u32))
1402 return -EINVAL;
1403 if (get_user(v, (u32 __user *)optval))
1404 return -EFAULT;
1406 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1407 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1408 return -EINVAL;
1410 rtnl_lock();
1411 ret = 0;
1412 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1413 ret = -EBUSY;
1414 } else {
1415 if (!ipmr_new_table(net, v))
1416 ret = -ENOMEM;
1417 else
1418 raw_sk(sk)->ipmr_table = v;
1420 rtnl_unlock();
1421 return ret;
1423 #endif
1425 * Spurious command, or MRT_VERSION which you cannot
1426 * set.
1428 default:
1429 return -ENOPROTOOPT;
1434 * Getsock opt support for the multicast routing system.
1437 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1439 int olr;
1440 int val;
1441 struct net *net = sock_net(sk);
1442 struct mr_table *mrt;
1444 if (sk->sk_type != SOCK_RAW ||
1445 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1446 return -EOPNOTSUPP;
1448 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1449 if (!mrt)
1450 return -ENOENT;
1452 if (optname != MRT_VERSION &&
1453 #ifdef CONFIG_IP_PIMSM
1454 optname != MRT_PIM &&
1455 #endif
1456 optname != MRT_ASSERT)
1457 return -ENOPROTOOPT;
1459 if (get_user(olr, optlen))
1460 return -EFAULT;
1462 olr = min_t(unsigned int, olr, sizeof(int));
1463 if (olr < 0)
1464 return -EINVAL;
1466 if (put_user(olr, optlen))
1467 return -EFAULT;
1468 if (optname == MRT_VERSION)
1469 val = 0x0305;
1470 #ifdef CONFIG_IP_PIMSM
1471 else if (optname == MRT_PIM)
1472 val = mrt->mroute_do_pim;
1473 #endif
1474 else
1475 val = mrt->mroute_do_assert;
1476 if (copy_to_user(optval, &val, olr))
1477 return -EFAULT;
1478 return 0;
1482 * The IP multicast ioctl support routines.
1485 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1487 struct sioc_sg_req sr;
1488 struct sioc_vif_req vr;
1489 struct vif_device *vif;
1490 struct mfc_cache *c;
1491 struct net *net = sock_net(sk);
1492 struct mr_table *mrt;
1494 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1495 if (!mrt)
1496 return -ENOENT;
1498 switch (cmd) {
1499 case SIOCGETVIFCNT:
1500 if (copy_from_user(&vr, arg, sizeof(vr)))
1501 return -EFAULT;
1502 if (vr.vifi >= mrt->maxvif)
1503 return -EINVAL;
1504 read_lock(&mrt_lock);
1505 vif = &mrt->vif_table[vr.vifi];
1506 if (VIF_EXISTS(mrt, vr.vifi)) {
1507 vr.icount = vif->pkt_in;
1508 vr.ocount = vif->pkt_out;
1509 vr.ibytes = vif->bytes_in;
1510 vr.obytes = vif->bytes_out;
1511 read_unlock(&mrt_lock);
1513 if (copy_to_user(arg, &vr, sizeof(vr)))
1514 return -EFAULT;
1515 return 0;
1517 read_unlock(&mrt_lock);
1518 return -EADDRNOTAVAIL;
1519 case SIOCGETSGCNT:
1520 if (copy_from_user(&sr, arg, sizeof(sr)))
1521 return -EFAULT;
1523 rcu_read_lock();
1524 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1525 if (c) {
1526 sr.pktcnt = c->mfc_un.res.pkt;
1527 sr.bytecnt = c->mfc_un.res.bytes;
1528 sr.wrong_if = c->mfc_un.res.wrong_if;
1529 rcu_read_unlock();
1531 if (copy_to_user(arg, &sr, sizeof(sr)))
1532 return -EFAULT;
1533 return 0;
1535 rcu_read_unlock();
1536 return -EADDRNOTAVAIL;
1537 default:
1538 return -ENOIOCTLCMD;
1542 #ifdef CONFIG_COMPAT
1543 struct compat_sioc_sg_req {
1544 struct in_addr src;
1545 struct in_addr grp;
1546 compat_ulong_t pktcnt;
1547 compat_ulong_t bytecnt;
1548 compat_ulong_t wrong_if;
1551 struct compat_sioc_vif_req {
1552 vifi_t vifi; /* Which iface */
1553 compat_ulong_t icount;
1554 compat_ulong_t ocount;
1555 compat_ulong_t ibytes;
1556 compat_ulong_t obytes;
1559 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1561 struct compat_sioc_sg_req sr;
1562 struct compat_sioc_vif_req vr;
1563 struct vif_device *vif;
1564 struct mfc_cache *c;
1565 struct net *net = sock_net(sk);
1566 struct mr_table *mrt;
1568 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1569 if (!mrt)
1570 return -ENOENT;
1572 switch (cmd) {
1573 case SIOCGETVIFCNT:
1574 if (copy_from_user(&vr, arg, sizeof(vr)))
1575 return -EFAULT;
1576 if (vr.vifi >= mrt->maxvif)
1577 return -EINVAL;
1578 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1579 read_lock(&mrt_lock);
1580 vif = &mrt->vif_table[vr.vifi];
1581 if (VIF_EXISTS(mrt, vr.vifi)) {
1582 vr.icount = vif->pkt_in;
1583 vr.ocount = vif->pkt_out;
1584 vr.ibytes = vif->bytes_in;
1585 vr.obytes = vif->bytes_out;
1586 read_unlock(&mrt_lock);
1588 if (copy_to_user(arg, &vr, sizeof(vr)))
1589 return -EFAULT;
1590 return 0;
1592 read_unlock(&mrt_lock);
1593 return -EADDRNOTAVAIL;
1594 case SIOCGETSGCNT:
1595 if (copy_from_user(&sr, arg, sizeof(sr)))
1596 return -EFAULT;
1598 rcu_read_lock();
1599 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1600 if (c) {
1601 sr.pktcnt = c->mfc_un.res.pkt;
1602 sr.bytecnt = c->mfc_un.res.bytes;
1603 sr.wrong_if = c->mfc_un.res.wrong_if;
1604 rcu_read_unlock();
1606 if (copy_to_user(arg, &sr, sizeof(sr)))
1607 return -EFAULT;
1608 return 0;
1610 rcu_read_unlock();
1611 return -EADDRNOTAVAIL;
1612 default:
1613 return -ENOIOCTLCMD;
1616 #endif
1619 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1621 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1622 struct net *net = dev_net(dev);
1623 struct mr_table *mrt;
1624 struct vif_device *v;
1625 int ct;
1627 if (event != NETDEV_UNREGISTER)
1628 return NOTIFY_DONE;
1630 ipmr_for_each_table(mrt, net) {
1631 v = &mrt->vif_table[0];
1632 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1633 if (v->dev == dev)
1634 vif_delete(mrt, ct, 1, NULL);
1637 return NOTIFY_DONE;
1641 static struct notifier_block ip_mr_notifier = {
1642 .notifier_call = ipmr_device_event,
1646 * Encapsulate a packet by attaching a valid IPIP header to it.
1647 * This avoids tunnel drivers and other mess and gives us the speed so
1648 * important for multicast video.
1651 static void ip_encap(struct net *net, struct sk_buff *skb,
1652 __be32 saddr, __be32 daddr)
1654 struct iphdr *iph;
1655 const struct iphdr *old_iph = ip_hdr(skb);
1657 skb_push(skb, sizeof(struct iphdr));
1658 skb->transport_header = skb->network_header;
1659 skb_reset_network_header(skb);
1660 iph = ip_hdr(skb);
1662 iph->version = 4;
1663 iph->tos = old_iph->tos;
1664 iph->ttl = old_iph->ttl;
1665 iph->frag_off = 0;
1666 iph->daddr = daddr;
1667 iph->saddr = saddr;
1668 iph->protocol = IPPROTO_IPIP;
1669 iph->ihl = 5;
1670 iph->tot_len = htons(skb->len);
1671 ip_select_ident(net, skb, NULL);
1672 ip_send_check(iph);
1674 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1675 nf_reset(skb);
1678 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1679 struct sk_buff *skb)
1681 struct ip_options *opt = &(IPCB(skb)->opt);
1683 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1684 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1686 if (unlikely(opt->optlen))
1687 ip_forward_options(skb);
1689 return dst_output(net, sk, skb);
1693 * Processing handlers for ipmr_forward
1696 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1697 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1699 const struct iphdr *iph = ip_hdr(skb);
1700 struct vif_device *vif = &mrt->vif_table[vifi];
1701 struct net_device *dev;
1702 struct rtable *rt;
1703 struct flowi4 fl4;
1704 int encap = 0;
1706 if (!vif->dev)
1707 goto out_free;
1709 #ifdef CONFIG_IP_PIMSM
1710 if (vif->flags & VIFF_REGISTER) {
1711 vif->pkt_out++;
1712 vif->bytes_out += skb->len;
1713 vif->dev->stats.tx_bytes += skb->len;
1714 vif->dev->stats.tx_packets++;
1715 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1716 goto out_free;
1718 #endif
1720 if (vif->flags & VIFF_TUNNEL) {
1721 rt = ip_route_output_ports(net, &fl4, NULL,
1722 vif->remote, vif->local,
1723 0, 0,
1724 IPPROTO_IPIP,
1725 RT_TOS(iph->tos), vif->link);
1726 if (IS_ERR(rt))
1727 goto out_free;
1728 encap = sizeof(struct iphdr);
1729 } else {
1730 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1731 0, 0,
1732 IPPROTO_IPIP,
1733 RT_TOS(iph->tos), vif->link);
1734 if (IS_ERR(rt))
1735 goto out_free;
1738 dev = rt->dst.dev;
1740 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1741 /* Do not fragment multicasts. Alas, IPv4 does not
1742 * allow to send ICMP, so that packets will disappear
1743 * to blackhole.
1746 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1747 ip_rt_put(rt);
1748 goto out_free;
1751 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1753 if (skb_cow(skb, encap)) {
1754 ip_rt_put(rt);
1755 goto out_free;
1758 vif->pkt_out++;
1759 vif->bytes_out += skb->len;
1761 skb_dst_drop(skb);
1762 skb_dst_set(skb, &rt->dst);
1763 ip_decrease_ttl(ip_hdr(skb));
1765 /* FIXME: forward and output firewalls used to be called here.
1766 * What do we do with netfilter? -- RR
1768 if (vif->flags & VIFF_TUNNEL) {
1769 ip_encap(net, skb, vif->local, vif->remote);
1770 /* FIXME: extra output firewall step used to be here. --RR */
1771 vif->dev->stats.tx_packets++;
1772 vif->dev->stats.tx_bytes += skb->len;
1775 IPCB(skb)->flags |= IPSKB_FORWARDED;
1778 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1779 * not only before forwarding, but after forwarding on all output
1780 * interfaces. It is clear, if mrouter runs a multicasting
1781 * program, it should receive packets not depending to what interface
1782 * program is joined.
1783 * If we will not make it, the program will have to join on all
1784 * interfaces. On the other hand, multihoming host (or router, but
1785 * not mrouter) cannot join to more than one interface - it will
1786 * result in receiving multiple packets.
1788 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1789 net, NULL, skb, skb->dev, dev,
1790 ipmr_forward_finish);
1791 return;
1793 out_free:
1794 kfree_skb(skb);
1797 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1799 int ct;
1801 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1802 if (mrt->vif_table[ct].dev == dev)
1803 break;
1805 return ct;
1808 /* "local" means that we should preserve one skb (for local delivery) */
1810 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1811 struct sk_buff *skb, struct mfc_cache *cache,
1812 int local)
1814 int psend = -1;
1815 int vif, ct;
1816 int true_vifi = ipmr_find_vif(mrt, skb->dev);
1818 vif = cache->mfc_parent;
1819 cache->mfc_un.res.pkt++;
1820 cache->mfc_un.res.bytes += skb->len;
1822 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1823 struct mfc_cache *cache_proxy;
1825 /* For an (*,G) entry, we only check that the incomming
1826 * interface is part of the static tree.
1828 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1829 if (cache_proxy &&
1830 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1831 goto forward;
1835 * Wrong interface: drop packet and (maybe) send PIM assert.
1837 if (mrt->vif_table[vif].dev != skb->dev) {
1838 if (rt_is_output_route(skb_rtable(skb))) {
1839 /* It is our own packet, looped back.
1840 * Very complicated situation...
1842 * The best workaround until routing daemons will be
1843 * fixed is not to redistribute packet, if it was
1844 * send through wrong interface. It means, that
1845 * multicast applications WILL NOT work for
1846 * (S,G), which have default multicast route pointing
1847 * to wrong oif. In any case, it is not a good
1848 * idea to use multicasting applications on router.
1850 goto dont_forward;
1853 cache->mfc_un.res.wrong_if++;
1855 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1856 /* pimsm uses asserts, when switching from RPT to SPT,
1857 * so that we cannot check that packet arrived on an oif.
1858 * It is bad, but otherwise we would need to move pretty
1859 * large chunk of pimd to kernel. Ough... --ANK
1861 (mrt->mroute_do_pim ||
1862 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1863 time_after(jiffies,
1864 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1865 cache->mfc_un.res.last_assert = jiffies;
1866 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1868 goto dont_forward;
1871 forward:
1872 mrt->vif_table[vif].pkt_in++;
1873 mrt->vif_table[vif].bytes_in += skb->len;
1876 * Forward the frame
1878 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1879 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
1880 if (true_vifi >= 0 &&
1881 true_vifi != cache->mfc_parent &&
1882 ip_hdr(skb)->ttl >
1883 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1884 /* It's an (*,*) entry and the packet is not coming from
1885 * the upstream: forward the packet to the upstream
1886 * only.
1888 psend = cache->mfc_parent;
1889 goto last_forward;
1891 goto dont_forward;
1893 for (ct = cache->mfc_un.res.maxvif - 1;
1894 ct >= cache->mfc_un.res.minvif; ct--) {
1895 /* For (*,G) entry, don't forward to the incoming interface */
1896 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1897 ct != true_vifi) &&
1898 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1899 if (psend != -1) {
1900 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1902 if (skb2)
1903 ipmr_queue_xmit(net, mrt, skb2, cache,
1904 psend);
1906 psend = ct;
1909 last_forward:
1910 if (psend != -1) {
1911 if (local) {
1912 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1914 if (skb2)
1915 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1916 } else {
1917 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1918 return;
1922 dont_forward:
1923 if (!local)
1924 kfree_skb(skb);
1927 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1929 struct rtable *rt = skb_rtable(skb);
1930 struct iphdr *iph = ip_hdr(skb);
1931 struct flowi4 fl4 = {
1932 .daddr = iph->daddr,
1933 .saddr = iph->saddr,
1934 .flowi4_tos = RT_TOS(iph->tos),
1935 .flowi4_oif = (rt_is_output_route(rt) ?
1936 skb->dev->ifindex : 0),
1937 .flowi4_iif = (rt_is_output_route(rt) ?
1938 LOOPBACK_IFINDEX :
1939 skb->dev->ifindex),
1940 .flowi4_mark = skb->mark,
1942 struct mr_table *mrt;
1943 int err;
1945 err = ipmr_fib_lookup(net, &fl4, &mrt);
1946 if (err)
1947 return ERR_PTR(err);
1948 return mrt;
1952 * Multicast packets for forwarding arrive here
1953 * Called with rcu_read_lock();
1956 int ip_mr_input(struct sk_buff *skb)
1958 struct mfc_cache *cache;
1959 struct net *net = dev_net(skb->dev);
1960 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1961 struct mr_table *mrt;
1963 /* Packet is looped back after forward, it should not be
1964 * forwarded second time, but still can be delivered locally.
1966 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1967 goto dont_forward;
1969 mrt = ipmr_rt_fib_lookup(net, skb);
1970 if (IS_ERR(mrt)) {
1971 kfree_skb(skb);
1972 return PTR_ERR(mrt);
1974 if (!local) {
1975 if (IPCB(skb)->opt.router_alert) {
1976 if (ip_call_ra_chain(skb))
1977 return 0;
1978 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1979 /* IGMPv1 (and broken IGMPv2 implementations sort of
1980 * Cisco IOS <= 11.2(8)) do not put router alert
1981 * option to IGMP packets destined to routable
1982 * groups. It is very bad, because it means
1983 * that we can forward NO IGMP messages.
1985 struct sock *mroute_sk;
1987 mroute_sk = rcu_dereference(mrt->mroute_sk);
1988 if (mroute_sk) {
1989 nf_reset(skb);
1990 raw_rcv(mroute_sk, skb);
1991 return 0;
1996 /* already under rcu_read_lock() */
1997 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1998 if (!cache) {
1999 int vif = ipmr_find_vif(mrt, skb->dev);
2001 if (vif >= 0)
2002 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2003 vif);
2007 * No usable cache entry
2009 if (!cache) {
2010 int vif;
2012 if (local) {
2013 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2014 ip_local_deliver(skb);
2015 if (!skb2)
2016 return -ENOBUFS;
2017 skb = skb2;
2020 read_lock(&mrt_lock);
2021 vif = ipmr_find_vif(mrt, skb->dev);
2022 if (vif >= 0) {
2023 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
2024 read_unlock(&mrt_lock);
2026 return err2;
2028 read_unlock(&mrt_lock);
2029 kfree_skb(skb);
2030 return -ENODEV;
2033 read_lock(&mrt_lock);
2034 ip_mr_forward(net, mrt, skb, cache, local);
2035 read_unlock(&mrt_lock);
2037 if (local)
2038 return ip_local_deliver(skb);
2040 return 0;
2042 dont_forward:
2043 if (local)
2044 return ip_local_deliver(skb);
2045 kfree_skb(skb);
2046 return 0;
2049 #ifdef CONFIG_IP_PIMSM
2050 /* called with rcu_read_lock() */
2051 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2052 unsigned int pimlen)
2054 struct net_device *reg_dev = NULL;
2055 struct iphdr *encap;
2057 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
2059 * Check that:
2060 * a. packet is really sent to a multicast group
2061 * b. packet is not a NULL-REGISTER
2062 * c. packet is not truncated
2064 if (!ipv4_is_multicast(encap->daddr) ||
2065 encap->tot_len == 0 ||
2066 ntohs(encap->tot_len) + pimlen > skb->len)
2067 return 1;
2069 read_lock(&mrt_lock);
2070 if (mrt->mroute_reg_vif_num >= 0)
2071 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
2072 read_unlock(&mrt_lock);
2074 if (!reg_dev)
2075 return 1;
2077 skb->mac_header = skb->network_header;
2078 skb_pull(skb, (u8 *)encap - skb->data);
2079 skb_reset_network_header(skb);
2080 skb->protocol = htons(ETH_P_IP);
2081 skb->ip_summed = CHECKSUM_NONE;
2083 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
2085 netif_rx(skb);
2087 return NET_RX_SUCCESS;
2089 #endif
2091 #ifdef CONFIG_IP_PIMSM_V1
2093 * Handle IGMP messages of PIMv1
2096 int pim_rcv_v1(struct sk_buff *skb)
2098 struct igmphdr *pim;
2099 struct net *net = dev_net(skb->dev);
2100 struct mr_table *mrt;
2102 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2103 goto drop;
2105 pim = igmp_hdr(skb);
2107 mrt = ipmr_rt_fib_lookup(net, skb);
2108 if (IS_ERR(mrt))
2109 goto drop;
2110 if (!mrt->mroute_do_pim ||
2111 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2112 goto drop;
2114 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2115 drop:
2116 kfree_skb(skb);
2118 return 0;
2120 #endif
2122 #ifdef CONFIG_IP_PIMSM_V2
2123 static int pim_rcv(struct sk_buff *skb)
2125 struct pimreghdr *pim;
2126 struct net *net = dev_net(skb->dev);
2127 struct mr_table *mrt;
2129 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2130 goto drop;
2132 pim = (struct pimreghdr *)skb_transport_header(skb);
2133 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2134 (pim->flags & PIM_NULL_REGISTER) ||
2135 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2136 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2137 goto drop;
2139 mrt = ipmr_rt_fib_lookup(net, skb);
2140 if (IS_ERR(mrt))
2141 goto drop;
2142 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2143 drop:
2144 kfree_skb(skb);
2146 return 0;
2148 #endif
2150 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2151 struct mfc_cache *c, struct rtmsg *rtm)
2153 int ct;
2154 struct rtnexthop *nhp;
2155 struct nlattr *mp_attr;
2156 struct rta_mfc_stats mfcs;
2158 /* If cache is unresolved, don't try to parse IIF and OIF */
2159 if (c->mfc_parent >= MAXVIFS)
2160 return -ENOENT;
2162 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2163 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2164 return -EMSGSIZE;
2166 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2167 return -EMSGSIZE;
2169 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2170 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2171 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2172 nla_nest_cancel(skb, mp_attr);
2173 return -EMSGSIZE;
2176 nhp->rtnh_flags = 0;
2177 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2178 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2179 nhp->rtnh_len = sizeof(*nhp);
2183 nla_nest_end(skb, mp_attr);
2185 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2186 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2187 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2188 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2189 return -EMSGSIZE;
2191 rtm->rtm_type = RTN_MULTICAST;
2192 return 1;
2195 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2196 __be32 saddr, __be32 daddr,
2197 struct rtmsg *rtm, int nowait, u32 portid)
2199 struct mfc_cache *cache;
2200 struct mr_table *mrt;
2201 int err;
2203 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2204 if (!mrt)
2205 return -ENOENT;
2207 rcu_read_lock();
2208 cache = ipmr_cache_find(mrt, saddr, daddr);
2209 if (!cache && skb->dev) {
2210 int vif = ipmr_find_vif(mrt, skb->dev);
2212 if (vif >= 0)
2213 cache = ipmr_cache_find_any(mrt, daddr, vif);
2215 if (!cache) {
2216 struct sk_buff *skb2;
2217 struct iphdr *iph;
2218 struct net_device *dev;
2219 int vif = -1;
2221 if (nowait) {
2222 rcu_read_unlock();
2223 return -EAGAIN;
2226 dev = skb->dev;
2227 read_lock(&mrt_lock);
2228 if (dev)
2229 vif = ipmr_find_vif(mrt, dev);
2230 if (vif < 0) {
2231 read_unlock(&mrt_lock);
2232 rcu_read_unlock();
2233 return -ENODEV;
2235 skb2 = skb_clone(skb, GFP_ATOMIC);
2236 if (!skb2) {
2237 read_unlock(&mrt_lock);
2238 rcu_read_unlock();
2239 return -ENOMEM;
2242 NETLINK_CB(skb2).portid = portid;
2243 skb_push(skb2, sizeof(struct iphdr));
2244 skb_reset_network_header(skb2);
2245 iph = ip_hdr(skb2);
2246 iph->ihl = sizeof(struct iphdr) >> 2;
2247 iph->saddr = saddr;
2248 iph->daddr = daddr;
2249 iph->version = 0;
2250 err = ipmr_cache_unresolved(mrt, vif, skb2);
2251 read_unlock(&mrt_lock);
2252 rcu_read_unlock();
2253 return err;
2256 read_lock(&mrt_lock);
2257 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2258 cache->mfc_flags |= MFC_NOTIFY;
2259 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2260 read_unlock(&mrt_lock);
2261 rcu_read_unlock();
2262 return err;
2265 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2266 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2267 int flags)
2269 struct nlmsghdr *nlh;
2270 struct rtmsg *rtm;
2271 int err;
2273 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2274 if (!nlh)
2275 return -EMSGSIZE;
2277 rtm = nlmsg_data(nlh);
2278 rtm->rtm_family = RTNL_FAMILY_IPMR;
2279 rtm->rtm_dst_len = 32;
2280 rtm->rtm_src_len = 32;
2281 rtm->rtm_tos = 0;
2282 rtm->rtm_table = mrt->id;
2283 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2284 goto nla_put_failure;
2285 rtm->rtm_type = RTN_MULTICAST;
2286 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2287 if (c->mfc_flags & MFC_STATIC)
2288 rtm->rtm_protocol = RTPROT_STATIC;
2289 else
2290 rtm->rtm_protocol = RTPROT_MROUTED;
2291 rtm->rtm_flags = 0;
2293 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2294 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2295 goto nla_put_failure;
2296 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2297 /* do not break the dump if cache is unresolved */
2298 if (err < 0 && err != -ENOENT)
2299 goto nla_put_failure;
2301 nlmsg_end(skb, nlh);
2302 return 0;
2304 nla_put_failure:
2305 nlmsg_cancel(skb, nlh);
2306 return -EMSGSIZE;
2309 static size_t mroute_msgsize(bool unresolved, int maxvif)
2311 size_t len =
2312 NLMSG_ALIGN(sizeof(struct rtmsg))
2313 + nla_total_size(4) /* RTA_TABLE */
2314 + nla_total_size(4) /* RTA_SRC */
2315 + nla_total_size(4) /* RTA_DST */
2318 if (!unresolved)
2319 len = len
2320 + nla_total_size(4) /* RTA_IIF */
2321 + nla_total_size(0) /* RTA_MULTIPATH */
2322 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2323 /* RTA_MFC_STATS */
2324 + nla_total_size(sizeof(struct rta_mfc_stats))
2327 return len;
2330 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2331 int cmd)
2333 struct net *net = read_pnet(&mrt->net);
2334 struct sk_buff *skb;
2335 int err = -ENOBUFS;
2337 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2338 GFP_ATOMIC);
2339 if (!skb)
2340 goto errout;
2342 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2343 if (err < 0)
2344 goto errout;
2346 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2347 return;
2349 errout:
2350 kfree_skb(skb);
2351 if (err < 0)
2352 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2355 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2357 struct net *net = sock_net(skb->sk);
2358 struct mr_table *mrt;
2359 struct mfc_cache *mfc;
2360 unsigned int t = 0, s_t;
2361 unsigned int h = 0, s_h;
2362 unsigned int e = 0, s_e;
2364 s_t = cb->args[0];
2365 s_h = cb->args[1];
2366 s_e = cb->args[2];
2368 rcu_read_lock();
2369 ipmr_for_each_table(mrt, net) {
2370 if (t < s_t)
2371 goto next_table;
2372 if (t > s_t)
2373 s_h = 0;
2374 for (h = s_h; h < MFC_LINES; h++) {
2375 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2376 if (e < s_e)
2377 goto next_entry;
2378 if (ipmr_fill_mroute(mrt, skb,
2379 NETLINK_CB(cb->skb).portid,
2380 cb->nlh->nlmsg_seq,
2381 mfc, RTM_NEWROUTE,
2382 NLM_F_MULTI) < 0)
2383 goto done;
2384 next_entry:
2385 e++;
2387 e = s_e = 0;
2389 spin_lock_bh(&mfc_unres_lock);
2390 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2391 if (e < s_e)
2392 goto next_entry2;
2393 if (ipmr_fill_mroute(mrt, skb,
2394 NETLINK_CB(cb->skb).portid,
2395 cb->nlh->nlmsg_seq,
2396 mfc, RTM_NEWROUTE,
2397 NLM_F_MULTI) < 0) {
2398 spin_unlock_bh(&mfc_unres_lock);
2399 goto done;
2401 next_entry2:
2402 e++;
2404 spin_unlock_bh(&mfc_unres_lock);
2405 e = s_e = 0;
2406 s_h = 0;
2407 next_table:
2408 t++;
2410 done:
2411 rcu_read_unlock();
2413 cb->args[2] = e;
2414 cb->args[1] = h;
2415 cb->args[0] = t;
2417 return skb->len;
2420 #ifdef CONFIG_PROC_FS
2422 * The /proc interfaces to multicast routing :
2423 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2425 struct ipmr_vif_iter {
2426 struct seq_net_private p;
2427 struct mr_table *mrt;
2428 int ct;
2431 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2432 struct ipmr_vif_iter *iter,
2433 loff_t pos)
2435 struct mr_table *mrt = iter->mrt;
2437 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2438 if (!VIF_EXISTS(mrt, iter->ct))
2439 continue;
2440 if (pos-- == 0)
2441 return &mrt->vif_table[iter->ct];
2443 return NULL;
2446 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2447 __acquires(mrt_lock)
2449 struct ipmr_vif_iter *iter = seq->private;
2450 struct net *net = seq_file_net(seq);
2451 struct mr_table *mrt;
2453 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2454 if (!mrt)
2455 return ERR_PTR(-ENOENT);
2457 iter->mrt = mrt;
2459 read_lock(&mrt_lock);
2460 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2461 : SEQ_START_TOKEN;
2464 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2466 struct ipmr_vif_iter *iter = seq->private;
2467 struct net *net = seq_file_net(seq);
2468 struct mr_table *mrt = iter->mrt;
2470 ++*pos;
2471 if (v == SEQ_START_TOKEN)
2472 return ipmr_vif_seq_idx(net, iter, 0);
2474 while (++iter->ct < mrt->maxvif) {
2475 if (!VIF_EXISTS(mrt, iter->ct))
2476 continue;
2477 return &mrt->vif_table[iter->ct];
2479 return NULL;
2482 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2483 __releases(mrt_lock)
2485 read_unlock(&mrt_lock);
2488 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2490 struct ipmr_vif_iter *iter = seq->private;
2491 struct mr_table *mrt = iter->mrt;
2493 if (v == SEQ_START_TOKEN) {
2494 seq_puts(seq,
2495 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2496 } else {
2497 const struct vif_device *vif = v;
2498 const char *name = vif->dev ? vif->dev->name : "none";
2500 seq_printf(seq,
2501 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2502 vif - mrt->vif_table,
2503 name, vif->bytes_in, vif->pkt_in,
2504 vif->bytes_out, vif->pkt_out,
2505 vif->flags, vif->local, vif->remote);
2507 return 0;
2510 static const struct seq_operations ipmr_vif_seq_ops = {
2511 .start = ipmr_vif_seq_start,
2512 .next = ipmr_vif_seq_next,
2513 .stop = ipmr_vif_seq_stop,
2514 .show = ipmr_vif_seq_show,
2517 static int ipmr_vif_open(struct inode *inode, struct file *file)
2519 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2520 sizeof(struct ipmr_vif_iter));
2523 static const struct file_operations ipmr_vif_fops = {
2524 .owner = THIS_MODULE,
2525 .open = ipmr_vif_open,
2526 .read = seq_read,
2527 .llseek = seq_lseek,
2528 .release = seq_release_net,
2531 struct ipmr_mfc_iter {
2532 struct seq_net_private p;
2533 struct mr_table *mrt;
2534 struct list_head *cache;
2535 int ct;
2539 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2540 struct ipmr_mfc_iter *it, loff_t pos)
2542 struct mr_table *mrt = it->mrt;
2543 struct mfc_cache *mfc;
2545 rcu_read_lock();
2546 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2547 it->cache = &mrt->mfc_cache_array[it->ct];
2548 list_for_each_entry_rcu(mfc, it->cache, list)
2549 if (pos-- == 0)
2550 return mfc;
2552 rcu_read_unlock();
2554 spin_lock_bh(&mfc_unres_lock);
2555 it->cache = &mrt->mfc_unres_queue;
2556 list_for_each_entry(mfc, it->cache, list)
2557 if (pos-- == 0)
2558 return mfc;
2559 spin_unlock_bh(&mfc_unres_lock);
2561 it->cache = NULL;
2562 return NULL;
2566 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2568 struct ipmr_mfc_iter *it = seq->private;
2569 struct net *net = seq_file_net(seq);
2570 struct mr_table *mrt;
2572 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2573 if (!mrt)
2574 return ERR_PTR(-ENOENT);
2576 it->mrt = mrt;
2577 it->cache = NULL;
2578 it->ct = 0;
2579 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2580 : SEQ_START_TOKEN;
2583 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2585 struct mfc_cache *mfc = v;
2586 struct ipmr_mfc_iter *it = seq->private;
2587 struct net *net = seq_file_net(seq);
2588 struct mr_table *mrt = it->mrt;
2590 ++*pos;
2592 if (v == SEQ_START_TOKEN)
2593 return ipmr_mfc_seq_idx(net, seq->private, 0);
2595 if (mfc->list.next != it->cache)
2596 return list_entry(mfc->list.next, struct mfc_cache, list);
2598 if (it->cache == &mrt->mfc_unres_queue)
2599 goto end_of_list;
2601 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2603 while (++it->ct < MFC_LINES) {
2604 it->cache = &mrt->mfc_cache_array[it->ct];
2605 if (list_empty(it->cache))
2606 continue;
2607 return list_first_entry(it->cache, struct mfc_cache, list);
2610 /* exhausted cache_array, show unresolved */
2611 rcu_read_unlock();
2612 it->cache = &mrt->mfc_unres_queue;
2613 it->ct = 0;
2615 spin_lock_bh(&mfc_unres_lock);
2616 if (!list_empty(it->cache))
2617 return list_first_entry(it->cache, struct mfc_cache, list);
2619 end_of_list:
2620 spin_unlock_bh(&mfc_unres_lock);
2621 it->cache = NULL;
2623 return NULL;
2626 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2628 struct ipmr_mfc_iter *it = seq->private;
2629 struct mr_table *mrt = it->mrt;
2631 if (it->cache == &mrt->mfc_unres_queue)
2632 spin_unlock_bh(&mfc_unres_lock);
2633 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2634 rcu_read_unlock();
2637 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2639 int n;
2641 if (v == SEQ_START_TOKEN) {
2642 seq_puts(seq,
2643 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2644 } else {
2645 const struct mfc_cache *mfc = v;
2646 const struct ipmr_mfc_iter *it = seq->private;
2647 const struct mr_table *mrt = it->mrt;
2649 seq_printf(seq, "%08X %08X %-3hd",
2650 (__force u32) mfc->mfc_mcastgrp,
2651 (__force u32) mfc->mfc_origin,
2652 mfc->mfc_parent);
2654 if (it->cache != &mrt->mfc_unres_queue) {
2655 seq_printf(seq, " %8lu %8lu %8lu",
2656 mfc->mfc_un.res.pkt,
2657 mfc->mfc_un.res.bytes,
2658 mfc->mfc_un.res.wrong_if);
2659 for (n = mfc->mfc_un.res.minvif;
2660 n < mfc->mfc_un.res.maxvif; n++) {
2661 if (VIF_EXISTS(mrt, n) &&
2662 mfc->mfc_un.res.ttls[n] < 255)
2663 seq_printf(seq,
2664 " %2d:%-3d",
2665 n, mfc->mfc_un.res.ttls[n]);
2667 } else {
2668 /* unresolved mfc_caches don't contain
2669 * pkt, bytes and wrong_if values
2671 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2673 seq_putc(seq, '\n');
2675 return 0;
2678 static const struct seq_operations ipmr_mfc_seq_ops = {
2679 .start = ipmr_mfc_seq_start,
2680 .next = ipmr_mfc_seq_next,
2681 .stop = ipmr_mfc_seq_stop,
2682 .show = ipmr_mfc_seq_show,
2685 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2687 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2688 sizeof(struct ipmr_mfc_iter));
2691 static const struct file_operations ipmr_mfc_fops = {
2692 .owner = THIS_MODULE,
2693 .open = ipmr_mfc_open,
2694 .read = seq_read,
2695 .llseek = seq_lseek,
2696 .release = seq_release_net,
2698 #endif
2700 #ifdef CONFIG_IP_PIMSM_V2
2701 static const struct net_protocol pim_protocol = {
2702 .handler = pim_rcv,
2703 .netns_ok = 1,
2705 #endif
2709 * Setup for IP multicast routing
2711 static int __net_init ipmr_net_init(struct net *net)
2713 int err;
2715 err = ipmr_rules_init(net);
2716 if (err < 0)
2717 goto fail;
2719 #ifdef CONFIG_PROC_FS
2720 err = -ENOMEM;
2721 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
2722 goto proc_vif_fail;
2723 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
2724 goto proc_cache_fail;
2725 #endif
2726 return 0;
2728 #ifdef CONFIG_PROC_FS
2729 proc_cache_fail:
2730 remove_proc_entry("ip_mr_vif", net->proc_net);
2731 proc_vif_fail:
2732 ipmr_rules_exit(net);
2733 #endif
2734 fail:
2735 return err;
2738 static void __net_exit ipmr_net_exit(struct net *net)
2740 #ifdef CONFIG_PROC_FS
2741 remove_proc_entry("ip_mr_cache", net->proc_net);
2742 remove_proc_entry("ip_mr_vif", net->proc_net);
2743 #endif
2744 ipmr_rules_exit(net);
2747 static struct pernet_operations ipmr_net_ops = {
2748 .init = ipmr_net_init,
2749 .exit = ipmr_net_exit,
2752 int __init ip_mr_init(void)
2754 int err;
2756 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2757 sizeof(struct mfc_cache),
2758 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2759 NULL);
2760 if (!mrt_cachep)
2761 return -ENOMEM;
2763 err = register_pernet_subsys(&ipmr_net_ops);
2764 if (err)
2765 goto reg_pernet_fail;
2767 err = register_netdevice_notifier(&ip_mr_notifier);
2768 if (err)
2769 goto reg_notif_fail;
2770 #ifdef CONFIG_IP_PIMSM_V2
2771 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2772 pr_err("%s: can't add PIM protocol\n", __func__);
2773 err = -EAGAIN;
2774 goto add_proto_fail;
2776 #endif
2777 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2778 NULL, ipmr_rtm_dumproute, NULL);
2779 return 0;
2781 #ifdef CONFIG_IP_PIMSM_V2
2782 add_proto_fail:
2783 unregister_netdevice_notifier(&ip_mr_notifier);
2784 #endif
2785 reg_notif_fail:
2786 unregister_pernet_subsys(&ipmr_net_ops);
2787 reg_pernet_fail:
2788 kmem_cache_destroy(mrt_cachep);
2789 return err;