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