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