1 // 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 21 struct mctp_dump_cb { 22 int h; 23 int idx; 24 size_t a_idx; 25 }; 26 27 /* unlocked: caller must hold rcu_read_lock */ 28 struct mctp_dev *__mctp_dev_get(const struct net_device *dev) 29 { 30 return rcu_dereference(dev->mctp_ptr); 31 } 32 33 struct mctp_dev *mctp_dev_get_rtnl(const struct net_device *dev) 34 { 35 return rtnl_dereference(dev->mctp_ptr); 36 } 37 38 static 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 66 cancel: 67 nlmsg_cancel(skb, nlh); 68 return -EMSGSIZE; 69 } 70 71 static 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 86 static 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 } 123 out: 124 rcu_read_unlock(); 125 mcb->idx = idx; 126 127 return skb->len; 128 } 129 130 static const struct nla_policy ifa_mctp_policy[IFA_MAX + 1] = { 131 [IFA_ADDRESS] = { .type = NLA_U8 }, 132 [IFA_LOCAL] = { .type = NLA_U8 }, 133 }; 134 135 static 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 192 mctp_route_add_local(mdev, addr->s_addr); 193 194 return 0; 195 } 196 197 static 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 237 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 242 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 250 void mctp_dev_hold(struct mctp_dev *mdev) 251 { 252 refcount_inc(&mdev->refs); 253 } 254 255 void 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 263 static 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 275 mdev->net = mctp_default_net(dev_net(dev)); 276 277 /* associate to net_device */ 278 refcount_set(&mdev->refs, 1); 279 rcu_assign_pointer(dev->mctp_ptr, mdev); 280 281 dev_hold(dev); 282 mdev->dev = dev; 283 284 return mdev; 285 } 286 287 static 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 300 static 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 314 static const struct nla_policy ifla_af_mctp_policy[IFLA_MCTP_MAX + 1] = { 315 [IFLA_MCTP_NET] = { .type = NLA_U32 }, 316 }; 317 318 static 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 340 /* Matches netdev types that should have MCTP handling */ 341 static bool mctp_known(struct net_device *dev) 342 { 343 /* only register specific types (inc. NONE for TUN devices) */ 344 return dev->type == ARPHRD_MCTP || 345 dev->type == ARPHRD_LOOPBACK || 346 dev->type == ARPHRD_NONE; 347 } 348 349 static void mctp_unregister(struct net_device *dev) 350 { 351 struct mctp_dev *mdev; 352 353 mdev = mctp_dev_get_rtnl(dev); 354 if (mctp_known(dev) != (bool)mdev) { 355 // Sanity check, should match what was set in mctp_register 356 netdev_warn(dev, "%s: mdev pointer %d but type (%d) match is %d", 357 __func__, (bool)mdev, mctp_known(dev), dev->type); 358 return; 359 } 360 if (!mdev) 361 return; 362 363 RCU_INIT_POINTER(mdev->dev->mctp_ptr, NULL); 364 365 mctp_route_remove_dev(mdev); 366 mctp_neigh_remove_dev(mdev); 367 kfree(mdev->addrs); 368 369 mctp_dev_put(mdev); 370 } 371 372 static int mctp_register(struct net_device *dev) 373 { 374 struct mctp_dev *mdev; 375 376 /* Already registered? */ 377 mdev = rtnl_dereference(dev->mctp_ptr); 378 379 if (mdev) { 380 if (!mctp_known(dev)) 381 netdev_warn(dev, "%s: mctp_dev set for unknown type %d", 382 __func__, dev->type); 383 return 0; 384 } 385 386 /* only register specific types */ 387 if (!mctp_known(dev)) 388 return 0; 389 390 mdev = mctp_add_dev(dev); 391 if (IS_ERR(mdev)) 392 return PTR_ERR(mdev); 393 394 return 0; 395 } 396 397 static int mctp_dev_notify(struct notifier_block *this, unsigned long event, 398 void *ptr) 399 { 400 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 401 int rc; 402 403 switch (event) { 404 case NETDEV_REGISTER: 405 rc = mctp_register(dev); 406 if (rc) 407 return notifier_from_errno(rc); 408 break; 409 case NETDEV_UNREGISTER: 410 mctp_unregister(dev); 411 break; 412 } 413 414 return NOTIFY_OK; 415 } 416 417 static struct rtnl_af_ops mctp_af_ops = { 418 .family = AF_MCTP, 419 .fill_link_af = mctp_fill_link_af, 420 .get_link_af_size = mctp_get_link_af_size, 421 .set_link_af = mctp_set_link_af, 422 }; 423 424 static struct notifier_block mctp_dev_nb = { 425 .notifier_call = mctp_dev_notify, 426 .priority = ADDRCONF_NOTIFY_PRIORITY, 427 }; 428 429 void __init mctp_device_init(void) 430 { 431 register_netdevice_notifier(&mctp_dev_nb); 432 433 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETADDR, 434 NULL, mctp_dump_addrinfo, 0); 435 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWADDR, 436 mctp_rtm_newaddr, NULL, 0); 437 rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELADDR, 438 mctp_rtm_deladdr, NULL, 0); 439 rtnl_af_register(&mctp_af_ops); 440 } 441 442 void __exit mctp_device_exit(void) 443 { 444 rtnl_af_unregister(&mctp_af_ops); 445 rtnl_unregister(PF_MCTP, RTM_DELADDR); 446 rtnl_unregister(PF_MCTP, RTM_NEWADDR); 447 rtnl_unregister(PF_MCTP, RTM_GETADDR); 448 449 unregister_netdevice_notifier(&mctp_dev_nb); 450 } 451