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