1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * INET An implementation of the TCP/IP protocol suite for the LINUX 4 * operating system. INET is implemented using the BSD Socket 5 * interface as the means of communication with the user level. 6 * 7 * Routing netlink socket interface: protocol independent part. 8 * 9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 10 * 11 * Fixes: 12 * Vitaly E. Lavrov RTA_OK arithmetic was wrong. 13 */ 14 15 #include <linux/bitops.h> 16 #include <linux/errno.h> 17 #include <linux/module.h> 18 #include <linux/types.h> 19 #include <linux/socket.h> 20 #include <linux/kernel.h> 21 #include <linux/timer.h> 22 #include <linux/string.h> 23 #include <linux/sockios.h> 24 #include <linux/net.h> 25 #include <linux/fcntl.h> 26 #include <linux/mm.h> 27 #include <linux/slab.h> 28 #include <linux/interrupt.h> 29 #include <linux/capability.h> 30 #include <linux/skbuff.h> 31 #include <linux/init.h> 32 #include <linux/security.h> 33 #include <linux/mutex.h> 34 #include <linux/if_addr.h> 35 #include <linux/if_bridge.h> 36 #include <linux/if_vlan.h> 37 #include <linux/pci.h> 38 #include <linux/etherdevice.h> 39 #include <linux/bpf.h> 40 41 #include <linux/uaccess.h> 42 43 #include <linux/inet.h> 44 #include <linux/netdevice.h> 45 #include <net/ip.h> 46 #include <net/protocol.h> 47 #include <net/arp.h> 48 #include <net/route.h> 49 #include <net/udp.h> 50 #include <net/tcp.h> 51 #include <net/sock.h> 52 #include <net/pkt_sched.h> 53 #include <net/fib_rules.h> 54 #include <net/rtnetlink.h> 55 #include <net/net_namespace.h> 56 #include <net/devlink.h> 57 #if IS_ENABLED(CONFIG_IPV6) 58 #include <net/addrconf.h> 59 #endif 60 #include <linux/dpll.h> 61 62 #include "dev.h" 63 64 #define RTNL_MAX_TYPE 50 65 #define RTNL_SLAVE_MAX_TYPE 44 66 67 struct rtnl_link { 68 rtnl_doit_func doit; 69 rtnl_dumpit_func dumpit; 70 struct module *owner; 71 unsigned int flags; 72 struct rcu_head rcu; 73 }; 74 75 static DEFINE_MUTEX(rtnl_mutex); 76 77 void rtnl_lock(void) 78 { 79 mutex_lock(&rtnl_mutex); 80 } 81 EXPORT_SYMBOL(rtnl_lock); 82 83 int rtnl_lock_killable(void) 84 { 85 return mutex_lock_killable(&rtnl_mutex); 86 } 87 EXPORT_SYMBOL(rtnl_lock_killable); 88 89 static struct sk_buff *defer_kfree_skb_list; 90 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 91 { 92 if (head && tail) { 93 tail->next = defer_kfree_skb_list; 94 defer_kfree_skb_list = head; 95 } 96 } 97 EXPORT_SYMBOL(rtnl_kfree_skbs); 98 99 void __rtnl_unlock(void) 100 { 101 struct sk_buff *head = defer_kfree_skb_list; 102 103 defer_kfree_skb_list = NULL; 104 105 /* Ensure that we didn't actually add any TODO item when __rtnl_unlock() 106 * is used. In some places, e.g. in cfg80211, we have code that will do 107 * something like 108 * rtnl_lock() 109 * wiphy_lock() 110 * ... 111 * rtnl_unlock() 112 * 113 * and because netdev_run_todo() acquires the RTNL for items on the list 114 * we could cause a situation such as this: 115 * Thread 1 Thread 2 116 * rtnl_lock() 117 * unregister_netdevice() 118 * __rtnl_unlock() 119 * rtnl_lock() 120 * wiphy_lock() 121 * rtnl_unlock() 122 * netdev_run_todo() 123 * __rtnl_unlock() 124 * 125 * // list not empty now 126 * // because of thread 2 127 * rtnl_lock() 128 * while (!list_empty(...)) 129 * rtnl_lock() 130 * wiphy_lock() 131 * **** DEADLOCK **** 132 * 133 * However, usage of __rtnl_unlock() is rare, and so we can ensure that 134 * it's not used in cases where something is added to do the list. 135 */ 136 WARN_ON(!list_empty(&net_todo_list)); 137 138 mutex_unlock(&rtnl_mutex); 139 140 while (head) { 141 struct sk_buff *next = head->next; 142 143 kfree_skb(head); 144 cond_resched(); 145 head = next; 146 } 147 } 148 149 void rtnl_unlock(void) 150 { 151 /* This fellow will unlock it for us. */ 152 netdev_run_todo(); 153 } 154 EXPORT_SYMBOL(rtnl_unlock); 155 156 int rtnl_trylock(void) 157 { 158 return mutex_trylock(&rtnl_mutex); 159 } 160 EXPORT_SYMBOL(rtnl_trylock); 161 162 int rtnl_is_locked(void) 163 { 164 return mutex_is_locked(&rtnl_mutex); 165 } 166 EXPORT_SYMBOL(rtnl_is_locked); 167 168 bool refcount_dec_and_rtnl_lock(refcount_t *r) 169 { 170 return refcount_dec_and_mutex_lock(r, &rtnl_mutex); 171 } 172 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock); 173 174 #ifdef CONFIG_PROVE_LOCKING 175 bool lockdep_rtnl_is_held(void) 176 { 177 return lockdep_is_held(&rtnl_mutex); 178 } 179 EXPORT_SYMBOL(lockdep_rtnl_is_held); 180 #endif /* #ifdef CONFIG_PROVE_LOCKING */ 181 182 #ifdef CONFIG_DEBUG_NET_SMALL_RTNL 183 void __rtnl_net_lock(struct net *net) 184 { 185 ASSERT_RTNL(); 186 187 mutex_lock(&net->rtnl_mutex); 188 } 189 EXPORT_SYMBOL(__rtnl_net_lock); 190 191 void __rtnl_net_unlock(struct net *net) 192 { 193 ASSERT_RTNL(); 194 195 mutex_unlock(&net->rtnl_mutex); 196 } 197 EXPORT_SYMBOL(__rtnl_net_unlock); 198 199 void rtnl_net_lock(struct net *net) 200 { 201 rtnl_lock(); 202 __rtnl_net_lock(net); 203 } 204 EXPORT_SYMBOL(rtnl_net_lock); 205 206 void rtnl_net_unlock(struct net *net) 207 { 208 __rtnl_net_unlock(net); 209 rtnl_unlock(); 210 } 211 EXPORT_SYMBOL(rtnl_net_unlock); 212 213 int rtnl_net_trylock(struct net *net) 214 { 215 int ret = rtnl_trylock(); 216 217 if (ret) 218 __rtnl_net_lock(net); 219 220 return ret; 221 } 222 EXPORT_SYMBOL(rtnl_net_trylock); 223 224 static int rtnl_net_cmp_locks(const struct net *net_a, const struct net *net_b) 225 { 226 if (net_eq(net_a, net_b)) 227 return 0; 228 229 /* always init_net first */ 230 if (net_eq(net_a, &init_net)) 231 return -1; 232 233 if (net_eq(net_b, &init_net)) 234 return 1; 235 236 /* otherwise lock in ascending order */ 237 return net_a < net_b ? -1 : 1; 238 } 239 240 int rtnl_net_lock_cmp_fn(const struct lockdep_map *a, const struct lockdep_map *b) 241 { 242 const struct net *net_a, *net_b; 243 244 net_a = container_of(a, struct net, rtnl_mutex.dep_map); 245 net_b = container_of(b, struct net, rtnl_mutex.dep_map); 246 247 return rtnl_net_cmp_locks(net_a, net_b); 248 } 249 250 bool rtnl_net_is_locked(struct net *net) 251 { 252 return rtnl_is_locked() && mutex_is_locked(&net->rtnl_mutex); 253 } 254 EXPORT_SYMBOL(rtnl_net_is_locked); 255 256 bool lockdep_rtnl_net_is_held(struct net *net) 257 { 258 return lockdep_rtnl_is_held() && lockdep_is_held(&net->rtnl_mutex); 259 } 260 EXPORT_SYMBOL(lockdep_rtnl_net_is_held); 261 #else 262 static int rtnl_net_cmp_locks(const struct net *net_a, const struct net *net_b) 263 { 264 /* No need to swap */ 265 return -1; 266 } 267 #endif 268 269 struct rtnl_nets { 270 /* ->newlink() needs to freeze 3 netns at most; 271 * 2 for the new device, 1 for its peer. 272 */ 273 struct net *net[3]; 274 unsigned char len; 275 }; 276 277 static void rtnl_nets_init(struct rtnl_nets *rtnl_nets) 278 { 279 memset(rtnl_nets, 0, sizeof(*rtnl_nets)); 280 } 281 282 static void rtnl_nets_destroy(struct rtnl_nets *rtnl_nets) 283 { 284 int i; 285 286 for (i = 0; i < rtnl_nets->len; i++) { 287 put_net(rtnl_nets->net[i]); 288 rtnl_nets->net[i] = NULL; 289 } 290 291 rtnl_nets->len = 0; 292 } 293 294 /** 295 * rtnl_nets_add - Add netns to be locked before ->newlink(). 296 * 297 * @rtnl_nets: rtnl_nets pointer passed to ->get_peer_net(). 298 * @net: netns pointer with an extra refcnt held. 299 * 300 * The extra refcnt is released in rtnl_nets_destroy(). 301 */ 302 static void rtnl_nets_add(struct rtnl_nets *rtnl_nets, struct net *net) 303 { 304 int i; 305 306 DEBUG_NET_WARN_ON_ONCE(rtnl_nets->len == ARRAY_SIZE(rtnl_nets->net)); 307 308 for (i = 0; i < rtnl_nets->len; i++) { 309 switch (rtnl_net_cmp_locks(rtnl_nets->net[i], net)) { 310 case 0: 311 put_net(net); 312 return; 313 case 1: 314 swap(rtnl_nets->net[i], net); 315 } 316 } 317 318 rtnl_nets->net[i] = net; 319 rtnl_nets->len++; 320 } 321 322 static void rtnl_nets_lock(struct rtnl_nets *rtnl_nets) 323 { 324 int i; 325 326 rtnl_lock(); 327 328 for (i = 0; i < rtnl_nets->len; i++) 329 __rtnl_net_lock(rtnl_nets->net[i]); 330 } 331 332 static void rtnl_nets_unlock(struct rtnl_nets *rtnl_nets) 333 { 334 int i; 335 336 for (i = 0; i < rtnl_nets->len; i++) 337 __rtnl_net_unlock(rtnl_nets->net[i]); 338 339 rtnl_unlock(); 340 } 341 342 static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 343 344 static inline int rtm_msgindex(int msgtype) 345 { 346 int msgindex = msgtype - RTM_BASE; 347 348 /* 349 * msgindex < 0 implies someone tried to register a netlink 350 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 351 * the message type has not been added to linux/rtnetlink.h 352 */ 353 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 354 355 return msgindex; 356 } 357 358 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype) 359 { 360 struct rtnl_link __rcu **tab; 361 362 if (protocol >= ARRAY_SIZE(rtnl_msg_handlers)) 363 protocol = PF_UNSPEC; 364 365 tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]); 366 if (!tab) 367 tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]); 368 369 return rcu_dereference_rtnl(tab[msgtype]); 370 } 371 372 static int rtnl_register_internal(struct module *owner, 373 int protocol, int msgtype, 374 rtnl_doit_func doit, rtnl_dumpit_func dumpit, 375 unsigned int flags) 376 { 377 struct rtnl_link *link, *old; 378 struct rtnl_link __rcu **tab; 379 int msgindex; 380 int ret = -ENOBUFS; 381 382 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 383 msgindex = rtm_msgindex(msgtype); 384 385 rtnl_lock(); 386 tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 387 if (tab == NULL) { 388 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL); 389 if (!tab) 390 goto unlock; 391 392 /* ensures we see the 0 stores */ 393 rcu_assign_pointer(rtnl_msg_handlers[protocol], tab); 394 } 395 396 old = rtnl_dereference(tab[msgindex]); 397 if (old) { 398 link = kmemdup(old, sizeof(*old), GFP_KERNEL); 399 if (!link) 400 goto unlock; 401 } else { 402 link = kzalloc(sizeof(*link), GFP_KERNEL); 403 if (!link) 404 goto unlock; 405 } 406 407 WARN_ON(link->owner && link->owner != owner); 408 link->owner = owner; 409 410 WARN_ON(doit && link->doit && link->doit != doit); 411 if (doit) 412 link->doit = doit; 413 WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit); 414 if (dumpit) 415 link->dumpit = dumpit; 416 417 WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL && 418 (flags & RTNL_FLAG_BULK_DEL_SUPPORTED)); 419 link->flags |= flags; 420 421 /* publish protocol:msgtype */ 422 rcu_assign_pointer(tab[msgindex], link); 423 ret = 0; 424 if (old) 425 kfree_rcu(old, rcu); 426 unlock: 427 rtnl_unlock(); 428 return ret; 429 } 430 431 /** 432 * rtnl_unregister - Unregister a rtnetlink message type 433 * @protocol: Protocol family or PF_UNSPEC 434 * @msgtype: rtnetlink message type 435 * 436 * Returns 0 on success or a negative error code. 437 */ 438 static int rtnl_unregister(int protocol, int msgtype) 439 { 440 struct rtnl_link __rcu **tab; 441 struct rtnl_link *link; 442 int msgindex; 443 444 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 445 msgindex = rtm_msgindex(msgtype); 446 447 rtnl_lock(); 448 tab = rtnl_dereference(rtnl_msg_handlers[protocol]); 449 if (!tab) { 450 rtnl_unlock(); 451 return -ENOENT; 452 } 453 454 link = rcu_replace_pointer_rtnl(tab[msgindex], NULL); 455 rtnl_unlock(); 456 457 kfree_rcu(link, rcu); 458 459 return 0; 460 } 461 462 /** 463 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 464 * @protocol : Protocol family or PF_UNSPEC 465 * 466 * Identical to calling rtnl_unregster() for all registered message types 467 * of a certain protocol family. 468 */ 469 void rtnl_unregister_all(int protocol) 470 { 471 struct rtnl_link __rcu **tab; 472 struct rtnl_link *link; 473 int msgindex; 474 475 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 476 477 rtnl_lock(); 478 tab = rcu_replace_pointer_rtnl(rtnl_msg_handlers[protocol], NULL); 479 if (!tab) { 480 rtnl_unlock(); 481 return; 482 } 483 for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) { 484 link = rcu_replace_pointer_rtnl(tab[msgindex], NULL); 485 kfree_rcu(link, rcu); 486 } 487 rtnl_unlock(); 488 489 synchronize_net(); 490 491 kfree(tab); 492 } 493 EXPORT_SYMBOL_GPL(rtnl_unregister_all); 494 495 /** 496 * __rtnl_register_many - Register rtnetlink message types 497 * @handlers: Array of struct rtnl_msg_handlers 498 * @n: The length of @handlers 499 * 500 * Registers the specified function pointers (at least one of them has 501 * to be non-NULL) to be called whenever a request message for the 502 * specified protocol family and message type is received. 503 * 504 * The special protocol family PF_UNSPEC may be used to define fallback 505 * function pointers for the case when no entry for the specific protocol 506 * family exists. 507 * 508 * When one element of @handlers fails to register, 509 * 1) built-in: panics. 510 * 2) modules : the previous successful registrations are unwinded 511 * and an error is returned. 512 * 513 * Use rtnl_register_many(). 514 */ 515 int __rtnl_register_many(const struct rtnl_msg_handler *handlers, int n) 516 { 517 const struct rtnl_msg_handler *handler; 518 int i, err; 519 520 for (i = 0, handler = handlers; i < n; i++, handler++) { 521 err = rtnl_register_internal(handler->owner, handler->protocol, 522 handler->msgtype, handler->doit, 523 handler->dumpit, handler->flags); 524 if (err) { 525 if (!handler->owner) 526 panic("Unable to register rtnetlink message " 527 "handlers, %pS\n", handlers); 528 529 __rtnl_unregister_many(handlers, i); 530 break; 531 } 532 } 533 534 return err; 535 } 536 EXPORT_SYMBOL_GPL(__rtnl_register_many); 537 538 void __rtnl_unregister_many(const struct rtnl_msg_handler *handlers, int n) 539 { 540 const struct rtnl_msg_handler *handler; 541 int i; 542 543 for (i = n - 1, handler = handlers + n - 1; i >= 0; i--, handler--) 544 rtnl_unregister(handler->protocol, handler->msgtype); 545 } 546 EXPORT_SYMBOL_GPL(__rtnl_unregister_many); 547 548 static DEFINE_MUTEX(link_ops_mutex); 549 static LIST_HEAD(link_ops); 550 551 static struct rtnl_link_ops *rtnl_link_ops_get(const char *kind, int *srcu_index) 552 { 553 struct rtnl_link_ops *ops; 554 555 rcu_read_lock(); 556 557 list_for_each_entry_rcu(ops, &link_ops, list) { 558 if (!strcmp(ops->kind, kind)) { 559 *srcu_index = srcu_read_lock(&ops->srcu); 560 goto unlock; 561 } 562 } 563 564 ops = NULL; 565 unlock: 566 rcu_read_unlock(); 567 568 return ops; 569 } 570 571 static void rtnl_link_ops_put(struct rtnl_link_ops *ops, int srcu_index) 572 { 573 srcu_read_unlock(&ops->srcu, srcu_index); 574 } 575 576 /** 577 * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 578 * @ops: struct rtnl_link_ops * to register 579 * 580 * Returns 0 on success or a negative error code. 581 */ 582 int rtnl_link_register(struct rtnl_link_ops *ops) 583 { 584 struct rtnl_link_ops *tmp; 585 int err; 586 587 /* Sanity-check max sizes to avoid stack buffer overflow. */ 588 if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE || 589 ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)) 590 return -EINVAL; 591 592 /* The check for alloc/setup is here because if ops 593 * does not have that filled up, it is not possible 594 * to use the ops for creating device. So do not 595 * fill up dellink as well. That disables rtnl_dellink. 596 */ 597 if ((ops->alloc || ops->setup) && !ops->dellink) 598 ops->dellink = unregister_netdevice_queue; 599 600 err = init_srcu_struct(&ops->srcu); 601 if (err) 602 return err; 603 604 mutex_lock(&link_ops_mutex); 605 606 list_for_each_entry(tmp, &link_ops, list) { 607 if (!strcmp(ops->kind, tmp->kind)) { 608 err = -EEXIST; 609 goto unlock; 610 } 611 } 612 613 list_add_tail_rcu(&ops->list, &link_ops); 614 unlock: 615 mutex_unlock(&link_ops_mutex); 616 617 return err; 618 } 619 EXPORT_SYMBOL_GPL(rtnl_link_register); 620 621 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 622 { 623 struct net_device *dev; 624 LIST_HEAD(list_kill); 625 626 for_each_netdev(net, dev) { 627 if (dev->rtnl_link_ops == ops) 628 ops->dellink(dev, &list_kill); 629 } 630 unregister_netdevice_many(&list_kill); 631 } 632 633 /* Return with the rtnl_lock held when there are no network 634 * devices unregistering in any network namespace. 635 */ 636 static void rtnl_lock_unregistering_all(void) 637 { 638 DEFINE_WAIT_FUNC(wait, woken_wake_function); 639 640 add_wait_queue(&netdev_unregistering_wq, &wait); 641 for (;;) { 642 rtnl_lock(); 643 /* We held write locked pernet_ops_rwsem, and parallel 644 * setup_net() and cleanup_net() are not possible. 645 */ 646 if (!atomic_read(&dev_unreg_count)) 647 break; 648 __rtnl_unlock(); 649 650 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 651 } 652 remove_wait_queue(&netdev_unregistering_wq, &wait); 653 } 654 655 /** 656 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 657 * @ops: struct rtnl_link_ops * to unregister 658 */ 659 void rtnl_link_unregister(struct rtnl_link_ops *ops) 660 { 661 struct net *net; 662 663 mutex_lock(&link_ops_mutex); 664 list_del_rcu(&ops->list); 665 mutex_unlock(&link_ops_mutex); 666 667 synchronize_srcu(&ops->srcu); 668 cleanup_srcu_struct(&ops->srcu); 669 670 /* Close the race with setup_net() and cleanup_net() */ 671 down_write(&pernet_ops_rwsem); 672 rtnl_lock_unregistering_all(); 673 674 for_each_net(net) 675 __rtnl_kill_links(net, ops); 676 677 rtnl_unlock(); 678 up_write(&pernet_ops_rwsem); 679 } 680 EXPORT_SYMBOL_GPL(rtnl_link_unregister); 681 682 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 683 { 684 struct net_device *master_dev; 685 const struct rtnl_link_ops *ops; 686 size_t size = 0; 687 688 rcu_read_lock(); 689 690 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 691 if (!master_dev) 692 goto out; 693 694 ops = master_dev->rtnl_link_ops; 695 if (!ops || !ops->get_slave_size) 696 goto out; 697 /* IFLA_INFO_SLAVE_DATA + nested data */ 698 size = nla_total_size(sizeof(struct nlattr)) + 699 ops->get_slave_size(master_dev, dev); 700 701 out: 702 rcu_read_unlock(); 703 return size; 704 } 705 706 static size_t rtnl_link_get_size(const struct net_device *dev) 707 { 708 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 709 size_t size; 710 711 if (!ops) 712 return 0; 713 714 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 715 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 716 717 if (ops->get_size) 718 /* IFLA_INFO_DATA + nested data */ 719 size += nla_total_size(sizeof(struct nlattr)) + 720 ops->get_size(dev); 721 722 if (ops->get_xstats_size) 723 /* IFLA_INFO_XSTATS */ 724 size += nla_total_size(ops->get_xstats_size(dev)); 725 726 size += rtnl_link_get_slave_info_data_size(dev); 727 728 return size; 729 } 730 731 static LIST_HEAD(rtnl_af_ops); 732 733 static struct rtnl_af_ops *rtnl_af_lookup(const int family, int *srcu_index) 734 { 735 struct rtnl_af_ops *ops; 736 737 ASSERT_RTNL(); 738 739 rcu_read_lock(); 740 741 list_for_each_entry_rcu(ops, &rtnl_af_ops, list) { 742 if (ops->family == family) { 743 *srcu_index = srcu_read_lock(&ops->srcu); 744 goto unlock; 745 } 746 } 747 748 ops = NULL; 749 unlock: 750 rcu_read_unlock(); 751 752 return ops; 753 } 754 755 static void rtnl_af_put(struct rtnl_af_ops *ops, int srcu_index) 756 { 757 srcu_read_unlock(&ops->srcu, srcu_index); 758 } 759 760 /** 761 * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 762 * @ops: struct rtnl_af_ops * to register 763 * 764 * Return: 0 on success or a negative error code. 765 */ 766 int rtnl_af_register(struct rtnl_af_ops *ops) 767 { 768 int err = init_srcu_struct(&ops->srcu); 769 770 if (err) 771 return err; 772 773 rtnl_lock(); 774 list_add_tail_rcu(&ops->list, &rtnl_af_ops); 775 rtnl_unlock(); 776 777 return 0; 778 } 779 EXPORT_SYMBOL_GPL(rtnl_af_register); 780 781 /** 782 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 783 * @ops: struct rtnl_af_ops * to unregister 784 */ 785 void rtnl_af_unregister(struct rtnl_af_ops *ops) 786 { 787 rtnl_lock(); 788 list_del_rcu(&ops->list); 789 rtnl_unlock(); 790 791 synchronize_rcu(); 792 synchronize_srcu(&ops->srcu); 793 cleanup_srcu_struct(&ops->srcu); 794 } 795 EXPORT_SYMBOL_GPL(rtnl_af_unregister); 796 797 static size_t rtnl_link_get_af_size(const struct net_device *dev, 798 u32 ext_filter_mask) 799 { 800 struct rtnl_af_ops *af_ops; 801 size_t size; 802 803 /* IFLA_AF_SPEC */ 804 size = nla_total_size(sizeof(struct nlattr)); 805 806 rcu_read_lock(); 807 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 808 if (af_ops->get_link_af_size) { 809 /* AF_* + nested data */ 810 size += nla_total_size(sizeof(struct nlattr)) + 811 af_ops->get_link_af_size(dev, ext_filter_mask); 812 } 813 } 814 rcu_read_unlock(); 815 816 return size; 817 } 818 819 static bool rtnl_have_link_slave_info(const struct net_device *dev) 820 { 821 struct net_device *master_dev; 822 bool ret = false; 823 824 rcu_read_lock(); 825 826 master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev); 827 if (master_dev && master_dev->rtnl_link_ops) 828 ret = true; 829 rcu_read_unlock(); 830 return ret; 831 } 832 833 static int rtnl_link_slave_info_fill(struct sk_buff *skb, 834 const struct net_device *dev) 835 { 836 struct net_device *master_dev; 837 const struct rtnl_link_ops *ops; 838 struct nlattr *slave_data; 839 int err; 840 841 master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 842 if (!master_dev) 843 return 0; 844 ops = master_dev->rtnl_link_ops; 845 if (!ops) 846 return 0; 847 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 848 return -EMSGSIZE; 849 if (ops->fill_slave_info) { 850 slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA); 851 if (!slave_data) 852 return -EMSGSIZE; 853 err = ops->fill_slave_info(skb, master_dev, dev); 854 if (err < 0) 855 goto err_cancel_slave_data; 856 nla_nest_end(skb, slave_data); 857 } 858 return 0; 859 860 err_cancel_slave_data: 861 nla_nest_cancel(skb, slave_data); 862 return err; 863 } 864 865 static int rtnl_link_info_fill(struct sk_buff *skb, 866 const struct net_device *dev) 867 { 868 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 869 struct nlattr *data; 870 int err; 871 872 if (!ops) 873 return 0; 874 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 875 return -EMSGSIZE; 876 if (ops->fill_xstats) { 877 err = ops->fill_xstats(skb, dev); 878 if (err < 0) 879 return err; 880 } 881 if (ops->fill_info) { 882 data = nla_nest_start_noflag(skb, IFLA_INFO_DATA); 883 if (data == NULL) 884 return -EMSGSIZE; 885 err = ops->fill_info(skb, dev); 886 if (err < 0) 887 goto err_cancel_data; 888 nla_nest_end(skb, data); 889 } 890 return 0; 891 892 err_cancel_data: 893 nla_nest_cancel(skb, data); 894 return err; 895 } 896 897 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 898 { 899 struct nlattr *linkinfo; 900 int err = -EMSGSIZE; 901 902 linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO); 903 if (linkinfo == NULL) 904 goto out; 905 906 err = rtnl_link_info_fill(skb, dev); 907 if (err < 0) 908 goto err_cancel_link; 909 910 err = rtnl_link_slave_info_fill(skb, dev); 911 if (err < 0) 912 goto err_cancel_link; 913 914 nla_nest_end(skb, linkinfo); 915 return 0; 916 917 err_cancel_link: 918 nla_nest_cancel(skb, linkinfo); 919 out: 920 return err; 921 } 922 923 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 924 { 925 struct sock *rtnl = net->rtnl; 926 927 return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL); 928 } 929 930 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 931 { 932 struct sock *rtnl = net->rtnl; 933 934 return nlmsg_unicast(rtnl, skb, pid); 935 } 936 EXPORT_SYMBOL(rtnl_unicast); 937 938 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 939 const struct nlmsghdr *nlh, gfp_t flags) 940 { 941 struct sock *rtnl = net->rtnl; 942 943 nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags); 944 } 945 EXPORT_SYMBOL(rtnl_notify); 946 947 void rtnl_set_sk_err(struct net *net, u32 group, int error) 948 { 949 struct sock *rtnl = net->rtnl; 950 951 netlink_set_err(rtnl, 0, group, error); 952 } 953 EXPORT_SYMBOL(rtnl_set_sk_err); 954 955 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 956 { 957 struct nlattr *mx; 958 int i, valid = 0; 959 960 /* nothing is dumped for dst_default_metrics, so just skip the loop */ 961 if (metrics == dst_default_metrics.metrics) 962 return 0; 963 964 mx = nla_nest_start_noflag(skb, RTA_METRICS); 965 if (mx == NULL) 966 return -ENOBUFS; 967 968 for (i = 0; i < RTAX_MAX; i++) { 969 if (metrics[i]) { 970 if (i == RTAX_CC_ALGO - 1) { 971 char tmp[TCP_CA_NAME_MAX], *name; 972 973 name = tcp_ca_get_name_by_key(metrics[i], tmp); 974 if (!name) 975 continue; 976 if (nla_put_string(skb, i + 1, name)) 977 goto nla_put_failure; 978 } else if (i == RTAX_FEATURES - 1) { 979 u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 980 981 if (!user_features) 982 continue; 983 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 984 if (nla_put_u32(skb, i + 1, user_features)) 985 goto nla_put_failure; 986 } else { 987 if (nla_put_u32(skb, i + 1, metrics[i])) 988 goto nla_put_failure; 989 } 990 valid++; 991 } 992 } 993 994 if (!valid) { 995 nla_nest_cancel(skb, mx); 996 return 0; 997 } 998 999 return nla_nest_end(skb, mx); 1000 1001 nla_put_failure: 1002 nla_nest_cancel(skb, mx); 1003 return -EMSGSIZE; 1004 } 1005 EXPORT_SYMBOL(rtnetlink_put_metrics); 1006 1007 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 1008 long expires, u32 error) 1009 { 1010 struct rta_cacheinfo ci = { 1011 .rta_error = error, 1012 .rta_id = id, 1013 }; 1014 1015 if (dst) { 1016 ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse); 1017 ci.rta_used = dst->__use; 1018 ci.rta_clntref = rcuref_read(&dst->__rcuref); 1019 } 1020 if (expires) { 1021 unsigned long clock; 1022 1023 clock = jiffies_to_clock_t(abs(expires)); 1024 clock = min_t(unsigned long, clock, INT_MAX); 1025 ci.rta_expires = (expires > 0) ? clock : -clock; 1026 } 1027 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 1028 } 1029 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 1030 1031 void netdev_set_operstate(struct net_device *dev, int newstate) 1032 { 1033 unsigned int old = READ_ONCE(dev->operstate); 1034 1035 do { 1036 if (old == newstate) 1037 return; 1038 } while (!try_cmpxchg(&dev->operstate, &old, newstate)); 1039 1040 netdev_state_change(dev); 1041 } 1042 EXPORT_SYMBOL(netdev_set_operstate); 1043 1044 static void set_operstate(struct net_device *dev, unsigned char transition) 1045 { 1046 unsigned char operstate = READ_ONCE(dev->operstate); 1047 1048 switch (transition) { 1049 case IF_OPER_UP: 1050 if ((operstate == IF_OPER_DORMANT || 1051 operstate == IF_OPER_TESTING || 1052 operstate == IF_OPER_UNKNOWN) && 1053 !netif_dormant(dev) && !netif_testing(dev)) 1054 operstate = IF_OPER_UP; 1055 break; 1056 1057 case IF_OPER_TESTING: 1058 if (netif_oper_up(dev)) 1059 operstate = IF_OPER_TESTING; 1060 break; 1061 1062 case IF_OPER_DORMANT: 1063 if (netif_oper_up(dev)) 1064 operstate = IF_OPER_DORMANT; 1065 break; 1066 } 1067 1068 netdev_set_operstate(dev, operstate); 1069 } 1070 1071 static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 1072 { 1073 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 1074 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 1075 } 1076 1077 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 1078 const struct ifinfomsg *ifm) 1079 { 1080 unsigned int flags = ifm->ifi_flags; 1081 1082 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 1083 if (ifm->ifi_change) 1084 flags = (flags & ifm->ifi_change) | 1085 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 1086 1087 return flags; 1088 } 1089 1090 static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 1091 const struct rtnl_link_stats64 *b) 1092 { 1093 a->rx_packets = b->rx_packets; 1094 a->tx_packets = b->tx_packets; 1095 a->rx_bytes = b->rx_bytes; 1096 a->tx_bytes = b->tx_bytes; 1097 a->rx_errors = b->rx_errors; 1098 a->tx_errors = b->tx_errors; 1099 a->rx_dropped = b->rx_dropped; 1100 a->tx_dropped = b->tx_dropped; 1101 1102 a->multicast = b->multicast; 1103 a->collisions = b->collisions; 1104 1105 a->rx_length_errors = b->rx_length_errors; 1106 a->rx_over_errors = b->rx_over_errors; 1107 a->rx_crc_errors = b->rx_crc_errors; 1108 a->rx_frame_errors = b->rx_frame_errors; 1109 a->rx_fifo_errors = b->rx_fifo_errors; 1110 a->rx_missed_errors = b->rx_missed_errors; 1111 1112 a->tx_aborted_errors = b->tx_aborted_errors; 1113 a->tx_carrier_errors = b->tx_carrier_errors; 1114 a->tx_fifo_errors = b->tx_fifo_errors; 1115 a->tx_heartbeat_errors = b->tx_heartbeat_errors; 1116 a->tx_window_errors = b->tx_window_errors; 1117 1118 a->rx_compressed = b->rx_compressed; 1119 a->tx_compressed = b->tx_compressed; 1120 1121 a->rx_nohandler = b->rx_nohandler; 1122 } 1123 1124 /* All VF info */ 1125 static inline int rtnl_vfinfo_size(const struct net_device *dev, 1126 u32 ext_filter_mask) 1127 { 1128 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 1129 int num_vfs = dev_num_vf(dev->dev.parent); 1130 size_t size = nla_total_size(0); 1131 size += num_vfs * 1132 (nla_total_size(0) + 1133 nla_total_size(sizeof(struct ifla_vf_mac)) + 1134 nla_total_size(sizeof(struct ifla_vf_broadcast)) + 1135 nla_total_size(sizeof(struct ifla_vf_vlan)) + 1136 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 1137 nla_total_size(MAX_VLAN_LIST_LEN * 1138 sizeof(struct ifla_vf_vlan_info)) + 1139 nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 1140 nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 1141 nla_total_size(sizeof(struct ifla_vf_rate)) + 1142 nla_total_size(sizeof(struct ifla_vf_link_state)) + 1143 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 1144 nla_total_size(sizeof(struct ifla_vf_trust))); 1145 if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { 1146 size += num_vfs * 1147 (nla_total_size(0) + /* nest IFLA_VF_STATS */ 1148 /* IFLA_VF_STATS_RX_PACKETS */ 1149 nla_total_size_64bit(sizeof(__u64)) + 1150 /* IFLA_VF_STATS_TX_PACKETS */ 1151 nla_total_size_64bit(sizeof(__u64)) + 1152 /* IFLA_VF_STATS_RX_BYTES */ 1153 nla_total_size_64bit(sizeof(__u64)) + 1154 /* IFLA_VF_STATS_TX_BYTES */ 1155 nla_total_size_64bit(sizeof(__u64)) + 1156 /* IFLA_VF_STATS_BROADCAST */ 1157 nla_total_size_64bit(sizeof(__u64)) + 1158 /* IFLA_VF_STATS_MULTICAST */ 1159 nla_total_size_64bit(sizeof(__u64)) + 1160 /* IFLA_VF_STATS_RX_DROPPED */ 1161 nla_total_size_64bit(sizeof(__u64)) + 1162 /* IFLA_VF_STATS_TX_DROPPED */ 1163 nla_total_size_64bit(sizeof(__u64))); 1164 } 1165 return size; 1166 } else 1167 return 0; 1168 } 1169 1170 static size_t rtnl_port_size(const struct net_device *dev, 1171 u32 ext_filter_mask) 1172 { 1173 size_t port_size = nla_total_size(4) /* PORT_VF */ 1174 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 1175 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 1176 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 1177 + nla_total_size(1) /* PROT_VDP_REQUEST */ 1178 + nla_total_size(2); /* PORT_VDP_RESPONSE */ 1179 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 1180 size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 1181 + port_size; 1182 size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 1183 + port_size; 1184 1185 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1186 !(ext_filter_mask & RTEXT_FILTER_VF)) 1187 return 0; 1188 if (dev_num_vf(dev->dev.parent)) 1189 return port_self_size + vf_ports_size + 1190 vf_port_size * dev_num_vf(dev->dev.parent); 1191 else 1192 return port_self_size; 1193 } 1194 1195 static size_t rtnl_xdp_size(void) 1196 { 1197 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 1198 nla_total_size(1) + /* XDP_ATTACHED */ 1199 nla_total_size(4) + /* XDP_PROG_ID (or 1st mode) */ 1200 nla_total_size(4); /* XDP_<mode>_PROG_ID */ 1201 1202 return xdp_size; 1203 } 1204 1205 static size_t rtnl_prop_list_size(const struct net_device *dev) 1206 { 1207 struct netdev_name_node *name_node; 1208 unsigned int cnt = 0; 1209 1210 rcu_read_lock(); 1211 list_for_each_entry_rcu(name_node, &dev->name_node->list, list) 1212 cnt++; 1213 rcu_read_unlock(); 1214 1215 if (!cnt) 1216 return 0; 1217 1218 return nla_total_size(0) + cnt * nla_total_size(ALTIFNAMSIZ); 1219 } 1220 1221 static size_t rtnl_proto_down_size(const struct net_device *dev) 1222 { 1223 size_t size = nla_total_size(1); 1224 1225 /* Assume dev->proto_down_reason is not zero. */ 1226 size += nla_total_size(0) + nla_total_size(4); 1227 1228 return size; 1229 } 1230 1231 static size_t rtnl_devlink_port_size(const struct net_device *dev) 1232 { 1233 size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */ 1234 1235 if (dev->devlink_port) 1236 size += devlink_nl_port_handle_size(dev->devlink_port); 1237 1238 return size; 1239 } 1240 1241 static size_t rtnl_dpll_pin_size(const struct net_device *dev) 1242 { 1243 size_t size = nla_total_size(0); /* nest IFLA_DPLL_PIN */ 1244 1245 size += dpll_netdev_pin_handle_size(dev); 1246 1247 return size; 1248 } 1249 1250 static noinline size_t if_nlmsg_size(const struct net_device *dev, 1251 u32 ext_filter_mask) 1252 { 1253 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 1254 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 1255 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 1256 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 1257 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 1258 + nla_total_size(sizeof(struct rtnl_link_stats)) 1259 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 1260 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 1261 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 1262 + nla_total_size(4) /* IFLA_TXQLEN */ 1263 + nla_total_size(4) /* IFLA_WEIGHT */ 1264 + nla_total_size(4) /* IFLA_MTU */ 1265 + nla_total_size(4) /* IFLA_LINK */ 1266 + nla_total_size(4) /* IFLA_MASTER */ 1267 + nla_total_size(1) /* IFLA_CARRIER */ 1268 + nla_total_size(4) /* IFLA_PROMISCUITY */ 1269 + nla_total_size(4) /* IFLA_ALLMULTI */ 1270 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 1271 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 1272 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 1273 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 1274 + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */ 1275 + nla_total_size(4) /* IFLA_GSO_IPV4_MAX_SIZE */ 1276 + nla_total_size(4) /* IFLA_GRO_IPV4_MAX_SIZE */ 1277 + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */ 1278 + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */ 1279 + nla_total_size(1) /* IFLA_OPERSTATE */ 1280 + nla_total_size(1) /* IFLA_LINKMODE */ 1281 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 1282 + nla_total_size(4) /* IFLA_LINK_NETNSID */ 1283 + nla_total_size(4) /* IFLA_GROUP */ 1284 + nla_total_size(ext_filter_mask 1285 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 1286 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 1287 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 1288 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 1289 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 1290 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 1291 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 1292 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 1293 + rtnl_xdp_size() /* IFLA_XDP */ 1294 + nla_total_size(4) /* IFLA_EVENT */ 1295 + nla_total_size(4) /* IFLA_NEW_NETNSID */ 1296 + nla_total_size(4) /* IFLA_NEW_IFINDEX */ 1297 + rtnl_proto_down_size(dev) /* proto down */ 1298 + nla_total_size(4) /* IFLA_TARGET_NETNSID */ 1299 + nla_total_size(4) /* IFLA_CARRIER_UP_COUNT */ 1300 + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ 1301 + nla_total_size(4) /* IFLA_MIN_MTU */ 1302 + nla_total_size(4) /* IFLA_MAX_MTU */ 1303 + rtnl_prop_list_size(dev) 1304 + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */ 1305 + rtnl_devlink_port_size(dev) 1306 + rtnl_dpll_pin_size(dev) 1307 + nla_total_size(8) /* IFLA_MAX_PACING_OFFLOAD_HORIZON */ 1308 + 0; 1309 } 1310 1311 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 1312 { 1313 struct nlattr *vf_ports; 1314 struct nlattr *vf_port; 1315 int vf; 1316 int err; 1317 1318 vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS); 1319 if (!vf_ports) 1320 return -EMSGSIZE; 1321 1322 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 1323 vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT); 1324 if (!vf_port) 1325 goto nla_put_failure; 1326 if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 1327 goto nla_put_failure; 1328 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 1329 if (err == -EMSGSIZE) 1330 goto nla_put_failure; 1331 if (err) { 1332 nla_nest_cancel(skb, vf_port); 1333 continue; 1334 } 1335 nla_nest_end(skb, vf_port); 1336 } 1337 1338 nla_nest_end(skb, vf_ports); 1339 1340 return 0; 1341 1342 nla_put_failure: 1343 nla_nest_cancel(skb, vf_ports); 1344 return -EMSGSIZE; 1345 } 1346 1347 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 1348 { 1349 struct nlattr *port_self; 1350 int err; 1351 1352 port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF); 1353 if (!port_self) 1354 return -EMSGSIZE; 1355 1356 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 1357 if (err) { 1358 nla_nest_cancel(skb, port_self); 1359 return (err == -EMSGSIZE) ? err : 0; 1360 } 1361 1362 nla_nest_end(skb, port_self); 1363 1364 return 0; 1365 } 1366 1367 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1368 u32 ext_filter_mask) 1369 { 1370 int err; 1371 1372 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1373 !(ext_filter_mask & RTEXT_FILTER_VF)) 1374 return 0; 1375 1376 err = rtnl_port_self_fill(skb, dev); 1377 if (err) 1378 return err; 1379 1380 if (dev_num_vf(dev->dev.parent)) { 1381 err = rtnl_vf_ports_fill(skb, dev); 1382 if (err) 1383 return err; 1384 } 1385 1386 return 0; 1387 } 1388 1389 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 1390 { 1391 int err; 1392 struct netdev_phys_item_id ppid; 1393 1394 err = dev_get_phys_port_id(dev, &ppid); 1395 if (err) { 1396 if (err == -EOPNOTSUPP) 1397 return 0; 1398 return err; 1399 } 1400 1401 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 1402 return -EMSGSIZE; 1403 1404 return 0; 1405 } 1406 1407 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1408 { 1409 char name[IFNAMSIZ]; 1410 int err; 1411 1412 err = dev_get_phys_port_name(dev, name, sizeof(name)); 1413 if (err) { 1414 if (err == -EOPNOTSUPP) 1415 return 0; 1416 return err; 1417 } 1418 1419 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1420 return -EMSGSIZE; 1421 1422 return 0; 1423 } 1424 1425 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 1426 { 1427 struct netdev_phys_item_id ppid = { }; 1428 int err; 1429 1430 err = dev_get_port_parent_id(dev, &ppid, false); 1431 if (err) { 1432 if (err == -EOPNOTSUPP) 1433 return 0; 1434 return err; 1435 } 1436 1437 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id)) 1438 return -EMSGSIZE; 1439 1440 return 0; 1441 } 1442 1443 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1444 struct net_device *dev) 1445 { 1446 struct rtnl_link_stats64 *sp; 1447 struct nlattr *attr; 1448 1449 attr = nla_reserve_64bit(skb, IFLA_STATS64, 1450 sizeof(struct rtnl_link_stats64), IFLA_PAD); 1451 if (!attr) 1452 return -EMSGSIZE; 1453 1454 sp = nla_data(attr); 1455 dev_get_stats(dev, sp); 1456 1457 attr = nla_reserve(skb, IFLA_STATS, 1458 sizeof(struct rtnl_link_stats)); 1459 if (!attr) 1460 return -EMSGSIZE; 1461 1462 copy_rtnl_link_stats(nla_data(attr), sp); 1463 1464 return 0; 1465 } 1466 1467 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1468 struct net_device *dev, 1469 int vfs_num, 1470 u32 ext_filter_mask) 1471 { 1472 struct ifla_vf_rss_query_en vf_rss_query_en; 1473 struct nlattr *vf, *vfstats, *vfvlanlist; 1474 struct ifla_vf_link_state vf_linkstate; 1475 struct ifla_vf_vlan_info vf_vlan_info; 1476 struct ifla_vf_spoofchk vf_spoofchk; 1477 struct ifla_vf_tx_rate vf_tx_rate; 1478 struct ifla_vf_stats vf_stats; 1479 struct ifla_vf_trust vf_trust; 1480 struct ifla_vf_vlan vf_vlan; 1481 struct ifla_vf_rate vf_rate; 1482 struct ifla_vf_mac vf_mac; 1483 struct ifla_vf_broadcast vf_broadcast; 1484 struct ifla_vf_info ivi; 1485 struct ifla_vf_guid node_guid; 1486 struct ifla_vf_guid port_guid; 1487 1488 memset(&ivi, 0, sizeof(ivi)); 1489 1490 /* Not all SR-IOV capable drivers support the 1491 * spoofcheck and "RSS query enable" query. Preset to 1492 * -1 so the user space tool can detect that the driver 1493 * didn't report anything. 1494 */ 1495 ivi.spoofchk = -1; 1496 ivi.rss_query_en = -1; 1497 ivi.trusted = -1; 1498 /* The default value for VF link state is "auto" 1499 * IFLA_VF_LINK_STATE_AUTO which equals zero 1500 */ 1501 ivi.linkstate = 0; 1502 /* VLAN Protocol by default is 802.1Q */ 1503 ivi.vlan_proto = htons(ETH_P_8021Q); 1504 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1505 return 0; 1506 1507 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 1508 memset(&node_guid, 0, sizeof(node_guid)); 1509 memset(&port_guid, 0, sizeof(port_guid)); 1510 1511 vf_mac.vf = 1512 vf_vlan.vf = 1513 vf_vlan_info.vf = 1514 vf_rate.vf = 1515 vf_tx_rate.vf = 1516 vf_spoofchk.vf = 1517 vf_linkstate.vf = 1518 vf_rss_query_en.vf = 1519 vf_trust.vf = 1520 node_guid.vf = 1521 port_guid.vf = ivi.vf; 1522 1523 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1524 memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len); 1525 vf_vlan.vlan = ivi.vlan; 1526 vf_vlan.qos = ivi.qos; 1527 vf_vlan_info.vlan = ivi.vlan; 1528 vf_vlan_info.qos = ivi.qos; 1529 vf_vlan_info.vlan_proto = ivi.vlan_proto; 1530 vf_tx_rate.rate = ivi.max_tx_rate; 1531 vf_rate.min_tx_rate = ivi.min_tx_rate; 1532 vf_rate.max_tx_rate = ivi.max_tx_rate; 1533 vf_spoofchk.setting = ivi.spoofchk; 1534 vf_linkstate.link_state = ivi.linkstate; 1535 vf_rss_query_en.setting = ivi.rss_query_en; 1536 vf_trust.setting = ivi.trusted; 1537 vf = nla_nest_start_noflag(skb, IFLA_VF_INFO); 1538 if (!vf) 1539 return -EMSGSIZE; 1540 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1541 nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) || 1542 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1543 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1544 &vf_rate) || 1545 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1546 &vf_tx_rate) || 1547 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1548 &vf_spoofchk) || 1549 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1550 &vf_linkstate) || 1551 nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1552 sizeof(vf_rss_query_en), 1553 &vf_rss_query_en) || 1554 nla_put(skb, IFLA_VF_TRUST, 1555 sizeof(vf_trust), &vf_trust)) 1556 goto nla_put_vf_failure; 1557 1558 if (dev->netdev_ops->ndo_get_vf_guid && 1559 !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid, 1560 &port_guid)) { 1561 if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid), 1562 &node_guid) || 1563 nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid), 1564 &port_guid)) 1565 goto nla_put_vf_failure; 1566 } 1567 vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST); 1568 if (!vfvlanlist) 1569 goto nla_put_vf_failure; 1570 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 1571 &vf_vlan_info)) { 1572 nla_nest_cancel(skb, vfvlanlist); 1573 goto nla_put_vf_failure; 1574 } 1575 nla_nest_end(skb, vfvlanlist); 1576 if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) { 1577 memset(&vf_stats, 0, sizeof(vf_stats)); 1578 if (dev->netdev_ops->ndo_get_vf_stats) 1579 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1580 &vf_stats); 1581 vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS); 1582 if (!vfstats) 1583 goto nla_put_vf_failure; 1584 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1585 vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1586 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1587 vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1588 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1589 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1590 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1591 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1592 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1593 vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1594 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1595 vf_stats.multicast, IFLA_VF_STATS_PAD) || 1596 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED, 1597 vf_stats.rx_dropped, IFLA_VF_STATS_PAD) || 1598 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED, 1599 vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) { 1600 nla_nest_cancel(skb, vfstats); 1601 goto nla_put_vf_failure; 1602 } 1603 nla_nest_end(skb, vfstats); 1604 } 1605 nla_nest_end(skb, vf); 1606 return 0; 1607 1608 nla_put_vf_failure: 1609 nla_nest_cancel(skb, vf); 1610 return -EMSGSIZE; 1611 } 1612 1613 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb, 1614 struct net_device *dev, 1615 u32 ext_filter_mask) 1616 { 1617 struct nlattr *vfinfo; 1618 int i, num_vfs; 1619 1620 if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0)) 1621 return 0; 1622 1623 num_vfs = dev_num_vf(dev->dev.parent); 1624 if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs)) 1625 return -EMSGSIZE; 1626 1627 if (!dev->netdev_ops->ndo_get_vf_config) 1628 return 0; 1629 1630 vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST); 1631 if (!vfinfo) 1632 return -EMSGSIZE; 1633 1634 for (i = 0; i < num_vfs; i++) { 1635 if (rtnl_fill_vfinfo(skb, dev, i, ext_filter_mask)) { 1636 nla_nest_cancel(skb, vfinfo); 1637 return -EMSGSIZE; 1638 } 1639 } 1640 1641 nla_nest_end(skb, vfinfo); 1642 return 0; 1643 } 1644 1645 static int rtnl_fill_link_ifmap(struct sk_buff *skb, 1646 const struct net_device *dev) 1647 { 1648 struct rtnl_link_ifmap map; 1649 1650 memset(&map, 0, sizeof(map)); 1651 map.mem_start = READ_ONCE(dev->mem_start); 1652 map.mem_end = READ_ONCE(dev->mem_end); 1653 map.base_addr = READ_ONCE(dev->base_addr); 1654 map.irq = READ_ONCE(dev->irq); 1655 map.dma = READ_ONCE(dev->dma); 1656 map.port = READ_ONCE(dev->if_port); 1657 1658 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1659 return -EMSGSIZE; 1660 1661 return 0; 1662 } 1663 1664 static u32 rtnl_xdp_prog_skb(struct net_device *dev) 1665 { 1666 const struct bpf_prog *generic_xdp_prog; 1667 u32 res = 0; 1668 1669 rcu_read_lock(); 1670 generic_xdp_prog = rcu_dereference(dev->xdp_prog); 1671 if (generic_xdp_prog) 1672 res = generic_xdp_prog->aux->id; 1673 rcu_read_unlock(); 1674 1675 return res; 1676 } 1677 1678 static u32 rtnl_xdp_prog_drv(struct net_device *dev) 1679 { 1680 return dev_xdp_prog_id(dev, XDP_MODE_DRV); 1681 } 1682 1683 static u32 rtnl_xdp_prog_hw(struct net_device *dev) 1684 { 1685 return dev_xdp_prog_id(dev, XDP_MODE_HW); 1686 } 1687 1688 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev, 1689 u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr, 1690 u32 (*get_prog_id)(struct net_device *dev)) 1691 { 1692 u32 curr_id; 1693 int err; 1694 1695 curr_id = get_prog_id(dev); 1696 if (!curr_id) 1697 return 0; 1698 1699 *prog_id = curr_id; 1700 err = nla_put_u32(skb, attr, curr_id); 1701 if (err) 1702 return err; 1703 1704 if (*mode != XDP_ATTACHED_NONE) 1705 *mode = XDP_ATTACHED_MULTI; 1706 else 1707 *mode = tgt_mode; 1708 1709 return 0; 1710 } 1711 1712 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1713 { 1714 struct nlattr *xdp; 1715 u32 prog_id; 1716 int err; 1717 u8 mode; 1718 1719 xdp = nla_nest_start_noflag(skb, IFLA_XDP); 1720 if (!xdp) 1721 return -EMSGSIZE; 1722 1723 prog_id = 0; 1724 mode = XDP_ATTACHED_NONE; 1725 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1726 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1727 if (err) 1728 goto err_cancel; 1729 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1730 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1731 if (err) 1732 goto err_cancel; 1733 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1734 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1735 if (err) 1736 goto err_cancel; 1737 1738 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1739 if (err) 1740 goto err_cancel; 1741 1742 if (prog_id && mode != XDP_ATTACHED_MULTI) { 1743 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 1744 if (err) 1745 goto err_cancel; 1746 } 1747 1748 nla_nest_end(skb, xdp); 1749 return 0; 1750 1751 err_cancel: 1752 nla_nest_cancel(skb, xdp); 1753 return err; 1754 } 1755 1756 static u32 rtnl_get_event(unsigned long event) 1757 { 1758 u32 rtnl_event_type = IFLA_EVENT_NONE; 1759 1760 switch (event) { 1761 case NETDEV_REBOOT: 1762 rtnl_event_type = IFLA_EVENT_REBOOT; 1763 break; 1764 case NETDEV_FEAT_CHANGE: 1765 rtnl_event_type = IFLA_EVENT_FEATURES; 1766 break; 1767 case NETDEV_BONDING_FAILOVER: 1768 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 1769 break; 1770 case NETDEV_NOTIFY_PEERS: 1771 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 1772 break; 1773 case NETDEV_RESEND_IGMP: 1774 rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 1775 break; 1776 case NETDEV_CHANGEINFODATA: 1777 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 1778 break; 1779 default: 1780 break; 1781 } 1782 1783 return rtnl_event_type; 1784 } 1785 1786 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 1787 { 1788 const struct net_device *upper_dev; 1789 int ret = 0; 1790 1791 rcu_read_lock(); 1792 1793 upper_dev = netdev_master_upper_dev_get_rcu(dev); 1794 if (upper_dev) 1795 ret = nla_put_u32(skb, IFLA_MASTER, 1796 READ_ONCE(upper_dev->ifindex)); 1797 1798 rcu_read_unlock(); 1799 return ret; 1800 } 1801 1802 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev, 1803 bool force) 1804 { 1805 int iflink = dev_get_iflink(dev); 1806 1807 if (force || READ_ONCE(dev->ifindex) != iflink) 1808 return nla_put_u32(skb, IFLA_LINK, iflink); 1809 1810 return 0; 1811 } 1812 1813 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 1814 struct net_device *dev) 1815 { 1816 char buf[IFALIASZ]; 1817 int ret; 1818 1819 ret = dev_get_alias(dev, buf, sizeof(buf)); 1820 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 1821 } 1822 1823 static int rtnl_fill_link_netnsid(struct sk_buff *skb, 1824 const struct net_device *dev, 1825 struct net *src_net, gfp_t gfp) 1826 { 1827 bool put_iflink = false; 1828 1829 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1830 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1831 1832 if (!net_eq(dev_net(dev), link_net)) { 1833 int id = peernet2id_alloc(src_net, link_net, gfp); 1834 1835 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1836 return -EMSGSIZE; 1837 1838 put_iflink = true; 1839 } 1840 } 1841 1842 return nla_put_iflink(skb, dev, put_iflink); 1843 } 1844 1845 static int rtnl_fill_link_af(struct sk_buff *skb, 1846 const struct net_device *dev, 1847 u32 ext_filter_mask) 1848 { 1849 const struct rtnl_af_ops *af_ops; 1850 struct nlattr *af_spec; 1851 1852 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1853 if (!af_spec) 1854 return -EMSGSIZE; 1855 1856 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1857 struct nlattr *af; 1858 int err; 1859 1860 if (!af_ops->fill_link_af) 1861 continue; 1862 1863 af = nla_nest_start_noflag(skb, af_ops->family); 1864 if (!af) 1865 return -EMSGSIZE; 1866 1867 err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1868 /* 1869 * Caller may return ENODATA to indicate that there 1870 * was no data to be dumped. This is not an error, it 1871 * means we should trim the attribute header and 1872 * continue. 1873 */ 1874 if (err == -ENODATA) 1875 nla_nest_cancel(skb, af); 1876 else if (err < 0) 1877 return -EMSGSIZE; 1878 1879 nla_nest_end(skb, af); 1880 } 1881 1882 nla_nest_end(skb, af_spec); 1883 return 0; 1884 } 1885 1886 static int rtnl_fill_alt_ifnames(struct sk_buff *skb, 1887 const struct net_device *dev) 1888 { 1889 struct netdev_name_node *name_node; 1890 int count = 0; 1891 1892 list_for_each_entry_rcu(name_node, &dev->name_node->list, list) { 1893 if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name)) 1894 return -EMSGSIZE; 1895 count++; 1896 } 1897 return count; 1898 } 1899 1900 /* RCU protected. */ 1901 static int rtnl_fill_prop_list(struct sk_buff *skb, 1902 const struct net_device *dev) 1903 { 1904 struct nlattr *prop_list; 1905 int ret; 1906 1907 prop_list = nla_nest_start(skb, IFLA_PROP_LIST); 1908 if (!prop_list) 1909 return -EMSGSIZE; 1910 1911 ret = rtnl_fill_alt_ifnames(skb, dev); 1912 if (ret <= 0) 1913 goto nest_cancel; 1914 1915 nla_nest_end(skb, prop_list); 1916 return 0; 1917 1918 nest_cancel: 1919 nla_nest_cancel(skb, prop_list); 1920 return ret; 1921 } 1922 1923 static int rtnl_fill_proto_down(struct sk_buff *skb, 1924 const struct net_device *dev) 1925 { 1926 struct nlattr *pr; 1927 u32 preason; 1928 1929 if (nla_put_u8(skb, IFLA_PROTO_DOWN, READ_ONCE(dev->proto_down))) 1930 goto nla_put_failure; 1931 1932 preason = READ_ONCE(dev->proto_down_reason); 1933 if (!preason) 1934 return 0; 1935 1936 pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON); 1937 if (!pr) 1938 return -EMSGSIZE; 1939 1940 if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) { 1941 nla_nest_cancel(skb, pr); 1942 goto nla_put_failure; 1943 } 1944 1945 nla_nest_end(skb, pr); 1946 return 0; 1947 1948 nla_put_failure: 1949 return -EMSGSIZE; 1950 } 1951 1952 static int rtnl_fill_devlink_port(struct sk_buff *skb, 1953 const struct net_device *dev) 1954 { 1955 struct nlattr *devlink_port_nest; 1956 int ret; 1957 1958 devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT); 1959 if (!devlink_port_nest) 1960 return -EMSGSIZE; 1961 1962 if (dev->devlink_port) { 1963 ret = devlink_nl_port_handle_fill(skb, dev->devlink_port); 1964 if (ret < 0) 1965 goto nest_cancel; 1966 } 1967 1968 nla_nest_end(skb, devlink_port_nest); 1969 return 0; 1970 1971 nest_cancel: 1972 nla_nest_cancel(skb, devlink_port_nest); 1973 return ret; 1974 } 1975 1976 static int rtnl_fill_dpll_pin(struct sk_buff *skb, 1977 const struct net_device *dev) 1978 { 1979 struct nlattr *dpll_pin_nest; 1980 int ret; 1981 1982 dpll_pin_nest = nla_nest_start(skb, IFLA_DPLL_PIN); 1983 if (!dpll_pin_nest) 1984 return -EMSGSIZE; 1985 1986 ret = dpll_netdev_add_pin_handle(skb, dev); 1987 if (ret < 0) 1988 goto nest_cancel; 1989 1990 nla_nest_end(skb, dpll_pin_nest); 1991 return 0; 1992 1993 nest_cancel: 1994 nla_nest_cancel(skb, dpll_pin_nest); 1995 return ret; 1996 } 1997 1998 static int rtnl_fill_ifinfo(struct sk_buff *skb, 1999 struct net_device *dev, struct net *src_net, 2000 int type, u32 pid, u32 seq, u32 change, 2001 unsigned int flags, u32 ext_filter_mask, 2002 u32 event, int *new_nsid, int new_ifindex, 2003 int tgt_netnsid, gfp_t gfp) 2004 { 2005 char devname[IFNAMSIZ]; 2006 struct ifinfomsg *ifm; 2007 struct nlmsghdr *nlh; 2008 struct Qdisc *qdisc; 2009 2010 ASSERT_RTNL(); 2011 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 2012 if (nlh == NULL) 2013 return -EMSGSIZE; 2014 2015 ifm = nlmsg_data(nlh); 2016 ifm->ifi_family = AF_UNSPEC; 2017 ifm->__ifi_pad = 0; 2018 ifm->ifi_type = READ_ONCE(dev->type); 2019 ifm->ifi_index = READ_ONCE(dev->ifindex); 2020 ifm->ifi_flags = dev_get_flags(dev); 2021 ifm->ifi_change = change; 2022 2023 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 2024 goto nla_put_failure; 2025 2026 netdev_copy_name(dev, devname); 2027 if (nla_put_string(skb, IFLA_IFNAME, devname)) 2028 goto nla_put_failure; 2029 2030 if (nla_put_u32(skb, IFLA_TXQLEN, READ_ONCE(dev->tx_queue_len)) || 2031 nla_put_u8(skb, IFLA_OPERSTATE, 2032 netif_running(dev) ? READ_ONCE(dev->operstate) : 2033 IF_OPER_DOWN) || 2034 nla_put_u8(skb, IFLA_LINKMODE, READ_ONCE(dev->link_mode)) || 2035 nla_put_u32(skb, IFLA_MTU, READ_ONCE(dev->mtu)) || 2036 nla_put_u32(skb, IFLA_MIN_MTU, READ_ONCE(dev->min_mtu)) || 2037 nla_put_u32(skb, IFLA_MAX_MTU, READ_ONCE(dev->max_mtu)) || 2038 nla_put_u32(skb, IFLA_GROUP, READ_ONCE(dev->group)) || 2039 nla_put_u32(skb, IFLA_PROMISCUITY, READ_ONCE(dev->promiscuity)) || 2040 nla_put_u32(skb, IFLA_ALLMULTI, READ_ONCE(dev->allmulti)) || 2041 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, 2042 READ_ONCE(dev->num_tx_queues)) || 2043 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, 2044 READ_ONCE(dev->gso_max_segs)) || 2045 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, 2046 READ_ONCE(dev->gso_max_size)) || 2047 nla_put_u32(skb, IFLA_GRO_MAX_SIZE, 2048 READ_ONCE(dev->gro_max_size)) || 2049 nla_put_u32(skb, IFLA_GSO_IPV4_MAX_SIZE, 2050 READ_ONCE(dev->gso_ipv4_max_size)) || 2051 nla_put_u32(skb, IFLA_GRO_IPV4_MAX_SIZE, 2052 READ_ONCE(dev->gro_ipv4_max_size)) || 2053 nla_put_u32(skb, IFLA_TSO_MAX_SIZE, 2054 READ_ONCE(dev->tso_max_size)) || 2055 nla_put_u32(skb, IFLA_TSO_MAX_SEGS, 2056 READ_ONCE(dev->tso_max_segs)) || 2057 nla_put_uint(skb, IFLA_MAX_PACING_OFFLOAD_HORIZON, 2058 READ_ONCE(dev->max_pacing_offload_horizon)) || 2059 #ifdef CONFIG_RPS 2060 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, 2061 READ_ONCE(dev->num_rx_queues)) || 2062 #endif 2063 put_master_ifindex(skb, dev) || 2064 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 2065 nla_put_ifalias(skb, dev) || 2066 nla_put_u32(skb, IFLA_CARRIER_CHANGES, 2067 atomic_read(&dev->carrier_up_count) + 2068 atomic_read(&dev->carrier_down_count)) || 2069 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 2070 atomic_read(&dev->carrier_up_count)) || 2071 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 2072 atomic_read(&dev->carrier_down_count))) 2073 goto nla_put_failure; 2074 2075 if (rtnl_fill_proto_down(skb, dev)) 2076 goto nla_put_failure; 2077 2078 if (event != IFLA_EVENT_NONE) { 2079 if (nla_put_u32(skb, IFLA_EVENT, event)) 2080 goto nla_put_failure; 2081 } 2082 2083 if (dev->addr_len) { 2084 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 2085 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 2086 goto nla_put_failure; 2087 } 2088 2089 if (rtnl_phys_port_id_fill(skb, dev)) 2090 goto nla_put_failure; 2091 2092 if (rtnl_phys_port_name_fill(skb, dev)) 2093 goto nla_put_failure; 2094 2095 if (rtnl_phys_switch_id_fill(skb, dev)) 2096 goto nla_put_failure; 2097 2098 if (rtnl_fill_stats(skb, dev)) 2099 goto nla_put_failure; 2100 2101 if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 2102 goto nla_put_failure; 2103 2104 if (rtnl_port_fill(skb, dev, ext_filter_mask)) 2105 goto nla_put_failure; 2106 2107 if (rtnl_xdp_fill(skb, dev)) 2108 goto nla_put_failure; 2109 2110 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 2111 if (rtnl_link_fill(skb, dev) < 0) 2112 goto nla_put_failure; 2113 } 2114 2115 if (new_nsid && 2116 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 2117 goto nla_put_failure; 2118 if (new_ifindex && 2119 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 2120 goto nla_put_failure; 2121 2122 if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) && 2123 nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr)) 2124 goto nla_put_failure; 2125 2126 rcu_read_lock(); 2127 if (rtnl_fill_link_netnsid(skb, dev, src_net, GFP_ATOMIC)) 2128 goto nla_put_failure_rcu; 2129 qdisc = rcu_dereference(dev->qdisc); 2130 if (qdisc && nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) 2131 goto nla_put_failure_rcu; 2132 if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 2133 goto nla_put_failure_rcu; 2134 if (rtnl_fill_link_ifmap(skb, dev)) 2135 goto nla_put_failure_rcu; 2136 if (rtnl_fill_prop_list(skb, dev)) 2137 goto nla_put_failure_rcu; 2138 rcu_read_unlock(); 2139 2140 if (dev->dev.parent && 2141 nla_put_string(skb, IFLA_PARENT_DEV_NAME, 2142 dev_name(dev->dev.parent))) 2143 goto nla_put_failure; 2144 2145 if (dev->dev.parent && dev->dev.parent->bus && 2146 nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME, 2147 dev->dev.parent->bus->name)) 2148 goto nla_put_failure; 2149 2150 if (rtnl_fill_devlink_port(skb, dev)) 2151 goto nla_put_failure; 2152 2153 if (rtnl_fill_dpll_pin(skb, dev)) 2154 goto nla_put_failure; 2155 2156 nlmsg_end(skb, nlh); 2157 return 0; 2158 2159 nla_put_failure_rcu: 2160 rcu_read_unlock(); 2161 nla_put_failure: 2162 nlmsg_cancel(skb, nlh); 2163 return -EMSGSIZE; 2164 } 2165 2166 static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 2167 [IFLA_UNSPEC] = { .strict_start_type = IFLA_DPLL_PIN }, 2168 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 2169 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 2170 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 2171 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 2172 [IFLA_MTU] = { .type = NLA_U32 }, 2173 [IFLA_LINK] = { .type = NLA_U32 }, 2174 [IFLA_MASTER] = { .type = NLA_U32 }, 2175 [IFLA_CARRIER] = { .type = NLA_U8 }, 2176 [IFLA_TXQLEN] = { .type = NLA_U32 }, 2177 [IFLA_WEIGHT] = { .type = NLA_U32 }, 2178 [IFLA_OPERSTATE] = { .type = NLA_U8 }, 2179 [IFLA_LINKMODE] = { .type = NLA_U8 }, 2180 [IFLA_LINKINFO] = { .type = NLA_NESTED }, 2181 [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 2182 [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 2183 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 2184 * allow 0-length string (needed to remove an alias). 2185 */ 2186 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 2187 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 2188 [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 2189 [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 2190 [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 2191 [IFLA_EXT_MASK] = { .type = NLA_U32 }, 2192 [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 2193 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 2194 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 2195 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 2196 [IFLA_GSO_MAX_SIZE] = NLA_POLICY_MIN(NLA_U32, MAX_TCP_HEADER + 1), 2197 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 2198 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 2199 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 2200 [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 2201 [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 2202 [IFLA_XDP] = { .type = NLA_NESTED }, 2203 [IFLA_EVENT] = { .type = NLA_U32 }, 2204 [IFLA_GROUP] = { .type = NLA_U32 }, 2205 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 2206 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 2207 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 2208 [IFLA_MIN_MTU] = { .type = NLA_U32 }, 2209 [IFLA_MAX_MTU] = { .type = NLA_U32 }, 2210 [IFLA_PROP_LIST] = { .type = NLA_NESTED }, 2211 [IFLA_ALT_IFNAME] = { .type = NLA_STRING, 2212 .len = ALTIFNAMSIZ - 1 }, 2213 [IFLA_PERM_ADDRESS] = { .type = NLA_REJECT }, 2214 [IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED }, 2215 [IFLA_NEW_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 2216 [IFLA_PARENT_DEV_NAME] = { .type = NLA_NUL_STRING }, 2217 [IFLA_GRO_MAX_SIZE] = { .type = NLA_U32 }, 2218 [IFLA_TSO_MAX_SIZE] = { .type = NLA_REJECT }, 2219 [IFLA_TSO_MAX_SEGS] = { .type = NLA_REJECT }, 2220 [IFLA_ALLMULTI] = { .type = NLA_REJECT }, 2221 [IFLA_GSO_IPV4_MAX_SIZE] = NLA_POLICY_MIN(NLA_U32, MAX_TCP_HEADER + 1), 2222 [IFLA_GRO_IPV4_MAX_SIZE] = { .type = NLA_U32 }, 2223 }; 2224 2225 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 2226 [IFLA_INFO_KIND] = { .type = NLA_STRING }, 2227 [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 2228 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 2229 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 2230 }; 2231 2232 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 2233 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 2234 [IFLA_VF_BROADCAST] = { .type = NLA_REJECT }, 2235 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 2236 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 2237 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 2238 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 2239 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 2240 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 2241 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 2242 [IFLA_VF_STATS] = { .type = NLA_NESTED }, 2243 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 2244 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 2245 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 2246 }; 2247 2248 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 2249 [IFLA_PORT_VF] = { .type = NLA_U32 }, 2250 [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 2251 .len = PORT_PROFILE_MAX }, 2252 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 2253 .len = PORT_UUID_MAX }, 2254 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 2255 .len = PORT_UUID_MAX }, 2256 [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 2257 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 2258 2259 /* Unused, but we need to keep it here since user space could 2260 * fill it. It's also broken with regard to NLA_BINARY use in 2261 * combination with structs. 2262 */ 2263 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 2264 .len = sizeof(struct ifla_port_vsi) }, 2265 }; 2266 2267 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 2268 [IFLA_XDP_UNSPEC] = { .strict_start_type = IFLA_XDP_EXPECTED_FD }, 2269 [IFLA_XDP_FD] = { .type = NLA_S32 }, 2270 [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, 2271 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 2272 [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 2273 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 2274 }; 2275 2276 static struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla, 2277 int *ops_srcu_index) 2278 { 2279 struct nlattr *linfo[IFLA_INFO_MAX + 1]; 2280 struct rtnl_link_ops *ops = NULL; 2281 2282 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 2283 return NULL; 2284 2285 if (linfo[IFLA_INFO_KIND]) { 2286 char kind[MODULE_NAME_LEN]; 2287 2288 nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 2289 ops = rtnl_link_ops_get(kind, ops_srcu_index); 2290 } 2291 2292 return ops; 2293 } 2294 2295 static bool link_master_filtered(struct net_device *dev, int master_idx) 2296 { 2297 struct net_device *master; 2298 2299 if (!master_idx) 2300 return false; 2301 2302 master = netdev_master_upper_dev_get(dev); 2303 2304 /* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need 2305 * another invalid value for ifindex to denote "no master". 2306 */ 2307 if (master_idx == -1) 2308 return !!master; 2309 2310 if (!master || master->ifindex != master_idx) 2311 return true; 2312 2313 return false; 2314 } 2315 2316 static bool link_kind_filtered(const struct net_device *dev, 2317 const struct rtnl_link_ops *kind_ops) 2318 { 2319 if (kind_ops && dev->rtnl_link_ops != kind_ops) 2320 return true; 2321 2322 return false; 2323 } 2324 2325 static bool link_dump_filtered(struct net_device *dev, 2326 int master_idx, 2327 const struct rtnl_link_ops *kind_ops) 2328 { 2329 if (link_master_filtered(dev, master_idx) || 2330 link_kind_filtered(dev, kind_ops)) 2331 return true; 2332 2333 return false; 2334 } 2335 2336 /** 2337 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 2338 * @sk: netlink socket 2339 * @netnsid: network namespace identifier 2340 * 2341 * Returns the network namespace identified by netnsid on success or an error 2342 * pointer on failure. 2343 */ 2344 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 2345 { 2346 struct net *net; 2347 2348 net = get_net_ns_by_id(sock_net(sk), netnsid); 2349 if (!net) 2350 return ERR_PTR(-EINVAL); 2351 2352 /* For now, the caller is required to have CAP_NET_ADMIN in 2353 * the user namespace owning the target net ns. 2354 */ 2355 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 2356 put_net(net); 2357 return ERR_PTR(-EACCES); 2358 } 2359 return net; 2360 } 2361 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 2362 2363 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 2364 bool strict_check, struct nlattr **tb, 2365 struct netlink_ext_ack *extack) 2366 { 2367 int hdrlen; 2368 2369 if (strict_check) { 2370 struct ifinfomsg *ifm; 2371 2372 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 2373 NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 2374 return -EINVAL; 2375 } 2376 2377 ifm = nlmsg_data(nlh); 2378 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 2379 ifm->ifi_change) { 2380 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 2381 return -EINVAL; 2382 } 2383 if (ifm->ifi_index) { 2384 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 2385 return -EINVAL; 2386 } 2387 2388 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 2389 IFLA_MAX, ifla_policy, 2390 extack); 2391 } 2392 2393 /* A hack to preserve kernel<->userspace interface. 2394 * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 2395 * However, before Linux v3.9 the code here assumed rtgenmsg and that's 2396 * what iproute2 < v3.9.0 used. 2397 * We can detect the old iproute2. Even including the IFLA_EXT_MASK 2398 * attribute, its netlink message is shorter than struct ifinfomsg. 2399 */ 2400 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2401 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2402 2403 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 2404 extack); 2405 } 2406 2407 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 2408 { 2409 struct netlink_ext_ack *extack = cb->extack; 2410 struct rtnl_link_ops *kind_ops = NULL; 2411 const struct nlmsghdr *nlh = cb->nlh; 2412 struct net *net = sock_net(skb->sk); 2413 unsigned int flags = NLM_F_MULTI; 2414 struct nlattr *tb[IFLA_MAX+1]; 2415 struct { 2416 unsigned long ifindex; 2417 } *ctx = (void *)cb->ctx; 2418 struct net *tgt_net = net; 2419 u32 ext_filter_mask = 0; 2420 struct net_device *dev; 2421 int ops_srcu_index; 2422 int master_idx = 0; 2423 int netnsid = -1; 2424 int err, i; 2425 2426 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 2427 if (err < 0) { 2428 if (cb->strict_check) 2429 return err; 2430 2431 goto walk_entries; 2432 } 2433 2434 for (i = 0; i <= IFLA_MAX; ++i) { 2435 if (!tb[i]) 2436 continue; 2437 2438 /* new attributes should only be added with strict checking */ 2439 switch (i) { 2440 case IFLA_TARGET_NETNSID: 2441 netnsid = nla_get_s32(tb[i]); 2442 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 2443 if (IS_ERR(tgt_net)) { 2444 NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 2445 return PTR_ERR(tgt_net); 2446 } 2447 break; 2448 case IFLA_EXT_MASK: 2449 ext_filter_mask = nla_get_u32(tb[i]); 2450 break; 2451 case IFLA_MASTER: 2452 master_idx = nla_get_u32(tb[i]); 2453 break; 2454 case IFLA_LINKINFO: 2455 kind_ops = linkinfo_to_kind_ops(tb[i], &ops_srcu_index); 2456 break; 2457 default: 2458 if (cb->strict_check) { 2459 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 2460 return -EINVAL; 2461 } 2462 } 2463 } 2464 2465 if (master_idx || kind_ops) 2466 flags |= NLM_F_DUMP_FILTERED; 2467 2468 walk_entries: 2469 err = 0; 2470 for_each_netdev_dump(tgt_net, dev, ctx->ifindex) { 2471 if (link_dump_filtered(dev, master_idx, kind_ops)) 2472 continue; 2473 err = rtnl_fill_ifinfo(skb, dev, net, RTM_NEWLINK, 2474 NETLINK_CB(cb->skb).portid, 2475 nlh->nlmsg_seq, 0, flags, 2476 ext_filter_mask, 0, NULL, 0, 2477 netnsid, GFP_KERNEL); 2478 if (err < 0) 2479 break; 2480 } 2481 2482 if (kind_ops) 2483 rtnl_link_ops_put(kind_ops, ops_srcu_index); 2484 2485 cb->seq = tgt_net->dev_base_seq; 2486 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 2487 if (netnsid >= 0) 2488 put_net(tgt_net); 2489 2490 return err; 2491 } 2492 2493 int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer, 2494 struct netlink_ext_ack *exterr) 2495 { 2496 const struct ifinfomsg *ifmp; 2497 const struct nlattr *attrs; 2498 size_t len; 2499 2500 ifmp = nla_data(nla_peer); 2501 attrs = nla_data(nla_peer) + sizeof(struct ifinfomsg); 2502 len = nla_len(nla_peer) - sizeof(struct ifinfomsg); 2503 2504 if (ifmp->ifi_index < 0) { 2505 NL_SET_ERR_MSG_ATTR(exterr, nla_peer, 2506 "ifindex can't be negative"); 2507 return -EINVAL; 2508 } 2509 2510 return nla_parse_deprecated(tb, IFLA_MAX, attrs, len, ifla_policy, 2511 exterr); 2512 } 2513 EXPORT_SYMBOL(rtnl_nla_parse_ifinfomsg); 2514 2515 static struct net *rtnl_link_get_net_ifla(struct nlattr *tb[]) 2516 { 2517 struct net *net = NULL; 2518 2519 /* Examine the link attributes and figure out which 2520 * network namespace we are talking about. 2521 */ 2522 if (tb[IFLA_NET_NS_PID]) 2523 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2524 else if (tb[IFLA_NET_NS_FD]) 2525 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 2526 2527 return net; 2528 } 2529 2530 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 2531 { 2532 struct net *net = rtnl_link_get_net_ifla(tb); 2533 2534 if (!net) 2535 net = get_net(src_net); 2536 2537 return net; 2538 } 2539 EXPORT_SYMBOL(rtnl_link_get_net); 2540 2541 /* Figure out which network namespace we are talking about by 2542 * examining the link attributes in the following order: 2543 * 2544 * 1. IFLA_NET_NS_PID 2545 * 2. IFLA_NET_NS_FD 2546 * 3. IFLA_TARGET_NETNSID 2547 */ 2548 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 2549 struct nlattr *tb[]) 2550 { 2551 struct net *net; 2552 2553 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 2554 return rtnl_link_get_net(src_net, tb); 2555 2556 if (!tb[IFLA_TARGET_NETNSID]) 2557 return get_net(src_net); 2558 2559 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 2560 if (!net) 2561 return ERR_PTR(-EINVAL); 2562 2563 return net; 2564 } 2565 2566 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 2567 struct net *src_net, 2568 struct nlattr *tb[], int cap) 2569 { 2570 struct net *net; 2571 2572 net = rtnl_link_get_net_by_nlattr(src_net, tb); 2573 if (IS_ERR(net)) 2574 return net; 2575 2576 if (!netlink_ns_capable(skb, net->user_ns, cap)) { 2577 put_net(net); 2578 return ERR_PTR(-EPERM); 2579 } 2580 2581 return net; 2582 } 2583 2584 /* Verify that rtnetlink requests do not pass additional properties 2585 * potentially referring to different network namespaces. 2586 */ 2587 static int rtnl_ensure_unique_netns(struct nlattr *tb[], 2588 struct netlink_ext_ack *extack, 2589 bool netns_id_only) 2590 { 2591 2592 if (netns_id_only) { 2593 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 2594 return 0; 2595 2596 NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 2597 return -EOPNOTSUPP; 2598 } 2599 2600 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 2601 goto invalid_attr; 2602 2603 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 2604 goto invalid_attr; 2605 2606 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 2607 goto invalid_attr; 2608 2609 return 0; 2610 2611 invalid_attr: 2612 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 2613 return -EINVAL; 2614 } 2615 2616 static int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate, 2617 int max_tx_rate) 2618 { 2619 const struct net_device_ops *ops = dev->netdev_ops; 2620 2621 if (!ops->ndo_set_vf_rate) 2622 return -EOPNOTSUPP; 2623 if (max_tx_rate && max_tx_rate < min_tx_rate) 2624 return -EINVAL; 2625 2626 return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate); 2627 } 2628 2629 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[], 2630 struct netlink_ext_ack *extack) 2631 { 2632 if (tb[IFLA_ADDRESS] && 2633 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 2634 return -EINVAL; 2635 2636 if (tb[IFLA_BROADCAST] && 2637 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 2638 return -EINVAL; 2639 2640 if (tb[IFLA_GSO_MAX_SIZE] && 2641 nla_get_u32(tb[IFLA_GSO_MAX_SIZE]) > dev->tso_max_size) { 2642 NL_SET_ERR_MSG(extack, "too big gso_max_size"); 2643 return -EINVAL; 2644 } 2645 2646 if (tb[IFLA_GSO_MAX_SEGS] && 2647 (nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > GSO_MAX_SEGS || 2648 nla_get_u32(tb[IFLA_GSO_MAX_SEGS]) > dev->tso_max_segs)) { 2649 NL_SET_ERR_MSG(extack, "too big gso_max_segs"); 2650 return -EINVAL; 2651 } 2652 2653 if (tb[IFLA_GRO_MAX_SIZE] && 2654 nla_get_u32(tb[IFLA_GRO_MAX_SIZE]) > GRO_MAX_SIZE) { 2655 NL_SET_ERR_MSG(extack, "too big gro_max_size"); 2656 return -EINVAL; 2657 } 2658 2659 if (tb[IFLA_GSO_IPV4_MAX_SIZE] && 2660 nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]) > dev->tso_max_size) { 2661 NL_SET_ERR_MSG(extack, "too big gso_ipv4_max_size"); 2662 return -EINVAL; 2663 } 2664 2665 if (tb[IFLA_GRO_IPV4_MAX_SIZE] && 2666 nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]) > GRO_MAX_SIZE) { 2667 NL_SET_ERR_MSG(extack, "too big gro_ipv4_max_size"); 2668 return -EINVAL; 2669 } 2670 2671 if (tb[IFLA_AF_SPEC]) { 2672 struct nlattr *af; 2673 int rem, err; 2674 2675 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2676 struct rtnl_af_ops *af_ops; 2677 int af_ops_srcu_index; 2678 2679 af_ops = rtnl_af_lookup(nla_type(af), &af_ops_srcu_index); 2680 if (!af_ops) 2681 return -EAFNOSUPPORT; 2682 2683 if (!af_ops->set_link_af) 2684 err = -EOPNOTSUPP; 2685 else if (af_ops->validate_link_af) 2686 err = af_ops->validate_link_af(dev, af, extack); 2687 else 2688 err = 0; 2689 2690 rtnl_af_put(af_ops, af_ops_srcu_index); 2691 2692 if (err < 0) 2693 return err; 2694 } 2695 } 2696 2697 return 0; 2698 } 2699 2700 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2701 int guid_type) 2702 { 2703 const struct net_device_ops *ops = dev->netdev_ops; 2704 2705 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2706 } 2707 2708 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2709 { 2710 if (dev->type != ARPHRD_INFINIBAND) 2711 return -EOPNOTSUPP; 2712 2713 return handle_infiniband_guid(dev, ivt, guid_type); 2714 } 2715 2716 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2717 { 2718 const struct net_device_ops *ops = dev->netdev_ops; 2719 int err = -EINVAL; 2720 2721 if (tb[IFLA_VF_MAC]) { 2722 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 2723 2724 if (ivm->vf >= INT_MAX) 2725 return -EINVAL; 2726 err = -EOPNOTSUPP; 2727 if (ops->ndo_set_vf_mac) 2728 err = ops->ndo_set_vf_mac(dev, ivm->vf, 2729 ivm->mac); 2730 if (err < 0) 2731 return err; 2732 } 2733 2734 if (tb[IFLA_VF_VLAN]) { 2735 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 2736 2737 if (ivv->vf >= INT_MAX) 2738 return -EINVAL; 2739 err = -EOPNOTSUPP; 2740 if (ops->ndo_set_vf_vlan) 2741 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 2742 ivv->qos, 2743 htons(ETH_P_8021Q)); 2744 if (err < 0) 2745 return err; 2746 } 2747 2748 if (tb[IFLA_VF_VLAN_LIST]) { 2749 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 2750 struct nlattr *attr; 2751 int rem, len = 0; 2752 2753 err = -EOPNOTSUPP; 2754 if (!ops->ndo_set_vf_vlan) 2755 return err; 2756 2757 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 2758 if (nla_type(attr) != IFLA_VF_VLAN_INFO || 2759 nla_len(attr) < sizeof(struct ifla_vf_vlan_info)) { 2760 return -EINVAL; 2761 } 2762 if (len >= MAX_VLAN_LIST_LEN) 2763 return -EOPNOTSUPP; 2764 ivvl[len] = nla_data(attr); 2765 2766 len++; 2767 } 2768 if (len == 0) 2769 return -EINVAL; 2770 2771 if (ivvl[0]->vf >= INT_MAX) 2772 return -EINVAL; 2773 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 2774 ivvl[0]->qos, ivvl[0]->vlan_proto); 2775 if (err < 0) 2776 return err; 2777 } 2778 2779 if (tb[IFLA_VF_TX_RATE]) { 2780 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2781 struct ifla_vf_info ivf; 2782 2783 if (ivt->vf >= INT_MAX) 2784 return -EINVAL; 2785 err = -EOPNOTSUPP; 2786 if (ops->ndo_get_vf_config) 2787 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 2788 if (err < 0) 2789 return err; 2790 2791 err = rtnl_set_vf_rate(dev, ivt->vf, 2792 ivf.min_tx_rate, ivt->rate); 2793 if (err < 0) 2794 return err; 2795 } 2796 2797 if (tb[IFLA_VF_RATE]) { 2798 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 2799 2800 if (ivt->vf >= INT_MAX) 2801 return -EINVAL; 2802 2803 err = rtnl_set_vf_rate(dev, ivt->vf, 2804 ivt->min_tx_rate, ivt->max_tx_rate); 2805 if (err < 0) 2806 return err; 2807 } 2808 2809 if (tb[IFLA_VF_SPOOFCHK]) { 2810 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 2811 2812 if (ivs->vf >= INT_MAX) 2813 return -EINVAL; 2814 err = -EOPNOTSUPP; 2815 if (ops->ndo_set_vf_spoofchk) 2816 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 2817 ivs->setting); 2818 if (err < 0) 2819 return err; 2820 } 2821 2822 if (tb[IFLA_VF_LINK_STATE]) { 2823 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 2824 2825 if (ivl->vf >= INT_MAX) 2826 return -EINVAL; 2827 err = -EOPNOTSUPP; 2828 if (ops->ndo_set_vf_link_state) 2829 err = ops->ndo_set_vf_link_state(dev, ivl->vf, 2830 ivl->link_state); 2831 if (err < 0) 2832 return err; 2833 } 2834 2835 if (tb[IFLA_VF_RSS_QUERY_EN]) { 2836 struct ifla_vf_rss_query_en *ivrssq_en; 2837 2838 err = -EOPNOTSUPP; 2839 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2840 if (ivrssq_en->vf >= INT_MAX) 2841 return -EINVAL; 2842 if (ops->ndo_set_vf_rss_query_en) 2843 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 2844 ivrssq_en->setting); 2845 if (err < 0) 2846 return err; 2847 } 2848 2849 if (tb[IFLA_VF_TRUST]) { 2850 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2851 2852 if (ivt->vf >= INT_MAX) 2853 return -EINVAL; 2854 err = -EOPNOTSUPP; 2855 if (ops->ndo_set_vf_trust) 2856 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2857 if (err < 0) 2858 return err; 2859 } 2860 2861 if (tb[IFLA_VF_IB_NODE_GUID]) { 2862 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2863 2864 if (ivt->vf >= INT_MAX) 2865 return -EINVAL; 2866 if (!ops->ndo_set_vf_guid) 2867 return -EOPNOTSUPP; 2868 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2869 } 2870 2871 if (tb[IFLA_VF_IB_PORT_GUID]) { 2872 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2873 2874 if (ivt->vf >= INT_MAX) 2875 return -EINVAL; 2876 if (!ops->ndo_set_vf_guid) 2877 return -EOPNOTSUPP; 2878 2879 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2880 } 2881 2882 return err; 2883 } 2884 2885 static int do_set_master(struct net_device *dev, int ifindex, 2886 struct netlink_ext_ack *extack) 2887 { 2888 struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2889 const struct net_device_ops *ops; 2890 int err; 2891 2892 if (upper_dev) { 2893 if (upper_dev->ifindex == ifindex) 2894 return 0; 2895 ops = upper_dev->netdev_ops; 2896 if (ops->ndo_del_slave) { 2897 err = ops->ndo_del_slave(upper_dev, dev); 2898 if (err) 2899 return err; 2900 } else { 2901 return -EOPNOTSUPP; 2902 } 2903 } 2904 2905 if (ifindex) { 2906 upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2907 if (!upper_dev) 2908 return -EINVAL; 2909 ops = upper_dev->netdev_ops; 2910 if (ops->ndo_add_slave) { 2911 err = ops->ndo_add_slave(upper_dev, dev, extack); 2912 if (err) 2913 return err; 2914 } else { 2915 return -EOPNOTSUPP; 2916 } 2917 } 2918 return 0; 2919 } 2920 2921 static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = { 2922 [IFLA_PROTO_DOWN_REASON_MASK] = { .type = NLA_U32 }, 2923 [IFLA_PROTO_DOWN_REASON_VALUE] = { .type = NLA_U32 }, 2924 }; 2925 2926 static int do_set_proto_down(struct net_device *dev, 2927 struct nlattr *nl_proto_down, 2928 struct nlattr *nl_proto_down_reason, 2929 struct netlink_ext_ack *extack) 2930 { 2931 struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1]; 2932 unsigned long mask = 0; 2933 u32 value; 2934 bool proto_down; 2935 int err; 2936 2937 if (!dev->change_proto_down) { 2938 NL_SET_ERR_MSG(extack, "Protodown not supported by device"); 2939 return -EOPNOTSUPP; 2940 } 2941 2942 if (nl_proto_down_reason) { 2943 err = nla_parse_nested_deprecated(pdreason, 2944 IFLA_PROTO_DOWN_REASON_MAX, 2945 nl_proto_down_reason, 2946 ifla_proto_down_reason_policy, 2947 NULL); 2948 if (err < 0) 2949 return err; 2950 2951 if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) { 2952 NL_SET_ERR_MSG(extack, "Invalid protodown reason value"); 2953 return -EINVAL; 2954 } 2955 2956 value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]); 2957 2958 if (pdreason[IFLA_PROTO_DOWN_REASON_MASK]) 2959 mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]); 2960 2961 dev_change_proto_down_reason(dev, mask, value); 2962 } 2963 2964 if (nl_proto_down) { 2965 proto_down = nla_get_u8(nl_proto_down); 2966 2967 /* Don't turn off protodown if there are active reasons */ 2968 if (!proto_down && dev->proto_down_reason) { 2969 NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons"); 2970 return -EBUSY; 2971 } 2972 err = dev_change_proto_down(dev, 2973 proto_down); 2974 if (err) 2975 return err; 2976 } 2977 2978 return 0; 2979 } 2980 2981 #define DO_SETLINK_MODIFIED 0x01 2982 /* notify flag means notify + modified. */ 2983 #define DO_SETLINK_NOTIFY 0x03 2984 static int do_setlink(const struct sk_buff *skb, struct net_device *dev, 2985 struct net *tgt_net, struct ifinfomsg *ifm, 2986 struct netlink_ext_ack *extack, 2987 struct nlattr **tb, int status) 2988 { 2989 const struct net_device_ops *ops = dev->netdev_ops; 2990 char ifname[IFNAMSIZ]; 2991 int err; 2992 2993 err = validate_linkmsg(dev, tb, extack); 2994 if (err < 0) 2995 goto errout; 2996 2997 if (tb[IFLA_IFNAME]) 2998 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2999 else 3000 ifname[0] = '\0'; 3001 3002 if (!net_eq(tgt_net, dev_net(dev))) { 3003 const char *pat = ifname[0] ? ifname : NULL; 3004 int new_ifindex; 3005 3006 new_ifindex = nla_get_s32_default(tb[IFLA_NEW_IFINDEX], 0); 3007 3008 err = __dev_change_net_namespace(dev, tgt_net, pat, new_ifindex); 3009 if (err) 3010 goto errout; 3011 3012 status |= DO_SETLINK_MODIFIED; 3013 } 3014 3015 if (tb[IFLA_MAP]) { 3016 struct rtnl_link_ifmap *u_map; 3017 struct ifmap k_map; 3018 3019 if (!ops->ndo_set_config) { 3020 err = -EOPNOTSUPP; 3021 goto errout; 3022 } 3023 3024 if (!netif_device_present(dev)) { 3025 err = -ENODEV; 3026 goto errout; 3027 } 3028 3029 u_map = nla_data(tb[IFLA_MAP]); 3030 k_map.mem_start = (unsigned long) u_map->mem_start; 3031 k_map.mem_end = (unsigned long) u_map->mem_end; 3032 k_map.base_addr = (unsigned short) u_map->base_addr; 3033 k_map.irq = (unsigned char) u_map->irq; 3034 k_map.dma = (unsigned char) u_map->dma; 3035 k_map.port = (unsigned char) u_map->port; 3036 3037 err = ops->ndo_set_config(dev, &k_map); 3038 if (err < 0) 3039 goto errout; 3040 3041 status |= DO_SETLINK_NOTIFY; 3042 } 3043 3044 if (tb[IFLA_ADDRESS]) { 3045 struct sockaddr *sa; 3046 int len; 3047 3048 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 3049 sizeof(*sa)); 3050 sa = kmalloc(len, GFP_KERNEL); 3051 if (!sa) { 3052 err = -ENOMEM; 3053 goto errout; 3054 } 3055 sa->sa_family = dev->type; 3056 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 3057 dev->addr_len); 3058 err = dev_set_mac_address_user(dev, sa, extack); 3059 kfree(sa); 3060 if (err) 3061 goto errout; 3062 status |= DO_SETLINK_MODIFIED; 3063 } 3064 3065 if (tb[IFLA_MTU]) { 3066 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 3067 if (err < 0) 3068 goto errout; 3069 status |= DO_SETLINK_MODIFIED; 3070 } 3071 3072 if (tb[IFLA_GROUP]) { 3073 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 3074 status |= DO_SETLINK_NOTIFY; 3075 } 3076 3077 /* 3078 * Interface selected by interface index but interface 3079 * name provided implies that a name change has been 3080 * requested. 3081 */ 3082 if (ifm->ifi_index > 0 && ifname[0]) { 3083 err = dev_change_name(dev, ifname); 3084 if (err < 0) 3085 goto errout; 3086 status |= DO_SETLINK_MODIFIED; 3087 } 3088 3089 if (tb[IFLA_IFALIAS]) { 3090 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 3091 nla_len(tb[IFLA_IFALIAS])); 3092 if (err < 0) 3093 goto errout; 3094 status |= DO_SETLINK_NOTIFY; 3095 } 3096 3097 if (tb[IFLA_BROADCAST]) { 3098 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 3099 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 3100 } 3101 3102 if (ifm->ifi_flags || ifm->ifi_change) { 3103 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 3104 extack); 3105 if (err < 0) 3106 goto errout; 3107 } 3108 3109 if (tb[IFLA_MASTER]) { 3110 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 3111 if (err) 3112 goto errout; 3113 status |= DO_SETLINK_MODIFIED; 3114 } 3115 3116 if (tb[IFLA_CARRIER]) { 3117 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 3118 if (err) 3119 goto errout; 3120 status |= DO_SETLINK_MODIFIED; 3121 } 3122 3123 if (tb[IFLA_TXQLEN]) { 3124 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 3125 3126 err = dev_change_tx_queue_len(dev, value); 3127 if (err) 3128 goto errout; 3129 status |= DO_SETLINK_MODIFIED; 3130 } 3131 3132 if (tb[IFLA_GSO_MAX_SIZE]) { 3133 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 3134 3135 if (dev->gso_max_size ^ max_size) { 3136 netif_set_gso_max_size(dev, max_size); 3137 status |= DO_SETLINK_MODIFIED; 3138 } 3139 } 3140 3141 if (tb[IFLA_GSO_MAX_SEGS]) { 3142 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 3143 3144 if (dev->gso_max_segs ^ max_segs) { 3145 netif_set_gso_max_segs(dev, max_segs); 3146 status |= DO_SETLINK_MODIFIED; 3147 } 3148 } 3149 3150 if (tb[IFLA_GRO_MAX_SIZE]) { 3151 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]); 3152 3153 if (dev->gro_max_size ^ gro_max_size) { 3154 netif_set_gro_max_size(dev, gro_max_size); 3155 status |= DO_SETLINK_MODIFIED; 3156 } 3157 } 3158 3159 if (tb[IFLA_GSO_IPV4_MAX_SIZE]) { 3160 u32 max_size = nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE]); 3161 3162 if (dev->gso_ipv4_max_size ^ max_size) { 3163 netif_set_gso_ipv4_max_size(dev, max_size); 3164 status |= DO_SETLINK_MODIFIED; 3165 } 3166 } 3167 3168 if (tb[IFLA_GRO_IPV4_MAX_SIZE]) { 3169 u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE]); 3170 3171 if (dev->gro_ipv4_max_size ^ gro_max_size) { 3172 netif_set_gro_ipv4_max_size(dev, gro_max_size); 3173 status |= DO_SETLINK_MODIFIED; 3174 } 3175 } 3176 3177 if (tb[IFLA_OPERSTATE]) 3178 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3179 3180 if (tb[IFLA_LINKMODE]) { 3181 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 3182 3183 if (dev->link_mode ^ value) 3184 status |= DO_SETLINK_NOTIFY; 3185 WRITE_ONCE(dev->link_mode, value); 3186 } 3187 3188 if (tb[IFLA_VFINFO_LIST]) { 3189 struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 3190 struct nlattr *attr; 3191 int rem; 3192 3193 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 3194 if (nla_type(attr) != IFLA_VF_INFO || 3195 nla_len(attr) < NLA_HDRLEN) { 3196 err = -EINVAL; 3197 goto errout; 3198 } 3199 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 3200 attr, 3201 ifla_vf_policy, 3202 NULL); 3203 if (err < 0) 3204 goto errout; 3205 err = do_setvfinfo(dev, vfinfo); 3206 if (err < 0) 3207 goto errout; 3208 status |= DO_SETLINK_NOTIFY; 3209 } 3210 } 3211 err = 0; 3212 3213 if (tb[IFLA_VF_PORTS]) { 3214 struct nlattr *port[IFLA_PORT_MAX+1]; 3215 struct nlattr *attr; 3216 int vf; 3217 int rem; 3218 3219 err = -EOPNOTSUPP; 3220 if (!ops->ndo_set_vf_port) 3221 goto errout; 3222 3223 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 3224 if (nla_type(attr) != IFLA_VF_PORT || 3225 nla_len(attr) < NLA_HDRLEN) { 3226 err = -EINVAL; 3227 goto errout; 3228 } 3229 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 3230 attr, 3231 ifla_port_policy, 3232 NULL); 3233 if (err < 0) 3234 goto errout; 3235 if (!port[IFLA_PORT_VF]) { 3236 err = -EOPNOTSUPP; 3237 goto errout; 3238 } 3239 vf = nla_get_u32(port[IFLA_PORT_VF]); 3240 err = ops->ndo_set_vf_port(dev, vf, port); 3241 if (err < 0) 3242 goto errout; 3243 status |= DO_SETLINK_NOTIFY; 3244 } 3245 } 3246 err = 0; 3247 3248 if (tb[IFLA_PORT_SELF]) { 3249 struct nlattr *port[IFLA_PORT_MAX+1]; 3250 3251 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 3252 tb[IFLA_PORT_SELF], 3253 ifla_port_policy, NULL); 3254 if (err < 0) 3255 goto errout; 3256 3257 err = -EOPNOTSUPP; 3258 if (ops->ndo_set_vf_port) 3259 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 3260 if (err < 0) 3261 goto errout; 3262 status |= DO_SETLINK_NOTIFY; 3263 } 3264 3265 if (tb[IFLA_AF_SPEC]) { 3266 struct nlattr *af; 3267 int rem; 3268 3269 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 3270 struct rtnl_af_ops *af_ops; 3271 int af_ops_srcu_index; 3272 3273 af_ops = rtnl_af_lookup(nla_type(af), &af_ops_srcu_index); 3274 if (!af_ops) { 3275 err = -EAFNOSUPPORT; 3276 goto errout; 3277 } 3278 3279 err = af_ops->set_link_af(dev, af, extack); 3280 rtnl_af_put(af_ops, af_ops_srcu_index); 3281 3282 if (err < 0) 3283 goto errout; 3284 3285 status |= DO_SETLINK_NOTIFY; 3286 } 3287 } 3288 err = 0; 3289 3290 if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) { 3291 err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN], 3292 tb[IFLA_PROTO_DOWN_REASON], extack); 3293 if (err) 3294 goto errout; 3295 status |= DO_SETLINK_NOTIFY; 3296 } 3297 3298 if (tb[IFLA_XDP]) { 3299 struct nlattr *xdp[IFLA_XDP_MAX + 1]; 3300 u32 xdp_flags = 0; 3301 3302 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 3303 tb[IFLA_XDP], 3304 ifla_xdp_policy, NULL); 3305 if (err < 0) 3306 goto errout; 3307 3308 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 3309 err = -EINVAL; 3310 goto errout; 3311 } 3312 3313 if (xdp[IFLA_XDP_FLAGS]) { 3314 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 3315 if (xdp_flags & ~XDP_FLAGS_MASK) { 3316 err = -EINVAL; 3317 goto errout; 3318 } 3319 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 3320 err = -EINVAL; 3321 goto errout; 3322 } 3323 } 3324 3325 if (xdp[IFLA_XDP_FD]) { 3326 int expected_fd = -1; 3327 3328 if (xdp_flags & XDP_FLAGS_REPLACE) { 3329 if (!xdp[IFLA_XDP_EXPECTED_FD]) { 3330 err = -EINVAL; 3331 goto errout; 3332 } 3333 expected_fd = 3334 nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); 3335 } 3336 3337 err = dev_change_xdp_fd(dev, extack, 3338 nla_get_s32(xdp[IFLA_XDP_FD]), 3339 expected_fd, 3340 xdp_flags); 3341 if (err) 3342 goto errout; 3343 status |= DO_SETLINK_NOTIFY; 3344 } 3345 } 3346 3347 errout: 3348 if (status & DO_SETLINK_MODIFIED) { 3349 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 3350 netdev_state_change(dev); 3351 3352 if (err < 0) 3353 net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n", 3354 dev->name); 3355 } 3356 3357 return err; 3358 } 3359 3360 static struct net_device *rtnl_dev_get(struct net *net, 3361 struct nlattr *tb[]) 3362 { 3363 char ifname[ALTIFNAMSIZ]; 3364 3365 if (tb[IFLA_IFNAME]) 3366 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3367 else if (tb[IFLA_ALT_IFNAME]) 3368 nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ); 3369 else 3370 return NULL; 3371 3372 return __dev_get_by_name(net, ifname); 3373 } 3374 3375 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3376 struct netlink_ext_ack *extack) 3377 { 3378 struct ifinfomsg *ifm = nlmsg_data(nlh); 3379 struct net *net = sock_net(skb->sk); 3380 struct nlattr *tb[IFLA_MAX+1]; 3381 struct net_device *dev = NULL; 3382 struct rtnl_nets rtnl_nets; 3383 struct net *tgt_net; 3384 int err; 3385 3386 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 3387 ifla_policy, extack); 3388 if (err < 0) 3389 goto errout; 3390 3391 err = rtnl_ensure_unique_netns(tb, extack, false); 3392 if (err < 0) 3393 goto errout; 3394 3395 tgt_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 3396 if (IS_ERR(tgt_net)) { 3397 err = PTR_ERR(tgt_net); 3398 goto errout; 3399 } 3400 3401 rtnl_nets_init(&rtnl_nets); 3402 rtnl_nets_add(&rtnl_nets, get_net(net)); 3403 rtnl_nets_add(&rtnl_nets, tgt_net); 3404 3405 rtnl_nets_lock(&rtnl_nets); 3406 3407 if (ifm->ifi_index > 0) 3408 dev = __dev_get_by_index(net, ifm->ifi_index); 3409 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 3410 dev = rtnl_dev_get(net, tb); 3411 else 3412 err = -EINVAL; 3413 3414 if (dev) 3415 err = do_setlink(skb, dev, tgt_net, ifm, extack, tb, 0); 3416 else if (!err) 3417 err = -ENODEV; 3418 3419 rtnl_nets_unlock(&rtnl_nets); 3420 errout: 3421 return err; 3422 } 3423 3424 static int rtnl_group_dellink(const struct net *net, int group) 3425 { 3426 struct net_device *dev, *aux; 3427 LIST_HEAD(list_kill); 3428 bool found = false; 3429 3430 if (!group) 3431 return -EPERM; 3432 3433 for_each_netdev(net, dev) { 3434 if (dev->group == group) { 3435 const struct rtnl_link_ops *ops; 3436 3437 found = true; 3438 ops = dev->rtnl_link_ops; 3439 if (!ops || !ops->dellink) 3440 return -EOPNOTSUPP; 3441 } 3442 } 3443 3444 if (!found) 3445 return -ENODEV; 3446 3447 for_each_netdev_safe(net, dev, aux) { 3448 if (dev->group == group) { 3449 const struct rtnl_link_ops *ops; 3450 3451 ops = dev->rtnl_link_ops; 3452 ops->dellink(dev, &list_kill); 3453 } 3454 } 3455 unregister_netdevice_many(&list_kill); 3456 3457 return 0; 3458 } 3459 3460 int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh) 3461 { 3462 const struct rtnl_link_ops *ops; 3463 LIST_HEAD(list_kill); 3464 3465 ops = dev->rtnl_link_ops; 3466 if (!ops || !ops->dellink) 3467 return -EOPNOTSUPP; 3468 3469 ops->dellink(dev, &list_kill); 3470 unregister_netdevice_many_notify(&list_kill, portid, nlh); 3471 3472 return 0; 3473 } 3474 EXPORT_SYMBOL_GPL(rtnl_delete_link); 3475 3476 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 3477 struct netlink_ext_ack *extack) 3478 { 3479 struct ifinfomsg *ifm = nlmsg_data(nlh); 3480 struct net *net = sock_net(skb->sk); 3481 u32 portid = NETLINK_CB(skb).portid; 3482 struct nlattr *tb[IFLA_MAX+1]; 3483 struct net_device *dev = NULL; 3484 struct net *tgt_net = net; 3485 int netnsid = -1; 3486 int err; 3487 3488 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 3489 ifla_policy, extack); 3490 if (err < 0) 3491 return err; 3492 3493 err = rtnl_ensure_unique_netns(tb, extack, true); 3494 if (err < 0) 3495 return err; 3496 3497 if (tb[IFLA_TARGET_NETNSID]) { 3498 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3499 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 3500 if (IS_ERR(tgt_net)) 3501 return PTR_ERR(tgt_net); 3502 } 3503 3504 rtnl_net_lock(tgt_net); 3505 3506 if (ifm->ifi_index > 0) 3507 dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3508 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 3509 dev = rtnl_dev_get(tgt_net, tb); 3510 3511 if (dev) 3512 err = rtnl_delete_link(dev, portid, nlh); 3513 else if (ifm->ifi_index > 0 || tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 3514 err = -ENODEV; 3515 else if (tb[IFLA_GROUP]) 3516 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 3517 else 3518 err = -EINVAL; 3519 3520 rtnl_net_unlock(tgt_net); 3521 3522 if (netnsid >= 0) 3523 put_net(tgt_net); 3524 3525 return err; 3526 } 3527 3528 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm, 3529 u32 portid, const struct nlmsghdr *nlh) 3530 { 3531 unsigned int old_flags; 3532 int err; 3533 3534 old_flags = dev->flags; 3535 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 3536 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 3537 NULL); 3538 if (err < 0) 3539 return err; 3540 } 3541 3542 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 3543 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags), portid, nlh); 3544 } else { 3545 dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 3546 __dev_notify_flags(dev, old_flags, ~0U, portid, nlh); 3547 } 3548 return 0; 3549 } 3550 EXPORT_SYMBOL(rtnl_configure_link); 3551 3552 struct net_device *rtnl_create_link(struct net *net, const char *ifname, 3553 unsigned char name_assign_type, 3554 const struct rtnl_link_ops *ops, 3555 struct nlattr *tb[], 3556 struct netlink_ext_ack *extack) 3557 { 3558 struct net_device *dev; 3559 unsigned int num_tx_queues = 1; 3560 unsigned int num_rx_queues = 1; 3561 int err; 3562 3563 if (tb[IFLA_NUM_TX_QUEUES]) 3564 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 3565 else if (ops->get_num_tx_queues) 3566 num_tx_queues = ops->get_num_tx_queues(); 3567 3568 if (tb[IFLA_NUM_RX_QUEUES]) 3569 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 3570 else if (ops->get_num_rx_queues) 3571 num_rx_queues = ops->get_num_rx_queues(); 3572 3573 if (num_tx_queues < 1 || num_tx_queues > 4096) { 3574 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 3575 return ERR_PTR(-EINVAL); 3576 } 3577 3578 if (num_rx_queues < 1 || num_rx_queues > 4096) { 3579 NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 3580 return ERR_PTR(-EINVAL); 3581 } 3582 3583 if (ops->alloc) { 3584 dev = ops->alloc(tb, ifname, name_assign_type, 3585 num_tx_queues, num_rx_queues); 3586 if (IS_ERR(dev)) 3587 return dev; 3588 } else { 3589 dev = alloc_netdev_mqs(ops->priv_size, ifname, 3590 name_assign_type, ops->setup, 3591 num_tx_queues, num_rx_queues); 3592 } 3593 3594 if (!dev) 3595 return ERR_PTR(-ENOMEM); 3596 3597 err = validate_linkmsg(dev, tb, extack); 3598 if (err < 0) { 3599 free_netdev(dev); 3600 return ERR_PTR(err); 3601 } 3602 3603 dev_net_set(dev, net); 3604 dev->rtnl_link_ops = ops; 3605 dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 3606 3607 if (tb[IFLA_MTU]) { 3608 u32 mtu = nla_get_u32(tb[IFLA_MTU]); 3609 3610 err = dev_validate_mtu(dev, mtu, extack); 3611 if (err) { 3612 free_netdev(dev); 3613 return ERR_PTR(err); 3614 } 3615 dev->mtu = mtu; 3616 } 3617 if (tb[IFLA_ADDRESS]) { 3618 __dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]), 3619 nla_len(tb[IFLA_ADDRESS])); 3620 dev->addr_assign_type = NET_ADDR_SET; 3621 } 3622 if (tb[IFLA_BROADCAST]) 3623 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 3624 nla_len(tb[IFLA_BROADCAST])); 3625 if (tb[IFLA_TXQLEN]) 3626 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 3627 if (tb[IFLA_OPERSTATE]) 3628 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 3629 if (tb[IFLA_LINKMODE]) 3630 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 3631 if (tb[IFLA_GROUP]) 3632 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 3633 if (tb[IFLA_GSO_MAX_SIZE]) 3634 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 3635 if (tb[IFLA_GSO_MAX_SEGS]) 3636 netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS])); 3637 if (tb[IFLA_GRO_MAX_SIZE]) 3638 netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE])); 3639 if (tb[IFLA_GSO_IPV4_MAX_SIZE]) 3640 netif_set_gso_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GSO_IPV4_MAX_SIZE])); 3641 if (tb[IFLA_GRO_IPV4_MAX_SIZE]) 3642 netif_set_gro_ipv4_max_size(dev, nla_get_u32(tb[IFLA_GRO_IPV4_MAX_SIZE])); 3643 3644 return dev; 3645 } 3646 EXPORT_SYMBOL(rtnl_create_link); 3647 3648 struct rtnl_newlink_tbs { 3649 struct nlattr *tb[IFLA_MAX + 1]; 3650 struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 3651 struct nlattr *attr[RTNL_MAX_TYPE + 1]; 3652 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 3653 }; 3654 3655 static int rtnl_changelink(const struct sk_buff *skb, struct nlmsghdr *nlh, 3656 const struct rtnl_link_ops *ops, 3657 struct net_device *dev, struct net *tgt_net, 3658 struct rtnl_newlink_tbs *tbs, 3659 struct nlattr **data, 3660 struct netlink_ext_ack *extack) 3661 { 3662 struct nlattr ** const linkinfo = tbs->linkinfo; 3663 struct nlattr ** const tb = tbs->tb; 3664 int status = 0; 3665 int err; 3666 3667 if (nlh->nlmsg_flags & NLM_F_EXCL) 3668 return -EEXIST; 3669 3670 if (nlh->nlmsg_flags & NLM_F_REPLACE) 3671 return -EOPNOTSUPP; 3672 3673 if (linkinfo[IFLA_INFO_DATA]) { 3674 if (!ops || ops != dev->rtnl_link_ops || !ops->changelink) 3675 return -EOPNOTSUPP; 3676 3677 err = ops->changelink(dev, tb, data, extack); 3678 if (err < 0) 3679 return err; 3680 3681 status |= DO_SETLINK_NOTIFY; 3682 } 3683 3684 if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3685 const struct rtnl_link_ops *m_ops = NULL; 3686 struct nlattr **slave_data = NULL; 3687 struct net_device *master_dev; 3688 3689 master_dev = netdev_master_upper_dev_get(dev); 3690 if (master_dev) 3691 m_ops = master_dev->rtnl_link_ops; 3692 3693 if (!m_ops || !m_ops->slave_changelink) 3694 return -EOPNOTSUPP; 3695 3696 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3697 return -EINVAL; 3698 3699 if (m_ops->slave_maxtype) { 3700 err = nla_parse_nested_deprecated(tbs->slave_attr, 3701 m_ops->slave_maxtype, 3702 linkinfo[IFLA_INFO_SLAVE_DATA], 3703 m_ops->slave_policy, extack); 3704 if (err < 0) 3705 return err; 3706 3707 slave_data = tbs->slave_attr; 3708 } 3709 3710 err = m_ops->slave_changelink(master_dev, dev, tb, slave_data, extack); 3711 if (err < 0) 3712 return err; 3713 3714 status |= DO_SETLINK_NOTIFY; 3715 } 3716 3717 return do_setlink(skb, dev, tgt_net, nlmsg_data(nlh), extack, tb, status); 3718 } 3719 3720 static int rtnl_group_changelink(const struct sk_buff *skb, 3721 struct net *net, struct net *tgt_net, 3722 int group, struct ifinfomsg *ifm, 3723 struct netlink_ext_ack *extack, 3724 struct nlattr **tb) 3725 { 3726 struct net_device *dev, *aux; 3727 int err; 3728 3729 for_each_netdev_safe(net, dev, aux) { 3730 if (dev->group == group) { 3731 err = do_setlink(skb, dev, tgt_net, ifm, extack, tb, 0); 3732 if (err < 0) 3733 return err; 3734 } 3735 } 3736 3737 return 0; 3738 } 3739 3740 static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm, 3741 const struct rtnl_link_ops *ops, 3742 struct net *tgt_net, struct net *link_net, 3743 const struct nlmsghdr *nlh, 3744 struct nlattr **tb, struct nlattr **data, 3745 struct netlink_ext_ack *extack) 3746 { 3747 unsigned char name_assign_type = NET_NAME_USER; 3748 struct net *net = sock_net(skb->sk); 3749 u32 portid = NETLINK_CB(skb).portid; 3750 struct net_device *dev; 3751 char ifname[IFNAMSIZ]; 3752 int err; 3753 3754 if (!ops->alloc && !ops->setup) 3755 return -EOPNOTSUPP; 3756 3757 if (tb[IFLA_IFNAME]) { 3758 nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3759 } else { 3760 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 3761 name_assign_type = NET_NAME_ENUM; 3762 } 3763 3764 dev = rtnl_create_link(link_net ? : tgt_net, ifname, 3765 name_assign_type, ops, tb, extack); 3766 if (IS_ERR(dev)) { 3767 err = PTR_ERR(dev); 3768 goto out; 3769 } 3770 3771 dev->ifindex = ifm->ifi_index; 3772 3773 if (ops->newlink) 3774 err = ops->newlink(link_net ? : net, dev, tb, data, extack); 3775 else 3776 err = register_netdevice(dev); 3777 if (err < 0) { 3778 free_netdev(dev); 3779 goto out; 3780 } 3781 3782 err = rtnl_configure_link(dev, ifm, portid, nlh); 3783 if (err < 0) 3784 goto out_unregister; 3785 if (link_net) { 3786 err = dev_change_net_namespace(dev, tgt_net, ifname); 3787 if (err < 0) 3788 goto out_unregister; 3789 } 3790 if (tb[IFLA_MASTER]) { 3791 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 3792 if (err) 3793 goto out_unregister; 3794 } 3795 out: 3796 return err; 3797 out_unregister: 3798 if (ops->newlink) { 3799 LIST_HEAD(list_kill); 3800 3801 ops->dellink(dev, &list_kill); 3802 unregister_netdevice_many(&list_kill); 3803 } else { 3804 unregister_netdevice(dev); 3805 } 3806 goto out; 3807 } 3808 3809 static int rtnl_add_peer_net(struct rtnl_nets *rtnl_nets, 3810 const struct rtnl_link_ops *ops, 3811 struct nlattr *data[], 3812 struct netlink_ext_ack *extack) 3813 { 3814 struct nlattr *tb[IFLA_MAX + 1]; 3815 struct net *net; 3816 int err; 3817 3818 if (!data || !data[ops->peer_type]) 3819 return 0; 3820 3821 err = rtnl_nla_parse_ifinfomsg(tb, data[ops->peer_type], extack); 3822 if (err < 0) 3823 return err; 3824 3825 if (ops->validate) { 3826 err = ops->validate(tb, NULL, extack); 3827 if (err < 0) 3828 return err; 3829 } 3830 3831 net = rtnl_link_get_net_ifla(tb); 3832 if (IS_ERR(net)) 3833 return PTR_ERR(net); 3834 if (net) 3835 rtnl_nets_add(rtnl_nets, net); 3836 3837 return 0; 3838 } 3839 3840 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3841 const struct rtnl_link_ops *ops, 3842 struct net *tgt_net, struct net *link_net, 3843 struct rtnl_newlink_tbs *tbs, 3844 struct nlattr **data, 3845 struct netlink_ext_ack *extack) 3846 { 3847 struct nlattr ** const tb = tbs->tb; 3848 struct net *net = sock_net(skb->sk); 3849 struct net_device *dev; 3850 struct ifinfomsg *ifm; 3851 bool link_specified; 3852 3853 ifm = nlmsg_data(nlh); 3854 if (ifm->ifi_index > 0) { 3855 link_specified = true; 3856 dev = __dev_get_by_index(net, ifm->ifi_index); 3857 } else if (ifm->ifi_index < 0) { 3858 NL_SET_ERR_MSG(extack, "ifindex can't be negative"); 3859 return -EINVAL; 3860 } else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) { 3861 link_specified = true; 3862 dev = rtnl_dev_get(net, tb); 3863 } else { 3864 link_specified = false; 3865 dev = NULL; 3866 } 3867 3868 if (dev) 3869 return rtnl_changelink(skb, nlh, ops, dev, tgt_net, tbs, data, extack); 3870 3871 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3872 /* No dev found and NLM_F_CREATE not set. Requested dev does not exist, 3873 * or it's for a group 3874 */ 3875 if (link_specified || !tb[IFLA_GROUP]) 3876 return -ENODEV; 3877 3878 return rtnl_group_changelink(skb, net, tgt_net, 3879 nla_get_u32(tb[IFLA_GROUP]), 3880 ifm, extack, tb); 3881 } 3882 3883 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 3884 return -EOPNOTSUPP; 3885 3886 if (!ops) { 3887 NL_SET_ERR_MSG(extack, "Unknown device type"); 3888 return -EOPNOTSUPP; 3889 } 3890 3891 return rtnl_newlink_create(skb, ifm, ops, tgt_net, link_net, nlh, tb, data, extack); 3892 } 3893 3894 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3895 struct netlink_ext_ack *extack) 3896 { 3897 struct nlattr **tb, **linkinfo, **data = NULL; 3898 struct net *tgt_net, *link_net = NULL; 3899 struct rtnl_link_ops *ops = NULL; 3900 struct rtnl_newlink_tbs *tbs; 3901 struct rtnl_nets rtnl_nets; 3902 int ops_srcu_index; 3903 int ret; 3904 3905 tbs = kmalloc(sizeof(*tbs), GFP_KERNEL); 3906 if (!tbs) 3907 return -ENOMEM; 3908 3909 tb = tbs->tb; 3910 ret = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), tb, 3911 IFLA_MAX, ifla_policy, extack); 3912 if (ret < 0) 3913 goto free; 3914 3915 ret = rtnl_ensure_unique_netns(tb, extack, false); 3916 if (ret < 0) 3917 goto free; 3918 3919 linkinfo = tbs->linkinfo; 3920 if (tb[IFLA_LINKINFO]) { 3921 ret = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 3922 tb[IFLA_LINKINFO], 3923 ifla_info_policy, NULL); 3924 if (ret < 0) 3925 goto free; 3926 } else { 3927 memset(linkinfo, 0, sizeof(tbs->linkinfo)); 3928 } 3929 3930 if (linkinfo[IFLA_INFO_KIND]) { 3931 char kind[MODULE_NAME_LEN]; 3932 3933 nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 3934 ops = rtnl_link_ops_get(kind, &ops_srcu_index); 3935 #ifdef CONFIG_MODULES 3936 if (!ops) { 3937 request_module("rtnl-link-%s", kind); 3938 ops = rtnl_link_ops_get(kind, &ops_srcu_index); 3939 } 3940 #endif 3941 } 3942 3943 rtnl_nets_init(&rtnl_nets); 3944 3945 if (ops) { 3946 if (ops->maxtype > RTNL_MAX_TYPE) { 3947 ret = -EINVAL; 3948 goto put_ops; 3949 } 3950 3951 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 3952 ret = nla_parse_nested_deprecated(tbs->attr, ops->maxtype, 3953 linkinfo[IFLA_INFO_DATA], 3954 ops->policy, extack); 3955 if (ret < 0) 3956 goto put_ops; 3957 3958 data = tbs->attr; 3959 } 3960 3961 if (ops->validate) { 3962 ret = ops->validate(tb, data, extack); 3963 if (ret < 0) 3964 goto put_ops; 3965 } 3966 3967 if (ops->peer_type) { 3968 ret = rtnl_add_peer_net(&rtnl_nets, ops, data, extack); 3969 if (ret < 0) 3970 goto put_ops; 3971 } 3972 } 3973 3974 tgt_net = rtnl_link_get_net_capable(skb, sock_net(skb->sk), tb, CAP_NET_ADMIN); 3975 if (IS_ERR(tgt_net)) { 3976 ret = PTR_ERR(tgt_net); 3977 goto put_net; 3978 } 3979 3980 rtnl_nets_add(&rtnl_nets, tgt_net); 3981 3982 if (tb[IFLA_LINK_NETNSID]) { 3983 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3984 3985 link_net = get_net_ns_by_id(tgt_net, id); 3986 if (!link_net) { 3987 NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 3988 ret = -EINVAL; 3989 goto put_net; 3990 } 3991 3992 rtnl_nets_add(&rtnl_nets, link_net); 3993 3994 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) { 3995 ret = -EPERM; 3996 goto put_net; 3997 } 3998 } 3999 4000 rtnl_nets_lock(&rtnl_nets); 4001 ret = __rtnl_newlink(skb, nlh, ops, tgt_net, link_net, tbs, data, extack); 4002 rtnl_nets_unlock(&rtnl_nets); 4003 4004 put_net: 4005 rtnl_nets_destroy(&rtnl_nets); 4006 put_ops: 4007 if (ops) 4008 rtnl_link_ops_put(ops, ops_srcu_index); 4009 free: 4010 kfree(tbs); 4011 return ret; 4012 } 4013 4014 static int rtnl_valid_getlink_req(struct sk_buff *skb, 4015 const struct nlmsghdr *nlh, 4016 struct nlattr **tb, 4017 struct netlink_ext_ack *extack) 4018 { 4019 struct ifinfomsg *ifm; 4020 int i, err; 4021 4022 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 4023 NL_SET_ERR_MSG(extack, "Invalid header for get link"); 4024 return -EINVAL; 4025 } 4026 4027 if (!netlink_strict_get_check(skb)) 4028 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 4029 ifla_policy, extack); 4030 4031 ifm = nlmsg_data(nlh); 4032 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 4033 ifm->ifi_change) { 4034 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 4035 return -EINVAL; 4036 } 4037 4038 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 4039 ifla_policy, extack); 4040 if (err) 4041 return err; 4042 4043 for (i = 0; i <= IFLA_MAX; i++) { 4044 if (!tb[i]) 4045 continue; 4046 4047 switch (i) { 4048 case IFLA_IFNAME: 4049 case IFLA_ALT_IFNAME: 4050 case IFLA_EXT_MASK: 4051 case IFLA_TARGET_NETNSID: 4052 break; 4053 default: 4054 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 4055 return -EINVAL; 4056 } 4057 } 4058 4059 return 0; 4060 } 4061 4062 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4063 struct netlink_ext_ack *extack) 4064 { 4065 struct net *net = sock_net(skb->sk); 4066 struct net *tgt_net = net; 4067 struct ifinfomsg *ifm; 4068 struct nlattr *tb[IFLA_MAX+1]; 4069 struct net_device *dev = NULL; 4070 struct sk_buff *nskb; 4071 int netnsid = -1; 4072 int err; 4073 u32 ext_filter_mask = 0; 4074 4075 err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 4076 if (err < 0) 4077 return err; 4078 4079 err = rtnl_ensure_unique_netns(tb, extack, true); 4080 if (err < 0) 4081 return err; 4082 4083 if (tb[IFLA_TARGET_NETNSID]) { 4084 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 4085 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 4086 if (IS_ERR(tgt_net)) 4087 return PTR_ERR(tgt_net); 4088 } 4089 4090 if (tb[IFLA_EXT_MASK]) 4091 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 4092 4093 err = -EINVAL; 4094 ifm = nlmsg_data(nlh); 4095 if (ifm->ifi_index > 0) 4096 dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 4097 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 4098 dev = rtnl_dev_get(tgt_net, tb); 4099 else 4100 goto out; 4101 4102 err = -ENODEV; 4103 if (dev == NULL) 4104 goto out; 4105 4106 err = -ENOBUFS; 4107 nskb = nlmsg_new_large(if_nlmsg_size(dev, ext_filter_mask)); 4108 if (nskb == NULL) 4109 goto out; 4110 4111 /* Synchronize the carrier state so we don't report a state 4112 * that we're not actually going to honour immediately; if 4113 * the driver just did a carrier off->on transition, we can 4114 * only TX if link watch work has run, but without this we'd 4115 * already report carrier on, even if it doesn't work yet. 4116 */ 4117 linkwatch_sync_dev(dev); 4118 4119 err = rtnl_fill_ifinfo(nskb, dev, net, 4120 RTM_NEWLINK, NETLINK_CB(skb).portid, 4121 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 4122 0, NULL, 0, netnsid, GFP_KERNEL); 4123 if (err < 0) { 4124 /* -EMSGSIZE implies BUG in if_nlmsg_size */ 4125 WARN_ON(err == -EMSGSIZE); 4126 kfree_skb(nskb); 4127 } else 4128 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 4129 out: 4130 if (netnsid >= 0) 4131 put_net(tgt_net); 4132 4133 return err; 4134 } 4135 4136 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr, 4137 bool *changed, struct netlink_ext_ack *extack) 4138 { 4139 char *alt_ifname; 4140 size_t size; 4141 int err; 4142 4143 err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack); 4144 if (err) 4145 return err; 4146 4147 if (cmd == RTM_NEWLINKPROP) { 4148 size = rtnl_prop_list_size(dev); 4149 size += nla_total_size(ALTIFNAMSIZ); 4150 if (size >= U16_MAX) { 4151 NL_SET_ERR_MSG(extack, 4152 "effective property list too long"); 4153 return -EINVAL; 4154 } 4155 } 4156 4157 alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT); 4158 if (!alt_ifname) 4159 return -ENOMEM; 4160 4161 if (cmd == RTM_NEWLINKPROP) { 4162 err = netdev_name_node_alt_create(dev, alt_ifname); 4163 if (!err) 4164 alt_ifname = NULL; 4165 } else if (cmd == RTM_DELLINKPROP) { 4166 err = netdev_name_node_alt_destroy(dev, alt_ifname); 4167 } else { 4168 WARN_ON_ONCE(1); 4169 err = -EINVAL; 4170 } 4171 4172 kfree(alt_ifname); 4173 if (!err) 4174 *changed = true; 4175 return err; 4176 } 4177 4178 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh, 4179 struct netlink_ext_ack *extack) 4180 { 4181 struct net *net = sock_net(skb->sk); 4182 struct nlattr *tb[IFLA_MAX + 1]; 4183 struct net_device *dev; 4184 struct ifinfomsg *ifm; 4185 bool changed = false; 4186 struct nlattr *attr; 4187 int err, rem; 4188 4189 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 4190 if (err) 4191 return err; 4192 4193 err = rtnl_ensure_unique_netns(tb, extack, true); 4194 if (err) 4195 return err; 4196 4197 ifm = nlmsg_data(nlh); 4198 if (ifm->ifi_index > 0) 4199 dev = __dev_get_by_index(net, ifm->ifi_index); 4200 else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) 4201 dev = rtnl_dev_get(net, tb); 4202 else 4203 return -EINVAL; 4204 4205 if (!dev) 4206 return -ENODEV; 4207 4208 if (!tb[IFLA_PROP_LIST]) 4209 return 0; 4210 4211 nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) { 4212 switch (nla_type(attr)) { 4213 case IFLA_ALT_IFNAME: 4214 err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack); 4215 if (err) 4216 return err; 4217 break; 4218 } 4219 } 4220 4221 if (changed) 4222 netdev_state_change(dev); 4223 return 0; 4224 } 4225 4226 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 4227 struct netlink_ext_ack *extack) 4228 { 4229 return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack); 4230 } 4231 4232 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh, 4233 struct netlink_ext_ack *extack) 4234 { 4235 return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack); 4236 } 4237 4238 static noinline_for_stack u32 rtnl_calcit(struct sk_buff *skb, 4239 struct nlmsghdr *nlh) 4240 { 4241 struct net *net = sock_net(skb->sk); 4242 size_t min_ifinfo_dump_size = 0; 4243 u32 ext_filter_mask = 0; 4244 struct net_device *dev; 4245 struct nlattr *nla; 4246 int hdrlen, rem; 4247 4248 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 4249 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 4250 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 4251 4252 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) 4253 return NLMSG_GOODSIZE; 4254 4255 nla_for_each_attr_type(nla, IFLA_EXT_MASK, 4256 nlmsg_attrdata(nlh, hdrlen), 4257 nlmsg_attrlen(nlh, hdrlen), rem) { 4258 if (nla_len(nla) == sizeof(u32)) 4259 ext_filter_mask = nla_get_u32(nla); 4260 } 4261 4262 if (!ext_filter_mask) 4263 return NLMSG_GOODSIZE; 4264 /* 4265 * traverse the list of net devices and compute the minimum 4266 * buffer size based upon the filter mask. 4267 */ 4268 rcu_read_lock(); 4269 for_each_netdev_rcu(net, dev) { 4270 min_ifinfo_dump_size = max(min_ifinfo_dump_size, 4271 if_nlmsg_size(dev, ext_filter_mask)); 4272 } 4273 rcu_read_unlock(); 4274 4275 return nlmsg_total_size(min_ifinfo_dump_size); 4276 } 4277 4278 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 4279 { 4280 int idx; 4281 int s_idx = cb->family; 4282 int type = cb->nlh->nlmsg_type - RTM_BASE; 4283 int ret = 0; 4284 4285 if (s_idx == 0) 4286 s_idx = 1; 4287 4288 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 4289 struct rtnl_link __rcu **tab; 4290 struct rtnl_link *link; 4291 rtnl_dumpit_func dumpit; 4292 4293 if (idx < s_idx || idx == PF_PACKET) 4294 continue; 4295 4296 if (type < 0 || type >= RTM_NR_MSGTYPES) 4297 continue; 4298 4299 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 4300 if (!tab) 4301 continue; 4302 4303 link = rcu_dereference_rtnl(tab[type]); 4304 if (!link) 4305 continue; 4306 4307 dumpit = link->dumpit; 4308 if (!dumpit) 4309 continue; 4310 4311 if (idx > s_idx) { 4312 memset(&cb->args[0], 0, sizeof(cb->args)); 4313 cb->prev_seq = 0; 4314 cb->seq = 0; 4315 } 4316 ret = dumpit(skb, cb); 4317 if (ret) 4318 break; 4319 } 4320 cb->family = idx; 4321 4322 return skb->len ? : ret; 4323 } 4324 4325 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 4326 unsigned int change, 4327 u32 event, gfp_t flags, int *new_nsid, 4328 int new_ifindex, u32 portid, 4329 const struct nlmsghdr *nlh) 4330 { 4331 struct net *net = dev_net(dev); 4332 struct sk_buff *skb; 4333 int err = -ENOBUFS; 4334 u32 seq = 0; 4335 4336 skb = nlmsg_new(if_nlmsg_size(dev, 0), flags); 4337 if (skb == NULL) 4338 goto errout; 4339 4340 if (nlmsg_report(nlh)) 4341 seq = nlmsg_seq(nlh); 4342 else 4343 portid = 0; 4344 4345 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 4346 type, portid, seq, change, 0, 0, event, 4347 new_nsid, new_ifindex, -1, flags); 4348 if (err < 0) { 4349 /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 4350 WARN_ON(err == -EMSGSIZE); 4351 kfree_skb(skb); 4352 goto errout; 4353 } 4354 return skb; 4355 errout: 4356 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 4357 return NULL; 4358 } 4359 4360 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags, 4361 u32 portid, const struct nlmsghdr *nlh) 4362 { 4363 struct net *net = dev_net(dev); 4364 4365 rtnl_notify(skb, net, portid, RTNLGRP_LINK, nlh, flags); 4366 } 4367 4368 static void rtmsg_ifinfo_event(int type, struct net_device *dev, 4369 unsigned int change, u32 event, 4370 gfp_t flags, int *new_nsid, int new_ifindex, 4371 u32 portid, const struct nlmsghdr *nlh) 4372 { 4373 struct sk_buff *skb; 4374 4375 if (dev->reg_state != NETREG_REGISTERED) 4376 return; 4377 4378 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 4379 new_ifindex, portid, nlh); 4380 if (skb) 4381 rtmsg_ifinfo_send(skb, dev, flags, portid, nlh); 4382 } 4383 4384 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 4385 gfp_t flags, u32 portid, const struct nlmsghdr *nlh) 4386 { 4387 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 4388 NULL, 0, portid, nlh); 4389 } 4390 4391 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 4392 gfp_t flags, int *new_nsid, int new_ifindex) 4393 { 4394 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 4395 new_nsid, new_ifindex, 0, NULL); 4396 } 4397 4398 static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 4399 struct net_device *dev, 4400 u8 *addr, u16 vid, u32 pid, u32 seq, 4401 int type, unsigned int flags, 4402 int nlflags, u16 ndm_state) 4403 { 4404 struct nlmsghdr *nlh; 4405 struct ndmsg *ndm; 4406 4407 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 4408 if (!nlh) 4409 return -EMSGSIZE; 4410 4411 ndm = nlmsg_data(nlh); 4412 ndm->ndm_family = AF_BRIDGE; 4413 ndm->ndm_pad1 = 0; 4414 ndm->ndm_pad2 = 0; 4415 ndm->ndm_flags = flags; 4416 ndm->ndm_type = 0; 4417 ndm->ndm_ifindex = dev->ifindex; 4418 ndm->ndm_state = ndm_state; 4419 4420 if (nla_put(skb, NDA_LLADDR, dev->addr_len, addr)) 4421 goto nla_put_failure; 4422 if (vid) 4423 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 4424 goto nla_put_failure; 4425 4426 nlmsg_end(skb, nlh); 4427 return 0; 4428 4429 nla_put_failure: 4430 nlmsg_cancel(skb, nlh); 4431 return -EMSGSIZE; 4432 } 4433 4434 static inline size_t rtnl_fdb_nlmsg_size(const struct net_device *dev) 4435 { 4436 return NLMSG_ALIGN(sizeof(struct ndmsg)) + 4437 nla_total_size(dev->addr_len) + /* NDA_LLADDR */ 4438 nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 4439 0; 4440 } 4441 4442 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 4443 u16 ndm_state) 4444 { 4445 struct net *net = dev_net(dev); 4446 struct sk_buff *skb; 4447 int err = -ENOBUFS; 4448 4449 skb = nlmsg_new(rtnl_fdb_nlmsg_size(dev), GFP_ATOMIC); 4450 if (!skb) 4451 goto errout; 4452 4453 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 4454 0, 0, type, NTF_SELF, 0, ndm_state); 4455 if (err < 0) { 4456 kfree_skb(skb); 4457 goto errout; 4458 } 4459 4460 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 4461 return; 4462 errout: 4463 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 4464 } 4465 4466 /* 4467 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 4468 */ 4469 int ndo_dflt_fdb_add(struct ndmsg *ndm, 4470 struct nlattr *tb[], 4471 struct net_device *dev, 4472 const unsigned char *addr, u16 vid, 4473 u16 flags) 4474 { 4475 int err = -EINVAL; 4476 4477 /* If aging addresses are supported device will need to 4478 * implement its own handler for this. 4479 */ 4480 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 4481 netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4482 return err; 4483 } 4484 4485 if (tb[NDA_FLAGS_EXT]) { 4486 netdev_info(dev, "invalid flags given to default FDB implementation\n"); 4487 return err; 4488 } 4489 4490 if (vid) { 4491 netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n"); 4492 return err; 4493 } 4494 4495 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4496 err = dev_uc_add_excl(dev, addr); 4497 else if (is_multicast_ether_addr(addr)) 4498 err = dev_mc_add_excl(dev, addr); 4499 4500 /* Only return duplicate errors if NLM_F_EXCL is set */ 4501 if (err == -EEXIST && !(flags & NLM_F_EXCL)) 4502 err = 0; 4503 4504 return err; 4505 } 4506 EXPORT_SYMBOL(ndo_dflt_fdb_add); 4507 4508 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 4509 struct netlink_ext_ack *extack) 4510 { 4511 u16 vid = 0; 4512 4513 if (vlan_attr) { 4514 if (nla_len(vlan_attr) != sizeof(u16)) { 4515 NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 4516 return -EINVAL; 4517 } 4518 4519 vid = nla_get_u16(vlan_attr); 4520 4521 if (!vid || vid >= VLAN_VID_MASK) { 4522 NL_SET_ERR_MSG(extack, "invalid vlan id"); 4523 return -EINVAL; 4524 } 4525 } 4526 *p_vid = vid; 4527 return 0; 4528 } 4529 4530 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 4531 struct netlink_ext_ack *extack) 4532 { 4533 struct net *net = sock_net(skb->sk); 4534 struct ndmsg *ndm; 4535 struct nlattr *tb[NDA_MAX+1]; 4536 struct net_device *dev; 4537 u8 *addr; 4538 u16 vid; 4539 int err; 4540 4541 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 4542 extack); 4543 if (err < 0) 4544 return err; 4545 4546 ndm = nlmsg_data(nlh); 4547 if (ndm->ndm_ifindex == 0) { 4548 NL_SET_ERR_MSG(extack, "invalid ifindex"); 4549 return -EINVAL; 4550 } 4551 4552 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 4553 if (dev == NULL) { 4554 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4555 return -ENODEV; 4556 } 4557 4558 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4559 NL_SET_ERR_MSG(extack, "invalid address"); 4560 return -EINVAL; 4561 } 4562 4563 if (dev->type != ARPHRD_ETHER) { 4564 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 4565 return -EINVAL; 4566 } 4567 4568 addr = nla_data(tb[NDA_LLADDR]); 4569 4570 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4571 if (err) 4572 return err; 4573 4574 err = -EOPNOTSUPP; 4575 4576 /* Support fdb on master device the net/bridge default case */ 4577 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 4578 netif_is_bridge_port(dev)) { 4579 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4580 const struct net_device_ops *ops = br_dev->netdev_ops; 4581 bool notified = false; 4582 4583 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 4584 nlh->nlmsg_flags, ¬ified, extack); 4585 if (err) 4586 goto out; 4587 else 4588 ndm->ndm_flags &= ~NTF_MASTER; 4589 } 4590 4591 /* Embedded bridge, macvlan, and any other device support */ 4592 if ((ndm->ndm_flags & NTF_SELF)) { 4593 bool notified = false; 4594 4595 if (dev->netdev_ops->ndo_fdb_add) 4596 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 4597 vid, 4598 nlh->nlmsg_flags, 4599 ¬ified, extack); 4600 else 4601 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 4602 nlh->nlmsg_flags); 4603 4604 if (!err && !notified) { 4605 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 4606 ndm->ndm_state); 4607 ndm->ndm_flags &= ~NTF_SELF; 4608 } 4609 } 4610 out: 4611 return err; 4612 } 4613 4614 /* 4615 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 4616 */ 4617 int ndo_dflt_fdb_del(struct ndmsg *ndm, 4618 struct nlattr *tb[], 4619 struct net_device *dev, 4620 const unsigned char *addr, u16 vid) 4621 { 4622 int err = -EINVAL; 4623 4624 /* If aging addresses are supported device will need to 4625 * implement its own handler for this. 4626 */ 4627 if (!(ndm->ndm_state & NUD_PERMANENT)) { 4628 netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4629 return err; 4630 } 4631 4632 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4633 err = dev_uc_del(dev, addr); 4634 else if (is_multicast_ether_addr(addr)) 4635 err = dev_mc_del(dev, addr); 4636 4637 return err; 4638 } 4639 EXPORT_SYMBOL(ndo_dflt_fdb_del); 4640 4641 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 4642 struct netlink_ext_ack *extack) 4643 { 4644 bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK); 4645 struct net *net = sock_net(skb->sk); 4646 const struct net_device_ops *ops; 4647 struct ndmsg *ndm; 4648 struct nlattr *tb[NDA_MAX+1]; 4649 struct net_device *dev; 4650 __u8 *addr = NULL; 4651 int err; 4652 u16 vid; 4653 4654 if (!netlink_capable(skb, CAP_NET_ADMIN)) 4655 return -EPERM; 4656 4657 if (!del_bulk) { 4658 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, 4659 NULL, extack); 4660 } else { 4661 /* For bulk delete, the drivers will parse the message with 4662 * policy. 4663 */ 4664 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 4665 } 4666 if (err < 0) 4667 return err; 4668 4669 ndm = nlmsg_data(nlh); 4670 if (ndm->ndm_ifindex == 0) { 4671 NL_SET_ERR_MSG(extack, "invalid ifindex"); 4672 return -EINVAL; 4673 } 4674 4675 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 4676 if (dev == NULL) { 4677 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4678 return -ENODEV; 4679 } 4680 4681 if (!del_bulk) { 4682 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4683 NL_SET_ERR_MSG(extack, "invalid address"); 4684 return -EINVAL; 4685 } 4686 addr = nla_data(tb[NDA_LLADDR]); 4687 4688 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4689 if (err) 4690 return err; 4691 } 4692 4693 if (dev->type != ARPHRD_ETHER) { 4694 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4695 return -EINVAL; 4696 } 4697 4698 err = -EOPNOTSUPP; 4699 4700 /* Support fdb on master device the net/bridge default case */ 4701 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 4702 netif_is_bridge_port(dev)) { 4703 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4704 bool notified = false; 4705 4706 ops = br_dev->netdev_ops; 4707 if (!del_bulk) { 4708 if (ops->ndo_fdb_del) 4709 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, 4710 ¬ified, extack); 4711 } else { 4712 if (ops->ndo_fdb_del_bulk) 4713 err = ops->ndo_fdb_del_bulk(nlh, dev, extack); 4714 } 4715 4716 if (err) 4717 goto out; 4718 else 4719 ndm->ndm_flags &= ~NTF_MASTER; 4720 } 4721 4722 /* Embedded bridge, macvlan, and any other device support */ 4723 if (ndm->ndm_flags & NTF_SELF) { 4724 bool notified = false; 4725 4726 ops = dev->netdev_ops; 4727 if (!del_bulk) { 4728 if (ops->ndo_fdb_del) 4729 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, 4730 ¬ified, extack); 4731 else 4732 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 4733 } else { 4734 /* in case err was cleared by NTF_MASTER call */ 4735 err = -EOPNOTSUPP; 4736 if (ops->ndo_fdb_del_bulk) 4737 err = ops->ndo_fdb_del_bulk(nlh, dev, extack); 4738 } 4739 4740 if (!err) { 4741 if (!del_bulk && !notified) 4742 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 4743 ndm->ndm_state); 4744 ndm->ndm_flags &= ~NTF_SELF; 4745 } 4746 } 4747 out: 4748 return err; 4749 } 4750 4751 static int nlmsg_populate_fdb(struct sk_buff *skb, 4752 struct netlink_callback *cb, 4753 struct net_device *dev, 4754 int *idx, 4755 struct netdev_hw_addr_list *list) 4756 { 4757 struct netdev_hw_addr *ha; 4758 int err; 4759 u32 portid, seq; 4760 4761 portid = NETLINK_CB(cb->skb).portid; 4762 seq = cb->nlh->nlmsg_seq; 4763 4764 list_for_each_entry(ha, &list->list, list) { 4765 if (*idx < cb->args[2]) 4766 goto skip; 4767 4768 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4769 portid, seq, 4770 RTM_NEWNEIGH, NTF_SELF, 4771 NLM_F_MULTI, NUD_PERMANENT); 4772 if (err < 0) 4773 return err; 4774 skip: 4775 *idx += 1; 4776 } 4777 return 0; 4778 } 4779 4780 /** 4781 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4782 * @skb: socket buffer to store message in 4783 * @cb: netlink callback 4784 * @dev: netdevice 4785 * @filter_dev: ignored 4786 * @idx: the number of FDB table entries dumped is added to *@idx 4787 * 4788 * Default netdevice operation to dump the existing unicast address list. 4789 * Returns number of addresses from list put in skb. 4790 */ 4791 int ndo_dflt_fdb_dump(struct sk_buff *skb, 4792 struct netlink_callback *cb, 4793 struct net_device *dev, 4794 struct net_device *filter_dev, 4795 int *idx) 4796 { 4797 int err; 4798 4799 if (dev->type != ARPHRD_ETHER) 4800 return -EINVAL; 4801 4802 netif_addr_lock_bh(dev); 4803 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4804 if (err) 4805 goto out; 4806 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4807 out: 4808 netif_addr_unlock_bh(dev); 4809 return err; 4810 } 4811 EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4812 4813 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 4814 int *br_idx, int *brport_idx, 4815 struct netlink_ext_ack *extack) 4816 { 4817 struct nlattr *tb[NDA_MAX + 1]; 4818 struct ndmsg *ndm; 4819 int err, i; 4820 4821 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 4822 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 4823 return -EINVAL; 4824 } 4825 4826 ndm = nlmsg_data(nlh); 4827 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 4828 ndm->ndm_flags || ndm->ndm_type) { 4829 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 4830 return -EINVAL; 4831 } 4832 4833 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 4834 NDA_MAX, NULL, extack); 4835 if (err < 0) 4836 return err; 4837 4838 *brport_idx = ndm->ndm_ifindex; 4839 for (i = 0; i <= NDA_MAX; ++i) { 4840 if (!tb[i]) 4841 continue; 4842 4843 switch (i) { 4844 case NDA_IFINDEX: 4845 if (nla_len(tb[i]) != sizeof(u32)) { 4846 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 4847 return -EINVAL; 4848 } 4849 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 4850 break; 4851 case NDA_MASTER: 4852 if (nla_len(tb[i]) != sizeof(u32)) { 4853 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 4854 return -EINVAL; 4855 } 4856 *br_idx = nla_get_u32(tb[NDA_MASTER]); 4857 break; 4858 default: 4859 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 4860 return -EINVAL; 4861 } 4862 } 4863 4864 return 0; 4865 } 4866 4867 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 4868 int *br_idx, int *brport_idx, 4869 struct netlink_ext_ack *extack) 4870 { 4871 struct nlattr *tb[IFLA_MAX+1]; 4872 int err; 4873 4874 /* A hack to preserve kernel<->userspace interface. 4875 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 4876 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 4877 * So, check for ndmsg with an optional u32 attribute (not used here). 4878 * Fortunately these sizes don't conflict with the size of ifinfomsg 4879 * with an optional attribute. 4880 */ 4881 if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 4882 (nlmsg_len(nlh) != sizeof(struct ndmsg) + 4883 nla_attr_size(sizeof(u32)))) { 4884 struct ifinfomsg *ifm; 4885 4886 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 4887 tb, IFLA_MAX, ifla_policy, 4888 extack); 4889 if (err < 0) { 4890 return -EINVAL; 4891 } else if (err == 0) { 4892 if (tb[IFLA_MASTER]) 4893 *br_idx = nla_get_u32(tb[IFLA_MASTER]); 4894 } 4895 4896 ifm = nlmsg_data(nlh); 4897 *brport_idx = ifm->ifi_index; 4898 } 4899 return 0; 4900 } 4901 4902 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 4903 { 4904 struct net_device *dev; 4905 struct net_device *br_dev = NULL; 4906 const struct net_device_ops *ops = NULL; 4907 const struct net_device_ops *cops = NULL; 4908 struct net *net = sock_net(skb->sk); 4909 struct hlist_head *head; 4910 int brport_idx = 0; 4911 int br_idx = 0; 4912 int h, s_h; 4913 int idx = 0, s_idx; 4914 int err = 0; 4915 int fidx = 0; 4916 4917 if (cb->strict_check) 4918 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 4919 cb->extack); 4920 else 4921 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 4922 cb->extack); 4923 if (err < 0) 4924 return err; 4925 4926 if (br_idx) { 4927 br_dev = __dev_get_by_index(net, br_idx); 4928 if (!br_dev) 4929 return -ENODEV; 4930 4931 ops = br_dev->netdev_ops; 4932 } 4933 4934 s_h = cb->args[0]; 4935 s_idx = cb->args[1]; 4936 4937 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4938 idx = 0; 4939 head = &net->dev_index_head[h]; 4940 hlist_for_each_entry(dev, head, index_hlist) { 4941 4942 if (brport_idx && (dev->ifindex != brport_idx)) 4943 continue; 4944 4945 if (!br_idx) { /* user did not specify a specific bridge */ 4946 if (netif_is_bridge_port(dev)) { 4947 br_dev = netdev_master_upper_dev_get(dev); 4948 cops = br_dev->netdev_ops; 4949 } 4950 } else { 4951 if (dev != br_dev && 4952 !netif_is_bridge_port(dev)) 4953 continue; 4954 4955 if (br_dev != netdev_master_upper_dev_get(dev) && 4956 !netif_is_bridge_master(dev)) 4957 continue; 4958 cops = ops; 4959 } 4960 4961 if (idx < s_idx) 4962 goto cont; 4963 4964 if (netif_is_bridge_port(dev)) { 4965 if (cops && cops->ndo_fdb_dump) { 4966 err = cops->ndo_fdb_dump(skb, cb, 4967 br_dev, dev, 4968 &fidx); 4969 if (err == -EMSGSIZE) 4970 goto out; 4971 } 4972 } 4973 4974 if (dev->netdev_ops->ndo_fdb_dump) 4975 err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4976 dev, NULL, 4977 &fidx); 4978 else 4979 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4980 &fidx); 4981 if (err == -EMSGSIZE) 4982 goto out; 4983 4984 cops = NULL; 4985 4986 /* reset fdb offset to 0 for rest of the interfaces */ 4987 cb->args[2] = 0; 4988 fidx = 0; 4989 cont: 4990 idx++; 4991 } 4992 } 4993 4994 out: 4995 cb->args[0] = h; 4996 cb->args[1] = idx; 4997 cb->args[2] = fidx; 4998 4999 return skb->len; 5000 } 5001 5002 static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 5003 struct nlattr **tb, u8 *ndm_flags, 5004 int *br_idx, int *brport_idx, u8 **addr, 5005 u16 *vid, struct netlink_ext_ack *extack) 5006 { 5007 struct ndmsg *ndm; 5008 int err, i; 5009 5010 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 5011 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 5012 return -EINVAL; 5013 } 5014 5015 ndm = nlmsg_data(nlh); 5016 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 5017 ndm->ndm_type) { 5018 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 5019 return -EINVAL; 5020 } 5021 5022 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 5023 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 5024 return -EINVAL; 5025 } 5026 5027 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 5028 NDA_MAX, nda_policy, extack); 5029 if (err < 0) 5030 return err; 5031 5032 *ndm_flags = ndm->ndm_flags; 5033 *brport_idx = ndm->ndm_ifindex; 5034 for (i = 0; i <= NDA_MAX; ++i) { 5035 if (!tb[i]) 5036 continue; 5037 5038 switch (i) { 5039 case NDA_MASTER: 5040 *br_idx = nla_get_u32(tb[i]); 5041 break; 5042 case NDA_LLADDR: 5043 if (nla_len(tb[i]) != ETH_ALEN) { 5044 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 5045 return -EINVAL; 5046 } 5047 *addr = nla_data(tb[i]); 5048 break; 5049 case NDA_VLAN: 5050 err = fdb_vid_parse(tb[i], vid, extack); 5051 if (err) 5052 return err; 5053 break; 5054 case NDA_VNI: 5055 break; 5056 default: 5057 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 5058 return -EINVAL; 5059 } 5060 } 5061 5062 return 0; 5063 } 5064 5065 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 5066 struct netlink_ext_ack *extack) 5067 { 5068 struct net_device *dev = NULL, *br_dev = NULL; 5069 const struct net_device_ops *ops = NULL; 5070 struct net *net = sock_net(in_skb->sk); 5071 struct nlattr *tb[NDA_MAX + 1]; 5072 struct sk_buff *skb; 5073 int brport_idx = 0; 5074 u8 ndm_flags = 0; 5075 int br_idx = 0; 5076 u8 *addr = NULL; 5077 u16 vid = 0; 5078 int err; 5079 5080 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 5081 &brport_idx, &addr, &vid, extack); 5082 if (err < 0) 5083 return err; 5084 5085 if (!addr) { 5086 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 5087 return -EINVAL; 5088 } 5089 5090 if (brport_idx) { 5091 dev = __dev_get_by_index(net, brport_idx); 5092 if (!dev) { 5093 NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 5094 return -ENODEV; 5095 } 5096 } 5097 5098 if (br_idx) { 5099 if (dev) { 5100 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 5101 return -EINVAL; 5102 } 5103 5104 br_dev = __dev_get_by_index(net, br_idx); 5105 if (!br_dev) { 5106 NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 5107 return -EINVAL; 5108 } 5109 ops = br_dev->netdev_ops; 5110 } 5111 5112 if (dev) { 5113 if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 5114 if (!netif_is_bridge_port(dev)) { 5115 NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 5116 return -EINVAL; 5117 } 5118 br_dev = netdev_master_upper_dev_get(dev); 5119 if (!br_dev) { 5120 NL_SET_ERR_MSG(extack, "Master of device not found"); 5121 return -EINVAL; 5122 } 5123 ops = br_dev->netdev_ops; 5124 } else { 5125 if (!(ndm_flags & NTF_SELF)) { 5126 NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 5127 return -EINVAL; 5128 } 5129 ops = dev->netdev_ops; 5130 } 5131 } 5132 5133 if (!br_dev && !dev) { 5134 NL_SET_ERR_MSG(extack, "No device specified"); 5135 return -ENODEV; 5136 } 5137 5138 if (!ops || !ops->ndo_fdb_get) { 5139 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 5140 return -EOPNOTSUPP; 5141 } 5142 5143 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 5144 if (!skb) 5145 return -ENOBUFS; 5146 5147 if (br_dev) 5148 dev = br_dev; 5149 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 5150 NETLINK_CB(in_skb).portid, 5151 nlh->nlmsg_seq, extack); 5152 if (err) 5153 goto out; 5154 5155 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 5156 out: 5157 kfree_skb(skb); 5158 return err; 5159 } 5160 5161 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 5162 unsigned int attrnum, unsigned int flag) 5163 { 5164 if (mask & flag) 5165 return nla_put_u8(skb, attrnum, !!(flags & flag)); 5166 return 0; 5167 } 5168 5169 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 5170 struct net_device *dev, u16 mode, 5171 u32 flags, u32 mask, int nlflags, 5172 u32 filter_mask, 5173 int (*vlan_fill)(struct sk_buff *skb, 5174 struct net_device *dev, 5175 u32 filter_mask)) 5176 { 5177 struct nlmsghdr *nlh; 5178 struct ifinfomsg *ifm; 5179 struct nlattr *br_afspec; 5180 struct nlattr *protinfo; 5181 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 5182 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5183 int err = 0; 5184 5185 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 5186 if (nlh == NULL) 5187 return -EMSGSIZE; 5188 5189 ifm = nlmsg_data(nlh); 5190 ifm->ifi_family = AF_BRIDGE; 5191 ifm->__ifi_pad = 0; 5192 ifm->ifi_type = dev->type; 5193 ifm->ifi_index = dev->ifindex; 5194 ifm->ifi_flags = dev_get_flags(dev); 5195 ifm->ifi_change = 0; 5196 5197 5198 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 5199 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 5200 nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 5201 (br_dev && 5202 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 5203 (dev->addr_len && 5204 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 5205 (dev->ifindex != dev_get_iflink(dev) && 5206 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 5207 goto nla_put_failure; 5208 5209 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 5210 if (!br_afspec) 5211 goto nla_put_failure; 5212 5213 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 5214 nla_nest_cancel(skb, br_afspec); 5215 goto nla_put_failure; 5216 } 5217 5218 if (mode != BRIDGE_MODE_UNDEF) { 5219 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 5220 nla_nest_cancel(skb, br_afspec); 5221 goto nla_put_failure; 5222 } 5223 } 5224 if (vlan_fill) { 5225 err = vlan_fill(skb, dev, filter_mask); 5226 if (err) { 5227 nla_nest_cancel(skb, br_afspec); 5228 goto nla_put_failure; 5229 } 5230 } 5231 nla_nest_end(skb, br_afspec); 5232 5233 protinfo = nla_nest_start(skb, IFLA_PROTINFO); 5234 if (!protinfo) 5235 goto nla_put_failure; 5236 5237 if (brport_nla_put_flag(skb, flags, mask, 5238 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 5239 brport_nla_put_flag(skb, flags, mask, 5240 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 5241 brport_nla_put_flag(skb, flags, mask, 5242 IFLA_BRPORT_FAST_LEAVE, 5243 BR_MULTICAST_FAST_LEAVE) || 5244 brport_nla_put_flag(skb, flags, mask, 5245 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 5246 brport_nla_put_flag(skb, flags, mask, 5247 IFLA_BRPORT_LEARNING, BR_LEARNING) || 5248 brport_nla_put_flag(skb, flags, mask, 5249 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 5250 brport_nla_put_flag(skb, flags, mask, 5251 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 5252 brport_nla_put_flag(skb, flags, mask, 5253 IFLA_BRPORT_PROXYARP, BR_PROXYARP) || 5254 brport_nla_put_flag(skb, flags, mask, 5255 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || 5256 brport_nla_put_flag(skb, flags, mask, 5257 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { 5258 nla_nest_cancel(skb, protinfo); 5259 goto nla_put_failure; 5260 } 5261 5262 nla_nest_end(skb, protinfo); 5263 5264 nlmsg_end(skb, nlh); 5265 return 0; 5266 nla_put_failure: 5267 nlmsg_cancel(skb, nlh); 5268 return err ? err : -EMSGSIZE; 5269 } 5270 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 5271 5272 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 5273 bool strict_check, u32 *filter_mask, 5274 struct netlink_ext_ack *extack) 5275 { 5276 struct nlattr *tb[IFLA_MAX+1]; 5277 int err, i; 5278 5279 if (strict_check) { 5280 struct ifinfomsg *ifm; 5281 5282 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 5283 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 5284 return -EINVAL; 5285 } 5286 5287 ifm = nlmsg_data(nlh); 5288 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 5289 ifm->ifi_change || ifm->ifi_index) { 5290 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 5291 return -EINVAL; 5292 } 5293 5294 err = nlmsg_parse_deprecated_strict(nlh, 5295 sizeof(struct ifinfomsg), 5296 tb, IFLA_MAX, ifla_policy, 5297 extack); 5298 } else { 5299 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 5300 tb, IFLA_MAX, ifla_policy, 5301 extack); 5302 } 5303 if (err < 0) 5304 return err; 5305 5306 /* new attributes should only be added with strict checking */ 5307 for (i = 0; i <= IFLA_MAX; ++i) { 5308 if (!tb[i]) 5309 continue; 5310 5311 switch (i) { 5312 case IFLA_EXT_MASK: 5313 *filter_mask = nla_get_u32(tb[i]); 5314 break; 5315 default: 5316 if (strict_check) { 5317 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 5318 return -EINVAL; 5319 } 5320 } 5321 } 5322 5323 return 0; 5324 } 5325 5326 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 5327 { 5328 const struct nlmsghdr *nlh = cb->nlh; 5329 struct net *net = sock_net(skb->sk); 5330 struct net_device *dev; 5331 int idx = 0; 5332 u32 portid = NETLINK_CB(cb->skb).portid; 5333 u32 seq = nlh->nlmsg_seq; 5334 u32 filter_mask = 0; 5335 int err; 5336 5337 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 5338 cb->extack); 5339 if (err < 0 && cb->strict_check) 5340 return err; 5341 5342 rcu_read_lock(); 5343 for_each_netdev_rcu(net, dev) { 5344 const struct net_device_ops *ops = dev->netdev_ops; 5345 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5346 5347 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 5348 if (idx >= cb->args[0]) { 5349 err = br_dev->netdev_ops->ndo_bridge_getlink( 5350 skb, portid, seq, dev, 5351 filter_mask, NLM_F_MULTI); 5352 if (err < 0 && err != -EOPNOTSUPP) { 5353 if (likely(skb->len)) 5354 break; 5355 5356 goto out_err; 5357 } 5358 } 5359 idx++; 5360 } 5361 5362 if (ops->ndo_bridge_getlink) { 5363 if (idx >= cb->args[0]) { 5364 err = ops->ndo_bridge_getlink(skb, portid, 5365 seq, dev, 5366 filter_mask, 5367 NLM_F_MULTI); 5368 if (err < 0 && err != -EOPNOTSUPP) { 5369 if (likely(skb->len)) 5370 break; 5371 5372 goto out_err; 5373 } 5374 } 5375 idx++; 5376 } 5377 } 5378 err = skb->len; 5379 out_err: 5380 rcu_read_unlock(); 5381 cb->args[0] = idx; 5382 5383 return err; 5384 } 5385 5386 static inline size_t bridge_nlmsg_size(void) 5387 { 5388 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 5389 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 5390 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 5391 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 5392 + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 5393 + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 5394 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 5395 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 5396 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 5397 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 5398 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 5399 } 5400 5401 static int rtnl_bridge_notify(struct net_device *dev) 5402 { 5403 struct net *net = dev_net(dev); 5404 struct sk_buff *skb; 5405 int err = -EOPNOTSUPP; 5406 5407 if (!dev->netdev_ops->ndo_bridge_getlink) 5408 return 0; 5409 5410 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 5411 if (!skb) { 5412 err = -ENOMEM; 5413 goto errout; 5414 } 5415 5416 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 5417 if (err < 0) 5418 goto errout; 5419 5420 /* Notification info is only filled for bridge ports, not the bridge 5421 * device itself. Therefore, a zero notification length is valid and 5422 * should not result in an error. 5423 */ 5424 if (!skb->len) 5425 goto errout; 5426 5427 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 5428 return 0; 5429 errout: 5430 WARN_ON(err == -EMSGSIZE); 5431 kfree_skb(skb); 5432 if (err) 5433 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 5434 return err; 5435 } 5436 5437 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 5438 struct netlink_ext_ack *extack) 5439 { 5440 struct net *net = sock_net(skb->sk); 5441 struct ifinfomsg *ifm; 5442 struct net_device *dev; 5443 struct nlattr *br_spec, *attr, *br_flags_attr = NULL; 5444 int rem, err = -EOPNOTSUPP; 5445 u16 flags = 0; 5446 5447 if (nlmsg_len(nlh) < sizeof(*ifm)) 5448 return -EINVAL; 5449 5450 ifm = nlmsg_data(nlh); 5451 if (ifm->ifi_family != AF_BRIDGE) 5452 return -EPFNOSUPPORT; 5453 5454 dev = __dev_get_by_index(net, ifm->ifi_index); 5455 if (!dev) { 5456 NL_SET_ERR_MSG(extack, "unknown ifindex"); 5457 return -ENODEV; 5458 } 5459 5460 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5461 if (br_spec) { 5462 nla_for_each_nested(attr, br_spec, rem) { 5463 if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !br_flags_attr) { 5464 if (nla_len(attr) < sizeof(flags)) 5465 return -EINVAL; 5466 5467 br_flags_attr = attr; 5468 flags = nla_get_u16(attr); 5469 } 5470 5471 if (nla_type(attr) == IFLA_BRIDGE_MODE) { 5472 if (nla_len(attr) < sizeof(u16)) 5473 return -EINVAL; 5474 } 5475 } 5476 } 5477 5478 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5479 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5480 5481 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 5482 err = -EOPNOTSUPP; 5483 goto out; 5484 } 5485 5486 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 5487 extack); 5488 if (err) 5489 goto out; 5490 5491 flags &= ~BRIDGE_FLAGS_MASTER; 5492 } 5493 5494 if ((flags & BRIDGE_FLAGS_SELF)) { 5495 if (!dev->netdev_ops->ndo_bridge_setlink) 5496 err = -EOPNOTSUPP; 5497 else 5498 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 5499 flags, 5500 extack); 5501 if (!err) { 5502 flags &= ~BRIDGE_FLAGS_SELF; 5503 5504 /* Generate event to notify upper layer of bridge 5505 * change 5506 */ 5507 err = rtnl_bridge_notify(dev); 5508 } 5509 } 5510 5511 if (br_flags_attr) 5512 memcpy(nla_data(br_flags_attr), &flags, sizeof(flags)); 5513 out: 5514 return err; 5515 } 5516 5517 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 5518 struct netlink_ext_ack *extack) 5519 { 5520 struct net *net = sock_net(skb->sk); 5521 struct ifinfomsg *ifm; 5522 struct net_device *dev; 5523 struct nlattr *br_spec, *attr = NULL; 5524 int rem, err = -EOPNOTSUPP; 5525 u16 flags = 0; 5526 bool have_flags = false; 5527 5528 if (nlmsg_len(nlh) < sizeof(*ifm)) 5529 return -EINVAL; 5530 5531 ifm = nlmsg_data(nlh); 5532 if (ifm->ifi_family != AF_BRIDGE) 5533 return -EPFNOSUPPORT; 5534 5535 dev = __dev_get_by_index(net, ifm->ifi_index); 5536 if (!dev) { 5537 NL_SET_ERR_MSG(extack, "unknown ifindex"); 5538 return -ENODEV; 5539 } 5540 5541 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5542 if (br_spec) { 5543 nla_for_each_nested_type(attr, IFLA_BRIDGE_FLAGS, br_spec, 5544 rem) { 5545 if (nla_len(attr) < sizeof(flags)) 5546 return -EINVAL; 5547 5548 have_flags = true; 5549 flags = nla_get_u16(attr); 5550 break; 5551 } 5552 } 5553 5554 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5555 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5556 5557 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 5558 err = -EOPNOTSUPP; 5559 goto out; 5560 } 5561 5562 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 5563 if (err) 5564 goto out; 5565 5566 flags &= ~BRIDGE_FLAGS_MASTER; 5567 } 5568 5569 if ((flags & BRIDGE_FLAGS_SELF)) { 5570 if (!dev->netdev_ops->ndo_bridge_dellink) 5571 err = -EOPNOTSUPP; 5572 else 5573 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 5574 flags); 5575 5576 if (!err) { 5577 flags &= ~BRIDGE_FLAGS_SELF; 5578 5579 /* Generate event to notify upper layer of bridge 5580 * change 5581 */ 5582 err = rtnl_bridge_notify(dev); 5583 } 5584 } 5585 5586 if (have_flags) 5587 memcpy(nla_data(attr), &flags, sizeof(flags)); 5588 out: 5589 return err; 5590 } 5591 5592 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 5593 { 5594 return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 5595 (!idxattr || idxattr == attrid); 5596 } 5597 5598 static bool 5599 rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id) 5600 { 5601 return dev->netdev_ops && 5602 dev->netdev_ops->ndo_has_offload_stats && 5603 dev->netdev_ops->ndo_get_offload_stats && 5604 dev->netdev_ops->ndo_has_offload_stats(dev, attr_id); 5605 } 5606 5607 static unsigned int 5608 rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id) 5609 { 5610 return rtnl_offload_xstats_have_ndo(dev, attr_id) ? 5611 sizeof(struct rtnl_link_stats64) : 0; 5612 } 5613 5614 static int 5615 rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id, 5616 struct sk_buff *skb) 5617 { 5618 unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id); 5619 struct nlattr *attr = NULL; 5620 void *attr_data; 5621 int err; 5622 5623 if (!size) 5624 return -ENODATA; 5625 5626 attr = nla_reserve_64bit(skb, attr_id, size, 5627 IFLA_OFFLOAD_XSTATS_UNSPEC); 5628 if (!attr) 5629 return -EMSGSIZE; 5630 5631 attr_data = nla_data(attr); 5632 memset(attr_data, 0, size); 5633 5634 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data); 5635 if (err) 5636 return err; 5637 5638 return 0; 5639 } 5640 5641 static unsigned int 5642 rtnl_offload_xstats_get_size_stats(const struct net_device *dev, 5643 enum netdev_offload_xstats_type type) 5644 { 5645 bool enabled = netdev_offload_xstats_enabled(dev, type); 5646 5647 return enabled ? sizeof(struct rtnl_hw_stats64) : 0; 5648 } 5649 5650 struct rtnl_offload_xstats_request_used { 5651 bool request; 5652 bool used; 5653 }; 5654 5655 static int 5656 rtnl_offload_xstats_get_stats(struct net_device *dev, 5657 enum netdev_offload_xstats_type type, 5658 struct rtnl_offload_xstats_request_used *ru, 5659 struct rtnl_hw_stats64 *stats, 5660 struct netlink_ext_ack *extack) 5661 { 5662 bool request; 5663 bool used; 5664 int err; 5665 5666 request = netdev_offload_xstats_enabled(dev, type); 5667 if (!request) { 5668 used = false; 5669 goto out; 5670 } 5671 5672 err = netdev_offload_xstats_get(dev, type, stats, &used, extack); 5673 if (err) 5674 return err; 5675 5676 out: 5677 if (ru) { 5678 ru->request = request; 5679 ru->used = used; 5680 } 5681 return 0; 5682 } 5683 5684 static int 5685 rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id, 5686 struct rtnl_offload_xstats_request_used *ru) 5687 { 5688 struct nlattr *nest; 5689 5690 nest = nla_nest_start(skb, attr_id); 5691 if (!nest) 5692 return -EMSGSIZE; 5693 5694 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request)) 5695 goto nla_put_failure; 5696 5697 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used)) 5698 goto nla_put_failure; 5699 5700 nla_nest_end(skb, nest); 5701 return 0; 5702 5703 nla_put_failure: 5704 nla_nest_cancel(skb, nest); 5705 return -EMSGSIZE; 5706 } 5707 5708 static int 5709 rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev, 5710 struct netlink_ext_ack *extack) 5711 { 5712 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5713 struct rtnl_offload_xstats_request_used ru_l3; 5714 struct nlattr *nest; 5715 int err; 5716 5717 err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack); 5718 if (err) 5719 return err; 5720 5721 nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO); 5722 if (!nest) 5723 return -EMSGSIZE; 5724 5725 if (rtnl_offload_xstats_fill_hw_s_info_one(skb, 5726 IFLA_OFFLOAD_XSTATS_L3_STATS, 5727 &ru_l3)) 5728 goto nla_put_failure; 5729 5730 nla_nest_end(skb, nest); 5731 return 0; 5732 5733 nla_put_failure: 5734 nla_nest_cancel(skb, nest); 5735 return -EMSGSIZE; 5736 } 5737 5738 static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev, 5739 int *prividx, u32 off_filter_mask, 5740 struct netlink_ext_ack *extack) 5741 { 5742 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5743 int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO; 5744 int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS; 5745 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 5746 bool have_data = false; 5747 int err; 5748 5749 if (*prividx <= attr_id_cpu_hit && 5750 (off_filter_mask & 5751 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) { 5752 err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb); 5753 if (!err) { 5754 have_data = true; 5755 } else if (err != -ENODATA) { 5756 *prividx = attr_id_cpu_hit; 5757 return err; 5758 } 5759 } 5760 5761 if (*prividx <= attr_id_hw_s_info && 5762 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) { 5763 *prividx = attr_id_hw_s_info; 5764 5765 err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack); 5766 if (err) 5767 return err; 5768 5769 have_data = true; 5770 *prividx = 0; 5771 } 5772 5773 if (*prividx <= attr_id_l3_stats && 5774 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) { 5775 unsigned int size_l3; 5776 struct nlattr *attr; 5777 5778 *prividx = attr_id_l3_stats; 5779 5780 size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3); 5781 if (!size_l3) 5782 goto skip_l3_stats; 5783 attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3, 5784 IFLA_OFFLOAD_XSTATS_UNSPEC); 5785 if (!attr) 5786 return -EMSGSIZE; 5787 5788 err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL, 5789 nla_data(attr), extack); 5790 if (err) 5791 return err; 5792 5793 have_data = true; 5794 skip_l3_stats: 5795 *prividx = 0; 5796 } 5797 5798 if (!have_data) 5799 return -ENODATA; 5800 5801 *prividx = 0; 5802 return 0; 5803 } 5804 5805 static unsigned int 5806 rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev, 5807 enum netdev_offload_xstats_type type) 5808 { 5809 return nla_total_size(0) + 5810 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */ 5811 nla_total_size(sizeof(u8)) + 5812 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */ 5813 nla_total_size(sizeof(u8)) + 5814 0; 5815 } 5816 5817 static unsigned int 5818 rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev) 5819 { 5820 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5821 5822 return nla_total_size(0) + 5823 /* IFLA_OFFLOAD_XSTATS_L3_STATS */ 5824 rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) + 5825 0; 5826 } 5827 5828 static int rtnl_offload_xstats_get_size(const struct net_device *dev, 5829 u32 off_filter_mask) 5830 { 5831 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5832 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 5833 int nla_size = 0; 5834 int size; 5835 5836 if (off_filter_mask & 5837 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) { 5838 size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit); 5839 nla_size += nla_total_size_64bit(size); 5840 } 5841 5842 if (off_filter_mask & 5843 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO)) 5844 nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev); 5845 5846 if (off_filter_mask & 5847 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) { 5848 size = rtnl_offload_xstats_get_size_stats(dev, t_l3); 5849 nla_size += nla_total_size_64bit(size); 5850 } 5851 5852 if (nla_size != 0) 5853 nla_size += nla_total_size(0); 5854 5855 return nla_size; 5856 } 5857 5858 struct rtnl_stats_dump_filters { 5859 /* mask[0] filters outer attributes. Then individual nests have their 5860 * filtering mask at the index of the nested attribute. 5861 */ 5862 u32 mask[IFLA_STATS_MAX + 1]; 5863 }; 5864 5865 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 5866 int type, u32 pid, u32 seq, u32 change, 5867 unsigned int flags, 5868 const struct rtnl_stats_dump_filters *filters, 5869 int *idxattr, int *prividx, 5870 struct netlink_ext_ack *extack) 5871 { 5872 unsigned int filter_mask = filters->mask[0]; 5873 struct if_stats_msg *ifsm; 5874 struct nlmsghdr *nlh; 5875 struct nlattr *attr; 5876 int s_prividx = *prividx; 5877 int err; 5878 5879 ASSERT_RTNL(); 5880 5881 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 5882 if (!nlh) 5883 return -EMSGSIZE; 5884 5885 ifsm = nlmsg_data(nlh); 5886 ifsm->family = PF_UNSPEC; 5887 ifsm->pad1 = 0; 5888 ifsm->pad2 = 0; 5889 ifsm->ifindex = dev->ifindex; 5890 ifsm->filter_mask = filter_mask; 5891 5892 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 5893 struct rtnl_link_stats64 *sp; 5894 5895 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 5896 sizeof(struct rtnl_link_stats64), 5897 IFLA_STATS_UNSPEC); 5898 if (!attr) { 5899 err = -EMSGSIZE; 5900 goto nla_put_failure; 5901 } 5902 5903 sp = nla_data(attr); 5904 dev_get_stats(dev, sp); 5905 } 5906 5907 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 5908 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 5909 5910 if (ops && ops->fill_linkxstats) { 5911 *idxattr = IFLA_STATS_LINK_XSTATS; 5912 attr = nla_nest_start_noflag(skb, 5913 IFLA_STATS_LINK_XSTATS); 5914 if (!attr) { 5915 err = -EMSGSIZE; 5916 goto nla_put_failure; 5917 } 5918 5919 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 5920 nla_nest_end(skb, attr); 5921 if (err) 5922 goto nla_put_failure; 5923 *idxattr = 0; 5924 } 5925 } 5926 5927 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 5928 *idxattr)) { 5929 const struct rtnl_link_ops *ops = NULL; 5930 const struct net_device *master; 5931 5932 master = netdev_master_upper_dev_get(dev); 5933 if (master) 5934 ops = master->rtnl_link_ops; 5935 if (ops && ops->fill_linkxstats) { 5936 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5937 attr = nla_nest_start_noflag(skb, 5938 IFLA_STATS_LINK_XSTATS_SLAVE); 5939 if (!attr) { 5940 err = -EMSGSIZE; 5941 goto nla_put_failure; 5942 } 5943 5944 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 5945 nla_nest_end(skb, attr); 5946 if (err) 5947 goto nla_put_failure; 5948 *idxattr = 0; 5949 } 5950 } 5951 5952 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 5953 *idxattr)) { 5954 u32 off_filter_mask; 5955 5956 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 5957 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5958 attr = nla_nest_start_noflag(skb, 5959 IFLA_STATS_LINK_OFFLOAD_XSTATS); 5960 if (!attr) { 5961 err = -EMSGSIZE; 5962 goto nla_put_failure; 5963 } 5964 5965 err = rtnl_offload_xstats_fill(skb, dev, prividx, 5966 off_filter_mask, extack); 5967 if (err == -ENODATA) 5968 nla_nest_cancel(skb, attr); 5969 else 5970 nla_nest_end(skb, attr); 5971 5972 if (err && err != -ENODATA) 5973 goto nla_put_failure; 5974 *idxattr = 0; 5975 } 5976 5977 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5978 struct rtnl_af_ops *af_ops; 5979 5980 *idxattr = IFLA_STATS_AF_SPEC; 5981 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5982 if (!attr) { 5983 err = -EMSGSIZE; 5984 goto nla_put_failure; 5985 } 5986 5987 rcu_read_lock(); 5988 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5989 if (af_ops->fill_stats_af) { 5990 struct nlattr *af; 5991 5992 af = nla_nest_start_noflag(skb, 5993 af_ops->family); 5994 if (!af) { 5995 rcu_read_unlock(); 5996 err = -EMSGSIZE; 5997 goto nla_put_failure; 5998 } 5999 err = af_ops->fill_stats_af(skb, dev); 6000 6001 if (err == -ENODATA) { 6002 nla_nest_cancel(skb, af); 6003 } else if (err < 0) { 6004 rcu_read_unlock(); 6005 goto nla_put_failure; 6006 } 6007 6008 nla_nest_end(skb, af); 6009 } 6010 } 6011 rcu_read_unlock(); 6012 6013 nla_nest_end(skb, attr); 6014 6015 *idxattr = 0; 6016 } 6017 6018 nlmsg_end(skb, nlh); 6019 6020 return 0; 6021 6022 nla_put_failure: 6023 /* not a multi message or no progress mean a real error */ 6024 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 6025 nlmsg_cancel(skb, nlh); 6026 else 6027 nlmsg_end(skb, nlh); 6028 6029 return err; 6030 } 6031 6032 static size_t if_nlmsg_stats_size(const struct net_device *dev, 6033 const struct rtnl_stats_dump_filters *filters) 6034 { 6035 size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); 6036 unsigned int filter_mask = filters->mask[0]; 6037 6038 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 6039 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 6040 6041 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 6042 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 6043 int attr = IFLA_STATS_LINK_XSTATS; 6044 6045 if (ops && ops->get_linkxstats_size) { 6046 size += nla_total_size(ops->get_linkxstats_size(dev, 6047 attr)); 6048 /* for IFLA_STATS_LINK_XSTATS */ 6049 size += nla_total_size(0); 6050 } 6051 } 6052 6053 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 6054 struct net_device *_dev = (struct net_device *)dev; 6055 const struct rtnl_link_ops *ops = NULL; 6056 const struct net_device *master; 6057 6058 /* netdev_master_upper_dev_get can't take const */ 6059 master = netdev_master_upper_dev_get(_dev); 6060 if (master) 6061 ops = master->rtnl_link_ops; 6062 if (ops && ops->get_linkxstats_size) { 6063 int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 6064 6065 size += nla_total_size(ops->get_linkxstats_size(dev, 6066 attr)); 6067 /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 6068 size += nla_total_size(0); 6069 } 6070 } 6071 6072 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) { 6073 u32 off_filter_mask; 6074 6075 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 6076 size += rtnl_offload_xstats_get_size(dev, off_filter_mask); 6077 } 6078 6079 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 6080 struct rtnl_af_ops *af_ops; 6081 6082 /* for IFLA_STATS_AF_SPEC */ 6083 size += nla_total_size(0); 6084 6085 rcu_read_lock(); 6086 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 6087 if (af_ops->get_stats_af_size) { 6088 size += nla_total_size( 6089 af_ops->get_stats_af_size(dev)); 6090 6091 /* for AF_* */ 6092 size += nla_total_size(0); 6093 } 6094 } 6095 rcu_read_unlock(); 6096 } 6097 6098 return size; 6099 } 6100 6101 #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1) 6102 6103 static const struct nla_policy 6104 rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = { 6105 [IFLA_STATS_LINK_OFFLOAD_XSTATS] = 6106 NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID), 6107 }; 6108 6109 static const struct nla_policy 6110 rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = { 6111 [IFLA_STATS_GET_FILTERS] = 6112 NLA_POLICY_NESTED(rtnl_stats_get_policy_filters), 6113 }; 6114 6115 static const struct nla_policy 6116 ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = { 6117 [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1), 6118 }; 6119 6120 static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters, 6121 struct rtnl_stats_dump_filters *filters, 6122 struct netlink_ext_ack *extack) 6123 { 6124 struct nlattr *tb[IFLA_STATS_MAX + 1]; 6125 int err; 6126 int at; 6127 6128 err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters, 6129 rtnl_stats_get_policy_filters, extack); 6130 if (err < 0) 6131 return err; 6132 6133 for (at = 1; at <= IFLA_STATS_MAX; at++) { 6134 if (tb[at]) { 6135 if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) { 6136 NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask"); 6137 return -EINVAL; 6138 } 6139 filters->mask[at] = nla_get_u32(tb[at]); 6140 } 6141 } 6142 6143 return 0; 6144 } 6145 6146 static int rtnl_stats_get_parse(const struct nlmsghdr *nlh, 6147 u32 filter_mask, 6148 struct rtnl_stats_dump_filters *filters, 6149 struct netlink_ext_ack *extack) 6150 { 6151 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 6152 int err; 6153 int i; 6154 6155 filters->mask[0] = filter_mask; 6156 for (i = 1; i < ARRAY_SIZE(filters->mask); i++) 6157 filters->mask[i] = -1U; 6158 6159 err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb, 6160 IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack); 6161 if (err < 0) 6162 return err; 6163 6164 if (tb[IFLA_STATS_GET_FILTERS]) { 6165 err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS], 6166 filters, extack); 6167 if (err) 6168 return err; 6169 } 6170 6171 return 0; 6172 } 6173 6174 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 6175 bool is_dump, struct netlink_ext_ack *extack) 6176 { 6177 struct if_stats_msg *ifsm; 6178 6179 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 6180 NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 6181 return -EINVAL; 6182 } 6183 6184 if (!strict_check) 6185 return 0; 6186 6187 ifsm = nlmsg_data(nlh); 6188 6189 /* only requests using strict checks can pass data to influence 6190 * the dump. The legacy exception is filter_mask. 6191 */ 6192 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 6193 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 6194 return -EINVAL; 6195 } 6196 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 6197 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 6198 return -EINVAL; 6199 } 6200 6201 return 0; 6202 } 6203 6204 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 6205 struct netlink_ext_ack *extack) 6206 { 6207 struct rtnl_stats_dump_filters filters; 6208 struct net *net = sock_net(skb->sk); 6209 struct net_device *dev = NULL; 6210 int idxattr = 0, prividx = 0; 6211 struct if_stats_msg *ifsm; 6212 struct sk_buff *nskb; 6213 int err; 6214 6215 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 6216 false, extack); 6217 if (err) 6218 return err; 6219 6220 ifsm = nlmsg_data(nlh); 6221 if (ifsm->ifindex > 0) 6222 dev = __dev_get_by_index(net, ifsm->ifindex); 6223 else 6224 return -EINVAL; 6225 6226 if (!dev) 6227 return -ENODEV; 6228 6229 if (!ifsm->filter_mask) { 6230 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get"); 6231 return -EINVAL; 6232 } 6233 6234 err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack); 6235 if (err) 6236 return err; 6237 6238 nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL); 6239 if (!nskb) 6240 return -ENOBUFS; 6241 6242 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 6243 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 6244 0, &filters, &idxattr, &prividx, extack); 6245 if (err < 0) { 6246 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 6247 WARN_ON(err == -EMSGSIZE); 6248 kfree_skb(nskb); 6249 } else { 6250 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 6251 } 6252 6253 return err; 6254 } 6255 6256 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 6257 { 6258 struct netlink_ext_ack *extack = cb->extack; 6259 struct rtnl_stats_dump_filters filters; 6260 struct net *net = sock_net(skb->sk); 6261 unsigned int flags = NLM_F_MULTI; 6262 struct if_stats_msg *ifsm; 6263 struct { 6264 unsigned long ifindex; 6265 int idxattr; 6266 int prividx; 6267 } *ctx = (void *)cb->ctx; 6268 struct net_device *dev; 6269 int err; 6270 6271 cb->seq = net->dev_base_seq; 6272 6273 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 6274 if (err) 6275 return err; 6276 6277 ifsm = nlmsg_data(cb->nlh); 6278 if (!ifsm->filter_mask) { 6279 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 6280 return -EINVAL; 6281 } 6282 6283 err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters, 6284 extack); 6285 if (err) 6286 return err; 6287 6288 for_each_netdev_dump(net, dev, ctx->ifindex) { 6289 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 6290 NETLINK_CB(cb->skb).portid, 6291 cb->nlh->nlmsg_seq, 0, 6292 flags, &filters, 6293 &ctx->idxattr, &ctx->prividx, 6294 extack); 6295 /* If we ran out of room on the first message, 6296 * we're in trouble. 6297 */ 6298 WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 6299 6300 if (err < 0) 6301 break; 6302 ctx->prividx = 0; 6303 ctx->idxattr = 0; 6304 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 6305 } 6306 6307 return err; 6308 } 6309 6310 void rtnl_offload_xstats_notify(struct net_device *dev) 6311 { 6312 struct rtnl_stats_dump_filters response_filters = {}; 6313 struct net *net = dev_net(dev); 6314 int idxattr = 0, prividx = 0; 6315 struct sk_buff *skb; 6316 int err = -ENOBUFS; 6317 6318 ASSERT_RTNL(); 6319 6320 response_filters.mask[0] |= 6321 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 6322 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 6323 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 6324 6325 skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters), 6326 GFP_KERNEL); 6327 if (!skb) 6328 goto errout; 6329 6330 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0, 6331 &response_filters, &idxattr, &prividx, NULL); 6332 if (err < 0) { 6333 kfree_skb(skb); 6334 goto errout; 6335 } 6336 6337 rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL); 6338 return; 6339 6340 errout: 6341 rtnl_set_sk_err(net, RTNLGRP_STATS, err); 6342 } 6343 EXPORT_SYMBOL(rtnl_offload_xstats_notify); 6344 6345 static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh, 6346 struct netlink_ext_ack *extack) 6347 { 6348 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 6349 struct rtnl_stats_dump_filters response_filters = {}; 6350 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 6351 struct net *net = sock_net(skb->sk); 6352 struct net_device *dev = NULL; 6353 struct if_stats_msg *ifsm; 6354 bool notify = false; 6355 int err; 6356 6357 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 6358 false, extack); 6359 if (err) 6360 return err; 6361 6362 ifsm = nlmsg_data(nlh); 6363 if (ifsm->family != AF_UNSPEC) { 6364 NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC"); 6365 return -EINVAL; 6366 } 6367 6368 if (ifsm->ifindex > 0) 6369 dev = __dev_get_by_index(net, ifsm->ifindex); 6370 else 6371 return -EINVAL; 6372 6373 if (!dev) 6374 return -ENODEV; 6375 6376 if (ifsm->filter_mask) { 6377 NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set"); 6378 return -EINVAL; 6379 } 6380 6381 err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX, 6382 ifla_stats_set_policy, extack); 6383 if (err < 0) 6384 return err; 6385 6386 if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) { 6387 u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]); 6388 6389 if (req) 6390 err = netdev_offload_xstats_enable(dev, t_l3, extack); 6391 else 6392 err = netdev_offload_xstats_disable(dev, t_l3); 6393 6394 if (!err) 6395 notify = true; 6396 else if (err != -EALREADY) 6397 return err; 6398 6399 response_filters.mask[0] |= 6400 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 6401 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 6402 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 6403 } 6404 6405 if (notify) 6406 rtnl_offload_xstats_notify(dev); 6407 6408 return 0; 6409 } 6410 6411 static int rtnl_mdb_valid_dump_req(const struct nlmsghdr *nlh, 6412 struct netlink_ext_ack *extack) 6413 { 6414 struct br_port_msg *bpm; 6415 6416 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) { 6417 NL_SET_ERR_MSG(extack, "Invalid header for mdb dump request"); 6418 return -EINVAL; 6419 } 6420 6421 bpm = nlmsg_data(nlh); 6422 if (bpm->ifindex) { 6423 NL_SET_ERR_MSG(extack, "Filtering by device index is not supported for mdb dump request"); 6424 return -EINVAL; 6425 } 6426 if (nlmsg_attrlen(nlh, sizeof(*bpm))) { 6427 NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request"); 6428 return -EINVAL; 6429 } 6430 6431 return 0; 6432 } 6433 6434 struct rtnl_mdb_dump_ctx { 6435 long idx; 6436 }; 6437 6438 static int rtnl_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 6439 { 6440 struct rtnl_mdb_dump_ctx *ctx = (void *)cb->ctx; 6441 struct net *net = sock_net(skb->sk); 6442 struct net_device *dev; 6443 int idx, s_idx; 6444 int err; 6445 6446 NL_ASSERT_CTX_FITS(struct rtnl_mdb_dump_ctx); 6447 6448 if (cb->strict_check) { 6449 err = rtnl_mdb_valid_dump_req(cb->nlh, cb->extack); 6450 if (err) 6451 return err; 6452 } 6453 6454 s_idx = ctx->idx; 6455 idx = 0; 6456 6457 for_each_netdev(net, dev) { 6458 if (idx < s_idx) 6459 goto skip; 6460 if (!dev->netdev_ops->ndo_mdb_dump) 6461 goto skip; 6462 6463 err = dev->netdev_ops->ndo_mdb_dump(dev, skb, cb); 6464 if (err == -EMSGSIZE) 6465 goto out; 6466 /* Moving on to next device, reset markers and sequence 6467 * counters since they are all maintained per-device. 6468 */ 6469 memset(cb->ctx, 0, sizeof(cb->ctx)); 6470 cb->prev_seq = 0; 6471 cb->seq = 0; 6472 skip: 6473 idx++; 6474 } 6475 6476 out: 6477 ctx->idx = idx; 6478 return skb->len; 6479 } 6480 6481 static int rtnl_validate_mdb_entry_get(const struct nlattr *attr, 6482 struct netlink_ext_ack *extack) 6483 { 6484 struct br_mdb_entry *entry = nla_data(attr); 6485 6486 if (nla_len(attr) != sizeof(struct br_mdb_entry)) { 6487 NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length"); 6488 return -EINVAL; 6489 } 6490 6491 if (entry->ifindex) { 6492 NL_SET_ERR_MSG(extack, "Entry ifindex cannot be specified"); 6493 return -EINVAL; 6494 } 6495 6496 if (entry->state) { 6497 NL_SET_ERR_MSG(extack, "Entry state cannot be specified"); 6498 return -EINVAL; 6499 } 6500 6501 if (entry->flags) { 6502 NL_SET_ERR_MSG(extack, "Entry flags cannot be specified"); 6503 return -EINVAL; 6504 } 6505 6506 if (entry->vid >= VLAN_VID_MASK) { 6507 NL_SET_ERR_MSG(extack, "Invalid entry VLAN id"); 6508 return -EINVAL; 6509 } 6510 6511 if (entry->addr.proto != htons(ETH_P_IP) && 6512 entry->addr.proto != htons(ETH_P_IPV6) && 6513 entry->addr.proto != 0) { 6514 NL_SET_ERR_MSG(extack, "Unknown entry protocol"); 6515 return -EINVAL; 6516 } 6517 6518 return 0; 6519 } 6520 6521 static const struct nla_policy mdba_get_policy[MDBA_GET_ENTRY_MAX + 1] = { 6522 [MDBA_GET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY, 6523 rtnl_validate_mdb_entry_get, 6524 sizeof(struct br_mdb_entry)), 6525 [MDBA_GET_ENTRY_ATTRS] = { .type = NLA_NESTED }, 6526 }; 6527 6528 static int rtnl_mdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 6529 struct netlink_ext_ack *extack) 6530 { 6531 struct nlattr *tb[MDBA_GET_ENTRY_MAX + 1]; 6532 struct net *net = sock_net(in_skb->sk); 6533 struct br_port_msg *bpm; 6534 struct net_device *dev; 6535 int err; 6536 6537 err = nlmsg_parse(nlh, sizeof(struct br_port_msg), tb, 6538 MDBA_GET_ENTRY_MAX, mdba_get_policy, extack); 6539 if (err) 6540 return err; 6541 6542 bpm = nlmsg_data(nlh); 6543 if (!bpm->ifindex) { 6544 NL_SET_ERR_MSG(extack, "Invalid ifindex"); 6545 return -EINVAL; 6546 } 6547 6548 dev = __dev_get_by_index(net, bpm->ifindex); 6549 if (!dev) { 6550 NL_SET_ERR_MSG(extack, "Device doesn't exist"); 6551 return -ENODEV; 6552 } 6553 6554 if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_GET_ENTRY)) { 6555 NL_SET_ERR_MSG(extack, "Missing MDBA_GET_ENTRY attribute"); 6556 return -EINVAL; 6557 } 6558 6559 if (!dev->netdev_ops->ndo_mdb_get) { 6560 NL_SET_ERR_MSG(extack, "Device does not support MDB operations"); 6561 return -EOPNOTSUPP; 6562 } 6563 6564 return dev->netdev_ops->ndo_mdb_get(dev, tb, NETLINK_CB(in_skb).portid, 6565 nlh->nlmsg_seq, extack); 6566 } 6567 6568 static int rtnl_validate_mdb_entry(const struct nlattr *attr, 6569 struct netlink_ext_ack *extack) 6570 { 6571 struct br_mdb_entry *entry = nla_data(attr); 6572 6573 if (nla_len(attr) != sizeof(struct br_mdb_entry)) { 6574 NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length"); 6575 return -EINVAL; 6576 } 6577 6578 if (entry->ifindex == 0) { 6579 NL_SET_ERR_MSG(extack, "Zero entry ifindex is not allowed"); 6580 return -EINVAL; 6581 } 6582 6583 if (entry->addr.proto == htons(ETH_P_IP)) { 6584 if (!ipv4_is_multicast(entry->addr.u.ip4) && 6585 !ipv4_is_zeronet(entry->addr.u.ip4)) { 6586 NL_SET_ERR_MSG(extack, "IPv4 entry group address is not multicast or 0.0.0.0"); 6587 return -EINVAL; 6588 } 6589 if (ipv4_is_local_multicast(entry->addr.u.ip4)) { 6590 NL_SET_ERR_MSG(extack, "IPv4 entry group address is local multicast"); 6591 return -EINVAL; 6592 } 6593 #if IS_ENABLED(CONFIG_IPV6) 6594 } else if (entry->addr.proto == htons(ETH_P_IPV6)) { 6595 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) { 6596 NL_SET_ERR_MSG(extack, "IPv6 entry group address is link-local all nodes"); 6597 return -EINVAL; 6598 } 6599 #endif 6600 } else if (entry->addr.proto == 0) { 6601 /* L2 mdb */ 6602 if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) { 6603 NL_SET_ERR_MSG(extack, "L2 entry group is not multicast"); 6604 return -EINVAL; 6605 } 6606 } else { 6607 NL_SET_ERR_MSG(extack, "Unknown entry protocol"); 6608 return -EINVAL; 6609 } 6610 6611 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) { 6612 NL_SET_ERR_MSG(extack, "Unknown entry state"); 6613 return -EINVAL; 6614 } 6615 if (entry->vid >= VLAN_VID_MASK) { 6616 NL_SET_ERR_MSG(extack, "Invalid entry VLAN id"); 6617 return -EINVAL; 6618 } 6619 6620 return 0; 6621 } 6622 6623 static const struct nla_policy mdba_policy[MDBA_SET_ENTRY_MAX + 1] = { 6624 [MDBA_SET_ENTRY_UNSPEC] = { .strict_start_type = MDBA_SET_ENTRY_ATTRS + 1 }, 6625 [MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY, 6626 rtnl_validate_mdb_entry, 6627 sizeof(struct br_mdb_entry)), 6628 [MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED }, 6629 }; 6630 6631 static int rtnl_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 6632 struct netlink_ext_ack *extack) 6633 { 6634 struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1]; 6635 struct net *net = sock_net(skb->sk); 6636 struct br_port_msg *bpm; 6637 struct net_device *dev; 6638 int err; 6639 6640 err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb, 6641 MDBA_SET_ENTRY_MAX, mdba_policy, extack); 6642 if (err) 6643 return err; 6644 6645 bpm = nlmsg_data(nlh); 6646 if (!bpm->ifindex) { 6647 NL_SET_ERR_MSG(extack, "Invalid ifindex"); 6648 return -EINVAL; 6649 } 6650 6651 dev = __dev_get_by_index(net, bpm->ifindex); 6652 if (!dev) { 6653 NL_SET_ERR_MSG(extack, "Device doesn't exist"); 6654 return -ENODEV; 6655 } 6656 6657 if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) { 6658 NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute"); 6659 return -EINVAL; 6660 } 6661 6662 if (!dev->netdev_ops->ndo_mdb_add) { 6663 NL_SET_ERR_MSG(extack, "Device does not support MDB operations"); 6664 return -EOPNOTSUPP; 6665 } 6666 6667 return dev->netdev_ops->ndo_mdb_add(dev, tb, nlh->nlmsg_flags, extack); 6668 } 6669 6670 static int rtnl_validate_mdb_entry_del_bulk(const struct nlattr *attr, 6671 struct netlink_ext_ack *extack) 6672 { 6673 struct br_mdb_entry *entry = nla_data(attr); 6674 struct br_mdb_entry zero_entry = {}; 6675 6676 if (nla_len(attr) != sizeof(struct br_mdb_entry)) { 6677 NL_SET_ERR_MSG_ATTR(extack, attr, "Invalid attribute length"); 6678 return -EINVAL; 6679 } 6680 6681 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) { 6682 NL_SET_ERR_MSG(extack, "Unknown entry state"); 6683 return -EINVAL; 6684 } 6685 6686 if (entry->flags) { 6687 NL_SET_ERR_MSG(extack, "Entry flags cannot be set"); 6688 return -EINVAL; 6689 } 6690 6691 if (entry->vid >= VLAN_N_VID - 1) { 6692 NL_SET_ERR_MSG(extack, "Invalid entry VLAN id"); 6693 return -EINVAL; 6694 } 6695 6696 if (memcmp(&entry->addr, &zero_entry.addr, sizeof(entry->addr))) { 6697 NL_SET_ERR_MSG(extack, "Entry address cannot be set"); 6698 return -EINVAL; 6699 } 6700 6701 return 0; 6702 } 6703 6704 static const struct nla_policy mdba_del_bulk_policy[MDBA_SET_ENTRY_MAX + 1] = { 6705 [MDBA_SET_ENTRY] = NLA_POLICY_VALIDATE_FN(NLA_BINARY, 6706 rtnl_validate_mdb_entry_del_bulk, 6707 sizeof(struct br_mdb_entry)), 6708 [MDBA_SET_ENTRY_ATTRS] = { .type = NLA_NESTED }, 6709 }; 6710 6711 static int rtnl_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 6712 struct netlink_ext_ack *extack) 6713 { 6714 bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK); 6715 struct nlattr *tb[MDBA_SET_ENTRY_MAX + 1]; 6716 struct net *net = sock_net(skb->sk); 6717 struct br_port_msg *bpm; 6718 struct net_device *dev; 6719 int err; 6720 6721 if (!del_bulk) 6722 err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb, 6723 MDBA_SET_ENTRY_MAX, mdba_policy, 6724 extack); 6725 else 6726 err = nlmsg_parse(nlh, sizeof(*bpm), tb, MDBA_SET_ENTRY_MAX, 6727 mdba_del_bulk_policy, extack); 6728 if (err) 6729 return err; 6730 6731 bpm = nlmsg_data(nlh); 6732 if (!bpm->ifindex) { 6733 NL_SET_ERR_MSG(extack, "Invalid ifindex"); 6734 return -EINVAL; 6735 } 6736 6737 dev = __dev_get_by_index(net, bpm->ifindex); 6738 if (!dev) { 6739 NL_SET_ERR_MSG(extack, "Device doesn't exist"); 6740 return -ENODEV; 6741 } 6742 6743 if (NL_REQ_ATTR_CHECK(extack, NULL, tb, MDBA_SET_ENTRY)) { 6744 NL_SET_ERR_MSG(extack, "Missing MDBA_SET_ENTRY attribute"); 6745 return -EINVAL; 6746 } 6747 6748 if (del_bulk) { 6749 if (!dev->netdev_ops->ndo_mdb_del_bulk) { 6750 NL_SET_ERR_MSG(extack, "Device does not support MDB bulk deletion"); 6751 return -EOPNOTSUPP; 6752 } 6753 return dev->netdev_ops->ndo_mdb_del_bulk(dev, tb, extack); 6754 } 6755 6756 if (!dev->netdev_ops->ndo_mdb_del) { 6757 NL_SET_ERR_MSG(extack, "Device does not support MDB operations"); 6758 return -EOPNOTSUPP; 6759 } 6760 6761 return dev->netdev_ops->ndo_mdb_del(dev, tb, extack); 6762 } 6763 6764 /* Process one rtnetlink message. */ 6765 6766 static int rtnl_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 6767 { 6768 const bool needs_lock = !(cb->flags & RTNL_FLAG_DUMP_UNLOCKED); 6769 rtnl_dumpit_func dumpit = cb->data; 6770 int err; 6771 6772 /* Previous iteration have already finished, avoid calling->dumpit() 6773 * again, it may not expect to be called after it reached the end. 6774 */ 6775 if (!dumpit) 6776 return 0; 6777 6778 if (needs_lock) 6779 rtnl_lock(); 6780 err = dumpit(skb, cb); 6781 if (needs_lock) 6782 rtnl_unlock(); 6783 6784 /* Old dump handlers used to send NLM_DONE as in a separate recvmsg(). 6785 * Some applications which parse netlink manually depend on this. 6786 */ 6787 if (cb->flags & RTNL_FLAG_DUMP_SPLIT_NLM_DONE) { 6788 if (err < 0 && err != -EMSGSIZE) 6789 return err; 6790 if (!err) 6791 cb->data = NULL; 6792 6793 return skb->len; 6794 } 6795 return err; 6796 } 6797 6798 static int rtnetlink_dump_start(struct sock *ssk, struct sk_buff *skb, 6799 const struct nlmsghdr *nlh, 6800 struct netlink_dump_control *control) 6801 { 6802 if (control->flags & RTNL_FLAG_DUMP_SPLIT_NLM_DONE || 6803 !(control->flags & RTNL_FLAG_DUMP_UNLOCKED)) { 6804 WARN_ON(control->data); 6805 control->data = control->dump; 6806 control->dump = rtnl_dumpit; 6807 } 6808 6809 return netlink_dump_start(ssk, skb, nlh, control); 6810 } 6811 6812 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 6813 struct netlink_ext_ack *extack) 6814 { 6815 struct net *net = sock_net(skb->sk); 6816 struct rtnl_link *link; 6817 enum rtnl_kinds kind; 6818 struct module *owner; 6819 int err = -EOPNOTSUPP; 6820 rtnl_doit_func doit; 6821 unsigned int flags; 6822 int family; 6823 int type; 6824 6825 type = nlh->nlmsg_type; 6826 if (type > RTM_MAX) 6827 return -EOPNOTSUPP; 6828 6829 type -= RTM_BASE; 6830 6831 /* All the messages must have at least 1 byte length */ 6832 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 6833 return 0; 6834 6835 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 6836 kind = rtnl_msgtype_kind(type); 6837 6838 if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN)) 6839 return -EPERM; 6840 6841 rcu_read_lock(); 6842 if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) { 6843 struct sock *rtnl; 6844 rtnl_dumpit_func dumpit; 6845 u32 min_dump_alloc = 0; 6846 6847 link = rtnl_get_link(family, type); 6848 if (!link || !link->dumpit) { 6849 family = PF_UNSPEC; 6850 link = rtnl_get_link(family, type); 6851 if (!link || !link->dumpit) 6852 goto err_unlock; 6853 } 6854 owner = link->owner; 6855 dumpit = link->dumpit; 6856 flags = link->flags; 6857 6858 if (type == RTM_GETLINK - RTM_BASE) 6859 min_dump_alloc = rtnl_calcit(skb, nlh); 6860 6861 err = 0; 6862 /* need to do this before rcu_read_unlock() */ 6863 if (!try_module_get(owner)) 6864 err = -EPROTONOSUPPORT; 6865 6866 rcu_read_unlock(); 6867 6868 rtnl = net->rtnl; 6869 if (err == 0) { 6870 struct netlink_dump_control c = { 6871 .dump = dumpit, 6872 .min_dump_alloc = min_dump_alloc, 6873 .module = owner, 6874 .flags = flags, 6875 }; 6876 err = rtnetlink_dump_start(rtnl, skb, nlh, &c); 6877 /* netlink_dump_start() will keep a reference on 6878 * module if dump is still in progress. 6879 */ 6880 module_put(owner); 6881 } 6882 return err; 6883 } 6884 6885 link = rtnl_get_link(family, type); 6886 if (!link || !link->doit) { 6887 family = PF_UNSPEC; 6888 link = rtnl_get_link(PF_UNSPEC, type); 6889 if (!link || !link->doit) 6890 goto out_unlock; 6891 } 6892 6893 owner = link->owner; 6894 if (!try_module_get(owner)) { 6895 err = -EPROTONOSUPPORT; 6896 goto out_unlock; 6897 } 6898 6899 flags = link->flags; 6900 if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) && 6901 !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) { 6902 NL_SET_ERR_MSG(extack, "Bulk delete is not supported"); 6903 module_put(owner); 6904 goto err_unlock; 6905 } 6906 6907 if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 6908 doit = link->doit; 6909 rcu_read_unlock(); 6910 if (doit) 6911 err = doit(skb, nlh, extack); 6912 module_put(owner); 6913 return err; 6914 } 6915 rcu_read_unlock(); 6916 6917 rtnl_lock(); 6918 link = rtnl_get_link(family, type); 6919 if (link && link->doit) 6920 err = link->doit(skb, nlh, extack); 6921 rtnl_unlock(); 6922 6923 module_put(owner); 6924 6925 return err; 6926 6927 out_unlock: 6928 rcu_read_unlock(); 6929 return err; 6930 6931 err_unlock: 6932 rcu_read_unlock(); 6933 return -EOPNOTSUPP; 6934 } 6935 6936 static void rtnetlink_rcv(struct sk_buff *skb) 6937 { 6938 netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 6939 } 6940 6941 static int rtnetlink_bind(struct net *net, int group) 6942 { 6943 switch (group) { 6944 case RTNLGRP_IPV4_MROUTE_R: 6945 case RTNLGRP_IPV6_MROUTE_R: 6946 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 6947 return -EPERM; 6948 break; 6949 } 6950 return 0; 6951 } 6952 6953 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 6954 { 6955 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6956 6957 switch (event) { 6958 case NETDEV_REBOOT: 6959 case NETDEV_CHANGEMTU: 6960 case NETDEV_CHANGEADDR: 6961 case NETDEV_CHANGENAME: 6962 case NETDEV_FEAT_CHANGE: 6963 case NETDEV_BONDING_FAILOVER: 6964 case NETDEV_POST_TYPE_CHANGE: 6965 case NETDEV_NOTIFY_PEERS: 6966 case NETDEV_CHANGEUPPER: 6967 case NETDEV_RESEND_IGMP: 6968 case NETDEV_CHANGEINFODATA: 6969 case NETDEV_CHANGELOWERSTATE: 6970 case NETDEV_CHANGE_TX_QUEUE_LEN: 6971 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 6972 GFP_KERNEL, NULL, 0, 0, NULL); 6973 break; 6974 default: 6975 break; 6976 } 6977 return NOTIFY_DONE; 6978 } 6979 6980 static struct notifier_block rtnetlink_dev_notifier = { 6981 .notifier_call = rtnetlink_event, 6982 }; 6983 6984 6985 static int __net_init rtnetlink_net_init(struct net *net) 6986 { 6987 struct sock *sk; 6988 struct netlink_kernel_cfg cfg = { 6989 .groups = RTNLGRP_MAX, 6990 .input = rtnetlink_rcv, 6991 .flags = NL_CFG_F_NONROOT_RECV, 6992 .bind = rtnetlink_bind, 6993 }; 6994 6995 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 6996 if (!sk) 6997 return -ENOMEM; 6998 net->rtnl = sk; 6999 return 0; 7000 } 7001 7002 static void __net_exit rtnetlink_net_exit(struct net *net) 7003 { 7004 netlink_kernel_release(net->rtnl); 7005 net->rtnl = NULL; 7006 } 7007 7008 static struct pernet_operations rtnetlink_net_ops = { 7009 .init = rtnetlink_net_init, 7010 .exit = rtnetlink_net_exit, 7011 }; 7012 7013 static const struct rtnl_msg_handler rtnetlink_rtnl_msg_handlers[] __initconst = { 7014 {.msgtype = RTM_NEWLINK, .doit = rtnl_newlink, 7015 .flags = RTNL_FLAG_DOIT_PERNET}, 7016 {.msgtype = RTM_DELLINK, .doit = rtnl_dellink, 7017 .flags = RTNL_FLAG_DOIT_PERNET_WIP}, 7018 {.msgtype = RTM_GETLINK, .doit = rtnl_getlink, 7019 .dumpit = rtnl_dump_ifinfo, .flags = RTNL_FLAG_DUMP_SPLIT_NLM_DONE}, 7020 {.msgtype = RTM_SETLINK, .doit = rtnl_setlink, 7021 .flags = RTNL_FLAG_DOIT_PERNET_WIP}, 7022 {.msgtype = RTM_GETADDR, .dumpit = rtnl_dump_all}, 7023 {.msgtype = RTM_GETROUTE, .dumpit = rtnl_dump_all}, 7024 {.msgtype = RTM_GETNETCONF, .dumpit = rtnl_dump_all}, 7025 {.msgtype = RTM_GETSTATS, .doit = rtnl_stats_get, 7026 .dumpit = rtnl_stats_dump}, 7027 {.msgtype = RTM_SETSTATS, .doit = rtnl_stats_set}, 7028 {.msgtype = RTM_NEWLINKPROP, .doit = rtnl_newlinkprop}, 7029 {.msgtype = RTM_DELLINKPROP, .doit = rtnl_dellinkprop}, 7030 {.protocol = PF_BRIDGE, .msgtype = RTM_GETLINK, 7031 .dumpit = rtnl_bridge_getlink}, 7032 {.protocol = PF_BRIDGE, .msgtype = RTM_DELLINK, 7033 .doit = rtnl_bridge_dellink}, 7034 {.protocol = PF_BRIDGE, .msgtype = RTM_SETLINK, 7035 .doit = rtnl_bridge_setlink}, 7036 {.protocol = PF_BRIDGE, .msgtype = RTM_NEWNEIGH, .doit = rtnl_fdb_add}, 7037 {.protocol = PF_BRIDGE, .msgtype = RTM_DELNEIGH, .doit = rtnl_fdb_del, 7038 .flags = RTNL_FLAG_BULK_DEL_SUPPORTED}, 7039 {.protocol = PF_BRIDGE, .msgtype = RTM_GETNEIGH, .doit = rtnl_fdb_get, 7040 .dumpit = rtnl_fdb_dump}, 7041 {.protocol = PF_BRIDGE, .msgtype = RTM_NEWMDB, .doit = rtnl_mdb_add}, 7042 {.protocol = PF_BRIDGE, .msgtype = RTM_DELMDB, .doit = rtnl_mdb_del, 7043 .flags = RTNL_FLAG_BULK_DEL_SUPPORTED}, 7044 {.protocol = PF_BRIDGE, .msgtype = RTM_GETMDB, .doit = rtnl_mdb_get, 7045 .dumpit = rtnl_mdb_dump}, 7046 }; 7047 7048 void __init rtnetlink_init(void) 7049 { 7050 if (register_pernet_subsys(&rtnetlink_net_ops)) 7051 panic("rtnetlink_init: cannot initialize rtnetlink\n"); 7052 7053 register_netdevice_notifier(&rtnetlink_dev_notifier); 7054 7055 rtnl_register_many(rtnetlink_rtnl_msg_handlers); 7056 } 7057