]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv6/ip6mr.c
ipv6: ip6mr: convert struct mfc_cache to struct list_head
[net-next-2.6.git] / net / ipv6 / ip6mr.c
1 /*
2  *      Linux IPv6 multicast routing support for BSD pim6sd
3  *      Based on net/ipv4/ipmr.c.
4  *
5  *      (c) 2004 Mickael Hoerdt, <hoerdt@clarinet.u-strasbg.fr>
6  *              LSIIT Laboratory, Strasbourg, France
7  *      (c) 2004 Jean-Philippe Andriot, <jean-philippe.andriot@6WIND.com>
8  *              6WIND, Paris, France
9  *      Copyright (C)2007,2008 USAGI/WIDE Project
10  *              YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  *
17  */
18
19 #include <asm/system.h>
20 #include <asm/uaccess.h>
21 #include <linux/types.h>
22 #include <linux/sched.h>
23 #include <linux/errno.h>
24 #include <linux/timer.h>
25 #include <linux/mm.h>
26 #include <linux/kernel.h>
27 #include <linux/fcntl.h>
28 #include <linux/stat.h>
29 #include <linux/socket.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/inetdevice.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <net/protocol.h>
38 #include <linux/skbuff.h>
39 #include <net/sock.h>
40 #include <net/raw.h>
41 #include <linux/notifier.h>
42 #include <linux/if_arp.h>
43 #include <net/checksum.h>
44 #include <net/netlink.h>
45
46 #include <net/ipv6.h>
47 #include <net/ip6_route.h>
48 #include <linux/mroute6.h>
49 #include <linux/pim.h>
50 #include <net/addrconf.h>
51 #include <linux/netfilter_ipv6.h>
52 #include <net/ip6_checksum.h>
53
54 /* Big lock, protecting vif table, mrt cache and mroute socket state.
55    Note that the changes are semaphored via rtnl_lock.
56  */
57
58 static DEFINE_RWLOCK(mrt_lock);
59
60 /*
61  *      Multicast router control variables
62  */
63
64 #define MIF_EXISTS(_net, _idx) ((_net)->ipv6.vif6_table[_idx].dev != NULL)
65
66 /* Special spinlock for queue of unresolved entries */
67 static DEFINE_SPINLOCK(mfc_unres_lock);
68
69 /* We return to original Alan's scheme. Hash table of resolved
70    entries is changed only in process context and protected
71    with weak lock mrt_lock. Queue of unresolved entries is protected
72    with strong spinlock mfc_unres_lock.
73
74    In this case data path is free of exclusive locks at all.
75  */
76
77 static struct kmem_cache *mrt_cachep __read_mostly;
78
79 static int ip6_mr_forward(struct net *net, struct sk_buff *skb,
80                           struct mfc6_cache *cache);
81 static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt,
82                               mifi_t mifi, int assert);
83 static int ip6mr_fill_mroute(struct net *net, struct sk_buff *skb,
84                              struct mfc6_cache *c, struct rtmsg *rtm);
85 static void mroute_clean_tables(struct net *net);
86
87
88 #ifdef CONFIG_PROC_FS
89
90 struct ipmr_mfc_iter {
91         struct seq_net_private p;
92         struct list_head *cache;
93         int ct;
94 };
95
96
97 static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
98                                            struct ipmr_mfc_iter *it, loff_t pos)
99 {
100         struct mfc6_cache *mfc;
101
102         read_lock(&mrt_lock);
103         for (it->ct = 0; it->ct < MFC6_LINES; it->ct++) {
104                 it->cache = &net->ipv6.mfc6_cache_array[it->ct];
105                 list_for_each_entry(mfc, it->cache, list)
106                         if (pos-- == 0)
107                                 return mfc;
108         }
109         read_unlock(&mrt_lock);
110
111         spin_lock_bh(&mfc_unres_lock);
112         it->cache = &net->ipv6.mfc6_unres_queue;
113         list_for_each_entry(mfc, it->cache, list)
114                 if (pos-- == 0)
115                         return mfc;
116         spin_unlock_bh(&mfc_unres_lock);
117
118         it->cache = NULL;
119         return NULL;
120 }
121
122 /*
123  *      The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
124  */
125
126 struct ipmr_vif_iter {
127         struct seq_net_private p;
128         int ct;
129 };
130
131 static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
132                                             struct ipmr_vif_iter *iter,
133                                             loff_t pos)
134 {
135         for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) {
136                 if (!MIF_EXISTS(net, iter->ct))
137                         continue;
138                 if (pos-- == 0)
139                         return &net->ipv6.vif6_table[iter->ct];
140         }
141         return NULL;
142 }
143
144 static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
145         __acquires(mrt_lock)
146 {
147         struct net *net = seq_file_net(seq);
148
149         read_lock(&mrt_lock);
150         return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
151                 : SEQ_START_TOKEN;
152 }
153
154 static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
155 {
156         struct ipmr_vif_iter *iter = seq->private;
157         struct net *net = seq_file_net(seq);
158
159         ++*pos;
160         if (v == SEQ_START_TOKEN)
161                 return ip6mr_vif_seq_idx(net, iter, 0);
162
163         while (++iter->ct < net->ipv6.maxvif) {
164                 if (!MIF_EXISTS(net, iter->ct))
165                         continue;
166                 return &net->ipv6.vif6_table[iter->ct];
167         }
168         return NULL;
169 }
170
171 static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
172         __releases(mrt_lock)
173 {
174         read_unlock(&mrt_lock);
175 }
176
177 static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
178 {
179         struct net *net = seq_file_net(seq);
180
181         if (v == SEQ_START_TOKEN) {
182                 seq_puts(seq,
183                          "Interface      BytesIn  PktsIn  BytesOut PktsOut Flags\n");
184         } else {
185                 const struct mif_device *vif = v;
186                 const char *name = vif->dev ? vif->dev->name : "none";
187
188                 seq_printf(seq,
189                            "%2td %-10s %8ld %7ld  %8ld %7ld %05X\n",
190                            vif - net->ipv6.vif6_table,
191                            name, vif->bytes_in, vif->pkt_in,
192                            vif->bytes_out, vif->pkt_out,
193                            vif->flags);
194         }
195         return 0;
196 }
197
198 static const struct seq_operations ip6mr_vif_seq_ops = {
199         .start = ip6mr_vif_seq_start,
200         .next  = ip6mr_vif_seq_next,
201         .stop  = ip6mr_vif_seq_stop,
202         .show  = ip6mr_vif_seq_show,
203 };
204
205 static int ip6mr_vif_open(struct inode *inode, struct file *file)
206 {
207         return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
208                             sizeof(struct ipmr_vif_iter));
209 }
210
211 static const struct file_operations ip6mr_vif_fops = {
212         .owner   = THIS_MODULE,
213         .open    = ip6mr_vif_open,
214         .read    = seq_read,
215         .llseek  = seq_lseek,
216         .release = seq_release_net,
217 };
218
219 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
220 {
221         struct net *net = seq_file_net(seq);
222
223         return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
224                 : SEQ_START_TOKEN;
225 }
226
227 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
228 {
229         struct mfc6_cache *mfc = v;
230         struct ipmr_mfc_iter *it = seq->private;
231         struct net *net = seq_file_net(seq);
232
233         ++*pos;
234
235         if (v == SEQ_START_TOKEN)
236                 return ipmr_mfc_seq_idx(net, seq->private, 0);
237
238         if (mfc->list.next != it->cache)
239                 return list_entry(mfc->list.next, struct mfc6_cache, list);
240
241         if (it->cache == &net->ipv6.mfc6_unres_queue)
242                 goto end_of_list;
243
244         BUG_ON(it->cache != &net->ipv6.mfc6_cache_array[it->ct]);
245
246         while (++it->ct < MFC6_LINES) {
247                 it->cache = &net->ipv6.mfc6_cache_array[it->ct];
248                 if (list_empty(it->cache))
249                         continue;
250                 return list_first_entry(it->cache, struct mfc6_cache, list);
251         }
252
253         /* exhausted cache_array, show unresolved */
254         read_unlock(&mrt_lock);
255         it->cache = &net->ipv6.mfc6_unres_queue;
256         it->ct = 0;
257
258         spin_lock_bh(&mfc_unres_lock);
259         if (!list_empty(it->cache))
260                 return list_first_entry(it->cache, struct mfc6_cache, list);
261
262  end_of_list:
263         spin_unlock_bh(&mfc_unres_lock);
264         it->cache = NULL;
265
266         return NULL;
267 }
268
269 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
270 {
271         struct ipmr_mfc_iter *it = seq->private;
272         struct net *net = seq_file_net(seq);
273
274         if (it->cache == &net->ipv6.mfc6_unres_queue)
275                 spin_unlock_bh(&mfc_unres_lock);
276         else if (it->cache == net->ipv6.mfc6_cache_array)
277                 read_unlock(&mrt_lock);
278 }
279
280 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
281 {
282         int n;
283         struct net *net = seq_file_net(seq);
284
285         if (v == SEQ_START_TOKEN) {
286                 seq_puts(seq,
287                          "Group                            "
288                          "Origin                           "
289                          "Iif      Pkts  Bytes     Wrong  Oifs\n");
290         } else {
291                 const struct mfc6_cache *mfc = v;
292                 const struct ipmr_mfc_iter *it = seq->private;
293
294                 seq_printf(seq, "%pI6 %pI6 %-3hd",
295                            &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
296                            mfc->mf6c_parent);
297
298                 if (it->cache != &net->ipv6.mfc6_unres_queue) {
299                         seq_printf(seq, " %8lu %8lu %8lu",
300                                    mfc->mfc_un.res.pkt,
301                                    mfc->mfc_un.res.bytes,
302                                    mfc->mfc_un.res.wrong_if);
303                         for (n = mfc->mfc_un.res.minvif;
304                              n < mfc->mfc_un.res.maxvif; n++) {
305                                 if (MIF_EXISTS(net, n) &&
306                                     mfc->mfc_un.res.ttls[n] < 255)
307                                         seq_printf(seq,
308                                                    " %2d:%-3d",
309                                                    n, mfc->mfc_un.res.ttls[n]);
310                         }
311                 } else {
312                         /* unresolved mfc_caches don't contain
313                          * pkt, bytes and wrong_if values
314                          */
315                         seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
316                 }
317                 seq_putc(seq, '\n');
318         }
319         return 0;
320 }
321
322 static const struct seq_operations ipmr_mfc_seq_ops = {
323         .start = ipmr_mfc_seq_start,
324         .next  = ipmr_mfc_seq_next,
325         .stop  = ipmr_mfc_seq_stop,
326         .show  = ipmr_mfc_seq_show,
327 };
328
329 static int ipmr_mfc_open(struct inode *inode, struct file *file)
330 {
331         return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
332                             sizeof(struct ipmr_mfc_iter));
333 }
334
335 static const struct file_operations ip6mr_mfc_fops = {
336         .owner   = THIS_MODULE,
337         .open    = ipmr_mfc_open,
338         .read    = seq_read,
339         .llseek  = seq_lseek,
340         .release = seq_release_net,
341 };
342 #endif
343
344 #ifdef CONFIG_IPV6_PIMSM_V2
345
346 static int pim6_rcv(struct sk_buff *skb)
347 {
348         struct pimreghdr *pim;
349         struct ipv6hdr   *encap;
350         struct net_device  *reg_dev = NULL;
351         struct net *net = dev_net(skb->dev);
352         int reg_vif_num = net->ipv6.mroute_reg_vif_num;
353
354         if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
355                 goto drop;
356
357         pim = (struct pimreghdr *)skb_transport_header(skb);
358         if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
359             (pim->flags & PIM_NULL_REGISTER) ||
360             (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
361                              sizeof(*pim), IPPROTO_PIM,
362                              csum_partial((void *)pim, sizeof(*pim), 0)) &&
363              csum_fold(skb_checksum(skb, 0, skb->len, 0))))
364                 goto drop;
365
366         /* check if the inner packet is destined to mcast group */
367         encap = (struct ipv6hdr *)(skb_transport_header(skb) +
368                                    sizeof(*pim));
369
370         if (!ipv6_addr_is_multicast(&encap->daddr) ||
371             encap->payload_len == 0 ||
372             ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
373                 goto drop;
374
375         read_lock(&mrt_lock);
376         if (reg_vif_num >= 0)
377                 reg_dev = net->ipv6.vif6_table[reg_vif_num].dev;
378         if (reg_dev)
379                 dev_hold(reg_dev);
380         read_unlock(&mrt_lock);
381
382         if (reg_dev == NULL)
383                 goto drop;
384
385         skb->mac_header = skb->network_header;
386         skb_pull(skb, (u8 *)encap - skb->data);
387         skb_reset_network_header(skb);
388         skb->dev = reg_dev;
389         skb->protocol = htons(ETH_P_IPV6);
390         skb->ip_summed = 0;
391         skb->pkt_type = PACKET_HOST;
392         skb_dst_drop(skb);
393         reg_dev->stats.rx_bytes += skb->len;
394         reg_dev->stats.rx_packets++;
395         nf_reset(skb);
396         netif_rx(skb);
397         dev_put(reg_dev);
398         return 0;
399  drop:
400         kfree_skb(skb);
401         return 0;
402 }
403
404 static const struct inet6_protocol pim6_protocol = {
405         .handler        =       pim6_rcv,
406 };
407
408 /* Service routines creating virtual interfaces: PIMREG */
409
410 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
411                                       struct net_device *dev)
412 {
413         struct net *net = dev_net(dev);
414
415         read_lock(&mrt_lock);
416         dev->stats.tx_bytes += skb->len;
417         dev->stats.tx_packets++;
418         ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num,
419                            MRT6MSG_WHOLEPKT);
420         read_unlock(&mrt_lock);
421         kfree_skb(skb);
422         return NETDEV_TX_OK;
423 }
424
425 static const struct net_device_ops reg_vif_netdev_ops = {
426         .ndo_start_xmit = reg_vif_xmit,
427 };
428
429 static void reg_vif_setup(struct net_device *dev)
430 {
431         dev->type               = ARPHRD_PIMREG;
432         dev->mtu                = 1500 - sizeof(struct ipv6hdr) - 8;
433         dev->flags              = IFF_NOARP;
434         dev->netdev_ops         = &reg_vif_netdev_ops;
435         dev->destructor         = free_netdev;
436         dev->features           |= NETIF_F_NETNS_LOCAL;
437 }
438
439 static struct net_device *ip6mr_reg_vif(struct net *net)
440 {
441         struct net_device *dev;
442
443         dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
444         if (dev == NULL)
445                 return NULL;
446
447         dev_net_set(dev, net);
448
449         if (register_netdevice(dev)) {
450                 free_netdev(dev);
451                 return NULL;
452         }
453         dev->iflink = 0;
454
455         if (dev_open(dev))
456                 goto failure;
457
458         dev_hold(dev);
459         return dev;
460
461 failure:
462         /* allow the register to be completed before unregistering. */
463         rtnl_unlock();
464         rtnl_lock();
465
466         unregister_netdevice(dev);
467         return NULL;
468 }
469 #endif
470
471 /*
472  *      Delete a VIF entry
473  */
474
475 static int mif6_delete(struct net *net, int vifi, struct list_head *head)
476 {
477         struct mif_device *v;
478         struct net_device *dev;
479         struct inet6_dev *in6_dev;
480         if (vifi < 0 || vifi >= net->ipv6.maxvif)
481                 return -EADDRNOTAVAIL;
482
483         v = &net->ipv6.vif6_table[vifi];
484
485         write_lock_bh(&mrt_lock);
486         dev = v->dev;
487         v->dev = NULL;
488
489         if (!dev) {
490                 write_unlock_bh(&mrt_lock);
491                 return -EADDRNOTAVAIL;
492         }
493
494 #ifdef CONFIG_IPV6_PIMSM_V2
495         if (vifi == net->ipv6.mroute_reg_vif_num)
496                 net->ipv6.mroute_reg_vif_num = -1;
497 #endif
498
499         if (vifi + 1 == net->ipv6.maxvif) {
500                 int tmp;
501                 for (tmp = vifi - 1; tmp >= 0; tmp--) {
502                         if (MIF_EXISTS(net, tmp))
503                                 break;
504                 }
505                 net->ipv6.maxvif = tmp + 1;
506         }
507
508         write_unlock_bh(&mrt_lock);
509
510         dev_set_allmulti(dev, -1);
511
512         in6_dev = __in6_dev_get(dev);
513         if (in6_dev)
514                 in6_dev->cnf.mc_forwarding--;
515
516         if (v->flags & MIFF_REGISTER)
517                 unregister_netdevice_queue(dev, head);
518
519         dev_put(dev);
520         return 0;
521 }
522
523 static inline void ip6mr_cache_free(struct mfc6_cache *c)
524 {
525         kmem_cache_free(mrt_cachep, c);
526 }
527
528 /* Destroy an unresolved cache entry, killing queued skbs
529    and reporting error to netlink readers.
530  */
531
532 static void ip6mr_destroy_unres(struct net *net, struct mfc6_cache *c)
533 {
534         struct sk_buff *skb;
535
536         atomic_dec(&net->ipv6.cache_resolve_queue_len);
537
538         while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
539                 if (ipv6_hdr(skb)->version == 0) {
540                         struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
541                         nlh->nlmsg_type = NLMSG_ERROR;
542                         nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
543                         skb_trim(skb, nlh->nlmsg_len);
544                         ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
545                         rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
546                 } else
547                         kfree_skb(skb);
548         }
549
550         ip6mr_cache_free(c);
551 }
552
553
554 /* Timer process for all the unresolved queue. */
555
556 static void ipmr_do_expire_process(struct net *net)
557 {
558         unsigned long now = jiffies;
559         unsigned long expires = 10 * HZ;
560         struct mfc6_cache *c, *next;
561
562         list_for_each_entry_safe(c, next, &net->ipv6.mfc6_unres_queue, list) {
563                 if (time_after(c->mfc_un.unres.expires, now)) {
564                         /* not yet... */
565                         unsigned long interval = c->mfc_un.unres.expires - now;
566                         if (interval < expires)
567                                 expires = interval;
568                         continue;
569                 }
570
571                 list_del(&c->list);
572                 ip6mr_destroy_unres(net, c);
573         }
574
575         if (!list_empty(&net->ipv6.mfc6_unres_queue))
576                 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + expires);
577 }
578
579 static void ipmr_expire_process(unsigned long arg)
580 {
581         struct net *net = (struct net *)arg;
582
583         if (!spin_trylock(&mfc_unres_lock)) {
584                 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + 1);
585                 return;
586         }
587
588         if (!list_empty(&net->ipv6.mfc6_unres_queue))
589                 ipmr_do_expire_process(net);
590
591         spin_unlock(&mfc_unres_lock);
592 }
593
594 /* Fill oifs list. It is called under write locked mrt_lock. */
595
596 static void ip6mr_update_thresholds(struct net *net, struct mfc6_cache *cache,
597                                     unsigned char *ttls)
598 {
599         int vifi;
600
601         cache->mfc_un.res.minvif = MAXMIFS;
602         cache->mfc_un.res.maxvif = 0;
603         memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
604
605         for (vifi = 0; vifi < net->ipv6.maxvif; vifi++) {
606                 if (MIF_EXISTS(net, vifi) &&
607                     ttls[vifi] && ttls[vifi] < 255) {
608                         cache->mfc_un.res.ttls[vifi] = ttls[vifi];
609                         if (cache->mfc_un.res.minvif > vifi)
610                                 cache->mfc_un.res.minvif = vifi;
611                         if (cache->mfc_un.res.maxvif <= vifi)
612                                 cache->mfc_un.res.maxvif = vifi + 1;
613                 }
614         }
615 }
616
617 static int mif6_add(struct net *net, struct mif6ctl *vifc, int mrtsock)
618 {
619         int vifi = vifc->mif6c_mifi;
620         struct mif_device *v = &net->ipv6.vif6_table[vifi];
621         struct net_device *dev;
622         struct inet6_dev *in6_dev;
623         int err;
624
625         /* Is vif busy ? */
626         if (MIF_EXISTS(net, vifi))
627                 return -EADDRINUSE;
628
629         switch (vifc->mif6c_flags) {
630 #ifdef CONFIG_IPV6_PIMSM_V2
631         case MIFF_REGISTER:
632                 /*
633                  * Special Purpose VIF in PIM
634                  * All the packets will be sent to the daemon
635                  */
636                 if (net->ipv6.mroute_reg_vif_num >= 0)
637                         return -EADDRINUSE;
638                 dev = ip6mr_reg_vif(net);
639                 if (!dev)
640                         return -ENOBUFS;
641                 err = dev_set_allmulti(dev, 1);
642                 if (err) {
643                         unregister_netdevice(dev);
644                         dev_put(dev);
645                         return err;
646                 }
647                 break;
648 #endif
649         case 0:
650                 dev = dev_get_by_index(net, vifc->mif6c_pifi);
651                 if (!dev)
652                         return -EADDRNOTAVAIL;
653                 err = dev_set_allmulti(dev, 1);
654                 if (err) {
655                         dev_put(dev);
656                         return err;
657                 }
658                 break;
659         default:
660                 return -EINVAL;
661         }
662
663         in6_dev = __in6_dev_get(dev);
664         if (in6_dev)
665                 in6_dev->cnf.mc_forwarding++;
666
667         /*
668          *      Fill in the VIF structures
669          */
670         v->rate_limit = vifc->vifc_rate_limit;
671         v->flags = vifc->mif6c_flags;
672         if (!mrtsock)
673                 v->flags |= VIFF_STATIC;
674         v->threshold = vifc->vifc_threshold;
675         v->bytes_in = 0;
676         v->bytes_out = 0;
677         v->pkt_in = 0;
678         v->pkt_out = 0;
679         v->link = dev->ifindex;
680         if (v->flags & MIFF_REGISTER)
681                 v->link = dev->iflink;
682
683         /* And finish update writing critical data */
684         write_lock_bh(&mrt_lock);
685         v->dev = dev;
686 #ifdef CONFIG_IPV6_PIMSM_V2
687         if (v->flags & MIFF_REGISTER)
688                 net->ipv6.mroute_reg_vif_num = vifi;
689 #endif
690         if (vifi + 1 > net->ipv6.maxvif)
691                 net->ipv6.maxvif = vifi + 1;
692         write_unlock_bh(&mrt_lock);
693         return 0;
694 }
695
696 static struct mfc6_cache *ip6mr_cache_find(struct net *net,
697                                            struct in6_addr *origin,
698                                            struct in6_addr *mcastgrp)
699 {
700         int line = MFC6_HASH(mcastgrp, origin);
701         struct mfc6_cache *c;
702
703         list_for_each_entry(c, &net->ipv6.mfc6_cache_array[line], list) {
704                 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
705                     ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
706                         return c;
707         }
708         return NULL;
709 }
710
711 /*
712  *      Allocate a multicast cache entry
713  */
714 static struct mfc6_cache *ip6mr_cache_alloc(void)
715 {
716         struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
717         if (c == NULL)
718                 return NULL;
719         c->mfc_un.res.minvif = MAXMIFS;
720         return c;
721 }
722
723 static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
724 {
725         struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
726         if (c == NULL)
727                 return NULL;
728         skb_queue_head_init(&c->mfc_un.unres.unresolved);
729         c->mfc_un.unres.expires = jiffies + 10 * HZ;
730         return c;
731 }
732
733 /*
734  *      A cache entry has gone into a resolved state from queued
735  */
736
737 static void ip6mr_cache_resolve(struct net *net, struct mfc6_cache *uc,
738                                 struct mfc6_cache *c)
739 {
740         struct sk_buff *skb;
741
742         /*
743          *      Play the pending entries through our router
744          */
745
746         while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
747                 if (ipv6_hdr(skb)->version == 0) {
748                         int err;
749                         struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
750
751                         if (ip6mr_fill_mroute(net, skb, c, NLMSG_DATA(nlh)) > 0) {
752                                 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
753                         } else {
754                                 nlh->nlmsg_type = NLMSG_ERROR;
755                                 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
756                                 skb_trim(skb, nlh->nlmsg_len);
757                                 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
758                         }
759                         err = rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
760                 } else
761                         ip6_mr_forward(net, skb, c);
762         }
763 }
764
765 /*
766  *      Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
767  *      expects the following bizarre scheme.
768  *
769  *      Called under mrt_lock.
770  */
771
772 static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi,
773                               int assert)
774 {
775         struct sk_buff *skb;
776         struct mrt6msg *msg;
777         int ret;
778
779 #ifdef CONFIG_IPV6_PIMSM_V2
780         if (assert == MRT6MSG_WHOLEPKT)
781                 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
782                                                 +sizeof(*msg));
783         else
784 #endif
785                 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
786
787         if (!skb)
788                 return -ENOBUFS;
789
790         /* I suppose that internal messages
791          * do not require checksums */
792
793         skb->ip_summed = CHECKSUM_UNNECESSARY;
794
795 #ifdef CONFIG_IPV6_PIMSM_V2
796         if (assert == MRT6MSG_WHOLEPKT) {
797                 /* Ugly, but we have no choice with this interface.
798                    Duplicate old header, fix length etc.
799                    And all this only to mangle msg->im6_msgtype and
800                    to set msg->im6_mbz to "mbz" :-)
801                  */
802                 skb_push(skb, -skb_network_offset(pkt));
803
804                 skb_push(skb, sizeof(*msg));
805                 skb_reset_transport_header(skb);
806                 msg = (struct mrt6msg *)skb_transport_header(skb);
807                 msg->im6_mbz = 0;
808                 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
809                 msg->im6_mif = net->ipv6.mroute_reg_vif_num;
810                 msg->im6_pad = 0;
811                 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
812                 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
813
814                 skb->ip_summed = CHECKSUM_UNNECESSARY;
815         } else
816 #endif
817         {
818         /*
819          *      Copy the IP header
820          */
821
822         skb_put(skb, sizeof(struct ipv6hdr));
823         skb_reset_network_header(skb);
824         skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
825
826         /*
827          *      Add our header
828          */
829         skb_put(skb, sizeof(*msg));
830         skb_reset_transport_header(skb);
831         msg = (struct mrt6msg *)skb_transport_header(skb);
832
833         msg->im6_mbz = 0;
834         msg->im6_msgtype = assert;
835         msg->im6_mif = mifi;
836         msg->im6_pad = 0;
837         ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
838         ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
839
840         skb_dst_set(skb, dst_clone(skb_dst(pkt)));
841         skb->ip_summed = CHECKSUM_UNNECESSARY;
842         }
843
844         if (net->ipv6.mroute6_sk == NULL) {
845                 kfree_skb(skb);
846                 return -EINVAL;
847         }
848
849         /*
850          *      Deliver to user space multicast routing algorithms
851          */
852         ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb);
853         if (ret < 0) {
854                 if (net_ratelimit())
855                         printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
856                 kfree_skb(skb);
857         }
858
859         return ret;
860 }
861
862 /*
863  *      Queue a packet for resolution. It gets locked cache entry!
864  */
865
866 static int
867 ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb)
868 {
869         bool found = false;
870         int err;
871         struct mfc6_cache *c;
872
873         spin_lock_bh(&mfc_unres_lock);
874         list_for_each_entry(c, &net->ipv6.mfc6_unres_queue, list) {
875                 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
876                     ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) {
877                         found = true;
878                         break;
879                 }
880         }
881
882         if (!found) {
883                 /*
884                  *      Create a new entry if allowable
885                  */
886
887                 if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 ||
888                     (c = ip6mr_cache_alloc_unres()) == NULL) {
889                         spin_unlock_bh(&mfc_unres_lock);
890
891                         kfree_skb(skb);
892                         return -ENOBUFS;
893                 }
894
895                 /*
896                  *      Fill in the new cache entry
897                  */
898                 c->mf6c_parent = -1;
899                 c->mf6c_origin = ipv6_hdr(skb)->saddr;
900                 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
901
902                 /*
903                  *      Reflect first query at pim6sd
904                  */
905                 err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE);
906                 if (err < 0) {
907                         /* If the report failed throw the cache entry
908                            out - Brad Parker
909                          */
910                         spin_unlock_bh(&mfc_unres_lock);
911
912                         ip6mr_cache_free(c);
913                         kfree_skb(skb);
914                         return err;
915                 }
916
917                 atomic_inc(&net->ipv6.cache_resolve_queue_len);
918                 list_add(&c->list, &net->ipv6.mfc6_unres_queue);
919
920                 ipmr_do_expire_process(net);
921         }
922
923         /*
924          *      See if we can append the packet
925          */
926         if (c->mfc_un.unres.unresolved.qlen > 3) {
927                 kfree_skb(skb);
928                 err = -ENOBUFS;
929         } else {
930                 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
931                 err = 0;
932         }
933
934         spin_unlock_bh(&mfc_unres_lock);
935         return err;
936 }
937
938 /*
939  *      MFC6 cache manipulation by user space
940  */
941
942 static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc)
943 {
944         int line;
945         struct mfc6_cache *c, *next;
946
947         line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
948
949         list_for_each_entry_safe(c, next, &net->ipv6.mfc6_cache_array[line], list) {
950                 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
951                     ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
952                         write_lock_bh(&mrt_lock);
953                         list_del(&c->list);
954                         write_unlock_bh(&mrt_lock);
955
956                         ip6mr_cache_free(c);
957                         return 0;
958                 }
959         }
960         return -ENOENT;
961 }
962
963 static int ip6mr_device_event(struct notifier_block *this,
964                               unsigned long event, void *ptr)
965 {
966         struct net_device *dev = ptr;
967         struct net *net = dev_net(dev);
968         struct mif_device *v;
969         int ct;
970         LIST_HEAD(list);
971
972         if (event != NETDEV_UNREGISTER)
973                 return NOTIFY_DONE;
974
975         v = &net->ipv6.vif6_table[0];
976         for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) {
977                 if (v->dev == dev)
978                         mif6_delete(net, ct, &list);
979         }
980         unregister_netdevice_many(&list);
981
982         return NOTIFY_DONE;
983 }
984
985 static struct notifier_block ip6_mr_notifier = {
986         .notifier_call = ip6mr_device_event
987 };
988
989 /*
990  *      Setup for IP multicast routing
991  */
992
993 static int __net_init ip6mr_net_init(struct net *net)
994 {
995         unsigned int i;
996         int err = 0;
997
998         net->ipv6.vif6_table = kcalloc(MAXMIFS, sizeof(struct mif_device),
999                                        GFP_KERNEL);
1000         if (!net->ipv6.vif6_table) {
1001                 err = -ENOMEM;
1002                 goto fail;
1003         }
1004
1005         /* Forwarding cache */
1006         net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES,
1007                                              sizeof(struct list_head),
1008                                              GFP_KERNEL);
1009         if (!net->ipv6.mfc6_cache_array) {
1010                 err = -ENOMEM;
1011                 goto fail_mfc6_cache;
1012         }
1013
1014         for (i = 0; i < MFC6_LINES; i++)
1015                 INIT_LIST_HEAD(&net->ipv6.mfc6_cache_array[i]);
1016
1017         INIT_LIST_HEAD(&net->ipv6.mfc6_unres_queue);
1018
1019         setup_timer(&net->ipv6.ipmr_expire_timer, ipmr_expire_process,
1020                     (unsigned long)net);
1021
1022 #ifdef CONFIG_IPV6_PIMSM_V2
1023         net->ipv6.mroute_reg_vif_num = -1;
1024 #endif
1025
1026 #ifdef CONFIG_PROC_FS
1027         err = -ENOMEM;
1028         if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1029                 goto proc_vif_fail;
1030         if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1031                 goto proc_cache_fail;
1032 #endif
1033         return 0;
1034
1035 #ifdef CONFIG_PROC_FS
1036 proc_cache_fail:
1037         proc_net_remove(net, "ip6_mr_vif");
1038 proc_vif_fail:
1039         kfree(net->ipv6.mfc6_cache_array);
1040 #endif
1041 fail_mfc6_cache:
1042         kfree(net->ipv6.vif6_table);
1043 fail:
1044         return err;
1045 }
1046
1047 static void __net_exit ip6mr_net_exit(struct net *net)
1048 {
1049 #ifdef CONFIG_PROC_FS
1050         proc_net_remove(net, "ip6_mr_cache");
1051         proc_net_remove(net, "ip6_mr_vif");
1052 #endif
1053         del_timer(&net->ipv6.ipmr_expire_timer);
1054         mroute_clean_tables(net);
1055         kfree(net->ipv6.mfc6_cache_array);
1056         kfree(net->ipv6.vif6_table);
1057 }
1058
1059 static struct pernet_operations ip6mr_net_ops = {
1060         .init = ip6mr_net_init,
1061         .exit = ip6mr_net_exit,
1062 };
1063
1064 int __init ip6_mr_init(void)
1065 {
1066         int err;
1067
1068         mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1069                                        sizeof(struct mfc6_cache),
1070                                        0, SLAB_HWCACHE_ALIGN,
1071                                        NULL);
1072         if (!mrt_cachep)
1073                 return -ENOMEM;
1074
1075         err = register_pernet_subsys(&ip6mr_net_ops);
1076         if (err)
1077                 goto reg_pernet_fail;
1078
1079         err = register_netdevice_notifier(&ip6_mr_notifier);
1080         if (err)
1081                 goto reg_notif_fail;
1082 #ifdef CONFIG_IPV6_PIMSM_V2
1083         if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1084                 printk(KERN_ERR "ip6_mr_init: can't add PIM protocol\n");
1085                 err = -EAGAIN;
1086                 goto add_proto_fail;
1087         }
1088 #endif
1089         return 0;
1090 #ifdef CONFIG_IPV6_PIMSM_V2
1091 add_proto_fail:
1092         unregister_netdevice_notifier(&ip6_mr_notifier);
1093 #endif
1094 reg_notif_fail:
1095         unregister_pernet_subsys(&ip6mr_net_ops);
1096 reg_pernet_fail:
1097         kmem_cache_destroy(mrt_cachep);
1098         return err;
1099 }
1100
1101 void ip6_mr_cleanup(void)
1102 {
1103         unregister_netdevice_notifier(&ip6_mr_notifier);
1104         unregister_pernet_subsys(&ip6mr_net_ops);
1105         kmem_cache_destroy(mrt_cachep);
1106 }
1107
1108 static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock)
1109 {
1110         bool found = false;
1111         int line;
1112         struct mfc6_cache *uc, *c;
1113         unsigned char ttls[MAXMIFS];
1114         int i;
1115
1116         if (mfc->mf6cc_parent >= MAXMIFS)
1117                 return -ENFILE;
1118
1119         memset(ttls, 255, MAXMIFS);
1120         for (i = 0; i < MAXMIFS; i++) {
1121                 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1122                         ttls[i] = 1;
1123
1124         }
1125
1126         line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1127
1128         list_for_each_entry(c, &net->ipv6.mfc6_cache_array[line], list) {
1129                 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1130                     ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
1131                         found = true;
1132                         break;
1133                 }
1134         }
1135
1136         if (found) {
1137                 write_lock_bh(&mrt_lock);
1138                 c->mf6c_parent = mfc->mf6cc_parent;
1139                 ip6mr_update_thresholds(net, c, ttls);
1140                 if (!mrtsock)
1141                         c->mfc_flags |= MFC_STATIC;
1142                 write_unlock_bh(&mrt_lock);
1143                 return 0;
1144         }
1145
1146         if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1147                 return -EINVAL;
1148
1149         c = ip6mr_cache_alloc();
1150         if (c == NULL)
1151                 return -ENOMEM;
1152
1153         c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1154         c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1155         c->mf6c_parent = mfc->mf6cc_parent;
1156         ip6mr_update_thresholds(net, c, ttls);
1157         if (!mrtsock)
1158                 c->mfc_flags |= MFC_STATIC;
1159
1160         write_lock_bh(&mrt_lock);
1161         list_add(&c->list, &net->ipv6.mfc6_cache_array[line]);
1162         write_unlock_bh(&mrt_lock);
1163
1164         /*
1165          *      Check to see if we resolved a queued list. If so we
1166          *      need to send on the frames and tidy up.
1167          */
1168         found = false;
1169         spin_lock_bh(&mfc_unres_lock);
1170         list_for_each_entry(uc, &net->ipv6.mfc6_unres_queue, list) {
1171                 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
1172                     ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1173                         list_del(&uc->list);
1174                         atomic_dec(&net->ipv6.cache_resolve_queue_len);
1175                         found = true;
1176                         break;
1177                 }
1178         }
1179         if (list_empty(&net->ipv6.mfc6_unres_queue))
1180                 del_timer(&net->ipv6.ipmr_expire_timer);
1181         spin_unlock_bh(&mfc_unres_lock);
1182
1183         if (found) {
1184                 ip6mr_cache_resolve(net, uc, c);
1185                 ip6mr_cache_free(uc);
1186         }
1187         return 0;
1188 }
1189
1190 /*
1191  *      Close the multicast socket, and clear the vif tables etc
1192  */
1193
1194 static void mroute_clean_tables(struct net *net)
1195 {
1196         int i;
1197         LIST_HEAD(list);
1198         struct mfc6_cache *c, *next;
1199
1200         /*
1201          *      Shut down all active vif entries
1202          */
1203         for (i = 0; i < net->ipv6.maxvif; i++) {
1204                 if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC))
1205                         mif6_delete(net, i, &list);
1206         }
1207         unregister_netdevice_many(&list);
1208
1209         /*
1210          *      Wipe the cache
1211          */
1212         for (i = 0; i < MFC6_LINES; i++) {
1213                 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_cache_array[i], list) {
1214                         if (c->mfc_flags & MFC_STATIC)
1215                                 continue;
1216                         write_lock_bh(&mrt_lock);
1217                         list_del(&c->list);
1218                         write_unlock_bh(&mrt_lock);
1219
1220                         ip6mr_cache_free(c);
1221                 }
1222         }
1223
1224         if (atomic_read(&net->ipv6.cache_resolve_queue_len) != 0) {
1225                 spin_lock_bh(&mfc_unres_lock);
1226                 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_unres_queue, list) {
1227                         list_del(&c->list);
1228                         ip6mr_destroy_unres(net, c);
1229                 }
1230                 spin_unlock_bh(&mfc_unres_lock);
1231         }
1232 }
1233
1234 static int ip6mr_sk_init(struct sock *sk)
1235 {
1236         int err = 0;
1237         struct net *net = sock_net(sk);
1238
1239         rtnl_lock();
1240         write_lock_bh(&mrt_lock);
1241         if (likely(net->ipv6.mroute6_sk == NULL)) {
1242                 net->ipv6.mroute6_sk = sk;
1243                 net->ipv6.devconf_all->mc_forwarding++;
1244         }
1245         else
1246                 err = -EADDRINUSE;
1247         write_unlock_bh(&mrt_lock);
1248
1249         rtnl_unlock();
1250
1251         return err;
1252 }
1253
1254 int ip6mr_sk_done(struct sock *sk)
1255 {
1256         int err = 0;
1257         struct net *net = sock_net(sk);
1258
1259         rtnl_lock();
1260         if (sk == net->ipv6.mroute6_sk) {
1261                 write_lock_bh(&mrt_lock);
1262                 net->ipv6.mroute6_sk = NULL;
1263                 net->ipv6.devconf_all->mc_forwarding--;
1264                 write_unlock_bh(&mrt_lock);
1265
1266                 mroute_clean_tables(net);
1267         } else
1268                 err = -EACCES;
1269         rtnl_unlock();
1270
1271         return err;
1272 }
1273
1274 /*
1275  *      Socket options and virtual interface manipulation. The whole
1276  *      virtual interface system is a complete heap, but unfortunately
1277  *      that's how BSD mrouted happens to think. Maybe one day with a proper
1278  *      MOSPF/PIM router set up we can clean this up.
1279  */
1280
1281 int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1282 {
1283         int ret;
1284         struct mif6ctl vif;
1285         struct mf6cctl mfc;
1286         mifi_t mifi;
1287         struct net *net = sock_net(sk);
1288
1289         if (optname != MRT6_INIT) {
1290                 if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN))
1291                         return -EACCES;
1292         }
1293
1294         switch (optname) {
1295         case MRT6_INIT:
1296                 if (sk->sk_type != SOCK_RAW ||
1297                     inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1298                         return -EOPNOTSUPP;
1299                 if (optlen < sizeof(int))
1300                         return -EINVAL;
1301
1302                 return ip6mr_sk_init(sk);
1303
1304         case MRT6_DONE:
1305                 return ip6mr_sk_done(sk);
1306
1307         case MRT6_ADD_MIF:
1308                 if (optlen < sizeof(vif))
1309                         return -EINVAL;
1310                 if (copy_from_user(&vif, optval, sizeof(vif)))
1311                         return -EFAULT;
1312                 if (vif.mif6c_mifi >= MAXMIFS)
1313                         return -ENFILE;
1314                 rtnl_lock();
1315                 ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk);
1316                 rtnl_unlock();
1317                 return ret;
1318
1319         case MRT6_DEL_MIF:
1320                 if (optlen < sizeof(mifi_t))
1321                         return -EINVAL;
1322                 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1323                         return -EFAULT;
1324                 rtnl_lock();
1325                 ret = mif6_delete(net, mifi, NULL);
1326                 rtnl_unlock();
1327                 return ret;
1328
1329         /*
1330          *      Manipulate the forwarding caches. These live
1331          *      in a sort of kernel/user symbiosis.
1332          */
1333         case MRT6_ADD_MFC:
1334         case MRT6_DEL_MFC:
1335                 if (optlen < sizeof(mfc))
1336                         return -EINVAL;
1337                 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1338                         return -EFAULT;
1339                 rtnl_lock();
1340                 if (optname == MRT6_DEL_MFC)
1341                         ret = ip6mr_mfc_delete(net, &mfc);
1342                 else
1343                         ret = ip6mr_mfc_add(net, &mfc,
1344                                             sk == net->ipv6.mroute6_sk);
1345                 rtnl_unlock();
1346                 return ret;
1347
1348         /*
1349          *      Control PIM assert (to activate pim will activate assert)
1350          */
1351         case MRT6_ASSERT:
1352         {
1353                 int v;
1354                 if (get_user(v, (int __user *)optval))
1355                         return -EFAULT;
1356                 net->ipv6.mroute_do_assert = !!v;
1357                 return 0;
1358         }
1359
1360 #ifdef CONFIG_IPV6_PIMSM_V2
1361         case MRT6_PIM:
1362         {
1363                 int v;
1364                 if (get_user(v, (int __user *)optval))
1365                         return -EFAULT;
1366                 v = !!v;
1367                 rtnl_lock();
1368                 ret = 0;
1369                 if (v != net->ipv6.mroute_do_pim) {
1370                         net->ipv6.mroute_do_pim = v;
1371                         net->ipv6.mroute_do_assert = v;
1372                 }
1373                 rtnl_unlock();
1374                 return ret;
1375         }
1376
1377 #endif
1378         /*
1379          *      Spurious command, or MRT6_VERSION which you cannot
1380          *      set.
1381          */
1382         default:
1383                 return -ENOPROTOOPT;
1384         }
1385 }
1386
1387 /*
1388  *      Getsock opt support for the multicast routing system.
1389  */
1390
1391 int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1392                           int __user *optlen)
1393 {
1394         int olr;
1395         int val;
1396         struct net *net = sock_net(sk);
1397
1398         switch (optname) {
1399         case MRT6_VERSION:
1400                 val = 0x0305;
1401                 break;
1402 #ifdef CONFIG_IPV6_PIMSM_V2
1403         case MRT6_PIM:
1404                 val = net->ipv6.mroute_do_pim;
1405                 break;
1406 #endif
1407         case MRT6_ASSERT:
1408                 val = net->ipv6.mroute_do_assert;
1409                 break;
1410         default:
1411                 return -ENOPROTOOPT;
1412         }
1413
1414         if (get_user(olr, optlen))
1415                 return -EFAULT;
1416
1417         olr = min_t(int, olr, sizeof(int));
1418         if (olr < 0)
1419                 return -EINVAL;
1420
1421         if (put_user(olr, optlen))
1422                 return -EFAULT;
1423         if (copy_to_user(optval, &val, olr))
1424                 return -EFAULT;
1425         return 0;
1426 }
1427
1428 /*
1429  *      The IP multicast ioctl support routines.
1430  */
1431
1432 int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1433 {
1434         struct sioc_sg_req6 sr;
1435         struct sioc_mif_req6 vr;
1436         struct mif_device *vif;
1437         struct mfc6_cache *c;
1438         struct net *net = sock_net(sk);
1439
1440         switch (cmd) {
1441         case SIOCGETMIFCNT_IN6:
1442                 if (copy_from_user(&vr, arg, sizeof(vr)))
1443                         return -EFAULT;
1444                 if (vr.mifi >= net->ipv6.maxvif)
1445                         return -EINVAL;
1446                 read_lock(&mrt_lock);
1447                 vif = &net->ipv6.vif6_table[vr.mifi];
1448                 if (MIF_EXISTS(net, vr.mifi)) {
1449                         vr.icount = vif->pkt_in;
1450                         vr.ocount = vif->pkt_out;
1451                         vr.ibytes = vif->bytes_in;
1452                         vr.obytes = vif->bytes_out;
1453                         read_unlock(&mrt_lock);
1454
1455                         if (copy_to_user(arg, &vr, sizeof(vr)))
1456                                 return -EFAULT;
1457                         return 0;
1458                 }
1459                 read_unlock(&mrt_lock);
1460                 return -EADDRNOTAVAIL;
1461         case SIOCGETSGCNT_IN6:
1462                 if (copy_from_user(&sr, arg, sizeof(sr)))
1463                         return -EFAULT;
1464
1465                 read_lock(&mrt_lock);
1466                 c = ip6mr_cache_find(net, &sr.src.sin6_addr, &sr.grp.sin6_addr);
1467                 if (c) {
1468                         sr.pktcnt = c->mfc_un.res.pkt;
1469                         sr.bytecnt = c->mfc_un.res.bytes;
1470                         sr.wrong_if = c->mfc_un.res.wrong_if;
1471                         read_unlock(&mrt_lock);
1472
1473                         if (copy_to_user(arg, &sr, sizeof(sr)))
1474                                 return -EFAULT;
1475                         return 0;
1476                 }
1477                 read_unlock(&mrt_lock);
1478                 return -EADDRNOTAVAIL;
1479         default:
1480                 return -ENOIOCTLCMD;
1481         }
1482 }
1483
1484
1485 static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1486 {
1487         IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
1488                          IPSTATS_MIB_OUTFORWDATAGRAMS);
1489         return dst_output(skb);
1490 }
1491
1492 /*
1493  *      Processing handlers for ip6mr_forward
1494  */
1495
1496 static int ip6mr_forward2(struct net *net, struct sk_buff *skb,
1497                           struct mfc6_cache *c, int vifi)
1498 {
1499         struct ipv6hdr *ipv6h;
1500         struct mif_device *vif = &net->ipv6.vif6_table[vifi];
1501         struct net_device *dev;
1502         struct dst_entry *dst;
1503         struct flowi fl;
1504
1505         if (vif->dev == NULL)
1506                 goto out_free;
1507
1508 #ifdef CONFIG_IPV6_PIMSM_V2
1509         if (vif->flags & MIFF_REGISTER) {
1510                 vif->pkt_out++;
1511                 vif->bytes_out += skb->len;
1512                 vif->dev->stats.tx_bytes += skb->len;
1513                 vif->dev->stats.tx_packets++;
1514                 ip6mr_cache_report(net, skb, vifi, MRT6MSG_WHOLEPKT);
1515                 goto out_free;
1516         }
1517 #endif
1518
1519         ipv6h = ipv6_hdr(skb);
1520
1521         fl = (struct flowi) {
1522                 .oif = vif->link,
1523                 .nl_u = { .ip6_u =
1524                                 { .daddr = ipv6h->daddr, }
1525                 }
1526         };
1527
1528         dst = ip6_route_output(net, NULL, &fl);
1529         if (!dst)
1530                 goto out_free;
1531
1532         skb_dst_drop(skb);
1533         skb_dst_set(skb, dst);
1534
1535         /*
1536          * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1537          * not only before forwarding, but after forwarding on all output
1538          * interfaces. It is clear, if mrouter runs a multicasting
1539          * program, it should receive packets not depending to what interface
1540          * program is joined.
1541          * If we will not make it, the program will have to join on all
1542          * interfaces. On the other hand, multihoming host (or router, but
1543          * not mrouter) cannot join to more than one interface - it will
1544          * result in receiving multiple packets.
1545          */
1546         dev = vif->dev;
1547         skb->dev = dev;
1548         vif->pkt_out++;
1549         vif->bytes_out += skb->len;
1550
1551         /* We are about to write */
1552         /* XXX: extension headers? */
1553         if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1554                 goto out_free;
1555
1556         ipv6h = ipv6_hdr(skb);
1557         ipv6h->hop_limit--;
1558
1559         IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1560
1561         return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
1562                        ip6mr_forward2_finish);
1563
1564 out_free:
1565         kfree_skb(skb);
1566         return 0;
1567 }
1568
1569 static int ip6mr_find_vif(struct net_device *dev)
1570 {
1571         struct net *net = dev_net(dev);
1572         int ct;
1573         for (ct = net->ipv6.maxvif - 1; ct >= 0; ct--) {
1574                 if (net->ipv6.vif6_table[ct].dev == dev)
1575                         break;
1576         }
1577         return ct;
1578 }
1579
1580 static int ip6_mr_forward(struct net *net, struct sk_buff *skb,
1581                           struct mfc6_cache *cache)
1582 {
1583         int psend = -1;
1584         int vif, ct;
1585
1586         vif = cache->mf6c_parent;
1587         cache->mfc_un.res.pkt++;
1588         cache->mfc_un.res.bytes += skb->len;
1589
1590         /*
1591          * Wrong interface: drop packet and (maybe) send PIM assert.
1592          */
1593         if (net->ipv6.vif6_table[vif].dev != skb->dev) {
1594                 int true_vifi;
1595
1596                 cache->mfc_un.res.wrong_if++;
1597                 true_vifi = ip6mr_find_vif(skb->dev);
1598
1599                 if (true_vifi >= 0 && net->ipv6.mroute_do_assert &&
1600                     /* pimsm uses asserts, when switching from RPT to SPT,
1601                        so that we cannot check that packet arrived on an oif.
1602                        It is bad, but otherwise we would need to move pretty
1603                        large chunk of pimd to kernel. Ough... --ANK
1604                      */
1605                     (net->ipv6.mroute_do_pim ||
1606                      cache->mfc_un.res.ttls[true_vifi] < 255) &&
1607                     time_after(jiffies,
1608                                cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1609                         cache->mfc_un.res.last_assert = jiffies;
1610                         ip6mr_cache_report(net, skb, true_vifi, MRT6MSG_WRONGMIF);
1611                 }
1612                 goto dont_forward;
1613         }
1614
1615         net->ipv6.vif6_table[vif].pkt_in++;
1616         net->ipv6.vif6_table[vif].bytes_in += skb->len;
1617
1618         /*
1619          *      Forward the frame
1620          */
1621         for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1622                 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1623                         if (psend != -1) {
1624                                 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1625                                 if (skb2)
1626                                         ip6mr_forward2(net, skb2, cache, psend);
1627                         }
1628                         psend = ct;
1629                 }
1630         }
1631         if (psend != -1) {
1632                 ip6mr_forward2(net, skb, cache, psend);
1633                 return 0;
1634         }
1635
1636 dont_forward:
1637         kfree_skb(skb);
1638         return 0;
1639 }
1640
1641
1642 /*
1643  *      Multicast packets for forwarding arrive here
1644  */
1645
1646 int ip6_mr_input(struct sk_buff *skb)
1647 {
1648         struct mfc6_cache *cache;
1649         struct net *net = dev_net(skb->dev);
1650
1651         read_lock(&mrt_lock);
1652         cache = ip6mr_cache_find(net,
1653                                  &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
1654
1655         /*
1656          *      No usable cache entry
1657          */
1658         if (cache == NULL) {
1659                 int vif;
1660
1661                 vif = ip6mr_find_vif(skb->dev);
1662                 if (vif >= 0) {
1663                         int err = ip6mr_cache_unresolved(net, vif, skb);
1664                         read_unlock(&mrt_lock);
1665
1666                         return err;
1667                 }
1668                 read_unlock(&mrt_lock);
1669                 kfree_skb(skb);
1670                 return -ENODEV;
1671         }
1672
1673         ip6_mr_forward(net, skb, cache);
1674
1675         read_unlock(&mrt_lock);
1676
1677         return 0;
1678 }
1679
1680
1681 static int
1682 ip6mr_fill_mroute(struct net *net, struct sk_buff *skb, struct mfc6_cache *c,
1683                   struct rtmsg *rtm)
1684 {
1685         int ct;
1686         struct rtnexthop *nhp;
1687         u8 *b = skb_tail_pointer(skb);
1688         struct rtattr *mp_head;
1689
1690         /* If cache is unresolved, don't try to parse IIF and OIF */
1691         if (c->mf6c_parent > MAXMIFS)
1692                 return -ENOENT;
1693
1694         if (MIF_EXISTS(net, c->mf6c_parent))
1695                 RTA_PUT(skb, RTA_IIF, 4, &net->ipv6.vif6_table[c->mf6c_parent].dev->ifindex);
1696
1697         mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1698
1699         for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1700                 if (MIF_EXISTS(net, ct) && c->mfc_un.res.ttls[ct] < 255) {
1701                         if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1702                                 goto rtattr_failure;
1703                         nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1704                         nhp->rtnh_flags = 0;
1705                         nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1706                         nhp->rtnh_ifindex = net->ipv6.vif6_table[ct].dev->ifindex;
1707                         nhp->rtnh_len = sizeof(*nhp);
1708                 }
1709         }
1710         mp_head->rta_type = RTA_MULTIPATH;
1711         mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
1712         rtm->rtm_type = RTN_MULTICAST;
1713         return 1;
1714
1715 rtattr_failure:
1716         nlmsg_trim(skb, b);
1717         return -EMSGSIZE;
1718 }
1719
1720 int ip6mr_get_route(struct net *net,
1721                     struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1722 {
1723         int err;
1724         struct mfc6_cache *cache;
1725         struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
1726
1727         read_lock(&mrt_lock);
1728         cache = ip6mr_cache_find(net, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
1729
1730         if (!cache) {
1731                 struct sk_buff *skb2;
1732                 struct ipv6hdr *iph;
1733                 struct net_device *dev;
1734                 int vif;
1735
1736                 if (nowait) {
1737                         read_unlock(&mrt_lock);
1738                         return -EAGAIN;
1739                 }
1740
1741                 dev = skb->dev;
1742                 if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
1743                         read_unlock(&mrt_lock);
1744                         return -ENODEV;
1745                 }
1746
1747                 /* really correct? */
1748                 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
1749                 if (!skb2) {
1750                         read_unlock(&mrt_lock);
1751                         return -ENOMEM;
1752                 }
1753
1754                 skb_reset_transport_header(skb2);
1755
1756                 skb_put(skb2, sizeof(struct ipv6hdr));
1757                 skb_reset_network_header(skb2);
1758
1759                 iph = ipv6_hdr(skb2);
1760                 iph->version = 0;
1761                 iph->priority = 0;
1762                 iph->flow_lbl[0] = 0;
1763                 iph->flow_lbl[1] = 0;
1764                 iph->flow_lbl[2] = 0;
1765                 iph->payload_len = 0;
1766                 iph->nexthdr = IPPROTO_NONE;
1767                 iph->hop_limit = 0;
1768                 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
1769                 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
1770
1771                 err = ip6mr_cache_unresolved(net, vif, skb2);
1772                 read_unlock(&mrt_lock);
1773
1774                 return err;
1775         }
1776
1777         if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1778                 cache->mfc_flags |= MFC_NOTIFY;
1779
1780         err = ip6mr_fill_mroute(net, skb, cache, rtm);
1781         read_unlock(&mrt_lock);
1782         return err;
1783 }
1784