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 bool force) 1501 { 1502 int ifindex = dev_get_iflink(dev); 1503 1504 if (force || dev->ifindex != ifindex) 1505 return nla_put_u32(skb, IFLA_LINK, ifindex); 1506 1507 return 0; 1508 } 1509 1510 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb, 1511 struct net_device *dev) 1512 { 1513 char buf[IFALIASZ]; 1514 int ret; 1515 1516 ret = dev_get_alias(dev, buf, sizeof(buf)); 1517 return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0; 1518 } 1519 1520 static int rtnl_fill_link_netnsid(struct sk_buff *skb, 1521 const struct net_device *dev, 1522 struct net *src_net) 1523 { 1524 bool put_iflink = false; 1525 1526 if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) { 1527 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1528 1529 if (!net_eq(dev_net(dev), link_net)) { 1530 int id = peernet2id_alloc(src_net, link_net); 1531 1532 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1533 return -EMSGSIZE; 1534 1535 put_iflink = true; 1536 } 1537 } 1538 1539 return nla_put_iflink(skb, dev, put_iflink); 1540 } 1541 1542 static int rtnl_fill_link_af(struct sk_buff *skb, 1543 const struct net_device *dev, 1544 u32 ext_filter_mask) 1545 { 1546 const struct rtnl_af_ops *af_ops; 1547 struct nlattr *af_spec; 1548 1549 af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 1550 if (!af_spec) 1551 return -EMSGSIZE; 1552 1553 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 1554 struct nlattr *af; 1555 int err; 1556 1557 if (!af_ops->fill_link_af) 1558 continue; 1559 1560 af = nla_nest_start_noflag(skb, af_ops->family); 1561 if (!af) 1562 return -EMSGSIZE; 1563 1564 err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1565 /* 1566 * Caller may return ENODATA to indicate that there 1567 * was no data to be dumped. This is not an error, it 1568 * means we should trim the attribute header and 1569 * continue. 1570 */ 1571 if (err == -ENODATA) 1572 nla_nest_cancel(skb, af); 1573 else if (err < 0) 1574 return -EMSGSIZE; 1575 1576 nla_nest_end(skb, af); 1577 } 1578 1579 nla_nest_end(skb, af_spec); 1580 return 0; 1581 } 1582 1583 static int rtnl_fill_ifinfo(struct sk_buff *skb, 1584 struct net_device *dev, struct net *src_net, 1585 int type, u32 pid, u32 seq, u32 change, 1586 unsigned int flags, u32 ext_filter_mask, 1587 u32 event, int *new_nsid, int new_ifindex, 1588 int tgt_netnsid) 1589 { 1590 struct ifinfomsg *ifm; 1591 struct nlmsghdr *nlh; 1592 1593 ASSERT_RTNL(); 1594 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1595 if (nlh == NULL) 1596 return -EMSGSIZE; 1597 1598 ifm = nlmsg_data(nlh); 1599 ifm->ifi_family = AF_UNSPEC; 1600 ifm->__ifi_pad = 0; 1601 ifm->ifi_type = dev->type; 1602 ifm->ifi_index = dev->ifindex; 1603 ifm->ifi_flags = dev_get_flags(dev); 1604 ifm->ifi_change = change; 1605 1606 if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid)) 1607 goto nla_put_failure; 1608 1609 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1610 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1611 nla_put_u8(skb, IFLA_OPERSTATE, 1612 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1613 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1614 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 1615 nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) || 1616 nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) || 1617 nla_put_u32(skb, IFLA_GROUP, dev->group) || 1618 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 1619 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1620 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1621 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 1622 #ifdef CONFIG_RPS 1623 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 1624 #endif 1625 put_master_ifindex(skb, dev) || 1626 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1627 (dev->qdisc && 1628 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 1629 nla_put_ifalias(skb, dev) || 1630 nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1631 atomic_read(&dev->carrier_up_count) + 1632 atomic_read(&dev->carrier_down_count)) || 1633 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down) || 1634 nla_put_u32(skb, IFLA_CARRIER_UP_COUNT, 1635 atomic_read(&dev->carrier_up_count)) || 1636 nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT, 1637 atomic_read(&dev->carrier_down_count))) 1638 goto nla_put_failure; 1639 1640 if (event != IFLA_EVENT_NONE) { 1641 if (nla_put_u32(skb, IFLA_EVENT, event)) 1642 goto nla_put_failure; 1643 } 1644 1645 if (rtnl_fill_link_ifmap(skb, dev)) 1646 goto nla_put_failure; 1647 1648 if (dev->addr_len) { 1649 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1650 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1651 goto nla_put_failure; 1652 } 1653 1654 if (rtnl_phys_port_id_fill(skb, dev)) 1655 goto nla_put_failure; 1656 1657 if (rtnl_phys_port_name_fill(skb, dev)) 1658 goto nla_put_failure; 1659 1660 if (rtnl_phys_switch_id_fill(skb, dev)) 1661 goto nla_put_failure; 1662 1663 if (rtnl_fill_stats(skb, dev)) 1664 goto nla_put_failure; 1665 1666 if (rtnl_fill_vf(skb, dev, ext_filter_mask)) 1667 goto nla_put_failure; 1668 1669 if (rtnl_port_fill(skb, dev, ext_filter_mask)) 1670 goto nla_put_failure; 1671 1672 if (rtnl_xdp_fill(skb, dev)) 1673 goto nla_put_failure; 1674 1675 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 1676 if (rtnl_link_fill(skb, dev) < 0) 1677 goto nla_put_failure; 1678 } 1679 1680 if (rtnl_fill_link_netnsid(skb, dev, src_net)) 1681 goto nla_put_failure; 1682 1683 if (new_nsid && 1684 nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0) 1685 goto nla_put_failure; 1686 if (new_ifindex && 1687 nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0) 1688 goto nla_put_failure; 1689 1690 1691 rcu_read_lock(); 1692 if (rtnl_fill_link_af(skb, dev, ext_filter_mask)) 1693 goto nla_put_failure_rcu; 1694 rcu_read_unlock(); 1695 1696 nlmsg_end(skb, nlh); 1697 return 0; 1698 1699 nla_put_failure_rcu: 1700 rcu_read_unlock(); 1701 nla_put_failure: 1702 nlmsg_cancel(skb, nlh); 1703 return -EMSGSIZE; 1704 } 1705 1706 static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1707 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1708 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1709 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1710 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1711 [IFLA_MTU] = { .type = NLA_U32 }, 1712 [IFLA_LINK] = { .type = NLA_U32 }, 1713 [IFLA_MASTER] = { .type = NLA_U32 }, 1714 [IFLA_CARRIER] = { .type = NLA_U8 }, 1715 [IFLA_TXQLEN] = { .type = NLA_U32 }, 1716 [IFLA_WEIGHT] = { .type = NLA_U32 }, 1717 [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1718 [IFLA_LINKMODE] = { .type = NLA_U8 }, 1719 [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1720 [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1721 [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 1722 /* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to 1723 * allow 0-length string (needed to remove an alias). 1724 */ 1725 [IFLA_IFALIAS] = { .type = NLA_BINARY, .len = IFALIASZ - 1 }, 1726 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1727 [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1728 [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1729 [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1730 [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1731 [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1732 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1733 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 1734 [IFLA_GSO_MAX_SEGS] = { .type = NLA_U32 }, 1735 [IFLA_GSO_MAX_SIZE] = { .type = NLA_U32 }, 1736 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1737 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 1738 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1739 [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 1740 [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1741 [IFLA_XDP] = { .type = NLA_NESTED }, 1742 [IFLA_EVENT] = { .type = NLA_U32 }, 1743 [IFLA_GROUP] = { .type = NLA_U32 }, 1744 [IFLA_TARGET_NETNSID] = { .type = NLA_S32 }, 1745 [IFLA_CARRIER_UP_COUNT] = { .type = NLA_U32 }, 1746 [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, 1747 [IFLA_MIN_MTU] = { .type = NLA_U32 }, 1748 [IFLA_MAX_MTU] = { .type = NLA_U32 }, 1749 }; 1750 1751 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1752 [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1753 [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1754 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1755 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1756 }; 1757 1758 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1759 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1760 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 1761 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1762 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1763 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1764 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1765 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 1766 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 1767 [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1768 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1769 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1770 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1771 }; 1772 1773 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1774 [IFLA_PORT_VF] = { .type = NLA_U32 }, 1775 [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1776 .len = PORT_PROFILE_MAX }, 1777 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1778 .len = PORT_UUID_MAX }, 1779 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1780 .len = PORT_UUID_MAX }, 1781 [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1782 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1783 1784 /* Unused, but we need to keep it here since user space could 1785 * fill it. It's also broken with regard to NLA_BINARY use in 1786 * combination with structs. 1787 */ 1788 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1789 .len = sizeof(struct ifla_port_vsi) }, 1790 }; 1791 1792 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1793 [IFLA_XDP_FD] = { .type = NLA_S32 }, 1794 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 1795 [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 1796 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1797 }; 1798 1799 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1800 { 1801 const struct rtnl_link_ops *ops = NULL; 1802 struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1803 1804 if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0) 1805 return NULL; 1806 1807 if (linfo[IFLA_INFO_KIND]) { 1808 char kind[MODULE_NAME_LEN]; 1809 1810 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1811 ops = rtnl_link_ops_get(kind); 1812 } 1813 1814 return ops; 1815 } 1816 1817 static bool link_master_filtered(struct net_device *dev, int master_idx) 1818 { 1819 struct net_device *master; 1820 1821 if (!master_idx) 1822 return false; 1823 1824 master = netdev_master_upper_dev_get(dev); 1825 if (!master || master->ifindex != master_idx) 1826 return true; 1827 1828 return false; 1829 } 1830 1831 static bool link_kind_filtered(const struct net_device *dev, 1832 const struct rtnl_link_ops *kind_ops) 1833 { 1834 if (kind_ops && dev->rtnl_link_ops != kind_ops) 1835 return true; 1836 1837 return false; 1838 } 1839 1840 static bool link_dump_filtered(struct net_device *dev, 1841 int master_idx, 1842 const struct rtnl_link_ops *kind_ops) 1843 { 1844 if (link_master_filtered(dev, master_idx) || 1845 link_kind_filtered(dev, kind_ops)) 1846 return true; 1847 1848 return false; 1849 } 1850 1851 /** 1852 * rtnl_get_net_ns_capable - Get netns if sufficiently privileged. 1853 * @sk: netlink socket 1854 * @netnsid: network namespace identifier 1855 * 1856 * Returns the network namespace identified by netnsid on success or an error 1857 * pointer on failure. 1858 */ 1859 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid) 1860 { 1861 struct net *net; 1862 1863 net = get_net_ns_by_id(sock_net(sk), netnsid); 1864 if (!net) 1865 return ERR_PTR(-EINVAL); 1866 1867 /* For now, the caller is required to have CAP_NET_ADMIN in 1868 * the user namespace owning the target net ns. 1869 */ 1870 if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) { 1871 put_net(net); 1872 return ERR_PTR(-EACCES); 1873 } 1874 return net; 1875 } 1876 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable); 1877 1878 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh, 1879 bool strict_check, struct nlattr **tb, 1880 struct netlink_ext_ack *extack) 1881 { 1882 int hdrlen; 1883 1884 if (strict_check) { 1885 struct ifinfomsg *ifm; 1886 1887 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 1888 NL_SET_ERR_MSG(extack, "Invalid header for link dump"); 1889 return -EINVAL; 1890 } 1891 1892 ifm = nlmsg_data(nlh); 1893 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 1894 ifm->ifi_change) { 1895 NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request"); 1896 return -EINVAL; 1897 } 1898 if (ifm->ifi_index) { 1899 NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps"); 1900 return -EINVAL; 1901 } 1902 1903 return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, 1904 IFLA_MAX, ifla_policy, 1905 extack); 1906 } 1907 1908 /* A hack to preserve kernel<->userspace interface. 1909 * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1910 * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1911 * what iproute2 < v3.9.0 used. 1912 * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1913 * attribute, its netlink message is shorter than struct ifinfomsg. 1914 */ 1915 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 1916 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1917 1918 return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, 1919 extack); 1920 } 1921 1922 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 1923 { 1924 struct netlink_ext_ack *extack = cb->extack; 1925 const struct nlmsghdr *nlh = cb->nlh; 1926 struct net *net = sock_net(skb->sk); 1927 struct net *tgt_net = net; 1928 int h, s_h; 1929 int idx = 0, s_idx; 1930 struct net_device *dev; 1931 struct hlist_head *head; 1932 struct nlattr *tb[IFLA_MAX+1]; 1933 u32 ext_filter_mask = 0; 1934 const struct rtnl_link_ops *kind_ops = NULL; 1935 unsigned int flags = NLM_F_MULTI; 1936 int master_idx = 0; 1937 int netnsid = -1; 1938 int err, i; 1939 1940 s_h = cb->args[0]; 1941 s_idx = cb->args[1]; 1942 1943 err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack); 1944 if (err < 0) { 1945 if (cb->strict_check) 1946 return err; 1947 1948 goto walk_entries; 1949 } 1950 1951 for (i = 0; i <= IFLA_MAX; ++i) { 1952 if (!tb[i]) 1953 continue; 1954 1955 /* new attributes should only be added with strict checking */ 1956 switch (i) { 1957 case IFLA_TARGET_NETNSID: 1958 netnsid = nla_get_s32(tb[i]); 1959 tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid); 1960 if (IS_ERR(tgt_net)) { 1961 NL_SET_ERR_MSG(extack, "Invalid target network namespace id"); 1962 return PTR_ERR(tgt_net); 1963 } 1964 break; 1965 case IFLA_EXT_MASK: 1966 ext_filter_mask = nla_get_u32(tb[i]); 1967 break; 1968 case IFLA_MASTER: 1969 master_idx = nla_get_u32(tb[i]); 1970 break; 1971 case IFLA_LINKINFO: 1972 kind_ops = linkinfo_to_kind_ops(tb[i]); 1973 break; 1974 default: 1975 if (cb->strict_check) { 1976 NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request"); 1977 return -EINVAL; 1978 } 1979 } 1980 } 1981 1982 if (master_idx || kind_ops) 1983 flags |= NLM_F_DUMP_FILTERED; 1984 1985 walk_entries: 1986 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 1987 idx = 0; 1988 head = &tgt_net->dev_index_head[h]; 1989 hlist_for_each_entry(dev, head, index_hlist) { 1990 if (link_dump_filtered(dev, master_idx, kind_ops)) 1991 goto cont; 1992 if (idx < s_idx) 1993 goto cont; 1994 err = rtnl_fill_ifinfo(skb, dev, net, 1995 RTM_NEWLINK, 1996 NETLINK_CB(cb->skb).portid, 1997 nlh->nlmsg_seq, 0, flags, 1998 ext_filter_mask, 0, NULL, 0, 1999 netnsid); 2000 2001 if (err < 0) { 2002 if (likely(skb->len)) 2003 goto out; 2004 2005 goto out_err; 2006 } 2007 cont: 2008 idx++; 2009 } 2010 } 2011 out: 2012 err = skb->len; 2013 out_err: 2014 cb->args[1] = idx; 2015 cb->args[0] = h; 2016 cb->seq = net->dev_base_seq; 2017 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 2018 if (netnsid >= 0) 2019 put_net(tgt_net); 2020 2021 return err; 2022 } 2023 2024 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 2025 struct netlink_ext_ack *exterr) 2026 { 2027 return nla_parse_deprecated(tb, IFLA_MAX, head, len, ifla_policy, 2028 exterr); 2029 } 2030 EXPORT_SYMBOL(rtnl_nla_parse_ifla); 2031 2032 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 2033 { 2034 struct net *net; 2035 /* Examine the link attributes and figure out which 2036 * network namespace we are talking about. 2037 */ 2038 if (tb[IFLA_NET_NS_PID]) 2039 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 2040 else if (tb[IFLA_NET_NS_FD]) 2041 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 2042 else 2043 net = get_net(src_net); 2044 return net; 2045 } 2046 EXPORT_SYMBOL(rtnl_link_get_net); 2047 2048 /* Figure out which network namespace we are talking about by 2049 * examining the link attributes in the following order: 2050 * 2051 * 1. IFLA_NET_NS_PID 2052 * 2. IFLA_NET_NS_FD 2053 * 3. IFLA_TARGET_NETNSID 2054 */ 2055 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net, 2056 struct nlattr *tb[]) 2057 { 2058 struct net *net; 2059 2060 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) 2061 return rtnl_link_get_net(src_net, tb); 2062 2063 if (!tb[IFLA_TARGET_NETNSID]) 2064 return get_net(src_net); 2065 2066 net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID])); 2067 if (!net) 2068 return ERR_PTR(-EINVAL); 2069 2070 return net; 2071 } 2072 2073 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb, 2074 struct net *src_net, 2075 struct nlattr *tb[], int cap) 2076 { 2077 struct net *net; 2078 2079 net = rtnl_link_get_net_by_nlattr(src_net, tb); 2080 if (IS_ERR(net)) 2081 return net; 2082 2083 if (!netlink_ns_capable(skb, net->user_ns, cap)) { 2084 put_net(net); 2085 return ERR_PTR(-EPERM); 2086 } 2087 2088 return net; 2089 } 2090 2091 /* Verify that rtnetlink requests do not pass additional properties 2092 * potentially referring to different network namespaces. 2093 */ 2094 static int rtnl_ensure_unique_netns(struct nlattr *tb[], 2095 struct netlink_ext_ack *extack, 2096 bool netns_id_only) 2097 { 2098 2099 if (netns_id_only) { 2100 if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD]) 2101 return 0; 2102 2103 NL_SET_ERR_MSG(extack, "specified netns attribute not supported"); 2104 return -EOPNOTSUPP; 2105 } 2106 2107 if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])) 2108 goto invalid_attr; 2109 2110 if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD])) 2111 goto invalid_attr; 2112 2113 if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID])) 2114 goto invalid_attr; 2115 2116 return 0; 2117 2118 invalid_attr: 2119 NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified"); 2120 return -EINVAL; 2121 } 2122 2123 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 2124 { 2125 if (dev) { 2126 if (tb[IFLA_ADDRESS] && 2127 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 2128 return -EINVAL; 2129 2130 if (tb[IFLA_BROADCAST] && 2131 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 2132 return -EINVAL; 2133 } 2134 2135 if (tb[IFLA_AF_SPEC]) { 2136 struct nlattr *af; 2137 int rem, err; 2138 2139 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2140 const struct rtnl_af_ops *af_ops; 2141 2142 rcu_read_lock(); 2143 af_ops = rtnl_af_lookup(nla_type(af)); 2144 if (!af_ops) { 2145 rcu_read_unlock(); 2146 return -EAFNOSUPPORT; 2147 } 2148 2149 if (!af_ops->set_link_af) { 2150 rcu_read_unlock(); 2151 return -EOPNOTSUPP; 2152 } 2153 2154 if (af_ops->validate_link_af) { 2155 err = af_ops->validate_link_af(dev, af); 2156 if (err < 0) { 2157 rcu_read_unlock(); 2158 return err; 2159 } 2160 } 2161 2162 rcu_read_unlock(); 2163 } 2164 } 2165 2166 return 0; 2167 } 2168 2169 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 2170 int guid_type) 2171 { 2172 const struct net_device_ops *ops = dev->netdev_ops; 2173 2174 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 2175 } 2176 2177 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 2178 { 2179 if (dev->type != ARPHRD_INFINIBAND) 2180 return -EOPNOTSUPP; 2181 2182 return handle_infiniband_guid(dev, ivt, guid_type); 2183 } 2184 2185 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 2186 { 2187 const struct net_device_ops *ops = dev->netdev_ops; 2188 int err = -EINVAL; 2189 2190 if (tb[IFLA_VF_MAC]) { 2191 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 2192 2193 err = -EOPNOTSUPP; 2194 if (ops->ndo_set_vf_mac) 2195 err = ops->ndo_set_vf_mac(dev, ivm->vf, 2196 ivm->mac); 2197 if (err < 0) 2198 return err; 2199 } 2200 2201 if (tb[IFLA_VF_VLAN]) { 2202 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 2203 2204 err = -EOPNOTSUPP; 2205 if (ops->ndo_set_vf_vlan) 2206 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 2207 ivv->qos, 2208 htons(ETH_P_8021Q)); 2209 if (err < 0) 2210 return err; 2211 } 2212 2213 if (tb[IFLA_VF_VLAN_LIST]) { 2214 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 2215 struct nlattr *attr; 2216 int rem, len = 0; 2217 2218 err = -EOPNOTSUPP; 2219 if (!ops->ndo_set_vf_vlan) 2220 return err; 2221 2222 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 2223 if (nla_type(attr) != IFLA_VF_VLAN_INFO || 2224 nla_len(attr) < NLA_HDRLEN) { 2225 return -EINVAL; 2226 } 2227 if (len >= MAX_VLAN_LIST_LEN) 2228 return -EOPNOTSUPP; 2229 ivvl[len] = nla_data(attr); 2230 2231 len++; 2232 } 2233 if (len == 0) 2234 return -EINVAL; 2235 2236 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 2237 ivvl[0]->qos, ivvl[0]->vlan_proto); 2238 if (err < 0) 2239 return err; 2240 } 2241 2242 if (tb[IFLA_VF_TX_RATE]) { 2243 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 2244 struct ifla_vf_info ivf; 2245 2246 err = -EOPNOTSUPP; 2247 if (ops->ndo_get_vf_config) 2248 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 2249 if (err < 0) 2250 return err; 2251 2252 err = -EOPNOTSUPP; 2253 if (ops->ndo_set_vf_rate) 2254 err = ops->ndo_set_vf_rate(dev, ivt->vf, 2255 ivf.min_tx_rate, 2256 ivt->rate); 2257 if (err < 0) 2258 return err; 2259 } 2260 2261 if (tb[IFLA_VF_RATE]) { 2262 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 2263 2264 err = -EOPNOTSUPP; 2265 if (ops->ndo_set_vf_rate) 2266 err = ops->ndo_set_vf_rate(dev, ivt->vf, 2267 ivt->min_tx_rate, 2268 ivt->max_tx_rate); 2269 if (err < 0) 2270 return err; 2271 } 2272 2273 if (tb[IFLA_VF_SPOOFCHK]) { 2274 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 2275 2276 err = -EOPNOTSUPP; 2277 if (ops->ndo_set_vf_spoofchk) 2278 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 2279 ivs->setting); 2280 if (err < 0) 2281 return err; 2282 } 2283 2284 if (tb[IFLA_VF_LINK_STATE]) { 2285 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 2286 2287 err = -EOPNOTSUPP; 2288 if (ops->ndo_set_vf_link_state) 2289 err = ops->ndo_set_vf_link_state(dev, ivl->vf, 2290 ivl->link_state); 2291 if (err < 0) 2292 return err; 2293 } 2294 2295 if (tb[IFLA_VF_RSS_QUERY_EN]) { 2296 struct ifla_vf_rss_query_en *ivrssq_en; 2297 2298 err = -EOPNOTSUPP; 2299 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 2300 if (ops->ndo_set_vf_rss_query_en) 2301 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 2302 ivrssq_en->setting); 2303 if (err < 0) 2304 return err; 2305 } 2306 2307 if (tb[IFLA_VF_TRUST]) { 2308 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 2309 2310 err = -EOPNOTSUPP; 2311 if (ops->ndo_set_vf_trust) 2312 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 2313 if (err < 0) 2314 return err; 2315 } 2316 2317 if (tb[IFLA_VF_IB_NODE_GUID]) { 2318 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 2319 2320 if (!ops->ndo_set_vf_guid) 2321 return -EOPNOTSUPP; 2322 2323 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 2324 } 2325 2326 if (tb[IFLA_VF_IB_PORT_GUID]) { 2327 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 2328 2329 if (!ops->ndo_set_vf_guid) 2330 return -EOPNOTSUPP; 2331 2332 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 2333 } 2334 2335 return err; 2336 } 2337 2338 static int do_set_master(struct net_device *dev, int ifindex, 2339 struct netlink_ext_ack *extack) 2340 { 2341 struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 2342 const struct net_device_ops *ops; 2343 int err; 2344 2345 if (upper_dev) { 2346 if (upper_dev->ifindex == ifindex) 2347 return 0; 2348 ops = upper_dev->netdev_ops; 2349 if (ops->ndo_del_slave) { 2350 err = ops->ndo_del_slave(upper_dev, dev); 2351 if (err) 2352 return err; 2353 } else { 2354 return -EOPNOTSUPP; 2355 } 2356 } 2357 2358 if (ifindex) { 2359 upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 2360 if (!upper_dev) 2361 return -EINVAL; 2362 ops = upper_dev->netdev_ops; 2363 if (ops->ndo_add_slave) { 2364 err = ops->ndo_add_slave(upper_dev, dev, extack); 2365 if (err) 2366 return err; 2367 } else { 2368 return -EOPNOTSUPP; 2369 } 2370 } 2371 return 0; 2372 } 2373 2374 #define DO_SETLINK_MODIFIED 0x01 2375 /* notify flag means notify + modified. */ 2376 #define DO_SETLINK_NOTIFY 0x03 2377 static int do_setlink(const struct sk_buff *skb, 2378 struct net_device *dev, struct ifinfomsg *ifm, 2379 struct netlink_ext_ack *extack, 2380 struct nlattr **tb, char *ifname, int status) 2381 { 2382 const struct net_device_ops *ops = dev->netdev_ops; 2383 int err; 2384 2385 err = validate_linkmsg(dev, tb); 2386 if (err < 0) 2387 return err; 2388 2389 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) { 2390 struct net *net = rtnl_link_get_net_capable(skb, dev_net(dev), 2391 tb, CAP_NET_ADMIN); 2392 if (IS_ERR(net)) { 2393 err = PTR_ERR(net); 2394 goto errout; 2395 } 2396 2397 err = dev_change_net_namespace(dev, net, ifname); 2398 put_net(net); 2399 if (err) 2400 goto errout; 2401 status |= DO_SETLINK_MODIFIED; 2402 } 2403 2404 if (tb[IFLA_MAP]) { 2405 struct rtnl_link_ifmap *u_map; 2406 struct ifmap k_map; 2407 2408 if (!ops->ndo_set_config) { 2409 err = -EOPNOTSUPP; 2410 goto errout; 2411 } 2412 2413 if (!netif_device_present(dev)) { 2414 err = -ENODEV; 2415 goto errout; 2416 } 2417 2418 u_map = nla_data(tb[IFLA_MAP]); 2419 k_map.mem_start = (unsigned long) u_map->mem_start; 2420 k_map.mem_end = (unsigned long) u_map->mem_end; 2421 k_map.base_addr = (unsigned short) u_map->base_addr; 2422 k_map.irq = (unsigned char) u_map->irq; 2423 k_map.dma = (unsigned char) u_map->dma; 2424 k_map.port = (unsigned char) u_map->port; 2425 2426 err = ops->ndo_set_config(dev, &k_map); 2427 if (err < 0) 2428 goto errout; 2429 2430 status |= DO_SETLINK_NOTIFY; 2431 } 2432 2433 if (tb[IFLA_ADDRESS]) { 2434 struct sockaddr *sa; 2435 int len; 2436 2437 len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len, 2438 sizeof(*sa)); 2439 sa = kmalloc(len, GFP_KERNEL); 2440 if (!sa) { 2441 err = -ENOMEM; 2442 goto errout; 2443 } 2444 sa->sa_family = dev->type; 2445 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 2446 dev->addr_len); 2447 err = dev_set_mac_address(dev, sa, extack); 2448 kfree(sa); 2449 if (err) 2450 goto errout; 2451 status |= DO_SETLINK_MODIFIED; 2452 } 2453 2454 if (tb[IFLA_MTU]) { 2455 err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack); 2456 if (err < 0) 2457 goto errout; 2458 status |= DO_SETLINK_MODIFIED; 2459 } 2460 2461 if (tb[IFLA_GROUP]) { 2462 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2463 status |= DO_SETLINK_NOTIFY; 2464 } 2465 2466 /* 2467 * Interface selected by interface index but interface 2468 * name provided implies that a name change has been 2469 * requested. 2470 */ 2471 if (ifm->ifi_index > 0 && ifname[0]) { 2472 err = dev_change_name(dev, ifname); 2473 if (err < 0) 2474 goto errout; 2475 status |= DO_SETLINK_MODIFIED; 2476 } 2477 2478 if (tb[IFLA_IFALIAS]) { 2479 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 2480 nla_len(tb[IFLA_IFALIAS])); 2481 if (err < 0) 2482 goto errout; 2483 status |= DO_SETLINK_NOTIFY; 2484 } 2485 2486 if (tb[IFLA_BROADCAST]) { 2487 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2488 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 2489 } 2490 2491 if (ifm->ifi_flags || ifm->ifi_change) { 2492 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2493 extack); 2494 if (err < 0) 2495 goto errout; 2496 } 2497 2498 if (tb[IFLA_MASTER]) { 2499 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 2500 if (err) 2501 goto errout; 2502 status |= DO_SETLINK_MODIFIED; 2503 } 2504 2505 if (tb[IFLA_CARRIER]) { 2506 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 2507 if (err) 2508 goto errout; 2509 status |= DO_SETLINK_MODIFIED; 2510 } 2511 2512 if (tb[IFLA_TXQLEN]) { 2513 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 2514 2515 err = dev_change_tx_queue_len(dev, value); 2516 if (err) 2517 goto errout; 2518 status |= DO_SETLINK_MODIFIED; 2519 } 2520 2521 if (tb[IFLA_GSO_MAX_SIZE]) { 2522 u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]); 2523 2524 if (max_size > GSO_MAX_SIZE) { 2525 err = -EINVAL; 2526 goto errout; 2527 } 2528 2529 if (dev->gso_max_size ^ max_size) { 2530 netif_set_gso_max_size(dev, max_size); 2531 status |= DO_SETLINK_MODIFIED; 2532 } 2533 } 2534 2535 if (tb[IFLA_GSO_MAX_SEGS]) { 2536 u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2537 2538 if (max_segs > GSO_MAX_SEGS) { 2539 err = -EINVAL; 2540 goto errout; 2541 } 2542 2543 if (dev->gso_max_segs ^ max_segs) { 2544 dev->gso_max_segs = max_segs; 2545 status |= DO_SETLINK_MODIFIED; 2546 } 2547 } 2548 2549 if (tb[IFLA_OPERSTATE]) 2550 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2551 2552 if (tb[IFLA_LINKMODE]) { 2553 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 2554 2555 write_lock_bh(&dev_base_lock); 2556 if (dev->link_mode ^ value) 2557 status |= DO_SETLINK_NOTIFY; 2558 dev->link_mode = value; 2559 write_unlock_bh(&dev_base_lock); 2560 } 2561 2562 if (tb[IFLA_VFINFO_LIST]) { 2563 struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2564 struct nlattr *attr; 2565 int rem; 2566 2567 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 2568 if (nla_type(attr) != IFLA_VF_INFO || 2569 nla_len(attr) < NLA_HDRLEN) { 2570 err = -EINVAL; 2571 goto errout; 2572 } 2573 err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX, 2574 attr, 2575 ifla_vf_policy, 2576 NULL); 2577 if (err < 0) 2578 goto errout; 2579 err = do_setvfinfo(dev, vfinfo); 2580 if (err < 0) 2581 goto errout; 2582 status |= DO_SETLINK_NOTIFY; 2583 } 2584 } 2585 err = 0; 2586 2587 if (tb[IFLA_VF_PORTS]) { 2588 struct nlattr *port[IFLA_PORT_MAX+1]; 2589 struct nlattr *attr; 2590 int vf; 2591 int rem; 2592 2593 err = -EOPNOTSUPP; 2594 if (!ops->ndo_set_vf_port) 2595 goto errout; 2596 2597 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2598 if (nla_type(attr) != IFLA_VF_PORT || 2599 nla_len(attr) < NLA_HDRLEN) { 2600 err = -EINVAL; 2601 goto errout; 2602 } 2603 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 2604 attr, 2605 ifla_port_policy, 2606 NULL); 2607 if (err < 0) 2608 goto errout; 2609 if (!port[IFLA_PORT_VF]) { 2610 err = -EOPNOTSUPP; 2611 goto errout; 2612 } 2613 vf = nla_get_u32(port[IFLA_PORT_VF]); 2614 err = ops->ndo_set_vf_port(dev, vf, port); 2615 if (err < 0) 2616 goto errout; 2617 status |= DO_SETLINK_NOTIFY; 2618 } 2619 } 2620 err = 0; 2621 2622 if (tb[IFLA_PORT_SELF]) { 2623 struct nlattr *port[IFLA_PORT_MAX+1]; 2624 2625 err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX, 2626 tb[IFLA_PORT_SELF], 2627 ifla_port_policy, NULL); 2628 if (err < 0) 2629 goto errout; 2630 2631 err = -EOPNOTSUPP; 2632 if (ops->ndo_set_vf_port) 2633 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 2634 if (err < 0) 2635 goto errout; 2636 status |= DO_SETLINK_NOTIFY; 2637 } 2638 2639 if (tb[IFLA_AF_SPEC]) { 2640 struct nlattr *af; 2641 int rem; 2642 2643 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2644 const struct rtnl_af_ops *af_ops; 2645 2646 rcu_read_lock(); 2647 2648 BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af)))); 2649 2650 err = af_ops->set_link_af(dev, af); 2651 if (err < 0) { 2652 rcu_read_unlock(); 2653 goto errout; 2654 } 2655 2656 rcu_read_unlock(); 2657 status |= DO_SETLINK_NOTIFY; 2658 } 2659 } 2660 err = 0; 2661 2662 if (tb[IFLA_PROTO_DOWN]) { 2663 err = dev_change_proto_down(dev, 2664 nla_get_u8(tb[IFLA_PROTO_DOWN])); 2665 if (err) 2666 goto errout; 2667 status |= DO_SETLINK_NOTIFY; 2668 } 2669 2670 if (tb[IFLA_XDP]) { 2671 struct nlattr *xdp[IFLA_XDP_MAX + 1]; 2672 u32 xdp_flags = 0; 2673 2674 err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX, 2675 tb[IFLA_XDP], 2676 ifla_xdp_policy, NULL); 2677 if (err < 0) 2678 goto errout; 2679 2680 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2681 err = -EINVAL; 2682 goto errout; 2683 } 2684 2685 if (xdp[IFLA_XDP_FLAGS]) { 2686 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 2687 if (xdp_flags & ~XDP_FLAGS_MASK) { 2688 err = -EINVAL; 2689 goto errout; 2690 } 2691 if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) { 2692 err = -EINVAL; 2693 goto errout; 2694 } 2695 } 2696 2697 if (xdp[IFLA_XDP_FD]) { 2698 err = dev_change_xdp_fd(dev, extack, 2699 nla_get_s32(xdp[IFLA_XDP_FD]), 2700 xdp_flags); 2701 if (err) 2702 goto errout; 2703 status |= DO_SETLINK_NOTIFY; 2704 } 2705 } 2706 2707 errout: 2708 if (status & DO_SETLINK_MODIFIED) { 2709 if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY) 2710 netdev_state_change(dev); 2711 2712 if (err < 0) 2713 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", 2714 dev->name); 2715 } 2716 2717 return err; 2718 } 2719 2720 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2721 struct netlink_ext_ack *extack) 2722 { 2723 struct net *net = sock_net(skb->sk); 2724 struct ifinfomsg *ifm; 2725 struct net_device *dev; 2726 int err; 2727 struct nlattr *tb[IFLA_MAX+1]; 2728 char ifname[IFNAMSIZ]; 2729 2730 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 2731 ifla_policy, extack); 2732 if (err < 0) 2733 goto errout; 2734 2735 err = rtnl_ensure_unique_netns(tb, extack, false); 2736 if (err < 0) 2737 goto errout; 2738 2739 if (tb[IFLA_IFNAME]) 2740 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2741 else 2742 ifname[0] = '\0'; 2743 2744 err = -EINVAL; 2745 ifm = nlmsg_data(nlh); 2746 if (ifm->ifi_index > 0) 2747 dev = __dev_get_by_index(net, ifm->ifi_index); 2748 else if (tb[IFLA_IFNAME]) 2749 dev = __dev_get_by_name(net, ifname); 2750 else 2751 goto errout; 2752 2753 if (dev == NULL) { 2754 err = -ENODEV; 2755 goto errout; 2756 } 2757 2758 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2759 errout: 2760 return err; 2761 } 2762 2763 static int rtnl_group_dellink(const struct net *net, int group) 2764 { 2765 struct net_device *dev, *aux; 2766 LIST_HEAD(list_kill); 2767 bool found = false; 2768 2769 if (!group) 2770 return -EPERM; 2771 2772 for_each_netdev(net, dev) { 2773 if (dev->group == group) { 2774 const struct rtnl_link_ops *ops; 2775 2776 found = true; 2777 ops = dev->rtnl_link_ops; 2778 if (!ops || !ops->dellink) 2779 return -EOPNOTSUPP; 2780 } 2781 } 2782 2783 if (!found) 2784 return -ENODEV; 2785 2786 for_each_netdev_safe(net, dev, aux) { 2787 if (dev->group == group) { 2788 const struct rtnl_link_ops *ops; 2789 2790 ops = dev->rtnl_link_ops; 2791 ops->dellink(dev, &list_kill); 2792 } 2793 } 2794 unregister_netdevice_many(&list_kill); 2795 2796 return 0; 2797 } 2798 2799 int rtnl_delete_link(struct net_device *dev) 2800 { 2801 const struct rtnl_link_ops *ops; 2802 LIST_HEAD(list_kill); 2803 2804 ops = dev->rtnl_link_ops; 2805 if (!ops || !ops->dellink) 2806 return -EOPNOTSUPP; 2807 2808 ops->dellink(dev, &list_kill); 2809 unregister_netdevice_many(&list_kill); 2810 2811 return 0; 2812 } 2813 EXPORT_SYMBOL_GPL(rtnl_delete_link); 2814 2815 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2816 struct netlink_ext_ack *extack) 2817 { 2818 struct net *net = sock_net(skb->sk); 2819 struct net *tgt_net = net; 2820 struct net_device *dev = NULL; 2821 struct ifinfomsg *ifm; 2822 char ifname[IFNAMSIZ]; 2823 struct nlattr *tb[IFLA_MAX+1]; 2824 int err; 2825 int netnsid = -1; 2826 2827 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 2828 ifla_policy, extack); 2829 if (err < 0) 2830 return err; 2831 2832 err = rtnl_ensure_unique_netns(tb, extack, true); 2833 if (err < 0) 2834 return err; 2835 2836 if (tb[IFLA_IFNAME]) 2837 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2838 2839 if (tb[IFLA_TARGET_NETNSID]) { 2840 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 2841 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 2842 if (IS_ERR(tgt_net)) 2843 return PTR_ERR(tgt_net); 2844 } 2845 2846 err = -EINVAL; 2847 ifm = nlmsg_data(nlh); 2848 if (ifm->ifi_index > 0) 2849 dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 2850 else if (tb[IFLA_IFNAME]) 2851 dev = __dev_get_by_name(tgt_net, ifname); 2852 else if (tb[IFLA_GROUP]) 2853 err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); 2854 else 2855 goto out; 2856 2857 if (!dev) { 2858 if (tb[IFLA_IFNAME] || ifm->ifi_index > 0) 2859 err = -ENODEV; 2860 2861 goto out; 2862 } 2863 2864 err = rtnl_delete_link(dev); 2865 2866 out: 2867 if (netnsid >= 0) 2868 put_net(tgt_net); 2869 2870 return err; 2871 } 2872 2873 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 2874 { 2875 unsigned int old_flags; 2876 int err; 2877 2878 old_flags = dev->flags; 2879 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 2880 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm), 2881 NULL); 2882 if (err < 0) 2883 return err; 2884 } 2885 2886 if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) { 2887 __dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags)); 2888 } else { 2889 dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2890 __dev_notify_flags(dev, old_flags, ~0U); 2891 } 2892 return 0; 2893 } 2894 EXPORT_SYMBOL(rtnl_configure_link); 2895 2896 struct net_device *rtnl_create_link(struct net *net, const char *ifname, 2897 unsigned char name_assign_type, 2898 const struct rtnl_link_ops *ops, 2899 struct nlattr *tb[], 2900 struct netlink_ext_ack *extack) 2901 { 2902 struct net_device *dev; 2903 unsigned int num_tx_queues = 1; 2904 unsigned int num_rx_queues = 1; 2905 2906 if (tb[IFLA_NUM_TX_QUEUES]) 2907 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 2908 else if (ops->get_num_tx_queues) 2909 num_tx_queues = ops->get_num_tx_queues(); 2910 2911 if (tb[IFLA_NUM_RX_QUEUES]) 2912 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 2913 else if (ops->get_num_rx_queues) 2914 num_rx_queues = ops->get_num_rx_queues(); 2915 2916 if (num_tx_queues < 1 || num_tx_queues > 4096) { 2917 NL_SET_ERR_MSG(extack, "Invalid number of transmit queues"); 2918 return ERR_PTR(-EINVAL); 2919 } 2920 2921 if (num_rx_queues < 1 || num_rx_queues > 4096) { 2922 NL_SET_ERR_MSG(extack, "Invalid number of receive queues"); 2923 return ERR_PTR(-EINVAL); 2924 } 2925 2926 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2927 ops->setup, num_tx_queues, num_rx_queues); 2928 if (!dev) 2929 return ERR_PTR(-ENOMEM); 2930 2931 dev_net_set(dev, net); 2932 dev->rtnl_link_ops = ops; 2933 dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 2934 2935 if (tb[IFLA_MTU]) 2936 dev->mtu = nla_get_u32(tb[IFLA_MTU]); 2937 if (tb[IFLA_ADDRESS]) { 2938 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2939 nla_len(tb[IFLA_ADDRESS])); 2940 dev->addr_assign_type = NET_ADDR_SET; 2941 } 2942 if (tb[IFLA_BROADCAST]) 2943 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2944 nla_len(tb[IFLA_BROADCAST])); 2945 if (tb[IFLA_TXQLEN]) 2946 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2947 if (tb[IFLA_OPERSTATE]) 2948 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2949 if (tb[IFLA_LINKMODE]) 2950 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2951 if (tb[IFLA_GROUP]) 2952 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2953 if (tb[IFLA_GSO_MAX_SIZE]) 2954 netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE])); 2955 if (tb[IFLA_GSO_MAX_SEGS]) 2956 dev->gso_max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]); 2957 2958 return dev; 2959 } 2960 EXPORT_SYMBOL(rtnl_create_link); 2961 2962 static int rtnl_group_changelink(const struct sk_buff *skb, 2963 struct net *net, int group, 2964 struct ifinfomsg *ifm, 2965 struct netlink_ext_ack *extack, 2966 struct nlattr **tb) 2967 { 2968 struct net_device *dev, *aux; 2969 int err; 2970 2971 for_each_netdev_safe(net, dev, aux) { 2972 if (dev->group == group) { 2973 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2974 if (err < 0) 2975 return err; 2976 } 2977 } 2978 2979 return 0; 2980 } 2981 2982 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2983 struct nlattr **attr, struct netlink_ext_ack *extack) 2984 { 2985 struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1]; 2986 unsigned char name_assign_type = NET_NAME_USER; 2987 struct nlattr *linkinfo[IFLA_INFO_MAX + 1]; 2988 const struct rtnl_link_ops *m_ops = NULL; 2989 struct net_device *master_dev = NULL; 2990 struct net *net = sock_net(skb->sk); 2991 const struct rtnl_link_ops *ops; 2992 struct nlattr *tb[IFLA_MAX + 1]; 2993 struct net *dest_net, *link_net; 2994 struct nlattr **slave_data; 2995 char kind[MODULE_NAME_LEN]; 2996 struct net_device *dev; 2997 struct ifinfomsg *ifm; 2998 char ifname[IFNAMSIZ]; 2999 struct nlattr **data; 3000 int err; 3001 3002 #ifdef CONFIG_MODULES 3003 replay: 3004 #endif 3005 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 3006 ifla_policy, extack); 3007 if (err < 0) 3008 return err; 3009 3010 err = rtnl_ensure_unique_netns(tb, extack, false); 3011 if (err < 0) 3012 return err; 3013 3014 if (tb[IFLA_IFNAME]) 3015 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3016 else 3017 ifname[0] = '\0'; 3018 3019 ifm = nlmsg_data(nlh); 3020 if (ifm->ifi_index > 0) 3021 dev = __dev_get_by_index(net, ifm->ifi_index); 3022 else { 3023 if (ifname[0]) 3024 dev = __dev_get_by_name(net, ifname); 3025 else 3026 dev = NULL; 3027 } 3028 3029 if (dev) { 3030 master_dev = netdev_master_upper_dev_get(dev); 3031 if (master_dev) 3032 m_ops = master_dev->rtnl_link_ops; 3033 } 3034 3035 err = validate_linkmsg(dev, tb); 3036 if (err < 0) 3037 return err; 3038 3039 if (tb[IFLA_LINKINFO]) { 3040 err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, 3041 tb[IFLA_LINKINFO], 3042 ifla_info_policy, NULL); 3043 if (err < 0) 3044 return err; 3045 } else 3046 memset(linkinfo, 0, sizeof(linkinfo)); 3047 3048 if (linkinfo[IFLA_INFO_KIND]) { 3049 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 3050 ops = rtnl_link_ops_get(kind); 3051 } else { 3052 kind[0] = '\0'; 3053 ops = NULL; 3054 } 3055 3056 data = NULL; 3057 if (ops) { 3058 if (ops->maxtype > RTNL_MAX_TYPE) 3059 return -EINVAL; 3060 3061 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 3062 err = nla_parse_nested_deprecated(attr, ops->maxtype, 3063 linkinfo[IFLA_INFO_DATA], 3064 ops->policy, extack); 3065 if (err < 0) 3066 return err; 3067 data = attr; 3068 } 3069 if (ops->validate) { 3070 err = ops->validate(tb, data, extack); 3071 if (err < 0) 3072 return err; 3073 } 3074 } 3075 3076 slave_data = NULL; 3077 if (m_ops) { 3078 if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE) 3079 return -EINVAL; 3080 3081 if (m_ops->slave_maxtype && 3082 linkinfo[IFLA_INFO_SLAVE_DATA]) { 3083 err = nla_parse_nested_deprecated(slave_attr, 3084 m_ops->slave_maxtype, 3085 linkinfo[IFLA_INFO_SLAVE_DATA], 3086 m_ops->slave_policy, 3087 extack); 3088 if (err < 0) 3089 return err; 3090 slave_data = slave_attr; 3091 } 3092 } 3093 3094 if (dev) { 3095 int status = 0; 3096 3097 if (nlh->nlmsg_flags & NLM_F_EXCL) 3098 return -EEXIST; 3099 if (nlh->nlmsg_flags & NLM_F_REPLACE) 3100 return -EOPNOTSUPP; 3101 3102 if (linkinfo[IFLA_INFO_DATA]) { 3103 if (!ops || ops != dev->rtnl_link_ops || 3104 !ops->changelink) 3105 return -EOPNOTSUPP; 3106 3107 err = ops->changelink(dev, tb, data, extack); 3108 if (err < 0) 3109 return err; 3110 status |= DO_SETLINK_NOTIFY; 3111 } 3112 3113 if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 3114 if (!m_ops || !m_ops->slave_changelink) 3115 return -EOPNOTSUPP; 3116 3117 err = m_ops->slave_changelink(master_dev, dev, tb, 3118 slave_data, extack); 3119 if (err < 0) 3120 return err; 3121 status |= DO_SETLINK_NOTIFY; 3122 } 3123 3124 return do_setlink(skb, dev, ifm, extack, tb, ifname, status); 3125 } 3126 3127 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 3128 if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 3129 return rtnl_group_changelink(skb, net, 3130 nla_get_u32(tb[IFLA_GROUP]), 3131 ifm, extack, tb); 3132 return -ENODEV; 3133 } 3134 3135 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 3136 return -EOPNOTSUPP; 3137 3138 if (!ops) { 3139 #ifdef CONFIG_MODULES 3140 if (kind[0]) { 3141 __rtnl_unlock(); 3142 request_module("rtnl-link-%s", kind); 3143 rtnl_lock(); 3144 ops = rtnl_link_ops_get(kind); 3145 if (ops) 3146 goto replay; 3147 } 3148 #endif 3149 NL_SET_ERR_MSG(extack, "Unknown device type"); 3150 return -EOPNOTSUPP; 3151 } 3152 3153 if (!ops->setup) 3154 return -EOPNOTSUPP; 3155 3156 if (!ifname[0]) { 3157 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 3158 name_assign_type = NET_NAME_ENUM; 3159 } 3160 3161 dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN); 3162 if (IS_ERR(dest_net)) 3163 return PTR_ERR(dest_net); 3164 3165 if (tb[IFLA_LINK_NETNSID]) { 3166 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 3167 3168 link_net = get_net_ns_by_id(dest_net, id); 3169 if (!link_net) { 3170 NL_SET_ERR_MSG(extack, "Unknown network namespace id"); 3171 err = -EINVAL; 3172 goto out; 3173 } 3174 err = -EPERM; 3175 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 3176 goto out; 3177 } else { 3178 link_net = NULL; 3179 } 3180 3181 dev = rtnl_create_link(link_net ? : dest_net, ifname, 3182 name_assign_type, ops, tb, extack); 3183 if (IS_ERR(dev)) { 3184 err = PTR_ERR(dev); 3185 goto out; 3186 } 3187 3188 dev->ifindex = ifm->ifi_index; 3189 3190 if (ops->newlink) { 3191 err = ops->newlink(link_net ? : net, dev, tb, data, extack); 3192 /* Drivers should call free_netdev() in ->destructor 3193 * and unregister it on failure after registration 3194 * so that device could be finally freed in rtnl_unlock. 3195 */ 3196 if (err < 0) { 3197 /* If device is not registered at all, free it now */ 3198 if (dev->reg_state == NETREG_UNINITIALIZED) 3199 free_netdev(dev); 3200 goto out; 3201 } 3202 } else { 3203 err = register_netdevice(dev); 3204 if (err < 0) { 3205 free_netdev(dev); 3206 goto out; 3207 } 3208 } 3209 err = rtnl_configure_link(dev, ifm); 3210 if (err < 0) 3211 goto out_unregister; 3212 if (link_net) { 3213 err = dev_change_net_namespace(dev, dest_net, ifname); 3214 if (err < 0) 3215 goto out_unregister; 3216 } 3217 if (tb[IFLA_MASTER]) { 3218 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack); 3219 if (err) 3220 goto out_unregister; 3221 } 3222 out: 3223 if (link_net) 3224 put_net(link_net); 3225 put_net(dest_net); 3226 return err; 3227 out_unregister: 3228 if (ops->newlink) { 3229 LIST_HEAD(list_kill); 3230 3231 ops->dellink(dev, &list_kill); 3232 unregister_netdevice_many(&list_kill); 3233 } else { 3234 unregister_netdevice(dev); 3235 } 3236 goto out; 3237 } 3238 3239 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3240 struct netlink_ext_ack *extack) 3241 { 3242 struct nlattr **attr; 3243 int ret; 3244 3245 attr = kmalloc_array(RTNL_MAX_TYPE + 1, sizeof(*attr), GFP_KERNEL); 3246 if (!attr) 3247 return -ENOMEM; 3248 3249 ret = __rtnl_newlink(skb, nlh, attr, extack); 3250 kfree(attr); 3251 return ret; 3252 } 3253 3254 static int rtnl_valid_getlink_req(struct sk_buff *skb, 3255 const struct nlmsghdr *nlh, 3256 struct nlattr **tb, 3257 struct netlink_ext_ack *extack) 3258 { 3259 struct ifinfomsg *ifm; 3260 int i, err; 3261 3262 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 3263 NL_SET_ERR_MSG(extack, "Invalid header for get link"); 3264 return -EINVAL; 3265 } 3266 3267 if (!netlink_strict_get_check(skb)) 3268 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX, 3269 ifla_policy, extack); 3270 3271 ifm = nlmsg_data(nlh); 3272 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 3273 ifm->ifi_change) { 3274 NL_SET_ERR_MSG(extack, "Invalid values in header for get link request"); 3275 return -EINVAL; 3276 } 3277 3278 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX, 3279 ifla_policy, extack); 3280 if (err) 3281 return err; 3282 3283 for (i = 0; i <= IFLA_MAX; i++) { 3284 if (!tb[i]) 3285 continue; 3286 3287 switch (i) { 3288 case IFLA_IFNAME: 3289 case IFLA_EXT_MASK: 3290 case IFLA_TARGET_NETNSID: 3291 break; 3292 default: 3293 NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request"); 3294 return -EINVAL; 3295 } 3296 } 3297 3298 return 0; 3299 } 3300 3301 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3302 struct netlink_ext_ack *extack) 3303 { 3304 struct net *net = sock_net(skb->sk); 3305 struct net *tgt_net = net; 3306 struct ifinfomsg *ifm; 3307 char ifname[IFNAMSIZ]; 3308 struct nlattr *tb[IFLA_MAX+1]; 3309 struct net_device *dev = NULL; 3310 struct sk_buff *nskb; 3311 int netnsid = -1; 3312 int err; 3313 u32 ext_filter_mask = 0; 3314 3315 err = rtnl_valid_getlink_req(skb, nlh, tb, extack); 3316 if (err < 0) 3317 return err; 3318 3319 err = rtnl_ensure_unique_netns(tb, extack, true); 3320 if (err < 0) 3321 return err; 3322 3323 if (tb[IFLA_TARGET_NETNSID]) { 3324 netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); 3325 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); 3326 if (IS_ERR(tgt_net)) 3327 return PTR_ERR(tgt_net); 3328 } 3329 3330 if (tb[IFLA_IFNAME]) 3331 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 3332 3333 if (tb[IFLA_EXT_MASK]) 3334 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3335 3336 err = -EINVAL; 3337 ifm = nlmsg_data(nlh); 3338 if (ifm->ifi_index > 0) 3339 dev = __dev_get_by_index(tgt_net, ifm->ifi_index); 3340 else if (tb[IFLA_IFNAME]) 3341 dev = __dev_get_by_name(tgt_net, ifname); 3342 else 3343 goto out; 3344 3345 err = -ENODEV; 3346 if (dev == NULL) 3347 goto out; 3348 3349 err = -ENOBUFS; 3350 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 3351 if (nskb == NULL) 3352 goto out; 3353 3354 err = rtnl_fill_ifinfo(nskb, dev, net, 3355 RTM_NEWLINK, NETLINK_CB(skb).portid, 3356 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 3357 0, NULL, 0, netnsid); 3358 if (err < 0) { 3359 /* -EMSGSIZE implies BUG in if_nlmsg_size */ 3360 WARN_ON(err == -EMSGSIZE); 3361 kfree_skb(nskb); 3362 } else 3363 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 3364 out: 3365 if (netnsid >= 0) 3366 put_net(tgt_net); 3367 3368 return err; 3369 } 3370 3371 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 3372 { 3373 struct net *net = sock_net(skb->sk); 3374 struct net_device *dev; 3375 struct nlattr *tb[IFLA_MAX+1]; 3376 u32 ext_filter_mask = 0; 3377 u16 min_ifinfo_dump_size = 0; 3378 int hdrlen; 3379 3380 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 3381 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 3382 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 3383 3384 if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 3385 if (tb[IFLA_EXT_MASK]) 3386 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 3387 } 3388 3389 if (!ext_filter_mask) 3390 return NLMSG_GOODSIZE; 3391 /* 3392 * traverse the list of net devices and compute the minimum 3393 * buffer size based upon the filter mask. 3394 */ 3395 rcu_read_lock(); 3396 for_each_netdev_rcu(net, dev) { 3397 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 3398 if_nlmsg_size(dev, 3399 ext_filter_mask)); 3400 } 3401 rcu_read_unlock(); 3402 3403 return nlmsg_total_size(min_ifinfo_dump_size); 3404 } 3405 3406 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 3407 { 3408 int idx; 3409 int s_idx = cb->family; 3410 int type = cb->nlh->nlmsg_type - RTM_BASE; 3411 int ret = 0; 3412 3413 if (s_idx == 0) 3414 s_idx = 1; 3415 3416 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 3417 struct rtnl_link **tab; 3418 struct rtnl_link *link; 3419 rtnl_dumpit_func dumpit; 3420 3421 if (idx < s_idx || idx == PF_PACKET) 3422 continue; 3423 3424 if (type < 0 || type >= RTM_NR_MSGTYPES) 3425 continue; 3426 3427 tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]); 3428 if (!tab) 3429 continue; 3430 3431 link = tab[type]; 3432 if (!link) 3433 continue; 3434 3435 dumpit = link->dumpit; 3436 if (!dumpit) 3437 continue; 3438 3439 if (idx > s_idx) { 3440 memset(&cb->args[0], 0, sizeof(cb->args)); 3441 cb->prev_seq = 0; 3442 cb->seq = 0; 3443 } 3444 ret = dumpit(skb, cb); 3445 if (ret) 3446 break; 3447 } 3448 cb->family = idx; 3449 3450 return skb->len ? : ret; 3451 } 3452 3453 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 3454 unsigned int change, 3455 u32 event, gfp_t flags, int *new_nsid, 3456 int new_ifindex) 3457 { 3458 struct net *net = dev_net(dev); 3459 struct sk_buff *skb; 3460 int err = -ENOBUFS; 3461 size_t if_info_size; 3462 3463 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 3464 if (skb == NULL) 3465 goto errout; 3466 3467 err = rtnl_fill_ifinfo(skb, dev, dev_net(dev), 3468 type, 0, 0, change, 0, 0, event, 3469 new_nsid, new_ifindex, -1); 3470 if (err < 0) { 3471 /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 3472 WARN_ON(err == -EMSGSIZE); 3473 kfree_skb(skb); 3474 goto errout; 3475 } 3476 return skb; 3477 errout: 3478 if (err < 0) 3479 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3480 return NULL; 3481 } 3482 3483 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 3484 { 3485 struct net *net = dev_net(dev); 3486 3487 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 3488 } 3489 3490 static void rtmsg_ifinfo_event(int type, struct net_device *dev, 3491 unsigned int change, u32 event, 3492 gfp_t flags, int *new_nsid, int new_ifindex) 3493 { 3494 struct sk_buff *skb; 3495 3496 if (dev->reg_state != NETREG_REGISTERED) 3497 return; 3498 3499 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid, 3500 new_ifindex); 3501 if (skb) 3502 rtmsg_ifinfo_send(skb, dev, flags); 3503 } 3504 3505 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 3506 gfp_t flags) 3507 { 3508 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 3509 NULL, 0); 3510 } 3511 3512 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, 3513 gfp_t flags, int *new_nsid, int new_ifindex) 3514 { 3515 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags, 3516 new_nsid, new_ifindex); 3517 } 3518 3519 static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 3520 struct net_device *dev, 3521 u8 *addr, u16 vid, u32 pid, u32 seq, 3522 int type, unsigned int flags, 3523 int nlflags, u16 ndm_state) 3524 { 3525 struct nlmsghdr *nlh; 3526 struct ndmsg *ndm; 3527 3528 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 3529 if (!nlh) 3530 return -EMSGSIZE; 3531 3532 ndm = nlmsg_data(nlh); 3533 ndm->ndm_family = AF_BRIDGE; 3534 ndm->ndm_pad1 = 0; 3535 ndm->ndm_pad2 = 0; 3536 ndm->ndm_flags = flags; 3537 ndm->ndm_type = 0; 3538 ndm->ndm_ifindex = dev->ifindex; 3539 ndm->ndm_state = ndm_state; 3540 3541 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 3542 goto nla_put_failure; 3543 if (vid) 3544 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 3545 goto nla_put_failure; 3546 3547 nlmsg_end(skb, nlh); 3548 return 0; 3549 3550 nla_put_failure: 3551 nlmsg_cancel(skb, nlh); 3552 return -EMSGSIZE; 3553 } 3554 3555 static inline size_t rtnl_fdb_nlmsg_size(void) 3556 { 3557 return NLMSG_ALIGN(sizeof(struct ndmsg)) + 3558 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 3559 nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 3560 0; 3561 } 3562 3563 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 3564 u16 ndm_state) 3565 { 3566 struct net *net = dev_net(dev); 3567 struct sk_buff *skb; 3568 int err = -ENOBUFS; 3569 3570 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 3571 if (!skb) 3572 goto errout; 3573 3574 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 3575 0, 0, type, NTF_SELF, 0, ndm_state); 3576 if (err < 0) { 3577 kfree_skb(skb); 3578 goto errout; 3579 } 3580 3581 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 3582 return; 3583 errout: 3584 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 3585 } 3586 3587 /* 3588 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 3589 */ 3590 int ndo_dflt_fdb_add(struct ndmsg *ndm, 3591 struct nlattr *tb[], 3592 struct net_device *dev, 3593 const unsigned char *addr, u16 vid, 3594 u16 flags) 3595 { 3596 int err = -EINVAL; 3597 3598 /* If aging addresses are supported device will need to 3599 * implement its own handler for this. 3600 */ 3601 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3602 pr_info("%s: FDB only supports static addresses\n", dev->name); 3603 return err; 3604 } 3605 3606 if (vid) { 3607 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 3608 return err; 3609 } 3610 3611 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3612 err = dev_uc_add_excl(dev, addr); 3613 else if (is_multicast_ether_addr(addr)) 3614 err = dev_mc_add_excl(dev, addr); 3615 3616 /* Only return duplicate errors if NLM_F_EXCL is set */ 3617 if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3618 err = 0; 3619 3620 return err; 3621 } 3622 EXPORT_SYMBOL(ndo_dflt_fdb_add); 3623 3624 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid, 3625 struct netlink_ext_ack *extack) 3626 { 3627 u16 vid = 0; 3628 3629 if (vlan_attr) { 3630 if (nla_len(vlan_attr) != sizeof(u16)) { 3631 NL_SET_ERR_MSG(extack, "invalid vlan attribute size"); 3632 return -EINVAL; 3633 } 3634 3635 vid = nla_get_u16(vlan_attr); 3636 3637 if (!vid || vid >= VLAN_VID_MASK) { 3638 NL_SET_ERR_MSG(extack, "invalid vlan id"); 3639 return -EINVAL; 3640 } 3641 } 3642 *p_vid = vid; 3643 return 0; 3644 } 3645 3646 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3647 struct netlink_ext_ack *extack) 3648 { 3649 struct net *net = sock_net(skb->sk); 3650 struct ndmsg *ndm; 3651 struct nlattr *tb[NDA_MAX+1]; 3652 struct net_device *dev; 3653 u8 *addr; 3654 u16 vid; 3655 int err; 3656 3657 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 3658 extack); 3659 if (err < 0) 3660 return err; 3661 3662 ndm = nlmsg_data(nlh); 3663 if (ndm->ndm_ifindex == 0) { 3664 NL_SET_ERR_MSG(extack, "invalid ifindex"); 3665 return -EINVAL; 3666 } 3667 3668 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 3669 if (dev == NULL) { 3670 NL_SET_ERR_MSG(extack, "unknown ifindex"); 3671 return -ENODEV; 3672 } 3673 3674 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3675 NL_SET_ERR_MSG(extack, "invalid address"); 3676 return -EINVAL; 3677 } 3678 3679 if (dev->type != ARPHRD_ETHER) { 3680 NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices"); 3681 return -EINVAL; 3682 } 3683 3684 addr = nla_data(tb[NDA_LLADDR]); 3685 3686 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3687 if (err) 3688 return err; 3689 3690 err = -EOPNOTSUPP; 3691 3692 /* Support fdb on master device the net/bridge default case */ 3693 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 3694 (dev->priv_flags & IFF_BRIDGE_PORT)) { 3695 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3696 const struct net_device_ops *ops = br_dev->netdev_ops; 3697 3698 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3699 nlh->nlmsg_flags, extack); 3700 if (err) 3701 goto out; 3702 else 3703 ndm->ndm_flags &= ~NTF_MASTER; 3704 } 3705 3706 /* Embedded bridge, macvlan, and any other device support */ 3707 if ((ndm->ndm_flags & NTF_SELF)) { 3708 if (dev->netdev_ops->ndo_fdb_add) 3709 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3710 vid, 3711 nlh->nlmsg_flags, 3712 extack); 3713 else 3714 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 3715 nlh->nlmsg_flags); 3716 3717 if (!err) { 3718 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3719 ndm->ndm_state); 3720 ndm->ndm_flags &= ~NTF_SELF; 3721 } 3722 } 3723 out: 3724 return err; 3725 } 3726 3727 /* 3728 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3729 */ 3730 int ndo_dflt_fdb_del(struct ndmsg *ndm, 3731 struct nlattr *tb[], 3732 struct net_device *dev, 3733 const unsigned char *addr, u16 vid) 3734 { 3735 int err = -EINVAL; 3736 3737 /* If aging addresses are supported device will need to 3738 * implement its own handler for this. 3739 */ 3740 if (!(ndm->ndm_state & NUD_PERMANENT)) { 3741 pr_info("%s: FDB only supports static addresses\n", dev->name); 3742 return err; 3743 } 3744 3745 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3746 err = dev_uc_del(dev, addr); 3747 else if (is_multicast_ether_addr(addr)) 3748 err = dev_mc_del(dev, addr); 3749 3750 return err; 3751 } 3752 EXPORT_SYMBOL(ndo_dflt_fdb_del); 3753 3754 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3755 struct netlink_ext_ack *extack) 3756 { 3757 struct net *net = sock_net(skb->sk); 3758 struct ndmsg *ndm; 3759 struct nlattr *tb[NDA_MAX+1]; 3760 struct net_device *dev; 3761 int err = -EINVAL; 3762 __u8 *addr; 3763 u16 vid; 3764 3765 if (!netlink_capable(skb, CAP_NET_ADMIN)) 3766 return -EPERM; 3767 3768 err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, 3769 extack); 3770 if (err < 0) 3771 return err; 3772 3773 ndm = nlmsg_data(nlh); 3774 if (ndm->ndm_ifindex == 0) { 3775 NL_SET_ERR_MSG(extack, "invalid ifindex"); 3776 return -EINVAL; 3777 } 3778 3779 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 3780 if (dev == NULL) { 3781 NL_SET_ERR_MSG(extack, "unknown ifindex"); 3782 return -ENODEV; 3783 } 3784 3785 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3786 NL_SET_ERR_MSG(extack, "invalid address"); 3787 return -EINVAL; 3788 } 3789 3790 if (dev->type != ARPHRD_ETHER) { 3791 NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices"); 3792 return -EINVAL; 3793 } 3794 3795 addr = nla_data(tb[NDA_LLADDR]); 3796 3797 err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack); 3798 if (err) 3799 return err; 3800 3801 err = -EOPNOTSUPP; 3802 3803 /* Support fdb on master device the net/bridge default case */ 3804 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 3805 (dev->priv_flags & IFF_BRIDGE_PORT)) { 3806 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3807 const struct net_device_ops *ops = br_dev->netdev_ops; 3808 3809 if (ops->ndo_fdb_del) 3810 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 3811 3812 if (err) 3813 goto out; 3814 else 3815 ndm->ndm_flags &= ~NTF_MASTER; 3816 } 3817 3818 /* Embedded bridge, macvlan, and any other device support */ 3819 if (ndm->ndm_flags & NTF_SELF) { 3820 if (dev->netdev_ops->ndo_fdb_del) 3821 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3822 vid); 3823 else 3824 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 3825 3826 if (!err) { 3827 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3828 ndm->ndm_state); 3829 ndm->ndm_flags &= ~NTF_SELF; 3830 } 3831 } 3832 out: 3833 return err; 3834 } 3835 3836 static int nlmsg_populate_fdb(struct sk_buff *skb, 3837 struct netlink_callback *cb, 3838 struct net_device *dev, 3839 int *idx, 3840 struct netdev_hw_addr_list *list) 3841 { 3842 struct netdev_hw_addr *ha; 3843 int err; 3844 u32 portid, seq; 3845 3846 portid = NETLINK_CB(cb->skb).portid; 3847 seq = cb->nlh->nlmsg_seq; 3848 3849 list_for_each_entry(ha, &list->list, list) { 3850 if (*idx < cb->args[2]) 3851 goto skip; 3852 3853 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3854 portid, seq, 3855 RTM_NEWNEIGH, NTF_SELF, 3856 NLM_F_MULTI, NUD_PERMANENT); 3857 if (err < 0) 3858 return err; 3859 skip: 3860 *idx += 1; 3861 } 3862 return 0; 3863 } 3864 3865 /** 3866 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3867 * @skb: socket buffer to store message in 3868 * @cb: netlink callback 3869 * @dev: netdevice 3870 * @filter_dev: ignored 3871 * @idx: the number of FDB table entries dumped is added to *@idx 3872 * 3873 * Default netdevice operation to dump the existing unicast address list. 3874 * Returns number of addresses from list put in skb. 3875 */ 3876 int ndo_dflt_fdb_dump(struct sk_buff *skb, 3877 struct netlink_callback *cb, 3878 struct net_device *dev, 3879 struct net_device *filter_dev, 3880 int *idx) 3881 { 3882 int err; 3883 3884 if (dev->type != ARPHRD_ETHER) 3885 return -EINVAL; 3886 3887 netif_addr_lock_bh(dev); 3888 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3889 if (err) 3890 goto out; 3891 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3892 out: 3893 netif_addr_unlock_bh(dev); 3894 return err; 3895 } 3896 EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3897 3898 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh, 3899 int *br_idx, int *brport_idx, 3900 struct netlink_ext_ack *extack) 3901 { 3902 struct nlattr *tb[NDA_MAX + 1]; 3903 struct ndmsg *ndm; 3904 int err, i; 3905 3906 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 3907 NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request"); 3908 return -EINVAL; 3909 } 3910 3911 ndm = nlmsg_data(nlh); 3912 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 3913 ndm->ndm_flags || ndm->ndm_type) { 3914 NL_SET_ERR_MSG(extack, "Invalid values in header for fbd dump request"); 3915 return -EINVAL; 3916 } 3917 3918 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 3919 NDA_MAX, NULL, extack); 3920 if (err < 0) 3921 return err; 3922 3923 *brport_idx = ndm->ndm_ifindex; 3924 for (i = 0; i <= NDA_MAX; ++i) { 3925 if (!tb[i]) 3926 continue; 3927 3928 switch (i) { 3929 case NDA_IFINDEX: 3930 if (nla_len(tb[i]) != sizeof(u32)) { 3931 NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request"); 3932 return -EINVAL; 3933 } 3934 *brport_idx = nla_get_u32(tb[NDA_IFINDEX]); 3935 break; 3936 case NDA_MASTER: 3937 if (nla_len(tb[i]) != sizeof(u32)) { 3938 NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request"); 3939 return -EINVAL; 3940 } 3941 *br_idx = nla_get_u32(tb[NDA_MASTER]); 3942 break; 3943 default: 3944 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request"); 3945 return -EINVAL; 3946 } 3947 } 3948 3949 return 0; 3950 } 3951 3952 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh, 3953 int *br_idx, int *brport_idx, 3954 struct netlink_ext_ack *extack) 3955 { 3956 struct nlattr *tb[IFLA_MAX+1]; 3957 int err; 3958 3959 /* A hack to preserve kernel<->userspace interface. 3960 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0. 3961 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails. 3962 * So, check for ndmsg with an optional u32 attribute (not used here). 3963 * Fortunately these sizes don't conflict with the size of ifinfomsg 3964 * with an optional attribute. 3965 */ 3966 if (nlmsg_len(nlh) != sizeof(struct ndmsg) && 3967 (nlmsg_len(nlh) != sizeof(struct ndmsg) + 3968 nla_attr_size(sizeof(u32)))) { 3969 struct ifinfomsg *ifm; 3970 3971 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 3972 tb, IFLA_MAX, ifla_policy, 3973 extack); 3974 if (err < 0) { 3975 return -EINVAL; 3976 } else if (err == 0) { 3977 if (tb[IFLA_MASTER]) 3978 *br_idx = nla_get_u32(tb[IFLA_MASTER]); 3979 } 3980 3981 ifm = nlmsg_data(nlh); 3982 *brport_idx = ifm->ifi_index; 3983 } 3984 return 0; 3985 } 3986 3987 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 3988 { 3989 struct net_device *dev; 3990 struct net_device *br_dev = NULL; 3991 const struct net_device_ops *ops = NULL; 3992 const struct net_device_ops *cops = NULL; 3993 struct net *net = sock_net(skb->sk); 3994 struct hlist_head *head; 3995 int brport_idx = 0; 3996 int br_idx = 0; 3997 int h, s_h; 3998 int idx = 0, s_idx; 3999 int err = 0; 4000 int fidx = 0; 4001 4002 if (cb->strict_check) 4003 err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx, 4004 cb->extack); 4005 else 4006 err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx, 4007 cb->extack); 4008 if (err < 0) 4009 return err; 4010 4011 if (br_idx) { 4012 br_dev = __dev_get_by_index(net, br_idx); 4013 if (!br_dev) 4014 return -ENODEV; 4015 4016 ops = br_dev->netdev_ops; 4017 } 4018 4019 s_h = cb->args[0]; 4020 s_idx = cb->args[1]; 4021 4022 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4023 idx = 0; 4024 head = &net->dev_index_head[h]; 4025 hlist_for_each_entry(dev, head, index_hlist) { 4026 4027 if (brport_idx && (dev->ifindex != brport_idx)) 4028 continue; 4029 4030 if (!br_idx) { /* user did not specify a specific bridge */ 4031 if (dev->priv_flags & IFF_BRIDGE_PORT) { 4032 br_dev = netdev_master_upper_dev_get(dev); 4033 cops = br_dev->netdev_ops; 4034 } 4035 } else { 4036 if (dev != br_dev && 4037 !(dev->priv_flags & IFF_BRIDGE_PORT)) 4038 continue; 4039 4040 if (br_dev != netdev_master_upper_dev_get(dev) && 4041 !(dev->priv_flags & IFF_EBRIDGE)) 4042 continue; 4043 cops = ops; 4044 } 4045 4046 if (idx < s_idx) 4047 goto cont; 4048 4049 if (dev->priv_flags & IFF_BRIDGE_PORT) { 4050 if (cops && cops->ndo_fdb_dump) { 4051 err = cops->ndo_fdb_dump(skb, cb, 4052 br_dev, dev, 4053 &fidx); 4054 if (err == -EMSGSIZE) 4055 goto out; 4056 } 4057 } 4058 4059 if (dev->netdev_ops->ndo_fdb_dump) 4060 err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 4061 dev, NULL, 4062 &fidx); 4063 else 4064 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 4065 &fidx); 4066 if (err == -EMSGSIZE) 4067 goto out; 4068 4069 cops = NULL; 4070 4071 /* reset fdb offset to 0 for rest of the interfaces */ 4072 cb->args[2] = 0; 4073 fidx = 0; 4074 cont: 4075 idx++; 4076 } 4077 } 4078 4079 out: 4080 cb->args[0] = h; 4081 cb->args[1] = idx; 4082 cb->args[2] = fidx; 4083 4084 return skb->len; 4085 } 4086 4087 static int valid_fdb_get_strict(const struct nlmsghdr *nlh, 4088 struct nlattr **tb, u8 *ndm_flags, 4089 int *br_idx, int *brport_idx, u8 **addr, 4090 u16 *vid, struct netlink_ext_ack *extack) 4091 { 4092 struct ndmsg *ndm; 4093 int err, i; 4094 4095 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) { 4096 NL_SET_ERR_MSG(extack, "Invalid header for fdb get request"); 4097 return -EINVAL; 4098 } 4099 4100 ndm = nlmsg_data(nlh); 4101 if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state || 4102 ndm->ndm_type) { 4103 NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request"); 4104 return -EINVAL; 4105 } 4106 4107 if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) { 4108 NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request"); 4109 return -EINVAL; 4110 } 4111 4112 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb, 4113 NDA_MAX, nda_policy, extack); 4114 if (err < 0) 4115 return err; 4116 4117 *ndm_flags = ndm->ndm_flags; 4118 *brport_idx = ndm->ndm_ifindex; 4119 for (i = 0; i <= NDA_MAX; ++i) { 4120 if (!tb[i]) 4121 continue; 4122 4123 switch (i) { 4124 case NDA_MASTER: 4125 *br_idx = nla_get_u32(tb[i]); 4126 break; 4127 case NDA_LLADDR: 4128 if (nla_len(tb[i]) != ETH_ALEN) { 4129 NL_SET_ERR_MSG(extack, "Invalid address in fdb get request"); 4130 return -EINVAL; 4131 } 4132 *addr = nla_data(tb[i]); 4133 break; 4134 case NDA_VLAN: 4135 err = fdb_vid_parse(tb[i], vid, extack); 4136 if (err) 4137 return err; 4138 break; 4139 case NDA_VNI: 4140 break; 4141 default: 4142 NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request"); 4143 return -EINVAL; 4144 } 4145 } 4146 4147 return 0; 4148 } 4149 4150 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh, 4151 struct netlink_ext_ack *extack) 4152 { 4153 struct net_device *dev = NULL, *br_dev = NULL; 4154 const struct net_device_ops *ops = NULL; 4155 struct net *net = sock_net(in_skb->sk); 4156 struct nlattr *tb[NDA_MAX + 1]; 4157 struct sk_buff *skb; 4158 int brport_idx = 0; 4159 u8 ndm_flags = 0; 4160 int br_idx = 0; 4161 u8 *addr = NULL; 4162 u16 vid = 0; 4163 int err; 4164 4165 err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx, 4166 &brport_idx, &addr, &vid, extack); 4167 if (err < 0) 4168 return err; 4169 4170 if (!addr) { 4171 NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request"); 4172 return -EINVAL; 4173 } 4174 4175 if (brport_idx) { 4176 dev = __dev_get_by_index(net, brport_idx); 4177 if (!dev) { 4178 NL_SET_ERR_MSG(extack, "Unknown device ifindex"); 4179 return -ENODEV; 4180 } 4181 } 4182 4183 if (br_idx) { 4184 if (dev) { 4185 NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive"); 4186 return -EINVAL; 4187 } 4188 4189 br_dev = __dev_get_by_index(net, br_idx); 4190 if (!br_dev) { 4191 NL_SET_ERR_MSG(extack, "Invalid master ifindex"); 4192 return -EINVAL; 4193 } 4194 ops = br_dev->netdev_ops; 4195 } 4196 4197 if (dev) { 4198 if (!ndm_flags || (ndm_flags & NTF_MASTER)) { 4199 if (!(dev->priv_flags & IFF_BRIDGE_PORT)) { 4200 NL_SET_ERR_MSG(extack, "Device is not a bridge port"); 4201 return -EINVAL; 4202 } 4203 br_dev = netdev_master_upper_dev_get(dev); 4204 if (!br_dev) { 4205 NL_SET_ERR_MSG(extack, "Master of device not found"); 4206 return -EINVAL; 4207 } 4208 ops = br_dev->netdev_ops; 4209 } else { 4210 if (!(ndm_flags & NTF_SELF)) { 4211 NL_SET_ERR_MSG(extack, "Missing NTF_SELF"); 4212 return -EINVAL; 4213 } 4214 ops = dev->netdev_ops; 4215 } 4216 } 4217 4218 if (!br_dev && !dev) { 4219 NL_SET_ERR_MSG(extack, "No device specified"); 4220 return -ENODEV; 4221 } 4222 4223 if (!ops || !ops->ndo_fdb_get) { 4224 NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device"); 4225 return -EOPNOTSUPP; 4226 } 4227 4228 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); 4229 if (!skb) 4230 return -ENOBUFS; 4231 4232 if (br_dev) 4233 dev = br_dev; 4234 err = ops->ndo_fdb_get(skb, tb, dev, addr, vid, 4235 NETLINK_CB(in_skb).portid, 4236 nlh->nlmsg_seq, extack); 4237 if (err) 4238 goto out; 4239 4240 return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 4241 out: 4242 kfree_skb(skb); 4243 return err; 4244 } 4245 4246 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 4247 unsigned int attrnum, unsigned int flag) 4248 { 4249 if (mask & flag) 4250 return nla_put_u8(skb, attrnum, !!(flags & flag)); 4251 return 0; 4252 } 4253 4254 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 4255 struct net_device *dev, u16 mode, 4256 u32 flags, u32 mask, int nlflags, 4257 u32 filter_mask, 4258 int (*vlan_fill)(struct sk_buff *skb, 4259 struct net_device *dev, 4260 u32 filter_mask)) 4261 { 4262 struct nlmsghdr *nlh; 4263 struct ifinfomsg *ifm; 4264 struct nlattr *br_afspec; 4265 struct nlattr *protinfo; 4266 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 4267 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4268 int err = 0; 4269 4270 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 4271 if (nlh == NULL) 4272 return -EMSGSIZE; 4273 4274 ifm = nlmsg_data(nlh); 4275 ifm->ifi_family = AF_BRIDGE; 4276 ifm->__ifi_pad = 0; 4277 ifm->ifi_type = dev->type; 4278 ifm->ifi_index = dev->ifindex; 4279 ifm->ifi_flags = dev_get_flags(dev); 4280 ifm->ifi_change = 0; 4281 4282 4283 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 4284 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4285 nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 4286 (br_dev && 4287 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 4288 (dev->addr_len && 4289 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4290 (dev->ifindex != dev_get_iflink(dev) && 4291 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 4292 goto nla_put_failure; 4293 4294 br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 4295 if (!br_afspec) 4296 goto nla_put_failure; 4297 4298 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 4299 nla_nest_cancel(skb, br_afspec); 4300 goto nla_put_failure; 4301 } 4302 4303 if (mode != BRIDGE_MODE_UNDEF) { 4304 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 4305 nla_nest_cancel(skb, br_afspec); 4306 goto nla_put_failure; 4307 } 4308 } 4309 if (vlan_fill) { 4310 err = vlan_fill(skb, dev, filter_mask); 4311 if (err) { 4312 nla_nest_cancel(skb, br_afspec); 4313 goto nla_put_failure; 4314 } 4315 } 4316 nla_nest_end(skb, br_afspec); 4317 4318 protinfo = nla_nest_start(skb, IFLA_PROTINFO); 4319 if (!protinfo) 4320 goto nla_put_failure; 4321 4322 if (brport_nla_put_flag(skb, flags, mask, 4323 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 4324 brport_nla_put_flag(skb, flags, mask, 4325 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 4326 brport_nla_put_flag(skb, flags, mask, 4327 IFLA_BRPORT_FAST_LEAVE, 4328 BR_MULTICAST_FAST_LEAVE) || 4329 brport_nla_put_flag(skb, flags, mask, 4330 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 4331 brport_nla_put_flag(skb, flags, mask, 4332 IFLA_BRPORT_LEARNING, BR_LEARNING) || 4333 brport_nla_put_flag(skb, flags, mask, 4334 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 4335 brport_nla_put_flag(skb, flags, mask, 4336 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 4337 brport_nla_put_flag(skb, flags, mask, 4338 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 4339 nla_nest_cancel(skb, protinfo); 4340 goto nla_put_failure; 4341 } 4342 4343 nla_nest_end(skb, protinfo); 4344 4345 nlmsg_end(skb, nlh); 4346 return 0; 4347 nla_put_failure: 4348 nlmsg_cancel(skb, nlh); 4349 return err ? err : -EMSGSIZE; 4350 } 4351 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 4352 4353 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh, 4354 bool strict_check, u32 *filter_mask, 4355 struct netlink_ext_ack *extack) 4356 { 4357 struct nlattr *tb[IFLA_MAX+1]; 4358 int err, i; 4359 4360 if (strict_check) { 4361 struct ifinfomsg *ifm; 4362 4363 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 4364 NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump"); 4365 return -EINVAL; 4366 } 4367 4368 ifm = nlmsg_data(nlh); 4369 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 4370 ifm->ifi_change || ifm->ifi_index) { 4371 NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request"); 4372 return -EINVAL; 4373 } 4374 4375 err = nlmsg_parse_deprecated_strict(nlh, 4376 sizeof(struct ifinfomsg), 4377 tb, IFLA_MAX, ifla_policy, 4378 extack); 4379 } else { 4380 err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg), 4381 tb, IFLA_MAX, ifla_policy, 4382 extack); 4383 } 4384 if (err < 0) 4385 return err; 4386 4387 /* new attributes should only be added with strict checking */ 4388 for (i = 0; i <= IFLA_MAX; ++i) { 4389 if (!tb[i]) 4390 continue; 4391 4392 switch (i) { 4393 case IFLA_EXT_MASK: 4394 *filter_mask = nla_get_u32(tb[i]); 4395 break; 4396 default: 4397 if (strict_check) { 4398 NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request"); 4399 return -EINVAL; 4400 } 4401 } 4402 } 4403 4404 return 0; 4405 } 4406 4407 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 4408 { 4409 const struct nlmsghdr *nlh = cb->nlh; 4410 struct net *net = sock_net(skb->sk); 4411 struct net_device *dev; 4412 int idx = 0; 4413 u32 portid = NETLINK_CB(cb->skb).portid; 4414 u32 seq = nlh->nlmsg_seq; 4415 u32 filter_mask = 0; 4416 int err; 4417 4418 err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask, 4419 cb->extack); 4420 if (err < 0 && cb->strict_check) 4421 return err; 4422 4423 rcu_read_lock(); 4424 for_each_netdev_rcu(net, dev) { 4425 const struct net_device_ops *ops = dev->netdev_ops; 4426 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4427 4428 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 4429 if (idx >= cb->args[0]) { 4430 err = br_dev->netdev_ops->ndo_bridge_getlink( 4431 skb, portid, seq, dev, 4432 filter_mask, NLM_F_MULTI); 4433 if (err < 0 && err != -EOPNOTSUPP) { 4434 if (likely(skb->len)) 4435 break; 4436 4437 goto out_err; 4438 } 4439 } 4440 idx++; 4441 } 4442 4443 if (ops->ndo_bridge_getlink) { 4444 if (idx >= cb->args[0]) { 4445 err = ops->ndo_bridge_getlink(skb, portid, 4446 seq, dev, 4447 filter_mask, 4448 NLM_F_MULTI); 4449 if (err < 0 && err != -EOPNOTSUPP) { 4450 if (likely(skb->len)) 4451 break; 4452 4453 goto out_err; 4454 } 4455 } 4456 idx++; 4457 } 4458 } 4459 err = skb->len; 4460 out_err: 4461 rcu_read_unlock(); 4462 cb->args[0] = idx; 4463 4464 return err; 4465 } 4466 4467 static inline size_t bridge_nlmsg_size(void) 4468 { 4469 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 4470 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 4471 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 4472 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 4473 + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 4474 + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 4475 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 4476 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 4477 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 4478 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 4479 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 4480 } 4481 4482 static int rtnl_bridge_notify(struct net_device *dev) 4483 { 4484 struct net *net = dev_net(dev); 4485 struct sk_buff *skb; 4486 int err = -EOPNOTSUPP; 4487 4488 if (!dev->netdev_ops->ndo_bridge_getlink) 4489 return 0; 4490 4491 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 4492 if (!skb) { 4493 err = -ENOMEM; 4494 goto errout; 4495 } 4496 4497 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 4498 if (err < 0) 4499 goto errout; 4500 4501 if (!skb->len) 4502 goto errout; 4503 4504 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 4505 return 0; 4506 errout: 4507 WARN_ON(err == -EMSGSIZE); 4508 kfree_skb(skb); 4509 if (err) 4510 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 4511 return err; 4512 } 4513 4514 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 4515 struct netlink_ext_ack *extack) 4516 { 4517 struct net *net = sock_net(skb->sk); 4518 struct ifinfomsg *ifm; 4519 struct net_device *dev; 4520 struct nlattr *br_spec, *attr = NULL; 4521 int rem, err = -EOPNOTSUPP; 4522 u16 flags = 0; 4523 bool have_flags = false; 4524 4525 if (nlmsg_len(nlh) < sizeof(*ifm)) 4526 return -EINVAL; 4527 4528 ifm = nlmsg_data(nlh); 4529 if (ifm->ifi_family != AF_BRIDGE) 4530 return -EPFNOSUPPORT; 4531 4532 dev = __dev_get_by_index(net, ifm->ifi_index); 4533 if (!dev) { 4534 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4535 return -ENODEV; 4536 } 4537 4538 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4539 if (br_spec) { 4540 nla_for_each_nested(attr, br_spec, rem) { 4541 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 4542 if (nla_len(attr) < sizeof(flags)) 4543 return -EINVAL; 4544 4545 have_flags = true; 4546 flags = nla_get_u16(attr); 4547 break; 4548 } 4549 } 4550 } 4551 4552 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4553 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4554 4555 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 4556 err = -EOPNOTSUPP; 4557 goto out; 4558 } 4559 4560 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags, 4561 extack); 4562 if (err) 4563 goto out; 4564 4565 flags &= ~BRIDGE_FLAGS_MASTER; 4566 } 4567 4568 if ((flags & BRIDGE_FLAGS_SELF)) { 4569 if (!dev->netdev_ops->ndo_bridge_setlink) 4570 err = -EOPNOTSUPP; 4571 else 4572 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 4573 flags, 4574 extack); 4575 if (!err) { 4576 flags &= ~BRIDGE_FLAGS_SELF; 4577 4578 /* Generate event to notify upper layer of bridge 4579 * change 4580 */ 4581 err = rtnl_bridge_notify(dev); 4582 } 4583 } 4584 4585 if (have_flags) 4586 memcpy(nla_data(attr), &flags, sizeof(flags)); 4587 out: 4588 return err; 4589 } 4590 4591 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 4592 struct netlink_ext_ack *extack) 4593 { 4594 struct net *net = sock_net(skb->sk); 4595 struct ifinfomsg *ifm; 4596 struct net_device *dev; 4597 struct nlattr *br_spec, *attr = NULL; 4598 int rem, err = -EOPNOTSUPP; 4599 u16 flags = 0; 4600 bool have_flags = false; 4601 4602 if (nlmsg_len(nlh) < sizeof(*ifm)) 4603 return -EINVAL; 4604 4605 ifm = nlmsg_data(nlh); 4606 if (ifm->ifi_family != AF_BRIDGE) 4607 return -EPFNOSUPPORT; 4608 4609 dev = __dev_get_by_index(net, ifm->ifi_index); 4610 if (!dev) { 4611 NL_SET_ERR_MSG(extack, "unknown ifindex"); 4612 return -ENODEV; 4613 } 4614 4615 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 4616 if (br_spec) { 4617 nla_for_each_nested(attr, br_spec, rem) { 4618 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 4619 if (nla_len(attr) < sizeof(flags)) 4620 return -EINVAL; 4621 4622 have_flags = true; 4623 flags = nla_get_u16(attr); 4624 break; 4625 } 4626 } 4627 } 4628 4629 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 4630 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 4631 4632 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 4633 err = -EOPNOTSUPP; 4634 goto out; 4635 } 4636 4637 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 4638 if (err) 4639 goto out; 4640 4641 flags &= ~BRIDGE_FLAGS_MASTER; 4642 } 4643 4644 if ((flags & BRIDGE_FLAGS_SELF)) { 4645 if (!dev->netdev_ops->ndo_bridge_dellink) 4646 err = -EOPNOTSUPP; 4647 else 4648 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 4649 flags); 4650 4651 if (!err) { 4652 flags &= ~BRIDGE_FLAGS_SELF; 4653 4654 /* Generate event to notify upper layer of bridge 4655 * change 4656 */ 4657 err = rtnl_bridge_notify(dev); 4658 } 4659 } 4660 4661 if (have_flags) 4662 memcpy(nla_data(attr), &flags, sizeof(flags)); 4663 out: 4664 return err; 4665 } 4666 4667 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 4668 { 4669 return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 4670 (!idxattr || idxattr == attrid); 4671 } 4672 4673 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 4674 static int rtnl_get_offload_stats_attr_size(int attr_id) 4675 { 4676 switch (attr_id) { 4677 case IFLA_OFFLOAD_XSTATS_CPU_HIT: 4678 return sizeof(struct rtnl_link_stats64); 4679 } 4680 4681 return 0; 4682 } 4683 4684 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 4685 int *prividx) 4686 { 4687 struct nlattr *attr = NULL; 4688 int attr_id, size; 4689 void *attr_data; 4690 int err; 4691 4692 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 4693 dev->netdev_ops->ndo_get_offload_stats)) 4694 return -ENODATA; 4695 4696 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 4697 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 4698 if (attr_id < *prividx) 4699 continue; 4700 4701 size = rtnl_get_offload_stats_attr_size(attr_id); 4702 if (!size) 4703 continue; 4704 4705 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 4706 continue; 4707 4708 attr = nla_reserve_64bit(skb, attr_id, size, 4709 IFLA_OFFLOAD_XSTATS_UNSPEC); 4710 if (!attr) 4711 goto nla_put_failure; 4712 4713 attr_data = nla_data(attr); 4714 memset(attr_data, 0, size); 4715 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 4716 attr_data); 4717 if (err) 4718 goto get_offload_stats_failure; 4719 } 4720 4721 if (!attr) 4722 return -ENODATA; 4723 4724 *prividx = 0; 4725 return 0; 4726 4727 nla_put_failure: 4728 err = -EMSGSIZE; 4729 get_offload_stats_failure: 4730 *prividx = attr_id; 4731 return err; 4732 } 4733 4734 static int rtnl_get_offload_stats_size(const struct net_device *dev) 4735 { 4736 int nla_size = 0; 4737 int attr_id; 4738 int size; 4739 4740 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 4741 dev->netdev_ops->ndo_get_offload_stats)) 4742 return 0; 4743 4744 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 4745 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 4746 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 4747 continue; 4748 size = rtnl_get_offload_stats_attr_size(attr_id); 4749 nla_size += nla_total_size_64bit(size); 4750 } 4751 4752 if (nla_size != 0) 4753 nla_size += nla_total_size(0); 4754 4755 return nla_size; 4756 } 4757 4758 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 4759 int type, u32 pid, u32 seq, u32 change, 4760 unsigned int flags, unsigned int filter_mask, 4761 int *idxattr, int *prividx) 4762 { 4763 struct if_stats_msg *ifsm; 4764 struct nlmsghdr *nlh; 4765 struct nlattr *attr; 4766 int s_prividx = *prividx; 4767 int err; 4768 4769 ASSERT_RTNL(); 4770 4771 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 4772 if (!nlh) 4773 return -EMSGSIZE; 4774 4775 ifsm = nlmsg_data(nlh); 4776 ifsm->family = PF_UNSPEC; 4777 ifsm->pad1 = 0; 4778 ifsm->pad2 = 0; 4779 ifsm->ifindex = dev->ifindex; 4780 ifsm->filter_mask = filter_mask; 4781 4782 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 4783 struct rtnl_link_stats64 *sp; 4784 4785 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 4786 sizeof(struct rtnl_link_stats64), 4787 IFLA_STATS_UNSPEC); 4788 if (!attr) 4789 goto nla_put_failure; 4790 4791 sp = nla_data(attr); 4792 dev_get_stats(dev, sp); 4793 } 4794 4795 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 4796 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 4797 4798 if (ops && ops->fill_linkxstats) { 4799 *idxattr = IFLA_STATS_LINK_XSTATS; 4800 attr = nla_nest_start_noflag(skb, 4801 IFLA_STATS_LINK_XSTATS); 4802 if (!attr) 4803 goto nla_put_failure; 4804 4805 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 4806 nla_nest_end(skb, attr); 4807 if (err) 4808 goto nla_put_failure; 4809 *idxattr = 0; 4810 } 4811 } 4812 4813 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 4814 *idxattr)) { 4815 const struct rtnl_link_ops *ops = NULL; 4816 const struct net_device *master; 4817 4818 master = netdev_master_upper_dev_get(dev); 4819 if (master) 4820 ops = master->rtnl_link_ops; 4821 if (ops && ops->fill_linkxstats) { 4822 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 4823 attr = nla_nest_start_noflag(skb, 4824 IFLA_STATS_LINK_XSTATS_SLAVE); 4825 if (!attr) 4826 goto nla_put_failure; 4827 4828 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 4829 nla_nest_end(skb, attr); 4830 if (err) 4831 goto nla_put_failure; 4832 *idxattr = 0; 4833 } 4834 } 4835 4836 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 4837 *idxattr)) { 4838 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 4839 attr = nla_nest_start_noflag(skb, 4840 IFLA_STATS_LINK_OFFLOAD_XSTATS); 4841 if (!attr) 4842 goto nla_put_failure; 4843 4844 err = rtnl_get_offload_stats(skb, dev, prividx); 4845 if (err == -ENODATA) 4846 nla_nest_cancel(skb, attr); 4847 else 4848 nla_nest_end(skb, attr); 4849 4850 if (err && err != -ENODATA) 4851 goto nla_put_failure; 4852 *idxattr = 0; 4853 } 4854 4855 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 4856 struct rtnl_af_ops *af_ops; 4857 4858 *idxattr = IFLA_STATS_AF_SPEC; 4859 attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC); 4860 if (!attr) 4861 goto nla_put_failure; 4862 4863 rcu_read_lock(); 4864 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4865 if (af_ops->fill_stats_af) { 4866 struct nlattr *af; 4867 int err; 4868 4869 af = nla_nest_start_noflag(skb, 4870 af_ops->family); 4871 if (!af) { 4872 rcu_read_unlock(); 4873 goto nla_put_failure; 4874 } 4875 err = af_ops->fill_stats_af(skb, dev); 4876 4877 if (err == -ENODATA) { 4878 nla_nest_cancel(skb, af); 4879 } else if (err < 0) { 4880 rcu_read_unlock(); 4881 goto nla_put_failure; 4882 } 4883 4884 nla_nest_end(skb, af); 4885 } 4886 } 4887 rcu_read_unlock(); 4888 4889 nla_nest_end(skb, attr); 4890 4891 *idxattr = 0; 4892 } 4893 4894 nlmsg_end(skb, nlh); 4895 4896 return 0; 4897 4898 nla_put_failure: 4899 /* not a multi message or no progress mean a real error */ 4900 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 4901 nlmsg_cancel(skb, nlh); 4902 else 4903 nlmsg_end(skb, nlh); 4904 4905 return -EMSGSIZE; 4906 } 4907 4908 static size_t if_nlmsg_stats_size(const struct net_device *dev, 4909 u32 filter_mask) 4910 { 4911 size_t size = 0; 4912 4913 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 4914 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 4915 4916 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 4917 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 4918 int attr = IFLA_STATS_LINK_XSTATS; 4919 4920 if (ops && ops->get_linkxstats_size) { 4921 size += nla_total_size(ops->get_linkxstats_size(dev, 4922 attr)); 4923 /* for IFLA_STATS_LINK_XSTATS */ 4924 size += nla_total_size(0); 4925 } 4926 } 4927 4928 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 4929 struct net_device *_dev = (struct net_device *)dev; 4930 const struct rtnl_link_ops *ops = NULL; 4931 const struct net_device *master; 4932 4933 /* netdev_master_upper_dev_get can't take const */ 4934 master = netdev_master_upper_dev_get(_dev); 4935 if (master) 4936 ops = master->rtnl_link_ops; 4937 if (ops && ops->get_linkxstats_size) { 4938 int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 4939 4940 size += nla_total_size(ops->get_linkxstats_size(dev, 4941 attr)); 4942 /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 4943 size += nla_total_size(0); 4944 } 4945 } 4946 4947 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 4948 size += rtnl_get_offload_stats_size(dev); 4949 4950 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4951 struct rtnl_af_ops *af_ops; 4952 4953 /* for IFLA_STATS_AF_SPEC */ 4954 size += nla_total_size(0); 4955 4956 rcu_read_lock(); 4957 list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) { 4958 if (af_ops->get_stats_af_size) { 4959 size += nla_total_size( 4960 af_ops->get_stats_af_size(dev)); 4961 4962 /* for AF_* */ 4963 size += nla_total_size(0); 4964 } 4965 } 4966 rcu_read_unlock(); 4967 } 4968 4969 return size; 4970 } 4971 4972 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check, 4973 bool is_dump, struct netlink_ext_ack *extack) 4974 { 4975 struct if_stats_msg *ifsm; 4976 4977 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) { 4978 NL_SET_ERR_MSG(extack, "Invalid header for stats dump"); 4979 return -EINVAL; 4980 } 4981 4982 if (!strict_check) 4983 return 0; 4984 4985 ifsm = nlmsg_data(nlh); 4986 4987 /* only requests using strict checks can pass data to influence 4988 * the dump. The legacy exception is filter_mask. 4989 */ 4990 if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) { 4991 NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request"); 4992 return -EINVAL; 4993 } 4994 if (nlmsg_attrlen(nlh, sizeof(*ifsm))) { 4995 NL_SET_ERR_MSG(extack, "Invalid attributes after stats header"); 4996 return -EINVAL; 4997 } 4998 if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) { 4999 NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask"); 5000 return -EINVAL; 5001 } 5002 5003 return 0; 5004 } 5005 5006 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 5007 struct netlink_ext_ack *extack) 5008 { 5009 struct net *net = sock_net(skb->sk); 5010 struct net_device *dev = NULL; 5011 int idxattr = 0, prividx = 0; 5012 struct if_stats_msg *ifsm; 5013 struct sk_buff *nskb; 5014 u32 filter_mask; 5015 int err; 5016 5017 err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb), 5018 false, extack); 5019 if (err) 5020 return err; 5021 5022 ifsm = nlmsg_data(nlh); 5023 if (ifsm->ifindex > 0) 5024 dev = __dev_get_by_index(net, ifsm->ifindex); 5025 else 5026 return -EINVAL; 5027 5028 if (!dev) 5029 return -ENODEV; 5030 5031 filter_mask = ifsm->filter_mask; 5032 if (!filter_mask) 5033 return -EINVAL; 5034 5035 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 5036 if (!nskb) 5037 return -ENOBUFS; 5038 5039 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 5040 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 5041 0, filter_mask, &idxattr, &prividx); 5042 if (err < 0) { 5043 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 5044 WARN_ON(err == -EMSGSIZE); 5045 kfree_skb(nskb); 5046 } else { 5047 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 5048 } 5049 5050 return err; 5051 } 5052 5053 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 5054 { 5055 struct netlink_ext_ack *extack = cb->extack; 5056 int h, s_h, err, s_idx, s_idxattr, s_prividx; 5057 struct net *net = sock_net(skb->sk); 5058 unsigned int flags = NLM_F_MULTI; 5059 struct if_stats_msg *ifsm; 5060 struct hlist_head *head; 5061 struct net_device *dev; 5062 u32 filter_mask = 0; 5063 int idx = 0; 5064 5065 s_h = cb->args[0]; 5066 s_idx = cb->args[1]; 5067 s_idxattr = cb->args[2]; 5068 s_prividx = cb->args[3]; 5069 5070 cb->seq = net->dev_base_seq; 5071 5072 err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack); 5073 if (err) 5074 return err; 5075 5076 ifsm = nlmsg_data(cb->nlh); 5077 filter_mask = ifsm->filter_mask; 5078 if (!filter_mask) { 5079 NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump"); 5080 return -EINVAL; 5081 } 5082 5083 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 5084 idx = 0; 5085 head = &net->dev_index_head[h]; 5086 hlist_for_each_entry(dev, head, index_hlist) { 5087 if (idx < s_idx) 5088 goto cont; 5089 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 5090 NETLINK_CB(cb->skb).portid, 5091 cb->nlh->nlmsg_seq, 0, 5092 flags, filter_mask, 5093 &s_idxattr, &s_prividx); 5094 /* If we ran out of room on the first message, 5095 * we're in trouble 5096 */ 5097 WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 5098 5099 if (err < 0) 5100 goto out; 5101 s_prividx = 0; 5102 s_idxattr = 0; 5103 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 5104 cont: 5105 idx++; 5106 } 5107 } 5108 out: 5109 cb->args[3] = s_prividx; 5110 cb->args[2] = s_idxattr; 5111 cb->args[1] = idx; 5112 cb->args[0] = h; 5113 5114 return skb->len; 5115 } 5116 5117 /* Process one rtnetlink message. */ 5118 5119 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 5120 struct netlink_ext_ack *extack) 5121 { 5122 struct net *net = sock_net(skb->sk); 5123 struct rtnl_link *link; 5124 struct module *owner; 5125 int err = -EOPNOTSUPP; 5126 rtnl_doit_func doit; 5127 unsigned int flags; 5128 int kind; 5129 int family; 5130 int type; 5131 5132 type = nlh->nlmsg_type; 5133 if (type > RTM_MAX) 5134 return -EOPNOTSUPP; 5135 5136 type -= RTM_BASE; 5137 5138 /* All the messages must have at least 1 byte length */ 5139 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 5140 return 0; 5141 5142 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 5143 kind = type&3; 5144 5145 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 5146 return -EPERM; 5147 5148 rcu_read_lock(); 5149 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 5150 struct sock *rtnl; 5151 rtnl_dumpit_func dumpit; 5152 u16 min_dump_alloc = 0; 5153 5154 link = rtnl_get_link(family, type); 5155 if (!link || !link->dumpit) { 5156 family = PF_UNSPEC; 5157 link = rtnl_get_link(family, type); 5158 if (!link || !link->dumpit) 5159 goto err_unlock; 5160 } 5161 owner = link->owner; 5162 dumpit = link->dumpit; 5163 5164 if (type == RTM_GETLINK - RTM_BASE) 5165 min_dump_alloc = rtnl_calcit(skb, nlh); 5166 5167 err = 0; 5168 /* need to do this before rcu_read_unlock() */ 5169 if (!try_module_get(owner)) 5170 err = -EPROTONOSUPPORT; 5171 5172 rcu_read_unlock(); 5173 5174 rtnl = net->rtnl; 5175 if (err == 0) { 5176 struct netlink_dump_control c = { 5177 .dump = dumpit, 5178 .min_dump_alloc = min_dump_alloc, 5179 .module = owner, 5180 }; 5181 err = netlink_dump_start(rtnl, skb, nlh, &c); 5182 /* netlink_dump_start() will keep a reference on 5183 * module if dump is still in progress. 5184 */ 5185 module_put(owner); 5186 } 5187 return err; 5188 } 5189 5190 link = rtnl_get_link(family, type); 5191 if (!link || !link->doit) { 5192 family = PF_UNSPEC; 5193 link = rtnl_get_link(PF_UNSPEC, type); 5194 if (!link || !link->doit) 5195 goto out_unlock; 5196 } 5197 5198 owner = link->owner; 5199 if (!try_module_get(owner)) { 5200 err = -EPROTONOSUPPORT; 5201 goto out_unlock; 5202 } 5203 5204 flags = link->flags; 5205 if (flags & RTNL_FLAG_DOIT_UNLOCKED) { 5206 doit = link->doit; 5207 rcu_read_unlock(); 5208 if (doit) 5209 err = doit(skb, nlh, extack); 5210 module_put(owner); 5211 return err; 5212 } 5213 rcu_read_unlock(); 5214 5215 rtnl_lock(); 5216 link = rtnl_get_link(family, type); 5217 if (link && link->doit) 5218 err = link->doit(skb, nlh, extack); 5219 rtnl_unlock(); 5220 5221 module_put(owner); 5222 5223 return err; 5224 5225 out_unlock: 5226 rcu_read_unlock(); 5227 return err; 5228 5229 err_unlock: 5230 rcu_read_unlock(); 5231 return -EOPNOTSUPP; 5232 } 5233 5234 static void rtnetlink_rcv(struct sk_buff *skb) 5235 { 5236 netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 5237 } 5238 5239 static int rtnetlink_bind(struct net *net, int group) 5240 { 5241 switch (group) { 5242 case RTNLGRP_IPV4_MROUTE_R: 5243 case RTNLGRP_IPV6_MROUTE_R: 5244 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 5245 return -EPERM; 5246 break; 5247 } 5248 return 0; 5249 } 5250 5251 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 5252 { 5253 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 5254 5255 switch (event) { 5256 case NETDEV_REBOOT: 5257 case NETDEV_CHANGEMTU: 5258 case NETDEV_CHANGEADDR: 5259 case NETDEV_CHANGENAME: 5260 case NETDEV_FEAT_CHANGE: 5261 case NETDEV_BONDING_FAILOVER: 5262 case NETDEV_POST_TYPE_CHANGE: 5263 case NETDEV_NOTIFY_PEERS: 5264 case NETDEV_CHANGEUPPER: 5265 case NETDEV_RESEND_IGMP: 5266 case NETDEV_CHANGEINFODATA: 5267 case NETDEV_CHANGELOWERSTATE: 5268 case NETDEV_CHANGE_TX_QUEUE_LEN: 5269 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 5270 GFP_KERNEL, NULL, 0); 5271 break; 5272 default: 5273 break; 5274 } 5275 return NOTIFY_DONE; 5276 } 5277 5278 static struct notifier_block rtnetlink_dev_notifier = { 5279 .notifier_call = rtnetlink_event, 5280 }; 5281 5282 5283 static int __net_init rtnetlink_net_init(struct net *net) 5284 { 5285 struct sock *sk; 5286 struct netlink_kernel_cfg cfg = { 5287 .groups = RTNLGRP_MAX, 5288 .input = rtnetlink_rcv, 5289 .cb_mutex = &rtnl_mutex, 5290 .flags = NL_CFG_F_NONROOT_RECV, 5291 .bind = rtnetlink_bind, 5292 }; 5293 5294 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 5295 if (!sk) 5296 return -ENOMEM; 5297 net->rtnl = sk; 5298 return 0; 5299 } 5300 5301 static void __net_exit rtnetlink_net_exit(struct net *net) 5302 { 5303 netlink_kernel_release(net->rtnl); 5304 net->rtnl = NULL; 5305 } 5306 5307 static struct pernet_operations rtnetlink_net_ops = { 5308 .init = rtnetlink_net_init, 5309 .exit = rtnetlink_net_exit, 5310 }; 5311 5312 void __init rtnetlink_init(void) 5313 { 5314 if (register_pernet_subsys(&rtnetlink_net_ops)) 5315 panic("rtnetlink_init: cannot initialize rtnetlink\n"); 5316 5317 register_netdevice_notifier(&rtnetlink_dev_notifier); 5318 5319 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 5320 rtnl_dump_ifinfo, 0); 5321 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0); 5322 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0); 5323 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0); 5324 5325 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0); 5326 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); 5327 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); 5328 5329 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); 5330 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); 5331 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); 5332 5333 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); 5334 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); 5335 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0); 5336 5337 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 5338 0); 5339 } 5340