2 * Linux IPv6 multicast routing support for BSD pim6sd
3 * Based on net/ipv4/ipmr.c.
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>
9 * Copyright (C)2007,2008 USAGI/WIDE Project
10 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
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.
19 #include <linux/uaccess.h>
20 #include <linux/types.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
24 #include <linux/kernel.h>
25 #include <linux/fcntl.h>
26 #include <linux/stat.h>
27 #include <linux/socket.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/inetdevice.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/init.h>
34 #include <linux/compat.h>
35 #include <linux/rhashtable.h>
36 #include <net/protocol.h>
37 #include <linux/skbuff.h>
39 #include <linux/notifier.h>
40 #include <linux/if_arp.h>
41 #include <net/checksum.h>
42 #include <net/netlink.h>
43 #include <net/fib_rules.h>
46 #include <net/ip6_route.h>
47 #include <linux/mroute6.h>
48 #include <linux/pim.h>
49 #include <net/addrconf.h>
50 #include <linux/netfilter_ipv6.h>
51 #include <linux/export.h>
52 #include <net/ip6_checksum.h>
53 #include <linux/netconf.h>
56 struct fib_rule common
;
63 /* Big lock, protecting vif table, mrt cache and mroute socket state.
64 Note that the changes are semaphored via rtnl_lock.
67 static DEFINE_RWLOCK(mrt_lock
);
69 /* Multicast router control variables */
71 /* Special spinlock for queue of unresolved entries */
72 static DEFINE_SPINLOCK(mfc_unres_lock
);
74 /* We return to original Alan's scheme. Hash table of resolved
75 entries is changed only in process context and protected
76 with weak lock mrt_lock. Queue of unresolved entries is protected
77 with strong spinlock mfc_unres_lock.
79 In this case data path is free of exclusive locks at all.
82 static struct kmem_cache
*mrt_cachep __read_mostly
;
84 static struct mr_table
*ip6mr_new_table(struct net
*net
, u32 id
);
85 static void ip6mr_free_table(struct mr_table
*mrt
);
87 static void ip6_mr_forward(struct net
*net
, struct mr_table
*mrt
,
88 struct net_device
*dev
, struct sk_buff
*skb
,
89 struct mfc6_cache
*cache
);
90 static int ip6mr_cache_report(struct mr_table
*mrt
, struct sk_buff
*pkt
,
91 mifi_t mifi
, int assert);
92 static void mr6_netlink_event(struct mr_table
*mrt
, struct mfc6_cache
*mfc
,
94 static void mrt6msg_netlink_event(struct mr_table
*mrt
, struct sk_buff
*pkt
);
95 static int ip6mr_rtm_dumproute(struct sk_buff
*skb
,
96 struct netlink_callback
*cb
);
97 static void mroute_clean_tables(struct mr_table
*mrt
, bool all
);
98 static void ipmr_expire_process(struct timer_list
*t
);
100 #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
101 #define ip6mr_for_each_table(mrt, net) \
102 list_for_each_entry_rcu(mrt, &net->ipv6.mr6_tables, list)
104 static struct mr_table
*ip6mr_mr_table_iter(struct net
*net
,
105 struct mr_table
*mrt
)
107 struct mr_table
*ret
;
110 ret
= list_entry_rcu(net
->ipv6
.mr6_tables
.next
,
111 struct mr_table
, list
);
113 ret
= list_entry_rcu(mrt
->list
.next
,
114 struct mr_table
, list
);
116 if (&ret
->list
== &net
->ipv6
.mr6_tables
)
121 static struct mr_table
*ip6mr_get_table(struct net
*net
, u32 id
)
123 struct mr_table
*mrt
;
125 ip6mr_for_each_table(mrt
, net
) {
132 static int ip6mr_fib_lookup(struct net
*net
, struct flowi6
*flp6
,
133 struct mr_table
**mrt
)
136 struct ip6mr_result res
;
137 struct fib_lookup_arg arg
= {
139 .flags
= FIB_LOOKUP_NOREF
,
142 /* update flow if oif or iif point to device enslaved to l3mdev */
143 l3mdev_update_flow(net
, flowi6_to_flowi(flp6
));
145 err
= fib_rules_lookup(net
->ipv6
.mr6_rules_ops
,
146 flowi6_to_flowi(flp6
), 0, &arg
);
153 static int ip6mr_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
154 int flags
, struct fib_lookup_arg
*arg
)
156 struct ip6mr_result
*res
= arg
->result
;
157 struct mr_table
*mrt
;
159 switch (rule
->action
) {
162 case FR_ACT_UNREACHABLE
:
164 case FR_ACT_PROHIBIT
:
166 case FR_ACT_BLACKHOLE
:
171 arg
->table
= fib_rule_get_table(rule
, arg
);
173 mrt
= ip6mr_get_table(rule
->fr_net
, arg
->table
);
180 static int ip6mr_rule_match(struct fib_rule
*rule
, struct flowi
*flp
, int flags
)
185 static const struct nla_policy ip6mr_rule_policy
[FRA_MAX
+ 1] = {
189 static int ip6mr_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
190 struct fib_rule_hdr
*frh
, struct nlattr
**tb
,
191 struct netlink_ext_ack
*extack
)
196 static int ip6mr_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
202 static int ip6mr_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
203 struct fib_rule_hdr
*frh
)
211 static const struct fib_rules_ops __net_initconst ip6mr_rules_ops_template
= {
212 .family
= RTNL_FAMILY_IP6MR
,
213 .rule_size
= sizeof(struct ip6mr_rule
),
214 .addr_size
= sizeof(struct in6_addr
),
215 .action
= ip6mr_rule_action
,
216 .match
= ip6mr_rule_match
,
217 .configure
= ip6mr_rule_configure
,
218 .compare
= ip6mr_rule_compare
,
219 .fill
= ip6mr_rule_fill
,
220 .nlgroup
= RTNLGRP_IPV6_RULE
,
221 .policy
= ip6mr_rule_policy
,
222 .owner
= THIS_MODULE
,
225 static int __net_init
ip6mr_rules_init(struct net
*net
)
227 struct fib_rules_ops
*ops
;
228 struct mr_table
*mrt
;
231 ops
= fib_rules_register(&ip6mr_rules_ops_template
, net
);
235 INIT_LIST_HEAD(&net
->ipv6
.mr6_tables
);
237 mrt
= ip6mr_new_table(net
, RT6_TABLE_DFLT
);
243 err
= fib_default_rule_add(ops
, 0x7fff, RT6_TABLE_DFLT
, 0);
247 net
->ipv6
.mr6_rules_ops
= ops
;
251 ip6mr_free_table(mrt
);
253 fib_rules_unregister(ops
);
257 static void __net_exit
ip6mr_rules_exit(struct net
*net
)
259 struct mr_table
*mrt
, *next
;
262 list_for_each_entry_safe(mrt
, next
, &net
->ipv6
.mr6_tables
, list
) {
263 list_del(&mrt
->list
);
264 ip6mr_free_table(mrt
);
266 fib_rules_unregister(net
->ipv6
.mr6_rules_ops
);
270 static int ip6mr_rules_dump(struct net
*net
, struct notifier_block
*nb
)
272 return fib_rules_dump(net
, nb
, RTNL_FAMILY_IP6MR
);
275 static unsigned int ip6mr_rules_seq_read(struct net
*net
)
277 return fib_rules_seq_read(net
, RTNL_FAMILY_IP6MR
);
280 bool ip6mr_rule_default(const struct fib_rule
*rule
)
282 return fib_rule_matchall(rule
) && rule
->action
== FR_ACT_TO_TBL
&&
283 rule
->table
== RT6_TABLE_DFLT
&& !rule
->l3mdev
;
285 EXPORT_SYMBOL(ip6mr_rule_default
);
287 #define ip6mr_for_each_table(mrt, net) \
288 for (mrt = net->ipv6.mrt6; mrt; mrt = NULL)
290 static struct mr_table
*ip6mr_mr_table_iter(struct net
*net
,
291 struct mr_table
*mrt
)
294 return net
->ipv6
.mrt6
;
298 static struct mr_table
*ip6mr_get_table(struct net
*net
, u32 id
)
300 return net
->ipv6
.mrt6
;
303 static int ip6mr_fib_lookup(struct net
*net
, struct flowi6
*flp6
,
304 struct mr_table
**mrt
)
306 *mrt
= net
->ipv6
.mrt6
;
310 static int __net_init
ip6mr_rules_init(struct net
*net
)
312 struct mr_table
*mrt
;
314 mrt
= ip6mr_new_table(net
, RT6_TABLE_DFLT
);
317 net
->ipv6
.mrt6
= mrt
;
321 static void __net_exit
ip6mr_rules_exit(struct net
*net
)
324 ip6mr_free_table(net
->ipv6
.mrt6
);
325 net
->ipv6
.mrt6
= NULL
;
329 static int ip6mr_rules_dump(struct net
*net
, struct notifier_block
*nb
)
334 static unsigned int ip6mr_rules_seq_read(struct net
*net
)
340 static int ip6mr_hash_cmp(struct rhashtable_compare_arg
*arg
,
343 const struct mfc6_cache_cmp_arg
*cmparg
= arg
->key
;
344 struct mfc6_cache
*c
= (struct mfc6_cache
*)ptr
;
346 return !ipv6_addr_equal(&c
->mf6c_mcastgrp
, &cmparg
->mf6c_mcastgrp
) ||
347 !ipv6_addr_equal(&c
->mf6c_origin
, &cmparg
->mf6c_origin
);
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
),
356 .obj_cmpfn
= ip6mr_hash_cmp
,
357 .automatic_shrinking
= true,
360 static void ip6mr_new_table_set(struct mr_table
*mrt
,
363 #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
364 list_add_tail_rcu(&mrt
->list
, &net
->ipv6
.mr6_tables
);
368 static struct mfc6_cache_cmp_arg ip6mr_mr_table_ops_cmparg_any
= {
369 .mf6c_origin
= IN6ADDR_ANY_INIT
,
370 .mf6c_mcastgrp
= IN6ADDR_ANY_INIT
,
373 static struct mr_table_ops ip6mr_mr_table_ops
= {
374 .rht_params
= &ip6mr_rht_params
,
375 .cmparg_any
= &ip6mr_mr_table_ops_cmparg_any
,
378 static struct mr_table
*ip6mr_new_table(struct net
*net
, u32 id
)
380 struct mr_table
*mrt
;
382 mrt
= ip6mr_get_table(net
, id
);
386 return mr_table_alloc(net
, id
, &ip6mr_mr_table_ops
,
387 ipmr_expire_process
, ip6mr_new_table_set
);
390 static void ip6mr_free_table(struct mr_table
*mrt
)
392 del_timer_sync(&mrt
->ipmr_expire_timer
);
393 mroute_clean_tables(mrt
, true);
394 rhltable_destroy(&mrt
->mfc_hash
);
398 #ifdef CONFIG_PROC_FS
399 /* The /proc interfaces to multicast routing
400 * /proc/ip6_mr_cache /proc/ip6_mr_vif
403 static void *ip6mr_vif_seq_start(struct seq_file
*seq
, loff_t
*pos
)
406 struct mr_vif_iter
*iter
= seq
->private;
407 struct net
*net
= seq_file_net(seq
);
408 struct mr_table
*mrt
;
410 mrt
= ip6mr_get_table(net
, RT6_TABLE_DFLT
);
412 return ERR_PTR(-ENOENT
);
416 read_lock(&mrt_lock
);
417 return mr_vif_seq_start(seq
, pos
);
420 static void ip6mr_vif_seq_stop(struct seq_file
*seq
, void *v
)
423 read_unlock(&mrt_lock
);
426 static int ip6mr_vif_seq_show(struct seq_file
*seq
, void *v
)
428 struct mr_vif_iter
*iter
= seq
->private;
429 struct mr_table
*mrt
= iter
->mrt
;
431 if (v
== SEQ_START_TOKEN
) {
433 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
435 const struct vif_device
*vif
= v
;
436 const char *name
= vif
->dev
? vif
->dev
->name
: "none";
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
,
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
,
455 static void *ipmr_mfc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
457 struct net
*net
= seq_file_net(seq
);
458 struct mr_table
*mrt
;
460 mrt
= ip6mr_get_table(net
, RT6_TABLE_DFLT
);
462 return ERR_PTR(-ENOENT
);
464 return mr_mfc_seq_start(seq
, pos
, mrt
, &mfc_unres_lock
);
467 static int ipmr_mfc_seq_show(struct seq_file
*seq
, void *v
)
471 if (v
== SEQ_START_TOKEN
) {
475 "Iif Pkts Bytes Wrong Oifs\n");
477 const struct mfc6_cache
*mfc
= v
;
478 const struct mr_mfc_iter
*it
= seq
->private;
479 struct mr_table
*mrt
= it
->mrt
;
481 seq_printf(seq
, "%pI6 %pI6 %-3hd",
482 &mfc
->mf6c_mcastgrp
, &mfc
->mf6c_origin
,
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)
496 mfc
->_c
.mfc_un
.res
.ttls
[n
]);
499 /* unresolved mfc_caches don't contain
500 * pkt, bytes and wrong_if values
502 seq_printf(seq
, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
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
,
517 #ifdef CONFIG_IPV6_PIMSM_V2
519 static int pim6_rcv(struct sk_buff
*skb
)
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
,
532 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(*encap
)))
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))))
544 /* check if the inner packet is destined to mcast group */
545 encap
= (struct ipv6hdr
*)(skb_transport_header(skb
) +
548 if (!ipv6_addr_is_multicast(&encap
->daddr
) ||
549 encap
->payload_len
== 0 ||
550 ntohs(encap
->payload_len
) + sizeof(*pim
) > skb
->len
)
553 if (ip6mr_fib_lookup(net
, &fl6
, &mrt
) < 0)
555 reg_vif_num
= mrt
->mroute_reg_vif_num
;
557 read_lock(&mrt_lock
);
558 if (reg_vif_num
>= 0)
559 reg_dev
= mrt
->vif_table
[reg_vif_num
].dev
;
562 read_unlock(&mrt_lock
);
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
;
573 skb_tunnel_rx(skb
, reg_dev
, dev_net(reg_dev
));
584 static const struct inet6_protocol pim6_protocol
= {
588 /* Service routines creating virtual interfaces: PIMREG */
590 static netdev_tx_t
reg_vif_xmit(struct sk_buff
*skb
,
591 struct net_device
*dev
)
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
,
602 err
= ip6mr_fib_lookup(net
, &fl6
, &mrt
);
608 read_lock(&mrt_lock
);
609 dev
->stats
.tx_bytes
+= skb
->len
;
610 dev
->stats
.tx_packets
++;
611 ip6mr_cache_report(mrt
, skb
, mrt
->mroute_reg_vif_num
, MRT6MSG_WHOLEPKT
);
612 read_unlock(&mrt_lock
);
617 static int reg_vif_get_iflink(const struct net_device
*dev
)
622 static const struct net_device_ops reg_vif_netdev_ops
= {
623 .ndo_start_xmit
= reg_vif_xmit
,
624 .ndo_get_iflink
= reg_vif_get_iflink
,
627 static void reg_vif_setup(struct net_device
*dev
)
629 dev
->type
= ARPHRD_PIMREG
;
630 dev
->mtu
= 1500 - sizeof(struct ipv6hdr
) - 8;
631 dev
->flags
= IFF_NOARP
;
632 dev
->netdev_ops
= ®_vif_netdev_ops
;
633 dev
->needs_free_netdev
= true;
634 dev
->features
|= NETIF_F_NETNS_LOCAL
;
637 static struct net_device
*ip6mr_reg_vif(struct net
*net
, struct mr_table
*mrt
)
639 struct net_device
*dev
;
642 if (mrt
->id
== RT6_TABLE_DFLT
)
643 sprintf(name
, "pim6reg");
645 sprintf(name
, "pim6reg%u", mrt
->id
);
647 dev
= alloc_netdev(0, name
, NET_NAME_UNKNOWN
, reg_vif_setup
);
651 dev_net_set(dev
, net
);
653 if (register_netdevice(dev
)) {
665 unregister_netdevice(dev
);
670 static int call_ip6mr_vif_entry_notifiers(struct net
*net
,
671 enum fib_event_type event_type
,
672 struct vif_device
*vif
,
673 mifi_t vif_index
, u32 tb_id
)
675 return mr_call_vif_notifiers(net
, RTNL_FAMILY_IP6MR
, event_type
,
676 vif
, vif_index
, tb_id
,
677 &net
->ipv6
.ipmr_seq
);
680 static int call_ip6mr_mfc_entry_notifiers(struct net
*net
,
681 enum fib_event_type event_type
,
682 struct mfc6_cache
*mfc
, u32 tb_id
)
684 return mr_call_mfc_notifiers(net
, RTNL_FAMILY_IP6MR
, event_type
,
685 &mfc
->_c
, tb_id
, &net
->ipv6
.ipmr_seq
);
688 /* Delete a VIF entry */
689 static int mif6_delete(struct mr_table
*mrt
, int vifi
, int notify
,
690 struct list_head
*head
)
692 struct vif_device
*v
;
693 struct net_device
*dev
;
694 struct inet6_dev
*in6_dev
;
696 if (vifi
< 0 || vifi
>= mrt
->maxvif
)
697 return -EADDRNOTAVAIL
;
699 v
= &mrt
->vif_table
[vifi
];
701 if (VIF_EXISTS(mrt
, vifi
))
702 call_ip6mr_vif_entry_notifiers(read_pnet(&mrt
->net
),
703 FIB_EVENT_VIF_DEL
, v
, vifi
,
706 write_lock_bh(&mrt_lock
);
711 write_unlock_bh(&mrt_lock
);
712 return -EADDRNOTAVAIL
;
715 #ifdef CONFIG_IPV6_PIMSM_V2
716 if (vifi
== mrt
->mroute_reg_vif_num
)
717 mrt
->mroute_reg_vif_num
= -1;
720 if (vifi
+ 1 == mrt
->maxvif
) {
722 for (tmp
= vifi
- 1; tmp
>= 0; tmp
--) {
723 if (VIF_EXISTS(mrt
, tmp
))
726 mrt
->maxvif
= tmp
+ 1;
729 write_unlock_bh(&mrt_lock
);
731 dev_set_allmulti(dev
, -1);
733 in6_dev
= __in6_dev_get(dev
);
735 in6_dev
->cnf
.mc_forwarding
--;
736 inet6_netconf_notify_devconf(dev_net(dev
), RTM_NEWNETCONF
,
737 NETCONFA_MC_FORWARDING
,
738 dev
->ifindex
, &in6_dev
->cnf
);
741 if ((v
->flags
& MIFF_REGISTER
) && !notify
)
742 unregister_netdevice_queue(dev
, head
);
748 static inline void ip6mr_cache_free_rcu(struct rcu_head
*head
)
750 struct mr_mfc
*c
= container_of(head
, struct mr_mfc
, rcu
);
752 kmem_cache_free(mrt_cachep
, (struct mfc6_cache
*)c
);
755 static inline void ip6mr_cache_free(struct mfc6_cache
*c
)
757 call_rcu(&c
->_c
.rcu
, ip6mr_cache_free_rcu
);
760 /* Destroy an unresolved cache entry, killing queued skbs
761 and reporting error to netlink readers.
764 static void ip6mr_destroy_unres(struct mr_table
*mrt
, struct mfc6_cache
*c
)
766 struct net
*net
= read_pnet(&mrt
->net
);
769 atomic_dec(&mrt
->cache_resolve_queue_len
);
771 while ((skb
= skb_dequeue(&c
->_c
.mfc_un
.unres
.unresolved
)) != NULL
) {
772 if (ipv6_hdr(skb
)->version
== 0) {
773 struct nlmsghdr
*nlh
= skb_pull(skb
,
774 sizeof(struct ipv6hdr
));
775 nlh
->nlmsg_type
= NLMSG_ERROR
;
776 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
777 skb_trim(skb
, nlh
->nlmsg_len
);
778 ((struct nlmsgerr
*)nlmsg_data(nlh
))->error
= -ETIMEDOUT
;
779 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
788 /* Timer process for all the unresolved queue. */
790 static void ipmr_do_expire_process(struct mr_table
*mrt
)
792 unsigned long now
= jiffies
;
793 unsigned long expires
= 10 * HZ
;
794 struct mr_mfc
*c
, *next
;
796 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
797 if (time_after(c
->mfc_un
.unres
.expires
, now
)) {
799 unsigned long interval
= c
->mfc_un
.unres
.expires
- now
;
800 if (interval
< expires
)
806 mr6_netlink_event(mrt
, (struct mfc6_cache
*)c
, RTM_DELROUTE
);
807 ip6mr_destroy_unres(mrt
, (struct mfc6_cache
*)c
);
810 if (!list_empty(&mrt
->mfc_unres_queue
))
811 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+ expires
);
814 static void ipmr_expire_process(struct timer_list
*t
)
816 struct mr_table
*mrt
= from_timer(mrt
, t
, ipmr_expire_timer
);
818 if (!spin_trylock(&mfc_unres_lock
)) {
819 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+ 1);
823 if (!list_empty(&mrt
->mfc_unres_queue
))
824 ipmr_do_expire_process(mrt
);
826 spin_unlock(&mfc_unres_lock
);
829 /* Fill oifs list. It is called under write locked mrt_lock. */
831 static void ip6mr_update_thresholds(struct mr_table
*mrt
,
832 struct mr_mfc
*cache
,
837 cache
->mfc_un
.res
.minvif
= MAXMIFS
;
838 cache
->mfc_un
.res
.maxvif
= 0;
839 memset(cache
->mfc_un
.res
.ttls
, 255, MAXMIFS
);
841 for (vifi
= 0; vifi
< mrt
->maxvif
; vifi
++) {
842 if (VIF_EXISTS(mrt
, vifi
) &&
843 ttls
[vifi
] && ttls
[vifi
] < 255) {
844 cache
->mfc_un
.res
.ttls
[vifi
] = ttls
[vifi
];
845 if (cache
->mfc_un
.res
.minvif
> vifi
)
846 cache
->mfc_un
.res
.minvif
= vifi
;
847 if (cache
->mfc_un
.res
.maxvif
<= vifi
)
848 cache
->mfc_un
.res
.maxvif
= vifi
+ 1;
851 cache
->mfc_un
.res
.lastuse
= jiffies
;
854 static int mif6_add(struct net
*net
, struct mr_table
*mrt
,
855 struct mif6ctl
*vifc
, int mrtsock
)
857 int vifi
= vifc
->mif6c_mifi
;
858 struct vif_device
*v
= &mrt
->vif_table
[vifi
];
859 struct net_device
*dev
;
860 struct inet6_dev
*in6_dev
;
864 if (VIF_EXISTS(mrt
, vifi
))
867 switch (vifc
->mif6c_flags
) {
868 #ifdef CONFIG_IPV6_PIMSM_V2
871 * Special Purpose VIF in PIM
872 * All the packets will be sent to the daemon
874 if (mrt
->mroute_reg_vif_num
>= 0)
876 dev
= ip6mr_reg_vif(net
, mrt
);
879 err
= dev_set_allmulti(dev
, 1);
881 unregister_netdevice(dev
);
888 dev
= dev_get_by_index(net
, vifc
->mif6c_pifi
);
890 return -EADDRNOTAVAIL
;
891 err
= dev_set_allmulti(dev
, 1);
901 in6_dev
= __in6_dev_get(dev
);
903 in6_dev
->cnf
.mc_forwarding
++;
904 inet6_netconf_notify_devconf(dev_net(dev
), RTM_NEWNETCONF
,
905 NETCONFA_MC_FORWARDING
,
906 dev
->ifindex
, &in6_dev
->cnf
);
909 /* Fill in the VIF structures */
910 vif_device_init(v
, dev
, vifc
->vifc_rate_limit
, vifc
->vifc_threshold
,
911 vifc
->mif6c_flags
| (!mrtsock
? VIFF_STATIC
: 0),
914 /* And finish update writing critical data */
915 write_lock_bh(&mrt_lock
);
917 #ifdef CONFIG_IPV6_PIMSM_V2
918 if (v
->flags
& MIFF_REGISTER
)
919 mrt
->mroute_reg_vif_num
= vifi
;
921 if (vifi
+ 1 > mrt
->maxvif
)
922 mrt
->maxvif
= vifi
+ 1;
923 write_unlock_bh(&mrt_lock
);
924 call_ip6mr_vif_entry_notifiers(net
, FIB_EVENT_VIF_ADD
,
929 static struct mfc6_cache
*ip6mr_cache_find(struct mr_table
*mrt
,
930 const struct in6_addr
*origin
,
931 const struct in6_addr
*mcastgrp
)
933 struct mfc6_cache_cmp_arg arg
= {
934 .mf6c_origin
= *origin
,
935 .mf6c_mcastgrp
= *mcastgrp
,
938 return mr_mfc_find(mrt
, &arg
);
941 /* Look for a (*,G) entry */
942 static struct mfc6_cache
*ip6mr_cache_find_any(struct mr_table
*mrt
,
943 struct in6_addr
*mcastgrp
,
946 struct mfc6_cache_cmp_arg arg
= {
947 .mf6c_origin
= in6addr_any
,
948 .mf6c_mcastgrp
= *mcastgrp
,
951 if (ipv6_addr_any(mcastgrp
))
952 return mr_mfc_find_any_parent(mrt
, mifi
);
953 return mr_mfc_find_any(mrt
, mifi
, &arg
);
956 /* Look for a (S,G,iif) entry if parent != -1 */
957 static struct mfc6_cache
*
958 ip6mr_cache_find_parent(struct mr_table
*mrt
,
959 const struct in6_addr
*origin
,
960 const struct in6_addr
*mcastgrp
,
963 struct mfc6_cache_cmp_arg arg
= {
964 .mf6c_origin
= *origin
,
965 .mf6c_mcastgrp
= *mcastgrp
,
968 return mr_mfc_find_parent(mrt
, &arg
, parent
);
971 /* Allocate a multicast cache entry */
972 static struct mfc6_cache
*ip6mr_cache_alloc(void)
974 struct mfc6_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_KERNEL
);
977 c
->_c
.mfc_un
.res
.last_assert
= jiffies
- MFC_ASSERT_THRESH
- 1;
978 c
->_c
.mfc_un
.res
.minvif
= MAXMIFS
;
979 c
->_c
.free
= ip6mr_cache_free_rcu
;
980 refcount_set(&c
->_c
.mfc_un
.res
.refcount
, 1);
984 static struct mfc6_cache
*ip6mr_cache_alloc_unres(void)
986 struct mfc6_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_ATOMIC
);
989 skb_queue_head_init(&c
->_c
.mfc_un
.unres
.unresolved
);
990 c
->_c
.mfc_un
.unres
.expires
= jiffies
+ 10 * HZ
;
995 * A cache entry has gone into a resolved state from queued
998 static void ip6mr_cache_resolve(struct net
*net
, struct mr_table
*mrt
,
999 struct mfc6_cache
*uc
, struct mfc6_cache
*c
)
1001 struct sk_buff
*skb
;
1004 * Play the pending entries through our router
1007 while ((skb
= __skb_dequeue(&uc
->_c
.mfc_un
.unres
.unresolved
))) {
1008 if (ipv6_hdr(skb
)->version
== 0) {
1009 struct nlmsghdr
*nlh
= skb_pull(skb
,
1010 sizeof(struct ipv6hdr
));
1012 if (mr_fill_mroute(mrt
, skb
, &c
->_c
,
1013 nlmsg_data(nlh
)) > 0) {
1014 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - (u8
*)nlh
;
1016 nlh
->nlmsg_type
= NLMSG_ERROR
;
1017 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
1018 skb_trim(skb
, nlh
->nlmsg_len
);
1019 ((struct nlmsgerr
*)nlmsg_data(nlh
))->error
= -EMSGSIZE
;
1021 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
1023 ip6_mr_forward(net
, mrt
, skb
->dev
, skb
, c
);
1028 * Bounce a cache query up to pim6sd and netlink.
1030 * Called under mrt_lock.
1033 static int ip6mr_cache_report(struct mr_table
*mrt
, struct sk_buff
*pkt
,
1034 mifi_t mifi
, int assert)
1036 struct sock
*mroute6_sk
;
1037 struct sk_buff
*skb
;
1038 struct mrt6msg
*msg
;
1041 #ifdef CONFIG_IPV6_PIMSM_V2
1042 if (assert == MRT6MSG_WHOLEPKT
)
1043 skb
= skb_realloc_headroom(pkt
, -skb_network_offset(pkt
)
1047 skb
= alloc_skb(sizeof(struct ipv6hdr
) + sizeof(*msg
), GFP_ATOMIC
);
1052 /* I suppose that internal messages
1053 * do not require checksums */
1055 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1057 #ifdef CONFIG_IPV6_PIMSM_V2
1058 if (assert == MRT6MSG_WHOLEPKT
) {
1059 /* Ugly, but we have no choice with this interface.
1060 Duplicate old header, fix length etc.
1061 And all this only to mangle msg->im6_msgtype and
1062 to set msg->im6_mbz to "mbz" :-)
1064 skb_push(skb
, -skb_network_offset(pkt
));
1066 skb_push(skb
, sizeof(*msg
));
1067 skb_reset_transport_header(skb
);
1068 msg
= (struct mrt6msg
*)skb_transport_header(skb
);
1070 msg
->im6_msgtype
= MRT6MSG_WHOLEPKT
;
1071 msg
->im6_mif
= mrt
->mroute_reg_vif_num
;
1073 msg
->im6_src
= ipv6_hdr(pkt
)->saddr
;
1074 msg
->im6_dst
= ipv6_hdr(pkt
)->daddr
;
1076 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1081 * Copy the IP header
1084 skb_put(skb
, sizeof(struct ipv6hdr
));
1085 skb_reset_network_header(skb
);
1086 skb_copy_to_linear_data(skb
, ipv6_hdr(pkt
), sizeof(struct ipv6hdr
));
1091 skb_put(skb
, sizeof(*msg
));
1092 skb_reset_transport_header(skb
);
1093 msg
= (struct mrt6msg
*)skb_transport_header(skb
);
1096 msg
->im6_msgtype
= assert;
1097 msg
->im6_mif
= mifi
;
1099 msg
->im6_src
= ipv6_hdr(pkt
)->saddr
;
1100 msg
->im6_dst
= ipv6_hdr(pkt
)->daddr
;
1102 skb_dst_set(skb
, dst_clone(skb_dst(pkt
)));
1103 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1107 mroute6_sk
= rcu_dereference(mrt
->mroute_sk
);
1114 mrt6msg_netlink_event(mrt
, skb
);
1116 /* Deliver to user space multicast routing algorithms */
1117 ret
= sock_queue_rcv_skb(mroute6_sk
, skb
);
1120 net_warn_ratelimited("mroute6: pending queue full, dropping entries\n");
1127 /* Queue a packet for resolution. It gets locked cache entry! */
1128 static int ip6mr_cache_unresolved(struct mr_table
*mrt
, mifi_t mifi
,
1129 struct sk_buff
*skb
, struct net_device
*dev
)
1131 struct mfc6_cache
*c
;
1135 spin_lock_bh(&mfc_unres_lock
);
1136 list_for_each_entry(c
, &mrt
->mfc_unres_queue
, _c
.list
) {
1137 if (ipv6_addr_equal(&c
->mf6c_mcastgrp
, &ipv6_hdr(skb
)->daddr
) &&
1138 ipv6_addr_equal(&c
->mf6c_origin
, &ipv6_hdr(skb
)->saddr
)) {
1146 * Create a new entry if allowable
1149 if (atomic_read(&mrt
->cache_resolve_queue_len
) >= 10 ||
1150 (c
= ip6mr_cache_alloc_unres()) == NULL
) {
1151 spin_unlock_bh(&mfc_unres_lock
);
1157 /* Fill in the new cache entry */
1158 c
->_c
.mfc_parent
= -1;
1159 c
->mf6c_origin
= ipv6_hdr(skb
)->saddr
;
1160 c
->mf6c_mcastgrp
= ipv6_hdr(skb
)->daddr
;
1163 * Reflect first query at pim6sd
1165 err
= ip6mr_cache_report(mrt
, skb
, mifi
, MRT6MSG_NOCACHE
);
1167 /* If the report failed throw the cache entry
1170 spin_unlock_bh(&mfc_unres_lock
);
1172 ip6mr_cache_free(c
);
1177 atomic_inc(&mrt
->cache_resolve_queue_len
);
1178 list_add(&c
->_c
.list
, &mrt
->mfc_unres_queue
);
1179 mr6_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1181 ipmr_do_expire_process(mrt
);
1184 /* See if we can append the packet */
1185 if (c
->_c
.mfc_un
.unres
.unresolved
.qlen
> 3) {
1191 skb
->skb_iif
= dev
->ifindex
;
1193 skb_queue_tail(&c
->_c
.mfc_un
.unres
.unresolved
, skb
);
1197 spin_unlock_bh(&mfc_unres_lock
);
1202 * MFC6 cache manipulation by user space
1205 static int ip6mr_mfc_delete(struct mr_table
*mrt
, struct mf6cctl
*mfc
,
1208 struct mfc6_cache
*c
;
1210 /* The entries are added/deleted only under RTNL */
1212 c
= ip6mr_cache_find_parent(mrt
, &mfc
->mf6cc_origin
.sin6_addr
,
1213 &mfc
->mf6cc_mcastgrp
.sin6_addr
, parent
);
1217 rhltable_remove(&mrt
->mfc_hash
, &c
->_c
.mnode
, ip6mr_rht_params
);
1218 list_del_rcu(&c
->_c
.list
);
1220 call_ip6mr_mfc_entry_notifiers(read_pnet(&mrt
->net
),
1221 FIB_EVENT_ENTRY_DEL
, c
, mrt
->id
);
1222 mr6_netlink_event(mrt
, c
, RTM_DELROUTE
);
1223 mr_cache_put(&c
->_c
);
1227 static int ip6mr_device_event(struct notifier_block
*this,
1228 unsigned long event
, void *ptr
)
1230 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1231 struct net
*net
= dev_net(dev
);
1232 struct mr_table
*mrt
;
1233 struct vif_device
*v
;
1236 if (event
!= NETDEV_UNREGISTER
)
1239 ip6mr_for_each_table(mrt
, net
) {
1240 v
= &mrt
->vif_table
[0];
1241 for (ct
= 0; ct
< mrt
->maxvif
; ct
++, v
++) {
1243 mif6_delete(mrt
, ct
, 1, NULL
);
1250 static unsigned int ip6mr_seq_read(struct net
*net
)
1254 return net
->ipv6
.ipmr_seq
+ ip6mr_rules_seq_read(net
);
1257 static int ip6mr_dump(struct net
*net
, struct notifier_block
*nb
)
1259 return mr_dump(net
, nb
, RTNL_FAMILY_IP6MR
, ip6mr_rules_dump
,
1260 ip6mr_mr_table_iter
, &mrt_lock
);
1263 static struct notifier_block ip6_mr_notifier
= {
1264 .notifier_call
= ip6mr_device_event
1267 static const struct fib_notifier_ops ip6mr_notifier_ops_template
= {
1268 .family
= RTNL_FAMILY_IP6MR
,
1269 .fib_seq_read
= ip6mr_seq_read
,
1270 .fib_dump
= ip6mr_dump
,
1271 .owner
= THIS_MODULE
,
1274 static int __net_init
ip6mr_notifier_init(struct net
*net
)
1276 struct fib_notifier_ops
*ops
;
1278 net
->ipv6
.ipmr_seq
= 0;
1280 ops
= fib_notifier_ops_register(&ip6mr_notifier_ops_template
, net
);
1282 return PTR_ERR(ops
);
1284 net
->ipv6
.ip6mr_notifier_ops
= ops
;
1289 static void __net_exit
ip6mr_notifier_exit(struct net
*net
)
1291 fib_notifier_ops_unregister(net
->ipv6
.ip6mr_notifier_ops
);
1292 net
->ipv6
.ip6mr_notifier_ops
= NULL
;
1295 /* Setup for IP multicast routing */
1296 static int __net_init
ip6mr_net_init(struct net
*net
)
1300 err
= ip6mr_notifier_init(net
);
1304 err
= ip6mr_rules_init(net
);
1306 goto ip6mr_rules_fail
;
1308 #ifdef CONFIG_PROC_FS
1310 if (!proc_create_net("ip6_mr_vif", 0, net
->proc_net
, &ip6mr_vif_seq_ops
,
1311 sizeof(struct mr_vif_iter
)))
1313 if (!proc_create_net("ip6_mr_cache", 0, net
->proc_net
, &ipmr_mfc_seq_ops
,
1314 sizeof(struct mr_mfc_iter
)))
1315 goto proc_cache_fail
;
1320 #ifdef CONFIG_PROC_FS
1322 remove_proc_entry("ip6_mr_vif", net
->proc_net
);
1324 ip6mr_rules_exit(net
);
1327 ip6mr_notifier_exit(net
);
1331 static void __net_exit
ip6mr_net_exit(struct net
*net
)
1333 #ifdef CONFIG_PROC_FS
1334 remove_proc_entry("ip6_mr_cache", net
->proc_net
);
1335 remove_proc_entry("ip6_mr_vif", net
->proc_net
);
1337 ip6mr_rules_exit(net
);
1338 ip6mr_notifier_exit(net
);
1341 static struct pernet_operations ip6mr_net_ops
= {
1342 .init
= ip6mr_net_init
,
1343 .exit
= ip6mr_net_exit
,
1346 int __init
ip6_mr_init(void)
1350 mrt_cachep
= kmem_cache_create("ip6_mrt_cache",
1351 sizeof(struct mfc6_cache
),
1352 0, SLAB_HWCACHE_ALIGN
,
1357 err
= register_pernet_subsys(&ip6mr_net_ops
);
1359 goto reg_pernet_fail
;
1361 err
= register_netdevice_notifier(&ip6_mr_notifier
);
1363 goto reg_notif_fail
;
1364 #ifdef CONFIG_IPV6_PIMSM_V2
1365 if (inet6_add_protocol(&pim6_protocol
, IPPROTO_PIM
) < 0) {
1366 pr_err("%s: can't add PIM protocol\n", __func__
);
1368 goto add_proto_fail
;
1371 err
= rtnl_register_module(THIS_MODULE
, RTNL_FAMILY_IP6MR
, RTM_GETROUTE
,
1372 NULL
, ip6mr_rtm_dumproute
, 0);
1376 #ifdef CONFIG_IPV6_PIMSM_V2
1377 inet6_del_protocol(&pim6_protocol
, IPPROTO_PIM
);
1379 unregister_netdevice_notifier(&ip6_mr_notifier
);
1382 unregister_pernet_subsys(&ip6mr_net_ops
);
1384 kmem_cache_destroy(mrt_cachep
);
1388 void ip6_mr_cleanup(void)
1390 rtnl_unregister(RTNL_FAMILY_IP6MR
, RTM_GETROUTE
);
1391 #ifdef CONFIG_IPV6_PIMSM_V2
1392 inet6_del_protocol(&pim6_protocol
, IPPROTO_PIM
);
1394 unregister_netdevice_notifier(&ip6_mr_notifier
);
1395 unregister_pernet_subsys(&ip6mr_net_ops
);
1396 kmem_cache_destroy(mrt_cachep
);
1399 static int ip6mr_mfc_add(struct net
*net
, struct mr_table
*mrt
,
1400 struct mf6cctl
*mfc
, int mrtsock
, int parent
)
1402 unsigned char ttls
[MAXMIFS
];
1403 struct mfc6_cache
*uc
, *c
;
1408 if (mfc
->mf6cc_parent
>= MAXMIFS
)
1411 memset(ttls
, 255, MAXMIFS
);
1412 for (i
= 0; i
< MAXMIFS
; i
++) {
1413 if (IF_ISSET(i
, &mfc
->mf6cc_ifset
))
1417 /* The entries are added/deleted only under RTNL */
1419 c
= ip6mr_cache_find_parent(mrt
, &mfc
->mf6cc_origin
.sin6_addr
,
1420 &mfc
->mf6cc_mcastgrp
.sin6_addr
, parent
);
1423 write_lock_bh(&mrt_lock
);
1424 c
->_c
.mfc_parent
= mfc
->mf6cc_parent
;
1425 ip6mr_update_thresholds(mrt
, &c
->_c
, ttls
);
1427 c
->_c
.mfc_flags
|= MFC_STATIC
;
1428 write_unlock_bh(&mrt_lock
);
1429 call_ip6mr_mfc_entry_notifiers(net
, FIB_EVENT_ENTRY_REPLACE
,
1431 mr6_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1435 if (!ipv6_addr_any(&mfc
->mf6cc_mcastgrp
.sin6_addr
) &&
1436 !ipv6_addr_is_multicast(&mfc
->mf6cc_mcastgrp
.sin6_addr
))
1439 c
= ip6mr_cache_alloc();
1443 c
->mf6c_origin
= mfc
->mf6cc_origin
.sin6_addr
;
1444 c
->mf6c_mcastgrp
= mfc
->mf6cc_mcastgrp
.sin6_addr
;
1445 c
->_c
.mfc_parent
= mfc
->mf6cc_parent
;
1446 ip6mr_update_thresholds(mrt
, &c
->_c
, ttls
);
1448 c
->_c
.mfc_flags
|= MFC_STATIC
;
1450 err
= rhltable_insert_key(&mrt
->mfc_hash
, &c
->cmparg
, &c
->_c
.mnode
,
1453 pr_err("ip6mr: rhtable insert error %d\n", err
);
1454 ip6mr_cache_free(c
);
1457 list_add_tail_rcu(&c
->_c
.list
, &mrt
->mfc_cache_list
);
1459 /* Check to see if we resolved a queued list. If so we
1460 * need to send on the frames and tidy up.
1463 spin_lock_bh(&mfc_unres_lock
);
1464 list_for_each_entry(_uc
, &mrt
->mfc_unres_queue
, list
) {
1465 uc
= (struct mfc6_cache
*)_uc
;
1466 if (ipv6_addr_equal(&uc
->mf6c_origin
, &c
->mf6c_origin
) &&
1467 ipv6_addr_equal(&uc
->mf6c_mcastgrp
, &c
->mf6c_mcastgrp
)) {
1468 list_del(&_uc
->list
);
1469 atomic_dec(&mrt
->cache_resolve_queue_len
);
1474 if (list_empty(&mrt
->mfc_unres_queue
))
1475 del_timer(&mrt
->ipmr_expire_timer
);
1476 spin_unlock_bh(&mfc_unres_lock
);
1479 ip6mr_cache_resolve(net
, mrt
, uc
, c
);
1480 ip6mr_cache_free(uc
);
1482 call_ip6mr_mfc_entry_notifiers(net
, FIB_EVENT_ENTRY_ADD
,
1484 mr6_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1489 * Close the multicast socket, and clear the vif tables etc
1492 static void mroute_clean_tables(struct mr_table
*mrt
, bool all
)
1494 struct mr_mfc
*c
, *tmp
;
1498 /* Shut down all active vif entries */
1499 for (i
= 0; i
< mrt
->maxvif
; i
++) {
1500 if (!all
&& (mrt
->vif_table
[i
].flags
& VIFF_STATIC
))
1502 mif6_delete(mrt
, i
, 0, &list
);
1504 unregister_netdevice_many(&list
);
1506 /* Wipe the cache */
1507 list_for_each_entry_safe(c
, tmp
, &mrt
->mfc_cache_list
, list
) {
1508 if (!all
&& (c
->mfc_flags
& MFC_STATIC
))
1510 rhltable_remove(&mrt
->mfc_hash
, &c
->mnode
, ip6mr_rht_params
);
1511 list_del_rcu(&c
->list
);
1512 mr6_netlink_event(mrt
, (struct mfc6_cache
*)c
, RTM_DELROUTE
);
1516 if (atomic_read(&mrt
->cache_resolve_queue_len
) != 0) {
1517 spin_lock_bh(&mfc_unres_lock
);
1518 list_for_each_entry_safe(c
, tmp
, &mrt
->mfc_unres_queue
, list
) {
1520 call_ip6mr_mfc_entry_notifiers(read_pnet(&mrt
->net
),
1521 FIB_EVENT_ENTRY_DEL
,
1522 (struct mfc6_cache
*)c
,
1524 mr6_netlink_event(mrt
, (struct mfc6_cache
*)c
,
1526 ip6mr_destroy_unres(mrt
, (struct mfc6_cache
*)c
);
1528 spin_unlock_bh(&mfc_unres_lock
);
1532 static int ip6mr_sk_init(struct mr_table
*mrt
, struct sock
*sk
)
1535 struct net
*net
= sock_net(sk
);
1538 write_lock_bh(&mrt_lock
);
1539 if (rtnl_dereference(mrt
->mroute_sk
)) {
1542 rcu_assign_pointer(mrt
->mroute_sk
, sk
);
1543 sock_set_flag(sk
, SOCK_RCU_FREE
);
1544 net
->ipv6
.devconf_all
->mc_forwarding
++;
1546 write_unlock_bh(&mrt_lock
);
1549 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
1550 NETCONFA_MC_FORWARDING
,
1551 NETCONFA_IFINDEX_ALL
,
1552 net
->ipv6
.devconf_all
);
1558 int ip6mr_sk_done(struct sock
*sk
)
1561 struct net
*net
= sock_net(sk
);
1562 struct mr_table
*mrt
;
1564 if (sk
->sk_type
!= SOCK_RAW
||
1565 inet_sk(sk
)->inet_num
!= IPPROTO_ICMPV6
)
1569 ip6mr_for_each_table(mrt
, net
) {
1570 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1571 write_lock_bh(&mrt_lock
);
1572 RCU_INIT_POINTER(mrt
->mroute_sk
, NULL
);
1573 /* Note that mroute_sk had SOCK_RCU_FREE set,
1574 * so the RCU grace period before sk freeing
1575 * is guaranteed by sk_destruct()
1577 net
->ipv6
.devconf_all
->mc_forwarding
--;
1578 write_unlock_bh(&mrt_lock
);
1579 inet6_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
1580 NETCONFA_MC_FORWARDING
,
1581 NETCONFA_IFINDEX_ALL
,
1582 net
->ipv6
.devconf_all
);
1584 mroute_clean_tables(mrt
, false);
1594 bool mroute6_is_socket(struct net
*net
, struct sk_buff
*skb
)
1596 struct mr_table
*mrt
;
1597 struct flowi6 fl6
= {
1598 .flowi6_iif
= skb
->skb_iif
? : LOOPBACK_IFINDEX
,
1599 .flowi6_oif
= skb
->dev
->ifindex
,
1600 .flowi6_mark
= skb
->mark
,
1603 if (ip6mr_fib_lookup(net
, &fl6
, &mrt
) < 0)
1606 return rcu_access_pointer(mrt
->mroute_sk
);
1608 EXPORT_SYMBOL(mroute6_is_socket
);
1611 * Socket options and virtual interface manipulation. The whole
1612 * virtual interface system is a complete heap, but unfortunately
1613 * that's how BSD mrouted happens to think. Maybe one day with a proper
1614 * MOSPF/PIM router set up we can clean this up.
1617 int ip6_mroute_setsockopt(struct sock
*sk
, int optname
, char __user
*optval
, unsigned int optlen
)
1619 int ret
, parent
= 0;
1623 struct net
*net
= sock_net(sk
);
1624 struct mr_table
*mrt
;
1626 if (sk
->sk_type
!= SOCK_RAW
||
1627 inet_sk(sk
)->inet_num
!= IPPROTO_ICMPV6
)
1630 mrt
= ip6mr_get_table(net
, raw6_sk(sk
)->ip6mr_table
? : RT6_TABLE_DFLT
);
1634 if (optname
!= MRT6_INIT
) {
1635 if (sk
!= rcu_access_pointer(mrt
->mroute_sk
) &&
1636 !ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1642 if (optlen
< sizeof(int))
1645 return ip6mr_sk_init(mrt
, sk
);
1648 return ip6mr_sk_done(sk
);
1651 if (optlen
< sizeof(vif
))
1653 if (copy_from_user(&vif
, optval
, sizeof(vif
)))
1655 if (vif
.mif6c_mifi
>= MAXMIFS
)
1658 ret
= mif6_add(net
, mrt
, &vif
,
1659 sk
== rtnl_dereference(mrt
->mroute_sk
));
1664 if (optlen
< sizeof(mifi_t
))
1666 if (copy_from_user(&mifi
, optval
, sizeof(mifi_t
)))
1669 ret
= mif6_delete(mrt
, mifi
, 0, NULL
);
1674 * Manipulate the forwarding caches. These live
1675 * in a sort of kernel/user symbiosis.
1681 case MRT6_ADD_MFC_PROXY
:
1682 case MRT6_DEL_MFC_PROXY
:
1683 if (optlen
< sizeof(mfc
))
1685 if (copy_from_user(&mfc
, optval
, sizeof(mfc
)))
1688 parent
= mfc
.mf6cc_parent
;
1690 if (optname
== MRT6_DEL_MFC
|| optname
== MRT6_DEL_MFC_PROXY
)
1691 ret
= ip6mr_mfc_delete(mrt
, &mfc
, parent
);
1693 ret
= ip6mr_mfc_add(net
, mrt
, &mfc
,
1695 rtnl_dereference(mrt
->mroute_sk
),
1701 * Control PIM assert (to activate pim will activate assert)
1707 if (optlen
!= sizeof(v
))
1709 if (get_user(v
, (int __user
*)optval
))
1711 mrt
->mroute_do_assert
= v
;
1715 #ifdef CONFIG_IPV6_PIMSM_V2
1720 if (optlen
!= sizeof(v
))
1722 if (get_user(v
, (int __user
*)optval
))
1727 if (v
!= mrt
->mroute_do_pim
) {
1728 mrt
->mroute_do_pim
= v
;
1729 mrt
->mroute_do_assert
= v
;
1736 #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
1741 if (optlen
!= sizeof(u32
))
1743 if (get_user(v
, (u32 __user
*)optval
))
1745 /* "pim6reg%u" should not exceed 16 bytes (IFNAMSIZ) */
1746 if (v
!= RT_TABLE_DEFAULT
&& v
>= 100000000)
1748 if (sk
== rcu_access_pointer(mrt
->mroute_sk
))
1753 mrt
= ip6mr_new_table(net
, v
);
1757 raw6_sk(sk
)->ip6mr_table
= v
;
1763 * Spurious command, or MRT6_VERSION which you cannot
1767 return -ENOPROTOOPT
;
1772 * Getsock opt support for the multicast routing system.
1775 int ip6_mroute_getsockopt(struct sock
*sk
, int optname
, char __user
*optval
,
1780 struct net
*net
= sock_net(sk
);
1781 struct mr_table
*mrt
;
1783 if (sk
->sk_type
!= SOCK_RAW
||
1784 inet_sk(sk
)->inet_num
!= IPPROTO_ICMPV6
)
1787 mrt
= ip6mr_get_table(net
, raw6_sk(sk
)->ip6mr_table
? : RT6_TABLE_DFLT
);
1795 #ifdef CONFIG_IPV6_PIMSM_V2
1797 val
= mrt
->mroute_do_pim
;
1801 val
= mrt
->mroute_do_assert
;
1804 return -ENOPROTOOPT
;
1807 if (get_user(olr
, optlen
))
1810 olr
= min_t(int, olr
, sizeof(int));
1814 if (put_user(olr
, optlen
))
1816 if (copy_to_user(optval
, &val
, olr
))
1822 * The IP multicast ioctl support routines.
1825 int ip6mr_ioctl(struct sock
*sk
, int cmd
, void __user
*arg
)
1827 struct sioc_sg_req6 sr
;
1828 struct sioc_mif_req6 vr
;
1829 struct vif_device
*vif
;
1830 struct mfc6_cache
*c
;
1831 struct net
*net
= sock_net(sk
);
1832 struct mr_table
*mrt
;
1834 mrt
= ip6mr_get_table(net
, raw6_sk(sk
)->ip6mr_table
? : RT6_TABLE_DFLT
);
1839 case SIOCGETMIFCNT_IN6
:
1840 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1842 if (vr
.mifi
>= mrt
->maxvif
)
1844 read_lock(&mrt_lock
);
1845 vif
= &mrt
->vif_table
[vr
.mifi
];
1846 if (VIF_EXISTS(mrt
, vr
.mifi
)) {
1847 vr
.icount
= vif
->pkt_in
;
1848 vr
.ocount
= vif
->pkt_out
;
1849 vr
.ibytes
= vif
->bytes_in
;
1850 vr
.obytes
= vif
->bytes_out
;
1851 read_unlock(&mrt_lock
);
1853 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1857 read_unlock(&mrt_lock
);
1858 return -EADDRNOTAVAIL
;
1859 case SIOCGETSGCNT_IN6
:
1860 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1864 c
= ip6mr_cache_find(mrt
, &sr
.src
.sin6_addr
, &sr
.grp
.sin6_addr
);
1866 sr
.pktcnt
= c
->_c
.mfc_un
.res
.pkt
;
1867 sr
.bytecnt
= c
->_c
.mfc_un
.res
.bytes
;
1868 sr
.wrong_if
= c
->_c
.mfc_un
.res
.wrong_if
;
1871 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1876 return -EADDRNOTAVAIL
;
1878 return -ENOIOCTLCMD
;
1882 #ifdef CONFIG_COMPAT
1883 struct compat_sioc_sg_req6
{
1884 struct sockaddr_in6 src
;
1885 struct sockaddr_in6 grp
;
1886 compat_ulong_t pktcnt
;
1887 compat_ulong_t bytecnt
;
1888 compat_ulong_t wrong_if
;
1891 struct compat_sioc_mif_req6
{
1893 compat_ulong_t icount
;
1894 compat_ulong_t ocount
;
1895 compat_ulong_t ibytes
;
1896 compat_ulong_t obytes
;
1899 int ip6mr_compat_ioctl(struct sock
*sk
, unsigned int cmd
, void __user
*arg
)
1901 struct compat_sioc_sg_req6 sr
;
1902 struct compat_sioc_mif_req6 vr
;
1903 struct vif_device
*vif
;
1904 struct mfc6_cache
*c
;
1905 struct net
*net
= sock_net(sk
);
1906 struct mr_table
*mrt
;
1908 mrt
= ip6mr_get_table(net
, raw6_sk(sk
)->ip6mr_table
? : RT6_TABLE_DFLT
);
1913 case SIOCGETMIFCNT_IN6
:
1914 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1916 if (vr
.mifi
>= mrt
->maxvif
)
1918 read_lock(&mrt_lock
);
1919 vif
= &mrt
->vif_table
[vr
.mifi
];
1920 if (VIF_EXISTS(mrt
, vr
.mifi
)) {
1921 vr
.icount
= vif
->pkt_in
;
1922 vr
.ocount
= vif
->pkt_out
;
1923 vr
.ibytes
= vif
->bytes_in
;
1924 vr
.obytes
= vif
->bytes_out
;
1925 read_unlock(&mrt_lock
);
1927 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1931 read_unlock(&mrt_lock
);
1932 return -EADDRNOTAVAIL
;
1933 case SIOCGETSGCNT_IN6
:
1934 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1938 c
= ip6mr_cache_find(mrt
, &sr
.src
.sin6_addr
, &sr
.grp
.sin6_addr
);
1940 sr
.pktcnt
= c
->_c
.mfc_un
.res
.pkt
;
1941 sr
.bytecnt
= c
->_c
.mfc_un
.res
.bytes
;
1942 sr
.wrong_if
= c
->_c
.mfc_un
.res
.wrong_if
;
1945 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1950 return -EADDRNOTAVAIL
;
1952 return -ENOIOCTLCMD
;
1957 static inline int ip6mr_forward2_finish(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
1959 __IP6_INC_STATS(net
, ip6_dst_idev(skb_dst(skb
)),
1960 IPSTATS_MIB_OUTFORWDATAGRAMS
);
1961 __IP6_ADD_STATS(net
, ip6_dst_idev(skb_dst(skb
)),
1962 IPSTATS_MIB_OUTOCTETS
, skb
->len
);
1963 return dst_output(net
, sk
, skb
);
1967 * Processing handlers for ip6mr_forward
1970 static int ip6mr_forward2(struct net
*net
, struct mr_table
*mrt
,
1971 struct sk_buff
*skb
, struct mfc6_cache
*c
, int vifi
)
1973 struct ipv6hdr
*ipv6h
;
1974 struct vif_device
*vif
= &mrt
->vif_table
[vifi
];
1975 struct net_device
*dev
;
1976 struct dst_entry
*dst
;
1982 #ifdef CONFIG_IPV6_PIMSM_V2
1983 if (vif
->flags
& MIFF_REGISTER
) {
1985 vif
->bytes_out
+= skb
->len
;
1986 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1987 vif
->dev
->stats
.tx_packets
++;
1988 ip6mr_cache_report(mrt
, skb
, vifi
, MRT6MSG_WHOLEPKT
);
1993 ipv6h
= ipv6_hdr(skb
);
1995 fl6
= (struct flowi6
) {
1996 .flowi6_oif
= vif
->link
,
1997 .daddr
= ipv6h
->daddr
,
2000 dst
= ip6_route_output(net
, NULL
, &fl6
);
2007 skb_dst_set(skb
, dst
);
2010 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
2011 * not only before forwarding, but after forwarding on all output
2012 * interfaces. It is clear, if mrouter runs a multicasting
2013 * program, it should receive packets not depending to what interface
2014 * program is joined.
2015 * If we will not make it, the program will have to join on all
2016 * interfaces. On the other hand, multihoming host (or router, but
2017 * not mrouter) cannot join to more than one interface - it will
2018 * result in receiving multiple packets.
2023 vif
->bytes_out
+= skb
->len
;
2025 /* We are about to write */
2026 /* XXX: extension headers? */
2027 if (skb_cow(skb
, sizeof(*ipv6h
) + LL_RESERVED_SPACE(dev
)))
2030 ipv6h
= ipv6_hdr(skb
);
2033 IP6CB(skb
)->flags
|= IP6SKB_FORWARDED
;
2035 return NF_HOOK(NFPROTO_IPV6
, NF_INET_FORWARD
,
2036 net
, NULL
, skb
, skb
->dev
, dev
,
2037 ip6mr_forward2_finish
);
2044 static int ip6mr_find_vif(struct mr_table
*mrt
, struct net_device
*dev
)
2048 for (ct
= mrt
->maxvif
- 1; ct
>= 0; ct
--) {
2049 if (mrt
->vif_table
[ct
].dev
== dev
)
2055 static void ip6_mr_forward(struct net
*net
, struct mr_table
*mrt
,
2056 struct net_device
*dev
, struct sk_buff
*skb
,
2057 struct mfc6_cache
*c
)
2061 int true_vifi
= ip6mr_find_vif(mrt
, dev
);
2063 vif
= c
->_c
.mfc_parent
;
2064 c
->_c
.mfc_un
.res
.pkt
++;
2065 c
->_c
.mfc_un
.res
.bytes
+= skb
->len
;
2066 c
->_c
.mfc_un
.res
.lastuse
= jiffies
;
2068 if (ipv6_addr_any(&c
->mf6c_origin
) && true_vifi
>= 0) {
2069 struct mfc6_cache
*cache_proxy
;
2071 /* For an (*,G) entry, we only check that the incoming
2072 * interface is part of the static tree.
2075 cache_proxy
= mr_mfc_find_any_parent(mrt
, vif
);
2077 cache_proxy
->_c
.mfc_un
.res
.ttls
[true_vifi
] < 255) {
2085 * Wrong interface: drop packet and (maybe) send PIM assert.
2087 if (mrt
->vif_table
[vif
].dev
!= dev
) {
2088 c
->_c
.mfc_un
.res
.wrong_if
++;
2090 if (true_vifi
>= 0 && mrt
->mroute_do_assert
&&
2091 /* pimsm uses asserts, when switching from RPT to SPT,
2092 so that we cannot check that packet arrived on an oif.
2093 It is bad, but otherwise we would need to move pretty
2094 large chunk of pimd to kernel. Ough... --ANK
2096 (mrt
->mroute_do_pim
||
2097 c
->_c
.mfc_un
.res
.ttls
[true_vifi
] < 255) &&
2099 c
->_c
.mfc_un
.res
.last_assert
+
2100 MFC_ASSERT_THRESH
)) {
2101 c
->_c
.mfc_un
.res
.last_assert
= jiffies
;
2102 ip6mr_cache_report(mrt
, skb
, true_vifi
, MRT6MSG_WRONGMIF
);
2108 mrt
->vif_table
[vif
].pkt_in
++;
2109 mrt
->vif_table
[vif
].bytes_in
+= skb
->len
;
2114 if (ipv6_addr_any(&c
->mf6c_origin
) &&
2115 ipv6_addr_any(&c
->mf6c_mcastgrp
)) {
2116 if (true_vifi
>= 0 &&
2117 true_vifi
!= c
->_c
.mfc_parent
&&
2118 ipv6_hdr(skb
)->hop_limit
>
2119 c
->_c
.mfc_un
.res
.ttls
[c
->_c
.mfc_parent
]) {
2120 /* It's an (*,*) entry and the packet is not coming from
2121 * the upstream: forward the packet to the upstream
2124 psend
= c
->_c
.mfc_parent
;
2129 for (ct
= c
->_c
.mfc_un
.res
.maxvif
- 1;
2130 ct
>= c
->_c
.mfc_un
.res
.minvif
; ct
--) {
2131 /* For (*,G) entry, don't forward to the incoming interface */
2132 if ((!ipv6_addr_any(&c
->mf6c_origin
) || ct
!= true_vifi
) &&
2133 ipv6_hdr(skb
)->hop_limit
> c
->_c
.mfc_un
.res
.ttls
[ct
]) {
2135 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
2137 ip6mr_forward2(net
, mrt
, skb2
,
2145 ip6mr_forward2(net
, mrt
, skb
, c
, psend
);
2155 * Multicast packets for forwarding arrive here
2158 int ip6_mr_input(struct sk_buff
*skb
)
2160 struct mfc6_cache
*cache
;
2161 struct net
*net
= dev_net(skb
->dev
);
2162 struct mr_table
*mrt
;
2163 struct flowi6 fl6
= {
2164 .flowi6_iif
= skb
->dev
->ifindex
,
2165 .flowi6_mark
= skb
->mark
,
2168 struct net_device
*dev
;
2170 /* skb->dev passed in is the master dev for vrfs.
2171 * Get the proper interface that does have a vif associated with it.
2174 if (netif_is_l3_master(skb
->dev
)) {
2175 dev
= dev_get_by_index_rcu(net
, IPCB(skb
)->iif
);
2182 err
= ip6mr_fib_lookup(net
, &fl6
, &mrt
);
2188 read_lock(&mrt_lock
);
2189 cache
= ip6mr_cache_find(mrt
,
2190 &ipv6_hdr(skb
)->saddr
, &ipv6_hdr(skb
)->daddr
);
2192 int vif
= ip6mr_find_vif(mrt
, dev
);
2195 cache
= ip6mr_cache_find_any(mrt
,
2196 &ipv6_hdr(skb
)->daddr
,
2201 * No usable cache entry
2206 vif
= ip6mr_find_vif(mrt
, dev
);
2208 int err
= ip6mr_cache_unresolved(mrt
, vif
, skb
, dev
);
2209 read_unlock(&mrt_lock
);
2213 read_unlock(&mrt_lock
);
2218 ip6_mr_forward(net
, mrt
, dev
, skb
, cache
);
2220 read_unlock(&mrt_lock
);
2225 int ip6mr_get_route(struct net
*net
, struct sk_buff
*skb
, struct rtmsg
*rtm
,
2229 struct mr_table
*mrt
;
2230 struct mfc6_cache
*cache
;
2231 struct rt6_info
*rt
= (struct rt6_info
*)skb_dst(skb
);
2233 mrt
= ip6mr_get_table(net
, RT6_TABLE_DFLT
);
2237 read_lock(&mrt_lock
);
2238 cache
= ip6mr_cache_find(mrt
, &rt
->rt6i_src
.addr
, &rt
->rt6i_dst
.addr
);
2239 if (!cache
&& skb
->dev
) {
2240 int vif
= ip6mr_find_vif(mrt
, skb
->dev
);
2243 cache
= ip6mr_cache_find_any(mrt
, &rt
->rt6i_dst
.addr
,
2248 struct sk_buff
*skb2
;
2249 struct ipv6hdr
*iph
;
2250 struct net_device
*dev
;
2254 if (!dev
|| (vif
= ip6mr_find_vif(mrt
, dev
)) < 0) {
2255 read_unlock(&mrt_lock
);
2259 /* really correct? */
2260 skb2
= alloc_skb(sizeof(struct ipv6hdr
), GFP_ATOMIC
);
2262 read_unlock(&mrt_lock
);
2266 NETLINK_CB(skb2
).portid
= portid
;
2267 skb_reset_transport_header(skb2
);
2269 skb_put(skb2
, sizeof(struct ipv6hdr
));
2270 skb_reset_network_header(skb2
);
2272 iph
= ipv6_hdr(skb2
);
2275 iph
->flow_lbl
[0] = 0;
2276 iph
->flow_lbl
[1] = 0;
2277 iph
->flow_lbl
[2] = 0;
2278 iph
->payload_len
= 0;
2279 iph
->nexthdr
= IPPROTO_NONE
;
2281 iph
->saddr
= rt
->rt6i_src
.addr
;
2282 iph
->daddr
= rt
->rt6i_dst
.addr
;
2284 err
= ip6mr_cache_unresolved(mrt
, vif
, skb2
, dev
);
2285 read_unlock(&mrt_lock
);
2290 err
= mr_fill_mroute(mrt
, skb
, &cache
->_c
, rtm
);
2291 read_unlock(&mrt_lock
);
2295 static int ip6mr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2296 u32 portid
, u32 seq
, struct mfc6_cache
*c
, int cmd
,
2299 struct nlmsghdr
*nlh
;
2303 nlh
= nlmsg_put(skb
, portid
, seq
, cmd
, sizeof(*rtm
), flags
);
2307 rtm
= nlmsg_data(nlh
);
2308 rtm
->rtm_family
= RTNL_FAMILY_IP6MR
;
2309 rtm
->rtm_dst_len
= 128;
2310 rtm
->rtm_src_len
= 128;
2312 rtm
->rtm_table
= mrt
->id
;
2313 if (nla_put_u32(skb
, RTA_TABLE
, mrt
->id
))
2314 goto nla_put_failure
;
2315 rtm
->rtm_type
= RTN_MULTICAST
;
2316 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2317 if (c
->_c
.mfc_flags
& MFC_STATIC
)
2318 rtm
->rtm_protocol
= RTPROT_STATIC
;
2320 rtm
->rtm_protocol
= RTPROT_MROUTED
;
2323 if (nla_put_in6_addr(skb
, RTA_SRC
, &c
->mf6c_origin
) ||
2324 nla_put_in6_addr(skb
, RTA_DST
, &c
->mf6c_mcastgrp
))
2325 goto nla_put_failure
;
2326 err
= mr_fill_mroute(mrt
, skb
, &c
->_c
, rtm
);
2327 /* do not break the dump if cache is unresolved */
2328 if (err
< 0 && err
!= -ENOENT
)
2329 goto nla_put_failure
;
2331 nlmsg_end(skb
, nlh
);
2335 nlmsg_cancel(skb
, nlh
);
2339 static int _ip6mr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2340 u32 portid
, u32 seq
, struct mr_mfc
*c
,
2343 return ip6mr_fill_mroute(mrt
, skb
, portid
, seq
, (struct mfc6_cache
*)c
,
2347 static int mr6_msgsize(bool unresolved
, int maxvif
)
2350 NLMSG_ALIGN(sizeof(struct rtmsg
))
2351 + nla_total_size(4) /* RTA_TABLE */
2352 + nla_total_size(sizeof(struct in6_addr
)) /* RTA_SRC */
2353 + nla_total_size(sizeof(struct in6_addr
)) /* RTA_DST */
2358 + nla_total_size(4) /* RTA_IIF */
2359 + nla_total_size(0) /* RTA_MULTIPATH */
2360 + maxvif
* NLA_ALIGN(sizeof(struct rtnexthop
))
2362 + nla_total_size_64bit(sizeof(struct rta_mfc_stats
))
2368 static void mr6_netlink_event(struct mr_table
*mrt
, struct mfc6_cache
*mfc
,
2371 struct net
*net
= read_pnet(&mrt
->net
);
2372 struct sk_buff
*skb
;
2375 skb
= nlmsg_new(mr6_msgsize(mfc
->_c
.mfc_parent
>= MAXMIFS
, mrt
->maxvif
),
2380 err
= ip6mr_fill_mroute(mrt
, skb
, 0, 0, mfc
, cmd
, 0);
2384 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_MROUTE
, NULL
, GFP_ATOMIC
);
2390 rtnl_set_sk_err(net
, RTNLGRP_IPV6_MROUTE
, err
);
2393 static size_t mrt6msg_netlink_msgsize(size_t payloadlen
)
2396 NLMSG_ALIGN(sizeof(struct rtgenmsg
))
2397 + nla_total_size(1) /* IP6MRA_CREPORT_MSGTYPE */
2398 + nla_total_size(4) /* IP6MRA_CREPORT_MIF_ID */
2399 /* IP6MRA_CREPORT_SRC_ADDR */
2400 + nla_total_size(sizeof(struct in6_addr
))
2401 /* IP6MRA_CREPORT_DST_ADDR */
2402 + nla_total_size(sizeof(struct in6_addr
))
2403 /* IP6MRA_CREPORT_PKT */
2404 + nla_total_size(payloadlen
)
2410 static void mrt6msg_netlink_event(struct mr_table
*mrt
, struct sk_buff
*pkt
)
2412 struct net
*net
= read_pnet(&mrt
->net
);
2413 struct nlmsghdr
*nlh
;
2414 struct rtgenmsg
*rtgenm
;
2415 struct mrt6msg
*msg
;
2416 struct sk_buff
*skb
;
2420 payloadlen
= pkt
->len
- sizeof(struct mrt6msg
);
2421 msg
= (struct mrt6msg
*)skb_transport_header(pkt
);
2423 skb
= nlmsg_new(mrt6msg_netlink_msgsize(payloadlen
), GFP_ATOMIC
);
2427 nlh
= nlmsg_put(skb
, 0, 0, RTM_NEWCACHEREPORT
,
2428 sizeof(struct rtgenmsg
), 0);
2431 rtgenm
= nlmsg_data(nlh
);
2432 rtgenm
->rtgen_family
= RTNL_FAMILY_IP6MR
;
2433 if (nla_put_u8(skb
, IP6MRA_CREPORT_MSGTYPE
, msg
->im6_msgtype
) ||
2434 nla_put_u32(skb
, IP6MRA_CREPORT_MIF_ID
, msg
->im6_mif
) ||
2435 nla_put_in6_addr(skb
, IP6MRA_CREPORT_SRC_ADDR
,
2437 nla_put_in6_addr(skb
, IP6MRA_CREPORT_DST_ADDR
,
2439 goto nla_put_failure
;
2441 nla
= nla_reserve(skb
, IP6MRA_CREPORT_PKT
, payloadlen
);
2442 if (!nla
|| skb_copy_bits(pkt
, sizeof(struct mrt6msg
),
2443 nla_data(nla
), payloadlen
))
2444 goto nla_put_failure
;
2446 nlmsg_end(skb
, nlh
);
2448 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV6_MROUTE_R
, NULL
, GFP_ATOMIC
);
2452 nlmsg_cancel(skb
, nlh
);
2455 rtnl_set_sk_err(net
, RTNLGRP_IPV6_MROUTE_R
, -ENOBUFS
);
2458 static int ip6mr_rtm_dumproute(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2460 const struct nlmsghdr
*nlh
= cb
->nlh
;
2461 struct fib_dump_filter filter
= {};
2464 if (cb
->strict_check
) {
2465 err
= ip_valid_fib_dump_req(sock_net(skb
->sk
), nlh
,
2471 if (filter
.table_id
) {
2472 struct mr_table
*mrt
;
2474 mrt
= ip6mr_get_table(sock_net(skb
->sk
), filter
.table_id
);
2476 if (filter
.dump_all_families
)
2479 NL_SET_ERR_MSG_MOD(cb
->extack
, "MR table does not exist");
2482 err
= mr_table_dump(mrt
, skb
, cb
, _ip6mr_fill_mroute
,
2483 &mfc_unres_lock
, &filter
);
2484 return skb
->len
? : err
;
2487 return mr_rtm_dumproute(skb
, cb
, ip6mr_mr_table_iter
,
2488 _ip6mr_fill_mroute
, &mfc_unres_lock
, &filter
);