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