1472 lines · 37.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* *  linux/drivers/net/netconsole.c * *  Copyright (C) 2001  Ingo Molnar <mingo@redhat.com> * *  This file contains the implementation of an IRQ-safe, crash-safe *  kernel console implementation that outputs kernel messages to the *  network. * * Modification history: * * 2001-09-17    started by Ingo Molnar. * 2003-08-11    2.6 port by Matt Mackall *               simplified options *               generic card hooks *               works non-modular * 2003-09-07    rewritten with netpoll api */ /**************************************************************** * ****************************************************************/ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/mm.h>#include <linux/init.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/console.h>#include <linux/moduleparam.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/netpoll.h>#include <linux/inet.h>#include <linux/configfs.h>#include <linux/etherdevice.h>#include <linux/utsname.h>#include <linux/rtnetlink.h> MODULE_AUTHOR("Matt Mackall <mpm@selenic.com>");MODULE_DESCRIPTION("Console driver for network interfaces");MODULE_LICENSE("GPL"); #define MAX_PARAM_LENGTH		256#define MAX_USERDATA_ENTRY_LENGTH	256#define MAX_USERDATA_VALUE_LENGTH	200/* The number 3 comes from userdata entry format characters (' ', '=', '\n') */#define MAX_USERDATA_NAME_LENGTH	(MAX_USERDATA_ENTRY_LENGTH - \					MAX_USERDATA_VALUE_LENGTH - 3)#define MAX_USERDATA_ITEMS		16#define MAX_PRINT_CHUNK			1000 static char config[MAX_PARAM_LENGTH];module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]"); static bool oops_only;module_param(oops_only, bool, 0600);MODULE_PARM_DESC(oops_only, "Only log oops messages"); #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline" #ifndef	MODULEstatic int __init option_setup(char *opt){	strscpy(config, opt, MAX_PARAM_LENGTH);	return 1;}__setup("netconsole=", option_setup);#endif	/* MODULE */ /* Linked list of all configured targets */static LIST_HEAD(target_list);/* target_cleanup_list is used to track targets that need to be cleaned outside * of target_list_lock. It should be cleaned in the same function it is * populated. */static LIST_HEAD(target_cleanup_list); /* This needs to be a spinlock because write_msg() cannot sleep */static DEFINE_SPINLOCK(target_list_lock);/* This needs to be a mutex because netpoll_cleanup might sleep */static DEFINE_MUTEX(target_cleanup_list_lock); /* * Console driver for extended netconsoles.  Registered on the first use to * avoid unnecessarily enabling ext message formatting. */static struct console netconsole_ext; /** * struct netconsole_target - Represents a configured netconsole target. * @list:	Links this target into the target_list. * @group:	Links us into the configfs subsystem hierarchy. * @userdata_group:	Links to the userdata configfs hierarchy * @userdata_complete:	Cached, formatted string of append * @userdata_length:	String length of userdata_complete * @enabled:	On / off knob to enable / disable target. *		Visible from userspace (read-write). *		We maintain a strict 1:1 correspondence between this and *		whether the corresponding netpoll is active or inactive. *		Also, other parameters of a target may be modified at *		runtime only when it is disabled (enabled == 0). * @extended:	Denotes whether console is extended or not. * @release:	Denotes whether kernel release version should be prepended *		to the message. Depends on extended console. * @np:		The netpoll structure for this target. *		Contains the other userspace visible parameters: *		dev_name	(read-write) *		local_port	(read-write) *		remote_port	(read-write) *		local_ip	(read-write) *		remote_ip	(read-write) *		local_mac	(read-only) *		remote_mac	(read-write) */struct netconsole_target {	struct list_head	list;#ifdef	CONFIG_NETCONSOLE_DYNAMIC	struct config_group	group;	struct config_group	userdata_group;	char userdata_complete[MAX_USERDATA_ENTRY_LENGTH * MAX_USERDATA_ITEMS];	size_t			userdata_length;#endif	bool			enabled;	bool			extended;	bool			release;	struct netpoll		np;}; #ifdef	CONFIG_NETCONSOLE_DYNAMIC static struct configfs_subsystem netconsole_subsys;static DEFINE_MUTEX(dynamic_netconsole_mutex); static int __init dynamic_netconsole_init(void){	config_group_init(&netconsole_subsys.su_group);	mutex_init(&netconsole_subsys.su_mutex);	return configfs_register_subsystem(&netconsole_subsys);} static void __exit dynamic_netconsole_exit(void){	configfs_unregister_subsystem(&netconsole_subsys);} /* * Targets that were created by parsing the boot/module option string * do not exist in the configfs hierarchy (and have NULL names) and will * never go away, so make these a no-op for them. */static void netconsole_target_get(struct netconsole_target *nt){	if (config_item_name(&nt->group.cg_item))		config_group_get(&nt->group);} static void netconsole_target_put(struct netconsole_target *nt){	if (config_item_name(&nt->group.cg_item))		config_group_put(&nt->group);} #else	/* !CONFIG_NETCONSOLE_DYNAMIC */ static int __init dynamic_netconsole_init(void){	return 0;} static void __exit dynamic_netconsole_exit(void){} /* * No danger of targets going away from under us when dynamic * reconfigurability is off. */static void netconsole_target_get(struct netconsole_target *nt){} static void netconsole_target_put(struct netconsole_target *nt){} static void populate_configfs_item(struct netconsole_target *nt,				   int cmdline_count){}#endif	/* CONFIG_NETCONSOLE_DYNAMIC */ /* Allocate and initialize with defaults. * Note that these targets get their config_item fields zeroed-out. */static struct netconsole_target *alloc_and_init(void){	struct netconsole_target *nt; 	nt = kzalloc(sizeof(*nt), GFP_KERNEL);	if (!nt)		return nt; 	if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))		nt->extended = true;	if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))		nt->release = true; 	nt->np.name = "netconsole";	strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);	nt->np.local_port = 6665;	nt->np.remote_port = 6666;	eth_broadcast_addr(nt->np.remote_mac); 	return nt;} /* Clean up every target in the cleanup_list and move the clean targets back to * the main target_list. */static void netconsole_process_cleanups_core(void){	struct netconsole_target *nt, *tmp;	unsigned long flags; 	/* The cleanup needs RTNL locked */	ASSERT_RTNL(); 	mutex_lock(&target_cleanup_list_lock);	list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) {		/* all entries in the cleanup_list needs to be disabled */		WARN_ON_ONCE(nt->enabled);		do_netpoll_cleanup(&nt->np);		/* moved the cleaned target to target_list. Need to hold both		 * locks		 */		spin_lock_irqsave(&target_list_lock, flags);		list_move(&nt->list, &target_list);		spin_unlock_irqrestore(&target_list_lock, flags);	}	WARN_ON_ONCE(!list_empty(&target_cleanup_list));	mutex_unlock(&target_cleanup_list_lock);} #ifdef	CONFIG_NETCONSOLE_DYNAMIC /* * Our subsystem hierarchy is: * * /sys/kernel/config/netconsole/ *				| *				<target>/ *				|	enabled *				|	release *				|	dev_name *				|	local_port *				|	remote_port *				|	local_ip *				|	remote_ip *				|	local_mac *				|	remote_mac *				|	userdata/ *				|		<key>/ *				|			value *				|		... *				| *				<target>/... */ static struct netconsole_target *to_target(struct config_item *item){	struct config_group *cfg_group; 	cfg_group = to_config_group(item);	if (!cfg_group)		return NULL;	return container_of(to_config_group(item),			    struct netconsole_target, group);} /* Do the list cleanup with the rtnl lock hold.  rtnl lock is necessary because * netdev might be cleaned-up by calling __netpoll_cleanup(), */static void netconsole_process_cleanups(void){	/* rtnl lock is called here, because it has precedence over	 * target_cleanup_list_lock mutex and target_cleanup_list	 */	rtnl_lock();	netconsole_process_cleanups_core();	rtnl_unlock();} /* Get rid of possible trailing newline, returning the new length */static void trim_newline(char *s, size_t maxlen){	size_t len; 	len = strnlen(s, maxlen);	if (s[len - 1] == '\n')		s[len - 1] = '\0';} /* * Attribute operations for netconsole_target. */ static ssize_t enabled_show(struct config_item *item, char *buf){	return sysfs_emit(buf, "%d\n", to_target(item)->enabled);} static ssize_t extended_show(struct config_item *item, char *buf){	return sysfs_emit(buf, "%d\n", to_target(item)->extended);} static ssize_t release_show(struct config_item *item, char *buf){	return sysfs_emit(buf, "%d\n", to_target(item)->release);} static ssize_t dev_name_show(struct config_item *item, char *buf){	return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);} static ssize_t local_port_show(struct config_item *item, char *buf){	return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);} static ssize_t remote_port_show(struct config_item *item, char *buf){	return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);} static ssize_t local_ip_show(struct config_item *item, char *buf){	struct netconsole_target *nt = to_target(item); 	if (nt->np.ipv6)		return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);	else		return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);} static ssize_t remote_ip_show(struct config_item *item, char *buf){	struct netconsole_target *nt = to_target(item); 	if (nt->np.ipv6)		return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);	else		return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);} static ssize_t local_mac_show(struct config_item *item, char *buf){	struct net_device *dev = to_target(item)->np.dev;	static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 	return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);} static ssize_t remote_mac_show(struct config_item *item, char *buf){	return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);} /* * This one is special -- targets created through the configfs interface * are not enabled (and the corresponding netpoll activated) by default. * The user is expected to set the desired parameters first (which * would enable him to dynamically add new netpoll targets for new * network interfaces as and when they come up). */static ssize_t enabled_store(struct config_item *item,		const char *buf, size_t count){	struct netconsole_target *nt = to_target(item);	unsigned long flags;	bool enabled;	ssize_t ret; 	mutex_lock(&dynamic_netconsole_mutex);	ret = kstrtobool(buf, &enabled);	if (ret)		goto out_unlock; 	ret = -EINVAL;	if (enabled == nt->enabled) {		pr_info("network logging has already %s\n",			nt->enabled ? "started" : "stopped");		goto out_unlock;	} 	if (enabled) {	/* true */		if (nt->release && !nt->extended) {			pr_err("Not enabling netconsole. Release feature requires extended log message");			goto out_unlock;		} 		if (nt->extended && !console_is_registered(&netconsole_ext))			register_console(&netconsole_ext); 		/*		 * Skip netpoll_parse_options() -- all the attributes are		 * already configured via configfs. Just print them out.		 */		netpoll_print_options(&nt->np); 		ret = netpoll_setup(&nt->np);		if (ret)			goto out_unlock; 		nt->enabled = true;		pr_info("network logging started\n");	} else {	/* false */		/* We need to disable the netconsole before cleaning it up		 * otherwise we might end up in write_msg() with		 * nt->np.dev == NULL and nt->enabled == true		 */		mutex_lock(&target_cleanup_list_lock);		spin_lock_irqsave(&target_list_lock, flags);		nt->enabled = false;		/* Remove the target from the list, while holding		 * target_list_lock		 */		list_move(&nt->list, &target_cleanup_list);		spin_unlock_irqrestore(&target_list_lock, flags);		mutex_unlock(&target_cleanup_list_lock);	} 	ret = strnlen(buf, count);	/* Deferred cleanup */	netconsole_process_cleanups();out_unlock:	mutex_unlock(&dynamic_netconsole_mutex);	return ret;} static ssize_t release_store(struct config_item *item, const char *buf,			     size_t count){	struct netconsole_target *nt = to_target(item);	bool release;	ssize_t ret; 	mutex_lock(&dynamic_netconsole_mutex);	if (nt->enabled) {		pr_err("target (%s) is enabled, disable to update parameters\n",		       config_item_name(&nt->group.cg_item));		ret = -EINVAL;		goto out_unlock;	} 	ret = kstrtobool(buf, &release);	if (ret)		goto out_unlock; 	nt->release = release; 	ret = strnlen(buf, count);out_unlock:	mutex_unlock(&dynamic_netconsole_mutex);	return ret;} static ssize_t extended_store(struct config_item *item, const char *buf,		size_t count){	struct netconsole_target *nt = to_target(item);	bool extended;	ssize_t ret; 	mutex_lock(&dynamic_netconsole_mutex);	if (nt->enabled) {		pr_err("target (%s) is enabled, disable to update parameters\n",		       config_item_name(&nt->group.cg_item));		ret = -EINVAL;		goto out_unlock;	} 	ret = kstrtobool(buf, &extended);	if (ret)		goto out_unlock; 	nt->extended = extended;	ret = strnlen(buf, count);out_unlock:	mutex_unlock(&dynamic_netconsole_mutex);	return ret;} static ssize_t dev_name_store(struct config_item *item, const char *buf,		size_t count)

Lines 1–500 of 1472.