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