1 // SPDX-License-Identifier: GPL-2.0
3 * Management Component Transport Protocol (MCTP) - routing
6 * This is currently based on a simple routing table, with no dst cache. The
7 * number of routes should stay fairly small, so the lookup cost is small.
9 * Copyright (c) 2021 Code Construct
10 * Copyright (c) 2021 Google
13 #include <linux/idr.h>
14 #include <linux/mctp.h>
15 #include <linux/netdevice.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/skbuff.h>
20 #include <net/mctpdevice.h>
21 #include <net/netlink.h>
24 static int mctp_neigh_add(struct mctp_dev *mdev, mctp_eid_t eid,
25 enum mctp_neigh_source source,
26 size_t lladdr_len, const void *lladdr)
28 struct net *net = dev_net(mdev->dev);
29 struct mctp_neigh *neigh;
32 mutex_lock(&net->mctp.neigh_lock);
33 if (mctp_neigh_lookup(mdev, eid, NULL) == 0) {
38 if (lladdr_len > sizeof(neigh->ha)) {
43 neigh = kzalloc(sizeof(*neigh), GFP_KERNEL);
48 INIT_LIST_HEAD(&neigh->list);
50 dev_hold(neigh->dev->dev);
52 neigh->source = source;
53 memcpy(neigh->ha, lladdr, lladdr_len);
55 list_add_rcu(&neigh->list, &net->mctp.neighbours);
58 mutex_unlock(&net->mctp.neigh_lock);
62 static void __mctp_neigh_free(struct rcu_head *rcu)
64 struct mctp_neigh *neigh = container_of(rcu, struct mctp_neigh, rcu);
66 dev_put(neigh->dev->dev);
70 /* Removes all neighbour entries referring to a device */
71 void mctp_neigh_remove_dev(struct mctp_dev *mdev)
73 struct net *net = dev_net(mdev->dev);
74 struct mctp_neigh *neigh, *tmp;
76 mutex_lock(&net->mctp.neigh_lock);
77 list_for_each_entry_safe(neigh, tmp, &net->mctp.neighbours, list) {
78 if (neigh->dev == mdev) {
79 list_del_rcu(&neigh->list);
80 /* TODO: immediate RTM_DELNEIGH */
81 call_rcu(&neigh->rcu, __mctp_neigh_free);
85 mutex_unlock(&net->mctp.neigh_lock);
88 // TODO: add a "source" flag so netlink can only delete static neighbours?
89 static int mctp_neigh_remove(struct mctp_dev *mdev, mctp_eid_t eid)
91 struct net *net = dev_net(mdev->dev);
92 struct mctp_neigh *neigh, *tmp;
95 mutex_lock(&net->mctp.neigh_lock);
96 list_for_each_entry_safe(neigh, tmp, &net->mctp.neighbours, list) {
97 if (neigh->dev == mdev && neigh->eid == eid) {
98 list_del_rcu(&neigh->list);
99 /* TODO: immediate RTM_DELNEIGH */
100 call_rcu(&neigh->rcu, __mctp_neigh_free);
105 mutex_unlock(&net->mctp.neigh_lock);
106 return dropped ? 0 : -ENOENT;
109 static const struct nla_policy nd_mctp_policy[NDA_MAX + 1] = {
110 [NDA_DST] = { .type = NLA_U8 },
111 [NDA_LLADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
114 static int mctp_rtm_newneigh(struct sk_buff *skb, struct nlmsghdr *nlh,
115 struct netlink_ext_ack *extack)
117 struct net *net = sock_net(skb->sk);
118 struct net_device *dev;
119 struct mctp_dev *mdev;
121 struct nlattr *tb[NDA_MAX + 1];
127 rc = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, nd_mctp_policy,
130 NL_SET_ERR_MSG(extack, "lladdr too large?");
135 NL_SET_ERR_MSG(extack, "Neighbour EID must be specified");
139 if (!tb[NDA_LLADDR]) {
140 NL_SET_ERR_MSG(extack, "Neighbour lladdr must be specified");
144 eid = nla_get_u8(tb[NDA_DST]);
145 if (!mctp_address_ok(eid)) {
146 NL_SET_ERR_MSG(extack, "Invalid neighbour EID");
150 lladdr = nla_data(tb[NDA_LLADDR]);
151 lladdr_len = nla_len(tb[NDA_LLADDR]);
153 ndm = nlmsg_data(nlh);
155 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
159 mdev = mctp_dev_get_rtnl(dev);
163 if (lladdr_len != dev->addr_len) {
164 NL_SET_ERR_MSG(extack, "Wrong lladdr length");
168 return mctp_neigh_add(mdev, eid, MCTP_NEIGH_STATIC,
172 static int mctp_rtm_delneigh(struct sk_buff *skb, struct nlmsghdr *nlh,
173 struct netlink_ext_ack *extack)
175 struct net *net = sock_net(skb->sk);
176 struct nlattr *tb[NDA_MAX + 1];
177 struct net_device *dev;
178 struct mctp_dev *mdev;
183 rc = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, nd_mctp_policy,
186 NL_SET_ERR_MSG(extack, "incorrect format");
191 NL_SET_ERR_MSG(extack, "Neighbour EID must be specified");
194 eid = nla_get_u8(tb[NDA_DST]);
196 ndm = nlmsg_data(nlh);
197 dev = __dev_get_by_index(net, ndm->ndm_ifindex);
201 mdev = mctp_dev_get_rtnl(dev);
205 return mctp_neigh_remove(mdev, eid);
208 static int mctp_fill_neigh(struct sk_buff *skb, u32 portid, u32 seq, int event,
209 unsigned int flags, struct mctp_neigh *neigh)
211 struct net_device *dev = neigh->dev->dev;
212 struct nlmsghdr *nlh;
215 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
219 hdr = nlmsg_data(nlh);
220 hdr->ndm_family = AF_MCTP;
221 hdr->ndm_ifindex = dev->ifindex;
222 hdr->ndm_state = 0; // TODO other state bits?
223 if (neigh->source == MCTP_NEIGH_STATIC)
224 hdr->ndm_state |= NUD_PERMANENT;
226 hdr->ndm_type = RTN_UNICAST; // TODO: is loopback RTN_LOCAL?
228 if (nla_put_u8(skb, NDA_DST, neigh->eid))
231 if (nla_put(skb, NDA_LLADDR, dev->addr_len, neigh->ha))
238 nlmsg_cancel(skb, nlh);
242 static int mctp_rtm_getneigh(struct sk_buff *skb, struct netlink_callback *cb)
244 struct net *net = sock_net(skb->sk);
245 int rc, idx, req_ifindex;
246 struct mctp_neigh *neigh;
250 } *cbctx = (void *)cb->ctx;
252 ndmsg = nlmsg_data(cb->nlh);
253 req_ifindex = ndmsg->ndm_ifindex;
257 list_for_each_entry_rcu(neigh, &net->mctp.neighbours, list) {
258 if (idx < cbctx->idx)
262 if (req_ifindex == 0 || req_ifindex == neigh->dev->dev->ifindex)
263 rc = mctp_fill_neigh(skb, NETLINK_CB(cb->skb).portid,
265 RTM_NEWNEIGH, NLM_F_MULTI, neigh);
278 int mctp_neigh_lookup(struct mctp_dev *mdev, mctp_eid_t eid, void *ret_hwaddr)
280 struct net *net = dev_net(mdev->dev);
281 struct mctp_neigh *neigh;
282 int rc = -EHOSTUNREACH; // TODO: or ENOENT?
285 list_for_each_entry_rcu(neigh, &net->mctp.neighbours, list) {
286 if (mdev == neigh->dev && eid == neigh->eid) {
288 memcpy(ret_hwaddr, neigh->ha,
298 /* namespace registration */
299 static int __net_init mctp_neigh_net_init(struct net *net)
301 struct netns_mctp *ns = &net->mctp;
303 INIT_LIST_HEAD(&ns->neighbours);
304 mutex_init(&ns->neigh_lock);
308 static void __net_exit mctp_neigh_net_exit(struct net *net)
310 struct netns_mctp *ns = &net->mctp;
311 struct mctp_neigh *neigh;
313 list_for_each_entry(neigh, &ns->neighbours, list)
314 call_rcu(&neigh->rcu, __mctp_neigh_free);
317 /* net namespace implementation */
319 static struct pernet_operations mctp_net_ops = {
320 .init = mctp_neigh_net_init,
321 .exit = mctp_neigh_net_exit,
324 int __init mctp_neigh_init(void)
326 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWNEIGH,
327 mctp_rtm_newneigh, NULL, 0);
328 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELNEIGH,
329 mctp_rtm_delneigh, NULL, 0);
330 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETNEIGH,
331 NULL, mctp_rtm_getneigh, 0);
333 return register_pernet_subsys(&mctp_net_ops);
336 void __exit mctp_neigh_exit(void)
338 unregister_pernet_subsys(&mctp_net_ops);
339 rtnl_unregister(PF_MCTP, RTM_GETNEIGH);
340 rtnl_unregister(PF_MCTP, RTM_DELNEIGH);
341 rtnl_unregister(PF_MCTP, RTM_NEWNEIGH);