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