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