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