blob: f556c6d01abcad29feeb72f460780a4d93c989be [file] [log] [blame]
Jeremy Kerr583be982021-07-29 10:20:44 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Management Component Transport Protocol (MCTP) - device implementation.
4 *
5 * Copyright (c) 2021 Code Construct
6 * Copyright (c) 2021 Google
7 */
8
9#include <linux/if_link.h>
10#include <linux/mctp.h>
11#include <linux/netdevice.h>
12#include <linux/rcupdate.h>
13#include <linux/rtnetlink.h>
14
15#include <net/addrconf.h>
16#include <net/netlink.h>
17#include <net/mctp.h>
18#include <net/mctpdevice.h>
19#include <net/sock.h>
20
21struct mctp_dump_cb {
22 int h;
23 int idx;
24 size_t a_idx;
25};
26
27/* unlocked: caller must hold rcu_read_lock */
28struct mctp_dev *__mctp_dev_get(const struct net_device *dev)
29{
30 return rcu_dereference(dev->mctp_ptr);
31}
32
33struct mctp_dev *mctp_dev_get_rtnl(const struct net_device *dev)
34{
35 return rtnl_dereference(dev->mctp_ptr);
36}
37
Jeremy Kerr583be982021-07-29 10:20:44 +080038static int mctp_fill_addrinfo(struct sk_buff *skb, struct netlink_callback *cb,
39 struct mctp_dev *mdev, mctp_eid_t eid)
40{
41 struct ifaddrmsg *hdr;
42 struct nlmsghdr *nlh;
43
44 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
45 RTM_NEWADDR, sizeof(*hdr), NLM_F_MULTI);
46 if (!nlh)
47 return -EMSGSIZE;
48
49 hdr = nlmsg_data(nlh);
50 hdr->ifa_family = AF_MCTP;
51 hdr->ifa_prefixlen = 0;
52 hdr->ifa_flags = 0;
53 hdr->ifa_scope = 0;
54 hdr->ifa_index = mdev->dev->ifindex;
55
56 if (nla_put_u8(skb, IFA_LOCAL, eid))
57 goto cancel;
58
59 if (nla_put_u8(skb, IFA_ADDRESS, eid))
60 goto cancel;
61
62 nlmsg_end(skb, nlh);
63
64 return 0;
65
66cancel:
67 nlmsg_cancel(skb, nlh);
68 return -EMSGSIZE;
69}
70
71static int mctp_dump_dev_addrinfo(struct mctp_dev *mdev, struct sk_buff *skb,
72 struct netlink_callback *cb)
73{
74 struct mctp_dump_cb *mcb = (void *)cb->ctx;
75 int rc = 0;
76
77 for (; mcb->a_idx < mdev->num_addrs; mcb->a_idx++) {
78 rc = mctp_fill_addrinfo(skb, cb, mdev, mdev->addrs[mcb->a_idx]);
79 if (rc < 0)
80 break;
81 }
82
83 return rc;
84}
85
86static int mctp_dump_addrinfo(struct sk_buff *skb, struct netlink_callback *cb)
87{
88 struct mctp_dump_cb *mcb = (void *)cb->ctx;
89 struct net *net = sock_net(skb->sk);
90 struct hlist_head *head;
91 struct net_device *dev;
92 struct ifaddrmsg *hdr;
93 struct mctp_dev *mdev;
94 int ifindex;
95 int idx, rc;
96
97 hdr = nlmsg_data(cb->nlh);
98 // filter by ifindex if requested
99 ifindex = hdr->ifa_index;
100
101 rcu_read_lock();
102 for (; mcb->h < NETDEV_HASHENTRIES; mcb->h++, mcb->idx = 0) {
103 idx = 0;
104 head = &net->dev_index_head[mcb->h];
105 hlist_for_each_entry_rcu(dev, head, index_hlist) {
106 if (idx >= mcb->idx &&
107 (ifindex == 0 || ifindex == dev->ifindex)) {
108 mdev = __mctp_dev_get(dev);
109 if (mdev) {
110 rc = mctp_dump_dev_addrinfo(mdev,
111 skb, cb);
112 // Error indicates full buffer, this
113 // callback will get retried.
114 if (rc < 0)
115 goto out;
116 }
117 }
118 idx++;
119 // reset for next iteration
120 mcb->a_idx = 0;
121 }
122 }
123out:
124 rcu_read_unlock();
125 mcb->idx = idx;
126
127 return skb->len;
128}
129
130static const struct nla_policy ifa_mctp_policy[IFA_MAX + 1] = {
131 [IFA_ADDRESS] = { .type = NLA_U8 },
132 [IFA_LOCAL] = { .type = NLA_U8 },
133};
134
135static int mctp_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
136 struct netlink_ext_ack *extack)
137{
138 struct net *net = sock_net(skb->sk);
139 struct nlattr *tb[IFA_MAX + 1];
140 struct net_device *dev;
141 struct mctp_addr *addr;
142 struct mctp_dev *mdev;
143 struct ifaddrmsg *ifm;
144 unsigned long flags;
145 u8 *tmp_addrs;
146 int rc;
147
148 rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_mctp_policy,
149 extack);
150 if (rc < 0)
151 return rc;
152
153 ifm = nlmsg_data(nlh);
154
155 if (tb[IFA_LOCAL])
156 addr = nla_data(tb[IFA_LOCAL]);
157 else if (tb[IFA_ADDRESS])
158 addr = nla_data(tb[IFA_ADDRESS]);
159 else
160 return -EINVAL;
161
162 /* find device */
163 dev = __dev_get_by_index(net, ifm->ifa_index);
164 if (!dev)
165 return -ENODEV;
166
167 mdev = mctp_dev_get_rtnl(dev);
168 if (!mdev)
169 return -ENODEV;
170
171 if (!mctp_address_ok(addr->s_addr))
172 return -EINVAL;
173
174 /* Prevent duplicates. Under RTNL so don't need to lock for reading */
175 if (memchr(mdev->addrs, addr->s_addr, mdev->num_addrs))
176 return -EEXIST;
177
178 tmp_addrs = kmalloc(mdev->num_addrs + 1, GFP_KERNEL);
179 if (!tmp_addrs)
180 return -ENOMEM;
181 memcpy(tmp_addrs, mdev->addrs, mdev->num_addrs);
182 tmp_addrs[mdev->num_addrs] = addr->s_addr;
183
184 /* Lock to write */
185 spin_lock_irqsave(&mdev->addrs_lock, flags);
186 mdev->num_addrs++;
187 swap(mdev->addrs, tmp_addrs);
188 spin_unlock_irqrestore(&mdev->addrs_lock, flags);
189
190 kfree(tmp_addrs);
191
Jeremy Kerr889b7da2021-07-29 10:20:45 +0800192 mctp_route_add_local(mdev, addr->s_addr);
193
Jeremy Kerr583be982021-07-29 10:20:44 +0800194 return 0;
195}
196
197static int mctp_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
198 struct netlink_ext_ack *extack)
199{
200 struct net *net = sock_net(skb->sk);
201 struct nlattr *tb[IFA_MAX + 1];
202 struct net_device *dev;
203 struct mctp_addr *addr;
204 struct mctp_dev *mdev;
205 struct ifaddrmsg *ifm;
206 unsigned long flags;
207 u8 *pos;
208 int rc;
209
210 rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_mctp_policy,
211 extack);
212 if (rc < 0)
213 return rc;
214
215 ifm = nlmsg_data(nlh);
216
217 if (tb[IFA_LOCAL])
218 addr = nla_data(tb[IFA_LOCAL]);
219 else if (tb[IFA_ADDRESS])
220 addr = nla_data(tb[IFA_ADDRESS]);
221 else
222 return -EINVAL;
223
224 /* find device */
225 dev = __dev_get_by_index(net, ifm->ifa_index);
226 if (!dev)
227 return -ENODEV;
228
229 mdev = mctp_dev_get_rtnl(dev);
230 if (!mdev)
231 return -ENODEV;
232
233 pos = memchr(mdev->addrs, addr->s_addr, mdev->num_addrs);
234 if (!pos)
235 return -ENOENT;
236
Jeremy Kerr889b7da2021-07-29 10:20:45 +0800237 rc = mctp_route_remove_local(mdev, addr->s_addr);
238 // we can ignore -ENOENT in the case a route was already removed
239 if (rc < 0 && rc != -ENOENT)
240 return rc;
241
Jeremy Kerr583be982021-07-29 10:20:44 +0800242 spin_lock_irqsave(&mdev->addrs_lock, flags);
243 memmove(pos, pos + 1, mdev->num_addrs - 1 - (pos - mdev->addrs));
244 mdev->num_addrs--;
245 spin_unlock_irqrestore(&mdev->addrs_lock, flags);
246
247 return 0;
248}
249
Jeremy Kerr43f55f22021-09-29 15:26:08 +0800250void mctp_dev_hold(struct mctp_dev *mdev)
251{
252 refcount_inc(&mdev->refs);
253}
254
255void mctp_dev_put(struct mctp_dev *mdev)
256{
257 if (refcount_dec_and_test(&mdev->refs)) {
258 dev_put(mdev->dev);
259 kfree_rcu(mdev, rcu);
260 }
261}
262
Jeremy Kerr583be982021-07-29 10:20:44 +0800263static struct mctp_dev *mctp_add_dev(struct net_device *dev)
264{
265 struct mctp_dev *mdev;
266
267 ASSERT_RTNL();
268
269 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
270 if (!mdev)
271 return ERR_PTR(-ENOMEM);
272
273 spin_lock_init(&mdev->addrs_lock);
274
Matt Johnston03f2bbc2021-07-29 10:20:52 +0800275 mdev->net = mctp_default_net(dev_net(dev));
Jeremy Kerr583be982021-07-29 10:20:44 +0800276
277 /* associate to net_device */
Jeremy Kerr43f55f22021-09-29 15:26:08 +0800278 refcount_set(&mdev->refs, 1);
Jeremy Kerr583be982021-07-29 10:20:44 +0800279 rcu_assign_pointer(dev->mctp_ptr, mdev);
Jeremy Kerr43f55f22021-09-29 15:26:08 +0800280
Jeremy Kerr583be982021-07-29 10:20:44 +0800281 dev_hold(dev);
282 mdev->dev = dev;
283
284 return mdev;
285}
286
287static int mctp_fill_link_af(struct sk_buff *skb,
288 const struct net_device *dev, u32 ext_filter_mask)
289{
290 struct mctp_dev *mdev;
291
292 mdev = mctp_dev_get_rtnl(dev);
293 if (!mdev)
294 return -ENODATA;
295 if (nla_put_u32(skb, IFLA_MCTP_NET, mdev->net))
296 return -EMSGSIZE;
297 return 0;
298}
299
300static size_t mctp_get_link_af_size(const struct net_device *dev,
301 u32 ext_filter_mask)
302{
303 struct mctp_dev *mdev;
304 unsigned int ret;
305
306 /* caller holds RCU */
307 mdev = __mctp_dev_get(dev);
308 if (!mdev)
309 return 0;
310 ret = nla_total_size(4); /* IFLA_MCTP_NET */
311 return ret;
312}
313
314static const struct nla_policy ifla_af_mctp_policy[IFLA_MCTP_MAX + 1] = {
315 [IFLA_MCTP_NET] = { .type = NLA_U32 },
316};
317
318static int mctp_set_link_af(struct net_device *dev, const struct nlattr *attr,
319 struct netlink_ext_ack *extack)
320{
321 struct nlattr *tb[IFLA_MCTP_MAX + 1];
322 struct mctp_dev *mdev;
323 int rc;
324
325 rc = nla_parse_nested(tb, IFLA_MCTP_MAX, attr, ifla_af_mctp_policy,
326 NULL);
327 if (rc)
328 return rc;
329
330 mdev = mctp_dev_get_rtnl(dev);
331 if (!mdev)
332 return 0;
333
334 if (tb[IFLA_MCTP_NET])
335 WRITE_ONCE(mdev->net, nla_get_u32(tb[IFLA_MCTP_NET]));
336
337 return 0;
338}
339
340static void mctp_unregister(struct net_device *dev)
341{
342 struct mctp_dev *mdev;
343
344 mdev = mctp_dev_get_rtnl(dev);
345
346 if (!mdev)
347 return;
348
349 RCU_INIT_POINTER(mdev->dev->mctp_ptr, NULL);
350
Jeremy Kerr889b7da2021-07-29 10:20:45 +0800351 mctp_route_remove_dev(mdev);
Matt Johnston4d8b9312021-07-29 10:20:47 +0800352 mctp_neigh_remove_dev(mdev);
Jeremy Kerr583be982021-07-29 10:20:44 +0800353 kfree(mdev->addrs);
354
Jeremy Kerr43f55f22021-09-29 15:26:08 +0800355 mctp_dev_put(mdev);
Jeremy Kerr583be982021-07-29 10:20:44 +0800356}
357
358static int mctp_register(struct net_device *dev)
359{
360 struct mctp_dev *mdev;
361
362 /* Already registered? */
363 if (rtnl_dereference(dev->mctp_ptr))
364 return 0;
365
Matt Johnstonf364dd72021-09-29 15:26:05 +0800366 /* only register specific types (inc. NONE for TUN devices) */
367 if (!(dev->type == ARPHRD_MCTP ||
368 dev->type == ARPHRD_LOOPBACK ||
369 dev->type == ARPHRD_NONE)) {
Jeremy Kerr583be982021-07-29 10:20:44 +0800370 return 0;
Matt Johnstonf364dd72021-09-29 15:26:05 +0800371 }
Jeremy Kerr583be982021-07-29 10:20:44 +0800372
373 mdev = mctp_add_dev(dev);
374 if (IS_ERR(mdev))
375 return PTR_ERR(mdev);
376
377 return 0;
378}
379
380static int mctp_dev_notify(struct notifier_block *this, unsigned long event,
381 void *ptr)
382{
383 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
384 int rc;
385
386 switch (event) {
387 case NETDEV_REGISTER:
388 rc = mctp_register(dev);
389 if (rc)
390 return notifier_from_errno(rc);
391 break;
392 case NETDEV_UNREGISTER:
393 mctp_unregister(dev);
394 break;
395 }
396
397 return NOTIFY_OK;
398}
399
400static struct rtnl_af_ops mctp_af_ops = {
401 .family = AF_MCTP,
402 .fill_link_af = mctp_fill_link_af,
403 .get_link_af_size = mctp_get_link_af_size,
404 .set_link_af = mctp_set_link_af,
405};
406
407static struct notifier_block mctp_dev_nb = {
408 .notifier_call = mctp_dev_notify,
409 .priority = ADDRCONF_NOTIFY_PRIORITY,
410};
411
412void __init mctp_device_init(void)
413{
414 register_netdevice_notifier(&mctp_dev_nb);
415
416 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETADDR,
417 NULL, mctp_dump_addrinfo, 0);
418 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWADDR,
419 mctp_rtm_newaddr, NULL, 0);
420 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELADDR,
421 mctp_rtm_deladdr, NULL, 0);
422 rtnl_af_register(&mctp_af_ops);
423}
424
425void __exit mctp_device_exit(void)
426{
427 rtnl_af_unregister(&mctp_af_ops);
428 rtnl_unregister(PF_MCTP, RTM_DELADDR);
429 rtnl_unregister(PF_MCTP, RTM_NEWADDR);
430 rtnl_unregister(PF_MCTP, RTM_GETADDR);
431
432 unregister_netdevice_notifier(&mctp_dev_nb);
433}