tcp: add tcp_min_snd_mss sysctl
[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, bool all);
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, true);
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.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
835                 c->mfc_un.res.minvif = MAXVIFS;
836         }
837         return c;
838 }
839
840 static struct mfc_cache *ipmr_cache_alloc_unres(void)
841 {
842         struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
843
844         if (c) {
845                 skb_queue_head_init(&c->mfc_un.unres.unresolved);
846                 c->mfc_un.unres.expires = jiffies + 10*HZ;
847         }
848         return c;
849 }
850
851 /*
852  *      A cache entry has gone into a resolved state from queued
853  */
854
855 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
856                                struct mfc_cache *uc, struct mfc_cache *c)
857 {
858         struct sk_buff *skb;
859         struct nlmsgerr *e;
860
861         /* Play the pending entries through our router */
862
863         while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
864                 if (ip_hdr(skb)->version == 0) {
865                         struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
866
867                         if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
868                                 nlh->nlmsg_len = skb_tail_pointer(skb) -
869                                                  (u8 *)nlh;
870                         } else {
871                                 nlh->nlmsg_type = NLMSG_ERROR;
872                                 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
873                                 skb_trim(skb, nlh->nlmsg_len);
874                                 e = NLMSG_DATA(nlh);
875                                 e->error = -EMSGSIZE;
876                                 memset(&e->msg, 0, sizeof(e->msg));
877                         }
878
879                         rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
880                 } else {
881                         ip_mr_forward(net, mrt, skb, c, 0);
882                 }
883         }
884 }
885
886 /*
887  *      Bounce a cache query up to mrouted. We could use netlink for this but mrouted
888  *      expects the following bizarre scheme.
889  *
890  *      Called under mrt_lock.
891  */
892
893 static int ipmr_cache_report(struct mr_table *mrt,
894                              struct sk_buff *pkt, vifi_t vifi, int assert)
895 {
896         struct sk_buff *skb;
897         const int ihl = ip_hdrlen(pkt);
898         struct igmphdr *igmp;
899         struct igmpmsg *msg;
900         struct sock *mroute_sk;
901         int ret;
902
903 #ifdef CONFIG_IP_PIMSM
904         if (assert == IGMPMSG_WHOLEPKT)
905                 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
906         else
907 #endif
908                 skb = alloc_skb(128, GFP_ATOMIC);
909
910         if (!skb)
911                 return -ENOBUFS;
912
913 #ifdef CONFIG_IP_PIMSM
914         if (assert == IGMPMSG_WHOLEPKT) {
915                 /* Ugly, but we have no choice with this interface.
916                  * Duplicate old header, fix ihl, length etc.
917                  * And all this only to mangle msg->im_msgtype and
918                  * to set msg->im_mbz to "mbz" :-)
919                  */
920                 skb_push(skb, sizeof(struct iphdr));
921                 skb_reset_network_header(skb);
922                 skb_reset_transport_header(skb);
923                 msg = (struct igmpmsg *)skb_network_header(skb);
924                 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
925                 msg->im_msgtype = IGMPMSG_WHOLEPKT;
926                 msg->im_mbz = 0;
927                 msg->im_vif = mrt->mroute_reg_vif_num;
928                 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
929                 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
930                                              sizeof(struct iphdr));
931         } else
932 #endif
933         {
934
935         /* Copy the IP header */
936
937         skb->network_header = skb->tail;
938         skb_put(skb, ihl);
939         skb_copy_to_linear_data(skb, pkt->data, ihl);
940         ip_hdr(skb)->protocol = 0;      /* Flag to the kernel this is a route add */
941         msg = (struct igmpmsg *)skb_network_header(skb);
942         msg->im_vif = vifi;
943         skb_dst_set(skb, dst_clone(skb_dst(pkt)));
944
945         /* Add our header */
946
947         igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
948         igmp->type      =
949         msg->im_msgtype = assert;
950         igmp->code      = 0;
951         ip_hdr(skb)->tot_len = htons(skb->len);         /* Fix the length */
952         skb->transport_header = skb->network_header;
953         }
954
955         rcu_read_lock();
956         mroute_sk = rcu_dereference(mrt->mroute_sk);
957         if (mroute_sk == NULL) {
958                 rcu_read_unlock();
959                 kfree_skb(skb);
960                 return -EINVAL;
961         }
962
963         /* Deliver to mrouted */
964
965         ret = sock_queue_rcv_skb(mroute_sk, skb);
966         rcu_read_unlock();
967         if (ret < 0) {
968                 if (net_ratelimit())
969                         printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
970                 kfree_skb(skb);
971         }
972
973         return ret;
974 }
975
976 /*
977  *      Queue a packet for resolution. It gets locked cache entry!
978  */
979
980 static int
981 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
982 {
983         bool found = false;
984         int err;
985         struct mfc_cache *c;
986         const struct iphdr *iph = ip_hdr(skb);
987
988         spin_lock_bh(&mfc_unres_lock);
989         list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
990                 if (c->mfc_mcastgrp == iph->daddr &&
991                     c->mfc_origin == iph->saddr) {
992                         found = true;
993                         break;
994                 }
995         }
996
997         if (!found) {
998                 /* Create a new entry if allowable */
999
1000                 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1001                     (c = ipmr_cache_alloc_unres()) == NULL) {
1002                         spin_unlock_bh(&mfc_unres_lock);
1003
1004                         kfree_skb(skb);
1005                         return -ENOBUFS;
1006                 }
1007
1008                 /* Fill in the new cache entry */
1009
1010                 c->mfc_parent   = -1;
1011                 c->mfc_origin   = iph->saddr;
1012                 c->mfc_mcastgrp = iph->daddr;
1013
1014                 /* Reflect first query at mrouted. */
1015
1016                 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1017                 if (err < 0) {
1018                         /* If the report failed throw the cache entry
1019                            out - Brad Parker
1020                          */
1021                         spin_unlock_bh(&mfc_unres_lock);
1022
1023                         ipmr_cache_free(c);
1024                         kfree_skb(skb);
1025                         return err;
1026                 }
1027
1028                 atomic_inc(&mrt->cache_resolve_queue_len);
1029                 list_add(&c->list, &mrt->mfc_unres_queue);
1030
1031                 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1032                         mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1033         }
1034
1035         /* See if we can append the packet */
1036
1037         if (c->mfc_un.unres.unresolved.qlen > 3) {
1038                 kfree_skb(skb);
1039                 err = -ENOBUFS;
1040         } else {
1041                 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1042                 err = 0;
1043         }
1044
1045         spin_unlock_bh(&mfc_unres_lock);
1046         return err;
1047 }
1048
1049 /*
1050  *      MFC cache manipulation by user space mroute daemon
1051  */
1052
1053 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1054 {
1055         int line;
1056         struct mfc_cache *c, *next;
1057
1058         line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1059
1060         list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1061                 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1062                     c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1063                         list_del_rcu(&c->list);
1064
1065                         ipmr_cache_free(c);
1066                         return 0;
1067                 }
1068         }
1069         return -ENOENT;
1070 }
1071
1072 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1073                         struct mfcctl *mfc, int mrtsock)
1074 {
1075         bool found = false;
1076         int line;
1077         struct mfc_cache *uc, *c;
1078
1079         if (mfc->mfcc_parent >= MAXVIFS)
1080                 return -ENFILE;
1081
1082         line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1083
1084         list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1085                 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1086                     c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1087                         found = true;
1088                         break;
1089                 }
1090         }
1091
1092         if (found) {
1093                 write_lock_bh(&mrt_lock);
1094                 c->mfc_parent = mfc->mfcc_parent;
1095                 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1096                 if (!mrtsock)
1097                         c->mfc_flags |= MFC_STATIC;
1098                 write_unlock_bh(&mrt_lock);
1099                 return 0;
1100         }
1101
1102         if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1103                 return -EINVAL;
1104
1105         c = ipmr_cache_alloc();
1106         if (c == NULL)
1107                 return -ENOMEM;
1108
1109         c->mfc_origin = mfc->mfcc_origin.s_addr;
1110         c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1111         c->mfc_parent = mfc->mfcc_parent;
1112         ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1113         if (!mrtsock)
1114                 c->mfc_flags |= MFC_STATIC;
1115
1116         list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1117
1118         /*
1119          *      Check to see if we resolved a queued list. If so we
1120          *      need to send on the frames and tidy up.
1121          */
1122         found = false;
1123         spin_lock_bh(&mfc_unres_lock);
1124         list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1125                 if (uc->mfc_origin == c->mfc_origin &&
1126                     uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1127                         list_del(&uc->list);
1128                         atomic_dec(&mrt->cache_resolve_queue_len);
1129                         found = true;
1130                         break;
1131                 }
1132         }
1133         if (list_empty(&mrt->mfc_unres_queue))
1134                 del_timer(&mrt->ipmr_expire_timer);
1135         spin_unlock_bh(&mfc_unres_lock);
1136
1137         if (found) {
1138                 ipmr_cache_resolve(net, mrt, uc, c);
1139                 ipmr_cache_free(uc);
1140         }
1141         return 0;
1142 }
1143
1144 /*
1145  *      Close the multicast socket, and clear the vif tables etc
1146  */
1147
1148 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1149 {
1150         int i;
1151         LIST_HEAD(list);
1152         struct mfc_cache *c, *next;
1153
1154         /* Shut down all active vif entries */
1155
1156         for (i = 0; i < mrt->maxvif; i++) {
1157                 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1158                         continue;
1159                 vif_delete(mrt, i, 0, &list);
1160         }
1161         unregister_netdevice_many(&list);
1162
1163         /* Wipe the cache */
1164
1165         for (i = 0; i < MFC_LINES; i++) {
1166                 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1167                         if (!all && (c->mfc_flags & MFC_STATIC))
1168                                 continue;
1169                         list_del_rcu(&c->list);
1170                         ipmr_cache_free(c);
1171                 }
1172         }
1173
1174         if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1175                 spin_lock_bh(&mfc_unres_lock);
1176                 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1177                         list_del(&c->list);
1178                         ipmr_destroy_unres(mrt, c);
1179                 }
1180                 spin_unlock_bh(&mfc_unres_lock);
1181         }
1182 }
1183
1184 /* called from ip_ra_control(), before an RCU grace period,
1185  * we dont need to call synchronize_rcu() here
1186  */
1187 static void mrtsock_destruct(struct sock *sk)
1188 {
1189         struct net *net = sock_net(sk);
1190         struct mr_table *mrt;
1191
1192         rtnl_lock();
1193         ipmr_for_each_table(mrt, net) {
1194                 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1195                         IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1196                         RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1197                         mroute_clean_tables(mrt, false);
1198                 }
1199         }
1200         rtnl_unlock();
1201 }
1202
1203 /*
1204  *      Socket options and virtual interface manipulation. The whole
1205  *      virtual interface system is a complete heap, but unfortunately
1206  *      that's how BSD mrouted happens to think. Maybe one day with a proper
1207  *      MOSPF/PIM router set up we can clean this up.
1208  */
1209
1210 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1211 {
1212         int ret;
1213         struct vifctl vif;
1214         struct mfcctl mfc;
1215         struct net *net = sock_net(sk);
1216         struct mr_table *mrt;
1217
1218         mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1219         if (mrt == NULL)
1220                 return -ENOENT;
1221
1222         if (optname != MRT_INIT) {
1223                 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1224                     !capable(CAP_NET_ADMIN))
1225                         return -EACCES;
1226         }
1227
1228         switch (optname) {
1229         case MRT_INIT:
1230                 if (sk->sk_type != SOCK_RAW ||
1231                     inet_sk(sk)->inet_num != IPPROTO_IGMP)
1232                         return -EOPNOTSUPP;
1233                 if (optlen != sizeof(int))
1234                         return -ENOPROTOOPT;
1235
1236                 rtnl_lock();
1237                 if (rtnl_dereference(mrt->mroute_sk)) {
1238                         rtnl_unlock();
1239                         return -EADDRINUSE;
1240                 }
1241
1242                 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1243                 if (ret == 0) {
1244                         rcu_assign_pointer(mrt->mroute_sk, sk);
1245                         IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1246                 }
1247                 rtnl_unlock();
1248                 return ret;
1249         case MRT_DONE:
1250                 if (sk != rcu_access_pointer(mrt->mroute_sk))
1251                         return -EACCES;
1252                 return ip_ra_control(sk, 0, NULL);
1253         case MRT_ADD_VIF:
1254         case MRT_DEL_VIF:
1255                 if (optlen != sizeof(vif))
1256                         return -EINVAL;
1257                 if (copy_from_user(&vif, optval, sizeof(vif)))
1258                         return -EFAULT;
1259                 if (vif.vifc_vifi >= MAXVIFS)
1260                         return -ENFILE;
1261                 rtnl_lock();
1262                 if (optname == MRT_ADD_VIF) {
1263                         ret = vif_add(net, mrt, &vif,
1264                                       sk == rtnl_dereference(mrt->mroute_sk));
1265                 } else {
1266                         ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1267                 }
1268                 rtnl_unlock();
1269                 return ret;
1270
1271                 /*
1272                  *      Manipulate the forwarding caches. These live
1273                  *      in a sort of kernel/user symbiosis.
1274                  */
1275         case MRT_ADD_MFC:
1276         case MRT_DEL_MFC:
1277                 if (optlen != sizeof(mfc))
1278                         return -EINVAL;
1279                 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1280                         return -EFAULT;
1281                 rtnl_lock();
1282                 if (optname == MRT_DEL_MFC)
1283                         ret = ipmr_mfc_delete(mrt, &mfc);
1284                 else
1285                         ret = ipmr_mfc_add(net, mrt, &mfc,
1286                                            sk == rtnl_dereference(mrt->mroute_sk));
1287                 rtnl_unlock();
1288                 return ret;
1289                 /*
1290                  *      Control PIM assert.
1291                  */
1292         case MRT_ASSERT:
1293         {
1294                 int v;
1295                 if (get_user(v, (int __user *)optval))
1296                         return -EFAULT;
1297                 mrt->mroute_do_assert = (v) ? 1 : 0;
1298                 return 0;
1299         }
1300 #ifdef CONFIG_IP_PIMSM
1301         case MRT_PIM:
1302         {
1303                 int v;
1304
1305                 if (get_user(v, (int __user *)optval))
1306                         return -EFAULT;
1307                 v = (v) ? 1 : 0;
1308
1309                 rtnl_lock();
1310                 ret = 0;
1311                 if (v != mrt->mroute_do_pim) {
1312                         mrt->mroute_do_pim = v;
1313                         mrt->mroute_do_assert = v;
1314                 }
1315                 rtnl_unlock();
1316                 return ret;
1317         }
1318 #endif
1319 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1320         case MRT_TABLE:
1321         {
1322                 u32 v;
1323
1324                 if (optlen != sizeof(u32))
1325                         return -EINVAL;
1326                 if (get_user(v, (u32 __user *)optval))
1327                         return -EFAULT;
1328
1329                 rtnl_lock();
1330                 ret = 0;
1331                 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1332                         ret = -EBUSY;
1333                 } else {
1334                         if (!ipmr_new_table(net, v))
1335                                 ret = -ENOMEM;
1336                         raw_sk(sk)->ipmr_table = v;
1337                 }
1338                 rtnl_unlock();
1339                 return ret;
1340         }
1341 #endif
1342         /*
1343          *      Spurious command, or MRT_VERSION which you cannot
1344          *      set.
1345          */
1346         default:
1347                 return -ENOPROTOOPT;
1348         }
1349 }
1350
1351 /*
1352  *      Getsock opt support for the multicast routing system.
1353  */
1354
1355 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1356 {
1357         int olr;
1358         int val;
1359         struct net *net = sock_net(sk);
1360         struct mr_table *mrt;
1361
1362         mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1363         if (mrt == NULL)
1364                 return -ENOENT;
1365
1366         if (optname != MRT_VERSION &&
1367 #ifdef CONFIG_IP_PIMSM
1368            optname != MRT_PIM &&
1369 #endif
1370            optname != MRT_ASSERT)
1371                 return -ENOPROTOOPT;
1372
1373         if (get_user(olr, optlen))
1374                 return -EFAULT;
1375
1376         olr = min_t(unsigned int, olr, sizeof(int));
1377         if (olr < 0)
1378                 return -EINVAL;
1379
1380         if (put_user(olr, optlen))
1381                 return -EFAULT;
1382         if (optname == MRT_VERSION)
1383                 val = 0x0305;
1384 #ifdef CONFIG_IP_PIMSM
1385         else if (optname == MRT_PIM)
1386                 val = mrt->mroute_do_pim;
1387 #endif
1388         else
1389                 val = mrt->mroute_do_assert;
1390         if (copy_to_user(optval, &val, olr))
1391                 return -EFAULT;
1392         return 0;
1393 }
1394
1395 /*
1396  *      The IP multicast ioctl support routines.
1397  */
1398
1399 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1400 {
1401         struct sioc_sg_req sr;
1402         struct sioc_vif_req vr;
1403         struct vif_device *vif;
1404         struct mfc_cache *c;
1405         struct net *net = sock_net(sk);
1406         struct mr_table *mrt;
1407
1408         mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1409         if (mrt == NULL)
1410                 return -ENOENT;
1411
1412         switch (cmd) {
1413         case SIOCGETVIFCNT:
1414                 if (copy_from_user(&vr, arg, sizeof(vr)))
1415                         return -EFAULT;
1416                 if (vr.vifi >= mrt->maxvif)
1417                         return -EINVAL;
1418                 read_lock(&mrt_lock);
1419                 vif = &mrt->vif_table[vr.vifi];
1420                 if (VIF_EXISTS(mrt, vr.vifi)) {
1421                         vr.icount = vif->pkt_in;
1422                         vr.ocount = vif->pkt_out;
1423                         vr.ibytes = vif->bytes_in;
1424                         vr.obytes = vif->bytes_out;
1425                         read_unlock(&mrt_lock);
1426
1427                         if (copy_to_user(arg, &vr, sizeof(vr)))
1428                                 return -EFAULT;
1429                         return 0;
1430                 }
1431                 read_unlock(&mrt_lock);
1432                 return -EADDRNOTAVAIL;
1433         case SIOCGETSGCNT:
1434                 if (copy_from_user(&sr, arg, sizeof(sr)))
1435                         return -EFAULT;
1436
1437                 rcu_read_lock();
1438                 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1439                 if (c) {
1440                         sr.pktcnt = c->mfc_un.res.pkt;
1441                         sr.bytecnt = c->mfc_un.res.bytes;
1442                         sr.wrong_if = c->mfc_un.res.wrong_if;
1443                         rcu_read_unlock();
1444
1445                         if (copy_to_user(arg, &sr, sizeof(sr)))
1446                                 return -EFAULT;
1447                         return 0;
1448                 }
1449                 rcu_read_unlock();
1450                 return -EADDRNOTAVAIL;
1451         default:
1452                 return -ENOIOCTLCMD;
1453         }
1454 }
1455
1456 #ifdef CONFIG_COMPAT
1457 struct compat_sioc_sg_req {
1458         struct in_addr src;
1459         struct in_addr grp;
1460         compat_ulong_t pktcnt;
1461         compat_ulong_t bytecnt;
1462         compat_ulong_t wrong_if;
1463 };
1464
1465 struct compat_sioc_vif_req {
1466         vifi_t  vifi;           /* Which iface */
1467         compat_ulong_t icount;
1468         compat_ulong_t ocount;
1469         compat_ulong_t ibytes;
1470         compat_ulong_t obytes;
1471 };
1472
1473 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1474 {
1475         struct compat_sioc_sg_req sr;
1476         struct compat_sioc_vif_req vr;
1477         struct vif_device *vif;
1478         struct mfc_cache *c;
1479         struct net *net = sock_net(sk);
1480         struct mr_table *mrt;
1481
1482         mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1483         if (mrt == NULL)
1484                 return -ENOENT;
1485
1486         switch (cmd) {
1487         case SIOCGETVIFCNT:
1488                 if (copy_from_user(&vr, arg, sizeof(vr)))
1489                         return -EFAULT;
1490                 if (vr.vifi >= mrt->maxvif)
1491                         return -EINVAL;
1492                 read_lock(&mrt_lock);
1493                 vif = &mrt->vif_table[vr.vifi];
1494                 if (VIF_EXISTS(mrt, vr.vifi)) {
1495                         vr.icount = vif->pkt_in;
1496                         vr.ocount = vif->pkt_out;
1497                         vr.ibytes = vif->bytes_in;
1498                         vr.obytes = vif->bytes_out;
1499                         read_unlock(&mrt_lock);
1500
1501                         if (copy_to_user(arg, &vr, sizeof(vr)))
1502                                 return -EFAULT;
1503                         return 0;
1504                 }
1505                 read_unlock(&mrt_lock);
1506                 return -EADDRNOTAVAIL;
1507         case SIOCGETSGCNT:
1508                 if (copy_from_user(&sr, arg, sizeof(sr)))
1509                         return -EFAULT;
1510
1511                 rcu_read_lock();
1512                 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1513                 if (c) {
1514                         sr.pktcnt = c->mfc_un.res.pkt;
1515                         sr.bytecnt = c->mfc_un.res.bytes;
1516                         sr.wrong_if = c->mfc_un.res.wrong_if;
1517                         rcu_read_unlock();
1518
1519                         if (copy_to_user(arg, &sr, sizeof(sr)))
1520                                 return -EFAULT;
1521                         return 0;
1522                 }
1523                 rcu_read_unlock();
1524                 return -EADDRNOTAVAIL;
1525         default:
1526                 return -ENOIOCTLCMD;
1527         }
1528 }
1529 #endif
1530
1531
1532 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1533 {
1534         struct net_device *dev = ptr;
1535         struct net *net = dev_net(dev);
1536         struct mr_table *mrt;
1537         struct vif_device *v;
1538         int ct;
1539         LIST_HEAD(list);
1540
1541         if (event != NETDEV_UNREGISTER)
1542                 return NOTIFY_DONE;
1543
1544         ipmr_for_each_table(mrt, net) {
1545                 v = &mrt->vif_table[0];
1546                 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1547                         if (v->dev == dev)
1548                                 vif_delete(mrt, ct, 1, &list);
1549                 }
1550         }
1551         unregister_netdevice_many(&list);
1552         return NOTIFY_DONE;
1553 }
1554
1555
1556 static struct notifier_block ip_mr_notifier = {
1557         .notifier_call = ipmr_device_event,
1558 };
1559
1560 /*
1561  *      Encapsulate a packet by attaching a valid IPIP header to it.
1562  *      This avoids tunnel drivers and other mess and gives us the speed so
1563  *      important for multicast video.
1564  */
1565
1566 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1567 {
1568         struct iphdr *iph;
1569         const struct iphdr *old_iph = ip_hdr(skb);
1570
1571         skb_push(skb, sizeof(struct iphdr));
1572         skb->transport_header = skb->network_header;
1573         skb_reset_network_header(skb);
1574         iph = ip_hdr(skb);
1575
1576         iph->version    =       4;
1577         iph->tos        =       old_iph->tos;
1578         iph->ttl        =       old_iph->ttl;
1579         iph->frag_off   =       0;
1580         iph->daddr      =       daddr;
1581         iph->saddr      =       saddr;
1582         iph->protocol   =       IPPROTO_IPIP;
1583         iph->ihl        =       5;
1584         iph->tot_len    =       htons(skb->len);
1585         ip_select_ident(skb, NULL);
1586         ip_send_check(iph);
1587
1588         memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1589         nf_reset(skb);
1590 }
1591
1592 static inline int ipmr_forward_finish(struct sk_buff *skb)
1593 {
1594         struct ip_options *opt = &(IPCB(skb)->opt);
1595
1596         IP_INC_STATS(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1597
1598         if (unlikely(opt->optlen))
1599                 ip_forward_options(skb);
1600
1601         return dst_output(skb);
1602 }
1603
1604 /*
1605  *      Processing handlers for ipmr_forward
1606  */
1607
1608 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1609                             struct sk_buff *skb, struct mfc_cache *c, int vifi)
1610 {
1611         const struct iphdr *iph = ip_hdr(skb);
1612         struct vif_device *vif = &mrt->vif_table[vifi];
1613         struct net_device *dev;
1614         struct rtable *rt;
1615         struct flowi4 fl4;
1616         int    encap = 0;
1617
1618         if (vif->dev == NULL)
1619                 goto out_free;
1620
1621 #ifdef CONFIG_IP_PIMSM
1622         if (vif->flags & VIFF_REGISTER) {
1623                 vif->pkt_out++;
1624                 vif->bytes_out += skb->len;
1625                 vif->dev->stats.tx_bytes += skb->len;
1626                 vif->dev->stats.tx_packets++;
1627                 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1628                 goto out_free;
1629         }
1630 #endif
1631
1632         if (vif->flags & VIFF_TUNNEL) {
1633                 rt = ip_route_output_ports(net, &fl4, NULL,
1634                                            vif->remote, vif->local,
1635                                            0, 0,
1636                                            IPPROTO_IPIP,
1637                                            RT_TOS(iph->tos), vif->link);
1638                 if (IS_ERR(rt))
1639                         goto out_free;
1640                 encap = sizeof(struct iphdr);
1641         } else {
1642                 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1643                                            0, 0,
1644                                            IPPROTO_IPIP,
1645                                            RT_TOS(iph->tos), vif->link);
1646                 if (IS_ERR(rt))
1647                         goto out_free;
1648         }
1649
1650         dev = rt->dst.dev;
1651
1652         if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1653                 /* Do not fragment multicasts. Alas, IPv4 does not
1654                  * allow to send ICMP, so that packets will disappear
1655                  * to blackhole.
1656                  */
1657
1658                 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1659                 ip_rt_put(rt);
1660                 goto out_free;
1661         }
1662
1663         encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1664
1665         if (skb_cow(skb, encap)) {
1666                 ip_rt_put(rt);
1667                 goto out_free;
1668         }
1669
1670         vif->pkt_out++;
1671         vif->bytes_out += skb->len;
1672
1673         skb_dst_drop(skb);
1674         skb_dst_set(skb, &rt->dst);
1675         ip_decrease_ttl(ip_hdr(skb));
1676
1677         /* FIXME: forward and output firewalls used to be called here.
1678          * What do we do with netfilter? -- RR
1679          */
1680         if (vif->flags & VIFF_TUNNEL) {
1681                 ip_encap(skb, vif->local, vif->remote);
1682                 /* FIXME: extra output firewall step used to be here. --RR */
1683                 vif->dev->stats.tx_packets++;
1684                 vif->dev->stats.tx_bytes += skb->len;
1685         }
1686
1687         IPCB(skb)->flags |= IPSKB_FORWARDED;
1688
1689         /*
1690          * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1691          * not only before forwarding, but after forwarding on all output
1692          * interfaces. It is clear, if mrouter runs a multicasting
1693          * program, it should receive packets not depending to what interface
1694          * program is joined.
1695          * If we will not make it, the program will have to join on all
1696          * interfaces. On the other hand, multihoming host (or router, but
1697          * not mrouter) cannot join to more than one interface - it will
1698          * result in receiving multiple packets.
1699          */
1700         NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1701                 ipmr_forward_finish);
1702         return;
1703
1704 out_free:
1705         kfree_skb(skb);
1706 }
1707
1708 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1709 {
1710         int ct;
1711
1712         for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1713                 if (mrt->vif_table[ct].dev == dev)
1714                         break;
1715         }
1716         return ct;
1717 }
1718
1719 /* "local" means that we should preserve one skb (for local delivery) */
1720
1721 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1722                          struct sk_buff *skb, struct mfc_cache *cache,
1723                          int local)
1724 {
1725         int psend = -1;
1726         int vif, ct;
1727
1728         vif = cache->mfc_parent;
1729         cache->mfc_un.res.pkt++;
1730         cache->mfc_un.res.bytes += skb->len;
1731
1732         /*
1733          * Wrong interface: drop packet and (maybe) send PIM assert.
1734          */
1735         if (mrt->vif_table[vif].dev != skb->dev) {
1736                 int true_vifi;
1737
1738                 if (rt_is_output_route(skb_rtable(skb))) {
1739                         /* It is our own packet, looped back.
1740                          * Very complicated situation...
1741                          *
1742                          * The best workaround until routing daemons will be
1743                          * fixed is not to redistribute packet, if it was
1744                          * send through wrong interface. It means, that
1745                          * multicast applications WILL NOT work for
1746                          * (S,G), which have default multicast route pointing
1747                          * to wrong oif. In any case, it is not a good
1748                          * idea to use multicasting applications on router.
1749                          */
1750                         goto dont_forward;
1751                 }
1752
1753                 cache->mfc_un.res.wrong_if++;
1754                 true_vifi = ipmr_find_vif(mrt, skb->dev);
1755
1756                 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1757                     /* pimsm uses asserts, when switching from RPT to SPT,
1758                      * so that we cannot check that packet arrived on an oif.
1759                      * It is bad, but otherwise we would need to move pretty
1760                      * large chunk of pimd to kernel. Ough... --ANK
1761                      */
1762                     (mrt->mroute_do_pim ||
1763                      cache->mfc_un.res.ttls[true_vifi] < 255) &&
1764                     time_after(jiffies,
1765                                cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1766                         cache->mfc_un.res.last_assert = jiffies;
1767                         ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1768                 }
1769                 goto dont_forward;
1770         }
1771
1772         mrt->vif_table[vif].pkt_in++;
1773         mrt->vif_table[vif].bytes_in += skb->len;
1774
1775         /*
1776          *      Forward the frame
1777          */
1778         for (ct = cache->mfc_un.res.maxvif - 1;
1779              ct >= cache->mfc_un.res.minvif; ct--) {
1780                 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1781                         if (psend != -1) {
1782                                 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1783
1784                                 if (skb2)
1785                                         ipmr_queue_xmit(net, mrt, skb2, cache,
1786                                                         psend);
1787                         }
1788                         psend = ct;
1789                 }
1790         }
1791         if (psend != -1) {
1792                 if (local) {
1793                         struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1794
1795                         if (skb2)
1796                                 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1797                 } else {
1798                         ipmr_queue_xmit(net, mrt, skb, cache, psend);
1799                         return 0;
1800                 }
1801         }
1802
1803 dont_forward:
1804         if (!local)
1805                 kfree_skb(skb);
1806         return 0;
1807 }
1808
1809 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1810 {
1811         struct rtable *rt = skb_rtable(skb);
1812         struct iphdr *iph = ip_hdr(skb);
1813         struct flowi4 fl4 = {
1814                 .daddr = iph->daddr,
1815                 .saddr = iph->saddr,
1816                 .flowi4_tos = RT_TOS(iph->tos),
1817                 .flowi4_oif = rt->rt_oif,
1818                 .flowi4_iif = rt->rt_iif,
1819                 .flowi4_mark = rt->rt_mark,
1820         };
1821         struct mr_table *mrt;
1822         int err;
1823
1824         err = ipmr_fib_lookup(net, &fl4, &mrt);
1825         if (err)
1826                 return ERR_PTR(err);
1827         return mrt;
1828 }
1829
1830 /*
1831  *      Multicast packets for forwarding arrive here
1832  *      Called with rcu_read_lock();
1833  */
1834
1835 int ip_mr_input(struct sk_buff *skb)
1836 {
1837         struct mfc_cache *cache;
1838         struct net *net = dev_net(skb->dev);
1839         int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1840         struct mr_table *mrt;
1841
1842         /* Packet is looped back after forward, it should not be
1843          * forwarded second time, but still can be delivered locally.
1844          */
1845         if (IPCB(skb)->flags & IPSKB_FORWARDED)
1846                 goto dont_forward;
1847
1848         mrt = ipmr_rt_fib_lookup(net, skb);
1849         if (IS_ERR(mrt)) {
1850                 kfree_skb(skb);
1851                 return PTR_ERR(mrt);
1852         }
1853         if (!local) {
1854                 if (IPCB(skb)->opt.router_alert) {
1855                         if (ip_call_ra_chain(skb))
1856                                 return 0;
1857                 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1858                         /* IGMPv1 (and broken IGMPv2 implementations sort of
1859                          * Cisco IOS <= 11.2(8)) do not put router alert
1860                          * option to IGMP packets destined to routable
1861                          * groups. It is very bad, because it means
1862                          * that we can forward NO IGMP messages.
1863                          */
1864                         struct sock *mroute_sk;
1865
1866                         mroute_sk = rcu_dereference(mrt->mroute_sk);
1867                         if (mroute_sk) {
1868                                 nf_reset(skb);
1869                                 raw_rcv(mroute_sk, skb);
1870                                 return 0;
1871                         }
1872                     }
1873         }
1874
1875         /* already under rcu_read_lock() */
1876         cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1877
1878         /*
1879          *      No usable cache entry
1880          */
1881         if (cache == NULL) {
1882                 int vif;
1883
1884                 if (local) {
1885                         struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1886                         ip_local_deliver(skb);
1887                         if (skb2 == NULL)
1888                                 return -ENOBUFS;
1889                         skb = skb2;
1890                 }
1891
1892                 read_lock(&mrt_lock);
1893                 vif = ipmr_find_vif(mrt, skb->dev);
1894                 if (vif >= 0) {
1895                         int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1896                         read_unlock(&mrt_lock);
1897
1898                         return err2;
1899                 }
1900                 read_unlock(&mrt_lock);
1901                 kfree_skb(skb);
1902                 return -ENODEV;
1903         }
1904
1905         read_lock(&mrt_lock);
1906         ip_mr_forward(net, mrt, skb, cache, local);
1907         read_unlock(&mrt_lock);
1908
1909         if (local)
1910                 return ip_local_deliver(skb);
1911
1912         return 0;
1913
1914 dont_forward:
1915         if (local)
1916                 return ip_local_deliver(skb);
1917         kfree_skb(skb);
1918         return 0;
1919 }
1920
1921 #ifdef CONFIG_IP_PIMSM
1922 /* called with rcu_read_lock() */
1923 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1924                      unsigned int pimlen)
1925 {
1926         struct net_device *reg_dev = NULL;
1927         struct iphdr *encap;
1928
1929         encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1930         /*
1931          * Check that:
1932          * a. packet is really sent to a multicast group
1933          * b. packet is not a NULL-REGISTER
1934          * c. packet is not truncated
1935          */
1936         if (!ipv4_is_multicast(encap->daddr) ||
1937             encap->tot_len == 0 ||
1938             ntohs(encap->tot_len) + pimlen > skb->len)
1939                 return 1;
1940
1941         read_lock(&mrt_lock);
1942         if (mrt->mroute_reg_vif_num >= 0)
1943                 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1944         read_unlock(&mrt_lock);
1945
1946         if (reg_dev == NULL)
1947                 return 1;
1948
1949         skb->mac_header = skb->network_header;
1950         skb_pull(skb, (u8 *)encap - skb->data);
1951         skb_reset_network_header(skb);
1952         skb->protocol = htons(ETH_P_IP);
1953         skb->ip_summed = CHECKSUM_NONE;
1954         skb->pkt_type = PACKET_HOST;
1955
1956         skb_tunnel_rx(skb, reg_dev);
1957
1958         netif_rx(skb);
1959
1960         return NET_RX_SUCCESS;
1961 }
1962 #endif
1963
1964 #ifdef CONFIG_IP_PIMSM_V1
1965 /*
1966  * Handle IGMP messages of PIMv1
1967  */
1968
1969 int pim_rcv_v1(struct sk_buff *skb)
1970 {
1971         struct igmphdr *pim;
1972         struct net *net = dev_net(skb->dev);
1973         struct mr_table *mrt;
1974
1975         if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1976                 goto drop;
1977
1978         pim = igmp_hdr(skb);
1979
1980         mrt = ipmr_rt_fib_lookup(net, skb);
1981         if (IS_ERR(mrt))
1982                 goto drop;
1983         if (!mrt->mroute_do_pim ||
1984             pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1985                 goto drop;
1986
1987         if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1988 drop:
1989                 kfree_skb(skb);
1990         }
1991         return 0;
1992 }
1993 #endif
1994
1995 #ifdef CONFIG_IP_PIMSM_V2
1996 static int pim_rcv(struct sk_buff *skb)
1997 {
1998         struct pimreghdr *pim;
1999         struct net *net = dev_net(skb->dev);
2000         struct mr_table *mrt;
2001
2002         if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2003                 goto drop;
2004
2005         pim = (struct pimreghdr *)skb_transport_header(skb);
2006         if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2007             (pim->flags & PIM_NULL_REGISTER) ||
2008             (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2009              csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2010                 goto drop;
2011
2012         mrt = ipmr_rt_fib_lookup(net, skb);
2013         if (IS_ERR(mrt))
2014                 goto drop;
2015         if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2016 drop:
2017                 kfree_skb(skb);
2018         }
2019         return 0;
2020 }
2021 #endif
2022
2023 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2024                               struct mfc_cache *c, struct rtmsg *rtm)
2025 {
2026         int ct;
2027         struct rtnexthop *nhp;
2028         u8 *b = skb_tail_pointer(skb);
2029         struct rtattr *mp_head;
2030
2031         /* If cache is unresolved, don't try to parse IIF and OIF */
2032         if (c->mfc_parent >= MAXVIFS)
2033                 return -ENOENT;
2034
2035         if (VIF_EXISTS(mrt, c->mfc_parent))
2036                 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
2037
2038         mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
2039
2040         for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2041                 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2042                         if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2043                                 goto rtattr_failure;
2044                         nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
2045                         nhp->rtnh_flags = 0;
2046                         nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2047                         nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2048                         nhp->rtnh_len = sizeof(*nhp);
2049                 }
2050         }
2051         mp_head->rta_type = RTA_MULTIPATH;
2052         mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
2053         rtm->rtm_type = RTN_MULTICAST;
2054         return 1;
2055
2056 rtattr_failure:
2057         nlmsg_trim(skb, b);
2058         return -EMSGSIZE;
2059 }
2060
2061 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2062                    __be32 saddr, __be32 daddr,
2063                    struct rtmsg *rtm, int nowait, u32 portid)
2064 {
2065         struct mfc_cache *cache;
2066         struct mr_table *mrt;
2067         int err;
2068
2069         mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2070         if (mrt == NULL)
2071                 return -ENOENT;
2072
2073         rcu_read_lock();
2074         cache = ipmr_cache_find(mrt, saddr, daddr);
2075
2076         if (cache == NULL) {
2077                 struct sk_buff *skb2;
2078                 struct iphdr *iph;
2079                 struct net_device *dev;
2080                 int vif = -1;
2081
2082                 if (nowait) {
2083                         rcu_read_unlock();
2084                         return -EAGAIN;
2085                 }
2086
2087                 dev = skb->dev;
2088                 read_lock(&mrt_lock);
2089                 if (dev)
2090                         vif = ipmr_find_vif(mrt, dev);
2091                 if (vif < 0) {
2092                         read_unlock(&mrt_lock);
2093                         rcu_read_unlock();
2094                         return -ENODEV;
2095                 }
2096                 skb2 = skb_clone(skb, GFP_ATOMIC);
2097                 if (!skb2) {
2098                         read_unlock(&mrt_lock);
2099                         rcu_read_unlock();
2100                         return -ENOMEM;
2101                 }
2102
2103                 NETLINK_CB(skb2).pid = portid;
2104                 skb_push(skb2, sizeof(struct iphdr));
2105                 skb_reset_network_header(skb2);
2106                 iph = ip_hdr(skb2);
2107                 iph->ihl = sizeof(struct iphdr) >> 2;
2108                 iph->saddr = saddr;
2109                 iph->daddr = daddr;
2110                 iph->version = 0;
2111                 err = ipmr_cache_unresolved(mrt, vif, skb2);
2112                 read_unlock(&mrt_lock);
2113                 rcu_read_unlock();
2114                 return err;
2115         }
2116
2117         read_lock(&mrt_lock);
2118         if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2119                 cache->mfc_flags |= MFC_NOTIFY;
2120         err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2121         read_unlock(&mrt_lock);
2122         rcu_read_unlock();
2123         return err;
2124 }
2125
2126 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2127                             u32 pid, u32 seq, struct mfc_cache *c)
2128 {
2129         struct nlmsghdr *nlh;
2130         struct rtmsg *rtm;
2131
2132         nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2133         if (nlh == NULL)
2134                 return -EMSGSIZE;
2135
2136         rtm = nlmsg_data(nlh);
2137         rtm->rtm_family   = RTNL_FAMILY_IPMR;
2138         rtm->rtm_dst_len  = 32;
2139         rtm->rtm_src_len  = 32;
2140         rtm->rtm_tos      = 0;
2141         rtm->rtm_table    = mrt->id;
2142         NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2143         rtm->rtm_type     = RTN_MULTICAST;
2144         rtm->rtm_scope    = RT_SCOPE_UNIVERSE;
2145         rtm->rtm_protocol = RTPROT_UNSPEC;
2146         rtm->rtm_flags    = 0;
2147
2148         NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2149         NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2150
2151         if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2152                 goto nla_put_failure;
2153
2154         return nlmsg_end(skb, nlh);
2155
2156 nla_put_failure:
2157         nlmsg_cancel(skb, nlh);
2158         return -EMSGSIZE;
2159 }
2160
2161 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2162 {
2163         struct net *net = sock_net(skb->sk);
2164         struct mr_table *mrt;
2165         struct mfc_cache *mfc;
2166         unsigned int t = 0, s_t;
2167         unsigned int h = 0, s_h;
2168         unsigned int e = 0, s_e;
2169
2170         s_t = cb->args[0];
2171         s_h = cb->args[1];
2172         s_e = cb->args[2];
2173
2174         rcu_read_lock();
2175         ipmr_for_each_table(mrt, net) {
2176                 if (t < s_t)
2177                         goto next_table;
2178                 if (t > s_t)
2179                         s_h = 0;
2180                 for (h = s_h; h < MFC_LINES; h++) {
2181                         list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2182                                 if (e < s_e)
2183                                         goto next_entry;
2184                                 if (ipmr_fill_mroute(mrt, skb,
2185                                                      NETLINK_CB(cb->skb).pid,
2186                                                      cb->nlh->nlmsg_seq,
2187                                                      mfc) < 0)
2188                                         goto done;
2189 next_entry:
2190                                 e++;
2191                         }
2192                         e = s_e = 0;
2193                 }
2194                 s_h = 0;
2195 next_table:
2196                 t++;
2197         }
2198 done:
2199         rcu_read_unlock();
2200
2201         cb->args[2] = e;
2202         cb->args[1] = h;
2203         cb->args[0] = t;
2204
2205         return skb->len;
2206 }
2207
2208 #ifdef CONFIG_PROC_FS
2209 /*
2210  *      The /proc interfaces to multicast routing :
2211  *      /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2212  */
2213 struct ipmr_vif_iter {
2214         struct seq_net_private p;
2215         struct mr_table *mrt;
2216         int ct;
2217 };
2218
2219 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2220                                            struct ipmr_vif_iter *iter,
2221                                            loff_t pos)
2222 {
2223         struct mr_table *mrt = iter->mrt;
2224
2225         for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2226                 if (!VIF_EXISTS(mrt, iter->ct))
2227                         continue;
2228                 if (pos-- == 0)
2229                         return &mrt->vif_table[iter->ct];
2230         }
2231         return NULL;
2232 }
2233
2234 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2235         __acquires(mrt_lock)
2236 {
2237         struct ipmr_vif_iter *iter = seq->private;
2238         struct net *net = seq_file_net(seq);
2239         struct mr_table *mrt;
2240
2241         mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2242         if (mrt == NULL)
2243                 return ERR_PTR(-ENOENT);
2244
2245         iter->mrt = mrt;
2246
2247         read_lock(&mrt_lock);
2248         return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2249                 : SEQ_START_TOKEN;
2250 }
2251
2252 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2253 {
2254         struct ipmr_vif_iter *iter = seq->private;
2255         struct net *net = seq_file_net(seq);
2256         struct mr_table *mrt = iter->mrt;
2257
2258         ++*pos;
2259         if (v == SEQ_START_TOKEN)
2260                 return ipmr_vif_seq_idx(net, iter, 0);
2261
2262         while (++iter->ct < mrt->maxvif) {
2263                 if (!VIF_EXISTS(mrt, iter->ct))
2264                         continue;
2265                 return &mrt->vif_table[iter->ct];
2266         }
2267         return NULL;
2268 }
2269
2270 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2271         __releases(mrt_lock)
2272 {
2273         read_unlock(&mrt_lock);
2274 }
2275
2276 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2277 {
2278         struct ipmr_vif_iter *iter = seq->private;
2279         struct mr_table *mrt = iter->mrt;
2280
2281         if (v == SEQ_START_TOKEN) {
2282                 seq_puts(seq,
2283                          "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags Local    Remote\n");
2284         } else {
2285                 const struct vif_device *vif = v;
2286                 const char *name =  vif->dev ? vif->dev->name : "none";
2287
2288                 seq_printf(seq,
2289                            "%2Zd %-10s %8ld %7ld  %8ld %7ld %05X %08X %08X\n",
2290                            vif - mrt->vif_table,
2291                            name, vif->bytes_in, vif->pkt_in,
2292                            vif->bytes_out, vif->pkt_out,
2293                            vif->flags, vif->local, vif->remote);
2294         }
2295         return 0;
2296 }
2297
2298 static const struct seq_operations ipmr_vif_seq_ops = {
2299         .start = ipmr_vif_seq_start,
2300         .next  = ipmr_vif_seq_next,
2301         .stop  = ipmr_vif_seq_stop,
2302         .show  = ipmr_vif_seq_show,
2303 };
2304
2305 static int ipmr_vif_open(struct inode *inode, struct file *file)
2306 {
2307         return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2308                             sizeof(struct ipmr_vif_iter));
2309 }
2310
2311 static const struct file_operations ipmr_vif_fops = {
2312         .owner   = THIS_MODULE,
2313         .open    = ipmr_vif_open,
2314         .read    = seq_read,
2315         .llseek  = seq_lseek,
2316         .release = seq_release_net,
2317 };
2318
2319 struct ipmr_mfc_iter {
2320         struct seq_net_private p;
2321         struct mr_table *mrt;
2322         struct list_head *cache;
2323         int ct;
2324 };
2325
2326
2327 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2328                                           struct ipmr_mfc_iter *it, loff_t pos)
2329 {
2330         struct mr_table *mrt = it->mrt;
2331         struct mfc_cache *mfc;
2332
2333         rcu_read_lock();
2334         for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2335                 it->cache = &mrt->mfc_cache_array[it->ct];
2336                 list_for_each_entry_rcu(mfc, it->cache, list)
2337                         if (pos-- == 0)
2338                                 return mfc;
2339         }
2340         rcu_read_unlock();
2341
2342         spin_lock_bh(&mfc_unres_lock);
2343         it->cache = &mrt->mfc_unres_queue;
2344         list_for_each_entry(mfc, it->cache, list)
2345                 if (pos-- == 0)
2346                         return mfc;
2347         spin_unlock_bh(&mfc_unres_lock);
2348
2349         it->cache = NULL;
2350         return NULL;
2351 }
2352
2353
2354 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2355 {
2356         struct ipmr_mfc_iter *it = seq->private;
2357         struct net *net = seq_file_net(seq);
2358         struct mr_table *mrt;
2359
2360         mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2361         if (mrt == NULL)
2362                 return ERR_PTR(-ENOENT);
2363
2364         it->mrt = mrt;
2365         it->cache = NULL;
2366         it->ct = 0;
2367         return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2368                 : SEQ_START_TOKEN;
2369 }
2370
2371 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2372 {
2373         struct mfc_cache *mfc = v;
2374         struct ipmr_mfc_iter *it = seq->private;
2375         struct net *net = seq_file_net(seq);
2376         struct mr_table *mrt = it->mrt;
2377
2378         ++*pos;
2379
2380         if (v == SEQ_START_TOKEN)
2381                 return ipmr_mfc_seq_idx(net, seq->private, 0);
2382
2383         if (mfc->list.next != it->cache)
2384                 return list_entry(mfc->list.next, struct mfc_cache, list);
2385
2386         if (it->cache == &mrt->mfc_unres_queue)
2387                 goto end_of_list;
2388
2389         BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2390
2391         while (++it->ct < MFC_LINES) {
2392                 it->cache = &mrt->mfc_cache_array[it->ct];
2393                 if (list_empty(it->cache))
2394                         continue;
2395                 return list_first_entry(it->cache, struct mfc_cache, list);
2396         }
2397
2398         /* exhausted cache_array, show unresolved */
2399         rcu_read_unlock();
2400         it->cache = &mrt->mfc_unres_queue;
2401         it->ct = 0;
2402
2403         spin_lock_bh(&mfc_unres_lock);
2404         if (!list_empty(it->cache))
2405                 return list_first_entry(it->cache, struct mfc_cache, list);
2406
2407 end_of_list:
2408         spin_unlock_bh(&mfc_unres_lock);
2409         it->cache = NULL;
2410
2411         return NULL;
2412 }
2413
2414 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2415 {
2416         struct ipmr_mfc_iter *it = seq->private;
2417         struct mr_table *mrt = it->mrt;
2418
2419         if (it->cache == &mrt->mfc_unres_queue)
2420                 spin_unlock_bh(&mfc_unres_lock);
2421         else if (it->cache == &mrt->mfc_cache_array[it->ct])
2422                 rcu_read_unlock();
2423 }
2424
2425 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2426 {
2427         int n;
2428
2429         if (v == SEQ_START_TOKEN) {
2430                 seq_puts(seq,
2431                  "Group    Origin   Iif     Pkts    Bytes    Wrong Oifs\n");
2432         } else {
2433                 const struct mfc_cache *mfc = v;
2434                 const struct ipmr_mfc_iter *it = seq->private;
2435                 const struct mr_table *mrt = it->mrt;
2436
2437                 seq_printf(seq, "%08X %08X %-3hd",
2438                            (__force u32) mfc->mfc_mcastgrp,
2439                            (__force u32) mfc->mfc_origin,
2440                            mfc->mfc_parent);
2441
2442                 if (it->cache != &mrt->mfc_unres_queue) {
2443                         seq_printf(seq, " %8lu %8lu %8lu",
2444                                    mfc->mfc_un.res.pkt,
2445                                    mfc->mfc_un.res.bytes,
2446                                    mfc->mfc_un.res.wrong_if);
2447                         for (n = mfc->mfc_un.res.minvif;
2448                              n < mfc->mfc_un.res.maxvif; n++) {
2449                                 if (VIF_EXISTS(mrt, n) &&
2450                                     mfc->mfc_un.res.ttls[n] < 255)
2451                                         seq_printf(seq,
2452                                            " %2d:%-3d",
2453                                            n, mfc->mfc_un.res.ttls[n]);
2454                         }
2455                 } else {
2456                         /* unresolved mfc_caches don't contain
2457                          * pkt, bytes and wrong_if values
2458                          */
2459                         seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2460                 }
2461                 seq_putc(seq, '\n');
2462         }
2463         return 0;
2464 }
2465
2466 static const struct seq_operations ipmr_mfc_seq_ops = {
2467         .start = ipmr_mfc_seq_start,
2468         .next  = ipmr_mfc_seq_next,
2469         .stop  = ipmr_mfc_seq_stop,
2470         .show  = ipmr_mfc_seq_show,
2471 };
2472
2473 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2474 {
2475         return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2476                             sizeof(struct ipmr_mfc_iter));
2477 }
2478
2479 static const struct file_operations ipmr_mfc_fops = {
2480         .owner   = THIS_MODULE,
2481         .open    = ipmr_mfc_open,
2482         .read    = seq_read,
2483         .llseek  = seq_lseek,
2484         .release = seq_release_net,
2485 };
2486 #endif
2487
2488 #ifdef CONFIG_IP_PIMSM_V2
2489 static const struct net_protocol pim_protocol = {
2490         .handler        =       pim_rcv,
2491         .netns_ok       =       1,
2492 };
2493 #endif
2494
2495
2496 /*
2497  *      Setup for IP multicast routing
2498  */
2499 static int __net_init ipmr_net_init(struct net *net)
2500 {
2501         int err;
2502
2503         err = ipmr_rules_init(net);
2504         if (err < 0)
2505                 goto fail;
2506
2507 #ifdef CONFIG_PROC_FS
2508         err = -ENOMEM;
2509         if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2510                 goto proc_vif_fail;
2511         if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2512                 goto proc_cache_fail;
2513 #endif
2514         return 0;
2515
2516 #ifdef CONFIG_PROC_FS
2517 proc_cache_fail:
2518         proc_net_remove(net, "ip_mr_vif");
2519 proc_vif_fail:
2520         ipmr_rules_exit(net);
2521 #endif
2522 fail:
2523         return err;
2524 }
2525
2526 static void __net_exit ipmr_net_exit(struct net *net)
2527 {
2528 #ifdef CONFIG_PROC_FS
2529         proc_net_remove(net, "ip_mr_cache");
2530         proc_net_remove(net, "ip_mr_vif");
2531 #endif
2532         ipmr_rules_exit(net);
2533 }
2534
2535 static struct pernet_operations ipmr_net_ops = {
2536         .init = ipmr_net_init,
2537         .exit = ipmr_net_exit,
2538 };
2539
2540 int __init ip_mr_init(void)
2541 {
2542         int err;
2543
2544         mrt_cachep = kmem_cache_create("ip_mrt_cache",
2545                                        sizeof(struct mfc_cache),
2546                                        0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2547                                        NULL);
2548         if (!mrt_cachep)
2549                 return -ENOMEM;
2550
2551         err = register_pernet_subsys(&ipmr_net_ops);
2552         if (err)
2553                 goto reg_pernet_fail;
2554
2555         err = register_netdevice_notifier(&ip_mr_notifier);
2556         if (err)
2557                 goto reg_notif_fail;
2558 #ifdef CONFIG_IP_PIMSM_V2
2559         if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2560                 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2561                 err = -EAGAIN;
2562                 goto add_proto_fail;
2563         }
2564 #endif
2565         rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2566                       NULL, ipmr_rtm_dumproute, NULL);
2567         return 0;
2568
2569 #ifdef CONFIG_IP_PIMSM_V2
2570 add_proto_fail:
2571         unregister_netdevice_notifier(&ip_mr_notifier);
2572 #endif
2573 reg_notif_fail:
2574         unregister_pernet_subsys(&ipmr_net_ops);
2575 reg_pernet_fail:
2576         kmem_cache_destroy(mrt_cachep);
2577         return err;
2578 }