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