1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/net/netconsole.c
7 * This file contains the implementation of an IRQ-safe, crash-safe
8 * kernel console implementation that outputs kernel messages to the
11 * Modification history:
13 * 2001-09-17 started by Ingo Molnar.
14 * 2003-08-11 2.6 port by Matt Mackall
18 * 2003-09-07 rewritten with netpoll api
21 /****************************************************************
23 ****************************************************************/
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/console.h>
32 #include <linux/moduleparam.h>
33 #include <linux/kernel.h>
34 #include <linux/string.h>
35 #include <linux/netpoll.h>
36 #include <linux/inet.h>
37 #include <linux/configfs.h>
38 #include <linux/etherdevice.h>
39 #include <linux/utsname.h>
42 MODULE_DESCRIPTION("Console driver for network interfaces");
43 MODULE_LICENSE("GPL");
45 #define MAX_PARAM_LENGTH 256
46 #define MAX_USERDATA_ENTRY_LENGTH 256
47 #define MAX_USERDATA_VALUE_LENGTH 200
48 /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
49 #define MAX_USERDATA_NAME_LENGTH (MAX_USERDATA_ENTRY_LENGTH - \
50 MAX_USERDATA_VALUE_LENGTH - 3)
51 #define MAX_USERDATA_ITEMS 16
52 #define MAX_PRINT_CHUNK 1000
54 static char config[MAX_PARAM_LENGTH];
55 module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
56 MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
58 static bool oops_only;
59 module_param(oops_only, bool, 0600);
60 MODULE_PARM_DESC(oops_only, "Only log oops messages");
62 #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
65 static int __init option_setup(char *opt)
67 strscpy(config, opt, MAX_PARAM_LENGTH);
70 __setup("netconsole=", option_setup);
73 /* Linked list of all configured targets */
74 static LIST_HEAD(target_list);
76 /* This needs to be a spinlock because write_msg() cannot sleep */
77 static DEFINE_SPINLOCK(target_list_lock);
80 * Console driver for extended netconsoles. Registered on the first use to
81 * avoid unnecessarily enabling ext message formatting.
83 static struct console netconsole_ext;
86 * struct netconsole_target - Represents a configured netconsole target.
87 * @list: Links this target into the target_list.
88 * @group: Links us into the configfs subsystem hierarchy.
89 * @userdata_group: Links to the userdata configfs hierarchy
90 * @userdata_complete: Cached, formatted string of append
91 * @userdata_length: String length of userdata_complete
92 * @enabled: On / off knob to enable / disable target.
93 * Visible from userspace (read-write).
94 * We maintain a strict 1:1 correspondence between this and
95 * whether the corresponding netpoll is active or inactive.
96 * Also, other parameters of a target may be modified at
97 * runtime only when it is disabled (enabled == 0).
98 * @extended: Denotes whether console is extended or not.
99 * @release: Denotes whether kernel release version should be prepended
100 * to the message. Depends on extended console.
101 * @np: The netpoll structure for this target.
102 * Contains the other userspace visible parameters:
103 * dev_name (read-write)
104 * local_port (read-write)
105 * remote_port (read-write)
106 * local_ip (read-write)
107 * remote_ip (read-write)
108 * local_mac (read-only)
109 * remote_mac (read-write)
111 struct netconsole_target {
112 struct list_head list;
113 #ifdef CONFIG_NETCONSOLE_DYNAMIC
114 struct config_group group;
115 struct config_group userdata_group;
116 char userdata_complete[MAX_USERDATA_ENTRY_LENGTH * MAX_USERDATA_ITEMS];
117 size_t userdata_length;
125 #ifdef CONFIG_NETCONSOLE_DYNAMIC
127 static struct configfs_subsystem netconsole_subsys;
128 static DEFINE_MUTEX(dynamic_netconsole_mutex);
130 static int __init dynamic_netconsole_init(void)
132 config_group_init(&netconsole_subsys.su_group);
133 mutex_init(&netconsole_subsys.su_mutex);
134 return configfs_register_subsystem(&netconsole_subsys);
137 static void __exit dynamic_netconsole_exit(void)
139 configfs_unregister_subsystem(&netconsole_subsys);
143 * Targets that were created by parsing the boot/module option string
144 * do not exist in the configfs hierarchy (and have NULL names) and will
145 * never go away, so make these a no-op for them.
147 static void netconsole_target_get(struct netconsole_target *nt)
149 if (config_item_name(&nt->group.cg_item))
150 config_group_get(&nt->group);
153 static void netconsole_target_put(struct netconsole_target *nt)
155 if (config_item_name(&nt->group.cg_item))
156 config_group_put(&nt->group);
159 #else /* !CONFIG_NETCONSOLE_DYNAMIC */
161 static int __init dynamic_netconsole_init(void)
166 static void __exit dynamic_netconsole_exit(void)
171 * No danger of targets going away from under us when dynamic
172 * reconfigurability is off.
174 static void netconsole_target_get(struct netconsole_target *nt)
178 static void netconsole_target_put(struct netconsole_target *nt)
182 static void populate_configfs_item(struct netconsole_target *nt,
186 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
188 /* Allocate and initialize with defaults.
189 * Note that these targets get their config_item fields zeroed-out.
191 static struct netconsole_target *alloc_and_init(void)
193 struct netconsole_target *nt;
195 nt = kzalloc(sizeof(*nt), GFP_KERNEL);
199 if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
201 if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
204 nt->np.name = "netconsole";
205 strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
206 nt->np.local_port = 6665;
207 nt->np.remote_port = 6666;
208 eth_broadcast_addr(nt->np.remote_mac);
213 #ifdef CONFIG_NETCONSOLE_DYNAMIC
216 * Our subsystem hierarchy is:
218 * /sys/kernel/config/netconsole/
238 static struct netconsole_target *to_target(struct config_item *item)
240 struct config_group *cfg_group;
242 cfg_group = to_config_group(item);
245 return container_of(to_config_group(item),
246 struct netconsole_target, group);
249 /* Get rid of possible trailing newline, returning the new length */
250 static void trim_newline(char *s, size_t maxlen)
254 len = strnlen(s, maxlen);
255 if (s[len - 1] == '\n')
260 * Attribute operations for netconsole_target.
263 static ssize_t enabled_show(struct config_item *item, char *buf)
265 return sysfs_emit(buf, "%d\n", to_target(item)->enabled);
268 static ssize_t extended_show(struct config_item *item, char *buf)
270 return sysfs_emit(buf, "%d\n", to_target(item)->extended);
273 static ssize_t release_show(struct config_item *item, char *buf)
275 return sysfs_emit(buf, "%d\n", to_target(item)->release);
278 static ssize_t dev_name_show(struct config_item *item, char *buf)
280 return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
283 static ssize_t local_port_show(struct config_item *item, char *buf)
285 return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);
288 static ssize_t remote_port_show(struct config_item *item, char *buf)
290 return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);
293 static ssize_t local_ip_show(struct config_item *item, char *buf)
295 struct netconsole_target *nt = to_target(item);
298 return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);
300 return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);
303 static ssize_t remote_ip_show(struct config_item *item, char *buf)
305 struct netconsole_target *nt = to_target(item);
308 return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);
310 return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);
313 static ssize_t local_mac_show(struct config_item *item, char *buf)
315 struct net_device *dev = to_target(item)->np.dev;
316 static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
318 return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
321 static ssize_t remote_mac_show(struct config_item *item, char *buf)
323 return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);
327 * This one is special -- targets created through the configfs interface
328 * are not enabled (and the corresponding netpoll activated) by default.
329 * The user is expected to set the desired parameters first (which
330 * would enable him to dynamically add new netpoll targets for new
331 * network interfaces as and when they come up).
333 static ssize_t enabled_store(struct config_item *item,
334 const char *buf, size_t count)
336 struct netconsole_target *nt = to_target(item);
341 mutex_lock(&dynamic_netconsole_mutex);
342 err = kstrtobool(buf, &enabled);
347 if (enabled == nt->enabled) {
348 pr_info("network logging has already %s\n",
349 nt->enabled ? "started" : "stopped");
353 if (enabled) { /* true */
354 if (nt->release && !nt->extended) {
355 pr_err("Not enabling netconsole. Release feature requires extended log message");
359 if (nt->extended && !console_is_registered(&netconsole_ext))
360 register_console(&netconsole_ext);
363 * Skip netpoll_parse_options() -- all the attributes are
364 * already configured via configfs. Just print them out.
366 netpoll_print_options(&nt->np);
368 err = netpoll_setup(&nt->np);
373 pr_info("network logging started\n");
375 /* We need to disable the netconsole before cleaning it up
376 * otherwise we might end up in write_msg() with
377 * nt->np.dev == NULL and nt->enabled == true
379 spin_lock_irqsave(&target_list_lock, flags);
381 spin_unlock_irqrestore(&target_list_lock, flags);
382 netpoll_cleanup(&nt->np);
385 mutex_unlock(&dynamic_netconsole_mutex);
386 return strnlen(buf, count);
388 mutex_unlock(&dynamic_netconsole_mutex);
392 static ssize_t release_store(struct config_item *item, const char *buf,
395 struct netconsole_target *nt = to_target(item);
399 mutex_lock(&dynamic_netconsole_mutex);
401 pr_err("target (%s) is enabled, disable to update parameters\n",
402 config_item_name(&nt->group.cg_item));
407 err = kstrtobool(buf, &release);
411 nt->release = release;
413 mutex_unlock(&dynamic_netconsole_mutex);
414 return strnlen(buf, count);
416 mutex_unlock(&dynamic_netconsole_mutex);
420 static ssize_t extended_store(struct config_item *item, const char *buf,
423 struct netconsole_target *nt = to_target(item);
427 mutex_lock(&dynamic_netconsole_mutex);
429 pr_err("target (%s) is enabled, disable to update parameters\n",
430 config_item_name(&nt->group.cg_item));
435 err = kstrtobool(buf, &extended);
439 nt->extended = extended;
441 mutex_unlock(&dynamic_netconsole_mutex);
442 return strnlen(buf, count);
444 mutex_unlock(&dynamic_netconsole_mutex);
448 static ssize_t dev_name_store(struct config_item *item, const char *buf,
451 struct netconsole_target *nt = to_target(item);
453 mutex_lock(&dynamic_netconsole_mutex);
455 pr_err("target (%s) is enabled, disable to update parameters\n",
456 config_item_name(&nt->group.cg_item));
457 mutex_unlock(&dynamic_netconsole_mutex);
461 strscpy(nt->np.dev_name, buf, IFNAMSIZ);
462 trim_newline(nt->np.dev_name, IFNAMSIZ);
464 mutex_unlock(&dynamic_netconsole_mutex);
465 return strnlen(buf, count);
468 static ssize_t local_port_store(struct config_item *item, const char *buf,
471 struct netconsole_target *nt = to_target(item);
474 mutex_lock(&dynamic_netconsole_mutex);
476 pr_err("target (%s) is enabled, disable to update parameters\n",
477 config_item_name(&nt->group.cg_item));
481 rv = kstrtou16(buf, 10, &nt->np.local_port);
484 mutex_unlock(&dynamic_netconsole_mutex);
485 return strnlen(buf, count);
487 mutex_unlock(&dynamic_netconsole_mutex);
491 static ssize_t remote_port_store(struct config_item *item,
492 const char *buf, size_t count)
494 struct netconsole_target *nt = to_target(item);
497 mutex_lock(&dynamic_netconsole_mutex);
499 pr_err("target (%s) is enabled, disable to update parameters\n",
500 config_item_name(&nt->group.cg_item));
504 rv = kstrtou16(buf, 10, &nt->np.remote_port);
507 mutex_unlock(&dynamic_netconsole_mutex);
508 return strnlen(buf, count);
510 mutex_unlock(&dynamic_netconsole_mutex);
514 static ssize_t local_ip_store(struct config_item *item, const char *buf,
517 struct netconsole_target *nt = to_target(item);
519 mutex_lock(&dynamic_netconsole_mutex);
521 pr_err("target (%s) is enabled, disable to update parameters\n",
522 config_item_name(&nt->group.cg_item));
526 if (strnchr(buf, count, ':')) {
529 if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
530 if (*end && *end != '\n') {
531 pr_err("invalid IPv6 address at: <%c>\n", *end);
539 nt->np.local_ip.ip = in_aton(buf);
544 mutex_unlock(&dynamic_netconsole_mutex);
545 return strnlen(buf, count);
547 mutex_unlock(&dynamic_netconsole_mutex);
551 static ssize_t remote_ip_store(struct config_item *item, const char *buf,
554 struct netconsole_target *nt = to_target(item);
556 mutex_lock(&dynamic_netconsole_mutex);
558 pr_err("target (%s) is enabled, disable to update parameters\n",
559 config_item_name(&nt->group.cg_item));
563 if (strnchr(buf, count, ':')) {
566 if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
567 if (*end && *end != '\n') {
568 pr_err("invalid IPv6 address at: <%c>\n", *end);
576 nt->np.remote_ip.ip = in_aton(buf);
581 mutex_unlock(&dynamic_netconsole_mutex);
582 return strnlen(buf, count);
584 mutex_unlock(&dynamic_netconsole_mutex);
588 static ssize_t remote_mac_store(struct config_item *item, const char *buf,
591 struct netconsole_target *nt = to_target(item);
592 u8 remote_mac[ETH_ALEN];
594 mutex_lock(&dynamic_netconsole_mutex);
596 pr_err("target (%s) is enabled, disable to update parameters\n",
597 config_item_name(&nt->group.cg_item));
601 if (!mac_pton(buf, remote_mac))
603 if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
605 memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
607 mutex_unlock(&dynamic_netconsole_mutex);
608 return strnlen(buf, count);
610 mutex_unlock(&dynamic_netconsole_mutex);
615 struct config_item item;
616 char value[MAX_USERDATA_VALUE_LENGTH];
619 static struct userdatum *to_userdatum(struct config_item *item)
621 return container_of(item, struct userdatum, item);
625 struct config_group group;
628 static struct userdata *to_userdata(struct config_item *item)
630 return container_of(to_config_group(item), struct userdata, group);
633 static struct netconsole_target *userdata_to_target(struct userdata *ud)
635 struct config_group *netconsole_group;
637 netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
638 return to_target(&netconsole_group->cg_item);
641 static ssize_t userdatum_value_show(struct config_item *item, char *buf)
643 return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
646 static void update_userdata(struct netconsole_target *nt)
648 int complete_idx = 0, child_count = 0;
649 struct list_head *entry;
651 /* Clear the current string in case the last userdatum was deleted */
652 nt->userdata_length = 0;
653 nt->userdata_complete[0] = 0;
655 list_for_each(entry, &nt->userdata_group.cg_children) {
656 struct userdatum *udm_item;
657 struct config_item *item;
659 if (child_count >= MAX_USERDATA_ITEMS)
663 item = container_of(entry, struct config_item, ci_entry);
664 udm_item = to_userdatum(item);
666 /* Skip userdata with no value set */
667 if (strnlen(udm_item->value, MAX_USERDATA_VALUE_LENGTH) == 0)
670 /* This doesn't overflow userdata_complete since it will write
671 * one entry length (1/MAX_USERDATA_ITEMS long), entry count is
672 * checked to not exceed MAX items with child_count above
674 complete_idx += scnprintf(&nt->userdata_complete[complete_idx],
675 MAX_USERDATA_ENTRY_LENGTH, " %s=%s\n",
676 item->ci_name, udm_item->value);
678 nt->userdata_length = strnlen(nt->userdata_complete,
679 sizeof(nt->userdata_complete));
682 static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
685 struct userdatum *udm = to_userdatum(item);
686 struct netconsole_target *nt;
690 if (count > MAX_USERDATA_VALUE_LENGTH)
693 mutex_lock(&dynamic_netconsole_mutex);
695 ret = strscpy(udm->value, buf, sizeof(udm->value));
698 trim_newline(udm->value, sizeof(udm->value));
700 ud = to_userdata(item->ci_parent);
701 nt = userdata_to_target(ud);
704 mutex_unlock(&dynamic_netconsole_mutex);
707 mutex_unlock(&dynamic_netconsole_mutex);
711 CONFIGFS_ATTR(userdatum_, value);
713 static struct configfs_attribute *userdatum_attrs[] = {
714 &userdatum_attr_value,
718 static void userdatum_release(struct config_item *item)
720 kfree(to_userdatum(item));
723 static struct configfs_item_operations userdatum_ops = {
724 .release = userdatum_release,
727 static const struct config_item_type userdatum_type = {
728 .ct_item_ops = &userdatum_ops,
729 .ct_attrs = userdatum_attrs,
730 .ct_owner = THIS_MODULE,
733 static struct config_item *userdatum_make_item(struct config_group *group,
736 struct netconsole_target *nt;
737 struct userdatum *udm;
741 if (strlen(name) > MAX_USERDATA_NAME_LENGTH)
742 return ERR_PTR(-ENAMETOOLONG);
744 ud = to_userdata(&group->cg_item);
745 nt = userdata_to_target(ud);
746 child_count = list_count_nodes(&nt->userdata_group.cg_children);
747 if (child_count >= MAX_USERDATA_ITEMS)
748 return ERR_PTR(-ENOSPC);
750 udm = kzalloc(sizeof(*udm), GFP_KERNEL);
752 return ERR_PTR(-ENOMEM);
754 config_item_init_type_name(&udm->item, name, &userdatum_type);
758 static void userdatum_drop(struct config_group *group, struct config_item *item)
760 struct netconsole_target *nt;
763 ud = to_userdata(&group->cg_item);
764 nt = userdata_to_target(ud);
766 mutex_lock(&dynamic_netconsole_mutex);
768 config_item_put(item);
769 mutex_unlock(&dynamic_netconsole_mutex);
772 static struct configfs_attribute *userdata_attrs[] = {
776 static struct configfs_group_operations userdata_ops = {
777 .make_item = userdatum_make_item,
778 .drop_item = userdatum_drop,
781 static struct config_item_type userdata_type = {
782 .ct_item_ops = &userdatum_ops,
783 .ct_group_ops = &userdata_ops,
784 .ct_attrs = userdata_attrs,
785 .ct_owner = THIS_MODULE,
788 CONFIGFS_ATTR(, enabled);
789 CONFIGFS_ATTR(, extended);
790 CONFIGFS_ATTR(, dev_name);
791 CONFIGFS_ATTR(, local_port);
792 CONFIGFS_ATTR(, remote_port);
793 CONFIGFS_ATTR(, local_ip);
794 CONFIGFS_ATTR(, remote_ip);
795 CONFIGFS_ATTR_RO(, local_mac);
796 CONFIGFS_ATTR(, remote_mac);
797 CONFIGFS_ATTR(, release);
799 static struct configfs_attribute *netconsole_target_attrs[] = {
814 * Item operations and type for netconsole_target.
817 static void netconsole_target_release(struct config_item *item)
819 kfree(to_target(item));
822 static struct configfs_item_operations netconsole_target_item_ops = {
823 .release = netconsole_target_release,
826 static const struct config_item_type netconsole_target_type = {
827 .ct_attrs = netconsole_target_attrs,
828 .ct_item_ops = &netconsole_target_item_ops,
829 .ct_owner = THIS_MODULE,
832 static void init_target_config_group(struct netconsole_target *nt,
835 config_group_init_type_name(&nt->group, name, &netconsole_target_type);
836 config_group_init_type_name(&nt->userdata_group, "userdata",
838 configfs_add_default_group(&nt->userdata_group, &nt->group);
841 static struct netconsole_target *find_cmdline_target(const char *name)
843 struct netconsole_target *nt, *ret = NULL;
846 spin_lock_irqsave(&target_list_lock, flags);
847 list_for_each_entry(nt, &target_list, list) {
848 if (!strcmp(nt->group.cg_item.ci_name, name)) {
853 spin_unlock_irqrestore(&target_list_lock, flags);
859 * Group operations and type for netconsole_subsys.
862 static struct config_group *make_netconsole_target(struct config_group *group,
865 struct netconsole_target *nt;
868 /* Checking if a target by this name was created at boot time. If so,
869 * attach a configfs entry to that target. This enables dynamic
872 if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
873 strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
874 nt = find_cmdline_target(name);
876 init_target_config_group(nt, name);
881 nt = alloc_and_init();
883 return ERR_PTR(-ENOMEM);
885 /* Initialize the config_group member */
886 init_target_config_group(nt, name);
888 /* Adding, but it is disabled */
889 spin_lock_irqsave(&target_list_lock, flags);
890 list_add(&nt->list, &target_list);
891 spin_unlock_irqrestore(&target_list_lock, flags);
896 static void drop_netconsole_target(struct config_group *group,
897 struct config_item *item)
900 struct netconsole_target *nt = to_target(item);
902 spin_lock_irqsave(&target_list_lock, flags);
904 spin_unlock_irqrestore(&target_list_lock, flags);
907 * The target may have never been enabled, or was manually disabled
908 * before being removed so netpoll may have already been cleaned up.
911 netpoll_cleanup(&nt->np);
913 config_item_put(&nt->group.cg_item);
916 static struct configfs_group_operations netconsole_subsys_group_ops = {
917 .make_group = make_netconsole_target,
918 .drop_item = drop_netconsole_target,
921 static const struct config_item_type netconsole_subsys_type = {
922 .ct_group_ops = &netconsole_subsys_group_ops,
923 .ct_owner = THIS_MODULE,
926 /* The netconsole configfs subsystem */
927 static struct configfs_subsystem netconsole_subsys = {
930 .ci_namebuf = "netconsole",
931 .ci_type = &netconsole_subsys_type,
936 static void populate_configfs_item(struct netconsole_target *nt,
939 char target_name[16];
941 snprintf(target_name, sizeof(target_name), "%s%d",
942 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
943 init_target_config_group(nt, target_name);
946 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
948 /* Handle network interface device notifications */
949 static int netconsole_netdev_event(struct notifier_block *this,
950 unsigned long event, void *ptr)
953 struct netconsole_target *nt;
954 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
955 bool stopped = false;
957 if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
958 event == NETDEV_RELEASE || event == NETDEV_JOIN))
961 spin_lock_irqsave(&target_list_lock, flags);
963 list_for_each_entry(nt, &target_list, list) {
964 netconsole_target_get(nt);
965 if (nt->np.dev == dev) {
967 case NETDEV_CHANGENAME:
968 strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
972 case NETDEV_UNREGISTER:
973 /* rtnl_lock already held
974 * we might sleep in __netpoll_cleanup()
977 spin_unlock_irqrestore(&target_list_lock, flags);
979 __netpoll_cleanup(&nt->np);
981 spin_lock_irqsave(&target_list_lock, flags);
982 netdev_put(nt->np.dev, &nt->np.dev_tracker);
985 netconsole_target_put(nt);
989 netconsole_target_put(nt);
991 spin_unlock_irqrestore(&target_list_lock, flags);
993 const char *msg = "had an event";
996 case NETDEV_UNREGISTER:
997 msg = "unregistered";
1000 msg = "released slaves";
1003 msg = "is joining a master device";
1006 pr_info("network logging stopped on interface %s as it %s\n",
1014 static struct notifier_block netconsole_netdev_notifier = {
1015 .notifier_call = netconsole_netdev_event,
1019 * send_ext_msg_udp - send extended log message to target
1020 * @nt: target to send message to
1021 * @msg: extended log message to send
1022 * @msg_len: length of message
1024 * Transfer extended log @msg to @nt. If @msg is longer than
1025 * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
1026 * ncfrag header field added to identify them.
1028 static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
1031 static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
1032 const char *header, *body;
1034 int header_len, body_len;
1035 const char *msg_ready = msg;
1036 const char *release;
1037 int release_len = 0;
1038 int userdata_len = 0;
1039 char *userdata = NULL;
1041 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1042 userdata = nt->userdata_complete;
1043 userdata_len = nt->userdata_length;
1047 release = init_utsname()->release;
1048 release_len = strlen(release) + 1;
1051 if (msg_len + release_len + userdata_len <= MAX_PRINT_CHUNK) {
1052 /* No fragmentation needed */
1054 scnprintf(buf, MAX_PRINT_CHUNK, "%s,%s", release, msg);
1055 msg_len += release_len;
1057 memcpy(buf, msg, msg_len);
1061 msg_len += scnprintf(&buf[msg_len],
1062 MAX_PRINT_CHUNK - msg_len,
1066 netpoll_send_udp(&nt->np, msg_ready, msg_len);
1070 /* need to insert extra header fields, detect header and body */
1072 body = memchr(msg, ';', msg_len);
1073 if (WARN_ON_ONCE(!body))
1076 header_len = body - header;
1077 body_len = msg_len - header_len - 1;
1081 * Transfer multiple chunks with the following extra header.
1082 * "ncfrag=<byte-offset>/<total-bytes>"
1085 scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
1086 memcpy(buf + release_len, header, header_len);
1087 header_len += release_len;
1089 while (offset < body_len + userdata_len) {
1090 int this_header = header_len;
1091 int this_offset = 0;
1094 this_header += scnprintf(buf + this_header,
1095 sizeof(buf) - this_header,
1096 ",ncfrag=%d/%d;", offset,
1097 body_len + userdata_len);
1099 /* Not all body data has been written yet */
1100 if (offset < body_len) {
1101 this_chunk = min(body_len - offset,
1102 MAX_PRINT_CHUNK - this_header);
1103 if (WARN_ON_ONCE(this_chunk <= 0))
1105 memcpy(buf + this_header, body + offset, this_chunk);
1106 this_offset += this_chunk;
1108 /* Body is fully written and there is pending userdata to write,
1109 * append userdata in this chunk
1111 if (offset + this_offset >= body_len &&
1112 offset + this_offset < userdata_len + body_len) {
1113 int sent_userdata = (offset + this_offset) - body_len;
1114 int preceding_bytes = this_chunk + this_header;
1116 if (WARN_ON_ONCE(sent_userdata < 0))
1119 this_chunk = min(userdata_len - sent_userdata,
1120 MAX_PRINT_CHUNK - preceding_bytes);
1121 if (WARN_ON_ONCE(this_chunk <= 0))
1123 memcpy(buf + this_header + this_offset,
1124 userdata + sent_userdata,
1126 this_offset += this_chunk;
1129 netpoll_send_udp(&nt->np, buf, this_header + this_offset);
1130 offset += this_offset;
1134 static void write_ext_msg(struct console *con, const char *msg,
1137 struct netconsole_target *nt;
1138 unsigned long flags;
1140 if ((oops_only && !oops_in_progress) || list_empty(&target_list))
1143 spin_lock_irqsave(&target_list_lock, flags);
1144 list_for_each_entry(nt, &target_list, list)
1145 if (nt->extended && nt->enabled && netif_running(nt->np.dev))
1146 send_ext_msg_udp(nt, msg, len);
1147 spin_unlock_irqrestore(&target_list_lock, flags);
1150 static void write_msg(struct console *con, const char *msg, unsigned int len)
1153 unsigned long flags;
1154 struct netconsole_target *nt;
1157 if (oops_only && !oops_in_progress)
1159 /* Avoid taking lock and disabling interrupts unnecessarily */
1160 if (list_empty(&target_list))
1163 spin_lock_irqsave(&target_list_lock, flags);
1164 list_for_each_entry(nt, &target_list, list) {
1165 if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
1167 * We nest this inside the for-each-target loop above
1168 * so that we're able to get as much logging out to
1169 * at least one target if we die inside here, instead
1170 * of unnecessarily keeping all targets in lock-step.
1173 for (left = len; left;) {
1174 frag = min(left, MAX_PRINT_CHUNK);
1175 netpoll_send_udp(&nt->np, tmp, frag);
1181 spin_unlock_irqrestore(&target_list_lock, flags);
1184 /* Allocate new target (from boot/module param) and setup netpoll for it */
1185 static struct netconsole_target *alloc_param_target(char *target_config,
1188 struct netconsole_target *nt;
1191 nt = alloc_and_init();
1197 if (*target_config == '+') {
1198 nt->extended = true;
1202 if (*target_config == 'r') {
1203 if (!nt->extended) {
1204 pr_err("Netconsole configuration error. Release feature requires extended log message");
1212 /* Parse parameters and setup netpoll */
1213 err = netpoll_parse_options(&nt->np, target_config);
1217 err = netpoll_setup(&nt->np);
1221 populate_configfs_item(nt, cmdline_count);
1228 return ERR_PTR(err);
1231 /* Cleanup netpoll for given target (from boot/module param) and free it */
1232 static void free_param_target(struct netconsole_target *nt)
1234 netpoll_cleanup(&nt->np);
1238 static struct console netconsole_ext = {
1239 .name = "netcon_ext",
1240 .flags = CON_ENABLED | CON_EXTENDED,
1241 .write = write_ext_msg,
1244 static struct console netconsole = {
1246 .flags = CON_ENABLED,
1250 static int __init init_netconsole(void)
1253 struct netconsole_target *nt, *tmp;
1254 unsigned int count = 0;
1255 bool extended = false;
1256 unsigned long flags;
1257 char *target_config;
1258 char *input = config;
1260 if (strnlen(input, MAX_PARAM_LENGTH)) {
1261 while ((target_config = strsep(&input, ";"))) {
1262 nt = alloc_param_target(target_config, count);
1264 if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
1269 /* Dump existing printks when we register */
1272 netconsole_ext.flags |= CON_PRINTBUFFER;
1274 netconsole.flags |= CON_PRINTBUFFER;
1277 spin_lock_irqsave(&target_list_lock, flags);
1278 list_add(&nt->list, &target_list);
1279 spin_unlock_irqrestore(&target_list_lock, flags);
1284 err = register_netdevice_notifier(&netconsole_netdev_notifier);
1288 err = dynamic_netconsole_init();
1293 register_console(&netconsole_ext);
1294 register_console(&netconsole);
1295 pr_info("network logging started\n");
1300 unregister_netdevice_notifier(&netconsole_netdev_notifier);
1303 pr_err("cleaning up\n");
1306 * Remove all targets and destroy them (only targets created
1307 * from the boot/module option exist here). Skipping the list
1308 * lock is safe here, and netpoll_cleanup() will sleep.
1310 list_for_each_entry_safe(nt, tmp, &target_list, list) {
1311 list_del(&nt->list);
1312 free_param_target(nt);
1318 static void __exit cleanup_netconsole(void)
1320 struct netconsole_target *nt, *tmp;
1322 if (console_is_registered(&netconsole_ext))
1323 unregister_console(&netconsole_ext);
1324 unregister_console(&netconsole);
1325 dynamic_netconsole_exit();
1326 unregister_netdevice_notifier(&netconsole_netdev_notifier);
1329 * Targets created via configfs pin references on our module
1330 * and would first be rmdir(2)'ed from userspace. We reach
1331 * here only when they are already destroyed, and only those
1332 * created from the boot/module option are left, so remove and
1333 * destroy them. Skipping the list lock is safe here, and
1334 * netpoll_cleanup() will sleep.
1336 list_for_each_entry_safe(nt, tmp, &target_list, list) {
1337 list_del(&nt->list);
1338 free_param_target(nt);
1343 * Use late_initcall to ensure netconsole is
1344 * initialized after network device driver if built-in.
1346 * late_initcall() and module_init() are identical if built as module.
1348 late_initcall(init_netconsole);
1349 module_exit(cleanup_netconsole);