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