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_link.h>
10 #include <linux/mctp.h>
11 #include <linux/netdevice.h>
12 #include <linux/rcupdate.h>
13 #include <linux/rtnetlink.h>
15 #include <net/addrconf.h>
16 #include <net/netlink.h>
18 #include <net/mctpdevice.h>
27 /* unlocked: caller must hold rcu_read_lock */
28 struct mctp_dev *__mctp_dev_get(const struct net_device *dev)
30 return rcu_dereference(dev->mctp_ptr);
33 struct mctp_dev *mctp_dev_get_rtnl(const struct net_device *dev)
35 return rtnl_dereference(dev->mctp_ptr);
38 static void mctp_dev_destroy(struct mctp_dev *mdev)
40 struct net_device *dev = mdev->dev;
46 static int mctp_fill_addrinfo(struct sk_buff *skb, struct netlink_callback *cb,
47 struct mctp_dev *mdev, mctp_eid_t eid)
49 struct ifaddrmsg *hdr;
52 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
53 RTM_NEWADDR, sizeof(*hdr), NLM_F_MULTI);
57 hdr = nlmsg_data(nlh);
58 hdr->ifa_family = AF_MCTP;
59 hdr->ifa_prefixlen = 0;
62 hdr->ifa_index = mdev->dev->ifindex;
64 if (nla_put_u8(skb, IFA_LOCAL, eid))
67 if (nla_put_u8(skb, IFA_ADDRESS, eid))
75 nlmsg_cancel(skb, nlh);
79 static int mctp_dump_dev_addrinfo(struct mctp_dev *mdev, struct sk_buff *skb,
80 struct netlink_callback *cb)
82 struct mctp_dump_cb *mcb = (void *)cb->ctx;
85 for (; mcb->a_idx < mdev->num_addrs; mcb->a_idx++) {
86 rc = mctp_fill_addrinfo(skb, cb, mdev, mdev->addrs[mcb->a_idx]);
94 static int mctp_dump_addrinfo(struct sk_buff *skb, struct netlink_callback *cb)
96 struct mctp_dump_cb *mcb = (void *)cb->ctx;
97 struct net *net = sock_net(skb->sk);
98 struct hlist_head *head;
99 struct net_device *dev;
100 struct ifaddrmsg *hdr;
101 struct mctp_dev *mdev;
105 hdr = nlmsg_data(cb->nlh);
106 // filter by ifindex if requested
107 ifindex = hdr->ifa_index;
110 for (; mcb->h < NETDEV_HASHENTRIES; mcb->h++, mcb->idx = 0) {
112 head = &net->dev_index_head[mcb->h];
113 hlist_for_each_entry_rcu(dev, head, index_hlist) {
114 if (idx >= mcb->idx &&
115 (ifindex == 0 || ifindex == dev->ifindex)) {
116 mdev = __mctp_dev_get(dev);
118 rc = mctp_dump_dev_addrinfo(mdev,
120 // Error indicates full buffer, this
121 // callback will get retried.
127 // reset for next iteration
138 static const struct nla_policy ifa_mctp_policy[IFA_MAX + 1] = {
139 [IFA_ADDRESS] = { .type = NLA_U8 },
140 [IFA_LOCAL] = { .type = NLA_U8 },
143 static int mctp_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
144 struct netlink_ext_ack *extack)
146 struct net *net = sock_net(skb->sk);
147 struct nlattr *tb[IFA_MAX + 1];
148 struct net_device *dev;
149 struct mctp_addr *addr;
150 struct mctp_dev *mdev;
151 struct ifaddrmsg *ifm;
156 rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_mctp_policy,
161 ifm = nlmsg_data(nlh);
164 addr = nla_data(tb[IFA_LOCAL]);
165 else if (tb[IFA_ADDRESS])
166 addr = nla_data(tb[IFA_ADDRESS]);
171 dev = __dev_get_by_index(net, ifm->ifa_index);
175 mdev = mctp_dev_get_rtnl(dev);
179 if (!mctp_address_ok(addr->s_addr))
182 /* Prevent duplicates. Under RTNL so don't need to lock for reading */
183 if (memchr(mdev->addrs, addr->s_addr, mdev->num_addrs))
186 tmp_addrs = kmalloc(mdev->num_addrs + 1, GFP_KERNEL);
189 memcpy(tmp_addrs, mdev->addrs, mdev->num_addrs);
190 tmp_addrs[mdev->num_addrs] = addr->s_addr;
193 spin_lock_irqsave(&mdev->addrs_lock, flags);
195 swap(mdev->addrs, tmp_addrs);
196 spin_unlock_irqrestore(&mdev->addrs_lock, flags);
200 mctp_route_add_local(mdev, addr->s_addr);
205 static int mctp_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
206 struct netlink_ext_ack *extack)
208 struct net *net = sock_net(skb->sk);
209 struct nlattr *tb[IFA_MAX + 1];
210 struct net_device *dev;
211 struct mctp_addr *addr;
212 struct mctp_dev *mdev;
213 struct ifaddrmsg *ifm;
218 rc = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_mctp_policy,
223 ifm = nlmsg_data(nlh);
226 addr = nla_data(tb[IFA_LOCAL]);
227 else if (tb[IFA_ADDRESS])
228 addr = nla_data(tb[IFA_ADDRESS]);
233 dev = __dev_get_by_index(net, ifm->ifa_index);
237 mdev = mctp_dev_get_rtnl(dev);
241 pos = memchr(mdev->addrs, addr->s_addr, mdev->num_addrs);
245 rc = mctp_route_remove_local(mdev, addr->s_addr);
246 // we can ignore -ENOENT in the case a route was already removed
247 if (rc < 0 && rc != -ENOENT)
250 spin_lock_irqsave(&mdev->addrs_lock, flags);
251 memmove(pos, pos + 1, mdev->num_addrs - 1 - (pos - mdev->addrs));
253 spin_unlock_irqrestore(&mdev->addrs_lock, flags);
258 static struct mctp_dev *mctp_add_dev(struct net_device *dev)
260 struct mctp_dev *mdev;
264 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
266 return ERR_PTR(-ENOMEM);
268 spin_lock_init(&mdev->addrs_lock);
270 mdev->net = mctp_default_net(dev_net(dev));
272 /* associate to net_device */
273 rcu_assign_pointer(dev->mctp_ptr, mdev);
280 static int mctp_fill_link_af(struct sk_buff *skb,
281 const struct net_device *dev, u32 ext_filter_mask)
283 struct mctp_dev *mdev;
285 mdev = mctp_dev_get_rtnl(dev);
288 if (nla_put_u32(skb, IFLA_MCTP_NET, mdev->net))
293 static size_t mctp_get_link_af_size(const struct net_device *dev,
296 struct mctp_dev *mdev;
299 /* caller holds RCU */
300 mdev = __mctp_dev_get(dev);
303 ret = nla_total_size(4); /* IFLA_MCTP_NET */
307 static const struct nla_policy ifla_af_mctp_policy[IFLA_MCTP_MAX + 1] = {
308 [IFLA_MCTP_NET] = { .type = NLA_U32 },
311 static int mctp_set_link_af(struct net_device *dev, const struct nlattr *attr,
312 struct netlink_ext_ack *extack)
314 struct nlattr *tb[IFLA_MCTP_MAX + 1];
315 struct mctp_dev *mdev;
318 rc = nla_parse_nested(tb, IFLA_MCTP_MAX, attr, ifla_af_mctp_policy,
323 mdev = mctp_dev_get_rtnl(dev);
327 if (tb[IFLA_MCTP_NET])
328 WRITE_ONCE(mdev->net, nla_get_u32(tb[IFLA_MCTP_NET]));
333 static void mctp_unregister(struct net_device *dev)
335 struct mctp_dev *mdev;
337 mdev = mctp_dev_get_rtnl(dev);
342 RCU_INIT_POINTER(mdev->dev->mctp_ptr, NULL);
344 mctp_route_remove_dev(mdev);
345 mctp_neigh_remove_dev(mdev);
348 mctp_dev_destroy(mdev);
351 static int mctp_register(struct net_device *dev)
353 struct mctp_dev *mdev;
355 /* Already registered? */
356 if (rtnl_dereference(dev->mctp_ptr))
359 /* only register specific types; MCTP-specific and loopback for now */
360 if (dev->type != ARPHRD_MCTP && dev->type != ARPHRD_LOOPBACK)
363 mdev = mctp_add_dev(dev);
365 return PTR_ERR(mdev);
370 static int mctp_dev_notify(struct notifier_block *this, unsigned long event,
373 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
377 case NETDEV_REGISTER:
378 rc = mctp_register(dev);
380 return notifier_from_errno(rc);
382 case NETDEV_UNREGISTER:
383 mctp_unregister(dev);
390 static struct rtnl_af_ops mctp_af_ops = {
392 .fill_link_af = mctp_fill_link_af,
393 .get_link_af_size = mctp_get_link_af_size,
394 .set_link_af = mctp_set_link_af,
397 static struct notifier_block mctp_dev_nb = {
398 .notifier_call = mctp_dev_notify,
399 .priority = ADDRCONF_NOTIFY_PRIORITY,
402 void __init mctp_device_init(void)
404 register_netdevice_notifier(&mctp_dev_nb);
406 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETADDR,
407 NULL, mctp_dump_addrinfo, 0);
408 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWADDR,
409 mctp_rtm_newaddr, NULL, 0);
410 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELADDR,
411 mctp_rtm_deladdr, NULL, 0);
412 rtnl_af_register(&mctp_af_ops);
415 void __exit mctp_device_exit(void)
417 rtnl_af_unregister(&mctp_af_ops);
418 rtnl_unregister(PF_MCTP, RTM_DELADDR);
419 rtnl_unregister(PF_MCTP, RTM_NEWADDR);
420 rtnl_unregister(PF_MCTP, RTM_GETADDR);
422 unregister_netdevice_notifier(&mctp_dev_nb);