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