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