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