blob: ef2755f82f87b38d604db30498c250c489ac7a53 [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
Matt Johnstondbcefde2021-12-20 10:31:04 +080038static int mctp_addrinfo_size(void)
39{
40 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
41 + nla_total_size(1) // IFA_LOCAL
42 + nla_total_size(1) // IFA_ADDRESS
43 ;
44}
45
46/* flag should be NLM_F_MULTI for dump calls */
47static int mctp_fill_addrinfo(struct sk_buff *skb,
48 struct mctp_dev *mdev, mctp_eid_t eid,
49 int msg_type, u32 portid, u32 seq, int flag)
Jeremy Kerr583be982021-07-29 10:20:44 +080050{
51 struct ifaddrmsg *hdr;
52 struct nlmsghdr *nlh;
53
Matt Johnstondbcefde2021-12-20 10:31:04 +080054 nlh = nlmsg_put(skb, portid, seq,
55 msg_type, sizeof(*hdr), flag);
Jeremy Kerr583be982021-07-29 10:20:44 +080056 if (!nlh)
57 return -EMSGSIZE;
58
59 hdr = nlmsg_data(nlh);
60 hdr->ifa_family = AF_MCTP;
61 hdr->ifa_prefixlen = 0;
62 hdr->ifa_flags = 0;
63 hdr->ifa_scope = 0;
64 hdr->ifa_index = mdev->dev->ifindex;
65
66 if (nla_put_u8(skb, IFA_LOCAL, eid))
67 goto cancel;
68
69 if (nla_put_u8(skb, IFA_ADDRESS, eid))
70 goto cancel;
71
72 nlmsg_end(skb, nlh);
73
74 return 0;
75
76cancel:
77 nlmsg_cancel(skb, nlh);
78 return -EMSGSIZE;
79}
80
81static int mctp_dump_dev_addrinfo(struct mctp_dev *mdev, struct sk_buff *skb,
82 struct netlink_callback *cb)
83{
84 struct mctp_dump_cb *mcb = (void *)cb->ctx;
Matt Johnstondbcefde2021-12-20 10:31:04 +080085 u32 portid, seq;
Jeremy Kerr583be982021-07-29 10:20:44 +080086 int rc = 0;
87
Matt Johnstondbcefde2021-12-20 10:31:04 +080088 portid = NETLINK_CB(cb->skb).portid;
89 seq = cb->nlh->nlmsg_seq;
Jeremy Kerr583be982021-07-29 10:20:44 +080090 for (; mcb->a_idx < mdev->num_addrs; mcb->a_idx++) {
Matt Johnstondbcefde2021-12-20 10:31:04 +080091 rc = mctp_fill_addrinfo(skb, mdev, mdev->addrs[mcb->a_idx],
92 RTM_NEWADDR, portid, seq, NLM_F_MULTI);
Jeremy Kerr583be982021-07-29 10:20:44 +080093 if (rc < 0)
94 break;
95 }
96
97 return rc;
98}
99
100static int mctp_dump_addrinfo(struct sk_buff *skb, struct netlink_callback *cb)
101{
102 struct mctp_dump_cb *mcb = (void *)cb->ctx;
103 struct net *net = sock_net(skb->sk);
104 struct hlist_head *head;
105 struct net_device *dev;
106 struct ifaddrmsg *hdr;
107 struct mctp_dev *mdev;
108 int ifindex;
109 int idx, rc;
110
111 hdr = nlmsg_data(cb->nlh);
112 // filter by ifindex if requested
113 ifindex = hdr->ifa_index;
114
115 rcu_read_lock();
116 for (; mcb->h < NETDEV_HASHENTRIES; mcb->h++, mcb->idx = 0) {
117 idx = 0;
118 head = &net->dev_index_head[mcb->h];
119 hlist_for_each_entry_rcu(dev, head, index_hlist) {
120 if (idx >= mcb->idx &&
121 (ifindex == 0 || ifindex == dev->ifindex)) {
122 mdev = __mctp_dev_get(dev);
123 if (mdev) {
124 rc = mctp_dump_dev_addrinfo(mdev,
125 skb, cb);
126 // Error indicates full buffer, this
127 // callback will get retried.
128 if (rc < 0)
129 goto out;
130 }
131 }
132 idx++;
133 // reset for next iteration
134 mcb->a_idx = 0;
135 }
136 }
137out:
138 rcu_read_unlock();
139 mcb->idx = idx;
140
141 return skb->len;
142}
143
Matt Johnstondbcefde2021-12-20 10:31:04 +0800144static void mctp_addr_notify(struct mctp_dev *mdev, mctp_eid_t eid, int msg_type,
145 struct sk_buff *req_skb, struct nlmsghdr *req_nlh)
146{
147 u32 portid = NETLINK_CB(req_skb).portid;
148 struct net *net = dev_net(mdev->dev);
149 struct sk_buff *skb;
150 int rc = -ENOBUFS;
151
152 skb = nlmsg_new(mctp_addrinfo_size(), GFP_KERNEL);
153 if (!skb)
154 goto out;
155
156 rc = mctp_fill_addrinfo(skb, mdev, eid, msg_type,
157 portid, req_nlh->nlmsg_seq, 0);
158 if (rc < 0) {
159 WARN_ON_ONCE(rc == -EMSGSIZE);
160 goto out;
161 }
162
163 rtnl_notify(skb, net, portid, RTNLGRP_MCTP_IFADDR, req_nlh, GFP_KERNEL);
164 return;
165out:
166 kfree_skb(skb);
167 rtnl_set_sk_err(net, RTNLGRP_MCTP_IFADDR, rc);
168}
169
Jeremy Kerr583be982021-07-29 10:20:44 +0800170static const struct nla_policy ifa_mctp_policy[IFA_MAX + 1] = {
171 [IFA_ADDRESS] = { .type = NLA_U8 },
172 [IFA_LOCAL] = { .type = NLA_U8 },
173};
174
175static int mctp_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
176 struct netlink_ext_ack *extack)
177{
178 struct net *net = sock_net(skb->sk);
179 struct nlattr *tb[IFA_MAX + 1];
180 struct net_device *dev;
181 struct mctp_addr *addr;
182 struct mctp_dev *mdev;
183 struct ifaddrmsg *ifm;
184 unsigned long flags;
185 u8 *tmp_addrs;
186 int rc;
187
188 rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_mctp_policy,
189 extack);
190 if (rc < 0)
191 return rc;
192
193 ifm = nlmsg_data(nlh);
194
195 if (tb[IFA_LOCAL])
196 addr = nla_data(tb[IFA_LOCAL]);
197 else if (tb[IFA_ADDRESS])
198 addr = nla_data(tb[IFA_ADDRESS]);
199 else
200 return -EINVAL;
201
202 /* find device */
203 dev = __dev_get_by_index(net, ifm->ifa_index);
204 if (!dev)
205 return -ENODEV;
206
207 mdev = mctp_dev_get_rtnl(dev);
208 if (!mdev)
209 return -ENODEV;
210
211 if (!mctp_address_ok(addr->s_addr))
212 return -EINVAL;
213
214 /* Prevent duplicates. Under RTNL so don't need to lock for reading */
215 if (memchr(mdev->addrs, addr->s_addr, mdev->num_addrs))
216 return -EEXIST;
217
218 tmp_addrs = kmalloc(mdev->num_addrs + 1, GFP_KERNEL);
219 if (!tmp_addrs)
220 return -ENOMEM;
221 memcpy(tmp_addrs, mdev->addrs, mdev->num_addrs);
222 tmp_addrs[mdev->num_addrs] = addr->s_addr;
223
224 /* Lock to write */
225 spin_lock_irqsave(&mdev->addrs_lock, flags);
226 mdev->num_addrs++;
227 swap(mdev->addrs, tmp_addrs);
228 spin_unlock_irqrestore(&mdev->addrs_lock, flags);
229
230 kfree(tmp_addrs);
231
Matt Johnstondbcefde2021-12-20 10:31:04 +0800232 mctp_addr_notify(mdev, addr->s_addr, RTM_NEWADDR, skb, nlh);
Jeremy Kerr889b7da2021-07-29 10:20:45 +0800233 mctp_route_add_local(mdev, addr->s_addr);
234
Jeremy Kerr583be982021-07-29 10:20:44 +0800235 return 0;
236}
237
238static int mctp_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
239 struct netlink_ext_ack *extack)
240{
241 struct net *net = sock_net(skb->sk);
242 struct nlattr *tb[IFA_MAX + 1];
243 struct net_device *dev;
244 struct mctp_addr *addr;
245 struct mctp_dev *mdev;
246 struct ifaddrmsg *ifm;
247 unsigned long flags;
248 u8 *pos;
249 int rc;
250
251 rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_mctp_policy,
252 extack);
253 if (rc < 0)
254 return rc;
255
256 ifm = nlmsg_data(nlh);
257
258 if (tb[IFA_LOCAL])
259 addr = nla_data(tb[IFA_LOCAL]);
260 else if (tb[IFA_ADDRESS])
261 addr = nla_data(tb[IFA_ADDRESS]);
262 else
263 return -EINVAL;
264
265 /* find device */
266 dev = __dev_get_by_index(net, ifm->ifa_index);
267 if (!dev)
268 return -ENODEV;
269
270 mdev = mctp_dev_get_rtnl(dev);
271 if (!mdev)
272 return -ENODEV;
273
274 pos = memchr(mdev->addrs, addr->s_addr, mdev->num_addrs);
275 if (!pos)
276 return -ENOENT;
277
Jeremy Kerr889b7da2021-07-29 10:20:45 +0800278 rc = mctp_route_remove_local(mdev, addr->s_addr);
279 // we can ignore -ENOENT in the case a route was already removed
280 if (rc < 0 && rc != -ENOENT)
281 return rc;
282
Jeremy Kerr583be982021-07-29 10:20:44 +0800283 spin_lock_irqsave(&mdev->addrs_lock, flags);
284 memmove(pos, pos + 1, mdev->num_addrs - 1 - (pos - mdev->addrs));
285 mdev->num_addrs--;
286 spin_unlock_irqrestore(&mdev->addrs_lock, flags);
287
Matt Johnstondbcefde2021-12-20 10:31:04 +0800288 mctp_addr_notify(mdev, addr->s_addr, RTM_DELADDR, skb, nlh);
289
Jeremy Kerr583be982021-07-29 10:20:44 +0800290 return 0;
291}
292
Jeremy Kerr43f55f22021-09-29 15:26:08 +0800293void mctp_dev_hold(struct mctp_dev *mdev)
294{
295 refcount_inc(&mdev->refs);
296}
297
298void mctp_dev_put(struct mctp_dev *mdev)
299{
300 if (refcount_dec_and_test(&mdev->refs)) {
301 dev_put(mdev->dev);
302 kfree_rcu(mdev, rcu);
303 }
304}
305
Jeremy Kerr67737c42021-10-29 11:01:45 +0800306void mctp_dev_release_key(struct mctp_dev *dev, struct mctp_sk_key *key)
307 __must_hold(&key->lock)
308{
309 if (!dev)
310 return;
311 if (dev->ops && dev->ops->release_flow)
312 dev->ops->release_flow(dev, key);
313 key->dev = NULL;
314 mctp_dev_put(dev);
315}
316
317void mctp_dev_set_key(struct mctp_dev *dev, struct mctp_sk_key *key)
318 __must_hold(&key->lock)
319{
320 mctp_dev_hold(dev);
321 key->dev = dev;
322}
323
Jeremy Kerr583be982021-07-29 10:20:44 +0800324static struct mctp_dev *mctp_add_dev(struct net_device *dev)
325{
326 struct mctp_dev *mdev;
327
328 ASSERT_RTNL();
329
330 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
331 if (!mdev)
332 return ERR_PTR(-ENOMEM);
333
334 spin_lock_init(&mdev->addrs_lock);
335
Matt Johnston03f2bbc2021-07-29 10:20:52 +0800336 mdev->net = mctp_default_net(dev_net(dev));
Jeremy Kerr583be982021-07-29 10:20:44 +0800337
338 /* associate to net_device */
Jeremy Kerr43f55f22021-09-29 15:26:08 +0800339 refcount_set(&mdev->refs, 1);
Jeremy Kerr583be982021-07-29 10:20:44 +0800340 rcu_assign_pointer(dev->mctp_ptr, mdev);
Jeremy Kerr43f55f22021-09-29 15:26:08 +0800341
Jeremy Kerr583be982021-07-29 10:20:44 +0800342 dev_hold(dev);
343 mdev->dev = dev;
344
345 return mdev;
346}
347
348static int mctp_fill_link_af(struct sk_buff *skb,
349 const struct net_device *dev, u32 ext_filter_mask)
350{
351 struct mctp_dev *mdev;
352
353 mdev = mctp_dev_get_rtnl(dev);
354 if (!mdev)
355 return -ENODATA;
356 if (nla_put_u32(skb, IFLA_MCTP_NET, mdev->net))
357 return -EMSGSIZE;
358 return 0;
359}
360
361static size_t mctp_get_link_af_size(const struct net_device *dev,
362 u32 ext_filter_mask)
363{
364 struct mctp_dev *mdev;
365 unsigned int ret;
366
367 /* caller holds RCU */
368 mdev = __mctp_dev_get(dev);
369 if (!mdev)
370 return 0;
371 ret = nla_total_size(4); /* IFLA_MCTP_NET */
372 return ret;
373}
374
375static const struct nla_policy ifla_af_mctp_policy[IFLA_MCTP_MAX + 1] = {
376 [IFLA_MCTP_NET] = { .type = NLA_U32 },
377};
378
379static int mctp_set_link_af(struct net_device *dev, const struct nlattr *attr,
380 struct netlink_ext_ack *extack)
381{
382 struct nlattr *tb[IFLA_MCTP_MAX + 1];
383 struct mctp_dev *mdev;
384 int rc;
385
386 rc = nla_parse_nested(tb, IFLA_MCTP_MAX, attr, ifla_af_mctp_policy,
387 NULL);
388 if (rc)
389 return rc;
390
391 mdev = mctp_dev_get_rtnl(dev);
392 if (!mdev)
393 return 0;
394
395 if (tb[IFLA_MCTP_NET])
396 WRITE_ONCE(mdev->net, nla_get_u32(tb[IFLA_MCTP_NET]));
397
398 return 0;
399}
400
Matt Johnston7b1871a2021-09-29 15:26:14 +0800401/* Matches netdev types that should have MCTP handling */
402static bool mctp_known(struct net_device *dev)
403{
404 /* only register specific types (inc. NONE for TUN devices) */
405 return dev->type == ARPHRD_MCTP ||
406 dev->type == ARPHRD_LOOPBACK ||
407 dev->type == ARPHRD_NONE;
408}
409
Jeremy Kerr583be982021-07-29 10:20:44 +0800410static void mctp_unregister(struct net_device *dev)
411{
412 struct mctp_dev *mdev;
413
414 mdev = mctp_dev_get_rtnl(dev);
Matt Johnston7b1871a2021-09-29 15:26:14 +0800415 if (mctp_known(dev) != (bool)mdev) {
416 // Sanity check, should match what was set in mctp_register
417 netdev_warn(dev, "%s: mdev pointer %d but type (%d) match is %d",
418 __func__, (bool)mdev, mctp_known(dev), dev->type);
419 return;
420 }
Jeremy Kerr583be982021-07-29 10:20:44 +0800421 if (!mdev)
422 return;
423
424 RCU_INIT_POINTER(mdev->dev->mctp_ptr, NULL);
425
Jeremy Kerr889b7da2021-07-29 10:20:45 +0800426 mctp_route_remove_dev(mdev);
Matt Johnston4d8b9312021-07-29 10:20:47 +0800427 mctp_neigh_remove_dev(mdev);
Jeremy Kerr583be982021-07-29 10:20:44 +0800428 kfree(mdev->addrs);
429
Jeremy Kerr43f55f22021-09-29 15:26:08 +0800430 mctp_dev_put(mdev);
Jeremy Kerr583be982021-07-29 10:20:44 +0800431}
432
433static int mctp_register(struct net_device *dev)
434{
435 struct mctp_dev *mdev;
436
437 /* Already registered? */
Matt Johnston7b1871a2021-09-29 15:26:14 +0800438 mdev = rtnl_dereference(dev->mctp_ptr);
Jeremy Kerr583be982021-07-29 10:20:44 +0800439
Matt Johnston7b1871a2021-09-29 15:26:14 +0800440 if (mdev) {
441 if (!mctp_known(dev))
442 netdev_warn(dev, "%s: mctp_dev set for unknown type %d",
443 __func__, dev->type);
Jeremy Kerr583be982021-07-29 10:20:44 +0800444 return 0;
Matt Johnstonf364dd72021-09-29 15:26:05 +0800445 }
Jeremy Kerr583be982021-07-29 10:20:44 +0800446
Matt Johnston7b1871a2021-09-29 15:26:14 +0800447 /* only register specific types */
448 if (!mctp_known(dev))
449 return 0;
450
Jeremy Kerr583be982021-07-29 10:20:44 +0800451 mdev = mctp_add_dev(dev);
452 if (IS_ERR(mdev))
453 return PTR_ERR(mdev);
454
455 return 0;
456}
457
458static int mctp_dev_notify(struct notifier_block *this, unsigned long event,
459 void *ptr)
460{
461 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
462 int rc;
463
464 switch (event) {
465 case NETDEV_REGISTER:
466 rc = mctp_register(dev);
467 if (rc)
468 return notifier_from_errno(rc);
469 break;
470 case NETDEV_UNREGISTER:
471 mctp_unregister(dev);
472 break;
473 }
474
475 return NOTIFY_OK;
476}
477
Jeremy Kerr67737c42021-10-29 11:01:45 +0800478static int mctp_register_netdevice(struct net_device *dev,
479 const struct mctp_netdev_ops *ops)
480{
481 struct mctp_dev *mdev;
482
483 mdev = mctp_add_dev(dev);
484 if (IS_ERR(mdev))
485 return PTR_ERR(mdev);
486
487 mdev->ops = ops;
488
489 return register_netdevice(dev);
490}
491
492int mctp_register_netdev(struct net_device *dev,
493 const struct mctp_netdev_ops *ops)
494{
495 int rc;
496
497 rtnl_lock();
498 rc = mctp_register_netdevice(dev, ops);
499 rtnl_unlock();
500
501 return rc;
502}
503EXPORT_SYMBOL_GPL(mctp_register_netdev);
504
505void mctp_unregister_netdev(struct net_device *dev)
506{
507 unregister_netdev(dev);
508}
509EXPORT_SYMBOL_GPL(mctp_unregister_netdev);
510
Jeremy Kerr583be982021-07-29 10:20:44 +0800511static struct rtnl_af_ops mctp_af_ops = {
512 .family = AF_MCTP,
513 .fill_link_af = mctp_fill_link_af,
514 .get_link_af_size = mctp_get_link_af_size,
515 .set_link_af = mctp_set_link_af,
516};
517
518static struct notifier_block mctp_dev_nb = {
519 .notifier_call = mctp_dev_notify,
520 .priority = ADDRCONF_NOTIFY_PRIORITY,
521};
522
523void __init mctp_device_init(void)
524{
525 register_netdevice_notifier(&mctp_dev_nb);
526
527 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETADDR,
528 NULL, mctp_dump_addrinfo, 0);
529 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWADDR,
530 mctp_rtm_newaddr, NULL, 0);
531 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELADDR,
532 mctp_rtm_deladdr, NULL, 0);
533 rtnl_af_register(&mctp_af_ops);
534}
535
536void __exit mctp_device_exit(void)
537{
538 rtnl_af_unregister(&mctp_af_ops);
539 rtnl_unregister(PF_MCTP, RTM_DELADDR);
540 rtnl_unregister(PF_MCTP, RTM_NEWADDR);
541 rtnl_unregister(PF_MCTP, RTM_GETADDR);
542
543 unregister_netdevice_notifier(&mctp_dev_nb);
544}