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 809 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b) 810 { 811 memcpy(v, b, sizeof(*b)); 812 } 813 814 /* All VF info */ 815 static inline int rtnl_vfinfo_size(const struct net_device *dev, 816 u32 ext_filter_mask) 817 { 818 if (dev->dev.parent && dev_is_pci(dev->dev.parent) && 819 (ext_filter_mask & RTEXT_FILTER_VF)) { 820 int num_vfs = dev_num_vf(dev->dev.parent); 821 size_t size = nla_total_size(sizeof(struct nlattr)); 822 size += nla_total_size(num_vfs * sizeof(struct nlattr)); 823 size += num_vfs * 824 (nla_total_size(sizeof(struct ifla_vf_mac)) + 825 nla_total_size(sizeof(struct ifla_vf_vlan)) + 826 nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 827 nla_total_size(sizeof(struct ifla_vf_rate)) + 828 nla_total_size(sizeof(struct ifla_vf_link_state)) + 829 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) + 830 /* IFLA_VF_STATS_RX_PACKETS */ 831 nla_total_size(sizeof(__u64)) + 832 /* IFLA_VF_STATS_TX_PACKETS */ 833 nla_total_size(sizeof(__u64)) + 834 /* IFLA_VF_STATS_RX_BYTES */ 835 nla_total_size(sizeof(__u64)) + 836 /* IFLA_VF_STATS_TX_BYTES */ 837 nla_total_size(sizeof(__u64)) + 838 /* IFLA_VF_STATS_BROADCAST */ 839 nla_total_size(sizeof(__u64)) + 840 /* IFLA_VF_STATS_MULTICAST */ 841 nla_total_size(sizeof(__u64)) + 842 nla_total_size(sizeof(struct ifla_vf_trust))); 843 return size; 844 } else 845 return 0; 846 } 847 848 static size_t rtnl_port_size(const struct net_device *dev, 849 u32 ext_filter_mask) 850 { 851 size_t port_size = nla_total_size(4) /* PORT_VF */ 852 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 853 + nla_total_size(sizeof(struct ifla_port_vsi)) 854 /* PORT_VSI_TYPE */ 855 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 856 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 857 + nla_total_size(1) /* PROT_VDP_REQUEST */ 858 + nla_total_size(2); /* PORT_VDP_RESPONSE */ 859 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 860 size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 861 + port_size; 862 size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 863 + port_size; 864 865 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 866 !(ext_filter_mask & RTEXT_FILTER_VF)) 867 return 0; 868 if (dev_num_vf(dev->dev.parent)) 869 return port_self_size + vf_ports_size + 870 vf_port_size * dev_num_vf(dev->dev.parent); 871 else 872 return port_self_size; 873 } 874 875 static noinline size_t if_nlmsg_size(const struct net_device *dev, 876 u32 ext_filter_mask) 877 { 878 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 879 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 880 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 881 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 882 + nla_total_size(sizeof(struct rtnl_link_ifmap)) 883 + nla_total_size(sizeof(struct rtnl_link_stats)) 884 + nla_total_size(sizeof(struct rtnl_link_stats64)) 885 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 886 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 887 + nla_total_size(4) /* IFLA_TXQLEN */ 888 + nla_total_size(4) /* IFLA_WEIGHT */ 889 + nla_total_size(4) /* IFLA_MTU */ 890 + nla_total_size(4) /* IFLA_LINK */ 891 + nla_total_size(4) /* IFLA_MASTER */ 892 + nla_total_size(1) /* IFLA_CARRIER */ 893 + nla_total_size(4) /* IFLA_PROMISCUITY */ 894 + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */ 895 + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */ 896 + nla_total_size(1) /* IFLA_OPERSTATE */ 897 + nla_total_size(1) /* IFLA_LINKMODE */ 898 + nla_total_size(4) /* IFLA_CARRIER_CHANGES */ 899 + nla_total_size(4) /* IFLA_LINK_NETNSID */ 900 + nla_total_size(ext_filter_mask 901 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 902 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 903 + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 904 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 905 + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */ 906 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */ 907 + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */ 908 + nla_total_size(1); /* IFLA_PROTO_DOWN */ 909 910 } 911 912 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 913 { 914 struct nlattr *vf_ports; 915 struct nlattr *vf_port; 916 int vf; 917 int err; 918 919 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 920 if (!vf_ports) 921 return -EMSGSIZE; 922 923 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 924 vf_port = nla_nest_start(skb, IFLA_VF_PORT); 925 if (!vf_port) 926 goto nla_put_failure; 927 if (nla_put_u32(skb, IFLA_PORT_VF, vf)) 928 goto nla_put_failure; 929 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 930 if (err == -EMSGSIZE) 931 goto nla_put_failure; 932 if (err) { 933 nla_nest_cancel(skb, vf_port); 934 continue; 935 } 936 nla_nest_end(skb, vf_port); 937 } 938 939 nla_nest_end(skb, vf_ports); 940 941 return 0; 942 943 nla_put_failure: 944 nla_nest_cancel(skb, vf_ports); 945 return -EMSGSIZE; 946 } 947 948 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 949 { 950 struct nlattr *port_self; 951 int err; 952 953 port_self = nla_nest_start(skb, IFLA_PORT_SELF); 954 if (!port_self) 955 return -EMSGSIZE; 956 957 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 958 if (err) { 959 nla_nest_cancel(skb, port_self); 960 return (err == -EMSGSIZE) ? err : 0; 961 } 962 963 nla_nest_end(skb, port_self); 964 965 return 0; 966 } 967 968 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev, 969 u32 ext_filter_mask) 970 { 971 int err; 972 973 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent || 974 !(ext_filter_mask & RTEXT_FILTER_VF)) 975 return 0; 976 977 err = rtnl_port_self_fill(skb, dev); 978 if (err) 979 return err; 980 981 if (dev_num_vf(dev->dev.parent)) { 982 err = rtnl_vf_ports_fill(skb, dev); 983 if (err) 984 return err; 985 } 986 987 return 0; 988 } 989 990 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev) 991 { 992 int err; 993 struct netdev_phys_item_id ppid; 994 995 err = dev_get_phys_port_id(dev, &ppid); 996 if (err) { 997 if (err == -EOPNOTSUPP) 998 return 0; 999 return err; 1000 } 1001 1002 if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id)) 1003 return -EMSGSIZE; 1004 1005 return 0; 1006 } 1007 1008 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev) 1009 { 1010 char name[IFNAMSIZ]; 1011 int err; 1012 1013 err = dev_get_phys_port_name(dev, name, sizeof(name)); 1014 if (err) { 1015 if (err == -EOPNOTSUPP) 1016 return 0; 1017 return err; 1018 } 1019 1020 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name)) 1021 return -EMSGSIZE; 1022 1023 return 0; 1024 } 1025 1026 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 1027 { 1028 int err; 1029 struct switchdev_attr attr = { 1030 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID, 1031 .flags = SWITCHDEV_F_NO_RECURSE, 1032 }; 1033 1034 err = switchdev_port_attr_get(dev, &attr); 1035 if (err) { 1036 if (err == -EOPNOTSUPP) 1037 return 0; 1038 return err; 1039 } 1040 1041 if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len, 1042 attr.u.ppid.id)) 1043 return -EMSGSIZE; 1044 1045 return 0; 1046 } 1047 1048 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 1049 int type, u32 pid, u32 seq, u32 change, 1050 unsigned int flags, u32 ext_filter_mask) 1051 { 1052 struct ifinfomsg *ifm; 1053 struct nlmsghdr *nlh; 1054 struct rtnl_link_stats64 temp; 1055 const struct rtnl_link_stats64 *stats; 1056 struct nlattr *attr, *af_spec; 1057 struct rtnl_af_ops *af_ops; 1058 struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 1059 1060 ASSERT_RTNL(); 1061 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 1062 if (nlh == NULL) 1063 return -EMSGSIZE; 1064 1065 ifm = nlmsg_data(nlh); 1066 ifm->ifi_family = AF_UNSPEC; 1067 ifm->__ifi_pad = 0; 1068 ifm->ifi_type = dev->type; 1069 ifm->ifi_index = dev->ifindex; 1070 ifm->ifi_flags = dev_get_flags(dev); 1071 ifm->ifi_change = change; 1072 1073 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 1074 nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) || 1075 nla_put_u8(skb, IFLA_OPERSTATE, 1076 netif_running(dev) ? dev->operstate : IF_OPER_DOWN) || 1077 nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) || 1078 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 1079 nla_put_u32(skb, IFLA_GROUP, dev->group) || 1080 nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) || 1081 nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) || 1082 #ifdef CONFIG_RPS 1083 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 1084 #endif 1085 (dev->ifindex != dev_get_iflink(dev) && 1086 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) || 1087 (upper_dev && 1088 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || 1089 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1090 (dev->qdisc && 1091 nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) || 1092 (dev->ifalias && 1093 nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) || 1094 nla_put_u32(skb, IFLA_CARRIER_CHANGES, 1095 atomic_read(&dev->carrier_changes)) || 1096 nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down)) 1097 goto nla_put_failure; 1098 1099 if (1) { 1100 struct rtnl_link_ifmap map = { 1101 .mem_start = dev->mem_start, 1102 .mem_end = dev->mem_end, 1103 .base_addr = dev->base_addr, 1104 .irq = dev->irq, 1105 .dma = dev->dma, 1106 .port = dev->if_port, 1107 }; 1108 if (nla_put(skb, IFLA_MAP, sizeof(map), &map)) 1109 goto nla_put_failure; 1110 } 1111 1112 if (dev->addr_len) { 1113 if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) || 1114 nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast)) 1115 goto nla_put_failure; 1116 } 1117 1118 if (rtnl_phys_port_id_fill(skb, dev)) 1119 goto nla_put_failure; 1120 1121 if (rtnl_phys_port_name_fill(skb, dev)) 1122 goto nla_put_failure; 1123 1124 if (rtnl_phys_switch_id_fill(skb, dev)) 1125 goto nla_put_failure; 1126 1127 attr = nla_reserve(skb, IFLA_STATS, 1128 sizeof(struct rtnl_link_stats)); 1129 if (attr == NULL) 1130 goto nla_put_failure; 1131 1132 stats = dev_get_stats(dev, &temp); 1133 copy_rtnl_link_stats(nla_data(attr), stats); 1134 1135 attr = nla_reserve(skb, IFLA_STATS64, 1136 sizeof(struct rtnl_link_stats64)); 1137 if (attr == NULL) 1138 goto nla_put_failure; 1139 copy_rtnl_link_stats64(nla_data(attr), stats); 1140 1141 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) && 1142 nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent))) 1143 goto nla_put_failure; 1144 1145 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent 1146 && (ext_filter_mask & RTEXT_FILTER_VF)) { 1147 int i; 1148 1149 struct nlattr *vfinfo, *vf, *vfstats; 1150 int num_vfs = dev_num_vf(dev->dev.parent); 1151 1152 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 1153 if (!vfinfo) 1154 goto nla_put_failure; 1155 for (i = 0; i < num_vfs; i++) { 1156 struct ifla_vf_info ivi; 1157 struct ifla_vf_mac vf_mac; 1158 struct ifla_vf_vlan vf_vlan; 1159 struct ifla_vf_rate vf_rate; 1160 struct ifla_vf_tx_rate vf_tx_rate; 1161 struct ifla_vf_spoofchk vf_spoofchk; 1162 struct ifla_vf_link_state vf_linkstate; 1163 struct ifla_vf_rss_query_en vf_rss_query_en; 1164 struct ifla_vf_stats vf_stats; 1165 struct ifla_vf_trust vf_trust; 1166 1167 /* 1168 * Not all SR-IOV capable drivers support the 1169 * spoofcheck and "RSS query enable" query. Preset to 1170 * -1 so the user space tool can detect that the driver 1171 * didn't report anything. 1172 */ 1173 ivi.spoofchk = -1; 1174 ivi.rss_query_en = -1; 1175 ivi.trusted = -1; 1176 memset(ivi.mac, 0, sizeof(ivi.mac)); 1177 /* The default value for VF link state is "auto" 1178 * IFLA_VF_LINK_STATE_AUTO which equals zero 1179 */ 1180 ivi.linkstate = 0; 1181 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) 1182 break; 1183 vf_mac.vf = 1184 vf_vlan.vf = 1185 vf_rate.vf = 1186 vf_tx_rate.vf = 1187 vf_spoofchk.vf = 1188 vf_linkstate.vf = 1189 vf_rss_query_en.vf = 1190 vf_trust.vf = ivi.vf; 1191 1192 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1193 vf_vlan.vlan = ivi.vlan; 1194 vf_vlan.qos = ivi.qos; 1195 vf_tx_rate.rate = ivi.max_tx_rate; 1196 vf_rate.min_tx_rate = ivi.min_tx_rate; 1197 vf_rate.max_tx_rate = ivi.max_tx_rate; 1198 vf_spoofchk.setting = ivi.spoofchk; 1199 vf_linkstate.link_state = ivi.linkstate; 1200 vf_rss_query_en.setting = ivi.rss_query_en; 1201 vf_trust.setting = ivi.trusted; 1202 vf = nla_nest_start(skb, IFLA_VF_INFO); 1203 if (!vf) { 1204 nla_nest_cancel(skb, vfinfo); 1205 goto nla_put_failure; 1206 } 1207 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) || 1208 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) || 1209 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate), 1210 &vf_rate) || 1211 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 1212 &vf_tx_rate) || 1213 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1214 &vf_spoofchk) || 1215 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1216 &vf_linkstate) || 1217 nla_put(skb, IFLA_VF_RSS_QUERY_EN, 1218 sizeof(vf_rss_query_en), 1219 &vf_rss_query_en) || 1220 nla_put(skb, IFLA_VF_TRUST, 1221 sizeof(vf_trust), &vf_trust)) 1222 goto nla_put_failure; 1223 memset(&vf_stats, 0, sizeof(vf_stats)); 1224 if (dev->netdev_ops->ndo_get_vf_stats) 1225 dev->netdev_ops->ndo_get_vf_stats(dev, i, 1226 &vf_stats); 1227 vfstats = nla_nest_start(skb, IFLA_VF_STATS); 1228 if (!vfstats) { 1229 nla_nest_cancel(skb, vf); 1230 nla_nest_cancel(skb, vfinfo); 1231 goto nla_put_failure; 1232 } 1233 if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS, 1234 vf_stats.rx_packets) || 1235 nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS, 1236 vf_stats.tx_packets) || 1237 nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES, 1238 vf_stats.rx_bytes) || 1239 nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES, 1240 vf_stats.tx_bytes) || 1241 nla_put_u64(skb, IFLA_VF_STATS_BROADCAST, 1242 vf_stats.broadcast) || 1243 nla_put_u64(skb, IFLA_VF_STATS_MULTICAST, 1244 vf_stats.multicast)) 1245 goto nla_put_failure; 1246 nla_nest_end(skb, vfstats); 1247 nla_nest_end(skb, vf); 1248 } 1249 nla_nest_end(skb, vfinfo); 1250 } 1251 1252 if (rtnl_port_fill(skb, dev, ext_filter_mask)) 1253 goto nla_put_failure; 1254 1255 if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) { 1256 if (rtnl_link_fill(skb, dev) < 0) 1257 goto nla_put_failure; 1258 } 1259 1260 if (dev->rtnl_link_ops && 1261 dev->rtnl_link_ops->get_link_net) { 1262 struct net *link_net = dev->rtnl_link_ops->get_link_net(dev); 1263 1264 if (!net_eq(dev_net(dev), link_net)) { 1265 int id = peernet2id_alloc(dev_net(dev), link_net); 1266 1267 if (nla_put_s32(skb, IFLA_LINK_NETNSID, id)) 1268 goto nla_put_failure; 1269 } 1270 } 1271 1272 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) 1273 goto nla_put_failure; 1274 1275 list_for_each_entry(af_ops, &rtnl_af_ops, list) { 1276 if (af_ops->fill_link_af) { 1277 struct nlattr *af; 1278 int err; 1279 1280 if (!(af = nla_nest_start(skb, af_ops->family))) 1281 goto nla_put_failure; 1282 1283 err = af_ops->fill_link_af(skb, dev, ext_filter_mask); 1284 1285 /* 1286 * Caller may return ENODATA to indicate that there 1287 * was no data to be dumped. This is not an error, it 1288 * means we should trim the attribute header and 1289 * continue. 1290 */ 1291 if (err == -ENODATA) 1292 nla_nest_cancel(skb, af); 1293 else if (err < 0) 1294 goto nla_put_failure; 1295 1296 nla_nest_end(skb, af); 1297 } 1298 } 1299 1300 nla_nest_end(skb, af_spec); 1301 1302 nlmsg_end(skb, nlh); 1303 return 0; 1304 1305 nla_put_failure: 1306 nlmsg_cancel(skb, nlh); 1307 return -EMSGSIZE; 1308 } 1309 1310 static const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1311 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1312 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1313 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1314 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1315 [IFLA_MTU] = { .type = NLA_U32 }, 1316 [IFLA_LINK] = { .type = NLA_U32 }, 1317 [IFLA_MASTER] = { .type = NLA_U32 }, 1318 [IFLA_CARRIER] = { .type = NLA_U8 }, 1319 [IFLA_TXQLEN] = { .type = NLA_U32 }, 1320 [IFLA_WEIGHT] = { .type = NLA_U32 }, 1321 [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1322 [IFLA_LINKMODE] = { .type = NLA_U8 }, 1323 [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1324 [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1325 [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 1326 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, 1327 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1328 [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1329 [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1330 [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1331 [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1332 [IFLA_PROMISCUITY] = { .type = NLA_U32 }, 1333 [IFLA_NUM_TX_QUEUES] = { .type = NLA_U32 }, 1334 [IFLA_NUM_RX_QUEUES] = { .type = NLA_U32 }, 1335 [IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1336 [IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */ 1337 [IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN }, 1338 [IFLA_LINK_NETNSID] = { .type = NLA_S32 }, 1339 [IFLA_PROTO_DOWN] = { .type = NLA_U8 }, 1340 }; 1341 1342 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1343 [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1344 [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1345 [IFLA_INFO_SLAVE_KIND] = { .type = NLA_STRING }, 1346 [IFLA_INFO_SLAVE_DATA] = { .type = NLA_NESTED }, 1347 }; 1348 1349 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1350 [IFLA_VF_MAC] = { .len = sizeof(struct ifla_vf_mac) }, 1351 [IFLA_VF_VLAN] = { .len = sizeof(struct ifla_vf_vlan) }, 1352 [IFLA_VF_TX_RATE] = { .len = sizeof(struct ifla_vf_tx_rate) }, 1353 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1354 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1355 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 1356 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) }, 1357 [IFLA_VF_STATS] = { .type = NLA_NESTED }, 1358 [IFLA_VF_TRUST] = { .len = sizeof(struct ifla_vf_trust) }, 1359 }; 1360 1361 static const struct nla_policy ifla_vf_stats_policy[IFLA_VF_STATS_MAX + 1] = { 1362 [IFLA_VF_STATS_RX_PACKETS] = { .type = NLA_U64 }, 1363 [IFLA_VF_STATS_TX_PACKETS] = { .type = NLA_U64 }, 1364 [IFLA_VF_STATS_RX_BYTES] = { .type = NLA_U64 }, 1365 [IFLA_VF_STATS_TX_BYTES] = { .type = NLA_U64 }, 1366 [IFLA_VF_STATS_BROADCAST] = { .type = NLA_U64 }, 1367 [IFLA_VF_STATS_MULTICAST] = { .type = NLA_U64 }, 1368 }; 1369 1370 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1371 [IFLA_PORT_VF] = { .type = NLA_U32 }, 1372 [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1373 .len = PORT_PROFILE_MAX }, 1374 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1375 .len = sizeof(struct ifla_port_vsi)}, 1376 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1377 .len = PORT_UUID_MAX }, 1378 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1379 .len = PORT_UUID_MAX }, 1380 [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1381 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1382 }; 1383 1384 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 1385 { 1386 struct net *net = sock_net(skb->sk); 1387 int h, s_h; 1388 int idx = 0, s_idx; 1389 struct net_device *dev; 1390 struct hlist_head *head; 1391 struct nlattr *tb[IFLA_MAX+1]; 1392 u32 ext_filter_mask = 0; 1393 int err; 1394 int hdrlen; 1395 1396 s_h = cb->args[0]; 1397 s_idx = cb->args[1]; 1398 1399 cb->seq = net->dev_base_seq; 1400 1401 /* A hack to preserve kernel<->userspace interface. 1402 * The correct header is ifinfomsg. It is consistent with rtnl_getlink. 1403 * However, before Linux v3.9 the code here assumed rtgenmsg and that's 1404 * what iproute2 < v3.9.0 used. 1405 * We can detect the old iproute2. Even including the IFLA_EXT_MASK 1406 * attribute, its netlink message is shorter than struct ifinfomsg. 1407 */ 1408 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ? 1409 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 1410 1411 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { 1412 1413 if (tb[IFLA_EXT_MASK]) 1414 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1415 } 1416 1417 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 1418 idx = 0; 1419 head = &net->dev_index_head[h]; 1420 hlist_for_each_entry(dev, head, index_hlist) { 1421 if (idx < s_idx) 1422 goto cont; 1423 err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 1424 NETLINK_CB(cb->skb).portid, 1425 cb->nlh->nlmsg_seq, 0, 1426 NLM_F_MULTI, 1427 ext_filter_mask); 1428 /* If we ran out of room on the first message, 1429 * we're in trouble 1430 */ 1431 WARN_ON((err == -EMSGSIZE) && (skb->len == 0)); 1432 1433 if (err < 0) 1434 goto out; 1435 1436 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 1437 cont: 1438 idx++; 1439 } 1440 } 1441 out: 1442 cb->args[1] = idx; 1443 cb->args[0] = h; 1444 1445 return skb->len; 1446 } 1447 1448 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len) 1449 { 1450 return nla_parse(tb, IFLA_MAX, head, len, ifla_policy); 1451 } 1452 EXPORT_SYMBOL(rtnl_nla_parse_ifla); 1453 1454 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 1455 { 1456 struct net *net; 1457 /* Examine the link attributes and figure out which 1458 * network namespace we are talking about. 1459 */ 1460 if (tb[IFLA_NET_NS_PID]) 1461 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1462 else if (tb[IFLA_NET_NS_FD]) 1463 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 1464 else 1465 net = get_net(src_net); 1466 return net; 1467 } 1468 EXPORT_SYMBOL(rtnl_link_get_net); 1469 1470 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 1471 { 1472 if (dev) { 1473 if (tb[IFLA_ADDRESS] && 1474 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 1475 return -EINVAL; 1476 1477 if (tb[IFLA_BROADCAST] && 1478 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 1479 return -EINVAL; 1480 } 1481 1482 if (tb[IFLA_AF_SPEC]) { 1483 struct nlattr *af; 1484 int rem, err; 1485 1486 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1487 const struct rtnl_af_ops *af_ops; 1488 1489 if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1490 return -EAFNOSUPPORT; 1491 1492 if (!af_ops->set_link_af) 1493 return -EOPNOTSUPP; 1494 1495 if (af_ops->validate_link_af) { 1496 err = af_ops->validate_link_af(dev, af); 1497 if (err < 0) 1498 return err; 1499 } 1500 } 1501 } 1502 1503 return 0; 1504 } 1505 1506 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb) 1507 { 1508 const struct net_device_ops *ops = dev->netdev_ops; 1509 int err = -EINVAL; 1510 1511 if (tb[IFLA_VF_MAC]) { 1512 struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]); 1513 1514 err = -EOPNOTSUPP; 1515 if (ops->ndo_set_vf_mac) 1516 err = ops->ndo_set_vf_mac(dev, ivm->vf, 1517 ivm->mac); 1518 if (err < 0) 1519 return err; 1520 } 1521 1522 if (tb[IFLA_VF_VLAN]) { 1523 struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]); 1524 1525 err = -EOPNOTSUPP; 1526 if (ops->ndo_set_vf_vlan) 1527 err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan, 1528 ivv->qos); 1529 if (err < 0) 1530 return err; 1531 } 1532 1533 if (tb[IFLA_VF_TX_RATE]) { 1534 struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]); 1535 struct ifla_vf_info ivf; 1536 1537 err = -EOPNOTSUPP; 1538 if (ops->ndo_get_vf_config) 1539 err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf); 1540 if (err < 0) 1541 return err; 1542 1543 err = -EOPNOTSUPP; 1544 if (ops->ndo_set_vf_rate) 1545 err = ops->ndo_set_vf_rate(dev, ivt->vf, 1546 ivf.min_tx_rate, 1547 ivt->rate); 1548 if (err < 0) 1549 return err; 1550 } 1551 1552 if (tb[IFLA_VF_RATE]) { 1553 struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]); 1554 1555 err = -EOPNOTSUPP; 1556 if (ops->ndo_set_vf_rate) 1557 err = ops->ndo_set_vf_rate(dev, ivt->vf, 1558 ivt->min_tx_rate, 1559 ivt->max_tx_rate); 1560 if (err < 0) 1561 return err; 1562 } 1563 1564 if (tb[IFLA_VF_SPOOFCHK]) { 1565 struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]); 1566 1567 err = -EOPNOTSUPP; 1568 if (ops->ndo_set_vf_spoofchk) 1569 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 1570 ivs->setting); 1571 if (err < 0) 1572 return err; 1573 } 1574 1575 if (tb[IFLA_VF_LINK_STATE]) { 1576 struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]); 1577 1578 err = -EOPNOTSUPP; 1579 if (ops->ndo_set_vf_link_state) 1580 err = ops->ndo_set_vf_link_state(dev, ivl->vf, 1581 ivl->link_state); 1582 if (err < 0) 1583 return err; 1584 } 1585 1586 if (tb[IFLA_VF_RSS_QUERY_EN]) { 1587 struct ifla_vf_rss_query_en *ivrssq_en; 1588 1589 err = -EOPNOTSUPP; 1590 ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]); 1591 if (ops->ndo_set_vf_rss_query_en) 1592 err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf, 1593 ivrssq_en->setting); 1594 if (err < 0) 1595 return err; 1596 } 1597 1598 if (tb[IFLA_VF_TRUST]) { 1599 struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]); 1600 1601 err = -EOPNOTSUPP; 1602 if (ops->ndo_set_vf_trust) 1603 err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting); 1604 if (err < 0) 1605 return err; 1606 } 1607 1608 return err; 1609 } 1610 1611 static int do_set_master(struct net_device *dev, int ifindex) 1612 { 1613 struct net_device *upper_dev = netdev_master_upper_dev_get(dev); 1614 const struct net_device_ops *ops; 1615 int err; 1616 1617 if (upper_dev) { 1618 if (upper_dev->ifindex == ifindex) 1619 return 0; 1620 ops = upper_dev->netdev_ops; 1621 if (ops->ndo_del_slave) { 1622 err = ops->ndo_del_slave(upper_dev, dev); 1623 if (err) 1624 return err; 1625 } else { 1626 return -EOPNOTSUPP; 1627 } 1628 } 1629 1630 if (ifindex) { 1631 upper_dev = __dev_get_by_index(dev_net(dev), ifindex); 1632 if (!upper_dev) 1633 return -EINVAL; 1634 ops = upper_dev->netdev_ops; 1635 if (ops->ndo_add_slave) { 1636 err = ops->ndo_add_slave(upper_dev, dev); 1637 if (err) 1638 return err; 1639 } else { 1640 return -EOPNOTSUPP; 1641 } 1642 } 1643 return 0; 1644 } 1645 1646 #define DO_SETLINK_MODIFIED 0x01 1647 /* notify flag means notify + modified. */ 1648 #define DO_SETLINK_NOTIFY 0x03 1649 static int do_setlink(const struct sk_buff *skb, 1650 struct net_device *dev, struct ifinfomsg *ifm, 1651 struct nlattr **tb, char *ifname, int status) 1652 { 1653 const struct net_device_ops *ops = dev->netdev_ops; 1654 int err; 1655 1656 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { 1657 struct net *net = rtnl_link_get_net(dev_net(dev), tb); 1658 if (IS_ERR(net)) { 1659 err = PTR_ERR(net); 1660 goto errout; 1661 } 1662 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { 1663 put_net(net); 1664 err = -EPERM; 1665 goto errout; 1666 } 1667 err = dev_change_net_namespace(dev, net, ifname); 1668 put_net(net); 1669 if (err) 1670 goto errout; 1671 status |= DO_SETLINK_MODIFIED; 1672 } 1673 1674 if (tb[IFLA_MAP]) { 1675 struct rtnl_link_ifmap *u_map; 1676 struct ifmap k_map; 1677 1678 if (!ops->ndo_set_config) { 1679 err = -EOPNOTSUPP; 1680 goto errout; 1681 } 1682 1683 if (!netif_device_present(dev)) { 1684 err = -ENODEV; 1685 goto errout; 1686 } 1687 1688 u_map = nla_data(tb[IFLA_MAP]); 1689 k_map.mem_start = (unsigned long) u_map->mem_start; 1690 k_map.mem_end = (unsigned long) u_map->mem_end; 1691 k_map.base_addr = (unsigned short) u_map->base_addr; 1692 k_map.irq = (unsigned char) u_map->irq; 1693 k_map.dma = (unsigned char) u_map->dma; 1694 k_map.port = (unsigned char) u_map->port; 1695 1696 err = ops->ndo_set_config(dev, &k_map); 1697 if (err < 0) 1698 goto errout; 1699 1700 status |= DO_SETLINK_NOTIFY; 1701 } 1702 1703 if (tb[IFLA_ADDRESS]) { 1704 struct sockaddr *sa; 1705 int len; 1706 1707 len = sizeof(sa_family_t) + dev->addr_len; 1708 sa = kmalloc(len, GFP_KERNEL); 1709 if (!sa) { 1710 err = -ENOMEM; 1711 goto errout; 1712 } 1713 sa->sa_family = dev->type; 1714 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 1715 dev->addr_len); 1716 err = dev_set_mac_address(dev, sa); 1717 kfree(sa); 1718 if (err) 1719 goto errout; 1720 status |= DO_SETLINK_MODIFIED; 1721 } 1722 1723 if (tb[IFLA_MTU]) { 1724 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 1725 if (err < 0) 1726 goto errout; 1727 status |= DO_SETLINK_MODIFIED; 1728 } 1729 1730 if (tb[IFLA_GROUP]) { 1731 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 1732 status |= DO_SETLINK_NOTIFY; 1733 } 1734 1735 /* 1736 * Interface selected by interface index but interface 1737 * name provided implies that a name change has been 1738 * requested. 1739 */ 1740 if (ifm->ifi_index > 0 && ifname[0]) { 1741 err = dev_change_name(dev, ifname); 1742 if (err < 0) 1743 goto errout; 1744 status |= DO_SETLINK_MODIFIED; 1745 } 1746 1747 if (tb[IFLA_IFALIAS]) { 1748 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 1749 nla_len(tb[IFLA_IFALIAS])); 1750 if (err < 0) 1751 goto errout; 1752 status |= DO_SETLINK_NOTIFY; 1753 } 1754 1755 if (tb[IFLA_BROADCAST]) { 1756 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 1757 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 1758 } 1759 1760 if (ifm->ifi_flags || ifm->ifi_change) { 1761 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 1762 if (err < 0) 1763 goto errout; 1764 } 1765 1766 if (tb[IFLA_MASTER]) { 1767 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); 1768 if (err) 1769 goto errout; 1770 status |= DO_SETLINK_MODIFIED; 1771 } 1772 1773 if (tb[IFLA_CARRIER]) { 1774 err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER])); 1775 if (err) 1776 goto errout; 1777 status |= DO_SETLINK_MODIFIED; 1778 } 1779 1780 if (tb[IFLA_TXQLEN]) { 1781 unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]); 1782 1783 if (dev->tx_queue_len ^ value) 1784 status |= DO_SETLINK_NOTIFY; 1785 1786 dev->tx_queue_len = value; 1787 } 1788 1789 if (tb[IFLA_OPERSTATE]) 1790 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1791 1792 if (tb[IFLA_LINKMODE]) { 1793 unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]); 1794 1795 write_lock_bh(&dev_base_lock); 1796 if (dev->link_mode ^ value) 1797 status |= DO_SETLINK_NOTIFY; 1798 dev->link_mode = value; 1799 write_unlock_bh(&dev_base_lock); 1800 } 1801 1802 if (tb[IFLA_VFINFO_LIST]) { 1803 struct nlattr *vfinfo[IFLA_VF_MAX + 1]; 1804 struct nlattr *attr; 1805 int rem; 1806 1807 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 1808 if (nla_type(attr) != IFLA_VF_INFO || 1809 nla_len(attr) < NLA_HDRLEN) { 1810 err = -EINVAL; 1811 goto errout; 1812 } 1813 err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr, 1814 ifla_vf_policy); 1815 if (err < 0) 1816 goto errout; 1817 err = do_setvfinfo(dev, vfinfo); 1818 if (err < 0) 1819 goto errout; 1820 status |= DO_SETLINK_NOTIFY; 1821 } 1822 } 1823 err = 0; 1824 1825 if (tb[IFLA_VF_PORTS]) { 1826 struct nlattr *port[IFLA_PORT_MAX+1]; 1827 struct nlattr *attr; 1828 int vf; 1829 int rem; 1830 1831 err = -EOPNOTSUPP; 1832 if (!ops->ndo_set_vf_port) 1833 goto errout; 1834 1835 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 1836 if (nla_type(attr) != IFLA_VF_PORT || 1837 nla_len(attr) < NLA_HDRLEN) { 1838 err = -EINVAL; 1839 goto errout; 1840 } 1841 err = nla_parse_nested(port, IFLA_PORT_MAX, attr, 1842 ifla_port_policy); 1843 if (err < 0) 1844 goto errout; 1845 if (!port[IFLA_PORT_VF]) { 1846 err = -EOPNOTSUPP; 1847 goto errout; 1848 } 1849 vf = nla_get_u32(port[IFLA_PORT_VF]); 1850 err = ops->ndo_set_vf_port(dev, vf, port); 1851 if (err < 0) 1852 goto errout; 1853 status |= DO_SETLINK_NOTIFY; 1854 } 1855 } 1856 err = 0; 1857 1858 if (tb[IFLA_PORT_SELF]) { 1859 struct nlattr *port[IFLA_PORT_MAX+1]; 1860 1861 err = nla_parse_nested(port, IFLA_PORT_MAX, 1862 tb[IFLA_PORT_SELF], ifla_port_policy); 1863 if (err < 0) 1864 goto errout; 1865 1866 err = -EOPNOTSUPP; 1867 if (ops->ndo_set_vf_port) 1868 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 1869 if (err < 0) 1870 goto errout; 1871 status |= DO_SETLINK_NOTIFY; 1872 } 1873 1874 if (tb[IFLA_AF_SPEC]) { 1875 struct nlattr *af; 1876 int rem; 1877 1878 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1879 const struct rtnl_af_ops *af_ops; 1880 1881 if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1882 BUG(); 1883 1884 err = af_ops->set_link_af(dev, af); 1885 if (err < 0) 1886 goto errout; 1887 1888 status |= DO_SETLINK_NOTIFY; 1889 } 1890 } 1891 err = 0; 1892 1893 if (tb[IFLA_PROTO_DOWN]) { 1894 err = dev_change_proto_down(dev, 1895 nla_get_u8(tb[IFLA_PROTO_DOWN])); 1896 if (err) 1897 goto errout; 1898 status |= DO_SETLINK_NOTIFY; 1899 } 1900 1901 errout: 1902 if (status & DO_SETLINK_MODIFIED) { 1903 if (status & DO_SETLINK_NOTIFY) 1904 netdev_state_change(dev); 1905 1906 if (err < 0) 1907 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", 1908 dev->name); 1909 } 1910 1911 return err; 1912 } 1913 1914 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) 1915 { 1916 struct net *net = sock_net(skb->sk); 1917 struct ifinfomsg *ifm; 1918 struct net_device *dev; 1919 int err; 1920 struct nlattr *tb[IFLA_MAX+1]; 1921 char ifname[IFNAMSIZ]; 1922 1923 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1924 if (err < 0) 1925 goto errout; 1926 1927 if (tb[IFLA_IFNAME]) 1928 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1929 else 1930 ifname[0] = '\0'; 1931 1932 err = -EINVAL; 1933 ifm = nlmsg_data(nlh); 1934 if (ifm->ifi_index > 0) 1935 dev = __dev_get_by_index(net, ifm->ifi_index); 1936 else if (tb[IFLA_IFNAME]) 1937 dev = __dev_get_by_name(net, ifname); 1938 else 1939 goto errout; 1940 1941 if (dev == NULL) { 1942 err = -ENODEV; 1943 goto errout; 1944 } 1945 1946 err = validate_linkmsg(dev, tb); 1947 if (err < 0) 1948 goto errout; 1949 1950 err = do_setlink(skb, dev, ifm, tb, ifname, 0); 1951 errout: 1952 return err; 1953 } 1954 1955 static int rtnl_group_dellink(const struct net *net, int group) 1956 { 1957 struct net_device *dev, *aux; 1958 LIST_HEAD(list_kill); 1959 bool found = false; 1960 1961 if (!group) 1962 return -EPERM; 1963 1964 for_each_netdev(net, dev) { 1965 if (dev->group == group) { 1966 const struct rtnl_link_ops *ops; 1967 1968 found = true; 1969 ops = dev->rtnl_link_ops; 1970 if (!ops || !ops->dellink) 1971 return -EOPNOTSUPP; 1972 } 1973 } 1974 1975 if (!found) 1976 return -ENODEV; 1977 1978 for_each_netdev_safe(net, dev, aux) { 1979 if (dev->group == group) { 1980 const struct rtnl_link_ops *ops; 1981 1982 ops = dev->rtnl_link_ops; 1983 ops->dellink(dev, &list_kill); 1984 } 1985 } 1986 unregister_netdevice_many(&list_kill); 1987 1988 return 0; 1989 } 1990 1991 int rtnl_delete_link(struct net_device *dev) 1992 { 1993 const struct rtnl_link_ops *ops; 1994 LIST_HEAD(list_kill); 1995 1996 ops = dev->rtnl_link_ops; 1997 if (!ops || !ops->dellink) 1998 return -EOPNOTSUPP; 1999 2000 ops->dellink(dev, &list_kill); 2001 unregister_netdevice_many(&list_kill); 2002 2003 return 0; 2004 } 2005 EXPORT_SYMBOL_GPL(rtnl_delete_link); 2006 2007 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) 2008 { 2009 struct net *net = sock_net(skb->sk); 2010 struct net_device *dev; 2011 struct ifinfomsg *ifm; 2012 char ifname[IFNAMSIZ]; 2013 struct nlattr *tb[IFLA_MAX+1]; 2014 int err; 2015 2016 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 2017 if (err < 0) 2018 return err; 2019 2020 if (tb[IFLA_IFNAME]) 2021 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2022 2023 ifm = nlmsg_data(nlh); 2024 if (ifm->ifi_index > 0) 2025 dev = __dev_get_by_index(net, ifm->ifi_index); 2026 else if (tb[IFLA_IFNAME]) 2027 dev = __dev_get_by_name(net, ifname); 2028 else if (tb[IFLA_GROUP]) 2029 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP])); 2030 else 2031 return -EINVAL; 2032 2033 if (!dev) 2034 return -ENODEV; 2035 2036 return rtnl_delete_link(dev); 2037 } 2038 2039 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 2040 { 2041 unsigned int old_flags; 2042 int err; 2043 2044 old_flags = dev->flags; 2045 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 2046 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 2047 if (err < 0) 2048 return err; 2049 } 2050 2051 dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 2052 2053 __dev_notify_flags(dev, old_flags, ~0U); 2054 return 0; 2055 } 2056 EXPORT_SYMBOL(rtnl_configure_link); 2057 2058 struct net_device *rtnl_create_link(struct net *net, 2059 const char *ifname, unsigned char name_assign_type, 2060 const struct rtnl_link_ops *ops, struct nlattr *tb[]) 2061 { 2062 int err; 2063 struct net_device *dev; 2064 unsigned int num_tx_queues = 1; 2065 unsigned int num_rx_queues = 1; 2066 2067 if (tb[IFLA_NUM_TX_QUEUES]) 2068 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]); 2069 else if (ops->get_num_tx_queues) 2070 num_tx_queues = ops->get_num_tx_queues(); 2071 2072 if (tb[IFLA_NUM_RX_QUEUES]) 2073 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]); 2074 else if (ops->get_num_rx_queues) 2075 num_rx_queues = ops->get_num_rx_queues(); 2076 2077 err = -ENOMEM; 2078 dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type, 2079 ops->setup, num_tx_queues, num_rx_queues); 2080 if (!dev) 2081 goto err; 2082 2083 dev_net_set(dev, net); 2084 dev->rtnl_link_ops = ops; 2085 dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 2086 2087 if (tb[IFLA_MTU]) 2088 dev->mtu = nla_get_u32(tb[IFLA_MTU]); 2089 if (tb[IFLA_ADDRESS]) { 2090 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 2091 nla_len(tb[IFLA_ADDRESS])); 2092 dev->addr_assign_type = NET_ADDR_SET; 2093 } 2094 if (tb[IFLA_BROADCAST]) 2095 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 2096 nla_len(tb[IFLA_BROADCAST])); 2097 if (tb[IFLA_TXQLEN]) 2098 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 2099 if (tb[IFLA_OPERSTATE]) 2100 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 2101 if (tb[IFLA_LINKMODE]) 2102 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 2103 if (tb[IFLA_GROUP]) 2104 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 2105 2106 return dev; 2107 2108 err: 2109 return ERR_PTR(err); 2110 } 2111 EXPORT_SYMBOL(rtnl_create_link); 2112 2113 static int rtnl_group_changelink(const struct sk_buff *skb, 2114 struct net *net, int group, 2115 struct ifinfomsg *ifm, 2116 struct nlattr **tb) 2117 { 2118 struct net_device *dev, *aux; 2119 int err; 2120 2121 for_each_netdev_safe(net, dev, aux) { 2122 if (dev->group == group) { 2123 err = do_setlink(skb, dev, ifm, tb, NULL, 0); 2124 if (err < 0) 2125 return err; 2126 } 2127 } 2128 2129 return 0; 2130 } 2131 2132 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh) 2133 { 2134 struct net *net = sock_net(skb->sk); 2135 const struct rtnl_link_ops *ops; 2136 const struct rtnl_link_ops *m_ops = NULL; 2137 struct net_device *dev; 2138 struct net_device *master_dev = NULL; 2139 struct ifinfomsg *ifm; 2140 char kind[MODULE_NAME_LEN]; 2141 char ifname[IFNAMSIZ]; 2142 struct nlattr *tb[IFLA_MAX+1]; 2143 struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 2144 unsigned char name_assign_type = NET_NAME_USER; 2145 int err; 2146 2147 #ifdef CONFIG_MODULES 2148 replay: 2149 #endif 2150 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 2151 if (err < 0) 2152 return err; 2153 2154 if (tb[IFLA_IFNAME]) 2155 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2156 else 2157 ifname[0] = '\0'; 2158 2159 ifm = nlmsg_data(nlh); 2160 if (ifm->ifi_index > 0) 2161 dev = __dev_get_by_index(net, ifm->ifi_index); 2162 else { 2163 if (ifname[0]) 2164 dev = __dev_get_by_name(net, ifname); 2165 else 2166 dev = NULL; 2167 } 2168 2169 if (dev) { 2170 master_dev = netdev_master_upper_dev_get(dev); 2171 if (master_dev) 2172 m_ops = master_dev->rtnl_link_ops; 2173 } 2174 2175 err = validate_linkmsg(dev, tb); 2176 if (err < 0) 2177 return err; 2178 2179 if (tb[IFLA_LINKINFO]) { 2180 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 2181 tb[IFLA_LINKINFO], ifla_info_policy); 2182 if (err < 0) 2183 return err; 2184 } else 2185 memset(linkinfo, 0, sizeof(linkinfo)); 2186 2187 if (linkinfo[IFLA_INFO_KIND]) { 2188 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 2189 ops = rtnl_link_ops_get(kind); 2190 } else { 2191 kind[0] = '\0'; 2192 ops = NULL; 2193 } 2194 2195 if (1) { 2196 struct nlattr *attr[ops ? ops->maxtype + 1 : 1]; 2197 struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1]; 2198 struct nlattr **data = NULL; 2199 struct nlattr **slave_data = NULL; 2200 struct net *dest_net, *link_net = NULL; 2201 2202 if (ops) { 2203 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 2204 err = nla_parse_nested(attr, ops->maxtype, 2205 linkinfo[IFLA_INFO_DATA], 2206 ops->policy); 2207 if (err < 0) 2208 return err; 2209 data = attr; 2210 } 2211 if (ops->validate) { 2212 err = ops->validate(tb, data); 2213 if (err < 0) 2214 return err; 2215 } 2216 } 2217 2218 if (m_ops) { 2219 if (m_ops->slave_maxtype && 2220 linkinfo[IFLA_INFO_SLAVE_DATA]) { 2221 err = nla_parse_nested(slave_attr, 2222 m_ops->slave_maxtype, 2223 linkinfo[IFLA_INFO_SLAVE_DATA], 2224 m_ops->slave_policy); 2225 if (err < 0) 2226 return err; 2227 slave_data = slave_attr; 2228 } 2229 if (m_ops->slave_validate) { 2230 err = m_ops->slave_validate(tb, slave_data); 2231 if (err < 0) 2232 return err; 2233 } 2234 } 2235 2236 if (dev) { 2237 int status = 0; 2238 2239 if (nlh->nlmsg_flags & NLM_F_EXCL) 2240 return -EEXIST; 2241 if (nlh->nlmsg_flags & NLM_F_REPLACE) 2242 return -EOPNOTSUPP; 2243 2244 if (linkinfo[IFLA_INFO_DATA]) { 2245 if (!ops || ops != dev->rtnl_link_ops || 2246 !ops->changelink) 2247 return -EOPNOTSUPP; 2248 2249 err = ops->changelink(dev, tb, data); 2250 if (err < 0) 2251 return err; 2252 status |= DO_SETLINK_NOTIFY; 2253 } 2254 2255 if (linkinfo[IFLA_INFO_SLAVE_DATA]) { 2256 if (!m_ops || !m_ops->slave_changelink) 2257 return -EOPNOTSUPP; 2258 2259 err = m_ops->slave_changelink(master_dev, dev, 2260 tb, slave_data); 2261 if (err < 0) 2262 return err; 2263 status |= DO_SETLINK_NOTIFY; 2264 } 2265 2266 return do_setlink(skb, dev, ifm, tb, ifname, status); 2267 } 2268 2269 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 2270 if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 2271 return rtnl_group_changelink(skb, net, 2272 nla_get_u32(tb[IFLA_GROUP]), 2273 ifm, tb); 2274 return -ENODEV; 2275 } 2276 2277 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) 2278 return -EOPNOTSUPP; 2279 2280 if (!ops) { 2281 #ifdef CONFIG_MODULES 2282 if (kind[0]) { 2283 __rtnl_unlock(); 2284 request_module("rtnl-link-%s", kind); 2285 rtnl_lock(); 2286 ops = rtnl_link_ops_get(kind); 2287 if (ops) 2288 goto replay; 2289 } 2290 #endif 2291 return -EOPNOTSUPP; 2292 } 2293 2294 if (!ops->setup) 2295 return -EOPNOTSUPP; 2296 2297 if (!ifname[0]) { 2298 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 2299 name_assign_type = NET_NAME_ENUM; 2300 } 2301 2302 dest_net = rtnl_link_get_net(net, tb); 2303 if (IS_ERR(dest_net)) 2304 return PTR_ERR(dest_net); 2305 2306 err = -EPERM; 2307 if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN)) 2308 goto out; 2309 2310 if (tb[IFLA_LINK_NETNSID]) { 2311 int id = nla_get_s32(tb[IFLA_LINK_NETNSID]); 2312 2313 link_net = get_net_ns_by_id(dest_net, id); 2314 if (!link_net) { 2315 err = -EINVAL; 2316 goto out; 2317 } 2318 err = -EPERM; 2319 if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN)) 2320 goto out; 2321 } 2322 2323 dev = rtnl_create_link(link_net ? : dest_net, ifname, 2324 name_assign_type, ops, tb); 2325 if (IS_ERR(dev)) { 2326 err = PTR_ERR(dev); 2327 goto out; 2328 } 2329 2330 dev->ifindex = ifm->ifi_index; 2331 2332 if (ops->newlink) { 2333 err = ops->newlink(link_net ? : net, dev, tb, data); 2334 /* Drivers should call free_netdev() in ->destructor 2335 * and unregister it on failure after registration 2336 * so that device could be finally freed in rtnl_unlock. 2337 */ 2338 if (err < 0) { 2339 /* If device is not registered at all, free it now */ 2340 if (dev->reg_state == NETREG_UNINITIALIZED) 2341 free_netdev(dev); 2342 goto out; 2343 } 2344 } else { 2345 err = register_netdevice(dev); 2346 if (err < 0) { 2347 free_netdev(dev); 2348 goto out; 2349 } 2350 } 2351 err = rtnl_configure_link(dev, ifm); 2352 if (err < 0) 2353 goto out_unregister; 2354 if (link_net) { 2355 err = dev_change_net_namespace(dev, dest_net, ifname); 2356 if (err < 0) 2357 goto out_unregister; 2358 } 2359 out: 2360 if (link_net) 2361 put_net(link_net); 2362 put_net(dest_net); 2363 return err; 2364 out_unregister: 2365 if (ops->newlink) { 2366 LIST_HEAD(list_kill); 2367 2368 ops->dellink(dev, &list_kill); 2369 unregister_netdevice_many(&list_kill); 2370 } else { 2371 unregister_netdevice(dev); 2372 } 2373 goto out; 2374 } 2375 } 2376 2377 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh) 2378 { 2379 struct net *net = sock_net(skb->sk); 2380 struct ifinfomsg *ifm; 2381 char ifname[IFNAMSIZ]; 2382 struct nlattr *tb[IFLA_MAX+1]; 2383 struct net_device *dev = NULL; 2384 struct sk_buff *nskb; 2385 int err; 2386 u32 ext_filter_mask = 0; 2387 2388 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 2389 if (err < 0) 2390 return err; 2391 2392 if (tb[IFLA_IFNAME]) 2393 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 2394 2395 if (tb[IFLA_EXT_MASK]) 2396 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 2397 2398 ifm = nlmsg_data(nlh); 2399 if (ifm->ifi_index > 0) 2400 dev = __dev_get_by_index(net, ifm->ifi_index); 2401 else if (tb[IFLA_IFNAME]) 2402 dev = __dev_get_by_name(net, ifname); 2403 else 2404 return -EINVAL; 2405 2406 if (dev == NULL) 2407 return -ENODEV; 2408 2409 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 2410 if (nskb == NULL) 2411 return -ENOBUFS; 2412 2413 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid, 2414 nlh->nlmsg_seq, 0, 0, ext_filter_mask); 2415 if (err < 0) { 2416 /* -EMSGSIZE implies BUG in if_nlmsg_size */ 2417 WARN_ON(err == -EMSGSIZE); 2418 kfree_skb(nskb); 2419 } else 2420 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid); 2421 2422 return err; 2423 } 2424 2425 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 2426 { 2427 struct net *net = sock_net(skb->sk); 2428 struct net_device *dev; 2429 struct nlattr *tb[IFLA_MAX+1]; 2430 u32 ext_filter_mask = 0; 2431 u16 min_ifinfo_dump_size = 0; 2432 int hdrlen; 2433 2434 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */ 2435 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ? 2436 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg); 2437 2438 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) { 2439 if (tb[IFLA_EXT_MASK]) 2440 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 2441 } 2442 2443 if (!ext_filter_mask) 2444 return NLMSG_GOODSIZE; 2445 /* 2446 * traverse the list of net devices and compute the minimum 2447 * buffer size based upon the filter mask. 2448 */ 2449 list_for_each_entry(dev, &net->dev_base_head, dev_list) { 2450 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 2451 if_nlmsg_size(dev, 2452 ext_filter_mask)); 2453 } 2454 2455 return min_ifinfo_dump_size; 2456 } 2457 2458 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 2459 { 2460 int idx; 2461 int s_idx = cb->family; 2462 2463 if (s_idx == 0) 2464 s_idx = 1; 2465 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 2466 int type = cb->nlh->nlmsg_type-RTM_BASE; 2467 if (idx < s_idx || idx == PF_PACKET) 2468 continue; 2469 if (rtnl_msg_handlers[idx] == NULL || 2470 rtnl_msg_handlers[idx][type].dumpit == NULL) 2471 continue; 2472 if (idx > s_idx) { 2473 memset(&cb->args[0], 0, sizeof(cb->args)); 2474 cb->prev_seq = 0; 2475 cb->seq = 0; 2476 } 2477 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 2478 break; 2479 } 2480 cb->family = idx; 2481 2482 return skb->len; 2483 } 2484 2485 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev, 2486 unsigned int change, gfp_t flags) 2487 { 2488 struct net *net = dev_net(dev); 2489 struct sk_buff *skb; 2490 int err = -ENOBUFS; 2491 size_t if_info_size; 2492 2493 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags); 2494 if (skb == NULL) 2495 goto errout; 2496 2497 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); 2498 if (err < 0) { 2499 /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 2500 WARN_ON(err == -EMSGSIZE); 2501 kfree_skb(skb); 2502 goto errout; 2503 } 2504 return skb; 2505 errout: 2506 if (err < 0) 2507 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 2508 return NULL; 2509 } 2510 2511 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags) 2512 { 2513 struct net *net = dev_net(dev); 2514 2515 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags); 2516 } 2517 2518 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change, 2519 gfp_t flags) 2520 { 2521 struct sk_buff *skb; 2522 2523 if (dev->reg_state != NETREG_REGISTERED) 2524 return; 2525 2526 skb = rtmsg_ifinfo_build_skb(type, dev, change, flags); 2527 if (skb) 2528 rtmsg_ifinfo_send(skb, dev, flags); 2529 } 2530 EXPORT_SYMBOL(rtmsg_ifinfo); 2531 2532 static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 2533 struct net_device *dev, 2534 u8 *addr, u16 vid, u32 pid, u32 seq, 2535 int type, unsigned int flags, 2536 int nlflags) 2537 { 2538 struct nlmsghdr *nlh; 2539 struct ndmsg *ndm; 2540 2541 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags); 2542 if (!nlh) 2543 return -EMSGSIZE; 2544 2545 ndm = nlmsg_data(nlh); 2546 ndm->ndm_family = AF_BRIDGE; 2547 ndm->ndm_pad1 = 0; 2548 ndm->ndm_pad2 = 0; 2549 ndm->ndm_flags = flags; 2550 ndm->ndm_type = 0; 2551 ndm->ndm_ifindex = dev->ifindex; 2552 ndm->ndm_state = NUD_PERMANENT; 2553 2554 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 2555 goto nla_put_failure; 2556 if (vid) 2557 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid)) 2558 goto nla_put_failure; 2559 2560 nlmsg_end(skb, nlh); 2561 return 0; 2562 2563 nla_put_failure: 2564 nlmsg_cancel(skb, nlh); 2565 return -EMSGSIZE; 2566 } 2567 2568 static inline size_t rtnl_fdb_nlmsg_size(void) 2569 { 2570 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN); 2571 } 2572 2573 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type) 2574 { 2575 struct net *net = dev_net(dev); 2576 struct sk_buff *skb; 2577 int err = -ENOBUFS; 2578 2579 skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC); 2580 if (!skb) 2581 goto errout; 2582 2583 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid, 2584 0, 0, type, NTF_SELF, 0); 2585 if (err < 0) { 2586 kfree_skb(skb); 2587 goto errout; 2588 } 2589 2590 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC); 2591 return; 2592 errout: 2593 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err); 2594 } 2595 2596 /** 2597 * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry 2598 */ 2599 int ndo_dflt_fdb_add(struct ndmsg *ndm, 2600 struct nlattr *tb[], 2601 struct net_device *dev, 2602 const unsigned char *addr, u16 vid, 2603 u16 flags) 2604 { 2605 int err = -EINVAL; 2606 2607 /* If aging addresses are supported device will need to 2608 * implement its own handler for this. 2609 */ 2610 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 2611 pr_info("%s: FDB only supports static addresses\n", dev->name); 2612 return err; 2613 } 2614 2615 if (vid) { 2616 pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name); 2617 return err; 2618 } 2619 2620 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 2621 err = dev_uc_add_excl(dev, addr); 2622 else if (is_multicast_ether_addr(addr)) 2623 err = dev_mc_add_excl(dev, addr); 2624 2625 /* Only return duplicate errors if NLM_F_EXCL is set */ 2626 if (err == -EEXIST && !(flags & NLM_F_EXCL)) 2627 err = 0; 2628 2629 return err; 2630 } 2631 EXPORT_SYMBOL(ndo_dflt_fdb_add); 2632 2633 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid) 2634 { 2635 u16 vid = 0; 2636 2637 if (vlan_attr) { 2638 if (nla_len(vlan_attr) != sizeof(u16)) { 2639 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n"); 2640 return -EINVAL; 2641 } 2642 2643 vid = nla_get_u16(vlan_attr); 2644 2645 if (!vid || vid >= VLAN_VID_MASK) { 2646 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n", 2647 vid); 2648 return -EINVAL; 2649 } 2650 } 2651 *p_vid = vid; 2652 return 0; 2653 } 2654 2655 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) 2656 { 2657 struct net *net = sock_net(skb->sk); 2658 struct ndmsg *ndm; 2659 struct nlattr *tb[NDA_MAX+1]; 2660 struct net_device *dev; 2661 u8 *addr; 2662 u16 vid; 2663 int err; 2664 2665 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 2666 if (err < 0) 2667 return err; 2668 2669 ndm = nlmsg_data(nlh); 2670 if (ndm->ndm_ifindex == 0) { 2671 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n"); 2672 return -EINVAL; 2673 } 2674 2675 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 2676 if (dev == NULL) { 2677 pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n"); 2678 return -ENODEV; 2679 } 2680 2681 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 2682 pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n"); 2683 return -EINVAL; 2684 } 2685 2686 addr = nla_data(tb[NDA_LLADDR]); 2687 2688 err = fdb_vid_parse(tb[NDA_VLAN], &vid); 2689 if (err) 2690 return err; 2691 2692 err = -EOPNOTSUPP; 2693 2694 /* Support fdb on master device the net/bridge default case */ 2695 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 2696 (dev->priv_flags & IFF_BRIDGE_PORT)) { 2697 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 2698 const struct net_device_ops *ops = br_dev->netdev_ops; 2699 2700 err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid, 2701 nlh->nlmsg_flags); 2702 if (err) 2703 goto out; 2704 else 2705 ndm->ndm_flags &= ~NTF_MASTER; 2706 } 2707 2708 /* Embedded bridge, macvlan, and any other device support */ 2709 if ((ndm->ndm_flags & NTF_SELF)) { 2710 if (dev->netdev_ops->ndo_fdb_add) 2711 err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr, 2712 vid, 2713 nlh->nlmsg_flags); 2714 else 2715 err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, 2716 nlh->nlmsg_flags); 2717 2718 if (!err) { 2719 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH); 2720 ndm->ndm_flags &= ~NTF_SELF; 2721 } 2722 } 2723 out: 2724 return err; 2725 } 2726 2727 /** 2728 * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry 2729 */ 2730 int ndo_dflt_fdb_del(struct ndmsg *ndm, 2731 struct nlattr *tb[], 2732 struct net_device *dev, 2733 const unsigned char *addr, u16 vid) 2734 { 2735 int err = -EINVAL; 2736 2737 /* If aging addresses are supported device will need to 2738 * implement its own handler for this. 2739 */ 2740 if (!(ndm->ndm_state & NUD_PERMANENT)) { 2741 pr_info("%s: FDB only supports static addresses\n", dev->name); 2742 return err; 2743 } 2744 2745 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 2746 err = dev_uc_del(dev, addr); 2747 else if (is_multicast_ether_addr(addr)) 2748 err = dev_mc_del(dev, addr); 2749 2750 return err; 2751 } 2752 EXPORT_SYMBOL(ndo_dflt_fdb_del); 2753 2754 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) 2755 { 2756 struct net *net = sock_net(skb->sk); 2757 struct ndmsg *ndm; 2758 struct nlattr *tb[NDA_MAX+1]; 2759 struct net_device *dev; 2760 int err = -EINVAL; 2761 __u8 *addr; 2762 u16 vid; 2763 2764 if (!netlink_capable(skb, CAP_NET_ADMIN)) 2765 return -EPERM; 2766 2767 err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL); 2768 if (err < 0) 2769 return err; 2770 2771 ndm = nlmsg_data(nlh); 2772 if (ndm->ndm_ifindex == 0) { 2773 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n"); 2774 return -EINVAL; 2775 } 2776 2777 dev = __dev_get_by_index(net, ndm->ndm_ifindex); 2778 if (dev == NULL) { 2779 pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n"); 2780 return -ENODEV; 2781 } 2782 2783 if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) { 2784 pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n"); 2785 return -EINVAL; 2786 } 2787 2788 addr = nla_data(tb[NDA_LLADDR]); 2789 2790 err = fdb_vid_parse(tb[NDA_VLAN], &vid); 2791 if (err) 2792 return err; 2793 2794 err = -EOPNOTSUPP; 2795 2796 /* Support fdb on master device the net/bridge default case */ 2797 if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) && 2798 (dev->priv_flags & IFF_BRIDGE_PORT)) { 2799 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 2800 const struct net_device_ops *ops = br_dev->netdev_ops; 2801 2802 if (ops->ndo_fdb_del) 2803 err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid); 2804 2805 if (err) 2806 goto out; 2807 else 2808 ndm->ndm_flags &= ~NTF_MASTER; 2809 } 2810 2811 /* Embedded bridge, macvlan, and any other device support */ 2812 if (ndm->ndm_flags & NTF_SELF) { 2813 if (dev->netdev_ops->ndo_fdb_del) 2814 err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr, 2815 vid); 2816 else 2817 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 2818 2819 if (!err) { 2820 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH); 2821 ndm->ndm_flags &= ~NTF_SELF; 2822 } 2823 } 2824 out: 2825 return err; 2826 } 2827 2828 static int nlmsg_populate_fdb(struct sk_buff *skb, 2829 struct netlink_callback *cb, 2830 struct net_device *dev, 2831 int *idx, 2832 struct netdev_hw_addr_list *list) 2833 { 2834 struct netdev_hw_addr *ha; 2835 int err; 2836 u32 portid, seq; 2837 2838 portid = NETLINK_CB(cb->skb).portid; 2839 seq = cb->nlh->nlmsg_seq; 2840 2841 list_for_each_entry(ha, &list->list, list) { 2842 if (*idx < cb->args[0]) 2843 goto skip; 2844 2845 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0, 2846 portid, seq, 2847 RTM_NEWNEIGH, NTF_SELF, 2848 NLM_F_MULTI); 2849 if (err < 0) 2850 return err; 2851 skip: 2852 *idx += 1; 2853 } 2854 return 0; 2855 } 2856 2857 /** 2858 * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table. 2859 * @nlh: netlink message header 2860 * @dev: netdevice 2861 * 2862 * Default netdevice operation to dump the existing unicast address list. 2863 * Returns number of addresses from list put in skb. 2864 */ 2865 int ndo_dflt_fdb_dump(struct sk_buff *skb, 2866 struct netlink_callback *cb, 2867 struct net_device *dev, 2868 struct net_device *filter_dev, 2869 int idx) 2870 { 2871 int err; 2872 2873 netif_addr_lock_bh(dev); 2874 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc); 2875 if (err) 2876 goto out; 2877 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc); 2878 out: 2879 netif_addr_unlock_bh(dev); 2880 return idx; 2881 } 2882 EXPORT_SYMBOL(ndo_dflt_fdb_dump); 2883 2884 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) 2885 { 2886 struct net_device *dev; 2887 struct nlattr *tb[IFLA_MAX+1]; 2888 struct net_device *br_dev = NULL; 2889 const struct net_device_ops *ops = NULL; 2890 const struct net_device_ops *cops = NULL; 2891 struct ifinfomsg *ifm = nlmsg_data(cb->nlh); 2892 struct net *net = sock_net(skb->sk); 2893 int brport_idx = 0; 2894 int br_idx = 0; 2895 int idx = 0; 2896 2897 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX, 2898 ifla_policy) == 0) { 2899 if (tb[IFLA_MASTER]) 2900 br_idx = nla_get_u32(tb[IFLA_MASTER]); 2901 } 2902 2903 brport_idx = ifm->ifi_index; 2904 2905 if (br_idx) { 2906 br_dev = __dev_get_by_index(net, br_idx); 2907 if (!br_dev) 2908 return -ENODEV; 2909 2910 ops = br_dev->netdev_ops; 2911 } 2912 2913 for_each_netdev(net, dev) { 2914 if (brport_idx && (dev->ifindex != brport_idx)) 2915 continue; 2916 2917 if (!br_idx) { /* user did not specify a specific bridge */ 2918 if (dev->priv_flags & IFF_BRIDGE_PORT) { 2919 br_dev = netdev_master_upper_dev_get(dev); 2920 cops = br_dev->netdev_ops; 2921 } 2922 2923 } else { 2924 if (dev != br_dev && 2925 !(dev->priv_flags & IFF_BRIDGE_PORT)) 2926 continue; 2927 2928 if (br_dev != netdev_master_upper_dev_get(dev) && 2929 !(dev->priv_flags & IFF_EBRIDGE)) 2930 continue; 2931 2932 cops = ops; 2933 } 2934 2935 if (dev->priv_flags & IFF_BRIDGE_PORT) { 2936 if (cops && cops->ndo_fdb_dump) 2937 idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev, 2938 idx); 2939 } 2940 2941 if (dev->netdev_ops->ndo_fdb_dump) 2942 idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL, 2943 idx); 2944 else 2945 idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx); 2946 2947 cops = NULL; 2948 } 2949 2950 cb->args[0] = idx; 2951 return skb->len; 2952 } 2953 2954 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, 2955 unsigned int attrnum, unsigned int flag) 2956 { 2957 if (mask & flag) 2958 return nla_put_u8(skb, attrnum, !!(flags & flag)); 2959 return 0; 2960 } 2961 2962 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 2963 struct net_device *dev, u16 mode, 2964 u32 flags, u32 mask, int nlflags, 2965 u32 filter_mask, 2966 int (*vlan_fill)(struct sk_buff *skb, 2967 struct net_device *dev, 2968 u32 filter_mask)) 2969 { 2970 struct nlmsghdr *nlh; 2971 struct ifinfomsg *ifm; 2972 struct nlattr *br_afspec; 2973 struct nlattr *protinfo; 2974 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN; 2975 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 2976 int err = 0; 2977 2978 nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags); 2979 if (nlh == NULL) 2980 return -EMSGSIZE; 2981 2982 ifm = nlmsg_data(nlh); 2983 ifm->ifi_family = AF_BRIDGE; 2984 ifm->__ifi_pad = 0; 2985 ifm->ifi_type = dev->type; 2986 ifm->ifi_index = dev->ifindex; 2987 ifm->ifi_flags = dev_get_flags(dev); 2988 ifm->ifi_change = 0; 2989 2990 2991 if (nla_put_string(skb, IFLA_IFNAME, dev->name) || 2992 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 2993 nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 2994 (br_dev && 2995 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 2996 (dev->addr_len && 2997 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 2998 (dev->ifindex != dev_get_iflink(dev) && 2999 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev)))) 3000 goto nla_put_failure; 3001 3002 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 3003 if (!br_afspec) 3004 goto nla_put_failure; 3005 3006 if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) { 3007 nla_nest_cancel(skb, br_afspec); 3008 goto nla_put_failure; 3009 } 3010 3011 if (mode != BRIDGE_MODE_UNDEF) { 3012 if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) { 3013 nla_nest_cancel(skb, br_afspec); 3014 goto nla_put_failure; 3015 } 3016 } 3017 if (vlan_fill) { 3018 err = vlan_fill(skb, dev, filter_mask); 3019 if (err) { 3020 nla_nest_cancel(skb, br_afspec); 3021 goto nla_put_failure; 3022 } 3023 } 3024 nla_nest_end(skb, br_afspec); 3025 3026 protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED); 3027 if (!protinfo) 3028 goto nla_put_failure; 3029 3030 if (brport_nla_put_flag(skb, flags, mask, 3031 IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) || 3032 brport_nla_put_flag(skb, flags, mask, 3033 IFLA_BRPORT_GUARD, BR_BPDU_GUARD) || 3034 brport_nla_put_flag(skb, flags, mask, 3035 IFLA_BRPORT_FAST_LEAVE, 3036 BR_MULTICAST_FAST_LEAVE) || 3037 brport_nla_put_flag(skb, flags, mask, 3038 IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) || 3039 brport_nla_put_flag(skb, flags, mask, 3040 IFLA_BRPORT_LEARNING, BR_LEARNING) || 3041 brport_nla_put_flag(skb, flags, mask, 3042 IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) || 3043 brport_nla_put_flag(skb, flags, mask, 3044 IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) || 3045 brport_nla_put_flag(skb, flags, mask, 3046 IFLA_BRPORT_PROXYARP, BR_PROXYARP)) { 3047 nla_nest_cancel(skb, protinfo); 3048 goto nla_put_failure; 3049 } 3050 3051 nla_nest_end(skb, protinfo); 3052 3053 nlmsg_end(skb, nlh); 3054 return 0; 3055 nla_put_failure: 3056 nlmsg_cancel(skb, nlh); 3057 return err ? err : -EMSGSIZE; 3058 } 3059 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink); 3060 3061 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb) 3062 { 3063 struct net *net = sock_net(skb->sk); 3064 struct net_device *dev; 3065 int idx = 0; 3066 u32 portid = NETLINK_CB(cb->skb).portid; 3067 u32 seq = cb->nlh->nlmsg_seq; 3068 u32 filter_mask = 0; 3069 int err; 3070 3071 if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) { 3072 struct nlattr *extfilt; 3073 3074 extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg), 3075 IFLA_EXT_MASK); 3076 if (extfilt) { 3077 if (nla_len(extfilt) < sizeof(filter_mask)) 3078 return -EINVAL; 3079 3080 filter_mask = nla_get_u32(extfilt); 3081 } 3082 } 3083 3084 rcu_read_lock(); 3085 for_each_netdev_rcu(net, dev) { 3086 const struct net_device_ops *ops = dev->netdev_ops; 3087 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3088 3089 if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) { 3090 if (idx >= cb->args[0]) { 3091 err = br_dev->netdev_ops->ndo_bridge_getlink( 3092 skb, portid, seq, dev, 3093 filter_mask, NLM_F_MULTI); 3094 if (err < 0 && err != -EOPNOTSUPP) 3095 break; 3096 } 3097 idx++; 3098 } 3099 3100 if (ops->ndo_bridge_getlink) { 3101 if (idx >= cb->args[0]) { 3102 err = ops->ndo_bridge_getlink(skb, portid, 3103 seq, dev, 3104 filter_mask, 3105 NLM_F_MULTI); 3106 if (err < 0 && err != -EOPNOTSUPP) 3107 break; 3108 } 3109 idx++; 3110 } 3111 } 3112 rcu_read_unlock(); 3113 cb->args[0] = idx; 3114 3115 return skb->len; 3116 } 3117 3118 static inline size_t bridge_nlmsg_size(void) 3119 { 3120 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 3121 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 3122 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 3123 + nla_total_size(sizeof(u32)) /* IFLA_MASTER */ 3124 + nla_total_size(sizeof(u32)) /* IFLA_MTU */ 3125 + nla_total_size(sizeof(u32)) /* IFLA_LINK */ 3126 + nla_total_size(sizeof(u32)) /* IFLA_OPERSTATE */ 3127 + nla_total_size(sizeof(u8)) /* IFLA_PROTINFO */ 3128 + nla_total_size(sizeof(struct nlattr)) /* IFLA_AF_SPEC */ 3129 + nla_total_size(sizeof(u16)) /* IFLA_BRIDGE_FLAGS */ 3130 + nla_total_size(sizeof(u16)); /* IFLA_BRIDGE_MODE */ 3131 } 3132 3133 static int rtnl_bridge_notify(struct net_device *dev) 3134 { 3135 struct net *net = dev_net(dev); 3136 struct sk_buff *skb; 3137 int err = -EOPNOTSUPP; 3138 3139 if (!dev->netdev_ops->ndo_bridge_getlink) 3140 return 0; 3141 3142 skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC); 3143 if (!skb) { 3144 err = -ENOMEM; 3145 goto errout; 3146 } 3147 3148 err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0); 3149 if (err < 0) 3150 goto errout; 3151 3152 if (!skb->len) 3153 goto errout; 3154 3155 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC); 3156 return 0; 3157 errout: 3158 WARN_ON(err == -EMSGSIZE); 3159 kfree_skb(skb); 3160 if (err) 3161 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 3162 return err; 3163 } 3164 3165 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh) 3166 { 3167 struct net *net = sock_net(skb->sk); 3168 struct ifinfomsg *ifm; 3169 struct net_device *dev; 3170 struct nlattr *br_spec, *attr = NULL; 3171 int rem, err = -EOPNOTSUPP; 3172 u16 flags = 0; 3173 bool have_flags = false; 3174 3175 if (nlmsg_len(nlh) < sizeof(*ifm)) 3176 return -EINVAL; 3177 3178 ifm = nlmsg_data(nlh); 3179 if (ifm->ifi_family != AF_BRIDGE) 3180 return -EPFNOSUPPORT; 3181 3182 dev = __dev_get_by_index(net, ifm->ifi_index); 3183 if (!dev) { 3184 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); 3185 return -ENODEV; 3186 } 3187 3188 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 3189 if (br_spec) { 3190 nla_for_each_nested(attr, br_spec, rem) { 3191 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 3192 if (nla_len(attr) < sizeof(flags)) 3193 return -EINVAL; 3194 3195 have_flags = true; 3196 flags = nla_get_u16(attr); 3197 break; 3198 } 3199 } 3200 } 3201 3202 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 3203 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3204 3205 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) { 3206 err = -EOPNOTSUPP; 3207 goto out; 3208 } 3209 3210 err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags); 3211 if (err) 3212 goto out; 3213 3214 flags &= ~BRIDGE_FLAGS_MASTER; 3215 } 3216 3217 if ((flags & BRIDGE_FLAGS_SELF)) { 3218 if (!dev->netdev_ops->ndo_bridge_setlink) 3219 err = -EOPNOTSUPP; 3220 else 3221 err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh, 3222 flags); 3223 if (!err) { 3224 flags &= ~BRIDGE_FLAGS_SELF; 3225 3226 /* Generate event to notify upper layer of bridge 3227 * change 3228 */ 3229 err = rtnl_bridge_notify(dev); 3230 } 3231 } 3232 3233 if (have_flags) 3234 memcpy(nla_data(attr), &flags, sizeof(flags)); 3235 out: 3236 return err; 3237 } 3238 3239 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) 3240 { 3241 struct net *net = sock_net(skb->sk); 3242 struct ifinfomsg *ifm; 3243 struct net_device *dev; 3244 struct nlattr *br_spec, *attr = NULL; 3245 int rem, err = -EOPNOTSUPP; 3246 u16 flags = 0; 3247 bool have_flags = false; 3248 3249 if (nlmsg_len(nlh) < sizeof(*ifm)) 3250 return -EINVAL; 3251 3252 ifm = nlmsg_data(nlh); 3253 if (ifm->ifi_family != AF_BRIDGE) 3254 return -EPFNOSUPPORT; 3255 3256 dev = __dev_get_by_index(net, ifm->ifi_index); 3257 if (!dev) { 3258 pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n"); 3259 return -ENODEV; 3260 } 3261 3262 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 3263 if (br_spec) { 3264 nla_for_each_nested(attr, br_spec, rem) { 3265 if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 3266 if (nla_len(attr) < sizeof(flags)) 3267 return -EINVAL; 3268 3269 have_flags = true; 3270 flags = nla_get_u16(attr); 3271 break; 3272 } 3273 } 3274 } 3275 3276 if (!flags || (flags & BRIDGE_FLAGS_MASTER)) { 3277 struct net_device *br_dev = netdev_master_upper_dev_get(dev); 3278 3279 if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) { 3280 err = -EOPNOTSUPP; 3281 goto out; 3282 } 3283 3284 err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags); 3285 if (err) 3286 goto out; 3287 3288 flags &= ~BRIDGE_FLAGS_MASTER; 3289 } 3290 3291 if ((flags & BRIDGE_FLAGS_SELF)) { 3292 if (!dev->netdev_ops->ndo_bridge_dellink) 3293 err = -EOPNOTSUPP; 3294 else 3295 err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh, 3296 flags); 3297 3298 if (!err) { 3299 flags &= ~BRIDGE_FLAGS_SELF; 3300 3301 /* Generate event to notify upper layer of bridge 3302 * change 3303 */ 3304 err = rtnl_bridge_notify(dev); 3305 } 3306 } 3307 3308 if (have_flags) 3309 memcpy(nla_data(attr), &flags, sizeof(flags)); 3310 out: 3311 return err; 3312 } 3313 3314 /* Process one rtnetlink message. */ 3315 3316 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 3317 { 3318 struct net *net = sock_net(skb->sk); 3319 rtnl_doit_func doit; 3320 int sz_idx, kind; 3321 int family; 3322 int type; 3323 int err; 3324 3325 type = nlh->nlmsg_type; 3326 if (type > RTM_MAX) 3327 return -EOPNOTSUPP; 3328 3329 type -= RTM_BASE; 3330 3331 /* All the messages must have at least 1 byte length */ 3332 if (nlmsg_len(nlh) < sizeof(struct rtgenmsg)) 3333 return 0; 3334 3335 family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family; 3336 sz_idx = type>>2; 3337 kind = type&3; 3338 3339 if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN)) 3340 return -EPERM; 3341 3342 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 3343 struct sock *rtnl; 3344 rtnl_dumpit_func dumpit; 3345 rtnl_calcit_func calcit; 3346 u16 min_dump_alloc = 0; 3347 3348 dumpit = rtnl_get_dumpit(family, type); 3349 if (dumpit == NULL) 3350 return -EOPNOTSUPP; 3351 calcit = rtnl_get_calcit(family, type); 3352 if (calcit) 3353 min_dump_alloc = calcit(skb, nlh); 3354 3355 __rtnl_unlock(); 3356 rtnl = net->rtnl; 3357 { 3358 struct netlink_dump_control c = { 3359 .dump = dumpit, 3360 .min_dump_alloc = min_dump_alloc, 3361 }; 3362 err = netlink_dump_start(rtnl, skb, nlh, &c); 3363 } 3364 rtnl_lock(); 3365 return err; 3366 } 3367 3368 doit = rtnl_get_doit(family, type); 3369 if (doit == NULL) 3370 return -EOPNOTSUPP; 3371 3372 return doit(skb, nlh); 3373 } 3374 3375 static void rtnetlink_rcv(struct sk_buff *skb) 3376 { 3377 rtnl_lock(); 3378 netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 3379 rtnl_unlock(); 3380 } 3381 3382 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 3383 { 3384 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3385 3386 switch (event) { 3387 case NETDEV_UP: 3388 case NETDEV_DOWN: 3389 case NETDEV_PRE_UP: 3390 case NETDEV_POST_INIT: 3391 case NETDEV_REGISTER: 3392 case NETDEV_CHANGE: 3393 case NETDEV_PRE_TYPE_CHANGE: 3394 case NETDEV_GOING_DOWN: 3395 case NETDEV_UNREGISTER: 3396 case NETDEV_UNREGISTER_FINAL: 3397 case NETDEV_RELEASE: 3398 case NETDEV_JOIN: 3399 case NETDEV_BONDING_INFO: 3400 break; 3401 default: 3402 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); 3403 break; 3404 } 3405 return NOTIFY_DONE; 3406 } 3407 3408 static struct notifier_block rtnetlink_dev_notifier = { 3409 .notifier_call = rtnetlink_event, 3410 }; 3411 3412 3413 static int __net_init rtnetlink_net_init(struct net *net) 3414 { 3415 struct sock *sk; 3416 struct netlink_kernel_cfg cfg = { 3417 .groups = RTNLGRP_MAX, 3418 .input = rtnetlink_rcv, 3419 .cb_mutex = &rtnl_mutex, 3420 .flags = NL_CFG_F_NONROOT_RECV, 3421 }; 3422 3423 sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg); 3424 if (!sk) 3425 return -ENOMEM; 3426 net->rtnl = sk; 3427 return 0; 3428 } 3429 3430 static void __net_exit rtnetlink_net_exit(struct net *net) 3431 { 3432 netlink_kernel_release(net->rtnl); 3433 net->rtnl = NULL; 3434 } 3435 3436 static struct pernet_operations rtnetlink_net_ops = { 3437 .init = rtnetlink_net_init, 3438 .exit = rtnetlink_net_exit, 3439 }; 3440 3441 void __init rtnetlink_init(void) 3442 { 3443 if (register_pernet_subsys(&rtnetlink_net_ops)) 3444 panic("rtnetlink_init: cannot initialize rtnetlink\n"); 3445 3446 register_netdevice_notifier(&rtnetlink_dev_notifier); 3447 3448 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 3449 rtnl_dump_ifinfo, rtnl_calcit); 3450 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); 3451 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); 3452 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); 3453 3454 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); 3455 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); 3456 3457 rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL); 3458 rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL); 3459 rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL); 3460 3461 rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL); 3462 rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL); 3463 rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL); 3464 } 3465