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