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