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