1 // SPDX-License-Identifier: GPL-2.0
3 * Management Component Transport Protocol (MCTP) - device implementation.
5 * Copyright (c) 2021 Code Construct
6 * Copyright (c) 2021 Google
9 #include <linux/if_arp.h>
10 #include <linux/if_link.h>
11 #include <linux/mctp.h>
12 #include <linux/netdevice.h>
13 #include <linux/rcupdate.h>
14 #include <linux/rtnetlink.h>
16 #include <net/addrconf.h>
17 #include <net/netlink.h>
19 #include <net/mctpdevice.h>
23 unsigned long ifindex;
27 /* unlocked: caller must hold rcu_read_lock.
28 * Returned mctp_dev has its refcount incremented, or NULL if unset.
30 struct mctp_dev *__mctp_dev_get(const struct net_device *dev)
32 struct mctp_dev *mdev = rcu_dereference(dev->mctp_ptr);
34 /* RCU guarantees that any mdev is still live.
35 * Zero refcount implies a pending free, return NULL.
38 if (!refcount_inc_not_zero(&mdev->refs))
43 /* Returned mctp_dev does not have refcount incremented. The returned pointer
44 * remains live while rtnl_lock is held, as that prevents mctp_unregister()
46 struct mctp_dev *mctp_dev_get_rtnl(const struct net_device *dev)
48 return rtnl_dereference(dev->mctp_ptr);
51 static int mctp_addrinfo_size(void)
53 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
54 + nla_total_size(1) // IFA_LOCAL
55 + nla_total_size(1) // IFA_ADDRESS
59 /* flag should be NLM_F_MULTI for dump calls */
60 static int mctp_fill_addrinfo(struct sk_buff *skb,
61 struct mctp_dev *mdev, mctp_eid_t eid,
62 int msg_type, u32 portid, u32 seq, int flag)
64 struct ifaddrmsg *hdr;
67 nlh = nlmsg_put(skb, portid, seq,
68 msg_type, sizeof(*hdr), flag);
72 hdr = nlmsg_data(nlh);
73 hdr->ifa_family = AF_MCTP;
74 hdr->ifa_prefixlen = 0;
77 hdr->ifa_index = mdev->dev->ifindex;
79 if (nla_put_u8(skb, IFA_LOCAL, eid))
82 if (nla_put_u8(skb, IFA_ADDRESS, eid))
90 nlmsg_cancel(skb, nlh);
94 static int mctp_dump_dev_addrinfo(struct mctp_dev *mdev, struct sk_buff *skb,
95 struct netlink_callback *cb)
97 struct mctp_dump_cb *mcb = (void *)cb->ctx;
101 portid = NETLINK_CB(cb->skb).portid;
102 seq = cb->nlh->nlmsg_seq;
103 for (; mcb->a_idx < mdev->num_addrs; mcb->a_idx++) {
104 rc = mctp_fill_addrinfo(skb, mdev, mdev->addrs[mcb->a_idx],
105 RTM_NEWADDR, portid, seq, NLM_F_MULTI);
113 static int mctp_dump_addrinfo(struct sk_buff *skb, struct netlink_callback *cb)
115 struct mctp_dump_cb *mcb = (void *)cb->ctx;
116 struct net *net = sock_net(skb->sk);
117 struct net_device *dev;
118 struct ifaddrmsg *hdr;
119 struct mctp_dev *mdev;
122 hdr = nlmsg_data(cb->nlh);
123 // filter by ifindex if requested
124 ifindex = hdr->ifa_index;
127 for_each_netdev_dump(net, dev, mcb->ifindex) {
128 if (ifindex && ifindex != dev->ifindex)
130 mdev = __mctp_dev_get(dev);
133 rc = mctp_dump_dev_addrinfo(mdev, skb, cb);
144 static 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)
147 u32 portid = NETLINK_CB(req_skb).portid;
148 struct net *net = dev_net(mdev->dev);
152 skb = nlmsg_new(mctp_addrinfo_size(), GFP_KERNEL);
156 rc = mctp_fill_addrinfo(skb, mdev, eid, msg_type,
157 portid, req_nlh->nlmsg_seq, 0);
159 WARN_ON_ONCE(rc == -EMSGSIZE);
163 rtnl_notify(skb, net, portid, RTNLGRP_MCTP_IFADDR, req_nlh, GFP_KERNEL);
167 rtnl_set_sk_err(net, RTNLGRP_MCTP_IFADDR, rc);
170 static const struct nla_policy ifa_mctp_policy[IFA_MAX + 1] = {
171 [IFA_ADDRESS] = { .type = NLA_U8 },
172 [IFA_LOCAL] = { .type = NLA_U8 },
175 static int mctp_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
176 struct netlink_ext_ack *extack)
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;
188 rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_mctp_policy,
193 ifm = nlmsg_data(nlh);
196 addr = nla_data(tb[IFA_LOCAL]);
197 else if (tb[IFA_ADDRESS])
198 addr = nla_data(tb[IFA_ADDRESS]);
203 dev = __dev_get_by_index(net, ifm->ifa_index);
207 mdev = mctp_dev_get_rtnl(dev);
211 if (!mctp_address_unicast(addr->s_addr))
214 /* Prevent duplicates. Under RTNL so don't need to lock for reading */
215 if (memchr(mdev->addrs, addr->s_addr, mdev->num_addrs))
218 tmp_addrs = kmalloc(mdev->num_addrs + 1, GFP_KERNEL);
221 memcpy(tmp_addrs, mdev->addrs, mdev->num_addrs);
222 tmp_addrs[mdev->num_addrs] = addr->s_addr;
225 spin_lock_irqsave(&mdev->addrs_lock, flags);
227 swap(mdev->addrs, tmp_addrs);
228 spin_unlock_irqrestore(&mdev->addrs_lock, flags);
232 mctp_addr_notify(mdev, addr->s_addr, RTM_NEWADDR, skb, nlh);
233 mctp_route_add_local(mdev, addr->s_addr);
238 static int mctp_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
239 struct netlink_ext_ack *extack)
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;
251 rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_mctp_policy,
256 ifm = nlmsg_data(nlh);
259 addr = nla_data(tb[IFA_LOCAL]);
260 else if (tb[IFA_ADDRESS])
261 addr = nla_data(tb[IFA_ADDRESS]);
266 dev = __dev_get_by_index(net, ifm->ifa_index);
270 mdev = mctp_dev_get_rtnl(dev);
274 pos = memchr(mdev->addrs, addr->s_addr, mdev->num_addrs);
278 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)
283 spin_lock_irqsave(&mdev->addrs_lock, flags);
284 memmove(pos, pos + 1, mdev->num_addrs - 1 - (pos - mdev->addrs));
286 spin_unlock_irqrestore(&mdev->addrs_lock, flags);
288 mctp_addr_notify(mdev, addr->s_addr, RTM_DELADDR, skb, nlh);
293 void mctp_dev_hold(struct mctp_dev *mdev)
295 refcount_inc(&mdev->refs);
298 void mctp_dev_put(struct mctp_dev *mdev)
300 if (mdev && refcount_dec_and_test(&mdev->refs)) {
303 kfree_rcu(mdev, rcu);
307 void mctp_dev_release_key(struct mctp_dev *dev, struct mctp_sk_key *key)
308 __must_hold(&key->lock)
312 if (dev->ops && dev->ops->release_flow)
313 dev->ops->release_flow(dev, key);
318 void mctp_dev_set_key(struct mctp_dev *dev, struct mctp_sk_key *key)
319 __must_hold(&key->lock)
325 static struct mctp_dev *mctp_add_dev(struct net_device *dev)
327 struct mctp_dev *mdev;
331 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
333 return ERR_PTR(-ENOMEM);
335 spin_lock_init(&mdev->addrs_lock);
337 mdev->net = mctp_default_net(dev_net(dev));
339 /* associate to net_device */
340 refcount_set(&mdev->refs, 1);
341 rcu_assign_pointer(dev->mctp_ptr, mdev);
349 static int mctp_fill_link_af(struct sk_buff *skb,
350 const struct net_device *dev, u32 ext_filter_mask)
352 struct mctp_dev *mdev;
354 mdev = mctp_dev_get_rtnl(dev);
357 if (nla_put_u32(skb, IFLA_MCTP_NET, mdev->net))
359 if (nla_put_u8(skb, IFLA_MCTP_PHYS_BINDING, mdev->binding))
364 static size_t mctp_get_link_af_size(const struct net_device *dev,
367 struct mctp_dev *mdev;
370 /* caller holds RCU */
371 mdev = __mctp_dev_get(dev);
374 ret = nla_total_size(4); /* IFLA_MCTP_NET */
375 ret += nla_total_size(1); /* IFLA_MCTP_PHYS_BINDING */
380 static const struct nla_policy ifla_af_mctp_policy[IFLA_MCTP_MAX + 1] = {
381 [IFLA_MCTP_NET] = { .type = NLA_U32 },
384 static int mctp_set_link_af(struct net_device *dev, const struct nlattr *attr,
385 struct netlink_ext_ack *extack)
387 struct nlattr *tb[IFLA_MCTP_MAX + 1];
388 struct mctp_dev *mdev;
391 rc = nla_parse_nested(tb, IFLA_MCTP_MAX, attr, ifla_af_mctp_policy,
396 mdev = mctp_dev_get_rtnl(dev);
400 if (tb[IFLA_MCTP_NET])
401 WRITE_ONCE(mdev->net, nla_get_u32(tb[IFLA_MCTP_NET]));
406 /* Matches netdev types that should have MCTP handling */
407 static bool mctp_known(struct net_device *dev)
409 /* only register specific types (inc. NONE for TUN devices) */
410 return dev->type == ARPHRD_MCTP ||
411 dev->type == ARPHRD_LOOPBACK ||
412 dev->type == ARPHRD_NONE;
415 static void mctp_unregister(struct net_device *dev)
417 struct mctp_dev *mdev;
419 mdev = mctp_dev_get_rtnl(dev);
423 RCU_INIT_POINTER(mdev->dev->mctp_ptr, NULL);
425 mctp_route_remove_dev(mdev);
426 mctp_neigh_remove_dev(mdev);
431 static int mctp_register(struct net_device *dev)
433 struct mctp_dev *mdev;
435 /* Already registered? */
436 if (rtnl_dereference(dev->mctp_ptr))
439 /* only register specific types */
440 if (!mctp_known(dev))
443 mdev = mctp_add_dev(dev);
445 return PTR_ERR(mdev);
450 static int mctp_dev_notify(struct notifier_block *this, unsigned long event,
453 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
457 case NETDEV_REGISTER:
458 rc = mctp_register(dev);
460 return notifier_from_errno(rc);
462 case NETDEV_UNREGISTER:
463 mctp_unregister(dev);
470 static int mctp_register_netdevice(struct net_device *dev,
471 const struct mctp_netdev_ops *ops,
472 enum mctp_phys_binding binding)
474 struct mctp_dev *mdev;
476 mdev = mctp_add_dev(dev);
478 return PTR_ERR(mdev);
481 mdev->binding = binding;
483 return register_netdevice(dev);
486 int mctp_register_netdev(struct net_device *dev,
487 const struct mctp_netdev_ops *ops,
488 enum mctp_phys_binding binding)
493 rc = mctp_register_netdevice(dev, ops, binding);
498 EXPORT_SYMBOL_GPL(mctp_register_netdev);
500 void mctp_unregister_netdev(struct net_device *dev)
502 unregister_netdev(dev);
504 EXPORT_SYMBOL_GPL(mctp_unregister_netdev);
506 static struct rtnl_af_ops mctp_af_ops = {
508 .fill_link_af = mctp_fill_link_af,
509 .get_link_af_size = mctp_get_link_af_size,
510 .set_link_af = mctp_set_link_af,
513 static struct notifier_block mctp_dev_nb = {
514 .notifier_call = mctp_dev_notify,
515 .priority = ADDRCONF_NOTIFY_PRIORITY,
518 static const struct rtnl_msg_handler mctp_device_rtnl_msg_handlers[] = {
519 {.owner = THIS_MODULE, .protocol = PF_MCTP, .msgtype = RTM_NEWADDR,
520 .doit = mctp_rtm_newaddr},
521 {.owner = THIS_MODULE, .protocol = PF_MCTP, .msgtype = RTM_DELADDR,
522 .doit = mctp_rtm_deladdr},
523 {.owner = THIS_MODULE, .protocol = PF_MCTP, .msgtype = RTM_GETADDR,
524 .dumpit = mctp_dump_addrinfo},
527 int __init mctp_device_init(void)
531 register_netdevice_notifier(&mctp_dev_nb);
533 err = rtnl_af_register(&mctp_af_ops);
537 err = rtnl_register_many(mctp_device_rtnl_msg_handlers);
543 rtnl_af_unregister(&mctp_af_ops);
545 unregister_netdevice_notifier(&mctp_dev_nb);
549 void __exit mctp_device_exit(void)
551 rtnl_unregister_many(mctp_device_rtnl_msg_handlers);
552 rtnl_af_unregister(&mctp_af_ops);
553 unregister_netdevice_notifier(&mctp_dev_nb);