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