1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * linux/drivers/net/netconsole.c 4 * 5 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com> 6 * 7 * This file contains the implementation of an IRQ-safe, crash-safe 8 * kernel console implementation that outputs kernel messages to the 9 * network. 10 * 11 * Modification history: 12 * 13 * 2001-09-17 started by Ingo Molnar. 14 * 2003-08-11 2.6 port by Matt Mackall 15 * simplified options 16 * generic card hooks 17 * works non-modular 18 * 2003-09-07 rewritten with netpoll api 19 */ 20 21 /**************************************************************** 22 * 23 ****************************************************************/ 24 25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 26 27 #include <linux/mm.h> 28 #include <linux/init.h> 29 #include <linux/module.h> 30 #include <linux/slab.h> 31 #include <linux/console.h> 32 #include <linux/moduleparam.h> 33 #include <linux/kernel.h> 34 #include <linux/string.h> 35 #include <linux/ip.h> 36 #include <linux/ipv6.h> 37 #include <linux/udp.h> 38 #include <linux/netpoll.h> 39 #include <linux/inet.h> 40 #include <linux/unaligned.h> 41 #include <net/ip6_checksum.h> 42 #include <linux/configfs.h> 43 #include <linux/etherdevice.h> 44 #include <linux/hex.h> 45 #include <linux/u64_stats_sync.h> 46 #include <linux/utsname.h> 47 #include <linux/rtnetlink.h> 48 #include <linux/workqueue.h> 49 50 MODULE_AUTHOR("Matt Mackall <mpm@selenic.com>"); 51 MODULE_DESCRIPTION("Console driver for network interfaces"); 52 MODULE_LICENSE("GPL"); 53 MODULE_IMPORT_NS("NETDEV_INTERNAL"); 54 55 #define MAX_PARAM_LENGTH 256 56 #define MAX_EXTRADATA_ENTRY_LEN 256 57 #define MAX_EXTRADATA_VALUE_LEN 200 58 /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */ 59 #define MAX_EXTRADATA_NAME_LEN (MAX_EXTRADATA_ENTRY_LEN - \ 60 MAX_EXTRADATA_VALUE_LEN - 3) 61 #define MAX_USERDATA_ITEMS 256 62 #define MAX_PRINT_CHUNK 1000 63 64 /* 65 * Sizing for the per-target fallback skb pool consulted by find_skb() 66 * when its GFP_ATOMIC allocation fails so messages still get out under 67 * memory pressure. 68 */ 69 #define MAX_UDP_CHUNK 1460 70 #define MAX_SKBS 32 71 #define MAX_SKB_SIZE \ 72 (sizeof(struct ethhdr) + \ 73 sizeof(struct iphdr) + \ 74 sizeof(struct udphdr) + \ 75 MAX_UDP_CHUNK) 76 77 static char config[MAX_PARAM_LENGTH]; 78 module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0); 79 MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]"); 80 81 static bool oops_only; 82 module_param(oops_only, bool, 0600); 83 MODULE_PARM_DESC(oops_only, "Only log oops messages"); 84 85 #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline" 86 87 #ifndef MODULE 88 static int __init option_setup(char *opt) 89 { 90 strscpy(config, opt, MAX_PARAM_LENGTH); 91 return 1; 92 } 93 __setup("netconsole=", option_setup); 94 #endif /* MODULE */ 95 96 /* Linked list of all configured targets */ 97 static LIST_HEAD(target_list); 98 /* target_cleanup_list is used to track targets that need to be cleaned outside 99 * of target_list_lock. It should be cleaned in the same function it is 100 * populated. 101 */ 102 static LIST_HEAD(target_cleanup_list); 103 104 /* This needs to be a spinlock because write_msg() cannot sleep */ 105 static DEFINE_SPINLOCK(target_list_lock); 106 /* This needs to be a mutex because netpoll_cleanup might sleep */ 107 static DEFINE_MUTEX(target_cleanup_list_lock); 108 109 static struct workqueue_struct *netconsole_wq; 110 111 /* 112 * Console driver for netconsoles. Register only consoles that have 113 * an associated target of the same type. 114 */ 115 static struct console netconsole_ext, netconsole; 116 117 struct netconsole_target_stats { 118 u64_stats_t xmit_drop_count; 119 u64_stats_t enomem_count; 120 struct u64_stats_sync syncp; 121 }; 122 123 enum console_type { 124 CONS_BASIC = BIT(0), 125 CONS_EXTENDED = BIT(1), 126 }; 127 128 /* Features enabled in sysdata. Contrary to userdata, this data is populated by 129 * the kernel. The fields are designed as bitwise flags, allowing multiple 130 * features to be set in sysdata_fields. 131 */ 132 enum sysdata_feature { 133 /* Populate the CPU that sends the message */ 134 SYSDATA_CPU_NR = BIT(0), 135 /* Populate the task name (as in current->comm) in sysdata */ 136 SYSDATA_TASKNAME = BIT(1), 137 /* Kernel release/version as part of sysdata */ 138 SYSDATA_RELEASE = BIT(2), 139 /* Include a per-target message ID as part of sysdata */ 140 SYSDATA_MSGID = BIT(3), 141 /* Sentinel: highest bit position */ 142 MAX_SYSDATA_ITEMS = 4, 143 }; 144 145 enum target_state { 146 STATE_DISABLED, 147 STATE_ENABLED, 148 STATE_DEACTIVATED, 149 }; 150 151 /** 152 * struct netconsole_target - Represents a configured netconsole target. 153 * @list: Links this target into the target_list. 154 * @group: Links us into the configfs subsystem hierarchy. 155 * @userdata_group: Links to the userdata configfs hierarchy 156 * @userdata: Cached, formatted string of append 157 * @userdata_length: String length of userdata. 158 * @sysdata: Cached, formatted string of append 159 * @sysdata_fields: Sysdata features enabled. 160 * @msgcounter: Message sent counter. 161 * @stats: Packet send stats for the target. Used for debugging. 162 * @state: State of the target. 163 * Visible from userspace (read-write). 164 * From a userspace perspective, the target is either enabled or 165 * disabled. Internally, although both STATE_DISABLED and 166 * STATE_DEACTIVATED correspond to inactive targets, the latter is 167 * due to automatic interface state changes and will try 168 * recover automatically, if the interface comes back 169 * online. 170 * Also, other parameters of a target may be modified at 171 * runtime only when it is disabled (state != STATE_ENABLED). 172 * @extended: Denotes whether console is extended or not. 173 * @release: Denotes whether kernel release version should be prepended 174 * to the message. Depends on extended console. 175 * @np: The netpoll structure for this target. 176 * Contains the other userspace visible parameters: 177 * dev_name (read-write) 178 * local_port (read-write) 179 * remote_port (read-write) 180 * local_ip (read-write) 181 * remote_ip (read-write) 182 * local_mac (read-only) 183 * remote_mac (read-write) 184 * @buf: The buffer used to send the full msg to the network stack 185 * @resume_wq: Workqueue to resume deactivated target 186 * @skb_pool: Per-target fallback skb pool consulted by find_skb() when 187 * its GFP_ATOMIC allocation fails. Lifetime brackets a 188 * successful netpoll_setup() / netpoll_cleanup() pair on @np. 189 * @refill_wq: Work item that asynchronously tops @skb_pool back up to 190 * MAX_SKBS after find_skb() drains an entry. 191 */ 192 struct netconsole_target { 193 struct list_head list; 194 #ifdef CONFIG_NETCONSOLE_DYNAMIC 195 struct config_group group; 196 struct config_group userdata_group; 197 char *userdata; 198 size_t userdata_length; 199 char sysdata[MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS]; 200 201 /* bit-wise with sysdata_feature bits */ 202 u32 sysdata_fields; 203 /* protected by target_list_lock */ 204 u32 msgcounter; 205 #endif 206 struct netconsole_target_stats stats; 207 enum target_state state; 208 bool extended; 209 bool release; 210 struct netpoll np; 211 /* protected by target_list_lock; +1 gives scnprintf() room for its 212 * NUL terminator so a full MAX_PRINT_CHUNK payload is not truncated 213 */ 214 char buf[MAX_PRINT_CHUNK + 1]; 215 struct work_struct resume_wq; 216 struct sk_buff_head skb_pool; 217 struct work_struct refill_wq; 218 }; 219 220 #ifdef CONFIG_NETCONSOLE_DYNAMIC 221 222 static struct configfs_subsystem netconsole_subsys; 223 static DEFINE_MUTEX(dynamic_netconsole_mutex); 224 225 static int __init dynamic_netconsole_init(void) 226 { 227 config_group_init(&netconsole_subsys.su_group); 228 mutex_init(&netconsole_subsys.su_mutex); 229 return configfs_register_subsystem(&netconsole_subsys); 230 } 231 232 static void __exit dynamic_netconsole_exit(void) 233 { 234 configfs_unregister_subsystem(&netconsole_subsys); 235 } 236 237 /* 238 * Targets that were created by parsing the boot/module option string 239 * do not exist in the configfs hierarchy (and have NULL names) and will 240 * never go away, so make these a no-op for them. 241 */ 242 static void netconsole_target_get(struct netconsole_target *nt) 243 { 244 if (config_item_name(&nt->group.cg_item)) 245 config_group_get(&nt->group); 246 } 247 248 static void netconsole_target_put(struct netconsole_target *nt) 249 { 250 if (config_item_name(&nt->group.cg_item)) 251 config_group_put(&nt->group); 252 } 253 254 static void dynamic_netconsole_mutex_lock(void) 255 { 256 mutex_lock(&dynamic_netconsole_mutex); 257 } 258 259 static void dynamic_netconsole_mutex_unlock(void) 260 { 261 mutex_unlock(&dynamic_netconsole_mutex); 262 } 263 264 #else /* !CONFIG_NETCONSOLE_DYNAMIC */ 265 266 static int __init dynamic_netconsole_init(void) 267 { 268 return 0; 269 } 270 271 static void __exit dynamic_netconsole_exit(void) 272 { 273 } 274 275 /* 276 * No danger of targets going away from under us when dynamic 277 * reconfigurability is off. 278 */ 279 static void netconsole_target_get(struct netconsole_target *nt) 280 { 281 } 282 283 static void netconsole_target_put(struct netconsole_target *nt) 284 { 285 } 286 287 static void populate_configfs_item(struct netconsole_target *nt, 288 int cmdline_count) 289 { 290 } 291 292 static void dynamic_netconsole_mutex_lock(void) 293 { 294 } 295 296 static void dynamic_netconsole_mutex_unlock(void) 297 { 298 } 299 300 #endif /* CONFIG_NETCONSOLE_DYNAMIC */ 301 302 /* Check if the target was bound by mac address. */ 303 static bool bound_by_mac(struct netconsole_target *nt) 304 { 305 return is_valid_ether_addr(nt->np.dev_mac); 306 } 307 308 static void netcons_release_dev(struct netconsole_target *nt) 309 { 310 do_netpoll_cleanup(&nt->np); 311 if (bound_by_mac(nt)) 312 memset(&nt->np.dev_name, 0, IFNAMSIZ); 313 } 314 315 static void refill_skbs(struct netconsole_target *nt) 316 { 317 struct sk_buff_head *skb_pool = &nt->skb_pool; 318 struct sk_buff *skb; 319 320 while (READ_ONCE(skb_pool->qlen) < MAX_SKBS) { 321 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC | __GFP_NOWARN); 322 if (!skb) 323 break; 324 325 skb_queue_tail(skb_pool, skb); 326 } 327 } 328 329 static void refill_skbs_work_handler(struct work_struct *work) 330 { 331 struct netconsole_target *nt = 332 container_of(work, struct netconsole_target, refill_wq); 333 334 refill_skbs(nt); 335 } 336 337 /* Seed the per-target skb pool that find_skb() falls back to. The queue 338 * head and refill work are set up once in alloc_and_init(); this only 339 * (re)fills the pool. Pair with netconsole_skb_pool_flush(). 340 */ 341 static void netconsole_skb_pool_init(struct netconsole_target *nt) 342 { 343 refill_skbs(nt); 344 } 345 346 static void netconsole_skb_pool_flush(struct netconsole_target *nt) 347 { 348 cancel_work_sync(&nt->refill_wq); 349 skb_queue_purge_reason(&nt->skb_pool, SKB_CONSUMED); 350 } 351 352 /* Attempts to resume logging to a deactivated target. */ 353 static void resume_target(struct netconsole_target *nt) 354 { 355 /* Initialise the skb pool before netpoll_setup() makes nt->np.dev 356 * visible to target_list walkers (e.g. netconsole_netdev_event), 357 * which otherwise may move the target to the cleanup list and 358 * call netconsole_skb_pool_flush() on uninitialised state. 359 */ 360 netconsole_skb_pool_init(nt); 361 362 if (netpoll_setup(&nt->np)) { 363 /* netpoll fails setup once, do not try again. */ 364 netconsole_skb_pool_flush(nt); 365 nt->state = STATE_DISABLED; 366 return; 367 } 368 369 nt->state = STATE_ENABLED; 370 pr_info("network logging resumed on interface %s\n", nt->np.dev_name); 371 } 372 373 /* Checks if a deactivated target matches a device. */ 374 static bool deactivated_target_match(struct netconsole_target *nt, 375 struct net_device *ndev) 376 { 377 if (nt->state != STATE_DEACTIVATED) 378 return false; 379 380 if (bound_by_mac(nt)) 381 return !memcmp(nt->np.dev_mac, ndev->dev_addr, ETH_ALEN); 382 return !strncmp(nt->np.dev_name, ndev->name, IFNAMSIZ); 383 } 384 385 /* Process work scheduled for target resume. */ 386 static void process_resume_target(struct work_struct *work) 387 { 388 struct netconsole_target *nt; 389 unsigned long flags; 390 391 nt = container_of(work, struct netconsole_target, resume_wq); 392 393 dynamic_netconsole_mutex_lock(); 394 395 spin_lock_irqsave(&target_list_lock, flags); 396 /* Check if target is still deactivated as it may have been disabled 397 * while resume was being scheduled. 398 */ 399 if (nt->state != STATE_DEACTIVATED) { 400 spin_unlock_irqrestore(&target_list_lock, flags); 401 goto out_unlock; 402 } 403 404 /* resume_target is IRQ unsafe, remove target from 405 * target_list in order to resume it with IRQ enabled. 406 */ 407 list_del_init(&nt->list); 408 spin_unlock_irqrestore(&target_list_lock, flags); 409 410 resume_target(nt); 411 412 /* netpoll_setup() took a net_device reference and dropped the RTNL 413 * before returning, all while this target was off target_list and 414 * thus invisible to netconsole_netdev_event(). If the device was 415 * unregistered in that window the NETDEV_UNREGISTER notifier could not 416 * tear this target down, which would leak the reference and hang 417 * unregister_netdevice(). Re-check under the RTNL before re-publishing: 418 * taking it across the check and the list_add() serialises against the 419 * notifier (which also runs under the RTNL), so the device is either 420 * still registered (the notifier will find the re-added target) or 421 * already unregistering (we drop the reference here). 422 */ 423 rtnl_lock(); 424 if (nt->state == STATE_ENABLED && nt->np.dev && 425 nt->np.dev->reg_state != NETREG_REGISTERED) { 426 netconsole_skb_pool_flush(nt); 427 netcons_release_dev(nt); 428 nt->state = STATE_DISABLED; 429 } 430 431 /* At this point the target is either enabled or disabled and 432 * was cleaned up before getting deactivated. Either way, add it 433 * back to target list. 434 */ 435 spin_lock_irqsave(&target_list_lock, flags); 436 list_add(&nt->list, &target_list); 437 spin_unlock_irqrestore(&target_list_lock, flags); 438 rtnl_unlock(); 439 440 out_unlock: 441 dynamic_netconsole_mutex_unlock(); 442 } 443 444 /* Allocate and initialize with defaults. 445 * Note that these targets get their config_item fields zeroed-out. 446 */ 447 static struct netconsole_target *alloc_and_init(void) 448 { 449 struct netconsole_target *nt; 450 451 nt = kzalloc_obj(*nt); 452 if (!nt) 453 return nt; 454 455 if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG)) 456 nt->extended = true; 457 if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE)) 458 nt->release = true; 459 460 nt->np.name = "netconsole"; 461 strscpy(nt->np.dev_name, "eth0", IFNAMSIZ); 462 nt->np.local_port = 6665; 463 nt->np.remote_port = 6666; 464 eth_broadcast_addr(nt->np.remote_mac); 465 nt->state = STATE_DISABLED; 466 INIT_WORK(&nt->resume_wq, process_resume_target); 467 /* Set up the skb pool primitives once; enabling only refills it. */ 468 skb_queue_head_init(&nt->skb_pool); 469 INIT_WORK(&nt->refill_wq, refill_skbs_work_handler); 470 471 return nt; 472 } 473 474 /* Clean up every target in the cleanup_list and move the clean targets back to 475 * the main target_list. 476 */ 477 static void netconsole_process_cleanups_core(void) 478 { 479 struct netconsole_target *nt, *tmp; 480 unsigned long flags; 481 482 /* The cleanup needs RTNL locked */ 483 ASSERT_RTNL(); 484 485 mutex_lock(&target_cleanup_list_lock); 486 list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) { 487 /* all entries in the cleanup_list needs to be disabled */ 488 WARN_ON_ONCE(nt->state == STATE_ENABLED); 489 netconsole_skb_pool_flush(nt); 490 netcons_release_dev(nt); 491 /* moved the cleaned target to target_list. Need to hold both 492 * locks 493 */ 494 spin_lock_irqsave(&target_list_lock, flags); 495 list_move(&nt->list, &target_list); 496 spin_unlock_irqrestore(&target_list_lock, flags); 497 } 498 WARN_ON_ONCE(!list_empty(&target_cleanup_list)); 499 mutex_unlock(&target_cleanup_list_lock); 500 } 501 502 static void netconsole_print_banner(struct netpoll *np) 503 { 504 np_info(np, "local port %d\n", np->local_port); 505 if (np->ipv6) 506 np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6); 507 else 508 np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip); 509 np_info(np, "interface name '%s'\n", np->dev_name); 510 np_info(np, "local ethernet address '%pM'\n", np->dev_mac); 511 np_info(np, "remote port %d\n", np->remote_port); 512 if (np->ipv6) 513 np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6); 514 else 515 np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip); 516 np_info(np, "remote ethernet address %pM\n", np->remote_mac); 517 } 518 519 /* Parse the string and populate the `inet_addr` union. Return 0 if IPv4 is 520 * populated, 1 if IPv6 is populated, and -1 upon failure. 521 */ 522 static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr) 523 { 524 const char *end = NULL; 525 int len; 526 527 len = strlen(str); 528 if (!len) 529 return -1; 530 531 if (str[len - 1] == '\n') 532 len -= 1; 533 534 if (in4_pton(str, len, (void *)addr, -1, &end) > 0 && 535 (!end || *end == 0 || *end == '\n')) 536 return 0; 537 538 if (IS_ENABLED(CONFIG_IPV6) && 539 in6_pton(str, len, (void *)addr, -1, &end) > 0 && 540 (!end || *end == 0 || *end == '\n')) 541 return 1; 542 543 return -1; 544 } 545 546 #ifdef CONFIG_NETCONSOLE_DYNAMIC 547 548 /* 549 * Our subsystem hierarchy is: 550 * 551 * /sys/kernel/config/netconsole/ 552 * | 553 * <target>/ 554 * | enabled 555 * | release 556 * | dev_name 557 * | local_port 558 * | remote_port 559 * | local_ip 560 * | remote_ip 561 * | local_mac 562 * | remote_mac 563 * | transmit_errors 564 * | userdata/ 565 * | <key>/ 566 * | value 567 * | ... 568 * | 569 * <target>/... 570 */ 571 572 static struct netconsole_target *to_target(struct config_item *item) 573 { 574 struct config_group *cfg_group; 575 576 cfg_group = to_config_group(item); 577 if (!cfg_group) 578 return NULL; 579 return container_of(to_config_group(item), 580 struct netconsole_target, group); 581 } 582 583 /* Do the list cleanup with the rtnl lock hold. rtnl lock is necessary because 584 * netdev might be cleaned-up by calling __netpoll_cleanup(), 585 */ 586 static void netconsole_process_cleanups(void) 587 { 588 /* rtnl lock is called here, because it has precedence over 589 * target_cleanup_list_lock mutex and target_cleanup_list 590 */ 591 rtnl_lock(); 592 netconsole_process_cleanups_core(); 593 rtnl_unlock(); 594 } 595 596 /* Get rid of possible trailing newline, returning the new length */ 597 static void trim_newline(char *s, size_t maxlen) 598 { 599 size_t len; 600 601 len = strnlen(s, maxlen); 602 if (!len) 603 return; 604 if (s[len - 1] == '\n') 605 s[len - 1] = '\0'; 606 } 607 608 /* 609 * Attribute operations for netconsole_target. 610 */ 611 612 static ssize_t enabled_show(struct config_item *item, char *buf) 613 { 614 return sysfs_emit(buf, "%d\n", to_target(item)->state == STATE_ENABLED); 615 } 616 617 static ssize_t extended_show(struct config_item *item, char *buf) 618 { 619 return sysfs_emit(buf, "%d\n", to_target(item)->extended); 620 } 621 622 static ssize_t release_show(struct config_item *item, char *buf) 623 { 624 return sysfs_emit(buf, "%d\n", to_target(item)->release); 625 } 626 627 static ssize_t dev_name_show(struct config_item *item, char *buf) 628 { 629 return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name); 630 } 631 632 static ssize_t local_port_show(struct config_item *item, char *buf) 633 { 634 return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port); 635 } 636 637 static ssize_t remote_port_show(struct config_item *item, char *buf) 638 { 639 return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port); 640 } 641 642 static ssize_t local_ip_show(struct config_item *item, char *buf) 643 { 644 struct netconsole_target *nt = to_target(item); 645 646 if (nt->np.ipv6) 647 return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6); 648 else 649 return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip); 650 } 651 652 static ssize_t remote_ip_show(struct config_item *item, char *buf) 653 { 654 struct netconsole_target *nt = to_target(item); 655 656 if (nt->np.ipv6) 657 return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6); 658 else 659 return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip); 660 } 661 662 static ssize_t local_mac_show(struct config_item *item, char *buf) 663 { 664 struct net_device *dev = to_target(item)->np.dev; 665 static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 666 667 return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast); 668 } 669 670 static ssize_t remote_mac_show(struct config_item *item, char *buf) 671 { 672 return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac); 673 } 674 675 static ssize_t transmit_errors_show(struct config_item *item, char *buf) 676 { 677 struct netconsole_target *nt = to_target(item); 678 u64 xmit_drop_count, enomem_count; 679 unsigned int start; 680 681 do { 682 start = u64_stats_fetch_begin(&nt->stats.syncp); 683 xmit_drop_count = u64_stats_read(&nt->stats.xmit_drop_count); 684 enomem_count = u64_stats_read(&nt->stats.enomem_count); 685 } while (u64_stats_fetch_retry(&nt->stats.syncp, start)); 686 687 return sysfs_emit(buf, "%llu\n", xmit_drop_count + enomem_count); 688 } 689 690 /* configfs helper to display if cpu_nr sysdata feature is enabled */ 691 static ssize_t sysdata_cpu_nr_enabled_show(struct config_item *item, char *buf) 692 { 693 struct netconsole_target *nt = to_target(item->ci_parent); 694 bool cpu_nr_enabled; 695 696 dynamic_netconsole_mutex_lock(); 697 cpu_nr_enabled = !!(nt->sysdata_fields & SYSDATA_CPU_NR); 698 dynamic_netconsole_mutex_unlock(); 699 700 return sysfs_emit(buf, "%d\n", cpu_nr_enabled); 701 } 702 703 /* configfs helper to display if taskname sysdata feature is enabled */ 704 static ssize_t sysdata_taskname_enabled_show(struct config_item *item, 705 char *buf) 706 { 707 struct netconsole_target *nt = to_target(item->ci_parent); 708 bool taskname_enabled; 709 710 dynamic_netconsole_mutex_lock(); 711 taskname_enabled = !!(nt->sysdata_fields & SYSDATA_TASKNAME); 712 dynamic_netconsole_mutex_unlock(); 713 714 return sysfs_emit(buf, "%d\n", taskname_enabled); 715 } 716 717 static ssize_t sysdata_release_enabled_show(struct config_item *item, 718 char *buf) 719 { 720 struct netconsole_target *nt = to_target(item->ci_parent); 721 bool release_enabled; 722 723 dynamic_netconsole_mutex_lock(); 724 release_enabled = !!(nt->sysdata_fields & SYSDATA_RELEASE); 725 dynamic_netconsole_mutex_unlock(); 726 727 return sysfs_emit(buf, "%d\n", release_enabled); 728 } 729 730 /* Iterate in the list of target, and make sure we don't have any console 731 * register without targets of the same type 732 */ 733 static void unregister_netcons_consoles(void) 734 { 735 struct netconsole_target *nt; 736 u32 console_type_needed = 0; 737 unsigned long flags; 738 739 spin_lock_irqsave(&target_list_lock, flags); 740 list_for_each_entry(nt, &target_list, list) { 741 if (nt->extended) 742 console_type_needed |= CONS_EXTENDED; 743 else 744 console_type_needed |= CONS_BASIC; 745 } 746 spin_unlock_irqrestore(&target_list_lock, flags); 747 748 if (!(console_type_needed & CONS_EXTENDED) && 749 console_is_registered(&netconsole_ext)) 750 unregister_console(&netconsole_ext); 751 752 if (!(console_type_needed & CONS_BASIC) && 753 console_is_registered(&netconsole)) 754 unregister_console(&netconsole); 755 } 756 757 static ssize_t sysdata_msgid_enabled_show(struct config_item *item, 758 char *buf) 759 { 760 struct netconsole_target *nt = to_target(item->ci_parent); 761 bool msgid_enabled; 762 763 dynamic_netconsole_mutex_lock(); 764 msgid_enabled = !!(nt->sysdata_fields & SYSDATA_MSGID); 765 dynamic_netconsole_mutex_unlock(); 766 767 return sysfs_emit(buf, "%d\n", msgid_enabled); 768 } 769 770 /* 771 * This one is special -- targets created through the configfs interface 772 * are not enabled (and the corresponding netpoll activated) by default. 773 * The user is expected to set the desired parameters first (which 774 * would enable him to dynamically add new netpoll targets for new 775 * network interfaces as and when they come up). 776 */ 777 static ssize_t enabled_store(struct config_item *item, 778 const char *buf, size_t count) 779 { 780 struct netconsole_target *nt = to_target(item); 781 bool enabled, current_enabled; 782 unsigned long flags; 783 ssize_t ret; 784 785 dynamic_netconsole_mutex_lock(); 786 ret = kstrtobool(buf, &enabled); 787 if (ret) 788 goto out_unlock; 789 790 /* When the user explicitly enables or disables a target that is 791 * currently deactivated, reset its state to disabled. The DEACTIVATED 792 * state only tracks interface-driven deactivation and should _not_ 793 * persist when the user manually changes the target's enabled state. 794 */ 795 if (nt->state == STATE_DEACTIVATED) 796 nt->state = STATE_DISABLED; 797 798 ret = -EINVAL; 799 current_enabled = nt->state == STATE_ENABLED; 800 if (enabled == current_enabled) { 801 pr_info("network logging has already %s\n", 802 current_enabled ? "started" : "stopped"); 803 goto out_unlock; 804 } 805 806 if (enabled) { /* true */ 807 if (nt->release && !nt->extended) { 808 pr_err("Not enabling netconsole. Release feature requires extended log message"); 809 goto out_unlock; 810 } 811 812 if (nt->extended && !console_is_registered(&netconsole_ext)) { 813 netconsole_ext.flags |= CON_ENABLED; 814 register_console(&netconsole_ext); 815 } 816 817 /* User might be enabling the basic format target for the very 818 * first time, make sure the console is registered. 819 */ 820 if (!nt->extended && !console_is_registered(&netconsole)) { 821 netconsole.flags |= CON_ENABLED; 822 register_console(&netconsole); 823 } 824 825 /* 826 * Skip netconsole_parser_cmdline() -- all the attributes are 827 * already configured via configfs. Just print them out. 828 */ 829 netconsole_print_banner(&nt->np); 830 831 /* Initialise the skb pool before netpoll_setup() so the pool 832 * is valid as soon as nt->np.dev becomes visible to 833 * target_list walkers (netconsole_netdev_event), which would 834 * otherwise call netconsole_skb_pool_flush() on uninitialised 835 * state. 836 */ 837 netconsole_skb_pool_init(nt); 838 839 ret = netpoll_setup(&nt->np); 840 if (ret) { 841 netconsole_skb_pool_flush(nt); 842 goto out_unlock; 843 } 844 845 nt->state = STATE_ENABLED; 846 pr_info("network logging started\n"); 847 } else { /* false */ 848 /* We need to disable the netconsole before cleaning it up 849 * otherwise we might end up in write_msg() with 850 * nt->np.dev == NULL and nt->state == STATE_ENABLED 851 */ 852 mutex_lock(&target_cleanup_list_lock); 853 spin_lock_irqsave(&target_list_lock, flags); 854 nt->state = STATE_DISABLED; 855 /* Remove the target from the list, while holding 856 * target_list_lock 857 */ 858 list_move(&nt->list, &target_cleanup_list); 859 spin_unlock_irqrestore(&target_list_lock, flags); 860 mutex_unlock(&target_cleanup_list_lock); 861 /* Unregister consoles, whose the last target of that type got 862 * disabled. 863 */ 864 unregister_netcons_consoles(); 865 } 866 867 ret = count; 868 /* Deferred cleanup */ 869 netconsole_process_cleanups(); 870 out_unlock: 871 dynamic_netconsole_mutex_unlock(); 872 return ret; 873 } 874 875 static ssize_t release_store(struct config_item *item, const char *buf, 876 size_t count) 877 { 878 struct netconsole_target *nt = to_target(item); 879 bool release; 880 ssize_t ret; 881 882 dynamic_netconsole_mutex_lock(); 883 if (nt->state == STATE_ENABLED) { 884 pr_err("target (%s) is enabled, disable to update parameters\n", 885 config_item_name(&nt->group.cg_item)); 886 ret = -EINVAL; 887 goto out_unlock; 888 } 889 890 ret = kstrtobool(buf, &release); 891 if (ret) 892 goto out_unlock; 893 894 nt->release = release; 895 896 ret = count; 897 out_unlock: 898 dynamic_netconsole_mutex_unlock(); 899 return ret; 900 } 901 902 static ssize_t extended_store(struct config_item *item, const char *buf, 903 size_t count) 904 { 905 struct netconsole_target *nt = to_target(item); 906 bool extended; 907 ssize_t ret; 908 909 dynamic_netconsole_mutex_lock(); 910 if (nt->state == STATE_ENABLED) { 911 pr_err("target (%s) is enabled, disable to update parameters\n", 912 config_item_name(&nt->group.cg_item)); 913 ret = -EINVAL; 914 goto out_unlock; 915 } 916 917 ret = kstrtobool(buf, &extended); 918 if (ret) 919 goto out_unlock; 920 921 nt->extended = extended; 922 ret = count; 923 out_unlock: 924 dynamic_netconsole_mutex_unlock(); 925 return ret; 926 } 927 928 static ssize_t dev_name_store(struct config_item *item, const char *buf, 929 size_t count) 930 { 931 struct netconsole_target *nt = to_target(item); 932 size_t len = count; 933 934 /* Account for a trailing newline appended by tools like echo */ 935 if (len && buf[len - 1] == '\n') 936 len--; 937 if (len >= IFNAMSIZ) 938 return -ENAMETOOLONG; 939 940 dynamic_netconsole_mutex_lock(); 941 if (nt->state == STATE_ENABLED) { 942 pr_err("target (%s) is enabled, disable to update parameters\n", 943 config_item_name(&nt->group.cg_item)); 944 dynamic_netconsole_mutex_unlock(); 945 return -EINVAL; 946 } 947 948 strscpy(nt->np.dev_name, buf, IFNAMSIZ); 949 trim_newline(nt->np.dev_name, IFNAMSIZ); 950 951 dynamic_netconsole_mutex_unlock(); 952 return count; 953 } 954 955 static ssize_t local_port_store(struct config_item *item, const char *buf, 956 size_t count) 957 { 958 struct netconsole_target *nt = to_target(item); 959 ssize_t ret = -EINVAL; 960 961 dynamic_netconsole_mutex_lock(); 962 if (nt->state == STATE_ENABLED) { 963 pr_err("target (%s) is enabled, disable to update parameters\n", 964 config_item_name(&nt->group.cg_item)); 965 goto out_unlock; 966 } 967 968 ret = kstrtou16(buf, 10, &nt->np.local_port); 969 if (ret < 0) 970 goto out_unlock; 971 ret = count; 972 out_unlock: 973 dynamic_netconsole_mutex_unlock(); 974 return ret; 975 } 976 977 static ssize_t remote_port_store(struct config_item *item, 978 const char *buf, size_t count) 979 { 980 struct netconsole_target *nt = to_target(item); 981 ssize_t ret = -EINVAL; 982 983 dynamic_netconsole_mutex_lock(); 984 if (nt->state == STATE_ENABLED) { 985 pr_err("target (%s) is enabled, disable to update parameters\n", 986 config_item_name(&nt->group.cg_item)); 987 goto out_unlock; 988 } 989 990 ret = kstrtou16(buf, 10, &nt->np.remote_port); 991 if (ret < 0) 992 goto out_unlock; 993 ret = count; 994 out_unlock: 995 dynamic_netconsole_mutex_unlock(); 996 return ret; 997 } 998 999 static ssize_t local_ip_store(struct config_item *item, const char *buf, 1000 size_t count) 1001 { 1002 struct netconsole_target *nt = to_target(item); 1003 ssize_t ret = -EINVAL; 1004 int ipv6; 1005 1006 dynamic_netconsole_mutex_lock(); 1007 if (nt->state == STATE_ENABLED) { 1008 pr_err("target (%s) is enabled, disable to update parameters\n", 1009 config_item_name(&nt->group.cg_item)); 1010 goto out_unlock; 1011 } 1012 1013 ipv6 = netpoll_parse_ip_addr(buf, &nt->np.local_ip); 1014 if (ipv6 == -1) 1015 goto out_unlock; 1016 nt->np.ipv6 = !!ipv6; 1017 1018 ret = count; 1019 out_unlock: 1020 dynamic_netconsole_mutex_unlock(); 1021 return ret; 1022 } 1023 1024 static ssize_t remote_ip_store(struct config_item *item, const char *buf, 1025 size_t count) 1026 { 1027 struct netconsole_target *nt = to_target(item); 1028 ssize_t ret = -EINVAL; 1029 int ipv6; 1030 1031 dynamic_netconsole_mutex_lock(); 1032 if (nt->state == STATE_ENABLED) { 1033 pr_err("target (%s) is enabled, disable to update parameters\n", 1034 config_item_name(&nt->group.cg_item)); 1035 goto out_unlock; 1036 } 1037 1038 ipv6 = netpoll_parse_ip_addr(buf, &nt->np.remote_ip); 1039 if (ipv6 == -1) 1040 goto out_unlock; 1041 nt->np.ipv6 = !!ipv6; 1042 1043 ret = count; 1044 out_unlock: 1045 dynamic_netconsole_mutex_unlock(); 1046 return ret; 1047 } 1048 1049 /* Count number of entries we have in userdata. 1050 * This is important because userdata only supports MAX_USERDATA_ITEMS 1051 * entries. Before enabling any new userdata feature, number of entries needs 1052 * to checked for available space. 1053 */ 1054 static size_t count_userdata_entries(struct netconsole_target *nt) 1055 { 1056 return list_count_nodes(&nt->userdata_group.cg_children); 1057 } 1058 1059 static ssize_t remote_mac_store(struct config_item *item, const char *buf, 1060 size_t count) 1061 { 1062 struct netconsole_target *nt = to_target(item); 1063 u8 remote_mac[ETH_ALEN]; 1064 ssize_t ret = -EINVAL; 1065 1066 dynamic_netconsole_mutex_lock(); 1067 if (nt->state == STATE_ENABLED) { 1068 pr_err("target (%s) is enabled, disable to update parameters\n", 1069 config_item_name(&nt->group.cg_item)); 1070 goto out_unlock; 1071 } 1072 1073 if (!mac_pton(buf, remote_mac)) 1074 goto out_unlock; 1075 if (buf[MAC_ADDR_STR_LEN] && buf[MAC_ADDR_STR_LEN] != '\n') 1076 goto out_unlock; 1077 memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN); 1078 1079 ret = count; 1080 out_unlock: 1081 dynamic_netconsole_mutex_unlock(); 1082 return ret; 1083 } 1084 1085 struct userdatum { 1086 struct config_item item; 1087 char value[MAX_EXTRADATA_VALUE_LEN]; 1088 }; 1089 1090 static struct userdatum *to_userdatum(struct config_item *item) 1091 { 1092 return container_of(item, struct userdatum, item); 1093 } 1094 1095 struct userdata { 1096 struct config_group group; 1097 }; 1098 1099 static struct userdata *to_userdata(struct config_item *item) 1100 { 1101 return container_of(to_config_group(item), struct userdata, group); 1102 } 1103 1104 static struct netconsole_target *userdata_to_target(struct userdata *ud) 1105 { 1106 struct config_group *netconsole_group; 1107 1108 netconsole_group = to_config_group(ud->group.cg_item.ci_parent); 1109 return to_target(&netconsole_group->cg_item); 1110 } 1111 1112 static ssize_t userdatum_value_show(struct config_item *item, char *buf) 1113 { 1114 return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0])); 1115 } 1116 1117 /* Navigate configfs and calculate the lentgh of the formatted string 1118 * representing userdata. 1119 * Must be called holding netconsole_subsys.su_mutex 1120 */ 1121 static int calc_userdata_len(struct netconsole_target *nt) 1122 { 1123 struct userdatum *udm_item; 1124 struct config_item *item; 1125 struct list_head *entry; 1126 int len = 0; 1127 1128 list_for_each(entry, &nt->userdata_group.cg_children) { 1129 item = container_of(entry, struct config_item, ci_entry); 1130 udm_item = to_userdatum(item); 1131 /* Skip userdata with no value set */ 1132 if (udm_item->value[0]) { 1133 len += snprintf(NULL, 0, " %s=%s\n", item->ci_name, 1134 udm_item->value); 1135 } 1136 } 1137 return len; 1138 } 1139 1140 static int update_userdata(struct netconsole_target *nt) 1141 { 1142 struct userdatum *udm_item; 1143 struct config_item *item; 1144 struct list_head *entry; 1145 char *old_buf = NULL; 1146 char *new_buf = NULL; 1147 unsigned long flags; 1148 int offset = 0; 1149 int len; 1150 1151 /* Calculate required buffer size */ 1152 len = calc_userdata_len(nt); 1153 1154 if (WARN_ON_ONCE(len > MAX_EXTRADATA_ENTRY_LEN * MAX_USERDATA_ITEMS)) 1155 return -ENOSPC; 1156 1157 /* Allocate new buffer */ 1158 if (len) { 1159 new_buf = kmalloc(len + 1, GFP_KERNEL); 1160 if (!new_buf) 1161 return -ENOMEM; 1162 } 1163 1164 /* Write userdata to new buffer */ 1165 list_for_each(entry, &nt->userdata_group.cg_children) { 1166 item = container_of(entry, struct config_item, ci_entry); 1167 udm_item = to_userdatum(item); 1168 /* Skip userdata with no value set */ 1169 if (udm_item->value[0]) { 1170 offset += scnprintf(&new_buf[offset], len + 1 - offset, 1171 " %s=%s\n", item->ci_name, 1172 udm_item->value); 1173 } 1174 } 1175 1176 WARN_ON_ONCE(offset != len); 1177 1178 /* Switch to new buffer and free old buffer */ 1179 spin_lock_irqsave(&target_list_lock, flags); 1180 old_buf = nt->userdata; 1181 nt->userdata = new_buf; 1182 nt->userdata_length = offset; 1183 spin_unlock_irqrestore(&target_list_lock, flags); 1184 1185 kfree(old_buf); 1186 1187 return 0; 1188 } 1189 1190 static ssize_t userdatum_value_store(struct config_item *item, const char *buf, 1191 size_t count) 1192 { 1193 struct userdatum *udm = to_userdatum(item); 1194 char old_value[MAX_EXTRADATA_VALUE_LEN]; 1195 struct netconsole_target *nt; 1196 struct userdata *ud; 1197 ssize_t ret; 1198 1199 if (count >= MAX_EXTRADATA_VALUE_LEN) 1200 return -EMSGSIZE; 1201 1202 mutex_lock(&netconsole_subsys.su_mutex); 1203 dynamic_netconsole_mutex_lock(); 1204 /* Snapshot for rollback if update_userdata() fails below */ 1205 strscpy(old_value, udm->value, sizeof(old_value)); 1206 /* count is bounded above, so strscpy() cannot truncate here */ 1207 strscpy(udm->value, buf, sizeof(udm->value)); 1208 trim_newline(udm->value, sizeof(udm->value)); 1209 1210 ud = to_userdata(item->ci_parent); 1211 nt = userdata_to_target(ud); 1212 ret = update_userdata(nt); 1213 if (ret < 0) { 1214 /* Restore the previous value so it matches the live payload */ 1215 strscpy(udm->value, old_value, sizeof(udm->value)); 1216 goto out_unlock; 1217 } 1218 ret = count; 1219 out_unlock: 1220 dynamic_netconsole_mutex_unlock(); 1221 mutex_unlock(&netconsole_subsys.su_mutex); 1222 return ret; 1223 } 1224 1225 /* disable_sysdata_feature - Disable sysdata feature and clean sysdata 1226 * @nt: target that is disabling the feature 1227 * @feature: feature being disabled 1228 */ 1229 static void disable_sysdata_feature(struct netconsole_target *nt, 1230 enum sysdata_feature feature) 1231 { 1232 nt->sysdata_fields &= ~feature; 1233 nt->sysdata[0] = 0; 1234 } 1235 1236 static ssize_t sysdata_msgid_enabled_store(struct config_item *item, 1237 const char *buf, size_t count) 1238 { 1239 struct netconsole_target *nt = to_target(item->ci_parent); 1240 bool msgid_enabled, curr; 1241 ssize_t ret; 1242 1243 ret = kstrtobool(buf, &msgid_enabled); 1244 if (ret) 1245 return ret; 1246 1247 mutex_lock(&netconsole_subsys.su_mutex); 1248 dynamic_netconsole_mutex_lock(); 1249 curr = !!(nt->sysdata_fields & SYSDATA_MSGID); 1250 if (msgid_enabled == curr) 1251 goto unlock_ok; 1252 1253 if (msgid_enabled) 1254 nt->sysdata_fields |= SYSDATA_MSGID; 1255 else 1256 disable_sysdata_feature(nt, SYSDATA_MSGID); 1257 1258 unlock_ok: 1259 ret = count; 1260 dynamic_netconsole_mutex_unlock(); 1261 mutex_unlock(&netconsole_subsys.su_mutex); 1262 return ret; 1263 } 1264 1265 static ssize_t sysdata_release_enabled_store(struct config_item *item, 1266 const char *buf, size_t count) 1267 { 1268 struct netconsole_target *nt = to_target(item->ci_parent); 1269 bool release_enabled, curr; 1270 ssize_t ret; 1271 1272 ret = kstrtobool(buf, &release_enabled); 1273 if (ret) 1274 return ret; 1275 1276 mutex_lock(&netconsole_subsys.su_mutex); 1277 dynamic_netconsole_mutex_lock(); 1278 curr = !!(nt->sysdata_fields & SYSDATA_RELEASE); 1279 if (release_enabled == curr) 1280 goto unlock_ok; 1281 1282 if (release_enabled) 1283 nt->sysdata_fields |= SYSDATA_RELEASE; 1284 else 1285 disable_sysdata_feature(nt, SYSDATA_RELEASE); 1286 1287 unlock_ok: 1288 ret = count; 1289 dynamic_netconsole_mutex_unlock(); 1290 mutex_unlock(&netconsole_subsys.su_mutex); 1291 return ret; 1292 } 1293 1294 static ssize_t sysdata_taskname_enabled_store(struct config_item *item, 1295 const char *buf, size_t count) 1296 { 1297 struct netconsole_target *nt = to_target(item->ci_parent); 1298 bool taskname_enabled, curr; 1299 ssize_t ret; 1300 1301 ret = kstrtobool(buf, &taskname_enabled); 1302 if (ret) 1303 return ret; 1304 1305 mutex_lock(&netconsole_subsys.su_mutex); 1306 dynamic_netconsole_mutex_lock(); 1307 curr = !!(nt->sysdata_fields & SYSDATA_TASKNAME); 1308 if (taskname_enabled == curr) 1309 goto unlock_ok; 1310 1311 if (taskname_enabled) 1312 nt->sysdata_fields |= SYSDATA_TASKNAME; 1313 else 1314 disable_sysdata_feature(nt, SYSDATA_TASKNAME); 1315 1316 unlock_ok: 1317 ret = count; 1318 dynamic_netconsole_mutex_unlock(); 1319 mutex_unlock(&netconsole_subsys.su_mutex); 1320 return ret; 1321 } 1322 1323 /* configfs helper to sysdata cpu_nr feature */ 1324 static ssize_t sysdata_cpu_nr_enabled_store(struct config_item *item, 1325 const char *buf, size_t count) 1326 { 1327 struct netconsole_target *nt = to_target(item->ci_parent); 1328 bool cpu_nr_enabled, curr; 1329 ssize_t ret; 1330 1331 ret = kstrtobool(buf, &cpu_nr_enabled); 1332 if (ret) 1333 return ret; 1334 1335 mutex_lock(&netconsole_subsys.su_mutex); 1336 dynamic_netconsole_mutex_lock(); 1337 curr = !!(nt->sysdata_fields & SYSDATA_CPU_NR); 1338 if (cpu_nr_enabled == curr) 1339 /* no change requested */ 1340 goto unlock_ok; 1341 1342 if (cpu_nr_enabled) 1343 nt->sysdata_fields |= SYSDATA_CPU_NR; 1344 else 1345 /* This is special because sysdata might have remaining data 1346 * from previous sysdata, and it needs to be cleaned. 1347 */ 1348 disable_sysdata_feature(nt, SYSDATA_CPU_NR); 1349 1350 unlock_ok: 1351 ret = count; 1352 dynamic_netconsole_mutex_unlock(); 1353 mutex_unlock(&netconsole_subsys.su_mutex); 1354 return ret; 1355 } 1356 1357 CONFIGFS_ATTR(userdatum_, value); 1358 CONFIGFS_ATTR(sysdata_, cpu_nr_enabled); 1359 CONFIGFS_ATTR(sysdata_, taskname_enabled); 1360 CONFIGFS_ATTR(sysdata_, release_enabled); 1361 CONFIGFS_ATTR(sysdata_, msgid_enabled); 1362 1363 static struct configfs_attribute *userdatum_attrs[] = { 1364 &userdatum_attr_value, 1365 NULL, 1366 }; 1367 1368 static void userdatum_release(struct config_item *item) 1369 { 1370 kfree(to_userdatum(item)); 1371 } 1372 1373 static const struct configfs_item_operations userdatum_ops = { 1374 .release = userdatum_release, 1375 }; 1376 1377 static const struct config_item_type userdatum_type = { 1378 .ct_item_ops = &userdatum_ops, 1379 .ct_attrs = userdatum_attrs, 1380 .ct_owner = THIS_MODULE, 1381 }; 1382 1383 static struct config_item *userdatum_make_item(struct config_group *group, 1384 const char *name) 1385 { 1386 struct netconsole_target *nt; 1387 struct userdatum *udm; 1388 struct userdata *ud; 1389 1390 if (strlen(name) > MAX_EXTRADATA_NAME_LEN) 1391 return ERR_PTR(-ENAMETOOLONG); 1392 1393 ud = to_userdata(&group->cg_item); 1394 nt = userdata_to_target(ud); 1395 if (count_userdata_entries(nt) >= MAX_USERDATA_ITEMS) 1396 return ERR_PTR(-ENOSPC); 1397 1398 udm = kzalloc_obj(*udm); 1399 if (!udm) 1400 return ERR_PTR(-ENOMEM); 1401 1402 config_item_init_type_name(&udm->item, name, &userdatum_type); 1403 return &udm->item; 1404 } 1405 1406 static void userdatum_drop(struct config_group *group, struct config_item *item) 1407 { 1408 struct netconsole_target *nt; 1409 struct userdata *ud; 1410 1411 ud = to_userdata(&group->cg_item); 1412 nt = userdata_to_target(ud); 1413 1414 dynamic_netconsole_mutex_lock(); 1415 update_userdata(nt); 1416 config_item_put(item); 1417 dynamic_netconsole_mutex_unlock(); 1418 } 1419 1420 static struct configfs_attribute *userdata_attrs[] = { 1421 &sysdata_attr_cpu_nr_enabled, 1422 &sysdata_attr_taskname_enabled, 1423 &sysdata_attr_release_enabled, 1424 &sysdata_attr_msgid_enabled, 1425 NULL, 1426 }; 1427 1428 static const struct configfs_group_operations userdata_ops = { 1429 .make_item = userdatum_make_item, 1430 .drop_item = userdatum_drop, 1431 }; 1432 1433 static const struct config_item_type userdata_type = { 1434 .ct_item_ops = &userdatum_ops, 1435 .ct_group_ops = &userdata_ops, 1436 .ct_attrs = userdata_attrs, 1437 .ct_owner = THIS_MODULE, 1438 }; 1439 1440 CONFIGFS_ATTR(, enabled); 1441 CONFIGFS_ATTR(, extended); 1442 CONFIGFS_ATTR(, dev_name); 1443 CONFIGFS_ATTR(, local_port); 1444 CONFIGFS_ATTR(, remote_port); 1445 CONFIGFS_ATTR(, local_ip); 1446 CONFIGFS_ATTR(, remote_ip); 1447 CONFIGFS_ATTR_RO(, local_mac); 1448 CONFIGFS_ATTR(, remote_mac); 1449 CONFIGFS_ATTR(, release); 1450 CONFIGFS_ATTR_RO(, transmit_errors); 1451 1452 static struct configfs_attribute *netconsole_target_attrs[] = { 1453 &attr_enabled, 1454 &attr_extended, 1455 &attr_release, 1456 &attr_dev_name, 1457 &attr_local_port, 1458 &attr_remote_port, 1459 &attr_local_ip, 1460 &attr_remote_ip, 1461 &attr_local_mac, 1462 &attr_remote_mac, 1463 &attr_transmit_errors, 1464 NULL, 1465 }; 1466 1467 /* 1468 * Item operations and type for netconsole_target. 1469 */ 1470 1471 static void netconsole_target_release(struct config_item *item) 1472 { 1473 struct netconsole_target *nt = to_target(item); 1474 1475 kfree(nt->userdata); 1476 kfree(nt); 1477 } 1478 1479 static const struct configfs_item_operations netconsole_target_item_ops = { 1480 .release = netconsole_target_release, 1481 }; 1482 1483 static const struct config_item_type netconsole_target_type = { 1484 .ct_attrs = netconsole_target_attrs, 1485 .ct_item_ops = &netconsole_target_item_ops, 1486 .ct_owner = THIS_MODULE, 1487 }; 1488 1489 static void init_target_config_group(struct netconsole_target *nt, 1490 const char *name) 1491 { 1492 config_group_init_type_name(&nt->group, name, &netconsole_target_type); 1493 config_group_init_type_name(&nt->userdata_group, "userdata", 1494 &userdata_type); 1495 configfs_add_default_group(&nt->userdata_group, &nt->group); 1496 } 1497 1498 static struct netconsole_target *find_cmdline_target(const char *name) 1499 { 1500 struct netconsole_target *nt, *ret = NULL; 1501 unsigned long flags; 1502 1503 spin_lock_irqsave(&target_list_lock, flags); 1504 list_for_each_entry(nt, &target_list, list) { 1505 if (!strcmp(nt->group.cg_item.ci_name, name)) { 1506 ret = nt; 1507 break; 1508 } 1509 } 1510 spin_unlock_irqrestore(&target_list_lock, flags); 1511 1512 return ret; 1513 } 1514 1515 /* 1516 * Group operations and type for netconsole_subsys. 1517 */ 1518 1519 static struct config_group *make_netconsole_target(struct config_group *group, 1520 const char *name) 1521 { 1522 struct netconsole_target *nt; 1523 unsigned long flags; 1524 1525 /* Checking if a target by this name was created at boot time. If so, 1526 * attach a configfs entry to that target. This enables dynamic 1527 * control. 1528 */ 1529 if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX, 1530 strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) { 1531 nt = find_cmdline_target(name); 1532 if (nt) { 1533 init_target_config_group(nt, name); 1534 return &nt->group; 1535 } 1536 } 1537 1538 nt = alloc_and_init(); 1539 if (!nt) 1540 return ERR_PTR(-ENOMEM); 1541 1542 /* Initialize the config_group member */ 1543 init_target_config_group(nt, name); 1544 1545 /* Adding, but it is disabled */ 1546 spin_lock_irqsave(&target_list_lock, flags); 1547 list_add(&nt->list, &target_list); 1548 spin_unlock_irqrestore(&target_list_lock, flags); 1549 1550 return &nt->group; 1551 } 1552 1553 static void drop_netconsole_target(struct config_group *group, 1554 struct config_item *item) 1555 { 1556 struct netconsole_target *nt = to_target(item); 1557 unsigned long flags; 1558 bool needs_cleanup; 1559 1560 dynamic_netconsole_mutex_lock(); 1561 1562 mutex_lock(&target_cleanup_list_lock); 1563 spin_lock_irqsave(&target_list_lock, flags); 1564 /* A target moved to target_cleanup_list by netconsole_netdev_event() 1565 * but not yet processed still owns a netpoll; unlinking it below hides 1566 * it from the cleanup worker, so this path must tear it down itself. 1567 * This covers NETDEV_UNREGISTER (STATE_DEACTIVATED) and 1568 * NETDEV_RELEASE / NETDEV_JOIN (STATE_DISABLED); key off nt->np.dev, 1569 * which stays set until the netpoll is cleaned up. 1570 */ 1571 needs_cleanup = nt->state == STATE_ENABLED || 1572 nt->state == STATE_DEACTIVATED || nt->np.dev; 1573 /* Disable deactivated target to prevent races between resume attempt 1574 * and target removal. 1575 */ 1576 if (nt->state == STATE_DEACTIVATED) 1577 nt->state = STATE_DISABLED; 1578 list_del(&nt->list); 1579 spin_unlock_irqrestore(&target_list_lock, flags); 1580 mutex_unlock(&target_cleanup_list_lock); 1581 1582 dynamic_netconsole_mutex_unlock(); 1583 1584 /* Now that the target has been marked disabled no further work 1585 * can be scheduled. Existing work will skip as targets are not 1586 * deactivated anymore. Cancel any scheduled resume and wait for 1587 * completion. 1588 */ 1589 cancel_work_sync(&nt->resume_wq); 1590 1591 /* 1592 * The target may have never been enabled, or was manually disabled 1593 * before being removed so netpoll may have already been cleaned up. 1594 * netpoll_cleanup() is idempotent (it skips when np->dev is NULL), so 1595 * it is safe even if the cleanup worker already tore the netpoll down. 1596 */ 1597 if (needs_cleanup) { 1598 netconsole_skb_pool_flush(nt); 1599 netpoll_cleanup(&nt->np); 1600 } 1601 1602 config_item_put(&nt->group.cg_item); 1603 } 1604 1605 static const struct configfs_group_operations netconsole_subsys_group_ops = { 1606 .make_group = make_netconsole_target, 1607 .drop_item = drop_netconsole_target, 1608 }; 1609 1610 static const struct config_item_type netconsole_subsys_type = { 1611 .ct_group_ops = &netconsole_subsys_group_ops, 1612 .ct_owner = THIS_MODULE, 1613 }; 1614 1615 /* The netconsole configfs subsystem */ 1616 static struct configfs_subsystem netconsole_subsys = { 1617 .su_group = { 1618 .cg_item = { 1619 .ci_namebuf = "netconsole", 1620 .ci_type = &netconsole_subsys_type, 1621 }, 1622 }, 1623 }; 1624 1625 static void populate_configfs_item(struct netconsole_target *nt, 1626 int cmdline_count) 1627 { 1628 char target_name[16]; 1629 1630 snprintf(target_name, sizeof(target_name), "%s%d", 1631 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count); 1632 init_target_config_group(nt, target_name); 1633 } 1634 1635 static int sysdata_append_cpu_nr(struct netconsole_target *nt, int offset, 1636 struct nbcon_write_context *wctxt) 1637 { 1638 return scnprintf(&nt->sysdata[offset], 1639 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n", 1640 wctxt->cpu); 1641 } 1642 1643 static int sysdata_append_taskname(struct netconsole_target *nt, int offset, 1644 struct nbcon_write_context *wctxt) 1645 { 1646 return scnprintf(&nt->sysdata[offset], 1647 MAX_EXTRADATA_ENTRY_LEN, " taskname=%s\n", 1648 wctxt->comm); 1649 } 1650 1651 static int sysdata_append_release(struct netconsole_target *nt, int offset) 1652 { 1653 return scnprintf(&nt->sysdata[offset], 1654 MAX_EXTRADATA_ENTRY_LEN, " release=%s\n", 1655 init_utsname()->release); 1656 } 1657 1658 static int sysdata_append_msgid(struct netconsole_target *nt, int offset) 1659 { 1660 wrapping_assign_add(nt->msgcounter, 1); 1661 return scnprintf(&nt->sysdata[offset], 1662 MAX_EXTRADATA_ENTRY_LEN, " msgid=%u\n", 1663 nt->msgcounter); 1664 } 1665 1666 /* 1667 * prepare_sysdata - append sysdata in runtime 1668 * @nt: target to send message to 1669 * @wctxt: nbcon write context containing message metadata 1670 */ 1671 static int prepare_sysdata(struct netconsole_target *nt, 1672 struct nbcon_write_context *wctxt) 1673 { 1674 int sysdata_len = 0; 1675 1676 if (!nt->sysdata_fields) 1677 goto out; 1678 1679 if (nt->sysdata_fields & SYSDATA_CPU_NR) 1680 sysdata_len += sysdata_append_cpu_nr(nt, sysdata_len, wctxt); 1681 if (nt->sysdata_fields & SYSDATA_TASKNAME) 1682 sysdata_len += sysdata_append_taskname(nt, sysdata_len, wctxt); 1683 if (nt->sysdata_fields & SYSDATA_RELEASE) 1684 sysdata_len += sysdata_append_release(nt, sysdata_len); 1685 if (nt->sysdata_fields & SYSDATA_MSGID) 1686 sysdata_len += sysdata_append_msgid(nt, sysdata_len); 1687 1688 WARN_ON_ONCE(sysdata_len > 1689 MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS); 1690 1691 out: 1692 return sysdata_len; 1693 } 1694 #endif /* CONFIG_NETCONSOLE_DYNAMIC */ 1695 1696 /* Handle network interface device notifications */ 1697 static int netconsole_netdev_event(struct notifier_block *this, 1698 unsigned long event, void *ptr) 1699 { 1700 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1701 struct netconsole_target *nt, *tmp; 1702 bool stopped = false; 1703 unsigned long flags; 1704 1705 if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER || 1706 event == NETDEV_RELEASE || event == NETDEV_JOIN || 1707 event == NETDEV_REGISTER)) 1708 goto done; 1709 1710 mutex_lock(&target_cleanup_list_lock); 1711 spin_lock_irqsave(&target_list_lock, flags); 1712 list_for_each_entry_safe(nt, tmp, &target_list, list) { 1713 netconsole_target_get(nt); 1714 if (nt->np.dev == dev) { 1715 switch (event) { 1716 case NETDEV_CHANGENAME: 1717 strscpy(nt->np.dev_name, dev->name, IFNAMSIZ); 1718 break; 1719 case NETDEV_RELEASE: 1720 case NETDEV_JOIN: 1721 /* transition target to DISABLED instead of 1722 * DEACTIVATED when (de)enslaving devices as 1723 * their targets should not be automatically 1724 * resumed when the interface is brought up. 1725 */ 1726 nt->state = STATE_DISABLED; 1727 list_move(&nt->list, &target_cleanup_list); 1728 stopped = true; 1729 break; 1730 case NETDEV_UNREGISTER: 1731 nt->state = STATE_DEACTIVATED; 1732 list_move(&nt->list, &target_cleanup_list); 1733 stopped = true; 1734 } 1735 } 1736 if ((event == NETDEV_REGISTER || event == NETDEV_CHANGENAME) && 1737 deactivated_target_match(nt, dev)) 1738 /* Schedule resume on a workqueue as it will attempt 1739 * to UP the device, which can't be done as part of this 1740 * notifier. 1741 */ 1742 queue_work(netconsole_wq, &nt->resume_wq); 1743 netconsole_target_put(nt); 1744 } 1745 spin_unlock_irqrestore(&target_list_lock, flags); 1746 mutex_unlock(&target_cleanup_list_lock); 1747 1748 if (stopped) { 1749 const char *msg = "had an event"; 1750 1751 switch (event) { 1752 case NETDEV_UNREGISTER: 1753 msg = "unregistered"; 1754 break; 1755 case NETDEV_RELEASE: 1756 msg = "released slaves"; 1757 break; 1758 case NETDEV_JOIN: 1759 msg = "is joining a master device"; 1760 break; 1761 } 1762 pr_info("network logging stopped on interface %s as it %s\n", 1763 dev->name, msg); 1764 } 1765 1766 /* Process target_cleanup_list entries. By the end, target_cleanup_list 1767 * should be empty 1768 */ 1769 netconsole_process_cleanups_core(); 1770 1771 done: 1772 return NOTIFY_DONE; 1773 } 1774 1775 static struct notifier_block netconsole_netdev_notifier = { 1776 .notifier_call = netconsole_netdev_event, 1777 }; 1778 1779 /* Pop a pre-allocated skb from the pool and request a refill. 1780 * 1781 * The pool is refilled with MAX_SKB_SIZE buffers, so a pooled skb cannot 1782 * satisfy a larger request. Return NULL in that case rather than handing 1783 * back a too-small skb that would later trip skb_over_panic() in skb_put(); 1784 * the caller still polls and retries, and alloc_skb() itself can satisfy the 1785 * oversized request once memory frees up. 1786 * 1787 * The refill is requested via schedule_work(), which takes the workqueue 1788 * pool locks and is therefore not NMI-safe. Skip the refill when called 1789 * from NMI context; the next non-NMI caller will top the pool back up. 1790 */ 1791 static struct sk_buff *netcons_skb_pop(struct netconsole_target *nt, int len) 1792 { 1793 struct sk_buff *skb; 1794 1795 if (len > MAX_SKB_SIZE) { 1796 /* net_warn_ratelimited() pulls in printk machinery that is not 1797 * NMI-safe and could recurse into the nbcon console we are 1798 * servicing, so only warn outside NMI. 1799 */ 1800 if (!in_nmi()) 1801 net_warn_ratelimited("netconsole: dropping message, requested skb len %d exceeds pool buffer size %zu on %s\n", 1802 len, (size_t)MAX_SKB_SIZE, 1803 nt->np.dev->name); 1804 return NULL; 1805 } 1806 1807 skb = skb_dequeue(&nt->skb_pool); 1808 if (!in_nmi()) 1809 schedule_work(&nt->refill_wq); 1810 1811 return skb; 1812 } 1813 1814 static struct sk_buff *find_skb(struct netconsole_target *nt, int len, 1815 int reserve) 1816 { 1817 struct netpoll *np = &nt->np; 1818 int count = 0; 1819 struct sk_buff *skb; 1820 1821 netpoll_zap_completion_queue(); 1822 repeat: 1823 1824 skb = alloc_skb(len, GFP_ATOMIC | __GFP_NOWARN); 1825 if (!skb) 1826 skb = netcons_skb_pop(nt, len); 1827 1828 if (!skb) { 1829 if (++count < 10) { 1830 netpoll_poll_dev(np->dev); 1831 goto repeat; 1832 } 1833 return NULL; 1834 } 1835 1836 refcount_set(&skb->users, 1); 1837 skb_reserve(skb, reserve); 1838 return skb; 1839 } 1840 1841 static void netpoll_udp_checksum(struct netpoll *np, struct sk_buff *skb, 1842 int len) 1843 { 1844 struct udphdr *udph; 1845 int udp_len; 1846 1847 udp_len = len + sizeof(struct udphdr); 1848 udph = udp_hdr(skb); 1849 1850 /* check needs to be set, since it will be consumed in csum_partial */ 1851 udph->check = 0; 1852 if (np->ipv6) 1853 udph->check = csum_ipv6_magic(&np->local_ip.in6, 1854 &np->remote_ip.in6, 1855 udp_len, IPPROTO_UDP, 1856 csum_partial(udph, udp_len, 0)); 1857 else 1858 udph->check = csum_tcpudp_magic(np->local_ip.ip, 1859 np->remote_ip.ip, 1860 udp_len, IPPROTO_UDP, 1861 csum_partial(udph, udp_len, 0)); 1862 if (udph->check == 0) 1863 udph->check = CSUM_MANGLED_0; 1864 } 1865 1866 static void push_udp(struct netpoll *np, struct sk_buff *skb, int len) 1867 { 1868 struct udphdr *udph; 1869 int udp_len; 1870 1871 udp_len = len + sizeof(struct udphdr); 1872 1873 skb_push(skb, sizeof(struct udphdr)); 1874 skb_reset_transport_header(skb); 1875 1876 udph = udp_hdr(skb); 1877 udph->source = htons(np->local_port); 1878 udph->dest = htons(np->remote_port); 1879 udp_set_len_short(udph, udp_len); 1880 1881 netpoll_udp_checksum(np, skb, len); 1882 } 1883 1884 static void push_eth(struct netpoll *np, struct sk_buff *skb) 1885 { 1886 struct ethhdr *eth; 1887 1888 eth = skb_push(skb, ETH_HLEN); 1889 skb_reset_mac_header(skb); 1890 ether_addr_copy(eth->h_source, np->dev->dev_addr); 1891 ether_addr_copy(eth->h_dest, np->remote_mac); 1892 if (np->ipv6) 1893 eth->h_proto = htons(ETH_P_IPV6); 1894 else 1895 eth->h_proto = htons(ETH_P_IP); 1896 } 1897 1898 static void push_ipv4(struct netpoll *np, struct sk_buff *skb, int len) 1899 { 1900 static atomic_t ip_ident; 1901 struct iphdr *iph; 1902 int ip_len; 1903 1904 ip_len = len + sizeof(struct udphdr) + sizeof(struct iphdr); 1905 1906 skb_push(skb, sizeof(struct iphdr)); 1907 skb_reset_network_header(skb); 1908 iph = ip_hdr(skb); 1909 1910 /* iph->version = 4; iph->ihl = 5; */ 1911 *(unsigned char *)iph = 0x45; 1912 iph->tos = 0; 1913 put_unaligned(htons(ip_len), &iph->tot_len); 1914 iph->id = htons(atomic_inc_return(&ip_ident)); 1915 iph->frag_off = 0; 1916 iph->ttl = 64; 1917 iph->protocol = IPPROTO_UDP; 1918 iph->check = 0; 1919 put_unaligned(np->local_ip.ip, &iph->saddr); 1920 put_unaligned(np->remote_ip.ip, &iph->daddr); 1921 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 1922 skb->protocol = htons(ETH_P_IP); 1923 } 1924 1925 static void push_ipv6(struct netpoll *np, struct sk_buff *skb, int len) 1926 { 1927 struct ipv6hdr *ip6h; 1928 1929 skb_push(skb, sizeof(struct ipv6hdr)); 1930 skb_reset_network_header(skb); 1931 ip6h = ipv6_hdr(skb); 1932 1933 /* ip6h->version = 6; ip6h->priority = 0; */ 1934 *(unsigned char *)ip6h = 0x60; 1935 ip6h->flow_lbl[0] = 0; 1936 ip6h->flow_lbl[1] = 0; 1937 ip6h->flow_lbl[2] = 0; 1938 1939 ip6h->payload_len = htons(sizeof(struct udphdr) + len); 1940 ip6h->nexthdr = IPPROTO_UDP; 1941 ip6h->hop_limit = 32; 1942 ip6h->saddr = np->local_ip.in6; 1943 ip6h->daddr = np->remote_ip.in6; 1944 1945 skb->protocol = htons(ETH_P_IPV6); 1946 } 1947 1948 static int netpoll_send_udp(struct netconsole_target *nt, const char *msg, 1949 int len) 1950 { 1951 struct netpoll *np = &nt->np; 1952 int total_len, ip_len, udp_len; 1953 struct sk_buff *skb; 1954 1955 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 1956 WARN_ON_ONCE(!irqs_disabled()); 1957 1958 udp_len = len + sizeof(struct udphdr); 1959 if (np->ipv6) 1960 ip_len = udp_len + sizeof(struct ipv6hdr); 1961 else 1962 ip_len = udp_len + sizeof(struct iphdr); 1963 1964 total_len = ip_len + LL_RESERVED_SPACE(np->dev); 1965 1966 skb = find_skb(nt, total_len + np->dev->needed_tailroom, 1967 total_len - len); 1968 if (!skb) 1969 return -ENOMEM; 1970 1971 skb_copy_to_linear_data(skb, msg, len); 1972 skb_put(skb, len); 1973 1974 push_udp(np, skb, len); 1975 if (np->ipv6) 1976 push_ipv6(np, skb, len); 1977 else 1978 push_ipv4(np, skb, len); 1979 push_eth(np, skb); 1980 skb->dev = np->dev; 1981 1982 return (int)netpoll_send_skb(np, skb); 1983 } 1984 1985 /** 1986 * send_udp - Wrapper for netpoll_send_udp that counts errors 1987 * @nt: target to send message to 1988 * @msg: message to send 1989 * @len: length of message 1990 * 1991 * Calls netpoll_send_udp and classifies the return value. If an error 1992 * occurred it increments statistics in nt->stats accordingly. 1993 * Only calls netpoll_send_udp if CONFIG_NETCONSOLE_DYNAMIC is disabled. 1994 */ 1995 static void send_udp(struct netconsole_target *nt, const char *msg, int len) 1996 { 1997 int result = netpoll_send_udp(nt, msg, len); 1998 1999 if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) { 2000 if (result == NET_XMIT_DROP) { 2001 u64_stats_update_begin(&nt->stats.syncp); 2002 u64_stats_inc(&nt->stats.xmit_drop_count); 2003 u64_stats_update_end(&nt->stats.syncp); 2004 } else if (result == -ENOMEM) { 2005 u64_stats_update_begin(&nt->stats.syncp); 2006 u64_stats_inc(&nt->stats.enomem_count); 2007 u64_stats_update_end(&nt->stats.syncp); 2008 } 2009 } 2010 } 2011 2012 static void send_msg_no_fragmentation(struct netconsole_target *nt, 2013 const char *msg, 2014 int msg_len, 2015 int release_len) 2016 { 2017 const char *userdata = NULL; 2018 const char *sysdata = NULL; 2019 const char *release; 2020 2021 #ifdef CONFIG_NETCONSOLE_DYNAMIC 2022 userdata = nt->userdata; 2023 sysdata = nt->sysdata; 2024 #endif 2025 2026 if (release_len) { 2027 release = init_utsname()->release; 2028 2029 scnprintf(nt->buf, sizeof(nt->buf), "%s,%.*s", release, 2030 msg_len, msg); 2031 msg_len += release_len; 2032 } else { 2033 memcpy(nt->buf, msg, msg_len); 2034 } 2035 2036 if (userdata) 2037 msg_len += scnprintf(&nt->buf[msg_len], 2038 sizeof(nt->buf) - msg_len, "%s", 2039 userdata); 2040 2041 if (sysdata) 2042 msg_len += scnprintf(&nt->buf[msg_len], 2043 sizeof(nt->buf) - msg_len, "%s", 2044 sysdata); 2045 2046 send_udp(nt, nt->buf, msg_len); 2047 } 2048 2049 static void append_release(char *buf) 2050 { 2051 const char *release; 2052 2053 release = init_utsname()->release; 2054 scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release); 2055 } 2056 2057 static void send_fragmented_body(struct netconsole_target *nt, 2058 const char *msgbody_ptr, int header_len, 2059 int msgbody_len, int sysdata_len) 2060 { 2061 const char *userdata_ptr = NULL; 2062 const char *sysdata_ptr = NULL; 2063 int data_len, data_sent = 0; 2064 int userdata_offset = 0; 2065 int sysdata_offset = 0; 2066 int msgbody_offset = 0; 2067 int userdata_len = 0; 2068 2069 #ifdef CONFIG_NETCONSOLE_DYNAMIC 2070 userdata_ptr = nt->userdata; 2071 sysdata_ptr = nt->sysdata; 2072 userdata_len = nt->userdata_length; 2073 #endif 2074 if (WARN_ON_ONCE(!userdata_ptr && userdata_len != 0)) 2075 return; 2076 2077 if (WARN_ON_ONCE(!sysdata_ptr && sysdata_len != 0)) 2078 return; 2079 2080 /* data_len represents the number of bytes that will be sent. This is 2081 * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple 2082 * packets 2083 */ 2084 data_len = msgbody_len + userdata_len + sysdata_len; 2085 2086 /* In each iteration of the while loop below, we send a packet 2087 * containing the header and a portion of the data. The data is 2088 * composed of three parts: msgbody, userdata, and sysdata. 2089 * We keep track of how many bytes have been sent from each part using 2090 * the *_offset variables. 2091 * We keep track of how many bytes have been sent overall using the 2092 * data_sent variable, which ranges from 0 to the total bytes to be 2093 * sent. 2094 */ 2095 while (data_sent < data_len) { 2096 int userdata_left = userdata_len - userdata_offset; 2097 int sysdata_left = sysdata_len - sysdata_offset; 2098 int msgbody_left = msgbody_len - msgbody_offset; 2099 int buf_offset = 0; 2100 int this_chunk = 0; 2101 2102 /* header is already populated in nt->buf, just append to it */ 2103 buf_offset = header_len; 2104 2105 buf_offset += scnprintf(nt->buf + buf_offset, 2106 MAX_PRINT_CHUNK - buf_offset, 2107 ",ncfrag=%d/%d;", data_sent, 2108 data_len); 2109 2110 /* append msgbody first */ 2111 this_chunk = min(msgbody_left, MAX_PRINT_CHUNK - buf_offset); 2112 memcpy(nt->buf + buf_offset, msgbody_ptr + msgbody_offset, 2113 this_chunk); 2114 msgbody_offset += this_chunk; 2115 buf_offset += this_chunk; 2116 data_sent += this_chunk; 2117 2118 /* after msgbody, append userdata */ 2119 if (userdata_ptr && userdata_left) { 2120 this_chunk = min(userdata_left, 2121 MAX_PRINT_CHUNK - buf_offset); 2122 memcpy(nt->buf + buf_offset, 2123 userdata_ptr + userdata_offset, this_chunk); 2124 userdata_offset += this_chunk; 2125 buf_offset += this_chunk; 2126 data_sent += this_chunk; 2127 } 2128 2129 /* after userdata, append sysdata */ 2130 if (sysdata_ptr && sysdata_left) { 2131 this_chunk = min(sysdata_left, 2132 MAX_PRINT_CHUNK - buf_offset); 2133 memcpy(nt->buf + buf_offset, 2134 sysdata_ptr + sysdata_offset, this_chunk); 2135 sysdata_offset += this_chunk; 2136 buf_offset += this_chunk; 2137 data_sent += this_chunk; 2138 } 2139 2140 /* if all is good, send the packet out */ 2141 if (WARN_ON_ONCE(data_sent > data_len)) 2142 return; 2143 2144 send_udp(nt, nt->buf, buf_offset); 2145 } 2146 } 2147 2148 static void send_msg_fragmented(struct netconsole_target *nt, 2149 const char *msg, 2150 int msg_len, 2151 int release_len, 2152 int sysdata_len) 2153 { 2154 int header_len, msgbody_len; 2155 const char *msgbody; 2156 2157 /* need to insert extra header fields, detect header and msgbody */ 2158 msgbody = memchr(msg, ';', msg_len); 2159 if (WARN_ON_ONCE(!msgbody)) 2160 return; 2161 2162 header_len = msgbody - msg; 2163 msgbody_len = msg_len - header_len - 1; 2164 msgbody++; 2165 2166 /* 2167 * Transfer multiple chunks with the following extra header. 2168 * "ncfrag=<byte-offset>/<total-bytes>" 2169 */ 2170 if (release_len) 2171 append_release(nt->buf); 2172 2173 /* Copy the header into the buffer */ 2174 memcpy(nt->buf + release_len, msg, header_len); 2175 header_len += release_len; 2176 2177 /* for now on, the header will be persisted, and the msgbody 2178 * will be replaced 2179 */ 2180 send_fragmented_body(nt, msgbody, header_len, msgbody_len, 2181 sysdata_len); 2182 } 2183 2184 /** 2185 * send_ext_msg_udp - send extended log message to target 2186 * @nt: target to send message to 2187 * @wctxt: nbcon write context containing message and metadata 2188 * 2189 * Transfer extended log message to @nt. If message is longer than 2190 * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with 2191 * ncfrag header field added to identify them. 2192 */ 2193 static void send_ext_msg_udp(struct netconsole_target *nt, 2194 struct nbcon_write_context *wctxt) 2195 { 2196 int userdata_len = 0; 2197 int release_len = 0; 2198 int sysdata_len = 0; 2199 int len; 2200 2201 #ifdef CONFIG_NETCONSOLE_DYNAMIC 2202 sysdata_len = prepare_sysdata(nt, wctxt); 2203 userdata_len = nt->userdata_length; 2204 #endif 2205 if (nt->release) 2206 release_len = strlen(init_utsname()->release) + 1; 2207 2208 len = wctxt->len + release_len + sysdata_len + userdata_len; 2209 if (len <= MAX_PRINT_CHUNK) 2210 return send_msg_no_fragmentation(nt, wctxt->outbuf, 2211 wctxt->len, release_len); 2212 2213 return send_msg_fragmented(nt, wctxt->outbuf, wctxt->len, release_len, 2214 sysdata_len); 2215 } 2216 2217 static void send_msg_udp(struct netconsole_target *nt, const char *msg, 2218 unsigned int len) 2219 { 2220 const char *tmp = msg; 2221 int frag, left = len; 2222 2223 while (left > 0) { 2224 frag = min(left, MAX_PRINT_CHUNK); 2225 send_udp(nt, tmp, frag); 2226 tmp += frag; 2227 left -= frag; 2228 } 2229 } 2230 2231 /** 2232 * netconsole_write - Generic function to send a msg to all targets 2233 * @wctxt: nbcon write context 2234 * @extended: "true" for extended console mode 2235 * 2236 * Given an nbcon write context, send the message to the netconsole targets 2237 */ 2238 static void netconsole_write(struct nbcon_write_context *wctxt, bool extended) 2239 { 2240 struct netconsole_target *nt; 2241 2242 if (oops_only && !oops_in_progress) 2243 return; 2244 2245 list_for_each_entry(nt, &target_list, list) { 2246 if (nt->extended != extended || nt->state != STATE_ENABLED || 2247 !netif_running(nt->np.dev)) 2248 continue; 2249 2250 /* If nbcon_enter_unsafe() fails, just return given netconsole 2251 * lost the ownership, and iterating over the targets will not 2252 * be able to re-acquire. 2253 */ 2254 if (!nbcon_enter_unsafe(wctxt)) 2255 return; 2256 2257 if (extended) 2258 send_ext_msg_udp(nt, wctxt); 2259 else 2260 send_msg_udp(nt, wctxt->outbuf, wctxt->len); 2261 2262 nbcon_exit_unsafe(wctxt); 2263 } 2264 } 2265 2266 static void netconsole_write_ext(struct console *con __always_unused, 2267 struct nbcon_write_context *wctxt) 2268 { 2269 netconsole_write(wctxt, true); 2270 } 2271 2272 static void netconsole_write_basic(struct console *con __always_unused, 2273 struct nbcon_write_context *wctxt) 2274 { 2275 netconsole_write(wctxt, false); 2276 } 2277 2278 static void netconsole_device_lock(struct console *con __always_unused, 2279 unsigned long *flags) 2280 __acquires(&target_list_lock) 2281 { 2282 spin_lock_irqsave(&target_list_lock, *flags); 2283 } 2284 2285 static void netconsole_device_unlock(struct console *con __always_unused, 2286 unsigned long flags) 2287 __releases(&target_list_lock) 2288 { 2289 spin_unlock_irqrestore(&target_list_lock, flags); 2290 } 2291 2292 static int netconsole_parser_cmdline(struct netpoll *np, char *opt) 2293 { 2294 bool ipversion_set = false; 2295 char *cur = opt; 2296 char *delim; 2297 int ipv6; 2298 2299 if (*cur != '@') { 2300 delim = strchr(cur, '@'); 2301 if (!delim) 2302 goto parse_failed; 2303 *delim = 0; 2304 if (kstrtou16(cur, 10, &np->local_port)) 2305 goto parse_failed; 2306 cur = delim; 2307 } 2308 cur++; 2309 2310 if (*cur != '/') { 2311 ipversion_set = true; 2312 delim = strchr(cur, '/'); 2313 if (!delim) 2314 goto parse_failed; 2315 *delim = 0; 2316 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip); 2317 if (ipv6 < 0) 2318 goto parse_failed; 2319 else 2320 np->ipv6 = (bool)ipv6; 2321 cur = delim; 2322 } 2323 cur++; 2324 2325 if (*cur != ',') { 2326 /* parse out dev_name or dev_mac */ 2327 delim = strchr(cur, ','); 2328 if (!delim) 2329 goto parse_failed; 2330 *delim = 0; 2331 2332 np->dev_name[0] = '\0'; 2333 eth_broadcast_addr(np->dev_mac); 2334 if (!strchr(cur, ':')) 2335 strscpy(np->dev_name, cur, sizeof(np->dev_name)); 2336 else if (!mac_pton(cur, np->dev_mac)) 2337 goto parse_failed; 2338 2339 cur = delim; 2340 } 2341 cur++; 2342 2343 if (*cur != '@') { 2344 /* dst port */ 2345 delim = strchr(cur, '@'); 2346 if (!delim) 2347 goto parse_failed; 2348 *delim = 0; 2349 if (*cur == ' ' || *cur == '\t') 2350 np_info(np, "warning: whitespace is not allowed\n"); 2351 if (kstrtou16(cur, 10, &np->remote_port)) 2352 goto parse_failed; 2353 cur = delim; 2354 } 2355 cur++; 2356 2357 /* dst ip */ 2358 delim = strchr(cur, '/'); 2359 if (!delim) 2360 goto parse_failed; 2361 *delim = 0; 2362 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip); 2363 if (ipv6 < 0) 2364 goto parse_failed; 2365 else if (ipversion_set && np->ipv6 != (bool)ipv6) 2366 goto parse_failed; 2367 else 2368 np->ipv6 = (bool)ipv6; 2369 cur = delim + 1; 2370 2371 if (*cur != 0) { 2372 /* MAC address */ 2373 if (!mac_pton(cur, np->remote_mac)) 2374 goto parse_failed; 2375 } 2376 2377 netconsole_print_banner(np); 2378 2379 return 0; 2380 2381 parse_failed: 2382 np_info(np, "couldn't parse config at '%s'!\n", cur); 2383 return -1; 2384 } 2385 2386 /* Allocate new target (from boot/module param) and setup netpoll for it */ 2387 static struct netconsole_target *alloc_param_target(char *target_config, 2388 int cmdline_count) 2389 { 2390 struct netconsole_target *nt; 2391 int err; 2392 2393 nt = alloc_and_init(); 2394 if (!nt) { 2395 err = -ENOMEM; 2396 goto fail; 2397 } 2398 2399 if (*target_config == '+') { 2400 nt->extended = true; 2401 target_config++; 2402 } 2403 2404 if (*target_config == 'r') { 2405 if (!nt->extended) { 2406 pr_err("Netconsole configuration error. Release feature requires extended log message"); 2407 err = -EINVAL; 2408 goto fail; 2409 } 2410 nt->release = true; 2411 target_config++; 2412 } 2413 2414 /* Parse parameters and setup netpoll */ 2415 err = netconsole_parser_cmdline(&nt->np, target_config); 2416 if (err) 2417 goto fail; 2418 2419 /* Initialise the skb pool before netpoll_setup() so the pool is 2420 * valid as soon as nt->np.dev becomes visible. The target is not 2421 * yet on target_list, so a netdev event cannot reach it here, but 2422 * mirror the configfs path for symmetry. 2423 */ 2424 netconsole_skb_pool_init(nt); 2425 2426 err = netpoll_setup(&nt->np); 2427 if (err) { 2428 pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n", 2429 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count); 2430 netconsole_skb_pool_flush(nt); 2431 if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) 2432 /* only fail if dynamic reconfiguration is set, 2433 * otherwise, keep the target in the list, but disabled. 2434 */ 2435 goto fail; 2436 } else { 2437 nt->state = STATE_ENABLED; 2438 } 2439 populate_configfs_item(nt, cmdline_count); 2440 2441 return nt; 2442 2443 fail: 2444 kfree(nt); 2445 return ERR_PTR(err); 2446 } 2447 2448 /* Cleanup netpoll for given target (from boot/module param) and free it */ 2449 static void free_param_target(struct netconsole_target *nt) 2450 { 2451 cancel_work_sync(&nt->resume_wq); 2452 if (nt->state == STATE_ENABLED) 2453 netconsole_skb_pool_flush(nt); 2454 netpoll_cleanup(&nt->np); 2455 #ifdef CONFIG_NETCONSOLE_DYNAMIC 2456 kfree(nt->userdata); 2457 #endif 2458 kfree(nt); 2459 } 2460 2461 static struct console netconsole_ext = { 2462 .name = "netcon_ext", 2463 .flags = CON_ENABLED | CON_EXTENDED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE, 2464 .write_thread = netconsole_write_ext, 2465 .write_atomic = netconsole_write_ext, 2466 .device_lock = netconsole_device_lock, 2467 .device_unlock = netconsole_device_unlock, 2468 }; 2469 2470 static struct console netconsole = { 2471 .name = "netcon", 2472 .flags = CON_ENABLED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE, 2473 .write_thread = netconsole_write_basic, 2474 .write_atomic = netconsole_write_basic, 2475 .device_lock = netconsole_device_lock, 2476 .device_unlock = netconsole_device_unlock, 2477 }; 2478 2479 static int __init init_netconsole(void) 2480 { 2481 int err; 2482 struct netconsole_target *nt, *tmp; 2483 u32 console_type_needed = 0; 2484 unsigned int count = 0; 2485 unsigned long flags; 2486 char *target_config; 2487 char *input = config; 2488 2489 if (strnlen(input, MAX_PARAM_LENGTH)) { 2490 while ((target_config = strsep(&input, ";"))) { 2491 nt = alloc_param_target(target_config, count); 2492 if (IS_ERR(nt)) { 2493 if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) 2494 continue; 2495 err = PTR_ERR(nt); 2496 goto fail; 2497 } 2498 /* Dump existing printks when we register */ 2499 if (nt->extended) { 2500 console_type_needed |= CONS_EXTENDED; 2501 netconsole_ext.flags |= CON_PRINTBUFFER; 2502 } else { 2503 console_type_needed |= CONS_BASIC; 2504 netconsole.flags |= CON_PRINTBUFFER; 2505 } 2506 2507 spin_lock_irqsave(&target_list_lock, flags); 2508 list_add(&nt->list, &target_list); 2509 spin_unlock_irqrestore(&target_list_lock, flags); 2510 count++; 2511 } 2512 } 2513 2514 netconsole_wq = alloc_workqueue("netconsole", WQ_UNBOUND, 0); 2515 if (!netconsole_wq) { 2516 err = -ENOMEM; 2517 goto fail; 2518 } 2519 2520 err = register_netdevice_notifier(&netconsole_netdev_notifier); 2521 if (err) 2522 goto fail; 2523 2524 err = dynamic_netconsole_init(); 2525 if (err) 2526 goto undonotifier; 2527 2528 if (console_type_needed & CONS_EXTENDED) 2529 register_console(&netconsole_ext); 2530 if (console_type_needed & CONS_BASIC) 2531 register_console(&netconsole); 2532 pr_info("network logging started\n"); 2533 2534 return err; 2535 2536 undonotifier: 2537 unregister_netdevice_notifier(&netconsole_netdev_notifier); 2538 2539 fail: 2540 pr_err("cleaning up\n"); 2541 2542 if (netconsole_wq) 2543 flush_workqueue(netconsole_wq); 2544 /* 2545 * Remove all targets and destroy them (only targets created 2546 * from the boot/module option exist here). Skipping the list 2547 * lock is safe here, and netpoll_cleanup() will sleep. 2548 */ 2549 list_for_each_entry_safe(nt, tmp, &target_list, list) { 2550 list_del(&nt->list); 2551 free_param_target(nt); 2552 } 2553 2554 if (netconsole_wq) 2555 destroy_workqueue(netconsole_wq); 2556 2557 return err; 2558 } 2559 2560 static void __exit cleanup_netconsole(void) 2561 { 2562 struct netconsole_target *nt, *tmp; 2563 2564 if (console_is_registered(&netconsole_ext)) 2565 unregister_console(&netconsole_ext); 2566 if (console_is_registered(&netconsole)) 2567 unregister_console(&netconsole); 2568 dynamic_netconsole_exit(); 2569 unregister_netdevice_notifier(&netconsole_netdev_notifier); 2570 flush_workqueue(netconsole_wq); 2571 2572 /* 2573 * Targets created via configfs pin references on our module 2574 * and would first be rmdir(2)'ed from userspace. We reach 2575 * here only when they are already destroyed, and only those 2576 * created from the boot/module option are left, so remove and 2577 * destroy them. Skipping the list lock is safe here, and 2578 * netpoll_cleanup() will sleep. 2579 */ 2580 list_for_each_entry_safe(nt, tmp, &target_list, list) { 2581 list_del(&nt->list); 2582 free_param_target(nt); 2583 } 2584 2585 destroy_workqueue(netconsole_wq); 2586 } 2587 2588 /* 2589 * Use late_initcall to ensure netconsole is 2590 * initialized after network device driver if built-in. 2591 * 2592 * late_initcall() and module_init() are identical if built as module. 2593 */ 2594 late_initcall(init_netconsole); 2595 module_exit(cleanup_netconsole); 2596