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