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