2ccbc94dfb
commit 031ae72825cef43e4650140b800ad58bf7a6a466 upstream. syzbot found an use-after-free Read in ila_nf_input [1] Issue here is that ila_xlat_exit_net() frees the rhashtable, then call nf_unregister_net_hooks(). It should be done in the reverse way, with a synchronize_rcu(). This is a good match for a pre_exit() method. [1] BUG: KASAN: use-after-free in rht_key_hashfn include/linux/rhashtable.h:159 [inline] BUG: KASAN: use-after-free in __rhashtable_lookup include/linux/rhashtable.h:604 [inline] BUG: KASAN: use-after-free in rhashtable_lookup include/linux/rhashtable.h:646 [inline] BUG: KASAN: use-after-free in rhashtable_lookup_fast+0x77a/0x9b0 include/linux/rhashtable.h:672 Read of size 4 at addr ffff888064620008 by task ksoftirqd/0/16 CPU: 0 UID: 0 PID: 16 Comm: ksoftirqd/0 Not tainted 6.11.0-rc4-syzkaller-00238-g2ad6d23f465a #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 08/06/2024 Call Trace: <TASK> __dump_stack lib/dump_stack.c:93 [inline] dump_stack_lvl+0x241/0x360 lib/dump_stack.c:119 print_address_description mm/kasan/report.c:377 [inline] print_report+0x169/0x550 mm/kasan/report.c:488 kasan_report+0x143/0x180 mm/kasan/report.c:601 rht_key_hashfn include/linux/rhashtable.h:159 [inline] __rhashtable_lookup include/linux/rhashtable.h:604 [inline] rhashtable_lookup include/linux/rhashtable.h:646 [inline] rhashtable_lookup_fast+0x77a/0x9b0 include/linux/rhashtable.h:672 ila_lookup_wildcards net/ipv6/ila/ila_xlat.c:132 [inline] ila_xlat_addr net/ipv6/ila/ila_xlat.c:652 [inline] ila_nf_input+0x1fe/0x3c0 net/ipv6/ila/ila_xlat.c:190 nf_hook_entry_hookfn include/linux/netfilter.h:154 [inline] nf_hook_slow+0xc3/0x220 net/netfilter/core.c:626 nf_hook include/linux/netfilter.h:269 [inline] NF_HOOK+0x29e/0x450 include/linux/netfilter.h:312 __netif_receive_skb_one_core net/core/dev.c:5661 [inline] __netif_receive_skb+0x1ea/0x650 net/core/dev.c:5775 process_backlog+0x662/0x15b0 net/core/dev.c:6108 __napi_poll+0xcb/0x490 net/core/dev.c:6772 napi_poll net/core/dev.c:6841 [inline] net_rx_action+0x89b/0x1240 net/core/dev.c:6963 handle_softirqs+0x2c4/0x970 kernel/softirq.c:554 run_ksoftirqd+0xca/0x130 kernel/softirq.c:928 smpboot_thread_fn+0x544/0xa30 kernel/smpboot.c:164 kthread+0x2f0/0x390 kernel/kthread.c:389 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244 </TASK> The buggy address belongs to the physical page: page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x64620 flags: 0xfff00000000000(node=0|zone=1|lastcpupid=0x7ff) page_type: 0xbfffffff(buddy) raw: 00fff00000000000 ffffea0000959608 ffffea00019d9408 0000000000000000 raw: 0000000000000000 0000000000000003 00000000bfffffff 0000000000000000 page dumped because: kasan: bad access detected page_owner tracks the page as freed page last allocated via order 3, migratetype Unmovable, gfp_mask 0x52dc0(GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_ZERO), pid 5242, tgid 5242 (syz-executor), ts 73611328570, free_ts 618981657187 set_page_owner include/linux/page_owner.h:32 [inline] post_alloc_hook+0x1f3/0x230 mm/page_alloc.c:1493 prep_new_page mm/page_alloc.c:1501 [inline] get_page_from_freelist+0x2e4c/0x2f10 mm/page_alloc.c:3439 __alloc_pages_noprof+0x256/0x6c0 mm/page_alloc.c:4695 __alloc_pages_node_noprof include/linux/gfp.h:269 [inline] alloc_pages_node_noprof include/linux/gfp.h:296 [inline] ___kmalloc_large_node+0x8b/0x1d0 mm/slub.c:4103 __kmalloc_large_node_noprof+0x1a/0x80 mm/slub.c:4130 __do_kmalloc_node mm/slub.c:4146 [inline] __kmalloc_node_noprof+0x2d2/0x440 mm/slub.c:4164 __kvmalloc_node_noprof+0x72/0x190 mm/util.c:650 bucket_table_alloc lib/rhashtable.c:186 [inline] rhashtable_init_noprof+0x534/0xa60 lib/rhashtable.c:1071 ila_xlat_init_net+0xa0/0x110 net/ipv6/ila/ila_xlat.c:613 ops_init+0x359/0x610 net/core/net_namespace.c:139 setup_net+0x515/0xca0 net/core/net_namespace.c:343 copy_net_ns+0x4e2/0x7b0 net/core/net_namespace.c:508 create_new_namespaces+0x425/0x7b0 kernel/nsproxy.c:110 unshare_nsproxy_namespaces+0x124/0x180 kernel/nsproxy.c:228 ksys_unshare+0x619/0xc10 kernel/fork.c:3328 __do_sys_unshare kernel/fork.c:3399 [inline] __se_sys_unshare kernel/fork.c:3397 [inline] __x64_sys_unshare+0x38/0x40 kernel/fork.c:3397 page last free pid 11846 tgid 11846 stack trace: reset_page_owner include/linux/page_owner.h:25 [inline] free_pages_prepare mm/page_alloc.c:1094 [inline] free_unref_page+0xd22/0xea0 mm/page_alloc.c:2612 __folio_put+0x2c8/0x440 mm/swap.c:128 folio_put include/linux/mm.h:1486 [inline] free_large_kmalloc+0x105/0x1c0 mm/slub.c:4565 kfree+0x1c4/0x360 mm/slub.c:4588 rhashtable_free_and_destroy+0x7c6/0x920 lib/rhashtable.c:1169 ila_xlat_exit_net+0x55/0x110 net/ipv6/ila/ila_xlat.c:626 ops_exit_list net/core/net_namespace.c:173 [inline] cleanup_net+0x802/0xcc0 net/core/net_namespace.c:640 process_one_work kernel/workqueue.c:3231 [inline] process_scheduled_works+0xa2c/0x1830 kernel/workqueue.c:3312 worker_thread+0x86d/0xd40 kernel/workqueue.c:3390 kthread+0x2f0/0x390 kernel/kthread.c:389 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:147 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244 Memory state around the buggy address: ffff88806461ff00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc ffff88806461ff80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc >ffff888064620000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ^ ffff888064620080: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ffff888064620100: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff Fixes: 7f00feaf1076 ("ila: Add generic ILA translation facility") Reported-by: syzbot <syzkaller@googlegroups.com> Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Tom Herbert <tom@herbertland.com> Reviewed-by: Florian Westphal <fw@strlen.de> Link: https://patch.msgid.link/20240904144418.1162839-1-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
661 lines
14 KiB
C
Executable file
661 lines
14 KiB
C
Executable file
// SPDX-License-Identifier: GPL-2.0
|
|
#include <linux/jhash.h>
|
|
#include <linux/netfilter.h>
|
|
#include <linux/rcupdate.h>
|
|
#include <linux/rhashtable.h>
|
|
#include <linux/vmalloc.h>
|
|
#include <net/genetlink.h>
|
|
#include <net/ila.h>
|
|
#include <net/netns/generic.h>
|
|
#include <uapi/linux/genetlink.h>
|
|
#include "ila.h"
|
|
|
|
struct ila_xlat_params {
|
|
struct ila_params ip;
|
|
int ifindex;
|
|
};
|
|
|
|
struct ila_map {
|
|
struct ila_xlat_params xp;
|
|
struct rhash_head node;
|
|
struct ila_map __rcu *next;
|
|
struct rcu_head rcu;
|
|
};
|
|
|
|
#define MAX_LOCKS 1024
|
|
#define LOCKS_PER_CPU 10
|
|
|
|
static int alloc_ila_locks(struct ila_net *ilan)
|
|
{
|
|
return alloc_bucket_spinlocks(&ilan->xlat.locks, &ilan->xlat.locks_mask,
|
|
MAX_LOCKS, LOCKS_PER_CPU,
|
|
GFP_KERNEL);
|
|
}
|
|
|
|
static u32 hashrnd __read_mostly;
|
|
static __always_inline void __ila_hash_secret_init(void)
|
|
{
|
|
net_get_random_once(&hashrnd, sizeof(hashrnd));
|
|
}
|
|
|
|
static inline u32 ila_locator_hash(struct ila_locator loc)
|
|
{
|
|
u32 *v = (u32 *)loc.v32;
|
|
|
|
__ila_hash_secret_init();
|
|
return jhash_2words(v[0], v[1], hashrnd);
|
|
}
|
|
|
|
static inline spinlock_t *ila_get_lock(struct ila_net *ilan,
|
|
struct ila_locator loc)
|
|
{
|
|
return &ilan->xlat.locks[ila_locator_hash(loc) & ilan->xlat.locks_mask];
|
|
}
|
|
|
|
static inline int ila_cmp_wildcards(struct ila_map *ila,
|
|
struct ila_addr *iaddr, int ifindex)
|
|
{
|
|
return (ila->xp.ifindex && ila->xp.ifindex != ifindex);
|
|
}
|
|
|
|
static inline int ila_cmp_params(struct ila_map *ila,
|
|
struct ila_xlat_params *xp)
|
|
{
|
|
return (ila->xp.ifindex != xp->ifindex);
|
|
}
|
|
|
|
static int ila_cmpfn(struct rhashtable_compare_arg *arg,
|
|
const void *obj)
|
|
{
|
|
const struct ila_map *ila = obj;
|
|
|
|
return (ila->xp.ip.locator_match.v64 != *(__be64 *)arg->key);
|
|
}
|
|
|
|
static inline int ila_order(struct ila_map *ila)
|
|
{
|
|
int score = 0;
|
|
|
|
if (ila->xp.ifindex)
|
|
score += 1 << 1;
|
|
|
|
return score;
|
|
}
|
|
|
|
static const struct rhashtable_params rht_params = {
|
|
.nelem_hint = 1024,
|
|
.head_offset = offsetof(struct ila_map, node),
|
|
.key_offset = offsetof(struct ila_map, xp.ip.locator_match),
|
|
.key_len = sizeof(u64), /* identifier */
|
|
.max_size = 1048576,
|
|
.min_size = 256,
|
|
.automatic_shrinking = true,
|
|
.obj_cmpfn = ila_cmpfn,
|
|
};
|
|
|
|
static int parse_nl_config(struct genl_info *info,
|
|
struct ila_xlat_params *xp)
|
|
{
|
|
memset(xp, 0, sizeof(*xp));
|
|
|
|
if (info->attrs[ILA_ATTR_LOCATOR])
|
|
xp->ip.locator.v64 = (__force __be64)nla_get_u64(
|
|
info->attrs[ILA_ATTR_LOCATOR]);
|
|
|
|
if (info->attrs[ILA_ATTR_LOCATOR_MATCH])
|
|
xp->ip.locator_match.v64 = (__force __be64)nla_get_u64(
|
|
info->attrs[ILA_ATTR_LOCATOR_MATCH]);
|
|
|
|
if (info->attrs[ILA_ATTR_CSUM_MODE])
|
|
xp->ip.csum_mode = nla_get_u8(info->attrs[ILA_ATTR_CSUM_MODE]);
|
|
else
|
|
xp->ip.csum_mode = ILA_CSUM_NO_ACTION;
|
|
|
|
if (info->attrs[ILA_ATTR_IDENT_TYPE])
|
|
xp->ip.ident_type = nla_get_u8(
|
|
info->attrs[ILA_ATTR_IDENT_TYPE]);
|
|
else
|
|
xp->ip.ident_type = ILA_ATYPE_USE_FORMAT;
|
|
|
|
if (info->attrs[ILA_ATTR_IFINDEX])
|
|
xp->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* Must be called with rcu readlock */
|
|
static inline struct ila_map *ila_lookup_wildcards(struct ila_addr *iaddr,
|
|
int ifindex,
|
|
struct ila_net *ilan)
|
|
{
|
|
struct ila_map *ila;
|
|
|
|
ila = rhashtable_lookup_fast(&ilan->xlat.rhash_table, &iaddr->loc,
|
|
rht_params);
|
|
while (ila) {
|
|
if (!ila_cmp_wildcards(ila, iaddr, ifindex))
|
|
return ila;
|
|
ila = rcu_access_pointer(ila->next);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/* Must be called with rcu readlock */
|
|
static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *xp,
|
|
struct ila_net *ilan)
|
|
{
|
|
struct ila_map *ila;
|
|
|
|
ila = rhashtable_lookup_fast(&ilan->xlat.rhash_table,
|
|
&xp->ip.locator_match,
|
|
rht_params);
|
|
while (ila) {
|
|
if (!ila_cmp_params(ila, xp))
|
|
return ila;
|
|
ila = rcu_access_pointer(ila->next);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static inline void ila_release(struct ila_map *ila)
|
|
{
|
|
kfree_rcu(ila, rcu);
|
|
}
|
|
|
|
static void ila_free_node(struct ila_map *ila)
|
|
{
|
|
struct ila_map *next;
|
|
|
|
/* Assume rcu_readlock held */
|
|
while (ila) {
|
|
next = rcu_access_pointer(ila->next);
|
|
ila_release(ila);
|
|
ila = next;
|
|
}
|
|
}
|
|
|
|
static void ila_free_cb(void *ptr, void *arg)
|
|
{
|
|
ila_free_node((struct ila_map *)ptr);
|
|
}
|
|
|
|
static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila);
|
|
|
|
static unsigned int
|
|
ila_nf_input(void *priv,
|
|
struct sk_buff *skb,
|
|
const struct nf_hook_state *state)
|
|
{
|
|
ila_xlat_addr(skb, false);
|
|
return NF_ACCEPT;
|
|
}
|
|
|
|
static const struct nf_hook_ops ila_nf_hook_ops[] = {
|
|
{
|
|
.hook = ila_nf_input,
|
|
.pf = NFPROTO_IPV6,
|
|
.hooknum = NF_INET_PRE_ROUTING,
|
|
.priority = -1,
|
|
},
|
|
};
|
|
|
|
static int ila_add_mapping(struct net *net, struct ila_xlat_params *xp)
|
|
{
|
|
struct ila_net *ilan = net_generic(net, ila_net_id);
|
|
struct ila_map *ila, *head;
|
|
spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
|
|
int err = 0, order;
|
|
|
|
if (!ilan->xlat.hooks_registered) {
|
|
/* We defer registering net hooks in the namespace until the
|
|
* first mapping is added.
|
|
*/
|
|
err = nf_register_net_hooks(net, ila_nf_hook_ops,
|
|
ARRAY_SIZE(ila_nf_hook_ops));
|
|
if (err)
|
|
return err;
|
|
|
|
ilan->xlat.hooks_registered = true;
|
|
}
|
|
|
|
ila = kzalloc(sizeof(*ila), GFP_KERNEL);
|
|
if (!ila)
|
|
return -ENOMEM;
|
|
|
|
ila_init_saved_csum(&xp->ip);
|
|
|
|
ila->xp = *xp;
|
|
|
|
order = ila_order(ila);
|
|
|
|
spin_lock(lock);
|
|
|
|
head = rhashtable_lookup_fast(&ilan->xlat.rhash_table,
|
|
&xp->ip.locator_match,
|
|
rht_params);
|
|
if (!head) {
|
|
/* New entry for the rhash_table */
|
|
err = rhashtable_lookup_insert_fast(&ilan->xlat.rhash_table,
|
|
&ila->node, rht_params);
|
|
} else {
|
|
struct ila_map *tila = head, *prev = NULL;
|
|
|
|
do {
|
|
if (!ila_cmp_params(tila, xp)) {
|
|
err = -EEXIST;
|
|
goto out;
|
|
}
|
|
|
|
if (order > ila_order(tila))
|
|
break;
|
|
|
|
prev = tila;
|
|
tila = rcu_dereference_protected(tila->next,
|
|
lockdep_is_held(lock));
|
|
} while (tila);
|
|
|
|
if (prev) {
|
|
/* Insert in sub list of head */
|
|
RCU_INIT_POINTER(ila->next, tila);
|
|
rcu_assign_pointer(prev->next, ila);
|
|
} else {
|
|
/* Make this ila new head */
|
|
RCU_INIT_POINTER(ila->next, head);
|
|
err = rhashtable_replace_fast(&ilan->xlat.rhash_table,
|
|
&head->node,
|
|
&ila->node, rht_params);
|
|
if (err)
|
|
goto out;
|
|
}
|
|
}
|
|
|
|
out:
|
|
spin_unlock(lock);
|
|
|
|
if (err)
|
|
kfree(ila);
|
|
|
|
return err;
|
|
}
|
|
|
|
static int ila_del_mapping(struct net *net, struct ila_xlat_params *xp)
|
|
{
|
|
struct ila_net *ilan = net_generic(net, ila_net_id);
|
|
struct ila_map *ila, *head, *prev;
|
|
spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
|
|
int err = -ENOENT;
|
|
|
|
spin_lock(lock);
|
|
|
|
head = rhashtable_lookup_fast(&ilan->xlat.rhash_table,
|
|
&xp->ip.locator_match, rht_params);
|
|
ila = head;
|
|
|
|
prev = NULL;
|
|
|
|
while (ila) {
|
|
if (ila_cmp_params(ila, xp)) {
|
|
prev = ila;
|
|
ila = rcu_dereference_protected(ila->next,
|
|
lockdep_is_held(lock));
|
|
continue;
|
|
}
|
|
|
|
err = 0;
|
|
|
|
if (prev) {
|
|
/* Not head, just delete from list */
|
|
rcu_assign_pointer(prev->next, ila->next);
|
|
} else {
|
|
/* It is the head. If there is something in the
|
|
* sublist we need to make a new head.
|
|
*/
|
|
head = rcu_dereference_protected(ila->next,
|
|
lockdep_is_held(lock));
|
|
if (head) {
|
|
/* Put first entry in the sublist into the
|
|
* table
|
|
*/
|
|
err = rhashtable_replace_fast(
|
|
&ilan->xlat.rhash_table, &ila->node,
|
|
&head->node, rht_params);
|
|
if (err)
|
|
goto out;
|
|
} else {
|
|
/* Entry no longer used */
|
|
err = rhashtable_remove_fast(
|
|
&ilan->xlat.rhash_table,
|
|
&ila->node, rht_params);
|
|
}
|
|
}
|
|
|
|
ila_release(ila);
|
|
|
|
break;
|
|
}
|
|
|
|
out:
|
|
spin_unlock(lock);
|
|
|
|
return err;
|
|
}
|
|
|
|
int ila_xlat_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info)
|
|
{
|
|
struct net *net = genl_info_net(info);
|
|
struct ila_xlat_params p;
|
|
int err;
|
|
|
|
err = parse_nl_config(info, &p);
|
|
if (err)
|
|
return err;
|
|
|
|
return ila_add_mapping(net, &p);
|
|
}
|
|
|
|
int ila_xlat_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info)
|
|
{
|
|
struct net *net = genl_info_net(info);
|
|
struct ila_xlat_params xp;
|
|
int err;
|
|
|
|
err = parse_nl_config(info, &xp);
|
|
if (err)
|
|
return err;
|
|
|
|
ila_del_mapping(net, &xp);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static inline spinlock_t *lock_from_ila_map(struct ila_net *ilan,
|
|
struct ila_map *ila)
|
|
{
|
|
return ila_get_lock(ilan, ila->xp.ip.locator_match);
|
|
}
|
|
|
|
int ila_xlat_nl_cmd_flush(struct sk_buff *skb, struct genl_info *info)
|
|
{
|
|
struct net *net = genl_info_net(info);
|
|
struct ila_net *ilan = net_generic(net, ila_net_id);
|
|
struct rhashtable_iter iter;
|
|
struct ila_map *ila;
|
|
spinlock_t *lock;
|
|
int ret = 0;
|
|
|
|
rhashtable_walk_enter(&ilan->xlat.rhash_table, &iter);
|
|
rhashtable_walk_start(&iter);
|
|
|
|
for (;;) {
|
|
ila = rhashtable_walk_next(&iter);
|
|
|
|
if (IS_ERR(ila)) {
|
|
if (PTR_ERR(ila) == -EAGAIN)
|
|
continue;
|
|
ret = PTR_ERR(ila);
|
|
goto done;
|
|
} else if (!ila) {
|
|
break;
|
|
}
|
|
|
|
lock = lock_from_ila_map(ilan, ila);
|
|
|
|
spin_lock(lock);
|
|
|
|
ret = rhashtable_remove_fast(&ilan->xlat.rhash_table,
|
|
&ila->node, rht_params);
|
|
if (!ret)
|
|
ila_free_node(ila);
|
|
|
|
spin_unlock(lock);
|
|
|
|
if (ret)
|
|
break;
|
|
}
|
|
|
|
done:
|
|
rhashtable_walk_stop(&iter);
|
|
rhashtable_walk_exit(&iter);
|
|
return ret;
|
|
}
|
|
|
|
static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
|
|
{
|
|
if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
|
|
(__force u64)ila->xp.ip.locator.v64,
|
|
ILA_ATTR_PAD) ||
|
|
nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
|
|
(__force u64)ila->xp.ip.locator_match.v64,
|
|
ILA_ATTR_PAD) ||
|
|
nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
|
|
nla_put_u8(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode) ||
|
|
nla_put_u8(msg, ILA_ATTR_IDENT_TYPE, ila->xp.ip.ident_type))
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int ila_dump_info(struct ila_map *ila,
|
|
u32 portid, u32 seq, u32 flags,
|
|
struct sk_buff *skb, u8 cmd)
|
|
{
|
|
void *hdr;
|
|
|
|
hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
|
|
if (!hdr)
|
|
return -ENOMEM;
|
|
|
|
if (ila_fill_info(ila, skb) < 0)
|
|
goto nla_put_failure;
|
|
|
|
genlmsg_end(skb, hdr);
|
|
return 0;
|
|
|
|
nla_put_failure:
|
|
genlmsg_cancel(skb, hdr);
|
|
return -EMSGSIZE;
|
|
}
|
|
|
|
int ila_xlat_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
|
|
{
|
|
struct net *net = genl_info_net(info);
|
|
struct ila_net *ilan = net_generic(net, ila_net_id);
|
|
struct sk_buff *msg;
|
|
struct ila_xlat_params xp;
|
|
struct ila_map *ila;
|
|
int ret;
|
|
|
|
ret = parse_nl_config(info, &xp);
|
|
if (ret)
|
|
return ret;
|
|
|
|
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
|
|
if (!msg)
|
|
return -ENOMEM;
|
|
|
|
rcu_read_lock();
|
|
|
|
ret = -ESRCH;
|
|
ila = ila_lookup_by_params(&xp, ilan);
|
|
if (ila) {
|
|
ret = ila_dump_info(ila,
|
|
info->snd_portid,
|
|
info->snd_seq, 0, msg,
|
|
info->genlhdr->cmd);
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
if (ret < 0)
|
|
goto out_free;
|
|
|
|
return genlmsg_reply(msg, info);
|
|
|
|
out_free:
|
|
nlmsg_free(msg);
|
|
return ret;
|
|
}
|
|
|
|
struct ila_dump_iter {
|
|
struct rhashtable_iter rhiter;
|
|
int skip;
|
|
};
|
|
|
|
int ila_xlat_nl_dump_start(struct netlink_callback *cb)
|
|
{
|
|
struct net *net = sock_net(cb->skb->sk);
|
|
struct ila_net *ilan = net_generic(net, ila_net_id);
|
|
struct ila_dump_iter *iter;
|
|
|
|
iter = kmalloc(sizeof(*iter), GFP_KERNEL);
|
|
if (!iter)
|
|
return -ENOMEM;
|
|
|
|
rhashtable_walk_enter(&ilan->xlat.rhash_table, &iter->rhiter);
|
|
|
|
iter->skip = 0;
|
|
cb->args[0] = (long)iter;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ila_xlat_nl_dump_done(struct netlink_callback *cb)
|
|
{
|
|
struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
|
|
|
|
rhashtable_walk_exit(&iter->rhiter);
|
|
|
|
kfree(iter);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ila_xlat_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
|
|
{
|
|
struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
|
|
struct rhashtable_iter *rhiter = &iter->rhiter;
|
|
int skip = iter->skip;
|
|
struct ila_map *ila;
|
|
int ret;
|
|
|
|
rhashtable_walk_start(rhiter);
|
|
|
|
/* Get first entry */
|
|
ila = rhashtable_walk_peek(rhiter);
|
|
|
|
if (ila && !IS_ERR(ila) && skip) {
|
|
/* Skip over visited entries */
|
|
|
|
while (ila && skip) {
|
|
/* Skip over any ila entries in this list that we
|
|
* have already dumped.
|
|
*/
|
|
ila = rcu_access_pointer(ila->next);
|
|
skip--;
|
|
}
|
|
}
|
|
|
|
skip = 0;
|
|
|
|
for (;;) {
|
|
if (IS_ERR(ila)) {
|
|
ret = PTR_ERR(ila);
|
|
if (ret == -EAGAIN) {
|
|
/* Table has changed and iter has reset. Return
|
|
* -EAGAIN to the application even if we have
|
|
* written data to the skb. The application
|
|
* needs to deal with this.
|
|
*/
|
|
|
|
goto out_ret;
|
|
} else {
|
|
break;
|
|
}
|
|
} else if (!ila) {
|
|
ret = 0;
|
|
break;
|
|
}
|
|
|
|
while (ila) {
|
|
ret = ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
|
|
cb->nlh->nlmsg_seq, NLM_F_MULTI,
|
|
skb, ILA_CMD_GET);
|
|
if (ret)
|
|
goto out;
|
|
|
|
skip++;
|
|
ila = rcu_access_pointer(ila->next);
|
|
}
|
|
|
|
skip = 0;
|
|
ila = rhashtable_walk_next(rhiter);
|
|
}
|
|
|
|
out:
|
|
iter->skip = skip;
|
|
ret = (skb->len ? : ret);
|
|
|
|
out_ret:
|
|
rhashtable_walk_stop(rhiter);
|
|
return ret;
|
|
}
|
|
|
|
int ila_xlat_init_net(struct net *net)
|
|
{
|
|
struct ila_net *ilan = net_generic(net, ila_net_id);
|
|
int err;
|
|
|
|
err = alloc_ila_locks(ilan);
|
|
if (err)
|
|
return err;
|
|
|
|
rhashtable_init(&ilan->xlat.rhash_table, &rht_params);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void ila_xlat_pre_exit_net(struct net *net)
|
|
{
|
|
struct ila_net *ilan = net_generic(net, ila_net_id);
|
|
|
|
if (ilan->xlat.hooks_registered)
|
|
nf_unregister_net_hooks(net, ila_nf_hook_ops,
|
|
ARRAY_SIZE(ila_nf_hook_ops));
|
|
}
|
|
|
|
void ila_xlat_exit_net(struct net *net)
|
|
{
|
|
struct ila_net *ilan = net_generic(net, ila_net_id);
|
|
|
|
rhashtable_free_and_destroy(&ilan->xlat.rhash_table, ila_free_cb, NULL);
|
|
|
|
free_bucket_spinlocks(ilan->xlat.locks);
|
|
}
|
|
|
|
static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila)
|
|
{
|
|
struct ila_map *ila;
|
|
struct ipv6hdr *ip6h = ipv6_hdr(skb);
|
|
struct net *net = dev_net(skb->dev);
|
|
struct ila_net *ilan = net_generic(net, ila_net_id);
|
|
struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
|
|
|
|
/* Assumes skb contains a valid IPv6 header that is pulled */
|
|
|
|
/* No check here that ILA type in the mapping matches what is in the
|
|
* address. We assume that whatever sender gaves us can be translated.
|
|
* The checksum mode however is relevant.
|
|
*/
|
|
|
|
rcu_read_lock();
|
|
|
|
ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
|
|
if (ila)
|
|
ila_update_ipv6_locator(skb, &ila->xp.ip, sir2ila);
|
|
|
|
rcu_read_unlock();
|
|
|
|
return 0;
|
|
}
|