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