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 (tb[NDA_FLAGS_EXT]) { 4055 netdev_info(dev, "invalid flags given to default FDB implementation\n"); 4056 return err; 4057 } 4058 4059 if (vid) { 4060 netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n"); 4061 return err; 4062 } 4063 4064 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4065 err = dev_uc_add_excl(dev, addr); 4066 else if (is_multicast_ether_addr(addr)) 4067 err = dev_mc_add_excl(dev, addr); 4068 4069 /* Only return duplicate errors if NLM_F_EXCL is set */ 4070 if (err == -EEXIST && !(flags & NLM_F_EXCL)) 4071 err = 0; 4072 4073 return err; 4074 } 4075 EXPORT_SYMBOL(ndo_dflt_fdb_add); 4076 4077 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 4078 struct netlink_ext_ack *extack) 4079 { 4080 u16 vid = 0; 4081 4082 if (vlan_attr) { 4083 if (nla_len(vlan_attr) != sizeof(u16)) { 4084 NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 4085 return -EINVAL; 4086 } 4087 4088 vid = nla_get_u16(vlan_attr); 4089 4090 if (!vid || vid >= VLAN_VID_MASK) { 4091 NL_SET_ERR_MSG(extack, "invalid vlan id"); 4092 return -EINVAL; 4093 } 4094 } 4095 *p_vid = vid; 4096 return 0; 4097 } 4098 4099 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 4100 struct netlink_ext_ack *extack) 4101 { 4102 struct net *net = sock_net(skb->sk); 4103 struct ndmsg *ndm; 4104 struct nlattr *tb[NDA_MAX+1]; 4105 struct net_device *dev; 4106 u8 *addr; 4107 u16 vid; 4108 int err; 4109 4110 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 4111 extack); 4112 if (err < 0) 4113 return err; 4114 4115 ndm = nlmsg_data(nlh); 4116 if (ndm->ndm_ifindex == 0) { 4117 NL_SET_ERR_MSG(extack, "invalid ifindex"); 4118 return -EINVAL; 4119 } 4120 4121 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 4122 if (dev == NULL) { 4123 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4124 return -ENODEV; 4125 } 4126 4127 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4128 NL_SET_ERR_MSG(extack, "invalid address"); 4129 return -EINVAL; 4130 } 4131 4132 if (dev->type != ARPHRD_ETHER) { 4133 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 4134 return -EINVAL; 4135 } 4136 4137 addr = nla_data(tb[NDA_LLADDR]); 4138 4139 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4140 if (err) 4141 return err; 4142 4143 err = -EOPNOTSUPP; 4144 4145 /* Support fdb on master device the net/bridge default case */ 4146 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 4147 netif_is_bridge_port(dev)) { 4148 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4149 const struct net_device_ops *ops = br_dev->netdev_ops; 4150 4151 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 4152 nlh->nlmsg_flags, extack); 4153 if (err) 4154 goto out; 4155 else 4156 ndm->ndm_flags &= ~NTF_MASTER; 4157 } 4158 4159 /* Embedded bridge, macvlan, and any other device support */ 4160 if ((ndm->ndm_flags & NTF_SELF)) { 4161 if (dev->netdev_ops->ndo_fdb_add) 4162 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 4163 vid, 4164 nlh->nlmsg_flags, 4165 extack); 4166 else 4167 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 4168 nlh->nlmsg_flags); 4169 4170 if (!err) { 4171 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 4172 ndm->ndm_state); 4173 ndm->ndm_flags &= ~NTF_SELF; 4174 } 4175 } 4176 out: 4177 return err; 4178 } 4179 4180 /* 4181 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 4182 */ 4183 int ndo_dflt_fdb_del(struct ndmsg *ndm, 4184 struct nlattr *tb[], 4185 struct net_device *dev, 4186 const unsigned char *addr, u16 vid) 4187 { 4188 int err = -EINVAL; 4189 4190 /* If aging addresses are supported device will need to 4191 * implement its own handler for this. 4192 */ 4193 if (!(ndm->ndm_state & NUD_PERMANENT)) { 4194 netdev_info(dev, "default FDB implementation only supports local addresses\n"); 4195 return err; 4196 } 4197 4198 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 4199 err = dev_uc_del(dev, addr); 4200 else if (is_multicast_ether_addr(addr)) 4201 err = dev_mc_del(dev, addr); 4202 4203 return err; 4204 } 4205 EXPORT_SYMBOL(ndo_dflt_fdb_del); 4206 4207 static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = { 4208 [NDA_VLAN] = { .type = NLA_U16 }, 4209 [NDA_IFINDEX] = NLA_POLICY_MIN(NLA_S32, 1), 4210 [NDA_NDM_STATE_MASK] = { .type = NLA_U16 }, 4211 [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 }, 4212 }; 4213 4214 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 4215 struct netlink_ext_ack *extack) 4216 { 4217 bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK); 4218 struct net *net = sock_net(skb->sk); 4219 const struct net_device_ops *ops; 4220 struct ndmsg *ndm; 4221 struct nlattr *tb[NDA_MAX+1]; 4222 struct net_device *dev; 4223 __u8 *addr = NULL; 4224 int err; 4225 u16 vid; 4226 4227 if (!netlink_capable(skb, CAP_NET_ADMIN)) 4228 return -EPERM; 4229 4230 if (!del_bulk) { 4231 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, 4232 NULL, extack); 4233 } else { 4234 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, 4235 fdb_del_bulk_policy, extack); 4236 } 4237 if (err < 0) 4238 return err; 4239 4240 ndm = nlmsg_data(nlh); 4241 if (ndm->ndm_ifindex == 0) { 4242 NL_SET_ERR_MSG(extack, "invalid ifindex"); 4243 return -EINVAL; 4244 } 4245 4246 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 4247 if (dev == NULL) { 4248 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4249 return -ENODEV; 4250 } 4251 4252 if (!del_bulk) { 4253 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 4254 NL_SET_ERR_MSG(extack, "invalid address"); 4255 return -EINVAL; 4256 } 4257 addr = nla_data(tb[NDA_LLADDR]); 4258 } 4259 4260 if (dev->type != ARPHRD_ETHER) { 4261 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 4262 return -EINVAL; 4263 } 4264 4265 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 4266 if (err) 4267 return err; 4268 4269 err = -EOPNOTSUPP; 4270 4271 /* Support fdb on master device the net/bridge default case */ 4272 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 4273 netif_is_bridge_port(dev)) { 4274 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4275 4276 ops = br_dev->netdev_ops; 4277 if (!del_bulk) { 4278 if (ops->ndo_fdb_del) 4279 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 4280 } else { 4281 if (ops->ndo_fdb_del_bulk) 4282 err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 4283 extack); 4284 } 4285 4286 if (err) 4287 goto out; 4288 else 4289 ndm->ndm_flags &= ~NTF_MASTER; 4290 } 4291 4292 /* Embedded bridge, macvlan, and any other device support */ 4293 if (ndm->ndm_flags & NTF_SELF) { 4294 ops = dev->netdev_ops; 4295 if (!del_bulk) { 4296 if (ops->ndo_fdb_del) 4297 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack); 4298 else 4299 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 4300 } else { 4301 /* in case err was cleared by NTF_MASTER call */ 4302 err = -EOPNOTSUPP; 4303 if (ops->ndo_fdb_del_bulk) 4304 err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid, 4305 extack); 4306 } 4307 4308 if (!err) { 4309 if (!del_bulk) 4310 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 4311 ndm->ndm_state); 4312 ndm->ndm_flags &= ~NTF_SELF; 4313 } 4314 } 4315 out: 4316 return err; 4317 } 4318 4319 static int nlmsg_populate_fdb(struct sk_buff *skb, 4320 struct netlink_callback *cb, 4321 struct net_device *dev, 4322 int *idx, 4323 struct netdev_hw_addr_list *list) 4324 { 4325 struct netdev_hw_addr *ha; 4326 int err; 4327 u32 portid, seq; 4328 4329 portid = NETLINK_CB(cb->skb).portid; 4330 seq = cb->nlh->nlmsg_seq; 4331 4332 list_for_each_entry(ha, &list->list, list) { 4333 if (*idx < cb->args[2]) 4334 goto skip; 4335 4336 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 4337 portid, seq, 4338 RTM_NEWNEIGH, NTF_SELF, 4339 NLM_F_MULTI, NUD_PERMANENT); 4340 if (err < 0) 4341 return err; 4342 skip: 4343 *idx += 1; 4344 } 4345 return 0; 4346 } 4347 4348 /** 4349 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 4350 * @skb: socket buffer to store message in 4351 * @cb: netlink callback 4352 * @dev: netdevice 4353 * @filter_dev: ignored 4354 * @idx: the number of FDB table entries dumped is added to *@idx 4355 * 4356 * Default netdevice operation to dump the existing unicast address list. 4357 * Returns number of addresses from list put in skb. 4358 */ 4359 int ndo_dflt_fdb_dump(struct sk_buff *skb, 4360 struct netlink_callback *cb, 4361 struct net_device *dev, 4362 struct net_device *filter_dev, 4363 int *idx) 4364 { 4365 int err; 4366 4367 if (dev->type != ARPHRD_ETHER) 4368 return -EINVAL; 4369 4370 netif_addr_lock_bh(dev); 4371 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 4372 if (err) 4373 goto out; 4374 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 4375 out: 4376 netif_addr_unlock_bh(dev); 4377 return err; 4378 } 4379 EXPORT_SYMBOL(ndo_dflt_fdb_dump); 4380 4381 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 4382 int *br_idx, int *brport_idx, 4383 struct netlink_ext_ack *extack) 4384 { 4385 struct nlattr *tb[NDA_MAX + 1]; 4386 struct ndmsg *ndm; 4387 int err, i; 4388 4389 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 4390 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 4391 return -EINVAL; 4392 } 4393 4394 ndm = nlmsg_data(nlh); 4395 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 4396 ndm->ndm_flags || ndm->ndm_type) { 4397 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request"); 4398 return -EINVAL; 4399 } 4400 4401 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 4402 NDA_MAX, NULL, extack); 4403 if (err < 0) 4404 return err; 4405 4406 *brport_idx = ndm->ndm_ifindex; 4407 for (i = 0; i <= NDA_MAX; ++i) { 4408 if (!tb[i]) 4409 continue; 4410 4411 switch (i) { 4412 case NDA_IFINDEX: 4413 if (nla_len(tb[i]) != sizeof(u32)) { 4414 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 4415 return -EINVAL; 4416 } 4417 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 4418 break; 4419 case NDA_MASTER: 4420 if (nla_len(tb[i]) != sizeof(u32)) { 4421 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 4422 return -EINVAL; 4423 } 4424 *br_idx = nla_get_u32(tb[NDA_MASTER]); 4425 break; 4426 default: 4427 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 4428 return -EINVAL; 4429 } 4430 } 4431 4432 return 0; 4433 } 4434 4435 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 4436 int *br_idx, int *brport_idx, 4437 struct netlink_ext_ack *extack) 4438 { 4439 struct nlattr *tb[IFLA_MAX+1]; 4440 int err; 4441 4442 /* A hack to preserve kernel<->userspace interface. 4443 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 4444 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 4445 * So, check for ndmsg with an optional u32 attribute (not used here). 4446 * Fortunately these sizes don't conflict with the size of ifinfomsg 4447 * with an optional attribute. 4448 */ 4449 if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 4450 (nlmsg_len(nlh) != sizeof(struct ndmsg) + 4451 nla_attr_size(sizeof(u32)))) { 4452 struct ifinfomsg *ifm; 4453 4454 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 4455 tb, IFLA_MAX, ifla_policy, 4456 extack); 4457 if (err < 0) { 4458 return -EINVAL; 4459 } else if (err == 0) { 4460 if (tb[IFLA_MASTER]) 4461 *br_idx = nla_get_u32(tb[IFLA_MASTER]); 4462 } 4463 4464 ifm = nlmsg_data(nlh); 4465 *brport_idx = ifm->ifi_index; 4466 } 4467 return 0; 4468 } 4469 4470 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 4471 { 4472 struct net_device *dev; 4473 struct net_device *br_dev = NULL; 4474 const struct net_device_ops *ops = NULL; 4475 const struct net_device_ops *cops = NULL; 4476 struct net *net = sock_net(skb->sk); 4477 struct hlist_head *head; 4478 int brport_idx = 0; 4479 int br_idx = 0; 4480 int h, s_h; 4481 int idx = 0, s_idx; 4482 int err = 0; 4483 int fidx = 0; 4484 4485 if (cb->strict_check) 4486 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 4487 cb->extack); 4488 else 4489 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 4490 cb->extack); 4491 if (err < 0) 4492 return err; 4493 4494 if (br_idx) { 4495 br_dev = __dev_get_by_index(net, br_idx); 4496 if (!br_dev) 4497 return -ENODEV; 4498 4499 ops = br_dev->netdev_ops; 4500 } 4501 4502 s_h = cb->args[0]; 4503 s_idx = cb->args[1]; 4504 4505 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4506 idx = 0; 4507 head = &net->dev_index_head[h]; 4508 hlist_for_each_entry(dev, head, index_hlist) { 4509 4510 if (brport_idx && (dev->ifindex != brport_idx)) 4511 continue; 4512 4513 if (!br_idx) { /* user did not specify a specific bridge */ 4514 if (netif_is_bridge_port(dev)) { 4515 br_dev = netdev_master_upper_dev_get(dev); 4516 cops = br_dev->netdev_ops; 4517 } 4518 } else { 4519 if (dev != br_dev && 4520 !netif_is_bridge_port(dev)) 4521 continue; 4522 4523 if (br_dev != netdev_master_upper_dev_get(dev) && 4524 !netif_is_bridge_master(dev)) 4525 continue; 4526 cops = ops; 4527 } 4528 4529 if (idx < s_idx) 4530 goto cont; 4531 4532 if (netif_is_bridge_port(dev)) { 4533 if (cops && cops->ndo_fdb_dump) { 4534 err = cops->ndo_fdb_dump(skb, cb, 4535 br_dev, dev, 4536 &fidx); 4537 if (err == -EMSGSIZE) 4538 goto out; 4539 } 4540 } 4541 4542 if (dev->netdev_ops->ndo_fdb_dump) 4543 err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4544 dev, NULL, 4545 &fidx); 4546 else 4547 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4548 &fidx); 4549 if (err == -EMSGSIZE) 4550 goto out; 4551 4552 cops = NULL; 4553 4554 /* reset fdb offset to 0 for rest of the interfaces */ 4555 cb->args[2] = 0; 4556 fidx = 0; 4557 cont: 4558 idx++; 4559 } 4560 } 4561 4562 out: 4563 cb->args[0] = h; 4564 cb->args[1] = idx; 4565 cb->args[2] = fidx; 4566 4567 return skb->len; 4568 } 4569 4570 static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 4571 struct nlattr **tb, u8 *ndm_flags, 4572 int *br_idx, int *brport_idx, u8 **addr, 4573 u16 *vid, struct netlink_ext_ack *extack) 4574 { 4575 struct ndmsg *ndm; 4576 int err, i; 4577 4578 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 4579 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 4580 return -EINVAL; 4581 } 4582 4583 ndm = nlmsg_data(nlh); 4584 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 4585 ndm->ndm_type) { 4586 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 4587 return -EINVAL; 4588 } 4589 4590 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 4591 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 4592 return -EINVAL; 4593 } 4594 4595 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 4596 NDA_MAX, nda_policy, extack); 4597 if (err < 0) 4598 return err; 4599 4600 *ndm_flags = ndm->ndm_flags; 4601 *brport_idx = ndm->ndm_ifindex; 4602 for (i = 0; i <= NDA_MAX; ++i) { 4603 if (!tb[i]) 4604 continue; 4605 4606 switch (i) { 4607 case NDA_MASTER: 4608 *br_idx = nla_get_u32(tb[i]); 4609 break; 4610 case NDA_LLADDR: 4611 if (nla_len(tb[i]) != ETH_ALEN) { 4612 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 4613 return -EINVAL; 4614 } 4615 *addr = nla_data(tb[i]); 4616 break; 4617 case NDA_VLAN: 4618 err = fdb_vid_parse(tb[i], vid, extack); 4619 if (err) 4620 return err; 4621 break; 4622 case NDA_VNI: 4623 break; 4624 default: 4625 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 4626 return -EINVAL; 4627 } 4628 } 4629 4630 return 0; 4631 } 4632 4633 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4634 struct netlink_ext_ack *extack) 4635 { 4636 struct net_device *dev = NULL, *br_dev = NULL; 4637 const struct net_device_ops *ops = NULL; 4638 struct net *net = sock_net(in_skb->sk); 4639 struct nlattr *tb[NDA_MAX + 1]; 4640 struct sk_buff *skb; 4641 int brport_idx = 0; 4642 u8 ndm_flags = 0; 4643 int br_idx = 0; 4644 u8 *addr = NULL; 4645 u16 vid = 0; 4646 int err; 4647 4648 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 4649 &brport_idx, &addr, &vid, extack); 4650 if (err < 0) 4651 return err; 4652 4653 if (!addr) { 4654 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4655 return -EINVAL; 4656 } 4657 4658 if (brport_idx) { 4659 dev = __dev_get_by_index(net, brport_idx); 4660 if (!dev) { 4661 NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 4662 return -ENODEV; 4663 } 4664 } 4665 4666 if (br_idx) { 4667 if (dev) { 4668 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 4669 return -EINVAL; 4670 } 4671 4672 br_dev = __dev_get_by_index(net, br_idx); 4673 if (!br_dev) { 4674 NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 4675 return -EINVAL; 4676 } 4677 ops = br_dev->netdev_ops; 4678 } 4679 4680 if (dev) { 4681 if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 4682 if (!netif_is_bridge_port(dev)) { 4683 NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 4684 return -EINVAL; 4685 } 4686 br_dev = netdev_master_upper_dev_get(dev); 4687 if (!br_dev) { 4688 NL_SET_ERR_MSG(extack, "Master of device not found"); 4689 return -EINVAL; 4690 } 4691 ops = br_dev->netdev_ops; 4692 } else { 4693 if (!(ndm_flags & NTF_SELF)) { 4694 NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 4695 return -EINVAL; 4696 } 4697 ops = dev->netdev_ops; 4698 } 4699 } 4700 4701 if (!br_dev && !dev) { 4702 NL_SET_ERR_MSG(extack, "No device specified"); 4703 return -ENODEV; 4704 } 4705 4706 if (!ops || !ops->ndo_fdb_get) { 4707 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 4708 return -EOPNOTSUPP; 4709 } 4710 4711 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 4712 if (!skb) 4713 return -ENOBUFS; 4714 4715 if (br_dev) 4716 dev = br_dev; 4717 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 4718 NETLINK_CB(in_skb).portid, 4719 nlh->nlmsg_seq, extack); 4720 if (err) 4721 goto out; 4722 4723 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4724 out: 4725 kfree_skb(skb); 4726 return err; 4727 } 4728 4729 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 4730 unsigned int attrnum, unsigned int flag) 4731 { 4732 if (mask & flag) 4733 return nla_put_u8(skb, attrnum, !!(flags & flag)); 4734 return 0; 4735 } 4736 4737 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 4738 struct net_device *dev, u16 mode, 4739 u32 flags, u32 mask, int nlflags, 4740 u32 filter_mask, 4741 int (*vlan_fill)(struct sk_buff *skb, 4742 struct net_device *dev, 4743 u32 filter_mask)) 4744 { 4745 struct nlmsghdr *nlh; 4746 struct ifinfomsg *ifm; 4747 struct nlattr *br_afspec; 4748 struct nlattr *protinfo; 4749 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4750 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4751 int err = 0; 4752 4753 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4754 if (nlh == NULL) 4755 return -EMSGSIZE; 4756 4757 ifm = nlmsg_data(nlh); 4758 ifm->ifi_family = AF_BRIDGE; 4759 ifm->__ifi_pad = 0; 4760 ifm->ifi_type = dev->type; 4761 ifm->ifi_index = dev->ifindex; 4762 ifm->ifi_flags = dev_get_flags(dev); 4763 ifm->ifi_change = 0; 4764 4765 4766 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4767 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4768 nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4769 (br_dev && 4770 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4771 (dev->addr_len && 4772 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4773 (dev->ifindex != dev_get_iflink(dev) && 4774 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4775 goto nla_put_failure; 4776 4777 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4778 if (!br_afspec) 4779 goto nla_put_failure; 4780 4781 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4782 nla_nest_cancel(skb, br_afspec); 4783 goto nla_put_failure; 4784 } 4785 4786 if (mode != BRIDGE_MODE_UNDEF) { 4787 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 4788 nla_nest_cancel(skb, br_afspec); 4789 goto nla_put_failure; 4790 } 4791 } 4792 if (vlan_fill) { 4793 err = vlan_fill(skb, dev, filter_mask); 4794 if (err) { 4795 nla_nest_cancel(skb, br_afspec); 4796 goto nla_put_failure; 4797 } 4798 } 4799 nla_nest_end(skb, br_afspec); 4800 4801 protinfo = nla_nest_start(skb, IFLA_PROTINFO); 4802 if (!protinfo) 4803 goto nla_put_failure; 4804 4805 if (brport_nla_put_flag(skb, flags, mask, 4806 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 4807 brport_nla_put_flag(skb, flags, mask, 4808 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 4809 brport_nla_put_flag(skb, flags, mask, 4810 IFLA_BRPORT_FAST_LEAVE, 4811 BR_MULTICAST_FAST_LEAVE) || 4812 brport_nla_put_flag(skb, flags, mask, 4813 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 4814 brport_nla_put_flag(skb, flags, mask, 4815 IFLA_BRPORT_LEARNING, BR_LEARNING) || 4816 brport_nla_put_flag(skb, flags, mask, 4817 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 4818 brport_nla_put_flag(skb, flags, mask, 4819 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 4820 brport_nla_put_flag(skb, flags, mask, 4821 IFLA_BRPORT_PROXYARP, BR_PROXYARP) || 4822 brport_nla_put_flag(skb, flags, mask, 4823 IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) || 4824 brport_nla_put_flag(skb, flags, mask, 4825 IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) { 4826 nla_nest_cancel(skb, protinfo); 4827 goto nla_put_failure; 4828 } 4829 4830 nla_nest_end(skb, protinfo); 4831 4832 nlmsg_end(skb, nlh); 4833 return 0; 4834 nla_put_failure: 4835 nlmsg_cancel(skb, nlh); 4836 return err ? err : -EMSGSIZE; 4837 } 4838 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4839 4840 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 4841 bool strict_check, u32 *filter_mask, 4842 struct netlink_ext_ack *extack) 4843 { 4844 struct nlattr *tb[IFLA_MAX+1]; 4845 int err, i; 4846 4847 if (strict_check) { 4848 struct ifinfomsg *ifm; 4849 4850 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 4851 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 4852 return -EINVAL; 4853 } 4854 4855 ifm = nlmsg_data(nlh); 4856 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 4857 ifm->ifi_change || ifm->ifi_index) { 4858 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 4859 return -EINVAL; 4860 } 4861 4862 err = nlmsg_parse_deprecated_strict(nlh, 4863 sizeof(struct ifinfomsg), 4864 tb, IFLA_MAX, ifla_policy, 4865 extack); 4866 } else { 4867 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 4868 tb, IFLA_MAX, ifla_policy, 4869 extack); 4870 } 4871 if (err < 0) 4872 return err; 4873 4874 /* new attributes should only be added with strict checking */ 4875 for (i = 0; i <= IFLA_MAX; ++i) { 4876 if (!tb[i]) 4877 continue; 4878 4879 switch (i) { 4880 case IFLA_EXT_MASK: 4881 *filter_mask = nla_get_u32(tb[i]); 4882 break; 4883 default: 4884 if (strict_check) { 4885 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 4886 return -EINVAL; 4887 } 4888 } 4889 } 4890 4891 return 0; 4892 } 4893 4894 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4895 { 4896 const struct nlmsghdr *nlh = cb->nlh; 4897 struct net *net = sock_net(skb->sk); 4898 struct net_device *dev; 4899 int idx = 0; 4900 u32 portid = NETLINK_CB(cb->skb).portid; 4901 u32 seq = nlh->nlmsg_seq; 4902 u32 filter_mask = 0; 4903 int err; 4904 4905 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 4906 cb->extack); 4907 if (err < 0 && cb->strict_check) 4908 return err; 4909 4910 rcu_read_lock(); 4911 for_each_netdev_rcu(net, dev) { 4912 const struct net_device_ops *ops = dev->netdev_ops; 4913 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4914 4915 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4916 if (idx >= cb->args[0]) { 4917 err = br_dev->netdev_ops->ndo_bridge_getlink( 4918 skb, portid, seq, dev, 4919 filter_mask, NLM_F_MULTI); 4920 if (err < 0 && err != -EOPNOTSUPP) { 4921 if (likely(skb->len)) 4922 break; 4923 4924 goto out_err; 4925 } 4926 } 4927 idx++; 4928 } 4929 4930 if (ops->ndo_bridge_getlink) { 4931 if (idx >= cb->args[0]) { 4932 err = ops->ndo_bridge_getlink(skb, portid, 4933 seq, dev, 4934 filter_mask, 4935 NLM_F_MULTI); 4936 if (err < 0 && err != -EOPNOTSUPP) { 4937 if (likely(skb->len)) 4938 break; 4939 4940 goto out_err; 4941 } 4942 } 4943 idx++; 4944 } 4945 } 4946 err = skb->len; 4947 out_err: 4948 rcu_read_unlock(); 4949 cb->args[0] = idx; 4950 4951 return err; 4952 } 4953 4954 static inline size_t bridge_nlmsg_size(void) 4955 { 4956 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4957 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4958 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4959 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 4960 + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 4961 + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 4962 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 4963 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 4964 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 4965 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 4966 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 4967 } 4968 4969 static int rtnl_bridge_notify(struct net_device *dev) 4970 { 4971 struct net *net = dev_net(dev); 4972 struct sk_buff *skb; 4973 int err = -EOPNOTSUPP; 4974 4975 if (!dev->netdev_ops->ndo_bridge_getlink) 4976 return 0; 4977 4978 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 4979 if (!skb) { 4980 err = -ENOMEM; 4981 goto errout; 4982 } 4983 4984 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4985 if (err < 0) 4986 goto errout; 4987 4988 /* Notification info is only filled for bridge ports, not the bridge 4989 * device itself. Therefore, a zero notification length is valid and 4990 * should not result in an error. 4991 */ 4992 if (!skb->len) 4993 goto errout; 4994 4995 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 4996 return 0; 4997 errout: 4998 WARN_ON(err == -EMSGSIZE); 4999 kfree_skb(skb); 5000 if (err) 5001 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 5002 return err; 5003 } 5004 5005 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 5006 struct netlink_ext_ack *extack) 5007 { 5008 struct net *net = sock_net(skb->sk); 5009 struct ifinfomsg *ifm; 5010 struct net_device *dev; 5011 struct nlattr *br_spec, *attr = NULL; 5012 int rem, err = -EOPNOTSUPP; 5013 u16 flags = 0; 5014 bool have_flags = false; 5015 5016 if (nlmsg_len(nlh) < sizeof(*ifm)) 5017 return -EINVAL; 5018 5019 ifm = nlmsg_data(nlh); 5020 if (ifm->ifi_family != AF_BRIDGE) 5021 return -EPFNOSUPPORT; 5022 5023 dev = __dev_get_by_index(net, ifm->ifi_index); 5024 if (!dev) { 5025 NL_SET_ERR_MSG(extack, "unknown ifindex"); 5026 return -ENODEV; 5027 } 5028 5029 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5030 if (br_spec) { 5031 nla_for_each_nested(attr, br_spec, rem) { 5032 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 5033 if (nla_len(attr) < sizeof(flags)) 5034 return -EINVAL; 5035 5036 have_flags = true; 5037 flags = nla_get_u16(attr); 5038 break; 5039 } 5040 } 5041 } 5042 5043 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5044 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5045 5046 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 5047 err = -EOPNOTSUPP; 5048 goto out; 5049 } 5050 5051 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 5052 extack); 5053 if (err) 5054 goto out; 5055 5056 flags &= ~BRIDGE_FLAGS_MASTER; 5057 } 5058 5059 if ((flags & BRIDGE_FLAGS_SELF)) { 5060 if (!dev->netdev_ops->ndo_bridge_setlink) 5061 err = -EOPNOTSUPP; 5062 else 5063 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 5064 flags, 5065 extack); 5066 if (!err) { 5067 flags &= ~BRIDGE_FLAGS_SELF; 5068 5069 /* Generate event to notify upper layer of bridge 5070 * change 5071 */ 5072 err = rtnl_bridge_notify(dev); 5073 } 5074 } 5075 5076 if (have_flags) 5077 memcpy(nla_data(attr), &flags, sizeof(flags)); 5078 out: 5079 return err; 5080 } 5081 5082 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 5083 struct netlink_ext_ack *extack) 5084 { 5085 struct net *net = sock_net(skb->sk); 5086 struct ifinfomsg *ifm; 5087 struct net_device *dev; 5088 struct nlattr *br_spec, *attr = NULL; 5089 int rem, err = -EOPNOTSUPP; 5090 u16 flags = 0; 5091 bool have_flags = false; 5092 5093 if (nlmsg_len(nlh) < sizeof(*ifm)) 5094 return -EINVAL; 5095 5096 ifm = nlmsg_data(nlh); 5097 if (ifm->ifi_family != AF_BRIDGE) 5098 return -EPFNOSUPPORT; 5099 5100 dev = __dev_get_by_index(net, ifm->ifi_index); 5101 if (!dev) { 5102 NL_SET_ERR_MSG(extack, "unknown ifindex"); 5103 return -ENODEV; 5104 } 5105 5106 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5107 if (br_spec) { 5108 nla_for_each_nested(attr, br_spec, rem) { 5109 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 5110 if (nla_len(attr) < sizeof(flags)) 5111 return -EINVAL; 5112 5113 have_flags = true; 5114 flags = nla_get_u16(attr); 5115 break; 5116 } 5117 } 5118 } 5119 5120 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 5121 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 5122 5123 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 5124 err = -EOPNOTSUPP; 5125 goto out; 5126 } 5127 5128 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 5129 if (err) 5130 goto out; 5131 5132 flags &= ~BRIDGE_FLAGS_MASTER; 5133 } 5134 5135 if ((flags & BRIDGE_FLAGS_SELF)) { 5136 if (!dev->netdev_ops->ndo_bridge_dellink) 5137 err = -EOPNOTSUPP; 5138 else 5139 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 5140 flags); 5141 5142 if (!err) { 5143 flags &= ~BRIDGE_FLAGS_SELF; 5144 5145 /* Generate event to notify upper layer of bridge 5146 * change 5147 */ 5148 err = rtnl_bridge_notify(dev); 5149 } 5150 } 5151 5152 if (have_flags) 5153 memcpy(nla_data(attr), &flags, sizeof(flags)); 5154 out: 5155 return err; 5156 } 5157 5158 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 5159 { 5160 return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 5161 (!idxattr || idxattr == attrid); 5162 } 5163 5164 static bool 5165 rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id) 5166 { 5167 return dev->netdev_ops && 5168 dev->netdev_ops->ndo_has_offload_stats && 5169 dev->netdev_ops->ndo_get_offload_stats && 5170 dev->netdev_ops->ndo_has_offload_stats(dev, attr_id); 5171 } 5172 5173 static unsigned int 5174 rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id) 5175 { 5176 return rtnl_offload_xstats_have_ndo(dev, attr_id) ? 5177 sizeof(struct rtnl_link_stats64) : 0; 5178 } 5179 5180 static int 5181 rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id, 5182 struct sk_buff *skb) 5183 { 5184 unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id); 5185 struct nlattr *attr = NULL; 5186 void *attr_data; 5187 int err; 5188 5189 if (!size) 5190 return -ENODATA; 5191 5192 attr = nla_reserve_64bit(skb, attr_id, size, 5193 IFLA_OFFLOAD_XSTATS_UNSPEC); 5194 if (!attr) 5195 return -EMSGSIZE; 5196 5197 attr_data = nla_data(attr); 5198 memset(attr_data, 0, size); 5199 5200 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data); 5201 if (err) 5202 return err; 5203 5204 return 0; 5205 } 5206 5207 static unsigned int 5208 rtnl_offload_xstats_get_size_stats(const struct net_device *dev, 5209 enum netdev_offload_xstats_type type) 5210 { 5211 bool enabled = netdev_offload_xstats_enabled(dev, type); 5212 5213 return enabled ? sizeof(struct rtnl_hw_stats64) : 0; 5214 } 5215 5216 struct rtnl_offload_xstats_request_used { 5217 bool request; 5218 bool used; 5219 }; 5220 5221 static int 5222 rtnl_offload_xstats_get_stats(struct net_device *dev, 5223 enum netdev_offload_xstats_type type, 5224 struct rtnl_offload_xstats_request_used *ru, 5225 struct rtnl_hw_stats64 *stats, 5226 struct netlink_ext_ack *extack) 5227 { 5228 bool request; 5229 bool used; 5230 int err; 5231 5232 request = netdev_offload_xstats_enabled(dev, type); 5233 if (!request) { 5234 used = false; 5235 goto out; 5236 } 5237 5238 err = netdev_offload_xstats_get(dev, type, stats, &used, extack); 5239 if (err) 5240 return err; 5241 5242 out: 5243 if (ru) { 5244 ru->request = request; 5245 ru->used = used; 5246 } 5247 return 0; 5248 } 5249 5250 static int 5251 rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id, 5252 struct rtnl_offload_xstats_request_used *ru) 5253 { 5254 struct nlattr *nest; 5255 5256 nest = nla_nest_start(skb, attr_id); 5257 if (!nest) 5258 return -EMSGSIZE; 5259 5260 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request)) 5261 goto nla_put_failure; 5262 5263 if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used)) 5264 goto nla_put_failure; 5265 5266 nla_nest_end(skb, nest); 5267 return 0; 5268 5269 nla_put_failure: 5270 nla_nest_cancel(skb, nest); 5271 return -EMSGSIZE; 5272 } 5273 5274 static int 5275 rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev, 5276 struct netlink_ext_ack *extack) 5277 { 5278 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5279 struct rtnl_offload_xstats_request_used ru_l3; 5280 struct nlattr *nest; 5281 int err; 5282 5283 err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack); 5284 if (err) 5285 return err; 5286 5287 nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO); 5288 if (!nest) 5289 return -EMSGSIZE; 5290 5291 if (rtnl_offload_xstats_fill_hw_s_info_one(skb, 5292 IFLA_OFFLOAD_XSTATS_L3_STATS, 5293 &ru_l3)) 5294 goto nla_put_failure; 5295 5296 nla_nest_end(skb, nest); 5297 return 0; 5298 5299 nla_put_failure: 5300 nla_nest_cancel(skb, nest); 5301 return -EMSGSIZE; 5302 } 5303 5304 static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev, 5305 int *prividx, u32 off_filter_mask, 5306 struct netlink_ext_ack *extack) 5307 { 5308 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5309 int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO; 5310 int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS; 5311 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 5312 bool have_data = false; 5313 int err; 5314 5315 if (*prividx <= attr_id_cpu_hit && 5316 (off_filter_mask & 5317 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) { 5318 err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb); 5319 if (!err) { 5320 have_data = true; 5321 } else if (err != -ENODATA) { 5322 *prividx = attr_id_cpu_hit; 5323 return err; 5324 } 5325 } 5326 5327 if (*prividx <= attr_id_hw_s_info && 5328 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) { 5329 *prividx = attr_id_hw_s_info; 5330 5331 err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack); 5332 if (err) 5333 return err; 5334 5335 have_data = true; 5336 *prividx = 0; 5337 } 5338 5339 if (*prividx <= attr_id_l3_stats && 5340 (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) { 5341 unsigned int size_l3; 5342 struct nlattr *attr; 5343 5344 *prividx = attr_id_l3_stats; 5345 5346 size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3); 5347 if (!size_l3) 5348 goto skip_l3_stats; 5349 attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3, 5350 IFLA_OFFLOAD_XSTATS_UNSPEC); 5351 if (!attr) 5352 return -EMSGSIZE; 5353 5354 err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL, 5355 nla_data(attr), extack); 5356 if (err) 5357 return err; 5358 5359 have_data = true; 5360 skip_l3_stats: 5361 *prividx = 0; 5362 } 5363 5364 if (!have_data) 5365 return -ENODATA; 5366 5367 *prividx = 0; 5368 return 0; 5369 } 5370 5371 static unsigned int 5372 rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev, 5373 enum netdev_offload_xstats_type type) 5374 { 5375 bool enabled = netdev_offload_xstats_enabled(dev, type); 5376 5377 return nla_total_size(0) + 5378 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */ 5379 nla_total_size(sizeof(u8)) + 5380 /* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */ 5381 (enabled ? nla_total_size(sizeof(u8)) : 0) + 5382 0; 5383 } 5384 5385 static unsigned int 5386 rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev) 5387 { 5388 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5389 5390 return nla_total_size(0) + 5391 /* IFLA_OFFLOAD_XSTATS_L3_STATS */ 5392 rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) + 5393 0; 5394 } 5395 5396 static int rtnl_offload_xstats_get_size(const struct net_device *dev, 5397 u32 off_filter_mask) 5398 { 5399 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5400 int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT; 5401 int nla_size = 0; 5402 int size; 5403 5404 if (off_filter_mask & 5405 IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) { 5406 size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit); 5407 nla_size += nla_total_size_64bit(size); 5408 } 5409 5410 if (off_filter_mask & 5411 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO)) 5412 nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev); 5413 5414 if (off_filter_mask & 5415 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) { 5416 size = rtnl_offload_xstats_get_size_stats(dev, t_l3); 5417 nla_size += nla_total_size_64bit(size); 5418 } 5419 5420 if (nla_size != 0) 5421 nla_size += nla_total_size(0); 5422 5423 return nla_size; 5424 } 5425 5426 struct rtnl_stats_dump_filters { 5427 /* mask[0] filters outer attributes. Then individual nests have their 5428 * filtering mask at the index of the nested attribute. 5429 */ 5430 u32 mask[IFLA_STATS_MAX + 1]; 5431 }; 5432 5433 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 5434 int type, u32 pid, u32 seq, u32 change, 5435 unsigned int flags, 5436 const struct rtnl_stats_dump_filters *filters, 5437 int *idxattr, int *prividx, 5438 struct netlink_ext_ack *extack) 5439 { 5440 unsigned int filter_mask = filters->mask[0]; 5441 struct if_stats_msg *ifsm; 5442 struct nlmsghdr *nlh; 5443 struct nlattr *attr; 5444 int s_prividx = *prividx; 5445 int err; 5446 5447 ASSERT_RTNL(); 5448 5449 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 5450 if (!nlh) 5451 return -EMSGSIZE; 5452 5453 ifsm = nlmsg_data(nlh); 5454 ifsm->family = PF_UNSPEC; 5455 ifsm->pad1 = 0; 5456 ifsm->pad2 = 0; 5457 ifsm->ifindex = dev->ifindex; 5458 ifsm->filter_mask = filter_mask; 5459 5460 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 5461 struct rtnl_link_stats64 *sp; 5462 5463 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 5464 sizeof(struct rtnl_link_stats64), 5465 IFLA_STATS_UNSPEC); 5466 if (!attr) { 5467 err = -EMSGSIZE; 5468 goto nla_put_failure; 5469 } 5470 5471 sp = nla_data(attr); 5472 dev_get_stats(dev, sp); 5473 } 5474 5475 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 5476 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 5477 5478 if (ops && ops->fill_linkxstats) { 5479 *idxattr = IFLA_STATS_LINK_XSTATS; 5480 attr = nla_nest_start_noflag(skb, 5481 IFLA_STATS_LINK_XSTATS); 5482 if (!attr) { 5483 err = -EMSGSIZE; 5484 goto nla_put_failure; 5485 } 5486 5487 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 5488 nla_nest_end(skb, attr); 5489 if (err) 5490 goto nla_put_failure; 5491 *idxattr = 0; 5492 } 5493 } 5494 5495 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 5496 *idxattr)) { 5497 const struct rtnl_link_ops *ops = NULL; 5498 const struct net_device *master; 5499 5500 master = netdev_master_upper_dev_get(dev); 5501 if (master) 5502 ops = master->rtnl_link_ops; 5503 if (ops && ops->fill_linkxstats) { 5504 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 5505 attr = nla_nest_start_noflag(skb, 5506 IFLA_STATS_LINK_XSTATS_SLAVE); 5507 if (!attr) { 5508 err = -EMSGSIZE; 5509 goto nla_put_failure; 5510 } 5511 5512 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 5513 nla_nest_end(skb, attr); 5514 if (err) 5515 goto nla_put_failure; 5516 *idxattr = 0; 5517 } 5518 } 5519 5520 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 5521 *idxattr)) { 5522 u32 off_filter_mask; 5523 5524 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 5525 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 5526 attr = nla_nest_start_noflag(skb, 5527 IFLA_STATS_LINK_OFFLOAD_XSTATS); 5528 if (!attr) { 5529 err = -EMSGSIZE; 5530 goto nla_put_failure; 5531 } 5532 5533 err = rtnl_offload_xstats_fill(skb, dev, prividx, 5534 off_filter_mask, extack); 5535 if (err == -ENODATA) 5536 nla_nest_cancel(skb, attr); 5537 else 5538 nla_nest_end(skb, attr); 5539 5540 if (err && err != -ENODATA) 5541 goto nla_put_failure; 5542 *idxattr = 0; 5543 } 5544 5545 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 5546 struct rtnl_af_ops *af_ops; 5547 5548 *idxattr = IFLA_STATS_AF_SPEC; 5549 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 5550 if (!attr) { 5551 err = -EMSGSIZE; 5552 goto nla_put_failure; 5553 } 5554 5555 rcu_read_lock(); 5556 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5557 if (af_ops->fill_stats_af) { 5558 struct nlattr *af; 5559 5560 af = nla_nest_start_noflag(skb, 5561 af_ops->family); 5562 if (!af) { 5563 rcu_read_unlock(); 5564 err = -EMSGSIZE; 5565 goto nla_put_failure; 5566 } 5567 err = af_ops->fill_stats_af(skb, dev); 5568 5569 if (err == -ENODATA) { 5570 nla_nest_cancel(skb, af); 5571 } else if (err < 0) { 5572 rcu_read_unlock(); 5573 goto nla_put_failure; 5574 } 5575 5576 nla_nest_end(skb, af); 5577 } 5578 } 5579 rcu_read_unlock(); 5580 5581 nla_nest_end(skb, attr); 5582 5583 *idxattr = 0; 5584 } 5585 5586 nlmsg_end(skb, nlh); 5587 5588 return 0; 5589 5590 nla_put_failure: 5591 /* not a multi message or no progress mean a real error */ 5592 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 5593 nlmsg_cancel(skb, nlh); 5594 else 5595 nlmsg_end(skb, nlh); 5596 5597 return err; 5598 } 5599 5600 static size_t if_nlmsg_stats_size(const struct net_device *dev, 5601 const struct rtnl_stats_dump_filters *filters) 5602 { 5603 size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg)); 5604 unsigned int filter_mask = filters->mask[0]; 5605 5606 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 5607 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 5608 5609 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 5610 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 5611 int attr = IFLA_STATS_LINK_XSTATS; 5612 5613 if (ops && ops->get_linkxstats_size) { 5614 size += nla_total_size(ops->get_linkxstats_size(dev, 5615 attr)); 5616 /* for IFLA_STATS_LINK_XSTATS */ 5617 size += nla_total_size(0); 5618 } 5619 } 5620 5621 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 5622 struct net_device *_dev = (struct net_device *)dev; 5623 const struct rtnl_link_ops *ops = NULL; 5624 const struct net_device *master; 5625 5626 /* netdev_master_upper_dev_get can't take const */ 5627 master = netdev_master_upper_dev_get(_dev); 5628 if (master) 5629 ops = master->rtnl_link_ops; 5630 if (ops && ops->get_linkxstats_size) { 5631 int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 5632 5633 size += nla_total_size(ops->get_linkxstats_size(dev, 5634 attr)); 5635 /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 5636 size += nla_total_size(0); 5637 } 5638 } 5639 5640 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) { 5641 u32 off_filter_mask; 5642 5643 off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS]; 5644 size += rtnl_offload_xstats_get_size(dev, off_filter_mask); 5645 } 5646 5647 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 5648 struct rtnl_af_ops *af_ops; 5649 5650 /* for IFLA_STATS_AF_SPEC */ 5651 size += nla_total_size(0); 5652 5653 rcu_read_lock(); 5654 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 5655 if (af_ops->get_stats_af_size) { 5656 size += nla_total_size( 5657 af_ops->get_stats_af_size(dev)); 5658 5659 /* for AF_* */ 5660 size += nla_total_size(0); 5661 } 5662 } 5663 rcu_read_unlock(); 5664 } 5665 5666 return size; 5667 } 5668 5669 #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1) 5670 5671 static const struct nla_policy 5672 rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = { 5673 [IFLA_STATS_LINK_OFFLOAD_XSTATS] = 5674 NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID), 5675 }; 5676 5677 static const struct nla_policy 5678 rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = { 5679 [IFLA_STATS_GET_FILTERS] = 5680 NLA_POLICY_NESTED(rtnl_stats_get_policy_filters), 5681 }; 5682 5683 static const struct nla_policy 5684 ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = { 5685 [IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1), 5686 }; 5687 5688 static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters, 5689 struct rtnl_stats_dump_filters *filters, 5690 struct netlink_ext_ack *extack) 5691 { 5692 struct nlattr *tb[IFLA_STATS_MAX + 1]; 5693 int err; 5694 int at; 5695 5696 err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters, 5697 rtnl_stats_get_policy_filters, extack); 5698 if (err < 0) 5699 return err; 5700 5701 for (at = 1; at <= IFLA_STATS_MAX; at++) { 5702 if (tb[at]) { 5703 if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) { 5704 NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask"); 5705 return -EINVAL; 5706 } 5707 filters->mask[at] = nla_get_u32(tb[at]); 5708 } 5709 } 5710 5711 return 0; 5712 } 5713 5714 static int rtnl_stats_get_parse(const struct nlmsghdr *nlh, 5715 u32 filter_mask, 5716 struct rtnl_stats_dump_filters *filters, 5717 struct netlink_ext_ack *extack) 5718 { 5719 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 5720 int err; 5721 int i; 5722 5723 filters->mask[0] = filter_mask; 5724 for (i = 1; i < ARRAY_SIZE(filters->mask); i++) 5725 filters->mask[i] = -1U; 5726 5727 err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb, 5728 IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack); 5729 if (err < 0) 5730 return err; 5731 5732 if (tb[IFLA_STATS_GET_FILTERS]) { 5733 err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS], 5734 filters, extack); 5735 if (err) 5736 return err; 5737 } 5738 5739 return 0; 5740 } 5741 5742 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 5743 bool is_dump, struct netlink_ext_ack *extack) 5744 { 5745 struct if_stats_msg *ifsm; 5746 5747 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 5748 NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 5749 return -EINVAL; 5750 } 5751 5752 if (!strict_check) 5753 return 0; 5754 5755 ifsm = nlmsg_data(nlh); 5756 5757 /* only requests using strict checks can pass data to influence 5758 * the dump. The legacy exception is filter_mask. 5759 */ 5760 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 5761 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 5762 return -EINVAL; 5763 } 5764 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 5765 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 5766 return -EINVAL; 5767 } 5768 5769 return 0; 5770 } 5771 5772 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5773 struct netlink_ext_ack *extack) 5774 { 5775 struct rtnl_stats_dump_filters filters; 5776 struct net *net = sock_net(skb->sk); 5777 struct net_device *dev = NULL; 5778 int idxattr = 0, prividx = 0; 5779 struct if_stats_msg *ifsm; 5780 struct sk_buff *nskb; 5781 int err; 5782 5783 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 5784 false, extack); 5785 if (err) 5786 return err; 5787 5788 ifsm = nlmsg_data(nlh); 5789 if (ifsm->ifindex > 0) 5790 dev = __dev_get_by_index(net, ifsm->ifindex); 5791 else 5792 return -EINVAL; 5793 5794 if (!dev) 5795 return -ENODEV; 5796 5797 if (!ifsm->filter_mask) { 5798 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get"); 5799 return -EINVAL; 5800 } 5801 5802 err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack); 5803 if (err) 5804 return err; 5805 5806 nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL); 5807 if (!nskb) 5808 return -ENOBUFS; 5809 5810 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 5811 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 5812 0, &filters, &idxattr, &prividx, extack); 5813 if (err < 0) { 5814 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 5815 WARN_ON(err == -EMSGSIZE); 5816 kfree_skb(nskb); 5817 } else { 5818 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 5819 } 5820 5821 return err; 5822 } 5823 5824 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 5825 { 5826 struct netlink_ext_ack *extack = cb->extack; 5827 int h, s_h, err, s_idx, s_idxattr, s_prividx; 5828 struct rtnl_stats_dump_filters filters; 5829 struct net *net = sock_net(skb->sk); 5830 unsigned int flags = NLM_F_MULTI; 5831 struct if_stats_msg *ifsm; 5832 struct hlist_head *head; 5833 struct net_device *dev; 5834 int idx = 0; 5835 5836 s_h = cb->args[0]; 5837 s_idx = cb->args[1]; 5838 s_idxattr = cb->args[2]; 5839 s_prividx = cb->args[3]; 5840 5841 cb->seq = net->dev_base_seq; 5842 5843 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 5844 if (err) 5845 return err; 5846 5847 ifsm = nlmsg_data(cb->nlh); 5848 if (!ifsm->filter_mask) { 5849 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5850 return -EINVAL; 5851 } 5852 5853 err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters, 5854 extack); 5855 if (err) 5856 return err; 5857 5858 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 5859 idx = 0; 5860 head = &net->dev_index_head[h]; 5861 hlist_for_each_entry(dev, head, index_hlist) { 5862 if (idx < s_idx) 5863 goto cont; 5864 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 5865 NETLINK_CB(cb->skb).portid, 5866 cb->nlh->nlmsg_seq, 0, 5867 flags, &filters, 5868 &s_idxattr, &s_prividx, 5869 extack); 5870 /* If we ran out of room on the first message, 5871 * we're in trouble 5872 */ 5873 WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 5874 5875 if (err < 0) 5876 goto out; 5877 s_prividx = 0; 5878 s_idxattr = 0; 5879 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 5880 cont: 5881 idx++; 5882 } 5883 } 5884 out: 5885 cb->args[3] = s_prividx; 5886 cb->args[2] = s_idxattr; 5887 cb->args[1] = idx; 5888 cb->args[0] = h; 5889 5890 return skb->len; 5891 } 5892 5893 void rtnl_offload_xstats_notify(struct net_device *dev) 5894 { 5895 struct rtnl_stats_dump_filters response_filters = {}; 5896 struct net *net = dev_net(dev); 5897 int idxattr = 0, prividx = 0; 5898 struct sk_buff *skb; 5899 int err = -ENOBUFS; 5900 5901 ASSERT_RTNL(); 5902 5903 response_filters.mask[0] |= 5904 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 5905 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 5906 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 5907 5908 skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters), 5909 GFP_KERNEL); 5910 if (!skb) 5911 goto errout; 5912 5913 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0, 5914 &response_filters, &idxattr, &prividx, NULL); 5915 if (err < 0) { 5916 kfree_skb(skb); 5917 goto errout; 5918 } 5919 5920 rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL); 5921 return; 5922 5923 errout: 5924 rtnl_set_sk_err(net, RTNLGRP_STATS, err); 5925 } 5926 EXPORT_SYMBOL(rtnl_offload_xstats_notify); 5927 5928 static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh, 5929 struct netlink_ext_ack *extack) 5930 { 5931 enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3; 5932 struct rtnl_stats_dump_filters response_filters = {}; 5933 struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1]; 5934 struct net *net = sock_net(skb->sk); 5935 struct net_device *dev = NULL; 5936 struct if_stats_msg *ifsm; 5937 bool notify = false; 5938 int err; 5939 5940 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 5941 false, extack); 5942 if (err) 5943 return err; 5944 5945 ifsm = nlmsg_data(nlh); 5946 if (ifsm->family != AF_UNSPEC) { 5947 NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC"); 5948 return -EINVAL; 5949 } 5950 5951 if (ifsm->ifindex > 0) 5952 dev = __dev_get_by_index(net, ifsm->ifindex); 5953 else 5954 return -EINVAL; 5955 5956 if (!dev) 5957 return -ENODEV; 5958 5959 if (ifsm->filter_mask) { 5960 NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set"); 5961 return -EINVAL; 5962 } 5963 5964 err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX, 5965 ifla_stats_set_policy, extack); 5966 if (err < 0) 5967 return err; 5968 5969 if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) { 5970 u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]); 5971 5972 if (req) 5973 err = netdev_offload_xstats_enable(dev, t_l3, extack); 5974 else 5975 err = netdev_offload_xstats_disable(dev, t_l3); 5976 5977 if (!err) 5978 notify = true; 5979 else if (err != -EALREADY) 5980 return err; 5981 5982 response_filters.mask[0] |= 5983 IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS); 5984 response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |= 5985 IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO); 5986 } 5987 5988 if (notify) 5989 rtnl_offload_xstats_notify(dev); 5990 5991 return 0; 5992 } 5993 5994 /* Process one rtnetlink message. */ 5995 5996 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 5997 struct netlink_ext_ack *extack) 5998 { 5999 struct net *net = sock_net(skb->sk); 6000 struct rtnl_link *link; 6001 enum rtnl_kinds kind; 6002 struct module *owner; 6003 int err = -EOPNOTSUPP; 6004 rtnl_doit_func doit; 6005 unsigned int flags; 6006 int family; 6007 int type; 6008 6009 type = nlh->nlmsg_type; 6010 if (type > RTM_MAX) 6011 return -EOPNOTSUPP; 6012 6013 type -= RTM_BASE; 6014 6015 /* All the messages must have at least 1 byte length */ 6016 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 6017 return 0; 6018 6019 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 6020 kind = rtnl_msgtype_kind(type); 6021 6022 if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN)) 6023 return -EPERM; 6024 6025 rcu_read_lock(); 6026 if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) { 6027 struct sock *rtnl; 6028 rtnl_dumpit_func dumpit; 6029 u32 min_dump_alloc = 0; 6030 6031 link = rtnl_get_link(family, type); 6032 if (!link || !link->dumpit) { 6033 family = PF_UNSPEC; 6034 link = rtnl_get_link(family, type); 6035 if (!link || !link->dumpit) 6036 goto err_unlock; 6037 } 6038 owner = link->owner; 6039 dumpit = link->dumpit; 6040 6041 if (type == RTM_GETLINK - RTM_BASE) 6042 min_dump_alloc = rtnl_calcit(skb, nlh); 6043 6044 err = 0; 6045 /* need to do this before rcu_read_unlock() */ 6046 if (!try_module_get(owner)) 6047 err = -EPROTONOSUPPORT; 6048 6049 rcu_read_unlock(); 6050 6051 rtnl = net->rtnl; 6052 if (err == 0) { 6053 struct netlink_dump_control c = { 6054 .dump = dumpit, 6055 .min_dump_alloc = min_dump_alloc, 6056 .module = owner, 6057 }; 6058 err = netlink_dump_start(rtnl, skb, nlh, &c); 6059 /* netlink_dump_start() will keep a reference on 6060 * module if dump is still in progress. 6061 */ 6062 module_put(owner); 6063 } 6064 return err; 6065 } 6066 6067 link = rtnl_get_link(family, type); 6068 if (!link || !link->doit) { 6069 family = PF_UNSPEC; 6070 link = rtnl_get_link(PF_UNSPEC, type); 6071 if (!link || !link->doit) 6072 goto out_unlock; 6073 } 6074 6075 owner = link->owner; 6076 if (!try_module_get(owner)) { 6077 err = -EPROTONOSUPPORT; 6078 goto out_unlock; 6079 } 6080 6081 flags = link->flags; 6082 if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) && 6083 !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) { 6084 NL_SET_ERR_MSG(extack, "Bulk delete is not supported"); 6085 module_put(owner); 6086 goto err_unlock; 6087 } 6088 6089 if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 6090 doit = link->doit; 6091 rcu_read_unlock(); 6092 if (doit) 6093 err = doit(skb, nlh, extack); 6094 module_put(owner); 6095 return err; 6096 } 6097 rcu_read_unlock(); 6098 6099 rtnl_lock(); 6100 link = rtnl_get_link(family, type); 6101 if (link && link->doit) 6102 err = link->doit(skb, nlh, extack); 6103 rtnl_unlock(); 6104 6105 module_put(owner); 6106 6107 return err; 6108 6109 out_unlock: 6110 rcu_read_unlock(); 6111 return err; 6112 6113 err_unlock: 6114 rcu_read_unlock(); 6115 return -EOPNOTSUPP; 6116 } 6117 6118 static void rtnetlink_rcv(struct sk_buff *skb) 6119 { 6120 netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 6121 } 6122 6123 static int rtnetlink_bind(struct net *net, int group) 6124 { 6125 switch (group) { 6126 case RTNLGRP_IPV4_MROUTE_R: 6127 case RTNLGRP_IPV6_MROUTE_R: 6128 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 6129 return -EPERM; 6130 break; 6131 } 6132 return 0; 6133 } 6134 6135 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 6136 { 6137 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6138 6139 switch (event) { 6140 case NETDEV_REBOOT: 6141 case NETDEV_CHANGEMTU: 6142 case NETDEV_CHANGEADDR: 6143 case NETDEV_CHANGENAME: 6144 case NETDEV_FEAT_CHANGE: 6145 case NETDEV_BONDING_FAILOVER: 6146 case NETDEV_POST_TYPE_CHANGE: 6147 case NETDEV_NOTIFY_PEERS: 6148 case NETDEV_CHANGEUPPER: 6149 case NETDEV_RESEND_IGMP: 6150 case NETDEV_CHANGEINFODATA: 6151 case NETDEV_CHANGELOWERSTATE: 6152 case NETDEV_CHANGE_TX_QUEUE_LEN: 6153 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 6154 GFP_KERNEL, NULL, 0, 0, NULL); 6155 break; 6156 default: 6157 break; 6158 } 6159 return NOTIFY_DONE; 6160 } 6161 6162 static struct notifier_block rtnetlink_dev_notifier = { 6163 .notifier_call = rtnetlink_event, 6164 }; 6165 6166 6167 static int __net_init rtnetlink_net_init(struct net *net) 6168 { 6169 struct sock *sk; 6170 struct netlink_kernel_cfg cfg = { 6171 .groups = RTNLGRP_MAX, 6172 .input = rtnetlink_rcv, 6173 .cb_mutex = &rtnl_mutex, 6174 .flags = NL_CFG_F_NONROOT_RECV, 6175 .bind = rtnetlink_bind, 6176 }; 6177 6178 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 6179 if (!sk) 6180 return -ENOMEM; 6181 net->rtnl = sk; 6182 return 0; 6183 } 6184 6185 static void __net_exit rtnetlink_net_exit(struct net *net) 6186 { 6187 netlink_kernel_release(net->rtnl); 6188 net->rtnl = NULL; 6189 } 6190 6191 static struct pernet_operations rtnetlink_net_ops = { 6192 .init = rtnetlink_net_init, 6193 .exit = rtnetlink_net_exit, 6194 }; 6195 6196 void __init rtnetlink_init(void) 6197 { 6198 if (register_pernet_subsys(&rtnetlink_net_ops)) 6199 panic("rtnetlink_init: cannot initialize rtnetlink\n"); 6200 6201 register_netdevice_notifier(&rtnetlink_dev_notifier); 6202 6203 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 6204 rtnl_dump_ifinfo, 0); 6205 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 6206 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 6207 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 6208 6209 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 6210 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 6211 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 6212 6213 rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0); 6214 rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0); 6215 6216 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 6217 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 6218 RTNL_FLAG_BULK_DEL_SUPPORTED); 6219 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 6220 6221 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 6222 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 6223 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 6224 6225 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 6226 0); 6227 rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0); 6228 } 6229