xref: /linux/drivers/net/netconsole.c (revision e5763491237ffee22d9b554febc2d00669f81dee)
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(&dynamic_netconsole_mutex);
940 
941 	ret = strscpy(udm->value, buf, sizeof(udm->value));
942 	if (ret < 0)
943 		goto out_unlock;
944 	trim_newline(udm->value, sizeof(udm->value));
945 
946 	ud = to_userdata(item->ci_parent);
947 	nt = userdata_to_target(ud);
948 	update_userdata(nt);
949 	ret = count;
950 out_unlock:
951 	mutex_unlock(&dynamic_netconsole_mutex);
952 	return ret;
953 }
954 
955 /* disable_sysdata_feature - Disable sysdata feature and clean sysdata
956  * @nt: target that is disabling the feature
957  * @feature: feature being disabled
958  */
disable_sysdata_feature(struct netconsole_target * nt,enum sysdata_feature feature)959 static void disable_sysdata_feature(struct netconsole_target *nt,
960 				    enum sysdata_feature feature)
961 {
962 	nt->sysdata_fields &= ~feature;
963 	nt->extradata_complete[nt->userdata_length] = 0;
964 }
965 
sysdata_msgid_enabled_store(struct config_item * item,const char * buf,size_t count)966 static ssize_t sysdata_msgid_enabled_store(struct config_item *item,
967 					   const char *buf, size_t count)
968 {
969 	struct netconsole_target *nt = to_target(item->ci_parent);
970 	bool msgid_enabled, curr;
971 	ssize_t ret;
972 
973 	ret = kstrtobool(buf, &msgid_enabled);
974 	if (ret)
975 		return ret;
976 
977 	mutex_lock(&dynamic_netconsole_mutex);
978 	curr = !!(nt->sysdata_fields & SYSDATA_MSGID);
979 	if (msgid_enabled == curr)
980 		goto unlock_ok;
981 
982 	if (msgid_enabled &&
983 	    count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS) {
984 		ret = -ENOSPC;
985 		goto unlock;
986 	}
987 
988 	if (msgid_enabled)
989 		nt->sysdata_fields |= SYSDATA_MSGID;
990 	else
991 		disable_sysdata_feature(nt, SYSDATA_MSGID);
992 
993 unlock_ok:
994 	ret = strnlen(buf, count);
995 unlock:
996 	mutex_unlock(&dynamic_netconsole_mutex);
997 	return ret;
998 }
999 
sysdata_release_enabled_store(struct config_item * item,const char * buf,size_t count)1000 static ssize_t sysdata_release_enabled_store(struct config_item *item,
1001 					     const char *buf, size_t count)
1002 {
1003 	struct netconsole_target *nt = to_target(item->ci_parent);
1004 	bool release_enabled, curr;
1005 	ssize_t ret;
1006 
1007 	ret = kstrtobool(buf, &release_enabled);
1008 	if (ret)
1009 		return ret;
1010 
1011 	mutex_lock(&dynamic_netconsole_mutex);
1012 	curr = !!(nt->sysdata_fields & SYSDATA_RELEASE);
1013 	if (release_enabled == curr)
1014 		goto unlock_ok;
1015 
1016 	if (release_enabled &&
1017 	    count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS) {
1018 		ret = -ENOSPC;
1019 		goto unlock;
1020 	}
1021 
1022 	if (release_enabled)
1023 		nt->sysdata_fields |= SYSDATA_RELEASE;
1024 	else
1025 		disable_sysdata_feature(nt, SYSDATA_RELEASE);
1026 
1027 unlock_ok:
1028 	ret = strnlen(buf, count);
1029 unlock:
1030 	mutex_unlock(&dynamic_netconsole_mutex);
1031 	return ret;
1032 }
1033 
sysdata_taskname_enabled_store(struct config_item * item,const char * buf,size_t count)1034 static ssize_t sysdata_taskname_enabled_store(struct config_item *item,
1035 					      const char *buf, size_t count)
1036 {
1037 	struct netconsole_target *nt = to_target(item->ci_parent);
1038 	bool taskname_enabled, curr;
1039 	ssize_t ret;
1040 
1041 	ret = kstrtobool(buf, &taskname_enabled);
1042 	if (ret)
1043 		return ret;
1044 
1045 	mutex_lock(&dynamic_netconsole_mutex);
1046 	curr = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
1047 	if (taskname_enabled == curr)
1048 		goto unlock_ok;
1049 
1050 	if (taskname_enabled &&
1051 	    count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS) {
1052 		ret = -ENOSPC;
1053 		goto unlock;
1054 	}
1055 
1056 	if (taskname_enabled)
1057 		nt->sysdata_fields |= SYSDATA_TASKNAME;
1058 	else
1059 		disable_sysdata_feature(nt, SYSDATA_TASKNAME);
1060 
1061 unlock_ok:
1062 	ret = strnlen(buf, count);
1063 unlock:
1064 	mutex_unlock(&dynamic_netconsole_mutex);
1065 	return ret;
1066 }
1067 
1068 /* configfs helper to sysdata cpu_nr feature */
sysdata_cpu_nr_enabled_store(struct config_item * item,const char * buf,size_t count)1069 static ssize_t sysdata_cpu_nr_enabled_store(struct config_item *item,
1070 					    const char *buf, size_t count)
1071 {
1072 	struct netconsole_target *nt = to_target(item->ci_parent);
1073 	bool cpu_nr_enabled, curr;
1074 	ssize_t ret;
1075 
1076 	ret = kstrtobool(buf, &cpu_nr_enabled);
1077 	if (ret)
1078 		return ret;
1079 
1080 	mutex_lock(&dynamic_netconsole_mutex);
1081 	curr = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
1082 	if (cpu_nr_enabled == curr)
1083 		/* no change requested */
1084 		goto unlock_ok;
1085 
1086 	if (cpu_nr_enabled &&
1087 	    count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS) {
1088 		/* user wants the new feature, but there is no space in the
1089 		 * buffer.
1090 		 */
1091 		ret = -ENOSPC;
1092 		goto unlock;
1093 	}
1094 
1095 	if (cpu_nr_enabled)
1096 		nt->sysdata_fields |= SYSDATA_CPU_NR;
1097 	else
1098 		/* This is special because extradata_complete might have
1099 		 * remaining data from previous sysdata, and it needs to be
1100 		 * cleaned.
1101 		 */
1102 		disable_sysdata_feature(nt, SYSDATA_CPU_NR);
1103 
1104 unlock_ok:
1105 	ret = strnlen(buf, count);
1106 unlock:
1107 	mutex_unlock(&dynamic_netconsole_mutex);
1108 	return ret;
1109 }
1110 
1111 CONFIGFS_ATTR(userdatum_, value);
1112 CONFIGFS_ATTR(sysdata_, cpu_nr_enabled);
1113 CONFIGFS_ATTR(sysdata_, taskname_enabled);
1114 CONFIGFS_ATTR(sysdata_, release_enabled);
1115 CONFIGFS_ATTR(sysdata_, msgid_enabled);
1116 
1117 static struct configfs_attribute *userdatum_attrs[] = {
1118 	&userdatum_attr_value,
1119 	NULL,
1120 };
1121 
userdatum_release(struct config_item * item)1122 static void userdatum_release(struct config_item *item)
1123 {
1124 	kfree(to_userdatum(item));
1125 }
1126 
1127 static struct configfs_item_operations userdatum_ops = {
1128 	.release = userdatum_release,
1129 };
1130 
1131 static const struct config_item_type userdatum_type = {
1132 	.ct_item_ops	= &userdatum_ops,
1133 	.ct_attrs	= userdatum_attrs,
1134 	.ct_owner	= THIS_MODULE,
1135 };
1136 
userdatum_make_item(struct config_group * group,const char * name)1137 static struct config_item *userdatum_make_item(struct config_group *group,
1138 					       const char *name)
1139 {
1140 	struct netconsole_target *nt;
1141 	struct userdatum *udm;
1142 	struct userdata *ud;
1143 
1144 	if (strlen(name) > MAX_EXTRADATA_NAME_LEN)
1145 		return ERR_PTR(-ENAMETOOLONG);
1146 
1147 	ud = to_userdata(&group->cg_item);
1148 	nt = userdata_to_target(ud);
1149 	if (count_extradata_entries(nt) >= MAX_EXTRADATA_ITEMS)
1150 		return ERR_PTR(-ENOSPC);
1151 
1152 	udm = kzalloc(sizeof(*udm), GFP_KERNEL);
1153 	if (!udm)
1154 		return ERR_PTR(-ENOMEM);
1155 
1156 	config_item_init_type_name(&udm->item, name, &userdatum_type);
1157 	return &udm->item;
1158 }
1159 
userdatum_drop(struct config_group * group,struct config_item * item)1160 static void userdatum_drop(struct config_group *group, struct config_item *item)
1161 {
1162 	struct netconsole_target *nt;
1163 	struct userdata *ud;
1164 
1165 	ud = to_userdata(&group->cg_item);
1166 	nt = userdata_to_target(ud);
1167 
1168 	mutex_lock(&dynamic_netconsole_mutex);
1169 	update_userdata(nt);
1170 	config_item_put(item);
1171 	mutex_unlock(&dynamic_netconsole_mutex);
1172 }
1173 
1174 static struct configfs_attribute *userdata_attrs[] = {
1175 	&sysdata_attr_cpu_nr_enabled,
1176 	&sysdata_attr_taskname_enabled,
1177 	&sysdata_attr_release_enabled,
1178 	&sysdata_attr_msgid_enabled,
1179 	NULL,
1180 };
1181 
1182 static struct configfs_group_operations userdata_ops = {
1183 	.make_item		= userdatum_make_item,
1184 	.drop_item		= userdatum_drop,
1185 };
1186 
1187 static const struct config_item_type userdata_type = {
1188 	.ct_item_ops	= &userdatum_ops,
1189 	.ct_group_ops	= &userdata_ops,
1190 	.ct_attrs	= userdata_attrs,
1191 	.ct_owner	= THIS_MODULE,
1192 };
1193 
1194 CONFIGFS_ATTR(, enabled);
1195 CONFIGFS_ATTR(, extended);
1196 CONFIGFS_ATTR(, dev_name);
1197 CONFIGFS_ATTR(, local_port);
1198 CONFIGFS_ATTR(, remote_port);
1199 CONFIGFS_ATTR(, local_ip);
1200 CONFIGFS_ATTR(, remote_ip);
1201 CONFIGFS_ATTR_RO(, local_mac);
1202 CONFIGFS_ATTR(, remote_mac);
1203 CONFIGFS_ATTR(, release);
1204 CONFIGFS_ATTR_RO(, transmit_errors);
1205 
1206 static struct configfs_attribute *netconsole_target_attrs[] = {
1207 	&attr_enabled,
1208 	&attr_extended,
1209 	&attr_release,
1210 	&attr_dev_name,
1211 	&attr_local_port,
1212 	&attr_remote_port,
1213 	&attr_local_ip,
1214 	&attr_remote_ip,
1215 	&attr_local_mac,
1216 	&attr_remote_mac,
1217 	&attr_transmit_errors,
1218 	NULL,
1219 };
1220 
1221 /*
1222  * Item operations and type for netconsole_target.
1223  */
1224 
netconsole_target_release(struct config_item * item)1225 static void netconsole_target_release(struct config_item *item)
1226 {
1227 	kfree(to_target(item));
1228 }
1229 
1230 static struct configfs_item_operations netconsole_target_item_ops = {
1231 	.release		= netconsole_target_release,
1232 };
1233 
1234 static const struct config_item_type netconsole_target_type = {
1235 	.ct_attrs		= netconsole_target_attrs,
1236 	.ct_item_ops		= &netconsole_target_item_ops,
1237 	.ct_owner		= THIS_MODULE,
1238 };
1239 
init_target_config_group(struct netconsole_target * nt,const char * name)1240 static void init_target_config_group(struct netconsole_target *nt,
1241 				     const char *name)
1242 {
1243 	config_group_init_type_name(&nt->group, name, &netconsole_target_type);
1244 	config_group_init_type_name(&nt->userdata_group, "userdata",
1245 				    &userdata_type);
1246 	configfs_add_default_group(&nt->userdata_group, &nt->group);
1247 }
1248 
find_cmdline_target(const char * name)1249 static struct netconsole_target *find_cmdline_target(const char *name)
1250 {
1251 	struct netconsole_target *nt, *ret = NULL;
1252 	unsigned long flags;
1253 
1254 	spin_lock_irqsave(&target_list_lock, flags);
1255 	list_for_each_entry(nt, &target_list, list) {
1256 		if (!strcmp(nt->group.cg_item.ci_name, name)) {
1257 			ret = nt;
1258 			break;
1259 		}
1260 	}
1261 	spin_unlock_irqrestore(&target_list_lock, flags);
1262 
1263 	return ret;
1264 }
1265 
1266 /*
1267  * Group operations and type for netconsole_subsys.
1268  */
1269 
make_netconsole_target(struct config_group * group,const char * name)1270 static struct config_group *make_netconsole_target(struct config_group *group,
1271 						   const char *name)
1272 {
1273 	struct netconsole_target *nt;
1274 	unsigned long flags;
1275 
1276 	/* Checking if a target by this name was created at boot time.  If so,
1277 	 * attach a configfs entry to that target.  This enables dynamic
1278 	 * control.
1279 	 */
1280 	if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
1281 		     strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
1282 		nt = find_cmdline_target(name);
1283 		if (nt) {
1284 			init_target_config_group(nt, name);
1285 			return &nt->group;
1286 		}
1287 	}
1288 
1289 	nt = alloc_and_init();
1290 	if (!nt)
1291 		return ERR_PTR(-ENOMEM);
1292 
1293 	/* Initialize the config_group member */
1294 	init_target_config_group(nt, name);
1295 
1296 	/* Adding, but it is disabled */
1297 	spin_lock_irqsave(&target_list_lock, flags);
1298 	list_add(&nt->list, &target_list);
1299 	spin_unlock_irqrestore(&target_list_lock, flags);
1300 
1301 	return &nt->group;
1302 }
1303 
drop_netconsole_target(struct config_group * group,struct config_item * item)1304 static void drop_netconsole_target(struct config_group *group,
1305 				   struct config_item *item)
1306 {
1307 	unsigned long flags;
1308 	struct netconsole_target *nt = to_target(item);
1309 
1310 	spin_lock_irqsave(&target_list_lock, flags);
1311 	list_del(&nt->list);
1312 	spin_unlock_irqrestore(&target_list_lock, flags);
1313 
1314 	/*
1315 	 * The target may have never been enabled, or was manually disabled
1316 	 * before being removed so netpoll may have already been cleaned up.
1317 	 */
1318 	if (nt->enabled)
1319 		netpoll_cleanup(&nt->np);
1320 
1321 	config_item_put(&nt->group.cg_item);
1322 }
1323 
1324 static struct configfs_group_operations netconsole_subsys_group_ops = {
1325 	.make_group	= make_netconsole_target,
1326 	.drop_item	= drop_netconsole_target,
1327 };
1328 
1329 static const struct config_item_type netconsole_subsys_type = {
1330 	.ct_group_ops	= &netconsole_subsys_group_ops,
1331 	.ct_owner	= THIS_MODULE,
1332 };
1333 
1334 /* The netconsole configfs subsystem */
1335 static struct configfs_subsystem netconsole_subsys = {
1336 	.su_group	= {
1337 		.cg_item	= {
1338 			.ci_namebuf	= "netconsole",
1339 			.ci_type	= &netconsole_subsys_type,
1340 		},
1341 	},
1342 };
1343 
populate_configfs_item(struct netconsole_target * nt,int cmdline_count)1344 static void populate_configfs_item(struct netconsole_target *nt,
1345 				   int cmdline_count)
1346 {
1347 	char target_name[16];
1348 
1349 	snprintf(target_name, sizeof(target_name), "%s%d",
1350 		 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
1351 	init_target_config_group(nt, target_name);
1352 }
1353 
sysdata_append_cpu_nr(struct netconsole_target * nt,int offset)1354 static int sysdata_append_cpu_nr(struct netconsole_target *nt, int offset)
1355 {
1356 	/* Append cpu=%d at extradata_complete after userdata str */
1357 	return scnprintf(&nt->extradata_complete[offset],
1358 			 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
1359 			 raw_smp_processor_id());
1360 }
1361 
sysdata_append_taskname(struct netconsole_target * nt,int offset)1362 static int sysdata_append_taskname(struct netconsole_target *nt, int offset)
1363 {
1364 	return scnprintf(&nt->extradata_complete[offset],
1365 			 MAX_EXTRADATA_ENTRY_LEN, " taskname=%s\n",
1366 			 current->comm);
1367 }
1368 
sysdata_append_release(struct netconsole_target * nt,int offset)1369 static int sysdata_append_release(struct netconsole_target *nt, int offset)
1370 {
1371 	return scnprintf(&nt->extradata_complete[offset],
1372 			 MAX_EXTRADATA_ENTRY_LEN, " release=%s\n",
1373 			 init_utsname()->release);
1374 }
1375 
sysdata_append_msgid(struct netconsole_target * nt,int offset)1376 static int sysdata_append_msgid(struct netconsole_target *nt, int offset)
1377 {
1378 	wrapping_assign_add(nt->msgcounter, 1);
1379 	return scnprintf(&nt->extradata_complete[offset],
1380 			 MAX_EXTRADATA_ENTRY_LEN, " msgid=%u\n",
1381 			 nt->msgcounter);
1382 }
1383 
1384 /*
1385  * prepare_extradata - append sysdata at extradata_complete in runtime
1386  * @nt: target to send message to
1387  */
prepare_extradata(struct netconsole_target * nt)1388 static int prepare_extradata(struct netconsole_target *nt)
1389 {
1390 	int extradata_len;
1391 
1392 	/* userdata was appended when configfs write helper was called
1393 	 * by update_userdata().
1394 	 */
1395 	extradata_len = nt->userdata_length;
1396 
1397 	if (!nt->sysdata_fields)
1398 		goto out;
1399 
1400 	if (nt->sysdata_fields & SYSDATA_CPU_NR)
1401 		extradata_len += sysdata_append_cpu_nr(nt, extradata_len);
1402 	if (nt->sysdata_fields & SYSDATA_TASKNAME)
1403 		extradata_len += sysdata_append_taskname(nt, extradata_len);
1404 	if (nt->sysdata_fields & SYSDATA_RELEASE)
1405 		extradata_len += sysdata_append_release(nt, extradata_len);
1406 	if (nt->sysdata_fields & SYSDATA_MSGID)
1407 		extradata_len += sysdata_append_msgid(nt, extradata_len);
1408 
1409 	WARN_ON_ONCE(extradata_len >
1410 		     MAX_EXTRADATA_ENTRY_LEN * MAX_EXTRADATA_ITEMS);
1411 
1412 out:
1413 	return extradata_len;
1414 }
1415 #else /* CONFIG_NETCONSOLE_DYNAMIC not set */
prepare_extradata(struct netconsole_target * nt)1416 static int prepare_extradata(struct netconsole_target *nt)
1417 {
1418 	return 0;
1419 }
1420 #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
1421 
1422 /* Handle network interface device notifications */
netconsole_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)1423 static int netconsole_netdev_event(struct notifier_block *this,
1424 				   unsigned long event, void *ptr)
1425 {
1426 	unsigned long flags;
1427 	struct netconsole_target *nt, *tmp;
1428 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1429 	bool stopped = false;
1430 
1431 	if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
1432 	      event == NETDEV_RELEASE || event == NETDEV_JOIN))
1433 		goto done;
1434 
1435 	mutex_lock(&target_cleanup_list_lock);
1436 	spin_lock_irqsave(&target_list_lock, flags);
1437 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
1438 		netconsole_target_get(nt);
1439 		if (nt->np.dev == dev) {
1440 			switch (event) {
1441 			case NETDEV_CHANGENAME:
1442 				strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
1443 				break;
1444 			case NETDEV_RELEASE:
1445 			case NETDEV_JOIN:
1446 			case NETDEV_UNREGISTER:
1447 				nt->enabled = false;
1448 				list_move(&nt->list, &target_cleanup_list);
1449 				stopped = true;
1450 			}
1451 		}
1452 		netconsole_target_put(nt);
1453 	}
1454 	spin_unlock_irqrestore(&target_list_lock, flags);
1455 	mutex_unlock(&target_cleanup_list_lock);
1456 
1457 	if (stopped) {
1458 		const char *msg = "had an event";
1459 
1460 		switch (event) {
1461 		case NETDEV_UNREGISTER:
1462 			msg = "unregistered";
1463 			break;
1464 		case NETDEV_RELEASE:
1465 			msg = "released slaves";
1466 			break;
1467 		case NETDEV_JOIN:
1468 			msg = "is joining a master device";
1469 			break;
1470 		}
1471 		pr_info("network logging stopped on interface %s as it %s\n",
1472 			dev->name, msg);
1473 	}
1474 
1475 	/* Process target_cleanup_list entries. By the end, target_cleanup_list
1476 	 * should be empty
1477 	 */
1478 	netconsole_process_cleanups_core();
1479 
1480 done:
1481 	return NOTIFY_DONE;
1482 }
1483 
1484 static struct notifier_block netconsole_netdev_notifier = {
1485 	.notifier_call  = netconsole_netdev_event,
1486 };
1487 
1488 /**
1489  * send_udp - Wrapper for netpoll_send_udp that counts errors
1490  * @nt: target to send message to
1491  * @msg: message to send
1492  * @len: length of message
1493  *
1494  * Calls netpoll_send_udp and classifies the return value. If an error
1495  * occurred it increments statistics in nt->stats accordingly.
1496  * Only calls netpoll_send_udp if CONFIG_NETCONSOLE_DYNAMIC is disabled.
1497  */
send_udp(struct netconsole_target * nt,const char * msg,int len)1498 static void send_udp(struct netconsole_target *nt, const char *msg, int len)
1499 {
1500 	int result = netpoll_send_udp(&nt->np, msg, len);
1501 
1502 	if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) {
1503 		if (result == NET_XMIT_DROP) {
1504 			u64_stats_update_begin(&nt->stats.syncp);
1505 			u64_stats_inc(&nt->stats.xmit_drop_count);
1506 			u64_stats_update_end(&nt->stats.syncp);
1507 		} else if (result == -ENOMEM) {
1508 			u64_stats_update_begin(&nt->stats.syncp);
1509 			u64_stats_inc(&nt->stats.enomem_count);
1510 			u64_stats_update_end(&nt->stats.syncp);
1511 		}
1512 	}
1513 }
1514 
send_msg_no_fragmentation(struct netconsole_target * nt,const char * msg,int msg_len,int release_len)1515 static void send_msg_no_fragmentation(struct netconsole_target *nt,
1516 				      const char *msg,
1517 				      int msg_len,
1518 				      int release_len)
1519 {
1520 	const char *extradata = NULL;
1521 	const char *release;
1522 
1523 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1524 	extradata = nt->extradata_complete;
1525 #endif
1526 
1527 	if (release_len) {
1528 		release = init_utsname()->release;
1529 
1530 		scnprintf(nt->buf, MAX_PRINT_CHUNK, "%s,%s", release, msg);
1531 		msg_len += release_len;
1532 	} else {
1533 		memcpy(nt->buf, msg, msg_len);
1534 	}
1535 
1536 	if (extradata)
1537 		msg_len += scnprintf(&nt->buf[msg_len],
1538 				     MAX_PRINT_CHUNK - msg_len,
1539 				     "%s", extradata);
1540 
1541 	send_udp(nt, nt->buf, msg_len);
1542 }
1543 
append_release(char * buf)1544 static void append_release(char *buf)
1545 {
1546 	const char *release;
1547 
1548 	release = init_utsname()->release;
1549 	scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
1550 }
1551 
send_fragmented_body(struct netconsole_target * nt,const char * msgbody,int header_len,int msgbody_len,int extradata_len)1552 static void send_fragmented_body(struct netconsole_target *nt,
1553 				 const char *msgbody, int header_len,
1554 				 int msgbody_len, int extradata_len)
1555 {
1556 	int sent_extradata, preceding_bytes;
1557 	const char *extradata = NULL;
1558 	int body_len, offset = 0;
1559 
1560 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1561 	extradata = nt->extradata_complete;
1562 #endif
1563 
1564 	/* body_len represents the number of bytes that will be sent. This is
1565 	 * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple
1566 	 * packets
1567 	 */
1568 	body_len = msgbody_len + extradata_len;
1569 
1570 	/* In each iteration of the while loop below, we send a packet
1571 	 * containing the header and a portion of the body. The body is
1572 	 * composed of two parts: msgbody and extradata. We keep track of how
1573 	 * many bytes have been sent so far using the offset variable, which
1574 	 * ranges from 0 to the total length of the body.
1575 	 */
1576 	while (offset < body_len) {
1577 		int this_header = header_len;
1578 		bool msgbody_written = false;
1579 		int this_offset = 0;
1580 		int this_chunk = 0;
1581 
1582 		this_header += scnprintf(nt->buf + this_header,
1583 					 MAX_PRINT_CHUNK - this_header,
1584 					 ",ncfrag=%d/%d;", offset,
1585 					 body_len);
1586 
1587 		/* Not all msgbody data has been written yet */
1588 		if (offset < msgbody_len) {
1589 			this_chunk = min(msgbody_len - offset,
1590 					 MAX_PRINT_CHUNK - this_header);
1591 			if (WARN_ON_ONCE(this_chunk <= 0))
1592 				return;
1593 			memcpy(nt->buf + this_header, msgbody + offset,
1594 			       this_chunk);
1595 			this_offset += this_chunk;
1596 		}
1597 
1598 		/* msgbody was finally written, either in the previous
1599 		 * messages and/or in the current buf. Time to write
1600 		 * the extradata.
1601 		 */
1602 		msgbody_written |= offset + this_offset >= msgbody_len;
1603 
1604 		/* Msg body is fully written and there is pending extradata to
1605 		 * write, append extradata in this chunk
1606 		 */
1607 		if (msgbody_written && offset + this_offset < body_len) {
1608 			/* Track how much user data was already sent. First
1609 			 * time here, sent_userdata is zero
1610 			 */
1611 			sent_extradata = (offset + this_offset) - msgbody_len;
1612 			/* offset of bytes used in current buf */
1613 			preceding_bytes = this_chunk + this_header;
1614 
1615 			if (WARN_ON_ONCE(sent_extradata < 0))
1616 				return;
1617 
1618 			this_chunk = min(extradata_len - sent_extradata,
1619 					 MAX_PRINT_CHUNK - preceding_bytes);
1620 			if (WARN_ON_ONCE(this_chunk < 0))
1621 				/* this_chunk could be zero if all the previous
1622 				 * message used all the buffer. This is not a
1623 				 * problem, extradata will be sent in the next
1624 				 * iteration
1625 				 */
1626 				return;
1627 
1628 			memcpy(nt->buf + this_header + this_offset,
1629 			       extradata + sent_extradata,
1630 			       this_chunk);
1631 			this_offset += this_chunk;
1632 		}
1633 
1634 		send_udp(nt, nt->buf, this_header + this_offset);
1635 		offset += this_offset;
1636 	}
1637 }
1638 
send_msg_fragmented(struct netconsole_target * nt,const char * msg,int msg_len,int release_len,int extradata_len)1639 static void send_msg_fragmented(struct netconsole_target *nt,
1640 				const char *msg,
1641 				int msg_len,
1642 				int release_len,
1643 				int extradata_len)
1644 {
1645 	int header_len, msgbody_len;
1646 	const char *msgbody;
1647 
1648 	/* need to insert extra header fields, detect header and msgbody */
1649 	msgbody = memchr(msg, ';', msg_len);
1650 	if (WARN_ON_ONCE(!msgbody))
1651 		return;
1652 
1653 	header_len = msgbody - msg;
1654 	msgbody_len = msg_len - header_len - 1;
1655 	msgbody++;
1656 
1657 	/*
1658 	 * Transfer multiple chunks with the following extra header.
1659 	 * "ncfrag=<byte-offset>/<total-bytes>"
1660 	 */
1661 	if (release_len)
1662 		append_release(nt->buf);
1663 
1664 	/* Copy the header into the buffer */
1665 	memcpy(nt->buf + release_len, msg, header_len);
1666 	header_len += release_len;
1667 
1668 	/* for now on, the header will be persisted, and the msgbody
1669 	 * will be replaced
1670 	 */
1671 	send_fragmented_body(nt, msgbody, header_len, msgbody_len,
1672 			     extradata_len);
1673 }
1674 
1675 /**
1676  * send_ext_msg_udp - send extended log message to target
1677  * @nt: target to send message to
1678  * @msg: extended log message to send
1679  * @msg_len: length of message
1680  *
1681  * Transfer extended log @msg to @nt.  If @msg is longer than
1682  * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
1683  * ncfrag header field added to identify them.
1684  */
send_ext_msg_udp(struct netconsole_target * nt,const char * msg,int msg_len)1685 static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
1686 			     int msg_len)
1687 {
1688 	int release_len = 0;
1689 	int extradata_len;
1690 
1691 	extradata_len = prepare_extradata(nt);
1692 
1693 	if (nt->release)
1694 		release_len = strlen(init_utsname()->release) + 1;
1695 
1696 	if (msg_len + release_len + extradata_len <= MAX_PRINT_CHUNK)
1697 		return send_msg_no_fragmentation(nt, msg, msg_len, release_len);
1698 
1699 	return send_msg_fragmented(nt, msg, msg_len, release_len,
1700 				   extradata_len);
1701 }
1702 
write_ext_msg(struct console * con,const char * msg,unsigned int len)1703 static void write_ext_msg(struct console *con, const char *msg,
1704 			  unsigned int len)
1705 {
1706 	struct netconsole_target *nt;
1707 	unsigned long flags;
1708 
1709 	if ((oops_only && !oops_in_progress) || list_empty(&target_list))
1710 		return;
1711 
1712 	spin_lock_irqsave(&target_list_lock, flags);
1713 	list_for_each_entry(nt, &target_list, list)
1714 		if (nt->extended && nt->enabled && netif_running(nt->np.dev))
1715 			send_ext_msg_udp(nt, msg, len);
1716 	spin_unlock_irqrestore(&target_list_lock, flags);
1717 }
1718 
write_msg(struct console * con,const char * msg,unsigned int len)1719 static void write_msg(struct console *con, const char *msg, unsigned int len)
1720 {
1721 	int frag, left;
1722 	unsigned long flags;
1723 	struct netconsole_target *nt;
1724 	const char *tmp;
1725 
1726 	if (oops_only && !oops_in_progress)
1727 		return;
1728 	/* Avoid taking lock and disabling interrupts unnecessarily */
1729 	if (list_empty(&target_list))
1730 		return;
1731 
1732 	spin_lock_irqsave(&target_list_lock, flags);
1733 	list_for_each_entry(nt, &target_list, list) {
1734 		if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
1735 			/*
1736 			 * We nest this inside the for-each-target loop above
1737 			 * so that we're able to get as much logging out to
1738 			 * at least one target if we die inside here, instead
1739 			 * of unnecessarily keeping all targets in lock-step.
1740 			 */
1741 			tmp = msg;
1742 			for (left = len; left;) {
1743 				frag = min(left, MAX_PRINT_CHUNK);
1744 				send_udp(nt, tmp, frag);
1745 				tmp += frag;
1746 				left -= frag;
1747 			}
1748 		}
1749 	}
1750 	spin_unlock_irqrestore(&target_list_lock, flags);
1751 }
1752 
netconsole_parser_cmdline(struct netpoll * np,char * opt)1753 static int netconsole_parser_cmdline(struct netpoll *np, char *opt)
1754 {
1755 	bool ipversion_set = false;
1756 	char *cur = opt;
1757 	char *delim;
1758 	int ipv6;
1759 
1760 	if (*cur != '@') {
1761 		delim = strchr(cur, '@');
1762 		if (!delim)
1763 			goto parse_failed;
1764 		*delim = 0;
1765 		if (kstrtou16(cur, 10, &np->local_port))
1766 			goto parse_failed;
1767 		cur = delim;
1768 	}
1769 	cur++;
1770 
1771 	if (*cur != '/') {
1772 		ipversion_set = true;
1773 		delim = strchr(cur, '/');
1774 		if (!delim)
1775 			goto parse_failed;
1776 		*delim = 0;
1777 		ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
1778 		if (ipv6 < 0)
1779 			goto parse_failed;
1780 		else
1781 			np->ipv6 = (bool)ipv6;
1782 		cur = delim;
1783 	}
1784 	cur++;
1785 
1786 	if (*cur != ',') {
1787 		/* parse out dev_name or dev_mac */
1788 		delim = strchr(cur, ',');
1789 		if (!delim)
1790 			goto parse_failed;
1791 		*delim = 0;
1792 
1793 		np->dev_name[0] = '\0';
1794 		eth_broadcast_addr(np->dev_mac);
1795 		if (!strchr(cur, ':'))
1796 			strscpy(np->dev_name, cur, sizeof(np->dev_name));
1797 		else if (!mac_pton(cur, np->dev_mac))
1798 			goto parse_failed;
1799 
1800 		cur = delim;
1801 	}
1802 	cur++;
1803 
1804 	if (*cur != '@') {
1805 		/* dst port */
1806 		delim = strchr(cur, '@');
1807 		if (!delim)
1808 			goto parse_failed;
1809 		*delim = 0;
1810 		if (*cur == ' ' || *cur == '\t')
1811 			np_info(np, "warning: whitespace is not allowed\n");
1812 		if (kstrtou16(cur, 10, &np->remote_port))
1813 			goto parse_failed;
1814 		cur = delim;
1815 	}
1816 	cur++;
1817 
1818 	/* dst ip */
1819 	delim = strchr(cur, '/');
1820 	if (!delim)
1821 		goto parse_failed;
1822 	*delim = 0;
1823 	ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
1824 	if (ipv6 < 0)
1825 		goto parse_failed;
1826 	else if (ipversion_set && np->ipv6 != (bool)ipv6)
1827 		goto parse_failed;
1828 	else
1829 		np->ipv6 = (bool)ipv6;
1830 	cur = delim + 1;
1831 
1832 	if (*cur != 0) {
1833 		/* MAC address */
1834 		if (!mac_pton(cur, np->remote_mac))
1835 			goto parse_failed;
1836 	}
1837 
1838 	netconsole_print_banner(np);
1839 
1840 	return 0;
1841 
1842  parse_failed:
1843 	np_info(np, "couldn't parse config at '%s'!\n", cur);
1844 	return -1;
1845 }
1846 
1847 /* Allocate new target (from boot/module param) and setup netpoll for it */
alloc_param_target(char * target_config,int cmdline_count)1848 static struct netconsole_target *alloc_param_target(char *target_config,
1849 						    int cmdline_count)
1850 {
1851 	struct netconsole_target *nt;
1852 	int err;
1853 
1854 	nt = alloc_and_init();
1855 	if (!nt) {
1856 		err = -ENOMEM;
1857 		goto fail;
1858 	}
1859 
1860 	if (*target_config == '+') {
1861 		nt->extended = true;
1862 		target_config++;
1863 	}
1864 
1865 	if (*target_config == 'r') {
1866 		if (!nt->extended) {
1867 			pr_err("Netconsole configuration error. Release feature requires extended log message");
1868 			err = -EINVAL;
1869 			goto fail;
1870 		}
1871 		nt->release = true;
1872 		target_config++;
1873 	}
1874 
1875 	/* Parse parameters and setup netpoll */
1876 	err = netconsole_parser_cmdline(&nt->np, target_config);
1877 	if (err)
1878 		goto fail;
1879 
1880 	err = netpoll_setup(&nt->np);
1881 	if (err) {
1882 		pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n",
1883 		       NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
1884 		if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
1885 			/* only fail if dynamic reconfiguration is set,
1886 			 * otherwise, keep the target in the list, but disabled.
1887 			 */
1888 			goto fail;
1889 	} else {
1890 		nt->enabled = true;
1891 	}
1892 	populate_configfs_item(nt, cmdline_count);
1893 
1894 	return nt;
1895 
1896 fail:
1897 	kfree(nt);
1898 	return ERR_PTR(err);
1899 }
1900 
1901 /* Cleanup netpoll for given target (from boot/module param) and free it */
free_param_target(struct netconsole_target * nt)1902 static void free_param_target(struct netconsole_target *nt)
1903 {
1904 	netpoll_cleanup(&nt->np);
1905 	kfree(nt);
1906 }
1907 
1908 static struct console netconsole_ext = {
1909 	.name	= "netcon_ext",
1910 	.flags	= CON_ENABLED | CON_EXTENDED,
1911 	.write	= write_ext_msg,
1912 };
1913 
1914 static struct console netconsole = {
1915 	.name	= "netcon",
1916 	.flags	= CON_ENABLED,
1917 	.write	= write_msg,
1918 };
1919 
init_netconsole(void)1920 static int __init init_netconsole(void)
1921 {
1922 	int err;
1923 	struct netconsole_target *nt, *tmp;
1924 	u32 console_type_needed = 0;
1925 	unsigned int count = 0;
1926 	unsigned long flags;
1927 	char *target_config;
1928 	char *input = config;
1929 
1930 	if (strnlen(input, MAX_PARAM_LENGTH)) {
1931 		while ((target_config = strsep(&input, ";"))) {
1932 			nt = alloc_param_target(target_config, count);
1933 			if (IS_ERR(nt)) {
1934 				if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
1935 					continue;
1936 				err = PTR_ERR(nt);
1937 				goto fail;
1938 			}
1939 			/* Dump existing printks when we register */
1940 			if (nt->extended) {
1941 				console_type_needed |= CONS_EXTENDED;
1942 				netconsole_ext.flags |= CON_PRINTBUFFER;
1943 			} else {
1944 				console_type_needed |= CONS_BASIC;
1945 				netconsole.flags |= CON_PRINTBUFFER;
1946 			}
1947 
1948 			spin_lock_irqsave(&target_list_lock, flags);
1949 			list_add(&nt->list, &target_list);
1950 			spin_unlock_irqrestore(&target_list_lock, flags);
1951 			count++;
1952 		}
1953 	}
1954 
1955 	err = register_netdevice_notifier(&netconsole_netdev_notifier);
1956 	if (err)
1957 		goto fail;
1958 
1959 	err = dynamic_netconsole_init();
1960 	if (err)
1961 		goto undonotifier;
1962 
1963 	if (console_type_needed & CONS_EXTENDED)
1964 		register_console(&netconsole_ext);
1965 	if (console_type_needed & CONS_BASIC)
1966 		register_console(&netconsole);
1967 	pr_info("network logging started\n");
1968 
1969 	return err;
1970 
1971 undonotifier:
1972 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
1973 
1974 fail:
1975 	pr_err("cleaning up\n");
1976 
1977 	/*
1978 	 * Remove all targets and destroy them (only targets created
1979 	 * from the boot/module option exist here). Skipping the list
1980 	 * lock is safe here, and netpoll_cleanup() will sleep.
1981 	 */
1982 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
1983 		list_del(&nt->list);
1984 		free_param_target(nt);
1985 	}
1986 
1987 	return err;
1988 }
1989 
cleanup_netconsole(void)1990 static void __exit cleanup_netconsole(void)
1991 {
1992 	struct netconsole_target *nt, *tmp;
1993 
1994 	if (console_is_registered(&netconsole_ext))
1995 		unregister_console(&netconsole_ext);
1996 	if (console_is_registered(&netconsole))
1997 		unregister_console(&netconsole);
1998 	dynamic_netconsole_exit();
1999 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
2000 
2001 	/*
2002 	 * Targets created via configfs pin references on our module
2003 	 * and would first be rmdir(2)'ed from userspace. We reach
2004 	 * here only when they are already destroyed, and only those
2005 	 * created from the boot/module option are left, so remove and
2006 	 * destroy them. Skipping the list lock is safe here, and
2007 	 * netpoll_cleanup() will sleep.
2008 	 */
2009 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
2010 		list_del(&nt->list);
2011 		free_param_target(nt);
2012 	}
2013 }
2014 
2015 /*
2016  * Use late_initcall to ensure netconsole is
2017  * initialized after network device driver if built-in.
2018  *
2019  * late_initcall() and module_init() are identical if built as module.
2020  */
2021 late_initcall(init_netconsole);
2022 module_exit(cleanup_netconsole);
2023