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