aboutsummaryrefslogtreecommitdiff
path: root/net/mctp/neigh.c
blob: 6ad3e33bd4d47dc6264ccebaf938d785f2b26776 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// 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 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;
	mctp_dev_hold(neigh->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);

	mctp_dev_put(neigh->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);
}

static int mctp_neigh_remove(struct mctp_dev *mdev, mctp_eid_t eid,
			     enum mctp_neigh_source source)
{
	struct net *net = dev_net(mdev->dev);
	struct mctp_neigh *neigh, *tmp;
	bool dropped = false;

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

	mutex_unlock(&net->mctp.neigh_lock);
	return dropped ? 0 : -ENOENT;
}

static const struct nla_policy nd_mctp_policy[NDA_MAX + 1] = {
	[NDA_DST]		= { .type = NLA_U8 },
	[NDA_LLADDR]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
};

static int mctp_rtm_newneigh(struct sk_buff *skb, struct nlmsghdr *nlh,
			     struct netlink_ext_ack *extack)
{
	struct net *net = sock_net(skb->sk);
	struct net_device *dev;
	struct mctp_dev *mdev;
	struct ndmsg *ndm;
	struct nlattr *tb[NDA_MAX + 1];
	int rc;
	mctp_eid_t eid;
	void *lladdr;
	int lladdr_len;

	rc = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, nd_mctp_policy,
			 extack);
	if (rc < 0) {
		NL_SET_ERR_MSG(extack, "lladdr too large?");
		return rc;
	}

	if (!tb[NDA_DST]) {
		NL_SET_ERR_MSG(extack, "Neighbour EID must be specified");
		return -EINVAL;
	}

	if (!tb[NDA_LLADDR]) {
		NL_SET_ERR_MSG(extack, "Neighbour lladdr must be specified");
		return -EINVAL;
	}

	eid = nla_get_u8(tb[NDA_DST]);
	if (!mctp_address_ok(eid)) {
		NL_SET_ERR_MSG(extack, "Invalid neighbour EID");
		return -EINVAL;
	}

	lladdr = nla_data(tb[NDA_LLADDR]);
	lladdr_len = nla_len(tb[NDA_LLADDR]);

	ndm = nlmsg_data(nlh);

	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
	if (!dev)
		return -ENODEV;

	mdev = mctp_dev_get_rtnl(dev);
	if (!mdev)
		return -ENODEV;

	if (lladdr_len != dev->addr_len) {
		NL_SET_ERR_MSG(extack, "Wrong lladdr length");
		return -EINVAL;
	}

	return mctp_neigh_add(mdev, eid, MCTP_NEIGH_STATIC,
			lladdr_len, lladdr);
}

static int mctp_rtm_delneigh(struct sk_buff *skb, struct nlmsghdr *nlh,
			     struct netlink_ext_ack *extack)
{
	struct net *net = sock_net(skb->sk);
	struct nlattr *tb[NDA_MAX + 1];
	struct net_device *dev;
	struct mctp_dev *mdev;
	struct ndmsg *ndm;
	int rc;
	mctp_eid_t eid;

	rc = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, nd_mctp_policy,
			 extack);
	if (rc < 0) {
		NL_SET_ERR_MSG(extack, "incorrect format");
		return rc;
	}

	if (!tb[NDA_DST]) {
		NL_SET_ERR_MSG(extack, "Neighbour EID must be specified");
		return -EINVAL;
	}
	eid = nla_get_u8(tb[NDA_DST]);

	ndm = nlmsg_data(nlh);
	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
	if (!dev)
		return -ENODEV;

	mdev = mctp_dev_get_rtnl(dev);
	if (!mdev)
		return -ENODEV;

	return mctp_neigh_remove(mdev, eid, MCTP_NEIGH_STATIC);
}

static int mctp_fill_neigh(struct sk_buff *skb, u32 portid, u32 seq, int event,
			   unsigned int flags, struct mctp_neigh *neigh)
{
	struct net_device *dev = neigh->dev->dev;
	struct nlmsghdr *nlh;
	struct ndmsg *hdr;

	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
	if (!nlh)
		return -EMSGSIZE;

	hdr = nlmsg_data(nlh);
	hdr->ndm_family = AF_MCTP;
	hdr->ndm_ifindex = dev->ifindex;
	hdr->ndm_state = 0; // TODO other state bits?
	if (neigh->source == MCTP_NEIGH_STATIC)
		hdr->ndm_state |= NUD_PERMANENT;
	hdr->ndm_flags = 0;
	hdr->ndm_type = RTN_UNICAST; // TODO: is loopback RTN_LOCAL?

	if (nla_put_u8(skb, NDA_DST, neigh->eid))
		goto cancel;

	if (nla_put(skb, NDA_LLADDR, dev->addr_len, neigh->ha))
		goto cancel;

	nlmsg_end(skb, nlh);

	return 0;
cancel:
	nlmsg_cancel(skb, nlh);
	return -EMSGSIZE;
}

static int mctp_rtm_getneigh(struct sk_buff *skb, struct netlink_callback *cb)
{
	struct net *net = sock_net(skb->sk);
	int rc, idx, req_ifindex;
	struct mctp_neigh *neigh;
	struct ndmsg *ndmsg;
	struct {
		int idx;
	} *cbctx = (void *)cb->ctx;

	ndmsg = nlmsg_data(cb->nlh);
	req_ifindex = ndmsg->ndm_ifindex;

	idx = 0;
	rcu_read_lock();
	list_for_each_entry_rcu(neigh, &net->mctp.neighbours, list) {
		if (idx < cbctx->idx)
			goto cont;

		rc = 0;
		if (req_ifindex == 0 || req_ifindex == neigh->dev->dev->ifindex)
			rc = mctp_fill_neigh(skb, NETLINK_CB(cb->skb).portid,
					     cb->nlh->nlmsg_seq,
					     RTM_NEWNEIGH, NLM_F_MULTI, neigh);

		if (rc)
			break;
cont:
		idx++;
	}
	rcu_read_unlock();

	cbctx->idx = idx;
	return skb->len;
}

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);
	mutex_init(&ns->neigh_lock);
	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)
{
	rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWNEIGH,
			     mctp_rtm_newneigh, NULL, 0);
	rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELNEIGH,
			     mctp_rtm_delneigh, NULL, 0);
	rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETNEIGH,
			     NULL, mctp_rtm_getneigh, 0);

	return register_pernet_subsys(&mctp_net_ops);
}

void __exit mctp_neigh_exit(void)
{
	unregister_pernet_subsys(&mctp_net_ops);
	rtnl_unregister(PF_MCTP, RTM_GETNEIGH);
	rtnl_unregister(PF_MCTP, RTM_DELNEIGH);
	rtnl_unregister(PF_MCTP, RTM_NEWNEIGH);
}