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