1 /* 2 * INET An implementation of the TCP/IP protocol suite for the LINUX 3 * operating system. INET is implemented using the BSD Socket 4 * interface as the means of communication with the user level. 5 * 6 * Routing netlink socket interface: protocol independent part. 7 * 8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 13 * 2 of the License, or (at your option) any later version. 14 * 15 * Fixes: 16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong. 17 */ 18 19 #include <linux/errno.h> 20 #include <linux/module.h> 21 #include <linux/types.h> 22 #include <linux/socket.h> 23 #include <linux/kernel.h> 24 #include <linux/timer.h> 25 #include <linux/string.h> 26 #include <linux/sockios.h> 27 #include <linux/net.h> 28 #include <linux/fcntl.h> 29 #include <linux/mm.h> 30 #include <linux/slab.h> 31 #include <linux/interrupt.h> 32 #include <linux/capability.h> 33 #include <linux/skbuff.h> 34 #include <linux/init.h> 35 #include <linux/security.h> 36 #include <linux/mutex.h> 37 #include <linux/if_addr.h> 38 #include <linux/if_bridge.h> 39 #include <linux/if_vlan.h> 40 #include <linux/pci.h> 41 #include <linux/etherdevice.h> 42 #include <linux/bpf.h> 43 44 #include <linux/uaccess.h> 45 46 #include <linux/inet.h> 47 #include <linux/netdevice.h> 48 #include <net/switchdev.h> 49 #include <net/ip.h> 50 #include <net/protocol.h> 51 #include <net/arp.h> 52 #include <net/route.h> 53 #include <net/udp.h> 54 #include <net/tcp.h> 55 #include <net/sock.h> 56 #include <net/pkt_sched.h> 57 #include <net/fib_rules.h> 58 #include <net/rtnetlink.h> 59 #include <net/net_namespace.h> 60 61 struct rtnl_link { 62 rtnl_doit_func doit; 63 rtnl_dumpit_func dumpit; 64 rtnl_calcit_func calcit; 65 }; 66 67 static DEFINE_MUTEX(rtnl_mutex); 68 69 void rtnl_lock(void) 70 { 71 mutex_lock(&rtnl_mutex); 72 } 73 EXPORT_SYMBOL(rtnl_lock); 74 75 static struct sk_buff *defer_kfree_skb_list; 76 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail) 77 { 78 if (head && tail) { 79 tail->next = defer_kfree_skb_list; 80 defer_kfree_skb_list = head; 81 } 82 } 83 EXPORT_SYMBOL(rtnl_kfree_skbs); 84 85 void __rtnl_unlock(void) 86 { 87 struct sk_buff *head = defer_kfree_skb_list; 88 89 defer_kfree_skb_list = NULL; 90 91 mutex_unlock(&rtnl_mutex); 92 93 while (head) { 94 struct sk_buff *next = head->next; 95 96 kfree_skb(head); 97 cond_resched(); 98 head = next; 99 } 100 } 101 102 void rtnl_unlock(void) 103 { 104 /* This fellow will unlock it for us. */ 105 netdev_run_todo(); 106 } 107 EXPORT_SYMBOL(rtnl_unlock); 108 109 int rtnl_trylock(void) 110 { 111 return mutex_trylock(&rtnl_mutex); 112 } 113 EXPORT_SYMBOL(rtnl_trylock); 114 115 int rtnl_is_locked(void) 116 { 117 return mutex_is_locked(&rtnl_mutex); 118 } 119 EXPORT_SYMBOL(rtnl_is_locked); 120 121 #ifdef CONFIG_PROVE_LOCKING 122 bool lockdep_rtnl_is_held(void) 123 { 124 return lockdep_is_held(&rtnl_mutex); 125 } 126 EXPORT_SYMBOL(lockdep_rtnl_is_held); 127 #endif /* #ifdef CONFIG_PROVE_LOCKING */ 128 129 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 130 131 static inline int rtm_msgindex(int msgtype) 132 { 133 int msgindex = msgtype - RTM_BASE; 134 135 /* 136 * msgindex < 0 implies someone tried to register a netlink 137 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 138 * the message type has not been added to linux/rtnetlink.h 139 */ 140 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 141 142 return msgindex; 143 } 144 145 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) 146 { 147 struct rtnl_link *tab; 148 149 if (protocol <= RTNL_FAMILY_MAX) 150 tab = rtnl_msg_handlers[protocol]; 151 else 152 tab = NULL; 153 154 if (tab == NULL || tab[msgindex].doit == NULL) 155 tab = rtnl_msg_handlers[PF_UNSPEC]; 156 157 return tab[msgindex].doit; 158 } 159 160 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) 161 { 162 struct rtnl_link *tab; 163 164 if (protocol <= RTNL_FAMILY_MAX) 165 tab = rtnl_msg_handlers[protocol]; 166 else 167 tab = NULL; 168 169 if (tab == NULL || tab[msgindex].dumpit == NULL) 170 tab = rtnl_msg_handlers[PF_UNSPEC]; 171 172 return tab[msgindex].dumpit; 173 } 174 175 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex) 176 { 177 struct rtnl_link *tab; 178 179 if (protocol <= RTNL_FAMILY_MAX) 180 tab = rtnl_msg_handlers[protocol]; 181 else 182 tab = NULL; 183 184 if (tab == NULL || tab[msgindex].calcit == NULL) 185 tab = rtnl_msg_handlers[PF_UNSPEC]; 186 187 return tab[msgindex].calcit; 188 } 189 190 /** 191 * __rtnl_register - Register a rtnetlink message type 192 * @protocol: Protocol family or PF_UNSPEC 193 * @msgtype: rtnetlink message type 194 * @doit: Function pointer called for each request message 195 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 196 * @calcit: Function pointer to calc size of dump message 197 * 198 * Registers the specified function pointers (at least one of them has 199 * to be non-NULL) to be called whenever a request message for the 200 * specified protocol family and message type is received. 201 * 202 * The special protocol family PF_UNSPEC may be used to define fallback 203 * function pointers for the case when no entry for the specific protocol 204 * family exists. 205 * 206 * Returns 0 on success or a negative error code. 207 */ 208 int __rtnl_register(int protocol, int msgtype, 209 rtnl_doit_func doit, rtnl_dumpit_func dumpit, 210 rtnl_calcit_func calcit) 211 { 212 struct rtnl_link *tab; 213 int msgindex; 214 215 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 216 msgindex = rtm_msgindex(msgtype); 217 218 tab = rtnl_msg_handlers[protocol]; 219 if (tab == NULL) { 220 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); 221 if (tab == NULL) 222 return -ENOBUFS; 223 224 rtnl_msg_handlers[protocol] = tab; 225 } 226 227 if (doit) 228 tab[msgindex].doit = doit; 229 230 if (dumpit) 231 tab[msgindex].dumpit = dumpit; 232 233 if (calcit) 234 tab[msgindex].calcit = calcit; 235 236 return 0; 237 } 238 EXPORT_SYMBOL_GPL(__rtnl_register); 239 240 /** 241 * rtnl_register - Register a rtnetlink message type 242 * 243 * Identical to __rtnl_register() but panics on failure. This is useful 244 * as failure of this function is very unlikely, it can only happen due 245 * to lack of memory when allocating the chain to store all message 246 * handlers for a protocol. Meant for use in init functions where lack 247 * of memory implies no sense in continuing. 248 */ 249 void rtnl_register(int protocol, int msgtype, 250 rtnl_doit_func doit, rtnl_dumpit_func dumpit, 251 rtnl_calcit_func calcit) 252 { 253 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0) 254 panic("Unable to register rtnetlink message handler, " 255 "protocol = %d, message type = %d\n", 256 protocol, msgtype); 257 } 258 EXPORT_SYMBOL_GPL(rtnl_register); 259 260 /** 261 * rtnl_unregister - Unregister a rtnetlink message type 262 * @protocol: Protocol family or PF_UNSPEC 263 * @msgtype: rtnetlink message type 264 * 265 * Returns 0 on success or a negative error code. 266 */ 267 int rtnl_unregister(int protocol, int msgtype) 268 { 269 int msgindex; 270 271 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 272 msgindex = rtm_msgindex(msgtype); 273 274 if (rtnl_msg_handlers[protocol] == NULL) 275 return -ENOENT; 276 277 rtnl_msg_handlers[protocol][msgindex].doit = NULL; 278 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; 279 rtnl_msg_handlers[protocol][msgindex].calcit = NULL; 280 281 return 0; 282 } 283 EXPORT_SYMBOL_GPL(rtnl_unregister); 284 285 /** 286 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 287 * @protocol : Protocol family or PF_UNSPEC 288 * 289 * Identical to calling rtnl_unregster() for all registered message types 290 * of a certain protocol family. 291 */ 292 void rtnl_unregister_all(int protocol) 293 { 294 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 295 296 kfree(rtnl_msg_handlers[protocol]); 297 rtnl_msg_handlers[protocol] = NULL; 298 } 299 EXPORT_SYMBOL_GPL(rtnl_unregister_all); 300 301 static LIST_HEAD(link_ops); 302 303 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 304 { 305 const struct rtnl_link_ops *ops; 306 307 list_for_each_entry(ops, &link_ops, list) { 308 if (!strcmp(ops->kind, kind)) 309 return ops; 310 } 311 return NULL; 312 } 313 314 /** 315 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 316 * @ops: struct rtnl_link_ops * to register 317 * 318 * The caller must hold the rtnl_mutex. This function should be used 319 * by drivers that create devices during module initialization. It 320 * must be called before registering the devices. 321 * 322 * Returns 0 on success or a negative error code. 323 */ 324 int __rtnl_link_register(struct rtnl_link_ops *ops) 325 { 326 if (rtnl_link_ops_get(ops->kind)) 327 return -EEXIST; 328 329 /* The check for setup is here because if ops 330 * does not have that filled up, it is not possible 331 * to use the ops for creating device. So do not 332 * fill up dellink as well. That disables rtnl_dellink. 333 */ 334 if (ops->setup && !ops->dellink) 335 ops->dellink = unregister_netdevice_queue; 336 337 list_add_tail(&ops->list, &link_ops); 338 return 0; 339 } 340 EXPORT_SYMBOL_GPL(__rtnl_link_register); 341 342 /** 343 * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 344 * @ops: struct rtnl_link_ops * to register 345 * 346 * Returns 0 on success or a negative error code. 347 */ 348 int rtnl_link_register(struct rtnl_link_ops *ops) 349 { 350 int err; 351 352 rtnl_lock(); 353 err = __rtnl_link_register(ops); 354 rtnl_unlock(); 355 return err; 356 } 357 EXPORT_SYMBOL_GPL(rtnl_link_register); 358 359 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 360 { 361 struct net_device *dev; 362 LIST_HEAD(list_kill); 363 364 for_each_netdev(net, dev) { 365 if (dev->rtnl_link_ops == ops) 366 ops->dellink(dev, &list_kill); 367 } 368 unregister_netdevice_many(&list_kill); 369 } 370 371 /** 372 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 373 * @ops: struct rtnl_link_ops * to unregister 374 * 375 * The caller must hold the rtnl_mutex. 376 */ 377 void __rtnl_link_unregister(struct rtnl_link_ops *ops) 378 { 379 struct net *net; 380 381 for_each_net(net) { 382 __rtnl_kill_links(net, ops); 383 } 384 list_del(&ops->list); 385 } 386 EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 387 388 /* Return with the rtnl_lock held when there are no network 389 * devices unregistering in any network namespace. 390 */ 391 static void rtnl_lock_unregistering_all(void) 392 { 393 struct net *net; 394 bool unregistering; 395 DEFINE_WAIT_FUNC(wait, woken_wake_function); 396 397 add_wait_queue(&netdev_unregistering_wq, &wait); 398 for (;;) { 399 unregistering = false; 400 rtnl_lock(); 401 for_each_net(net) { 402 if (net->dev_unreg_count > 0) { 403 unregistering = true; 404 break; 405 } 406 } 407 if (!unregistering) 408 break; 409 __rtnl_unlock(); 410 411 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); 412 } 413 remove_wait_queue(&netdev_unregistering_wq, &wait); 414 } 415 416 /** 417 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 418 * @ops: struct rtnl_link_ops * to unregister 419 */ 420 void rtnl_link_unregister(struct rtnl_link_ops *ops) 421 { 422 /* Close the race with cleanup_net() */ 423 mutex_lock(&net_mutex); 424 rtnl_lock_unregistering_all(); 425 __rtnl_link_unregister(ops); 426 rtnl_unlock(); 427 mutex_unlock(&net_mutex); 428 } 429 EXPORT_SYMBOL_GPL(rtnl_link_unregister); 430 431 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev) 432 { 433 struct net_device *master_dev; 434 const struct rtnl_link_ops *ops; 435 436 master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 437 if (!master_dev) 438 return 0; 439 ops = master_dev->rtnl_link_ops; 440 if (!ops || !ops->get_slave_size) 441 return 0; 442 /* IFLA_INFO_SLAVE_DATA + nested data */ 443 return nla_total_size(sizeof(struct nlattr)) + 444 ops->get_slave_size(master_dev, dev); 445 } 446 447 static size_t rtnl_link_get_size(const struct net_device *dev) 448 { 449 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 450 size_t size; 451 452 if (!ops) 453 return 0; 454 455 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 456 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 457 458 if (ops->get_size) 459 /* IFLA_INFO_DATA + nested data */ 460 size += nla_total_size(sizeof(struct nlattr)) + 461 ops->get_size(dev); 462 463 if (ops->get_xstats_size) 464 /* IFLA_INFO_XSTATS */ 465 size += nla_total_size(ops->get_xstats_size(dev)); 466 467 size += rtnl_link_get_slave_info_data_size(dev); 468 469 return size; 470 } 471 472 static LIST_HEAD(rtnl_af_ops); 473 474 static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 475 { 476 const struct rtnl_af_ops *ops; 477 478 list_for_each_entry(ops, &rtnl_af_ops, list) { 479 if (ops->family == family) 480 return ops; 481 } 482 483 return NULL; 484 } 485 486 /** 487 * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 488 * @ops: struct rtnl_af_ops * to register 489 * 490 * Returns 0 on success or a negative error code. 491 */ 492 void rtnl_af_register(struct rtnl_af_ops *ops) 493 { 494 rtnl_lock(); 495 list_add_tail(&ops->list, &rtnl_af_ops); 496 rtnl_unlock(); 497 } 498 EXPORT_SYMBOL_GPL(rtnl_af_register); 499 500 /** 501 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 502 * @ops: struct rtnl_af_ops * to unregister 503 * 504 * The caller must hold the rtnl_mutex. 505 */ 506 void __rtnl_af_unregister(struct rtnl_af_ops *ops) 507 { 508 list_del(&ops->list); 509 } 510 EXPORT_SYMBOL_GPL(__rtnl_af_unregister); 511 512 /** 513 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 514 * @ops: struct rtnl_af_ops * to unregister 515 */ 516 void rtnl_af_unregister(struct rtnl_af_ops *ops) 517 { 518 rtnl_lock(); 519 __rtnl_af_unregister(ops); 520 rtnl_unlock(); 521 } 522 EXPORT_SYMBOL_GPL(rtnl_af_unregister); 523 524 static size_t rtnl_link_get_af_size(const struct net_device *dev, 525 u32 ext_filter_mask) 526 { 527 struct rtnl_af_ops *af_ops; 528 size_t size; 529 530 /* IFLA_AF_SPEC */ 531 size = nla_total_size(sizeof(struct nlattr)); 532 533 list_for_each_entry(af_ops, &rtnl_af_ops, list) { 534 if (af_ops->get_link_af_size) { 535 /* AF_* + nested data */ 536 size += nla_total_size(sizeof(struct nlattr)) + 537 af_ops->get_link_af_size(dev, ext_filter_mask); 538 } 539 } 540 541 return size; 542 } 543 544 static bool rtnl_have_link_slave_info(const struct net_device *dev) 545 { 546 struct net_device *master_dev; 547 548 master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 549 if (master_dev && master_dev->rtnl_link_ops) 550 return true; 551 return false; 552 } 553 554 static int rtnl_link_slave_info_fill(struct sk_buff *skb, 555 const struct net_device *dev) 556 { 557 struct net_device *master_dev; 558 const struct rtnl_link_ops *ops; 559 struct nlattr *slave_data; 560 int err; 561 562 master_dev = netdev_master_upper_dev_get((struct net_device *) dev); 563 if (!master_dev) 564 return 0; 565 ops = master_dev->rtnl_link_ops; 566 if (!ops) 567 return 0; 568 if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0) 569 return -EMSGSIZE; 570 if (ops->fill_slave_info) { 571 slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA); 572 if (!slave_data) 573 return -EMSGSIZE; 574 err = ops->fill_slave_info(skb, master_dev, dev); 575 if (err < 0) 576 goto err_cancel_slave_data; 577 nla_nest_end(skb, slave_data); 578 } 579 return 0; 580 581 err_cancel_slave_data: 582 nla_nest_cancel(skb, slave_data); 583 return err; 584 } 585 586 static int rtnl_link_info_fill(struct sk_buff *skb, 587 const struct net_device *dev) 588 { 589 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 590 struct nlattr *data; 591 int err; 592 593 if (!ops) 594 return 0; 595 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 596 return -EMSGSIZE; 597 if (ops->fill_xstats) { 598 err = ops->fill_xstats(skb, dev); 599 if (err < 0) 600 return err; 601 } 602 if (ops->fill_info) { 603 data = nla_nest_start(skb, IFLA_INFO_DATA); 604 if (data == NULL) 605 return -EMSGSIZE; 606 err = ops->fill_info(skb, dev); 607 if (err < 0) 608 goto err_cancel_data; 609 nla_nest_end(skb, data); 610 } 611 return 0; 612 613 err_cancel_data: 614 nla_nest_cancel(skb, data); 615 return err; 616 } 617 618 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 619 { 620 struct nlattr *linkinfo; 621 int err = -EMSGSIZE; 622 623 linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 624 if (linkinfo == NULL) 625 goto out; 626 627 err = rtnl_link_info_fill(skb, dev); 628 if (err < 0) 629 goto err_cancel_link; 630 631 err = rtnl_link_slave_info_fill(skb, dev); 632 if (err < 0) 633 goto err_cancel_link; 634 635 nla_nest_end(skb, linkinfo); 636 return 0; 637 638 err_cancel_link: 639 nla_nest_cancel(skb, linkinfo); 640 out: 641 return err; 642 } 643 644 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo) 645 { 646 struct sock *rtnl = net->rtnl; 647 int err = 0; 648 649 NETLINK_CB(skb).dst_group = group; 650 if (echo) 651 atomic_inc(&skb->users); 652 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 653 if (echo) 654 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 655 return err; 656 } 657 658 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 659 { 660 struct sock *rtnl = net->rtnl; 661 662 return nlmsg_unicast(rtnl, skb, pid); 663 } 664 EXPORT_SYMBOL(rtnl_unicast); 665 666 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 667 struct nlmsghdr *nlh, gfp_t flags) 668 { 669 struct sock *rtnl = net->rtnl; 670 int report = 0; 671 672 if (nlh) 673 report = nlmsg_report(nlh); 674 675 nlmsg_notify(rtnl, skb, pid, group, report, flags); 676 } 677 EXPORT_SYMBOL(rtnl_notify); 678 679 void rtnl_set_sk_err(struct net *net, u32 group, int error) 680 { 681 struct sock *rtnl = net->rtnl; 682 683 netlink_set_err(rtnl, 0, group, error); 684 } 685 EXPORT_SYMBOL(rtnl_set_sk_err); 686 687 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 688 { 689 struct nlattr *mx; 690 int i, valid = 0; 691 692 mx = nla_nest_start(skb, RTA_METRICS); 693 if (mx == NULL) 694 return -ENOBUFS; 695 696 for (i = 0; i < RTAX_MAX; i++) { 697 if (metrics[i]) { 698 if (i == RTAX_CC_ALGO - 1) { 699 char tmp[TCP_CA_NAME_MAX], *name; 700 701 name = tcp_ca_get_name_by_key(metrics[i], tmp); 702 if (!name) 703 continue; 704 if (nla_put_string(skb, i + 1, name)) 705 goto nla_put_failure; 706 } else if (i == RTAX_FEATURES - 1) { 707 u32 user_features = metrics[i] & RTAX_FEATURE_MASK; 708 709 if (!user_features) 710 continue; 711 BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK); 712 if (nla_put_u32(skb, i + 1, user_features)) 713 goto nla_put_failure; 714 } else { 715 if (nla_put_u32(skb, i + 1, metrics[i])) 716 goto nla_put_failure; 717 } 718 valid++; 719 } 720 } 721 722 if (!valid) { 723 nla_nest_cancel(skb, mx); 724 return 0; 725 } 726 727 return nla_nest_end(skb, mx); 728 729 nla_put_failure: 730 nla_nest_cancel(skb, mx); 731 return -EMSGSIZE; 732 } 733 EXPORT_SYMBOL(rtnetlink_put_metrics); 734 735 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 736 long expires, u32 error) 737 { 738 struct rta_cacheinfo ci = { 739 .rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse), 740 .rta_used = dst->__use, 741 .rta_clntref = atomic_read(&(dst->__refcnt)), 742 .rta_error = error, 743 .rta_id = id, 744 }; 745 746 if (expires) { 747 unsigned long clock; 748 749 clock = jiffies_to_clock_t(abs(expires)); 750 clock = min_t(unsigned long, clock, INT_MAX); 751 ci.rta_expires = (expires > 0) ? clock : -clock; 752 } 753 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 754 } 755 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 756 757 static void set_operstate(struct net_device *dev, unsigned char transition) 758 { 759 unsigned char operstate = dev->operstate; 760 761 switch (transition) { 762 case IF_OPER_UP: 763 if ((operstate == IF_OPER_DORMANT || 764 operstate == IF_OPER_UNKNOWN) && 765 !netif_dormant(dev)) 766 operstate = IF_OPER_UP; 767 break; 768 769 case IF_OPER_DORMANT: 770 if (operstate == IF_OPER_UP || 771 operstate == IF_OPER_UNKNOWN) 772 operstate = IF_OPER_DORMANT; 773 break; 774 } 775 776 if (dev->operstate != operstate) { 777 write_lock_bh(&dev_base_lock); 778 dev->operstate = operstate; 779 write_unlock_bh(&dev_base_lock); 780 netdev_state_change(dev); 781 } 782 } 783 784 static unsigned int rtnl_dev_get_flags(const struct net_device *dev) 785 { 786 return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) | 787 (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI)); 788 } 789 790 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 791 const struct ifinfomsg *ifm) 792 { 793 unsigned int flags = ifm->ifi_flags; 794 795 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 796 if (ifm->ifi_change) 797 flags = (flags & ifm->ifi_change) | 798 (rtnl_dev_get_flags(dev) & ~ifm->ifi_change); 799 800 return flags; 801 } 802 803 static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 804 const struct rtnl_link_stats64 *b) 805 { 806 a->rx_packets = b->rx_packets; 807 a->tx_packets = b->tx_packets; 808 a->rx_bytes = b->rx_bytes; 809 a->tx_bytes = b->tx_bytes; 810 a->rx_errors = b->rx_errors; 811 a->tx_errors = b->tx_errors; 812 a->rx_dropped = b->rx_dropped; 813 a->tx_dropped = b->tx_dropped; 814 815 a->multicast = b->multicast; 816 a->collisions = b->collisions; 817 818 a->rx_length_errors = b->rx_length_errors; 819 a->rx_over_errors = b->rx_over_errors; 820 a->rx_crc_errors = b->rx_crc_errors; 821 a->rx_frame_errors = b->rx_frame_errors; 822 a->rx_fifo_errors = b->rx_fifo_errors; 823 a->rx_missed_errors = b->rx_missed_errors; 824 825 a->tx_aborted_errors = b->tx_aborted_errors; 826 a->tx_carrier_errors = b->tx_carrier_errors; 827 a->tx_fifo_errors = b->tx_fifo_errors; 828 a->tx_heartbeat_errors = b->tx_heartbeat_errors; 829 a->tx_window_errors = b->tx_window_errors; 830 831 a->rx_compressed = b->rx_compressed; 832 a->tx_compressed = b->tx_compressed; 833 834 a->rx_nohandler = b->rx_nohandler; 835 } 836 837 /* All VF info */ 838 static inline int rtnl_vfinfo_size(const struct net_device *dev, 839 u32 ext_filter_mask) 840 { 841 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) { 842 int num_vfs = dev_num_vf(dev->dev.parent); 843 size_t size = nla_total_size(0); 844 size += num_vfs * 845 (nla_total_size(0) + 846 nla_total_size(sizeof(struct ifla_vf_mac)) + 847 nla_total_size(sizeof(struct ifla_vf_vlan)) + 848 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */ 849 nla_total_size(MAX_VLAN_LIST_LEN * 850 sizeof(struct ifla_vf_vlan_info)) + 851 nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 852 nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 853 nla_total_size(sizeof(struct ifla_vf_rate)) + 854 nla_total_size(sizeof(struct ifla_vf_link_state)) + 855 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 856 nla_total_size(0) + /* nest IFLA_VF_STATS */ 857 /* IFLA_VF_STATS_RX_PACKETS */ 858 nla_total_size_64bit(sizeof(__u64)) + 859 /* IFLA_VF_STATS_TX_PACKETS */ 860 nla_total_size_64bit(sizeof(__u64)) + 861 /* IFLA_VF_STATS_RX_BYTES */ 862 nla_total_size_64bit(sizeof(__u64)) + 863 /* IFLA_VF_STATS_TX_BYTES */ 864 nla_total_size_64bit(sizeof(__u64)) + 865 /* IFLA_VF_STATS_BROADCAST */ 866 nla_total_size_64bit(sizeof(__u64)) + 867 /* IFLA_VF_STATS_MULTICAST */ 868 nla_total_size_64bit(sizeof(__u64)) + 869 nla_total_size(sizeof(struct ifla_vf_trust))); 870 return size; 871 } else 872 return 0; 873 } 874 875 static size_t rtnl_port_size(const struct net_device *dev, 876 u32 ext_filter_mask) 877 { 878 size_t port_size = nla_total_size(4) /* PORT_VF */ 879 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 880 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 881 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 882 + nla_total_size(1) /* PROT_VDP_REQUEST */ 883 + nla_total_size(2); /* PORT_VDP_RESPONSE */ 884 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 885 size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 886 + port_size; 887 size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 888 + port_size; 889 890 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 891 !(ext_filter_mask & RTEXT_FILTER_VF)) 892 return 0; 893 if (dev_num_vf(dev->dev.parent)) 894 return port_self_size + vf_ports_size + 895 vf_port_size * dev_num_vf(dev->dev.parent); 896 else 897 return port_self_size; 898 } 899 900 static size_t rtnl_xdp_size(void) 901 { 902 size_t xdp_size = nla_total_size(0) + /* nest IFLA_XDP */ 903 nla_total_size(1) + /* XDP_ATTACHED */ 904 nla_total_size(4); /* XDP_PROG_ID */ 905 906 return xdp_size; 907 } 908 909 static noinline size_t if_nlmsg_size(const struct net_device *dev, 910 u32 ext_filter_mask) 911 { 912 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 913 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 914 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 915 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 916 + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap)) 917 + nla_total_size(sizeof(struct rtnl_link_stats)) 918 + nla_total_size_64bit(sizeof(struct rtnl_link_stats64)) 919 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 920 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 921 + nla_total_size(4) /* IFLA_TXQLEN */ 922 + nla_total_size(4) /* IFLA_WEIGHT */ 923 + nla_total_size(4) /* IFLA_MTU */ 924 + nla_total_size(4) /* IFLA_LINK */ 925 + nla_total_size(4) /* IFLA_MASTER */ 926 + nla_total_size(1) /* IFLA_CARRIER */ 927 + nla_total_size(4) /* IFLA_PROMISCUITY */ 928 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 929 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 930 + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */ 931 + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */ 932 + nla_total_size(1) /* IFLA_OPERSTATE */ 933 + nla_total_size(1) /* IFLA_LINKMODE */ 934 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 935 + nla_total_size(4) /* IFLA_LINK_NETNSID */ 936 + nla_total_size(ext_filter_mask 937 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 938 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 939 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 940 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 941 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 942 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 943 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 944 + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */ 945 + rtnl_xdp_size() /* IFLA_XDP */ 946 + nla_total_size(4) /* IFLA_EVENT */ 947 + nla_total_size(1); /* IFLA_PROTO_DOWN */ 948 949 } 950 951 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 952 { 953 struct nlattr *vf_ports; 954 struct nlattr *vf_port; 955 int vf; 956 int err; 957 958 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 959 if (!vf_ports) 960 return -EMSGSIZE; 961 962 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 963 vf_port = nla_nest_start(skb, IFLA_VF_PORT); 964 if (!vf_port) 965 goto nla_put_failure; 966 if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 967 goto nla_put_failure; 968 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 969 if (err == -EMSGSIZE) 970 goto nla_put_failure; 971 if (err) { 972 nla_nest_cancel(skb, vf_port); 973 continue; 974 } 975 nla_nest_end(skb, vf_port); 976 } 977 978 nla_nest_end(skb, vf_ports); 979 980 return 0; 981 982 nla_put_failure: 983 nla_nest_cancel(skb, vf_ports); 984 return -EMSGSIZE; 985 } 986 987 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 988 { 989 struct nlattr *port_self; 990 int err; 991 992 port_self = nla_nest_start(skb, IFLA_PORT_SELF); 993 if (!port_self) 994 return -EMSGSIZE; 995 996 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 997 if (err) { 998 nla_nest_cancel(skb, port_self); 999 return (err == -EMSGSIZE) ? err : 0; 1000 } 1001 1002 nla_nest_end(skb, port_self); 1003 1004 return 0; 1005 } 1006 1007 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 1008 u32 ext_filter_mask) 1009 { 1010 int err; 1011 1012 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 1013 !(ext_filter_mask & RTEXT_FILTER_VF)) 1014 return 0; 1015 1016 err = rtnl_port_self_fill(skb, dev); 1017 if (err) 1018 return err; 1019 1020 if (dev_num_vf(dev->dev.parent)) { 1021 err = rtnl_vf_ports_fill(skb, dev); 1022 if (err) 1023 return err; 1024 } 1025 1026 return 0; 1027 } 1028 1029 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 1030 { 1031 int err; 1032 struct netdev_phys_item_id ppid; 1033 1034 err = dev_get_phys_port_id(dev, &ppid); 1035 if (err) { 1036 if (err == -EOPNOTSUPP) 1037 return 0; 1038 return err; 1039 } 1040 1041 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 1042 return -EMSGSIZE; 1043 1044 return 0; 1045 } 1046 1047 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1048 { 1049 char name[IFNAMSIZ]; 1050 int err; 1051 1052 err = dev_get_phys_port_name(dev, name, sizeof(name)); 1053 if (err) { 1054 if (err == -EOPNOTSUPP) 1055 return 0; 1056 return err; 1057 } 1058 1059 if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name)) 1060 return -EMSGSIZE; 1061 1062 return 0; 1063 } 1064 1065 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 1066 { 1067 int err; 1068 struct switchdev_attr attr = { 1069 .orig_dev = dev, 1070 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, 1071 .flags = SWITCHDEV_F_NO_RECURSE, 1072 }; 1073 1074 err = switchdev_port_attr_get(dev, &attr); 1075 if (err) { 1076 if (err == -EOPNOTSUPP) 1077 return 0; 1078 return err; 1079 } 1080 1081 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, 1082 attr.u.ppid.id)) 1083 return -EMSGSIZE; 1084 1085 return 0; 1086 } 1087 1088 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb, 1089 struct net_device *dev) 1090 { 1091 struct rtnl_link_stats64 *sp; 1092 struct nlattr *attr; 1093 1094 attr = nla_reserve_64bit(skb, IFLA_STATS64, 1095 sizeof(struct rtnl_link_stats64), IFLA_PAD); 1096 if (!attr) 1097 return -EMSGSIZE; 1098 1099 sp = nla_data(attr); 1100 dev_get_stats(dev, sp); 1101 1102 attr = nla_reserve(skb, IFLA_STATS, 1103 sizeof(struct rtnl_link_stats)); 1104 if (!attr) 1105 return -EMSGSIZE; 1106 1107 copy_rtnl_link_stats(nla_data(attr), sp); 1108 1109 return 0; 1110 } 1111 1112 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb, 1113 struct net_device *dev, 1114 int vfs_num, 1115 struct nlattr *vfinfo) 1116 { 1117 struct ifla_vf_rss_query_en vf_rss_query_en; 1118 struct nlattr *vf, *vfstats, *vfvlanlist; 1119 struct ifla_vf_link_state vf_linkstate; 1120 struct ifla_vf_vlan_info vf_vlan_info; 1121 struct ifla_vf_spoofchk vf_spoofchk; 1122 struct ifla_vf_tx_rate vf_tx_rate; 1123 struct ifla_vf_stats vf_stats; 1124 struct ifla_vf_trust vf_trust; 1125 struct ifla_vf_vlan vf_vlan; 1126 struct ifla_vf_rate vf_rate; 1127 struct ifla_vf_mac vf_mac; 1128 struct ifla_vf_info ivi; 1129 1130 memset(&ivi, 0, sizeof(ivi)); 1131 1132 /* Not all SR-IOV capable drivers support the 1133 * spoofcheck and "RSS query enable" query. Preset to 1134 * -1 so the user space tool can detect that the driver 1135 * didn't report anything. 1136 */ 1137 ivi.spoofchk = -1; 1138 ivi.rss_query_en = -1; 1139 ivi.trusted = -1; 1140 /* The default value for VF link state is "auto" 1141 * IFLA_VF_LINK_STATE_AUTO which equals zero 1142 */ 1143 ivi.linkstate = 0; 1144 /* VLAN Protocol by default is 802.1Q */ 1145 ivi.vlan_proto = htons(ETH_P_8021Q); 1146 if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi)) 1147 return 0; 1148 1149 memset(&vf_vlan_info, 0, sizeof(vf_vlan_info)); 1150 1151 vf_mac.vf = 1152 vf_vlan.vf = 1153 vf_vlan_info.vf = 1154 vf_rate.vf = 1155 vf_tx_rate.vf = 1156 vf_spoofchk.vf = 1157 vf_linkstate.vf = 1158 vf_rss_query_en.vf = 1159 vf_trust.vf = ivi.vf; 1160 1161 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1162 vf_vlan.vlan = ivi.vlan; 1163 vf_vlan.qos = ivi.qos; 1164 vf_vlan_info.vlan = ivi.vlan; 1165 vf_vlan_info.qos = ivi.qos; 1166 vf_vlan_info.vlan_proto = ivi.vlan_proto; 1167 vf_tx_rate.rate = ivi.max_tx_rate; 1168 vf_rate.min_tx_rate = ivi.min_tx_rate; 1169 vf_rate.max_tx_rate = ivi.max_tx_rate; 1170 vf_spoofchk.setting = ivi.spoofchk; 1171 vf_linkstate.link_state = ivi.linkstate; 1172 vf_rss_query_en.setting = ivi.rss_query_en; 1173 vf_trust.setting = ivi.trusted; 1174 vf = nla_nest_start(skb, IFLA_VF_INFO); 1175 if (!vf) 1176 goto nla_put_vfinfo_failure; 1177 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1178 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1179 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1180 &vf_rate) || 1181 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1182 &vf_tx_rate) || 1183 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1184 &vf_spoofchk) || 1185 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1186 &vf_linkstate) || 1187 nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1188 sizeof(vf_rss_query_en), 1189 &vf_rss_query_en) || 1190 nla_put(skb, IFLA_VF_TRUST, 1191 sizeof(vf_trust), &vf_trust)) 1192 goto nla_put_vf_failure; 1193 vfvlanlist = nla_nest_start(skb, IFLA_VF_VLAN_LIST); 1194 if (!vfvlanlist) 1195 goto nla_put_vf_failure; 1196 if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info), 1197 &vf_vlan_info)) { 1198 nla_nest_cancel(skb, vfvlanlist); 1199 goto nla_put_vf_failure; 1200 } 1201 nla_nest_end(skb, vfvlanlist); 1202 memset(&vf_stats, 0, sizeof(vf_stats)); 1203 if (dev->netdev_ops->ndo_get_vf_stats) 1204 dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num, 1205 &vf_stats); 1206 vfstats = nla_nest_start(skb, IFLA_VF_STATS); 1207 if (!vfstats) 1208 goto nla_put_vf_failure; 1209 if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS, 1210 vf_stats.rx_packets, IFLA_VF_STATS_PAD) || 1211 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS, 1212 vf_stats.tx_packets, IFLA_VF_STATS_PAD) || 1213 nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES, 1214 vf_stats.rx_bytes, IFLA_VF_STATS_PAD) || 1215 nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES, 1216 vf_stats.tx_bytes, IFLA_VF_STATS_PAD) || 1217 nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST, 1218 vf_stats.broadcast, IFLA_VF_STATS_PAD) || 1219 nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST, 1220 vf_stats.multicast, IFLA_VF_STATS_PAD)) { 1221 nla_nest_cancel(skb, vfstats); 1222 goto nla_put_vf_failure; 1223 } 1224 nla_nest_end(skb, vfstats); 1225 nla_nest_end(skb, vf); 1226 return 0; 1227 1228 nla_put_vf_failure: 1229 nla_nest_cancel(skb, vf); 1230 nla_put_vfinfo_failure: 1231 nla_nest_cancel(skb, vfinfo); 1232 return -EMSGSIZE; 1233 } 1234 1235 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev) 1236 { 1237 struct rtnl_link_ifmap map; 1238 1239 memset(&map, 0, sizeof(map)); 1240 map.mem_start = dev->mem_start; 1241 map.mem_end = dev->mem_end; 1242 map.base_addr = dev->base_addr; 1243 map.irq = dev->irq; 1244 map.dma = dev->dma; 1245 map.port = dev->if_port; 1246 1247 if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD)) 1248 return -EMSGSIZE; 1249 1250 return 0; 1251 } 1252 1253 static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id) 1254 { 1255 const struct net_device_ops *ops = dev->netdev_ops; 1256 const struct bpf_prog *generic_xdp_prog; 1257 1258 ASSERT_RTNL(); 1259 1260 *prog_id = 0; 1261 generic_xdp_prog = rtnl_dereference(dev->xdp_prog); 1262 if (generic_xdp_prog) { 1263 *prog_id = generic_xdp_prog->aux->id; 1264 return XDP_ATTACHED_SKB; 1265 } 1266 if (ops->ndo_xdp && __dev_xdp_attached(dev, ops->ndo_xdp, prog_id)) 1267 return XDP_ATTACHED_DRV; 1268 1269 return XDP_ATTACHED_NONE; 1270 } 1271 1272 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev) 1273 { 1274 struct nlattr *xdp; 1275 u32 prog_id; 1276 int err; 1277 1278 xdp = nla_nest_start(skb, IFLA_XDP); 1279 if (!xdp) 1280 return -EMSGSIZE; 1281 1282 err = nla_put_u8(skb, IFLA_XDP_ATTACHED, 1283 rtnl_xdp_attached_mode(dev, &prog_id)); 1284 if (err) 1285 goto err_cancel; 1286 1287 if (prog_id) { 1288 err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id); 1289 if (err) 1290 goto err_cancel; 1291 } 1292 1293 nla_nest_end(skb, xdp); 1294 return 0; 1295 1296 err_cancel: 1297 nla_nest_cancel(skb, xdp); 1298 return err; 1299 } 1300 1301 static u32 rtnl_get_event(unsigned long event) 1302 { 1303 u32 rtnl_event_type = IFLA_EVENT_NONE; 1304 1305 switch (event) { 1306 case NETDEV_REBOOT: 1307 rtnl_event_type = IFLA_EVENT_REBOOT; 1308 break; 1309 case NETDEV_FEAT_CHANGE: 1310 rtnl_event_type = IFLA_EVENT_FEATURES; 1311 break; 1312 case NETDEV_BONDING_FAILOVER: 1313 rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER; 1314 break; 1315 case NETDEV_NOTIFY_PEERS: 1316 rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS; 1317 break; 1318 case NETDEV_RESEND_IGMP: 1319 rtnl_event_type = IFLA_EVENT_IGMP_RESEND; 1320 break; 1321 case NETDEV_CHANGEINFODATA: 1322 rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS; 1323 break; 1324 default: 1325 break; 1326 } 1327 1328 return rtnl_event_type; 1329 } 1330 1331 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 1332 int type, u32 pid, u32 seq, u32 change, 1333 unsigned int flags, u32 ext_filter_mask, 1334 u32 event) 1335 { 1336 struct ifinfomsg *ifm; 1337 struct nlmsghdr *nlh; 1338 struct nlattr *af_spec; 1339 struct rtnl_af_ops *af_ops; 1340 struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 1341 1342 ASSERT_RTNL(); 1343 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1344 if (nlh == NULL) 1345 return -EMSGSIZE; 1346 1347 ifm = nlmsg_data(nlh); 1348 ifm->ifi_family = AF_UNSPEC; 1349 ifm->__ifi_pad = 0; 1350 ifm->ifi_type = dev->type; 1351 ifm->ifi_index = dev->ifindex; 1352 ifm->ifi_flags = dev_get_flags(dev); 1353 ifm->ifi_change = change; 1354 1355 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1356 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1357 nla_put_u8(skb, IFLA_OPERSTATE, 1358 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1359 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1360 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 1361 nla_put_u32(skb, IFLA_GROUP, dev->group) || 1362 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 1363 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1364 nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) || 1365 nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) || 1366 #ifdef CONFIG_RPS 1367 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 1368 #endif 1369 (dev->ifindex != dev_get_iflink(dev) && 1370 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 1371 (upper_dev && 1372 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || 1373 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1374 (dev->qdisc && 1375 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 1376 (dev->ifalias && 1377 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) || 1378 nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1379 atomic_read(&dev->carrier_changes)) || 1380 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) 1381 goto nla_put_failure; 1382 1383 if (event != IFLA_EVENT_NONE) { 1384 if (nla_put_u32(skb, IFLA_EVENT, event)) 1385 goto nla_put_failure; 1386 } 1387 1388 if (rtnl_fill_link_ifmap(skb, dev)) 1389 goto nla_put_failure; 1390 1391 if (dev->addr_len) { 1392 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1393 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1394 goto nla_put_failure; 1395 } 1396 1397 if (rtnl_phys_port_id_fill(skb, dev)) 1398 goto nla_put_failure; 1399 1400 if (rtnl_phys_port_name_fill(skb, dev)) 1401 goto nla_put_failure; 1402 1403 if (rtnl_phys_switch_id_fill(skb, dev)) 1404 goto nla_put_failure; 1405 1406 if (rtnl_fill_stats(skb, dev)) 1407 goto nla_put_failure; 1408 1409 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) && 1410 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent))) 1411 goto nla_put_failure; 1412 1413 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent && 1414 ext_filter_mask & RTEXT_FILTER_VF) { 1415 int i; 1416 struct nlattr *vfinfo; 1417 int num_vfs = dev_num_vf(dev->dev.parent); 1418 1419 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1420 if (!vfinfo) 1421 goto nla_put_failure; 1422 for (i = 0; i < num_vfs; i++) { 1423 if (rtnl_fill_vfinfo(skb, dev, i, vfinfo)) 1424 goto nla_put_failure; 1425 } 1426 1427 nla_nest_end(skb, vfinfo); 1428 } 1429 1430 if (rtnl_port_fill(skb, dev, ext_filter_mask)) 1431 goto nla_put_failure; 1432 1433 if (rtnl_xdp_fill(skb, dev)) 1434 goto nla_put_failure; 1435 1436 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 1437 if (rtnl_link_fill(skb, dev) < 0) 1438 goto nla_put_failure; 1439 } 1440 1441 if (dev->rtnl_link_ops && 1442 dev->rtnl_link_ops->get_link_net) { 1443 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1444 1445 if (!net_eq(dev_net(dev), link_net)) { 1446 int id = peernet2id_alloc(dev_net(dev), link_net); 1447 1448 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1449 goto nla_put_failure; 1450 } 1451 } 1452 1453 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) 1454 goto nla_put_failure; 1455 1456 list_for_each_entry(af_ops, &rtnl_af_ops, list) { 1457 if (af_ops->fill_link_af) { 1458 struct nlattr *af; 1459 int err; 1460 1461 if (!(af = nla_nest_start(skb, af_ops->family))) 1462 goto nla_put_failure; 1463 1464 err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1465 1466 /* 1467 * Caller may return ENODATA to indicate that there 1468 * was no data to be dumped. This is not an error, it 1469 * means we should trim the attribute header and 1470 * continue. 1471 */ 1472 if (err == -ENODATA) 1473 nla_nest_cancel(skb, af); 1474 else if (err < 0) 1475 goto nla_put_failure; 1476 1477 nla_nest_end(skb, af); 1478 } 1479 } 1480 1481 nla_nest_end(skb, af_spec); 1482 1483 nlmsg_end(skb, nlh); 1484 return 0; 1485 1486 nla_put_failure: 1487 nlmsg_cancel(skb, nlh); 1488 return -EMSGSIZE; 1489 } 1490 1491 static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1492 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1493 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1494 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1495 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1496 [IFLA_MTU] = { .type = NLA_U32 }, 1497 [IFLA_LINK] = { .type = NLA_U32 }, 1498 [IFLA_MASTER] = { .type = NLA_U32 }, 1499 [IFLA_CARRIER] = { .type = NLA_U8 }, 1500 [IFLA_TXQLEN] = { .type = NLA_U32 }, 1501 [IFLA_WEIGHT] = { .type = NLA_U32 }, 1502 [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1503 [IFLA_LINKMODE] = { .type = NLA_U8 }, 1504 [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1505 [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1506 [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 1507 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, 1508 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1509 [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1510 [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1511 [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1512 [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1513 [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1514 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1515 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 1516 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1517 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 1518 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1519 [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 1520 [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1521 [IFLA_XDP] = { .type = NLA_NESTED }, 1522 [IFLA_EVENT] = { .type = NLA_U32 }, 1523 }; 1524 1525 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1526 [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1527 [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1528 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1529 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1530 }; 1531 1532 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1533 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1534 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 1535 [IFLA_VF_VLAN_LIST] = { .type = NLA_NESTED }, 1536 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1537 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1538 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1539 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 1540 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 1541 [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1542 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1543 [IFLA_VF_IB_NODE_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1544 [IFLA_VF_IB_PORT_GUID] = { .len = sizeof(struct ifla_vf_guid) }, 1545 }; 1546 1547 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1548 [IFLA_PORT_VF] = { .type = NLA_U32 }, 1549 [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1550 .len = PORT_PROFILE_MAX }, 1551 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1552 .len = PORT_UUID_MAX }, 1553 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1554 .len = PORT_UUID_MAX }, 1555 [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1556 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1557 1558 /* Unused, but we need to keep it here since user space could 1559 * fill it. It's also broken with regard to NLA_BINARY use in 1560 * combination with structs. 1561 */ 1562 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1563 .len = sizeof(struct ifla_port_vsi) }, 1564 }; 1565 1566 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { 1567 [IFLA_XDP_FD] = { .type = NLA_S32 }, 1568 [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, 1569 [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, 1570 [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, 1571 }; 1572 1573 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) 1574 { 1575 const struct rtnl_link_ops *ops = NULL; 1576 struct nlattr *linfo[IFLA_INFO_MAX + 1]; 1577 1578 if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, 1579 ifla_info_policy, NULL) < 0) 1580 return NULL; 1581 1582 if (linfo[IFLA_INFO_KIND]) { 1583 char kind[MODULE_NAME_LEN]; 1584 1585 nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind)); 1586 ops = rtnl_link_ops_get(kind); 1587 } 1588 1589 return ops; 1590 } 1591 1592 static bool link_master_filtered(struct net_device *dev, int master_idx) 1593 { 1594 struct net_device *master; 1595 1596 if (!master_idx) 1597 return false; 1598 1599 master = netdev_master_upper_dev_get(dev); 1600 if (!master || master->ifindex != master_idx) 1601 return true; 1602 1603 return false; 1604 } 1605 1606 static bool link_kind_filtered(const struct net_device *dev, 1607 const struct rtnl_link_ops *kind_ops) 1608 { 1609 if (kind_ops && dev->rtnl_link_ops != kind_ops) 1610 return true; 1611 1612 return false; 1613 } 1614 1615 static bool link_dump_filtered(struct net_device *dev, 1616 int master_idx, 1617 const struct rtnl_link_ops *kind_ops) 1618 { 1619 if (link_master_filtered(dev, master_idx) || 1620 link_kind_filtered(dev, kind_ops)) 1621 return true; 1622 1623 return false; 1624 } 1625 1626 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 1627 { 1628 struct net *net = sock_net(skb->sk); 1629 int h, s_h; 1630 int idx = 0, s_idx; 1631 struct net_device *dev; 1632 struct hlist_head *head; 1633 struct nlattr *tb[IFLA_MAX+1]; 1634 u32 ext_filter_mask = 0; 1635 const struct rtnl_link_ops *kind_ops = NULL; 1636 unsigned int flags = NLM_F_MULTI; 1637 int master_idx = 0; 1638 int err; 1639 int hdrlen; 1640 1641 s_h = cb->args[0]; 1642 s_idx = cb->args[1]; 1643 1644 cb->seq = net->dev_base_seq; 1645 1646 /* A hack to preserve kernel<->userspace interface. 1647 * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1648 * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1649 * what iproute2 < v3.9.0 used. 1650 * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1651 * attribute, its netlink message is shorter than struct ifinfomsg. 1652 */ 1653 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? 1654 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1655 1656 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, 1657 ifla_policy, NULL) >= 0) { 1658 if (tb[IFLA_EXT_MASK]) 1659 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1660 1661 if (tb[IFLA_MASTER]) 1662 master_idx = nla_get_u32(tb[IFLA_MASTER]); 1663 1664 if (tb[IFLA_LINKINFO]) 1665 kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]); 1666 1667 if (master_idx || kind_ops) 1668 flags |= NLM_F_DUMP_FILTERED; 1669 } 1670 1671 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 1672 idx = 0; 1673 head = &net->dev_index_head[h]; 1674 hlist_for_each_entry(dev, head, index_hlist) { 1675 if (link_dump_filtered(dev, master_idx, kind_ops)) 1676 goto cont; 1677 if (idx < s_idx) 1678 goto cont; 1679 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 1680 NETLINK_CB(cb->skb).portid, 1681 cb->nlh->nlmsg_seq, 0, 1682 flags, 1683 ext_filter_mask, 0); 1684 1685 if (err < 0) { 1686 if (likely(skb->len)) 1687 goto out; 1688 1689 goto out_err; 1690 } 1691 1692 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 1693 cont: 1694 idx++; 1695 } 1696 } 1697 out: 1698 err = skb->len; 1699 out_err: 1700 cb->args[1] = idx; 1701 cb->args[0] = h; 1702 1703 return err; 1704 } 1705 1706 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len, 1707 struct netlink_ext_ack *exterr) 1708 { 1709 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy, exterr); 1710 } 1711 EXPORT_SYMBOL(rtnl_nla_parse_ifla); 1712 1713 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 1714 { 1715 struct net *net; 1716 /* Examine the link attributes and figure out which 1717 * network namespace we are talking about. 1718 */ 1719 if (tb[IFLA_NET_NS_PID]) 1720 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1721 else if (tb[IFLA_NET_NS_FD]) 1722 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 1723 else 1724 net = get_net(src_net); 1725 return net; 1726 } 1727 EXPORT_SYMBOL(rtnl_link_get_net); 1728 1729 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 1730 { 1731 if (dev) { 1732 if (tb[IFLA_ADDRESS] && 1733 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 1734 return -EINVAL; 1735 1736 if (tb[IFLA_BROADCAST] && 1737 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 1738 return -EINVAL; 1739 } 1740 1741 if (tb[IFLA_AF_SPEC]) { 1742 struct nlattr *af; 1743 int rem, err; 1744 1745 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1746 const struct rtnl_af_ops *af_ops; 1747 1748 if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1749 return -EAFNOSUPPORT; 1750 1751 if (!af_ops->set_link_af) 1752 return -EOPNOTSUPP; 1753 1754 if (af_ops->validate_link_af) { 1755 err = af_ops->validate_link_af(dev, af); 1756 if (err < 0) 1757 return err; 1758 } 1759 } 1760 } 1761 1762 return 0; 1763 } 1764 1765 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt, 1766 int guid_type) 1767 { 1768 const struct net_device_ops *ops = dev->netdev_ops; 1769 1770 return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type); 1771 } 1772 1773 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type) 1774 { 1775 if (dev->type != ARPHRD_INFINIBAND) 1776 return -EOPNOTSUPP; 1777 1778 return handle_infiniband_guid(dev, ivt, guid_type); 1779 } 1780 1781 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 1782 { 1783 const struct net_device_ops *ops = dev->netdev_ops; 1784 int err = -EINVAL; 1785 1786 if (tb[IFLA_VF_MAC]) { 1787 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 1788 1789 err = -EOPNOTSUPP; 1790 if (ops->ndo_set_vf_mac) 1791 err = ops->ndo_set_vf_mac(dev, ivm->vf, 1792 ivm->mac); 1793 if (err < 0) 1794 return err; 1795 } 1796 1797 if (tb[IFLA_VF_VLAN]) { 1798 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 1799 1800 err = -EOPNOTSUPP; 1801 if (ops->ndo_set_vf_vlan) 1802 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 1803 ivv->qos, 1804 htons(ETH_P_8021Q)); 1805 if (err < 0) 1806 return err; 1807 } 1808 1809 if (tb[IFLA_VF_VLAN_LIST]) { 1810 struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN]; 1811 struct nlattr *attr; 1812 int rem, len = 0; 1813 1814 err = -EOPNOTSUPP; 1815 if (!ops->ndo_set_vf_vlan) 1816 return err; 1817 1818 nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) { 1819 if (nla_type(attr) != IFLA_VF_VLAN_INFO || 1820 nla_len(attr) < NLA_HDRLEN) { 1821 return -EINVAL; 1822 } 1823 if (len >= MAX_VLAN_LIST_LEN) 1824 return -EOPNOTSUPP; 1825 ivvl[len] = nla_data(attr); 1826 1827 len++; 1828 } 1829 if (len == 0) 1830 return -EINVAL; 1831 1832 err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan, 1833 ivvl[0]->qos, ivvl[0]->vlan_proto); 1834 if (err < 0) 1835 return err; 1836 } 1837 1838 if (tb[IFLA_VF_TX_RATE]) { 1839 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 1840 struct ifla_vf_info ivf; 1841 1842 err = -EOPNOTSUPP; 1843 if (ops->ndo_get_vf_config) 1844 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 1845 if (err < 0) 1846 return err; 1847 1848 err = -EOPNOTSUPP; 1849 if (ops->ndo_set_vf_rate) 1850 err = ops->ndo_set_vf_rate(dev, ivt->vf, 1851 ivf.min_tx_rate, 1852 ivt->rate); 1853 if (err < 0) 1854 return err; 1855 } 1856 1857 if (tb[IFLA_VF_RATE]) { 1858 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 1859 1860 err = -EOPNOTSUPP; 1861 if (ops->ndo_set_vf_rate) 1862 err = ops->ndo_set_vf_rate(dev, ivt->vf, 1863 ivt->min_tx_rate, 1864 ivt->max_tx_rate); 1865 if (err < 0) 1866 return err; 1867 } 1868 1869 if (tb[IFLA_VF_SPOOFCHK]) { 1870 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 1871 1872 err = -EOPNOTSUPP; 1873 if (ops->ndo_set_vf_spoofchk) 1874 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 1875 ivs->setting); 1876 if (err < 0) 1877 return err; 1878 } 1879 1880 if (tb[IFLA_VF_LINK_STATE]) { 1881 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 1882 1883 err = -EOPNOTSUPP; 1884 if (ops->ndo_set_vf_link_state) 1885 err = ops->ndo_set_vf_link_state(dev, ivl->vf, 1886 ivl->link_state); 1887 if (err < 0) 1888 return err; 1889 } 1890 1891 if (tb[IFLA_VF_RSS_QUERY_EN]) { 1892 struct ifla_vf_rss_query_en *ivrssq_en; 1893 1894 err = -EOPNOTSUPP; 1895 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 1896 if (ops->ndo_set_vf_rss_query_en) 1897 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 1898 ivrssq_en->setting); 1899 if (err < 0) 1900 return err; 1901 } 1902 1903 if (tb[IFLA_VF_TRUST]) { 1904 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 1905 1906 err = -EOPNOTSUPP; 1907 if (ops->ndo_set_vf_trust) 1908 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 1909 if (err < 0) 1910 return err; 1911 } 1912 1913 if (tb[IFLA_VF_IB_NODE_GUID]) { 1914 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]); 1915 1916 if (!ops->ndo_set_vf_guid) 1917 return -EOPNOTSUPP; 1918 1919 return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID); 1920 } 1921 1922 if (tb[IFLA_VF_IB_PORT_GUID]) { 1923 struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]); 1924 1925 if (!ops->ndo_set_vf_guid) 1926 return -EOPNOTSUPP; 1927 1928 return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID); 1929 } 1930 1931 return err; 1932 } 1933 1934 static int do_set_master(struct net_device *dev, int ifindex) 1935 { 1936 struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 1937 const struct net_device_ops *ops; 1938 int err; 1939 1940 if (upper_dev) { 1941 if (upper_dev->ifindex == ifindex) 1942 return 0; 1943 ops = upper_dev->netdev_ops; 1944 if (ops->ndo_del_slave) { 1945 err = ops->ndo_del_slave(upper_dev, dev); 1946 if (err) 1947 return err; 1948 } else { 1949 return -EOPNOTSUPP; 1950 } 1951 } 1952 1953 if (ifindex) { 1954 upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 1955 if (!upper_dev) 1956 return -EINVAL; 1957 ops = upper_dev->netdev_ops; 1958 if (ops->ndo_add_slave) { 1959 err = ops->ndo_add_slave(upper_dev, dev); 1960 if (err) 1961 return err; 1962 } else { 1963 return -EOPNOTSUPP; 1964 } 1965 } 1966 return 0; 1967 } 1968 1969 #define DO_SETLINK_MODIFIED 0x01 1970 /* notify flag means notify + modified. */ 1971 #define DO_SETLINK_NOTIFY 0x03 1972 static int do_setlink(const struct sk_buff *skb, 1973 struct net_device *dev, struct ifinfomsg *ifm, 1974 struct netlink_ext_ack *extack, 1975 struct nlattr **tb, char *ifname, int status) 1976 { 1977 const struct net_device_ops *ops = dev->netdev_ops; 1978 int err; 1979 1980 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { 1981 struct net *net = rtnl_link_get_net(dev_net(dev), tb); 1982 if (IS_ERR(net)) { 1983 err = PTR_ERR(net); 1984 goto errout; 1985 } 1986 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { 1987 put_net(net); 1988 err = -EPERM; 1989 goto errout; 1990 } 1991 err = dev_change_net_namespace(dev, net, ifname); 1992 put_net(net); 1993 if (err) 1994 goto errout; 1995 status |= DO_SETLINK_MODIFIED; 1996 } 1997 1998 if (tb[IFLA_MAP]) { 1999 struct rtnl_link_ifmap *u_map; 2000 struct ifmap k_map; 2001 2002 if (!ops->ndo_set_config) { 2003 err = -EOPNOTSUPP; 2004 goto errout; 2005 } 2006 2007 if (!netif_device_present(dev)) { 2008 err = -ENODEV; 2009 goto errout; 2010 } 2011 2012 u_map = nla_data(tb[IFLA_MAP]); 2013 k_map.mem_start = (unsigned long) u_map->mem_start; 2014 k_map.mem_end = (unsigned long) u_map->mem_end; 2015 k_map.base_addr = (unsigned short) u_map->base_addr; 2016 k_map.irq = (unsigned char) u_map->irq; 2017 k_map.dma = (unsigned char) u_map->dma; 2018 k_map.port = (unsigned char) u_map->port; 2019 2020 err = ops->ndo_set_config(dev, &k_map); 2021 if (err < 0) 2022 goto errout; 2023 2024 status |= DO_SETLINK_NOTIFY; 2025 } 2026 2027 if (tb[IFLA_ADDRESS]) { 2028 struct sockaddr *sa; 2029 int len; 2030 2031 len = sizeof(sa_family_t) + dev->addr_len; 2032 sa = kmalloc(len, GFP_KERNEL); 2033 if (!sa) { 2034 err = -ENOMEM; 2035 goto errout; 2036 } 2037 sa->sa_family = dev->type; 2038 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 2039 dev->addr_len); 2040 err = dev_set_mac_address(dev, sa); 2041 kfree(sa); 2042 if (err) 2043 goto errout; 2044 status |= DO_SETLINK_MODIFIED; 2045 } 2046 2047 if (tb[IFLA_MTU]) { 2048 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 2049 if (err < 0) 2050 goto errout; 2051 status |= DO_SETLINK_MODIFIED; 2052 } 2053 2054 if (tb[IFLA_GROUP]) { 2055 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2056 status |= DO_SETLINK_NOTIFY; 2057 } 2058 2059 /* 2060 * Interface selected by interface index but interface 2061 * name provided implies that a name change has been 2062 * requested. 2063 */ 2064 if (ifm->ifi_index > 0 && ifname[0]) { 2065 err = dev_change_name(dev, ifname); 2066 if (err < 0) 2067 goto errout; 2068 status |= DO_SETLINK_MODIFIED; 2069 } 2070 2071 if (tb[IFLA_IFALIAS]) { 2072 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 2073 nla_len(tb[IFLA_IFALIAS])); 2074 if (err < 0) 2075 goto errout; 2076 status |= DO_SETLINK_NOTIFY; 2077 } 2078 2079 if (tb[IFLA_BROADCAST]) { 2080 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 2081 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 2082 } 2083 2084 if (ifm->ifi_flags || ifm->ifi_change) { 2085 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 2086 if (err < 0) 2087 goto errout; 2088 } 2089 2090 if (tb[IFLA_MASTER]) { 2091 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); 2092 if (err) 2093 goto errout; 2094 status |= DO_SETLINK_MODIFIED; 2095 } 2096 2097 if (tb[IFLA_CARRIER]) { 2098 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 2099 if (err) 2100 goto errout; 2101 status |= DO_SETLINK_MODIFIED; 2102 } 2103 2104 if (tb[IFLA_TXQLEN]) { 2105 unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]); 2106 unsigned int orig_len = dev->tx_queue_len; 2107 2108 if (dev->tx_queue_len ^ value) { 2109 dev->tx_queue_len = value; 2110 err = call_netdevice_notifiers( 2111 NETDEV_CHANGE_TX_QUEUE_LEN, dev); 2112 err = notifier_to_errno(err); 2113 if (err) { 2114 dev->tx_queue_len = orig_len; 2115 goto errout; 2116 } 2117 status |= DO_SETLINK_NOTIFY; 2118 } 2119 } 2120 2121 if (tb[IFLA_OPERSTATE]) 2122 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2123 2124 if (tb[IFLA_LINKMODE]) { 2125 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 2126 2127 write_lock_bh(&dev_base_lock); 2128 if (dev->link_mode ^ value) 2129 status |= DO_SETLINK_NOTIFY; 2130 dev->link_mode = value; 2131 write_unlock_bh(&dev_base_lock); 2132 } 2133 2134 if (tb[IFLA_VFINFO_LIST]) { 2135 struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 2136 struct nlattr *attr; 2137 int rem; 2138 2139 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 2140 if (nla_type(attr) != IFLA_VF_INFO || 2141 nla_len(attr) < NLA_HDRLEN) { 2142 err = -EINVAL; 2143 goto errout; 2144 } 2145 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 2146 ifla_vf_policy, NULL); 2147 if (err < 0) 2148 goto errout; 2149 err = do_setvfinfo(dev, vfinfo); 2150 if (err < 0) 2151 goto errout; 2152 status |= DO_SETLINK_NOTIFY; 2153 } 2154 } 2155 err = 0; 2156 2157 if (tb[IFLA_VF_PORTS]) { 2158 struct nlattr *port[IFLA_PORT_MAX+1]; 2159 struct nlattr *attr; 2160 int vf; 2161 int rem; 2162 2163 err = -EOPNOTSUPP; 2164 if (!ops->ndo_set_vf_port) 2165 goto errout; 2166 2167 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 2168 if (nla_type(attr) != IFLA_VF_PORT || 2169 nla_len(attr) < NLA_HDRLEN) { 2170 err = -EINVAL; 2171 goto errout; 2172 } 2173 err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 2174 ifla_port_policy, NULL); 2175 if (err < 0) 2176 goto errout; 2177 if (!port[IFLA_PORT_VF]) { 2178 err = -EOPNOTSUPP; 2179 goto errout; 2180 } 2181 vf = nla_get_u32(port[IFLA_PORT_VF]); 2182 err = ops->ndo_set_vf_port(dev, vf, port); 2183 if (err < 0) 2184 goto errout; 2185 status |= DO_SETLINK_NOTIFY; 2186 } 2187 } 2188 err = 0; 2189 2190 if (tb[IFLA_PORT_SELF]) { 2191 struct nlattr *port[IFLA_PORT_MAX+1]; 2192 2193 err = nla_parse_nested(port, IFLA_PORT_MAX, 2194 tb[IFLA_PORT_SELF], ifla_port_policy, 2195 NULL); 2196 if (err < 0) 2197 goto errout; 2198 2199 err = -EOPNOTSUPP; 2200 if (ops->ndo_set_vf_port) 2201 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 2202 if (err < 0) 2203 goto errout; 2204 status |= DO_SETLINK_NOTIFY; 2205 } 2206 2207 if (tb[IFLA_AF_SPEC]) { 2208 struct nlattr *af; 2209 int rem; 2210 2211 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 2212 const struct rtnl_af_ops *af_ops; 2213 2214 if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 2215 BUG(); 2216 2217 err = af_ops->set_link_af(dev, af); 2218 if (err < 0) 2219 goto errout; 2220 2221 status |= DO_SETLINK_NOTIFY; 2222 } 2223 } 2224 err = 0; 2225 2226 if (tb[IFLA_PROTO_DOWN]) { 2227 err = dev_change_proto_down(dev, 2228 nla_get_u8(tb[IFLA_PROTO_DOWN])); 2229 if (err) 2230 goto errout; 2231 status |= DO_SETLINK_NOTIFY; 2232 } 2233 2234 if (tb[IFLA_XDP]) { 2235 struct nlattr *xdp[IFLA_XDP_MAX + 1]; 2236 u32 xdp_flags = 0; 2237 2238 err = nla_parse_nested(xdp, IFLA_XDP_MAX, tb[IFLA_XDP], 2239 ifla_xdp_policy, NULL); 2240 if (err < 0) 2241 goto errout; 2242 2243 if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) { 2244 err = -EINVAL; 2245 goto errout; 2246 } 2247 2248 if (xdp[IFLA_XDP_FLAGS]) { 2249 xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]); 2250 if (xdp_flags & ~XDP_FLAGS_MASK) { 2251 err = -EINVAL; 2252 goto errout; 2253 } 2254 if ((xdp_flags & XDP_FLAGS_SKB_MODE) && 2255 (xdp_flags & XDP_FLAGS_DRV_MODE)) { 2256 err = -EINVAL; 2257 goto errout; 2258 } 2259 } 2260 2261 if (xdp[IFLA_XDP_FD]) { 2262 err = dev_change_xdp_fd(dev, extack, 2263 nla_get_s32(xdp[IFLA_XDP_FD]), 2264 xdp_flags); 2265 if (err) 2266 goto errout; 2267 status |= DO_SETLINK_NOTIFY; 2268 } 2269 } 2270 2271 errout: 2272 if (status & DO_SETLINK_MODIFIED) { 2273 if (status & DO_SETLINK_NOTIFY) 2274 netdev_state_change(dev); 2275 2276 if (err < 0) 2277 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", 2278 dev->name); 2279 } 2280 2281 return err; 2282 } 2283 2284 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2285 struct netlink_ext_ack *extack) 2286 { 2287 struct net *net = sock_net(skb->sk); 2288 struct ifinfomsg *ifm; 2289 struct net_device *dev; 2290 int err; 2291 struct nlattr *tb[IFLA_MAX+1]; 2292 char ifname[IFNAMSIZ]; 2293 2294 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, 2295 extack); 2296 if (err < 0) 2297 goto errout; 2298 2299 if (tb[IFLA_IFNAME]) 2300 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2301 else 2302 ifname[0] = '\0'; 2303 2304 err = -EINVAL; 2305 ifm = nlmsg_data(nlh); 2306 if (ifm->ifi_index > 0) 2307 dev = __dev_get_by_index(net, ifm->ifi_index); 2308 else if (tb[IFLA_IFNAME]) 2309 dev = __dev_get_by_name(net, ifname); 2310 else 2311 goto errout; 2312 2313 if (dev == NULL) { 2314 err = -ENODEV; 2315 goto errout; 2316 } 2317 2318 err = validate_linkmsg(dev, tb); 2319 if (err < 0) 2320 goto errout; 2321 2322 err = do_setlink(skb, dev, ifm, extack, tb, ifname, 0); 2323 errout: 2324 return err; 2325 } 2326 2327 static int rtnl_group_dellink(const struct net *net, int group) 2328 { 2329 struct net_device *dev, *aux; 2330 LIST_HEAD(list_kill); 2331 bool found = false; 2332 2333 if (!group) 2334 return -EPERM; 2335 2336 for_each_netdev(net, dev) { 2337 if (dev->group == group) { 2338 const struct rtnl_link_ops *ops; 2339 2340 found = true; 2341 ops = dev->rtnl_link_ops; 2342 if (!ops || !ops->dellink) 2343 return -EOPNOTSUPP; 2344 } 2345 } 2346 2347 if (!found) 2348 return -ENODEV; 2349 2350 for_each_netdev_safe(net, dev, aux) { 2351 if (dev->group == group) { 2352 const struct rtnl_link_ops *ops; 2353 2354 ops = dev->rtnl_link_ops; 2355 ops->dellink(dev, &list_kill); 2356 } 2357 } 2358 unregister_netdevice_many(&list_kill); 2359 2360 return 0; 2361 } 2362 2363 int rtnl_delete_link(struct net_device *dev) 2364 { 2365 const struct rtnl_link_ops *ops; 2366 LIST_HEAD(list_kill); 2367 2368 ops = dev->rtnl_link_ops; 2369 if (!ops || !ops->dellink) 2370 return -EOPNOTSUPP; 2371 2372 ops->dellink(dev, &list_kill); 2373 unregister_netdevice_many(&list_kill); 2374 2375 return 0; 2376 } 2377 EXPORT_SYMBOL_GPL(rtnl_delete_link); 2378 2379 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 2380 struct netlink_ext_ack *extack) 2381 { 2382 struct net *net = sock_net(skb->sk); 2383 struct net_device *dev; 2384 struct ifinfomsg *ifm; 2385 char ifname[IFNAMSIZ]; 2386 struct nlattr *tb[IFLA_MAX+1]; 2387 int err; 2388 2389 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 2390 if (err < 0) 2391 return err; 2392 2393 if (tb[IFLA_IFNAME]) 2394 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2395 2396 ifm = nlmsg_data(nlh); 2397 if (ifm->ifi_index > 0) 2398 dev = __dev_get_by_index(net, ifm->ifi_index); 2399 else if (tb[IFLA_IFNAME]) 2400 dev = __dev_get_by_name(net, ifname); 2401 else if (tb[IFLA_GROUP]) 2402 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP])); 2403 else 2404 return -EINVAL; 2405 2406 if (!dev) 2407 return -ENODEV; 2408 2409 return rtnl_delete_link(dev); 2410 } 2411 2412 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 2413 { 2414 unsigned int old_flags; 2415 int err; 2416 2417 old_flags = dev->flags; 2418 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 2419 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 2420 if (err < 0) 2421 return err; 2422 } 2423 2424 dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2425 2426 __dev_notify_flags(dev, old_flags, ~0U); 2427 return 0; 2428 } 2429 EXPORT_SYMBOL(rtnl_configure_link); 2430 2431 struct net_device *rtnl_create_link(struct net *net, 2432 const char *ifname, unsigned char name_assign_type, 2433 const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2434 { 2435 struct net_device *dev; 2436 unsigned int num_tx_queues = 1; 2437 unsigned int num_rx_queues = 1; 2438 2439 if (tb[IFLA_NUM_TX_QUEUES]) 2440 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 2441 else if (ops->get_num_tx_queues) 2442 num_tx_queues = ops->get_num_tx_queues(); 2443 2444 if (tb[IFLA_NUM_RX_QUEUES]) 2445 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 2446 else if (ops->get_num_rx_queues) 2447 num_rx_queues = ops->get_num_rx_queues(); 2448 2449 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2450 ops->setup, num_tx_queues, num_rx_queues); 2451 if (!dev) 2452 return ERR_PTR(-ENOMEM); 2453 2454 dev_net_set(dev, net); 2455 dev->rtnl_link_ops = ops; 2456 dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 2457 2458 if (tb[IFLA_MTU]) 2459 dev->mtu = nla_get_u32(tb[IFLA_MTU]); 2460 if (tb[IFLA_ADDRESS]) { 2461 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2462 nla_len(tb[IFLA_ADDRESS])); 2463 dev->addr_assign_type = NET_ADDR_SET; 2464 } 2465 if (tb[IFLA_BROADCAST]) 2466 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2467 nla_len(tb[IFLA_BROADCAST])); 2468 if (tb[IFLA_TXQLEN]) 2469 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2470 if (tb[IFLA_OPERSTATE]) 2471 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2472 if (tb[IFLA_LINKMODE]) 2473 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2474 if (tb[IFLA_GROUP]) 2475 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2476 2477 return dev; 2478 } 2479 EXPORT_SYMBOL(rtnl_create_link); 2480 2481 static int rtnl_group_changelink(const struct sk_buff *skb, 2482 struct net *net, int group, 2483 struct ifinfomsg *ifm, 2484 struct netlink_ext_ack *extack, 2485 struct nlattr **tb) 2486 { 2487 struct net_device *dev, *aux; 2488 int err; 2489 2490 for_each_netdev_safe(net, dev, aux) { 2491 if (dev->group == group) { 2492 err = do_setlink(skb, dev, ifm, extack, tb, NULL, 0); 2493 if (err < 0) 2494 return err; 2495 } 2496 } 2497 2498 return 0; 2499 } 2500 2501 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2502 struct netlink_ext_ack *extack) 2503 { 2504 struct net *net = sock_net(skb->sk); 2505 const struct rtnl_link_ops *ops; 2506 const struct rtnl_link_ops *m_ops = NULL; 2507 struct net_device *dev; 2508 struct net_device *master_dev = NULL; 2509 struct ifinfomsg *ifm; 2510 char kind[MODULE_NAME_LEN]; 2511 char ifname[IFNAMSIZ]; 2512 struct nlattr *tb[IFLA_MAX+1]; 2513 struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 2514 unsigned char name_assign_type = NET_NAME_USER; 2515 int err; 2516 2517 #ifdef CONFIG_MODULES 2518 replay: 2519 #endif 2520 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 2521 if (err < 0) 2522 return err; 2523 2524 if (tb[IFLA_IFNAME]) 2525 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2526 else 2527 ifname[0] = '\0'; 2528 2529 ifm = nlmsg_data(nlh); 2530 if (ifm->ifi_index > 0) 2531 dev = __dev_get_by_index(net, ifm->ifi_index); 2532 else { 2533 if (ifname[0]) 2534 dev = __dev_get_by_name(net, ifname); 2535 else 2536 dev = NULL; 2537 } 2538 2539 if (dev) { 2540 master_dev = netdev_master_upper_dev_get(dev); 2541 if (master_dev) 2542 m_ops = master_dev->rtnl_link_ops; 2543 } 2544 2545 err = validate_linkmsg(dev, tb); 2546 if (err < 0) 2547 return err; 2548 2549 if (tb[IFLA_LINKINFO]) { 2550 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 2551 tb[IFLA_LINKINFO], ifla_info_policy, 2552 NULL); 2553 if (err < 0) 2554 return err; 2555 } else 2556 memset(linkinfo, 0, sizeof(linkinfo)); 2557 2558 if (linkinfo[IFLA_INFO_KIND]) { 2559 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 2560 ops = rtnl_link_ops_get(kind); 2561 } else { 2562 kind[0] = '\0'; 2563 ops = NULL; 2564 } 2565 2566 if (1) { 2567 struct nlattr *attr[ops ? ops->maxtype + 1 : 1]; 2568 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1]; 2569 struct nlattr **data = NULL; 2570 struct nlattr **slave_data = NULL; 2571 struct net *dest_net, *link_net = NULL; 2572 2573 if (ops) { 2574 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 2575 err = nla_parse_nested(attr, ops->maxtype, 2576 linkinfo[IFLA_INFO_DATA], 2577 ops->policy, NULL); 2578 if (err < 0) 2579 return err; 2580 data = attr; 2581 } 2582 if (ops->validate) { 2583 err = ops->validate(tb, data); 2584 if (err < 0) 2585 return err; 2586 } 2587 } 2588 2589 if (m_ops) { 2590 if (m_ops->slave_maxtype && 2591 linkinfo[IFLA_INFO_SLAVE_DATA]) { 2592 err = nla_parse_nested(slave_attr, 2593 m_ops->slave_maxtype, 2594 linkinfo[IFLA_INFO_SLAVE_DATA], 2595 m_ops->slave_policy, 2596 NULL); 2597 if (err < 0) 2598 return err; 2599 slave_data = slave_attr; 2600 } 2601 if (m_ops->slave_validate) { 2602 err = m_ops->slave_validate(tb, slave_data); 2603 if (err < 0) 2604 return err; 2605 } 2606 } 2607 2608 if (dev) { 2609 int status = 0; 2610 2611 if (nlh->nlmsg_flags & NLM_F_EXCL) 2612 return -EEXIST; 2613 if (nlh->nlmsg_flags & NLM_F_REPLACE) 2614 return -EOPNOTSUPP; 2615 2616 if (linkinfo[IFLA_INFO_DATA]) { 2617 if (!ops || ops != dev->rtnl_link_ops || 2618 !ops->changelink) 2619 return -EOPNOTSUPP; 2620 2621 err = ops->changelink(dev, tb, data); 2622 if (err < 0) 2623 return err; 2624 status |= DO_SETLINK_NOTIFY; 2625 } 2626 2627 if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 2628 if (!m_ops || !m_ops->slave_changelink) 2629 return -EOPNOTSUPP; 2630 2631 err = m_ops->slave_changelink(master_dev, dev, 2632 tb, slave_data); 2633 if (err < 0) 2634 return err; 2635 status |= DO_SETLINK_NOTIFY; 2636 } 2637 2638 return do_setlink(skb, dev, ifm, extack, tb, ifname, 2639 status); 2640 } 2641 2642 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 2643 if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 2644 return rtnl_group_changelink(skb, net, 2645 nla_get_u32(tb[IFLA_GROUP]), 2646 ifm, extack, tb); 2647 return -ENODEV; 2648 } 2649 2650 if (tb[IFLA_MAP] || tb[IFLA_PROTINFO]) 2651 return -EOPNOTSUPP; 2652 2653 if (!ops) { 2654 #ifdef CONFIG_MODULES 2655 if (kind[0]) { 2656 __rtnl_unlock(); 2657 request_module("rtnl-link-%s", kind); 2658 rtnl_lock(); 2659 ops = rtnl_link_ops_get(kind); 2660 if (ops) 2661 goto replay; 2662 } 2663 #endif 2664 return -EOPNOTSUPP; 2665 } 2666 2667 if (!ops->setup) 2668 return -EOPNOTSUPP; 2669 2670 if (!ifname[0]) { 2671 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 2672 name_assign_type = NET_NAME_ENUM; 2673 } 2674 2675 dest_net = rtnl_link_get_net(net, tb); 2676 if (IS_ERR(dest_net)) 2677 return PTR_ERR(dest_net); 2678 2679 err = -EPERM; 2680 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN)) 2681 goto out; 2682 2683 if (tb[IFLA_LINK_NETNSID]) { 2684 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 2685 2686 link_net = get_net_ns_by_id(dest_net, id); 2687 if (!link_net) { 2688 err = -EINVAL; 2689 goto out; 2690 } 2691 err = -EPERM; 2692 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 2693 goto out; 2694 } 2695 2696 dev = rtnl_create_link(link_net ? : dest_net, ifname, 2697 name_assign_type, ops, tb); 2698 if (IS_ERR(dev)) { 2699 err = PTR_ERR(dev); 2700 goto out; 2701 } 2702 2703 dev->ifindex = ifm->ifi_index; 2704 2705 if (ops->newlink) { 2706 err = ops->newlink(link_net ? : net, dev, tb, data); 2707 /* Drivers should call free_netdev() in ->destructor 2708 * and unregister it on failure after registration 2709 * so that device could be finally freed in rtnl_unlock. 2710 */ 2711 if (err < 0) { 2712 /* If device is not registered at all, free it now */ 2713 if (dev->reg_state == NETREG_UNINITIALIZED) 2714 free_netdev(dev); 2715 goto out; 2716 } 2717 } else { 2718 err = register_netdevice(dev); 2719 if (err < 0) { 2720 free_netdev(dev); 2721 goto out; 2722 } 2723 } 2724 err = rtnl_configure_link(dev, ifm); 2725 if (err < 0) 2726 goto out_unregister; 2727 if (link_net) { 2728 err = dev_change_net_namespace(dev, dest_net, ifname); 2729 if (err < 0) 2730 goto out_unregister; 2731 } 2732 if (tb[IFLA_MASTER]) { 2733 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); 2734 if (err) 2735 goto out_unregister; 2736 } 2737 out: 2738 if (link_net) 2739 put_net(link_net); 2740 put_net(dest_net); 2741 return err; 2742 out_unregister: 2743 if (ops->newlink) { 2744 LIST_HEAD(list_kill); 2745 2746 ops->dellink(dev, &list_kill); 2747 unregister_netdevice_many(&list_kill); 2748 } else { 2749 unregister_netdevice(dev); 2750 } 2751 goto out; 2752 } 2753 } 2754 2755 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, 2756 struct netlink_ext_ack *extack) 2757 { 2758 struct net *net = sock_net(skb->sk); 2759 struct ifinfomsg *ifm; 2760 char ifname[IFNAMSIZ]; 2761 struct nlattr *tb[IFLA_MAX+1]; 2762 struct net_device *dev = NULL; 2763 struct sk_buff *nskb; 2764 int err; 2765 u32 ext_filter_mask = 0; 2766 2767 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack); 2768 if (err < 0) 2769 return err; 2770 2771 if (tb[IFLA_IFNAME]) 2772 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2773 2774 if (tb[IFLA_EXT_MASK]) 2775 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 2776 2777 ifm = nlmsg_data(nlh); 2778 if (ifm->ifi_index > 0) 2779 dev = __dev_get_by_index(net, ifm->ifi_index); 2780 else if (tb[IFLA_IFNAME]) 2781 dev = __dev_get_by_name(net, ifname); 2782 else 2783 return -EINVAL; 2784 2785 if (dev == NULL) 2786 return -ENODEV; 2787 2788 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 2789 if (nskb == NULL) 2790 return -ENOBUFS; 2791 2792 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, 2793 nlh->nlmsg_seq, 0, 0, ext_filter_mask, 0); 2794 if (err < 0) { 2795 /* -EMSGSIZE implies BUG in if_nlmsg_size */ 2796 WARN_ON(err == -EMSGSIZE); 2797 kfree_skb(nskb); 2798 } else 2799 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 2800 2801 return err; 2802 } 2803 2804 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 2805 { 2806 struct net *net = sock_net(skb->sk); 2807 struct net_device *dev; 2808 struct nlattr *tb[IFLA_MAX+1]; 2809 u32 ext_filter_mask = 0; 2810 u16 min_ifinfo_dump_size = 0; 2811 int hdrlen; 2812 2813 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 2814 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2815 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2816 2817 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) { 2818 if (tb[IFLA_EXT_MASK]) 2819 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 2820 } 2821 2822 if (!ext_filter_mask) 2823 return NLMSG_GOODSIZE; 2824 /* 2825 * traverse the list of net devices and compute the minimum 2826 * buffer size based upon the filter mask. 2827 */ 2828 list_for_each_entry(dev, &net->dev_base_head, dev_list) { 2829 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 2830 if_nlmsg_size(dev, 2831 ext_filter_mask)); 2832 } 2833 2834 return nlmsg_total_size(min_ifinfo_dump_size); 2835 } 2836 2837 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 2838 { 2839 int idx; 2840 int s_idx = cb->family; 2841 2842 if (s_idx == 0) 2843 s_idx = 1; 2844 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 2845 int type = cb->nlh->nlmsg_type-RTM_BASE; 2846 if (idx < s_idx || idx == PF_PACKET) 2847 continue; 2848 if (rtnl_msg_handlers[idx] == NULL || 2849 rtnl_msg_handlers[idx][type].dumpit == NULL) 2850 continue; 2851 if (idx > s_idx) { 2852 memset(&cb->args[0], 0, sizeof(cb->args)); 2853 cb->prev_seq = 0; 2854 cb->seq = 0; 2855 } 2856 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 2857 break; 2858 } 2859 cb->family = idx; 2860 2861 return skb->len; 2862 } 2863 2864 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 2865 unsigned int change, 2866 u32 event, gfp_t flags) 2867 { 2868 struct net *net = dev_net(dev); 2869 struct sk_buff *skb; 2870 int err = -ENOBUFS; 2871 size_t if_info_size; 2872 2873 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 2874 if (skb == NULL) 2875 goto errout; 2876 2877 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0, event); 2878 if (err < 0) { 2879 /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 2880 WARN_ON(err == -EMSGSIZE); 2881 kfree_skb(skb); 2882 goto errout; 2883 } 2884 return skb; 2885 errout: 2886 if (err < 0) 2887 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 2888 return NULL; 2889 } 2890 2891 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 2892 { 2893 struct net *net = dev_net(dev); 2894 2895 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 2896 } 2897 2898 static void rtmsg_ifinfo_event(int type, struct net_device *dev, 2899 unsigned int change, u32 event, 2900 gfp_t flags) 2901 { 2902 struct sk_buff *skb; 2903 2904 if (dev->reg_state != NETREG_REGISTERED) 2905 return; 2906 2907 skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags); 2908 if (skb) 2909 rtmsg_ifinfo_send(skb, dev, flags); 2910 } 2911 2912 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 2913 gfp_t flags) 2914 { 2915 rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags); 2916 } 2917 EXPORT_SYMBOL(rtmsg_ifinfo); 2918 2919 static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 2920 struct net_device *dev, 2921 u8 *addr, u16 vid, u32 pid, u32 seq, 2922 int type, unsigned int flags, 2923 int nlflags, u16 ndm_state) 2924 { 2925 struct nlmsghdr *nlh; 2926 struct ndmsg *ndm; 2927 2928 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 2929 if (!nlh) 2930 return -EMSGSIZE; 2931 2932 ndm = nlmsg_data(nlh); 2933 ndm->ndm_family = AF_BRIDGE; 2934 ndm->ndm_pad1 = 0; 2935 ndm->ndm_pad2 = 0; 2936 ndm->ndm_flags = flags; 2937 ndm->ndm_type = 0; 2938 ndm->ndm_ifindex = dev->ifindex; 2939 ndm->ndm_state = ndm_state; 2940 2941 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 2942 goto nla_put_failure; 2943 if (vid) 2944 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 2945 goto nla_put_failure; 2946 2947 nlmsg_end(skb, nlh); 2948 return 0; 2949 2950 nla_put_failure: 2951 nlmsg_cancel(skb, nlh); 2952 return -EMSGSIZE; 2953 } 2954 2955 static inline size_t rtnl_fdb_nlmsg_size(void) 2956 { 2957 return NLMSG_ALIGN(sizeof(struct ndmsg)) + 2958 nla_total_size(ETH_ALEN) + /* NDA_LLADDR */ 2959 nla_total_size(sizeof(u16)) + /* NDA_VLAN */ 2960 0; 2961 } 2962 2963 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type, 2964 u16 ndm_state) 2965 { 2966 struct net *net = dev_net(dev); 2967 struct sk_buff *skb; 2968 int err = -ENOBUFS; 2969 2970 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 2971 if (!skb) 2972 goto errout; 2973 2974 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 2975 0, 0, type, NTF_SELF, 0, ndm_state); 2976 if (err < 0) { 2977 kfree_skb(skb); 2978 goto errout; 2979 } 2980 2981 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 2982 return; 2983 errout: 2984 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 2985 } 2986 2987 /** 2988 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 2989 */ 2990 int ndo_dflt_fdb_add(struct ndmsg *ndm, 2991 struct nlattr *tb[], 2992 struct net_device *dev, 2993 const unsigned char *addr, u16 vid, 2994 u16 flags) 2995 { 2996 int err = -EINVAL; 2997 2998 /* If aging addresses are supported device will need to 2999 * implement its own handler for this. 3000 */ 3001 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3002 pr_info("%s: FDB only supports static addresses\n", dev->name); 3003 return err; 3004 } 3005 3006 if (vid) { 3007 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 3008 return err; 3009 } 3010 3011 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3012 err = dev_uc_add_excl(dev, addr); 3013 else if (is_multicast_ether_addr(addr)) 3014 err = dev_mc_add_excl(dev, addr); 3015 3016 /* Only return duplicate errors if NLM_F_EXCL is set */ 3017 if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3018 err = 0; 3019 3020 return err; 3021 } 3022 EXPORT_SYMBOL(ndo_dflt_fdb_add); 3023 3024 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid) 3025 { 3026 u16 vid = 0; 3027 3028 if (vlan_attr) { 3029 if (nla_len(vlan_attr) != sizeof(u16)) { 3030 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n"); 3031 return -EINVAL; 3032 } 3033 3034 vid = nla_get_u16(vlan_attr); 3035 3036 if (!vid || vid >= VLAN_VID_MASK) { 3037 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n", 3038 vid); 3039 return -EINVAL; 3040 } 3041 } 3042 *p_vid = vid; 3043 return 0; 3044 } 3045 3046 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, 3047 struct netlink_ext_ack *extack) 3048 { 3049 struct net *net = sock_net(skb->sk); 3050 struct ndmsg *ndm; 3051 struct nlattr *tb[NDA_MAX+1]; 3052 struct net_device *dev; 3053 u8 *addr; 3054 u16 vid; 3055 int err; 3056 3057 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 3058 if (err < 0) 3059 return err; 3060 3061 ndm = nlmsg_data(nlh); 3062 if (ndm->ndm_ifindex == 0) { 3063 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); 3064 return -EINVAL; 3065 } 3066 3067 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 3068 if (dev == NULL) { 3069 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); 3070 return -ENODEV; 3071 } 3072 3073 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3074 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); 3075 return -EINVAL; 3076 } 3077 3078 addr = nla_data(tb[NDA_LLADDR]); 3079 3080 err = fdb_vid_parse(tb[NDA_VLAN], &vid); 3081 if (err) 3082 return err; 3083 3084 err = -EOPNOTSUPP; 3085 3086 /* Support fdb on master device the net/bridge default case */ 3087 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 3088 (dev->priv_flags & IFF_BRIDGE_PORT)) { 3089 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3090 const struct net_device_ops *ops = br_dev->netdev_ops; 3091 3092 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 3093 nlh->nlmsg_flags); 3094 if (err) 3095 goto out; 3096 else 3097 ndm->ndm_flags &= ~NTF_MASTER; 3098 } 3099 3100 /* Embedded bridge, macvlan, and any other device support */ 3101 if ((ndm->ndm_flags & NTF_SELF)) { 3102 if (dev->netdev_ops->ndo_fdb_add) 3103 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 3104 vid, 3105 nlh->nlmsg_flags); 3106 else 3107 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 3108 nlh->nlmsg_flags); 3109 3110 if (!err) { 3111 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH, 3112 ndm->ndm_state); 3113 ndm->ndm_flags &= ~NTF_SELF; 3114 } 3115 } 3116 out: 3117 return err; 3118 } 3119 3120 /** 3121 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 3122 */ 3123 int ndo_dflt_fdb_del(struct ndmsg *ndm, 3124 struct nlattr *tb[], 3125 struct net_device *dev, 3126 const unsigned char *addr, u16 vid) 3127 { 3128 int err = -EINVAL; 3129 3130 /* If aging addresses are supported device will need to 3131 * implement its own handler for this. 3132 */ 3133 if (!(ndm->ndm_state & NUD_PERMANENT)) { 3134 pr_info("%s: FDB only supports static addresses\n", dev->name); 3135 return err; 3136 } 3137 3138 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3139 err = dev_uc_del(dev, addr); 3140 else if (is_multicast_ether_addr(addr)) 3141 err = dev_mc_del(dev, addr); 3142 3143 return err; 3144 } 3145 EXPORT_SYMBOL(ndo_dflt_fdb_del); 3146 3147 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh, 3148 struct netlink_ext_ack *extack) 3149 { 3150 struct net *net = sock_net(skb->sk); 3151 struct ndmsg *ndm; 3152 struct nlattr *tb[NDA_MAX+1]; 3153 struct net_device *dev; 3154 int err = -EINVAL; 3155 __u8 *addr; 3156 u16 vid; 3157 3158 if (!netlink_capable(skb, CAP_NET_ADMIN)) 3159 return -EPERM; 3160 3161 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL, extack); 3162 if (err < 0) 3163 return err; 3164 3165 ndm = nlmsg_data(nlh); 3166 if (ndm->ndm_ifindex == 0) { 3167 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); 3168 return -EINVAL; 3169 } 3170 3171 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 3172 if (dev == NULL) { 3173 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); 3174 return -ENODEV; 3175 } 3176 3177 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 3178 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); 3179 return -EINVAL; 3180 } 3181 3182 addr = nla_data(tb[NDA_LLADDR]); 3183 3184 err = fdb_vid_parse(tb[NDA_VLAN], &vid); 3185 if (err) 3186 return err; 3187 3188 err = -EOPNOTSUPP; 3189 3190 /* Support fdb on master device the net/bridge default case */ 3191 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 3192 (dev->priv_flags & IFF_BRIDGE_PORT)) { 3193 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3194 const struct net_device_ops *ops = br_dev->netdev_ops; 3195 3196 if (ops->ndo_fdb_del) 3197 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 3198 3199 if (err) 3200 goto out; 3201 else 3202 ndm->ndm_flags &= ~NTF_MASTER; 3203 } 3204 3205 /* Embedded bridge, macvlan, and any other device support */ 3206 if (ndm->ndm_flags & NTF_SELF) { 3207 if (dev->netdev_ops->ndo_fdb_del) 3208 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 3209 vid); 3210 else 3211 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 3212 3213 if (!err) { 3214 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH, 3215 ndm->ndm_state); 3216 ndm->ndm_flags &= ~NTF_SELF; 3217 } 3218 } 3219 out: 3220 return err; 3221 } 3222 3223 static int nlmsg_populate_fdb(struct sk_buff *skb, 3224 struct netlink_callback *cb, 3225 struct net_device *dev, 3226 int *idx, 3227 struct netdev_hw_addr_list *list) 3228 { 3229 struct netdev_hw_addr *ha; 3230 int err; 3231 u32 portid, seq; 3232 3233 portid = NETLINK_CB(cb->skb).portid; 3234 seq = cb->nlh->nlmsg_seq; 3235 3236 list_for_each_entry(ha, &list->list, list) { 3237 if (*idx < cb->args[2]) 3238 goto skip; 3239 3240 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 3241 portid, seq, 3242 RTM_NEWNEIGH, NTF_SELF, 3243 NLM_F_MULTI, NUD_PERMANENT); 3244 if (err < 0) 3245 return err; 3246 skip: 3247 *idx += 1; 3248 } 3249 return 0; 3250 } 3251 3252 /** 3253 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 3254 * @nlh: netlink message header 3255 * @dev: netdevice 3256 * 3257 * Default netdevice operation to dump the existing unicast address list. 3258 * Returns number of addresses from list put in skb. 3259 */ 3260 int ndo_dflt_fdb_dump(struct sk_buff *skb, 3261 struct netlink_callback *cb, 3262 struct net_device *dev, 3263 struct net_device *filter_dev, 3264 int *idx) 3265 { 3266 int err; 3267 3268 netif_addr_lock_bh(dev); 3269 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc); 3270 if (err) 3271 goto out; 3272 err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc); 3273 out: 3274 netif_addr_unlock_bh(dev); 3275 return err; 3276 } 3277 EXPORT_SYMBOL(ndo_dflt_fdb_dump); 3278 3279 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 3280 { 3281 struct net_device *dev; 3282 struct nlattr *tb[IFLA_MAX+1]; 3283 struct net_device *br_dev = NULL; 3284 const struct net_device_ops *ops = NULL; 3285 const struct net_device_ops *cops = NULL; 3286 struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 3287 struct net *net = sock_net(skb->sk); 3288 struct hlist_head *head; 3289 int brport_idx = 0; 3290 int br_idx = 0; 3291 int h, s_h; 3292 int idx = 0, s_idx; 3293 int err = 0; 3294 int fidx = 0; 3295 3296 err = nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, 3297 IFLA_MAX, ifla_policy, NULL); 3298 if (err < 0) { 3299 return -EINVAL; 3300 } else if (err == 0) { 3301 if (tb[IFLA_MASTER]) 3302 br_idx = nla_get_u32(tb[IFLA_MASTER]); 3303 } 3304 3305 brport_idx = ifm->ifi_index; 3306 3307 if (br_idx) { 3308 br_dev = __dev_get_by_index(net, br_idx); 3309 if (!br_dev) 3310 return -ENODEV; 3311 3312 ops = br_dev->netdev_ops; 3313 } 3314 3315 s_h = cb->args[0]; 3316 s_idx = cb->args[1]; 3317 3318 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 3319 idx = 0; 3320 head = &net->dev_index_head[h]; 3321 hlist_for_each_entry(dev, head, index_hlist) { 3322 3323 if (brport_idx && (dev->ifindex != brport_idx)) 3324 continue; 3325 3326 if (!br_idx) { /* user did not specify a specific bridge */ 3327 if (dev->priv_flags & IFF_BRIDGE_PORT) { 3328 br_dev = netdev_master_upper_dev_get(dev); 3329 cops = br_dev->netdev_ops; 3330 } 3331 } else { 3332 if (dev != br_dev && 3333 !(dev->priv_flags & IFF_BRIDGE_PORT)) 3334 continue; 3335 3336 if (br_dev != netdev_master_upper_dev_get(dev) && 3337 !(dev->priv_flags & IFF_EBRIDGE)) 3338 continue; 3339 cops = ops; 3340 } 3341 3342 if (idx < s_idx) 3343 goto cont; 3344 3345 if (dev->priv_flags & IFF_BRIDGE_PORT) { 3346 if (cops && cops->ndo_fdb_dump) { 3347 err = cops->ndo_fdb_dump(skb, cb, 3348 br_dev, dev, 3349 &fidx); 3350 if (err == -EMSGSIZE) 3351 goto out; 3352 } 3353 } 3354 3355 if (dev->netdev_ops->ndo_fdb_dump) 3356 err = dev->netdev_ops->ndo_fdb_dump(skb, cb, 3357 dev, NULL, 3358 &fidx); 3359 else 3360 err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, 3361 &fidx); 3362 if (err == -EMSGSIZE) 3363 goto out; 3364 3365 cops = NULL; 3366 3367 /* reset fdb offset to 0 for rest of the interfaces */ 3368 cb->args[2] = 0; 3369 fidx = 0; 3370 cont: 3371 idx++; 3372 } 3373 } 3374 3375 out: 3376 cb->args[0] = h; 3377 cb->args[1] = idx; 3378 cb->args[2] = fidx; 3379 3380 return skb->len; 3381 } 3382 3383 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 3384 unsigned int attrnum, unsigned int flag) 3385 { 3386 if (mask & flag) 3387 return nla_put_u8(skb, attrnum, !!(flags & flag)); 3388 return 0; 3389 } 3390 3391 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 3392 struct net_device *dev, u16 mode, 3393 u32 flags, u32 mask, int nlflags, 3394 u32 filter_mask, 3395 int (*vlan_fill)(struct sk_buff *skb, 3396 struct net_device *dev, 3397 u32 filter_mask)) 3398 { 3399 struct nlmsghdr *nlh; 3400 struct ifinfomsg *ifm; 3401 struct nlattr *br_afspec; 3402 struct nlattr *protinfo; 3403 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 3404 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3405 int err = 0; 3406 3407 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 3408 if (nlh == NULL) 3409 return -EMSGSIZE; 3410 3411 ifm = nlmsg_data(nlh); 3412 ifm->ifi_family = AF_BRIDGE; 3413 ifm->__ifi_pad = 0; 3414 ifm->ifi_type = dev->type; 3415 ifm->ifi_index = dev->ifindex; 3416 ifm->ifi_flags = dev_get_flags(dev); 3417 ifm->ifi_change = 0; 3418 3419 3420 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 3421 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 3422 nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 3423 (br_dev && 3424 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 3425 (dev->addr_len && 3426 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 3427 (dev->ifindex != dev_get_iflink(dev) && 3428 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3429 goto nla_put_failure; 3430 3431 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3432 if (!br_afspec) 3433 goto nla_put_failure; 3434 3435 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3436 nla_nest_cancel(skb, br_afspec); 3437 goto nla_put_failure; 3438 } 3439 3440 if (mode != BRIDGE_MODE_UNDEF) { 3441 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 3442 nla_nest_cancel(skb, br_afspec); 3443 goto nla_put_failure; 3444 } 3445 } 3446 if (vlan_fill) { 3447 err = vlan_fill(skb, dev, filter_mask); 3448 if (err) { 3449 nla_nest_cancel(skb, br_afspec); 3450 goto nla_put_failure; 3451 } 3452 } 3453 nla_nest_end(skb, br_afspec); 3454 3455 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 3456 if (!protinfo) 3457 goto nla_put_failure; 3458 3459 if (brport_nla_put_flag(skb, flags, mask, 3460 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 3461 brport_nla_put_flag(skb, flags, mask, 3462 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 3463 brport_nla_put_flag(skb, flags, mask, 3464 IFLA_BRPORT_FAST_LEAVE, 3465 BR_MULTICAST_FAST_LEAVE) || 3466 brport_nla_put_flag(skb, flags, mask, 3467 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 3468 brport_nla_put_flag(skb, flags, mask, 3469 IFLA_BRPORT_LEARNING, BR_LEARNING) || 3470 brport_nla_put_flag(skb, flags, mask, 3471 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 3472 brport_nla_put_flag(skb, flags, mask, 3473 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 3474 brport_nla_put_flag(skb, flags, mask, 3475 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 3476 nla_nest_cancel(skb, protinfo); 3477 goto nla_put_failure; 3478 } 3479 3480 nla_nest_end(skb, protinfo); 3481 3482 nlmsg_end(skb, nlh); 3483 return 0; 3484 nla_put_failure: 3485 nlmsg_cancel(skb, nlh); 3486 return err ? err : -EMSGSIZE; 3487 } 3488 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 3489 3490 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 3491 { 3492 struct net *net = sock_net(skb->sk); 3493 struct net_device *dev; 3494 int idx = 0; 3495 u32 portid = NETLINK_CB(cb->skb).portid; 3496 u32 seq = cb->nlh->nlmsg_seq; 3497 u32 filter_mask = 0; 3498 int err; 3499 3500 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 3501 struct nlattr *extfilt; 3502 3503 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 3504 IFLA_EXT_MASK); 3505 if (extfilt) { 3506 if (nla_len(extfilt) < sizeof(filter_mask)) 3507 return -EINVAL; 3508 3509 filter_mask = nla_get_u32(extfilt); 3510 } 3511 } 3512 3513 rcu_read_lock(); 3514 for_each_netdev_rcu(net, dev) { 3515 const struct net_device_ops *ops = dev->netdev_ops; 3516 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3517 3518 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 3519 if (idx >= cb->args[0]) { 3520 err = br_dev->netdev_ops->ndo_bridge_getlink( 3521 skb, portid, seq, dev, 3522 filter_mask, NLM_F_MULTI); 3523 if (err < 0 && err != -EOPNOTSUPP) { 3524 if (likely(skb->len)) 3525 break; 3526 3527 goto out_err; 3528 } 3529 } 3530 idx++; 3531 } 3532 3533 if (ops->ndo_bridge_getlink) { 3534 if (idx >= cb->args[0]) { 3535 err = ops->ndo_bridge_getlink(skb, portid, 3536 seq, dev, 3537 filter_mask, 3538 NLM_F_MULTI); 3539 if (err < 0 && err != -EOPNOTSUPP) { 3540 if (likely(skb->len)) 3541 break; 3542 3543 goto out_err; 3544 } 3545 } 3546 idx++; 3547 } 3548 } 3549 err = skb->len; 3550 out_err: 3551 rcu_read_unlock(); 3552 cb->args[0] = idx; 3553 3554 return err; 3555 } 3556 3557 static inline size_t bridge_nlmsg_size(void) 3558 { 3559 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3560 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3561 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3562 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 3563 + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 3564 + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 3565 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 3566 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 3567 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 3568 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 3569 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 3570 } 3571 3572 static int rtnl_bridge_notify(struct net_device *dev) 3573 { 3574 struct net *net = dev_net(dev); 3575 struct sk_buff *skb; 3576 int err = -EOPNOTSUPP; 3577 3578 if (!dev->netdev_ops->ndo_bridge_getlink) 3579 return 0; 3580 3581 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 3582 if (!skb) { 3583 err = -ENOMEM; 3584 goto errout; 3585 } 3586 3587 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 3588 if (err < 0) 3589 goto errout; 3590 3591 if (!skb->len) 3592 goto errout; 3593 3594 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 3595 return 0; 3596 errout: 3597 WARN_ON(err == -EMSGSIZE); 3598 kfree_skb(skb); 3599 if (err) 3600 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3601 return err; 3602 } 3603 3604 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, 3605 struct netlink_ext_ack *extack) 3606 { 3607 struct net *net = sock_net(skb->sk); 3608 struct ifinfomsg *ifm; 3609 struct net_device *dev; 3610 struct nlattr *br_spec, *attr = NULL; 3611 int rem, err = -EOPNOTSUPP; 3612 u16 flags = 0; 3613 bool have_flags = false; 3614 3615 if (nlmsg_len(nlh) < sizeof(*ifm)) 3616 return -EINVAL; 3617 3618 ifm = nlmsg_data(nlh); 3619 if (ifm->ifi_family != AF_BRIDGE) 3620 return -EPFNOSUPPORT; 3621 3622 dev = __dev_get_by_index(net, ifm->ifi_index); 3623 if (!dev) { 3624 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); 3625 return -ENODEV; 3626 } 3627 3628 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 3629 if (br_spec) { 3630 nla_for_each_nested(attr, br_spec, rem) { 3631 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 3632 if (nla_len(attr) < sizeof(flags)) 3633 return -EINVAL; 3634 3635 have_flags = true; 3636 flags = nla_get_u16(attr); 3637 break; 3638 } 3639 } 3640 } 3641 3642 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 3643 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3644 3645 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 3646 err = -EOPNOTSUPP; 3647 goto out; 3648 } 3649 3650 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 3651 if (err) 3652 goto out; 3653 3654 flags &= ~BRIDGE_FLAGS_MASTER; 3655 } 3656 3657 if ((flags & BRIDGE_FLAGS_SELF)) { 3658 if (!dev->netdev_ops->ndo_bridge_setlink) 3659 err = -EOPNOTSUPP; 3660 else 3661 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 3662 flags); 3663 if (!err) { 3664 flags &= ~BRIDGE_FLAGS_SELF; 3665 3666 /* Generate event to notify upper layer of bridge 3667 * change 3668 */ 3669 err = rtnl_bridge_notify(dev); 3670 } 3671 } 3672 3673 if (have_flags) 3674 memcpy(nla_data(attr), &flags, sizeof(flags)); 3675 out: 3676 return err; 3677 } 3678 3679 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, 3680 struct netlink_ext_ack *extack) 3681 { 3682 struct net *net = sock_net(skb->sk); 3683 struct ifinfomsg *ifm; 3684 struct net_device *dev; 3685 struct nlattr *br_spec, *attr = NULL; 3686 int rem, err = -EOPNOTSUPP; 3687 u16 flags = 0; 3688 bool have_flags = false; 3689 3690 if (nlmsg_len(nlh) < sizeof(*ifm)) 3691 return -EINVAL; 3692 3693 ifm = nlmsg_data(nlh); 3694 if (ifm->ifi_family != AF_BRIDGE) 3695 return -EPFNOSUPPORT; 3696 3697 dev = __dev_get_by_index(net, ifm->ifi_index); 3698 if (!dev) { 3699 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); 3700 return -ENODEV; 3701 } 3702 3703 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 3704 if (br_spec) { 3705 nla_for_each_nested(attr, br_spec, rem) { 3706 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 3707 if (nla_len(attr) < sizeof(flags)) 3708 return -EINVAL; 3709 3710 have_flags = true; 3711 flags = nla_get_u16(attr); 3712 break; 3713 } 3714 } 3715 } 3716 3717 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 3718 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3719 3720 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 3721 err = -EOPNOTSUPP; 3722 goto out; 3723 } 3724 3725 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 3726 if (err) 3727 goto out; 3728 3729 flags &= ~BRIDGE_FLAGS_MASTER; 3730 } 3731 3732 if ((flags & BRIDGE_FLAGS_SELF)) { 3733 if (!dev->netdev_ops->ndo_bridge_dellink) 3734 err = -EOPNOTSUPP; 3735 else 3736 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 3737 flags); 3738 3739 if (!err) { 3740 flags &= ~BRIDGE_FLAGS_SELF; 3741 3742 /* Generate event to notify upper layer of bridge 3743 * change 3744 */ 3745 err = rtnl_bridge_notify(dev); 3746 } 3747 } 3748 3749 if (have_flags) 3750 memcpy(nla_data(attr), &flags, sizeof(flags)); 3751 out: 3752 return err; 3753 } 3754 3755 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr) 3756 { 3757 return (mask & IFLA_STATS_FILTER_BIT(attrid)) && 3758 (!idxattr || idxattr == attrid); 3759 } 3760 3761 #define IFLA_OFFLOAD_XSTATS_FIRST (IFLA_OFFLOAD_XSTATS_UNSPEC + 1) 3762 static int rtnl_get_offload_stats_attr_size(int attr_id) 3763 { 3764 switch (attr_id) { 3765 case IFLA_OFFLOAD_XSTATS_CPU_HIT: 3766 return sizeof(struct rtnl_link_stats64); 3767 } 3768 3769 return 0; 3770 } 3771 3772 static int rtnl_get_offload_stats(struct sk_buff *skb, struct net_device *dev, 3773 int *prividx) 3774 { 3775 struct nlattr *attr = NULL; 3776 int attr_id, size; 3777 void *attr_data; 3778 int err; 3779 3780 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 3781 dev->netdev_ops->ndo_get_offload_stats)) 3782 return -ENODATA; 3783 3784 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 3785 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 3786 if (attr_id < *prividx) 3787 continue; 3788 3789 size = rtnl_get_offload_stats_attr_size(attr_id); 3790 if (!size) 3791 continue; 3792 3793 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 3794 continue; 3795 3796 attr = nla_reserve_64bit(skb, attr_id, size, 3797 IFLA_OFFLOAD_XSTATS_UNSPEC); 3798 if (!attr) 3799 goto nla_put_failure; 3800 3801 attr_data = nla_data(attr); 3802 memset(attr_data, 0, size); 3803 err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, 3804 attr_data); 3805 if (err) 3806 goto get_offload_stats_failure; 3807 } 3808 3809 if (!attr) 3810 return -ENODATA; 3811 3812 *prividx = 0; 3813 return 0; 3814 3815 nla_put_failure: 3816 err = -EMSGSIZE; 3817 get_offload_stats_failure: 3818 *prividx = attr_id; 3819 return err; 3820 } 3821 3822 static int rtnl_get_offload_stats_size(const struct net_device *dev) 3823 { 3824 int nla_size = 0; 3825 int attr_id; 3826 int size; 3827 3828 if (!(dev->netdev_ops && dev->netdev_ops->ndo_has_offload_stats && 3829 dev->netdev_ops->ndo_get_offload_stats)) 3830 return 0; 3831 3832 for (attr_id = IFLA_OFFLOAD_XSTATS_FIRST; 3833 attr_id <= IFLA_OFFLOAD_XSTATS_MAX; attr_id++) { 3834 if (!dev->netdev_ops->ndo_has_offload_stats(dev, attr_id)) 3835 continue; 3836 size = rtnl_get_offload_stats_attr_size(attr_id); 3837 nla_size += nla_total_size_64bit(size); 3838 } 3839 3840 if (nla_size != 0) 3841 nla_size += nla_total_size(0); 3842 3843 return nla_size; 3844 } 3845 3846 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev, 3847 int type, u32 pid, u32 seq, u32 change, 3848 unsigned int flags, unsigned int filter_mask, 3849 int *idxattr, int *prividx) 3850 { 3851 struct if_stats_msg *ifsm; 3852 struct nlmsghdr *nlh; 3853 struct nlattr *attr; 3854 int s_prividx = *prividx; 3855 int err; 3856 3857 ASSERT_RTNL(); 3858 3859 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags); 3860 if (!nlh) 3861 return -EMSGSIZE; 3862 3863 ifsm = nlmsg_data(nlh); 3864 ifsm->ifindex = dev->ifindex; 3865 ifsm->filter_mask = filter_mask; 3866 3867 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) { 3868 struct rtnl_link_stats64 *sp; 3869 3870 attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64, 3871 sizeof(struct rtnl_link_stats64), 3872 IFLA_STATS_UNSPEC); 3873 if (!attr) 3874 goto nla_put_failure; 3875 3876 sp = nla_data(attr); 3877 dev_get_stats(dev, sp); 3878 } 3879 3880 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) { 3881 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 3882 3883 if (ops && ops->fill_linkxstats) { 3884 *idxattr = IFLA_STATS_LINK_XSTATS; 3885 attr = nla_nest_start(skb, 3886 IFLA_STATS_LINK_XSTATS); 3887 if (!attr) 3888 goto nla_put_failure; 3889 3890 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 3891 nla_nest_end(skb, attr); 3892 if (err) 3893 goto nla_put_failure; 3894 *idxattr = 0; 3895 } 3896 } 3897 3898 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 3899 *idxattr)) { 3900 const struct rtnl_link_ops *ops = NULL; 3901 const struct net_device *master; 3902 3903 master = netdev_master_upper_dev_get(dev); 3904 if (master) 3905 ops = master->rtnl_link_ops; 3906 if (ops && ops->fill_linkxstats) { 3907 *idxattr = IFLA_STATS_LINK_XSTATS_SLAVE; 3908 attr = nla_nest_start(skb, 3909 IFLA_STATS_LINK_XSTATS_SLAVE); 3910 if (!attr) 3911 goto nla_put_failure; 3912 3913 err = ops->fill_linkxstats(skb, dev, prividx, *idxattr); 3914 nla_nest_end(skb, attr); 3915 if (err) 3916 goto nla_put_failure; 3917 *idxattr = 0; 3918 } 3919 } 3920 3921 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 3922 *idxattr)) { 3923 *idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS; 3924 attr = nla_nest_start(skb, IFLA_STATS_LINK_OFFLOAD_XSTATS); 3925 if (!attr) 3926 goto nla_put_failure; 3927 3928 err = rtnl_get_offload_stats(skb, dev, prividx); 3929 if (err == -ENODATA) 3930 nla_nest_cancel(skb, attr); 3931 else 3932 nla_nest_end(skb, attr); 3933 3934 if (err && err != -ENODATA) 3935 goto nla_put_failure; 3936 *idxattr = 0; 3937 } 3938 3939 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) { 3940 struct rtnl_af_ops *af_ops; 3941 3942 *idxattr = IFLA_STATS_AF_SPEC; 3943 attr = nla_nest_start(skb, IFLA_STATS_AF_SPEC); 3944 if (!attr) 3945 goto nla_put_failure; 3946 3947 list_for_each_entry(af_ops, &rtnl_af_ops, list) { 3948 if (af_ops->fill_stats_af) { 3949 struct nlattr *af; 3950 int err; 3951 3952 af = nla_nest_start(skb, af_ops->family); 3953 if (!af) 3954 goto nla_put_failure; 3955 3956 err = af_ops->fill_stats_af(skb, dev); 3957 3958 if (err == -ENODATA) 3959 nla_nest_cancel(skb, af); 3960 else if (err < 0) 3961 goto nla_put_failure; 3962 3963 nla_nest_end(skb, af); 3964 } 3965 } 3966 3967 nla_nest_end(skb, attr); 3968 3969 *idxattr = 0; 3970 } 3971 3972 nlmsg_end(skb, nlh); 3973 3974 return 0; 3975 3976 nla_put_failure: 3977 /* not a multi message or no progress mean a real error */ 3978 if (!(flags & NLM_F_MULTI) || s_prividx == *prividx) 3979 nlmsg_cancel(skb, nlh); 3980 else 3981 nlmsg_end(skb, nlh); 3982 3983 return -EMSGSIZE; 3984 } 3985 3986 static size_t if_nlmsg_stats_size(const struct net_device *dev, 3987 u32 filter_mask) 3988 { 3989 size_t size = 0; 3990 3991 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0)) 3992 size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64)); 3993 3994 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) { 3995 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 3996 int attr = IFLA_STATS_LINK_XSTATS; 3997 3998 if (ops && ops->get_linkxstats_size) { 3999 size += nla_total_size(ops->get_linkxstats_size(dev, 4000 attr)); 4001 /* for IFLA_STATS_LINK_XSTATS */ 4002 size += nla_total_size(0); 4003 } 4004 } 4005 4006 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) { 4007 struct net_device *_dev = (struct net_device *)dev; 4008 const struct rtnl_link_ops *ops = NULL; 4009 const struct net_device *master; 4010 4011 /* netdev_master_upper_dev_get can't take const */ 4012 master = netdev_master_upper_dev_get(_dev); 4013 if (master) 4014 ops = master->rtnl_link_ops; 4015 if (ops && ops->get_linkxstats_size) { 4016 int attr = IFLA_STATS_LINK_XSTATS_SLAVE; 4017 4018 size += nla_total_size(ops->get_linkxstats_size(dev, 4019 attr)); 4020 /* for IFLA_STATS_LINK_XSTATS_SLAVE */ 4021 size += nla_total_size(0); 4022 } 4023 } 4024 4025 if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) 4026 size += rtnl_get_offload_stats_size(dev); 4027 4028 if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) { 4029 struct rtnl_af_ops *af_ops; 4030 4031 /* for IFLA_STATS_AF_SPEC */ 4032 size += nla_total_size(0); 4033 4034 list_for_each_entry(af_ops, &rtnl_af_ops, list) { 4035 if (af_ops->get_stats_af_size) { 4036 size += nla_total_size( 4037 af_ops->get_stats_af_size(dev)); 4038 4039 /* for AF_* */ 4040 size += nla_total_size(0); 4041 } 4042 } 4043 } 4044 4045 return size; 4046 } 4047 4048 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh, 4049 struct netlink_ext_ack *extack) 4050 { 4051 struct net *net = sock_net(skb->sk); 4052 struct net_device *dev = NULL; 4053 int idxattr = 0, prividx = 0; 4054 struct if_stats_msg *ifsm; 4055 struct sk_buff *nskb; 4056 u32 filter_mask; 4057 int err; 4058 4059 if (nlmsg_len(nlh) < sizeof(*ifsm)) 4060 return -EINVAL; 4061 4062 ifsm = nlmsg_data(nlh); 4063 if (ifsm->ifindex > 0) 4064 dev = __dev_get_by_index(net, ifsm->ifindex); 4065 else 4066 return -EINVAL; 4067 4068 if (!dev) 4069 return -ENODEV; 4070 4071 filter_mask = ifsm->filter_mask; 4072 if (!filter_mask) 4073 return -EINVAL; 4074 4075 nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL); 4076 if (!nskb) 4077 return -ENOBUFS; 4078 4079 err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS, 4080 NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 4081 0, filter_mask, &idxattr, &prividx); 4082 if (err < 0) { 4083 /* -EMSGSIZE implies BUG in if_nlmsg_stats_size */ 4084 WARN_ON(err == -EMSGSIZE); 4085 kfree_skb(nskb); 4086 } else { 4087 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 4088 } 4089 4090 return err; 4091 } 4092 4093 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb) 4094 { 4095 int h, s_h, err, s_idx, s_idxattr, s_prividx; 4096 struct net *net = sock_net(skb->sk); 4097 unsigned int flags = NLM_F_MULTI; 4098 struct if_stats_msg *ifsm; 4099 struct hlist_head *head; 4100 struct net_device *dev; 4101 u32 filter_mask = 0; 4102 int idx = 0; 4103 4104 s_h = cb->args[0]; 4105 s_idx = cb->args[1]; 4106 s_idxattr = cb->args[2]; 4107 s_prividx = cb->args[3]; 4108 4109 cb->seq = net->dev_base_seq; 4110 4111 if (nlmsg_len(cb->nlh) < sizeof(*ifsm)) 4112 return -EINVAL; 4113 4114 ifsm = nlmsg_data(cb->nlh); 4115 filter_mask = ifsm->filter_mask; 4116 if (!filter_mask) 4117 return -EINVAL; 4118 4119 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 4120 idx = 0; 4121 head = &net->dev_index_head[h]; 4122 hlist_for_each_entry(dev, head, index_hlist) { 4123 if (idx < s_idx) 4124 goto cont; 4125 err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 4126 NETLINK_CB(cb->skb).portid, 4127 cb->nlh->nlmsg_seq, 0, 4128 flags, filter_mask, 4129 &s_idxattr, &s_prividx); 4130 /* If we ran out of room on the first message, 4131 * we're in trouble 4132 */ 4133 WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 4134 4135 if (err < 0) 4136 goto out; 4137 s_prividx = 0; 4138 s_idxattr = 0; 4139 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 4140 cont: 4141 idx++; 4142 } 4143 } 4144 out: 4145 cb->args[3] = s_prividx; 4146 cb->args[2] = s_idxattr; 4147 cb->args[1] = idx; 4148 cb->args[0] = h; 4149 4150 return skb->len; 4151 } 4152 4153 /* Process one rtnetlink message. */ 4154 4155 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 4156 struct netlink_ext_ack *extack) 4157 { 4158 struct net *net = sock_net(skb->sk); 4159 rtnl_doit_func doit; 4160 int kind; 4161 int family; 4162 int type; 4163 int err; 4164 4165 type = nlh->nlmsg_type; 4166 if (type > RTM_MAX) 4167 return -EOPNOTSUPP; 4168 4169 type -= RTM_BASE; 4170 4171 /* All the messages must have at least 1 byte length */ 4172 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 4173 return 0; 4174 4175 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 4176 kind = type&3; 4177 4178 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 4179 return -EPERM; 4180 4181 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 4182 struct sock *rtnl; 4183 rtnl_dumpit_func dumpit; 4184 rtnl_calcit_func calcit; 4185 u16 min_dump_alloc = 0; 4186 4187 dumpit = rtnl_get_dumpit(family, type); 4188 if (dumpit == NULL) 4189 return -EOPNOTSUPP; 4190 calcit = rtnl_get_calcit(family, type); 4191 if (calcit) 4192 min_dump_alloc = calcit(skb, nlh); 4193 4194 __rtnl_unlock(); 4195 rtnl = net->rtnl; 4196 { 4197 struct netlink_dump_control c = { 4198 .dump = dumpit, 4199 .min_dump_alloc = min_dump_alloc, 4200 }; 4201 err = netlink_dump_start(rtnl, skb, nlh, &c); 4202 } 4203 rtnl_lock(); 4204 return err; 4205 } 4206 4207 doit = rtnl_get_doit(family, type); 4208 if (doit == NULL) 4209 return -EOPNOTSUPP; 4210 4211 return doit(skb, nlh, extack); 4212 } 4213 4214 static void rtnetlink_rcv(struct sk_buff *skb) 4215 { 4216 rtnl_lock(); 4217 netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 4218 rtnl_unlock(); 4219 } 4220 4221 static int rtnetlink_bind(struct net *net, int group) 4222 { 4223 switch (group) { 4224 case RTNLGRP_IPV4_MROUTE_R: 4225 case RTNLGRP_IPV6_MROUTE_R: 4226 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 4227 return -EPERM; 4228 break; 4229 } 4230 return 0; 4231 } 4232 4233 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 4234 { 4235 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4236 4237 switch (event) { 4238 case NETDEV_REBOOT: 4239 case NETDEV_CHANGENAME: 4240 case NETDEV_FEAT_CHANGE: 4241 case NETDEV_BONDING_FAILOVER: 4242 case NETDEV_NOTIFY_PEERS: 4243 case NETDEV_RESEND_IGMP: 4244 case NETDEV_CHANGEINFODATA: 4245 rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event), 4246 GFP_KERNEL); 4247 break; 4248 default: 4249 break; 4250 } 4251 return NOTIFY_DONE; 4252 } 4253 4254 static struct notifier_block rtnetlink_dev_notifier = { 4255 .notifier_call = rtnetlink_event, 4256 }; 4257 4258 4259 static int __net_init rtnetlink_net_init(struct net *net) 4260 { 4261 struct sock *sk; 4262 struct netlink_kernel_cfg cfg = { 4263 .groups = RTNLGRP_MAX, 4264 .input = rtnetlink_rcv, 4265 .cb_mutex = &rtnl_mutex, 4266 .flags = NL_CFG_F_NONROOT_RECV, 4267 .bind = rtnetlink_bind, 4268 }; 4269 4270 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 4271 if (!sk) 4272 return -ENOMEM; 4273 net->rtnl = sk; 4274 return 0; 4275 } 4276 4277 static void __net_exit rtnetlink_net_exit(struct net *net) 4278 { 4279 netlink_kernel_release(net->rtnl); 4280 net->rtnl = NULL; 4281 } 4282 4283 static struct pernet_operations rtnetlink_net_ops = { 4284 .init = rtnetlink_net_init, 4285 .exit = rtnetlink_net_exit, 4286 }; 4287 4288 void __init rtnetlink_init(void) 4289 { 4290 if (register_pernet_subsys(&rtnetlink_net_ops)) 4291 panic("rtnetlink_init: cannot initialize rtnetlink\n"); 4292 4293 register_netdevice_notifier(&rtnetlink_dev_notifier); 4294 4295 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 4296 rtnl_dump_ifinfo, rtnl_calcit); 4297 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); 4298 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); 4299 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); 4300 4301 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); 4302 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); 4303 rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, NULL); 4304 4305 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL); 4306 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL); 4307 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL); 4308 4309 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL); 4310 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL); 4311 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL); 4312 4313 rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump, 4314 NULL); 4315 } 4316