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