ipmr/ip6mr: advertise mfc stats via rtnetlink
[deliverable/linux.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/uaccess.h>
20 #include <linux/types.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/timer.h>
24 #include <linux/mm.h>
25 #include <linux/kernel.h>
26 #include <linux/fcntl.h>
27 #include <linux/stat.h>
28 #include <linux/socket.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/compat.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 #include <net/fib_rules.h>
46
47 #include <net/ipv6.h>
48 #include <net/ip6_route.h>
49 #include <linux/mroute6.h>
50 #include <linux/pim.h>
51 #include <net/addrconf.h>
52 #include <linux/netfilter_ipv6.h>
53 #include <linux/export.h>
54 #include <net/ip6_checksum.h>
55 #include <linux/netconf.h>
56
57 struct mr6_table {
58 struct list_head list;
59 #ifdef CONFIG_NET_NS
60 struct net *net;
61 #endif
62 u32 id;
63 struct sock *mroute6_sk;
64 struct timer_list ipmr_expire_timer;
65 struct list_head mfc6_unres_queue;
66 struct list_head mfc6_cache_array[MFC6_LINES];
67 struct mif_device vif6_table[MAXMIFS];
68 int maxvif;
69 atomic_t cache_resolve_queue_len;
70 bool mroute_do_assert;
71 bool mroute_do_pim;
72 #ifdef CONFIG_IPV6_PIMSM_V2
73 int mroute_reg_vif_num;
74 #endif
75 };
76
77 struct ip6mr_rule {
78 struct fib_rule common;
79 };
80
81 struct ip6mr_result {
82 struct mr6_table *mrt;
83 };
84
85 /* Big lock, protecting vif table, mrt cache and mroute socket state.
86 Note that the changes are semaphored via rtnl_lock.
87 */
88
89 static DEFINE_RWLOCK(mrt_lock);
90
91 /*
92 * Multicast router control variables
93 */
94
95 #define MIF_EXISTS(_mrt, _idx) ((_mrt)->vif6_table[_idx].dev != NULL)
96
97 /* Special spinlock for queue of unresolved entries */
98 static DEFINE_SPINLOCK(mfc_unres_lock);
99
100 /* We return to original Alan's scheme. Hash table of resolved
101 entries is changed only in process context and protected
102 with weak lock mrt_lock. Queue of unresolved entries is protected
103 with strong spinlock mfc_unres_lock.
104
105 In this case data path is free of exclusive locks at all.
106 */
107
108 static struct kmem_cache *mrt_cachep __read_mostly;
109
110 static struct mr6_table *ip6mr_new_table(struct net *net, u32 id);
111 static void ip6mr_free_table(struct mr6_table *mrt);
112
113 static int ip6_mr_forward(struct net *net, struct mr6_table *mrt,
114 struct sk_buff *skb, struct mfc6_cache *cache);
115 static int ip6mr_cache_report(struct mr6_table *mrt, struct sk_buff *pkt,
116 mifi_t mifi, int assert);
117 static int __ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
118 struct mfc6_cache *c, struct rtmsg *rtm);
119 static int ip6mr_rtm_dumproute(struct sk_buff *skb,
120 struct netlink_callback *cb);
121 static void mroute_clean_tables(struct mr6_table *mrt);
122 static void ipmr_expire_process(unsigned long arg);
123
124 #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
125 #define ip6mr_for_each_table(mrt, net) \
126 list_for_each_entry_rcu(mrt, &net->ipv6.mr6_tables, list)
127
128 static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
129 {
130 struct mr6_table *mrt;
131
132 ip6mr_for_each_table(mrt, net) {
133 if (mrt->id == id)
134 return mrt;
135 }
136 return NULL;
137 }
138
139 static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
140 struct mr6_table **mrt)
141 {
142 struct ip6mr_result res;
143 struct fib_lookup_arg arg = { .result = &res, };
144 int err;
145
146 err = fib_rules_lookup(net->ipv6.mr6_rules_ops,
147 flowi6_to_flowi(flp6), 0, &arg);
148 if (err < 0)
149 return err;
150 *mrt = res.mrt;
151 return 0;
152 }
153
154 static int ip6mr_rule_action(struct fib_rule *rule, struct flowi *flp,
155 int flags, struct fib_lookup_arg *arg)
156 {
157 struct ip6mr_result *res = arg->result;
158 struct mr6_table *mrt;
159
160 switch (rule->action) {
161 case FR_ACT_TO_TBL:
162 break;
163 case FR_ACT_UNREACHABLE:
164 return -ENETUNREACH;
165 case FR_ACT_PROHIBIT:
166 return -EACCES;
167 case FR_ACT_BLACKHOLE:
168 default:
169 return -EINVAL;
170 }
171
172 mrt = ip6mr_get_table(rule->fr_net, rule->table);
173 if (mrt == NULL)
174 return -EAGAIN;
175 res->mrt = mrt;
176 return 0;
177 }
178
179 static int ip6mr_rule_match(struct fib_rule *rule, struct flowi *flp, int flags)
180 {
181 return 1;
182 }
183
184 static const struct nla_policy ip6mr_rule_policy[FRA_MAX + 1] = {
185 FRA_GENERIC_POLICY,
186 };
187
188 static int ip6mr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
189 struct fib_rule_hdr *frh, struct nlattr **tb)
190 {
191 return 0;
192 }
193
194 static int ip6mr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
195 struct nlattr **tb)
196 {
197 return 1;
198 }
199
200 static int ip6mr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
201 struct fib_rule_hdr *frh)
202 {
203 frh->dst_len = 0;
204 frh->src_len = 0;
205 frh->tos = 0;
206 return 0;
207 }
208
209 static const struct fib_rules_ops __net_initconst ip6mr_rules_ops_template = {
210 .family = RTNL_FAMILY_IP6MR,
211 .rule_size = sizeof(struct ip6mr_rule),
212 .addr_size = sizeof(struct in6_addr),
213 .action = ip6mr_rule_action,
214 .match = ip6mr_rule_match,
215 .configure = ip6mr_rule_configure,
216 .compare = ip6mr_rule_compare,
217 .default_pref = fib_default_rule_pref,
218 .fill = ip6mr_rule_fill,
219 .nlgroup = RTNLGRP_IPV6_RULE,
220 .policy = ip6mr_rule_policy,
221 .owner = THIS_MODULE,
222 };
223
224 static int __net_init ip6mr_rules_init(struct net *net)
225 {
226 struct fib_rules_ops *ops;
227 struct mr6_table *mrt;
228 int err;
229
230 ops = fib_rules_register(&ip6mr_rules_ops_template, net);
231 if (IS_ERR(ops))
232 return PTR_ERR(ops);
233
234 INIT_LIST_HEAD(&net->ipv6.mr6_tables);
235
236 mrt = ip6mr_new_table(net, RT6_TABLE_DFLT);
237 if (mrt == NULL) {
238 err = -ENOMEM;
239 goto err1;
240 }
241
242 err = fib_default_rule_add(ops, 0x7fff, RT6_TABLE_DFLT, 0);
243 if (err < 0)
244 goto err2;
245
246 net->ipv6.mr6_rules_ops = ops;
247 return 0;
248
249 err2:
250 kfree(mrt);
251 err1:
252 fib_rules_unregister(ops);
253 return err;
254 }
255
256 static void __net_exit ip6mr_rules_exit(struct net *net)
257 {
258 struct mr6_table *mrt, *next;
259
260 list_for_each_entry_safe(mrt, next, &net->ipv6.mr6_tables, list) {
261 list_del(&mrt->list);
262 ip6mr_free_table(mrt);
263 }
264 fib_rules_unregister(net->ipv6.mr6_rules_ops);
265 }
266 #else
267 #define ip6mr_for_each_table(mrt, net) \
268 for (mrt = net->ipv6.mrt6; mrt; mrt = NULL)
269
270 static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
271 {
272 return net->ipv6.mrt6;
273 }
274
275 static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
276 struct mr6_table **mrt)
277 {
278 *mrt = net->ipv6.mrt6;
279 return 0;
280 }
281
282 static int __net_init ip6mr_rules_init(struct net *net)
283 {
284 net->ipv6.mrt6 = ip6mr_new_table(net, RT6_TABLE_DFLT);
285 return net->ipv6.mrt6 ? 0 : -ENOMEM;
286 }
287
288 static void __net_exit ip6mr_rules_exit(struct net *net)
289 {
290 ip6mr_free_table(net->ipv6.mrt6);
291 }
292 #endif
293
294 static struct mr6_table *ip6mr_new_table(struct net *net, u32 id)
295 {
296 struct mr6_table *mrt;
297 unsigned int i;
298
299 mrt = ip6mr_get_table(net, id);
300 if (mrt != NULL)
301 return mrt;
302
303 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
304 if (mrt == NULL)
305 return NULL;
306 mrt->id = id;
307 write_pnet(&mrt->net, net);
308
309 /* Forwarding cache */
310 for (i = 0; i < MFC6_LINES; i++)
311 INIT_LIST_HEAD(&mrt->mfc6_cache_array[i]);
312
313 INIT_LIST_HEAD(&mrt->mfc6_unres_queue);
314
315 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
316 (unsigned long)mrt);
317
318 #ifdef CONFIG_IPV6_PIMSM_V2
319 mrt->mroute_reg_vif_num = -1;
320 #endif
321 #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
322 list_add_tail_rcu(&mrt->list, &net->ipv6.mr6_tables);
323 #endif
324 return mrt;
325 }
326
327 static void ip6mr_free_table(struct mr6_table *mrt)
328 {
329 del_timer(&mrt->ipmr_expire_timer);
330 mroute_clean_tables(mrt);
331 kfree(mrt);
332 }
333
334 #ifdef CONFIG_PROC_FS
335
336 struct ipmr_mfc_iter {
337 struct seq_net_private p;
338 struct mr6_table *mrt;
339 struct list_head *cache;
340 int ct;
341 };
342
343
344 static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
345 struct ipmr_mfc_iter *it, loff_t pos)
346 {
347 struct mr6_table *mrt = it->mrt;
348 struct mfc6_cache *mfc;
349
350 read_lock(&mrt_lock);
351 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++) {
352 it->cache = &mrt->mfc6_cache_array[it->ct];
353 list_for_each_entry(mfc, it->cache, list)
354 if (pos-- == 0)
355 return mfc;
356 }
357 read_unlock(&mrt_lock);
358
359 spin_lock_bh(&mfc_unres_lock);
360 it->cache = &mrt->mfc6_unres_queue;
361 list_for_each_entry(mfc, it->cache, list)
362 if (pos-- == 0)
363 return mfc;
364 spin_unlock_bh(&mfc_unres_lock);
365
366 it->cache = NULL;
367 return NULL;
368 }
369
370 /*
371 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
372 */
373
374 struct ipmr_vif_iter {
375 struct seq_net_private p;
376 struct mr6_table *mrt;
377 int ct;
378 };
379
380 static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
381 struct ipmr_vif_iter *iter,
382 loff_t pos)
383 {
384 struct mr6_table *mrt = iter->mrt;
385
386 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
387 if (!MIF_EXISTS(mrt, iter->ct))
388 continue;
389 if (pos-- == 0)
390 return &mrt->vif6_table[iter->ct];
391 }
392 return NULL;
393 }
394
395 static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
396 __acquires(mrt_lock)
397 {
398 struct ipmr_vif_iter *iter = seq->private;
399 struct net *net = seq_file_net(seq);
400 struct mr6_table *mrt;
401
402 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
403 if (mrt == NULL)
404 return ERR_PTR(-ENOENT);
405
406 iter->mrt = mrt;
407
408 read_lock(&mrt_lock);
409 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
410 : SEQ_START_TOKEN;
411 }
412
413 static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
414 {
415 struct ipmr_vif_iter *iter = seq->private;
416 struct net *net = seq_file_net(seq);
417 struct mr6_table *mrt = iter->mrt;
418
419 ++*pos;
420 if (v == SEQ_START_TOKEN)
421 return ip6mr_vif_seq_idx(net, iter, 0);
422
423 while (++iter->ct < mrt->maxvif) {
424 if (!MIF_EXISTS(mrt, iter->ct))
425 continue;
426 return &mrt->vif6_table[iter->ct];
427 }
428 return NULL;
429 }
430
431 static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
432 __releases(mrt_lock)
433 {
434 read_unlock(&mrt_lock);
435 }
436
437 static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
438 {
439 struct ipmr_vif_iter *iter = seq->private;
440 struct mr6_table *mrt = iter->mrt;
441
442 if (v == SEQ_START_TOKEN) {
443 seq_puts(seq,
444 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
445 } else {
446 const struct mif_device *vif = v;
447 const char *name = vif->dev ? vif->dev->name : "none";
448
449 seq_printf(seq,
450 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
451 vif - mrt->vif6_table,
452 name, vif->bytes_in, vif->pkt_in,
453 vif->bytes_out, vif->pkt_out,
454 vif->flags);
455 }
456 return 0;
457 }
458
459 static const struct seq_operations ip6mr_vif_seq_ops = {
460 .start = ip6mr_vif_seq_start,
461 .next = ip6mr_vif_seq_next,
462 .stop = ip6mr_vif_seq_stop,
463 .show = ip6mr_vif_seq_show,
464 };
465
466 static int ip6mr_vif_open(struct inode *inode, struct file *file)
467 {
468 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
469 sizeof(struct ipmr_vif_iter));
470 }
471
472 static const struct file_operations ip6mr_vif_fops = {
473 .owner = THIS_MODULE,
474 .open = ip6mr_vif_open,
475 .read = seq_read,
476 .llseek = seq_lseek,
477 .release = seq_release_net,
478 };
479
480 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
481 {
482 struct ipmr_mfc_iter *it = seq->private;
483 struct net *net = seq_file_net(seq);
484 struct mr6_table *mrt;
485
486 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
487 if (mrt == NULL)
488 return ERR_PTR(-ENOENT);
489
490 it->mrt = mrt;
491 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
492 : SEQ_START_TOKEN;
493 }
494
495 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
496 {
497 struct mfc6_cache *mfc = v;
498 struct ipmr_mfc_iter *it = seq->private;
499 struct net *net = seq_file_net(seq);
500 struct mr6_table *mrt = it->mrt;
501
502 ++*pos;
503
504 if (v == SEQ_START_TOKEN)
505 return ipmr_mfc_seq_idx(net, seq->private, 0);
506
507 if (mfc->list.next != it->cache)
508 return list_entry(mfc->list.next, struct mfc6_cache, list);
509
510 if (it->cache == &mrt->mfc6_unres_queue)
511 goto end_of_list;
512
513 BUG_ON(it->cache != &mrt->mfc6_cache_array[it->ct]);
514
515 while (++it->ct < MFC6_LINES) {
516 it->cache = &mrt->mfc6_cache_array[it->ct];
517 if (list_empty(it->cache))
518 continue;
519 return list_first_entry(it->cache, struct mfc6_cache, list);
520 }
521
522 /* exhausted cache_array, show unresolved */
523 read_unlock(&mrt_lock);
524 it->cache = &mrt->mfc6_unres_queue;
525 it->ct = 0;
526
527 spin_lock_bh(&mfc_unres_lock);
528 if (!list_empty(it->cache))
529 return list_first_entry(it->cache, struct mfc6_cache, list);
530
531 end_of_list:
532 spin_unlock_bh(&mfc_unres_lock);
533 it->cache = NULL;
534
535 return NULL;
536 }
537
538 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
539 {
540 struct ipmr_mfc_iter *it = seq->private;
541 struct mr6_table *mrt = it->mrt;
542
543 if (it->cache == &mrt->mfc6_unres_queue)
544 spin_unlock_bh(&mfc_unres_lock);
545 else if (it->cache == mrt->mfc6_cache_array)
546 read_unlock(&mrt_lock);
547 }
548
549 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
550 {
551 int n;
552
553 if (v == SEQ_START_TOKEN) {
554 seq_puts(seq,
555 "Group "
556 "Origin "
557 "Iif Pkts Bytes Wrong Oifs\n");
558 } else {
559 const struct mfc6_cache *mfc = v;
560 const struct ipmr_mfc_iter *it = seq->private;
561 struct mr6_table *mrt = it->mrt;
562
563 seq_printf(seq, "%pI6 %pI6 %-3hd",
564 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
565 mfc->mf6c_parent);
566
567 if (it->cache != &mrt->mfc6_unres_queue) {
568 seq_printf(seq, " %8lu %8lu %8lu",
569 mfc->mfc_un.res.pkt,
570 mfc->mfc_un.res.bytes,
571 mfc->mfc_un.res.wrong_if);
572 for (n = mfc->mfc_un.res.minvif;
573 n < mfc->mfc_un.res.maxvif; n++) {
574 if (MIF_EXISTS(mrt, n) &&
575 mfc->mfc_un.res.ttls[n] < 255)
576 seq_printf(seq,
577 " %2d:%-3d",
578 n, mfc->mfc_un.res.ttls[n]);
579 }
580 } else {
581 /* unresolved mfc_caches don't contain
582 * pkt, bytes and wrong_if values
583 */
584 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
585 }
586 seq_putc(seq, '\n');
587 }
588 return 0;
589 }
590
591 static const struct seq_operations ipmr_mfc_seq_ops = {
592 .start = ipmr_mfc_seq_start,
593 .next = ipmr_mfc_seq_next,
594 .stop = ipmr_mfc_seq_stop,
595 .show = ipmr_mfc_seq_show,
596 };
597
598 static int ipmr_mfc_open(struct inode *inode, struct file *file)
599 {
600 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
601 sizeof(struct ipmr_mfc_iter));
602 }
603
604 static const struct file_operations ip6mr_mfc_fops = {
605 .owner = THIS_MODULE,
606 .open = ipmr_mfc_open,
607 .read = seq_read,
608 .llseek = seq_lseek,
609 .release = seq_release_net,
610 };
611 #endif
612
613 #ifdef CONFIG_IPV6_PIMSM_V2
614
615 static int pim6_rcv(struct sk_buff *skb)
616 {
617 struct pimreghdr *pim;
618 struct ipv6hdr *encap;
619 struct net_device *reg_dev = NULL;
620 struct net *net = dev_net(skb->dev);
621 struct mr6_table *mrt;
622 struct flowi6 fl6 = {
623 .flowi6_iif = skb->dev->ifindex,
624 .flowi6_mark = skb->mark,
625 };
626 int reg_vif_num;
627
628 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
629 goto drop;
630
631 pim = (struct pimreghdr *)skb_transport_header(skb);
632 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
633 (pim->flags & PIM_NULL_REGISTER) ||
634 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
635 sizeof(*pim), IPPROTO_PIM,
636 csum_partial((void *)pim, sizeof(*pim), 0)) &&
637 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
638 goto drop;
639
640 /* check if the inner packet is destined to mcast group */
641 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
642 sizeof(*pim));
643
644 if (!ipv6_addr_is_multicast(&encap->daddr) ||
645 encap->payload_len == 0 ||
646 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
647 goto drop;
648
649 if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0)
650 goto drop;
651 reg_vif_num = mrt->mroute_reg_vif_num;
652
653 read_lock(&mrt_lock);
654 if (reg_vif_num >= 0)
655 reg_dev = mrt->vif6_table[reg_vif_num].dev;
656 if (reg_dev)
657 dev_hold(reg_dev);
658 read_unlock(&mrt_lock);
659
660 if (reg_dev == NULL)
661 goto drop;
662
663 skb->mac_header = skb->network_header;
664 skb_pull(skb, (u8 *)encap - skb->data);
665 skb_reset_network_header(skb);
666 skb->protocol = htons(ETH_P_IPV6);
667 skb->ip_summed = CHECKSUM_NONE;
668 skb->pkt_type = PACKET_HOST;
669
670 skb_tunnel_rx(skb, reg_dev);
671
672 netif_rx(skb);
673
674 dev_put(reg_dev);
675 return 0;
676 drop:
677 kfree_skb(skb);
678 return 0;
679 }
680
681 static const struct inet6_protocol pim6_protocol = {
682 .handler = pim6_rcv,
683 };
684
685 /* Service routines creating virtual interfaces: PIMREG */
686
687 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
688 struct net_device *dev)
689 {
690 struct net *net = dev_net(dev);
691 struct mr6_table *mrt;
692 struct flowi6 fl6 = {
693 .flowi6_oif = dev->ifindex,
694 .flowi6_iif = skb->skb_iif,
695 .flowi6_mark = skb->mark,
696 };
697 int err;
698
699 err = ip6mr_fib_lookup(net, &fl6, &mrt);
700 if (err < 0) {
701 kfree_skb(skb);
702 return err;
703 }
704
705 read_lock(&mrt_lock);
706 dev->stats.tx_bytes += skb->len;
707 dev->stats.tx_packets++;
708 ip6mr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, MRT6MSG_WHOLEPKT);
709 read_unlock(&mrt_lock);
710 kfree_skb(skb);
711 return NETDEV_TX_OK;
712 }
713
714 static const struct net_device_ops reg_vif_netdev_ops = {
715 .ndo_start_xmit = reg_vif_xmit,
716 };
717
718 static void reg_vif_setup(struct net_device *dev)
719 {
720 dev->type = ARPHRD_PIMREG;
721 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
722 dev->flags = IFF_NOARP;
723 dev->netdev_ops = &reg_vif_netdev_ops;
724 dev->destructor = free_netdev;
725 dev->features |= NETIF_F_NETNS_LOCAL;
726 }
727
728 static struct net_device *ip6mr_reg_vif(struct net *net, struct mr6_table *mrt)
729 {
730 struct net_device *dev;
731 char name[IFNAMSIZ];
732
733 if (mrt->id == RT6_TABLE_DFLT)
734 sprintf(name, "pim6reg");
735 else
736 sprintf(name, "pim6reg%u", mrt->id);
737
738 dev = alloc_netdev(0, name, reg_vif_setup);
739 if (dev == NULL)
740 return NULL;
741
742 dev_net_set(dev, net);
743
744 if (register_netdevice(dev)) {
745 free_netdev(dev);
746 return NULL;
747 }
748 dev->iflink = 0;
749
750 if (dev_open(dev))
751 goto failure;
752
753 dev_hold(dev);
754 return dev;
755
756 failure:
757 /* allow the register to be completed before unregistering. */
758 rtnl_unlock();
759 rtnl_lock();
760
761 unregister_netdevice(dev);
762 return NULL;
763 }
764 #endif
765
766 /*
767 * Delete a VIF entry
768 */
769
770 static int mif6_delete(struct mr6_table *mrt, int vifi, struct list_head *head)
771 {
772 struct mif_device *v;
773 struct net_device *dev;
774 struct inet6_dev *in6_dev;
775
776 if (vifi < 0 || vifi >= mrt->maxvif)
777 return -EADDRNOTAVAIL;
778
779 v = &mrt->vif6_table[vifi];
780
781 write_lock_bh(&mrt_lock);
782 dev = v->dev;
783 v->dev = NULL;
784
785 if (!dev) {
786 write_unlock_bh(&mrt_lock);
787 return -EADDRNOTAVAIL;
788 }
789
790 #ifdef CONFIG_IPV6_PIMSM_V2
791 if (vifi == mrt->mroute_reg_vif_num)
792 mrt->mroute_reg_vif_num = -1;
793 #endif
794
795 if (vifi + 1 == mrt->maxvif) {
796 int tmp;
797 for (tmp = vifi - 1; tmp >= 0; tmp--) {
798 if (MIF_EXISTS(mrt, tmp))
799 break;
800 }
801 mrt->maxvif = tmp + 1;
802 }
803
804 write_unlock_bh(&mrt_lock);
805
806 dev_set_allmulti(dev, -1);
807
808 in6_dev = __in6_dev_get(dev);
809 if (in6_dev) {
810 in6_dev->cnf.mc_forwarding--;
811 inet6_netconf_notify_devconf(dev_net(dev),
812 NETCONFA_MC_FORWARDING,
813 dev->ifindex, &in6_dev->cnf);
814 }
815
816 if (v->flags & MIFF_REGISTER)
817 unregister_netdevice_queue(dev, head);
818
819 dev_put(dev);
820 return 0;
821 }
822
823 static inline void ip6mr_cache_free(struct mfc6_cache *c)
824 {
825 kmem_cache_free(mrt_cachep, c);
826 }
827
828 /* Destroy an unresolved cache entry, killing queued skbs
829 and reporting error to netlink readers.
830 */
831
832 static void ip6mr_destroy_unres(struct mr6_table *mrt, struct mfc6_cache *c)
833 {
834 struct net *net = read_pnet(&mrt->net);
835 struct sk_buff *skb;
836
837 atomic_dec(&mrt->cache_resolve_queue_len);
838
839 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
840 if (ipv6_hdr(skb)->version == 0) {
841 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
842 nlh->nlmsg_type = NLMSG_ERROR;
843 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
844 skb_trim(skb, nlh->nlmsg_len);
845 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
846 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
847 } else
848 kfree_skb(skb);
849 }
850
851 ip6mr_cache_free(c);
852 }
853
854
855 /* Timer process for all the unresolved queue. */
856
857 static void ipmr_do_expire_process(struct mr6_table *mrt)
858 {
859 unsigned long now = jiffies;
860 unsigned long expires = 10 * HZ;
861 struct mfc6_cache *c, *next;
862
863 list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
864 if (time_after(c->mfc_un.unres.expires, now)) {
865 /* not yet... */
866 unsigned long interval = c->mfc_un.unres.expires - now;
867 if (interval < expires)
868 expires = interval;
869 continue;
870 }
871
872 list_del(&c->list);
873 ip6mr_destroy_unres(mrt, c);
874 }
875
876 if (!list_empty(&mrt->mfc6_unres_queue))
877 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
878 }
879
880 static void ipmr_expire_process(unsigned long arg)
881 {
882 struct mr6_table *mrt = (struct mr6_table *)arg;
883
884 if (!spin_trylock(&mfc_unres_lock)) {
885 mod_timer(&mrt->ipmr_expire_timer, jiffies + 1);
886 return;
887 }
888
889 if (!list_empty(&mrt->mfc6_unres_queue))
890 ipmr_do_expire_process(mrt);
891
892 spin_unlock(&mfc_unres_lock);
893 }
894
895 /* Fill oifs list. It is called under write locked mrt_lock. */
896
897 static void ip6mr_update_thresholds(struct mr6_table *mrt, struct mfc6_cache *cache,
898 unsigned char *ttls)
899 {
900 int vifi;
901
902 cache->mfc_un.res.minvif = MAXMIFS;
903 cache->mfc_un.res.maxvif = 0;
904 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
905
906 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
907 if (MIF_EXISTS(mrt, vifi) &&
908 ttls[vifi] && ttls[vifi] < 255) {
909 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
910 if (cache->mfc_un.res.minvif > vifi)
911 cache->mfc_un.res.minvif = vifi;
912 if (cache->mfc_un.res.maxvif <= vifi)
913 cache->mfc_un.res.maxvif = vifi + 1;
914 }
915 }
916 }
917
918 static int mif6_add(struct net *net, struct mr6_table *mrt,
919 struct mif6ctl *vifc, int mrtsock)
920 {
921 int vifi = vifc->mif6c_mifi;
922 struct mif_device *v = &mrt->vif6_table[vifi];
923 struct net_device *dev;
924 struct inet6_dev *in6_dev;
925 int err;
926
927 /* Is vif busy ? */
928 if (MIF_EXISTS(mrt, vifi))
929 return -EADDRINUSE;
930
931 switch (vifc->mif6c_flags) {
932 #ifdef CONFIG_IPV6_PIMSM_V2
933 case MIFF_REGISTER:
934 /*
935 * Special Purpose VIF in PIM
936 * All the packets will be sent to the daemon
937 */
938 if (mrt->mroute_reg_vif_num >= 0)
939 return -EADDRINUSE;
940 dev = ip6mr_reg_vif(net, mrt);
941 if (!dev)
942 return -ENOBUFS;
943 err = dev_set_allmulti(dev, 1);
944 if (err) {
945 unregister_netdevice(dev);
946 dev_put(dev);
947 return err;
948 }
949 break;
950 #endif
951 case 0:
952 dev = dev_get_by_index(net, vifc->mif6c_pifi);
953 if (!dev)
954 return -EADDRNOTAVAIL;
955 err = dev_set_allmulti(dev, 1);
956 if (err) {
957 dev_put(dev);
958 return err;
959 }
960 break;
961 default:
962 return -EINVAL;
963 }
964
965 in6_dev = __in6_dev_get(dev);
966 if (in6_dev) {
967 in6_dev->cnf.mc_forwarding++;
968 inet6_netconf_notify_devconf(dev_net(dev),
969 NETCONFA_MC_FORWARDING,
970 dev->ifindex, &in6_dev->cnf);
971 }
972
973 /*
974 * Fill in the VIF structures
975 */
976 v->rate_limit = vifc->vifc_rate_limit;
977 v->flags = vifc->mif6c_flags;
978 if (!mrtsock)
979 v->flags |= VIFF_STATIC;
980 v->threshold = vifc->vifc_threshold;
981 v->bytes_in = 0;
982 v->bytes_out = 0;
983 v->pkt_in = 0;
984 v->pkt_out = 0;
985 v->link = dev->ifindex;
986 if (v->flags & MIFF_REGISTER)
987 v->link = dev->iflink;
988
989 /* And finish update writing critical data */
990 write_lock_bh(&mrt_lock);
991 v->dev = dev;
992 #ifdef CONFIG_IPV6_PIMSM_V2
993 if (v->flags & MIFF_REGISTER)
994 mrt->mroute_reg_vif_num = vifi;
995 #endif
996 if (vifi + 1 > mrt->maxvif)
997 mrt->maxvif = vifi + 1;
998 write_unlock_bh(&mrt_lock);
999 return 0;
1000 }
1001
1002 static struct mfc6_cache *ip6mr_cache_find(struct mr6_table *mrt,
1003 const struct in6_addr *origin,
1004 const struct in6_addr *mcastgrp)
1005 {
1006 int line = MFC6_HASH(mcastgrp, origin);
1007 struct mfc6_cache *c;
1008
1009 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
1010 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
1011 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
1012 return c;
1013 }
1014 return NULL;
1015 }
1016
1017 /*
1018 * Allocate a multicast cache entry
1019 */
1020 static struct mfc6_cache *ip6mr_cache_alloc(void)
1021 {
1022 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
1023 if (c == NULL)
1024 return NULL;
1025 c->mfc_un.res.minvif = MAXMIFS;
1026 return c;
1027 }
1028
1029 static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
1030 {
1031 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
1032 if (c == NULL)
1033 return NULL;
1034 skb_queue_head_init(&c->mfc_un.unres.unresolved);
1035 c->mfc_un.unres.expires = jiffies + 10 * HZ;
1036 return c;
1037 }
1038
1039 /*
1040 * A cache entry has gone into a resolved state from queued
1041 */
1042
1043 static void ip6mr_cache_resolve(struct net *net, struct mr6_table *mrt,
1044 struct mfc6_cache *uc, struct mfc6_cache *c)
1045 {
1046 struct sk_buff *skb;
1047
1048 /*
1049 * Play the pending entries through our router
1050 */
1051
1052 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
1053 if (ipv6_hdr(skb)->version == 0) {
1054 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
1055
1056 if (__ip6mr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
1057 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
1058 } else {
1059 nlh->nlmsg_type = NLMSG_ERROR;
1060 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
1061 skb_trim(skb, nlh->nlmsg_len);
1062 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
1063 }
1064 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1065 } else
1066 ip6_mr_forward(net, mrt, skb, c);
1067 }
1068 }
1069
1070 /*
1071 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
1072 * expects the following bizarre scheme.
1073 *
1074 * Called under mrt_lock.
1075 */
1076
1077 static int ip6mr_cache_report(struct mr6_table *mrt, struct sk_buff *pkt,
1078 mifi_t mifi, int assert)
1079 {
1080 struct sk_buff *skb;
1081 struct mrt6msg *msg;
1082 int ret;
1083
1084 #ifdef CONFIG_IPV6_PIMSM_V2
1085 if (assert == MRT6MSG_WHOLEPKT)
1086 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
1087 +sizeof(*msg));
1088 else
1089 #endif
1090 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
1091
1092 if (!skb)
1093 return -ENOBUFS;
1094
1095 /* I suppose that internal messages
1096 * do not require checksums */
1097
1098 skb->ip_summed = CHECKSUM_UNNECESSARY;
1099
1100 #ifdef CONFIG_IPV6_PIMSM_V2
1101 if (assert == MRT6MSG_WHOLEPKT) {
1102 /* Ugly, but we have no choice with this interface.
1103 Duplicate old header, fix length etc.
1104 And all this only to mangle msg->im6_msgtype and
1105 to set msg->im6_mbz to "mbz" :-)
1106 */
1107 skb_push(skb, -skb_network_offset(pkt));
1108
1109 skb_push(skb, sizeof(*msg));
1110 skb_reset_transport_header(skb);
1111 msg = (struct mrt6msg *)skb_transport_header(skb);
1112 msg->im6_mbz = 0;
1113 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
1114 msg->im6_mif = mrt->mroute_reg_vif_num;
1115 msg->im6_pad = 0;
1116 msg->im6_src = ipv6_hdr(pkt)->saddr;
1117 msg->im6_dst = ipv6_hdr(pkt)->daddr;
1118
1119 skb->ip_summed = CHECKSUM_UNNECESSARY;
1120 } else
1121 #endif
1122 {
1123 /*
1124 * Copy the IP header
1125 */
1126
1127 skb_put(skb, sizeof(struct ipv6hdr));
1128 skb_reset_network_header(skb);
1129 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
1130
1131 /*
1132 * Add our header
1133 */
1134 skb_put(skb, sizeof(*msg));
1135 skb_reset_transport_header(skb);
1136 msg = (struct mrt6msg *)skb_transport_header(skb);
1137
1138 msg->im6_mbz = 0;
1139 msg->im6_msgtype = assert;
1140 msg->im6_mif = mifi;
1141 msg->im6_pad = 0;
1142 msg->im6_src = ipv6_hdr(pkt)->saddr;
1143 msg->im6_dst = ipv6_hdr(pkt)->daddr;
1144
1145 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1146 skb->ip_summed = CHECKSUM_UNNECESSARY;
1147 }
1148
1149 if (mrt->mroute6_sk == NULL) {
1150 kfree_skb(skb);
1151 return -EINVAL;
1152 }
1153
1154 /*
1155 * Deliver to user space multicast routing algorithms
1156 */
1157 ret = sock_queue_rcv_skb(mrt->mroute6_sk, skb);
1158 if (ret < 0) {
1159 net_warn_ratelimited("mroute6: pending queue full, dropping entries\n");
1160 kfree_skb(skb);
1161 }
1162
1163 return ret;
1164 }
1165
1166 /*
1167 * Queue a packet for resolution. It gets locked cache entry!
1168 */
1169
1170 static int
1171 ip6mr_cache_unresolved(struct mr6_table *mrt, mifi_t mifi, struct sk_buff *skb)
1172 {
1173 bool found = false;
1174 int err;
1175 struct mfc6_cache *c;
1176
1177 spin_lock_bh(&mfc_unres_lock);
1178 list_for_each_entry(c, &mrt->mfc6_unres_queue, list) {
1179 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
1180 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) {
1181 found = true;
1182 break;
1183 }
1184 }
1185
1186 if (!found) {
1187 /*
1188 * Create a new entry if allowable
1189 */
1190
1191 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1192 (c = ip6mr_cache_alloc_unres()) == NULL) {
1193 spin_unlock_bh(&mfc_unres_lock);
1194
1195 kfree_skb(skb);
1196 return -ENOBUFS;
1197 }
1198
1199 /*
1200 * Fill in the new cache entry
1201 */
1202 c->mf6c_parent = -1;
1203 c->mf6c_origin = ipv6_hdr(skb)->saddr;
1204 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
1205
1206 /*
1207 * Reflect first query at pim6sd
1208 */
1209 err = ip6mr_cache_report(mrt, skb, mifi, MRT6MSG_NOCACHE);
1210 if (err < 0) {
1211 /* If the report failed throw the cache entry
1212 out - Brad Parker
1213 */
1214 spin_unlock_bh(&mfc_unres_lock);
1215
1216 ip6mr_cache_free(c);
1217 kfree_skb(skb);
1218 return err;
1219 }
1220
1221 atomic_inc(&mrt->cache_resolve_queue_len);
1222 list_add(&c->list, &mrt->mfc6_unres_queue);
1223
1224 ipmr_do_expire_process(mrt);
1225 }
1226
1227 /*
1228 * See if we can append the packet
1229 */
1230 if (c->mfc_un.unres.unresolved.qlen > 3) {
1231 kfree_skb(skb);
1232 err = -ENOBUFS;
1233 } else {
1234 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1235 err = 0;
1236 }
1237
1238 spin_unlock_bh(&mfc_unres_lock);
1239 return err;
1240 }
1241
1242 /*
1243 * MFC6 cache manipulation by user space
1244 */
1245
1246 static int ip6mr_mfc_delete(struct mr6_table *mrt, struct mf6cctl *mfc)
1247 {
1248 int line;
1249 struct mfc6_cache *c, *next;
1250
1251 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1252
1253 list_for_each_entry_safe(c, next, &mrt->mfc6_cache_array[line], list) {
1254 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1255 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
1256 write_lock_bh(&mrt_lock);
1257 list_del(&c->list);
1258 write_unlock_bh(&mrt_lock);
1259
1260 ip6mr_cache_free(c);
1261 return 0;
1262 }
1263 }
1264 return -ENOENT;
1265 }
1266
1267 static int ip6mr_device_event(struct notifier_block *this,
1268 unsigned long event, void *ptr)
1269 {
1270 struct net_device *dev = ptr;
1271 struct net *net = dev_net(dev);
1272 struct mr6_table *mrt;
1273 struct mif_device *v;
1274 int ct;
1275 LIST_HEAD(list);
1276
1277 if (event != NETDEV_UNREGISTER)
1278 return NOTIFY_DONE;
1279
1280 ip6mr_for_each_table(mrt, net) {
1281 v = &mrt->vif6_table[0];
1282 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1283 if (v->dev == dev)
1284 mif6_delete(mrt, ct, &list);
1285 }
1286 }
1287 unregister_netdevice_many(&list);
1288
1289 return NOTIFY_DONE;
1290 }
1291
1292 static struct notifier_block ip6_mr_notifier = {
1293 .notifier_call = ip6mr_device_event
1294 };
1295
1296 /*
1297 * Setup for IP multicast routing
1298 */
1299
1300 static int __net_init ip6mr_net_init(struct net *net)
1301 {
1302 int err;
1303
1304 err = ip6mr_rules_init(net);
1305 if (err < 0)
1306 goto fail;
1307
1308 #ifdef CONFIG_PROC_FS
1309 err = -ENOMEM;
1310 if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1311 goto proc_vif_fail;
1312 if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1313 goto proc_cache_fail;
1314 #endif
1315
1316 return 0;
1317
1318 #ifdef CONFIG_PROC_FS
1319 proc_cache_fail:
1320 proc_net_remove(net, "ip6_mr_vif");
1321 proc_vif_fail:
1322 ip6mr_rules_exit(net);
1323 #endif
1324 fail:
1325 return err;
1326 }
1327
1328 static void __net_exit ip6mr_net_exit(struct net *net)
1329 {
1330 #ifdef CONFIG_PROC_FS
1331 proc_net_remove(net, "ip6_mr_cache");
1332 proc_net_remove(net, "ip6_mr_vif");
1333 #endif
1334 ip6mr_rules_exit(net);
1335 }
1336
1337 static struct pernet_operations ip6mr_net_ops = {
1338 .init = ip6mr_net_init,
1339 .exit = ip6mr_net_exit,
1340 };
1341
1342 int __init ip6_mr_init(void)
1343 {
1344 int err;
1345
1346 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1347 sizeof(struct mfc6_cache),
1348 0, SLAB_HWCACHE_ALIGN,
1349 NULL);
1350 if (!mrt_cachep)
1351 return -ENOMEM;
1352
1353 err = register_pernet_subsys(&ip6mr_net_ops);
1354 if (err)
1355 goto reg_pernet_fail;
1356
1357 err = register_netdevice_notifier(&ip6_mr_notifier);
1358 if (err)
1359 goto reg_notif_fail;
1360 #ifdef CONFIG_IPV6_PIMSM_V2
1361 if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1362 pr_err("%s: can't add PIM protocol\n", __func__);
1363 err = -EAGAIN;
1364 goto add_proto_fail;
1365 }
1366 #endif
1367 rtnl_register(RTNL_FAMILY_IP6MR, RTM_GETROUTE, NULL,
1368 ip6mr_rtm_dumproute, NULL);
1369 return 0;
1370 #ifdef CONFIG_IPV6_PIMSM_V2
1371 add_proto_fail:
1372 unregister_netdevice_notifier(&ip6_mr_notifier);
1373 #endif
1374 reg_notif_fail:
1375 unregister_pernet_subsys(&ip6mr_net_ops);
1376 reg_pernet_fail:
1377 kmem_cache_destroy(mrt_cachep);
1378 return err;
1379 }
1380
1381 void ip6_mr_cleanup(void)
1382 {
1383 unregister_netdevice_notifier(&ip6_mr_notifier);
1384 unregister_pernet_subsys(&ip6mr_net_ops);
1385 kmem_cache_destroy(mrt_cachep);
1386 }
1387
1388 static int ip6mr_mfc_add(struct net *net, struct mr6_table *mrt,
1389 struct mf6cctl *mfc, int mrtsock)
1390 {
1391 bool found = false;
1392 int line;
1393 struct mfc6_cache *uc, *c;
1394 unsigned char ttls[MAXMIFS];
1395 int i;
1396
1397 if (mfc->mf6cc_parent >= MAXMIFS)
1398 return -ENFILE;
1399
1400 memset(ttls, 255, MAXMIFS);
1401 for (i = 0; i < MAXMIFS; i++) {
1402 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1403 ttls[i] = 1;
1404
1405 }
1406
1407 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1408
1409 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
1410 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1411 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
1412 found = true;
1413 break;
1414 }
1415 }
1416
1417 if (found) {
1418 write_lock_bh(&mrt_lock);
1419 c->mf6c_parent = mfc->mf6cc_parent;
1420 ip6mr_update_thresholds(mrt, c, ttls);
1421 if (!mrtsock)
1422 c->mfc_flags |= MFC_STATIC;
1423 write_unlock_bh(&mrt_lock);
1424 return 0;
1425 }
1426
1427 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1428 return -EINVAL;
1429
1430 c = ip6mr_cache_alloc();
1431 if (c == NULL)
1432 return -ENOMEM;
1433
1434 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1435 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1436 c->mf6c_parent = mfc->mf6cc_parent;
1437 ip6mr_update_thresholds(mrt, c, ttls);
1438 if (!mrtsock)
1439 c->mfc_flags |= MFC_STATIC;
1440
1441 write_lock_bh(&mrt_lock);
1442 list_add(&c->list, &mrt->mfc6_cache_array[line]);
1443 write_unlock_bh(&mrt_lock);
1444
1445 /*
1446 * Check to see if we resolved a queued list. If so we
1447 * need to send on the frames and tidy up.
1448 */
1449 found = false;
1450 spin_lock_bh(&mfc_unres_lock);
1451 list_for_each_entry(uc, &mrt->mfc6_unres_queue, list) {
1452 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
1453 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1454 list_del(&uc->list);
1455 atomic_dec(&mrt->cache_resolve_queue_len);
1456 found = true;
1457 break;
1458 }
1459 }
1460 if (list_empty(&mrt->mfc6_unres_queue))
1461 del_timer(&mrt->ipmr_expire_timer);
1462 spin_unlock_bh(&mfc_unres_lock);
1463
1464 if (found) {
1465 ip6mr_cache_resolve(net, mrt, uc, c);
1466 ip6mr_cache_free(uc);
1467 }
1468 return 0;
1469 }
1470
1471 /*
1472 * Close the multicast socket, and clear the vif tables etc
1473 */
1474
1475 static void mroute_clean_tables(struct mr6_table *mrt)
1476 {
1477 int i;
1478 LIST_HEAD(list);
1479 struct mfc6_cache *c, *next;
1480
1481 /*
1482 * Shut down all active vif entries
1483 */
1484 for (i = 0; i < mrt->maxvif; i++) {
1485 if (!(mrt->vif6_table[i].flags & VIFF_STATIC))
1486 mif6_delete(mrt, i, &list);
1487 }
1488 unregister_netdevice_many(&list);
1489
1490 /*
1491 * Wipe the cache
1492 */
1493 for (i = 0; i < MFC6_LINES; i++) {
1494 list_for_each_entry_safe(c, next, &mrt->mfc6_cache_array[i], list) {
1495 if (c->mfc_flags & MFC_STATIC)
1496 continue;
1497 write_lock_bh(&mrt_lock);
1498 list_del(&c->list);
1499 write_unlock_bh(&mrt_lock);
1500
1501 ip6mr_cache_free(c);
1502 }
1503 }
1504
1505 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1506 spin_lock_bh(&mfc_unres_lock);
1507 list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
1508 list_del(&c->list);
1509 ip6mr_destroy_unres(mrt, c);
1510 }
1511 spin_unlock_bh(&mfc_unres_lock);
1512 }
1513 }
1514
1515 static int ip6mr_sk_init(struct mr6_table *mrt, struct sock *sk)
1516 {
1517 int err = 0;
1518 struct net *net = sock_net(sk);
1519
1520 rtnl_lock();
1521 write_lock_bh(&mrt_lock);
1522 if (likely(mrt->mroute6_sk == NULL)) {
1523 mrt->mroute6_sk = sk;
1524 net->ipv6.devconf_all->mc_forwarding++;
1525 inet6_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1526 NETCONFA_IFINDEX_ALL,
1527 net->ipv6.devconf_all);
1528 }
1529 else
1530 err = -EADDRINUSE;
1531 write_unlock_bh(&mrt_lock);
1532
1533 rtnl_unlock();
1534
1535 return err;
1536 }
1537
1538 int ip6mr_sk_done(struct sock *sk)
1539 {
1540 int err = -EACCES;
1541 struct net *net = sock_net(sk);
1542 struct mr6_table *mrt;
1543
1544 rtnl_lock();
1545 ip6mr_for_each_table(mrt, net) {
1546 if (sk == mrt->mroute6_sk) {
1547 write_lock_bh(&mrt_lock);
1548 mrt->mroute6_sk = NULL;
1549 net->ipv6.devconf_all->mc_forwarding--;
1550 inet6_netconf_notify_devconf(net,
1551 NETCONFA_MC_FORWARDING,
1552 NETCONFA_IFINDEX_ALL,
1553 net->ipv6.devconf_all);
1554 write_unlock_bh(&mrt_lock);
1555
1556 mroute_clean_tables(mrt);
1557 err = 0;
1558 break;
1559 }
1560 }
1561 rtnl_unlock();
1562
1563 return err;
1564 }
1565
1566 struct sock *mroute6_socket(struct net *net, struct sk_buff *skb)
1567 {
1568 struct mr6_table *mrt;
1569 struct flowi6 fl6 = {
1570 .flowi6_iif = skb->skb_iif,
1571 .flowi6_oif = skb->dev->ifindex,
1572 .flowi6_mark = skb->mark,
1573 };
1574
1575 if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0)
1576 return NULL;
1577
1578 return mrt->mroute6_sk;
1579 }
1580
1581 /*
1582 * Socket options and virtual interface manipulation. The whole
1583 * virtual interface system is a complete heap, but unfortunately
1584 * that's how BSD mrouted happens to think. Maybe one day with a proper
1585 * MOSPF/PIM router set up we can clean this up.
1586 */
1587
1588 int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1589 {
1590 int ret;
1591 struct mif6ctl vif;
1592 struct mf6cctl mfc;
1593 mifi_t mifi;
1594 struct net *net = sock_net(sk);
1595 struct mr6_table *mrt;
1596
1597 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1598 if (mrt == NULL)
1599 return -ENOENT;
1600
1601 if (optname != MRT6_INIT) {
1602 if (sk != mrt->mroute6_sk && !ns_capable(net->user_ns, CAP_NET_ADMIN))
1603 return -EACCES;
1604 }
1605
1606 switch (optname) {
1607 case MRT6_INIT:
1608 if (sk->sk_type != SOCK_RAW ||
1609 inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1610 return -EOPNOTSUPP;
1611 if (optlen < sizeof(int))
1612 return -EINVAL;
1613
1614 return ip6mr_sk_init(mrt, sk);
1615
1616 case MRT6_DONE:
1617 return ip6mr_sk_done(sk);
1618
1619 case MRT6_ADD_MIF:
1620 if (optlen < sizeof(vif))
1621 return -EINVAL;
1622 if (copy_from_user(&vif, optval, sizeof(vif)))
1623 return -EFAULT;
1624 if (vif.mif6c_mifi >= MAXMIFS)
1625 return -ENFILE;
1626 rtnl_lock();
1627 ret = mif6_add(net, mrt, &vif, sk == mrt->mroute6_sk);
1628 rtnl_unlock();
1629 return ret;
1630
1631 case MRT6_DEL_MIF:
1632 if (optlen < sizeof(mifi_t))
1633 return -EINVAL;
1634 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1635 return -EFAULT;
1636 rtnl_lock();
1637 ret = mif6_delete(mrt, mifi, NULL);
1638 rtnl_unlock();
1639 return ret;
1640
1641 /*
1642 * Manipulate the forwarding caches. These live
1643 * in a sort of kernel/user symbiosis.
1644 */
1645 case MRT6_ADD_MFC:
1646 case MRT6_DEL_MFC:
1647 if (optlen < sizeof(mfc))
1648 return -EINVAL;
1649 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1650 return -EFAULT;
1651 rtnl_lock();
1652 if (optname == MRT6_DEL_MFC)
1653 ret = ip6mr_mfc_delete(mrt, &mfc);
1654 else
1655 ret = ip6mr_mfc_add(net, mrt, &mfc, sk == mrt->mroute6_sk);
1656 rtnl_unlock();
1657 return ret;
1658
1659 /*
1660 * Control PIM assert (to activate pim will activate assert)
1661 */
1662 case MRT6_ASSERT:
1663 {
1664 int v;
1665
1666 if (optlen != sizeof(v))
1667 return -EINVAL;
1668 if (get_user(v, (int __user *)optval))
1669 return -EFAULT;
1670 mrt->mroute_do_assert = v;
1671 return 0;
1672 }
1673
1674 #ifdef CONFIG_IPV6_PIMSM_V2
1675 case MRT6_PIM:
1676 {
1677 int v;
1678
1679 if (optlen != sizeof(v))
1680 return -EINVAL;
1681 if (get_user(v, (int __user *)optval))
1682 return -EFAULT;
1683 v = !!v;
1684 rtnl_lock();
1685 ret = 0;
1686 if (v != mrt->mroute_do_pim) {
1687 mrt->mroute_do_pim = v;
1688 mrt->mroute_do_assert = v;
1689 }
1690 rtnl_unlock();
1691 return ret;
1692 }
1693
1694 #endif
1695 #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
1696 case MRT6_TABLE:
1697 {
1698 u32 v;
1699
1700 if (optlen != sizeof(u32))
1701 return -EINVAL;
1702 if (get_user(v, (u32 __user *)optval))
1703 return -EFAULT;
1704 if (sk == mrt->mroute6_sk)
1705 return -EBUSY;
1706
1707 rtnl_lock();
1708 ret = 0;
1709 if (!ip6mr_new_table(net, v))
1710 ret = -ENOMEM;
1711 raw6_sk(sk)->ip6mr_table = v;
1712 rtnl_unlock();
1713 return ret;
1714 }
1715 #endif
1716 /*
1717 * Spurious command, or MRT6_VERSION which you cannot
1718 * set.
1719 */
1720 default:
1721 return -ENOPROTOOPT;
1722 }
1723 }
1724
1725 /*
1726 * Getsock opt support for the multicast routing system.
1727 */
1728
1729 int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1730 int __user *optlen)
1731 {
1732 int olr;
1733 int val;
1734 struct net *net = sock_net(sk);
1735 struct mr6_table *mrt;
1736
1737 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1738 if (mrt == NULL)
1739 return -ENOENT;
1740
1741 switch (optname) {
1742 case MRT6_VERSION:
1743 val = 0x0305;
1744 break;
1745 #ifdef CONFIG_IPV6_PIMSM_V2
1746 case MRT6_PIM:
1747 val = mrt->mroute_do_pim;
1748 break;
1749 #endif
1750 case MRT6_ASSERT:
1751 val = mrt->mroute_do_assert;
1752 break;
1753 default:
1754 return -ENOPROTOOPT;
1755 }
1756
1757 if (get_user(olr, optlen))
1758 return -EFAULT;
1759
1760 olr = min_t(int, olr, sizeof(int));
1761 if (olr < 0)
1762 return -EINVAL;
1763
1764 if (put_user(olr, optlen))
1765 return -EFAULT;
1766 if (copy_to_user(optval, &val, olr))
1767 return -EFAULT;
1768 return 0;
1769 }
1770
1771 /*
1772 * The IP multicast ioctl support routines.
1773 */
1774
1775 int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1776 {
1777 struct sioc_sg_req6 sr;
1778 struct sioc_mif_req6 vr;
1779 struct mif_device *vif;
1780 struct mfc6_cache *c;
1781 struct net *net = sock_net(sk);
1782 struct mr6_table *mrt;
1783
1784 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1785 if (mrt == NULL)
1786 return -ENOENT;
1787
1788 switch (cmd) {
1789 case SIOCGETMIFCNT_IN6:
1790 if (copy_from_user(&vr, arg, sizeof(vr)))
1791 return -EFAULT;
1792 if (vr.mifi >= mrt->maxvif)
1793 return -EINVAL;
1794 read_lock(&mrt_lock);
1795 vif = &mrt->vif6_table[vr.mifi];
1796 if (MIF_EXISTS(mrt, vr.mifi)) {
1797 vr.icount = vif->pkt_in;
1798 vr.ocount = vif->pkt_out;
1799 vr.ibytes = vif->bytes_in;
1800 vr.obytes = vif->bytes_out;
1801 read_unlock(&mrt_lock);
1802
1803 if (copy_to_user(arg, &vr, sizeof(vr)))
1804 return -EFAULT;
1805 return 0;
1806 }
1807 read_unlock(&mrt_lock);
1808 return -EADDRNOTAVAIL;
1809 case SIOCGETSGCNT_IN6:
1810 if (copy_from_user(&sr, arg, sizeof(sr)))
1811 return -EFAULT;
1812
1813 read_lock(&mrt_lock);
1814 c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
1815 if (c) {
1816 sr.pktcnt = c->mfc_un.res.pkt;
1817 sr.bytecnt = c->mfc_un.res.bytes;
1818 sr.wrong_if = c->mfc_un.res.wrong_if;
1819 read_unlock(&mrt_lock);
1820
1821 if (copy_to_user(arg, &sr, sizeof(sr)))
1822 return -EFAULT;
1823 return 0;
1824 }
1825 read_unlock(&mrt_lock);
1826 return -EADDRNOTAVAIL;
1827 default:
1828 return -ENOIOCTLCMD;
1829 }
1830 }
1831
1832 #ifdef CONFIG_COMPAT
1833 struct compat_sioc_sg_req6 {
1834 struct sockaddr_in6 src;
1835 struct sockaddr_in6 grp;
1836 compat_ulong_t pktcnt;
1837 compat_ulong_t bytecnt;
1838 compat_ulong_t wrong_if;
1839 };
1840
1841 struct compat_sioc_mif_req6 {
1842 mifi_t mifi;
1843 compat_ulong_t icount;
1844 compat_ulong_t ocount;
1845 compat_ulong_t ibytes;
1846 compat_ulong_t obytes;
1847 };
1848
1849 int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1850 {
1851 struct compat_sioc_sg_req6 sr;
1852 struct compat_sioc_mif_req6 vr;
1853 struct mif_device *vif;
1854 struct mfc6_cache *c;
1855 struct net *net = sock_net(sk);
1856 struct mr6_table *mrt;
1857
1858 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1859 if (mrt == NULL)
1860 return -ENOENT;
1861
1862 switch (cmd) {
1863 case SIOCGETMIFCNT_IN6:
1864 if (copy_from_user(&vr, arg, sizeof(vr)))
1865 return -EFAULT;
1866 if (vr.mifi >= mrt->maxvif)
1867 return -EINVAL;
1868 read_lock(&mrt_lock);
1869 vif = &mrt->vif6_table[vr.mifi];
1870 if (MIF_EXISTS(mrt, vr.mifi)) {
1871 vr.icount = vif->pkt_in;
1872 vr.ocount = vif->pkt_out;
1873 vr.ibytes = vif->bytes_in;
1874 vr.obytes = vif->bytes_out;
1875 read_unlock(&mrt_lock);
1876
1877 if (copy_to_user(arg, &vr, sizeof(vr)))
1878 return -EFAULT;
1879 return 0;
1880 }
1881 read_unlock(&mrt_lock);
1882 return -EADDRNOTAVAIL;
1883 case SIOCGETSGCNT_IN6:
1884 if (copy_from_user(&sr, arg, sizeof(sr)))
1885 return -EFAULT;
1886
1887 read_lock(&mrt_lock);
1888 c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
1889 if (c) {
1890 sr.pktcnt = c->mfc_un.res.pkt;
1891 sr.bytecnt = c->mfc_un.res.bytes;
1892 sr.wrong_if = c->mfc_un.res.wrong_if;
1893 read_unlock(&mrt_lock);
1894
1895 if (copy_to_user(arg, &sr, sizeof(sr)))
1896 return -EFAULT;
1897 return 0;
1898 }
1899 read_unlock(&mrt_lock);
1900 return -EADDRNOTAVAIL;
1901 default:
1902 return -ENOIOCTLCMD;
1903 }
1904 }
1905 #endif
1906
1907 static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1908 {
1909 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
1910 IPSTATS_MIB_OUTFORWDATAGRAMS);
1911 IP6_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
1912 IPSTATS_MIB_OUTOCTETS, skb->len);
1913 return dst_output(skb);
1914 }
1915
1916 /*
1917 * Processing handlers for ip6mr_forward
1918 */
1919
1920 static int ip6mr_forward2(struct net *net, struct mr6_table *mrt,
1921 struct sk_buff *skb, struct mfc6_cache *c, int vifi)
1922 {
1923 struct ipv6hdr *ipv6h;
1924 struct mif_device *vif = &mrt->vif6_table[vifi];
1925 struct net_device *dev;
1926 struct dst_entry *dst;
1927 struct flowi6 fl6;
1928
1929 if (vif->dev == NULL)
1930 goto out_free;
1931
1932 #ifdef CONFIG_IPV6_PIMSM_V2
1933 if (vif->flags & MIFF_REGISTER) {
1934 vif->pkt_out++;
1935 vif->bytes_out += skb->len;
1936 vif->dev->stats.tx_bytes += skb->len;
1937 vif->dev->stats.tx_packets++;
1938 ip6mr_cache_report(mrt, skb, vifi, MRT6MSG_WHOLEPKT);
1939 goto out_free;
1940 }
1941 #endif
1942
1943 ipv6h = ipv6_hdr(skb);
1944
1945 fl6 = (struct flowi6) {
1946 .flowi6_oif = vif->link,
1947 .daddr = ipv6h->daddr,
1948 };
1949
1950 dst = ip6_route_output(net, NULL, &fl6);
1951 if (dst->error) {
1952 dst_release(dst);
1953 goto out_free;
1954 }
1955
1956 skb_dst_drop(skb);
1957 skb_dst_set(skb, dst);
1958
1959 /*
1960 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1961 * not only before forwarding, but after forwarding on all output
1962 * interfaces. It is clear, if mrouter runs a multicasting
1963 * program, it should receive packets not depending to what interface
1964 * program is joined.
1965 * If we will not make it, the program will have to join on all
1966 * interfaces. On the other hand, multihoming host (or router, but
1967 * not mrouter) cannot join to more than one interface - it will
1968 * result in receiving multiple packets.
1969 */
1970 dev = vif->dev;
1971 skb->dev = dev;
1972 vif->pkt_out++;
1973 vif->bytes_out += skb->len;
1974
1975 /* We are about to write */
1976 /* XXX: extension headers? */
1977 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1978 goto out_free;
1979
1980 ipv6h = ipv6_hdr(skb);
1981 ipv6h->hop_limit--;
1982
1983 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1984
1985 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
1986 ip6mr_forward2_finish);
1987
1988 out_free:
1989 kfree_skb(skb);
1990 return 0;
1991 }
1992
1993 static int ip6mr_find_vif(struct mr6_table *mrt, struct net_device *dev)
1994 {
1995 int ct;
1996
1997 for (ct = mrt->maxvif - 1; ct >= 0; ct--) {
1998 if (mrt->vif6_table[ct].dev == dev)
1999 break;
2000 }
2001 return ct;
2002 }
2003
2004 static int ip6_mr_forward(struct net *net, struct mr6_table *mrt,
2005 struct sk_buff *skb, struct mfc6_cache *cache)
2006 {
2007 int psend = -1;
2008 int vif, ct;
2009
2010 vif = cache->mf6c_parent;
2011 cache->mfc_un.res.pkt++;
2012 cache->mfc_un.res.bytes += skb->len;
2013
2014 /*
2015 * Wrong interface: drop packet and (maybe) send PIM assert.
2016 */
2017 if (mrt->vif6_table[vif].dev != skb->dev) {
2018 int true_vifi;
2019
2020 cache->mfc_un.res.wrong_if++;
2021 true_vifi = ip6mr_find_vif(mrt, skb->dev);
2022
2023 if (true_vifi >= 0 && mrt->mroute_do_assert &&
2024 /* pimsm uses asserts, when switching from RPT to SPT,
2025 so that we cannot check that packet arrived on an oif.
2026 It is bad, but otherwise we would need to move pretty
2027 large chunk of pimd to kernel. Ough... --ANK
2028 */
2029 (mrt->mroute_do_pim ||
2030 cache->mfc_un.res.ttls[true_vifi] < 255) &&
2031 time_after(jiffies,
2032 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
2033 cache->mfc_un.res.last_assert = jiffies;
2034 ip6mr_cache_report(mrt, skb, true_vifi, MRT6MSG_WRONGMIF);
2035 }
2036 goto dont_forward;
2037 }
2038
2039 mrt->vif6_table[vif].pkt_in++;
2040 mrt->vif6_table[vif].bytes_in += skb->len;
2041
2042 /*
2043 * Forward the frame
2044 */
2045 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
2046 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
2047 if (psend != -1) {
2048 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2049 if (skb2)
2050 ip6mr_forward2(net, mrt, skb2, cache, psend);
2051 }
2052 psend = ct;
2053 }
2054 }
2055 if (psend != -1) {
2056 ip6mr_forward2(net, mrt, skb, cache, psend);
2057 return 0;
2058 }
2059
2060 dont_forward:
2061 kfree_skb(skb);
2062 return 0;
2063 }
2064
2065
2066 /*
2067 * Multicast packets for forwarding arrive here
2068 */
2069
2070 int ip6_mr_input(struct sk_buff *skb)
2071 {
2072 struct mfc6_cache *cache;
2073 struct net *net = dev_net(skb->dev);
2074 struct mr6_table *mrt;
2075 struct flowi6 fl6 = {
2076 .flowi6_iif = skb->dev->ifindex,
2077 .flowi6_mark = skb->mark,
2078 };
2079 int err;
2080
2081 err = ip6mr_fib_lookup(net, &fl6, &mrt);
2082 if (err < 0) {
2083 kfree_skb(skb);
2084 return err;
2085 }
2086
2087 read_lock(&mrt_lock);
2088 cache = ip6mr_cache_find(mrt,
2089 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
2090
2091 /*
2092 * No usable cache entry
2093 */
2094 if (cache == NULL) {
2095 int vif;
2096
2097 vif = ip6mr_find_vif(mrt, skb->dev);
2098 if (vif >= 0) {
2099 int err = ip6mr_cache_unresolved(mrt, vif, skb);
2100 read_unlock(&mrt_lock);
2101
2102 return err;
2103 }
2104 read_unlock(&mrt_lock);
2105 kfree_skb(skb);
2106 return -ENODEV;
2107 }
2108
2109 ip6_mr_forward(net, mrt, skb, cache);
2110
2111 read_unlock(&mrt_lock);
2112
2113 return 0;
2114 }
2115
2116
2117 static int __ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2118 struct mfc6_cache *c, struct rtmsg *rtm)
2119 {
2120 int ct;
2121 struct rtnexthop *nhp;
2122 struct nlattr *mp_attr;
2123 struct rta_mfc_stats mfcs;
2124
2125 /* If cache is unresolved, don't try to parse IIF and OIF */
2126 if (c->mf6c_parent >= MAXMIFS)
2127 return -ENOENT;
2128
2129 if (MIF_EXISTS(mrt, c->mf6c_parent) &&
2130 nla_put_u32(skb, RTA_IIF, mrt->vif6_table[c->mf6c_parent].dev->ifindex) < 0)
2131 return -EMSGSIZE;
2132 mp_attr = nla_nest_start(skb, RTA_MULTIPATH);
2133 if (mp_attr == NULL)
2134 return -EMSGSIZE;
2135
2136 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2137 if (MIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2138 nhp = nla_reserve_nohdr(skb, sizeof(*nhp));
2139 if (nhp == NULL) {
2140 nla_nest_cancel(skb, mp_attr);
2141 return -EMSGSIZE;
2142 }
2143
2144 nhp->rtnh_flags = 0;
2145 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2146 nhp->rtnh_ifindex = mrt->vif6_table[ct].dev->ifindex;
2147 nhp->rtnh_len = sizeof(*nhp);
2148 }
2149 }
2150
2151 nla_nest_end(skb, mp_attr);
2152
2153 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2154 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2155 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2156 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2157 return -EMSGSIZE;
2158
2159 rtm->rtm_type = RTN_MULTICAST;
2160 return 1;
2161 }
2162
2163 int ip6mr_get_route(struct net *net,
2164 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
2165 {
2166 int err;
2167 struct mr6_table *mrt;
2168 struct mfc6_cache *cache;
2169 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
2170
2171 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
2172 if (mrt == NULL)
2173 return -ENOENT;
2174
2175 read_lock(&mrt_lock);
2176 cache = ip6mr_cache_find(mrt, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
2177
2178 if (!cache) {
2179 struct sk_buff *skb2;
2180 struct ipv6hdr *iph;
2181 struct net_device *dev;
2182 int vif;
2183
2184 if (nowait) {
2185 read_unlock(&mrt_lock);
2186 return -EAGAIN;
2187 }
2188
2189 dev = skb->dev;
2190 if (dev == NULL || (vif = ip6mr_find_vif(mrt, dev)) < 0) {
2191 read_unlock(&mrt_lock);
2192 return -ENODEV;
2193 }
2194
2195 /* really correct? */
2196 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
2197 if (!skb2) {
2198 read_unlock(&mrt_lock);
2199 return -ENOMEM;
2200 }
2201
2202 skb_reset_transport_header(skb2);
2203
2204 skb_put(skb2, sizeof(struct ipv6hdr));
2205 skb_reset_network_header(skb2);
2206
2207 iph = ipv6_hdr(skb2);
2208 iph->version = 0;
2209 iph->priority = 0;
2210 iph->flow_lbl[0] = 0;
2211 iph->flow_lbl[1] = 0;
2212 iph->flow_lbl[2] = 0;
2213 iph->payload_len = 0;
2214 iph->nexthdr = IPPROTO_NONE;
2215 iph->hop_limit = 0;
2216 iph->saddr = rt->rt6i_src.addr;
2217 iph->daddr = rt->rt6i_dst.addr;
2218
2219 err = ip6mr_cache_unresolved(mrt, vif, skb2);
2220 read_unlock(&mrt_lock);
2221
2222 return err;
2223 }
2224
2225 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
2226 cache->mfc_flags |= MFC_NOTIFY;
2227
2228 err = __ip6mr_fill_mroute(mrt, skb, cache, rtm);
2229 read_unlock(&mrt_lock);
2230 return err;
2231 }
2232
2233 static int ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2234 u32 portid, u32 seq, struct mfc6_cache *c)
2235 {
2236 struct nlmsghdr *nlh;
2237 struct rtmsg *rtm;
2238
2239 nlh = nlmsg_put(skb, portid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2240 if (nlh == NULL)
2241 return -EMSGSIZE;
2242
2243 rtm = nlmsg_data(nlh);
2244 rtm->rtm_family = RTNL_FAMILY_IPMR;
2245 rtm->rtm_dst_len = 128;
2246 rtm->rtm_src_len = 128;
2247 rtm->rtm_tos = 0;
2248 rtm->rtm_table = mrt->id;
2249 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2250 goto nla_put_failure;
2251 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2252 rtm->rtm_protocol = RTPROT_UNSPEC;
2253 rtm->rtm_flags = 0;
2254
2255 if (nla_put(skb, RTA_SRC, 16, &c->mf6c_origin) ||
2256 nla_put(skb, RTA_DST, 16, &c->mf6c_mcastgrp))
2257 goto nla_put_failure;
2258 if (__ip6mr_fill_mroute(mrt, skb, c, rtm) < 0)
2259 goto nla_put_failure;
2260
2261 return nlmsg_end(skb, nlh);
2262
2263 nla_put_failure:
2264 nlmsg_cancel(skb, nlh);
2265 return -EMSGSIZE;
2266 }
2267
2268 static int ip6mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2269 {
2270 struct net *net = sock_net(skb->sk);
2271 struct mr6_table *mrt;
2272 struct mfc6_cache *mfc;
2273 unsigned int t = 0, s_t;
2274 unsigned int h = 0, s_h;
2275 unsigned int e = 0, s_e;
2276
2277 s_t = cb->args[0];
2278 s_h = cb->args[1];
2279 s_e = cb->args[2];
2280
2281 read_lock(&mrt_lock);
2282 ip6mr_for_each_table(mrt, net) {
2283 if (t < s_t)
2284 goto next_table;
2285 if (t > s_t)
2286 s_h = 0;
2287 for (h = s_h; h < MFC6_LINES; h++) {
2288 list_for_each_entry(mfc, &mrt->mfc6_cache_array[h], list) {
2289 if (e < s_e)
2290 goto next_entry;
2291 if (ip6mr_fill_mroute(mrt, skb,
2292 NETLINK_CB(cb->skb).portid,
2293 cb->nlh->nlmsg_seq,
2294 mfc) < 0)
2295 goto done;
2296 next_entry:
2297 e++;
2298 }
2299 e = s_e = 0;
2300 }
2301 s_h = 0;
2302 next_table:
2303 t++;
2304 }
2305 done:
2306 read_unlock(&mrt_lock);
2307
2308 cb->args[2] = e;
2309 cb->args[1] = h;
2310 cb->args[0] = t;
2311
2312 return skb->len;
2313 }
This page took 0.165827 seconds and 5 git commands to generate.