1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/ethtool.h> 4 #include <linux/net_namespace.h> 5 #include <linux/skbuff.h> 6 #include <linux/xarray.h> 7 #include <net/genetlink.h> 8 #include <net/psp.h> 9 #include <net/sock.h> 10 11 #include "psp-nl-gen.h" 12 #include "psp.h" 13 14 /* Netlink helpers */ 15 16 static struct sk_buff *psp_nl_reply_new(struct genl_info *info) 17 { 18 struct sk_buff *rsp; 19 void *hdr; 20 21 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 22 if (!rsp) 23 return NULL; 24 25 hdr = genlmsg_iput(rsp, info); 26 if (!hdr) { 27 nlmsg_free(rsp); 28 return NULL; 29 } 30 31 return rsp; 32 } 33 34 static int psp_nl_reply_send(struct sk_buff *rsp, struct genl_info *info) 35 { 36 /* Note that this *only* works with a single message per skb! */ 37 nlmsg_end(rsp, (struct nlmsghdr *)rsp->data); 38 39 return genlmsg_reply(rsp, info); 40 } 41 42 /** 43 * psp_nl_multicast_per_ns() - multicast a notification to each unique netns 44 * @psd: PSP device (must be locked) 45 * @group: multicast group 46 * @build_ntf: callback to build an skb for a given netns, or NULL on failure 47 * @ctx: opaque context passed to @build_ntf 48 * 49 * Iterates all unique network namespaces from the associated device list 50 * plus the main device's netns. For each unique netns, calls @build_ntf 51 * to construct a notification skb and multicasts it. 52 */ 53 static void 54 psp_nl_multicast_per_ns(struct psp_dev *psd, unsigned int group, 55 struct sk_buff *(*build_ntf)(struct psp_dev *, 56 struct net *, 57 void *), 58 void *ctx) 59 { 60 struct psp_assoc_dev *entry; 61 struct xarray sent_nets; 62 struct net *main_net; 63 struct sk_buff *ntf; 64 65 /* device may be changing netns in parallel */ 66 rcu_read_lock(); 67 main_net = maybe_get_net(dev_net_rcu(psd->main_netdev)); 68 rcu_read_unlock(); 69 70 if (!main_net) 71 return; 72 73 xa_init(&sent_nets); 74 75 list_for_each_entry(entry, &psd->assoc_dev_list, dev_list) { 76 struct net *assoc_net = dev_net(entry->assoc_dev); 77 int ret; 78 79 if (net_eq(assoc_net, main_net)) 80 continue; 81 82 ret = xa_insert(&sent_nets, (unsigned long)assoc_net, assoc_net, 83 GFP_KERNEL); 84 if (ret == -EBUSY) 85 continue; 86 87 ntf = build_ntf(psd, assoc_net, ctx); 88 if (!ntf) 89 continue; 90 91 genlmsg_multicast_netns(&psp_nl_family, assoc_net, ntf, 0, 92 group, GFP_KERNEL); 93 } 94 xa_destroy(&sent_nets); 95 96 /* Send to main device netns */ 97 ntf = build_ntf(psd, main_net, ctx); 98 if (ntf) 99 genlmsg_multicast_netns(&psp_nl_family, main_net, ntf, 0, group, 100 GFP_KERNEL); 101 put_net(main_net); 102 } 103 104 static struct sk_buff *psp_nl_clone_ntf(struct psp_dev *psd, struct net *net, 105 void *ctx) 106 { 107 return skb_clone(ctx, GFP_KERNEL); 108 } 109 110 static void psp_nl_multicast_all_ns(struct psp_dev *psd, struct sk_buff *ntf, 111 unsigned int group) 112 { 113 psp_nl_multicast_per_ns(psd, group, psp_nl_clone_ntf, ntf); 114 nlmsg_consume(ntf); 115 } 116 117 /* Device stuff */ 118 119 static struct psp_dev * 120 psp_device_get_and_lock(struct net *net, struct nlattr *dev_id, 121 bool admin) 122 { 123 struct psp_dev *psd; 124 int err; 125 126 mutex_lock(&psp_devs_lock); 127 psd = xa_load(&psp_devs, nla_get_u32(dev_id)); 128 if (!psd) { 129 mutex_unlock(&psp_devs_lock); 130 return ERR_PTR(-ENODEV); 131 } 132 133 mutex_lock(&psd->lock); 134 mutex_unlock(&psp_devs_lock); 135 136 err = psp_dev_check_access(psd, net, admin); 137 if (err) { 138 mutex_unlock(&psd->lock); 139 return ERR_PTR(err); 140 } 141 142 return psd; 143 } 144 145 static int __psp_device_get_locked(const struct genl_split_ops *ops, 146 struct sk_buff *skb, struct genl_info *info, 147 bool admin) 148 { 149 if (GENL_REQ_ATTR_CHECK(info, PSP_A_DEV_ID)) 150 return -EINVAL; 151 152 info->user_ptr[0] = psp_device_get_and_lock(genl_info_net(info), 153 info->attrs[PSP_A_DEV_ID], 154 admin); 155 return PTR_ERR_OR_ZERO(info->user_ptr[0]); 156 } 157 158 int psp_device_get_locked_admin(const struct genl_split_ops *ops, 159 struct sk_buff *skb, struct genl_info *info) 160 { 161 return __psp_device_get_locked(ops, skb, info, true); 162 } 163 164 int psp_device_get_locked(const struct genl_split_ops *ops, 165 struct sk_buff *skb, struct genl_info *info) 166 { 167 return __psp_device_get_locked(ops, skb, info, false); 168 } 169 170 /* 171 * Non-admin version of psp_device_get_locked() + psp_attach_netdev_notifier() 172 * only used for dev-assoc. 173 */ 174 int psp_device_get_locked_dev_assoc(const struct genl_split_ops *ops, 175 struct sk_buff *skb, struct genl_info *info) 176 { 177 int err; 178 179 err = psp_attach_netdev_notifier(); 180 if (err) 181 return err; 182 183 return __psp_device_get_locked(ops, skb, info, false); 184 } 185 186 static struct net *psp_nl_resolve_assoc_dev_ns(struct psp_dev *psd, 187 struct genl_info *info) 188 { 189 struct net *net; 190 int nsid; 191 192 if (GENL_REQ_ATTR_CHECK(info, PSP_A_DEV_IFINDEX)) 193 return ERR_PTR(-EINVAL); 194 195 if (info->attrs[PSP_A_DEV_NSID]) { 196 /* Only callers in the main netns may specify nsid */ 197 if (dev_net(psd->main_netdev) != genl_info_net(info)) { 198 NL_SET_BAD_ATTR(info->extack, 199 info->attrs[PSP_A_DEV_NSID]); 200 return ERR_PTR(-EPERM); 201 } 202 203 nsid = nla_get_s32(info->attrs[PSP_A_DEV_NSID]); 204 205 net = get_net_ns_by_id(genl_info_net(info), nsid); 206 if (!net) { 207 NL_SET_BAD_ATTR(info->extack, 208 info->attrs[PSP_A_DEV_NSID]); 209 return ERR_PTR(-EINVAL); 210 } 211 } else { 212 net = get_net(genl_info_net(info)); 213 } 214 215 return net; 216 } 217 218 void 219 psp_device_unlock(const struct genl_split_ops *ops, struct sk_buff *skb, 220 struct genl_info *info) 221 { 222 struct socket *socket = info->user_ptr[1]; 223 struct psp_dev *psd = info->user_ptr[0]; 224 225 mutex_unlock(&psd->lock); 226 if (socket) 227 sockfd_put(socket); 228 } 229 230 bool psp_has_assoc_dev_in_ns(struct psp_dev *psd, struct net *net) 231 { 232 struct psp_assoc_dev *entry; 233 234 list_for_each_entry(entry, &psd->assoc_dev_list, dev_list) { 235 if (dev_net(entry->assoc_dev) == net) 236 return true; 237 } 238 239 return false; 240 } 241 242 static int psp_nl_fill_assoc_dev_list(struct psp_dev *psd, struct sk_buff *rsp, 243 struct net *cur_net, 244 struct net *filter_net) 245 { 246 struct psp_assoc_dev *entry; 247 struct net *dev_net_ns; 248 struct nlattr *nest; 249 int nsid; 250 251 list_for_each_entry(entry, &psd->assoc_dev_list, dev_list) { 252 dev_net_ns = dev_net(entry->assoc_dev); 253 254 if (filter_net && dev_net_ns != filter_net) 255 continue; 256 257 /* When filtering by namespace, all devices are in the caller's 258 * namespace so nsid is always NETNSA_NSID_NOT_ASSIGNED (-1). 259 * Otherwise, calculate the nsid relative to cur_net. 260 */ 261 nsid = filter_net ? NETNSA_NSID_NOT_ASSIGNED : 262 peernet2id_alloc(cur_net, dev_net_ns, 263 GFP_KERNEL); 264 265 nest = nla_nest_start(rsp, PSP_A_DEV_ASSOC_LIST); 266 if (!nest) 267 return -EMSGSIZE; 268 269 if (nla_put_u32(rsp, PSP_A_ASSOC_DEV_INFO_IFINDEX, 270 entry->assoc_dev->ifindex) || 271 nla_put_s32(rsp, PSP_A_ASSOC_DEV_INFO_NSID, nsid)) { 272 nla_nest_cancel(rsp, nest); 273 return -EMSGSIZE; 274 } 275 276 nla_nest_end(rsp, nest); 277 } 278 279 return 0; 280 } 281 282 static int 283 psp_nl_dev_fill(struct psp_dev *psd, struct sk_buff *rsp, 284 const struct genl_info *info) 285 { 286 struct net *cur_net; 287 void *hdr; 288 int err; 289 290 cur_net = genl_info_net(info); 291 292 hdr = genlmsg_iput(rsp, info); 293 if (!hdr) 294 return -EMSGSIZE; 295 296 if (nla_put_u32(rsp, PSP_A_DEV_ID, psd->id) || 297 nla_put_u32(rsp, PSP_A_DEV_PSP_VERSIONS_CAP, psd->caps->versions) || 298 nla_put_u32(rsp, PSP_A_DEV_PSP_VERSIONS_ENA, psd->config.versions)) 299 goto err_cancel_msg; 300 301 if (cur_net == dev_net(psd->main_netdev)) { 302 /* Primary device - report the netdev, dump assoc list. */ 303 if (nla_put_u32(rsp, PSP_A_DEV_IFINDEX, 304 psd->main_netdev->ifindex)) 305 goto err_cancel_msg; 306 307 err = psp_nl_fill_assoc_dev_list(psd, rsp, cur_net, NULL); 308 if (err) 309 goto err_cancel_msg; 310 } else { 311 /* In netns: set by-association flag and dump filtered 312 * assoc list containing only devices in cur_net 313 */ 314 if (nla_put_flag(rsp, PSP_A_DEV_BY_ASSOCIATION)) 315 goto err_cancel_msg; 316 err = psp_nl_fill_assoc_dev_list(psd, rsp, cur_net, cur_net); 317 if (err) 318 goto err_cancel_msg; 319 } 320 321 genlmsg_end(rsp, hdr); 322 return 0; 323 324 err_cancel_msg: 325 genlmsg_cancel(rsp, hdr); 326 return -EMSGSIZE; 327 } 328 329 static struct sk_buff *psp_nl_build_dev_ntf(struct psp_dev *psd, 330 struct net *net, void *ctx) 331 { 332 u32 cmd = *(u32 *)ctx; 333 struct genl_info info; 334 struct sk_buff *ntf; 335 336 if (!genl_has_listeners(&psp_nl_family, net, PSP_NLGRP_MGMT)) 337 return NULL; 338 339 ntf = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 340 if (!ntf) 341 return NULL; 342 343 genl_info_init_ntf(&info, &psp_nl_family, cmd); 344 genl_info_net_set(&info, net); 345 if (psp_nl_dev_fill(psd, ntf, &info)) { 346 nlmsg_free(ntf); 347 return NULL; 348 } 349 350 return ntf; 351 } 352 353 void psp_nl_notify_dev(struct psp_dev *psd, u32 cmd) 354 { 355 psp_nl_multicast_per_ns(psd, PSP_NLGRP_MGMT, 356 psp_nl_build_dev_ntf, &cmd); 357 } 358 359 /** 360 * psp_nl_notify_disassoc() - notify about a device losing an association 361 * @psd: PSP device (must be locked) 362 * @net: netns of the netdevice which got disassociated 363 * 364 * Must be called once @psd no longer has the association, so that the 365 * notifications carry the state after the change. 366 */ 367 void psp_nl_notify_disassoc(struct psp_dev *psd, struct net *net) 368 { 369 struct sk_buff *ntf; 370 bool still_visible; 371 u32 cmd; 372 373 lockdep_assert_held(&psd->lock); 374 375 psp_nl_notify_dev(psd, PSP_CMD_DEV_CHANGE_NTF); 376 377 /* psp_nl_notify_dev() reaches the main netdevice's netns and every 378 * netns which still has an associated device. If @net is neither, 379 * the device is gone from @net and we should send a delete ntf. 380 */ 381 still_visible = !psp_dev_check_access(psd, net, false); 382 if (still_visible || !maybe_get_net(net)) 383 return; 384 385 cmd = PSP_CMD_DEV_DEL_NTF; 386 ntf = psp_nl_build_dev_ntf(psd, net, &cmd); 387 if (ntf) 388 genlmsg_multicast_netns(&psp_nl_family, net, ntf, 0, 389 PSP_NLGRP_MGMT, GFP_KERNEL); 390 put_net(net); 391 } 392 393 int psp_nl_dev_get_doit(struct sk_buff *req, struct genl_info *info) 394 { 395 struct psp_dev *psd = info->user_ptr[0]; 396 struct sk_buff *rsp; 397 int err; 398 399 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 400 if (!rsp) 401 return -ENOMEM; 402 403 err = psp_nl_dev_fill(psd, rsp, info); 404 if (err) 405 goto err_free_msg; 406 407 return genlmsg_reply(rsp, info); 408 409 err_free_msg: 410 nlmsg_free(rsp); 411 return err; 412 } 413 414 static int 415 psp_nl_dev_get_dumpit_one(struct sk_buff *rsp, struct netlink_callback *cb, 416 struct psp_dev *psd) 417 { 418 if (psp_dev_check_access(psd, sock_net(rsp->sk), false)) 419 return 0; 420 421 return psp_nl_dev_fill(psd, rsp, genl_info_dump(cb)); 422 } 423 424 int psp_nl_dev_get_dumpit(struct sk_buff *rsp, struct netlink_callback *cb) 425 { 426 struct psp_dev *psd; 427 int err = 0; 428 429 mutex_lock(&psp_devs_lock); 430 xa_for_each_start(&psp_devs, cb->args[0], psd, cb->args[0]) { 431 mutex_lock(&psd->lock); 432 err = psp_nl_dev_get_dumpit_one(rsp, cb, psd); 433 mutex_unlock(&psd->lock); 434 if (err) 435 break; 436 } 437 mutex_unlock(&psp_devs_lock); 438 439 return err; 440 } 441 442 int psp_nl_dev_set_doit(struct sk_buff *skb, struct genl_info *info) 443 { 444 struct psp_dev *psd = info->user_ptr[0]; 445 struct psp_dev_config new_config; 446 struct sk_buff *rsp; 447 int err; 448 449 memcpy(&new_config, &psd->config, sizeof(new_config)); 450 451 if (info->attrs[PSP_A_DEV_PSP_VERSIONS_ENA]) { 452 new_config.versions = 453 nla_get_u32(info->attrs[PSP_A_DEV_PSP_VERSIONS_ENA]); 454 if (new_config.versions & ~psd->caps->versions) { 455 NL_SET_ERR_MSG(info->extack, "Requested PSP versions not supported by the device"); 456 return -EINVAL; 457 } 458 } else { 459 NL_SET_ERR_MSG(info->extack, "No settings present"); 460 return -EINVAL; 461 } 462 463 rsp = psp_nl_reply_new(info); 464 if (!rsp) 465 return -ENOMEM; 466 467 if (memcmp(&new_config, &psd->config, sizeof(new_config))) { 468 err = psd->ops->set_config(psd, &new_config, info->extack); 469 if (err) 470 goto err_free_rsp; 471 472 memcpy(&psd->config, &new_config, sizeof(new_config)); 473 } 474 475 psp_nl_notify_dev(psd, PSP_CMD_DEV_CHANGE_NTF); 476 477 return psp_nl_reply_send(rsp, info); 478 479 err_free_rsp: 480 nlmsg_free(rsp); 481 return err; 482 } 483 484 int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info) 485 { 486 struct psp_dev *psd = info->user_ptr[0]; 487 struct genl_info ntf_info; 488 struct sk_buff *ntf, *rsp; 489 u8 prev_gen; 490 int err; 491 492 rsp = psp_nl_reply_new(info); 493 if (!rsp) 494 return -ENOMEM; 495 496 genl_info_init_ntf(&ntf_info, &psp_nl_family, PSP_CMD_KEY_ROTATE_NTF); 497 ntf = psp_nl_reply_new(&ntf_info); 498 if (!ntf) { 499 err = -ENOMEM; 500 goto err_free_rsp; 501 } 502 503 if (nla_put_u32(rsp, PSP_A_DEV_ID, psd->id) || 504 nla_put_u32(ntf, PSP_A_DEV_ID, psd->id)) { 505 err = -EMSGSIZE; 506 goto err_free_ntf; 507 } 508 509 /* suggest the next gen number, driver can override */ 510 prev_gen = psd->generation; 511 psd->generation = (prev_gen + 1) & PSP_GEN_VALID_MASK; 512 513 err = psd->ops->key_rotate(psd, info->extack); 514 if (err) 515 goto err_free_ntf; 516 517 WARN_ON_ONCE((psd->generation && psd->generation == prev_gen) || 518 psd->generation & ~PSP_GEN_VALID_MASK); 519 520 psp_assocs_key_rotated(psd); 521 psd->stats.rotations++; 522 523 nlmsg_end(ntf, (struct nlmsghdr *)ntf->data); 524 525 psp_nl_multicast_all_ns(psd, ntf, PSP_NLGRP_USE); 526 527 return psp_nl_reply_send(rsp, info); 528 529 err_free_ntf: 530 nlmsg_free(ntf); 531 err_free_rsp: 532 nlmsg_free(rsp); 533 return err; 534 } 535 536 int psp_nl_dev_assoc_doit(struct sk_buff *skb, struct genl_info *info) 537 { 538 struct psp_dev *psd = info->user_ptr[0]; 539 struct psp_assoc_dev *psp_assoc_dev; 540 struct net_device *assoc_dev; 541 struct sk_buff *rsp; 542 u32 assoc_ifindex; 543 struct net *net; 544 int err; 545 546 if (psd->assoc_dev_cnt >= PSP_ASSOC_DEV_MAX) { 547 NL_SET_ERR_MSG(info->extack, 548 "Maximum number of associated devices reached"); 549 return -ENOSPC; 550 } 551 552 net = psp_nl_resolve_assoc_dev_ns(psd, info); 553 if (IS_ERR(net)) 554 return PTR_ERR(net); 555 556 psp_assoc_dev = kzalloc_obj(*psp_assoc_dev); 557 if (!psp_assoc_dev) { 558 err = -ENOMEM; 559 goto err_put_net; 560 } 561 562 assoc_ifindex = nla_get_u32(info->attrs[PSP_A_DEV_IFINDEX]); 563 assoc_dev = netdev_get_by_index(net, assoc_ifindex, 564 &psp_assoc_dev->dev_tracker, 565 GFP_KERNEL); 566 if (!assoc_dev) { 567 NL_SET_BAD_ATTR(info->extack, info->attrs[PSP_A_DEV_IFINDEX]); 568 err = -ENODEV; 569 goto err_free_assoc; 570 } 571 572 /* Check if device is already associated with a PSP device */ 573 if (unrcu_pointer(cmpxchg(&assoc_dev->psp_dev, NULL, 574 RCU_INITIALIZER(psd)))) { 575 NL_SET_ERR_MSG(info->extack, 576 "Device already associated with a PSP device"); 577 err = -EBUSY; 578 goto err_put_dev; 579 } 580 581 psp_assoc_dev->assoc_dev = assoc_dev; 582 583 /* Check for race with NETDEV_UNREGISTER. The cmpxchg above is a 584 * full barrier, and the unregister path has synchronize_net() 585 * between setting NETREG_UNREGISTERING and reading psp_dev in the 586 * notifier. So at least one side would do the clean-up if we are in 587 * the middle of unregitering assoc_dev. 588 * And the clean-up is serialized by psd->lock. 589 */ 590 if (READ_ONCE(assoc_dev->reg_state) != NETREG_REGISTERED) { 591 err = -ENODEV; 592 goto err_clean_ptr; 593 } 594 595 rsp = psp_nl_reply_new(info); 596 if (!rsp) { 597 err = -ENOMEM; 598 goto err_clean_ptr; 599 } 600 601 list_add_tail(&psp_assoc_dev->dev_list, &psd->assoc_dev_list); 602 psd->assoc_dev_cnt++; 603 604 put_net(net); 605 606 psp_nl_notify_dev(psd, PSP_CMD_DEV_CHANGE_NTF); 607 608 return psp_nl_reply_send(rsp, info); 609 610 err_clean_ptr: 611 rcu_assign_pointer(assoc_dev->psp_dev, NULL); 612 err_put_dev: 613 netdev_put(assoc_dev, &psp_assoc_dev->dev_tracker); 614 err_free_assoc: 615 kfree(psp_assoc_dev); 616 err_put_net: 617 put_net(net); 618 619 return err; 620 } 621 622 int psp_nl_dev_disassoc_doit(struct sk_buff *skb, struct genl_info *info) 623 { 624 struct psp_assoc_dev *entry, *found = NULL; 625 struct psp_dev *psd = info->user_ptr[0]; 626 struct sk_buff *rsp; 627 u32 assoc_ifindex; 628 struct net *net; 629 630 net = psp_nl_resolve_assoc_dev_ns(psd, info); 631 if (IS_ERR(net)) 632 return PTR_ERR(net); 633 634 assoc_ifindex = nla_get_u32(info->attrs[PSP_A_DEV_IFINDEX]); 635 636 /* Search the association list by ifindex and netns */ 637 list_for_each_entry(entry, &psd->assoc_dev_list, dev_list) { 638 if (entry->assoc_dev->ifindex == assoc_ifindex && 639 dev_net(entry->assoc_dev) == net) { 640 found = entry; 641 break; 642 } 643 } 644 645 if (!found) { 646 put_net(net); 647 NL_SET_BAD_ATTR(info->extack, info->attrs[PSP_A_DEV_IFINDEX]); 648 return -ENODEV; 649 } 650 651 rsp = psp_nl_reply_new(info); 652 if (!rsp) { 653 put_net(net); 654 return -ENOMEM; 655 } 656 657 /* Remove from the association list */ 658 list_del(&found->dev_list); 659 psd->assoc_dev_cnt--; 660 rcu_assign_pointer(found->assoc_dev->psp_dev, NULL); 661 netdev_put(found->assoc_dev, &found->dev_tracker); 662 kfree(found); 663 664 psp_nl_notify_disassoc(psd, net); 665 put_net(net); 666 667 return psp_nl_reply_send(rsp, info); 668 } 669 670 /* Key etc. */ 671 672 int psp_assoc_device_get_locked(const struct genl_split_ops *ops, 673 struct sk_buff *skb, struct genl_info *info) 674 { 675 struct socket *socket; 676 struct psp_dev *psd; 677 struct nlattr *id; 678 int fd, err; 679 680 if (GENL_REQ_ATTR_CHECK(info, PSP_A_ASSOC_SOCK_FD)) 681 return -EINVAL; 682 683 fd = nla_get_u32(info->attrs[PSP_A_ASSOC_SOCK_FD]); 684 socket = sockfd_lookup(fd, &err); 685 if (!socket) 686 return err; 687 688 if (!sk_is_tcp(socket->sk)) { 689 NL_SET_ERR_MSG_ATTR(info->extack, 690 info->attrs[PSP_A_ASSOC_SOCK_FD], 691 "Unsupported socket family and type"); 692 err = -EOPNOTSUPP; 693 goto err_sock_put; 694 } 695 696 psd = psp_dev_get_for_sock(socket->sk); 697 if (psd) { 698 /* Extra care needed here, psp_dev_get_for_sock() only gives 699 * us access to struct psp_dev's memory, which is quite weak. 700 */ 701 mutex_lock(&psd->lock); 702 if (!psp_dev_is_registered(psd) || 703 psp_dev_check_access(psd, genl_info_net(info), false)) { 704 mutex_unlock(&psd->lock); 705 psp_dev_put(psd); 706 psd = NULL; 707 } 708 } 709 710 if (!psd && GENL_REQ_ATTR_CHECK(info, PSP_A_ASSOC_DEV_ID)) { 711 err = -EINVAL; 712 goto err_sock_put; 713 } 714 715 id = info->attrs[PSP_A_ASSOC_DEV_ID]; 716 if (psd) { 717 if (id && psd->id != nla_get_u32(id)) { 718 mutex_unlock(&psd->lock); 719 NL_SET_ERR_MSG_ATTR(info->extack, id, 720 "Device id vs socket mismatch"); 721 err = -EINVAL; 722 goto err_psd_put; 723 } 724 725 psp_dev_put(psd); 726 } else { 727 psd = psp_device_get_and_lock(genl_info_net(info), id, false); 728 if (IS_ERR(psd)) { 729 err = PTR_ERR(psd); 730 goto err_sock_put; 731 } 732 } 733 734 info->user_ptr[0] = psd; 735 info->user_ptr[1] = socket; 736 737 return 0; 738 739 err_psd_put: 740 psp_dev_put(psd); 741 err_sock_put: 742 sockfd_put(socket); 743 return err; 744 } 745 746 static int 747 psp_nl_parse_key(struct genl_info *info, u32 attr, struct psp_key_parsed *key, 748 unsigned int key_sz) 749 { 750 struct nlattr *nest = info->attrs[attr]; 751 struct nlattr *tb[PSP_A_KEYS_SPI + 1]; 752 u32 spi; 753 int err; 754 755 err = nla_parse_nested(tb, ARRAY_SIZE(tb) - 1, nest, 756 psp_keys_nl_policy, info->extack); 757 if (err) 758 return err; 759 760 if (NL_REQ_ATTR_CHECK(info->extack, nest, tb, PSP_A_KEYS_KEY) || 761 NL_REQ_ATTR_CHECK(info->extack, nest, tb, PSP_A_KEYS_SPI)) 762 return -EINVAL; 763 764 if (nla_len(tb[PSP_A_KEYS_KEY]) != key_sz) { 765 NL_SET_ERR_MSG_ATTR(info->extack, tb[PSP_A_KEYS_KEY], 766 "incorrect key length"); 767 return -EINVAL; 768 } 769 770 spi = nla_get_u32(tb[PSP_A_KEYS_SPI]); 771 if (!(spi & PSP_SPI_KEY_ID)) { 772 NL_SET_ERR_MSG_ATTR(info->extack, tb[PSP_A_KEYS_KEY], 773 "invalid SPI: lower 31b must be non-zero"); 774 return -EINVAL; 775 } 776 777 key->spi = cpu_to_be32(spi); 778 memcpy(key->key, nla_data(tb[PSP_A_KEYS_KEY]), key_sz); 779 780 return 0; 781 } 782 783 static int 784 psp_nl_put_key(struct sk_buff *skb, u32 attr, u32 version, 785 struct psp_key_parsed *key) 786 { 787 int key_sz = psp_key_size(version); 788 void *nest; 789 790 nest = nla_nest_start(skb, attr); 791 792 if (nla_put_u32(skb, PSP_A_KEYS_SPI, be32_to_cpu(key->spi)) || 793 nla_put(skb, PSP_A_KEYS_KEY, key_sz, key->key)) { 794 nla_nest_cancel(skb, nest); 795 return -EMSGSIZE; 796 } 797 798 nla_nest_end(skb, nest); 799 800 return 0; 801 } 802 803 int psp_nl_rx_assoc_doit(struct sk_buff *skb, struct genl_info *info) 804 { 805 struct socket *socket = info->user_ptr[1]; 806 struct psp_dev *psd = info->user_ptr[0]; 807 struct psp_key_parsed key; 808 struct psp_assoc *pas; 809 struct sk_buff *rsp; 810 u32 version; 811 int err; 812 813 if (GENL_REQ_ATTR_CHECK(info, PSP_A_ASSOC_VERSION)) 814 return -EINVAL; 815 816 version = nla_get_u32(info->attrs[PSP_A_ASSOC_VERSION]); 817 if (!(psd->caps->versions & (1 << version))) { 818 NL_SET_BAD_ATTR(info->extack, info->attrs[PSP_A_ASSOC_VERSION]); 819 return -EOPNOTSUPP; 820 } 821 822 rsp = psp_nl_reply_new(info); 823 if (!rsp) 824 return -ENOMEM; 825 826 pas = psp_assoc_create(psd); 827 if (!pas) { 828 err = -ENOMEM; 829 goto err_free_rsp; 830 } 831 pas->version = version; 832 833 err = psd->ops->rx_spi_alloc(psd, version, &key, info->extack); 834 if (err) 835 goto err_free_pas; 836 837 if (nla_put_u32(rsp, PSP_A_ASSOC_DEV_ID, psd->id) || 838 psp_nl_put_key(rsp, PSP_A_ASSOC_RX_KEY, version, &key)) { 839 err = -EMSGSIZE; 840 goto err_free_pas; 841 } 842 843 err = psp_sock_assoc_set_rx(socket->sk, pas, &key, info->extack); 844 if (err) { 845 NL_SET_BAD_ATTR(info->extack, info->attrs[PSP_A_ASSOC_SOCK_FD]); 846 goto err_free_pas; 847 } 848 psp_assoc_put(pas); 849 850 return psp_nl_reply_send(rsp, info); 851 852 err_free_pas: 853 psp_assoc_put(pas); 854 err_free_rsp: 855 nlmsg_free(rsp); 856 return err; 857 } 858 859 int psp_nl_tx_assoc_doit(struct sk_buff *skb, struct genl_info *info) 860 { 861 struct socket *socket = info->user_ptr[1]; 862 struct psp_dev *psd = info->user_ptr[0]; 863 struct psp_key_parsed key; 864 struct sk_buff *rsp; 865 unsigned int key_sz; 866 u32 version; 867 int err; 868 869 if (GENL_REQ_ATTR_CHECK(info, PSP_A_ASSOC_VERSION) || 870 GENL_REQ_ATTR_CHECK(info, PSP_A_ASSOC_TX_KEY)) 871 return -EINVAL; 872 873 version = nla_get_u32(info->attrs[PSP_A_ASSOC_VERSION]); 874 if (!(psd->caps->versions & (1 << version))) { 875 NL_SET_BAD_ATTR(info->extack, info->attrs[PSP_A_ASSOC_VERSION]); 876 return -EOPNOTSUPP; 877 } 878 879 key_sz = psp_key_size(version); 880 if (!key_sz) 881 return -EINVAL; 882 883 err = psp_nl_parse_key(info, PSP_A_ASSOC_TX_KEY, &key, key_sz); 884 if (err < 0) 885 return err; 886 887 rsp = psp_nl_reply_new(info); 888 if (!rsp) 889 return -ENOMEM; 890 891 err = psp_sock_assoc_set_tx(socket->sk, psd, version, &key, 892 info->extack); 893 if (err) 894 goto err_free_msg; 895 896 return psp_nl_reply_send(rsp, info); 897 898 err_free_msg: 899 nlmsg_free(rsp); 900 return err; 901 } 902 903 static int 904 psp_nl_stats_fill(struct psp_dev *psd, struct sk_buff *rsp, 905 const struct genl_info *info) 906 { 907 unsigned int required_cnt = sizeof(struct psp_dev_stats) / sizeof(u64); 908 struct psp_dev_stats stats; 909 void *hdr; 910 int i; 911 912 memset(&stats, 0xff, sizeof(stats)); 913 psd->ops->get_stats(psd, &stats); 914 915 for (i = 0; i < required_cnt; i++) 916 if (WARN_ON_ONCE(stats.required[i] == ETHTOOL_STAT_NOT_SET)) 917 return -EOPNOTSUPP; 918 919 hdr = genlmsg_iput(rsp, info); 920 if (!hdr) 921 return -EMSGSIZE; 922 923 if (nla_put_u32(rsp, PSP_A_STATS_DEV_ID, psd->id) || 924 nla_put_uint(rsp, PSP_A_STATS_KEY_ROTATIONS, 925 psd->stats.rotations) || 926 nla_put_uint(rsp, PSP_A_STATS_STALE_EVENTS, psd->stats.stales) || 927 nla_put_uint(rsp, PSP_A_STATS_RX_PACKETS, stats.rx_packets) || 928 nla_put_uint(rsp, PSP_A_STATS_RX_BYTES, stats.rx_bytes) || 929 nla_put_uint(rsp, PSP_A_STATS_RX_AUTH_FAIL, stats.rx_auth_fail) || 930 nla_put_uint(rsp, PSP_A_STATS_RX_ERROR, stats.rx_error) || 931 nla_put_uint(rsp, PSP_A_STATS_RX_BAD, stats.rx_bad) || 932 nla_put_uint(rsp, PSP_A_STATS_TX_PACKETS, stats.tx_packets) || 933 nla_put_uint(rsp, PSP_A_STATS_TX_BYTES, stats.tx_bytes) || 934 nla_put_uint(rsp, PSP_A_STATS_TX_ERROR, stats.tx_error)) 935 goto err_cancel_msg; 936 937 genlmsg_end(rsp, hdr); 938 return 0; 939 940 err_cancel_msg: 941 genlmsg_cancel(rsp, hdr); 942 return -EMSGSIZE; 943 } 944 945 int psp_nl_get_stats_doit(struct sk_buff *skb, struct genl_info *info) 946 { 947 struct psp_dev *psd = info->user_ptr[0]; 948 struct sk_buff *rsp; 949 int err; 950 951 rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 952 if (!rsp) 953 return -ENOMEM; 954 955 err = psp_nl_stats_fill(psd, rsp, info); 956 if (err) 957 goto err_free_msg; 958 959 return genlmsg_reply(rsp, info); 960 961 err_free_msg: 962 nlmsg_free(rsp); 963 return err; 964 } 965 966 static int 967 psp_nl_stats_get_dumpit_one(struct sk_buff *rsp, struct netlink_callback *cb, 968 struct psp_dev *psd) 969 { 970 if (psp_dev_check_access(psd, sock_net(rsp->sk), false)) 971 return 0; 972 973 return psp_nl_stats_fill(psd, rsp, genl_info_dump(cb)); 974 } 975 976 int psp_nl_get_stats_dumpit(struct sk_buff *rsp, struct netlink_callback *cb) 977 { 978 struct psp_dev *psd; 979 int err = 0; 980 981 mutex_lock(&psp_devs_lock); 982 xa_for_each_start(&psp_devs, cb->args[0], psd, cb->args[0]) { 983 mutex_lock(&psd->lock); 984 err = psp_nl_stats_get_dumpit_one(rsp, cb, psd); 985 mutex_unlock(&psd->lock); 986 if (err) 987 break; 988 } 989 mutex_unlock(&psp_devs_lock); 990 991 return err; 992 } 993