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 dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2763 2764 __dev_notify_flags(dev, old_flags, ~0U); 2765 return 0; 2766 } 2767 EXPORT_SYMBOL(rtnl_configure_link); 2768 2769 struct net_device *rtnl_create_link(struct net *net, 2770 const char *ifname, unsigned char name_assign_type, 2771 const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2772 { 2773 struct net_device *dev; 2774 unsigned int num_tx_queues = 1; 2775 unsigned int num_rx_queues = 1; 2776 2777 if (tb[IFLA_NUM_TX_QUEUES]) 2778 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 2779 else if (ops->get_num_tx_queues) 2780 num_tx_queues = ops->get_num_tx_queues(); 2781 2782 if (tb[IFLA_NUM_RX_QUEUES]) 2783 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 2784 else if (ops->get_num_rx_queues) 2785 num_rx_queues = ops->get_num_rx_queues(); 2786 2787 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2788 ops->setup, num_tx_queues, num_rx_queues); 2789 if (!dev) 2790 return ERR_PTR(-ENOMEM); 2791 2792 dev_net_set(dev, net); 2793 dev->rtnl_link_ops = ops; 2794 dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 2795 2796 if (tb[IFLA_MTU]) 2797 dev->mtu = nla_get_u32(tb[IFLA_MTU]); 2798 if (tb[IFLA_ADDRESS]) { 2799 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2800 nla_len(tb[IFLA_ADDRESS])); 2801 dev->addr_assign_type = NET_ADDR_SET; 2802 } 2803 if (tb[IFLA_BROADCAST]) 2804 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2805 nla_len(tb[IFLA_BROADCAST])); 2806 if (tb[IFLA_TXQLEN]) 2807 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2808 if (tb[IFLA_OPERSTATE]) 2809 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2810 if (tb[IFLA_LINKMODE]) 2811 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2812 if (tb[IFLA_GROUP]) 2813 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2814 if (tb[IFLA_GSO_MAX_SIZE]) 2815 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 2816 if (tb[IFLA_GSO_MAX_SEGS]) 2817 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2818 2819 return dev; 2820 } 2821 EXPORT_SYMBOL(rtnl_create_link); 2822 2823 static int rtnl_group_changelink(const struct sk_buff *skb, 2824 struct net *net, int group, 2825 struct ifinfomsg *ifm, 2826 struct netlink_ext_ack *extack, 2827 struct nlattr **tb) 2828 { 2829 struct net_device *dev, *aux; 2830 int err; 2831 2832 for_each_netdev_safe(net, dev, aux) { 2833 if (dev->group == group) { 2834 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2835 if (err < 0) 2836 return err; 2837 } 2838 } 2839 2840 return 0; 2841 } 2842 2843 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2844 struct netlink_ext_ack *extack) 2845 { 2846 struct net *net = sock_net(skb->sk); 2847 const struct rtnl_link_ops *ops; 2848 const struct rtnl_link_ops *m_ops = NULL; 2849 struct net_device *dev; 2850 struct net_device *master_dev = NULL; 2851 struct ifinfomsg *ifm; 2852 char kind[MODULE_NAME_LEN]; 2853 char ifname[IFNAMSIZ]; 2854 struct nlattr *tb[IFLA_MAX+1]; 2855 struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 2856 unsigned char name_assign_type = NET_NAME_USER; 2857 int err; 2858 2859 #ifdef CONFIG_MODULES 2860 replay: 2861 #endif 2862 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 2863 if (err < 0) 2864 return err; 2865 2866 err = rtnl_ensure_unique_netns(tb, extack, false); 2867 if (err < 0) 2868 return err; 2869 2870 if (tb[IFLA_IFNAME]) 2871 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2872 else 2873 ifname[0] = '\0'; 2874 2875 ifm = nlmsg_data(nlh); 2876 if (ifm->ifi_index > 0) 2877 dev = __dev_get_by_index(net, ifm->ifi_index); 2878 else { 2879 if (ifname[0]) 2880 dev = __dev_get_by_name(net, ifname); 2881 else 2882 dev = NULL; 2883 } 2884 2885 if (dev) { 2886 master_dev = netdev_master_upper_dev_get(dev); 2887 if (master_dev) 2888 m_ops = master_dev->rtnl_link_ops; 2889 } 2890 2891 err = validate_linkmsg(dev, tb); 2892 if (err < 0) 2893 return err; 2894 2895 if (tb[IFLA_LINKINFO]) { 2896 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 2897 tb[IFLA_LINKINFO], ifla_info_policy, 2898 NULL); 2899 if (err < 0) 2900 return err; 2901 } else 2902 memset(linkinfo, 0, sizeof(linkinfo)); 2903 2904 if (linkinfo[IFLA_INFO_KIND]) { 2905 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 2906 ops = rtnl_link_ops_get(kind); 2907 } else { 2908 kind[0] = '\0'; 2909 ops = NULL; 2910 } 2911 2912 if (1) { 2913 struct nlattr *attr[RTNL_MAX_TYPE + 1]; 2914 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 2915 struct nlattr **data = NULL; 2916 struct nlattr **slave_data = NULL; 2917 struct net *dest_net, *link_net = NULL; 2918 2919 if (ops) { 2920 if (ops->maxtype > RTNL_MAX_TYPE) 2921 return -EINVAL; 2922 2923 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 2924 err = nla_parse_nested(attr, ops->maxtype, 2925 linkinfo[IFLA_INFO_DATA], 2926 ops->policy, NULL); 2927 if (err < 0) 2928 return err; 2929 data = attr; 2930 } 2931 if (ops->validate) { 2932 err = ops->validate(tb, data, extack); 2933 if (err < 0) 2934 return err; 2935 } 2936 } 2937 2938 if (m_ops) { 2939 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 2940 return -EINVAL; 2941 2942 if (m_ops->slave_maxtype && 2943 linkinfo[IFLA_INFO_SLAVE_DATA]) { 2944 err = nla_parse_nested(slave_attr, 2945 m_ops->slave_maxtype, 2946 linkinfo[IFLA_INFO_SLAVE_DATA], 2947 m_ops->slave_policy, 2948 NULL); 2949 if (err < 0) 2950 return err; 2951 slave_data = slave_attr; 2952 } 2953 } 2954 2955 if (dev) { 2956 int status = 0; 2957 2958 if (nlh->nlmsg_flags & NLM_F_EXCL) 2959 return -EEXIST; 2960 if (nlh->nlmsg_flags & NLM_F_REPLACE) 2961 return -EOPNOTSUPP; 2962 2963 if (linkinfo[IFLA_INFO_DATA]) { 2964 if (!ops || ops != dev->rtnl_link_ops || 2965 !ops->changelink) 2966 return -EOPNOTSUPP; 2967 2968 err = ops->changelink(dev, tb, data, extack); 2969 if (err < 0) 2970 return err; 2971 status |= DO_SETLINK_NOTIFY; 2972 } 2973 2974 if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 2975 if (!m_ops || !m_ops->slave_changelink) 2976 return -EOPNOTSUPP; 2977 2978 err = m_ops->slave_changelink(master_dev, dev, 2979 tb, slave_data, 2980 extack); 2981 if (err < 0) 2982 return err; 2983 status |= DO_SETLINK_NOTIFY; 2984 } 2985 2986 return do_setlink(skb, dev, ifm, extack, tb, ifname, 2987 status); 2988 } 2989 2990 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 2991 if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 2992 return rtnl_group_changelink(skb, net, 2993 nla_get_u32(tb[IFLA_GROUP]), 2994 ifm, extack, tb); 2995 return -ENODEV; 2996 } 2997 2998 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 2999 return -EOPNOTSUPP; 3000 3001 if (!ops) { 3002 #ifdef CONFIG_MODULES 3003 if (kind[0]) { 3004 __rtnl_unlock(); 3005 request_module("rtnl-link-%s", kind); 3006 rtnl_lock(); 3007 ops = rtnl_link_ops_get(kind); 3008 if (ops) 3009 goto replay; 3010 } 3011 #endif 3012 return -EOPNOTSUPP; 3013 } 3014 3015 if (!ops->setup) 3016 return -EOPNOTSUPP; 3017 3018 if (!ifname[0]) { 3019 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 3020 name_assign_type = NET_NAME_ENUM; 3021 } 3022 3023 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 3024 if (IS_ERR(dest_net)) 3025 return PTR_ERR(dest_net); 3026 3027 if (tb[IFLA_LINK_NETNSID]) { 3028 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3029 3030 link_net = get_net_ns_by_id(dest_net, id); 3031 if (!link_net) { 3032 err = -EINVAL; 3033 goto out; 3034 } 3035 err = -EPERM; 3036 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 3037 goto out; 3038 } 3039 3040 dev = rtnl_create_link(link_net ? : dest_net, ifname, 3041 name_assign_type, ops, tb); 3042 if (IS_ERR(dev)) { 3043 err = PTR_ERR(dev); 3044 goto out; 3045 } 3046 3047 dev->ifindex = ifm->ifi_index; 3048 3049 if (ops->newlink) { 3050 err = ops->newlink(link_net ? : net, dev, tb, data, 3051 extack); 3052 /* Drivers should call free_netdev() in ->destructor 3053 * and unregister it on failure after registration 3054 * so that device could be finally freed in rtnl_unlock. 3055 */ 3056 if (err < 0) { 3057 /* If device is not registered at all, free it now */ 3058 if (dev->reg_state == NETREG_UNINITIALIZED) 3059 free_netdev(dev); 3060 goto out; 3061 } 3062 } else { 3063 err = register_netdevice(dev); 3064 if (err < 0) { 3065 free_netdev(dev); 3066 goto out; 3067 } 3068 } 3069 err = rtnl_configure_link(dev, ifm); 3070 if (err < 0) 3071 goto out_unregister; 3072 if (link_net) { 3073 err = dev_change_net_namespace(dev, dest_net, ifname); 3074 if (err < 0) 3075 goto out_unregister; 3076 } 3077 if (tb[IFLA_MASTER]) { 3078 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), 3079 extack); 3080 if (err) 3081 goto out_unregister; 3082 } 3083 out: 3084 if (link_net) 3085 put_net(link_net); 3086 put_net(dest_net); 3087 return err; 3088 out_unregister: 3089 if (ops->newlink) { 3090 LIST_HEAD(list_kill); 3091 3092 ops->dellink(dev, &list_kill); 3093 unregister_netdevice_many(&list_kill); 3094 } else { 3095 unregister_netdevice(dev); 3096 } 3097 goto out; 3098 } 3099 } 3100 3101 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3102 struct netlink_ext_ack *extack) 3103 { 3104 struct net *net = sock_net(skb->sk); 3105 struct net *tgt_net = net; 3106 struct ifinfomsg *ifm; 3107 char ifname[IFNAMSIZ]; 3108 struct nlattr *tb[IFLA_MAX+1]; 3109 struct net_device *dev = NULL; 3110 struct sk_buff *nskb; 3111 int netnsid = -1; 3112 int err; 3113 u32 ext_filter_mask = 0; 3114 3115 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 3116 if (err < 0) 3117 return err; 3118 3119 err = rtnl_ensure_unique_netns(tb, extack, true); 3120 if (err < 0) 3121 return err; 3122 3123 if (tb[IFLA_IF_NETNSID]) { 3124 netnsid = nla_get_s32(tb[IFLA_IF_NETNSID]); 3125 tgt_net = get_target_net(NETLINK_CB(skb).sk, netnsid); 3126 if (IS_ERR(tgt_net)) 3127 return PTR_ERR(tgt_net); 3128 } 3129 3130 if (tb[IFLA_IFNAME]) 3131 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3132 3133 if (tb[IFLA_EXT_MASK]) 3134 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3135 3136 err = -EINVAL; 3137 ifm = nlmsg_data(nlh); 3138 if (ifm->ifi_index > 0) 3139 dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3140 else if (tb[IFLA_IFNAME]) 3141 dev = __dev_get_by_name(tgt_net, ifname); 3142 else 3143 goto out; 3144 3145 err = -ENODEV; 3146 if (dev == NULL) 3147 goto out; 3148 3149 err = -ENOBUFS; 3150 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3151 if (nskb == NULL) 3152 goto out; 3153 3154 err = rtnl_fill_ifinfo(nskb, dev, net, 3155 RTM_NEWLINK, NETLINK_CB(skb).portid, 3156 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3157 0, NULL, 0, netnsid); 3158 if (err < 0) { 3159 /* -EMSGSIZE implies BUG in if_nlmsg_size */ 3160 WARN_ON(err == -EMSGSIZE); 3161 kfree_skb(nskb); 3162 } else 3163 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 3164 out: 3165 if (netnsid >= 0) 3166 put_net(tgt_net); 3167 3168 return err; 3169 } 3170 3171 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3172 { 3173 struct net *net = sock_net(skb->sk); 3174 struct net_device *dev; 3175 struct nlattr *tb[IFLA_MAX+1]; 3176 u32 ext_filter_mask = 0; 3177 u16 min_ifinfo_dump_size = 0; 3178 int hdrlen; 3179 3180 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3181 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3182 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3183 3184 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3185 if (tb[IFLA_EXT_MASK]) 3186 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3187 } 3188 3189 if (!ext_filter_mask) 3190 return NLMSG_GOODSIZE; 3191 /* 3192 * traverse the list of net devices and compute the minimum 3193 * buffer size based upon the filter mask. 3194 */ 3195 rcu_read_lock(); 3196 for_each_netdev_rcu(net, dev) { 3197 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3198 if_nlmsg_size(dev, 3199 ext_filter_mask)); 3200 } 3201 rcu_read_unlock(); 3202 3203 return nlmsg_total_size(min_ifinfo_dump_size); 3204 } 3205 3206 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 3207 { 3208 int idx; 3209 int s_idx = cb->family; 3210 3211 if (s_idx == 0) 3212 s_idx = 1; 3213 3214 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3215 struct rtnl_link **tab; 3216 int type = cb->nlh->nlmsg_type-RTM_BASE; 3217 struct rtnl_link *link; 3218 rtnl_dumpit_func dumpit; 3219 3220 if (idx < s_idx || idx == PF_PACKET) 3221 continue; 3222 3223 if (type < 0 || type >= RTM_NR_MSGTYPES) 3224 continue; 3225 3226 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3227 if (!tab) 3228 continue; 3229 3230 link = tab[type]; 3231 if (!link) 3232 continue; 3233 3234 dumpit = link->dumpit; 3235 if (!dumpit) 3236 continue; 3237 3238 if (idx > s_idx) { 3239 memset(&cb->args[0], 0, sizeof(cb->args)); 3240 cb->prev_seq = 0; 3241 cb->seq = 0; 3242 } 3243 if (dumpit(skb, cb)) 3244 break; 3245 } 3246 cb->family = idx; 3247 3248 return skb->len; 3249 } 3250 3251 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 3252 unsigned int change, 3253 u32 event, gfp_t flags, int *new_nsid, 3254 int new_ifindex) 3255 { 3256 struct net *net = dev_net(dev); 3257 struct sk_buff *skb; 3258 int err = -ENOBUFS; 3259 size_t if_info_size; 3260 3261 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 3262 if (skb == NULL) 3263 goto errout; 3264 3265 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 3266 type, 0, 0, change, 0, 0, event, 3267 new_nsid, new_ifindex, -1); 3268 if (err < 0) { 3269 /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 3270 WARN_ON(err == -EMSGSIZE); 3271 kfree_skb(skb); 3272 goto errout; 3273 } 3274 return skb; 3275 errout: 3276 if (err < 0) 3277 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3278 return NULL; 3279 } 3280 3281 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3282 { 3283 struct net *net = dev_net(dev); 3284 3285 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3286 } 3287 3288 static void rtmsg_ifinfo_event(int type, struct net_device *dev, 3289 unsigned int change, u32 event, 3290 gfp_t flags, int *new_nsid, int new_ifindex) 3291 { 3292 struct sk_buff *skb; 3293 3294 if (dev->reg_state != NETREG_REGISTERED) 3295 return; 3296 3297 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 3298 new_ifindex); 3299 if (skb) 3300 rtmsg_ifinfo_send(skb, dev, flags); 3301 } 3302 3303 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 3304 gfp_t flags) 3305 { 3306 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 3307 NULL, 0); 3308 } 3309 3310 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 3311 gfp_t flags, int *new_nsid, int new_ifindex) 3312 { 3313 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 3314 new_nsid, new_ifindex); 3315 } 3316 3317 static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3318 struct net_device *dev, 3319 u8 *addr, u16 vid, u32 pid, u32 seq, 3320 int type, unsigned int flags, 3321 int nlflags, u16 ndm_state) 3322 { 3323 struct nlmsghdr *nlh; 3324 struct ndmsg *ndm; 3325 3326 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3327 if (!nlh) 3328 return -EMSGSIZE; 3329 3330 ndm = nlmsg_data(nlh); 3331 ndm->ndm_family = AF_BRIDGE; 3332 ndm->ndm_pad1 = 0; 3333 ndm->ndm_pad2 = 0; 3334 ndm->ndm_flags = flags; 3335 ndm->ndm_type = 0; 3336 ndm->ndm_ifindex = dev->ifindex; 3337 ndm->ndm_state = ndm_state; 3338 3339 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3340 goto nla_put_failure; 3341 if (vid) 3342 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 3343 goto nla_put_failure; 3344 3345 nlmsg_end(skb, nlh); 3346 return 0; 3347 3348 nla_put_failure: 3349 nlmsg_cancel(skb, nlh); 3350 return -EMSGSIZE; 3351 } 3352 3353 static inline size_t rtnl_fdb_nlmsg_size(void) 3354 { 3355 return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3356 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3357 nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3358 0; 3359 } 3360 3361 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3362 u16 ndm_state) 3363 { 3364 struct net *net = dev_net(dev); 3365 struct sk_buff *skb; 3366 int err = -ENOBUFS; 3367 3368 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 3369 if (!skb) 3370 goto errout; 3371 3372 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3373 0, 0, type, NTF_SELF, 0, ndm_state); 3374 if (err < 0) { 3375 kfree_skb(skb); 3376 goto errout; 3377 } 3378 3379 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 3380 return; 3381 errout: 3382 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 3383 } 3384 3385 /** 3386 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3387 */ 3388 int ndo_dflt_fdb_add(struct ndmsg *ndm, 3389 struct nlattr *tb[], 3390 struct net_device *dev, 3391 const unsigned char *addr, u16 vid, 3392 u16 flags) 3393 { 3394 int err = -EINVAL; 3395 3396 /* If aging addresses are supported device will need to 3397 * implement its own handler for this. 3398 */ 3399 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3400 pr_info("%s: FDB only supports static addresses\n", dev->name); 3401 return err; 3402 } 3403 3404 if (vid) { 3405 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 3406 return err; 3407 } 3408 3409 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3410 err = dev_uc_add_excl(dev, addr); 3411 else if (is_multicast_ether_addr(addr)) 3412 err = dev_mc_add_excl(dev, addr); 3413 3414 /* Only return duplicate errors if NLM_F_EXCL is set */ 3415 if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3416 err = 0; 3417 3418 return err; 3419 } 3420 EXPORT_SYMBOL(ndo_dflt_fdb_add); 3421 3422 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3423 struct netlink_ext_ack *extack) 3424 { 3425 u16 vid = 0; 3426 3427 if (vlan_attr) { 3428 if (nla_len(vlan_attr) != sizeof(u16)) { 3429 NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3430 return -EINVAL; 3431 } 3432 3433 vid = nla_get_u16(vlan_attr); 3434 3435 if (!vid || vid >= VLAN_VID_MASK) { 3436 NL_SET_ERR_MSG(extack, "invalid vlan id"); 3437 return -EINVAL; 3438 } 3439 } 3440 *p_vid = vid; 3441 return 0; 3442 } 3443 3444 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3445 struct netlink_ext_ack *extack) 3446 { 3447 struct net *net = sock_net(skb->sk); 3448 struct ndmsg *ndm; 3449 struct nlattr *tb[NDA_MAX+1]; 3450 struct net_device *dev; 3451 u8 *addr; 3452 u16 vid; 3453 int err; 3454 3455 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 3456 if (err < 0) 3457 return err; 3458 3459 ndm = nlmsg_data(nlh); 3460 if (ndm->ndm_ifindex == 0) { 3461 NL_SET_ERR_MSG(extack, "invalid ifindex"); 3462 return -EINVAL; 3463 } 3464 3465 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 3466 if (dev == NULL) { 3467 NL_SET_ERR_MSG(extack, "unknown ifindex"); 3468 return -ENODEV; 3469 } 3470 3471 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3472 NL_SET_ERR_MSG(extack, "invalid address"); 3473 return -EINVAL; 3474 } 3475 3476 addr = nla_data(tb[NDA_LLADDR]); 3477 3478 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3479 if (err) 3480 return err; 3481 3482 err = -EOPNOTSUPP; 3483 3484 /* Support fdb on master device the net/bridge default case */ 3485 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 3486 (dev->priv_flags & IFF_BRIDGE_PORT)) { 3487 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3488 const struct net_device_ops *ops = br_dev->netdev_ops; 3489 3490 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3491 nlh->nlmsg_flags); 3492 if (err) 3493 goto out; 3494 else 3495 ndm->ndm_flags &= ~NTF_MASTER; 3496 } 3497 3498 /* Embedded bridge, macvlan, and any other device support */ 3499 if ((ndm->ndm_flags & NTF_SELF)) { 3500 if (dev->netdev_ops->ndo_fdb_add) 3501 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3502 vid, 3503 nlh->nlmsg_flags); 3504 else 3505 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 3506 nlh->nlmsg_flags); 3507 3508 if (!err) { 3509 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3510 ndm->ndm_state); 3511 ndm->ndm_flags &= ~NTF_SELF; 3512 } 3513 } 3514 out: 3515 return err; 3516 } 3517 3518 /** 3519 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3520 */ 3521 int ndo_dflt_fdb_del(struct ndmsg *ndm, 3522 struct nlattr *tb[], 3523 struct net_device *dev, 3524 const unsigned char *addr, u16 vid) 3525 { 3526 int err = -EINVAL; 3527 3528 /* If aging addresses are supported device will need to 3529 * implement its own handler for this. 3530 */ 3531 if (!(ndm->ndm_state & NUD_PERMANENT)) { 3532 pr_info("%s: FDB only supports static addresses\n", dev->name); 3533 return err; 3534 } 3535 3536 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3537 err = dev_uc_del(dev, addr); 3538 else if (is_multicast_ether_addr(addr)) 3539 err = dev_mc_del(dev, addr); 3540 3541 return err; 3542 } 3543 EXPORT_SYMBOL(ndo_dflt_fdb_del); 3544 3545 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3546 struct netlink_ext_ack *extack) 3547 { 3548 struct net *net = sock_net(skb->sk); 3549 struct ndmsg *ndm; 3550 struct nlattr *tb[NDA_MAX+1]; 3551 struct net_device *dev; 3552 int err = -EINVAL; 3553 __u8 *addr; 3554 u16 vid; 3555 3556 if (!netlink_capable(skb, CAP_NET_ADMIN)) 3557 return -EPERM; 3558 3559 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 3560 if (err < 0) 3561 return err; 3562 3563 ndm = nlmsg_data(nlh); 3564 if (ndm->ndm_ifindex == 0) { 3565 NL_SET_ERR_MSG(extack, "invalid ifindex"); 3566 return -EINVAL; 3567 } 3568 3569 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 3570 if (dev == NULL) { 3571 NL_SET_ERR_MSG(extack, "unknown ifindex"); 3572 return -ENODEV; 3573 } 3574 3575 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3576 NL_SET_ERR_MSG(extack, "invalid address"); 3577 return -EINVAL; 3578 } 3579 3580 addr = nla_data(tb[NDA_LLADDR]); 3581 3582 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3583 if (err) 3584 return err; 3585 3586 err = -EOPNOTSUPP; 3587 3588 /* Support fdb on master device the net/bridge default case */ 3589 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 3590 (dev->priv_flags & IFF_BRIDGE_PORT)) { 3591 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3592 const struct net_device_ops *ops = br_dev->netdev_ops; 3593 3594 if (ops->ndo_fdb_del) 3595 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 3596 3597 if (err) 3598 goto out; 3599 else 3600 ndm->ndm_flags &= ~NTF_MASTER; 3601 } 3602 3603 /* Embedded bridge, macvlan, and any other device support */ 3604 if (ndm->ndm_flags & NTF_SELF) { 3605 if (dev->netdev_ops->ndo_fdb_del) 3606 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3607 vid); 3608 else 3609 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 3610 3611 if (!err) { 3612 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3613 ndm->ndm_state); 3614 ndm->ndm_flags &= ~NTF_SELF; 3615 } 3616 } 3617 out: 3618 return err; 3619 } 3620 3621 static int nlmsg_populate_fdb(struct sk_buff *skb, 3622 struct netlink_callback *cb, 3623 struct net_device *dev, 3624 int *idx, 3625 struct netdev_hw_addr_list *list) 3626 { 3627 struct netdev_hw_addr *ha; 3628 int err; 3629 u32 portid, seq; 3630 3631 portid = NETLINK_CB(cb->skb).portid; 3632 seq = cb->nlh->nlmsg_seq; 3633 3634 list_for_each_entry(ha, &list->list, list) { 3635 if (*idx < cb->args[2]) 3636 goto skip; 3637 3638 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3639 portid, seq, 3640 RTM_NEWNEIGH, NTF_SELF, 3641 NLM_F_MULTI, NUD_PERMANENT); 3642 if (err < 0) 3643 return err; 3644 skip: 3645 *idx += 1; 3646 } 3647 return 0; 3648 } 3649 3650 /** 3651 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3652 * @nlh: netlink message header 3653 * @dev: netdevice 3654 * 3655 * Default netdevice operation to dump the existing unicast address list. 3656 * Returns number of addresses from list put in skb. 3657 */ 3658 int ndo_dflt_fdb_dump(struct sk_buff *skb, 3659 struct netlink_callback *cb, 3660 struct net_device *dev, 3661 struct net_device *filter_dev, 3662 int *idx) 3663 { 3664 int err; 3665 3666 netif_addr_lock_bh(dev); 3667 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3668 if (err) 3669 goto out; 3670 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3671 out: 3672 netif_addr_unlock_bh(dev); 3673 return err; 3674 } 3675 EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3676 3677 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 3678 { 3679 struct net_device *dev; 3680 struct nlattr *tb[IFLA_MAX+1]; 3681 struct net_device *br_dev = NULL; 3682 const struct net_device_ops *ops = NULL; 3683 const struct net_device_ops *cops = NULL; 3684 struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 3685 struct net *net = sock_net(skb->sk); 3686 struct hlist_head *head; 3687 int brport_idx = 0; 3688 int br_idx = 0; 3689 int h, s_h; 3690 int idx = 0, s_idx; 3691 int err = 0; 3692 int fidx = 0; 3693 3694 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, 3695 IFLA_MAX, ifla_policy, NULL); 3696 if (err < 0) { 3697 return -EINVAL; 3698 } else if (err == 0) { 3699 if (tb[IFLA_MASTER]) 3700 br_idx = nla_get_u32(tb[IFLA_MASTER]); 3701 } 3702 3703 brport_idx = ifm->ifi_index; 3704 3705 if (br_idx) { 3706 br_dev = __dev_get_by_index(net, br_idx); 3707 if (!br_dev) 3708 return -ENODEV; 3709 3710 ops = br_dev->netdev_ops; 3711 } 3712 3713 s_h = cb->args[0]; 3714 s_idx = cb->args[1]; 3715 3716 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3717 idx = 0; 3718 head = &net->dev_index_head[h]; 3719 hlist_for_each_entry(dev, head, index_hlist) { 3720 3721 if (brport_idx && (dev->ifindex != brport_idx)) 3722 continue; 3723 3724 if (!br_idx) { /* user did not specify a specific bridge */ 3725 if (dev->priv_flags & IFF_BRIDGE_PORT) { 3726 br_dev = netdev_master_upper_dev_get(dev); 3727 cops = br_dev->netdev_ops; 3728 } 3729 } else { 3730 if (dev != br_dev && 3731 !(dev->priv_flags & IFF_BRIDGE_PORT)) 3732 continue; 3733 3734 if (br_dev != netdev_master_upper_dev_get(dev) && 3735 !(dev->priv_flags & IFF_EBRIDGE)) 3736 continue; 3737 cops = ops; 3738 } 3739 3740 if (idx < s_idx) 3741 goto cont; 3742 3743 if (dev->priv_flags & IFF_BRIDGE_PORT) { 3744 if (cops && cops->ndo_fdb_dump) { 3745 err = cops->ndo_fdb_dump(skb, cb, 3746 br_dev, dev, 3747 &fidx); 3748 if (err == -EMSGSIZE) 3749 goto out; 3750 } 3751 } 3752 3753 if (dev->netdev_ops->ndo_fdb_dump) 3754 err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3755 dev, NULL, 3756 &fidx); 3757 else 3758 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 3759 &fidx); 3760 if (err == -EMSGSIZE) 3761 goto out; 3762 3763 cops = NULL; 3764 3765 /* reset fdb offset to 0 for rest of the interfaces */ 3766 cb->args[2] = 0; 3767 fidx = 0; 3768 cont: 3769 idx++; 3770 } 3771 } 3772 3773 out: 3774 cb->args[0] = h; 3775 cb->args[1] = idx; 3776 cb->args[2] = fidx; 3777 3778 return skb->len; 3779 } 3780 3781 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 3782 unsigned int attrnum, unsigned int flag) 3783 { 3784 if (mask & flag) 3785 return nla_put_u8(skb, attrnum, !!(flags & flag)); 3786 return 0; 3787 } 3788 3789 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 3790 struct net_device *dev, u16 mode, 3791 u32 flags, u32 mask, int nlflags, 3792 u32 filter_mask, 3793 int (*vlan_fill)(struct sk_buff *skb, 3794 struct net_device *dev, 3795 u32 filter_mask)) 3796 { 3797 struct nlmsghdr *nlh; 3798 struct ifinfomsg *ifm; 3799 struct nlattr *br_afspec; 3800 struct nlattr *protinfo; 3801 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 3802 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3803 int err = 0; 3804 3805 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 3806 if (nlh == NULL) 3807 return -EMSGSIZE; 3808 3809 ifm = nlmsg_data(nlh); 3810 ifm->ifi_family = AF_BRIDGE; 3811 ifm->__ifi_pad = 0; 3812 ifm->ifi_type = dev->type; 3813 ifm->ifi_index = dev->ifindex; 3814 ifm->ifi_flags = dev_get_flags(dev); 3815 ifm->ifi_change = 0; 3816 3817 3818 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 3819 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 3820 nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 3821 (br_dev && 3822 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 3823 (dev->addr_len && 3824 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 3825 (dev->ifindex != dev_get_iflink(dev) && 3826 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3827 goto nla_put_failure; 3828 3829 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3830 if (!br_afspec) 3831 goto nla_put_failure; 3832 3833 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3834 nla_nest_cancel(skb, br_afspec); 3835 goto nla_put_failure; 3836 } 3837 3838 if (mode != BRIDGE_MODE_UNDEF) { 3839 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 3840 nla_nest_cancel(skb, br_afspec); 3841 goto nla_put_failure; 3842 } 3843 } 3844 if (vlan_fill) { 3845 err = vlan_fill(skb, dev, filter_mask); 3846 if (err) { 3847 nla_nest_cancel(skb, br_afspec); 3848 goto nla_put_failure; 3849 } 3850 } 3851 nla_nest_end(skb, br_afspec); 3852 3853 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 3854 if (!protinfo) 3855 goto nla_put_failure; 3856 3857 if (brport_nla_put_flag(skb, flags, mask, 3858 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 3859 brport_nla_put_flag(skb, flags, mask, 3860 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 3861 brport_nla_put_flag(skb, flags, mask, 3862 IFLA_BRPORT_FAST_LEAVE, 3863 BR_MULTICAST_FAST_LEAVE) || 3864 brport_nla_put_flag(skb, flags, mask, 3865 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 3866 brport_nla_put_flag(skb, flags, mask, 3867 IFLA_BRPORT_LEARNING, BR_LEARNING) || 3868 brport_nla_put_flag(skb, flags, mask, 3869 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 3870 brport_nla_put_flag(skb, flags, mask, 3871 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 3872 brport_nla_put_flag(skb, flags, mask, 3873 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 3874 nla_nest_cancel(skb, protinfo); 3875 goto nla_put_failure; 3876 } 3877 3878 nla_nest_end(skb, protinfo); 3879 3880 nlmsg_end(skb, nlh); 3881 return 0; 3882 nla_put_failure: 3883 nlmsg_cancel(skb, nlh); 3884 return err ? err : -EMSGSIZE; 3885 } 3886 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 3887 3888 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 3889 { 3890 struct net *net = sock_net(skb->sk); 3891 struct net_device *dev; 3892 int idx = 0; 3893 u32 portid = NETLINK_CB(cb->skb).portid; 3894 u32 seq = cb->nlh->nlmsg_seq; 3895 u32 filter_mask = 0; 3896 int err; 3897 3898 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 3899 struct nlattr *extfilt; 3900 3901 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 3902 IFLA_EXT_MASK); 3903 if (extfilt) { 3904 if (nla_len(extfilt) < sizeof(filter_mask)) 3905 return -EINVAL; 3906 3907 filter_mask = nla_get_u32(extfilt); 3908 } 3909 } 3910 3911 rcu_read_lock(); 3912 for_each_netdev_rcu(net, dev) { 3913 const struct net_device_ops *ops = dev->netdev_ops; 3914 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3915 3916 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 3917 if (idx >= cb->args[0]) { 3918 err = br_dev->netdev_ops->ndo_bridge_getlink( 3919 skb, portid, seq, dev, 3920 filter_mask, NLM_F_MULTI); 3921 if (err < 0 && err != -EOPNOTSUPP) { 3922 if (likely(skb->len)) 3923 break; 3924 3925 goto out_err; 3926 } 3927 } 3928 idx++; 3929 } 3930 3931 if (ops->ndo_bridge_getlink) { 3932 if (idx >= cb->args[0]) { 3933 err = ops->ndo_bridge_getlink(skb, portid, 3934 seq, dev, 3935 filter_mask, 3936 NLM_F_MULTI); 3937 if (err < 0 && err != -EOPNOTSUPP) { 3938 if (likely(skb->len)) 3939 break; 3940 3941 goto out_err; 3942 } 3943 } 3944 idx++; 3945 } 3946 } 3947 err = skb->len; 3948 out_err: 3949 rcu_read_unlock(); 3950 cb->args[0] = idx; 3951 3952 return err; 3953 } 3954 3955 static inline size_t bridge_nlmsg_size(void) 3956 { 3957 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3958 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3959 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3960 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 3961 + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 3962 + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 3963 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 3964 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 3965 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 3966 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 3967 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 3968 } 3969 3970 static int rtnl_bridge_notify(struct net_device *dev) 3971 { 3972 struct net *net = dev_net(dev); 3973 struct sk_buff *skb; 3974 int err = -EOPNOTSUPP; 3975 3976 if (!dev->netdev_ops->ndo_bridge_getlink) 3977 return 0; 3978 3979 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 3980 if (!skb) { 3981 err = -ENOMEM; 3982 goto errout; 3983 } 3984 3985 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 3986 if (err < 0) 3987 goto errout; 3988 3989 if (!skb->len) 3990 goto errout; 3991 3992 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 3993 return 0; 3994 errout: 3995 WARN_ON(err == -EMSGSIZE); 3996 kfree_skb(skb); 3997 if (err) 3998 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3999 return err; 4000 } 4001 4002 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4003 struct netlink_ext_ack *extack) 4004 { 4005 struct net *net = sock_net(skb->sk); 4006 struct ifinfomsg *ifm; 4007 struct net_device *dev; 4008 struct nlattr *br_spec, *attr = NULL; 4009 int rem, err = -EOPNOTSUPP; 4010 u16 flags = 0; 4011 bool have_flags = false; 4012 4013 if (nlmsg_len(nlh) < sizeof(*ifm)) 4014 return -EINVAL; 4015 4016 ifm = nlmsg_data(nlh); 4017 if (ifm->ifi_family != AF_BRIDGE) 4018 return -EPFNOSUPPORT; 4019 4020 dev = __dev_get_by_index(net, ifm->ifi_index); 4021 if (!dev) { 4022 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4023 return -ENODEV; 4024 } 4025 4026 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4027 if (br_spec) { 4028 nla_for_each_nested(attr, br_spec, rem) { 4029 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 4030 if (nla_len(attr) < sizeof(flags)) 4031 return -EINVAL; 4032 4033 have_flags = true; 4034 flags = nla_get_u16(attr); 4035 break; 4036 } 4037 } 4038 } 4039 4040 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4041 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4042 4043 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 4044 err = -EOPNOTSUPP; 4045 goto out; 4046 } 4047 4048 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 4049 if (err) 4050 goto out; 4051 4052 flags &= ~BRIDGE_FLAGS_MASTER; 4053 } 4054 4055 if ((flags & BRIDGE_FLAGS_SELF)) { 4056 if (!dev->netdev_ops->ndo_bridge_setlink) 4057 err = -EOPNOTSUPP; 4058 else 4059 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 4060 flags); 4061 if (!err) { 4062 flags &= ~BRIDGE_FLAGS_SELF; 4063 4064 /* Generate event to notify upper layer of bridge 4065 * change 4066 */ 4067 err = rtnl_bridge_notify(dev); 4068 } 4069 } 4070 4071 if (have_flags) 4072 memcpy(nla_data(attr), &flags, sizeof(flags)); 4073 out: 4074 return err; 4075 } 4076 4077 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4078 struct netlink_ext_ack *extack) 4079 { 4080 struct net *net = sock_net(skb->sk); 4081 struct ifinfomsg *ifm; 4082 struct net_device *dev; 4083 struct nlattr *br_spec, *attr = NULL; 4084 int rem, err = -EOPNOTSUPP; 4085 u16 flags = 0; 4086 bool have_flags = false; 4087 4088 if (nlmsg_len(nlh) < sizeof(*ifm)) 4089 return -EINVAL; 4090 4091 ifm = nlmsg_data(nlh); 4092 if (ifm->ifi_family != AF_BRIDGE) 4093 return -EPFNOSUPPORT; 4094 4095 dev = __dev_get_by_index(net, ifm->ifi_index); 4096 if (!dev) { 4097 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4098 return -ENODEV; 4099 } 4100 4101 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4102 if (br_spec) { 4103 nla_for_each_nested(attr, br_spec, rem) { 4104 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 4105 if (nla_len(attr) < sizeof(flags)) 4106 return -EINVAL; 4107 4108 have_flags = true; 4109 flags = nla_get_u16(attr); 4110 break; 4111 } 4112 } 4113 } 4114 4115 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4116 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4117 4118 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4119 err = -EOPNOTSUPP; 4120 goto out; 4121 } 4122 4123 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4124 if (err) 4125 goto out; 4126 4127 flags &= ~BRIDGE_FLAGS_MASTER; 4128 } 4129 4130 if ((flags & BRIDGE_FLAGS_SELF)) { 4131 if (!dev->netdev_ops->ndo_bridge_dellink) 4132 err = -EOPNOTSUPP; 4133 else 4134 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4135 flags); 4136 4137 if (!err) { 4138 flags &= ~BRIDGE_FLAGS_SELF; 4139 4140 /* Generate event to notify upper layer of bridge 4141 * change 4142 */ 4143 err = rtnl_bridge_notify(dev); 4144 } 4145 } 4146 4147 if (have_flags) 4148 memcpy(nla_data(attr), &flags, sizeof(flags)); 4149 out: 4150 return err; 4151 } 4152 4153 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4154 { 4155 return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4156 (!idxattr || idxattr == attrid); 4157 } 4158 4159 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 4160 static int rtnl_get_offload_stats_attr_size(int attr_id) 4161 { 4162 switch (attr_id) { 4163 case IFLA_OFFLOAD_XSTATS_CPU_HIT: 4164 return sizeof(struct rtnl_link_stats64); 4165 } 4166 4167 return 0; 4168 } 4169 4170 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 4171 int *prividx) 4172 { 4173 struct nlattr *attr = NULL; 4174 int attr_id, size; 4175 void *attr_data; 4176 int err; 4177 4178 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 4179 dev->netdev_ops->ndo_get_offload_stats)) 4180 return -ENODATA; 4181 4182 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 4183 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 4184 if (attr_id < *prividx) 4185 continue; 4186 4187 size = rtnl_get_offload_stats_attr_size(attr_id); 4188 if (!size) 4189 continue; 4190 4191 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 4192 continue; 4193 4194 attr = nla_reserve_64bit(skb, attr_id, size, 4195 IFLA_OFFLOAD_XSTATS_UNSPEC); 4196 if (!attr) 4197 goto nla_put_failure; 4198 4199 attr_data = nla_data(attr); 4200 memset(attr_data, 0, size); 4201 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 4202 attr_data); 4203 if (err) 4204 goto get_offload_stats_failure; 4205 } 4206 4207 if (!attr) 4208 return -ENODATA; 4209 4210 *prividx = 0; 4211 return 0; 4212 4213 nla_put_failure: 4214 err = -EMSGSIZE; 4215 get_offload_stats_failure: 4216 *prividx = attr_id; 4217 return err; 4218 } 4219 4220 static int rtnl_get_offload_stats_size(const struct net_device *dev) 4221 { 4222 int nla_size = 0; 4223 int attr_id; 4224 int size; 4225 4226 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 4227 dev->netdev_ops->ndo_get_offload_stats)) 4228 return 0; 4229 4230 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 4231 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 4232 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 4233 continue; 4234 size = rtnl_get_offload_stats_attr_size(attr_id); 4235 nla_size += nla_total_size_64bit(size); 4236 } 4237 4238 if (nla_size != 0) 4239 nla_size += nla_total_size(0); 4240 4241 return nla_size; 4242 } 4243 4244 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 4245 int type, u32 pid, u32 seq, u32 change, 4246 unsigned int flags, unsigned int filter_mask, 4247 int *idxattr, int *prividx) 4248 { 4249 struct if_stats_msg *ifsm; 4250 struct nlmsghdr *nlh; 4251 struct nlattr *attr; 4252 int s_prividx = *prividx; 4253 int err; 4254 4255 ASSERT_RTNL(); 4256 4257 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 4258 if (!nlh) 4259 return -EMSGSIZE; 4260 4261 ifsm = nlmsg_data(nlh); 4262 ifsm->family = PF_UNSPEC; 4263 ifsm->pad1 = 0; 4264 ifsm->pad2 = 0; 4265 ifsm->ifindex = dev->ifindex; 4266 ifsm->filter_mask = filter_mask; 4267 4268 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 4269 struct rtnl_link_stats64 *sp; 4270 4271 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 4272 sizeof(struct rtnl_link_stats64), 4273 IFLA_STATS_UNSPEC); 4274 if (!attr) 4275 goto nla_put_failure; 4276 4277 sp = nla_data(attr); 4278 dev_get_stats(dev, sp); 4279 } 4280 4281 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 4282 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 4283 4284 if (ops && ops->fill_linkxstats) { 4285 *idxattr = IFLA_STATS_LINK_XSTATS; 4286 attr = nla_nest_start(skb, 4287 IFLA_STATS_LINK_XSTATS); 4288 if (!attr) 4289 goto nla_put_failure; 4290 4291 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 4292 nla_nest_end(skb, attr); 4293 if (err) 4294 goto nla_put_failure; 4295 *idxattr = 0; 4296 } 4297 } 4298 4299 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 4300 *idxattr)) { 4301 const struct rtnl_link_ops *ops = NULL; 4302 const struct net_device *master; 4303 4304 master = netdev_master_upper_dev_get(dev); 4305 if (master) 4306 ops = master->rtnl_link_ops; 4307 if (ops && ops->fill_linkxstats) { 4308 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 4309 attr = nla_nest_start(skb, 4310 IFLA_STATS_LINK_XSTATS_SLAVE); 4311 if (!attr) 4312 goto nla_put_failure; 4313 4314 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 4315 nla_nest_end(skb, attr); 4316 if (err) 4317 goto nla_put_failure; 4318 *idxattr = 0; 4319 } 4320 } 4321 4322 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 4323 *idxattr)) { 4324 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 4325 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 4326 if (!attr) 4327 goto nla_put_failure; 4328 4329 err = rtnl_get_offload_stats(skb, dev, prividx); 4330 if (err == -ENODATA) 4331 nla_nest_cancel(skb, attr); 4332 else 4333 nla_nest_end(skb, attr); 4334 4335 if (err && err != -ENODATA) 4336 goto nla_put_failure; 4337 *idxattr = 0; 4338 } 4339 4340 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4341 struct rtnl_af_ops *af_ops; 4342 4343 *idxattr = IFLA_STATS_AF_SPEC; 4344 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 4345 if (!attr) 4346 goto nla_put_failure; 4347 4348 rcu_read_lock(); 4349 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4350 if (af_ops->fill_stats_af) { 4351 struct nlattr *af; 4352 int err; 4353 4354 af = nla_nest_start(skb, af_ops->family); 4355 if (!af) { 4356 rcu_read_unlock(); 4357 goto nla_put_failure; 4358 } 4359 err = af_ops->fill_stats_af(skb, dev); 4360 4361 if (err == -ENODATA) { 4362 nla_nest_cancel(skb, af); 4363 } else if (err < 0) { 4364 rcu_read_unlock(); 4365 goto nla_put_failure; 4366 } 4367 4368 nla_nest_end(skb, af); 4369 } 4370 } 4371 rcu_read_unlock(); 4372 4373 nla_nest_end(skb, attr); 4374 4375 *idxattr = 0; 4376 } 4377 4378 nlmsg_end(skb, nlh); 4379 4380 return 0; 4381 4382 nla_put_failure: 4383 /* not a multi message or no progress mean a real error */ 4384 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 4385 nlmsg_cancel(skb, nlh); 4386 else 4387 nlmsg_end(skb, nlh); 4388 4389 return -EMSGSIZE; 4390 } 4391 4392 static size_t if_nlmsg_stats_size(const struct net_device *dev, 4393 u32 filter_mask) 4394 { 4395 size_t size = 0; 4396 4397 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 4398 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 4399 4400 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 4401 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 4402 int attr = IFLA_STATS_LINK_XSTATS; 4403 4404 if (ops && ops->get_linkxstats_size) { 4405 size += nla_total_size(ops->get_linkxstats_size(dev, 4406 attr)); 4407 /* for IFLA_STATS_LINK_XSTATS */ 4408 size += nla_total_size(0); 4409 } 4410 } 4411 4412 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 4413 struct net_device *_dev = (struct net_device *)dev; 4414 const struct rtnl_link_ops *ops = NULL; 4415 const struct net_device *master; 4416 4417 /* netdev_master_upper_dev_get can't take const */ 4418 master = netdev_master_upper_dev_get(_dev); 4419 if (master) 4420 ops = master->rtnl_link_ops; 4421 if (ops && ops->get_linkxstats_size) { 4422 int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 4423 4424 size += nla_total_size(ops->get_linkxstats_size(dev, 4425 attr)); 4426 /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 4427 size += nla_total_size(0); 4428 } 4429 } 4430 4431 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 4432 size += rtnl_get_offload_stats_size(dev); 4433 4434 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4435 struct rtnl_af_ops *af_ops; 4436 4437 /* for IFLA_STATS_AF_SPEC */ 4438 size += nla_total_size(0); 4439 4440 rcu_read_lock(); 4441 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4442 if (af_ops->get_stats_af_size) { 4443 size += nla_total_size( 4444 af_ops->get_stats_af_size(dev)); 4445 4446 /* for AF_* */ 4447 size += nla_total_size(0); 4448 } 4449 } 4450 rcu_read_unlock(); 4451 } 4452 4453 return size; 4454 } 4455 4456 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4457 struct netlink_ext_ack *extack) 4458 { 4459 struct net *net = sock_net(skb->sk); 4460 struct net_device *dev = NULL; 4461 int idxattr = 0, prividx = 0; 4462 struct if_stats_msg *ifsm; 4463 struct sk_buff *nskb; 4464 u32 filter_mask; 4465 int err; 4466 4467 if (nlmsg_len(nlh) < sizeof(*ifsm)) 4468 return -EINVAL; 4469 4470 ifsm = nlmsg_data(nlh); 4471 if (ifsm->ifindex > 0) 4472 dev = __dev_get_by_index(net, ifsm->ifindex); 4473 else 4474 return -EINVAL; 4475 4476 if (!dev) 4477 return -ENODEV; 4478 4479 filter_mask = ifsm->filter_mask; 4480 if (!filter_mask) 4481 return -EINVAL; 4482 4483 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 4484 if (!nskb) 4485 return -ENOBUFS; 4486 4487 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 4488 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4489 0, filter_mask, &idxattr, &prividx); 4490 if (err < 0) { 4491 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 4492 WARN_ON(err == -EMSGSIZE); 4493 kfree_skb(nskb); 4494 } else { 4495 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 4496 } 4497 4498 return err; 4499 } 4500 4501 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 4502 { 4503 int h, s_h, err, s_idx, s_idxattr, s_prividx; 4504 struct net *net = sock_net(skb->sk); 4505 unsigned int flags = NLM_F_MULTI; 4506 struct if_stats_msg *ifsm; 4507 struct hlist_head *head; 4508 struct net_device *dev; 4509 u32 filter_mask = 0; 4510 int idx = 0; 4511 4512 s_h = cb->args[0]; 4513 s_idx = cb->args[1]; 4514 s_idxattr = cb->args[2]; 4515 s_prividx = cb->args[3]; 4516 4517 cb->seq = net->dev_base_seq; 4518 4519 if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) 4520 return -EINVAL; 4521 4522 ifsm = nlmsg_data(cb->nlh); 4523 filter_mask = ifsm->filter_mask; 4524 if (!filter_mask) 4525 return -EINVAL; 4526 4527 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4528 idx = 0; 4529 head = &net->dev_index_head[h]; 4530 hlist_for_each_entry(dev, head, index_hlist) { 4531 if (idx < s_idx) 4532 goto cont; 4533 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 4534 NETLINK_CB(cb->skb).portid, 4535 cb->nlh->nlmsg_seq, 0, 4536 flags, filter_mask, 4537 &s_idxattr, &s_prividx); 4538 /* If we ran out of room on the first message, 4539 * we're in trouble 4540 */ 4541 WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 4542 4543 if (err < 0) 4544 goto out; 4545 s_prividx = 0; 4546 s_idxattr = 0; 4547 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 4548 cont: 4549 idx++; 4550 } 4551 } 4552 out: 4553 cb->args[3] = s_prividx; 4554 cb->args[2] = s_idxattr; 4555 cb->args[1] = idx; 4556 cb->args[0] = h; 4557 4558 return skb->len; 4559 } 4560 4561 /* Process one rtnetlink message. */ 4562 4563 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 4564 struct netlink_ext_ack *extack) 4565 { 4566 struct net *net = sock_net(skb->sk); 4567 struct rtnl_link *link; 4568 struct module *owner; 4569 int err = -EOPNOTSUPP; 4570 rtnl_doit_func doit; 4571 unsigned int flags; 4572 int kind; 4573 int family; 4574 int type; 4575 4576 type = nlh->nlmsg_type; 4577 if (type > RTM_MAX) 4578 return -EOPNOTSUPP; 4579 4580 type -= RTM_BASE; 4581 4582 /* All the messages must have at least 1 byte length */ 4583 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 4584 return 0; 4585 4586 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 4587 kind = type&3; 4588 4589 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 4590 return -EPERM; 4591 4592 rcu_read_lock(); 4593 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 4594 struct sock *rtnl; 4595 rtnl_dumpit_func dumpit; 4596 u16 min_dump_alloc = 0; 4597 4598 link = rtnl_get_link(family, type); 4599 if (!link || !link->dumpit) { 4600 family = PF_UNSPEC; 4601 link = rtnl_get_link(family, type); 4602 if (!link || !link->dumpit) 4603 goto err_unlock; 4604 } 4605 owner = link->owner; 4606 dumpit = link->dumpit; 4607 4608 if (type == RTM_GETLINK - RTM_BASE) 4609 min_dump_alloc = rtnl_calcit(skb, nlh); 4610 4611 err = 0; 4612 /* need to do this before rcu_read_unlock() */ 4613 if (!try_module_get(owner)) 4614 err = -EPROTONOSUPPORT; 4615 4616 rcu_read_unlock(); 4617 4618 rtnl = net->rtnl; 4619 if (err == 0) { 4620 struct netlink_dump_control c = { 4621 .dump = dumpit, 4622 .min_dump_alloc = min_dump_alloc, 4623 .module = owner, 4624 }; 4625 err = netlink_dump_start(rtnl, skb, nlh, &c); 4626 /* netlink_dump_start() will keep a reference on 4627 * module if dump is still in progress. 4628 */ 4629 module_put(owner); 4630 } 4631 return err; 4632 } 4633 4634 link = rtnl_get_link(family, type); 4635 if (!link || !link->doit) { 4636 family = PF_UNSPEC; 4637 link = rtnl_get_link(PF_UNSPEC, type); 4638 if (!link || !link->doit) 4639 goto out_unlock; 4640 } 4641 4642 owner = link->owner; 4643 if (!try_module_get(owner)) { 4644 err = -EPROTONOSUPPORT; 4645 goto out_unlock; 4646 } 4647 4648 flags = link->flags; 4649 if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 4650 doit = link->doit; 4651 rcu_read_unlock(); 4652 if (doit) 4653 err = doit(skb, nlh, extack); 4654 module_put(owner); 4655 return err; 4656 } 4657 rcu_read_unlock(); 4658 4659 rtnl_lock(); 4660 link = rtnl_get_link(family, type); 4661 if (link && link->doit) 4662 err = link->doit(skb, nlh, extack); 4663 rtnl_unlock(); 4664 4665 module_put(owner); 4666 4667 return err; 4668 4669 out_unlock: 4670 rcu_read_unlock(); 4671 return err; 4672 4673 err_unlock: 4674 rcu_read_unlock(); 4675 return -EOPNOTSUPP; 4676 } 4677 4678 static void rtnetlink_rcv(struct sk_buff *skb) 4679 { 4680 netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 4681 } 4682 4683 static int rtnetlink_bind(struct net *net, int group) 4684 { 4685 switch (group) { 4686 case RTNLGRP_IPV4_MROUTE_R: 4687 case RTNLGRP_IPV6_MROUTE_R: 4688 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 4689 return -EPERM; 4690 break; 4691 } 4692 return 0; 4693 } 4694 4695 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 4696 { 4697 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4698 4699 switch (event) { 4700 case NETDEV_REBOOT: 4701 case NETDEV_CHANGEMTU: 4702 case NETDEV_CHANGEADDR: 4703 case NETDEV_CHANGENAME: 4704 case NETDEV_FEAT_CHANGE: 4705 case NETDEV_BONDING_FAILOVER: 4706 case NETDEV_POST_TYPE_CHANGE: 4707 case NETDEV_NOTIFY_PEERS: 4708 case NETDEV_CHANGEUPPER: 4709 case NETDEV_RESEND_IGMP: 4710 case NETDEV_CHANGEINFODATA: 4711 case NETDEV_CHANGELOWERSTATE: 4712 case NETDEV_CHANGE_TX_QUEUE_LEN: 4713 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 4714 GFP_KERNEL, NULL, 0); 4715 break; 4716 default: 4717 break; 4718 } 4719 return NOTIFY_DONE; 4720 } 4721 4722 static struct notifier_block rtnetlink_dev_notifier = { 4723 .notifier_call = rtnetlink_event, 4724 }; 4725 4726 4727 static int __net_init rtnetlink_net_init(struct net *net) 4728 { 4729 struct sock *sk; 4730 struct netlink_kernel_cfg cfg = { 4731 .groups = RTNLGRP_MAX, 4732 .input = rtnetlink_rcv, 4733 .cb_mutex = &rtnl_mutex, 4734 .flags = NL_CFG_F_NONROOT_RECV, 4735 .bind = rtnetlink_bind, 4736 }; 4737 4738 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 4739 if (!sk) 4740 return -ENOMEM; 4741 net->rtnl = sk; 4742 return 0; 4743 } 4744 4745 static void __net_exit rtnetlink_net_exit(struct net *net) 4746 { 4747 netlink_kernel_release(net->rtnl); 4748 net->rtnl = NULL; 4749 } 4750 4751 static struct pernet_operations rtnetlink_net_ops = { 4752 .init = rtnetlink_net_init, 4753 .exit = rtnetlink_net_exit, 4754 }; 4755 4756 void __init rtnetlink_init(void) 4757 { 4758 if (register_pernet_subsys(&rtnetlink_net_ops)) 4759 panic("rtnetlink_init: cannot initialize rtnetlink\n"); 4760 4761 register_netdevice_notifier(&rtnetlink_dev_notifier); 4762 4763 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 4764 rtnl_dump_ifinfo, 0); 4765 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 4766 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 4767 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 4768 4769 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 4770 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 4771 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 4772 4773 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 4774 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 4775 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); 4776 4777 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 4778 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 4779 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 4780 4781 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 4782 0); 4783 } 4784