xref: /openbmc/linux/drivers/net/netconsole.c (revision 369e5a88)
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 
3722ded577SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3822ded577SJoe Perches 
391da177e4SLinus Torvalds #include <linux/mm.h>
401da177e4SLinus Torvalds #include <linux/init.h>
411da177e4SLinus Torvalds #include <linux/module.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
431da177e4SLinus Torvalds #include <linux/console.h>
441da177e4SLinus Torvalds #include <linux/moduleparam.h>
454cd5773aSAndy Shevchenko #include <linux/kernel.h>
461da177e4SLinus Torvalds #include <linux/string.h>
471da177e4SLinus Torvalds #include <linux/netpoll.h>
480bcc1816SSatyam Sharma #include <linux/inet.h>
490bcc1816SSatyam Sharma #include <linux/configfs.h>
501667c942SJoe Perches #include <linux/etherdevice.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
531da177e4SLinus Torvalds MODULE_DESCRIPTION("Console driver for network interfaces");
541da177e4SLinus Torvalds MODULE_LICENSE("GPL");
551da177e4SLinus Torvalds 
56d39badf0SSatyam Sharma #define MAX_PARAM_LENGTH	256
57d39badf0SSatyam Sharma #define MAX_PRINT_CHUNK		1000
58d39badf0SSatyam Sharma 
59d39badf0SSatyam Sharma static char config[MAX_PARAM_LENGTH];
60d39badf0SSatyam Sharma module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
6161a2d07dSNiels de Vos MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
621da177e4SLinus Torvalds 
63c1a60851SAmerigo Wang static bool oops_only = false;
64c1a60851SAmerigo Wang module_param(oops_only, bool, 0600);
65c1a60851SAmerigo Wang MODULE_PARM_DESC(oops_only, "Only log oops messages");
66c1a60851SAmerigo Wang 
67d2b60881SSatyam Sharma #ifndef	MODULE
68d2b60881SSatyam Sharma static int __init option_setup(char *opt)
69d2b60881SSatyam Sharma {
70d2b60881SSatyam Sharma 	strlcpy(config, opt, MAX_PARAM_LENGTH);
71d2b60881SSatyam Sharma 	return 1;
72d2b60881SSatyam Sharma }
73d2b60881SSatyam Sharma __setup("netconsole=", option_setup);
74d2b60881SSatyam Sharma #endif	/* MODULE */
75d2b60881SSatyam Sharma 
76b5427c27SSatyam Sharma /* Linked list of all configured targets */
77b5427c27SSatyam Sharma static LIST_HEAD(target_list);
78b5427c27SSatyam Sharma 
79b5427c27SSatyam Sharma /* This needs to be a spinlock because write_msg() cannot sleep */
80b5427c27SSatyam Sharma static DEFINE_SPINLOCK(target_list_lock);
81b5427c27SSatyam Sharma 
82df180e36SSatyam Sharma /**
83df180e36SSatyam Sharma  * struct netconsole_target - Represents a configured netconsole target.
84b5427c27SSatyam Sharma  * @list:	Links this target into the target_list.
850bcc1816SSatyam Sharma  * @item:	Links us into the configfs subsystem hierarchy.
860bcc1816SSatyam Sharma  * @enabled:	On / off knob to enable / disable target.
870bcc1816SSatyam Sharma  *		Visible from userspace (read-write).
880bcc1816SSatyam Sharma  *		We maintain a strict 1:1 correspondence between this and
890bcc1816SSatyam Sharma  *		whether the corresponding netpoll is active or inactive.
900bcc1816SSatyam Sharma  *		Also, other parameters of a target may be modified at
910bcc1816SSatyam Sharma  *		runtime only when it is disabled (enabled == 0).
92df180e36SSatyam Sharma  * @np:		The netpoll structure for this target.
930bcc1816SSatyam Sharma  *		Contains the other userspace visible parameters:
940bcc1816SSatyam Sharma  *		dev_name	(read-write)
950bcc1816SSatyam Sharma  *		local_port	(read-write)
960bcc1816SSatyam Sharma  *		remote_port	(read-write)
970bcc1816SSatyam Sharma  *		local_ip	(read-write)
980bcc1816SSatyam Sharma  *		remote_ip	(read-write)
990bcc1816SSatyam Sharma  *		local_mac	(read-only)
1000bcc1816SSatyam Sharma  *		remote_mac	(read-write)
101df180e36SSatyam Sharma  */
102df180e36SSatyam Sharma struct netconsole_target {
103b5427c27SSatyam Sharma 	struct list_head	list;
1040bcc1816SSatyam Sharma #ifdef	CONFIG_NETCONSOLE_DYNAMIC
1050bcc1816SSatyam Sharma 	struct config_item	item;
1060bcc1816SSatyam Sharma #endif
107698cf1c6STejun Heo 	bool			enabled;
108df180e36SSatyam Sharma 	struct netpoll		np;
109df180e36SSatyam Sharma };
110df180e36SSatyam Sharma 
1110bcc1816SSatyam Sharma #ifdef	CONFIG_NETCONSOLE_DYNAMIC
1120bcc1816SSatyam Sharma 
1130bcc1816SSatyam Sharma static struct configfs_subsystem netconsole_subsys;
114369e5a88STejun Heo static DEFINE_MUTEX(dynamic_netconsole_mutex);
1150bcc1816SSatyam Sharma 
1160bcc1816SSatyam Sharma static int __init dynamic_netconsole_init(void)
1170bcc1816SSatyam Sharma {
1180bcc1816SSatyam Sharma 	config_group_init(&netconsole_subsys.su_group);
1190bcc1816SSatyam Sharma 	mutex_init(&netconsole_subsys.su_mutex);
1200bcc1816SSatyam Sharma 	return configfs_register_subsystem(&netconsole_subsys);
1210bcc1816SSatyam Sharma }
1220bcc1816SSatyam Sharma 
1230bcc1816SSatyam Sharma static void __exit dynamic_netconsole_exit(void)
1240bcc1816SSatyam Sharma {
1250bcc1816SSatyam Sharma 	configfs_unregister_subsystem(&netconsole_subsys);
1260bcc1816SSatyam Sharma }
1270bcc1816SSatyam Sharma 
1280bcc1816SSatyam Sharma /*
1290bcc1816SSatyam Sharma  * Targets that were created by parsing the boot/module option string
1300bcc1816SSatyam Sharma  * do not exist in the configfs hierarchy (and have NULL names) and will
1310bcc1816SSatyam Sharma  * never go away, so make these a no-op for them.
1320bcc1816SSatyam Sharma  */
1330bcc1816SSatyam Sharma static void netconsole_target_get(struct netconsole_target *nt)
1340bcc1816SSatyam Sharma {
1350bcc1816SSatyam Sharma 	if (config_item_name(&nt->item))
1360bcc1816SSatyam Sharma 		config_item_get(&nt->item);
1370bcc1816SSatyam Sharma }
1380bcc1816SSatyam Sharma 
1390bcc1816SSatyam Sharma static void netconsole_target_put(struct netconsole_target *nt)
1400bcc1816SSatyam Sharma {
1410bcc1816SSatyam Sharma 	if (config_item_name(&nt->item))
1420bcc1816SSatyam Sharma 		config_item_put(&nt->item);
1430bcc1816SSatyam Sharma }
1440bcc1816SSatyam Sharma 
1450bcc1816SSatyam Sharma #else	/* !CONFIG_NETCONSOLE_DYNAMIC */
1460bcc1816SSatyam Sharma 
1470bcc1816SSatyam Sharma static int __init dynamic_netconsole_init(void)
1480bcc1816SSatyam Sharma {
1490bcc1816SSatyam Sharma 	return 0;
1500bcc1816SSatyam Sharma }
1510bcc1816SSatyam Sharma 
1520bcc1816SSatyam Sharma static void __exit dynamic_netconsole_exit(void)
1530bcc1816SSatyam Sharma {
1540bcc1816SSatyam Sharma }
1550bcc1816SSatyam Sharma 
1560bcc1816SSatyam Sharma /*
1570bcc1816SSatyam Sharma  * No danger of targets going away from under us when dynamic
1580bcc1816SSatyam Sharma  * reconfigurability is off.
1590bcc1816SSatyam Sharma  */
1600bcc1816SSatyam Sharma static void netconsole_target_get(struct netconsole_target *nt)
1610bcc1816SSatyam Sharma {
1620bcc1816SSatyam Sharma }
1630bcc1816SSatyam Sharma 
1640bcc1816SSatyam Sharma static void netconsole_target_put(struct netconsole_target *nt)
1650bcc1816SSatyam Sharma {
1660bcc1816SSatyam Sharma }
1670bcc1816SSatyam Sharma 
1680bcc1816SSatyam Sharma #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
1690bcc1816SSatyam Sharma 
1700bcc1816SSatyam Sharma /* Allocate new target (from boot/module param) and setup netpoll for it */
1710bcc1816SSatyam Sharma static struct netconsole_target *alloc_param_target(char *target_config)
172b5427c27SSatyam Sharma {
173b5427c27SSatyam Sharma 	int err = -ENOMEM;
174b5427c27SSatyam Sharma 	struct netconsole_target *nt;
175b5427c27SSatyam Sharma 
1760bcc1816SSatyam Sharma 	/*
1770bcc1816SSatyam Sharma 	 * Allocate and initialize with defaults.
1780bcc1816SSatyam Sharma 	 * Note that these targets get their config_item fields zeroed-out.
1790bcc1816SSatyam Sharma 	 */
180b5427c27SSatyam Sharma 	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
181e404decbSJoe Perches 	if (!nt)
182b5427c27SSatyam Sharma 		goto fail;
183b5427c27SSatyam Sharma 
184b5427c27SSatyam Sharma 	nt->np.name = "netconsole";
185b5427c27SSatyam Sharma 	strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
186b5427c27SSatyam Sharma 	nt->np.local_port = 6665;
187b5427c27SSatyam Sharma 	nt->np.remote_port = 6666;
1881667c942SJoe Perches 	eth_broadcast_addr(nt->np.remote_mac);
189b5427c27SSatyam Sharma 
190b5427c27SSatyam Sharma 	/* Parse parameters and setup netpoll */
191b5427c27SSatyam Sharma 	err = netpoll_parse_options(&nt->np, target_config);
192b5427c27SSatyam Sharma 	if (err)
193b5427c27SSatyam Sharma 		goto fail;
194b5427c27SSatyam Sharma 
195b5427c27SSatyam Sharma 	err = netpoll_setup(&nt->np);
196b5427c27SSatyam Sharma 	if (err)
197b5427c27SSatyam Sharma 		goto fail;
198b5427c27SSatyam Sharma 
199698cf1c6STejun Heo 	nt->enabled = true;
2000bcc1816SSatyam Sharma 
201b5427c27SSatyam Sharma 	return nt;
202b5427c27SSatyam Sharma 
203b5427c27SSatyam Sharma fail:
204b5427c27SSatyam Sharma 	kfree(nt);
205b5427c27SSatyam Sharma 	return ERR_PTR(err);
206b5427c27SSatyam Sharma }
207b5427c27SSatyam Sharma 
2080bcc1816SSatyam Sharma /* Cleanup netpoll for given target (from boot/module param) and free it */
2090bcc1816SSatyam Sharma static void free_param_target(struct netconsole_target *nt)
210b5427c27SSatyam Sharma {
211b5427c27SSatyam Sharma 	netpoll_cleanup(&nt->np);
212b5427c27SSatyam Sharma 	kfree(nt);
213b5427c27SSatyam Sharma }
2141da177e4SLinus Torvalds 
2150bcc1816SSatyam Sharma #ifdef	CONFIG_NETCONSOLE_DYNAMIC
2160bcc1816SSatyam Sharma 
2170bcc1816SSatyam Sharma /*
2180bcc1816SSatyam Sharma  * Our subsystem hierarchy is:
2190bcc1816SSatyam Sharma  *
2200bcc1816SSatyam Sharma  * /sys/kernel/config/netconsole/
2210bcc1816SSatyam Sharma  *				|
2220bcc1816SSatyam Sharma  *				<target>/
2230bcc1816SSatyam Sharma  *				|	enabled
2240bcc1816SSatyam Sharma  *				|	dev_name
2250bcc1816SSatyam Sharma  *				|	local_port
2260bcc1816SSatyam Sharma  *				|	remote_port
2270bcc1816SSatyam Sharma  *				|	local_ip
2280bcc1816SSatyam Sharma  *				|	remote_ip
2290bcc1816SSatyam Sharma  *				|	local_mac
2300bcc1816SSatyam Sharma  *				|	remote_mac
2310bcc1816SSatyam Sharma  *				|
2320bcc1816SSatyam Sharma  *				<target>/...
2330bcc1816SSatyam Sharma  */
2340bcc1816SSatyam Sharma 
2350bcc1816SSatyam Sharma struct netconsole_target_attr {
2360bcc1816SSatyam Sharma 	struct configfs_attribute	attr;
2370bcc1816SSatyam Sharma 	ssize_t				(*show)(struct netconsole_target *nt,
2380bcc1816SSatyam Sharma 						char *buf);
2390bcc1816SSatyam Sharma 	ssize_t				(*store)(struct netconsole_target *nt,
2400bcc1816SSatyam Sharma 						 const char *buf,
2410bcc1816SSatyam Sharma 						 size_t count);
2420bcc1816SSatyam Sharma };
2430bcc1816SSatyam Sharma 
2440bcc1816SSatyam Sharma static struct netconsole_target *to_target(struct config_item *item)
2450bcc1816SSatyam Sharma {
2460bcc1816SSatyam Sharma 	return item ?
2470bcc1816SSatyam Sharma 		container_of(item, struct netconsole_target, item) :
2480bcc1816SSatyam Sharma 		NULL;
2490bcc1816SSatyam Sharma }
2500bcc1816SSatyam Sharma 
2510bcc1816SSatyam Sharma /*
2520bcc1816SSatyam Sharma  * Attribute operations for netconsole_target.
2530bcc1816SSatyam Sharma  */
2540bcc1816SSatyam Sharma 
2550bcc1816SSatyam Sharma static ssize_t show_enabled(struct netconsole_target *nt, char *buf)
2560bcc1816SSatyam Sharma {
2570bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%d\n", nt->enabled);
2580bcc1816SSatyam Sharma }
2590bcc1816SSatyam Sharma 
2600bcc1816SSatyam Sharma static ssize_t show_dev_name(struct netconsole_target *nt, char *buf)
2610bcc1816SSatyam Sharma {
2620bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%s\n", nt->np.dev_name);
2630bcc1816SSatyam Sharma }
2640bcc1816SSatyam Sharma 
2650bcc1816SSatyam Sharma static ssize_t show_local_port(struct netconsole_target *nt, char *buf)
2660bcc1816SSatyam Sharma {
2670bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.local_port);
2680bcc1816SSatyam Sharma }
2690bcc1816SSatyam Sharma 
2700bcc1816SSatyam Sharma static ssize_t show_remote_port(struct netconsole_target *nt, char *buf)
2710bcc1816SSatyam Sharma {
2720bcc1816SSatyam Sharma 	return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.remote_port);
2730bcc1816SSatyam Sharma }
2740bcc1816SSatyam Sharma 
2750bcc1816SSatyam Sharma static ssize_t show_local_ip(struct netconsole_target *nt, char *buf)
2760bcc1816SSatyam Sharma {
277b3d936f3SCong Wang 	if (nt->np.ipv6)
278b3d936f3SCong Wang 		return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.local_ip.in6);
279b3d936f3SCong Wang 	else
280e7557af5SHarvey Harrison 		return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip);
2810bcc1816SSatyam Sharma }
2820bcc1816SSatyam Sharma 
2830bcc1816SSatyam Sharma static ssize_t show_remote_ip(struct netconsole_target *nt, char *buf)
2840bcc1816SSatyam Sharma {
285b3d936f3SCong Wang 	if (nt->np.ipv6)
286b3d936f3SCong Wang 		return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.remote_ip.in6);
287b3d936f3SCong Wang 	else
288e7557af5SHarvey Harrison 		return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip);
2890bcc1816SSatyam Sharma }
2900bcc1816SSatyam Sharma 
2910bcc1816SSatyam Sharma static ssize_t show_local_mac(struct netconsole_target *nt, char *buf)
2920bcc1816SSatyam Sharma {
29309538641SStephen Hemminger 	struct net_device *dev = nt->np.dev;
294e174961cSJohannes Berg 	static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
29509538641SStephen Hemminger 
296e174961cSJohannes Berg 	return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast);
2970bcc1816SSatyam Sharma }
2980bcc1816SSatyam Sharma 
2990bcc1816SSatyam Sharma static ssize_t show_remote_mac(struct netconsole_target *nt, char *buf)
3000bcc1816SSatyam Sharma {
301e174961cSJohannes Berg 	return snprintf(buf, PAGE_SIZE, "%pM\n", nt->np.remote_mac);
3020bcc1816SSatyam Sharma }
3030bcc1816SSatyam Sharma 
3040bcc1816SSatyam Sharma /*
3050bcc1816SSatyam Sharma  * This one is special -- targets created through the configfs interface
3060bcc1816SSatyam Sharma  * are not enabled (and the corresponding netpoll activated) by default.
3070bcc1816SSatyam Sharma  * The user is expected to set the desired parameters first (which
3080bcc1816SSatyam Sharma  * would enable him to dynamically add new netpoll targets for new
3090bcc1816SSatyam Sharma  * network interfaces as and when they come up).
3100bcc1816SSatyam Sharma  */
3110bcc1816SSatyam Sharma static ssize_t store_enabled(struct netconsole_target *nt,
3120bcc1816SSatyam Sharma 			     const char *buf,
3130bcc1816SSatyam Sharma 			     size_t count)
3140bcc1816SSatyam Sharma {
31545e526e8SNikolay Aleksandrov 	unsigned long flags;
31699f823f9SAlexey Dobriyan 	int enabled;
3170bcc1816SSatyam Sharma 	int err;
3180bcc1816SSatyam Sharma 
31999f823f9SAlexey Dobriyan 	err = kstrtoint(buf, 10, &enabled);
32099f823f9SAlexey Dobriyan 	if (err < 0)
32199f823f9SAlexey Dobriyan 		return err;
32299f823f9SAlexey Dobriyan 	if (enabled < 0 || enabled > 1)
32399f823f9SAlexey Dobriyan 		return -EINVAL;
324698cf1c6STejun Heo 	if ((bool)enabled == nt->enabled) {
32522ded577SJoe Perches 		pr_info("network logging has already %s\n",
326d5123480SGao feng 			nt->enabled ? "started" : "stopped");
327d5123480SGao feng 		return -EINVAL;
328d5123480SGao feng 	}
3290bcc1816SSatyam Sharma 
330698cf1c6STejun Heo 	if (enabled) {	/* true */
3310bcc1816SSatyam Sharma 		/*
3320bcc1816SSatyam Sharma 		 * Skip netpoll_parse_options() -- all the attributes are
3330bcc1816SSatyam Sharma 		 * already configured via configfs. Just print them out.
3340bcc1816SSatyam Sharma 		 */
3350bcc1816SSatyam Sharma 		netpoll_print_options(&nt->np);
3360bcc1816SSatyam Sharma 
3370bcc1816SSatyam Sharma 		err = netpoll_setup(&nt->np);
338c7c6effdSNikolay Aleksandrov 		if (err)
3390bcc1816SSatyam Sharma 			return err;
3400bcc1816SSatyam Sharma 
341394efd19SDavid S. Miller 		pr_info("netconsole: network logging started\n");
342698cf1c6STejun Heo 	} else {	/* false */
34345e526e8SNikolay Aleksandrov 		/* We need to disable the netconsole before cleaning it up
34445e526e8SNikolay Aleksandrov 		 * otherwise we might end up in write_msg() with
345698cf1c6STejun Heo 		 * nt->np.dev == NULL and nt->enabled == true
34645e526e8SNikolay Aleksandrov 		 */
34745e526e8SNikolay Aleksandrov 		spin_lock_irqsave(&target_list_lock, flags);
348698cf1c6STejun Heo 		nt->enabled = false;
34945e526e8SNikolay Aleksandrov 		spin_unlock_irqrestore(&target_list_lock, flags);
3500bcc1816SSatyam Sharma 		netpoll_cleanup(&nt->np);
3510bcc1816SSatyam Sharma 	}
3520bcc1816SSatyam Sharma 
3530bcc1816SSatyam Sharma 	nt->enabled = enabled;
3540bcc1816SSatyam Sharma 
3550bcc1816SSatyam Sharma 	return strnlen(buf, count);
3560bcc1816SSatyam Sharma }
3570bcc1816SSatyam Sharma 
3580bcc1816SSatyam Sharma static ssize_t store_dev_name(struct netconsole_target *nt,
3590bcc1816SSatyam Sharma 			      const char *buf,
3600bcc1816SSatyam Sharma 			      size_t count)
3610bcc1816SSatyam Sharma {
3620bcc1816SSatyam Sharma 	size_t len;
3630bcc1816SSatyam Sharma 
3640bcc1816SSatyam Sharma 	if (nt->enabled) {
36522ded577SJoe Perches 		pr_err("target (%s) is enabled, disable to update parameters\n",
3660bcc1816SSatyam Sharma 		       config_item_name(&nt->item));
3670bcc1816SSatyam Sharma 		return -EINVAL;
3680bcc1816SSatyam Sharma 	}
3690bcc1816SSatyam Sharma 
3700bcc1816SSatyam Sharma 	strlcpy(nt->np.dev_name, buf, IFNAMSIZ);
3710bcc1816SSatyam Sharma 
3720bcc1816SSatyam Sharma 	/* Get rid of possible trailing newline from echo(1) */
3730bcc1816SSatyam Sharma 	len = strnlen(nt->np.dev_name, IFNAMSIZ);
3740bcc1816SSatyam Sharma 	if (nt->np.dev_name[len - 1] == '\n')
3750bcc1816SSatyam Sharma 		nt->np.dev_name[len - 1] = '\0';
3760bcc1816SSatyam Sharma 
3770bcc1816SSatyam Sharma 	return strnlen(buf, count);
3780bcc1816SSatyam Sharma }
3790bcc1816SSatyam Sharma 
3800bcc1816SSatyam Sharma static ssize_t store_local_port(struct netconsole_target *nt,
3810bcc1816SSatyam Sharma 				const char *buf,
3820bcc1816SSatyam Sharma 				size_t count)
3830bcc1816SSatyam Sharma {
38499f823f9SAlexey Dobriyan 	int rv;
3850bcc1816SSatyam Sharma 
3860bcc1816SSatyam Sharma 	if (nt->enabled) {
38722ded577SJoe Perches 		pr_err("target (%s) is enabled, disable to update parameters\n",
3880bcc1816SSatyam Sharma 		       config_item_name(&nt->item));
3890bcc1816SSatyam Sharma 		return -EINVAL;
3900bcc1816SSatyam Sharma 	}
3910bcc1816SSatyam Sharma 
39299f823f9SAlexey Dobriyan 	rv = kstrtou16(buf, 10, &nt->np.local_port);
39399f823f9SAlexey Dobriyan 	if (rv < 0)
39499f823f9SAlexey Dobriyan 		return rv;
3950bcc1816SSatyam Sharma 	return strnlen(buf, count);
3960bcc1816SSatyam Sharma }
3970bcc1816SSatyam Sharma 
3980bcc1816SSatyam Sharma static ssize_t store_remote_port(struct netconsole_target *nt,
3990bcc1816SSatyam Sharma 				 const char *buf,
4000bcc1816SSatyam Sharma 				 size_t count)
4010bcc1816SSatyam Sharma {
40299f823f9SAlexey Dobriyan 	int rv;
4030bcc1816SSatyam Sharma 
4040bcc1816SSatyam Sharma 	if (nt->enabled) {
40522ded577SJoe Perches 		pr_err("target (%s) is enabled, disable to update parameters\n",
4060bcc1816SSatyam Sharma 		       config_item_name(&nt->item));
4070bcc1816SSatyam Sharma 		return -EINVAL;
4080bcc1816SSatyam Sharma 	}
4090bcc1816SSatyam Sharma 
41099f823f9SAlexey Dobriyan 	rv = kstrtou16(buf, 10, &nt->np.remote_port);
41199f823f9SAlexey Dobriyan 	if (rv < 0)
41299f823f9SAlexey Dobriyan 		return rv;
4130bcc1816SSatyam Sharma 	return strnlen(buf, count);
4140bcc1816SSatyam Sharma }
4150bcc1816SSatyam Sharma 
4160bcc1816SSatyam Sharma static ssize_t store_local_ip(struct netconsole_target *nt,
4170bcc1816SSatyam Sharma 			      const char *buf,
4180bcc1816SSatyam Sharma 			      size_t count)
4190bcc1816SSatyam Sharma {
4200bcc1816SSatyam Sharma 	if (nt->enabled) {
42122ded577SJoe Perches 		pr_err("target (%s) is enabled, disable to update parameters\n",
4220bcc1816SSatyam Sharma 		       config_item_name(&nt->item));
4230bcc1816SSatyam Sharma 		return -EINVAL;
4240bcc1816SSatyam Sharma 	}
4250bcc1816SSatyam Sharma 
426b3d936f3SCong Wang 	if (strnchr(buf, count, ':')) {
427b3d936f3SCong Wang 		const char *end;
428b3d936f3SCong Wang 		if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
429b3d936f3SCong Wang 			if (*end && *end != '\n') {
43022ded577SJoe Perches 				pr_err("invalid IPv6 address at: <%c>\n", *end);
431b3d936f3SCong Wang 				return -EINVAL;
432b3d936f3SCong Wang 			}
433b3d936f3SCong Wang 			nt->np.ipv6 = true;
434b3d936f3SCong Wang 		} else
435b3d936f3SCong Wang 			return -EINVAL;
436b3d936f3SCong Wang 	} else {
437b3d936f3SCong Wang 		if (!nt->np.ipv6) {
438b7394d24SCong Wang 			nt->np.local_ip.ip = in_aton(buf);
439b3d936f3SCong Wang 		} else
440b3d936f3SCong Wang 			return -EINVAL;
441b3d936f3SCong Wang 	}
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) {
45122ded577SJoe Perches 		pr_err("target (%s) is enabled, disable to update parameters\n",
4520bcc1816SSatyam Sharma 		       config_item_name(&nt->item));
4530bcc1816SSatyam Sharma 		return -EINVAL;
4540bcc1816SSatyam Sharma 	}
4550bcc1816SSatyam Sharma 
456b3d936f3SCong Wang 	if (strnchr(buf, count, ':')) {
457b3d936f3SCong Wang 		const char *end;
458b3d936f3SCong Wang 		if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
459b3d936f3SCong Wang 			if (*end && *end != '\n') {
46022ded577SJoe Perches 				pr_err("invalid IPv6 address at: <%c>\n", *end);
461b3d936f3SCong Wang 				return -EINVAL;
462b3d936f3SCong Wang 			}
463b3d936f3SCong Wang 			nt->np.ipv6 = true;
464b3d936f3SCong Wang 		} else
465b3d936f3SCong Wang 			return -EINVAL;
466b3d936f3SCong Wang 	} else {
467b3d936f3SCong Wang 		if (!nt->np.ipv6) {
468b7394d24SCong Wang 			nt->np.remote_ip.ip = in_aton(buf);
469b3d936f3SCong Wang 		} else
470b3d936f3SCong Wang 			return -EINVAL;
471b3d936f3SCong Wang 	}
4720bcc1816SSatyam Sharma 
4730bcc1816SSatyam Sharma 	return strnlen(buf, count);
4740bcc1816SSatyam Sharma }
4750bcc1816SSatyam Sharma 
4760bcc1816SSatyam Sharma static ssize_t store_remote_mac(struct netconsole_target *nt,
4770bcc1816SSatyam Sharma 				const char *buf,
4780bcc1816SSatyam Sharma 				size_t count)
4790bcc1816SSatyam Sharma {
4800bcc1816SSatyam Sharma 	u8 remote_mac[ETH_ALEN];
4810bcc1816SSatyam Sharma 
4820bcc1816SSatyam Sharma 	if (nt->enabled) {
48322ded577SJoe Perches 		pr_err("target (%s) is enabled, disable to update parameters\n",
4840bcc1816SSatyam Sharma 		       config_item_name(&nt->item));
4850bcc1816SSatyam Sharma 		return -EINVAL;
4860bcc1816SSatyam Sharma 	}
4870bcc1816SSatyam Sharma 
4884940fc88SAlexey Dobriyan 	if (!mac_pton(buf, remote_mac))
4894940fc88SAlexey Dobriyan 		return -EINVAL;
4904940fc88SAlexey Dobriyan 	if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
4914940fc88SAlexey Dobriyan 		return -EINVAL;
4920bcc1816SSatyam Sharma 	memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
4930bcc1816SSatyam Sharma 
4940bcc1816SSatyam Sharma 	return strnlen(buf, count);
4950bcc1816SSatyam Sharma }
4960bcc1816SSatyam Sharma 
4970bcc1816SSatyam Sharma /*
4980bcc1816SSatyam Sharma  * Attribute definitions for netconsole_target.
4990bcc1816SSatyam Sharma  */
5000bcc1816SSatyam Sharma 
5010bcc1816SSatyam Sharma #define NETCONSOLE_TARGET_ATTR_RO(_name)				\
5020bcc1816SSatyam Sharma static struct netconsole_target_attr netconsole_target_##_name =	\
5030bcc1816SSatyam Sharma 	__CONFIGFS_ATTR(_name, S_IRUGO, show_##_name, NULL)
5040bcc1816SSatyam Sharma 
5050bcc1816SSatyam Sharma #define NETCONSOLE_TARGET_ATTR_RW(_name)				\
5060bcc1816SSatyam Sharma static struct netconsole_target_attr netconsole_target_##_name =	\
5070bcc1816SSatyam Sharma 	__CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name)
5080bcc1816SSatyam Sharma 
5090bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(enabled);
5100bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(dev_name);
5110bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(local_port);
5120bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(remote_port);
5130bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(local_ip);
5140bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(remote_ip);
5150bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RO(local_mac);
5160bcc1816SSatyam Sharma NETCONSOLE_TARGET_ATTR_RW(remote_mac);
5170bcc1816SSatyam Sharma 
5180bcc1816SSatyam Sharma static struct configfs_attribute *netconsole_target_attrs[] = {
5190bcc1816SSatyam Sharma 	&netconsole_target_enabled.attr,
5200bcc1816SSatyam Sharma 	&netconsole_target_dev_name.attr,
5210bcc1816SSatyam Sharma 	&netconsole_target_local_port.attr,
5220bcc1816SSatyam Sharma 	&netconsole_target_remote_port.attr,
5230bcc1816SSatyam Sharma 	&netconsole_target_local_ip.attr,
5240bcc1816SSatyam Sharma 	&netconsole_target_remote_ip.attr,
5250bcc1816SSatyam Sharma 	&netconsole_target_local_mac.attr,
5260bcc1816SSatyam Sharma 	&netconsole_target_remote_mac.attr,
5270bcc1816SSatyam Sharma 	NULL,
5280bcc1816SSatyam Sharma };
5290bcc1816SSatyam Sharma 
5300bcc1816SSatyam Sharma /*
5310bcc1816SSatyam Sharma  * Item operations and type for netconsole_target.
5320bcc1816SSatyam Sharma  */
5330bcc1816SSatyam Sharma 
5340bcc1816SSatyam Sharma static void netconsole_target_release(struct config_item *item)
5350bcc1816SSatyam Sharma {
5360bcc1816SSatyam Sharma 	kfree(to_target(item));
5370bcc1816SSatyam Sharma }
5380bcc1816SSatyam Sharma 
5390bcc1816SSatyam Sharma static ssize_t netconsole_target_attr_show(struct config_item *item,
5400bcc1816SSatyam Sharma 					   struct configfs_attribute *attr,
5410bcc1816SSatyam Sharma 					   char *buf)
5420bcc1816SSatyam Sharma {
5430bcc1816SSatyam Sharma 	ssize_t ret = -EINVAL;
5440bcc1816SSatyam Sharma 	struct netconsole_target *nt = to_target(item);
5450bcc1816SSatyam Sharma 	struct netconsole_target_attr *na =
5460bcc1816SSatyam Sharma 		container_of(attr, struct netconsole_target_attr, attr);
5470bcc1816SSatyam Sharma 
5480bcc1816SSatyam Sharma 	if (na->show)
5490bcc1816SSatyam Sharma 		ret = na->show(nt, buf);
5500bcc1816SSatyam Sharma 
5510bcc1816SSatyam Sharma 	return ret;
5520bcc1816SSatyam Sharma }
5530bcc1816SSatyam Sharma 
5540bcc1816SSatyam Sharma static ssize_t netconsole_target_attr_store(struct config_item *item,
5550bcc1816SSatyam Sharma 					    struct configfs_attribute *attr,
5560bcc1816SSatyam Sharma 					    const char *buf,
5570bcc1816SSatyam Sharma 					    size_t count)
5580bcc1816SSatyam Sharma {
5590bcc1816SSatyam Sharma 	ssize_t ret = -EINVAL;
5600bcc1816SSatyam Sharma 	struct netconsole_target *nt = to_target(item);
5610bcc1816SSatyam Sharma 	struct netconsole_target_attr *na =
5620bcc1816SSatyam Sharma 		container_of(attr, struct netconsole_target_attr, attr);
5630bcc1816SSatyam Sharma 
564369e5a88STejun Heo 	mutex_lock(&dynamic_netconsole_mutex);
5650bcc1816SSatyam Sharma 	if (na->store)
5660bcc1816SSatyam Sharma 		ret = na->store(nt, buf, count);
567369e5a88STejun Heo 	mutex_unlock(&dynamic_netconsole_mutex);
5680bcc1816SSatyam Sharma 
5690bcc1816SSatyam Sharma 	return ret;
5700bcc1816SSatyam Sharma }
5710bcc1816SSatyam Sharma 
5720bcc1816SSatyam Sharma static struct configfs_item_operations netconsole_target_item_ops = {
5730bcc1816SSatyam Sharma 	.release		= netconsole_target_release,
5740bcc1816SSatyam Sharma 	.show_attribute		= netconsole_target_attr_show,
5750bcc1816SSatyam Sharma 	.store_attribute	= netconsole_target_attr_store,
5760bcc1816SSatyam Sharma };
5770bcc1816SSatyam Sharma 
5780bcc1816SSatyam Sharma static struct config_item_type netconsole_target_type = {
5790bcc1816SSatyam Sharma 	.ct_attrs		= netconsole_target_attrs,
5800bcc1816SSatyam Sharma 	.ct_item_ops		= &netconsole_target_item_ops,
5810bcc1816SSatyam Sharma 	.ct_owner		= THIS_MODULE,
5820bcc1816SSatyam Sharma };
5830bcc1816SSatyam Sharma 
5840bcc1816SSatyam Sharma /*
5850bcc1816SSatyam Sharma  * Group operations and type for netconsole_subsys.
5860bcc1816SSatyam Sharma  */
5870bcc1816SSatyam Sharma 
588f89ab861SJoel Becker static struct config_item *make_netconsole_target(struct config_group *group,
589f89ab861SJoel Becker 						  const char *name)
5900bcc1816SSatyam Sharma {
5910bcc1816SSatyam Sharma 	unsigned long flags;
5920bcc1816SSatyam Sharma 	struct netconsole_target *nt;
5930bcc1816SSatyam Sharma 
5940bcc1816SSatyam Sharma 	/*
5950bcc1816SSatyam Sharma 	 * Allocate and initialize with defaults.
596698cf1c6STejun Heo 	 * Target is disabled at creation (!enabled).
5970bcc1816SSatyam Sharma 	 */
5980bcc1816SSatyam Sharma 	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
599e404decbSJoe Perches 	if (!nt)
600a6795e9eSJoel Becker 		return ERR_PTR(-ENOMEM);
6010bcc1816SSatyam Sharma 
6020bcc1816SSatyam Sharma 	nt->np.name = "netconsole";
6030bcc1816SSatyam Sharma 	strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
6040bcc1816SSatyam Sharma 	nt->np.local_port = 6665;
6050bcc1816SSatyam Sharma 	nt->np.remote_port = 6666;
6061667c942SJoe Perches 	eth_broadcast_addr(nt->np.remote_mac);
6070bcc1816SSatyam Sharma 
6080bcc1816SSatyam Sharma 	/* Initialize the config_item member */
6090bcc1816SSatyam Sharma 	config_item_init_type_name(&nt->item, name, &netconsole_target_type);
6100bcc1816SSatyam Sharma 
6110bcc1816SSatyam Sharma 	/* Adding, but it is disabled */
6120bcc1816SSatyam Sharma 	spin_lock_irqsave(&target_list_lock, flags);
6130bcc1816SSatyam Sharma 	list_add(&nt->list, &target_list);
6140bcc1816SSatyam Sharma 	spin_unlock_irqrestore(&target_list_lock, flags);
6150bcc1816SSatyam Sharma 
616f89ab861SJoel Becker 	return &nt->item;
6170bcc1816SSatyam Sharma }
6180bcc1816SSatyam Sharma 
6190bcc1816SSatyam Sharma static void drop_netconsole_target(struct config_group *group,
6200bcc1816SSatyam Sharma 				   struct config_item *item)
6210bcc1816SSatyam Sharma {
6220bcc1816SSatyam Sharma 	unsigned long flags;
6230bcc1816SSatyam Sharma 	struct netconsole_target *nt = to_target(item);
6240bcc1816SSatyam Sharma 
6250bcc1816SSatyam Sharma 	spin_lock_irqsave(&target_list_lock, flags);
6260bcc1816SSatyam Sharma 	list_del(&nt->list);
6270bcc1816SSatyam Sharma 	spin_unlock_irqrestore(&target_list_lock, flags);
6280bcc1816SSatyam Sharma 
6290bcc1816SSatyam Sharma 	/*
6300bcc1816SSatyam Sharma 	 * The target may have never been enabled, or was manually disabled
6310bcc1816SSatyam Sharma 	 * before being removed so netpoll may have already been cleaned up.
6320bcc1816SSatyam Sharma 	 */
6330bcc1816SSatyam Sharma 	if (nt->enabled)
6340bcc1816SSatyam Sharma 		netpoll_cleanup(&nt->np);
6350bcc1816SSatyam Sharma 
6360bcc1816SSatyam Sharma 	config_item_put(&nt->item);
6370bcc1816SSatyam Sharma }
6380bcc1816SSatyam Sharma 
6390bcc1816SSatyam Sharma static struct configfs_group_operations netconsole_subsys_group_ops = {
6400bcc1816SSatyam Sharma 	.make_item	= make_netconsole_target,
6410bcc1816SSatyam Sharma 	.drop_item	= drop_netconsole_target,
6420bcc1816SSatyam Sharma };
6430bcc1816SSatyam Sharma 
6440bcc1816SSatyam Sharma static struct config_item_type netconsole_subsys_type = {
6450bcc1816SSatyam Sharma 	.ct_group_ops	= &netconsole_subsys_group_ops,
6460bcc1816SSatyam Sharma 	.ct_owner	= THIS_MODULE,
6470bcc1816SSatyam Sharma };
6480bcc1816SSatyam Sharma 
6490bcc1816SSatyam Sharma /* The netconsole configfs subsystem */
6500bcc1816SSatyam Sharma static struct configfs_subsystem netconsole_subsys = {
6510bcc1816SSatyam Sharma 	.su_group	= {
6520bcc1816SSatyam Sharma 		.cg_item	= {
6530bcc1816SSatyam Sharma 			.ci_namebuf	= "netconsole",
6540bcc1816SSatyam Sharma 			.ci_type	= &netconsole_subsys_type,
6550bcc1816SSatyam Sharma 		},
6560bcc1816SSatyam Sharma 	},
6570bcc1816SSatyam Sharma };
6580bcc1816SSatyam Sharma 
6590bcc1816SSatyam Sharma #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
6600bcc1816SSatyam Sharma 
66117951f34SSatyam Sharma /* Handle network interface device notifications */
66217951f34SSatyam Sharma static int netconsole_netdev_event(struct notifier_block *this,
663351638e7SJiri Pirko 				   unsigned long event, void *ptr)
66417951f34SSatyam Sharma {
665b5427c27SSatyam Sharma 	unsigned long flags;
666b5427c27SSatyam Sharma 	struct netconsole_target *nt;
667351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
668141dfba3SFerenc Wagner 	bool stopped = false;
66917951f34SSatyam Sharma 
6700e34e931SWANG Cong 	if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
671daf9209bSAmerigo Wang 	      event == NETDEV_RELEASE || event == NETDEV_JOIN))
672b5427c27SSatyam Sharma 		goto done;
673b5427c27SSatyam Sharma 
674b5427c27SSatyam Sharma 	spin_lock_irqsave(&target_list_lock, flags);
6753f315befSVeaceslav Falico restart:
676b5427c27SSatyam Sharma 	list_for_each_entry(nt, &target_list, list) {
6770bcc1816SSatyam Sharma 		netconsole_target_get(nt);
67817951f34SSatyam Sharma 		if (nt->np.dev == dev) {
67917951f34SSatyam Sharma 			switch (event) {
68017951f34SSatyam Sharma 			case NETDEV_CHANGENAME:
68117951f34SSatyam Sharma 				strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ);
68217951f34SSatyam Sharma 				break;
683daf9209bSAmerigo Wang 			case NETDEV_RELEASE:
6848d8fc29dSAmerigo Wang 			case NETDEV_JOIN:
6852382b15bSBruno Prémont 			case NETDEV_UNREGISTER:
686c71380ffSNikolay Aleksandrov 				/* rtnl_lock already held
6873f315befSVeaceslav Falico 				 * we might sleep in __netpoll_cleanup()
6883b410a31SNeil Horman 				 */
6893f315befSVeaceslav Falico 				spin_unlock_irqrestore(&target_list_lock, flags);
6907a163bfbSDan Aloni 
6913b410a31SNeil Horman 				__netpoll_cleanup(&nt->np);
6927a163bfbSDan Aloni 
6933f315befSVeaceslav Falico 				spin_lock_irqsave(&target_list_lock, flags);
6943b410a31SNeil Horman 				dev_put(nt->np.dev);
6953b410a31SNeil Horman 				nt->np.dev = NULL;
696698cf1c6STejun Heo 				nt->enabled = false;
697141dfba3SFerenc Wagner 				stopped = true;
6983f315befSVeaceslav Falico 				netconsole_target_put(nt);
6993f315befSVeaceslav Falico 				goto restart;
70017951f34SSatyam Sharma 			}
70117951f34SSatyam Sharma 		}
7020bcc1816SSatyam Sharma 		netconsole_target_put(nt);
703b5427c27SSatyam Sharma 	}
704b5427c27SSatyam Sharma 	spin_unlock_irqrestore(&target_list_lock, flags);
7058d8fc29dSAmerigo Wang 	if (stopped) {
70622ded577SJoe Perches 		const char *msg = "had an event";
7078d8fc29dSAmerigo Wang 		switch (event) {
7088d8fc29dSAmerigo Wang 		case NETDEV_UNREGISTER:
70922ded577SJoe Perches 			msg = "unregistered";
7108d8fc29dSAmerigo Wang 			break;
711daf9209bSAmerigo Wang 		case NETDEV_RELEASE:
71222ded577SJoe Perches 			msg = "released slaves";
7138d8fc29dSAmerigo Wang 			break;
7148d8fc29dSAmerigo Wang 		case NETDEV_JOIN:
71522ded577SJoe Perches 			msg = "is joining a master device";
7168d8fc29dSAmerigo Wang 			break;
7178d8fc29dSAmerigo Wang 		}
71822ded577SJoe Perches 		pr_info("network logging stopped on interface %s as it %s\n",
71922ded577SJoe Perches 			dev->name, msg);
7208d8fc29dSAmerigo Wang 	}
72117951f34SSatyam Sharma 
722b5427c27SSatyam Sharma done:
72317951f34SSatyam Sharma 	return NOTIFY_DONE;
72417951f34SSatyam Sharma }
72517951f34SSatyam Sharma 
72617951f34SSatyam Sharma static struct notifier_block netconsole_netdev_notifier = {
72717951f34SSatyam Sharma 	.notifier_call  = netconsole_netdev_event,
72817951f34SSatyam Sharma };
72917951f34SSatyam Sharma 
7301da177e4SLinus Torvalds static void write_msg(struct console *con, const char *msg, unsigned int len)
7311da177e4SLinus Torvalds {
7321da177e4SLinus Torvalds 	int frag, left;
7331da177e4SLinus Torvalds 	unsigned long flags;
734b5427c27SSatyam Sharma 	struct netconsole_target *nt;
735b5427c27SSatyam Sharma 	const char *tmp;
7361da177e4SLinus Torvalds 
737c1a60851SAmerigo Wang 	if (oops_only && !oops_in_progress)
738c1a60851SAmerigo Wang 		return;
739b5427c27SSatyam Sharma 	/* Avoid taking lock and disabling interrupts unnecessarily */
740b5427c27SSatyam Sharma 	if (list_empty(&target_list))
741b5427c27SSatyam Sharma 		return;
742b5427c27SSatyam Sharma 
743b5427c27SSatyam Sharma 	spin_lock_irqsave(&target_list_lock, flags);
744b5427c27SSatyam Sharma 	list_for_each_entry(nt, &target_list, list) {
7450bcc1816SSatyam Sharma 		if (nt->enabled && netif_running(nt->np.dev)) {
746b5427c27SSatyam Sharma 			/*
747b5427c27SSatyam Sharma 			 * We nest this inside the for-each-target loop above
748b5427c27SSatyam Sharma 			 * so that we're able to get as much logging out to
749b5427c27SSatyam Sharma 			 * at least one target if we die inside here, instead
750b5427c27SSatyam Sharma 			 * of unnecessarily keeping all targets in lock-step.
751b5427c27SSatyam Sharma 			 */
752b5427c27SSatyam Sharma 			tmp = msg;
7531da177e4SLinus Torvalds 			for (left = len; left;) {
7541da177e4SLinus Torvalds 				frag = min(left, MAX_PRINT_CHUNK);
755b5427c27SSatyam Sharma 				netpoll_send_udp(&nt->np, tmp, frag);
756b5427c27SSatyam Sharma 				tmp += frag;
7571da177e4SLinus Torvalds 				left -= frag;
7581da177e4SLinus Torvalds 			}
7591da177e4SLinus Torvalds 		}
7600cc120beSSatyam Sharma 	}
761b5427c27SSatyam Sharma 	spin_unlock_irqrestore(&target_list_lock, flags);
762b5427c27SSatyam Sharma }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds static struct console netconsole = {
765d938ab44SRandy Dunlap 	.name	= "netcon",
7660517deedSMichael Ellerman 	.flags	= CON_ENABLED,
767d39badf0SSatyam Sharma 	.write	= write_msg,
7681da177e4SLinus Torvalds };
7691da177e4SLinus Torvalds 
770d39badf0SSatyam Sharma static int __init init_netconsole(void)
7711da177e4SLinus Torvalds {
7720bcc1816SSatyam Sharma 	int err;
773b5427c27SSatyam Sharma 	struct netconsole_target *nt, *tmp;
774b5427c27SSatyam Sharma 	unsigned long flags;
775b5427c27SSatyam Sharma 	char *target_config;
776b5427c27SSatyam Sharma 	char *input = config;
777b41848b6SStephen Hemminger 
7780bcc1816SSatyam Sharma 	if (strnlen(input, MAX_PARAM_LENGTH)) {
779b5427c27SSatyam Sharma 		while ((target_config = strsep(&input, ";"))) {
7800bcc1816SSatyam Sharma 			nt = alloc_param_target(target_config);
781b5427c27SSatyam Sharma 			if (IS_ERR(nt)) {
782b5427c27SSatyam Sharma 				err = PTR_ERR(nt);
783b5427c27SSatyam Sharma 				goto fail;
784b5427c27SSatyam Sharma 			}
7850517deedSMichael Ellerman 			/* Dump existing printks when we register */
7860517deedSMichael Ellerman 			netconsole.flags |= CON_PRINTBUFFER;
7870517deedSMichael Ellerman 
788b5427c27SSatyam Sharma 			spin_lock_irqsave(&target_list_lock, flags);
789b5427c27SSatyam Sharma 			list_add(&nt->list, &target_list);
790b5427c27SSatyam Sharma 			spin_unlock_irqrestore(&target_list_lock, flags);
791b5427c27SSatyam Sharma 		}
7920bcc1816SSatyam Sharma 	}
7931da177e4SLinus Torvalds 
79417951f34SSatyam Sharma 	err = register_netdevice_notifier(&netconsole_netdev_notifier);
79517951f34SSatyam Sharma 	if (err)
796b5427c27SSatyam Sharma 		goto fail;
79717951f34SSatyam Sharma 
7980bcc1816SSatyam Sharma 	err = dynamic_netconsole_init();
7990bcc1816SSatyam Sharma 	if (err)
8000bcc1816SSatyam Sharma 		goto undonotifier;
8010bcc1816SSatyam Sharma 
8021da177e4SLinus Torvalds 	register_console(&netconsole);
80322ded577SJoe Perches 	pr_info("network logging started\n");
804d39badf0SSatyam Sharma 
805d39badf0SSatyam Sharma 	return err;
806b5427c27SSatyam Sharma 
8070bcc1816SSatyam Sharma undonotifier:
8080bcc1816SSatyam Sharma 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
8090bcc1816SSatyam Sharma 
810b5427c27SSatyam Sharma fail:
81122ded577SJoe Perches 	pr_err("cleaning up\n");
812b5427c27SSatyam Sharma 
813b5427c27SSatyam Sharma 	/*
8140bcc1816SSatyam Sharma 	 * Remove all targets and destroy them (only targets created
8150bcc1816SSatyam Sharma 	 * from the boot/module option exist here). Skipping the list
816b5427c27SSatyam Sharma 	 * lock is safe here, and netpoll_cleanup() will sleep.
817b5427c27SSatyam Sharma 	 */
818b5427c27SSatyam Sharma 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
819b5427c27SSatyam Sharma 		list_del(&nt->list);
8200bcc1816SSatyam Sharma 		free_param_target(nt);
821b5427c27SSatyam Sharma 	}
822b5427c27SSatyam Sharma 
823b5427c27SSatyam Sharma 	return err;
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
826d39badf0SSatyam Sharma static void __exit cleanup_netconsole(void)
8271da177e4SLinus Torvalds {
828b5427c27SSatyam Sharma 	struct netconsole_target *nt, *tmp;
829df180e36SSatyam Sharma 
8301da177e4SLinus Torvalds 	unregister_console(&netconsole);
8310bcc1816SSatyam Sharma 	dynamic_netconsole_exit();
83217951f34SSatyam Sharma 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
833b5427c27SSatyam Sharma 
834b5427c27SSatyam Sharma 	/*
8350bcc1816SSatyam Sharma 	 * Targets created via configfs pin references on our module
8360bcc1816SSatyam Sharma 	 * and would first be rmdir(2)'ed from userspace. We reach
8370bcc1816SSatyam Sharma 	 * here only when they are already destroyed, and only those
8380bcc1816SSatyam Sharma 	 * created from the boot/module option are left, so remove and
8390bcc1816SSatyam Sharma 	 * destroy them. Skipping the list lock is safe here, and
8400bcc1816SSatyam Sharma 	 * netpoll_cleanup() will sleep.
841b5427c27SSatyam Sharma 	 */
842b5427c27SSatyam Sharma 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
843b5427c27SSatyam Sharma 		list_del(&nt->list);
8440bcc1816SSatyam Sharma 		free_param_target(nt);
845b5427c27SSatyam Sharma 	}
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
84897c7de05SLin Ming /*
84997c7de05SLin Ming  * Use late_initcall to ensure netconsole is
85097c7de05SLin Ming  * initialized after network device driver if built-in.
85197c7de05SLin Ming  *
85297c7de05SLin Ming  * late_initcall() and module_init() are identical if built as module.
85397c7de05SLin Ming  */
85497c7de05SLin Ming late_initcall(init_netconsole);
8551da177e4SLinus Torvalds module_exit(cleanup_netconsole);
856