xref: /linux/drivers/net/netconsole.c (revision 385ef48f468696d6d172eb367656a3466fa0408d)
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/utsname.h>
40 
41 MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
42 MODULE_DESCRIPTION("Console driver for network interfaces");
43 MODULE_LICENSE("GPL");
44 
45 #define MAX_PARAM_LENGTH	256
46 #define MAX_USERDATA_NAME_LENGTH	54
47 #define MAX_USERDATA_VALUE_LENGTH	200
48 #define MAX_USERDATA_ENTRY_LENGTH	256
49 #define MAX_USERDATA_ITEMS		16
50 #define MAX_PRINT_CHUNK		1000
51 
52 static char config[MAX_PARAM_LENGTH];
53 module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
54 MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
55 
56 static bool oops_only;
57 module_param(oops_only, bool, 0600);
58 MODULE_PARM_DESC(oops_only, "Only log oops messages");
59 
60 #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
61 
62 #ifndef	MODULE
63 static int __init option_setup(char *opt)
64 {
65 	strscpy(config, opt, MAX_PARAM_LENGTH);
66 	return 1;
67 }
68 __setup("netconsole=", option_setup);
69 #endif	/* MODULE */
70 
71 /* Linked list of all configured targets */
72 static LIST_HEAD(target_list);
73 
74 /* This needs to be a spinlock because write_msg() cannot sleep */
75 static DEFINE_SPINLOCK(target_list_lock);
76 
77 /*
78  * Console driver for extended netconsoles.  Registered on the first use to
79  * avoid unnecessarily enabling ext message formatting.
80  */
81 static struct console netconsole_ext;
82 
83 /**
84  * struct netconsole_target - Represents a configured netconsole target.
85  * @list:	Links this target into the target_list.
86  * @group:	Links us into the configfs subsystem hierarchy.
87  * @userdata_group:	Links to the userdata configfs hierarchy
88  * @userdata_complete:	Cached, formatted string of append
89  * @userdata_length:	String length of userdata_complete
90  * @enabled:	On / off knob to enable / disable target.
91  *		Visible from userspace (read-write).
92  *		We maintain a strict 1:1 correspondence between this and
93  *		whether the corresponding netpoll is active or inactive.
94  *		Also, other parameters of a target may be modified at
95  *		runtime only when it is disabled (enabled == 0).
96  * @extended:	Denotes whether console is extended or not.
97  * @release:	Denotes whether kernel release version should be prepended
98  *		to the message. Depends on extended console.
99  * @np:		The netpoll structure for this target.
100  *		Contains the other userspace visible parameters:
101  *		dev_name	(read-write)
102  *		local_port	(read-write)
103  *		remote_port	(read-write)
104  *		local_ip	(read-write)
105  *		remote_ip	(read-write)
106  *		local_mac	(read-only)
107  *		remote_mac	(read-write)
108  */
109 struct netconsole_target {
110 	struct list_head	list;
111 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
112 	struct config_group	group;
113 	struct config_group	userdata_group;
114 	char userdata_complete[MAX_USERDATA_ENTRY_LENGTH * MAX_USERDATA_ITEMS];
115 	size_t			userdata_length;
116 #endif
117 	bool			enabled;
118 	bool			extended;
119 	bool			release;
120 	struct netpoll		np;
121 };
122 
123 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
124 
125 static struct configfs_subsystem netconsole_subsys;
126 static DEFINE_MUTEX(dynamic_netconsole_mutex);
127 
128 static int __init dynamic_netconsole_init(void)
129 {
130 	config_group_init(&netconsole_subsys.su_group);
131 	mutex_init(&netconsole_subsys.su_mutex);
132 	return configfs_register_subsystem(&netconsole_subsys);
133 }
134 
135 static void __exit dynamic_netconsole_exit(void)
136 {
137 	configfs_unregister_subsystem(&netconsole_subsys);
138 }
139 
140 /*
141  * Targets that were created by parsing the boot/module option string
142  * do not exist in the configfs hierarchy (and have NULL names) and will
143  * never go away, so make these a no-op for them.
144  */
145 static void netconsole_target_get(struct netconsole_target *nt)
146 {
147 	if (config_item_name(&nt->group.cg_item))
148 		config_group_get(&nt->group);
149 }
150 
151 static void netconsole_target_put(struct netconsole_target *nt)
152 {
153 	if (config_item_name(&nt->group.cg_item))
154 		config_group_put(&nt->group);
155 }
156 
157 #else	/* !CONFIG_NETCONSOLE_DYNAMIC */
158 
159 static int __init dynamic_netconsole_init(void)
160 {
161 	return 0;
162 }
163 
164 static void __exit dynamic_netconsole_exit(void)
165 {
166 }
167 
168 /*
169  * No danger of targets going away from under us when dynamic
170  * reconfigurability is off.
171  */
172 static void netconsole_target_get(struct netconsole_target *nt)
173 {
174 }
175 
176 static void netconsole_target_put(struct netconsole_target *nt)
177 {
178 }
179 
180 static void populate_configfs_item(struct netconsole_target *nt,
181 				   int cmdline_count)
182 {
183 }
184 #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
185 
186 /* Allocate and initialize with defaults.
187  * Note that these targets get their config_item fields zeroed-out.
188  */
189 static struct netconsole_target *alloc_and_init(void)
190 {
191 	struct netconsole_target *nt;
192 
193 	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
194 	if (!nt)
195 		return nt;
196 
197 	if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
198 		nt->extended = true;
199 	if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
200 		nt->release = true;
201 
202 	nt->np.name = "netconsole";
203 	strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
204 	nt->np.local_port = 6665;
205 	nt->np.remote_port = 6666;
206 	eth_broadcast_addr(nt->np.remote_mac);
207 
208 	return nt;
209 }
210 
211 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
212 
213 /*
214  * Our subsystem hierarchy is:
215  *
216  * /sys/kernel/config/netconsole/
217  *				|
218  *				<target>/
219  *				|	enabled
220  *				|	release
221  *				|	dev_name
222  *				|	local_port
223  *				|	remote_port
224  *				|	local_ip
225  *				|	remote_ip
226  *				|	local_mac
227  *				|	remote_mac
228  *				|	userdata/
229  *				|		<key>/
230  *				|			value
231  *				|		...
232  *				|
233  *				<target>/...
234  */
235 
236 static struct netconsole_target *to_target(struct config_item *item)
237 {
238 	struct config_group *cfg_group;
239 
240 	cfg_group = to_config_group(item);
241 	if (!cfg_group)
242 		return NULL;
243 	return container_of(to_config_group(item),
244 			    struct netconsole_target, group);
245 }
246 
247 /* Get rid of possible trailing newline, returning the new length */
248 static void trim_newline(char *s, size_t maxlen)
249 {
250 	size_t len;
251 
252 	len = strnlen(s, maxlen);
253 	if (s[len - 1] == '\n')
254 		s[len - 1] = '\0';
255 }
256 
257 /*
258  * Attribute operations for netconsole_target.
259  */
260 
261 static ssize_t enabled_show(struct config_item *item, char *buf)
262 {
263 	return sysfs_emit(buf, "%d\n", to_target(item)->enabled);
264 }
265 
266 static ssize_t extended_show(struct config_item *item, char *buf)
267 {
268 	return sysfs_emit(buf, "%d\n", to_target(item)->extended);
269 }
270 
271 static ssize_t release_show(struct config_item *item, char *buf)
272 {
273 	return sysfs_emit(buf, "%d\n", to_target(item)->release);
274 }
275 
276 static ssize_t dev_name_show(struct config_item *item, char *buf)
277 {
278 	return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
279 }
280 
281 static ssize_t local_port_show(struct config_item *item, char *buf)
282 {
283 	return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);
284 }
285 
286 static ssize_t remote_port_show(struct config_item *item, char *buf)
287 {
288 	return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);
289 }
290 
291 static ssize_t local_ip_show(struct config_item *item, char *buf)
292 {
293 	struct netconsole_target *nt = to_target(item);
294 
295 	if (nt->np.ipv6)
296 		return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);
297 	else
298 		return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);
299 }
300 
301 static ssize_t remote_ip_show(struct config_item *item, char *buf)
302 {
303 	struct netconsole_target *nt = to_target(item);
304 
305 	if (nt->np.ipv6)
306 		return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);
307 	else
308 		return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);
309 }
310 
311 static ssize_t local_mac_show(struct config_item *item, char *buf)
312 {
313 	struct net_device *dev = to_target(item)->np.dev;
314 	static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
315 
316 	return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
317 }
318 
319 static ssize_t remote_mac_show(struct config_item *item, char *buf)
320 {
321 	return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);
322 }
323 
324 /*
325  * This one is special -- targets created through the configfs interface
326  * are not enabled (and the corresponding netpoll activated) by default.
327  * The user is expected to set the desired parameters first (which
328  * would enable him to dynamically add new netpoll targets for new
329  * network interfaces as and when they come up).
330  */
331 static ssize_t enabled_store(struct config_item *item,
332 		const char *buf, size_t count)
333 {
334 	struct netconsole_target *nt = to_target(item);
335 	unsigned long flags;
336 	bool enabled;
337 	int err;
338 
339 	mutex_lock(&dynamic_netconsole_mutex);
340 	err = kstrtobool(buf, &enabled);
341 	if (err)
342 		goto out_unlock;
343 
344 	err = -EINVAL;
345 	if ((bool)enabled == nt->enabled) {
346 		pr_info("network logging has already %s\n",
347 			nt->enabled ? "started" : "stopped");
348 		goto out_unlock;
349 	}
350 
351 	if (enabled) {	/* true */
352 		if (nt->release && !nt->extended) {
353 			pr_err("Not enabling netconsole. Release feature requires extended log message");
354 			goto out_unlock;
355 		}
356 
357 		if (nt->extended && !console_is_registered(&netconsole_ext))
358 			register_console(&netconsole_ext);
359 
360 		/*
361 		 * Skip netpoll_parse_options() -- all the attributes are
362 		 * already configured via configfs. Just print them out.
363 		 */
364 		netpoll_print_options(&nt->np);
365 
366 		err = netpoll_setup(&nt->np);
367 		if (err)
368 			goto out_unlock;
369 
370 		pr_info("network logging started\n");
371 	} else {	/* false */
372 		/* We need to disable the netconsole before cleaning it up
373 		 * otherwise we might end up in write_msg() with
374 		 * nt->np.dev == NULL and nt->enabled == true
375 		 */
376 		spin_lock_irqsave(&target_list_lock, flags);
377 		nt->enabled = false;
378 		spin_unlock_irqrestore(&target_list_lock, flags);
379 		netpoll_cleanup(&nt->np);
380 	}
381 
382 	nt->enabled = enabled;
383 
384 	mutex_unlock(&dynamic_netconsole_mutex);
385 	return strnlen(buf, count);
386 out_unlock:
387 	mutex_unlock(&dynamic_netconsole_mutex);
388 	return err;
389 }
390 
391 static ssize_t release_store(struct config_item *item, const char *buf,
392 			     size_t count)
393 {
394 	struct netconsole_target *nt = to_target(item);
395 	bool release;
396 	int err;
397 
398 	mutex_lock(&dynamic_netconsole_mutex);
399 	if (nt->enabled) {
400 		pr_err("target (%s) is enabled, disable to update parameters\n",
401 		       config_item_name(&nt->group.cg_item));
402 		err = -EINVAL;
403 		goto out_unlock;
404 	}
405 
406 	err = kstrtobool(buf, &release);
407 	if (err)
408 		goto out_unlock;
409 
410 	nt->release = release;
411 
412 	mutex_unlock(&dynamic_netconsole_mutex);
413 	return strnlen(buf, count);
414 out_unlock:
415 	mutex_unlock(&dynamic_netconsole_mutex);
416 	return err;
417 }
418 
419 static ssize_t extended_store(struct config_item *item, const char *buf,
420 		size_t count)
421 {
422 	struct netconsole_target *nt = to_target(item);
423 	bool extended;
424 	int err;
425 
426 	mutex_lock(&dynamic_netconsole_mutex);
427 	if (nt->enabled) {
428 		pr_err("target (%s) is enabled, disable to update parameters\n",
429 		       config_item_name(&nt->group.cg_item));
430 		err = -EINVAL;
431 		goto out_unlock;
432 	}
433 
434 	err = kstrtobool(buf, &extended);
435 	if (err)
436 		goto out_unlock;
437 
438 	nt->extended = extended;
439 
440 	mutex_unlock(&dynamic_netconsole_mutex);
441 	return strnlen(buf, count);
442 out_unlock:
443 	mutex_unlock(&dynamic_netconsole_mutex);
444 	return err;
445 }
446 
447 static ssize_t dev_name_store(struct config_item *item, const char *buf,
448 		size_t count)
449 {
450 	struct netconsole_target *nt = to_target(item);
451 
452 	mutex_lock(&dynamic_netconsole_mutex);
453 	if (nt->enabled) {
454 		pr_err("target (%s) is enabled, disable to update parameters\n",
455 		       config_item_name(&nt->group.cg_item));
456 		mutex_unlock(&dynamic_netconsole_mutex);
457 		return -EINVAL;
458 	}
459 
460 	strscpy(nt->np.dev_name, buf, IFNAMSIZ);
461 	trim_newline(nt->np.dev_name, IFNAMSIZ);
462 
463 	mutex_unlock(&dynamic_netconsole_mutex);
464 	return strnlen(buf, count);
465 }
466 
467 static ssize_t local_port_store(struct config_item *item, const char *buf,
468 		size_t count)
469 {
470 	struct netconsole_target *nt = to_target(item);
471 	int rv = -EINVAL;
472 
473 	mutex_lock(&dynamic_netconsole_mutex);
474 	if (nt->enabled) {
475 		pr_err("target (%s) is enabled, disable to update parameters\n",
476 		       config_item_name(&nt->group.cg_item));
477 		goto out_unlock;
478 	}
479 
480 	rv = kstrtou16(buf, 10, &nt->np.local_port);
481 	if (rv < 0)
482 		goto out_unlock;
483 	mutex_unlock(&dynamic_netconsole_mutex);
484 	return strnlen(buf, count);
485 out_unlock:
486 	mutex_unlock(&dynamic_netconsole_mutex);
487 	return rv;
488 }
489 
490 static ssize_t remote_port_store(struct config_item *item,
491 		const char *buf, size_t count)
492 {
493 	struct netconsole_target *nt = to_target(item);
494 	int rv = -EINVAL;
495 
496 	mutex_lock(&dynamic_netconsole_mutex);
497 	if (nt->enabled) {
498 		pr_err("target (%s) is enabled, disable to update parameters\n",
499 		       config_item_name(&nt->group.cg_item));
500 		goto out_unlock;
501 	}
502 
503 	rv = kstrtou16(buf, 10, &nt->np.remote_port);
504 	if (rv < 0)
505 		goto out_unlock;
506 	mutex_unlock(&dynamic_netconsole_mutex);
507 	return strnlen(buf, count);
508 out_unlock:
509 	mutex_unlock(&dynamic_netconsole_mutex);
510 	return rv;
511 }
512 
513 static ssize_t local_ip_store(struct config_item *item, const char *buf,
514 		size_t count)
515 {
516 	struct netconsole_target *nt = to_target(item);
517 
518 	mutex_lock(&dynamic_netconsole_mutex);
519 	if (nt->enabled) {
520 		pr_err("target (%s) is enabled, disable to update parameters\n",
521 		       config_item_name(&nt->group.cg_item));
522 		goto out_unlock;
523 	}
524 
525 	if (strnchr(buf, count, ':')) {
526 		const char *end;
527 
528 		if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
529 			if (*end && *end != '\n') {
530 				pr_err("invalid IPv6 address at: <%c>\n", *end);
531 				goto out_unlock;
532 			}
533 			nt->np.ipv6 = true;
534 		} else
535 			goto out_unlock;
536 	} else {
537 		if (!nt->np.ipv6)
538 			nt->np.local_ip.ip = in_aton(buf);
539 		else
540 			goto out_unlock;
541 	}
542 
543 	mutex_unlock(&dynamic_netconsole_mutex);
544 	return strnlen(buf, count);
545 out_unlock:
546 	mutex_unlock(&dynamic_netconsole_mutex);
547 	return -EINVAL;
548 }
549 
550 static ssize_t remote_ip_store(struct config_item *item, const char *buf,
551 	       size_t count)
552 {
553 	struct netconsole_target *nt = to_target(item);
554 
555 	mutex_lock(&dynamic_netconsole_mutex);
556 	if (nt->enabled) {
557 		pr_err("target (%s) is enabled, disable to update parameters\n",
558 		       config_item_name(&nt->group.cg_item));
559 		goto out_unlock;
560 	}
561 
562 	if (strnchr(buf, count, ':')) {
563 		const char *end;
564 
565 		if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
566 			if (*end && *end != '\n') {
567 				pr_err("invalid IPv6 address at: <%c>\n", *end);
568 				goto out_unlock;
569 			}
570 			nt->np.ipv6 = true;
571 		} else
572 			goto out_unlock;
573 	} else {
574 		if (!nt->np.ipv6)
575 			nt->np.remote_ip.ip = in_aton(buf);
576 		else
577 			goto out_unlock;
578 	}
579 
580 	mutex_unlock(&dynamic_netconsole_mutex);
581 	return strnlen(buf, count);
582 out_unlock:
583 	mutex_unlock(&dynamic_netconsole_mutex);
584 	return -EINVAL;
585 }
586 
587 static ssize_t remote_mac_store(struct config_item *item, const char *buf,
588 		size_t count)
589 {
590 	struct netconsole_target *nt = to_target(item);
591 	u8 remote_mac[ETH_ALEN];
592 
593 	mutex_lock(&dynamic_netconsole_mutex);
594 	if (nt->enabled) {
595 		pr_err("target (%s) is enabled, disable to update parameters\n",
596 		       config_item_name(&nt->group.cg_item));
597 		goto out_unlock;
598 	}
599 
600 	if (!mac_pton(buf, remote_mac))
601 		goto out_unlock;
602 	if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
603 		goto out_unlock;
604 	memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
605 
606 	mutex_unlock(&dynamic_netconsole_mutex);
607 	return strnlen(buf, count);
608 out_unlock:
609 	mutex_unlock(&dynamic_netconsole_mutex);
610 	return -EINVAL;
611 }
612 
613 struct userdatum {
614 	struct config_item item;
615 	char value[MAX_USERDATA_VALUE_LENGTH];
616 };
617 
618 static struct userdatum *to_userdatum(struct config_item *item)
619 {
620 	return container_of(item, struct userdatum, item);
621 }
622 
623 struct userdata {
624 	struct config_group group;
625 };
626 
627 static struct userdata *to_userdata(struct config_item *item)
628 {
629 	return container_of(to_config_group(item), struct userdata, group);
630 }
631 
632 static struct netconsole_target *userdata_to_target(struct userdata *ud)
633 {
634 	struct config_group *netconsole_group;
635 
636 	netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
637 	return to_target(&netconsole_group->cg_item);
638 }
639 
640 static ssize_t userdatum_value_show(struct config_item *item, char *buf)
641 {
642 	return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
643 }
644 
645 static void update_userdata(struct netconsole_target *nt)
646 {
647 	int complete_idx = 0, child_count = 0;
648 	struct list_head *entry;
649 
650 	/* Clear the current string in case the last userdatum was deleted */
651 	nt->userdata_length = 0;
652 	nt->userdata_complete[0] = 0;
653 
654 	list_for_each(entry, &nt->userdata_group.cg_children) {
655 		struct userdatum *udm_item;
656 		struct config_item *item;
657 
658 		if (child_count >= MAX_USERDATA_ITEMS)
659 			break;
660 		child_count++;
661 
662 		item = container_of(entry, struct config_item, ci_entry);
663 		udm_item = to_userdatum(item);
664 
665 		/* Skip userdata with no value set */
666 		if (strnlen(udm_item->value, MAX_USERDATA_VALUE_LENGTH) == 0)
667 			continue;
668 
669 		/* This doesn't overflow userdata_complete since it will write
670 		 * one entry length (1/MAX_USERDATA_ITEMS long), entry count is
671 		 * checked to not exceed MAX items with child_count above
672 		 */
673 		complete_idx += scnprintf(&nt->userdata_complete[complete_idx],
674 					  MAX_USERDATA_ENTRY_LENGTH, "%s=%s\n",
675 					  item->ci_name, udm_item->value);
676 	}
677 	nt->userdata_length = strnlen(nt->userdata_complete,
678 				      sizeof(nt->userdata_complete));
679 }
680 
681 static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
682 				     size_t count)
683 {
684 	struct userdatum *udm = to_userdatum(item);
685 	struct netconsole_target *nt;
686 	struct userdata *ud;
687 	int ret;
688 
689 	if (count > MAX_USERDATA_VALUE_LENGTH)
690 		return -EMSGSIZE;
691 
692 	mutex_lock(&dynamic_netconsole_mutex);
693 
694 	ret = strscpy(udm->value, buf, sizeof(udm->value));
695 	if (ret < 0)
696 		goto out_unlock;
697 	trim_newline(udm->value, sizeof(udm->value));
698 
699 	ud = to_userdata(item->ci_parent);
700 	nt = userdata_to_target(ud);
701 	update_userdata(nt);
702 
703 	mutex_unlock(&dynamic_netconsole_mutex);
704 	return count;
705 out_unlock:
706 	mutex_unlock(&dynamic_netconsole_mutex);
707 	return ret;
708 }
709 
710 CONFIGFS_ATTR(userdatum_, value);
711 
712 static struct configfs_attribute *userdatum_attrs[] = {
713 	&userdatum_attr_value,
714 	NULL,
715 };
716 
717 static void userdatum_release(struct config_item *item)
718 {
719 	kfree(to_userdatum(item));
720 }
721 
722 static struct configfs_item_operations userdatum_ops = {
723 	.release = userdatum_release,
724 };
725 
726 static const struct config_item_type userdatum_type = {
727 	.ct_item_ops	= &userdatum_ops,
728 	.ct_attrs	= userdatum_attrs,
729 	.ct_owner	= THIS_MODULE,
730 };
731 
732 static struct config_item *userdatum_make_item(struct config_group *group,
733 					       const char *name)
734 {
735 	struct netconsole_target *nt;
736 	struct userdatum *udm;
737 	struct userdata *ud;
738 	size_t child_count;
739 
740 	if (strlen(name) > MAX_USERDATA_NAME_LENGTH)
741 		return ERR_PTR(-ENAMETOOLONG);
742 
743 	ud = to_userdata(&group->cg_item);
744 	nt = userdata_to_target(ud);
745 	child_count = list_count_nodes(&nt->userdata_group.cg_children);
746 	if (child_count >= MAX_USERDATA_ITEMS)
747 		return ERR_PTR(-ENOSPC);
748 
749 	udm = kzalloc(sizeof(*udm), GFP_KERNEL);
750 	if (!udm)
751 		return ERR_PTR(-ENOMEM);
752 
753 	config_item_init_type_name(&udm->item, name, &userdatum_type);
754 	return &udm->item;
755 }
756 
757 static void userdatum_drop(struct config_group *group, struct config_item *item)
758 {
759 	struct netconsole_target *nt;
760 	struct userdata *ud;
761 
762 	ud = to_userdata(&group->cg_item);
763 	nt = userdata_to_target(ud);
764 
765 	mutex_lock(&dynamic_netconsole_mutex);
766 	update_userdata(nt);
767 	config_item_put(item);
768 	mutex_unlock(&dynamic_netconsole_mutex);
769 }
770 
771 static struct configfs_attribute *userdata_attrs[] = {
772 	NULL,
773 };
774 
775 static struct configfs_group_operations userdata_ops = {
776 	.make_item		= userdatum_make_item,
777 	.drop_item		= userdatum_drop,
778 };
779 
780 static struct config_item_type userdata_type = {
781 	.ct_item_ops	= &userdatum_ops,
782 	.ct_group_ops	= &userdata_ops,
783 	.ct_attrs	= userdata_attrs,
784 	.ct_owner	= THIS_MODULE,
785 };
786 
787 CONFIGFS_ATTR(, enabled);
788 CONFIGFS_ATTR(, extended);
789 CONFIGFS_ATTR(, dev_name);
790 CONFIGFS_ATTR(, local_port);
791 CONFIGFS_ATTR(, remote_port);
792 CONFIGFS_ATTR(, local_ip);
793 CONFIGFS_ATTR(, remote_ip);
794 CONFIGFS_ATTR_RO(, local_mac);
795 CONFIGFS_ATTR(, remote_mac);
796 CONFIGFS_ATTR(, release);
797 
798 static struct configfs_attribute *netconsole_target_attrs[] = {
799 	&attr_enabled,
800 	&attr_extended,
801 	&attr_release,
802 	&attr_dev_name,
803 	&attr_local_port,
804 	&attr_remote_port,
805 	&attr_local_ip,
806 	&attr_remote_ip,
807 	&attr_local_mac,
808 	&attr_remote_mac,
809 	NULL,
810 };
811 
812 /*
813  * Item operations and type for netconsole_target.
814  */
815 
816 static void netconsole_target_release(struct config_item *item)
817 {
818 	kfree(to_target(item));
819 }
820 
821 static struct configfs_item_operations netconsole_target_item_ops = {
822 	.release		= netconsole_target_release,
823 };
824 
825 static const struct config_item_type netconsole_target_type = {
826 	.ct_attrs		= netconsole_target_attrs,
827 	.ct_item_ops		= &netconsole_target_item_ops,
828 	.ct_owner		= THIS_MODULE,
829 };
830 
831 static void init_target_config_group(struct netconsole_target *nt,
832 				     const char *name)
833 {
834 	config_group_init_type_name(&nt->group, name, &netconsole_target_type);
835 	config_group_init_type_name(&nt->userdata_group, "userdata",
836 				    &userdata_type);
837 	configfs_add_default_group(&nt->userdata_group, &nt->group);
838 }
839 
840 static struct netconsole_target *find_cmdline_target(const char *name)
841 {
842 	struct netconsole_target *nt, *ret = NULL;
843 	unsigned long flags;
844 
845 	spin_lock_irqsave(&target_list_lock, flags);
846 	list_for_each_entry(nt, &target_list, list) {
847 		if (!strcmp(nt->group.cg_item.ci_name, name)) {
848 			ret = nt;
849 			break;
850 		}
851 	}
852 	spin_unlock_irqrestore(&target_list_lock, flags);
853 
854 	return ret;
855 }
856 
857 /*
858  * Group operations and type for netconsole_subsys.
859  */
860 
861 static struct config_group *make_netconsole_target(struct config_group *group,
862 						   const char *name)
863 {
864 	struct netconsole_target *nt;
865 	unsigned long flags;
866 
867 	/* Checking if a target by this name was created at boot time.  If so,
868 	 * attach a configfs entry to that target.  This enables dynamic
869 	 * control.
870 	 */
871 	if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
872 		     strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
873 		nt = find_cmdline_target(name);
874 		if (nt) {
875 			init_target_config_group(nt, name);
876 			return &nt->group;
877 		}
878 	}
879 
880 	nt = alloc_and_init();
881 	if (!nt)
882 		return ERR_PTR(-ENOMEM);
883 
884 	/* Initialize the config_group member */
885 	init_target_config_group(nt, name);
886 
887 	/* Adding, but it is disabled */
888 	spin_lock_irqsave(&target_list_lock, flags);
889 	list_add(&nt->list, &target_list);
890 	spin_unlock_irqrestore(&target_list_lock, flags);
891 
892 	return &nt->group;
893 }
894 
895 static void drop_netconsole_target(struct config_group *group,
896 				   struct config_item *item)
897 {
898 	unsigned long flags;
899 	struct netconsole_target *nt = to_target(item);
900 
901 	spin_lock_irqsave(&target_list_lock, flags);
902 	list_del(&nt->list);
903 	spin_unlock_irqrestore(&target_list_lock, flags);
904 
905 	/*
906 	 * The target may have never been enabled, or was manually disabled
907 	 * before being removed so netpoll may have already been cleaned up.
908 	 */
909 	if (nt->enabled)
910 		netpoll_cleanup(&nt->np);
911 
912 	config_item_put(&nt->group.cg_item);
913 }
914 
915 static struct configfs_group_operations netconsole_subsys_group_ops = {
916 	.make_group	= make_netconsole_target,
917 	.drop_item	= drop_netconsole_target,
918 };
919 
920 static const struct config_item_type netconsole_subsys_type = {
921 	.ct_group_ops	= &netconsole_subsys_group_ops,
922 	.ct_owner	= THIS_MODULE,
923 };
924 
925 /* The netconsole configfs subsystem */
926 static struct configfs_subsystem netconsole_subsys = {
927 	.su_group	= {
928 		.cg_item	= {
929 			.ci_namebuf	= "netconsole",
930 			.ci_type	= &netconsole_subsys_type,
931 		},
932 	},
933 };
934 
935 static void populate_configfs_item(struct netconsole_target *nt,
936 				   int cmdline_count)
937 {
938 	char target_name[16];
939 
940 	snprintf(target_name, sizeof(target_name), "%s%d",
941 		 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
942 	init_target_config_group(nt, target_name);
943 }
944 
945 #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
946 
947 /* Handle network interface device notifications */
948 static int netconsole_netdev_event(struct notifier_block *this,
949 				   unsigned long event, void *ptr)
950 {
951 	unsigned long flags;
952 	struct netconsole_target *nt;
953 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
954 	bool stopped = false;
955 
956 	if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
957 	      event == NETDEV_RELEASE || event == NETDEV_JOIN))
958 		goto done;
959 
960 	spin_lock_irqsave(&target_list_lock, flags);
961 restart:
962 	list_for_each_entry(nt, &target_list, list) {
963 		netconsole_target_get(nt);
964 		if (nt->np.dev == dev) {
965 			switch (event) {
966 			case NETDEV_CHANGENAME:
967 				strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
968 				break;
969 			case NETDEV_RELEASE:
970 			case NETDEV_JOIN:
971 			case NETDEV_UNREGISTER:
972 				/* rtnl_lock already held
973 				 * we might sleep in __netpoll_cleanup()
974 				 */
975 				spin_unlock_irqrestore(&target_list_lock, flags);
976 
977 				__netpoll_cleanup(&nt->np);
978 
979 				spin_lock_irqsave(&target_list_lock, flags);
980 				netdev_put(nt->np.dev, &nt->np.dev_tracker);
981 				nt->np.dev = NULL;
982 				nt->enabled = false;
983 				stopped = true;
984 				netconsole_target_put(nt);
985 				goto restart;
986 			}
987 		}
988 		netconsole_target_put(nt);
989 	}
990 	spin_unlock_irqrestore(&target_list_lock, flags);
991 	if (stopped) {
992 		const char *msg = "had an event";
993 
994 		switch (event) {
995 		case NETDEV_UNREGISTER:
996 			msg = "unregistered";
997 			break;
998 		case NETDEV_RELEASE:
999 			msg = "released slaves";
1000 			break;
1001 		case NETDEV_JOIN:
1002 			msg = "is joining a master device";
1003 			break;
1004 		}
1005 		pr_info("network logging stopped on interface %s as it %s\n",
1006 			dev->name, msg);
1007 	}
1008 
1009 done:
1010 	return NOTIFY_DONE;
1011 }
1012 
1013 static struct notifier_block netconsole_netdev_notifier = {
1014 	.notifier_call  = netconsole_netdev_event,
1015 };
1016 
1017 /**
1018  * send_ext_msg_udp - send extended log message to target
1019  * @nt: target to send message to
1020  * @msg: extended log message to send
1021  * @msg_len: length of message
1022  *
1023  * Transfer extended log @msg to @nt.  If @msg is longer than
1024  * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
1025  * ncfrag header field added to identify them.
1026  */
1027 static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
1028 			     int msg_len)
1029 {
1030 	static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
1031 	const char *header, *body;
1032 	int offset = 0;
1033 	int header_len, body_len;
1034 	const char *msg_ready = msg;
1035 	const char *release;
1036 	int release_len = 0;
1037 	int userdata_len = 0;
1038 	char *userdata = NULL;
1039 
1040 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1041 	userdata = nt->userdata_complete;
1042 	userdata_len = nt->userdata_length;
1043 #endif
1044 
1045 	if (nt->release) {
1046 		release = init_utsname()->release;
1047 		release_len = strlen(release) + 1;
1048 	}
1049 
1050 	if (msg_len + release_len + userdata_len <= MAX_PRINT_CHUNK) {
1051 		/* No fragmentation needed */
1052 		if (nt->release) {
1053 			scnprintf(buf, MAX_PRINT_CHUNK, "%s,%s", release, msg);
1054 			msg_len += release_len;
1055 		} else {
1056 			memcpy(buf, msg, msg_len);
1057 		}
1058 
1059 		if (userdata)
1060 			msg_len += scnprintf(&buf[msg_len],
1061 					     MAX_PRINT_CHUNK - msg_len,
1062 					     "%s", userdata);
1063 
1064 		msg_ready = buf;
1065 		netpoll_send_udp(&nt->np, msg_ready, msg_len);
1066 		return;
1067 	}
1068 
1069 	/* need to insert extra header fields, detect header and body */
1070 	header = msg;
1071 	body = memchr(msg, ';', msg_len);
1072 	if (WARN_ON_ONCE(!body))
1073 		return;
1074 
1075 	header_len = body - header;
1076 	body_len = msg_len - header_len - 1;
1077 	body++;
1078 
1079 	/*
1080 	 * Transfer multiple chunks with the following extra header.
1081 	 * "ncfrag=<byte-offset>/<total-bytes>"
1082 	 */
1083 	if (nt->release)
1084 		scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
1085 	memcpy(buf + release_len, header, header_len);
1086 	header_len += release_len;
1087 
1088 	while (offset < body_len + userdata_len) {
1089 		int this_header = header_len;
1090 		int this_offset = 0;
1091 		int this_chunk = 0;
1092 
1093 		this_header += scnprintf(buf + this_header,
1094 					 sizeof(buf) - this_header,
1095 					 ",ncfrag=%d/%d;", offset,
1096 					 body_len + userdata_len);
1097 
1098 		/* Not all body data has been written yet */
1099 		if (offset < body_len) {
1100 			this_chunk = min(body_len - offset,
1101 					 MAX_PRINT_CHUNK - this_header);
1102 			if (WARN_ON_ONCE(this_chunk <= 0))
1103 				return;
1104 			memcpy(buf + this_header, body + offset, this_chunk);
1105 			this_offset += this_chunk;
1106 		}
1107 		/* Body is fully written and there is pending userdata to write,
1108 		 * append userdata in this chunk
1109 		 */
1110 		if (offset + this_offset >= body_len &&
1111 		    offset + this_offset < userdata_len + body_len) {
1112 			int sent_userdata = (offset + this_offset) - body_len;
1113 			int preceding_bytes = this_chunk + this_header;
1114 
1115 			if (WARN_ON_ONCE(sent_userdata < 0))
1116 				return;
1117 
1118 			this_chunk = min(userdata_len - sent_userdata,
1119 					 MAX_PRINT_CHUNK - preceding_bytes);
1120 			if (WARN_ON_ONCE(this_chunk <= 0))
1121 				return;
1122 			memcpy(buf + this_header + this_offset,
1123 			       userdata + sent_userdata,
1124 			       this_chunk);
1125 			this_offset += this_chunk;
1126 		}
1127 
1128 		netpoll_send_udp(&nt->np, buf, this_header + this_offset);
1129 		offset += this_offset;
1130 	}
1131 }
1132 
1133 static void write_ext_msg(struct console *con, const char *msg,
1134 			  unsigned int len)
1135 {
1136 	struct netconsole_target *nt;
1137 	unsigned long flags;
1138 
1139 	if ((oops_only && !oops_in_progress) || list_empty(&target_list))
1140 		return;
1141 
1142 	spin_lock_irqsave(&target_list_lock, flags);
1143 	list_for_each_entry(nt, &target_list, list)
1144 		if (nt->extended && nt->enabled && netif_running(nt->np.dev))
1145 			send_ext_msg_udp(nt, msg, len);
1146 	spin_unlock_irqrestore(&target_list_lock, flags);
1147 }
1148 
1149 static void write_msg(struct console *con, const char *msg, unsigned int len)
1150 {
1151 	int frag, left;
1152 	unsigned long flags;
1153 	struct netconsole_target *nt;
1154 	const char *tmp;
1155 
1156 	if (oops_only && !oops_in_progress)
1157 		return;
1158 	/* Avoid taking lock and disabling interrupts unnecessarily */
1159 	if (list_empty(&target_list))
1160 		return;
1161 
1162 	spin_lock_irqsave(&target_list_lock, flags);
1163 	list_for_each_entry(nt, &target_list, list) {
1164 		if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
1165 			/*
1166 			 * We nest this inside the for-each-target loop above
1167 			 * so that we're able to get as much logging out to
1168 			 * at least one target if we die inside here, instead
1169 			 * of unnecessarily keeping all targets in lock-step.
1170 			 */
1171 			tmp = msg;
1172 			for (left = len; left;) {
1173 				frag = min(left, MAX_PRINT_CHUNK);
1174 				netpoll_send_udp(&nt->np, tmp, frag);
1175 				tmp += frag;
1176 				left -= frag;
1177 			}
1178 		}
1179 	}
1180 	spin_unlock_irqrestore(&target_list_lock, flags);
1181 }
1182 
1183 /* Allocate new target (from boot/module param) and setup netpoll for it */
1184 static struct netconsole_target *alloc_param_target(char *target_config,
1185 						    int cmdline_count)
1186 {
1187 	struct netconsole_target *nt;
1188 	int err;
1189 
1190 	nt = alloc_and_init();
1191 	if (!nt) {
1192 		err = -ENOMEM;
1193 		goto fail;
1194 	}
1195 
1196 	if (*target_config == '+') {
1197 		nt->extended = true;
1198 		target_config++;
1199 	}
1200 
1201 	if (*target_config == 'r') {
1202 		if (!nt->extended) {
1203 			pr_err("Netconsole configuration error. Release feature requires extended log message");
1204 			err = -EINVAL;
1205 			goto fail;
1206 		}
1207 		nt->release = true;
1208 		target_config++;
1209 	}
1210 
1211 	/* Parse parameters and setup netpoll */
1212 	err = netpoll_parse_options(&nt->np, target_config);
1213 	if (err)
1214 		goto fail;
1215 
1216 	err = netpoll_setup(&nt->np);
1217 	if (err)
1218 		goto fail;
1219 
1220 	populate_configfs_item(nt, cmdline_count);
1221 	nt->enabled = true;
1222 
1223 	return nt;
1224 
1225 fail:
1226 	kfree(nt);
1227 	return ERR_PTR(err);
1228 }
1229 
1230 /* Cleanup netpoll for given target (from boot/module param) and free it */
1231 static void free_param_target(struct netconsole_target *nt)
1232 {
1233 	netpoll_cleanup(&nt->np);
1234 	kfree(nt);
1235 }
1236 
1237 static struct console netconsole_ext = {
1238 	.name	= "netcon_ext",
1239 	.flags	= CON_ENABLED | CON_EXTENDED,
1240 	.write	= write_ext_msg,
1241 };
1242 
1243 static struct console netconsole = {
1244 	.name	= "netcon",
1245 	.flags	= CON_ENABLED,
1246 	.write	= write_msg,
1247 };
1248 
1249 static int __init init_netconsole(void)
1250 {
1251 	int err;
1252 	struct netconsole_target *nt, *tmp;
1253 	unsigned int count = 0;
1254 	bool extended = false;
1255 	unsigned long flags;
1256 	char *target_config;
1257 	char *input = config;
1258 
1259 	if (strnlen(input, MAX_PARAM_LENGTH)) {
1260 		while ((target_config = strsep(&input, ";"))) {
1261 			nt = alloc_param_target(target_config, count);
1262 			if (IS_ERR(nt)) {
1263 				err = PTR_ERR(nt);
1264 				goto fail;
1265 			}
1266 			/* Dump existing printks when we register */
1267 			if (nt->extended) {
1268 				extended = true;
1269 				netconsole_ext.flags |= CON_PRINTBUFFER;
1270 			} else {
1271 				netconsole.flags |= CON_PRINTBUFFER;
1272 			}
1273 
1274 			spin_lock_irqsave(&target_list_lock, flags);
1275 			list_add(&nt->list, &target_list);
1276 			spin_unlock_irqrestore(&target_list_lock, flags);
1277 			count++;
1278 		}
1279 	}
1280 
1281 	err = register_netdevice_notifier(&netconsole_netdev_notifier);
1282 	if (err)
1283 		goto fail;
1284 
1285 	err = dynamic_netconsole_init();
1286 	if (err)
1287 		goto undonotifier;
1288 
1289 	if (extended)
1290 		register_console(&netconsole_ext);
1291 	register_console(&netconsole);
1292 	pr_info("network logging started\n");
1293 
1294 	return err;
1295 
1296 undonotifier:
1297 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
1298 
1299 fail:
1300 	pr_err("cleaning up\n");
1301 
1302 	/*
1303 	 * Remove all targets and destroy them (only targets created
1304 	 * from the boot/module option exist here). Skipping the list
1305 	 * lock is safe here, and netpoll_cleanup() will sleep.
1306 	 */
1307 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
1308 		list_del(&nt->list);
1309 		free_param_target(nt);
1310 	}
1311 
1312 	return err;
1313 }
1314 
1315 static void __exit cleanup_netconsole(void)
1316 {
1317 	struct netconsole_target *nt, *tmp;
1318 
1319 	if (console_is_registered(&netconsole_ext))
1320 		unregister_console(&netconsole_ext);
1321 	unregister_console(&netconsole);
1322 	dynamic_netconsole_exit();
1323 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
1324 
1325 	/*
1326 	 * Targets created via configfs pin references on our module
1327 	 * and would first be rmdir(2)'ed from userspace. We reach
1328 	 * here only when they are already destroyed, and only those
1329 	 * created from the boot/module option are left, so remove and
1330 	 * destroy them. Skipping the list lock is safe here, and
1331 	 * netpoll_cleanup() will sleep.
1332 	 */
1333 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
1334 		list_del(&nt->list);
1335 		free_param_target(nt);
1336 	}
1337 }
1338 
1339 /*
1340  * Use late_initcall to ensure netconsole is
1341  * initialized after network device driver if built-in.
1342  *
1343  * late_initcall() and module_init() are identical if built as module.
1344  */
1345 late_initcall(init_netconsole);
1346 module_exit(cleanup_netconsole);
1347