Revert "net: ipv4: ip_forward: fix inverted local_df test"
[pandora-kernel.git] / net / ipv4 / ipmr.c
1 /*
2  *      IP multicast routing support for mrouted 3.6/3.8
3  *
4  *              (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
5  *        Linux Consultancy and Custom Driver Development
6  *
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.
11  *
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.
26  *
27  */
28
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <linux/types.h>
32 #include <linux/capability.h>
33 #include <linux/errno.h>
34 #include <linux/timer.h>
35 #include <linux/mm.h>
36 #include <linux/kernel.h>
37 #include <linux/fcntl.h>
38 #include <linux/stat.h>
39 #include <linux/socket.h>
40 #include <linux/in.h>
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/inetdevice.h>
44 #include <linux/igmp.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <linux/mroute.h>
48 #include <linux/init.h>
49 #include <linux/if_ether.h>
50 #include <linux/slab.h>
51 #include <net/net_namespace.h>
52 #include <net/ip.h>
53 #include <net/protocol.h>
54 #include <linux/skbuff.h>
55 #include <net/route.h>
56 #include <net/sock.h>
57 #include <net/icmp.h>
58 #include <net/udp.h>
59 #include <net/raw.h>
60 #include <linux/notifier.h>
61 #include <linux/if_arp.h>
62 #include <linux/netfilter_ipv4.h>
63 #include <linux/compat.h>
64 #include <linux/export.h>
65 #include <net/ipip.h>
66 #include <net/checksum.h>
67 #include <net/netlink.h>
68 #include <net/fib_rules.h>
69
70 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71 #define CONFIG_IP_PIMSM 1
72 #endif
73
74 struct mr_table {
75         struct list_head        list;
76 #ifdef CONFIG_NET_NS
77         struct net              *net;
78 #endif
79         u32                     id;
80         struct sock __rcu       *mroute_sk;
81         struct timer_list       ipmr_expire_timer;
82         struct list_head        mfc_unres_queue;
83         struct list_head        mfc_cache_array[MFC_LINES];
84         struct vif_device       vif_table[MAXVIFS];
85         int                     maxvif;
86         atomic_t                cache_resolve_queue_len;
87         int                     mroute_do_assert;
88         int                     mroute_do_pim;
89 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90         int                     mroute_reg_vif_num;
91 #endif
92 };
93
94 struct ipmr_rule {
95         struct fib_rule         common;
96 };
97
98 struct ipmr_result {
99         struct mr_table         *mrt;
100 };
101
102 /* Big lock, protecting vif table, mrt cache and mroute socket state.
103  * Note that the changes are semaphored via rtnl_lock.
104  */
105
106 static DEFINE_RWLOCK(mrt_lock);
107
108 /*
109  *      Multicast router control variables
110  */
111
112 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
113
114 /* Special spinlock for queue of unresolved entries */
115 static DEFINE_SPINLOCK(mfc_unres_lock);
116
117 /* We return to original Alan's scheme. Hash table of resolved
118  * entries is changed only in process context and protected
119  * with weak lock mrt_lock. Queue of unresolved entries is protected
120  * with strong spinlock mfc_unres_lock.
121  *
122  * In this case data path is free of exclusive locks at all.
123  */
124
125 static struct kmem_cache *mrt_cachep __read_mostly;
126
127 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
128 static void ipmr_free_table(struct mr_table *mrt);
129
130 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
131                          struct sk_buff *skb, struct mfc_cache *cache,
132                          int local);
133 static int ipmr_cache_report(struct mr_table *mrt,
134                              struct sk_buff *pkt, vifi_t vifi, int assert);
135 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136                               struct mfc_cache *c, struct rtmsg *rtm);
137 static void mroute_clean_tables(struct mr_table *mrt);
138 static void ipmr_expire_process(unsigned long arg);
139
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)
143
144 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
145 {
146         struct mr_table *mrt;
147
148         ipmr_for_each_table(mrt, net) {
149                 if (mrt->id == id)
150                         return mrt;
151         }
152         return NULL;
153 }
154
155 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
156                            struct mr_table **mrt)
157 {
158         int err;
159         struct ipmr_result res;
160         struct fib_lookup_arg arg = {
161                 .result = &res,
162                 .flags = FIB_LOOKUP_NOREF,
163         };
164
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;
171 }
172
173 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
174                             int flags, struct fib_lookup_arg *arg)
175 {
176         struct ipmr_result *res = arg->result;
177         struct mr_table *mrt;
178
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;
189         }
190
191         mrt = ipmr_get_table(rule->fr_net, rule->table);
192         if (mrt == NULL)
193                 return -EAGAIN;
194         res->mrt = mrt;
195         return 0;
196 }
197
198 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
199 {
200         return 1;
201 }
202
203 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
204         FRA_GENERIC_POLICY,
205 };
206
207 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
208                                struct fib_rule_hdr *frh, struct nlattr **tb)
209 {
210         return 0;
211 }
212
213 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
214                              struct nlattr **tb)
215 {
216         return 1;
217 }
218
219 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
220                           struct fib_rule_hdr *frh)
221 {
222         frh->dst_len = 0;
223         frh->src_len = 0;
224         frh->tos     = 0;
225         return 0;
226 }
227
228 static const struct fib_rules_ops __net_initdata 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,
241 };
242
243 static int __net_init ipmr_rules_init(struct net *net)
244 {
245         struct fib_rules_ops *ops;
246         struct mr_table *mrt;
247         int err;
248
249         ops = fib_rules_register(&ipmr_rules_ops_template, net);
250         if (IS_ERR(ops))
251                 return PTR_ERR(ops);
252
253         INIT_LIST_HEAD(&net->ipv4.mr_tables);
254
255         mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
256         if (mrt == NULL) {
257                 err = -ENOMEM;
258                 goto err1;
259         }
260
261         err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
262         if (err < 0)
263                 goto err2;
264
265         net->ipv4.mr_rules_ops = ops;
266         return 0;
267
268 err2:
269         kfree(mrt);
270 err1:
271         fib_rules_unregister(ops);
272         return err;
273 }
274
275 static void __net_exit ipmr_rules_exit(struct net *net)
276 {
277         struct mr_table *mrt, *next;
278
279         list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
280                 list_del(&mrt->list);
281                 ipmr_free_table(mrt);
282         }
283         fib_rules_unregister(net->ipv4.mr_rules_ops);
284 }
285 #else
286 #define ipmr_for_each_table(mrt, net) \
287         for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
288
289 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
290 {
291         return net->ipv4.mrt;
292 }
293
294 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
295                            struct mr_table **mrt)
296 {
297         *mrt = net->ipv4.mrt;
298         return 0;
299 }
300
301 static int __net_init ipmr_rules_init(struct net *net)
302 {
303         net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
304         return net->ipv4.mrt ? 0 : -ENOMEM;
305 }
306
307 static void __net_exit ipmr_rules_exit(struct net *net)
308 {
309         ipmr_free_table(net->ipv4.mrt);
310 }
311 #endif
312
313 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
314 {
315         struct mr_table *mrt;
316         unsigned int i;
317
318         mrt = ipmr_get_table(net, id);
319         if (mrt != NULL)
320                 return mrt;
321
322         mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
323         if (mrt == NULL)
324                 return NULL;
325         write_pnet(&mrt->net, net);
326         mrt->id = id;
327
328         /* Forwarding cache */
329         for (i = 0; i < MFC_LINES; i++)
330                 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
331
332         INIT_LIST_HEAD(&mrt->mfc_unres_queue);
333
334         setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
335                     (unsigned long)mrt);
336
337 #ifdef CONFIG_IP_PIMSM
338         mrt->mroute_reg_vif_num = -1;
339 #endif
340 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
341         list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
342 #endif
343         return mrt;
344 }
345
346 static void ipmr_free_table(struct mr_table *mrt)
347 {
348         del_timer_sync(&mrt->ipmr_expire_timer);
349         mroute_clean_tables(mrt);
350         kfree(mrt);
351 }
352
353 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
354
355 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
356 {
357         struct net *net = dev_net(dev);
358
359         dev_close(dev);
360
361         dev = __dev_get_by_name(net, "tunl0");
362         if (dev) {
363                 const struct net_device_ops *ops = dev->netdev_ops;
364                 struct ifreq ifr;
365                 struct ip_tunnel_parm p;
366
367                 memset(&p, 0, sizeof(p));
368                 p.iph.daddr = v->vifc_rmt_addr.s_addr;
369                 p.iph.saddr = v->vifc_lcl_addr.s_addr;
370                 p.iph.version = 4;
371                 p.iph.ihl = 5;
372                 p.iph.protocol = IPPROTO_IPIP;
373                 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
374                 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
375
376                 if (ops->ndo_do_ioctl) {
377                         mm_segment_t oldfs = get_fs();
378
379                         set_fs(KERNEL_DS);
380                         ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
381                         set_fs(oldfs);
382                 }
383         }
384 }
385
386 static
387 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
388 {
389         struct net_device  *dev;
390
391         dev = __dev_get_by_name(net, "tunl0");
392
393         if (dev) {
394                 const struct net_device_ops *ops = dev->netdev_ops;
395                 int err;
396                 struct ifreq ifr;
397                 struct ip_tunnel_parm p;
398                 struct in_device  *in_dev;
399
400                 memset(&p, 0, sizeof(p));
401                 p.iph.daddr = v->vifc_rmt_addr.s_addr;
402                 p.iph.saddr = v->vifc_lcl_addr.s_addr;
403                 p.iph.version = 4;
404                 p.iph.ihl = 5;
405                 p.iph.protocol = IPPROTO_IPIP;
406                 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
407                 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
408
409                 if (ops->ndo_do_ioctl) {
410                         mm_segment_t oldfs = get_fs();
411
412                         set_fs(KERNEL_DS);
413                         err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
414                         set_fs(oldfs);
415                 } else {
416                         err = -EOPNOTSUPP;
417                 }
418                 dev = NULL;
419
420                 if (err == 0 &&
421                     (dev = __dev_get_by_name(net, p.name)) != NULL) {
422                         dev->flags |= IFF_MULTICAST;
423
424                         in_dev = __in_dev_get_rtnl(dev);
425                         if (in_dev == NULL)
426                                 goto failure;
427
428                         ipv4_devconf_setall(in_dev);
429                         IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
430
431                         if (dev_open(dev))
432                                 goto failure;
433                         dev_hold(dev);
434                 }
435         }
436         return dev;
437
438 failure:
439         /* allow the register to be completed before unregistering. */
440         rtnl_unlock();
441         rtnl_lock();
442
443         unregister_netdevice(dev);
444         return NULL;
445 }
446
447 #ifdef CONFIG_IP_PIMSM
448
449 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
450 {
451         struct net *net = dev_net(dev);
452         struct mr_table *mrt;
453         struct flowi4 fl4 = {
454                 .flowi4_oif     = dev->ifindex,
455                 .flowi4_iif     = skb->skb_iif,
456                 .flowi4_mark    = skb->mark,
457         };
458         int err;
459
460         err = ipmr_fib_lookup(net, &fl4, &mrt);
461         if (err < 0) {
462                 kfree_skb(skb);
463                 return err;
464         }
465
466         read_lock(&mrt_lock);
467         dev->stats.tx_bytes += skb->len;
468         dev->stats.tx_packets++;
469         ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
470         read_unlock(&mrt_lock);
471         kfree_skb(skb);
472         return NETDEV_TX_OK;
473 }
474
475 static const struct net_device_ops reg_vif_netdev_ops = {
476         .ndo_start_xmit = reg_vif_xmit,
477 };
478
479 static void reg_vif_setup(struct net_device *dev)
480 {
481         dev->type               = ARPHRD_PIMREG;
482         dev->mtu                = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
483         dev->flags              = IFF_NOARP;
484         dev->netdev_ops         = &reg_vif_netdev_ops,
485         dev->destructor         = free_netdev;
486         dev->features           |= NETIF_F_NETNS_LOCAL;
487 }
488
489 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
490 {
491         struct net_device *dev;
492         struct in_device *in_dev;
493         char name[IFNAMSIZ];
494
495         if (mrt->id == RT_TABLE_DEFAULT)
496                 sprintf(name, "pimreg");
497         else
498                 sprintf(name, "pimreg%u", mrt->id);
499
500         dev = alloc_netdev(0, name, reg_vif_setup);
501
502         if (dev == NULL)
503                 return NULL;
504
505         dev_net_set(dev, net);
506
507         if (register_netdevice(dev)) {
508                 free_netdev(dev);
509                 return NULL;
510         }
511         dev->iflink = 0;
512
513         rcu_read_lock();
514         in_dev = __in_dev_get_rcu(dev);
515         if (!in_dev) {
516                 rcu_read_unlock();
517                 goto failure;
518         }
519
520         ipv4_devconf_setall(in_dev);
521         IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
522         rcu_read_unlock();
523
524         if (dev_open(dev))
525                 goto failure;
526
527         dev_hold(dev);
528
529         return dev;
530
531 failure:
532         /* allow the register to be completed before unregistering. */
533         rtnl_unlock();
534         rtnl_lock();
535
536         unregister_netdevice(dev);
537         return NULL;
538 }
539 #endif
540
541 /*
542  *      Delete a VIF entry
543  *      @notify: Set to 1, if the caller is a notifier_call
544  */
545
546 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
547                       struct list_head *head)
548 {
549         struct vif_device *v;
550         struct net_device *dev;
551         struct in_device *in_dev;
552
553         if (vifi < 0 || vifi >= mrt->maxvif)
554                 return -EADDRNOTAVAIL;
555
556         v = &mrt->vif_table[vifi];
557
558         write_lock_bh(&mrt_lock);
559         dev = v->dev;
560         v->dev = NULL;
561
562         if (!dev) {
563                 write_unlock_bh(&mrt_lock);
564                 return -EADDRNOTAVAIL;
565         }
566
567 #ifdef CONFIG_IP_PIMSM
568         if (vifi == mrt->mroute_reg_vif_num)
569                 mrt->mroute_reg_vif_num = -1;
570 #endif
571
572         if (vifi + 1 == mrt->maxvif) {
573                 int tmp;
574
575                 for (tmp = vifi - 1; tmp >= 0; tmp--) {
576                         if (VIF_EXISTS(mrt, tmp))
577                                 break;
578                 }
579                 mrt->maxvif = tmp+1;
580         }
581
582         write_unlock_bh(&mrt_lock);
583
584         dev_set_allmulti(dev, -1);
585
586         in_dev = __in_dev_get_rtnl(dev);
587         if (in_dev) {
588                 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
589                 ip_rt_multicast_event(in_dev);
590         }
591
592         if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
593                 unregister_netdevice_queue(dev, head);
594
595         dev_put(dev);
596         return 0;
597 }
598
599 static void ipmr_cache_free_rcu(struct rcu_head *head)
600 {
601         struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
602
603         kmem_cache_free(mrt_cachep, c);
604 }
605
606 static inline void ipmr_cache_free(struct mfc_cache *c)
607 {
608         call_rcu(&c->rcu, ipmr_cache_free_rcu);
609 }
610
611 /* Destroy an unresolved cache entry, killing queued skbs
612  * and reporting error to netlink readers.
613  */
614
615 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
616 {
617         struct net *net = read_pnet(&mrt->net);
618         struct sk_buff *skb;
619         struct nlmsgerr *e;
620
621         atomic_dec(&mrt->cache_resolve_queue_len);
622
623         while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
624                 if (ip_hdr(skb)->version == 0) {
625                         struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
626                         nlh->nlmsg_type = NLMSG_ERROR;
627                         nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
628                         skb_trim(skb, nlh->nlmsg_len);
629                         e = NLMSG_DATA(nlh);
630                         e->error = -ETIMEDOUT;
631                         memset(&e->msg, 0, sizeof(e->msg));
632
633                         rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
634                 } else {
635                         kfree_skb(skb);
636                 }
637         }
638
639         ipmr_cache_free(c);
640 }
641
642
643 /* Timer process for the unresolved queue. */
644
645 static void ipmr_expire_process(unsigned long arg)
646 {
647         struct mr_table *mrt = (struct mr_table *)arg;
648         unsigned long now;
649         unsigned long expires;
650         struct mfc_cache *c, *next;
651
652         if (!spin_trylock(&mfc_unres_lock)) {
653                 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
654                 return;
655         }
656
657         if (list_empty(&mrt->mfc_unres_queue))
658                 goto out;
659
660         now = jiffies;
661         expires = 10*HZ;
662
663         list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
664                 if (time_after(c->mfc_un.unres.expires, now)) {
665                         unsigned long interval = c->mfc_un.unres.expires - now;
666                         if (interval < expires)
667                                 expires = interval;
668                         continue;
669                 }
670
671                 list_del(&c->list);
672                 ipmr_destroy_unres(mrt, c);
673         }
674
675         if (!list_empty(&mrt->mfc_unres_queue))
676                 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
677
678 out:
679         spin_unlock(&mfc_unres_lock);
680 }
681
682 /* Fill oifs list. It is called under write locked mrt_lock. */
683
684 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
685                                    unsigned char *ttls)
686 {
687         int vifi;
688
689         cache->mfc_un.res.minvif = MAXVIFS;
690         cache->mfc_un.res.maxvif = 0;
691         memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
692
693         for (vifi = 0; vifi < mrt->maxvif; vifi++) {
694                 if (VIF_EXISTS(mrt, vifi) &&
695                     ttls[vifi] && ttls[vifi] < 255) {
696                         cache->mfc_un.res.ttls[vifi] = ttls[vifi];
697                         if (cache->mfc_un.res.minvif > vifi)
698                                 cache->mfc_un.res.minvif = vifi;
699                         if (cache->mfc_un.res.maxvif <= vifi)
700                                 cache->mfc_un.res.maxvif = vifi + 1;
701                 }
702         }
703 }
704
705 static int vif_add(struct net *net, struct mr_table *mrt,
706                    struct vifctl *vifc, int mrtsock)
707 {
708         int vifi = vifc->vifc_vifi;
709         struct vif_device *v = &mrt->vif_table[vifi];
710         struct net_device *dev;
711         struct in_device *in_dev;
712         int err;
713
714         /* Is vif busy ? */
715         if (VIF_EXISTS(mrt, vifi))
716                 return -EADDRINUSE;
717
718         switch (vifc->vifc_flags) {
719 #ifdef CONFIG_IP_PIMSM
720         case VIFF_REGISTER:
721                 /*
722                  * Special Purpose VIF in PIM
723                  * All the packets will be sent to the daemon
724                  */
725                 if (mrt->mroute_reg_vif_num >= 0)
726                         return -EADDRINUSE;
727                 dev = ipmr_reg_vif(net, mrt);
728                 if (!dev)
729                         return -ENOBUFS;
730                 err = dev_set_allmulti(dev, 1);
731                 if (err) {
732                         unregister_netdevice(dev);
733                         dev_put(dev);
734                         return err;
735                 }
736                 break;
737 #endif
738         case VIFF_TUNNEL:
739                 dev = ipmr_new_tunnel(net, vifc);
740                 if (!dev)
741                         return -ENOBUFS;
742                 err = dev_set_allmulti(dev, 1);
743                 if (err) {
744                         ipmr_del_tunnel(dev, vifc);
745                         dev_put(dev);
746                         return err;
747                 }
748                 break;
749
750         case VIFF_USE_IFINDEX:
751         case 0:
752                 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
753                         dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
754                         if (dev && __in_dev_get_rtnl(dev) == NULL) {
755                                 dev_put(dev);
756                                 return -EADDRNOTAVAIL;
757                         }
758                 } else {
759                         dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
760                 }
761                 if (!dev)
762                         return -EADDRNOTAVAIL;
763                 err = dev_set_allmulti(dev, 1);
764                 if (err) {
765                         dev_put(dev);
766                         return err;
767                 }
768                 break;
769         default:
770                 return -EINVAL;
771         }
772
773         in_dev = __in_dev_get_rtnl(dev);
774         if (!in_dev) {
775                 dev_put(dev);
776                 return -EADDRNOTAVAIL;
777         }
778         IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
779         ip_rt_multicast_event(in_dev);
780
781         /* Fill in the VIF structures */
782
783         v->rate_limit = vifc->vifc_rate_limit;
784         v->local = vifc->vifc_lcl_addr.s_addr;
785         v->remote = vifc->vifc_rmt_addr.s_addr;
786         v->flags = vifc->vifc_flags;
787         if (!mrtsock)
788                 v->flags |= VIFF_STATIC;
789         v->threshold = vifc->vifc_threshold;
790         v->bytes_in = 0;
791         v->bytes_out = 0;
792         v->pkt_in = 0;
793         v->pkt_out = 0;
794         v->link = dev->ifindex;
795         if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
796                 v->link = dev->iflink;
797
798         /* And finish update writing critical data */
799         write_lock_bh(&mrt_lock);
800         v->dev = dev;
801 #ifdef CONFIG_IP_PIMSM
802         if (v->flags & VIFF_REGISTER)
803                 mrt->mroute_reg_vif_num = vifi;
804 #endif
805         if (vifi+1 > mrt->maxvif)
806                 mrt->maxvif = vifi+1;
807         write_unlock_bh(&mrt_lock);
808         return 0;
809 }
810
811 /* called with rcu_read_lock() */
812 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
813                                          __be32 origin,
814                                          __be32 mcastgrp)
815 {
816         int line = MFC_HASH(mcastgrp, origin);
817         struct mfc_cache *c;
818
819         list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
820                 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
821                         return c;
822         }
823         return NULL;
824 }
825
826 /*
827  *      Allocate a multicast cache entry
828  */
829 static struct mfc_cache *ipmr_cache_alloc(void)
830 {
831         struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
832
833         if (c)
834                 c->mfc_un.res.minvif = MAXVIFS;
835         return c;
836 }
837
838 static struct mfc_cache *ipmr_cache_alloc_unres(void)
839 {
840         struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
841
842         if (c) {
843                 skb_queue_head_init(&c->mfc_un.unres.unresolved);
844                 c->mfc_un.unres.expires = jiffies + 10*HZ;
845         }
846         return c;
847 }
848
849 /*
850  *      A cache entry has gone into a resolved state from queued
851  */
852
853 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
854                                struct mfc_cache *uc, struct mfc_cache *c)
855 {
856         struct sk_buff *skb;
857         struct nlmsgerr *e;
858
859         /* Play the pending entries through our router */
860
861         while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
862                 if (ip_hdr(skb)->version == 0) {
863                         struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
864
865                         if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
866                                 nlh->nlmsg_len = skb_tail_pointer(skb) -
867                                                  (u8 *)nlh;
868                         } else {
869                                 nlh->nlmsg_type = NLMSG_ERROR;
870                                 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
871                                 skb_trim(skb, nlh->nlmsg_len);
872                                 e = NLMSG_DATA(nlh);
873                                 e->error = -EMSGSIZE;
874                                 memset(&e->msg, 0, sizeof(e->msg));
875                         }
876
877                         rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
878                 } else {
879                         ip_mr_forward(net, mrt, skb, c, 0);
880                 }
881         }
882 }
883
884 /*
885  *      Bounce a cache query up to mrouted. We could use netlink for this but mrouted
886  *      expects the following bizarre scheme.
887  *
888  *      Called under mrt_lock.
889  */
890
891 static int ipmr_cache_report(struct mr_table *mrt,
892                              struct sk_buff *pkt, vifi_t vifi, int assert)
893 {
894         struct sk_buff *skb;
895         const int ihl = ip_hdrlen(pkt);
896         struct igmphdr *igmp;
897         struct igmpmsg *msg;
898         struct sock *mroute_sk;
899         int ret;
900
901 #ifdef CONFIG_IP_PIMSM
902         if (assert == IGMPMSG_WHOLEPKT)
903                 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
904         else
905 #endif
906                 skb = alloc_skb(128, GFP_ATOMIC);
907
908         if (!skb)
909                 return -ENOBUFS;
910
911 #ifdef CONFIG_IP_PIMSM
912         if (assert == IGMPMSG_WHOLEPKT) {
913                 /* Ugly, but we have no choice with this interface.
914                  * Duplicate old header, fix ihl, length etc.
915                  * And all this only to mangle msg->im_msgtype and
916                  * to set msg->im_mbz to "mbz" :-)
917                  */
918                 skb_push(skb, sizeof(struct iphdr));
919                 skb_reset_network_header(skb);
920                 skb_reset_transport_header(skb);
921                 msg = (struct igmpmsg *)skb_network_header(skb);
922                 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
923                 msg->im_msgtype = IGMPMSG_WHOLEPKT;
924                 msg->im_mbz = 0;
925                 msg->im_vif = mrt->mroute_reg_vif_num;
926                 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
927                 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
928                                              sizeof(struct iphdr));
929         } else
930 #endif
931         {
932
933         /* Copy the IP header */
934
935         skb->network_header = skb->tail;
936         skb_put(skb, ihl);
937         skb_copy_to_linear_data(skb, pkt->data, ihl);
938         ip_hdr(skb)->protocol = 0;      /* Flag to the kernel this is a route add */
939         msg = (struct igmpmsg *)skb_network_header(skb);
940         msg->im_vif = vifi;
941         skb_dst_set(skb, dst_clone(skb_dst(pkt)));
942
943         /* Add our header */
944
945         igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
946         igmp->type      =
947         msg->im_msgtype = assert;
948         igmp->code      = 0;
949         ip_hdr(skb)->tot_len = htons(skb->len);         /* Fix the length */
950         skb->transport_header = skb->network_header;
951         }
952
953         rcu_read_lock();
954         mroute_sk = rcu_dereference(mrt->mroute_sk);
955         if (mroute_sk == NULL) {
956                 rcu_read_unlock();
957                 kfree_skb(skb);
958                 return -EINVAL;
959         }
960
961         /* Deliver to mrouted */
962
963         ret = sock_queue_rcv_skb(mroute_sk, skb);
964         rcu_read_unlock();
965         if (ret < 0) {
966                 if (net_ratelimit())
967                         printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
968                 kfree_skb(skb);
969         }
970
971         return ret;
972 }
973
974 /*
975  *      Queue a packet for resolution. It gets locked cache entry!
976  */
977
978 static int
979 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
980 {
981         bool found = false;
982         int err;
983         struct mfc_cache *c;
984         const struct iphdr *iph = ip_hdr(skb);
985
986         spin_lock_bh(&mfc_unres_lock);
987         list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
988                 if (c->mfc_mcastgrp == iph->daddr &&
989                     c->mfc_origin == iph->saddr) {
990                         found = true;
991                         break;
992                 }
993         }
994
995         if (!found) {
996                 /* Create a new entry if allowable */
997
998                 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
999                     (c = ipmr_cache_alloc_unres()) == NULL) {
1000                         spin_unlock_bh(&mfc_unres_lock);
1001
1002                         kfree_skb(skb);
1003                         return -ENOBUFS;
1004                 }
1005
1006                 /* Fill in the new cache entry */
1007
1008                 c->mfc_parent   = -1;
1009                 c->mfc_origin   = iph->saddr;
1010                 c->mfc_mcastgrp = iph->daddr;
1011
1012                 /* Reflect first query at mrouted. */
1013
1014                 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1015                 if (err < 0) {
1016                         /* If the report failed throw the cache entry
1017                            out - Brad Parker
1018                          */
1019                         spin_unlock_bh(&mfc_unres_lock);
1020
1021                         ipmr_cache_free(c);
1022                         kfree_skb(skb);
1023                         return err;
1024                 }
1025
1026                 atomic_inc(&mrt->cache_resolve_queue_len);
1027                 list_add(&c->list, &mrt->mfc_unres_queue);
1028
1029                 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1030                         mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1031         }
1032
1033         /* See if we can append the packet */
1034
1035         if (c->mfc_un.unres.unresolved.qlen > 3) {
1036                 kfree_skb(skb);
1037                 err = -ENOBUFS;
1038         } else {
1039                 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1040                 err = 0;
1041         }
1042
1043         spin_unlock_bh(&mfc_unres_lock);
1044         return err;
1045 }
1046
1047 /*
1048  *      MFC cache manipulation by user space mroute daemon
1049  */
1050
1051 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1052 {
1053         int line;
1054         struct mfc_cache *c, *next;
1055
1056         line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1057
1058         list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1059                 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1060                     c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1061                         list_del_rcu(&c->list);
1062
1063                         ipmr_cache_free(c);
1064                         return 0;
1065                 }
1066         }
1067         return -ENOENT;
1068 }
1069
1070 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1071                         struct mfcctl *mfc, int mrtsock)
1072 {
1073         bool found = false;
1074         int line;
1075         struct mfc_cache *uc, *c;
1076
1077         if (mfc->mfcc_parent >= MAXVIFS)
1078                 return -ENFILE;
1079
1080         line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1081
1082         list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1083                 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1084                     c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1085                         found = true;
1086                         break;
1087                 }
1088         }
1089
1090         if (found) {
1091                 write_lock_bh(&mrt_lock);
1092                 c->mfc_parent = mfc->mfcc_parent;
1093                 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1094                 if (!mrtsock)
1095                         c->mfc_flags |= MFC_STATIC;
1096                 write_unlock_bh(&mrt_lock);
1097                 return 0;
1098         }
1099
1100         if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1101                 return -EINVAL;
1102
1103         c = ipmr_cache_alloc();
1104         if (c == NULL)
1105                 return -ENOMEM;
1106
1107         c->mfc_origin = mfc->mfcc_origin.s_addr;
1108         c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1109         c->mfc_parent = mfc->mfcc_parent;
1110         ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1111         if (!mrtsock)
1112                 c->mfc_flags |= MFC_STATIC;
1113
1114         list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1115
1116         /*
1117          *      Check to see if we resolved a queued list. If so we
1118          *      need to send on the frames and tidy up.
1119          */
1120         found = false;
1121         spin_lock_bh(&mfc_unres_lock);
1122         list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1123                 if (uc->mfc_origin == c->mfc_origin &&
1124                     uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1125                         list_del(&uc->list);
1126                         atomic_dec(&mrt->cache_resolve_queue_len);
1127                         found = true;
1128                         break;
1129                 }
1130         }
1131         if (list_empty(&mrt->mfc_unres_queue))
1132                 del_timer(&mrt->ipmr_expire_timer);
1133         spin_unlock_bh(&mfc_unres_lock);
1134
1135         if (found) {
1136                 ipmr_cache_resolve(net, mrt, uc, c);
1137                 ipmr_cache_free(uc);
1138         }
1139         return 0;
1140 }
1141
1142 /*
1143  *      Close the multicast socket, and clear the vif tables etc
1144  */
1145
1146 static void mroute_clean_tables(struct mr_table *mrt)
1147 {
1148         int i;
1149         LIST_HEAD(list);
1150         struct mfc_cache *c, *next;
1151
1152         /* Shut down all active vif entries */
1153
1154         for (i = 0; i < mrt->maxvif; i++) {
1155                 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1156                         vif_delete(mrt, i, 0, &list);
1157         }
1158         unregister_netdevice_many(&list);
1159
1160         /* Wipe the cache */
1161
1162         for (i = 0; i < MFC_LINES; i++) {
1163                 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1164                         if (c->mfc_flags & MFC_STATIC)
1165                                 continue;
1166                         list_del_rcu(&c->list);
1167                         ipmr_cache_free(c);
1168                 }
1169         }
1170
1171         if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1172                 spin_lock_bh(&mfc_unres_lock);
1173                 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1174                         list_del(&c->list);
1175                         ipmr_destroy_unres(mrt, c);
1176                 }
1177                 spin_unlock_bh(&mfc_unres_lock);
1178         }
1179 }
1180
1181 /* called from ip_ra_control(), before an RCU grace period,
1182  * we dont need to call synchronize_rcu() here
1183  */
1184 static void mrtsock_destruct(struct sock *sk)
1185 {
1186         struct net *net = sock_net(sk);
1187         struct mr_table *mrt;
1188
1189         rtnl_lock();
1190         ipmr_for_each_table(mrt, net) {
1191                 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1192                         IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1193                         RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1194                         mroute_clean_tables(mrt);
1195                 }
1196         }
1197         rtnl_unlock();
1198 }
1199
1200 /*
1201  *      Socket options and virtual interface manipulation. The whole
1202  *      virtual interface system is a complete heap, but unfortunately
1203  *      that's how BSD mrouted happens to think. Maybe one day with a proper
1204  *      MOSPF/PIM router set up we can clean this up.
1205  */
1206
1207 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1208 {
1209         int ret;
1210         struct vifctl vif;
1211         struct mfcctl mfc;
1212         struct net *net = sock_net(sk);
1213         struct mr_table *mrt;
1214
1215         mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1216         if (mrt == NULL)
1217                 return -ENOENT;
1218
1219         if (optname != MRT_INIT) {
1220                 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1221                     !capable(CAP_NET_ADMIN))
1222                         return -EACCES;
1223         }
1224
1225         switch (optname) {
1226         case MRT_INIT:
1227                 if (sk->sk_type != SOCK_RAW ||
1228                     inet_sk(sk)->inet_num != IPPROTO_IGMP)
1229                         return -EOPNOTSUPP;
1230                 if (optlen != sizeof(int))
1231                         return -ENOPROTOOPT;
1232
1233                 rtnl_lock();
1234                 if (rtnl_dereference(mrt->mroute_sk)) {
1235                         rtnl_unlock();
1236                         return -EADDRINUSE;
1237                 }
1238
1239                 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1240                 if (ret == 0) {
1241                         rcu_assign_pointer(mrt->mroute_sk, sk);
1242                         IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1243                 }
1244                 rtnl_unlock();
1245                 return ret;
1246         case MRT_DONE:
1247                 if (sk != rcu_access_pointer(mrt->mroute_sk))
1248                         return -EACCES;
1249                 return ip_ra_control(sk, 0, NULL);
1250         case MRT_ADD_VIF:
1251         case MRT_DEL_VIF:
1252                 if (optlen != sizeof(vif))
1253                         return -EINVAL;
1254                 if (copy_from_user(&vif, optval, sizeof(vif)))
1255                         return -EFAULT;
1256                 if (vif.vifc_vifi >= MAXVIFS)
1257                         return -ENFILE;
1258                 rtnl_lock();
1259                 if (optname == MRT_ADD_VIF) {
1260                         ret = vif_add(net, mrt, &vif,
1261                                       sk == rtnl_dereference(mrt->mroute_sk));
1262                 } else {
1263                         ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1264                 }
1265                 rtnl_unlock();
1266                 return ret;
1267
1268                 /*
1269                  *      Manipulate the forwarding caches. These live
1270                  *      in a sort of kernel/user symbiosis.
1271                  */
1272         case MRT_ADD_MFC:
1273         case MRT_DEL_MFC:
1274                 if (optlen != sizeof(mfc))
1275                         return -EINVAL;
1276                 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1277                         return -EFAULT;
1278                 rtnl_lock();
1279                 if (optname == MRT_DEL_MFC)
1280                         ret = ipmr_mfc_delete(mrt, &mfc);
1281                 else
1282                         ret = ipmr_mfc_add(net, mrt, &mfc,
1283                                            sk == rtnl_dereference(mrt->mroute_sk));
1284                 rtnl_unlock();
1285                 return ret;
1286                 /*
1287                  *      Control PIM assert.
1288                  */
1289         case MRT_ASSERT:
1290         {
1291                 int v;
1292                 if (get_user(v, (int __user *)optval))
1293                         return -EFAULT;
1294                 mrt->mroute_do_assert = (v) ? 1 : 0;
1295                 return 0;
1296         }
1297 #ifdef CONFIG_IP_PIMSM
1298         case MRT_PIM:
1299         {
1300                 int v;
1301
1302                 if (get_user(v, (int __user *)optval))
1303                         return -EFAULT;
1304                 v = (v) ? 1 : 0;
1305
1306                 rtnl_lock();
1307                 ret = 0;
1308                 if (v != mrt->mroute_do_pim) {
1309                         mrt->mroute_do_pim = v;
1310                         mrt->mroute_do_assert = v;
1311                 }
1312                 rtnl_unlock();
1313                 return ret;
1314         }
1315 #endif
1316 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1317         case MRT_TABLE:
1318         {
1319                 u32 v;
1320
1321                 if (optlen != sizeof(u32))
1322                         return -EINVAL;
1323                 if (get_user(v, (u32 __user *)optval))
1324                         return -EFAULT;
1325
1326                 rtnl_lock();
1327                 ret = 0;
1328                 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1329                         ret = -EBUSY;
1330                 } else {
1331                         if (!ipmr_new_table(net, v))
1332                                 ret = -ENOMEM;
1333                         raw_sk(sk)->ipmr_table = v;
1334                 }
1335                 rtnl_unlock();
1336                 return ret;
1337         }
1338 #endif
1339         /*
1340          *      Spurious command, or MRT_VERSION which you cannot
1341          *      set.
1342          */
1343         default:
1344                 return -ENOPROTOOPT;
1345         }
1346 }
1347
1348 /*
1349  *      Getsock opt support for the multicast routing system.
1350  */
1351
1352 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1353 {
1354         int olr;
1355         int val;
1356         struct net *net = sock_net(sk);
1357         struct mr_table *mrt;
1358
1359         mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1360         if (mrt == NULL)
1361                 return -ENOENT;
1362
1363         if (optname != MRT_VERSION &&
1364 #ifdef CONFIG_IP_PIMSM
1365            optname != MRT_PIM &&
1366 #endif
1367            optname != MRT_ASSERT)
1368                 return -ENOPROTOOPT;
1369
1370         if (get_user(olr, optlen))
1371                 return -EFAULT;
1372
1373         olr = min_t(unsigned int, olr, sizeof(int));
1374         if (olr < 0)
1375                 return -EINVAL;
1376
1377         if (put_user(olr, optlen))
1378                 return -EFAULT;
1379         if (optname == MRT_VERSION)
1380                 val = 0x0305;
1381 #ifdef CONFIG_IP_PIMSM
1382         else if (optname == MRT_PIM)
1383                 val = mrt->mroute_do_pim;
1384 #endif
1385         else
1386                 val = mrt->mroute_do_assert;
1387         if (copy_to_user(optval, &val, olr))
1388                 return -EFAULT;
1389         return 0;
1390 }
1391
1392 /*
1393  *      The IP multicast ioctl support routines.
1394  */
1395
1396 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1397 {
1398         struct sioc_sg_req sr;
1399         struct sioc_vif_req vr;
1400         struct vif_device *vif;
1401         struct mfc_cache *c;
1402         struct net *net = sock_net(sk);
1403         struct mr_table *mrt;
1404
1405         mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1406         if (mrt == NULL)
1407                 return -ENOENT;
1408
1409         switch (cmd) {
1410         case SIOCGETVIFCNT:
1411                 if (copy_from_user(&vr, arg, sizeof(vr)))
1412                         return -EFAULT;
1413                 if (vr.vifi >= mrt->maxvif)
1414                         return -EINVAL;
1415                 read_lock(&mrt_lock);
1416                 vif = &mrt->vif_table[vr.vifi];
1417                 if (VIF_EXISTS(mrt, vr.vifi)) {
1418                         vr.icount = vif->pkt_in;
1419                         vr.ocount = vif->pkt_out;
1420                         vr.ibytes = vif->bytes_in;
1421                         vr.obytes = vif->bytes_out;
1422                         read_unlock(&mrt_lock);
1423
1424                         if (copy_to_user(arg, &vr, sizeof(vr)))
1425                                 return -EFAULT;
1426                         return 0;
1427                 }
1428                 read_unlock(&mrt_lock);
1429                 return -EADDRNOTAVAIL;
1430         case SIOCGETSGCNT:
1431                 if (copy_from_user(&sr, arg, sizeof(sr)))
1432                         return -EFAULT;
1433
1434                 rcu_read_lock();
1435                 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1436                 if (c) {
1437                         sr.pktcnt = c->mfc_un.res.pkt;
1438                         sr.bytecnt = c->mfc_un.res.bytes;
1439                         sr.wrong_if = c->mfc_un.res.wrong_if;
1440                         rcu_read_unlock();
1441
1442                         if (copy_to_user(arg, &sr, sizeof(sr)))
1443                                 return -EFAULT;
1444                         return 0;
1445                 }
1446                 rcu_read_unlock();
1447                 return -EADDRNOTAVAIL;
1448         default:
1449                 return -ENOIOCTLCMD;
1450         }
1451 }
1452
1453 #ifdef CONFIG_COMPAT
1454 struct compat_sioc_sg_req {
1455         struct in_addr src;
1456         struct in_addr grp;
1457         compat_ulong_t pktcnt;
1458         compat_ulong_t bytecnt;
1459         compat_ulong_t wrong_if;
1460 };
1461
1462 struct compat_sioc_vif_req {
1463         vifi_t  vifi;           /* Which iface */
1464         compat_ulong_t icount;
1465         compat_ulong_t ocount;
1466         compat_ulong_t ibytes;
1467         compat_ulong_t obytes;
1468 };
1469
1470 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1471 {
1472         struct compat_sioc_sg_req sr;
1473         struct compat_sioc_vif_req vr;
1474         struct vif_device *vif;
1475         struct mfc_cache *c;
1476         struct net *net = sock_net(sk);
1477         struct mr_table *mrt;
1478
1479         mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1480         if (mrt == NULL)
1481                 return -ENOENT;
1482
1483         switch (cmd) {
1484         case SIOCGETVIFCNT:
1485                 if (copy_from_user(&vr, arg, sizeof(vr)))
1486                         return -EFAULT;
1487                 if (vr.vifi >= mrt->maxvif)
1488                         return -EINVAL;
1489                 read_lock(&mrt_lock);
1490                 vif = &mrt->vif_table[vr.vifi];
1491                 if (VIF_EXISTS(mrt, vr.vifi)) {
1492                         vr.icount = vif->pkt_in;
1493                         vr.ocount = vif->pkt_out;
1494                         vr.ibytes = vif->bytes_in;
1495                         vr.obytes = vif->bytes_out;
1496                         read_unlock(&mrt_lock);
1497
1498                         if (copy_to_user(arg, &vr, sizeof(vr)))
1499                                 return -EFAULT;
1500                         return 0;
1501                 }
1502                 read_unlock(&mrt_lock);
1503                 return -EADDRNOTAVAIL;
1504         case SIOCGETSGCNT:
1505                 if (copy_from_user(&sr, arg, sizeof(sr)))
1506                         return -EFAULT;
1507
1508                 rcu_read_lock();
1509                 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1510                 if (c) {
1511                         sr.pktcnt = c->mfc_un.res.pkt;
1512                         sr.bytecnt = c->mfc_un.res.bytes;
1513                         sr.wrong_if = c->mfc_un.res.wrong_if;
1514                         rcu_read_unlock();
1515
1516                         if (copy_to_user(arg, &sr, sizeof(sr)))
1517                                 return -EFAULT;
1518                         return 0;
1519                 }
1520                 rcu_read_unlock();
1521                 return -EADDRNOTAVAIL;
1522         default:
1523                 return -ENOIOCTLCMD;
1524         }
1525 }
1526 #endif
1527
1528
1529 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1530 {
1531         struct net_device *dev = ptr;
1532         struct net *net = dev_net(dev);
1533         struct mr_table *mrt;
1534         struct vif_device *v;
1535         int ct;
1536         LIST_HEAD(list);
1537
1538         if (event != NETDEV_UNREGISTER)
1539                 return NOTIFY_DONE;
1540
1541         ipmr_for_each_table(mrt, net) {
1542                 v = &mrt->vif_table[0];
1543                 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1544                         if (v->dev == dev)
1545                                 vif_delete(mrt, ct, 1, &list);
1546                 }
1547         }
1548         unregister_netdevice_many(&list);
1549         return NOTIFY_DONE;
1550 }
1551
1552
1553 static struct notifier_block ip_mr_notifier = {
1554         .notifier_call = ipmr_device_event,
1555 };
1556
1557 /*
1558  *      Encapsulate a packet by attaching a valid IPIP header to it.
1559  *      This avoids tunnel drivers and other mess and gives us the speed so
1560  *      important for multicast video.
1561  */
1562
1563 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1564 {
1565         struct iphdr *iph;
1566         const struct iphdr *old_iph = ip_hdr(skb);
1567
1568         skb_push(skb, sizeof(struct iphdr));
1569         skb->transport_header = skb->network_header;
1570         skb_reset_network_header(skb);
1571         iph = ip_hdr(skb);
1572
1573         iph->version    =       4;
1574         iph->tos        =       old_iph->tos;
1575         iph->ttl        =       old_iph->ttl;
1576         iph->frag_off   =       0;
1577         iph->daddr      =       daddr;
1578         iph->saddr      =       saddr;
1579         iph->protocol   =       IPPROTO_IPIP;
1580         iph->ihl        =       5;
1581         iph->tot_len    =       htons(skb->len);
1582         ip_select_ident(skb, skb_dst(skb), NULL);
1583         ip_send_check(iph);
1584
1585         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1586         nf_reset(skb);
1587 }
1588
1589 static inline int ipmr_forward_finish(struct sk_buff *skb)
1590 {
1591         struct ip_options *opt = &(IPCB(skb)->opt);
1592
1593         IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1594
1595         if (unlikely(opt->optlen))
1596                 ip_forward_options(skb);
1597
1598         return dst_output(skb);
1599 }
1600
1601 /*
1602  *      Processing handlers for ipmr_forward
1603  */
1604
1605 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1606                             struct sk_buff *skb, struct mfc_cache *c, int vifi)
1607 {
1608         const struct iphdr *iph = ip_hdr(skb);
1609         struct vif_device *vif = &mrt->vif_table[vifi];
1610         struct net_device *dev;
1611         struct rtable *rt;
1612         struct flowi4 fl4;
1613         int    encap = 0;
1614
1615         if (vif->dev == NULL)
1616                 goto out_free;
1617
1618 #ifdef CONFIG_IP_PIMSM
1619         if (vif->flags & VIFF_REGISTER) {
1620                 vif->pkt_out++;
1621                 vif->bytes_out += skb->len;
1622                 vif->dev->stats.tx_bytes += skb->len;
1623                 vif->dev->stats.tx_packets++;
1624                 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1625                 goto out_free;
1626         }
1627 #endif
1628
1629         if (vif->flags & VIFF_TUNNEL) {
1630                 rt = ip_route_output_ports(net, &fl4, NULL,
1631                                            vif->remote, vif->local,
1632                                            0, 0,
1633                                            IPPROTO_IPIP,
1634                                            RT_TOS(iph->tos), vif->link);
1635                 if (IS_ERR(rt))
1636                         goto out_free;
1637                 encap = sizeof(struct iphdr);
1638         } else {
1639                 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1640                                            0, 0,
1641                                            IPPROTO_IPIP,
1642                                            RT_TOS(iph->tos), vif->link);
1643                 if (IS_ERR(rt))
1644                         goto out_free;
1645         }
1646
1647         dev = rt->dst.dev;
1648
1649         if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1650                 /* Do not fragment multicasts. Alas, IPv4 does not
1651                  * allow to send ICMP, so that packets will disappear
1652                  * to blackhole.
1653                  */
1654
1655                 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1656                 ip_rt_put(rt);
1657                 goto out_free;
1658         }
1659
1660         encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1661
1662         if (skb_cow(skb, encap)) {
1663                 ip_rt_put(rt);
1664                 goto out_free;
1665         }
1666
1667         vif->pkt_out++;
1668         vif->bytes_out += skb->len;
1669
1670         skb_dst_drop(skb);
1671         skb_dst_set(skb, &rt->dst);
1672         ip_decrease_ttl(ip_hdr(skb));
1673
1674         /* FIXME: forward and output firewalls used to be called here.
1675          * What do we do with netfilter? -- RR
1676          */
1677         if (vif->flags & VIFF_TUNNEL) {
1678                 ip_encap(skb, vif->local, vif->remote);
1679                 /* FIXME: extra output firewall step used to be here. --RR */
1680                 vif->dev->stats.tx_packets++;
1681                 vif->dev->stats.tx_bytes += skb->len;
1682         }
1683
1684         IPCB(skb)->flags |= IPSKB_FORWARDED;
1685
1686         /*
1687          * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1688          * not only before forwarding, but after forwarding on all output
1689          * interfaces. It is clear, if mrouter runs a multicasting
1690          * program, it should receive packets not depending to what interface
1691          * program is joined.
1692          * If we will not make it, the program will have to join on all
1693          * interfaces. On the other hand, multihoming host (or router, but
1694          * not mrouter) cannot join to more than one interface - it will
1695          * result in receiving multiple packets.
1696          */
1697         NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1698                 ipmr_forward_finish);
1699         return;
1700
1701 out_free:
1702         kfree_skb(skb);
1703 }
1704
1705 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1706 {
1707         int ct;
1708
1709         for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1710                 if (mrt->vif_table[ct].dev == dev)
1711                         break;
1712         }
1713         return ct;
1714 }
1715
1716 /* "local" means that we should preserve one skb (for local delivery) */
1717
1718 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1719                          struct sk_buff *skb, struct mfc_cache *cache,
1720                          int local)
1721 {
1722         int psend = -1;
1723         int vif, ct;
1724
1725         vif = cache->mfc_parent;
1726         cache->mfc_un.res.pkt++;
1727         cache->mfc_un.res.bytes += skb->len;
1728
1729         /*
1730          * Wrong interface: drop packet and (maybe) send PIM assert.
1731          */
1732         if (mrt->vif_table[vif].dev != skb->dev) {
1733                 int true_vifi;
1734
1735                 if (rt_is_output_route(skb_rtable(skb))) {
1736                         /* It is our own packet, looped back.
1737                          * Very complicated situation...
1738                          *
1739                          * The best workaround until routing daemons will be
1740                          * fixed is not to redistribute packet, if it was
1741                          * send through wrong interface. It means, that
1742                          * multicast applications WILL NOT work for
1743                          * (S,G), which have default multicast route pointing
1744                          * to wrong oif. In any case, it is not a good
1745                          * idea to use multicasting applications on router.
1746                          */
1747                         goto dont_forward;
1748                 }
1749
1750                 cache->mfc_un.res.wrong_if++;
1751                 true_vifi = ipmr_find_vif(mrt, skb->dev);
1752
1753                 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1754                     /* pimsm uses asserts, when switching from RPT to SPT,
1755                      * so that we cannot check that packet arrived on an oif.
1756                      * It is bad, but otherwise we would need to move pretty
1757                      * large chunk of pimd to kernel. Ough... --ANK
1758                      */
1759                     (mrt->mroute_do_pim ||
1760                      cache->mfc_un.res.ttls[true_vifi] < 255) &&
1761                     time_after(jiffies,
1762                                cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1763                         cache->mfc_un.res.last_assert = jiffies;
1764                         ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1765                 }
1766                 goto dont_forward;
1767         }
1768
1769         mrt->vif_table[vif].pkt_in++;
1770         mrt->vif_table[vif].bytes_in += skb->len;
1771
1772         /*
1773          *      Forward the frame
1774          */
1775         for (ct = cache->mfc_un.res.maxvif - 1;
1776              ct >= cache->mfc_un.res.minvif; ct--) {
1777                 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1778                         if (psend != -1) {
1779                                 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1780
1781                                 if (skb2)
1782                                         ipmr_queue_xmit(net, mrt, skb2, cache,
1783                                                         psend);
1784                         }
1785                         psend = ct;
1786                 }
1787         }
1788         if (psend != -1) {
1789                 if (local) {
1790                         struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1791
1792                         if (skb2)
1793                                 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1794                 } else {
1795                         ipmr_queue_xmit(net, mrt, skb, cache, psend);
1796                         return 0;
1797                 }
1798         }
1799
1800 dont_forward:
1801         if (!local)
1802                 kfree_skb(skb);
1803         return 0;
1804 }
1805
1806 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1807 {
1808         struct rtable *rt = skb_rtable(skb);
1809         struct iphdr *iph = ip_hdr(skb);
1810         struct flowi4 fl4 = {
1811                 .daddr = iph->daddr,
1812                 .saddr = iph->saddr,
1813                 .flowi4_tos = RT_TOS(iph->tos),
1814                 .flowi4_oif = rt->rt_oif,
1815                 .flowi4_iif = rt->rt_iif,
1816                 .flowi4_mark = rt->rt_mark,
1817         };
1818         struct mr_table *mrt;
1819         int err;
1820
1821         err = ipmr_fib_lookup(net, &fl4, &mrt);
1822         if (err)
1823                 return ERR_PTR(err);
1824         return mrt;
1825 }
1826
1827 /*
1828  *      Multicast packets for forwarding arrive here
1829  *      Called with rcu_read_lock();
1830  */
1831
1832 int ip_mr_input(struct sk_buff *skb)
1833 {
1834         struct mfc_cache *cache;
1835         struct net *net = dev_net(skb->dev);
1836         int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1837         struct mr_table *mrt;
1838
1839         /* Packet is looped back after forward, it should not be
1840          * forwarded second time, but still can be delivered locally.
1841          */
1842         if (IPCB(skb)->flags & IPSKB_FORWARDED)
1843                 goto dont_forward;
1844
1845         mrt = ipmr_rt_fib_lookup(net, skb);
1846         if (IS_ERR(mrt)) {
1847                 kfree_skb(skb);
1848                 return PTR_ERR(mrt);
1849         }
1850         if (!local) {
1851                 if (IPCB(skb)->opt.router_alert) {
1852                         if (ip_call_ra_chain(skb))
1853                                 return 0;
1854                 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1855                         /* IGMPv1 (and broken IGMPv2 implementations sort of
1856                          * Cisco IOS <= 11.2(8)) do not put router alert
1857                          * option to IGMP packets destined to routable
1858                          * groups. It is very bad, because it means
1859                          * that we can forward NO IGMP messages.
1860                          */
1861                         struct sock *mroute_sk;
1862
1863                         mroute_sk = rcu_dereference(mrt->mroute_sk);
1864                         if (mroute_sk) {
1865                                 nf_reset(skb);
1866                                 raw_rcv(mroute_sk, skb);
1867                                 return 0;
1868                         }
1869                     }
1870         }
1871
1872         /* already under rcu_read_lock() */
1873         cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1874
1875         /*
1876          *      No usable cache entry
1877          */
1878         if (cache == NULL) {
1879                 int vif;
1880
1881                 if (local) {
1882                         struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1883                         ip_local_deliver(skb);
1884                         if (skb2 == NULL)
1885                                 return -ENOBUFS;
1886                         skb = skb2;
1887                 }
1888
1889                 read_lock(&mrt_lock);
1890                 vif = ipmr_find_vif(mrt, skb->dev);
1891                 if (vif >= 0) {
1892                         int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1893                         read_unlock(&mrt_lock);
1894
1895                         return err2;
1896                 }
1897                 read_unlock(&mrt_lock);
1898                 kfree_skb(skb);
1899                 return -ENODEV;
1900         }
1901
1902         read_lock(&mrt_lock);
1903         ip_mr_forward(net, mrt, skb, cache, local);
1904         read_unlock(&mrt_lock);
1905
1906         if (local)
1907                 return ip_local_deliver(skb);
1908
1909         return 0;
1910
1911 dont_forward:
1912         if (local)
1913                 return ip_local_deliver(skb);
1914         kfree_skb(skb);
1915         return 0;
1916 }
1917
1918 #ifdef CONFIG_IP_PIMSM
1919 /* called with rcu_read_lock() */
1920 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1921                      unsigned int pimlen)
1922 {
1923         struct net_device *reg_dev = NULL;
1924         struct iphdr *encap;
1925
1926         encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1927         /*
1928          * Check that:
1929          * a. packet is really sent to a multicast group
1930          * b. packet is not a NULL-REGISTER
1931          * c. packet is not truncated
1932          */
1933         if (!ipv4_is_multicast(encap->daddr) ||
1934             encap->tot_len == 0 ||
1935             ntohs(encap->tot_len) + pimlen > skb->len)
1936                 return 1;
1937
1938         read_lock(&mrt_lock);
1939         if (mrt->mroute_reg_vif_num >= 0)
1940                 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1941         read_unlock(&mrt_lock);
1942
1943         if (reg_dev == NULL)
1944                 return 1;
1945
1946         skb->mac_header = skb->network_header;
1947         skb_pull(skb, (u8 *)encap - skb->data);
1948         skb_reset_network_header(skb);
1949         skb->protocol = htons(ETH_P_IP);
1950         skb->ip_summed = CHECKSUM_NONE;
1951         skb->pkt_type = PACKET_HOST;
1952
1953         skb_tunnel_rx(skb, reg_dev);
1954
1955         netif_rx(skb);
1956
1957         return NET_RX_SUCCESS;
1958 }
1959 #endif
1960
1961 #ifdef CONFIG_IP_PIMSM_V1
1962 /*
1963  * Handle IGMP messages of PIMv1
1964  */
1965
1966 int pim_rcv_v1(struct sk_buff *skb)
1967 {
1968         struct igmphdr *pim;
1969         struct net *net = dev_net(skb->dev);
1970         struct mr_table *mrt;
1971
1972         if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1973                 goto drop;
1974
1975         pim = igmp_hdr(skb);
1976
1977         mrt = ipmr_rt_fib_lookup(net, skb);
1978         if (IS_ERR(mrt))
1979                 goto drop;
1980         if (!mrt->mroute_do_pim ||
1981             pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1982                 goto drop;
1983
1984         if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1985 drop:
1986                 kfree_skb(skb);
1987         }
1988         return 0;
1989 }
1990 #endif
1991
1992 #ifdef CONFIG_IP_PIMSM_V2
1993 static int pim_rcv(struct sk_buff *skb)
1994 {
1995         struct pimreghdr *pim;
1996         struct net *net = dev_net(skb->dev);
1997         struct mr_table *mrt;
1998
1999         if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2000                 goto drop;
2001
2002         pim = (struct pimreghdr *)skb_transport_header(skb);
2003         if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2004             (pim->flags & PIM_NULL_REGISTER) ||
2005             (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2006              csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2007                 goto drop;
2008
2009         mrt = ipmr_rt_fib_lookup(net, skb);
2010         if (IS_ERR(mrt))
2011                 goto drop;
2012         if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2013 drop:
2014                 kfree_skb(skb);
2015         }
2016         return 0;
2017 }
2018 #endif
2019
2020 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2021                               struct mfc_cache *c, struct rtmsg *rtm)
2022 {
2023         int ct;
2024         struct rtnexthop *nhp;
2025         u8 *b = skb_tail_pointer(skb);
2026         struct rtattr *mp_head;
2027
2028         /* If cache is unresolved, don't try to parse IIF and OIF */
2029         if (c->mfc_parent >= MAXVIFS)
2030                 return -ENOENT;
2031
2032         if (VIF_EXISTS(mrt, c->mfc_parent))
2033                 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
2034
2035         mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
2036
2037         for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2038                 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2039                         if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2040                                 goto rtattr_failure;
2041                         nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
2042                         nhp->rtnh_flags = 0;
2043                         nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2044                         nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2045                         nhp->rtnh_len = sizeof(*nhp);
2046                 }
2047         }
2048         mp_head->rta_type = RTA_MULTIPATH;
2049         mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
2050         rtm->rtm_type = RTN_MULTICAST;
2051         return 1;
2052
2053 rtattr_failure:
2054         nlmsg_trim(skb, b);
2055         return -EMSGSIZE;
2056 }
2057
2058 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2059                    __be32 saddr, __be32 daddr,
2060                    struct rtmsg *rtm, int nowait)
2061 {
2062         struct mfc_cache *cache;
2063         struct mr_table *mrt;
2064         int err;
2065
2066         mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2067         if (mrt == NULL)
2068                 return -ENOENT;
2069
2070         rcu_read_lock();
2071         cache = ipmr_cache_find(mrt, saddr, daddr);
2072
2073         if (cache == NULL) {
2074                 struct sk_buff *skb2;
2075                 struct iphdr *iph;
2076                 struct net_device *dev;
2077                 int vif = -1;
2078
2079                 if (nowait) {
2080                         rcu_read_unlock();
2081                         return -EAGAIN;
2082                 }
2083
2084                 dev = skb->dev;
2085                 read_lock(&mrt_lock);
2086                 if (dev)
2087                         vif = ipmr_find_vif(mrt, dev);
2088                 if (vif < 0) {
2089                         read_unlock(&mrt_lock);
2090                         rcu_read_unlock();
2091                         return -ENODEV;
2092                 }
2093                 skb2 = skb_clone(skb, GFP_ATOMIC);
2094                 if (!skb2) {
2095                         read_unlock(&mrt_lock);
2096                         rcu_read_unlock();
2097                         return -ENOMEM;
2098                 }
2099
2100                 skb_push(skb2, sizeof(struct iphdr));
2101                 skb_reset_network_header(skb2);
2102                 iph = ip_hdr(skb2);
2103                 iph->ihl = sizeof(struct iphdr) >> 2;
2104                 iph->saddr = saddr;
2105                 iph->daddr = daddr;
2106                 iph->version = 0;
2107                 err = ipmr_cache_unresolved(mrt, vif, skb2);
2108                 read_unlock(&mrt_lock);
2109                 rcu_read_unlock();
2110                 return err;
2111         }
2112
2113         read_lock(&mrt_lock);
2114         if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2115                 cache->mfc_flags |= MFC_NOTIFY;
2116         err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2117         read_unlock(&mrt_lock);
2118         rcu_read_unlock();
2119         return err;
2120 }
2121
2122 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2123                             u32 pid, u32 seq, struct mfc_cache *c)
2124 {
2125         struct nlmsghdr *nlh;
2126         struct rtmsg *rtm;
2127
2128         nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2129         if (nlh == NULL)
2130                 return -EMSGSIZE;
2131
2132         rtm = nlmsg_data(nlh);
2133         rtm->rtm_family   = RTNL_FAMILY_IPMR;
2134         rtm->rtm_dst_len  = 32;
2135         rtm->rtm_src_len  = 32;
2136         rtm->rtm_tos      = 0;
2137         rtm->rtm_table    = mrt->id;
2138         NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2139         rtm->rtm_type     = RTN_MULTICAST;
2140         rtm->rtm_scope    = RT_SCOPE_UNIVERSE;
2141         rtm->rtm_protocol = RTPROT_UNSPEC;
2142         rtm->rtm_flags    = 0;
2143
2144         NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2145         NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2146
2147         if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2148                 goto nla_put_failure;
2149
2150         return nlmsg_end(skb, nlh);
2151
2152 nla_put_failure:
2153         nlmsg_cancel(skb, nlh);
2154         return -EMSGSIZE;
2155 }
2156
2157 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2158 {
2159         struct net *net = sock_net(skb->sk);
2160         struct mr_table *mrt;
2161         struct mfc_cache *mfc;
2162         unsigned int t = 0, s_t;
2163         unsigned int h = 0, s_h;
2164         unsigned int e = 0, s_e;
2165
2166         s_t = cb->args[0];
2167         s_h = cb->args[1];
2168         s_e = cb->args[2];
2169
2170         rcu_read_lock();
2171         ipmr_for_each_table(mrt, net) {
2172                 if (t < s_t)
2173                         goto next_table;
2174                 if (t > s_t)
2175                         s_h = 0;
2176                 for (h = s_h; h < MFC_LINES; h++) {
2177                         list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2178                                 if (e < s_e)
2179                                         goto next_entry;
2180                                 if (ipmr_fill_mroute(mrt, skb,
2181                                                      NETLINK_CB(cb->skb).pid,
2182                                                      cb->nlh->nlmsg_seq,
2183                                                      mfc) < 0)
2184                                         goto done;
2185 next_entry:
2186                                 e++;
2187                         }
2188                         e = s_e = 0;
2189                 }
2190                 s_h = 0;
2191 next_table:
2192                 t++;
2193         }
2194 done:
2195         rcu_read_unlock();
2196
2197         cb->args[2] = e;
2198         cb->args[1] = h;
2199         cb->args[0] = t;
2200
2201         return skb->len;
2202 }
2203
2204 #ifdef CONFIG_PROC_FS
2205 /*
2206  *      The /proc interfaces to multicast routing :
2207  *      /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2208  */
2209 struct ipmr_vif_iter {
2210         struct seq_net_private p;
2211         struct mr_table *mrt;
2212         int ct;
2213 };
2214
2215 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2216                                            struct ipmr_vif_iter *iter,
2217                                            loff_t pos)
2218 {
2219         struct mr_table *mrt = iter->mrt;
2220
2221         for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2222                 if (!VIF_EXISTS(mrt, iter->ct))
2223                         continue;
2224                 if (pos-- == 0)
2225                         return &mrt->vif_table[iter->ct];
2226         }
2227         return NULL;
2228 }
2229
2230 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2231         __acquires(mrt_lock)
2232 {
2233         struct ipmr_vif_iter *iter = seq->private;
2234         struct net *net = seq_file_net(seq);
2235         struct mr_table *mrt;
2236
2237         mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2238         if (mrt == NULL)
2239                 return ERR_PTR(-ENOENT);
2240
2241         iter->mrt = mrt;
2242
2243         read_lock(&mrt_lock);
2244         return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2245                 : SEQ_START_TOKEN;
2246 }
2247
2248 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2249 {
2250         struct ipmr_vif_iter *iter = seq->private;
2251         struct net *net = seq_file_net(seq);
2252         struct mr_table *mrt = iter->mrt;
2253
2254         ++*pos;
2255         if (v == SEQ_START_TOKEN)
2256                 return ipmr_vif_seq_idx(net, iter, 0);
2257
2258         while (++iter->ct < mrt->maxvif) {
2259                 if (!VIF_EXISTS(mrt, iter->ct))
2260                         continue;
2261                 return &mrt->vif_table[iter->ct];
2262         }
2263         return NULL;
2264 }
2265
2266 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2267         __releases(mrt_lock)
2268 {
2269         read_unlock(&mrt_lock);
2270 }
2271
2272 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2273 {
2274         struct ipmr_vif_iter *iter = seq->private;
2275         struct mr_table *mrt = iter->mrt;
2276
2277         if (v == SEQ_START_TOKEN) {
2278                 seq_puts(seq,
2279                          "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n");
2280         } else {
2281                 const struct vif_device *vif = v;
2282                 const char *name =  vif->dev ? vif->dev->name : "none";
2283
2284                 seq_printf(seq,
2285                            "%2Zd %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n",
2286                            vif - mrt->vif_table,
2287                            name, vif->bytes_in, vif->pkt_in,
2288                            vif->bytes_out, vif->pkt_out,
2289                            vif->flags, vif->local, vif->remote);
2290         }
2291         return 0;
2292 }
2293
2294 static const struct seq_operations ipmr_vif_seq_ops = {
2295         .start = ipmr_vif_seq_start,
2296         .next  = ipmr_vif_seq_next,
2297         .stop  = ipmr_vif_seq_stop,
2298         .show  = ipmr_vif_seq_show,
2299 };
2300
2301 static int ipmr_vif_open(struct inode *inode, struct file *file)
2302 {
2303         return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2304                             sizeof(struct ipmr_vif_iter));
2305 }
2306
2307 static const struct file_operations ipmr_vif_fops = {
2308         .owner   = THIS_MODULE,
2309         .open    = ipmr_vif_open,
2310         .read    = seq_read,
2311         .llseek  = seq_lseek,
2312         .release = seq_release_net,
2313 };
2314
2315 struct ipmr_mfc_iter {
2316         struct seq_net_private p;
2317         struct mr_table *mrt;
2318         struct list_head *cache;
2319         int ct;
2320 };
2321
2322
2323 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2324                                           struct ipmr_mfc_iter *it, loff_t pos)
2325 {
2326         struct mr_table *mrt = it->mrt;
2327         struct mfc_cache *mfc;
2328
2329         rcu_read_lock();
2330         for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2331                 it->cache = &mrt->mfc_cache_array[it->ct];
2332                 list_for_each_entry_rcu(mfc, it->cache, list)
2333                         if (pos-- == 0)
2334                                 return mfc;
2335         }
2336         rcu_read_unlock();
2337
2338         spin_lock_bh(&mfc_unres_lock);
2339         it->cache = &mrt->mfc_unres_queue;
2340         list_for_each_entry(mfc, it->cache, list)
2341                 if (pos-- == 0)
2342                         return mfc;
2343         spin_unlock_bh(&mfc_unres_lock);
2344
2345         it->cache = NULL;
2346         return NULL;
2347 }
2348
2349
2350 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2351 {
2352         struct ipmr_mfc_iter *it = seq->private;
2353         struct net *net = seq_file_net(seq);
2354         struct mr_table *mrt;
2355
2356         mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2357         if (mrt == NULL)
2358                 return ERR_PTR(-ENOENT);
2359
2360         it->mrt = mrt;
2361         it->cache = NULL;
2362         it->ct = 0;
2363         return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2364                 : SEQ_START_TOKEN;
2365 }
2366
2367 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2368 {
2369         struct mfc_cache *mfc = v;
2370         struct ipmr_mfc_iter *it = seq->private;
2371         struct net *net = seq_file_net(seq);
2372         struct mr_table *mrt = it->mrt;
2373
2374         ++*pos;
2375
2376         if (v == SEQ_START_TOKEN)
2377                 return ipmr_mfc_seq_idx(net, seq->private, 0);
2378
2379         if (mfc->list.next != it->cache)
2380                 return list_entry(mfc->list.next, struct mfc_cache, list);
2381
2382         if (it->cache == &mrt->mfc_unres_queue)
2383                 goto end_of_list;
2384
2385         BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2386
2387         while (++it->ct < MFC_LINES) {
2388                 it->cache = &mrt->mfc_cache_array[it->ct];
2389                 if (list_empty(it->cache))
2390                         continue;
2391                 return list_first_entry(it->cache, struct mfc_cache, list);
2392         }
2393
2394         /* exhausted cache_array, show unresolved */
2395         rcu_read_unlock();
2396         it->cache = &mrt->mfc_unres_queue;
2397         it->ct = 0;
2398
2399         spin_lock_bh(&mfc_unres_lock);
2400         if (!list_empty(it->cache))
2401                 return list_first_entry(it->cache, struct mfc_cache, list);
2402
2403 end_of_list:
2404         spin_unlock_bh(&mfc_unres_lock);
2405         it->cache = NULL;
2406
2407         return NULL;
2408 }
2409
2410 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2411 {
2412         struct ipmr_mfc_iter *it = seq->private;
2413         struct mr_table *mrt = it->mrt;
2414
2415         if (it->cache == &mrt->mfc_unres_queue)
2416                 spin_unlock_bh(&mfc_unres_lock);
2417         else if (it->cache == &mrt->mfc_cache_array[it->ct])
2418                 rcu_read_unlock();
2419 }
2420
2421 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2422 {
2423         int n;
2424
2425         if (v == SEQ_START_TOKEN) {
2426                 seq_puts(seq,
2427                  "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n");
2428         } else {
2429                 const struct mfc_cache *mfc = v;
2430                 const struct ipmr_mfc_iter *it = seq->private;
2431                 const struct mr_table *mrt = it->mrt;
2432
2433                 seq_printf(seq, "%08X %08X %-3hd",
2434                            (__force u32) mfc->mfc_mcastgrp,
2435                            (__force u32) mfc->mfc_origin,
2436                            mfc->mfc_parent);
2437
2438                 if (it->cache != &mrt->mfc_unres_queue) {
2439                         seq_printf(seq, " %8lu %8lu %8lu",
2440                                    mfc->mfc_un.res.pkt,
2441                                    mfc->mfc_un.res.bytes,
2442                                    mfc->mfc_un.res.wrong_if);
2443                         for (n = mfc->mfc_un.res.minvif;
2444                              n < mfc->mfc_un.res.maxvif; n++) {
2445                                 if (VIF_EXISTS(mrt, n) &&
2446                                     mfc->mfc_un.res.ttls[n] < 255)
2447                                         seq_printf(seq,
2448                                            " %2d:%-3d",
2449                                            n, mfc->mfc_un.res.ttls[n]);
2450                         }
2451                 } else {
2452                         /* unresolved mfc_caches don't contain
2453                          * pkt, bytes and wrong_if values
2454                          */
2455                         seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2456                 }
2457                 seq_putc(seq, '\n');
2458         }
2459         return 0;
2460 }
2461
2462 static const struct seq_operations ipmr_mfc_seq_ops = {
2463         .start = ipmr_mfc_seq_start,
2464         .next  = ipmr_mfc_seq_next,
2465         .stop  = ipmr_mfc_seq_stop,
2466         .show  = ipmr_mfc_seq_show,
2467 };
2468
2469 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2470 {
2471         return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2472                             sizeof(struct ipmr_mfc_iter));
2473 }
2474
2475 static const struct file_operations ipmr_mfc_fops = {
2476         .owner   = THIS_MODULE,
2477         .open    = ipmr_mfc_open,
2478         .read    = seq_read,
2479         .llseek  = seq_lseek,
2480         .release = seq_release_net,
2481 };
2482 #endif
2483
2484 #ifdef CONFIG_IP_PIMSM_V2
2485 static const struct net_protocol pim_protocol = {
2486         .handler        =       pim_rcv,
2487         .netns_ok       =       1,
2488 };
2489 #endif
2490
2491
2492 /*
2493  *      Setup for IP multicast routing
2494  */
2495 static int __net_init ipmr_net_init(struct net *net)
2496 {
2497         int err;
2498
2499         err = ipmr_rules_init(net);
2500         if (err < 0)
2501                 goto fail;
2502
2503 #ifdef CONFIG_PROC_FS
2504         err = -ENOMEM;
2505         if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2506                 goto proc_vif_fail;
2507         if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2508                 goto proc_cache_fail;
2509 #endif
2510         return 0;
2511
2512 #ifdef CONFIG_PROC_FS
2513 proc_cache_fail:
2514         proc_net_remove(net, "ip_mr_vif");
2515 proc_vif_fail:
2516         ipmr_rules_exit(net);
2517 #endif
2518 fail:
2519         return err;
2520 }
2521
2522 static void __net_exit ipmr_net_exit(struct net *net)
2523 {
2524 #ifdef CONFIG_PROC_FS
2525         proc_net_remove(net, "ip_mr_cache");
2526         proc_net_remove(net, "ip_mr_vif");
2527 #endif
2528         ipmr_rules_exit(net);
2529 }
2530
2531 static struct pernet_operations ipmr_net_ops = {
2532         .init = ipmr_net_init,
2533         .exit = ipmr_net_exit,
2534 };
2535
2536 int __init ip_mr_init(void)
2537 {
2538         int err;
2539
2540         mrt_cachep = kmem_cache_create("ip_mrt_cache",
2541                                        sizeof(struct mfc_cache),
2542                                        0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2543                                        NULL);
2544         if (!mrt_cachep)
2545                 return -ENOMEM;
2546
2547         err = register_pernet_subsys(&ipmr_net_ops);
2548         if (err)
2549                 goto reg_pernet_fail;
2550
2551         err = register_netdevice_notifier(&ip_mr_notifier);
2552         if (err)
2553                 goto reg_notif_fail;
2554 #ifdef CONFIG_IP_PIMSM_V2
2555         if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2556                 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2557                 err = -EAGAIN;
2558                 goto add_proto_fail;
2559         }
2560 #endif
2561         rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2562                       NULL, ipmr_rtm_dumproute, NULL);
2563         return 0;
2564
2565 #ifdef CONFIG_IP_PIMSM_V2
2566 add_proto_fail:
2567         unregister_netdevice_notifier(&ip_mr_notifier);
2568 #endif
2569 reg_notif_fail:
2570         unregister_pernet_subsys(&ipmr_net_ops);
2571 reg_pernet_fail:
2572         kmem_cache_destroy(mrt_cachep);
2573         return err;
2574 }