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