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