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