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