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