xref: /openbmc/linux/drivers/net/netconsole.c (revision e174961c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/net/netconsole.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2001  Ingo Molnar <mingo@redhat.com>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  This file contains the implementation of an IRQ-safe, crash-safe
71da177e4SLinus Torvalds  *  kernel console implementation that outputs kernel messages to the
81da177e4SLinus Torvalds  *  network.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Modification history:
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * 2001-09-17    started by Ingo Molnar.
131da177e4SLinus Torvalds  * 2003-08-11    2.6 port by Matt Mackall
141da177e4SLinus Torvalds  *               simplified options
151da177e4SLinus Torvalds  *               generic card hooks
161da177e4SLinus Torvalds  *               works non-modular
171da177e4SLinus Torvalds  * 2003-09-07    rewritten with netpoll api
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds /****************************************************************
211da177e4SLinus Torvalds  *      This program is free software; you can redistribute it and/or modify
221da177e4SLinus Torvalds  *      it under the terms of the GNU General Public License as published by
231da177e4SLinus Torvalds  *      the Free Software Foundation; either version 2, or (at your option)
241da177e4SLinus Torvalds  *      any later version.
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  *      This program is distributed in the hope that it will be useful,
271da177e4SLinus Torvalds  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
281da177e4SLinus Torvalds  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
291da177e4SLinus Torvalds  *      GNU General Public License for more details.
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *      You should have received a copy of the GNU General Public License
321da177e4SLinus Torvalds  *      along with this program; if not, write to the Free Software
331da177e4SLinus Torvalds  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  ****************************************************************/
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include <linux/mm.h>
381da177e4SLinus Torvalds #include <linux/init.h>
391da177e4SLinus Torvalds #include <linux/module.h>
401da177e4SLinus Torvalds #include <linux/console.h>
411da177e4SLinus Torvalds #include <linux/moduleparam.h>
421da177e4SLinus Torvalds #include <linux/string.h>
431da177e4SLinus Torvalds #include <linux/netpoll.h>
440bcc1816SSatyam Sharma #include <linux/inet.h>
450bcc1816SSatyam Sharma #include <linux/configfs.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
481da177e4SLinus Torvalds MODULE_DESCRIPTION("Console driver for network interfaces");
491da177e4SLinus Torvalds MODULE_LICENSE("GPL");
501da177e4SLinus Torvalds 
51d39badf0SSatyam Sharma #define MAX_PARAM_LENGTH	256
52d39badf0SSatyam Sharma #define MAX_PRINT_CHUNK		1000
53d39badf0SSatyam Sharma 
54d39badf0SSatyam Sharma static char config[MAX_PARAM_LENGTH];
55d39badf0SSatyam Sharma module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
5661a2d07dSNiels de Vos MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
571da177e4SLinus Torvalds 
58d2b60881SSatyam Sharma #ifndef	MODULE
59d2b60881SSatyam Sharma static int __init option_setup(char *opt)
60d2b60881SSatyam Sharma {
61d2b60881SSatyam Sharma 	strlcpy(config, opt, MAX_PARAM_LENGTH);
62d2b60881SSatyam Sharma 	return 1;
63d2b60881SSatyam Sharma }
64d2b60881SSatyam Sharma __setup("netconsole=", option_setup);
65d2b60881SSatyam Sharma #endif	/* MODULE */
66d2b60881SSatyam Sharma 
67b5427c27SSatyam Sharma /* Linked list of all configured targets */
68b5427c27SSatyam Sharma static LIST_HEAD(target_list);
69b5427c27SSatyam Sharma 
70b5427c27SSatyam Sharma /* This needs to be a spinlock because write_msg() cannot sleep */
71b5427c27SSatyam Sharma static DEFINE_SPINLOCK(target_list_lock);
72b5427c27SSatyam Sharma 
73df180e36SSatyam Sharma /**
74df180e36SSatyam Sharma  * struct netconsole_target - Represents a configured netconsole target.
75b5427c27SSatyam Sharma  * @list:	Links this target into the target_list.
760bcc1816SSatyam Sharma  * @item:	Links us into the configfs subsystem hierarchy.
770bcc1816SSatyam Sharma  * @enabled:	On / off knob to enable / disable target.
780bcc1816SSatyam Sharma  *		Visible from userspace (read-write).
790bcc1816SSatyam Sharma  *		We maintain a strict 1:1 correspondence between this and
800bcc1816SSatyam Sharma  *		whether the corresponding netpoll is active or inactive.
810bcc1816SSatyam Sharma  *		Also, other parameters of a target may be modified at
820bcc1816SSatyam Sharma  *		runtime only when it is disabled (enabled == 0).
83df180e36SSatyam Sharma  * @np:		The netpoll structure for this target.
840bcc1816SSatyam Sharma  *		Contains the other userspace visible parameters:
850bcc1816SSatyam Sharma  *		dev_name	(read-write)
860bcc1816SSatyam Sharma  *		local_port	(read-write)
870bcc1816SSatyam Sharma  *		remote_port	(read-write)
880bcc1816SSatyam Sharma  *		local_ip	(read-write)
890bcc1816SSatyam Sharma  *		remote_ip	(read-write)
900bcc1816SSatyam Sharma  *		local_mac	(read-only)
910bcc1816SSatyam Sharma  *		remote_mac	(read-write)
92df180e36SSatyam Sharma  */
93df180e36SSatyam Sharma struct netconsole_target {
94b5427c27SSatyam Sharma 	struct list_head	list;
950bcc1816SSatyam Sharma #ifdef	CONFIG_NETCONSOLE_DYNAMIC
960bcc1816SSatyam Sharma 	struct config_item	item;
970bcc1816SSatyam Sharma #endif
980bcc1816SSatyam Sharma 	int			enabled;
99df180e36SSatyam Sharma 	struct netpoll		np;
100df180e36SSatyam Sharma };
101df180e36SSatyam Sharma 
1020bcc1816SSatyam Sharma #ifdef	CONFIG_NETCONSOLE_DYNAMIC
1030bcc1816SSatyam Sharma 
1040bcc1816SSatyam Sharma static struct configfs_subsystem netconsole_subsys;
1050bcc1816SSatyam Sharma 
1060bcc1816SSatyam Sharma static int __init dynamic_netconsole_init(void)
1070bcc1816SSatyam Sharma {
1080bcc1816SSatyam Sharma 	config_group_init(&netconsole_subsys.su_group);
1090bcc1816SSatyam Sharma 	mutex_init(&netconsole_subsys.su_mutex);
1100bcc1816SSatyam Sharma 	return configfs_register_subsystem(&netconsole_subsys);
1110bcc1816SSatyam Sharma }
1120bcc1816SSatyam Sharma 
1130bcc1816SSatyam Sharma static void __exit dynamic_netconsole_exit(void)
1140bcc1816SSatyam Sharma {
1150bcc1816SSatyam Sharma 	configfs_unregister_subsystem(&netconsole_subsys);
1160bcc1816SSatyam Sharma }
1170bcc1816SSatyam Sharma 
1180bcc1816SSatyam Sharma /*
1190bcc1816SSatyam Sharma  * Targets that were created by parsing the boot/module option string
1200bcc1816SSatyam Sharma  * do not exist in the configfs hierarchy (and have NULL names) and will
1210bcc1816SSatyam Sharma  * never go away, so make these a no-op for them.
1220bcc1816SSatyam Sharma  */
1230bcc1816SSatyam Sharma static void netconsole_target_get(struct netconsole_target *nt)
1240bcc1816SSatyam Sharma {
1250bcc1816SSatyam Sharma 	if (config_item_name(&nt->item))
1260bcc1816SSatyam Sharma 		config_item_get(&nt->item);
1270bcc1816SSatyam Sharma }
1280bcc1816SSatyam Sharma 
1290bcc1816SSatyam Sharma static void netconsole_target_put(struct netconsole_target *nt)
1300bcc1816SSatyam Sharma {
1310bcc1816SSatyam Sharma 	if (config_item_name(&nt->item))
1320bcc1816SSatyam Sharma 		config_item_put(&nt->item);
1330bcc1816SSatyam Sharma }
1340bcc1816SSatyam Sharma 
1350bcc1816SSatyam Sharma #else	/* !CONFIG_NETCONSOLE_DYNAMIC */
1360bcc1816SSatyam Sharma 
1370bcc1816SSatyam Sharma static int __init dynamic_netconsole_init(void)
1380bcc1816SSatyam Sharma {
1390bcc1816SSatyam Sharma 	return 0;
1400bcc1816SSatyam Sharma }
1410bcc1816SSatyam Sharma 
1420bcc1816SSatyam Sharma static void __exit dynamic_netconsole_exit(void)
1430bcc1816SSatyam Sharma {
1440bcc1816SSatyam Sharma }
1450bcc1816SSatyam Sharma 
1460bcc1816SSatyam Sharma /*
1470bcc1816SSatyam Sharma  * No danger of targets going away from under us when dynamic
1480bcc1816SSatyam Sharma  * reconfigurability is off.
1490bcc1816SSatyam Sharma  */
1500bcc1816SSatyam Sharma static void netconsole_target_get(struct netconsole_target *nt)
1510bcc1816SSatyam Sharma {
1520bcc1816SSatyam Sharma }
1530bcc1816SSatyam Sharma 
1540bcc1816SSatyam Sharma static void netconsole_target_put(struct netconsole_target *nt)
1550bcc1816SSatyam Sharma {
1560bcc1816SSatyam Sharma }
1570bcc1816SSatyam Sharma 
1580bcc1816SSatyam Sharma #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
1590bcc1816SSatyam Sharma 
1600bcc1816SSatyam Sharma /* Allocate new target (from boot/module param) and setup netpoll for it */
1610bcc1816SSatyam Sharma static struct netconsole_target *alloc_param_target(char *target_config)
162b5427c27SSatyam Sharma {
163b5427c27SSatyam Sharma 	int err = -ENOMEM;
164b5427c27SSatyam Sharma 	struct netconsole_target *nt;
165b5427c27SSatyam Sharma 
1660bcc1816SSatyam Sharma 	/*
1670bcc1816SSatyam Sharma 	 * Allocate and initialize with defaults.
1680bcc1816SSatyam Sharma 	 * Note that these targets get their config_item fields zeroed-out.
1690bcc1816SSatyam Sharma 	 */
170b5427c27SSatyam Sharma 	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
171b5427c27SSatyam Sharma 	if (!nt) {
172b5427c27SSatyam Sharma 		printk(KERN_ERR "netconsole: failed to allocate memory\n");
173b5427c27SSatyam Sharma 		goto fail;
174b5427c27SSatyam Sharma 	}
175b5427c27SSatyam Sharma 
176b5427c27SSatyam Sharma 	nt->np.name = "netconsole";
177b5427c27SSatyam Sharma 	strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
178b5427c27SSatyam Sharma 	nt->np.local_port = 6665;
179b5427c27SSatyam Sharma 	nt->np.remote_port = 6666;
180b5427c27SSatyam Sharma 	memset(nt->np.remote_mac, 0xff, ETH_ALEN);
181b5427c27SSatyam Sharma 
182b5427c27SSatyam Sharma 	/* Parse parameters and setup netpoll */
183b5427c27SSatyam Sharma 	err = netpoll_parse_options(&nt->np, target_config);
184b5427c27SSatyam Sharma 	if (err)
185b5427c27SSatyam Sharma 		goto fail;
186b5427c27SSatyam Sharma 
187b5427c27SSatyam Sharma 	err = netpoll_setup(&nt->np);
188b5427c27SSatyam Sharma 	if (err)
189b5427c27SSatyam Sharma 		goto fail;
190b5427c27SSatyam Sharma 
1910bcc1816SSatyam Sharma 	nt->enabled = 1;
1920bcc1816SSatyam Sharma 
193b5427c27SSatyam Sharma 	return nt;
194b5427c27SSatyam Sharma 
195b5427c27SSatyam Sharma fail:
196b5427c27SSatyam Sharma 	kfree(nt);
197b5427c27SSatyam Sharma 	return ERR_PTR(err);
198b5427c27SSatyam Sharma }
199b5427c27SSatyam Sharma 
2000bcc1816SSatyam Sharma /* Cleanup netpoll for given target (from boot/module param) and free it */
2010bcc1816SSatyam Sharma static void free_param_target(struct netconsole_target *nt)
202b5427c27SSatyam Sharma {
203b5427c27SSatyam Sharma 	netpoll_cleanup(&nt->np);
204b5427c27SSatyam Sharma 	kfree(nt);
205b5427c27SSatyam Sharma }
2061da177e4SLinus Torvalds 
2070bcc1816SSatyam Sharma #ifdef	CONFIG_NETCONSOLE_DYNAMIC
2080bcc1816SSatyam Sharma 
2090bcc1816SSatyam Sharma /*
2100bcc1816SSatyam Sharma  * Our subsystem hierarchy is:
2110bcc1816SSatyam Sharma  *
2120bcc1816SSatyam Sharma  * /sys/kernel/config/netconsole/
2130bcc1816SSatyam Sharma  *				|
2140bcc1816SSatyam Sharma  *				<target>/
2150bcc1816SSatyam Sharma  *				|	enabled
2160bcc1816SSatyam Sharma  *				|	dev_name
2170bcc1816SSatyam Sharma  *				|	local_port
2180bcc1816SSatyam Sharma  *				|	remote_port
2190bcc1816SSatyam Sharma  *				|	local_ip
2200bcc1816SSatyam Sharma  *				|	remote_ip
2210bcc1816SSatyam Sharma  *				|	local_mac
2220bcc1816SSatyam Sharma  *				|	remote_mac
2230bcc1816SSatyam Sharma  *				|
2240bcc1816SSatyam Sharma  *				<target>/...
2250bcc1816SSatyam Sharma  */
2260bcc1816SSatyam Sharma 
2270bcc1816SSatyam Sharma struct netconsole_target_attr {
2280bcc1816SSatyam Sharma 	struct configfs_attribute	attr;
2290bcc1816SSatyam Sharma 	ssize_t				(*show)(struct netconsole_target *nt,
2300bcc1816SSatyam Sharma 						char *buf);
2310bcc1816SSatyam Sharma 	ssize_t				(*store)(struct netconsole_target *nt,
2320bcc1816SSatyam Sharma 						 const char *buf,
2330bcc1816SSatyam Sharma 						 size_t count);
2340bcc1816SSatyam Sharma };
2350bcc1816SSatyam Sharma 
2360bcc1816SSatyam Sharma static struct netconsole_target *to_target(struct config_item *item)
2370bcc1816SSatyam Sharma {
2380bcc1816SSatyam Sharma 	return item ?
2390bcc1816SSatyam Sharma 		container_of(item, struct netconsole_target, item) :
2400bcc1816SSatyam Sharma 		NULL;
2410bcc1816SSatyam Sharma }
2420bcc1816SSatyam Sharma 
2430bcc1816SSatyam Sharma /*
2440bcc1816SSatyam Sharma  * Wrapper over simple_strtol (base 10) with sanity and range checking.
2450bcc1816SSatyam Sharma  * We return (signed) long only because we may want to return errors.
2460bcc1816SSatyam Sharma  * Do not use this to convert numbers that are allowed to be negative.
2470bcc1816SSatyam Sharma  */
2480bcc1816SSatyam Sharma static long strtol10_check_range(const char *cp, long min, long max)
2490bcc1816SSatyam Sharma {
2500bcc1816SSatyam Sharma 	long ret;
2510bcc1816SSatyam Sharma 	char *p = (char *) cp;
2520bcc1816SSatyam Sharma 
2530bcc1816SSatyam Sharma 	WARN_ON(min < 0);
2540bcc1816SSatyam Sharma 	WARN_ON(max < min);
2550bcc1816SSatyam Sharma 
2560bcc1816SSatyam Sharma 	ret = simple_strtol(p, &p, 10);
2570bcc1816SSatyam Sharma 
2580bcc1816SSatyam Sharma 	if (*p && (*p != '\n')) {
2590bcc1816SSatyam Sharma 		printk(KERN_ERR "netconsole: invalid input\n");
2600bcc1816SSatyam Sharma 		return -EINVAL;
2610bcc1816SSatyam Sharma 	}
2620bcc1816SSatyam Sharma 	if ((ret < min) || (ret > max)) {
2630bcc1816SSatyam Sharma 		printk(KERN_ERR "netconsole: input %ld must be between "
2640bcc1816SSatyam Sharma 				"%ld and %ld\n", ret, min, max);
2650bcc1816SSatyam Sharma 		return -EINVAL;
2660bcc1816SSatyam Sharma 	}
2670bcc1816SSatyam Sharma 
2680bcc1816SSatyam Sharma 	return ret;
2690bcc1816SSatyam Sharma }
2700bcc1816SSatyam Sharma 
2710bcc1816SSatyam Sharma /*
2720bcc1816SSatyam Sharma  * Attribute operations for netconsole_target.
2730bcc1816SSatyam Sharma  */
2740bcc1816SSatyam Sharma 
2750bcc1816SSatyam Sharma static ssize_t show_enabled(struct netconsole_target *nt, char *buf)
2760bcc1816SSatyam Sharma {
2770bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%d\n", nt->enabled);
2780bcc1816SSatyam Sharma }
2790bcc1816SSatyam Sharma 
2800bcc1816SSatyam Sharma static ssize_t show_dev_name(struct netconsole_target *nt, char *buf)
2810bcc1816SSatyam Sharma {
2820bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%s\n", nt->np.dev_name);
2830bcc1816SSatyam Sharma }
2840bcc1816SSatyam Sharma 
2850bcc1816SSatyam Sharma static ssize_t show_local_port(struct netconsole_target *nt, char *buf)
2860bcc1816SSatyam Sharma {
2870bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.local_port);
2880bcc1816SSatyam Sharma }
2890bcc1816SSatyam Sharma 
2900bcc1816SSatyam Sharma static ssize_t show_remote_port(struct netconsole_target *nt, char *buf)
2910bcc1816SSatyam Sharma {
2920bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.remote_port);
2930bcc1816SSatyam Sharma }
2940bcc1816SSatyam Sharma 
2950bcc1816SSatyam Sharma static ssize_t show_local_ip(struct netconsole_target *nt, char *buf)
2960bcc1816SSatyam Sharma {
2970bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
2980bcc1816SSatyam Sharma 			HIPQUAD(nt->np.local_ip));
2990bcc1816SSatyam Sharma }
3000bcc1816SSatyam Sharma 
3010bcc1816SSatyam Sharma static ssize_t show_remote_ip(struct netconsole_target *nt, char *buf)
3020bcc1816SSatyam Sharma {
3030bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
3040bcc1816SSatyam Sharma 			HIPQUAD(nt->np.remote_ip));
3050bcc1816SSatyam Sharma }
3060bcc1816SSatyam Sharma 
3070bcc1816SSatyam Sharma static ssize_t show_local_mac(struct netconsole_target *nt, char *buf)
3080bcc1816SSatyam Sharma {
30909538641SStephen Hemminger 	struct net_device *dev = nt->np.dev;
310e174961cSJohannes Berg 	static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
31109538641SStephen Hemminger 
312e174961cSJohannes Berg 	return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast);
3130bcc1816SSatyam Sharma }
3140bcc1816SSatyam Sharma 
3150bcc1816SSatyam Sharma static ssize_t show_remote_mac(struct netconsole_target *nt, char *buf)
3160bcc1816SSatyam Sharma {
317e174961cSJohannes Berg 	return snprintf(buf, PAGE_SIZE, "%pM\n", nt->np.remote_mac);
3180bcc1816SSatyam Sharma }
3190bcc1816SSatyam Sharma 
3200bcc1816SSatyam Sharma /*
3210bcc1816SSatyam Sharma  * This one is special -- targets created through the configfs interface
3220bcc1816SSatyam Sharma  * are not enabled (and the corresponding netpoll activated) by default.
3230bcc1816SSatyam Sharma  * The user is expected to set the desired parameters first (which
3240bcc1816SSatyam Sharma  * would enable him to dynamically add new netpoll targets for new
3250bcc1816SSatyam Sharma  * network interfaces as and when they come up).
3260bcc1816SSatyam Sharma  */
3270bcc1816SSatyam Sharma static ssize_t store_enabled(struct netconsole_target *nt,
3280bcc1816SSatyam Sharma 			     const char *buf,
3290bcc1816SSatyam Sharma 			     size_t count)
3300bcc1816SSatyam Sharma {
3310bcc1816SSatyam Sharma 	int err;
3320bcc1816SSatyam Sharma 	long enabled;
3330bcc1816SSatyam Sharma 
3340bcc1816SSatyam Sharma 	enabled = strtol10_check_range(buf, 0, 1);
3350bcc1816SSatyam Sharma 	if (enabled < 0)
3360bcc1816SSatyam Sharma 		return enabled;
3370bcc1816SSatyam Sharma 
3380bcc1816SSatyam Sharma 	if (enabled) {	/* 1 */
3390bcc1816SSatyam Sharma 
3400bcc1816SSatyam Sharma 		/*
3410bcc1816SSatyam Sharma 		 * Skip netpoll_parse_options() -- all the attributes are
3420bcc1816SSatyam Sharma 		 * already configured via configfs. Just print them out.
3430bcc1816SSatyam Sharma 		 */
3440bcc1816SSatyam Sharma 		netpoll_print_options(&nt->np);
3450bcc1816SSatyam Sharma 
3460bcc1816SSatyam Sharma 		err = netpoll_setup(&nt->np);
3470bcc1816SSatyam Sharma 		if (err)
3480bcc1816SSatyam Sharma 			return err;
3490bcc1816SSatyam Sharma 
3500bcc1816SSatyam Sharma 		printk(KERN_INFO "netconsole: network logging started\n");
3510bcc1816SSatyam Sharma 
3520bcc1816SSatyam Sharma 	} else {	/* 0 */
3530bcc1816SSatyam Sharma 		netpoll_cleanup(&nt->np);
3540bcc1816SSatyam Sharma 	}
3550bcc1816SSatyam Sharma 
3560bcc1816SSatyam Sharma 	nt->enabled = enabled;
3570bcc1816SSatyam Sharma 
3580bcc1816SSatyam Sharma 	return strnlen(buf, count);
3590bcc1816SSatyam Sharma }
3600bcc1816SSatyam Sharma 
3610bcc1816SSatyam Sharma static ssize_t store_dev_name(struct netconsole_target *nt,
3620bcc1816SSatyam Sharma 			      const char *buf,
3630bcc1816SSatyam Sharma 			      size_t count)
3640bcc1816SSatyam Sharma {
3650bcc1816SSatyam Sharma 	size_t len;
3660bcc1816SSatyam Sharma 
3670bcc1816SSatyam Sharma 	if (nt->enabled) {
3680bcc1816SSatyam Sharma 		printk(KERN_ERR "netconsole: target (%s) is enabled, "
3690bcc1816SSatyam Sharma 				"disable to update parameters\n",
3700bcc1816SSatyam Sharma 				config_item_name(&nt->item));
3710bcc1816SSatyam Sharma 		return -EINVAL;
3720bcc1816SSatyam Sharma 	}
3730bcc1816SSatyam Sharma 
3740bcc1816SSatyam Sharma 	strlcpy(nt->np.dev_name, buf, IFNAMSIZ);
3750bcc1816SSatyam Sharma 
3760bcc1816SSatyam Sharma 	/* Get rid of possible trailing newline from echo(1) */
3770bcc1816SSatyam Sharma 	len = strnlen(nt->np.dev_name, IFNAMSIZ);
3780bcc1816SSatyam Sharma 	if (nt->np.dev_name[len - 1] == '\n')
3790bcc1816SSatyam Sharma 		nt->np.dev_name[len - 1] = '\0';
3800bcc1816SSatyam Sharma 
3810bcc1816SSatyam Sharma 	return strnlen(buf, count);
3820bcc1816SSatyam Sharma }
3830bcc1816SSatyam Sharma 
3840bcc1816SSatyam Sharma static ssize_t store_local_port(struct netconsole_target *nt,
3850bcc1816SSatyam Sharma 				const char *buf,
3860bcc1816SSatyam Sharma 				size_t count)
3870bcc1816SSatyam Sharma {
3880bcc1816SSatyam Sharma 	long local_port;
3890bcc1816SSatyam Sharma #define __U16_MAX	((__u16) ~0U)
3900bcc1816SSatyam Sharma 
3910bcc1816SSatyam Sharma 	if (nt->enabled) {
3920bcc1816SSatyam Sharma 		printk(KERN_ERR "netconsole: target (%s) is enabled, "
3930bcc1816SSatyam Sharma 				"disable to update parameters\n",
3940bcc1816SSatyam Sharma 				config_item_name(&nt->item));
3950bcc1816SSatyam Sharma 		return -EINVAL;
3960bcc1816SSatyam Sharma 	}
3970bcc1816SSatyam Sharma 
3980bcc1816SSatyam Sharma 	local_port = strtol10_check_range(buf, 0, __U16_MAX);
3990bcc1816SSatyam Sharma 	if (local_port < 0)
4000bcc1816SSatyam Sharma 		return local_port;
4010bcc1816SSatyam Sharma 
4020bcc1816SSatyam Sharma 	nt->np.local_port = local_port;
4030bcc1816SSatyam Sharma 
4040bcc1816SSatyam Sharma 	return strnlen(buf, count);
4050bcc1816SSatyam Sharma }
4060bcc1816SSatyam Sharma 
4070bcc1816SSatyam Sharma static ssize_t store_remote_port(struct netconsole_target *nt,
4080bcc1816SSatyam Sharma 				 const char *buf,
4090bcc1816SSatyam Sharma 				 size_t count)
4100bcc1816SSatyam Sharma {
4110bcc1816SSatyam Sharma 	long remote_port;
4120bcc1816SSatyam Sharma #define __U16_MAX	((__u16) ~0U)
4130bcc1816SSatyam Sharma 
4140bcc1816SSatyam Sharma 	if (nt->enabled) {
4150bcc1816SSatyam Sharma 		printk(KERN_ERR "netconsole: target (%s) is enabled, "
4160bcc1816SSatyam Sharma 				"disable to update parameters\n",
4170bcc1816SSatyam Sharma 				config_item_name(&nt->item));
4180bcc1816SSatyam Sharma 		return -EINVAL;
4190bcc1816SSatyam Sharma 	}
4200bcc1816SSatyam Sharma 
4210bcc1816SSatyam Sharma 	remote_port = strtol10_check_range(buf, 0, __U16_MAX);
4220bcc1816SSatyam Sharma 	if (remote_port < 0)
4230bcc1816SSatyam Sharma 		return remote_port;
4240bcc1816SSatyam Sharma 
4250bcc1816SSatyam Sharma 	nt->np.remote_port = remote_port;
4260bcc1816SSatyam Sharma 
4270bcc1816SSatyam Sharma 	return strnlen(buf, count);
4280bcc1816SSatyam Sharma }
4290bcc1816SSatyam Sharma 
4300bcc1816SSatyam Sharma static ssize_t store_local_ip(struct netconsole_target *nt,
4310bcc1816SSatyam Sharma 			      const char *buf,
4320bcc1816SSatyam Sharma 			      size_t count)
4330bcc1816SSatyam Sharma {
4340bcc1816SSatyam Sharma 	if (nt->enabled) {
4350bcc1816SSatyam Sharma 		printk(KERN_ERR "netconsole: target (%s) is enabled, "
4360bcc1816SSatyam Sharma 				"disable to update parameters\n",
4370bcc1816SSatyam Sharma 				config_item_name(&nt->item));
4380bcc1816SSatyam Sharma 		return -EINVAL;
4390bcc1816SSatyam Sharma 	}
4400bcc1816SSatyam Sharma 
4410bcc1816SSatyam Sharma 	nt->np.local_ip = ntohl(in_aton(buf));
4420bcc1816SSatyam Sharma 
4430bcc1816SSatyam Sharma 	return strnlen(buf, count);
4440bcc1816SSatyam Sharma }
4450bcc1816SSatyam Sharma 
4460bcc1816SSatyam Sharma static ssize_t store_remote_ip(struct netconsole_target *nt,
4470bcc1816SSatyam Sharma 			       const char *buf,
4480bcc1816SSatyam Sharma 			       size_t count)
4490bcc1816SSatyam Sharma {
4500bcc1816SSatyam Sharma 	if (nt->enabled) {
4510bcc1816SSatyam Sharma 		printk(KERN_ERR "netconsole: target (%s) is enabled, "
4520bcc1816SSatyam Sharma 				"disable to update parameters\n",
4530bcc1816SSatyam Sharma 				config_item_name(&nt->item));
4540bcc1816SSatyam Sharma 		return -EINVAL;
4550bcc1816SSatyam Sharma 	}
4560bcc1816SSatyam Sharma 
4570bcc1816SSatyam Sharma 	nt->np.remote_ip = ntohl(in_aton(buf));
4580bcc1816SSatyam Sharma 
4590bcc1816SSatyam Sharma 	return strnlen(buf, count);
4600bcc1816SSatyam Sharma }
4610bcc1816SSatyam Sharma 
4620bcc1816SSatyam Sharma static ssize_t store_remote_mac(struct netconsole_target *nt,
4630bcc1816SSatyam Sharma 				const char *buf,
4640bcc1816SSatyam Sharma 				size_t count)
4650bcc1816SSatyam Sharma {
4660bcc1816SSatyam Sharma 	u8 remote_mac[ETH_ALEN];
4670bcc1816SSatyam Sharma 	char *p = (char *) buf;
4680bcc1816SSatyam Sharma 	int i;
4690bcc1816SSatyam Sharma 
4700bcc1816SSatyam Sharma 	if (nt->enabled) {
4710bcc1816SSatyam Sharma 		printk(KERN_ERR "netconsole: target (%s) is enabled, "
4720bcc1816SSatyam Sharma 				"disable to update parameters\n",
4730bcc1816SSatyam Sharma 				config_item_name(&nt->item));
4740bcc1816SSatyam Sharma 		return -EINVAL;
4750bcc1816SSatyam Sharma 	}
4760bcc1816SSatyam Sharma 
4770bcc1816SSatyam Sharma 	for (i = 0; i < ETH_ALEN - 1; i++) {
4780bcc1816SSatyam Sharma 		remote_mac[i] = simple_strtoul(p, &p, 16);
4790bcc1816SSatyam Sharma 		if (*p != ':')
4800bcc1816SSatyam Sharma 			goto invalid;
4810bcc1816SSatyam Sharma 		p++;
4820bcc1816SSatyam Sharma 	}
4830bcc1816SSatyam Sharma 	remote_mac[ETH_ALEN - 1] = simple_strtoul(p, &p, 16);
4840bcc1816SSatyam Sharma 	if (*p && (*p != '\n'))
4850bcc1816SSatyam Sharma 		goto invalid;
4860bcc1816SSatyam Sharma 
4870bcc1816SSatyam Sharma 	memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
4880bcc1816SSatyam Sharma 
4890bcc1816SSatyam Sharma 	return strnlen(buf, count);
4900bcc1816SSatyam Sharma 
4910bcc1816SSatyam Sharma invalid:
4920bcc1816SSatyam Sharma 	printk(KERN_ERR "netconsole: invalid input\n");
4930bcc1816SSatyam Sharma 	return -EINVAL;
4940bcc1816SSatyam Sharma }
4950bcc1816SSatyam Sharma 
4960bcc1816SSatyam Sharma /*
4970bcc1816SSatyam Sharma  * Attribute definitions for netconsole_target.
4980bcc1816SSatyam Sharma  */
4990bcc1816SSatyam Sharma 
5000bcc1816SSatyam Sharma #define NETCONSOLE_TARGET_ATTR_RO(_name)				\
5010bcc1816SSatyam Sharma static struct netconsole_target_attr netconsole_target_##_name =	\
5020bcc1816SSatyam Sharma 	__CONFIGFS_ATTR(_name, S_IRUGO, show_##_name, NULL)
5030bcc1816SSatyam Sharma 
5040bcc1816SSatyam Sharma #define NETCONSOLE_TARGET_ATTR_RW(_name)				\
5050bcc1816SSatyam Sharma static struct netconsole_target_attr netconsole_target_##_name =	\
5060bcc1816SSatyam Sharma 	__CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name)
5070bcc1816SSatyam Sharma 
5080bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(enabled);
5090bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(dev_name);
5100bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(local_port);
5110bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(remote_port);
5120bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(local_ip);
5130bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(remote_ip);
5140bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RO(local_mac);
5150bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(remote_mac);
5160bcc1816SSatyam Sharma 
5170bcc1816SSatyam Sharma static struct configfs_attribute *netconsole_target_attrs[] = {
5180bcc1816SSatyam Sharma 	&netconsole_target_enabled.attr,
5190bcc1816SSatyam Sharma 	&netconsole_target_dev_name.attr,
5200bcc1816SSatyam Sharma 	&netconsole_target_local_port.attr,
5210bcc1816SSatyam Sharma 	&netconsole_target_remote_port.attr,
5220bcc1816SSatyam Sharma 	&netconsole_target_local_ip.attr,
5230bcc1816SSatyam Sharma 	&netconsole_target_remote_ip.attr,
5240bcc1816SSatyam Sharma 	&netconsole_target_local_mac.attr,
5250bcc1816SSatyam Sharma 	&netconsole_target_remote_mac.attr,
5260bcc1816SSatyam Sharma 	NULL,
5270bcc1816SSatyam Sharma };
5280bcc1816SSatyam Sharma 
5290bcc1816SSatyam Sharma /*
5300bcc1816SSatyam Sharma  * Item operations and type for netconsole_target.
5310bcc1816SSatyam Sharma  */
5320bcc1816SSatyam Sharma 
5330bcc1816SSatyam Sharma static void netconsole_target_release(struct config_item *item)
5340bcc1816SSatyam Sharma {
5350bcc1816SSatyam Sharma 	kfree(to_target(item));
5360bcc1816SSatyam Sharma }
5370bcc1816SSatyam Sharma 
5380bcc1816SSatyam Sharma static ssize_t netconsole_target_attr_show(struct config_item *item,
5390bcc1816SSatyam Sharma 					   struct configfs_attribute *attr,
5400bcc1816SSatyam Sharma 					   char *buf)
5410bcc1816SSatyam Sharma {
5420bcc1816SSatyam Sharma 	ssize_t ret = -EINVAL;
5430bcc1816SSatyam Sharma 	struct netconsole_target *nt = to_target(item);
5440bcc1816SSatyam Sharma 	struct netconsole_target_attr *na =
5450bcc1816SSatyam Sharma 		container_of(attr, struct netconsole_target_attr, attr);
5460bcc1816SSatyam Sharma 
5470bcc1816SSatyam Sharma 	if (na->show)
5480bcc1816SSatyam Sharma 		ret = na->show(nt, buf);
5490bcc1816SSatyam Sharma 
5500bcc1816SSatyam Sharma 	return ret;
5510bcc1816SSatyam Sharma }
5520bcc1816SSatyam Sharma 
5530bcc1816SSatyam Sharma static ssize_t netconsole_target_attr_store(struct config_item *item,
5540bcc1816SSatyam Sharma 					    struct configfs_attribute *attr,
5550bcc1816SSatyam Sharma 					    const char *buf,
5560bcc1816SSatyam Sharma 					    size_t count)
5570bcc1816SSatyam Sharma {
5580bcc1816SSatyam Sharma 	ssize_t ret = -EINVAL;
5590bcc1816SSatyam Sharma 	struct netconsole_target *nt = to_target(item);
5600bcc1816SSatyam Sharma 	struct netconsole_target_attr *na =
5610bcc1816SSatyam Sharma 		container_of(attr, struct netconsole_target_attr, attr);
5620bcc1816SSatyam Sharma 
5630bcc1816SSatyam Sharma 	if (na->store)
5640bcc1816SSatyam Sharma 		ret = na->store(nt, buf, count);
5650bcc1816SSatyam Sharma 
5660bcc1816SSatyam Sharma 	return ret;
5670bcc1816SSatyam Sharma }
5680bcc1816SSatyam Sharma 
5690bcc1816SSatyam Sharma static struct configfs_item_operations netconsole_target_item_ops = {
5700bcc1816SSatyam Sharma 	.release		= netconsole_target_release,
5710bcc1816SSatyam Sharma 	.show_attribute		= netconsole_target_attr_show,
5720bcc1816SSatyam Sharma 	.store_attribute	= netconsole_target_attr_store,
5730bcc1816SSatyam Sharma };
5740bcc1816SSatyam Sharma 
5750bcc1816SSatyam Sharma static struct config_item_type netconsole_target_type = {
5760bcc1816SSatyam Sharma 	.ct_attrs		= netconsole_target_attrs,
5770bcc1816SSatyam Sharma 	.ct_item_ops		= &netconsole_target_item_ops,
5780bcc1816SSatyam Sharma 	.ct_owner		= THIS_MODULE,
5790bcc1816SSatyam Sharma };
5800bcc1816SSatyam Sharma 
5810bcc1816SSatyam Sharma /*
5820bcc1816SSatyam Sharma  * Group operations and type for netconsole_subsys.
5830bcc1816SSatyam Sharma  */
5840bcc1816SSatyam Sharma 
585f89ab861SJoel Becker static struct config_item *make_netconsole_target(struct config_group *group,
586f89ab861SJoel Becker 						  const char *name)
5870bcc1816SSatyam Sharma {
5880bcc1816SSatyam Sharma 	unsigned long flags;
5890bcc1816SSatyam Sharma 	struct netconsole_target *nt;
5900bcc1816SSatyam Sharma 
5910bcc1816SSatyam Sharma 	/*
5920bcc1816SSatyam Sharma 	 * Allocate and initialize with defaults.
5930bcc1816SSatyam Sharma 	 * Target is disabled at creation (enabled == 0).
5940bcc1816SSatyam Sharma 	 */
5950bcc1816SSatyam Sharma 	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
5960bcc1816SSatyam Sharma 	if (!nt) {
5970bcc1816SSatyam Sharma 		printk(KERN_ERR "netconsole: failed to allocate memory\n");
598a6795e9eSJoel Becker 		return ERR_PTR(-ENOMEM);
5990bcc1816SSatyam Sharma 	}
6000bcc1816SSatyam Sharma 
6010bcc1816SSatyam Sharma 	nt->np.name = "netconsole";
6020bcc1816SSatyam Sharma 	strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
6030bcc1816SSatyam Sharma 	nt->np.local_port = 6665;
6040bcc1816SSatyam Sharma 	nt->np.remote_port = 6666;
6050bcc1816SSatyam Sharma 	memset(nt->np.remote_mac, 0xff, ETH_ALEN);
6060bcc1816SSatyam Sharma 
6070bcc1816SSatyam Sharma 	/* Initialize the config_item member */
6080bcc1816SSatyam Sharma 	config_item_init_type_name(&nt->item, name, &netconsole_target_type);
6090bcc1816SSatyam Sharma 
6100bcc1816SSatyam Sharma 	/* Adding, but it is disabled */
6110bcc1816SSatyam Sharma 	spin_lock_irqsave(&target_list_lock, flags);
6120bcc1816SSatyam Sharma 	list_add(&nt->list, &target_list);
6130bcc1816SSatyam Sharma 	spin_unlock_irqrestore(&target_list_lock, flags);
6140bcc1816SSatyam Sharma 
615f89ab861SJoel Becker 	return &nt->item;
6160bcc1816SSatyam Sharma }
6170bcc1816SSatyam Sharma 
6180bcc1816SSatyam Sharma static void drop_netconsole_target(struct config_group *group,
6190bcc1816SSatyam Sharma 				   struct config_item *item)
6200bcc1816SSatyam Sharma {
6210bcc1816SSatyam Sharma 	unsigned long flags;
6220bcc1816SSatyam Sharma 	struct netconsole_target *nt = to_target(item);
6230bcc1816SSatyam Sharma 
6240bcc1816SSatyam Sharma 	spin_lock_irqsave(&target_list_lock, flags);
6250bcc1816SSatyam Sharma 	list_del(&nt->list);
6260bcc1816SSatyam Sharma 	spin_unlock_irqrestore(&target_list_lock, flags);
6270bcc1816SSatyam Sharma 
6280bcc1816SSatyam Sharma 	/*
6290bcc1816SSatyam Sharma 	 * The target may have never been enabled, or was manually disabled
6300bcc1816SSatyam Sharma 	 * before being removed so netpoll may have already been cleaned up.
6310bcc1816SSatyam Sharma 	 */
6320bcc1816SSatyam Sharma 	if (nt->enabled)
6330bcc1816SSatyam Sharma 		netpoll_cleanup(&nt->np);
6340bcc1816SSatyam Sharma 
6350bcc1816SSatyam Sharma 	config_item_put(&nt->item);
6360bcc1816SSatyam Sharma }
6370bcc1816SSatyam Sharma 
6380bcc1816SSatyam Sharma static struct configfs_group_operations netconsole_subsys_group_ops = {
6390bcc1816SSatyam Sharma 	.make_item	= make_netconsole_target,
6400bcc1816SSatyam Sharma 	.drop_item	= drop_netconsole_target,
6410bcc1816SSatyam Sharma };
6420bcc1816SSatyam Sharma 
6430bcc1816SSatyam Sharma static struct config_item_type netconsole_subsys_type = {
6440bcc1816SSatyam Sharma 	.ct_group_ops	= &netconsole_subsys_group_ops,
6450bcc1816SSatyam Sharma 	.ct_owner	= THIS_MODULE,
6460bcc1816SSatyam Sharma };
6470bcc1816SSatyam Sharma 
6480bcc1816SSatyam Sharma /* The netconsole configfs subsystem */
6490bcc1816SSatyam Sharma static struct configfs_subsystem netconsole_subsys = {
6500bcc1816SSatyam Sharma 	.su_group	= {
6510bcc1816SSatyam Sharma 		.cg_item	= {
6520bcc1816SSatyam Sharma 			.ci_namebuf	= "netconsole",
6530bcc1816SSatyam Sharma 			.ci_type	= &netconsole_subsys_type,
6540bcc1816SSatyam Sharma 		},
6550bcc1816SSatyam Sharma 	},
6560bcc1816SSatyam Sharma };
6570bcc1816SSatyam Sharma 
6580bcc1816SSatyam Sharma #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
6590bcc1816SSatyam Sharma 
66017951f34SSatyam Sharma /* Handle network interface device notifications */
66117951f34SSatyam Sharma static int netconsole_netdev_event(struct notifier_block *this,
66217951f34SSatyam Sharma 				   unsigned long event,
66317951f34SSatyam Sharma 				   void *ptr)
66417951f34SSatyam Sharma {
665b5427c27SSatyam Sharma 	unsigned long flags;
666b5427c27SSatyam Sharma 	struct netconsole_target *nt;
66717951f34SSatyam Sharma 	struct net_device *dev = ptr;
66817951f34SSatyam Sharma 
66909538641SStephen Hemminger 	if (!(event == NETDEV_CHANGENAME))
670b5427c27SSatyam Sharma 		goto done;
671b5427c27SSatyam Sharma 
672b5427c27SSatyam Sharma 	spin_lock_irqsave(&target_list_lock, flags);
673b5427c27SSatyam Sharma 	list_for_each_entry(nt, &target_list, list) {
6740bcc1816SSatyam Sharma 		netconsole_target_get(nt);
67517951f34SSatyam Sharma 		if (nt->np.dev == dev) {
67617951f34SSatyam Sharma 			switch (event) {
67717951f34SSatyam Sharma 			case NETDEV_CHANGENAME:
67817951f34SSatyam Sharma 				strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ);
67917951f34SSatyam Sharma 				break;
68017951f34SSatyam Sharma 			}
68117951f34SSatyam Sharma 		}
6820bcc1816SSatyam Sharma 		netconsole_target_put(nt);
683b5427c27SSatyam Sharma 	}
684b5427c27SSatyam Sharma 	spin_unlock_irqrestore(&target_list_lock, flags);
68517951f34SSatyam Sharma 
686b5427c27SSatyam Sharma done:
68717951f34SSatyam Sharma 	return NOTIFY_DONE;
68817951f34SSatyam Sharma }
68917951f34SSatyam Sharma 
69017951f34SSatyam Sharma static struct notifier_block netconsole_netdev_notifier = {
69117951f34SSatyam Sharma 	.notifier_call  = netconsole_netdev_event,
69217951f34SSatyam Sharma };
69317951f34SSatyam Sharma 
6941da177e4SLinus Torvalds static void write_msg(struct console *con, const char *msg, unsigned int len)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	int frag, left;
6971da177e4SLinus Torvalds 	unsigned long flags;
698b5427c27SSatyam Sharma 	struct netconsole_target *nt;
699b5427c27SSatyam Sharma 	const char *tmp;
7001da177e4SLinus Torvalds 
701b5427c27SSatyam Sharma 	/* Avoid taking lock and disabling interrupts unnecessarily */
702b5427c27SSatyam Sharma 	if (list_empty(&target_list))
703b5427c27SSatyam Sharma 		return;
704b5427c27SSatyam Sharma 
705b5427c27SSatyam Sharma 	spin_lock_irqsave(&target_list_lock, flags);
706b5427c27SSatyam Sharma 	list_for_each_entry(nt, &target_list, list) {
7070bcc1816SSatyam Sharma 		netconsole_target_get(nt);
7080bcc1816SSatyam Sharma 		if (nt->enabled && netif_running(nt->np.dev)) {
709b5427c27SSatyam Sharma 			/*
710b5427c27SSatyam Sharma 			 * We nest this inside the for-each-target loop above
711b5427c27SSatyam Sharma 			 * so that we're able to get as much logging out to
712b5427c27SSatyam Sharma 			 * at least one target if we die inside here, instead
713b5427c27SSatyam Sharma 			 * of unnecessarily keeping all targets in lock-step.
714b5427c27SSatyam Sharma 			 */
715b5427c27SSatyam Sharma 			tmp = msg;
7161da177e4SLinus Torvalds 			for (left = len; left;) {
7171da177e4SLinus Torvalds 				frag = min(left, MAX_PRINT_CHUNK);
718b5427c27SSatyam Sharma 				netpoll_send_udp(&nt->np, tmp, frag);
719b5427c27SSatyam Sharma 				tmp += frag;
7201da177e4SLinus Torvalds 				left -= frag;
7211da177e4SLinus Torvalds 			}
7221da177e4SLinus Torvalds 		}
7230bcc1816SSatyam Sharma 		netconsole_target_put(nt);
7240cc120beSSatyam Sharma 	}
725b5427c27SSatyam Sharma 	spin_unlock_irqrestore(&target_list_lock, flags);
726b5427c27SSatyam Sharma }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds static struct console netconsole = {
729d938ab44SRandy Dunlap 	.name	= "netcon",
7300517deedSMichael Ellerman 	.flags	= CON_ENABLED,
731d39badf0SSatyam Sharma 	.write	= write_msg,
7321da177e4SLinus Torvalds };
7331da177e4SLinus Torvalds 
734d39badf0SSatyam Sharma static int __init init_netconsole(void)
7351da177e4SLinus Torvalds {
7360bcc1816SSatyam Sharma 	int err;
737b5427c27SSatyam Sharma 	struct netconsole_target *nt, *tmp;
738b5427c27SSatyam Sharma 	unsigned long flags;
739b5427c27SSatyam Sharma 	char *target_config;
740b5427c27SSatyam Sharma 	char *input = config;
741b41848b6SStephen Hemminger 
7420bcc1816SSatyam Sharma 	if (strnlen(input, MAX_PARAM_LENGTH)) {
743b5427c27SSatyam Sharma 		while ((target_config = strsep(&input, ";"))) {
7440bcc1816SSatyam Sharma 			nt = alloc_param_target(target_config);
745b5427c27SSatyam Sharma 			if (IS_ERR(nt)) {
746b5427c27SSatyam Sharma 				err = PTR_ERR(nt);
747b5427c27SSatyam Sharma 				goto fail;
748b5427c27SSatyam Sharma 			}
7490517deedSMichael Ellerman 			/* Dump existing printks when we register */
7500517deedSMichael Ellerman 			netconsole.flags |= CON_PRINTBUFFER;
7510517deedSMichael Ellerman 
752b5427c27SSatyam Sharma 			spin_lock_irqsave(&target_list_lock, flags);
753b5427c27SSatyam Sharma 			list_add(&nt->list, &target_list);
754b5427c27SSatyam Sharma 			spin_unlock_irqrestore(&target_list_lock, flags);
755b5427c27SSatyam Sharma 		}
7560bcc1816SSatyam Sharma 	}
7571da177e4SLinus Torvalds 
75817951f34SSatyam Sharma 	err = register_netdevice_notifier(&netconsole_netdev_notifier);
75917951f34SSatyam Sharma 	if (err)
760b5427c27SSatyam Sharma 		goto fail;
76117951f34SSatyam Sharma 
7620bcc1816SSatyam Sharma 	err = dynamic_netconsole_init();
7630bcc1816SSatyam Sharma 	if (err)
7640bcc1816SSatyam Sharma 		goto undonotifier;
7650bcc1816SSatyam Sharma 
7661da177e4SLinus Torvalds 	register_console(&netconsole);
7671da177e4SLinus Torvalds 	printk(KERN_INFO "netconsole: network logging started\n");
768d39badf0SSatyam Sharma 
769d39badf0SSatyam Sharma 	return err;
770b5427c27SSatyam Sharma 
7710bcc1816SSatyam Sharma undonotifier:
7720bcc1816SSatyam Sharma 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
7730bcc1816SSatyam Sharma 
774b5427c27SSatyam Sharma fail:
775b5427c27SSatyam Sharma 	printk(KERN_ERR "netconsole: cleaning up\n");
776b5427c27SSatyam Sharma 
777b5427c27SSatyam Sharma 	/*
7780bcc1816SSatyam Sharma 	 * Remove all targets and destroy them (only targets created
7790bcc1816SSatyam Sharma 	 * from the boot/module option exist here). Skipping the list
780b5427c27SSatyam Sharma 	 * lock is safe here, and netpoll_cleanup() will sleep.
781b5427c27SSatyam Sharma 	 */
782b5427c27SSatyam Sharma 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
783b5427c27SSatyam Sharma 		list_del(&nt->list);
7840bcc1816SSatyam Sharma 		free_param_target(nt);
785b5427c27SSatyam Sharma 	}
786b5427c27SSatyam Sharma 
787b5427c27SSatyam Sharma 	return err;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
790d39badf0SSatyam Sharma static void __exit cleanup_netconsole(void)
7911da177e4SLinus Torvalds {
792b5427c27SSatyam Sharma 	struct netconsole_target *nt, *tmp;
793df180e36SSatyam Sharma 
7941da177e4SLinus Torvalds 	unregister_console(&netconsole);
7950bcc1816SSatyam Sharma 	dynamic_netconsole_exit();
79617951f34SSatyam Sharma 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
797b5427c27SSatyam Sharma 
798b5427c27SSatyam Sharma 	/*
7990bcc1816SSatyam Sharma 	 * Targets created via configfs pin references on our module
8000bcc1816SSatyam Sharma 	 * and would first be rmdir(2)'ed from userspace. We reach
8010bcc1816SSatyam Sharma 	 * here only when they are already destroyed, and only those
8020bcc1816SSatyam Sharma 	 * created from the boot/module option are left, so remove and
8030bcc1816SSatyam Sharma 	 * destroy them. Skipping the list lock is safe here, and
8040bcc1816SSatyam Sharma 	 * netpoll_cleanup() will sleep.
805b5427c27SSatyam Sharma 	 */
806b5427c27SSatyam Sharma 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
807b5427c27SSatyam Sharma 		list_del(&nt->list);
8080bcc1816SSatyam Sharma 		free_param_target(nt);
809b5427c27SSatyam Sharma 	}
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds module_init(init_netconsole);
8131da177e4SLinus Torvalds module_exit(cleanup_netconsole);
814