summaryrefslogtreecommitdiff
path: root/net/mctp/neigh.c
blob: 8603f0c45a8f106fc7c8cbb5307d7bbf56a59539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// SPDX-License-Identifier: GPL-2.0
/*
 * Management Component Transport Protocol (MCTP) - routing
 * implementation.
 *
 * This is currently based on a simple routing table, with no dst cache. The
 * number of routes should stay fairly small, so the lookup cost is small.
 *
 * Copyright (c) 2021 Code Construct
 * Copyright (c) 2021 Google
 */

#include <linux/idr.h>
#include <linux/mctp.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <linux/skbuff.h>

#include <net/mctp.h>
#include <net/mctpdevice.h>
#include <net/netlink.h>
#include <net/sock.h>

static int __always_unused mctp_neigh_add(struct mctp_dev *mdev, mctp_eid_t eid,
					  enum mctp_neigh_source source,
					  size_t lladdr_len, const void *lladdr)
{
	struct net *net = dev_net(mdev->dev);
	struct mctp_neigh *neigh;
	int rc;

	mutex_lock(&net->mctp.neigh_lock);
	if (mctp_neigh_lookup(mdev, eid, NULL) == 0) {
		rc = -EEXIST;
		goto out;
	}

	if (lladdr_len > sizeof(neigh->ha)) {
		rc = -EINVAL;
		goto out;
	}

	neigh = kzalloc(sizeof(*neigh), GFP_KERNEL);
	if (!neigh) {
		rc = -ENOMEM;
		goto out;
	}
	INIT_LIST_HEAD(&neigh->list);
	neigh->dev = mdev;
	dev_hold(neigh->dev->dev);
	neigh->eid = eid;
	neigh->source = source;
	memcpy(neigh->ha, lladdr, lladdr_len);

	list_add_rcu(&neigh->list, &net->mctp.neighbours);
	rc = 0;
out:
	mutex_unlock(&net->mctp.neigh_lock);
	return rc;
}

static void __mctp_neigh_free(struct rcu_head *rcu)
{
	struct mctp_neigh *neigh = container_of(rcu, struct mctp_neigh, rcu);

	dev_put(neigh->dev->dev);
	kfree(neigh);
}

/* Removes all neighbour entries referring to a device */
void mctp_neigh_remove_dev(struct mctp_dev *mdev)
{
	struct net *net = dev_net(mdev->dev);
	struct mctp_neigh *neigh, *tmp;

	mutex_lock(&net->mctp.neigh_lock);
	list_for_each_entry_safe(neigh, tmp, &net->mctp.neighbours, list) {
		if (neigh->dev == mdev) {
			list_del_rcu(&neigh->list);
			/* TODO: immediate RTM_DELNEIGH */
			call_rcu(&neigh->rcu, __mctp_neigh_free);
		}
	}

	mutex_unlock(&net->mctp.neigh_lock);
}

int mctp_neigh_lookup(struct mctp_dev *mdev, mctp_eid_t eid, void *ret_hwaddr)
{
	struct net *net = dev_net(mdev->dev);
	struct mctp_neigh *neigh;
	int rc = -EHOSTUNREACH; // TODO: or ENOENT?

	rcu_read_lock();
	list_for_each_entry_rcu(neigh, &net->mctp.neighbours, list) {
		if (mdev == neigh->dev && eid == neigh->eid) {
			if (ret_hwaddr)
				memcpy(ret_hwaddr, neigh->ha,
				       sizeof(neigh->ha));
			rc = 0;
			break;
		}
	}
	rcu_read_unlock();
	return rc;
}

/* namespace registration */
static int __net_init mctp_neigh_net_init(struct net *net)
{
	struct netns_mctp *ns = &net->mctp;

	INIT_LIST_HEAD(&ns->neighbours);
	return 0;
}

static void __net_exit mctp_neigh_net_exit(struct net *net)
{
	struct netns_mctp *ns = &net->mctp;
	struct mctp_neigh *neigh;

	list_for_each_entry(neigh, &ns->neighbours, list)
		call_rcu(&neigh->rcu, __mctp_neigh_free);
}

/* net namespace implementation */

static struct pernet_operations mctp_net_ops = {
	.init = mctp_neigh_net_init,
	.exit = mctp_neigh_net_exit,
};

int __init mctp_neigh_init(void)
{
	return register_pernet_subsys(&mctp_net_ops);
}

void __exit mctp_neigh_exit(void)
{
	unregister_pernet_subsys(&mctp_net_ops);
}