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 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB, 1418 IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb); 1419 if (err) 1420 goto err_cancel; 1421 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV, 1422 IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv); 1423 if (err) 1424 goto err_cancel; 1425 err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW, 1426 IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw); 1427 if (err) 1428 goto err_cancel; 1429 1430 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode); 1431 if (err) 1432 goto err_cancel; 1433 1434 if (prog_id && mode != XDP_ATTACHED_MULTI) { 1435 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 1436 if (err) 1437 goto err_cancel; 1438 } 1439 1440 nla_nest_end(skb, xdp); 1441 return 0; 1442 1443 err_cancel: 1444 nla_nest_cancel(skb, xdp); 1445 return err; 1446 } 1447 1448 static u32 rtnl_get_event(unsigned long event) 1449 { 1450 u32 rtnl_event_type = IFLA_EVENT_NONE; 1451 1452 switch (event) { 1453 case NETDEV_REBOOT: 1454 rtnl_event_type = IFLA_EVENT_REBOOT; 1455 break; 1456 case NETDEV_FEAT_CHANGE: 1457 rtnl_event_type = IFLA_EVENT_FEATURES; 1458 break; 1459 case NETDEV_BONDING_FAILOVER: 1460 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 1461 break; 1462 case NETDEV_NOTIFY_PEERS: 1463 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 1464 break; 1465 case NETDEV_RESEND_IGMP: 1466 rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 1467 break; 1468 case NETDEV_CHANGEINFODATA: 1469 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 1470 break; 1471 default: 1472 break; 1473 } 1474 1475 return rtnl_event_type; 1476 } 1477 1478 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev) 1479 { 1480 const struct net_device *upper_dev; 1481 int ret = 0; 1482 1483 rcu_read_lock(); 1484 1485 upper_dev = netdev_master_upper_dev_get_rcu(dev); 1486 if (upper_dev) 1487 ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex); 1488 1489 rcu_read_unlock(); 1490 return ret; 1491 } 1492 1493 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev) 1494 { 1495 int ifindex = dev_get_iflink(dev); 1496 1497 if (dev->ifindex == ifindex) 1498 return 0; 1499 1500 return nla_put_u32(skb, IFLA_LINK, ifindex); 1501 } 1502 1503 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 1504 struct net_device *dev) 1505 { 1506 char buf[IFALIASZ]; 1507 int ret; 1508 1509 ret = dev_get_alias(dev, buf, sizeof(buf)); 1510 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 1511 } 1512 1513 static int rtnl_fill_link_netnsid(struct sk_buff *skb, 1514 const struct net_device *dev, 1515 struct net *src_net) 1516 { 1517 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1518 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1519 1520 if (!net_eq(dev_net(dev), link_net)) { 1521 int id = peernet2id_alloc(src_net, link_net); 1522 1523 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1524 return -EMSGSIZE; 1525 } 1526 } 1527 1528 return 0; 1529 } 1530 1531 static int rtnl_fill_link_af(struct sk_buff *skb, 1532 const struct net_device *dev, 1533 u32 ext_filter_mask) 1534 { 1535 const struct rtnl_af_ops *af_ops; 1536 struct nlattr *af_spec; 1537 1538 af_spec = nla_nest_start(skb, IFLA_AF_SPEC); 1539 if (!af_spec) 1540 return -EMSGSIZE; 1541 1542 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1543 struct nlattr *af; 1544 int err; 1545 1546 if (!af_ops->fill_link_af) 1547 continue; 1548 1549 af = nla_nest_start(skb, af_ops->family); 1550 if (!af) 1551 return -EMSGSIZE; 1552 1553 err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1554 /* 1555 * Caller may return ENODATA to indicate that there 1556 * was no data to be dumped. This is not an error, it 1557 * means we should trim the attribute header and 1558 * continue. 1559 */ 1560 if (err == -ENODATA) 1561 nla_nest_cancel(skb, af); 1562 else if (err < 0) 1563 return -EMSGSIZE; 1564 1565 nla_nest_end(skb, af); 1566 } 1567 1568 nla_nest_end(skb, af_spec); 1569 return 0; 1570 } 1571 1572 static int rtnl_fill_ifinfo(struct sk_buff *skb, 1573 struct net_device *dev, struct net *src_net, 1574 int type, u32 pid, u32 seq, u32 change, 1575 unsigned int flags, u32 ext_filter_mask, 1576 u32 event, int *new_nsid, int new_ifindex, 1577 int tgt_netnsid) 1578 { 1579 struct ifinfomsg *ifm; 1580 struct nlmsghdr *nlh; 1581 1582 ASSERT_RTNL(); 1583 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1584 if (nlh == NULL) 1585 return -EMSGSIZE; 1586 1587 ifm = nlmsg_data(nlh); 1588 ifm->ifi_family = AF_UNSPEC; 1589 ifm->__ifi_pad = 0; 1590 ifm->ifi_type = dev->type; 1591 ifm->ifi_index = dev->ifindex; 1592 ifm->ifi_flags = dev_get_flags(dev); 1593 ifm->ifi_change = change; 1594 1595 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_IF_NETNSID, tgt_netnsid)) 1596 goto nla_put_failure; 1597 1598 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1599 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1600 nla_put_u8(skb, IFLA_OPERSTATE, 1601 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1602 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1603 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 1604 nla_put_u32(skb, IFLA_GROUP, dev->group) || 1605 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 1606 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1607 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1608 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 1609 #ifdef CONFIG_RPS 1610 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 1611 #endif 1612 nla_put_iflink(skb, dev) || 1613 put_master_ifindex(skb, dev) || 1614 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1615 (dev->qdisc && 1616 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 1617 nla_put_ifalias(skb, dev) || 1618 nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1619 atomic_read(&dev->carrier_up_count) + 1620 atomic_read(&dev->carrier_down_count)) || 1621 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1622 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1623 atomic_read(&dev->carrier_up_count)) || 1624 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1625 atomic_read(&dev->carrier_down_count))) 1626 goto nla_put_failure; 1627 1628 if (event != IFLA_EVENT_NONE) { 1629 if (nla_put_u32(skb, IFLA_EVENT, event)) 1630 goto nla_put_failure; 1631 } 1632 1633 if (rtnl_fill_link_ifmap(skb, dev)) 1634 goto nla_put_failure; 1635 1636 if (dev->addr_len) { 1637 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1638 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1639 goto nla_put_failure; 1640 } 1641 1642 if (rtnl_phys_port_id_fill(skb, dev)) 1643 goto nla_put_failure; 1644 1645 if (rtnl_phys_port_name_fill(skb, dev)) 1646 goto nla_put_failure; 1647 1648 if (rtnl_phys_switch_id_fill(skb, dev)) 1649 goto nla_put_failure; 1650 1651 if (rtnl_fill_stats(skb, dev)) 1652 goto nla_put_failure; 1653 1654 if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1655 goto nla_put_failure; 1656 1657 if (rtnl_port_fill(skb, dev, ext_filter_mask)) 1658 goto nla_put_failure; 1659 1660 if (rtnl_xdp_fill(skb, dev)) 1661 goto nla_put_failure; 1662 1663 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 1664 if (rtnl_link_fill(skb, dev) < 0) 1665 goto nla_put_failure; 1666 } 1667 1668 if (rtnl_fill_link_netnsid(skb, dev, src_net)) 1669 goto nla_put_failure; 1670 1671 if (new_nsid && 1672 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 1673 goto nla_put_failure; 1674 if (new_ifindex && 1675 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 1676 goto nla_put_failure; 1677 1678 1679 rcu_read_lock(); 1680 if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 1681 goto nla_put_failure_rcu; 1682 rcu_read_unlock(); 1683 1684 nlmsg_end(skb, nlh); 1685 return 0; 1686 1687 nla_put_failure_rcu: 1688 rcu_read_unlock(); 1689 nla_put_failure: 1690 nlmsg_cancel(skb, nlh); 1691 return -EMSGSIZE; 1692 } 1693 1694 static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1695 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1696 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1697 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1698 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1699 [IFLA_MTU] = { .type = NLA_U32 }, 1700 [IFLA_LINK] = { .type = NLA_U32 }, 1701 [IFLA_MASTER] = { .type = NLA_U32 }, 1702 [IFLA_CARRIER] = { .type = NLA_U8 }, 1703 [IFLA_TXQLEN] = { .type = NLA_U32 }, 1704 [IFLA_WEIGHT] = { .type = NLA_U32 }, 1705 [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1706 [IFLA_LINKMODE] = { .type = NLA_U8 }, 1707 [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1708 [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1709 [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 1710 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 1711 * allow 0-length string (needed to remove an alias). 1712 */ 1713 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1714 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1715 [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1716 [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1717 [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1718 [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1719 [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1720 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1721 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 1722 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 1723 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 1724 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1725 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 1726 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1727 [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 1728 [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1729 [IFLA_XDP] = { .type = NLA_NESTED }, 1730 [IFLA_EVENT] = { .type = NLA_U32 }, 1731 [IFLA_GROUP] = { .type = NLA_U32 }, 1732 [IFLA_IF_NETNSID] = { .type = NLA_S32 }, 1733 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1734 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 1735 }; 1736 1737 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1738 [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1739 [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1740 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1741 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1742 }; 1743 1744 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1745 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1746 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 1747 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1748 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1749 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1750 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1751 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 1752 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 1753 [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1754 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1755 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1756 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1757 }; 1758 1759 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1760 [IFLA_PORT_VF] = { .type = NLA_U32 }, 1761 [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1762 .len = PORT_PROFILE_MAX }, 1763 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1764 .len = PORT_UUID_MAX }, 1765 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1766 .len = PORT_UUID_MAX }, 1767 [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1768 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1769 1770 /* Unused, but we need to keep it here since user space could 1771 * fill it. It's also broken with regard to NLA_BINARY use in 1772 * combination with structs. 1773 */ 1774 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1775 .len = sizeof(struct ifla_port_vsi) }, 1776 }; 1777 1778 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1779 [IFLA_XDP_FD] = { .type = NLA_S32 }, 1780 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 1781 [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 1782 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1783 }; 1784 1785 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1786 { 1787 const struct rtnl_link_ops *ops = NULL; 1788 struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1789 1790 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, 1791 ifla_info_policy, NULL) < 0) 1792 return NULL; 1793 1794 if (linfo[IFLA_INFO_KIND]) { 1795 char kind[MODULE_NAME_LEN]; 1796 1797 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1798 ops = rtnl_link_ops_get(kind); 1799 } 1800 1801 return ops; 1802 } 1803 1804 static bool link_master_filtered(struct net_device *dev, int master_idx) 1805 { 1806 struct net_device *master; 1807 1808 if (!master_idx) 1809 return false; 1810 1811 master = netdev_master_upper_dev_get(dev); 1812 if (!master || master->ifindex != master_idx) 1813 return true; 1814 1815 return false; 1816 } 1817 1818 static bool link_kind_filtered(const struct net_device *dev, 1819 const struct rtnl_link_ops *kind_ops) 1820 { 1821 if (kind_ops && dev->rtnl_link_ops != kind_ops) 1822 return true; 1823 1824 return false; 1825 } 1826 1827 static bool link_dump_filtered(struct net_device *dev, 1828 int master_idx, 1829 const struct rtnl_link_ops *kind_ops) 1830 { 1831 if (link_master_filtered(dev, master_idx) || 1832 link_kind_filtered(dev, kind_ops)) 1833 return true; 1834 1835 return false; 1836 } 1837 1838 static struct net *get_target_net(struct sock *sk, int netnsid) 1839 { 1840 struct net *net; 1841 1842 net = get_net_ns_by_id(sock_net(sk), netnsid); 1843 if (!net) 1844 return ERR_PTR(-EINVAL); 1845 1846 /* For now, the caller is required to have CAP_NET_ADMIN in 1847 * the user namespace owning the target net ns. 1848 */ 1849 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 1850 put_net(net); 1851 return ERR_PTR(-EACCES); 1852 } 1853 return net; 1854 } 1855 1856 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 1857 { 1858 struct net *net = sock_net(skb->sk); 1859 struct net *tgt_net = net; 1860 int h, s_h; 1861 int idx = 0, s_idx; 1862 struct net_device *dev; 1863 struct hlist_head *head; 1864 struct nlattr *tb[IFLA_MAX+1]; 1865 u32 ext_filter_mask = 0; 1866 const struct rtnl_link_ops *kind_ops = NULL; 1867 unsigned int flags = NLM_F_MULTI; 1868 int master_idx = 0; 1869 int netnsid = -1; 1870 int err; 1871 int hdrlen; 1872 1873 s_h = cb->args[0]; 1874 s_idx = cb->args[1]; 1875 1876 /* A hack to preserve kernel<->userspace interface. 1877 * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1878 * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1879 * what iproute2 < v3.9.0 used. 1880 * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1881 * attribute, its netlink message is shorter than struct ifinfomsg. 1882 */ 1883 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? 1884 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1885 1886 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, 1887 ifla_policy, NULL) >= 0) { 1888 if (tb[IFLA_IF_NETNSID]) { 1889 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 1890 tgt_net = get_target_net(skb->sk, netnsid); 1891 if (IS_ERR(tgt_net)) { 1892 tgt_net = net; 1893 netnsid = -1; 1894 } 1895 } 1896 1897 if (tb[IFLA_EXT_MASK]) 1898 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1899 1900 if (tb[IFLA_MASTER]) 1901 master_idx = nla_get_u32(tb[IFLA_MASTER]); 1902 1903 if (tb[IFLA_LINKINFO]) 1904 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); 1905 1906 if (master_idx || kind_ops) 1907 flags |= NLM_F_DUMP_FILTERED; 1908 } 1909 1910 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 1911 idx = 0; 1912 head = &tgt_net->dev_index_head[h]; 1913 hlist_for_each_entry(dev, head, index_hlist) { 1914 if (link_dump_filtered(dev, master_idx, kind_ops)) 1915 goto cont; 1916 if (idx < s_idx) 1917 goto cont; 1918 err = rtnl_fill_ifinfo(skb, dev, net, 1919 RTM_NEWLINK, 1920 NETLINK_CB(cb->skb).portid, 1921 cb->nlh->nlmsg_seq, 0, 1922 flags, 1923 ext_filter_mask, 0, NULL, 0, 1924 netnsid); 1925 1926 if (err < 0) { 1927 if (likely(skb->len)) 1928 goto out; 1929 1930 goto out_err; 1931 } 1932 cont: 1933 idx++; 1934 } 1935 } 1936 out: 1937 err = skb->len; 1938 out_err: 1939 cb->args[1] = idx; 1940 cb->args[0] = h; 1941 cb->seq = net->dev_base_seq; 1942 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 1943 if (netnsid >= 0) 1944 put_net(tgt_net); 1945 1946 return err; 1947 } 1948 1949 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 1950 struct netlink_ext_ack *exterr) 1951 { 1952 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); 1953 } 1954 EXPORT_SYMBOL(rtnl_nla_parse_ifla); 1955 1956 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 1957 { 1958 struct net *net; 1959 /* Examine the link attributes and figure out which 1960 * network namespace we are talking about. 1961 */ 1962 if (tb[IFLA_NET_NS_PID]) 1963 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1964 else if (tb[IFLA_NET_NS_FD]) 1965 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 1966 else 1967 net = get_net(src_net); 1968 return net; 1969 } 1970 EXPORT_SYMBOL(rtnl_link_get_net); 1971 1972 /* Figure out which network namespace we are talking about by 1973 * examining the link attributes in the following order: 1974 * 1975 * 1. IFLA_NET_NS_PID 1976 * 2. IFLA_NET_NS_FD 1977 * 3. IFLA_IF_NETNSID 1978 */ 1979 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 1980 struct nlattr *tb[]) 1981 { 1982 struct net *net; 1983 1984 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 1985 return rtnl_link_get_net(src_net, tb); 1986 1987 if (!tb[IFLA_IF_NETNSID]) 1988 return get_net(src_net); 1989 1990 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_IF_NETNSID])); 1991 if (!net) 1992 return ERR_PTR(-EINVAL); 1993 1994 return net; 1995 } 1996 1997 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 1998 struct net *src_net, 1999 struct nlattr *tb[], int cap) 2000 { 2001 struct net *net; 2002 2003 net = rtnl_link_get_net_by_nlattr(src_net, tb); 2004 if (IS_ERR(net)) 2005 return net; 2006 2007 if (!netlink_ns_capable(skb, net->user_ns, cap)) { 2008 put_net(net); 2009 return ERR_PTR(-EPERM); 2010 } 2011 2012 return net; 2013 } 2014 2015 /* Verify that rtnetlink requests do not pass additional properties 2016 * potentially referring to different network namespaces. 2017 */ 2018 static int rtnl_ensure_unique_netns(struct nlattr *tb[], 2019 struct netlink_ext_ack *extack, 2020 bool netns_id_only) 2021 { 2022 2023 if (netns_id_only) { 2024 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 2025 return 0; 2026 2027 NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 2028 return -EOPNOTSUPP; 2029 } 2030 2031 if (tb[IFLA_IF_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 2032 goto invalid_attr; 2033 2034 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_FD])) 2035 goto invalid_attr; 2036 2037 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_IF_NETNSID] || tb[IFLA_NET_NS_PID])) 2038 goto invalid_attr; 2039 2040 return 0; 2041 2042 invalid_attr: 2043 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 2044 return -EINVAL; 2045 } 2046 2047 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 2048 { 2049 if (dev) { 2050 if (tb[IFLA_ADDRESS] && 2051 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 2052 return -EINVAL; 2053 2054 if (tb[IFLA_BROADCAST] && 2055 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 2056 return -EINVAL; 2057 } 2058 2059 if (tb[IFLA_AF_SPEC]) { 2060 struct nlattr *af; 2061 int rem, err; 2062 2063 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2064 const struct rtnl_af_ops *af_ops; 2065 2066 rcu_read_lock(); 2067 af_ops = rtnl_af_lookup(nla_type(af)); 2068 if (!af_ops) { 2069 rcu_read_unlock(); 2070 return -EAFNOSUPPORT; 2071 } 2072 2073 if (!af_ops->set_link_af) { 2074 rcu_read_unlock(); 2075 return -EOPNOTSUPP; 2076 } 2077 2078 if (af_ops->validate_link_af) { 2079 err = af_ops->validate_link_af(dev, af); 2080 if (err < 0) { 2081 rcu_read_unlock(); 2082 return err; 2083 } 2084 } 2085 2086 rcu_read_unlock(); 2087 } 2088 } 2089 2090 return 0; 2091 } 2092 2093 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2094 int guid_type) 2095 { 2096 const struct net_device_ops *ops = dev->netdev_ops; 2097 2098 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2099 } 2100 2101 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2102 { 2103 if (dev->type != ARPHRD_INFINIBAND) 2104 return -EOPNOTSUPP; 2105 2106 return handle_infiniband_guid(dev, ivt, guid_type); 2107 } 2108 2109 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2110 { 2111 const struct net_device_ops *ops = dev->netdev_ops; 2112 int err = -EINVAL; 2113 2114 if (tb[IFLA_VF_MAC]) { 2115 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 2116 2117 err = -EOPNOTSUPP; 2118 if (ops->ndo_set_vf_mac) 2119 err = ops->ndo_set_vf_mac(dev, ivm->vf, 2120 ivm->mac); 2121 if (err < 0) 2122 return err; 2123 } 2124 2125 if (tb[IFLA_VF_VLAN]) { 2126 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 2127 2128 err = -EOPNOTSUPP; 2129 if (ops->ndo_set_vf_vlan) 2130 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 2131 ivv->qos, 2132 htons(ETH_P_8021Q)); 2133 if (err < 0) 2134 return err; 2135 } 2136 2137 if (tb[IFLA_VF_VLAN_LIST]) { 2138 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 2139 struct nlattr *attr; 2140 int rem, len = 0; 2141 2142 err = -EOPNOTSUPP; 2143 if (!ops->ndo_set_vf_vlan) 2144 return err; 2145 2146 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 2147 if (nla_type(attr) != IFLA_VF_VLAN_INFO || 2148 nla_len(attr) < NLA_HDRLEN) { 2149 return -EINVAL; 2150 } 2151 if (len >= MAX_VLAN_LIST_LEN) 2152 return -EOPNOTSUPP; 2153 ivvl[len] = nla_data(attr); 2154 2155 len++; 2156 } 2157 if (len == 0) 2158 return -EINVAL; 2159 2160 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 2161 ivvl[0]->qos, ivvl[0]->vlan_proto); 2162 if (err < 0) 2163 return err; 2164 } 2165 2166 if (tb[IFLA_VF_TX_RATE]) { 2167 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2168 struct ifla_vf_info ivf; 2169 2170 err = -EOPNOTSUPP; 2171 if (ops->ndo_get_vf_config) 2172 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 2173 if (err < 0) 2174 return err; 2175 2176 err = -EOPNOTSUPP; 2177 if (ops->ndo_set_vf_rate) 2178 err = ops->ndo_set_vf_rate(dev, ivt->vf, 2179 ivf.min_tx_rate, 2180 ivt->rate); 2181 if (err < 0) 2182 return err; 2183 } 2184 2185 if (tb[IFLA_VF_RATE]) { 2186 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 2187 2188 err = -EOPNOTSUPP; 2189 if (ops->ndo_set_vf_rate) 2190 err = ops->ndo_set_vf_rate(dev, ivt->vf, 2191 ivt->min_tx_rate, 2192 ivt->max_tx_rate); 2193 if (err < 0) 2194 return err; 2195 } 2196 2197 if (tb[IFLA_VF_SPOOFCHK]) { 2198 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 2199 2200 err = -EOPNOTSUPP; 2201 if (ops->ndo_set_vf_spoofchk) 2202 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 2203 ivs->setting); 2204 if (err < 0) 2205 return err; 2206 } 2207 2208 if (tb[IFLA_VF_LINK_STATE]) { 2209 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 2210 2211 err = -EOPNOTSUPP; 2212 if (ops->ndo_set_vf_link_state) 2213 err = ops->ndo_set_vf_link_state(dev, ivl->vf, 2214 ivl->link_state); 2215 if (err < 0) 2216 return err; 2217 } 2218 2219 if (tb[IFLA_VF_RSS_QUERY_EN]) { 2220 struct ifla_vf_rss_query_en *ivrssq_en; 2221 2222 err = -EOPNOTSUPP; 2223 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2224 if (ops->ndo_set_vf_rss_query_en) 2225 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 2226 ivrssq_en->setting); 2227 if (err < 0) 2228 return err; 2229 } 2230 2231 if (tb[IFLA_VF_TRUST]) { 2232 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2233 2234 err = -EOPNOTSUPP; 2235 if (ops->ndo_set_vf_trust) 2236 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2237 if (err < 0) 2238 return err; 2239 } 2240 2241 if (tb[IFLA_VF_IB_NODE_GUID]) { 2242 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2243 2244 if (!ops->ndo_set_vf_guid) 2245 return -EOPNOTSUPP; 2246 2247 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2248 } 2249 2250 if (tb[IFLA_VF_IB_PORT_GUID]) { 2251 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2252 2253 if (!ops->ndo_set_vf_guid) 2254 return -EOPNOTSUPP; 2255 2256 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2257 } 2258 2259 return err; 2260 } 2261 2262 static int do_set_master(struct net_device *dev, int ifindex, 2263 struct netlink_ext_ack *extack) 2264 { 2265 struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2266 const struct net_device_ops *ops; 2267 int err; 2268 2269 if (upper_dev) { 2270 if (upper_dev->ifindex == ifindex) 2271 return 0; 2272 ops = upper_dev->netdev_ops; 2273 if (ops->ndo_del_slave) { 2274 err = ops->ndo_del_slave(upper_dev, dev); 2275 if (err) 2276 return err; 2277 } else { 2278 return -EOPNOTSUPP; 2279 } 2280 } 2281 2282 if (ifindex) { 2283 upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2284 if (!upper_dev) 2285 return -EINVAL; 2286 ops = upper_dev->netdev_ops; 2287 if (ops->ndo_add_slave) { 2288 err = ops->ndo_add_slave(upper_dev, dev, extack); 2289 if (err) 2290 return err; 2291 } else { 2292 return -EOPNOTSUPP; 2293 } 2294 } 2295 return 0; 2296 } 2297 2298 #define DO_SETLINK_MODIFIED 0x01 2299 /* notify flag means notify + modified. */ 2300 #define DO_SETLINK_NOTIFY 0x03 2301 static int do_setlink(const struct sk_buff *skb, 2302 struct net_device *dev, struct ifinfomsg *ifm, 2303 struct netlink_ext_ack *extack, 2304 struct nlattr **tb, char *ifname, int status) 2305 { 2306 const struct net_device_ops *ops = dev->netdev_ops; 2307 int err; 2308 2309 err = validate_linkmsg(dev, tb); 2310 if (err < 0) 2311 return err; 2312 2313 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_IF_NETNSID]) { 2314 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 2315 tb, CAP_NET_ADMIN); 2316 if (IS_ERR(net)) { 2317 err = PTR_ERR(net); 2318 goto errout; 2319 } 2320 2321 err = dev_change_net_namespace(dev, net, ifname); 2322 put_net(net); 2323 if (err) 2324 goto errout; 2325 status |= DO_SETLINK_MODIFIED; 2326 } 2327 2328 if (tb[IFLA_MAP]) { 2329 struct rtnl_link_ifmap *u_map; 2330 struct ifmap k_map; 2331 2332 if (!ops->ndo_set_config) { 2333 err = -EOPNOTSUPP; 2334 goto errout; 2335 } 2336 2337 if (!netif_device_present(dev)) { 2338 err = -ENODEV; 2339 goto errout; 2340 } 2341 2342 u_map = nla_data(tb[IFLA_MAP]); 2343 k_map.mem_start = (unsigned long) u_map->mem_start; 2344 k_map.mem_end = (unsigned long) u_map->mem_end; 2345 k_map.base_addr = (unsigned short) u_map->base_addr; 2346 k_map.irq = (unsigned char) u_map->irq; 2347 k_map.dma = (unsigned char) u_map->dma; 2348 k_map.port = (unsigned char) u_map->port; 2349 2350 err = ops->ndo_set_config(dev, &k_map); 2351 if (err < 0) 2352 goto errout; 2353 2354 status |= DO_SETLINK_NOTIFY; 2355 } 2356 2357 if (tb[IFLA_ADDRESS]) { 2358 struct sockaddr *sa; 2359 int len; 2360 2361 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2362 sizeof(*sa)); 2363 sa = kmalloc(len, GFP_KERNEL); 2364 if (!sa) { 2365 err = -ENOMEM; 2366 goto errout; 2367 } 2368 sa->sa_family = dev->type; 2369 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 2370 dev->addr_len); 2371 err = dev_set_mac_address(dev, sa); 2372 kfree(sa); 2373 if (err) 2374 goto errout; 2375 status |= DO_SETLINK_MODIFIED; 2376 } 2377 2378 if (tb[IFLA_MTU]) { 2379 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 2380 if (err < 0) 2381 goto errout; 2382 status |= DO_SETLINK_MODIFIED; 2383 } 2384 2385 if (tb[IFLA_GROUP]) { 2386 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2387 status |= DO_SETLINK_NOTIFY; 2388 } 2389 2390 /* 2391 * Interface selected by interface index but interface 2392 * name provided implies that a name change has been 2393 * requested. 2394 */ 2395 if (ifm->ifi_index > 0 && ifname[0]) { 2396 err = dev_change_name(dev, ifname); 2397 if (err < 0) 2398 goto errout; 2399 status |= DO_SETLINK_MODIFIED; 2400 } 2401 2402 if (tb[IFLA_IFALIAS]) { 2403 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 2404 nla_len(tb[IFLA_IFALIAS])); 2405 if (err < 0) 2406 goto errout; 2407 status |= DO_SETLINK_NOTIFY; 2408 } 2409 2410 if (tb[IFLA_BROADCAST]) { 2411 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2412 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 2413 } 2414 2415 if (ifm->ifi_flags || ifm->ifi_change) { 2416 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 2417 if (err < 0) 2418 goto errout; 2419 } 2420 2421 if (tb[IFLA_MASTER]) { 2422 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2423 if (err) 2424 goto errout; 2425 status |= DO_SETLINK_MODIFIED; 2426 } 2427 2428 if (tb[IFLA_CARRIER]) { 2429 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 2430 if (err) 2431 goto errout; 2432 status |= DO_SETLINK_MODIFIED; 2433 } 2434 2435 if (tb[IFLA_TXQLEN]) { 2436 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 2437 2438 err = dev_change_tx_queue_len(dev, value); 2439 if (err) 2440 goto errout; 2441 status |= DO_SETLINK_MODIFIED; 2442 } 2443 2444 if (tb[IFLA_GSO_MAX_SIZE]) { 2445 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 2446 2447 if (max_size > GSO_MAX_SIZE) { 2448 err = -EINVAL; 2449 goto errout; 2450 } 2451 2452 if (dev->gso_max_size ^ max_size) { 2453 netif_set_gso_max_size(dev, max_size); 2454 status |= DO_SETLINK_MODIFIED; 2455 } 2456 } 2457 2458 if (tb[IFLA_GSO_MAX_SEGS]) { 2459 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2460 2461 if (max_segs > GSO_MAX_SEGS) { 2462 err = -EINVAL; 2463 goto errout; 2464 } 2465 2466 if (dev->gso_max_segs ^ max_segs) { 2467 dev->gso_max_segs = max_segs; 2468 status |= DO_SETLINK_MODIFIED; 2469 } 2470 } 2471 2472 if (tb[IFLA_OPERSTATE]) 2473 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2474 2475 if (tb[IFLA_LINKMODE]) { 2476 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 2477 2478 write_lock_bh(&dev_base_lock); 2479 if (dev->link_mode ^ value) 2480 status |= DO_SETLINK_NOTIFY; 2481 dev->link_mode = value; 2482 write_unlock_bh(&dev_base_lock); 2483 } 2484 2485 if (tb[IFLA_VFINFO_LIST]) { 2486 struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2487 struct nlattr *attr; 2488 int rem; 2489 2490 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 2491 if (nla_type(attr) != IFLA_VF_INFO || 2492 nla_len(attr) < NLA_HDRLEN) { 2493 err = -EINVAL; 2494 goto errout; 2495 } 2496 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2497 ifla_vf_policy, NULL); 2498 if (err < 0) 2499 goto errout; 2500 err = do_setvfinfo(dev, vfinfo); 2501 if (err < 0) 2502 goto errout; 2503 status |= DO_SETLINK_NOTIFY; 2504 } 2505 } 2506 err = 0; 2507 2508 if (tb[IFLA_VF_PORTS]) { 2509 struct nlattr *port[IFLA_PORT_MAX+1]; 2510 struct nlattr *attr; 2511 int vf; 2512 int rem; 2513 2514 err = -EOPNOTSUPP; 2515 if (!ops->ndo_set_vf_port) 2516 goto errout; 2517 2518 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2519 if (nla_type(attr) != IFLA_VF_PORT || 2520 nla_len(attr) < NLA_HDRLEN) { 2521 err = -EINVAL; 2522 goto errout; 2523 } 2524 err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2525 ifla_port_policy, NULL); 2526 if (err < 0) 2527 goto errout; 2528 if (!port[IFLA_PORT_VF]) { 2529 err = -EOPNOTSUPP; 2530 goto errout; 2531 } 2532 vf = nla_get_u32(port[IFLA_PORT_VF]); 2533 err = ops->ndo_set_vf_port(dev, vf, port); 2534 if (err < 0) 2535 goto errout; 2536 status |= DO_SETLINK_NOTIFY; 2537 } 2538 } 2539 err = 0; 2540 2541 if (tb[IFLA_PORT_SELF]) { 2542 struct nlattr *port[IFLA_PORT_MAX+1]; 2543 2544 err = nla_parse_nested(port, IFLA_PORT_MAX, 2545 tb[IFLA_PORT_SELF], ifla_port_policy, 2546 NULL); 2547 if (err < 0) 2548 goto errout; 2549 2550 err = -EOPNOTSUPP; 2551 if (ops->ndo_set_vf_port) 2552 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 2553 if (err < 0) 2554 goto errout; 2555 status |= DO_SETLINK_NOTIFY; 2556 } 2557 2558 if (tb[IFLA_AF_SPEC]) { 2559 struct nlattr *af; 2560 int rem; 2561 2562 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2563 const struct rtnl_af_ops *af_ops; 2564 2565 rcu_read_lock(); 2566 2567 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2568 2569 err = af_ops->set_link_af(dev, af); 2570 if (err < 0) { 2571 rcu_read_unlock(); 2572 goto errout; 2573 } 2574 2575 rcu_read_unlock(); 2576 status |= DO_SETLINK_NOTIFY; 2577 } 2578 } 2579 err = 0; 2580 2581 if (tb[IFLA_PROTO_DOWN]) { 2582 err = dev_change_proto_down(dev, 2583 nla_get_u8(tb[IFLA_PROTO_DOWN])); 2584 if (err) 2585 goto errout; 2586 status |= DO_SETLINK_NOTIFY; 2587 } 2588 2589 if (tb[IFLA_XDP]) { 2590 struct nlattr *xdp[IFLA_XDP_MAX + 1]; 2591 u32 xdp_flags = 0; 2592 2593 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2594 ifla_xdp_policy, NULL); 2595 if (err < 0) 2596 goto errout; 2597 2598 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2599 err = -EINVAL; 2600 goto errout; 2601 } 2602 2603 if (xdp[IFLA_XDP_FLAGS]) { 2604 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 2605 if (xdp_flags & ~XDP_FLAGS_MASK) { 2606 err = -EINVAL; 2607 goto errout; 2608 } 2609 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 2610 err = -EINVAL; 2611 goto errout; 2612 } 2613 } 2614 2615 if (xdp[IFLA_XDP_FD]) { 2616 err = dev_change_xdp_fd(dev, extack, 2617 nla_get_s32(xdp[IFLA_XDP_FD]), 2618 xdp_flags); 2619 if (err) 2620 goto errout; 2621 status |= DO_SETLINK_NOTIFY; 2622 } 2623 } 2624 2625 errout: 2626 if (status & DO_SETLINK_MODIFIED) { 2627 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2628 netdev_state_change(dev); 2629 2630 if (err < 0) 2631 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", 2632 dev->name); 2633 } 2634 2635 return err; 2636 } 2637 2638 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2639 struct netlink_ext_ack *extack) 2640 { 2641 struct net *net = sock_net(skb->sk); 2642 struct ifinfomsg *ifm; 2643 struct net_device *dev; 2644 int err; 2645 struct nlattr *tb[IFLA_MAX+1]; 2646 char ifname[IFNAMSIZ]; 2647 2648 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2649 extack); 2650 if (err < 0) 2651 goto errout; 2652 2653 err = rtnl_ensure_unique_netns(tb, extack, false); 2654 if (err < 0) 2655 goto errout; 2656 2657 if (tb[IFLA_IFNAME]) 2658 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2659 else 2660 ifname[0] = '\0'; 2661 2662 err = -EINVAL; 2663 ifm = nlmsg_data(nlh); 2664 if (ifm->ifi_index > 0) 2665 dev = __dev_get_by_index(net, ifm->ifi_index); 2666 else if (tb[IFLA_IFNAME]) 2667 dev = __dev_get_by_name(net, ifname); 2668 else 2669 goto errout; 2670 2671 if (dev == NULL) { 2672 err = -ENODEV; 2673 goto errout; 2674 } 2675 2676 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2677 errout: 2678 return err; 2679 } 2680 2681 static int rtnl_group_dellink(const struct net *net, int group) 2682 { 2683 struct net_device *dev, *aux; 2684 LIST_HEAD(list_kill); 2685 bool found = false; 2686 2687 if (!group) 2688 return -EPERM; 2689 2690 for_each_netdev(net, dev) { 2691 if (dev->group == group) { 2692 const struct rtnl_link_ops *ops; 2693 2694 found = true; 2695 ops = dev->rtnl_link_ops; 2696 if (!ops || !ops->dellink) 2697 return -EOPNOTSUPP; 2698 } 2699 } 2700 2701 if (!found) 2702 return -ENODEV; 2703 2704 for_each_netdev_safe(net, dev, aux) { 2705 if (dev->group == group) { 2706 const struct rtnl_link_ops *ops; 2707 2708 ops = dev->rtnl_link_ops; 2709 ops->dellink(dev, &list_kill); 2710 } 2711 } 2712 unregister_netdevice_many(&list_kill); 2713 2714 return 0; 2715 } 2716 2717 int rtnl_delete_link(struct net_device *dev) 2718 { 2719 const struct rtnl_link_ops *ops; 2720 LIST_HEAD(list_kill); 2721 2722 ops = dev->rtnl_link_ops; 2723 if (!ops || !ops->dellink) 2724 return -EOPNOTSUPP; 2725 2726 ops->dellink(dev, &list_kill); 2727 unregister_netdevice_many(&list_kill); 2728 2729 return 0; 2730 } 2731 EXPORT_SYMBOL_GPL(rtnl_delete_link); 2732 2733 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2734 struct netlink_ext_ack *extack) 2735 { 2736 struct net *net = sock_net(skb->sk); 2737 struct net *tgt_net = net; 2738 struct net_device *dev = NULL; 2739 struct ifinfomsg *ifm; 2740 char ifname[IFNAMSIZ]; 2741 struct nlattr *tb[IFLA_MAX+1]; 2742 int err; 2743 int netnsid = -1; 2744 2745 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 2746 if (err < 0) 2747 return err; 2748 2749 err = rtnl_ensure_unique_netns(tb, extack, true); 2750 if (err < 0) 2751 return err; 2752 2753 if (tb[IFLA_IFNAME]) 2754 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2755 2756 if (tb[IFLA_IF_NETNSID]) { 2757 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 2758 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 2759 if (IS_ERR(tgt_net)) 2760 return PTR_ERR(tgt_net); 2761 } 2762 2763 err = -EINVAL; 2764 ifm = nlmsg_data(nlh); 2765 if (ifm->ifi_index > 0) 2766 dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 2767 else if (tb[IFLA_IFNAME]) 2768 dev = __dev_get_by_name(tgt_net, ifname); 2769 else if (tb[IFLA_GROUP]) 2770 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 2771 else 2772 goto out; 2773 2774 if (!dev) { 2775 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2776 err = -ENODEV; 2777 2778 goto out; 2779 } 2780 2781 err = rtnl_delete_link(dev); 2782 2783 out: 2784 if (netnsid >= 0) 2785 put_net(tgt_net); 2786 2787 return err; 2788 } 2789 2790 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 2791 { 2792 unsigned int old_flags; 2793 int err; 2794 2795 old_flags = dev->flags; 2796 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 2797 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 2798 if (err < 0) 2799 return err; 2800 } 2801 2802 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 2803 __dev_notify_flags(dev, old_flags, 0U); 2804 } else { 2805 dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2806 __dev_notify_flags(dev, old_flags, ~0U); 2807 } 2808 return 0; 2809 } 2810 EXPORT_SYMBOL(rtnl_configure_link); 2811 2812 struct net_device *rtnl_create_link(struct net *net, 2813 const char *ifname, unsigned char name_assign_type, 2814 const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2815 { 2816 struct net_device *dev; 2817 unsigned int num_tx_queues = 1; 2818 unsigned int num_rx_queues = 1; 2819 2820 if (tb[IFLA_NUM_TX_QUEUES]) 2821 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 2822 else if (ops->get_num_tx_queues) 2823 num_tx_queues = ops->get_num_tx_queues(); 2824 2825 if (tb[IFLA_NUM_RX_QUEUES]) 2826 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 2827 else if (ops->get_num_rx_queues) 2828 num_rx_queues = ops->get_num_rx_queues(); 2829 2830 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2831 ops->setup, num_tx_queues, num_rx_queues); 2832 if (!dev) 2833 return ERR_PTR(-ENOMEM); 2834 2835 dev_net_set(dev, net); 2836 dev->rtnl_link_ops = ops; 2837 dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 2838 2839 if (tb[IFLA_MTU]) 2840 dev->mtu = nla_get_u32(tb[IFLA_MTU]); 2841 if (tb[IFLA_ADDRESS]) { 2842 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2843 nla_len(tb[IFLA_ADDRESS])); 2844 dev->addr_assign_type = NET_ADDR_SET; 2845 } 2846 if (tb[IFLA_BROADCAST]) 2847 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2848 nla_len(tb[IFLA_BROADCAST])); 2849 if (tb[IFLA_TXQLEN]) 2850 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2851 if (tb[IFLA_OPERSTATE]) 2852 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2853 if (tb[IFLA_LINKMODE]) 2854 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2855 if (tb[IFLA_GROUP]) 2856 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2857 if (tb[IFLA_GSO_MAX_SIZE]) 2858 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 2859 if (tb[IFLA_GSO_MAX_SEGS]) 2860 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2861 2862 return dev; 2863 } 2864 EXPORT_SYMBOL(rtnl_create_link); 2865 2866 static int rtnl_group_changelink(const struct sk_buff *skb, 2867 struct net *net, int group, 2868 struct ifinfomsg *ifm, 2869 struct netlink_ext_ack *extack, 2870 struct nlattr **tb) 2871 { 2872 struct net_device *dev, *aux; 2873 int err; 2874 2875 for_each_netdev_safe(net, dev, aux) { 2876 if (dev->group == group) { 2877 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2878 if (err < 0) 2879 return err; 2880 } 2881 } 2882 2883 return 0; 2884 } 2885 2886 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2887 struct netlink_ext_ack *extack) 2888 { 2889 struct net *net = sock_net(skb->sk); 2890 const struct rtnl_link_ops *ops; 2891 const struct rtnl_link_ops *m_ops = NULL; 2892 struct net_device *dev; 2893 struct net_device *master_dev = NULL; 2894 struct ifinfomsg *ifm; 2895 char kind[MODULE_NAME_LEN]; 2896 char ifname[IFNAMSIZ]; 2897 struct nlattr *tb[IFLA_MAX+1]; 2898 struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 2899 unsigned char name_assign_type = NET_NAME_USER; 2900 int err; 2901 2902 #ifdef CONFIG_MODULES 2903 replay: 2904 #endif 2905 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 2906 if (err < 0) 2907 return err; 2908 2909 err = rtnl_ensure_unique_netns(tb, extack, false); 2910 if (err < 0) 2911 return err; 2912 2913 if (tb[IFLA_IFNAME]) 2914 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2915 else 2916 ifname[0] = '\0'; 2917 2918 ifm = nlmsg_data(nlh); 2919 if (ifm->ifi_index > 0) 2920 dev = __dev_get_by_index(net, ifm->ifi_index); 2921 else { 2922 if (ifname[0]) 2923 dev = __dev_get_by_name(net, ifname); 2924 else 2925 dev = NULL; 2926 } 2927 2928 if (dev) { 2929 master_dev = netdev_master_upper_dev_get(dev); 2930 if (master_dev) 2931 m_ops = master_dev->rtnl_link_ops; 2932 } 2933 2934 err = validate_linkmsg(dev, tb); 2935 if (err < 0) 2936 return err; 2937 2938 if (tb[IFLA_LINKINFO]) { 2939 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 2940 tb[IFLA_LINKINFO], ifla_info_policy, 2941 NULL); 2942 if (err < 0) 2943 return err; 2944 } else 2945 memset(linkinfo, 0, sizeof(linkinfo)); 2946 2947 if (linkinfo[IFLA_INFO_KIND]) { 2948 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 2949 ops = rtnl_link_ops_get(kind); 2950 } else { 2951 kind[0] = '\0'; 2952 ops = NULL; 2953 } 2954 2955 if (1) { 2956 struct nlattr *attr[RTNL_MAX_TYPE + 1]; 2957 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 2958 struct nlattr **data = NULL; 2959 struct nlattr **slave_data = NULL; 2960 struct net *dest_net, *link_net = NULL; 2961 2962 if (ops) { 2963 if (ops->maxtype > RTNL_MAX_TYPE) 2964 return -EINVAL; 2965 2966 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 2967 err = nla_parse_nested(attr, ops->maxtype, 2968 linkinfo[IFLA_INFO_DATA], 2969 ops->policy, NULL); 2970 if (err < 0) 2971 return err; 2972 data = attr; 2973 } 2974 if (ops->validate) { 2975 err = ops->validate(tb, data, extack); 2976 if (err < 0) 2977 return err; 2978 } 2979 } 2980 2981 if (m_ops) { 2982 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 2983 return -EINVAL; 2984 2985 if (m_ops->slave_maxtype && 2986 linkinfo[IFLA_INFO_SLAVE_DATA]) { 2987 err = nla_parse_nested(slave_attr, 2988 m_ops->slave_maxtype, 2989 linkinfo[IFLA_INFO_SLAVE_DATA], 2990 m_ops->slave_policy, 2991 NULL); 2992 if (err < 0) 2993 return err; 2994 slave_data = slave_attr; 2995 } 2996 } 2997 2998 if (dev) { 2999 int status = 0; 3000 3001 if (nlh->nlmsg_flags & NLM_F_EXCL) 3002 return -EEXIST; 3003 if (nlh->nlmsg_flags & NLM_F_REPLACE) 3004 return -EOPNOTSUPP; 3005 3006 if (linkinfo[IFLA_INFO_DATA]) { 3007 if (!ops || ops != dev->rtnl_link_ops || 3008 !ops->changelink) 3009 return -EOPNOTSUPP; 3010 3011 err = ops->changelink(dev, tb, data, extack); 3012 if (err < 0) 3013 return err; 3014 status |= DO_SETLINK_NOTIFY; 3015 } 3016 3017 if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3018 if (!m_ops || !m_ops->slave_changelink) 3019 return -EOPNOTSUPP; 3020 3021 err = m_ops->slave_changelink(master_dev, dev, 3022 tb, slave_data, 3023 extack); 3024 if (err < 0) 3025 return err; 3026 status |= DO_SETLINK_NOTIFY; 3027 } 3028 3029 return do_setlink(skb, dev, ifm, extack, tb, ifname, 3030 status); 3031 } 3032 3033 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3034 if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 3035 return rtnl_group_changelink(skb, net, 3036 nla_get_u32(tb[IFLA_GROUP]), 3037 ifm, extack, tb); 3038 return -ENODEV; 3039 } 3040 3041 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 3042 return -EOPNOTSUPP; 3043 3044 if (!ops) { 3045 #ifdef CONFIG_MODULES 3046 if (kind[0]) { 3047 __rtnl_unlock(); 3048 request_module("rtnl-link-%s", kind); 3049 rtnl_lock(); 3050 ops = rtnl_link_ops_get(kind); 3051 if (ops) 3052 goto replay; 3053 } 3054 #endif 3055 return -EOPNOTSUPP; 3056 } 3057 3058 if (!ops->setup) 3059 return -EOPNOTSUPP; 3060 3061 if (!ifname[0]) { 3062 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 3063 name_assign_type = NET_NAME_ENUM; 3064 } 3065 3066 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 3067 if (IS_ERR(dest_net)) 3068 return PTR_ERR(dest_net); 3069 3070 if (tb[IFLA_LINK_NETNSID]) { 3071 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3072 3073 link_net = get_net_ns_by_id(dest_net, id); 3074 if (!link_net) { 3075 err = -EINVAL; 3076 goto out; 3077 } 3078 err = -EPERM; 3079 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 3080 goto out; 3081 } 3082 3083 dev = rtnl_create_link(link_net ? : dest_net, ifname, 3084 name_assign_type, ops, tb); 3085 if (IS_ERR(dev)) { 3086 err = PTR_ERR(dev); 3087 goto out; 3088 } 3089 3090 dev->ifindex = ifm->ifi_index; 3091 3092 if (ops->newlink) { 3093 err = ops->newlink(link_net ? : net, dev, tb, data, 3094 extack); 3095 /* Drivers should call free_netdev() in ->destructor 3096 * and unregister it on failure after registration 3097 * so that device could be finally freed in rtnl_unlock. 3098 */ 3099 if (err < 0) { 3100 /* If device is not registered at all, free it now */ 3101 if (dev->reg_state == NETREG_UNINITIALIZED) 3102 free_netdev(dev); 3103 goto out; 3104 } 3105 } else { 3106 err = register_netdevice(dev); 3107 if (err < 0) { 3108 free_netdev(dev); 3109 goto out; 3110 } 3111 } 3112 err = rtnl_configure_link(dev, ifm); 3113 if (err < 0) 3114 goto out_unregister; 3115 if (link_net) { 3116 err = dev_change_net_namespace(dev, dest_net, ifname); 3117 if (err < 0) 3118 goto out_unregister; 3119 } 3120 if (tb[IFLA_MASTER]) { 3121 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), 3122 extack); 3123 if (err) 3124 goto out_unregister; 3125 } 3126 out: 3127 if (link_net) 3128 put_net(link_net); 3129 put_net(dest_net); 3130 return err; 3131 out_unregister: 3132 if (ops->newlink) { 3133 LIST_HEAD(list_kill); 3134 3135 ops->dellink(dev, &list_kill); 3136 unregister_netdevice_many(&list_kill); 3137 } else { 3138 unregister_netdevice(dev); 3139 } 3140 goto out; 3141 } 3142 } 3143 3144 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3145 struct netlink_ext_ack *extack) 3146 { 3147 struct net *net = sock_net(skb->sk); 3148 struct net *tgt_net = net; 3149 struct ifinfomsg *ifm; 3150 char ifname[IFNAMSIZ]; 3151 struct nlattr *tb[IFLA_MAX+1]; 3152 struct net_device *dev = NULL; 3153 struct sk_buff *nskb; 3154 int netnsid = -1; 3155 int err; 3156 u32 ext_filter_mask = 0; 3157 3158 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 3159 if (err < 0) 3160 return err; 3161 3162 err = rtnl_ensure_unique_netns(tb, extack, true); 3163 if (err < 0) 3164 return err; 3165 3166 if (tb[IFLA_IF_NETNSID]) { 3167 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 3168 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 3169 if (IS_ERR(tgt_net)) 3170 return PTR_ERR(tgt_net); 3171 } 3172 3173 if (tb[IFLA_IFNAME]) 3174 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3175 3176 if (tb[IFLA_EXT_MASK]) 3177 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3178 3179 err = -EINVAL; 3180 ifm = nlmsg_data(nlh); 3181 if (ifm->ifi_index > 0) 3182 dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3183 else if (tb[IFLA_IFNAME]) 3184 dev = __dev_get_by_name(tgt_net, ifname); 3185 else 3186 goto out; 3187 3188 err = -ENODEV; 3189 if (dev == NULL) 3190 goto out; 3191 3192 err = -ENOBUFS; 3193 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3194 if (nskb == NULL) 3195 goto out; 3196 3197 err = rtnl_fill_ifinfo(nskb, dev, net, 3198 RTM_NEWLINK, NETLINK_CB(skb).portid, 3199 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3200 0, NULL, 0, netnsid); 3201 if (err < 0) { 3202 /* -EMSGSIZE implies BUG in if_nlmsg_size */ 3203 WARN_ON(err == -EMSGSIZE); 3204 kfree_skb(nskb); 3205 } else 3206 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 3207 out: 3208 if (netnsid >= 0) 3209 put_net(tgt_net); 3210 3211 return err; 3212 } 3213 3214 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3215 { 3216 struct net *net = sock_net(skb->sk); 3217 struct net_device *dev; 3218 struct nlattr *tb[IFLA_MAX+1]; 3219 u32 ext_filter_mask = 0; 3220 u16 min_ifinfo_dump_size = 0; 3221 int hdrlen; 3222 3223 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3224 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3225 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3226 3227 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3228 if (tb[IFLA_EXT_MASK]) 3229 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3230 } 3231 3232 if (!ext_filter_mask) 3233 return NLMSG_GOODSIZE; 3234 /* 3235 * traverse the list of net devices and compute the minimum 3236 * buffer size based upon the filter mask. 3237 */ 3238 rcu_read_lock(); 3239 for_each_netdev_rcu(net, dev) { 3240 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3241 if_nlmsg_size(dev, 3242 ext_filter_mask)); 3243 } 3244 rcu_read_unlock(); 3245 3246 return nlmsg_total_size(min_ifinfo_dump_size); 3247 } 3248 3249 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 3250 { 3251 int idx; 3252 int s_idx = cb->family; 3253 3254 if (s_idx == 0) 3255 s_idx = 1; 3256 3257 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3258 struct rtnl_link **tab; 3259 int type = cb->nlh->nlmsg_type-RTM_BASE; 3260 struct rtnl_link *link; 3261 rtnl_dumpit_func dumpit; 3262 3263 if (idx < s_idx || idx == PF_PACKET) 3264 continue; 3265 3266 if (type < 0 || type >= RTM_NR_MSGTYPES) 3267 continue; 3268 3269 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3270 if (!tab) 3271 continue; 3272 3273 link = tab[type]; 3274 if (!link) 3275 continue; 3276 3277 dumpit = link->dumpit; 3278 if (!dumpit) 3279 continue; 3280 3281 if (idx > s_idx) { 3282 memset(&cb->args[0], 0, sizeof(cb->args)); 3283 cb->prev_seq = 0; 3284 cb->seq = 0; 3285 } 3286 if (dumpit(skb, cb)) 3287 break; 3288 } 3289 cb->family = idx; 3290 3291 return skb->len; 3292 } 3293 3294 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 3295 unsigned int change, 3296 u32 event, gfp_t flags, int *new_nsid, 3297 int new_ifindex) 3298 { 3299 struct net *net = dev_net(dev); 3300 struct sk_buff *skb; 3301 int err = -ENOBUFS; 3302 size_t if_info_size; 3303 3304 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 3305 if (skb == NULL) 3306 goto errout; 3307 3308 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 3309 type, 0, 0, change, 0, 0, event, 3310 new_nsid, new_ifindex, -1); 3311 if (err < 0) { 3312 /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 3313 WARN_ON(err == -EMSGSIZE); 3314 kfree_skb(skb); 3315 goto errout; 3316 } 3317 return skb; 3318 errout: 3319 if (err < 0) 3320 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3321 return NULL; 3322 } 3323 3324 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3325 { 3326 struct net *net = dev_net(dev); 3327 3328 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3329 } 3330 3331 static void rtmsg_ifinfo_event(int type, struct net_device *dev, 3332 unsigned int change, u32 event, 3333 gfp_t flags, int *new_nsid, int new_ifindex) 3334 { 3335 struct sk_buff *skb; 3336 3337 if (dev->reg_state != NETREG_REGISTERED) 3338 return; 3339 3340 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 3341 new_ifindex); 3342 if (skb) 3343 rtmsg_ifinfo_send(skb, dev, flags); 3344 } 3345 3346 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 3347 gfp_t flags) 3348 { 3349 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 3350 NULL, 0); 3351 } 3352 3353 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 3354 gfp_t flags, int *new_nsid, int new_ifindex) 3355 { 3356 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 3357 new_nsid, new_ifindex); 3358 } 3359 3360 static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3361 struct net_device *dev, 3362 u8 *addr, u16 vid, u32 pid, u32 seq, 3363 int type, unsigned int flags, 3364 int nlflags, u16 ndm_state) 3365 { 3366 struct nlmsghdr *nlh; 3367 struct ndmsg *ndm; 3368 3369 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3370 if (!nlh) 3371 return -EMSGSIZE; 3372 3373 ndm = nlmsg_data(nlh); 3374 ndm->ndm_family = AF_BRIDGE; 3375 ndm->ndm_pad1 = 0; 3376 ndm->ndm_pad2 = 0; 3377 ndm->ndm_flags = flags; 3378 ndm->ndm_type = 0; 3379 ndm->ndm_ifindex = dev->ifindex; 3380 ndm->ndm_state = ndm_state; 3381 3382 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3383 goto nla_put_failure; 3384 if (vid) 3385 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 3386 goto nla_put_failure; 3387 3388 nlmsg_end(skb, nlh); 3389 return 0; 3390 3391 nla_put_failure: 3392 nlmsg_cancel(skb, nlh); 3393 return -EMSGSIZE; 3394 } 3395 3396 static inline size_t rtnl_fdb_nlmsg_size(void) 3397 { 3398 return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3399 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3400 nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3401 0; 3402 } 3403 3404 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3405 u16 ndm_state) 3406 { 3407 struct net *net = dev_net(dev); 3408 struct sk_buff *skb; 3409 int err = -ENOBUFS; 3410 3411 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 3412 if (!skb) 3413 goto errout; 3414 3415 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3416 0, 0, type, NTF_SELF, 0, ndm_state); 3417 if (err < 0) { 3418 kfree_skb(skb); 3419 goto errout; 3420 } 3421 3422 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 3423 return; 3424 errout: 3425 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 3426 } 3427 3428 /** 3429 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3430 */ 3431 int ndo_dflt_fdb_add(struct ndmsg *ndm, 3432 struct nlattr *tb[], 3433 struct net_device *dev, 3434 const unsigned char *addr, u16 vid, 3435 u16 flags) 3436 { 3437 int err = -EINVAL; 3438 3439 /* If aging addresses are supported device will need to 3440 * implement its own handler for this. 3441 */ 3442 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3443 pr_info("%s: FDB only supports static addresses\n", dev->name); 3444 return err; 3445 } 3446 3447 if (vid) { 3448 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 3449 return err; 3450 } 3451 3452 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3453 err = dev_uc_add_excl(dev, addr); 3454 else if (is_multicast_ether_addr(addr)) 3455 err = dev_mc_add_excl(dev, addr); 3456 3457 /* Only return duplicate errors if NLM_F_EXCL is set */ 3458 if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3459 err = 0; 3460 3461 return err; 3462 } 3463 EXPORT_SYMBOL(ndo_dflt_fdb_add); 3464 3465 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3466 struct netlink_ext_ack *extack) 3467 { 3468 u16 vid = 0; 3469 3470 if (vlan_attr) { 3471 if (nla_len(vlan_attr) != sizeof(u16)) { 3472 NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3473 return -EINVAL; 3474 } 3475 3476 vid = nla_get_u16(vlan_attr); 3477 3478 if (!vid || vid >= VLAN_VID_MASK) { 3479 NL_SET_ERR_MSG(extack, "invalid vlan id"); 3480 return -EINVAL; 3481 } 3482 } 3483 *p_vid = vid; 3484 return 0; 3485 } 3486 3487 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3488 struct netlink_ext_ack *extack) 3489 { 3490 struct net *net = sock_net(skb->sk); 3491 struct ndmsg *ndm; 3492 struct nlattr *tb[NDA_MAX+1]; 3493 struct net_device *dev; 3494 u8 *addr; 3495 u16 vid; 3496 int err; 3497 3498 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 3499 if (err < 0) 3500 return err; 3501 3502 ndm = nlmsg_data(nlh); 3503 if (ndm->ndm_ifindex == 0) { 3504 NL_SET_ERR_MSG(extack, "invalid ifindex"); 3505 return -EINVAL; 3506 } 3507 3508 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 3509 if (dev == NULL) { 3510 NL_SET_ERR_MSG(extack, "unknown ifindex"); 3511 return -ENODEV; 3512 } 3513 3514 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3515 NL_SET_ERR_MSG(extack, "invalid address"); 3516 return -EINVAL; 3517 } 3518 3519 addr = nla_data(tb[NDA_LLADDR]); 3520 3521 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3522 if (err) 3523 return err; 3524 3525 err = -EOPNOTSUPP; 3526 3527 /* Support fdb on master device the net/bridge default case */ 3528 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 3529 (dev->priv_flags & IFF_BRIDGE_PORT)) { 3530 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3531 const struct net_device_ops *ops = br_dev->netdev_ops; 3532 3533 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3534 nlh->nlmsg_flags); 3535 if (err) 3536 goto out; 3537 else 3538 ndm->ndm_flags &= ~NTF_MASTER; 3539 } 3540 3541 /* Embedded bridge, macvlan, and any other device support */ 3542 if ((ndm->ndm_flags & NTF_SELF)) { 3543 if (dev->netdev_ops->ndo_fdb_add) 3544 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3545 vid, 3546 nlh->nlmsg_flags); 3547 else 3548 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 3549 nlh->nlmsg_flags); 3550 3551 if (!err) { 3552 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3553 ndm->ndm_state); 3554 ndm->ndm_flags &= ~NTF_SELF; 3555 } 3556 } 3557 out: 3558 return err; 3559 } 3560 3561 /** 3562 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3563 */ 3564 int ndo_dflt_fdb_del(struct ndmsg *ndm, 3565 struct nlattr *tb[], 3566 struct net_device *dev, 3567 const unsigned char *addr, u16 vid) 3568 { 3569 int err = -EINVAL; 3570 3571 /* If aging addresses are supported device will need to 3572 * implement its own handler for this. 3573 */ 3574 if (!(ndm->ndm_state & NUD_PERMANENT)) { 3575 pr_info("%s: FDB only supports static addresses\n", dev->name); 3576 return err; 3577 } 3578 3579 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3580 err = dev_uc_del(dev, addr); 3581 else if (is_multicast_ether_addr(addr)) 3582 err = dev_mc_del(dev, addr); 3583 3584 return err; 3585 } 3586 EXPORT_SYMBOL(ndo_dflt_fdb_del); 3587 3588 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3589 struct netlink_ext_ack *extack) 3590 { 3591 struct net *net = sock_net(skb->sk); 3592 struct ndmsg *ndm; 3593 struct nlattr *tb[NDA_MAX+1]; 3594 struct net_device *dev; 3595 int err = -EINVAL; 3596 __u8 *addr; 3597 u16 vid; 3598 3599 if (!netlink_capable(skb, CAP_NET_ADMIN)) 3600 return -EPERM; 3601 3602 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 3603 if (err < 0) 3604 return err; 3605 3606 ndm = nlmsg_data(nlh); 3607 if (ndm->ndm_ifindex == 0) { 3608 NL_SET_ERR_MSG(extack, "invalid ifindex"); 3609 return -EINVAL; 3610 } 3611 3612 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 3613 if (dev == NULL) { 3614 NL_SET_ERR_MSG(extack, "unknown ifindex"); 3615 return -ENODEV; 3616 } 3617 3618 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3619 NL_SET_ERR_MSG(extack, "invalid address"); 3620 return -EINVAL; 3621 } 3622 3623 addr = nla_data(tb[NDA_LLADDR]); 3624 3625 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3626 if (err) 3627 return err; 3628 3629 err = -EOPNOTSUPP; 3630 3631 /* Support fdb on master device the net/bridge default case */ 3632 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 3633 (dev->priv_flags & IFF_BRIDGE_PORT)) { 3634 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3635 const struct net_device_ops *ops = br_dev->netdev_ops; 3636 3637 if (ops->ndo_fdb_del) 3638 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 3639 3640 if (err) 3641 goto out; 3642 else 3643 ndm->ndm_flags &= ~NTF_MASTER; 3644 } 3645 3646 /* Embedded bridge, macvlan, and any other device support */ 3647 if (ndm->ndm_flags & NTF_SELF) { 3648 if (dev->netdev_ops->ndo_fdb_del) 3649 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3650 vid); 3651 else 3652 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 3653 3654 if (!err) { 3655 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3656 ndm->ndm_state); 3657 ndm->ndm_flags &= ~NTF_SELF; 3658 } 3659 } 3660 out: 3661 return err; 3662 } 3663 3664 static int nlmsg_populate_fdb(struct sk_buff *skb, 3665 struct netlink_callback *cb, 3666 struct net_device *dev, 3667 int *idx, 3668 struct netdev_hw_addr_list *list) 3669 { 3670 struct netdev_hw_addr *ha; 3671 int err; 3672 u32 portid, seq; 3673 3674 portid = NETLINK_CB(cb->skb).portid; 3675 seq = cb->nlh->nlmsg_seq; 3676 3677 list_for_each_entry(ha, &list->list, list) { 3678 if (*idx < cb->args[2]) 3679 goto skip; 3680 3681 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3682 portid, seq, 3683 RTM_NEWNEIGH, NTF_SELF, 3684 NLM_F_MULTI, NUD_PERMANENT); 3685 if (err < 0) 3686 return err; 3687 skip: 3688 *idx += 1; 3689 } 3690 return 0; 3691 } 3692 3693 /** 3694 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3695 * @nlh: netlink message header 3696 * @dev: netdevice 3697 * 3698 * Default netdevice operation to dump the existing unicast address list. 3699 * Returns number of addresses from list put in skb. 3700 */ 3701 int ndo_dflt_fdb_dump(struct sk_buff *skb, 3702 struct netlink_callback *cb, 3703 struct net_device *dev, 3704 struct net_device *filter_dev, 3705 int *idx) 3706 { 3707 int err; 3708 3709 netif_addr_lock_bh(dev); 3710 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3711 if (err) 3712 goto out; 3713 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3714 out: 3715 netif_addr_unlock_bh(dev); 3716 return err; 3717 } 3718 EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3719 3720 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 3721 { 3722 struct net_device *dev; 3723 struct nlattr *tb[IFLA_MAX+1]; 3724 struct net_device *br_dev = NULL; 3725 const struct net_device_ops *ops = NULL; 3726 const struct net_device_ops *cops = NULL; 3727 struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 3728 struct net *net = sock_net(skb->sk); 3729 struct hlist_head *head; 3730 int brport_idx = 0; 3731 int br_idx = 0; 3732 int h, s_h; 3733 int idx = 0, s_idx; 3734 int err = 0; 3735 int fidx = 0; 3736 3737 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, 3738 IFLA_MAX, ifla_policy, NULL); 3739 if (err < 0) { 3740 return -EINVAL; 3741 } else if (err == 0) { 3742 if (tb[IFLA_MASTER]) 3743 br_idx = nla_get_u32(tb[IFLA_MASTER]); 3744 } 3745 3746 brport_idx = ifm->ifi_index; 3747 3748 if (br_idx) { 3749 br_dev = __dev_get_by_index(net, br_idx); 3750 if (!br_dev) 3751 return -ENODEV; 3752 3753 ops = br_dev->netdev_ops; 3754 } 3755 3756 s_h = cb->args[0]; 3757 s_idx = cb->args[1]; 3758 3759 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3760 idx = 0; 3761 head = &net->dev_index_head[h]; 3762 hlist_for_each_entry(dev, head, index_hlist) { 3763 3764 if (brport_idx && (dev->ifindex != brport_idx)) 3765 continue; 3766 3767 if (!br_idx) { /* user did not specify a specific bridge */ 3768 if (dev->priv_flags & IFF_BRIDGE_PORT) { 3769 br_dev = netdev_master_upper_dev_get(dev); 3770 cops = br_dev->netdev_ops; 3771 } 3772 } else { 3773 if (dev != br_dev && 3774 !(dev->priv_flags & IFF_BRIDGE_PORT)) 3775 continue; 3776 3777 if (br_dev != netdev_master_upper_dev_get(dev) && 3778 !(dev->priv_flags & IFF_EBRIDGE)) 3779 continue; 3780 cops = ops; 3781 } 3782 3783 if (idx < s_idx) 3784 goto cont; 3785 3786 if (dev->priv_flags & IFF_BRIDGE_PORT) { 3787 if (cops && cops->ndo_fdb_dump) { 3788 err = cops->ndo_fdb_dump(skb, cb, 3789 br_dev, dev, 3790 &fidx); 3791 if (err == -EMSGSIZE) 3792 goto out; 3793 } 3794 } 3795 3796 if (dev->netdev_ops->ndo_fdb_dump) 3797 err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3798 dev, NULL, 3799 &fidx); 3800 else 3801 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 3802 &fidx); 3803 if (err == -EMSGSIZE) 3804 goto out; 3805 3806 cops = NULL; 3807 3808 /* reset fdb offset to 0 for rest of the interfaces */ 3809 cb->args[2] = 0; 3810 fidx = 0; 3811 cont: 3812 idx++; 3813 } 3814 } 3815 3816 out: 3817 cb->args[0] = h; 3818 cb->args[1] = idx; 3819 cb->args[2] = fidx; 3820 3821 return skb->len; 3822 } 3823 3824 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 3825 unsigned int attrnum, unsigned int flag) 3826 { 3827 if (mask & flag) 3828 return nla_put_u8(skb, attrnum, !!(flags & flag)); 3829 return 0; 3830 } 3831 3832 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 3833 struct net_device *dev, u16 mode, 3834 u32 flags, u32 mask, int nlflags, 3835 u32 filter_mask, 3836 int (*vlan_fill)(struct sk_buff *skb, 3837 struct net_device *dev, 3838 u32 filter_mask)) 3839 { 3840 struct nlmsghdr *nlh; 3841 struct ifinfomsg *ifm; 3842 struct nlattr *br_afspec; 3843 struct nlattr *protinfo; 3844 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 3845 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3846 int err = 0; 3847 3848 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 3849 if (nlh == NULL) 3850 return -EMSGSIZE; 3851 3852 ifm = nlmsg_data(nlh); 3853 ifm->ifi_family = AF_BRIDGE; 3854 ifm->__ifi_pad = 0; 3855 ifm->ifi_type = dev->type; 3856 ifm->ifi_index = dev->ifindex; 3857 ifm->ifi_flags = dev_get_flags(dev); 3858 ifm->ifi_change = 0; 3859 3860 3861 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 3862 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 3863 nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 3864 (br_dev && 3865 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 3866 (dev->addr_len && 3867 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 3868 (dev->ifindex != dev_get_iflink(dev) && 3869 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3870 goto nla_put_failure; 3871 3872 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3873 if (!br_afspec) 3874 goto nla_put_failure; 3875 3876 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3877 nla_nest_cancel(skb, br_afspec); 3878 goto nla_put_failure; 3879 } 3880 3881 if (mode != BRIDGE_MODE_UNDEF) { 3882 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 3883 nla_nest_cancel(skb, br_afspec); 3884 goto nla_put_failure; 3885 } 3886 } 3887 if (vlan_fill) { 3888 err = vlan_fill(skb, dev, filter_mask); 3889 if (err) { 3890 nla_nest_cancel(skb, br_afspec); 3891 goto nla_put_failure; 3892 } 3893 } 3894 nla_nest_end(skb, br_afspec); 3895 3896 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 3897 if (!protinfo) 3898 goto nla_put_failure; 3899 3900 if (brport_nla_put_flag(skb, flags, mask, 3901 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 3902 brport_nla_put_flag(skb, flags, mask, 3903 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 3904 brport_nla_put_flag(skb, flags, mask, 3905 IFLA_BRPORT_FAST_LEAVE, 3906 BR_MULTICAST_FAST_LEAVE) || 3907 brport_nla_put_flag(skb, flags, mask, 3908 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 3909 brport_nla_put_flag(skb, flags, mask, 3910 IFLA_BRPORT_LEARNING, BR_LEARNING) || 3911 brport_nla_put_flag(skb, flags, mask, 3912 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 3913 brport_nla_put_flag(skb, flags, mask, 3914 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 3915 brport_nla_put_flag(skb, flags, mask, 3916 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 3917 nla_nest_cancel(skb, protinfo); 3918 goto nla_put_failure; 3919 } 3920 3921 nla_nest_end(skb, protinfo); 3922 3923 nlmsg_end(skb, nlh); 3924 return 0; 3925 nla_put_failure: 3926 nlmsg_cancel(skb, nlh); 3927 return err ? err : -EMSGSIZE; 3928 } 3929 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 3930 3931 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 3932 { 3933 struct net *net = sock_net(skb->sk); 3934 struct net_device *dev; 3935 int idx = 0; 3936 u32 portid = NETLINK_CB(cb->skb).portid; 3937 u32 seq = cb->nlh->nlmsg_seq; 3938 u32 filter_mask = 0; 3939 int err; 3940 3941 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 3942 struct nlattr *extfilt; 3943 3944 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 3945 IFLA_EXT_MASK); 3946 if (extfilt) { 3947 if (nla_len(extfilt) < sizeof(filter_mask)) 3948 return -EINVAL; 3949 3950 filter_mask = nla_get_u32(extfilt); 3951 } 3952 } 3953 3954 rcu_read_lock(); 3955 for_each_netdev_rcu(net, dev) { 3956 const struct net_device_ops *ops = dev->netdev_ops; 3957 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3958 3959 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 3960 if (idx >= cb->args[0]) { 3961 err = br_dev->netdev_ops->ndo_bridge_getlink( 3962 skb, portid, seq, dev, 3963 filter_mask, NLM_F_MULTI); 3964 if (err < 0 && err != -EOPNOTSUPP) { 3965 if (likely(skb->len)) 3966 break; 3967 3968 goto out_err; 3969 } 3970 } 3971 idx++; 3972 } 3973 3974 if (ops->ndo_bridge_getlink) { 3975 if (idx >= cb->args[0]) { 3976 err = ops->ndo_bridge_getlink(skb, portid, 3977 seq, dev, 3978 filter_mask, 3979 NLM_F_MULTI); 3980 if (err < 0 && err != -EOPNOTSUPP) { 3981 if (likely(skb->len)) 3982 break; 3983 3984 goto out_err; 3985 } 3986 } 3987 idx++; 3988 } 3989 } 3990 err = skb->len; 3991 out_err: 3992 rcu_read_unlock(); 3993 cb->args[0] = idx; 3994 3995 return err; 3996 } 3997 3998 static inline size_t bridge_nlmsg_size(void) 3999 { 4000 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4001 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4002 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4003 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 4004 + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 4005 + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 4006 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 4007 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 4008 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 4009 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 4010 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 4011 } 4012 4013 static int rtnl_bridge_notify(struct net_device *dev) 4014 { 4015 struct net *net = dev_net(dev); 4016 struct sk_buff *skb; 4017 int err = -EOPNOTSUPP; 4018 4019 if (!dev->netdev_ops->ndo_bridge_getlink) 4020 return 0; 4021 4022 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 4023 if (!skb) { 4024 err = -ENOMEM; 4025 goto errout; 4026 } 4027 4028 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4029 if (err < 0) 4030 goto errout; 4031 4032 if (!skb->len) 4033 goto errout; 4034 4035 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 4036 return 0; 4037 errout: 4038 WARN_ON(err == -EMSGSIZE); 4039 kfree_skb(skb); 4040 if (err) 4041 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 4042 return err; 4043 } 4044 4045 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4046 struct netlink_ext_ack *extack) 4047 { 4048 struct net *net = sock_net(skb->sk); 4049 struct ifinfomsg *ifm; 4050 struct net_device *dev; 4051 struct nlattr *br_spec, *attr = NULL; 4052 int rem, err = -EOPNOTSUPP; 4053 u16 flags = 0; 4054 bool have_flags = false; 4055 4056 if (nlmsg_len(nlh) < sizeof(*ifm)) 4057 return -EINVAL; 4058 4059 ifm = nlmsg_data(nlh); 4060 if (ifm->ifi_family != AF_BRIDGE) 4061 return -EPFNOSUPPORT; 4062 4063 dev = __dev_get_by_index(net, ifm->ifi_index); 4064 if (!dev) { 4065 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4066 return -ENODEV; 4067 } 4068 4069 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4070 if (br_spec) { 4071 nla_for_each_nested(attr, br_spec, rem) { 4072 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 4073 if (nla_len(attr) < sizeof(flags)) 4074 return -EINVAL; 4075 4076 have_flags = true; 4077 flags = nla_get_u16(attr); 4078 break; 4079 } 4080 } 4081 } 4082 4083 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4084 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4085 4086 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 4087 err = -EOPNOTSUPP; 4088 goto out; 4089 } 4090 4091 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 4092 if (err) 4093 goto out; 4094 4095 flags &= ~BRIDGE_FLAGS_MASTER; 4096 } 4097 4098 if ((flags & BRIDGE_FLAGS_SELF)) { 4099 if (!dev->netdev_ops->ndo_bridge_setlink) 4100 err = -EOPNOTSUPP; 4101 else 4102 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 4103 flags); 4104 if (!err) { 4105 flags &= ~BRIDGE_FLAGS_SELF; 4106 4107 /* Generate event to notify upper layer of bridge 4108 * change 4109 */ 4110 err = rtnl_bridge_notify(dev); 4111 } 4112 } 4113 4114 if (have_flags) 4115 memcpy(nla_data(attr), &flags, sizeof(flags)); 4116 out: 4117 return err; 4118 } 4119 4120 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4121 struct netlink_ext_ack *extack) 4122 { 4123 struct net *net = sock_net(skb->sk); 4124 struct ifinfomsg *ifm; 4125 struct net_device *dev; 4126 struct nlattr *br_spec, *attr = NULL; 4127 int rem, err = -EOPNOTSUPP; 4128 u16 flags = 0; 4129 bool have_flags = false; 4130 4131 if (nlmsg_len(nlh) < sizeof(*ifm)) 4132 return -EINVAL; 4133 4134 ifm = nlmsg_data(nlh); 4135 if (ifm->ifi_family != AF_BRIDGE) 4136 return -EPFNOSUPPORT; 4137 4138 dev = __dev_get_by_index(net, ifm->ifi_index); 4139 if (!dev) { 4140 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4141 return -ENODEV; 4142 } 4143 4144 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4145 if (br_spec) { 4146 nla_for_each_nested(attr, br_spec, rem) { 4147 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 4148 if (nla_len(attr) < sizeof(flags)) 4149 return -EINVAL; 4150 4151 have_flags = true; 4152 flags = nla_get_u16(attr); 4153 break; 4154 } 4155 } 4156 } 4157 4158 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4159 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4160 4161 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4162 err = -EOPNOTSUPP; 4163 goto out; 4164 } 4165 4166 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4167 if (err) 4168 goto out; 4169 4170 flags &= ~BRIDGE_FLAGS_MASTER; 4171 } 4172 4173 if ((flags & BRIDGE_FLAGS_SELF)) { 4174 if (!dev->netdev_ops->ndo_bridge_dellink) 4175 err = -EOPNOTSUPP; 4176 else 4177 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4178 flags); 4179 4180 if (!err) { 4181 flags &= ~BRIDGE_FLAGS_SELF; 4182 4183 /* Generate event to notify upper layer of bridge 4184 * change 4185 */ 4186 err = rtnl_bridge_notify(dev); 4187 } 4188 } 4189 4190 if (have_flags) 4191 memcpy(nla_data(attr), &flags, sizeof(flags)); 4192 out: 4193 return err; 4194 } 4195 4196 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4197 { 4198 return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4199 (!idxattr || idxattr == attrid); 4200 } 4201 4202 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 4203 static int rtnl_get_offload_stats_attr_size(int attr_id) 4204 { 4205 switch (attr_id) { 4206 case IFLA_OFFLOAD_XSTATS_CPU_HIT: 4207 return sizeof(struct rtnl_link_stats64); 4208 } 4209 4210 return 0; 4211 } 4212 4213 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 4214 int *prividx) 4215 { 4216 struct nlattr *attr = NULL; 4217 int attr_id, size; 4218 void *attr_data; 4219 int err; 4220 4221 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 4222 dev->netdev_ops->ndo_get_offload_stats)) 4223 return -ENODATA; 4224 4225 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 4226 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 4227 if (attr_id < *prividx) 4228 continue; 4229 4230 size = rtnl_get_offload_stats_attr_size(attr_id); 4231 if (!size) 4232 continue; 4233 4234 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 4235 continue; 4236 4237 attr = nla_reserve_64bit(skb, attr_id, size, 4238 IFLA_OFFLOAD_XSTATS_UNSPEC); 4239 if (!attr) 4240 goto nla_put_failure; 4241 4242 attr_data = nla_data(attr); 4243 memset(attr_data, 0, size); 4244 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 4245 attr_data); 4246 if (err) 4247 goto get_offload_stats_failure; 4248 } 4249 4250 if (!attr) 4251 return -ENODATA; 4252 4253 *prividx = 0; 4254 return 0; 4255 4256 nla_put_failure: 4257 err = -EMSGSIZE; 4258 get_offload_stats_failure: 4259 *prividx = attr_id; 4260 return err; 4261 } 4262 4263 static int rtnl_get_offload_stats_size(const struct net_device *dev) 4264 { 4265 int nla_size = 0; 4266 int attr_id; 4267 int size; 4268 4269 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 4270 dev->netdev_ops->ndo_get_offload_stats)) 4271 return 0; 4272 4273 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 4274 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 4275 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 4276 continue; 4277 size = rtnl_get_offload_stats_attr_size(attr_id); 4278 nla_size += nla_total_size_64bit(size); 4279 } 4280 4281 if (nla_size != 0) 4282 nla_size += nla_total_size(0); 4283 4284 return nla_size; 4285 } 4286 4287 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 4288 int type, u32 pid, u32 seq, u32 change, 4289 unsigned int flags, unsigned int filter_mask, 4290 int *idxattr, int *prividx) 4291 { 4292 struct if_stats_msg *ifsm; 4293 struct nlmsghdr *nlh; 4294 struct nlattr *attr; 4295 int s_prividx = *prividx; 4296 int err; 4297 4298 ASSERT_RTNL(); 4299 4300 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 4301 if (!nlh) 4302 return -EMSGSIZE; 4303 4304 ifsm = nlmsg_data(nlh); 4305 ifsm->family = PF_UNSPEC; 4306 ifsm->pad1 = 0; 4307 ifsm->pad2 = 0; 4308 ifsm->ifindex = dev->ifindex; 4309 ifsm->filter_mask = filter_mask; 4310 4311 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 4312 struct rtnl_link_stats64 *sp; 4313 4314 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 4315 sizeof(struct rtnl_link_stats64), 4316 IFLA_STATS_UNSPEC); 4317 if (!attr) 4318 goto nla_put_failure; 4319 4320 sp = nla_data(attr); 4321 dev_get_stats(dev, sp); 4322 } 4323 4324 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 4325 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 4326 4327 if (ops && ops->fill_linkxstats) { 4328 *idxattr = IFLA_STATS_LINK_XSTATS; 4329 attr = nla_nest_start(skb, 4330 IFLA_STATS_LINK_XSTATS); 4331 if (!attr) 4332 goto nla_put_failure; 4333 4334 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 4335 nla_nest_end(skb, attr); 4336 if (err) 4337 goto nla_put_failure; 4338 *idxattr = 0; 4339 } 4340 } 4341 4342 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 4343 *idxattr)) { 4344 const struct rtnl_link_ops *ops = NULL; 4345 const struct net_device *master; 4346 4347 master = netdev_master_upper_dev_get(dev); 4348 if (master) 4349 ops = master->rtnl_link_ops; 4350 if (ops && ops->fill_linkxstats) { 4351 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 4352 attr = nla_nest_start(skb, 4353 IFLA_STATS_LINK_XSTATS_SLAVE); 4354 if (!attr) 4355 goto nla_put_failure; 4356 4357 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 4358 nla_nest_end(skb, attr); 4359 if (err) 4360 goto nla_put_failure; 4361 *idxattr = 0; 4362 } 4363 } 4364 4365 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 4366 *idxattr)) { 4367 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 4368 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 4369 if (!attr) 4370 goto nla_put_failure; 4371 4372 err = rtnl_get_offload_stats(skb, dev, prividx); 4373 if (err == -ENODATA) 4374 nla_nest_cancel(skb, attr); 4375 else 4376 nla_nest_end(skb, attr); 4377 4378 if (err && err != -ENODATA) 4379 goto nla_put_failure; 4380 *idxattr = 0; 4381 } 4382 4383 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4384 struct rtnl_af_ops *af_ops; 4385 4386 *idxattr = IFLA_STATS_AF_SPEC; 4387 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4388 if (!attr) 4389 goto nla_put_failure; 4390 4391 rcu_read_lock(); 4392 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4393 if (af_ops->fill_stats_af) { 4394 struct nlattr *af; 4395 int err; 4396 4397 af = nla_nest_start(skb, af_ops->family); 4398 if (!af) { 4399 rcu_read_unlock(); 4400 goto nla_put_failure; 4401 } 4402 err = af_ops->fill_stats_af(skb, dev); 4403 4404 if (err == -ENODATA) { 4405 nla_nest_cancel(skb, af); 4406 } else if (err < 0) { 4407 rcu_read_unlock(); 4408 goto nla_put_failure; 4409 } 4410 4411 nla_nest_end(skb, af); 4412 } 4413 } 4414 rcu_read_unlock(); 4415 4416 nla_nest_end(skb, attr); 4417 4418 *idxattr = 0; 4419 } 4420 4421 nlmsg_end(skb, nlh); 4422 4423 return 0; 4424 4425 nla_put_failure: 4426 /* not a multi message or no progress mean a real error */ 4427 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 4428 nlmsg_cancel(skb, nlh); 4429 else 4430 nlmsg_end(skb, nlh); 4431 4432 return -EMSGSIZE; 4433 } 4434 4435 static size_t if_nlmsg_stats_size(const struct net_device *dev, 4436 u32 filter_mask) 4437 { 4438 size_t size = 0; 4439 4440 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 4441 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 4442 4443 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 4444 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 4445 int attr = IFLA_STATS_LINK_XSTATS; 4446 4447 if (ops && ops->get_linkxstats_size) { 4448 size += nla_total_size(ops->get_linkxstats_size(dev, 4449 attr)); 4450 /* for IFLA_STATS_LINK_XSTATS */ 4451 size += nla_total_size(0); 4452 } 4453 } 4454 4455 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 4456 struct net_device *_dev = (struct net_device *)dev; 4457 const struct rtnl_link_ops *ops = NULL; 4458 const struct net_device *master; 4459 4460 /* netdev_master_upper_dev_get can't take const */ 4461 master = netdev_master_upper_dev_get(_dev); 4462 if (master) 4463 ops = master->rtnl_link_ops; 4464 if (ops && ops->get_linkxstats_size) { 4465 int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 4466 4467 size += nla_total_size(ops->get_linkxstats_size(dev, 4468 attr)); 4469 /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 4470 size += nla_total_size(0); 4471 } 4472 } 4473 4474 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 4475 size += rtnl_get_offload_stats_size(dev); 4476 4477 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4478 struct rtnl_af_ops *af_ops; 4479 4480 /* for IFLA_STATS_AF_SPEC */ 4481 size += nla_total_size(0); 4482 4483 rcu_read_lock(); 4484 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4485 if (af_ops->get_stats_af_size) { 4486 size += nla_total_size( 4487 af_ops->get_stats_af_size(dev)); 4488 4489 /* for AF_* */ 4490 size += nla_total_size(0); 4491 } 4492 } 4493 rcu_read_unlock(); 4494 } 4495 4496 return size; 4497 } 4498 4499 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4500 struct netlink_ext_ack *extack) 4501 { 4502 struct net *net = sock_net(skb->sk); 4503 struct net_device *dev = NULL; 4504 int idxattr = 0, prividx = 0; 4505 struct if_stats_msg *ifsm; 4506 struct sk_buff *nskb; 4507 u32 filter_mask; 4508 int err; 4509 4510 if (nlmsg_len(nlh) < sizeof(*ifsm)) 4511 return -EINVAL; 4512 4513 ifsm = nlmsg_data(nlh); 4514 if (ifsm->ifindex > 0) 4515 dev = __dev_get_by_index(net, ifsm->ifindex); 4516 else 4517 return -EINVAL; 4518 4519 if (!dev) 4520 return -ENODEV; 4521 4522 filter_mask = ifsm->filter_mask; 4523 if (!filter_mask) 4524 return -EINVAL; 4525 4526 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 4527 if (!nskb) 4528 return -ENOBUFS; 4529 4530 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 4531 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4532 0, filter_mask, &idxattr, &prividx); 4533 if (err < 0) { 4534 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 4535 WARN_ON(err == -EMSGSIZE); 4536 kfree_skb(nskb); 4537 } else { 4538 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 4539 } 4540 4541 return err; 4542 } 4543 4544 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 4545 { 4546 int h, s_h, err, s_idx, s_idxattr, s_prividx; 4547 struct net *net = sock_net(skb->sk); 4548 unsigned int flags = NLM_F_MULTI; 4549 struct if_stats_msg *ifsm; 4550 struct hlist_head *head; 4551 struct net_device *dev; 4552 u32 filter_mask = 0; 4553 int idx = 0; 4554 4555 s_h = cb->args[0]; 4556 s_idx = cb->args[1]; 4557 s_idxattr = cb->args[2]; 4558 s_prividx = cb->args[3]; 4559 4560 cb->seq = net->dev_base_seq; 4561 4562 if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) 4563 return -EINVAL; 4564 4565 ifsm = nlmsg_data(cb->nlh); 4566 filter_mask = ifsm->filter_mask; 4567 if (!filter_mask) 4568 return -EINVAL; 4569 4570 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4571 idx = 0; 4572 head = &net->dev_index_head[h]; 4573 hlist_for_each_entry(dev, head, index_hlist) { 4574 if (idx < s_idx) 4575 goto cont; 4576 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 4577 NETLINK_CB(cb->skb).portid, 4578 cb->nlh->nlmsg_seq, 0, 4579 flags, filter_mask, 4580 &s_idxattr, &s_prividx); 4581 /* If we ran out of room on the first message, 4582 * we're in trouble 4583 */ 4584 WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 4585 4586 if (err < 0) 4587 goto out; 4588 s_prividx = 0; 4589 s_idxattr = 0; 4590 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 4591 cont: 4592 idx++; 4593 } 4594 } 4595 out: 4596 cb->args[3] = s_prividx; 4597 cb->args[2] = s_idxattr; 4598 cb->args[1] = idx; 4599 cb->args[0] = h; 4600 4601 return skb->len; 4602 } 4603 4604 /* Process one rtnetlink message. */ 4605 4606 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 4607 struct netlink_ext_ack *extack) 4608 { 4609 struct net *net = sock_net(skb->sk); 4610 struct rtnl_link *link; 4611 struct module *owner; 4612 int err = -EOPNOTSUPP; 4613 rtnl_doit_func doit; 4614 unsigned int flags; 4615 int kind; 4616 int family; 4617 int type; 4618 4619 type = nlh->nlmsg_type; 4620 if (type > RTM_MAX) 4621 return -EOPNOTSUPP; 4622 4623 type -= RTM_BASE; 4624 4625 /* All the messages must have at least 1 byte length */ 4626 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 4627 return 0; 4628 4629 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 4630 kind = type&3; 4631 4632 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 4633 return -EPERM; 4634 4635 rcu_read_lock(); 4636 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 4637 struct sock *rtnl; 4638 rtnl_dumpit_func dumpit; 4639 u16 min_dump_alloc = 0; 4640 4641 link = rtnl_get_link(family, type); 4642 if (!link || !link->dumpit) { 4643 family = PF_UNSPEC; 4644 link = rtnl_get_link(family, type); 4645 if (!link || !link->dumpit) 4646 goto err_unlock; 4647 } 4648 owner = link->owner; 4649 dumpit = link->dumpit; 4650 4651 if (type == RTM_GETLINK - RTM_BASE) 4652 min_dump_alloc = rtnl_calcit(skb, nlh); 4653 4654 err = 0; 4655 /* need to do this before rcu_read_unlock() */ 4656 if (!try_module_get(owner)) 4657 err = -EPROTONOSUPPORT; 4658 4659 rcu_read_unlock(); 4660 4661 rtnl = net->rtnl; 4662 if (err == 0) { 4663 struct netlink_dump_control c = { 4664 .dump = dumpit, 4665 .min_dump_alloc = min_dump_alloc, 4666 .module = owner, 4667 }; 4668 err = netlink_dump_start(rtnl, skb, nlh, &c); 4669 /* netlink_dump_start() will keep a reference on 4670 * module if dump is still in progress. 4671 */ 4672 module_put(owner); 4673 } 4674 return err; 4675 } 4676 4677 link = rtnl_get_link(family, type); 4678 if (!link || !link->doit) { 4679 family = PF_UNSPEC; 4680 link = rtnl_get_link(PF_UNSPEC, type); 4681 if (!link || !link->doit) 4682 goto out_unlock; 4683 } 4684 4685 owner = link->owner; 4686 if (!try_module_get(owner)) { 4687 err = -EPROTONOSUPPORT; 4688 goto out_unlock; 4689 } 4690 4691 flags = link->flags; 4692 if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 4693 doit = link->doit; 4694 rcu_read_unlock(); 4695 if (doit) 4696 err = doit(skb, nlh, extack); 4697 module_put(owner); 4698 return err; 4699 } 4700 rcu_read_unlock(); 4701 4702 rtnl_lock(); 4703 link = rtnl_get_link(family, type); 4704 if (link && link->doit) 4705 err = link->doit(skb, nlh, extack); 4706 rtnl_unlock(); 4707 4708 module_put(owner); 4709 4710 return err; 4711 4712 out_unlock: 4713 rcu_read_unlock(); 4714 return err; 4715 4716 err_unlock: 4717 rcu_read_unlock(); 4718 return -EOPNOTSUPP; 4719 } 4720 4721 static void rtnetlink_rcv(struct sk_buff *skb) 4722 { 4723 netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 4724 } 4725 4726 static int rtnetlink_bind(struct net *net, int group) 4727 { 4728 switch (group) { 4729 case RTNLGRP_IPV4_MROUTE_R: 4730 case RTNLGRP_IPV6_MROUTE_R: 4731 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 4732 return -EPERM; 4733 break; 4734 } 4735 return 0; 4736 } 4737 4738 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 4739 { 4740 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4741 4742 switch (event) { 4743 case NETDEV_REBOOT: 4744 case NETDEV_CHANGEMTU: 4745 case NETDEV_CHANGEADDR: 4746 case NETDEV_CHANGENAME: 4747 case NETDEV_FEAT_CHANGE: 4748 case NETDEV_BONDING_FAILOVER: 4749 case NETDEV_POST_TYPE_CHANGE: 4750 case NETDEV_NOTIFY_PEERS: 4751 case NETDEV_CHANGEUPPER: 4752 case NETDEV_RESEND_IGMP: 4753 case NETDEV_CHANGEINFODATA: 4754 case NETDEV_CHANGELOWERSTATE: 4755 case NETDEV_CHANGE_TX_QUEUE_LEN: 4756 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 4757 GFP_KERNEL, NULL, 0); 4758 break; 4759 default: 4760 break; 4761 } 4762 return NOTIFY_DONE; 4763 } 4764 4765 static struct notifier_block rtnetlink_dev_notifier = { 4766 .notifier_call = rtnetlink_event, 4767 }; 4768 4769 4770 static int __net_init rtnetlink_net_init(struct net *net) 4771 { 4772 struct sock *sk; 4773 struct netlink_kernel_cfg cfg = { 4774 .groups = RTNLGRP_MAX, 4775 .input = rtnetlink_rcv, 4776 .cb_mutex = &rtnl_mutex, 4777 .flags = NL_CFG_F_NONROOT_RECV, 4778 .bind = rtnetlink_bind, 4779 }; 4780 4781 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 4782 if (!sk) 4783 return -ENOMEM; 4784 net->rtnl = sk; 4785 return 0; 4786 } 4787 4788 static void __net_exit rtnetlink_net_exit(struct net *net) 4789 { 4790 netlink_kernel_release(net->rtnl); 4791 net->rtnl = NULL; 4792 } 4793 4794 static struct pernet_operations rtnetlink_net_ops = { 4795 .init = rtnetlink_net_init, 4796 .exit = rtnetlink_net_exit, 4797 }; 4798 4799 void __init rtnetlink_init(void) 4800 { 4801 if (register_pernet_subsys(&rtnetlink_net_ops)) 4802 panic("rtnetlink_init: cannot initialize rtnetlink\n"); 4803 4804 register_netdevice_notifier(&rtnetlink_dev_notifier); 4805 4806 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 4807 rtnl_dump_ifinfo, 0); 4808 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 4809 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 4810 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 4811 4812 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 4813 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 4814 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 4815 4816 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 4817 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 4818 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); 4819 4820 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 4821 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 4822 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 4823 4824 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 4825 0); 4826 } 4827