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