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