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