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