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/pci.h> 39 40 #include <asm/uaccess.h> 41 42 #include <linux/inet.h> 43 #include <linux/netdevice.h> 44 #include <net/ip.h> 45 #include <net/protocol.h> 46 #include <net/arp.h> 47 #include <net/route.h> 48 #include <net/udp.h> 49 #include <net/sock.h> 50 #include <net/pkt_sched.h> 51 #include <net/fib_rules.h> 52 #include <net/rtnetlink.h> 53 #include <net/net_namespace.h> 54 55 struct rtnl_link { 56 rtnl_doit_func doit; 57 rtnl_dumpit_func dumpit; 58 rtnl_calcit_func calcit; 59 }; 60 61 static DEFINE_MUTEX(rtnl_mutex); 62 63 void rtnl_lock(void) 64 { 65 mutex_lock(&rtnl_mutex); 66 } 67 EXPORT_SYMBOL(rtnl_lock); 68 69 void __rtnl_unlock(void) 70 { 71 mutex_unlock(&rtnl_mutex); 72 } 73 74 void rtnl_unlock(void) 75 { 76 /* This fellow will unlock it for us. */ 77 netdev_run_todo(); 78 } 79 EXPORT_SYMBOL(rtnl_unlock); 80 81 int rtnl_trylock(void) 82 { 83 return mutex_trylock(&rtnl_mutex); 84 } 85 EXPORT_SYMBOL(rtnl_trylock); 86 87 int rtnl_is_locked(void) 88 { 89 return mutex_is_locked(&rtnl_mutex); 90 } 91 EXPORT_SYMBOL(rtnl_is_locked); 92 93 #ifdef CONFIG_PROVE_LOCKING 94 int lockdep_rtnl_is_held(void) 95 { 96 return lockdep_is_held(&rtnl_mutex); 97 } 98 EXPORT_SYMBOL(lockdep_rtnl_is_held); 99 #endif /* #ifdef CONFIG_PROVE_LOCKING */ 100 101 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1]; 102 103 static inline int rtm_msgindex(int msgtype) 104 { 105 int msgindex = msgtype - RTM_BASE; 106 107 /* 108 * msgindex < 0 implies someone tried to register a netlink 109 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that 110 * the message type has not been added to linux/rtnetlink.h 111 */ 112 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES); 113 114 return msgindex; 115 } 116 117 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex) 118 { 119 struct rtnl_link *tab; 120 121 if (protocol <= RTNL_FAMILY_MAX) 122 tab = rtnl_msg_handlers[protocol]; 123 else 124 tab = NULL; 125 126 if (tab == NULL || tab[msgindex].doit == NULL) 127 tab = rtnl_msg_handlers[PF_UNSPEC]; 128 129 return tab ? tab[msgindex].doit : NULL; 130 } 131 132 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex) 133 { 134 struct rtnl_link *tab; 135 136 if (protocol <= RTNL_FAMILY_MAX) 137 tab = rtnl_msg_handlers[protocol]; 138 else 139 tab = NULL; 140 141 if (tab == NULL || tab[msgindex].dumpit == NULL) 142 tab = rtnl_msg_handlers[PF_UNSPEC]; 143 144 return tab ? tab[msgindex].dumpit : NULL; 145 } 146 147 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex) 148 { 149 struct rtnl_link *tab; 150 151 if (protocol <= RTNL_FAMILY_MAX) 152 tab = rtnl_msg_handlers[protocol]; 153 else 154 tab = NULL; 155 156 if (tab == NULL || tab[msgindex].calcit == NULL) 157 tab = rtnl_msg_handlers[PF_UNSPEC]; 158 159 return tab ? tab[msgindex].calcit : NULL; 160 } 161 162 /** 163 * __rtnl_register - Register a rtnetlink message type 164 * @protocol: Protocol family or PF_UNSPEC 165 * @msgtype: rtnetlink message type 166 * @doit: Function pointer called for each request message 167 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message 168 * @calcit: Function pointer to calc size of dump message 169 * 170 * Registers the specified function pointers (at least one of them has 171 * to be non-NULL) to be called whenever a request message for the 172 * specified protocol family and message type is received. 173 * 174 * The special protocol family PF_UNSPEC may be used to define fallback 175 * function pointers for the case when no entry for the specific protocol 176 * family exists. 177 * 178 * Returns 0 on success or a negative error code. 179 */ 180 int __rtnl_register(int protocol, int msgtype, 181 rtnl_doit_func doit, rtnl_dumpit_func dumpit, 182 rtnl_calcit_func calcit) 183 { 184 struct rtnl_link *tab; 185 int msgindex; 186 187 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 188 msgindex = rtm_msgindex(msgtype); 189 190 tab = rtnl_msg_handlers[protocol]; 191 if (tab == NULL) { 192 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL); 193 if (tab == NULL) 194 return -ENOBUFS; 195 196 rtnl_msg_handlers[protocol] = tab; 197 } 198 199 if (doit) 200 tab[msgindex].doit = doit; 201 202 if (dumpit) 203 tab[msgindex].dumpit = dumpit; 204 205 if (calcit) 206 tab[msgindex].calcit = calcit; 207 208 return 0; 209 } 210 EXPORT_SYMBOL_GPL(__rtnl_register); 211 212 /** 213 * rtnl_register - Register a rtnetlink message type 214 * 215 * Identical to __rtnl_register() but panics on failure. This is useful 216 * as failure of this function is very unlikely, it can only happen due 217 * to lack of memory when allocating the chain to store all message 218 * handlers for a protocol. Meant for use in init functions where lack 219 * of memory implies no sense in continuing. 220 */ 221 void rtnl_register(int protocol, int msgtype, 222 rtnl_doit_func doit, rtnl_dumpit_func dumpit, 223 rtnl_calcit_func calcit) 224 { 225 if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0) 226 panic("Unable to register rtnetlink message handler, " 227 "protocol = %d, message type = %d\n", 228 protocol, msgtype); 229 } 230 EXPORT_SYMBOL_GPL(rtnl_register); 231 232 /** 233 * rtnl_unregister - Unregister a rtnetlink message type 234 * @protocol: Protocol family or PF_UNSPEC 235 * @msgtype: rtnetlink message type 236 * 237 * Returns 0 on success or a negative error code. 238 */ 239 int rtnl_unregister(int protocol, int msgtype) 240 { 241 int msgindex; 242 243 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 244 msgindex = rtm_msgindex(msgtype); 245 246 if (rtnl_msg_handlers[protocol] == NULL) 247 return -ENOENT; 248 249 rtnl_msg_handlers[protocol][msgindex].doit = NULL; 250 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL; 251 252 return 0; 253 } 254 EXPORT_SYMBOL_GPL(rtnl_unregister); 255 256 /** 257 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol 258 * @protocol : Protocol family or PF_UNSPEC 259 * 260 * Identical to calling rtnl_unregster() for all registered message types 261 * of a certain protocol family. 262 */ 263 void rtnl_unregister_all(int protocol) 264 { 265 BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX); 266 267 kfree(rtnl_msg_handlers[protocol]); 268 rtnl_msg_handlers[protocol] = NULL; 269 } 270 EXPORT_SYMBOL_GPL(rtnl_unregister_all); 271 272 static LIST_HEAD(link_ops); 273 274 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind) 275 { 276 const struct rtnl_link_ops *ops; 277 278 list_for_each_entry(ops, &link_ops, list) { 279 if (!strcmp(ops->kind, kind)) 280 return ops; 281 } 282 return NULL; 283 } 284 285 /** 286 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink. 287 * @ops: struct rtnl_link_ops * to register 288 * 289 * The caller must hold the rtnl_mutex. This function should be used 290 * by drivers that create devices during module initialization. It 291 * must be called before registering the devices. 292 * 293 * Returns 0 on success or a negative error code. 294 */ 295 int __rtnl_link_register(struct rtnl_link_ops *ops) 296 { 297 if (rtnl_link_ops_get(ops->kind)) 298 return -EEXIST; 299 300 if (!ops->dellink) 301 ops->dellink = unregister_netdevice_queue; 302 303 list_add_tail(&ops->list, &link_ops); 304 return 0; 305 } 306 EXPORT_SYMBOL_GPL(__rtnl_link_register); 307 308 /** 309 * rtnl_link_register - Register rtnl_link_ops with rtnetlink. 310 * @ops: struct rtnl_link_ops * to register 311 * 312 * Returns 0 on success or a negative error code. 313 */ 314 int rtnl_link_register(struct rtnl_link_ops *ops) 315 { 316 int err; 317 318 rtnl_lock(); 319 err = __rtnl_link_register(ops); 320 rtnl_unlock(); 321 return err; 322 } 323 EXPORT_SYMBOL_GPL(rtnl_link_register); 324 325 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops) 326 { 327 struct net_device *dev; 328 LIST_HEAD(list_kill); 329 330 for_each_netdev(net, dev) { 331 if (dev->rtnl_link_ops == ops) 332 ops->dellink(dev, &list_kill); 333 } 334 unregister_netdevice_many(&list_kill); 335 } 336 337 /** 338 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 339 * @ops: struct rtnl_link_ops * to unregister 340 * 341 * The caller must hold the rtnl_mutex. 342 */ 343 void __rtnl_link_unregister(struct rtnl_link_ops *ops) 344 { 345 struct net *net; 346 347 for_each_net(net) { 348 __rtnl_kill_links(net, ops); 349 } 350 list_del(&ops->list); 351 } 352 EXPORT_SYMBOL_GPL(__rtnl_link_unregister); 353 354 /** 355 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink. 356 * @ops: struct rtnl_link_ops * to unregister 357 */ 358 void rtnl_link_unregister(struct rtnl_link_ops *ops) 359 { 360 rtnl_lock(); 361 __rtnl_link_unregister(ops); 362 rtnl_unlock(); 363 } 364 EXPORT_SYMBOL_GPL(rtnl_link_unregister); 365 366 static size_t rtnl_link_get_size(const struct net_device *dev) 367 { 368 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 369 size_t size; 370 371 if (!ops) 372 return 0; 373 374 size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */ 375 nla_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */ 376 377 if (ops->get_size) 378 /* IFLA_INFO_DATA + nested data */ 379 size += nla_total_size(sizeof(struct nlattr)) + 380 ops->get_size(dev); 381 382 if (ops->get_xstats_size) 383 /* IFLA_INFO_XSTATS */ 384 size += nla_total_size(ops->get_xstats_size(dev)); 385 386 return size; 387 } 388 389 static LIST_HEAD(rtnl_af_ops); 390 391 static const struct rtnl_af_ops *rtnl_af_lookup(const int family) 392 { 393 const struct rtnl_af_ops *ops; 394 395 list_for_each_entry(ops, &rtnl_af_ops, list) { 396 if (ops->family == family) 397 return ops; 398 } 399 400 return NULL; 401 } 402 403 /** 404 * __rtnl_af_register - Register rtnl_af_ops with rtnetlink. 405 * @ops: struct rtnl_af_ops * to register 406 * 407 * The caller must hold the rtnl_mutex. 408 * 409 * Returns 0 on success or a negative error code. 410 */ 411 int __rtnl_af_register(struct rtnl_af_ops *ops) 412 { 413 list_add_tail(&ops->list, &rtnl_af_ops); 414 return 0; 415 } 416 EXPORT_SYMBOL_GPL(__rtnl_af_register); 417 418 /** 419 * rtnl_af_register - Register rtnl_af_ops with rtnetlink. 420 * @ops: struct rtnl_af_ops * to register 421 * 422 * Returns 0 on success or a negative error code. 423 */ 424 int rtnl_af_register(struct rtnl_af_ops *ops) 425 { 426 int err; 427 428 rtnl_lock(); 429 err = __rtnl_af_register(ops); 430 rtnl_unlock(); 431 return err; 432 } 433 EXPORT_SYMBOL_GPL(rtnl_af_register); 434 435 /** 436 * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 437 * @ops: struct rtnl_af_ops * to unregister 438 * 439 * The caller must hold the rtnl_mutex. 440 */ 441 void __rtnl_af_unregister(struct rtnl_af_ops *ops) 442 { 443 list_del(&ops->list); 444 } 445 EXPORT_SYMBOL_GPL(__rtnl_af_unregister); 446 447 /** 448 * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink. 449 * @ops: struct rtnl_af_ops * to unregister 450 */ 451 void rtnl_af_unregister(struct rtnl_af_ops *ops) 452 { 453 rtnl_lock(); 454 __rtnl_af_unregister(ops); 455 rtnl_unlock(); 456 } 457 EXPORT_SYMBOL_GPL(rtnl_af_unregister); 458 459 static size_t rtnl_link_get_af_size(const struct net_device *dev) 460 { 461 struct rtnl_af_ops *af_ops; 462 size_t size; 463 464 /* IFLA_AF_SPEC */ 465 size = nla_total_size(sizeof(struct nlattr)); 466 467 list_for_each_entry(af_ops, &rtnl_af_ops, list) { 468 if (af_ops->get_link_af_size) { 469 /* AF_* + nested data */ 470 size += nla_total_size(sizeof(struct nlattr)) + 471 af_ops->get_link_af_size(dev); 472 } 473 } 474 475 return size; 476 } 477 478 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev) 479 { 480 const struct rtnl_link_ops *ops = dev->rtnl_link_ops; 481 struct nlattr *linkinfo, *data; 482 int err = -EMSGSIZE; 483 484 linkinfo = nla_nest_start(skb, IFLA_LINKINFO); 485 if (linkinfo == NULL) 486 goto out; 487 488 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0) 489 goto err_cancel_link; 490 if (ops->fill_xstats) { 491 err = ops->fill_xstats(skb, dev); 492 if (err < 0) 493 goto err_cancel_link; 494 } 495 if (ops->fill_info) { 496 data = nla_nest_start(skb, IFLA_INFO_DATA); 497 if (data == NULL) 498 goto err_cancel_link; 499 err = ops->fill_info(skb, dev); 500 if (err < 0) 501 goto err_cancel_data; 502 nla_nest_end(skb, data); 503 } 504 505 nla_nest_end(skb, linkinfo); 506 return 0; 507 508 err_cancel_data: 509 nla_nest_cancel(skb, data); 510 err_cancel_link: 511 nla_nest_cancel(skb, linkinfo); 512 out: 513 return err; 514 } 515 516 static const int rtm_min[RTM_NR_FAMILIES] = 517 { 518 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)), 519 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), 520 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)), 521 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)), 522 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 523 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 524 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)), 525 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)), 526 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 527 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)), 528 }; 529 530 static const int rta_max[RTM_NR_FAMILIES] = 531 { 532 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX, 533 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX, 534 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX, 535 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX, 536 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX, 537 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX, 538 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX, 539 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX, 540 }; 541 542 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data) 543 { 544 struct rtattr *rta; 545 int size = RTA_LENGTH(attrlen); 546 547 rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size)); 548 rta->rta_type = attrtype; 549 rta->rta_len = size; 550 memcpy(RTA_DATA(rta), data, attrlen); 551 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size); 552 } 553 EXPORT_SYMBOL(__rta_fill); 554 555 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo) 556 { 557 struct sock *rtnl = net->rtnl; 558 int err = 0; 559 560 NETLINK_CB(skb).dst_group = group; 561 if (echo) 562 atomic_inc(&skb->users); 563 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL); 564 if (echo) 565 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); 566 return err; 567 } 568 569 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid) 570 { 571 struct sock *rtnl = net->rtnl; 572 573 return nlmsg_unicast(rtnl, skb, pid); 574 } 575 EXPORT_SYMBOL(rtnl_unicast); 576 577 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group, 578 struct nlmsghdr *nlh, gfp_t flags) 579 { 580 struct sock *rtnl = net->rtnl; 581 int report = 0; 582 583 if (nlh) 584 report = nlmsg_report(nlh); 585 586 nlmsg_notify(rtnl, skb, pid, group, report, flags); 587 } 588 EXPORT_SYMBOL(rtnl_notify); 589 590 void rtnl_set_sk_err(struct net *net, u32 group, int error) 591 { 592 struct sock *rtnl = net->rtnl; 593 594 netlink_set_err(rtnl, 0, group, error); 595 } 596 EXPORT_SYMBOL(rtnl_set_sk_err); 597 598 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics) 599 { 600 struct nlattr *mx; 601 int i, valid = 0; 602 603 mx = nla_nest_start(skb, RTA_METRICS); 604 if (mx == NULL) 605 return -ENOBUFS; 606 607 for (i = 0; i < RTAX_MAX; i++) { 608 if (metrics[i]) { 609 valid++; 610 NLA_PUT_U32(skb, i+1, metrics[i]); 611 } 612 } 613 614 if (!valid) { 615 nla_nest_cancel(skb, mx); 616 return 0; 617 } 618 619 return nla_nest_end(skb, mx); 620 621 nla_put_failure: 622 nla_nest_cancel(skb, mx); 623 return -EMSGSIZE; 624 } 625 EXPORT_SYMBOL(rtnetlink_put_metrics); 626 627 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id, 628 u32 ts, u32 tsage, long expires, u32 error) 629 { 630 struct rta_cacheinfo ci = { 631 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse), 632 .rta_used = dst->__use, 633 .rta_clntref = atomic_read(&(dst->__refcnt)), 634 .rta_error = error, 635 .rta_id = id, 636 .rta_ts = ts, 637 .rta_tsage = tsage, 638 }; 639 640 if (expires) 641 ci.rta_expires = jiffies_to_clock_t(expires); 642 643 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci); 644 } 645 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo); 646 647 static void set_operstate(struct net_device *dev, unsigned char transition) 648 { 649 unsigned char operstate = dev->operstate; 650 651 switch (transition) { 652 case IF_OPER_UP: 653 if ((operstate == IF_OPER_DORMANT || 654 operstate == IF_OPER_UNKNOWN) && 655 !netif_dormant(dev)) 656 operstate = IF_OPER_UP; 657 break; 658 659 case IF_OPER_DORMANT: 660 if (operstate == IF_OPER_UP || 661 operstate == IF_OPER_UNKNOWN) 662 operstate = IF_OPER_DORMANT; 663 break; 664 } 665 666 if (dev->operstate != operstate) { 667 write_lock_bh(&dev_base_lock); 668 dev->operstate = operstate; 669 write_unlock_bh(&dev_base_lock); 670 netdev_state_change(dev); 671 } 672 } 673 674 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev, 675 const struct ifinfomsg *ifm) 676 { 677 unsigned int flags = ifm->ifi_flags; 678 679 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */ 680 if (ifm->ifi_change) 681 flags = (flags & ifm->ifi_change) | 682 (dev->flags & ~ifm->ifi_change); 683 684 return flags; 685 } 686 687 static void copy_rtnl_link_stats(struct rtnl_link_stats *a, 688 const struct rtnl_link_stats64 *b) 689 { 690 a->rx_packets = b->rx_packets; 691 a->tx_packets = b->tx_packets; 692 a->rx_bytes = b->rx_bytes; 693 a->tx_bytes = b->tx_bytes; 694 a->rx_errors = b->rx_errors; 695 a->tx_errors = b->tx_errors; 696 a->rx_dropped = b->rx_dropped; 697 a->tx_dropped = b->tx_dropped; 698 699 a->multicast = b->multicast; 700 a->collisions = b->collisions; 701 702 a->rx_length_errors = b->rx_length_errors; 703 a->rx_over_errors = b->rx_over_errors; 704 a->rx_crc_errors = b->rx_crc_errors; 705 a->rx_frame_errors = b->rx_frame_errors; 706 a->rx_fifo_errors = b->rx_fifo_errors; 707 a->rx_missed_errors = b->rx_missed_errors; 708 709 a->tx_aborted_errors = b->tx_aborted_errors; 710 a->tx_carrier_errors = b->tx_carrier_errors; 711 a->tx_fifo_errors = b->tx_fifo_errors; 712 a->tx_heartbeat_errors = b->tx_heartbeat_errors; 713 a->tx_window_errors = b->tx_window_errors; 714 715 a->rx_compressed = b->rx_compressed; 716 a->tx_compressed = b->tx_compressed; 717 } 718 719 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b) 720 { 721 memcpy(v, b, sizeof(*b)); 722 } 723 724 /* All VF info */ 725 static inline int rtnl_vfinfo_size(const struct net_device *dev, 726 u32 ext_filter_mask) 727 { 728 if (dev->dev.parent && dev_is_pci(dev->dev.parent) && 729 (ext_filter_mask & RTEXT_FILTER_VF)) { 730 int num_vfs = dev_num_vf(dev->dev.parent); 731 size_t size = nla_total_size(sizeof(struct nlattr)); 732 size += nla_total_size(num_vfs * sizeof(struct nlattr)); 733 size += num_vfs * 734 (nla_total_size(sizeof(struct ifla_vf_mac)) + 735 nla_total_size(sizeof(struct ifla_vf_vlan)) + 736 nla_total_size(sizeof(struct ifla_vf_tx_rate)) + 737 nla_total_size(sizeof(struct ifla_vf_spoofchk))); 738 return size; 739 } else 740 return 0; 741 } 742 743 static size_t rtnl_port_size(const struct net_device *dev) 744 { 745 size_t port_size = nla_total_size(4) /* PORT_VF */ 746 + nla_total_size(PORT_PROFILE_MAX) /* PORT_PROFILE */ 747 + nla_total_size(sizeof(struct ifla_port_vsi)) 748 /* PORT_VSI_TYPE */ 749 + nla_total_size(PORT_UUID_MAX) /* PORT_INSTANCE_UUID */ 750 + nla_total_size(PORT_UUID_MAX) /* PORT_HOST_UUID */ 751 + nla_total_size(1) /* PROT_VDP_REQUEST */ 752 + nla_total_size(2); /* PORT_VDP_RESPONSE */ 753 size_t vf_ports_size = nla_total_size(sizeof(struct nlattr)); 754 size_t vf_port_size = nla_total_size(sizeof(struct nlattr)) 755 + port_size; 756 size_t port_self_size = nla_total_size(sizeof(struct nlattr)) 757 + port_size; 758 759 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) 760 return 0; 761 if (dev_num_vf(dev->dev.parent)) 762 return port_self_size + vf_ports_size + 763 vf_port_size * dev_num_vf(dev->dev.parent); 764 else 765 return port_self_size; 766 } 767 768 static noinline size_t if_nlmsg_size(const struct net_device *dev, 769 u32 ext_filter_mask) 770 { 771 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 772 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 773 + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */ 774 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */ 775 + nla_total_size(sizeof(struct rtnl_link_ifmap)) 776 + nla_total_size(sizeof(struct rtnl_link_stats)) 777 + nla_total_size(sizeof(struct rtnl_link_stats64)) 778 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */ 779 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */ 780 + nla_total_size(4) /* IFLA_TXQLEN */ 781 + nla_total_size(4) /* IFLA_WEIGHT */ 782 + nla_total_size(4) /* IFLA_MTU */ 783 + nla_total_size(4) /* IFLA_LINK */ 784 + nla_total_size(4) /* IFLA_MASTER */ 785 + nla_total_size(1) /* IFLA_OPERSTATE */ 786 + nla_total_size(1) /* IFLA_LINKMODE */ 787 + nla_total_size(ext_filter_mask 788 & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */ 789 + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */ 790 + rtnl_port_size(dev) /* IFLA_VF_PORTS + IFLA_PORT_SELF */ 791 + rtnl_link_get_size(dev) /* IFLA_LINKINFO */ 792 + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */ 793 } 794 795 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev) 796 { 797 struct nlattr *vf_ports; 798 struct nlattr *vf_port; 799 int vf; 800 int err; 801 802 vf_ports = nla_nest_start(skb, IFLA_VF_PORTS); 803 if (!vf_ports) 804 return -EMSGSIZE; 805 806 for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) { 807 vf_port = nla_nest_start(skb, IFLA_VF_PORT); 808 if (!vf_port) 809 goto nla_put_failure; 810 NLA_PUT_U32(skb, IFLA_PORT_VF, vf); 811 err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb); 812 if (err == -EMSGSIZE) 813 goto nla_put_failure; 814 if (err) { 815 nla_nest_cancel(skb, vf_port); 816 continue; 817 } 818 nla_nest_end(skb, vf_port); 819 } 820 821 nla_nest_end(skb, vf_ports); 822 823 return 0; 824 825 nla_put_failure: 826 nla_nest_cancel(skb, vf_ports); 827 return -EMSGSIZE; 828 } 829 830 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev) 831 { 832 struct nlattr *port_self; 833 int err; 834 835 port_self = nla_nest_start(skb, IFLA_PORT_SELF); 836 if (!port_self) 837 return -EMSGSIZE; 838 839 err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb); 840 if (err) { 841 nla_nest_cancel(skb, port_self); 842 return (err == -EMSGSIZE) ? err : 0; 843 } 844 845 nla_nest_end(skb, port_self); 846 847 return 0; 848 } 849 850 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev) 851 { 852 int err; 853 854 if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent) 855 return 0; 856 857 err = rtnl_port_self_fill(skb, dev); 858 if (err) 859 return err; 860 861 if (dev_num_vf(dev->dev.parent)) { 862 err = rtnl_vf_ports_fill(skb, dev); 863 if (err) 864 return err; 865 } 866 867 return 0; 868 } 869 870 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev, 871 int type, u32 pid, u32 seq, u32 change, 872 unsigned int flags, u32 ext_filter_mask) 873 { 874 struct ifinfomsg *ifm; 875 struct nlmsghdr *nlh; 876 struct rtnl_link_stats64 temp; 877 const struct rtnl_link_stats64 *stats; 878 struct nlattr *attr, *af_spec; 879 struct rtnl_af_ops *af_ops; 880 881 ASSERT_RTNL(); 882 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags); 883 if (nlh == NULL) 884 return -EMSGSIZE; 885 886 ifm = nlmsg_data(nlh); 887 ifm->ifi_family = AF_UNSPEC; 888 ifm->__ifi_pad = 0; 889 ifm->ifi_type = dev->type; 890 ifm->ifi_index = dev->ifindex; 891 ifm->ifi_flags = dev_get_flags(dev); 892 ifm->ifi_change = change; 893 894 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name); 895 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len); 896 NLA_PUT_U8(skb, IFLA_OPERSTATE, 897 netif_running(dev) ? dev->operstate : IF_OPER_DOWN); 898 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode); 899 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu); 900 NLA_PUT_U32(skb, IFLA_GROUP, dev->group); 901 902 if (dev->ifindex != dev->iflink) 903 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink); 904 905 if (dev->master) 906 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex); 907 908 if (dev->qdisc) 909 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id); 910 911 if (dev->ifalias) 912 NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias); 913 914 if (1) { 915 struct rtnl_link_ifmap map = { 916 .mem_start = dev->mem_start, 917 .mem_end = dev->mem_end, 918 .base_addr = dev->base_addr, 919 .irq = dev->irq, 920 .dma = dev->dma, 921 .port = dev->if_port, 922 }; 923 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map); 924 } 925 926 if (dev->addr_len) { 927 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr); 928 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast); 929 } 930 931 attr = nla_reserve(skb, IFLA_STATS, 932 sizeof(struct rtnl_link_stats)); 933 if (attr == NULL) 934 goto nla_put_failure; 935 936 stats = dev_get_stats(dev, &temp); 937 copy_rtnl_link_stats(nla_data(attr), stats); 938 939 attr = nla_reserve(skb, IFLA_STATS64, 940 sizeof(struct rtnl_link_stats64)); 941 if (attr == NULL) 942 goto nla_put_failure; 943 copy_rtnl_link_stats64(nla_data(attr), stats); 944 945 if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) 946 NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)); 947 948 if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent 949 && (ext_filter_mask & RTEXT_FILTER_VF)) { 950 int i; 951 952 struct nlattr *vfinfo, *vf; 953 int num_vfs = dev_num_vf(dev->dev.parent); 954 955 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST); 956 if (!vfinfo) 957 goto nla_put_failure; 958 for (i = 0; i < num_vfs; i++) { 959 struct ifla_vf_info ivi; 960 struct ifla_vf_mac vf_mac; 961 struct ifla_vf_vlan vf_vlan; 962 struct ifla_vf_tx_rate vf_tx_rate; 963 struct ifla_vf_spoofchk vf_spoofchk; 964 965 /* 966 * Not all SR-IOV capable drivers support the 967 * spoofcheck query. Preset to -1 so the user 968 * space tool can detect that the driver didn't 969 * report anything. 970 */ 971 ivi.spoofchk = -1; 972 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi)) 973 break; 974 vf_mac.vf = 975 vf_vlan.vf = 976 vf_tx_rate.vf = 977 vf_spoofchk.vf = ivi.vf; 978 979 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 980 vf_vlan.vlan = ivi.vlan; 981 vf_vlan.qos = ivi.qos; 982 vf_tx_rate.rate = ivi.tx_rate; 983 vf_spoofchk.setting = ivi.spoofchk; 984 vf = nla_nest_start(skb, IFLA_VF_INFO); 985 if (!vf) { 986 nla_nest_cancel(skb, vfinfo); 987 goto nla_put_failure; 988 } 989 NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac); 990 NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan); 991 NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate), 992 &vf_tx_rate); 993 NLA_PUT(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 994 &vf_spoofchk); 995 nla_nest_end(skb, vf); 996 } 997 nla_nest_end(skb, vfinfo); 998 } 999 1000 if (rtnl_port_fill(skb, dev)) 1001 goto nla_put_failure; 1002 1003 if (dev->rtnl_link_ops) { 1004 if (rtnl_link_fill(skb, dev) < 0) 1005 goto nla_put_failure; 1006 } 1007 1008 if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC))) 1009 goto nla_put_failure; 1010 1011 list_for_each_entry(af_ops, &rtnl_af_ops, list) { 1012 if (af_ops->fill_link_af) { 1013 struct nlattr *af; 1014 int err; 1015 1016 if (!(af = nla_nest_start(skb, af_ops->family))) 1017 goto nla_put_failure; 1018 1019 err = af_ops->fill_link_af(skb, dev); 1020 1021 /* 1022 * Caller may return ENODATA to indicate that there 1023 * was no data to be dumped. This is not an error, it 1024 * means we should trim the attribute header and 1025 * continue. 1026 */ 1027 if (err == -ENODATA) 1028 nla_nest_cancel(skb, af); 1029 else if (err < 0) 1030 goto nla_put_failure; 1031 1032 nla_nest_end(skb, af); 1033 } 1034 } 1035 1036 nla_nest_end(skb, af_spec); 1037 1038 return nlmsg_end(skb, nlh); 1039 1040 nla_put_failure: 1041 nlmsg_cancel(skb, nlh); 1042 return -EMSGSIZE; 1043 } 1044 1045 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb) 1046 { 1047 struct net *net = sock_net(skb->sk); 1048 int h, s_h; 1049 int idx = 0, s_idx; 1050 struct net_device *dev; 1051 struct hlist_head *head; 1052 struct hlist_node *node; 1053 struct nlattr *tb[IFLA_MAX+1]; 1054 u32 ext_filter_mask = 0; 1055 1056 s_h = cb->args[0]; 1057 s_idx = cb->args[1]; 1058 1059 rcu_read_lock(); 1060 cb->seq = net->dev_base_seq; 1061 1062 if (nlmsg_parse(cb->nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX, 1063 ifla_policy) >= 0) { 1064 1065 if (tb[IFLA_EXT_MASK]) 1066 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1067 } 1068 1069 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) { 1070 idx = 0; 1071 head = &net->dev_index_head[h]; 1072 hlist_for_each_entry_rcu(dev, node, head, index_hlist) { 1073 if (idx < s_idx) 1074 goto cont; 1075 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK, 1076 NETLINK_CB(cb->skb).pid, 1077 cb->nlh->nlmsg_seq, 0, 1078 NLM_F_MULTI, 1079 ext_filter_mask) <= 0) 1080 goto out; 1081 1082 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 1083 cont: 1084 idx++; 1085 } 1086 } 1087 out: 1088 rcu_read_unlock(); 1089 cb->args[1] = idx; 1090 cb->args[0] = h; 1091 1092 return skb->len; 1093 } 1094 1095 const struct nla_policy ifla_policy[IFLA_MAX+1] = { 1096 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 }, 1097 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1098 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN }, 1099 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) }, 1100 [IFLA_MTU] = { .type = NLA_U32 }, 1101 [IFLA_LINK] = { .type = NLA_U32 }, 1102 [IFLA_MASTER] = { .type = NLA_U32 }, 1103 [IFLA_TXQLEN] = { .type = NLA_U32 }, 1104 [IFLA_WEIGHT] = { .type = NLA_U32 }, 1105 [IFLA_OPERSTATE] = { .type = NLA_U8 }, 1106 [IFLA_LINKMODE] = { .type = NLA_U8 }, 1107 [IFLA_LINKINFO] = { .type = NLA_NESTED }, 1108 [IFLA_NET_NS_PID] = { .type = NLA_U32 }, 1109 [IFLA_NET_NS_FD] = { .type = NLA_U32 }, 1110 [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, 1111 [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, 1112 [IFLA_VF_PORTS] = { .type = NLA_NESTED }, 1113 [IFLA_PORT_SELF] = { .type = NLA_NESTED }, 1114 [IFLA_AF_SPEC] = { .type = NLA_NESTED }, 1115 [IFLA_EXT_MASK] = { .type = NLA_U32 }, 1116 }; 1117 EXPORT_SYMBOL(ifla_policy); 1118 1119 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { 1120 [IFLA_INFO_KIND] = { .type = NLA_STRING }, 1121 [IFLA_INFO_DATA] = { .type = NLA_NESTED }, 1122 }; 1123 1124 static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = { 1125 [IFLA_VF_INFO] = { .type = NLA_NESTED }, 1126 }; 1127 1128 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = { 1129 [IFLA_VF_MAC] = { .type = NLA_BINARY, 1130 .len = sizeof(struct ifla_vf_mac) }, 1131 [IFLA_VF_VLAN] = { .type = NLA_BINARY, 1132 .len = sizeof(struct ifla_vf_vlan) }, 1133 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY, 1134 .len = sizeof(struct ifla_vf_tx_rate) }, 1135 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY, 1136 .len = sizeof(struct ifla_vf_spoofchk) }, 1137 }; 1138 1139 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1140 [IFLA_PORT_VF] = { .type = NLA_U32 }, 1141 [IFLA_PORT_PROFILE] = { .type = NLA_STRING, 1142 .len = PORT_PROFILE_MAX }, 1143 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY, 1144 .len = sizeof(struct ifla_port_vsi)}, 1145 [IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY, 1146 .len = PORT_UUID_MAX }, 1147 [IFLA_PORT_HOST_UUID] = { .type = NLA_STRING, 1148 .len = PORT_UUID_MAX }, 1149 [IFLA_PORT_REQUEST] = { .type = NLA_U8, }, 1150 [IFLA_PORT_RESPONSE] = { .type = NLA_U16, }, 1151 }; 1152 1153 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) 1154 { 1155 struct net *net; 1156 /* Examine the link attributes and figure out which 1157 * network namespace we are talking about. 1158 */ 1159 if (tb[IFLA_NET_NS_PID]) 1160 net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); 1161 else if (tb[IFLA_NET_NS_FD]) 1162 net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); 1163 else 1164 net = get_net(src_net); 1165 return net; 1166 } 1167 EXPORT_SYMBOL(rtnl_link_get_net); 1168 1169 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) 1170 { 1171 if (dev) { 1172 if (tb[IFLA_ADDRESS] && 1173 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len) 1174 return -EINVAL; 1175 1176 if (tb[IFLA_BROADCAST] && 1177 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len) 1178 return -EINVAL; 1179 } 1180 1181 if (tb[IFLA_AF_SPEC]) { 1182 struct nlattr *af; 1183 int rem, err; 1184 1185 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1186 const struct rtnl_af_ops *af_ops; 1187 1188 if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1189 return -EAFNOSUPPORT; 1190 1191 if (!af_ops->set_link_af) 1192 return -EOPNOTSUPP; 1193 1194 if (af_ops->validate_link_af) { 1195 err = af_ops->validate_link_af(dev, af); 1196 if (err < 0) 1197 return err; 1198 } 1199 } 1200 } 1201 1202 return 0; 1203 } 1204 1205 static int do_setvfinfo(struct net_device *dev, struct nlattr *attr) 1206 { 1207 int rem, err = -EINVAL; 1208 struct nlattr *vf; 1209 const struct net_device_ops *ops = dev->netdev_ops; 1210 1211 nla_for_each_nested(vf, attr, rem) { 1212 switch (nla_type(vf)) { 1213 case IFLA_VF_MAC: { 1214 struct ifla_vf_mac *ivm; 1215 ivm = nla_data(vf); 1216 err = -EOPNOTSUPP; 1217 if (ops->ndo_set_vf_mac) 1218 err = ops->ndo_set_vf_mac(dev, ivm->vf, 1219 ivm->mac); 1220 break; 1221 } 1222 case IFLA_VF_VLAN: { 1223 struct ifla_vf_vlan *ivv; 1224 ivv = nla_data(vf); 1225 err = -EOPNOTSUPP; 1226 if (ops->ndo_set_vf_vlan) 1227 err = ops->ndo_set_vf_vlan(dev, ivv->vf, 1228 ivv->vlan, 1229 ivv->qos); 1230 break; 1231 } 1232 case IFLA_VF_TX_RATE: { 1233 struct ifla_vf_tx_rate *ivt; 1234 ivt = nla_data(vf); 1235 err = -EOPNOTSUPP; 1236 if (ops->ndo_set_vf_tx_rate) 1237 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf, 1238 ivt->rate); 1239 break; 1240 } 1241 case IFLA_VF_SPOOFCHK: { 1242 struct ifla_vf_spoofchk *ivs; 1243 ivs = nla_data(vf); 1244 err = -EOPNOTSUPP; 1245 if (ops->ndo_set_vf_spoofchk) 1246 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf, 1247 ivs->setting); 1248 break; 1249 } 1250 default: 1251 err = -EINVAL; 1252 break; 1253 } 1254 if (err) 1255 break; 1256 } 1257 return err; 1258 } 1259 1260 static int do_set_master(struct net_device *dev, int ifindex) 1261 { 1262 struct net_device *master_dev; 1263 const struct net_device_ops *ops; 1264 int err; 1265 1266 if (dev->master) { 1267 if (dev->master->ifindex == ifindex) 1268 return 0; 1269 ops = dev->master->netdev_ops; 1270 if (ops->ndo_del_slave) { 1271 err = ops->ndo_del_slave(dev->master, dev); 1272 if (err) 1273 return err; 1274 } else { 1275 return -EOPNOTSUPP; 1276 } 1277 } 1278 1279 if (ifindex) { 1280 master_dev = __dev_get_by_index(dev_net(dev), ifindex); 1281 if (!master_dev) 1282 return -EINVAL; 1283 ops = master_dev->netdev_ops; 1284 if (ops->ndo_add_slave) { 1285 err = ops->ndo_add_slave(master_dev, dev); 1286 if (err) 1287 return err; 1288 } else { 1289 return -EOPNOTSUPP; 1290 } 1291 } 1292 return 0; 1293 } 1294 1295 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, 1296 struct nlattr **tb, char *ifname, int modified) 1297 { 1298 const struct net_device_ops *ops = dev->netdev_ops; 1299 int send_addr_notify = 0; 1300 int err; 1301 1302 if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { 1303 struct net *net = rtnl_link_get_net(dev_net(dev), tb); 1304 if (IS_ERR(net)) { 1305 err = PTR_ERR(net); 1306 goto errout; 1307 } 1308 err = dev_change_net_namespace(dev, net, ifname); 1309 put_net(net); 1310 if (err) 1311 goto errout; 1312 modified = 1; 1313 } 1314 1315 if (tb[IFLA_MAP]) { 1316 struct rtnl_link_ifmap *u_map; 1317 struct ifmap k_map; 1318 1319 if (!ops->ndo_set_config) { 1320 err = -EOPNOTSUPP; 1321 goto errout; 1322 } 1323 1324 if (!netif_device_present(dev)) { 1325 err = -ENODEV; 1326 goto errout; 1327 } 1328 1329 u_map = nla_data(tb[IFLA_MAP]); 1330 k_map.mem_start = (unsigned long) u_map->mem_start; 1331 k_map.mem_end = (unsigned long) u_map->mem_end; 1332 k_map.base_addr = (unsigned short) u_map->base_addr; 1333 k_map.irq = (unsigned char) u_map->irq; 1334 k_map.dma = (unsigned char) u_map->dma; 1335 k_map.port = (unsigned char) u_map->port; 1336 1337 err = ops->ndo_set_config(dev, &k_map); 1338 if (err < 0) 1339 goto errout; 1340 1341 modified = 1; 1342 } 1343 1344 if (tb[IFLA_ADDRESS]) { 1345 struct sockaddr *sa; 1346 int len; 1347 1348 if (!ops->ndo_set_mac_address) { 1349 err = -EOPNOTSUPP; 1350 goto errout; 1351 } 1352 1353 if (!netif_device_present(dev)) { 1354 err = -ENODEV; 1355 goto errout; 1356 } 1357 1358 len = sizeof(sa_family_t) + dev->addr_len; 1359 sa = kmalloc(len, GFP_KERNEL); 1360 if (!sa) { 1361 err = -ENOMEM; 1362 goto errout; 1363 } 1364 sa->sa_family = dev->type; 1365 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]), 1366 dev->addr_len); 1367 err = ops->ndo_set_mac_address(dev, sa); 1368 kfree(sa); 1369 if (err) 1370 goto errout; 1371 send_addr_notify = 1; 1372 modified = 1; 1373 } 1374 1375 if (tb[IFLA_MTU]) { 1376 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU])); 1377 if (err < 0) 1378 goto errout; 1379 modified = 1; 1380 } 1381 1382 if (tb[IFLA_GROUP]) { 1383 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 1384 modified = 1; 1385 } 1386 1387 /* 1388 * Interface selected by interface index but interface 1389 * name provided implies that a name change has been 1390 * requested. 1391 */ 1392 if (ifm->ifi_index > 0 && ifname[0]) { 1393 err = dev_change_name(dev, ifname); 1394 if (err < 0) 1395 goto errout; 1396 modified = 1; 1397 } 1398 1399 if (tb[IFLA_IFALIAS]) { 1400 err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]), 1401 nla_len(tb[IFLA_IFALIAS])); 1402 if (err < 0) 1403 goto errout; 1404 modified = 1; 1405 } 1406 1407 if (tb[IFLA_BROADCAST]) { 1408 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len); 1409 send_addr_notify = 1; 1410 } 1411 1412 if (ifm->ifi_flags || ifm->ifi_change) { 1413 err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 1414 if (err < 0) 1415 goto errout; 1416 } 1417 1418 if (tb[IFLA_MASTER]) { 1419 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER])); 1420 if (err) 1421 goto errout; 1422 modified = 1; 1423 } 1424 1425 if (tb[IFLA_TXQLEN]) 1426 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 1427 1428 if (tb[IFLA_OPERSTATE]) 1429 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1430 1431 if (tb[IFLA_LINKMODE]) { 1432 write_lock_bh(&dev_base_lock); 1433 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 1434 write_unlock_bh(&dev_base_lock); 1435 } 1436 1437 if (tb[IFLA_VFINFO_LIST]) { 1438 struct nlattr *attr; 1439 int rem; 1440 nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) { 1441 if (nla_type(attr) != IFLA_VF_INFO) { 1442 err = -EINVAL; 1443 goto errout; 1444 } 1445 err = do_setvfinfo(dev, attr); 1446 if (err < 0) 1447 goto errout; 1448 modified = 1; 1449 } 1450 } 1451 err = 0; 1452 1453 if (tb[IFLA_VF_PORTS]) { 1454 struct nlattr *port[IFLA_PORT_MAX+1]; 1455 struct nlattr *attr; 1456 int vf; 1457 int rem; 1458 1459 err = -EOPNOTSUPP; 1460 if (!ops->ndo_set_vf_port) 1461 goto errout; 1462 1463 nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) { 1464 if (nla_type(attr) != IFLA_VF_PORT) 1465 continue; 1466 err = nla_parse_nested(port, IFLA_PORT_MAX, 1467 attr, ifla_port_policy); 1468 if (err < 0) 1469 goto errout; 1470 if (!port[IFLA_PORT_VF]) { 1471 err = -EOPNOTSUPP; 1472 goto errout; 1473 } 1474 vf = nla_get_u32(port[IFLA_PORT_VF]); 1475 err = ops->ndo_set_vf_port(dev, vf, port); 1476 if (err < 0) 1477 goto errout; 1478 modified = 1; 1479 } 1480 } 1481 err = 0; 1482 1483 if (tb[IFLA_PORT_SELF]) { 1484 struct nlattr *port[IFLA_PORT_MAX+1]; 1485 1486 err = nla_parse_nested(port, IFLA_PORT_MAX, 1487 tb[IFLA_PORT_SELF], ifla_port_policy); 1488 if (err < 0) 1489 goto errout; 1490 1491 err = -EOPNOTSUPP; 1492 if (ops->ndo_set_vf_port) 1493 err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port); 1494 if (err < 0) 1495 goto errout; 1496 modified = 1; 1497 } 1498 1499 if (tb[IFLA_AF_SPEC]) { 1500 struct nlattr *af; 1501 int rem; 1502 1503 nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) { 1504 const struct rtnl_af_ops *af_ops; 1505 1506 if (!(af_ops = rtnl_af_lookup(nla_type(af)))) 1507 BUG(); 1508 1509 err = af_ops->set_link_af(dev, af); 1510 if (err < 0) 1511 goto errout; 1512 1513 modified = 1; 1514 } 1515 } 1516 err = 0; 1517 1518 errout: 1519 if (err < 0 && modified && net_ratelimit()) 1520 printk(KERN_WARNING "A link change request failed with " 1521 "some changes committed already. Interface %s may " 1522 "have been left with an inconsistent configuration, " 1523 "please check.\n", dev->name); 1524 1525 if (send_addr_notify) 1526 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); 1527 1528 return err; 1529 } 1530 1531 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1532 { 1533 struct net *net = sock_net(skb->sk); 1534 struct ifinfomsg *ifm; 1535 struct net_device *dev; 1536 int err; 1537 struct nlattr *tb[IFLA_MAX+1]; 1538 char ifname[IFNAMSIZ]; 1539 1540 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1541 if (err < 0) 1542 goto errout; 1543 1544 if (tb[IFLA_IFNAME]) 1545 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1546 else 1547 ifname[0] = '\0'; 1548 1549 err = -EINVAL; 1550 ifm = nlmsg_data(nlh); 1551 if (ifm->ifi_index > 0) 1552 dev = __dev_get_by_index(net, ifm->ifi_index); 1553 else if (tb[IFLA_IFNAME]) 1554 dev = __dev_get_by_name(net, ifname); 1555 else 1556 goto errout; 1557 1558 if (dev == NULL) { 1559 err = -ENODEV; 1560 goto errout; 1561 } 1562 1563 err = validate_linkmsg(dev, tb); 1564 if (err < 0) 1565 goto errout; 1566 1567 err = do_setlink(dev, ifm, tb, ifname, 0); 1568 errout: 1569 return err; 1570 } 1571 1572 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1573 { 1574 struct net *net = sock_net(skb->sk); 1575 const struct rtnl_link_ops *ops; 1576 struct net_device *dev; 1577 struct ifinfomsg *ifm; 1578 char ifname[IFNAMSIZ]; 1579 struct nlattr *tb[IFLA_MAX+1]; 1580 int err; 1581 LIST_HEAD(list_kill); 1582 1583 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1584 if (err < 0) 1585 return err; 1586 1587 if (tb[IFLA_IFNAME]) 1588 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1589 1590 ifm = nlmsg_data(nlh); 1591 if (ifm->ifi_index > 0) 1592 dev = __dev_get_by_index(net, ifm->ifi_index); 1593 else if (tb[IFLA_IFNAME]) 1594 dev = __dev_get_by_name(net, ifname); 1595 else 1596 return -EINVAL; 1597 1598 if (!dev) 1599 return -ENODEV; 1600 1601 ops = dev->rtnl_link_ops; 1602 if (!ops) 1603 return -EOPNOTSUPP; 1604 1605 ops->dellink(dev, &list_kill); 1606 unregister_netdevice_many(&list_kill); 1607 list_del(&list_kill); 1608 return 0; 1609 } 1610 1611 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm) 1612 { 1613 unsigned int old_flags; 1614 int err; 1615 1616 old_flags = dev->flags; 1617 if (ifm && (ifm->ifi_flags || ifm->ifi_change)) { 1618 err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm)); 1619 if (err < 0) 1620 return err; 1621 } 1622 1623 dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 1624 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 1625 1626 __dev_notify_flags(dev, old_flags); 1627 return 0; 1628 } 1629 EXPORT_SYMBOL(rtnl_configure_link); 1630 1631 struct net_device *rtnl_create_link(struct net *src_net, struct net *net, 1632 char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]) 1633 { 1634 int err; 1635 struct net_device *dev; 1636 unsigned int num_queues = 1; 1637 unsigned int real_num_queues = 1; 1638 1639 if (ops->get_tx_queues) { 1640 err = ops->get_tx_queues(src_net, tb, &num_queues, 1641 &real_num_queues); 1642 if (err) 1643 goto err; 1644 } 1645 err = -ENOMEM; 1646 dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues); 1647 if (!dev) 1648 goto err; 1649 1650 dev_net_set(dev, net); 1651 dev->rtnl_link_ops = ops; 1652 dev->rtnl_link_state = RTNL_LINK_INITIALIZING; 1653 1654 if (tb[IFLA_MTU]) 1655 dev->mtu = nla_get_u32(tb[IFLA_MTU]); 1656 if (tb[IFLA_ADDRESS]) 1657 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]), 1658 nla_len(tb[IFLA_ADDRESS])); 1659 if (tb[IFLA_BROADCAST]) 1660 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]), 1661 nla_len(tb[IFLA_BROADCAST])); 1662 if (tb[IFLA_TXQLEN]) 1663 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]); 1664 if (tb[IFLA_OPERSTATE]) 1665 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE])); 1666 if (tb[IFLA_LINKMODE]) 1667 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]); 1668 if (tb[IFLA_GROUP]) 1669 dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP])); 1670 1671 return dev; 1672 1673 err: 1674 return ERR_PTR(err); 1675 } 1676 EXPORT_SYMBOL(rtnl_create_link); 1677 1678 static int rtnl_group_changelink(struct net *net, int group, 1679 struct ifinfomsg *ifm, 1680 struct nlattr **tb) 1681 { 1682 struct net_device *dev; 1683 int err; 1684 1685 for_each_netdev(net, dev) { 1686 if (dev->group == group) { 1687 err = do_setlink(dev, ifm, tb, NULL, 0); 1688 if (err < 0) 1689 return err; 1690 } 1691 } 1692 1693 return 0; 1694 } 1695 1696 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) 1697 { 1698 struct net *net = sock_net(skb->sk); 1699 const struct rtnl_link_ops *ops; 1700 struct net_device *dev; 1701 struct ifinfomsg *ifm; 1702 char kind[MODULE_NAME_LEN]; 1703 char ifname[IFNAMSIZ]; 1704 struct nlattr *tb[IFLA_MAX+1]; 1705 struct nlattr *linkinfo[IFLA_INFO_MAX+1]; 1706 int err; 1707 1708 #ifdef CONFIG_MODULES 1709 replay: 1710 #endif 1711 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1712 if (err < 0) 1713 return err; 1714 1715 if (tb[IFLA_IFNAME]) 1716 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1717 else 1718 ifname[0] = '\0'; 1719 1720 ifm = nlmsg_data(nlh); 1721 if (ifm->ifi_index > 0) 1722 dev = __dev_get_by_index(net, ifm->ifi_index); 1723 else { 1724 if (ifname[0]) 1725 dev = __dev_get_by_name(net, ifname); 1726 else 1727 dev = NULL; 1728 } 1729 1730 err = validate_linkmsg(dev, tb); 1731 if (err < 0) 1732 return err; 1733 1734 if (tb[IFLA_LINKINFO]) { 1735 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX, 1736 tb[IFLA_LINKINFO], ifla_info_policy); 1737 if (err < 0) 1738 return err; 1739 } else 1740 memset(linkinfo, 0, sizeof(linkinfo)); 1741 1742 if (linkinfo[IFLA_INFO_KIND]) { 1743 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind)); 1744 ops = rtnl_link_ops_get(kind); 1745 } else { 1746 kind[0] = '\0'; 1747 ops = NULL; 1748 } 1749 1750 if (1) { 1751 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; 1752 struct net *dest_net; 1753 1754 if (ops) { 1755 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { 1756 err = nla_parse_nested(attr, ops->maxtype, 1757 linkinfo[IFLA_INFO_DATA], 1758 ops->policy); 1759 if (err < 0) 1760 return err; 1761 data = attr; 1762 } 1763 if (ops->validate) { 1764 err = ops->validate(tb, data); 1765 if (err < 0) 1766 return err; 1767 } 1768 } 1769 1770 if (dev) { 1771 int modified = 0; 1772 1773 if (nlh->nlmsg_flags & NLM_F_EXCL) 1774 return -EEXIST; 1775 if (nlh->nlmsg_flags & NLM_F_REPLACE) 1776 return -EOPNOTSUPP; 1777 1778 if (linkinfo[IFLA_INFO_DATA]) { 1779 if (!ops || ops != dev->rtnl_link_ops || 1780 !ops->changelink) 1781 return -EOPNOTSUPP; 1782 1783 err = ops->changelink(dev, tb, data); 1784 if (err < 0) 1785 return err; 1786 modified = 1; 1787 } 1788 1789 return do_setlink(dev, ifm, tb, ifname, modified); 1790 } 1791 1792 if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { 1793 if (ifm->ifi_index == 0 && tb[IFLA_GROUP]) 1794 return rtnl_group_changelink(net, 1795 nla_get_u32(tb[IFLA_GROUP]), 1796 ifm, tb); 1797 return -ENODEV; 1798 } 1799 1800 if (ifm->ifi_index) 1801 return -EOPNOTSUPP; 1802 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO]) 1803 return -EOPNOTSUPP; 1804 1805 if (!ops) { 1806 #ifdef CONFIG_MODULES 1807 if (kind[0]) { 1808 __rtnl_unlock(); 1809 request_module("rtnl-link-%s", kind); 1810 rtnl_lock(); 1811 ops = rtnl_link_ops_get(kind); 1812 if (ops) 1813 goto replay; 1814 } 1815 #endif 1816 return -EOPNOTSUPP; 1817 } 1818 1819 if (!ifname[0]) 1820 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); 1821 1822 dest_net = rtnl_link_get_net(net, tb); 1823 if (IS_ERR(dest_net)) 1824 return PTR_ERR(dest_net); 1825 1826 dev = rtnl_create_link(net, dest_net, ifname, ops, tb); 1827 1828 if (IS_ERR(dev)) 1829 err = PTR_ERR(dev); 1830 else if (ops->newlink) 1831 err = ops->newlink(net, dev, tb, data); 1832 else 1833 err = register_netdevice(dev); 1834 1835 if (err < 0 && !IS_ERR(dev)) 1836 free_netdev(dev); 1837 if (err < 0) 1838 goto out; 1839 1840 err = rtnl_configure_link(dev, ifm); 1841 if (err < 0) 1842 unregister_netdevice(dev); 1843 out: 1844 put_net(dest_net); 1845 return err; 1846 } 1847 } 1848 1849 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) 1850 { 1851 struct net *net = sock_net(skb->sk); 1852 struct ifinfomsg *ifm; 1853 char ifname[IFNAMSIZ]; 1854 struct nlattr *tb[IFLA_MAX+1]; 1855 struct net_device *dev = NULL; 1856 struct sk_buff *nskb; 1857 int err; 1858 u32 ext_filter_mask = 0; 1859 1860 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy); 1861 if (err < 0) 1862 return err; 1863 1864 if (tb[IFLA_IFNAME]) 1865 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ); 1866 1867 if (tb[IFLA_EXT_MASK]) 1868 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1869 1870 ifm = nlmsg_data(nlh); 1871 if (ifm->ifi_index > 0) 1872 dev = __dev_get_by_index(net, ifm->ifi_index); 1873 else if (tb[IFLA_IFNAME]) 1874 dev = __dev_get_by_name(net, ifname); 1875 else 1876 return -EINVAL; 1877 1878 if (dev == NULL) 1879 return -ENODEV; 1880 1881 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL); 1882 if (nskb == NULL) 1883 return -ENOBUFS; 1884 1885 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid, 1886 nlh->nlmsg_seq, 0, 0, ext_filter_mask); 1887 if (err < 0) { 1888 /* -EMSGSIZE implies BUG in if_nlmsg_size */ 1889 WARN_ON(err == -EMSGSIZE); 1890 kfree_skb(nskb); 1891 } else 1892 err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid); 1893 1894 return err; 1895 } 1896 1897 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) 1898 { 1899 struct net *net = sock_net(skb->sk); 1900 struct net_device *dev; 1901 struct nlattr *tb[IFLA_MAX+1]; 1902 u32 ext_filter_mask = 0; 1903 u16 min_ifinfo_dump_size = 0; 1904 1905 if (nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, IFLA_MAX, 1906 ifla_policy) >= 0) { 1907 if (tb[IFLA_EXT_MASK]) 1908 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); 1909 } 1910 1911 if (!ext_filter_mask) 1912 return NLMSG_GOODSIZE; 1913 /* 1914 * traverse the list of net devices and compute the minimum 1915 * buffer size based upon the filter mask. 1916 */ 1917 list_for_each_entry(dev, &net->dev_base_head, dev_list) { 1918 min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size, 1919 if_nlmsg_size(dev, 1920 ext_filter_mask)); 1921 } 1922 1923 return min_ifinfo_dump_size; 1924 } 1925 1926 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb) 1927 { 1928 int idx; 1929 int s_idx = cb->family; 1930 1931 if (s_idx == 0) 1932 s_idx = 1; 1933 for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) { 1934 int type = cb->nlh->nlmsg_type-RTM_BASE; 1935 if (idx < s_idx || idx == PF_PACKET) 1936 continue; 1937 if (rtnl_msg_handlers[idx] == NULL || 1938 rtnl_msg_handlers[idx][type].dumpit == NULL) 1939 continue; 1940 if (idx > s_idx) 1941 memset(&cb->args[0], 0, sizeof(cb->args)); 1942 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb)) 1943 break; 1944 } 1945 cb->family = idx; 1946 1947 return skb->len; 1948 } 1949 1950 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change) 1951 { 1952 struct net *net = dev_net(dev); 1953 struct sk_buff *skb; 1954 int err = -ENOBUFS; 1955 size_t if_info_size; 1956 1957 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL); 1958 if (skb == NULL) 1959 goto errout; 1960 1961 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0); 1962 if (err < 0) { 1963 /* -EMSGSIZE implies BUG in if_nlmsg_size() */ 1964 WARN_ON(err == -EMSGSIZE); 1965 kfree_skb(skb); 1966 goto errout; 1967 } 1968 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 1969 return; 1970 errout: 1971 if (err < 0) 1972 rtnl_set_sk_err(net, RTNLGRP_LINK, err); 1973 } 1974 1975 /* Protected by RTNL sempahore. */ 1976 static struct rtattr **rta_buf; 1977 static int rtattr_max; 1978 1979 /* Process one rtnetlink message. */ 1980 1981 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 1982 { 1983 struct net *net = sock_net(skb->sk); 1984 rtnl_doit_func doit; 1985 int sz_idx, kind; 1986 int min_len; 1987 int family; 1988 int type; 1989 int err; 1990 1991 type = nlh->nlmsg_type; 1992 if (type > RTM_MAX) 1993 return -EOPNOTSUPP; 1994 1995 type -= RTM_BASE; 1996 1997 /* All the messages must have at least 1 byte length */ 1998 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg))) 1999 return 0; 2000 2001 family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family; 2002 sz_idx = type>>2; 2003 kind = type&3; 2004 2005 if (kind != 2 && !capable(CAP_NET_ADMIN)) 2006 return -EPERM; 2007 2008 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) { 2009 struct sock *rtnl; 2010 rtnl_dumpit_func dumpit; 2011 rtnl_calcit_func calcit; 2012 u16 min_dump_alloc = 0; 2013 2014 dumpit = rtnl_get_dumpit(family, type); 2015 if (dumpit == NULL) 2016 return -EOPNOTSUPP; 2017 calcit = rtnl_get_calcit(family, type); 2018 if (calcit) 2019 min_dump_alloc = calcit(skb, nlh); 2020 2021 __rtnl_unlock(); 2022 rtnl = net->rtnl; 2023 { 2024 struct netlink_dump_control c = { 2025 .dump = dumpit, 2026 .min_dump_alloc = min_dump_alloc, 2027 }; 2028 err = netlink_dump_start(rtnl, skb, nlh, &c); 2029 } 2030 rtnl_lock(); 2031 return err; 2032 } 2033 2034 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *))); 2035 2036 min_len = rtm_min[sz_idx]; 2037 if (nlh->nlmsg_len < min_len) 2038 return -EINVAL; 2039 2040 if (nlh->nlmsg_len > min_len) { 2041 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len); 2042 struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len); 2043 2044 while (RTA_OK(attr, attrlen)) { 2045 unsigned flavor = attr->rta_type; 2046 if (flavor) { 2047 if (flavor > rta_max[sz_idx]) 2048 return -EINVAL; 2049 rta_buf[flavor-1] = attr; 2050 } 2051 attr = RTA_NEXT(attr, attrlen); 2052 } 2053 } 2054 2055 doit = rtnl_get_doit(family, type); 2056 if (doit == NULL) 2057 return -EOPNOTSUPP; 2058 2059 return doit(skb, nlh, (void *)&rta_buf[0]); 2060 } 2061 2062 static void rtnetlink_rcv(struct sk_buff *skb) 2063 { 2064 rtnl_lock(); 2065 netlink_rcv_skb(skb, &rtnetlink_rcv_msg); 2066 rtnl_unlock(); 2067 } 2068 2069 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr) 2070 { 2071 struct net_device *dev = ptr; 2072 2073 switch (event) { 2074 case NETDEV_UP: 2075 case NETDEV_DOWN: 2076 case NETDEV_PRE_UP: 2077 case NETDEV_POST_INIT: 2078 case NETDEV_REGISTER: 2079 case NETDEV_CHANGE: 2080 case NETDEV_PRE_TYPE_CHANGE: 2081 case NETDEV_GOING_DOWN: 2082 case NETDEV_UNREGISTER: 2083 case NETDEV_UNREGISTER_BATCH: 2084 case NETDEV_RELEASE: 2085 case NETDEV_JOIN: 2086 break; 2087 default: 2088 rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 2089 break; 2090 } 2091 return NOTIFY_DONE; 2092 } 2093 2094 static struct notifier_block rtnetlink_dev_notifier = { 2095 .notifier_call = rtnetlink_event, 2096 }; 2097 2098 2099 static int __net_init rtnetlink_net_init(struct net *net) 2100 { 2101 struct sock *sk; 2102 sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, 2103 rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); 2104 if (!sk) 2105 return -ENOMEM; 2106 net->rtnl = sk; 2107 return 0; 2108 } 2109 2110 static void __net_exit rtnetlink_net_exit(struct net *net) 2111 { 2112 netlink_kernel_release(net->rtnl); 2113 net->rtnl = NULL; 2114 } 2115 2116 static struct pernet_operations rtnetlink_net_ops = { 2117 .init = rtnetlink_net_init, 2118 .exit = rtnetlink_net_exit, 2119 }; 2120 2121 void __init rtnetlink_init(void) 2122 { 2123 int i; 2124 2125 rtattr_max = 0; 2126 for (i = 0; i < ARRAY_SIZE(rta_max); i++) 2127 if (rta_max[i] > rtattr_max) 2128 rtattr_max = rta_max[i]; 2129 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL); 2130 if (!rta_buf) 2131 panic("rtnetlink_init: cannot allocate rta_buf\n"); 2132 2133 if (register_pernet_subsys(&rtnetlink_net_ops)) 2134 panic("rtnetlink_init: cannot initialize rtnetlink\n"); 2135 2136 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV); 2137 register_netdevice_notifier(&rtnetlink_dev_notifier); 2138 2139 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, 2140 rtnl_dump_ifinfo, rtnl_calcit); 2141 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL); 2142 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL); 2143 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL); 2144 2145 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL); 2146 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL); 2147 } 2148 2149