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