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