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 struct nbcon_write_context *wctxt) 1495 { 1496 return scnprintf(&nt->sysdata[offset], 1497 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n", 1498 wctxt->cpu); 1499 } 1500 1501 static int sysdata_append_taskname(struct netconsole_target *nt, int offset, 1502 struct nbcon_write_context *wctxt) 1503 { 1504 return scnprintf(&nt->sysdata[offset], 1505 MAX_EXTRADATA_ENTRY_LEN, " taskname=%s\n", 1506 wctxt->comm); 1507 } 1508 1509 static int sysdata_append_release(struct netconsole_target *nt, int offset) 1510 { 1511 return scnprintf(&nt->sysdata[offset], 1512 MAX_EXTRADATA_ENTRY_LEN, " release=%s\n", 1513 init_utsname()->release); 1514 } 1515 1516 static int sysdata_append_msgid(struct netconsole_target *nt, int offset) 1517 { 1518 wrapping_assign_add(nt->msgcounter, 1); 1519 return scnprintf(&nt->sysdata[offset], 1520 MAX_EXTRADATA_ENTRY_LEN, " msgid=%u\n", 1521 nt->msgcounter); 1522 } 1523 1524 /* 1525 * prepare_sysdata - append sysdata in runtime 1526 * @nt: target to send message to 1527 * @wctxt: nbcon write context containing message metadata 1528 */ 1529 static int prepare_sysdata(struct netconsole_target *nt, 1530 struct nbcon_write_context *wctxt) 1531 { 1532 int sysdata_len = 0; 1533 1534 if (!nt->sysdata_fields) 1535 goto out; 1536 1537 if (nt->sysdata_fields & SYSDATA_CPU_NR) 1538 sysdata_len += sysdata_append_cpu_nr(nt, sysdata_len, wctxt); 1539 if (nt->sysdata_fields & SYSDATA_TASKNAME) 1540 sysdata_len += sysdata_append_taskname(nt, sysdata_len, wctxt); 1541 if (nt->sysdata_fields & SYSDATA_RELEASE) 1542 sysdata_len += sysdata_append_release(nt, sysdata_len); 1543 if (nt->sysdata_fields & SYSDATA_MSGID) 1544 sysdata_len += sysdata_append_msgid(nt, sysdata_len); 1545 1546 WARN_ON_ONCE(sysdata_len > 1547 MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS); 1548 1549 out: 1550 return sysdata_len; 1551 } 1552 #endif /* CONFIG_NETCONSOLE_DYNAMIC */ 1553 1554 /* Handle network interface device notifications */ 1555 static int netconsole_netdev_event(struct notifier_block *this, 1556 unsigned long event, void *ptr) 1557 { 1558 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1559 struct netconsole_target *nt, *tmp; 1560 bool stopped = false; 1561 unsigned long flags; 1562 1563 if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER || 1564 event == NETDEV_RELEASE || event == NETDEV_JOIN || 1565 event == NETDEV_REGISTER)) 1566 goto done; 1567 1568 mutex_lock(&target_cleanup_list_lock); 1569 spin_lock_irqsave(&target_list_lock, flags); 1570 list_for_each_entry_safe(nt, tmp, &target_list, list) { 1571 netconsole_target_get(nt); 1572 if (nt->np.dev == dev) { 1573 switch (event) { 1574 case NETDEV_CHANGENAME: 1575 strscpy(nt->np.dev_name, dev->name, IFNAMSIZ); 1576 break; 1577 case NETDEV_RELEASE: 1578 case NETDEV_JOIN: 1579 /* transition target to DISABLED instead of 1580 * DEACTIVATED when (de)enslaving devices as 1581 * their targets should not be automatically 1582 * resumed when the interface is brought up. 1583 */ 1584 nt->state = STATE_DISABLED; 1585 list_move(&nt->list, &target_cleanup_list); 1586 stopped = true; 1587 break; 1588 case NETDEV_UNREGISTER: 1589 nt->state = STATE_DEACTIVATED; 1590 list_move(&nt->list, &target_cleanup_list); 1591 stopped = true; 1592 } 1593 } 1594 if ((event == NETDEV_REGISTER || event == NETDEV_CHANGENAME) && 1595 deactivated_target_match(nt, dev)) 1596 /* Schedule resume on a workqueue as it will attempt 1597 * to UP the device, which can't be done as part of this 1598 * notifier. 1599 */ 1600 queue_work(netconsole_wq, &nt->resume_wq); 1601 netconsole_target_put(nt); 1602 } 1603 spin_unlock_irqrestore(&target_list_lock, flags); 1604 mutex_unlock(&target_cleanup_list_lock); 1605 1606 if (stopped) { 1607 const char *msg = "had an event"; 1608 1609 switch (event) { 1610 case NETDEV_UNREGISTER: 1611 msg = "unregistered"; 1612 break; 1613 case NETDEV_RELEASE: 1614 msg = "released slaves"; 1615 break; 1616 case NETDEV_JOIN: 1617 msg = "is joining a master device"; 1618 break; 1619 } 1620 pr_info("network logging stopped on interface %s as it %s\n", 1621 dev->name, msg); 1622 } 1623 1624 /* Process target_cleanup_list entries. By the end, target_cleanup_list 1625 * should be empty 1626 */ 1627 netconsole_process_cleanups_core(); 1628 1629 done: 1630 return NOTIFY_DONE; 1631 } 1632 1633 static struct notifier_block netconsole_netdev_notifier = { 1634 .notifier_call = netconsole_netdev_event, 1635 }; 1636 1637 /** 1638 * send_udp - Wrapper for netpoll_send_udp that counts errors 1639 * @nt: target to send message to 1640 * @msg: message to send 1641 * @len: length of message 1642 * 1643 * Calls netpoll_send_udp and classifies the return value. If an error 1644 * occurred it increments statistics in nt->stats accordingly. 1645 * Only calls netpoll_send_udp if CONFIG_NETCONSOLE_DYNAMIC is disabled. 1646 */ 1647 static void send_udp(struct netconsole_target *nt, const char *msg, int len) 1648 { 1649 int result = netpoll_send_udp(&nt->np, msg, len); 1650 1651 if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) { 1652 if (result == NET_XMIT_DROP) { 1653 u64_stats_update_begin(&nt->stats.syncp); 1654 u64_stats_inc(&nt->stats.xmit_drop_count); 1655 u64_stats_update_end(&nt->stats.syncp); 1656 } else if (result == -ENOMEM) { 1657 u64_stats_update_begin(&nt->stats.syncp); 1658 u64_stats_inc(&nt->stats.enomem_count); 1659 u64_stats_update_end(&nt->stats.syncp); 1660 } 1661 } 1662 } 1663 1664 static void send_msg_no_fragmentation(struct netconsole_target *nt, 1665 const char *msg, 1666 int msg_len, 1667 int release_len) 1668 { 1669 const char *userdata = NULL; 1670 const char *sysdata = NULL; 1671 const char *release; 1672 1673 #ifdef CONFIG_NETCONSOLE_DYNAMIC 1674 userdata = nt->userdata; 1675 sysdata = nt->sysdata; 1676 #endif 1677 1678 if (release_len) { 1679 release = init_utsname()->release; 1680 1681 scnprintf(nt->buf, MAX_PRINT_CHUNK, "%s,%s", release, msg); 1682 msg_len += release_len; 1683 } else { 1684 memcpy(nt->buf, msg, msg_len); 1685 } 1686 1687 if (userdata) 1688 msg_len += scnprintf(&nt->buf[msg_len], 1689 MAX_PRINT_CHUNK - msg_len, "%s", 1690 userdata); 1691 1692 if (sysdata) 1693 msg_len += scnprintf(&nt->buf[msg_len], 1694 MAX_PRINT_CHUNK - msg_len, "%s", 1695 sysdata); 1696 1697 send_udp(nt, nt->buf, msg_len); 1698 } 1699 1700 static void append_release(char *buf) 1701 { 1702 const char *release; 1703 1704 release = init_utsname()->release; 1705 scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release); 1706 } 1707 1708 static void send_fragmented_body(struct netconsole_target *nt, 1709 const char *msgbody_ptr, int header_len, 1710 int msgbody_len, int sysdata_len) 1711 { 1712 const char *userdata_ptr = NULL; 1713 const char *sysdata_ptr = NULL; 1714 int data_len, data_sent = 0; 1715 int userdata_offset = 0; 1716 int sysdata_offset = 0; 1717 int msgbody_offset = 0; 1718 int userdata_len = 0; 1719 1720 #ifdef CONFIG_NETCONSOLE_DYNAMIC 1721 userdata_ptr = nt->userdata; 1722 sysdata_ptr = nt->sysdata; 1723 userdata_len = nt->userdata_length; 1724 #endif 1725 if (WARN_ON_ONCE(!userdata_ptr && userdata_len != 0)) 1726 return; 1727 1728 if (WARN_ON_ONCE(!sysdata_ptr && sysdata_len != 0)) 1729 return; 1730 1731 /* data_len represents the number of bytes that will be sent. This is 1732 * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple 1733 * packets 1734 */ 1735 data_len = msgbody_len + userdata_len + sysdata_len; 1736 1737 /* In each iteration of the while loop below, we send a packet 1738 * containing the header and a portion of the data. The data is 1739 * composed of three parts: msgbody, userdata, and sysdata. 1740 * We keep track of how many bytes have been sent from each part using 1741 * the *_offset variables. 1742 * We keep track of how many bytes have been sent overall using the 1743 * data_sent variable, which ranges from 0 to the total bytes to be 1744 * sent. 1745 */ 1746 while (data_sent < data_len) { 1747 int userdata_left = userdata_len - userdata_offset; 1748 int sysdata_left = sysdata_len - sysdata_offset; 1749 int msgbody_left = msgbody_len - msgbody_offset; 1750 int buf_offset = 0; 1751 int this_chunk = 0; 1752 1753 /* header is already populated in nt->buf, just append to it */ 1754 buf_offset = header_len; 1755 1756 buf_offset += scnprintf(nt->buf + buf_offset, 1757 MAX_PRINT_CHUNK - buf_offset, 1758 ",ncfrag=%d/%d;", data_sent, 1759 data_len); 1760 1761 /* append msgbody first */ 1762 this_chunk = min(msgbody_left, MAX_PRINT_CHUNK - buf_offset); 1763 memcpy(nt->buf + buf_offset, msgbody_ptr + msgbody_offset, 1764 this_chunk); 1765 msgbody_offset += this_chunk; 1766 buf_offset += this_chunk; 1767 data_sent += this_chunk; 1768 1769 /* after msgbody, append userdata */ 1770 if (userdata_ptr && userdata_left) { 1771 this_chunk = min(userdata_left, 1772 MAX_PRINT_CHUNK - buf_offset); 1773 memcpy(nt->buf + buf_offset, 1774 userdata_ptr + userdata_offset, this_chunk); 1775 userdata_offset += this_chunk; 1776 buf_offset += this_chunk; 1777 data_sent += this_chunk; 1778 } 1779 1780 /* after userdata, append sysdata */ 1781 if (sysdata_ptr && sysdata_left) { 1782 this_chunk = min(sysdata_left, 1783 MAX_PRINT_CHUNK - buf_offset); 1784 memcpy(nt->buf + buf_offset, 1785 sysdata_ptr + sysdata_offset, this_chunk); 1786 sysdata_offset += this_chunk; 1787 buf_offset += this_chunk; 1788 data_sent += this_chunk; 1789 } 1790 1791 /* if all is good, send the packet out */ 1792 if (WARN_ON_ONCE(data_sent > data_len)) 1793 return; 1794 1795 send_udp(nt, nt->buf, buf_offset); 1796 } 1797 } 1798 1799 static void send_msg_fragmented(struct netconsole_target *nt, 1800 const char *msg, 1801 int msg_len, 1802 int release_len, 1803 int sysdata_len) 1804 { 1805 int header_len, msgbody_len; 1806 const char *msgbody; 1807 1808 /* need to insert extra header fields, detect header and msgbody */ 1809 msgbody = memchr(msg, ';', msg_len); 1810 if (WARN_ON_ONCE(!msgbody)) 1811 return; 1812 1813 header_len = msgbody - msg; 1814 msgbody_len = msg_len - header_len - 1; 1815 msgbody++; 1816 1817 /* 1818 * Transfer multiple chunks with the following extra header. 1819 * "ncfrag=<byte-offset>/<total-bytes>" 1820 */ 1821 if (release_len) 1822 append_release(nt->buf); 1823 1824 /* Copy the header into the buffer */ 1825 memcpy(nt->buf + release_len, msg, header_len); 1826 header_len += release_len; 1827 1828 /* for now on, the header will be persisted, and the msgbody 1829 * will be replaced 1830 */ 1831 send_fragmented_body(nt, msgbody, header_len, msgbody_len, 1832 sysdata_len); 1833 } 1834 1835 /** 1836 * send_ext_msg_udp - send extended log message to target 1837 * @nt: target to send message to 1838 * @wctxt: nbcon write context containing message and metadata 1839 * 1840 * Transfer extended log message to @nt. If message is longer than 1841 * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with 1842 * ncfrag header field added to identify them. 1843 */ 1844 static void send_ext_msg_udp(struct netconsole_target *nt, 1845 struct nbcon_write_context *wctxt) 1846 { 1847 int userdata_len = 0; 1848 int release_len = 0; 1849 int sysdata_len = 0; 1850 int len; 1851 1852 #ifdef CONFIG_NETCONSOLE_DYNAMIC 1853 sysdata_len = prepare_sysdata(nt, wctxt); 1854 userdata_len = nt->userdata_length; 1855 #endif 1856 if (nt->release) 1857 release_len = strlen(init_utsname()->release) + 1; 1858 1859 len = wctxt->len + release_len + sysdata_len + userdata_len; 1860 if (len <= MAX_PRINT_CHUNK) 1861 return send_msg_no_fragmentation(nt, wctxt->outbuf, 1862 wctxt->len, release_len); 1863 1864 return send_msg_fragmented(nt, wctxt->outbuf, wctxt->len, release_len, 1865 sysdata_len); 1866 } 1867 1868 static void send_msg_udp(struct netconsole_target *nt, const char *msg, 1869 unsigned int len) 1870 { 1871 const char *tmp = msg; 1872 int frag, left = len; 1873 1874 while (left > 0) { 1875 frag = min(left, MAX_PRINT_CHUNK); 1876 send_udp(nt, tmp, frag); 1877 tmp += frag; 1878 left -= frag; 1879 } 1880 } 1881 1882 /** 1883 * netconsole_write - Generic function to send a msg to all targets 1884 * @wctxt: nbcon write context 1885 * @extended: "true" for extended console mode 1886 * 1887 * Given an nbcon write context, send the message to the netconsole targets 1888 */ 1889 static void netconsole_write(struct nbcon_write_context *wctxt, bool extended) 1890 { 1891 struct netconsole_target *nt; 1892 1893 if (oops_only && !oops_in_progress) 1894 return; 1895 1896 list_for_each_entry(nt, &target_list, list) { 1897 if (nt->extended != extended || nt->state != STATE_ENABLED || 1898 !netif_running(nt->np.dev)) 1899 continue; 1900 1901 /* If nbcon_enter_unsafe() fails, just return given netconsole 1902 * lost the ownership, and iterating over the targets will not 1903 * be able to re-acquire. 1904 */ 1905 if (!nbcon_enter_unsafe(wctxt)) 1906 return; 1907 1908 if (extended) 1909 send_ext_msg_udp(nt, wctxt); 1910 else 1911 send_msg_udp(nt, wctxt->outbuf, wctxt->len); 1912 1913 nbcon_exit_unsafe(wctxt); 1914 } 1915 } 1916 1917 static void netconsole_write_ext(struct console *con __always_unused, 1918 struct nbcon_write_context *wctxt) 1919 { 1920 netconsole_write(wctxt, true); 1921 } 1922 1923 static void netconsole_write_basic(struct console *con __always_unused, 1924 struct nbcon_write_context *wctxt) 1925 { 1926 netconsole_write(wctxt, false); 1927 } 1928 1929 static void netconsole_device_lock(struct console *con __always_unused, 1930 unsigned long *flags) 1931 __acquires(&target_list_lock) 1932 { 1933 spin_lock_irqsave(&target_list_lock, *flags); 1934 } 1935 1936 static void netconsole_device_unlock(struct console *con __always_unused, 1937 unsigned long flags) 1938 __releases(&target_list_lock) 1939 { 1940 spin_unlock_irqrestore(&target_list_lock, flags); 1941 } 1942 1943 static int netconsole_parser_cmdline(struct netpoll *np, char *opt) 1944 { 1945 bool ipversion_set = false; 1946 char *cur = opt; 1947 char *delim; 1948 int ipv6; 1949 1950 if (*cur != '@') { 1951 delim = strchr(cur, '@'); 1952 if (!delim) 1953 goto parse_failed; 1954 *delim = 0; 1955 if (kstrtou16(cur, 10, &np->local_port)) 1956 goto parse_failed; 1957 cur = delim; 1958 } 1959 cur++; 1960 1961 if (*cur != '/') { 1962 ipversion_set = true; 1963 delim = strchr(cur, '/'); 1964 if (!delim) 1965 goto parse_failed; 1966 *delim = 0; 1967 ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip); 1968 if (ipv6 < 0) 1969 goto parse_failed; 1970 else 1971 np->ipv6 = (bool)ipv6; 1972 cur = delim; 1973 } 1974 cur++; 1975 1976 if (*cur != ',') { 1977 /* parse out dev_name or dev_mac */ 1978 delim = strchr(cur, ','); 1979 if (!delim) 1980 goto parse_failed; 1981 *delim = 0; 1982 1983 np->dev_name[0] = '\0'; 1984 eth_broadcast_addr(np->dev_mac); 1985 if (!strchr(cur, ':')) 1986 strscpy(np->dev_name, cur, sizeof(np->dev_name)); 1987 else if (!mac_pton(cur, np->dev_mac)) 1988 goto parse_failed; 1989 1990 cur = delim; 1991 } 1992 cur++; 1993 1994 if (*cur != '@') { 1995 /* dst port */ 1996 delim = strchr(cur, '@'); 1997 if (!delim) 1998 goto parse_failed; 1999 *delim = 0; 2000 if (*cur == ' ' || *cur == '\t') 2001 np_info(np, "warning: whitespace is not allowed\n"); 2002 if (kstrtou16(cur, 10, &np->remote_port)) 2003 goto parse_failed; 2004 cur = delim; 2005 } 2006 cur++; 2007 2008 /* dst ip */ 2009 delim = strchr(cur, '/'); 2010 if (!delim) 2011 goto parse_failed; 2012 *delim = 0; 2013 ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip); 2014 if (ipv6 < 0) 2015 goto parse_failed; 2016 else if (ipversion_set && np->ipv6 != (bool)ipv6) 2017 goto parse_failed; 2018 else 2019 np->ipv6 = (bool)ipv6; 2020 cur = delim + 1; 2021 2022 if (*cur != 0) { 2023 /* MAC address */ 2024 if (!mac_pton(cur, np->remote_mac)) 2025 goto parse_failed; 2026 } 2027 2028 netconsole_print_banner(np); 2029 2030 return 0; 2031 2032 parse_failed: 2033 np_info(np, "couldn't parse config at '%s'!\n", cur); 2034 return -1; 2035 } 2036 2037 /* Allocate new target (from boot/module param) and setup netpoll for it */ 2038 static struct netconsole_target *alloc_param_target(char *target_config, 2039 int cmdline_count) 2040 { 2041 struct netconsole_target *nt; 2042 int err; 2043 2044 nt = alloc_and_init(); 2045 if (!nt) { 2046 err = -ENOMEM; 2047 goto fail; 2048 } 2049 2050 if (*target_config == '+') { 2051 nt->extended = true; 2052 target_config++; 2053 } 2054 2055 if (*target_config == 'r') { 2056 if (!nt->extended) { 2057 pr_err("Netconsole configuration error. Release feature requires extended log message"); 2058 err = -EINVAL; 2059 goto fail; 2060 } 2061 nt->release = true; 2062 target_config++; 2063 } 2064 2065 /* Parse parameters and setup netpoll */ 2066 err = netconsole_parser_cmdline(&nt->np, target_config); 2067 if (err) 2068 goto fail; 2069 2070 err = netpoll_setup(&nt->np); 2071 if (err) { 2072 pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n", 2073 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count); 2074 if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) 2075 /* only fail if dynamic reconfiguration is set, 2076 * otherwise, keep the target in the list, but disabled. 2077 */ 2078 goto fail; 2079 } else { 2080 nt->state = STATE_ENABLED; 2081 } 2082 populate_configfs_item(nt, cmdline_count); 2083 2084 return nt; 2085 2086 fail: 2087 kfree(nt); 2088 return ERR_PTR(err); 2089 } 2090 2091 /* Cleanup netpoll for given target (from boot/module param) and free it */ 2092 static void free_param_target(struct netconsole_target *nt) 2093 { 2094 cancel_work_sync(&nt->resume_wq); 2095 netpoll_cleanup(&nt->np); 2096 #ifdef CONFIG_NETCONSOLE_DYNAMIC 2097 kfree(nt->userdata); 2098 #endif 2099 kfree(nt); 2100 } 2101 2102 static struct console netconsole_ext = { 2103 .name = "netcon_ext", 2104 .flags = CON_ENABLED | CON_EXTENDED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE, 2105 .write_thread = netconsole_write_ext, 2106 .write_atomic = netconsole_write_ext, 2107 .device_lock = netconsole_device_lock, 2108 .device_unlock = netconsole_device_unlock, 2109 }; 2110 2111 static struct console netconsole = { 2112 .name = "netcon", 2113 .flags = CON_ENABLED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE, 2114 .write_thread = netconsole_write_basic, 2115 .write_atomic = netconsole_write_basic, 2116 .device_lock = netconsole_device_lock, 2117 .device_unlock = netconsole_device_unlock, 2118 }; 2119 2120 static int __init init_netconsole(void) 2121 { 2122 int err; 2123 struct netconsole_target *nt, *tmp; 2124 u32 console_type_needed = 0; 2125 unsigned int count = 0; 2126 unsigned long flags; 2127 char *target_config; 2128 char *input = config; 2129 2130 if (strnlen(input, MAX_PARAM_LENGTH)) { 2131 while ((target_config = strsep(&input, ";"))) { 2132 nt = alloc_param_target(target_config, count); 2133 if (IS_ERR(nt)) { 2134 if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) 2135 continue; 2136 err = PTR_ERR(nt); 2137 goto fail; 2138 } 2139 /* Dump existing printks when we register */ 2140 if (nt->extended) { 2141 console_type_needed |= CONS_EXTENDED; 2142 netconsole_ext.flags |= CON_PRINTBUFFER; 2143 } else { 2144 console_type_needed |= CONS_BASIC; 2145 netconsole.flags |= CON_PRINTBUFFER; 2146 } 2147 2148 spin_lock_irqsave(&target_list_lock, flags); 2149 list_add(&nt->list, &target_list); 2150 spin_unlock_irqrestore(&target_list_lock, flags); 2151 count++; 2152 } 2153 } 2154 2155 netconsole_wq = alloc_workqueue("netconsole", WQ_UNBOUND, 0); 2156 if (!netconsole_wq) { 2157 err = -ENOMEM; 2158 goto fail; 2159 } 2160 2161 err = register_netdevice_notifier(&netconsole_netdev_notifier); 2162 if (err) 2163 goto fail; 2164 2165 err = dynamic_netconsole_init(); 2166 if (err) 2167 goto undonotifier; 2168 2169 if (console_type_needed & CONS_EXTENDED) 2170 register_console(&netconsole_ext); 2171 if (console_type_needed & CONS_BASIC) 2172 register_console(&netconsole); 2173 pr_info("network logging started\n"); 2174 2175 return err; 2176 2177 undonotifier: 2178 unregister_netdevice_notifier(&netconsole_netdev_notifier); 2179 2180 fail: 2181 pr_err("cleaning up\n"); 2182 2183 if (netconsole_wq) 2184 flush_workqueue(netconsole_wq); 2185 /* 2186 * Remove all targets and destroy them (only targets created 2187 * from the boot/module option exist here). Skipping the list 2188 * lock is safe here, and netpoll_cleanup() will sleep. 2189 */ 2190 list_for_each_entry_safe(nt, tmp, &target_list, list) { 2191 list_del(&nt->list); 2192 free_param_target(nt); 2193 } 2194 2195 if (netconsole_wq) 2196 destroy_workqueue(netconsole_wq); 2197 2198 return err; 2199 } 2200 2201 static void __exit cleanup_netconsole(void) 2202 { 2203 struct netconsole_target *nt, *tmp; 2204 2205 if (console_is_registered(&netconsole_ext)) 2206 unregister_console(&netconsole_ext); 2207 if (console_is_registered(&netconsole)) 2208 unregister_console(&netconsole); 2209 dynamic_netconsole_exit(); 2210 unregister_netdevice_notifier(&netconsole_netdev_notifier); 2211 flush_workqueue(netconsole_wq); 2212 2213 /* 2214 * Targets created via configfs pin references on our module 2215 * and would first be rmdir(2)'ed from userspace. We reach 2216 * here only when they are already destroyed, and only those 2217 * created from the boot/module option are left, so remove and 2218 * destroy them. Skipping the list lock is safe here, and 2219 * netpoll_cleanup() will sleep. 2220 */ 2221 list_for_each_entry_safe(nt, tmp, &target_list, list) { 2222 list_del(&nt->list); 2223 free_param_target(nt); 2224 } 2225 2226 destroy_workqueue(netconsole_wq); 2227 } 2228 2229 /* 2230 * Use late_initcall to ensure netconsole is 2231 * initialized after network device driver if built-in. 2232 * 2233 * late_initcall() and module_init() are identical if built as module. 2234 */ 2235 late_initcall(init_netconsole); 2236 module_exit(cleanup_netconsole); 2237