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