985 lines · 24.7 KB · C
RawDownload
/* *  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 */ /**************************************************************** *      This program is free software; you can redistribute it and/or modify *      it under the terms of the GNU General Public License as published by *      the Free Software Foundation; either version 2, or (at your option) *      any later version. * *      This program is distributed in the hope that it will be useful, *      but WITHOUT ANY WARRANTY; without even the implied warranty of *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *      GNU General Public License for more details. * *      You should have received a copy of the GNU General Public License *      along with this program; if not, write to the Free Software *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * ****************************************************************/ #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> MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");MODULE_DESCRIPTION("Console driver for network interfaces");MODULE_LICENSE("GPL"); #define MAX_PARAM_LENGTH	256#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 = false;module_param(oops_only, bool, 0600);MODULE_PARM_DESC(oops_only, "Only log oops messages"); #ifndef	MODULEstatic int __init option_setup(char *opt){	strlcpy(config, opt, MAX_PARAM_LENGTH);	return 1;}__setup("netconsole=", option_setup);#endif	/* MODULE */ /* Linked list of all configured targets */static LIST_HEAD(target_list); /* This needs to be a spinlock because write_msg() cannot sleep */static DEFINE_SPINLOCK(target_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. * @item:	Links us into the configfs subsystem hierarchy. * @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). * @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_item	item;#endif	bool			enabled;	bool			extended;	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->item))		config_item_get(&nt->item);} static void netconsole_target_put(struct netconsole_target *nt){	if (config_item_name(&nt->item))		config_item_put(&nt->item);} #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){} #endif	/* CONFIG_NETCONSOLE_DYNAMIC */ /* Allocate new target (from boot/module param) and setup netpoll for it */static struct netconsole_target *alloc_param_target(char *target_config){	int err = -ENOMEM;	struct netconsole_target *nt; 	/*	 * Allocate and initialize with defaults.	 * Note that these targets get their config_item fields zeroed-out.	 */	nt = kzalloc(sizeof(*nt), GFP_KERNEL);	if (!nt)		goto fail; 	nt->np.name = "netconsole";	strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);	nt->np.local_port = 6665;	nt->np.remote_port = 6666;	eth_broadcast_addr(nt->np.remote_mac); 	if (*target_config == '+') {		nt->extended = true;		target_config++;	} 	/* Parse parameters and setup netpoll */	err = netpoll_parse_options(&nt->np, target_config);	if (err)		goto fail; 	err = netpoll_setup(&nt->np);	if (err)		goto fail; 	nt->enabled = true; 	return nt; fail:	kfree(nt);	return ERR_PTR(err);} /* Cleanup netpoll for given target (from boot/module param) and free it */static void free_param_target(struct netconsole_target *nt){	netpoll_cleanup(&nt->np);	kfree(nt);} #ifdef	CONFIG_NETCONSOLE_DYNAMIC /* * Our subsystem hierarchy is: * * /sys/kernel/config/netconsole/ *				| *				<target>/ *				|	enabled *				|	dev_name *				|	local_port *				|	remote_port *				|	local_ip *				|	remote_ip *				|	local_mac *				|	remote_mac *				| *				<target>/... */ static struct netconsole_target *to_target(struct config_item *item){	return item ?		container_of(item, struct netconsole_target, item) :		NULL;} /* * Attribute operations for netconsole_target. */ static ssize_t enabled_show(struct config_item *item, char *buf){	return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->enabled);} static ssize_t extended_show(struct config_item *item, char *buf){	return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->extended);} static ssize_t dev_name_show(struct config_item *item, char *buf){	return snprintf(buf, PAGE_SIZE, "%s\n", to_target(item)->np.dev_name);} static ssize_t local_port_show(struct config_item *item, char *buf){	return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->np.local_port);} static ssize_t remote_port_show(struct config_item *item, char *buf){	return snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.local_ip.in6);	else		return snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.remote_ip.in6);	else		return snprintf(buf, PAGE_SIZE, "%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 snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast);} static ssize_t remote_mac_show(struct config_item *item, char *buf){	return snprintf(buf, PAGE_SIZE, "%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;	int enabled;	int err; 	mutex_lock(&dynamic_netconsole_mutex);	err = kstrtoint(buf, 10, &enabled);	if (err < 0)		goto out_unlock; 	err = -EINVAL;	if (enabled < 0 || enabled > 1)		goto out_unlock;	if ((bool)enabled == nt->enabled) {		pr_info("network logging has already %s\n",			nt->enabled ? "started" : "stopped");		goto out_unlock;	} 	if (enabled) {	/* true */		if (nt->extended && !(netconsole_ext.flags & CON_ENABLED)) {			netconsole_ext.flags |= CON_ENABLED;			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); 		err = netpoll_setup(&nt->np);		if (err)			goto out_unlock; 		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		 */		spin_lock_irqsave(&target_list_lock, flags);		nt->enabled = false;		spin_unlock_irqrestore(&target_list_lock, flags);		netpoll_cleanup(&nt->np);	} 	nt->enabled = enabled; 	mutex_unlock(&dynamic_netconsole_mutex);	return strnlen(buf, count);out_unlock:	mutex_unlock(&dynamic_netconsole_mutex);	return err;} static ssize_t extended_store(struct config_item *item, const char *buf,		size_t count){	struct netconsole_target *nt = to_target(item);	int extended;	int err; 	mutex_lock(&dynamic_netconsole_mutex);	if (nt->enabled) {		pr_err("target (%s) is enabled, disable to update parameters\n",		       config_item_name(&nt->item));		err = -EINVAL;		goto out_unlock;	} 	err = kstrtoint(buf, 10, &extended);	if (err < 0)		goto out_unlock;	if (extended < 0 || extended > 1) {		err = -EINVAL;		goto out_unlock;	} 	nt->extended = extended; 	mutex_unlock(&dynamic_netconsole_mutex);	return strnlen(buf, count);out_unlock:	mutex_unlock(&dynamic_netconsole_mutex);	return err;} static ssize_t dev_name_store(struct config_item *item, const char *buf,		size_t count){	struct netconsole_target *nt = to_target(item);	size_t len; 	mutex_lock(&dynamic_netconsole_mutex);	if (nt->enabled) {		pr_err("target (%s) is enabled, disable to update parameters\n",		       config_item_name(&nt->item));		mutex_unlock(&dynamic_netconsole_mutex);		return -EINVAL;	} 	strlcpy(nt->np.dev_name, buf, IFNAMSIZ); 	/* Get rid of possible trailing newline from echo(1) */	len = strnlen(nt->np.dev_name, IFNAMSIZ);	if (nt->np.dev_name[len - 1] == '\n')		nt->np.dev_name[len - 1] = '\0'; 	mutex_unlock(&dynamic_netconsole_mutex);	return strnlen(buf, count);} static ssize_t local_port_store(struct config_item *item, const char *buf,		size_t count){	struct netconsole_target *nt = to_target(item);	int rv = -EINVAL; 	mutex_lock(&dynamic_netconsole_mutex);	if (nt->enabled) {		pr_err("target (%s) is enabled, disable to update parameters\n",		       config_item_name(&nt->item));		goto out_unlock;	} 	rv = kstrtou16(buf, 10, &nt->np.local_port);	if (rv < 0)		goto out_unlock;	mutex_unlock(&dynamic_netconsole_mutex);	return strnlen(buf, count);out_unlock:	mutex_unlock(&dynamic_netconsole_mutex);	return rv;} static ssize_t remote_port_store(struct config_item *item,		const char *buf, size_t count){	struct netconsole_target *nt = to_target(item);	int rv = -EINVAL; 	mutex_lock(&dynamic_netconsole_mutex);	if (nt->enabled) {		pr_err("target (%s) is enabled, disable to update parameters\n",		       config_item_name(&nt->item));		goto out_unlock;	} 	rv = kstrtou16(buf, 10, &nt->np.remote_port);	if (rv < 0)		goto out_unlock;	mutex_unlock(&dynamic_netconsole_mutex);	return strnlen(buf, count);out_unlock:	mutex_unlock(&dynamic_netconsole_mutex);	return rv;} static ssize_t local_ip_store(struct config_item *item, const char *buf,		size_t count){	struct netconsole_target *nt = to_target(item); 	mutex_lock(&dynamic_netconsole_mutex);	if (nt->enabled) {		pr_err("target (%s) is enabled, disable to update parameters\n",		       config_item_name(&nt->item));		goto out_unlock;	} 	if (strnchr(buf, count, ':')) {		const char *end;		if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {			if (*end && *end != '\n') {

Lines 1–500 of 985.