xref: /linux/drivers/net/netconsole.c (revision c2c2ccfd4ba72718266a56f3ecc34c989cb5b7a0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  linux/drivers/net/netconsole.c
4  *
5  *  Copyright (C) 2001  Ingo Molnar <mingo@redhat.com>
6  *
7  *  This file contains the implementation of an IRQ-safe, crash-safe
8  *  kernel console implementation that outputs kernel messages to the
9  *  network.
10  *
11  * Modification history:
12  *
13  * 2001-09-17    started by Ingo Molnar.
14  * 2003-08-11    2.6 port by Matt Mackall
15  *               simplified options
16  *               generic card hooks
17  *               works non-modular
18  * 2003-09-07    rewritten with netpoll api
19  */
20 
21 /****************************************************************
22  *
23  ****************************************************************/
24 
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 
27 #include <linux/mm.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/console.h>
32 #include <linux/moduleparam.h>
33 #include <linux/kernel.h>
34 #include <linux/string.h>
35 #include <linux/netpoll.h>
36 #include <linux/inet.h>
37 #include <linux/configfs.h>
38 #include <linux/etherdevice.h>
39 #include <linux/u64_stats_sync.h>
40 #include <linux/utsname.h>
41 #include <linux/rtnetlink.h>
42 
43 MODULE_AUTHOR("Matt Mackall <mpm@selenic.com>");
44 MODULE_DESCRIPTION("Console driver for network interfaces");
45 MODULE_LICENSE("GPL");
46 
47 #define MAX_PARAM_LENGTH		256
48 #define MAX_EXTRADATA_ENTRY_LEN		256
49 #define MAX_EXTRADATA_VALUE_LEN	200
50 /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
51 #define MAX_EXTRADATA_NAME_LEN		(MAX_EXTRADATA_ENTRY_LEN - \
52 					MAX_EXTRADATA_VALUE_LEN - 3)
53 #define MAX_EXTRADATA_ITEMS		16
54 #define MAX_PRINT_CHUNK			1000
55 
56 static char config[MAX_PARAM_LENGTH];
57 module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
58 MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
59 
60 static bool oops_only;
61 module_param(oops_only, bool, 0600);
62 MODULE_PARM_DESC(oops_only, "Only log oops messages");
63 
64 #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
65 
66 #ifndef	MODULE
option_setup(char * opt)67 static int __init option_setup(char *opt)
68 {
69 	strscpy(config, opt, MAX_PARAM_LENGTH);
70 	return 1;
71 }
72 __setup("netconsole=", option_setup);
73 #endif	/* MODULE */
74 
75 /* Linked list of all configured targets */
76 static LIST_HEAD(target_list);
77 /* target_cleanup_list is used to track targets that need to be cleaned outside
78  * of target_list_lock. It should be cleaned in the same function it is
79  * populated.
80  */
81 static LIST_HEAD(target_cleanup_list);
82 
83 /* This needs to be a spinlock because write_msg() cannot sleep */
84 static DEFINE_SPINLOCK(target_list_lock);
85 /* This needs to be a mutex because netpoll_cleanup might sleep */
86 static DEFINE_MUTEX(target_cleanup_list_lock);
87 
88 /*
89  * Console driver for netconsoles.  Register only consoles that have
90  * an associated target of the same type.
91  */
92 static struct console netconsole_ext, netconsole;
93 
94 struct netconsole_target_stats  {
95 	u64_stats_t xmit_drop_count;
96 	u64_stats_t enomem_count;
97 	struct u64_stats_sync syncp;
98 };
99 
100 enum console_type {
101 	CONS_BASIC = BIT(0),
102 	CONS_EXTENDED = BIT(1),
103 };
104 
105 /* Features enabled in sysdata. Contrary to userdata, this data is populated by
106  * the kernel. The fields are designed as bitwise flags, allowing multiple
107  * features to be set in sysdata_fields.
108  */
109 enum sysdata_feature {
110 	/* Populate the CPU that sends the message */
111 	SYSDATA_CPU_NR = BIT(0),
112 	/* Populate the task name (as in current->comm) in sysdata */
113 	SYSDATA_TASKNAME = BIT(1),
114 	/* Kernel release/version as part of sysdata */
115 	SYSDATA_RELEASE = BIT(2),
116 	/* Include a per-target message ID as part of sysdata */
117 	SYSDATA_MSGID = BIT(3),
118 };
119 
120 /**
121  * struct netconsole_target - Represents a configured netconsole target.
122  * @list:	Links this target into the target_list.
123  * @group:	Links us into the configfs subsystem hierarchy.
124  * @userdata_group:	Links to the userdata configfs hierarchy
125  * @extradata_complete:	Cached, formatted string of append
126  * @userdata_length:	String length of usedata in extradata_complete.
127  * @sysdata_fields:	Sysdata features enabled.
128  * @msgcounter:	Message sent counter.
129  * @stats:	Packet send stats for the target. Used for debugging.
130  * @enabled:	On / off knob to enable / disable target.
131  *		Visible from userspace (read-write).
132  *		We maintain a strict 1:1 correspondence between this and
133  *		whether the corresponding netpoll is active or inactive.
134  *		Also, other parameters of a target may be modified at
135  *		runtime only when it is disabled (enabled == 0).
136  * @extended:	Denotes whether console is extended or not.
137  * @release:	Denotes whether kernel release version should be prepended
138  *		to the message. Depends on extended console.
139  * @np:		The netpoll structure for this target.
140  *		Contains the other userspace visible parameters:
141  *		dev_name	(read-write)
142  *		local_port	(read-write)
143  *		remote_port	(read-write)
144  *		local_ip	(read-write)
145  *		remote_ip	(read-write)
146  *		local_mac	(read-only)
147  *		remote_mac	(read-write)
148  * @buf:	The buffer used to send the full msg to the network stack
149  */
150 struct netconsole_target {
151 	struct list_head	list;
152 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
153 	struct config_group	group;
154 	struct config_group	userdata_group;
155 	char extradata_complete[MAX_EXTRADATA_ENTRY_LEN * MAX_EXTRADATA_ITEMS];
156 	size_t			userdata_length;
157 	/* bit-wise with sysdata_feature bits */
158 	u32			sysdata_fields;
159 	/* protected by target_list_lock */
160 	u32			msgcounter;
161 #endif
162 	struct netconsole_target_stats stats;
163 	bool			enabled;
164 	bool			extended;
165 	bool			release;
166 	struct netpoll		np;
167 	/* protected by target_list_lock */
168 	char			buf[MAX_PRINT_CHUNK];
169 };
170 
171 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
172 
173 static struct configfs_subsystem netconsole_subsys;
174 static DEFINE_MUTEX(dynamic_netconsole_mutex);
175 
dynamic_netconsole_init(void)176 static int __init dynamic_netconsole_init(void)
177 {
178 	config_group_init(&netconsole_subsys.su_group);
179 	mutex_init(&netconsole_subsys.su_mutex);
180 	return configfs_register_subsystem(&netconsole_subsys);
181 }
182 
dynamic_netconsole_exit(void)183 static void __exit dynamic_netconsole_exit(void)
184 {
185 	configfs_unregister_subsystem(&netconsole_subsys);
186 }
187 
188 /*
189  * Targets that were created by parsing the boot/module option string
190  * do not exist in the configfs hierarchy (and have NULL names) and will
191  * never go away, so make these a no-op for them.
192  */
netconsole_target_get(struct netconsole_target * nt)193 static void netconsole_target_get(struct netconsole_target *nt)
194 {
195 	if (config_item_name(&nt->group.cg_item))
196 		config_group_get(&nt->group);
197 }
198 
netconsole_target_put(struct netconsole_target * nt)199 static void netconsole_target_put(struct netconsole_target *nt)
200 {
201 	if (config_item_name(&nt->group.cg_item))
202 		config_group_put(&nt->group);
203 }
204 
205 #else	/* !CONFIG_NETCONSOLE_DYNAMIC */
206 
dynamic_netconsole_init(void)207 static int __init dynamic_netconsole_init(void)
208 {
209 	return 0;
210 }
211 
dynamic_netconsole_exit(void)212 static void __exit dynamic_netconsole_exit(void)
213 {
214 }
215 
216 /*
217  * No danger of targets going away from under us when dynamic
218  * reconfigurability is off.
219  */
netconsole_target_get(struct netconsole_target * nt)220 static void netconsole_target_get(struct netconsole_target *nt)
221 {
222 }
223 
netconsole_target_put(struct netconsole_target * nt)224 static void netconsole_target_put(struct netconsole_target *nt)
225 {
226 }
227 
populate_configfs_item(struct netconsole_target * nt,int cmdline_count)228 static void populate_configfs_item(struct netconsole_target *nt,
229 				   int cmdline_count)
230 {
231 }
232 #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
233 
234 /* Allocate and initialize with defaults.
235  * Note that these targets get their config_item fields zeroed-out.
236  */
alloc_and_init(void)237 static struct netconsole_target *alloc_and_init(void)
238 {
239 	struct netconsole_target *nt;
240 
241 	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
242 	if (!nt)
243 		return nt;
244 
245 	if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
246 		nt->extended = true;
247 	if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
248 		nt->release = true;
249 
250 	nt->np.name = "netconsole";
251 	strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
252 	nt->np.local_port = 6665;
253 	nt->np.remote_port = 6666;
254 	eth_broadcast_addr(nt->np.remote_mac);
255 
256 	return nt;
257 }
258 
259 /* Clean up every target in the cleanup_list and move the clean targets back to
260  * the main target_list.
261  */
netconsole_process_cleanups_core(void)262 static void netconsole_process_cleanups_core(void)
263 {
264 	struct netconsole_target *nt, *tmp;
265 	unsigned long flags;
266 
267 	/* The cleanup needs RTNL locked */
268 	ASSERT_RTNL();
269 
270 	mutex_lock(&target_cleanup_list_lock);
271 	list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) {
272 		/* all entries in the cleanup_list needs to be disabled */
273 		WARN_ON_ONCE(nt->enabled);
274 		do_netpoll_cleanup(&nt->np);
275 		/* moved the cleaned target to target_list. Need to hold both
276 		 * locks
277 		 */
278 		spin_lock_irqsave(&target_list_lock, flags);
279 		list_move(&nt->list, &target_list);
280 		spin_unlock_irqrestore(&target_list_lock, flags);
281 	}
282 	WARN_ON_ONCE(!list_empty(&target_cleanup_list));
283 	mutex_unlock(&target_cleanup_list_lock);
284 }
285 
netconsole_print_banner(struct netpoll * np)286 static void netconsole_print_banner(struct netpoll *np)
287 {
288 	np_info(np, "local port %d\n", np->local_port);
289 	if (np->ipv6)
290 		np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
291 	else
292 		np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
293 	np_info(np, "interface name '%s'\n", np->dev_name);
294 	np_info(np, "local ethernet address '%pM'\n", np->dev_mac);
295 	np_info(np, "remote port %d\n", np->remote_port);
296 	if (np->ipv6)
297 		np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
298 	else
299 		np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
300 	np_info(np, "remote ethernet address %pM\n", np->remote_mac);
301 }
302 
303 /* Parse the string and populate the `inet_addr` union. Return 0 if IPv4 is
304  * populated, 1 if IPv6 is populated, and -1 upon failure.
305  */
netpoll_parse_ip_addr(const char * str,union inet_addr * addr)306 static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
307 {
308 	const char *end = NULL;
309 	int len;
310 
311 	len = strlen(str);
312 	if (!len)
313 		return -1;
314 
315 	if (str[len - 1] == '\n')
316 		len -= 1;
317 
318 	if (in4_pton(str, len, (void *)addr, -1, &end) > 0 &&
319 	    (!end || *end == 0 || *end == '\n'))
320 		return 0;
321 
322 	if (IS_ENABLED(CONFIG_IPV6) &&
323 	    in6_pton(str, len, (void *)addr, -1, &end) > 0 &&
324 	    (!end || *end == 0 || *end == '\n'))
325 		return 1;
326 
327 	return -1;
328 }
329 
330 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
331 
332 /*
333  * Our subsystem hierarchy is:
334  *
335  * /sys/kernel/config/netconsole/
336  *				|
337  *				<target>/
338  *				|	enabled
339  *				|	release
340  *				|	dev_name
341  *				|	local_port
342  *				|	remote_port
343  *				|	local_ip
344  *				|	remote_ip
345  *				|	local_mac
346  *				|	remote_mac
347  *				|	transmit_errors
348  *				|	userdata/
349  *				|		<key>/
350  *				|			value
351  *				|		...
352  *				|
353  *				<target>/...
354  */
355 
to_target(struct config_item * item)356 static struct netconsole_target *to_target(struct config_item *item)
357 {
358 	struct config_group *cfg_group;
359 
360 	cfg_group = to_config_group(item);
361 	if (!cfg_group)
362 		return NULL;
363 	return container_of(to_config_group(item),
364 			    struct netconsole_target, group);
365 }
366 
367 /* Do the list cleanup with the rtnl lock hold.  rtnl lock is necessary because
368  * netdev might be cleaned-up by calling __netpoll_cleanup(),
369  */
netconsole_process_cleanups(void)370 static void netconsole_process_cleanups(void)
371 {
372 	/* rtnl lock is called here, because it has precedence over
373 	 * target_cleanup_list_lock mutex and target_cleanup_list
374 	 */
375 	rtnl_lock();
376 	netconsole_process_cleanups_core();
377 	rtnl_unlock();
378 }
379 
380 /* Get rid of possible trailing newline, returning the new length */
trim_newline(char * s,size_t maxlen)381 static void trim_newline(char *s, size_t maxlen)
382 {
383 	size_t len;
384 
385 	len = strnlen(s, maxlen);
386 	if (s[len - 1] == '\n')
387 		s[len - 1] = '\0';
388 }
389 
390 /*
391  * Attribute operations for netconsole_target.
392  */
393 
enabled_show(struct config_item * item,char * buf)394 static ssize_t enabled_show(struct config_item *item, char *buf)
395 {
396 	return sysfs_emit(buf, "%d\n", to_target(item)->enabled);
397 }
398 
extended_show(struct config_item * item,char * buf)399 static ssize_t extended_show(struct config_item *item, char *buf)
400 {
401 	return sysfs_emit(buf, "%d\n", to_target(item)->extended);
402 }
403 
release_show(struct config_item * item,char * buf)404 static ssize_t release_show(struct config_item *item, char *buf)
405 {
406 	return sysfs_emit(buf, "%d\n", to_target(item)->release);
407 }
408 
dev_name_show(struct config_item * item,char * buf)409 static ssize_t dev_name_show(struct config_item *item, char *buf)
410 {
411 	return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
412 }
413 
local_port_show(struct config_item * item,char * buf)414 static ssize_t local_port_show(struct config_item *item, char *buf)
415 {
416 	return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);
417 }
418 
remote_port_show(struct config_item * item,char * buf)419 static ssize_t remote_port_show(struct config_item *item, char *buf)
420 {
421 	return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);
422 }
423 
local_ip_show(struct config_item * item,char * buf)424 static ssize_t local_ip_show(struct config_item *item, char *buf)
425 {
426 	struct netconsole_target *nt = to_target(item);
427 
428 	if (nt->np.ipv6)
429 		return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);
430 	else
431 		return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);
432 }
433 
remote_ip_show(struct config_item * item,char * buf)434 static ssize_t remote_ip_show(struct config_item *item, char *buf)
435 {
436 	struct netconsole_target *nt = to_target(item);
437 
438 	if (nt->np.ipv6)
439 		return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);
440 	else
441 		return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);
442 }
443 
local_mac_show(struct config_item * item,char * buf)444 static ssize_t local_mac_show(struct config_item *item, char *buf)
445 {
446 	struct net_device *dev = to_target(item)->np.dev;
447 	static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
448 
449 	return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
450 }
451 
remote_mac_show(struct config_item * item,char * buf)452 static ssize_t remote_mac_show(struct config_item *item, char *buf)
453 {
454 	return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);
455 }
456 
transmit_errors_show(struct config_item * item,char * buf)457 static ssize_t transmit_errors_show(struct config_item *item, char *buf)
458 {
459 	struct netconsole_target *nt = to_target(item);
460 	u64 xmit_drop_count, enomem_count;
461 	unsigned int start;
462 
463 	do {
464 		start = u64_stats_fetch_begin(&nt->stats.syncp);
465 		xmit_drop_count = u64_stats_read(&nt->stats.xmit_drop_count);
466 		enomem_count = u64_stats_read(&nt->stats.enomem_count);
467 	} while (u64_stats_fetch_retry(&nt->stats.syncp, start));
468 
469 	return sysfs_emit(buf, "%llu\n", xmit_drop_count + enomem_count);
470 }
471 
472 /* configfs helper to display if cpu_nr sysdata feature is enabled */
sysdata_cpu_nr_enabled_show(struct config_item * item,char * buf)473 static ssize_t sysdata_cpu_nr_enabled_show(struct config_item *item, char *buf)
474 {
475 	struct netconsole_target *nt = to_target(item->ci_parent);
476 	bool cpu_nr_enabled;
477 
478 	mutex_lock(&dynamic_netconsole_mutex);
479 	cpu_nr_enabled = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
480 	mutex_unlock(&dynamic_netconsole_mutex);
481 
482 	return sysfs_emit(buf, "%d\n", cpu_nr_enabled);
483 }
484 
485 /* configfs helper to display if taskname sysdata feature is enabled */
sysdata_taskname_enabled_show(struct config_item * item,char * buf)486 static ssize_t sysdata_taskname_enabled_show(struct config_item *item,
487 					     char *buf)
488 {
489 	struct netconsole_target *nt = to_target(item->ci_parent);
490 	bool taskname_enabled;
491 
492 	mutex_lock(&dynamic_netconsole_mutex);
493 	taskname_enabled = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
494 	mutex_unlock(&dynamic_netconsole_mutex);
495 
496 	return sysfs_emit(buf, "%d\n", taskname_enabled);
497 }
498 
sysdata_release_enabled_show(struct config_item * item,char * buf)499 static ssize_t sysdata_release_enabled_show(struct config_item *item,
500 					    char *buf)
501 {
502 	struct netconsole_target *nt = to_target(item->ci_parent);
503 	bool release_enabled;
504 
505 	mutex_lock(&dynamic_netconsole_mutex);
506 	release_enabled = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
507 	mutex_unlock(&dynamic_netconsole_mutex);
508 
509 	return sysfs_emit(buf, "%d\n", release_enabled);
510 }
511 
512 /* Iterate in the list of target, and make sure we don't have any console
513  * register without targets of the same type
514  */
unregister_netcons_consoles(void)515 static void unregister_netcons_consoles(void)
516 {
517 	struct netconsole_target *nt;
518 	u32 console_type_needed = 0;
519 	unsigned long flags;
520 
521 	spin_lock_irqsave(&target_list_lock, flags);
522 	list_for_each_entry(nt, &target_list, list) {
523 		if (nt->extended)
524 			console_type_needed |= CONS_EXTENDED;
525 		else
526 			console_type_needed |= CONS_BASIC;
527 	}
528 	spin_unlock_irqrestore(&target_list_lock, flags);
529 
530 	if (!(console_type_needed & CONS_EXTENDED) &&
531 	    console_is_registered(&netconsole_ext))
532 		unregister_console(&netconsole_ext);
533 
534 	if (!(console_type_needed & CONS_BASIC) &&
535 	    console_is_registered(&netconsole))
536 		unregister_console(&netconsole);
537 }
538 
sysdata_msgid_enabled_show(struct config_item * item,char * buf)539 static ssize_t sysdata_msgid_enabled_show(struct config_item *item,
540 					  char *buf)
541 {
542 	struct netconsole_target *nt = to_target(item->ci_parent);
543 	bool msgid_enabled;
544 
545 	mutex_lock(&dynamic_netconsole_mutex);
546 	msgid_enabled = !!(nt->sysdata_fields & SYSDATA_MSGID);
547 	mutex_unlock(&dynamic_netconsole_mutex);
548 
549 	return sysfs_emit(buf, "%d\n", msgid_enabled);
550 }
551 
552 /*
553  * This one is special -- targets created through the configfs interface
554  * are not enabled (and the corresponding netpoll activated) by default.
555  * The user is expected to set the desired parameters first (which
556  * would enable him to dynamically add new netpoll targets for new
557  * network interfaces as and when they come up).
558  */
enabled_store(struct config_item * item,const char * buf,size_t count)559 static ssize_t enabled_store(struct config_item *item,
560 		const char *buf, size_t count)
561 {
562 	struct netconsole_target *nt = to_target(item);
563 	unsigned long flags;
564 	bool enabled;
565 	ssize_t ret;
566 
567 	mutex_lock(&dynamic_netconsole_mutex);
568 	ret = kstrtobool(buf, &enabled);
569 	if (ret)
570 		goto out_unlock;
571 
572 	ret = -EINVAL;
573 	if (enabled == nt->enabled) {
574 		pr_info("network logging has already %s\n",
575 			nt->enabled ? "started" : "stopped");
576 		goto out_unlock;
577 	}
578 
579 	if (enabled) {	/* true */
580 		if (nt->release && !nt->extended) {
581 			pr_err("Not enabling netconsole. Release feature requires extended log message");
582 			goto out_unlock;
583 		}
584 
585 		if (nt->extended && !console_is_registered(&netconsole_ext)) {
586 			netconsole_ext.flags |= CON_ENABLED;
587 			register_console(&netconsole_ext);
588 		}
589 
590 		/* User might be enabling the basic format target for the very
591 		 * first time, make sure the console is registered.
592 		 */
593 		if (!nt->extended && !console_is_registered(&netconsole)) {
594 			netconsole.flags |= CON_ENABLED;
595 			register_console(&netconsole);
596 		}
597 
598 		/*
599 		 * Skip netconsole_parser_cmdline() -- all the attributes are
600 		 * already configured via configfs. Just print them out.
601 		 */
602 		netconsole_print_banner(&nt->np);
603 
604 		ret = netpoll_setup(&nt->np);
605 		if (ret)
606 			goto out_unlock;
607 
608 		nt->enabled = true;
609 		pr_info("network logging started\n");
610 	} else {	/* false */
611 		/* We need to disable the netconsole before cleaning it up
612 		 * otherwise we might end up in write_msg() with
613 		 * nt->np.dev == NULL and nt->enabled == true
614 		 */
615 		mutex_lock(&target_cleanup_list_lock);
616 		spin_lock_irqsave(&target_list_lock, flags);
617 		nt->enabled = false;
618 		/* Remove the target from the list, while holding
619 		 * target_list_lock
620 		 */
621 		list_move(&nt->list, &target_cleanup_list);
622 		spin_unlock_irqrestore(&target_list_lock, flags);
623 		mutex_unlock(&target_cleanup_list_lock);
624 		/* Unregister consoles, whose the last target of that type got
625 		 * disabled.
626 		 */
627 		unregister_netcons_consoles();
628 	}
629 
630 	ret = strnlen(buf, count);
631 	/* Deferred cleanup */
632 	netconsole_process_cleanups();
633 out_unlock:
634 	mutex_unlock(&dynamic_netconsole_mutex);
635 	return ret;
636 }
637 
release_store(struct config_item * item,const char * buf,size_t count)638 static ssize_t release_store(struct config_item *item, const char *buf,
639 			     size_t count)
640 {
641 	struct netconsole_target *nt = to_target(item);
642 	bool release;
643 	ssize_t ret;
644 
645 	mutex_lock(&dynamic_netconsole_mutex);
646 	if (nt->enabled) {
647 		pr_err("target (%s) is enabled, disable to update parameters\n",
648 		       config_item_name(&nt->group.cg_item));
649 		ret = -EINVAL;
650 		goto out_unlock;
651 	}
652 
653 	ret = kstrtobool(buf, &release);
654 	if (ret)
655 		goto out_unlock;
656 
657 	nt->release = release;
658 
659 	ret = strnlen(buf, count);
660 out_unlock:
661 	mutex_unlock(&dynamic_netconsole_mutex);
662 	return ret;
663 }
664 
extended_store(struct config_item * item,const char * buf,size_t count)665 static ssize_t extended_store(struct config_item *item, const char *buf,
666 		size_t count)
667 {
668 	struct netconsole_target *nt = to_target(item);
669 	bool extended;
670 	ssize_t ret;
671 
672 	mutex_lock(&dynamic_netconsole_mutex);
673 	if (nt->enabled) {
674 		pr_err("target (%s) is enabled, disable to update parameters\n",
675 		       config_item_name(&nt->group.cg_item));
676 		ret = -EINVAL;
677 		goto out_unlock;
678 	}
679 
680 	ret = kstrtobool(buf, &extended);
681 	if (ret)
682 		goto out_unlock;
683 
684 	nt->extended = extended;
685 	ret = strnlen(buf, count);
686 out_unlock:
687 	mutex_unlock(&dynamic_netconsole_mutex);
688 	return ret;
689 }
690 
dev_name_store(struct config_item * item,const char * buf,size_t count)691 static ssize_t dev_name_store(struct config_item *item, const char *buf,
692 		size_t count)
693 {
694 	struct netconsole_target *nt = to_target(item);
695 
696 	mutex_lock(&dynamic_netconsole_mutex);
697 	if (nt->enabled) {
698 		pr_err("target (%s) is enabled, disable to update parameters\n",
699 		       config_item_name(&nt->group.cg_item));
700 		mutex_unlock(&dynamic_netconsole_mutex);
701 		return -EINVAL;
702 	}
703 
704 	strscpy(nt->np.dev_name, buf, IFNAMSIZ);
705 	trim_newline(nt->np.dev_name, IFNAMSIZ);
706 
707 	mutex_unlock(&dynamic_netconsole_mutex);
708 	return strnlen(buf, count);
709 }
710 
local_port_store(struct config_item * item,const char * buf,size_t count)711 static ssize_t local_port_store(struct config_item *item, const char *buf,
712 		size_t count)
713 {
714 	struct netconsole_target *nt = to_target(item);
715 	ssize_t ret = -EINVAL;
716 
717 	mutex_lock(&dynamic_netconsole_mutex);
718 	if (nt->enabled) {
719 		pr_err("target (%s) is enabled, disable to update parameters\n",
720 		       config_item_name(&nt->group.cg_item));
721 		goto out_unlock;
722 	}
723 
724 	ret = kstrtou16(buf, 10, &nt->np.local_port);
725 	if (ret < 0)
726 		goto out_unlock;
727 	ret = strnlen(buf, count);
728 out_unlock:
729 	mutex_unlock(&dynamic_netconsole_mutex);
730 	return ret;
731 }
732 
remote_port_store(struct config_item * item,const char * buf,size_t count)733 static ssize_t remote_port_store(struct config_item *item,
734 		const char *buf, size_t count)
735 {
736 	struct netconsole_target *nt = to_target(item);
737 	ssize_t ret = -EINVAL;
738 
739 	mutex_lock(&dynamic_netconsole_mutex);
740 	if (nt->enabled) {
741 		pr_err("target (%s) is enabled, disable to update parameters\n",
742 		       config_item_name(&nt->group.cg_item));
743 		goto out_unlock;
744 	}
745 
746 	ret = kstrtou16(buf, 10, &nt->np.remote_port);
747 	if (ret < 0)
748 		goto out_unlock;
749 	ret = strnlen(buf, count);
750 out_unlock:
751 	mutex_unlock(&dynamic_netconsole_mutex);
752 	return ret;
753 }
754 
local_ip_store(struct config_item * item,const char * buf,size_t count)755 static ssize_t local_ip_store(struct config_item *item, const char *buf,
756 		size_t count)
757 {
758 	struct netconsole_target *nt = to_target(item);
759 	ssize_t ret = -EINVAL;
760 	int ipv6;
761 
762 	mutex_lock(&dynamic_netconsole_mutex);
763 	if (nt->enabled) {
764 		pr_err("target (%s) is enabled, disable to update parameters\n",
765 		       config_item_name(&nt->group.cg_item));
766 		goto out_unlock;
767 	}
768 
769 	ipv6 = netpoll_parse_ip_addr(buf, &nt->np.local_ip);
770 	if (ipv6 == -1)
771 		goto out_unlock;
772 	nt->np.ipv6 = !!ipv6;
773 
774 	ret = strnlen(buf, count);
775 out_unlock:
776 	mutex_unlock(&dynamic_netconsole_mutex);
777 	return ret;
778 }
779 
remote_ip_store(struct config_item * item,const char * buf,size_t count)780 static ssize_t remote_ip_store(struct config_item *item, const char *buf,
781 	       size_t count)
782 {
783 	struct netconsole_target *nt = to_target(item);
784 	ssize_t ret = -EINVAL;
785 	int ipv6;
786 
787 	mutex_lock(&dynamic_netconsole_mutex);
788 	if (nt->enabled) {
789 		pr_err("target (%s) is enabled, disable to update parameters\n",
790 		       config_item_name(&nt->group.cg_item));
791 		goto out_unlock;
792 	}
793 
794 	ipv6 = netpoll_parse_ip_addr(buf, &nt->np.remote_ip);
795 	if (ipv6 == -1)
796 		goto out_unlock;
797 	nt->np.ipv6 = !!ipv6;
798 
799 	ret = strnlen(buf, count);
800 out_unlock:
801 	mutex_unlock(&dynamic_netconsole_mutex);
802 	return ret;
803 }
804 
805 /* Count number of entries we have in extradata.
806  * This is important because the extradata_complete only supports
807  * MAX_EXTRADATA_ITEMS entries. Before enabling any new {user,sys}data
808  * feature, number of entries needs to checked for available space.
809  */
count_extradata_entries(struct netconsole_target * nt)810 static size_t count_extradata_entries(struct netconsole_target *nt)
811 {
812 	size_t entries;
813 
814 	/* Userdata entries */
815 	entries = list_count_nodes(&nt->userdata_group.cg_children);
816 	/* Plus sysdata entries */
817 	if (nt->sysdata_fields & SYSDATA_CPU_NR)
818 		entries += 1;
819 	if (nt->sysdata_fields & SYSDATA_TASKNAME)
820 		entries += 1;
821 	if (nt->sysdata_fields & SYSDATA_RELEASE)
822 		entries += 1;
823 	if (nt->sysdata_fields & SYSDATA_MSGID)
824 		entries += 1;
825 
826 	return entries;
827 }
828 
remote_mac_store(struct config_item * item,const char * buf,size_t count)829 static ssize_t remote_mac_store(struct config_item *item, const char *buf,
830 		size_t count)
831 {
832 	struct netconsole_target *nt = to_target(item);
833 	u8 remote_mac[ETH_ALEN];
834 	ssize_t ret = -EINVAL;
835 
836 	mutex_lock(&dynamic_netconsole_mutex);
837 	if (nt->enabled) {
838 		pr_err("target (%s) is enabled, disable to update parameters\n",
839 		       config_item_name(&nt->group.cg_item));
840 		goto out_unlock;
841 	}
842 
843 	if (!mac_pton(buf, remote_mac))
844 		goto out_unlock;
845 	if (buf[MAC_ADDR_STR_LEN] && buf[MAC_ADDR_STR_LEN] != '\n')
846 		goto out_unlock;
847 	memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
848 
849 	ret = strnlen(buf, count);
850 out_unlock:
851 	mutex_unlock(&dynamic_netconsole_mutex);
852 	return ret;
853 }
854 
855 struct userdatum {
856 	struct config_item item;
857 	char value[MAX_EXTRADATA_VALUE_LEN];
858 };
859 
to_userdatum(struct config_item * item)860 static struct userdatum *to_userdatum(struct config_item *item)
861 {
862 	return container_of(item, struct userdatum, item);
863 }
864 
865 struct userdata {
866 	struct config_group group;
867 };
868 
to_userdata(struct config_item * item)869 static struct userdata *to_userdata(struct config_item *item)
870 {
871 	return container_of(to_config_group(item), struct userdata, group);
872 }
873 
userdata_to_target(struct userdata * ud)874 static struct netconsole_target *userdata_to_target(struct userdata *ud)
875 {
876 	struct config_group *netconsole_group;
877 
878 	netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
879 	return to_target(&netconsole_group->cg_item);
880 }
881 
userdatum_value_show(struct config_item * item,char * buf)882 static ssize_t userdatum_value_show(struct config_item *item, char *buf)
883 {
884 	return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
885 }
886 
update_userdata(struct netconsole_target * nt)887 static void update_userdata(struct netconsole_target *nt)
888 {
889 	struct list_head *entry;
890 	int child_count = 0;
891 	unsigned long flags;
892 
893 	spin_lock_irqsave(&target_list_lock, flags);
894 
895 	/* Clear the current string in case the last userdatum was deleted */
896 	nt->userdata_length = 0;
897 	nt->extradata_complete[0] = 0;
898 
899 	list_for_each(entry, &nt->userdata_group.cg_children) {
900 		struct userdatum *udm_item;
901 		struct config_item *item;
902 
903 		if (child_count >= MAX_EXTRADATA_ITEMS) {
904 			spin_unlock_irqrestore(&target_list_lock, flags);
905 			WARN_ON_ONCE(1);
906 			return;
907 		}
908 		child_count++;
909 
910 		item = container_of(entry, struct config_item, ci_entry);
911 		udm_item = to_userdatum(item);
912 
913 		/* Skip userdata with no value set */
914 		if (strnlen(udm_item->value, MAX_EXTRADATA_VALUE_LEN) == 0)
915 			continue;
916 
917 		/* This doesn't overflow extradata_complete since it will write
918 		 * one entry length (1/MAX_EXTRADATA_ITEMS long), entry count is
919 		 * checked to not exceed MAX items with child_count above
920 		 */
921 		nt->userdata_length += scnprintf(&nt->extradata_complete[nt->userdata_length],
922 						 MAX_EXTRADATA_ENTRY_LEN, " %s=%s\n",
923 						 item->ci_name, udm_item->value);
924 	}
925 	spin_unlock_irqrestore(&target_list_lock, flags);
926 }
927 
userdatum_value_store(struct config_item * item,const char * buf,size_t count)928 static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
929 				     size_t count)
930 {
931 	struct userdatum *udm = to_userdatum(item);
932 	struct netconsole_target *nt;
933 	struct userdata *ud;
934 	ssize_t ret;
935 
936 	if (count > MAX_EXTRADATA_VALUE_LEN)
937 		return -EMSGSIZE;
938 
939 	mutex_lock(&netconsole_subsys.su_mutex);
940 	mutex_lock(&dynamic_netconsole_mutex);
941 
942 	ret = strscpy(udm->value, buf, sizeof(udm->value));
943 	if (ret < 0)
944 		goto out_unlock;
945 	trim_newline(udm->value, sizeof(udm->value));
946 
947 	ud = to_userdata(item->ci_parent);
948 	nt = userdata_to_target(ud);
949 	update_userdata(nt);
950 	ret = count;
951 out_unlock:
952 	mutex_unlock(&dynamic_netconsole_mutex);
953 	mutex_unlock(&netconsole_subsys.su_mutex);
954 	return ret;
955 }
956 
957 /* disable_sysdata_feature - Disable sysdata feature and clean sysdata
958  * @nt: target that is disabling the feature
959  * @feature: feature being disabled
960  */
disable_sysdata_feature(struct netconsole_target * nt,enum sysdata_feature feature)961 static void disable_sysdata_feature(struct netconsole_target *nt,
962 				    enum sysdata_feature feature)
963 {
964 	nt->sysdata_fields &= ~feature;
965 	nt->extradata_complete[nt->userdata_length] = 0;
966 }
967 
sysdata_msgid_enabled_store(struct config_item * item,const char * buf,size_t count)968 static ssize_t sysdata_msgid_enabled_store(struct config_item *item,
969 					   const char *buf, size_t count)
970 {
971 	struct netconsole_target *nt = to_target(item->ci_parent);
972 	bool msgid_enabled, curr;
973 	ssize_t ret;
974 
975 	ret = kstrtobool(buf, &msgid_enabled);
976 	if (ret)
977 		return ret;
978 
979 	mutex_lock(&netconsole_subsys.su_mutex);
980 	mutex_lock(&dynamic_netconsole_mutex);
981 	curr = !!(nt->sysdata_fields & SYSDATA_MSGID);
982 	if (msgid_enabled == curr)
983 		goto unlock_ok;
984 
985 	if (msgid_enabled &&
986 	    count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS) {
987 		ret = -ENOSPC;
988 		goto unlock;
989 	}
990 
991 	if (msgid_enabled)
992 		nt->sysdata_fields |= SYSDATA_MSGID;
993 	else
994 		disable_sysdata_feature(nt, SYSDATA_MSGID);
995 
996 unlock_ok:
997 	ret = strnlen(buf, count);
998 unlock:
999 	mutex_unlock(&dynamic_netconsole_mutex);
1000 	mutex_unlock(&netconsole_subsys.su_mutex);
1001 	return ret;
1002 }
1003 
sysdata_release_enabled_store(struct config_item * item,const char * buf,size_t count)1004 static ssize_t sysdata_release_enabled_store(struct config_item *item,
1005 					     const char *buf, size_t count)
1006 {
1007 	struct netconsole_target *nt = to_target(item->ci_parent);
1008 	bool release_enabled, curr;
1009 	ssize_t ret;
1010 
1011 	ret = kstrtobool(buf, &release_enabled);
1012 	if (ret)
1013 		return ret;
1014 
1015 	mutex_lock(&netconsole_subsys.su_mutex);
1016 	mutex_lock(&dynamic_netconsole_mutex);
1017 	curr = !!(nt->sysdata_fields & SYSDATA_RELEASE);
1018 	if (release_enabled == curr)
1019 		goto unlock_ok;
1020 
1021 	if (release_enabled &&
1022 	    count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS) {
1023 		ret = -ENOSPC;
1024 		goto unlock;
1025 	}
1026 
1027 	if (release_enabled)
1028 		nt->sysdata_fields |= SYSDATA_RELEASE;
1029 	else
1030 		disable_sysdata_feature(nt, SYSDATA_RELEASE);
1031 
1032 unlock_ok:
1033 	ret = strnlen(buf, count);
1034 unlock:
1035 	mutex_unlock(&dynamic_netconsole_mutex);
1036 	mutex_unlock(&netconsole_subsys.su_mutex);
1037 	return ret;
1038 }
1039 
sysdata_taskname_enabled_store(struct config_item * item,const char * buf,size_t count)1040 static ssize_t sysdata_taskname_enabled_store(struct config_item *item,
1041 					      const char *buf, size_t count)
1042 {
1043 	struct netconsole_target *nt = to_target(item->ci_parent);
1044 	bool taskname_enabled, curr;
1045 	ssize_t ret;
1046 
1047 	ret = kstrtobool(buf, &taskname_enabled);
1048 	if (ret)
1049 		return ret;
1050 
1051 	mutex_lock(&netconsole_subsys.su_mutex);
1052 	mutex_lock(&dynamic_netconsole_mutex);
1053 	curr = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
1054 	if (taskname_enabled == curr)
1055 		goto unlock_ok;
1056 
1057 	if (taskname_enabled &&
1058 	    count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS) {
1059 		ret = -ENOSPC;
1060 		goto unlock;
1061 	}
1062 
1063 	if (taskname_enabled)
1064 		nt->sysdata_fields |= SYSDATA_TASKNAME;
1065 	else
1066 		disable_sysdata_feature(nt, SYSDATA_TASKNAME);
1067 
1068 unlock_ok:
1069 	ret = strnlen(buf, count);
1070 unlock:
1071 	mutex_unlock(&dynamic_netconsole_mutex);
1072 	mutex_unlock(&netconsole_subsys.su_mutex);
1073 	return ret;
1074 }
1075 
1076 /* configfs helper to sysdata cpu_nr feature */
sysdata_cpu_nr_enabled_store(struct config_item * item,const char * buf,size_t count)1077 static ssize_t sysdata_cpu_nr_enabled_store(struct config_item *item,
1078 					    const char *buf, size_t count)
1079 {
1080 	struct netconsole_target *nt = to_target(item->ci_parent);
1081 	bool cpu_nr_enabled, curr;
1082 	ssize_t ret;
1083 
1084 	ret = kstrtobool(buf, &cpu_nr_enabled);
1085 	if (ret)
1086 		return ret;
1087 
1088 	mutex_lock(&netconsole_subsys.su_mutex);
1089 	mutex_lock(&dynamic_netconsole_mutex);
1090 	curr = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
1091 	if (cpu_nr_enabled == curr)
1092 		/* no change requested */
1093 		goto unlock_ok;
1094 
1095 	if (cpu_nr_enabled &&
1096 	    count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS) {
1097 		/* user wants the new feature, but there is no space in the
1098 		 * buffer.
1099 		 */
1100 		ret = -ENOSPC;
1101 		goto unlock;
1102 	}
1103 
1104 	if (cpu_nr_enabled)
1105 		nt->sysdata_fields |= SYSDATA_CPU_NR;
1106 	else
1107 		/* This is special because extradata_complete might have
1108 		 * remaining data from previous sysdata, and it needs to be
1109 		 * cleaned.
1110 		 */
1111 		disable_sysdata_feature(nt, SYSDATA_CPU_NR);
1112 
1113 unlock_ok:
1114 	ret = strnlen(buf, count);
1115 unlock:
1116 	mutex_unlock(&dynamic_netconsole_mutex);
1117 	mutex_unlock(&netconsole_subsys.su_mutex);
1118 	return ret;
1119 }
1120 
1121 CONFIGFS_ATTR(userdatum_, value);
1122 CONFIGFS_ATTR(sysdata_, cpu_nr_enabled);
1123 CONFIGFS_ATTR(sysdata_, taskname_enabled);
1124 CONFIGFS_ATTR(sysdata_, release_enabled);
1125 CONFIGFS_ATTR(sysdata_, msgid_enabled);
1126 
1127 static struct configfs_attribute *userdatum_attrs[] = {
1128 	&userdatum_attr_value,
1129 	NULL,
1130 };
1131 
userdatum_release(struct config_item * item)1132 static void userdatum_release(struct config_item *item)
1133 {
1134 	kfree(to_userdatum(item));
1135 }
1136 
1137 static struct configfs_item_operations userdatum_ops = {
1138 	.release = userdatum_release,
1139 };
1140 
1141 static const struct config_item_type userdatum_type = {
1142 	.ct_item_ops	= &userdatum_ops,
1143 	.ct_attrs	= userdatum_attrs,
1144 	.ct_owner	= THIS_MODULE,
1145 };
1146 
userdatum_make_item(struct config_group * group,const char * name)1147 static struct config_item *userdatum_make_item(struct config_group *group,
1148 					       const char *name)
1149 {
1150 	struct netconsole_target *nt;
1151 	struct userdatum *udm;
1152 	struct userdata *ud;
1153 
1154 	if (strlen(name) > MAX_EXTRADATA_NAME_LEN)
1155 		return ERR_PTR(-ENAMETOOLONG);
1156 
1157 	ud = to_userdata(&group->cg_item);
1158 	nt = userdata_to_target(ud);
1159 	if (count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS)
1160 		return ERR_PTR(-ENOSPC);
1161 
1162 	udm = kzalloc(sizeof(*udm), GFP_KERNEL);
1163 	if (!udm)
1164 		return ERR_PTR(-ENOMEM);
1165 
1166 	config_item_init_type_name(&udm->item, name, &userdatum_type);
1167 	return &udm->item;
1168 }
1169 
userdatum_drop(struct config_group * group,struct config_item * item)1170 static void userdatum_drop(struct config_group *group, struct config_item *item)
1171 {
1172 	struct netconsole_target *nt;
1173 	struct userdata *ud;
1174 
1175 	ud = to_userdata(&group->cg_item);
1176 	nt = userdata_to_target(ud);
1177 
1178 	mutex_lock(&dynamic_netconsole_mutex);
1179 	update_userdata(nt);
1180 	config_item_put(item);
1181 	mutex_unlock(&dynamic_netconsole_mutex);
1182 }
1183 
1184 static struct configfs_attribute *userdata_attrs[] = {
1185 	&sysdata_attr_cpu_nr_enabled,
1186 	&sysdata_attr_taskname_enabled,
1187 	&sysdata_attr_release_enabled,
1188 	&sysdata_attr_msgid_enabled,
1189 	NULL,
1190 };
1191 
1192 static struct configfs_group_operations userdata_ops = {
1193 	.make_item		= userdatum_make_item,
1194 	.drop_item		= userdatum_drop,
1195 };
1196 
1197 static const struct config_item_type userdata_type = {
1198 	.ct_item_ops	= &userdatum_ops,
1199 	.ct_group_ops	= &userdata_ops,
1200 	.ct_attrs	= userdata_attrs,
1201 	.ct_owner	= THIS_MODULE,
1202 };
1203 
1204 CONFIGFS_ATTR(, enabled);
1205 CONFIGFS_ATTR(, extended);
1206 CONFIGFS_ATTR(, dev_name);
1207 CONFIGFS_ATTR(, local_port);
1208 CONFIGFS_ATTR(, remote_port);
1209 CONFIGFS_ATTR(, local_ip);
1210 CONFIGFS_ATTR(, remote_ip);
1211 CONFIGFS_ATTR_RO(, local_mac);
1212 CONFIGFS_ATTR(, remote_mac);
1213 CONFIGFS_ATTR(, release);
1214 CONFIGFS_ATTR_RO(, transmit_errors);
1215 
1216 static struct configfs_attribute *netconsole_target_attrs[] = {
1217 	&attr_enabled,
1218 	&attr_extended,
1219 	&attr_release,
1220 	&attr_dev_name,
1221 	&attr_local_port,
1222 	&attr_remote_port,
1223 	&attr_local_ip,
1224 	&attr_remote_ip,
1225 	&attr_local_mac,
1226 	&attr_remote_mac,
1227 	&attr_transmit_errors,
1228 	NULL,
1229 };
1230 
1231 /*
1232  * Item operations and type for netconsole_target.
1233  */
1234 
netconsole_target_release(struct config_item * item)1235 static void netconsole_target_release(struct config_item *item)
1236 {
1237 	kfree(to_target(item));
1238 }
1239 
1240 static struct configfs_item_operations netconsole_target_item_ops = {
1241 	.release		= netconsole_target_release,
1242 };
1243 
1244 static const struct config_item_type netconsole_target_type = {
1245 	.ct_attrs		= netconsole_target_attrs,
1246 	.ct_item_ops		= &netconsole_target_item_ops,
1247 	.ct_owner		= THIS_MODULE,
1248 };
1249 
init_target_config_group(struct netconsole_target * nt,const char * name)1250 static void init_target_config_group(struct netconsole_target *nt,
1251 				     const char *name)
1252 {
1253 	config_group_init_type_name(&nt->group, name, &netconsole_target_type);
1254 	config_group_init_type_name(&nt->userdata_group, "userdata",
1255 				    &userdata_type);
1256 	configfs_add_default_group(&nt->userdata_group, &nt->group);
1257 }
1258 
find_cmdline_target(const char * name)1259 static struct netconsole_target *find_cmdline_target(const char *name)
1260 {
1261 	struct netconsole_target *nt, *ret = NULL;
1262 	unsigned long flags;
1263 
1264 	spin_lock_irqsave(&target_list_lock, flags);
1265 	list_for_each_entry(nt, &target_list, list) {
1266 		if (!strcmp(nt->group.cg_item.ci_name, name)) {
1267 			ret = nt;
1268 			break;
1269 		}
1270 	}
1271 	spin_unlock_irqrestore(&target_list_lock, flags);
1272 
1273 	return ret;
1274 }
1275 
1276 /*
1277  * Group operations and type for netconsole_subsys.
1278  */
1279 
make_netconsole_target(struct config_group * group,const char * name)1280 static struct config_group *make_netconsole_target(struct config_group *group,
1281 						   const char *name)
1282 {
1283 	struct netconsole_target *nt;
1284 	unsigned long flags;
1285 
1286 	/* Checking if a target by this name was created at boot time.  If so,
1287 	 * attach a configfs entry to that target.  This enables dynamic
1288 	 * control.
1289 	 */
1290 	if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
1291 		     strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
1292 		nt = find_cmdline_target(name);
1293 		if (nt) {
1294 			init_target_config_group(nt, name);
1295 			return &nt->group;
1296 		}
1297 	}
1298 
1299 	nt = alloc_and_init();
1300 	if (!nt)
1301 		return ERR_PTR(-ENOMEM);
1302 
1303 	/* Initialize the config_group member */
1304 	init_target_config_group(nt, name);
1305 
1306 	/* Adding, but it is disabled */
1307 	spin_lock_irqsave(&target_list_lock, flags);
1308 	list_add(&nt->list, &target_list);
1309 	spin_unlock_irqrestore(&target_list_lock, flags);
1310 
1311 	return &nt->group;
1312 }
1313 
drop_netconsole_target(struct config_group * group,struct config_item * item)1314 static void drop_netconsole_target(struct config_group *group,
1315 				   struct config_item *item)
1316 {
1317 	unsigned long flags;
1318 	struct netconsole_target *nt = to_target(item);
1319 
1320 	spin_lock_irqsave(&target_list_lock, flags);
1321 	list_del(&nt->list);
1322 	spin_unlock_irqrestore(&target_list_lock, flags);
1323 
1324 	/*
1325 	 * The target may have never been enabled, or was manually disabled
1326 	 * before being removed so netpoll may have already been cleaned up.
1327 	 */
1328 	if (nt->enabled)
1329 		netpoll_cleanup(&nt->np);
1330 
1331 	config_item_put(&nt->group.cg_item);
1332 }
1333 
1334 static struct configfs_group_operations netconsole_subsys_group_ops = {
1335 	.make_group	= make_netconsole_target,
1336 	.drop_item	= drop_netconsole_target,
1337 };
1338 
1339 static const struct config_item_type netconsole_subsys_type = {
1340 	.ct_group_ops	= &netconsole_subsys_group_ops,
1341 	.ct_owner	= THIS_MODULE,
1342 };
1343 
1344 /* The netconsole configfs subsystem */
1345 static struct configfs_subsystem netconsole_subsys = {
1346 	.su_group	= {
1347 		.cg_item	= {
1348 			.ci_namebuf	= "netconsole",
1349 			.ci_type	= &netconsole_subsys_type,
1350 		},
1351 	},
1352 };
1353 
populate_configfs_item(struct netconsole_target * nt,int cmdline_count)1354 static void populate_configfs_item(struct netconsole_target *nt,
1355 				   int cmdline_count)
1356 {
1357 	char target_name[16];
1358 
1359 	snprintf(target_name, sizeof(target_name), "%s%d",
1360 		 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
1361 	init_target_config_group(nt, target_name);
1362 }
1363 
sysdata_append_cpu_nr(struct netconsole_target * nt,int offset)1364 static int sysdata_append_cpu_nr(struct netconsole_target *nt, int offset)
1365 {
1366 	/* Append cpu=%d at extradata_complete after userdata str */
1367 	return scnprintf(&nt->extradata_complete[offset],
1368 			 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
1369 			 raw_smp_processor_id());
1370 }
1371 
sysdata_append_taskname(struct netconsole_target * nt,int offset)1372 static int sysdata_append_taskname(struct netconsole_target *nt, int offset)
1373 {
1374 	return scnprintf(&nt->extradata_complete[offset],
1375 			 MAX_EXTRADATA_ENTRY_LEN, " taskname=%s\n",
1376 			 current->comm);
1377 }
1378 
sysdata_append_release(struct netconsole_target * nt,int offset)1379 static int sysdata_append_release(struct netconsole_target *nt, int offset)
1380 {
1381 	return scnprintf(&nt->extradata_complete[offset],
1382 			 MAX_EXTRADATA_ENTRY_LEN, " release=%s\n",
1383 			 init_utsname()->release);
1384 }
1385 
sysdata_append_msgid(struct netconsole_target * nt,int offset)1386 static int sysdata_append_msgid(struct netconsole_target *nt, int offset)
1387 {
1388 	wrapping_assign_add(nt->msgcounter, 1);
1389 	return scnprintf(&nt->extradata_complete[offset],
1390 			 MAX_EXTRADATA_ENTRY_LEN, " msgid=%u\n",
1391 			 nt->msgcounter);
1392 }
1393 
1394 /*
1395  * prepare_extradata - append sysdata at extradata_complete in runtime
1396  * @nt: target to send message to
1397  */
prepare_extradata(struct netconsole_target * nt)1398 static int prepare_extradata(struct netconsole_target *nt)
1399 {
1400 	int extradata_len;
1401 
1402 	/* userdata was appended when configfs write helper was called
1403 	 * by update_userdata().
1404 	 */
1405 	extradata_len = nt->userdata_length;
1406 
1407 	if (!nt->sysdata_fields)
1408 		goto out;
1409 
1410 	if (nt->sysdata_fields & SYSDATA_CPU_NR)
1411 		extradata_len += sysdata_append_cpu_nr(nt, extradata_len);
1412 	if (nt->sysdata_fields & SYSDATA_TASKNAME)
1413 		extradata_len += sysdata_append_taskname(nt, extradata_len);
1414 	if (nt->sysdata_fields & SYSDATA_RELEASE)
1415 		extradata_len += sysdata_append_release(nt, extradata_len);
1416 	if (nt->sysdata_fields & SYSDATA_MSGID)
1417 		extradata_len += sysdata_append_msgid(nt, extradata_len);
1418 
1419 	WARN_ON_ONCE(extradata_len >
1420 		     MAX_EXTRADATA_ENTRY_LEN * MAX_EXTRADATA_ITEMS);
1421 
1422 out:
1423 	return extradata_len;
1424 }
1425 #else /* CONFIG_NETCONSOLE_DYNAMIC not set */
prepare_extradata(struct netconsole_target * nt)1426 static int prepare_extradata(struct netconsole_target *nt)
1427 {
1428 	return 0;
1429 }
1430 #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
1431 
1432 /* Handle network interface device notifications */
netconsole_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)1433 static int netconsole_netdev_event(struct notifier_block *this,
1434 				   unsigned long event, void *ptr)
1435 {
1436 	unsigned long flags;
1437 	struct netconsole_target *nt, *tmp;
1438 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1439 	bool stopped = false;
1440 
1441 	if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
1442 	      event == NETDEV_RELEASE || event == NETDEV_JOIN))
1443 		goto done;
1444 
1445 	mutex_lock(&target_cleanup_list_lock);
1446 	spin_lock_irqsave(&target_list_lock, flags);
1447 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
1448 		netconsole_target_get(nt);
1449 		if (nt->np.dev == dev) {
1450 			switch (event) {
1451 			case NETDEV_CHANGENAME:
1452 				strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
1453 				break;
1454 			case NETDEV_RELEASE:
1455 			case NETDEV_JOIN:
1456 			case NETDEV_UNREGISTER:
1457 				nt->enabled = false;
1458 				list_move(&nt->list, &target_cleanup_list);
1459 				stopped = true;
1460 			}
1461 		}
1462 		netconsole_target_put(nt);
1463 	}
1464 	spin_unlock_irqrestore(&target_list_lock, flags);
1465 	mutex_unlock(&target_cleanup_list_lock);
1466 
1467 	if (stopped) {
1468 		const char *msg = "had an event";
1469 
1470 		switch (event) {
1471 		case NETDEV_UNREGISTER:
1472 			msg = "unregistered";
1473 			break;
1474 		case NETDEV_RELEASE:
1475 			msg = "released slaves";
1476 			break;
1477 		case NETDEV_JOIN:
1478 			msg = "is joining a master device";
1479 			break;
1480 		}
1481 		pr_info("network logging stopped on interface %s as it %s\n",
1482 			dev->name, msg);
1483 	}
1484 
1485 	/* Process target_cleanup_list entries. By the end, target_cleanup_list
1486 	 * should be empty
1487 	 */
1488 	netconsole_process_cleanups_core();
1489 
1490 done:
1491 	return NOTIFY_DONE;
1492 }
1493 
1494 static struct notifier_block netconsole_netdev_notifier = {
1495 	.notifier_call  = netconsole_netdev_event,
1496 };
1497 
1498 /**
1499  * send_udp - Wrapper for netpoll_send_udp that counts errors
1500  * @nt: target to send message to
1501  * @msg: message to send
1502  * @len: length of message
1503  *
1504  * Calls netpoll_send_udp and classifies the return value. If an error
1505  * occurred it increments statistics in nt->stats accordingly.
1506  * Only calls netpoll_send_udp if CONFIG_NETCONSOLE_DYNAMIC is disabled.
1507  */
send_udp(struct netconsole_target * nt,const char * msg,int len)1508 static void send_udp(struct netconsole_target *nt, const char *msg, int len)
1509 {
1510 	int result = netpoll_send_udp(&nt->np, msg, len);
1511 
1512 	if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) {
1513 		if (result == NET_XMIT_DROP) {
1514 			u64_stats_update_begin(&nt->stats.syncp);
1515 			u64_stats_inc(&nt->stats.xmit_drop_count);
1516 			u64_stats_update_end(&nt->stats.syncp);
1517 		} else if (result == -ENOMEM) {
1518 			u64_stats_update_begin(&nt->stats.syncp);
1519 			u64_stats_inc(&nt->stats.enomem_count);
1520 			u64_stats_update_end(&nt->stats.syncp);
1521 		}
1522 	}
1523 }
1524 
send_msg_no_fragmentation(struct netconsole_target * nt,const char * msg,int msg_len,int release_len)1525 static void send_msg_no_fragmentation(struct netconsole_target *nt,
1526 				      const char *msg,
1527 				      int msg_len,
1528 				      int release_len)
1529 {
1530 	const char *extradata = NULL;
1531 	const char *release;
1532 
1533 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1534 	extradata = nt->extradata_complete;
1535 #endif
1536 
1537 	if (release_len) {
1538 		release = init_utsname()->release;
1539 
1540 		scnprintf(nt->buf, MAX_PRINT_CHUNK, "%s,%s", release, msg);
1541 		msg_len += release_len;
1542 	} else {
1543 		memcpy(nt->buf, msg, msg_len);
1544 	}
1545 
1546 	if (extradata)
1547 		msg_len += scnprintf(&nt->buf[msg_len],
1548 				     MAX_PRINT_CHUNK - msg_len,
1549 				     "%s", extradata);
1550 
1551 	send_udp(nt, nt->buf, msg_len);
1552 }
1553 
append_release(char * buf)1554 static void append_release(char *buf)
1555 {
1556 	const char *release;
1557 
1558 	release = init_utsname()->release;
1559 	scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
1560 }
1561 
send_fragmented_body(struct netconsole_target * nt,const char * msgbody,int header_len,int msgbody_len,int extradata_len)1562 static void send_fragmented_body(struct netconsole_target *nt,
1563 				 const char *msgbody, int header_len,
1564 				 int msgbody_len, int extradata_len)
1565 {
1566 	int sent_extradata, preceding_bytes;
1567 	const char *extradata = NULL;
1568 	int body_len, offset = 0;
1569 
1570 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1571 	extradata = nt->extradata_complete;
1572 #endif
1573 
1574 	/* body_len represents the number of bytes that will be sent. This is
1575 	 * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple
1576 	 * packets
1577 	 */
1578 	body_len = msgbody_len + extradata_len;
1579 
1580 	/* In each iteration of the while loop below, we send a packet
1581 	 * containing the header and a portion of the body. The body is
1582 	 * composed of two parts: msgbody and extradata. We keep track of how
1583 	 * many bytes have been sent so far using the offset variable, which
1584 	 * ranges from 0 to the total length of the body.
1585 	 */
1586 	while (offset < body_len) {
1587 		int this_header = header_len;
1588 		bool msgbody_written = false;
1589 		int this_offset = 0;
1590 		int this_chunk = 0;
1591 
1592 		this_header += scnprintf(nt->buf + this_header,
1593 					 MAX_PRINT_CHUNK - this_header,
1594 					 ",ncfrag=%d/%d;", offset,
1595 					 body_len);
1596 
1597 		/* Not all msgbody data has been written yet */
1598 		if (offset < msgbody_len) {
1599 			this_chunk = min(msgbody_len - offset,
1600 					 MAX_PRINT_CHUNK - this_header);
1601 			if (WARN_ON_ONCE(this_chunk <= 0))
1602 				return;
1603 			memcpy(nt->buf + this_header, msgbody + offset,
1604 			       this_chunk);
1605 			this_offset += this_chunk;
1606 		}
1607 
1608 		/* msgbody was finally written, either in the previous
1609 		 * messages and/or in the current buf. Time to write
1610 		 * the extradata.
1611 		 */
1612 		msgbody_written |= offset + this_offset >= msgbody_len;
1613 
1614 		/* Msg body is fully written and there is pending extradata to
1615 		 * write, append extradata in this chunk
1616 		 */
1617 		if (msgbody_written && offset + this_offset < body_len) {
1618 			/* Track how much user data was already sent. First
1619 			 * time here, sent_userdata is zero
1620 			 */
1621 			sent_extradata = (offset + this_offset) - msgbody_len;
1622 			/* offset of bytes used in current buf */
1623 			preceding_bytes = this_chunk + this_header;
1624 
1625 			if (WARN_ON_ONCE(sent_extradata < 0))
1626 				return;
1627 
1628 			this_chunk = min(extradata_len - sent_extradata,
1629 					 MAX_PRINT_CHUNK - preceding_bytes);
1630 			if (WARN_ON_ONCE(this_chunk < 0))
1631 				/* this_chunk could be zero if all the previous
1632 				 * message used all the buffer. This is not a
1633 				 * problem, extradata will be sent in the next
1634 				 * iteration
1635 				 */
1636 				return;
1637 
1638 			memcpy(nt->buf + this_header + this_offset,
1639 			       extradata + sent_extradata,
1640 			       this_chunk);
1641 			this_offset += this_chunk;
1642 		}
1643 
1644 		send_udp(nt, nt->buf, this_header + this_offset);
1645 		offset += this_offset;
1646 	}
1647 }
1648 
send_msg_fragmented(struct netconsole_target * nt,const char * msg,int msg_len,int release_len,int extradata_len)1649 static void send_msg_fragmented(struct netconsole_target *nt,
1650 				const char *msg,
1651 				int msg_len,
1652 				int release_len,
1653 				int extradata_len)
1654 {
1655 	int header_len, msgbody_len;
1656 	const char *msgbody;
1657 
1658 	/* need to insert extra header fields, detect header and msgbody */
1659 	msgbody = memchr(msg, ';', msg_len);
1660 	if (WARN_ON_ONCE(!msgbody))
1661 		return;
1662 
1663 	header_len = msgbody - msg;
1664 	msgbody_len = msg_len - header_len - 1;
1665 	msgbody++;
1666 
1667 	/*
1668 	 * Transfer multiple chunks with the following extra header.
1669 	 * "ncfrag=<byte-offset>/<total-bytes>"
1670 	 */
1671 	if (release_len)
1672 		append_release(nt->buf);
1673 
1674 	/* Copy the header into the buffer */
1675 	memcpy(nt->buf + release_len, msg, header_len);
1676 	header_len += release_len;
1677 
1678 	/* for now on, the header will be persisted, and the msgbody
1679 	 * will be replaced
1680 	 */
1681 	send_fragmented_body(nt, msgbody, header_len, msgbody_len,
1682 			     extradata_len);
1683 }
1684 
1685 /**
1686  * send_ext_msg_udp - send extended log message to target
1687  * @nt: target to send message to
1688  * @msg: extended log message to send
1689  * @msg_len: length of message
1690  *
1691  * Transfer extended log @msg to @nt.  If @msg is longer than
1692  * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
1693  * ncfrag header field added to identify them.
1694  */
send_ext_msg_udp(struct netconsole_target * nt,const char * msg,int msg_len)1695 static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
1696 			     int msg_len)
1697 {
1698 	int release_len = 0;
1699 	int extradata_len;
1700 
1701 	extradata_len = prepare_extradata(nt);
1702 
1703 	if (nt->release)
1704 		release_len = strlen(init_utsname()->release) + 1;
1705 
1706 	if (msg_len + release_len + extradata_len <= MAX_PRINT_CHUNK)
1707 		return send_msg_no_fragmentation(nt, msg, msg_len, release_len);
1708 
1709 	return send_msg_fragmented(nt, msg, msg_len, release_len,
1710 				   extradata_len);
1711 }
1712 
write_ext_msg(struct console * con,const char * msg,unsigned int len)1713 static void write_ext_msg(struct console *con, const char *msg,
1714 			  unsigned int len)
1715 {
1716 	struct netconsole_target *nt;
1717 	unsigned long flags;
1718 
1719 	if ((oops_only && !oops_in_progress) || list_empty(&target_list))
1720 		return;
1721 
1722 	spin_lock_irqsave(&target_list_lock, flags);
1723 	list_for_each_entry(nt, &target_list, list)
1724 		if (nt->extended && nt->enabled && netif_running(nt->np.dev))
1725 			send_ext_msg_udp(nt, msg, len);
1726 	spin_unlock_irqrestore(&target_list_lock, flags);
1727 }
1728 
write_msg(struct console * con,const char * msg,unsigned int len)1729 static void write_msg(struct console *con, const char *msg, unsigned int len)
1730 {
1731 	int frag, left;
1732 	unsigned long flags;
1733 	struct netconsole_target *nt;
1734 	const char *tmp;
1735 
1736 	if (oops_only && !oops_in_progress)
1737 		return;
1738 	/* Avoid taking lock and disabling interrupts unnecessarily */
1739 	if (list_empty(&target_list))
1740 		return;
1741 
1742 	spin_lock_irqsave(&target_list_lock, flags);
1743 	list_for_each_entry(nt, &target_list, list) {
1744 		if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
1745 			/*
1746 			 * We nest this inside the for-each-target loop above
1747 			 * so that we're able to get as much logging out to
1748 			 * at least one target if we die inside here, instead
1749 			 * of unnecessarily keeping all targets in lock-step.
1750 			 */
1751 			tmp = msg;
1752 			for (left = len; left;) {
1753 				frag = min(left, MAX_PRINT_CHUNK);
1754 				send_udp(nt, tmp, frag);
1755 				tmp += frag;
1756 				left -= frag;
1757 			}
1758 		}
1759 	}
1760 	spin_unlock_irqrestore(&target_list_lock, flags);
1761 }
1762 
netconsole_parser_cmdline(struct netpoll * np,char * opt)1763 static int netconsole_parser_cmdline(struct netpoll *np, char *opt)
1764 {
1765 	bool ipversion_set = false;
1766 	char *cur = opt;
1767 	char *delim;
1768 	int ipv6;
1769 
1770 	if (*cur != '@') {
1771 		delim = strchr(cur, '@');
1772 		if (!delim)
1773 			goto parse_failed;
1774 		*delim = 0;
1775 		if (kstrtou16(cur, 10, &np->local_port))
1776 			goto parse_failed;
1777 		cur = delim;
1778 	}
1779 	cur++;
1780 
1781 	if (*cur != '/') {
1782 		ipversion_set = true;
1783 		delim = strchr(cur, '/');
1784 		if (!delim)
1785 			goto parse_failed;
1786 		*delim = 0;
1787 		ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
1788 		if (ipv6 < 0)
1789 			goto parse_failed;
1790 		else
1791 			np->ipv6 = (bool)ipv6;
1792 		cur = delim;
1793 	}
1794 	cur++;
1795 
1796 	if (*cur != ',') {
1797 		/* parse out dev_name or dev_mac */
1798 		delim = strchr(cur, ',');
1799 		if (!delim)
1800 			goto parse_failed;
1801 		*delim = 0;
1802 
1803 		np->dev_name[0] = '\0';
1804 		eth_broadcast_addr(np->dev_mac);
1805 		if (!strchr(cur, ':'))
1806 			strscpy(np->dev_name, cur, sizeof(np->dev_name));
1807 		else if (!mac_pton(cur, np->dev_mac))
1808 			goto parse_failed;
1809 
1810 		cur = delim;
1811 	}
1812 	cur++;
1813 
1814 	if (*cur != '@') {
1815 		/* dst port */
1816 		delim = strchr(cur, '@');
1817 		if (!delim)
1818 			goto parse_failed;
1819 		*delim = 0;
1820 		if (*cur == ' ' || *cur == '\t')
1821 			np_info(np, "warning: whitespace is not allowed\n");
1822 		if (kstrtou16(cur, 10, &np->remote_port))
1823 			goto parse_failed;
1824 		cur = delim;
1825 	}
1826 	cur++;
1827 
1828 	/* dst ip */
1829 	delim = strchr(cur, '/');
1830 	if (!delim)
1831 		goto parse_failed;
1832 	*delim = 0;
1833 	ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
1834 	if (ipv6 < 0)
1835 		goto parse_failed;
1836 	else if (ipversion_set && np->ipv6 != (bool)ipv6)
1837 		goto parse_failed;
1838 	else
1839 		np->ipv6 = (bool)ipv6;
1840 	cur = delim + 1;
1841 
1842 	if (*cur != 0) {
1843 		/* MAC address */
1844 		if (!mac_pton(cur, np->remote_mac))
1845 			goto parse_failed;
1846 	}
1847 
1848 	netconsole_print_banner(np);
1849 
1850 	return 0;
1851 
1852  parse_failed:
1853 	np_info(np, "couldn't parse config at '%s'!\n", cur);
1854 	return -1;
1855 }
1856 
1857 /* Allocate new target (from boot/module param) and setup netpoll for it */
alloc_param_target(char * target_config,int cmdline_count)1858 static struct netconsole_target *alloc_param_target(char *target_config,
1859 						    int cmdline_count)
1860 {
1861 	struct netconsole_target *nt;
1862 	int err;
1863 
1864 	nt = alloc_and_init();
1865 	if (!nt) {
1866 		err = -ENOMEM;
1867 		goto fail;
1868 	}
1869 
1870 	if (*target_config == '+') {
1871 		nt->extended = true;
1872 		target_config++;
1873 	}
1874 
1875 	if (*target_config == 'r') {
1876 		if (!nt->extended) {
1877 			pr_err("Netconsole configuration error. Release feature requires extended log message");
1878 			err = -EINVAL;
1879 			goto fail;
1880 		}
1881 		nt->release = true;
1882 		target_config++;
1883 	}
1884 
1885 	/* Parse parameters and setup netpoll */
1886 	err = netconsole_parser_cmdline(&nt->np, target_config);
1887 	if (err)
1888 		goto fail;
1889 
1890 	err = netpoll_setup(&nt->np);
1891 	if (err) {
1892 		pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n",
1893 		       NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
1894 		if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
1895 			/* only fail if dynamic reconfiguration is set,
1896 			 * otherwise, keep the target in the list, but disabled.
1897 			 */
1898 			goto fail;
1899 	} else {
1900 		nt->enabled = true;
1901 	}
1902 	populate_configfs_item(nt, cmdline_count);
1903 
1904 	return nt;
1905 
1906 fail:
1907 	kfree(nt);
1908 	return ERR_PTR(err);
1909 }
1910 
1911 /* Cleanup netpoll for given target (from boot/module param) and free it */
free_param_target(struct netconsole_target * nt)1912 static void free_param_target(struct netconsole_target *nt)
1913 {
1914 	netpoll_cleanup(&nt->np);
1915 	kfree(nt);
1916 }
1917 
1918 static struct console netconsole_ext = {
1919 	.name	= "netcon_ext",
1920 	.flags	= CON_ENABLED | CON_EXTENDED,
1921 	.write	= write_ext_msg,
1922 };
1923 
1924 static struct console netconsole = {
1925 	.name	= "netcon",
1926 	.flags	= CON_ENABLED,
1927 	.write	= write_msg,
1928 };
1929 
init_netconsole(void)1930 static int __init init_netconsole(void)
1931 {
1932 	int err;
1933 	struct netconsole_target *nt, *tmp;
1934 	u32 console_type_needed = 0;
1935 	unsigned int count = 0;
1936 	unsigned long flags;
1937 	char *target_config;
1938 	char *input = config;
1939 
1940 	if (strnlen(input, MAX_PARAM_LENGTH)) {
1941 		while ((target_config = strsep(&input, ";"))) {
1942 			nt = alloc_param_target(target_config, count);
1943 			if (IS_ERR(nt)) {
1944 				if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
1945 					continue;
1946 				err = PTR_ERR(nt);
1947 				goto fail;
1948 			}
1949 			/* Dump existing printks when we register */
1950 			if (nt->extended) {
1951 				console_type_needed |= CONS_EXTENDED;
1952 				netconsole_ext.flags |= CON_PRINTBUFFER;
1953 			} else {
1954 				console_type_needed |= CONS_BASIC;
1955 				netconsole.flags |= CON_PRINTBUFFER;
1956 			}
1957 
1958 			spin_lock_irqsave(&target_list_lock, flags);
1959 			list_add(&nt->list, &target_list);
1960 			spin_unlock_irqrestore(&target_list_lock, flags);
1961 			count++;
1962 		}
1963 	}
1964 
1965 	err = register_netdevice_notifier(&netconsole_netdev_notifier);
1966 	if (err)
1967 		goto fail;
1968 
1969 	err = dynamic_netconsole_init();
1970 	if (err)
1971 		goto undonotifier;
1972 
1973 	if (console_type_needed & CONS_EXTENDED)
1974 		register_console(&netconsole_ext);
1975 	if (console_type_needed & CONS_BASIC)
1976 		register_console(&netconsole);
1977 	pr_info("network logging started\n");
1978 
1979 	return err;
1980 
1981 undonotifier:
1982 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
1983 
1984 fail:
1985 	pr_err("cleaning up\n");
1986 
1987 	/*
1988 	 * Remove all targets and destroy them (only targets created
1989 	 * from the boot/module option exist here). Skipping the list
1990 	 * lock is safe here, and netpoll_cleanup() will sleep.
1991 	 */
1992 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
1993 		list_del(&nt->list);
1994 		free_param_target(nt);
1995 	}
1996 
1997 	return err;
1998 }
1999 
cleanup_netconsole(void)2000 static void __exit cleanup_netconsole(void)
2001 {
2002 	struct netconsole_target *nt, *tmp;
2003 
2004 	if (console_is_registered(&netconsole_ext))
2005 		unregister_console(&netconsole_ext);
2006 	if (console_is_registered(&netconsole))
2007 		unregister_console(&netconsole);
2008 	dynamic_netconsole_exit();
2009 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
2010 
2011 	/*
2012 	 * Targets created via configfs pin references on our module
2013 	 * and would first be rmdir(2)'ed from userspace. We reach
2014 	 * here only when they are already destroyed, and only those
2015 	 * created from the boot/module option are left, so remove and
2016 	 * destroy them. Skipping the list lock is safe here, and
2017 	 * netpoll_cleanup() will sleep.
2018 	 */
2019 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
2020 		list_del(&nt->list);
2021 		free_param_target(nt);
2022 	}
2023 }
2024 
2025 /*
2026  * Use late_initcall to ensure netconsole is
2027  * initialized after network device driver if built-in.
2028  *
2029  * late_initcall() and module_init() are identical if built as module.
2030  */
2031 late_initcall(init_netconsole);
2032 module_exit(cleanup_netconsole);
2033