Commit | Line | Data |
---|---|---|
de6cc651 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * linux/drivers/net/netconsole.c | |
4 | * | |
5 | * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com> | |
6 | * | |
7 | * This file contains the implementation of an IRQ-safe, crash-safe | |
8 | * kernel console implementation that outputs kernel messages to the | |
9 | * network. | |
10 | * | |
11 | * Modification history: | |
12 | * | |
13 | * 2001-09-17 started by Ingo Molnar. | |
14 | * 2003-08-11 2.6 port by Matt Mackall | |
15 | * simplified options | |
16 | * generic card hooks | |
17 | * works non-modular | |
18 | * 2003-09-07 rewritten with netpoll api | |
19 | */ | |
20 | ||
21 | /**************************************************************** | |
1da177e4 LT |
22 | * |
23 | ****************************************************************/ | |
24 | ||
22ded577 JP |
25 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
26 | ||
1da177e4 | 27 | #include <linux/mm.h> |
1da177e4 LT |
28 | #include <linux/init.h> |
29 | #include <linux/module.h> | |
5a0e3ad6 | 30 | #include <linux/slab.h> |
1da177e4 | 31 | #include <linux/console.h> |
1da177e4 | 32 | #include <linux/moduleparam.h> |
4cd5773a | 33 | #include <linux/kernel.h> |
1da177e4 | 34 | #include <linux/string.h> |
1da177e4 | 35 | #include <linux/netpoll.h> |
0bcc1816 SS |
36 | #include <linux/inet.h> |
37 | #include <linux/configfs.h> | |
1667c942 | 38 | #include <linux/etherdevice.h> |
1da177e4 LT |
39 | |
40 | MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>"); | |
41 | MODULE_DESCRIPTION("Console driver for network interfaces"); | |
42 | MODULE_LICENSE("GPL"); | |
43 | ||
d39badf0 SS |
44 | #define MAX_PARAM_LENGTH 256 |
45 | #define MAX_PRINT_CHUNK 1000 | |
46 | ||
47 | static char config[MAX_PARAM_LENGTH]; | |
48 | module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0); | |
61a2d07d | 49 | MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]"); |
1da177e4 | 50 | |
c1a60851 AW |
51 | static bool oops_only = false; |
52 | module_param(oops_only, bool, 0600); | |
53 | MODULE_PARM_DESC(oops_only, "Only log oops messages"); | |
54 | ||
d2b60881 SS |
55 | #ifndef MODULE |
56 | static int __init option_setup(char *opt) | |
57 | { | |
fb3ceec1 | 58 | strscpy(config, opt, MAX_PARAM_LENGTH); |
d2b60881 SS |
59 | return 1; |
60 | } | |
61 | __setup("netconsole=", option_setup); | |
62 | #endif /* MODULE */ | |
63 | ||
b5427c27 SS |
64 | /* Linked list of all configured targets */ |
65 | static LIST_HEAD(target_list); | |
66 | ||
67 | /* This needs to be a spinlock because write_msg() cannot sleep */ | |
68 | static DEFINE_SPINLOCK(target_list_lock); | |
69 | ||
e2f15f9a TH |
70 | /* |
71 | * Console driver for extended netconsoles. Registered on the first use to | |
72 | * avoid unnecessarily enabling ext message formatting. | |
73 | */ | |
74 | static struct console netconsole_ext; | |
75 | ||
df180e36 SS |
76 | /** |
77 | * struct netconsole_target - Represents a configured netconsole target. | |
b5427c27 | 78 | * @list: Links this target into the target_list. |
0bcc1816 SS |
79 | * @item: Links us into the configfs subsystem hierarchy. |
80 | * @enabled: On / off knob to enable / disable target. | |
81 | * Visible from userspace (read-write). | |
82 | * We maintain a strict 1:1 correspondence between this and | |
83 | * whether the corresponding netpoll is active or inactive. | |
84 | * Also, other parameters of a target may be modified at | |
85 | * runtime only when it is disabled (enabled == 0). | |
a8eb1a00 | 86 | * @extended: Denotes whether console is extended or not. |
df180e36 | 87 | * @np: The netpoll structure for this target. |
0bcc1816 SS |
88 | * Contains the other userspace visible parameters: |
89 | * dev_name (read-write) | |
90 | * local_port (read-write) | |
91 | * remote_port (read-write) | |
92 | * local_ip (read-write) | |
93 | * remote_ip (read-write) | |
94 | * local_mac (read-only) | |
95 | * remote_mac (read-write) | |
df180e36 SS |
96 | */ |
97 | struct netconsole_target { | |
b5427c27 | 98 | struct list_head list; |
0bcc1816 SS |
99 | #ifdef CONFIG_NETCONSOLE_DYNAMIC |
100 | struct config_item item; | |
101 | #endif | |
698cf1c6 | 102 | bool enabled; |
e2f15f9a | 103 | bool extended; |
df180e36 SS |
104 | struct netpoll np; |
105 | }; | |
106 | ||
0bcc1816 SS |
107 | #ifdef CONFIG_NETCONSOLE_DYNAMIC |
108 | ||
109 | static struct configfs_subsystem netconsole_subsys; | |
369e5a88 | 110 | static DEFINE_MUTEX(dynamic_netconsole_mutex); |
0bcc1816 SS |
111 | |
112 | static int __init dynamic_netconsole_init(void) | |
113 | { | |
114 | config_group_init(&netconsole_subsys.su_group); | |
115 | mutex_init(&netconsole_subsys.su_mutex); | |
116 | return configfs_register_subsystem(&netconsole_subsys); | |
117 | } | |
118 | ||
119 | static void __exit dynamic_netconsole_exit(void) | |
120 | { | |
121 | configfs_unregister_subsystem(&netconsole_subsys); | |
122 | } | |
123 | ||
124 | /* | |
125 | * Targets that were created by parsing the boot/module option string | |
126 | * do not exist in the configfs hierarchy (and have NULL names) and will | |
127 | * never go away, so make these a no-op for them. | |
128 | */ | |
129 | static void netconsole_target_get(struct netconsole_target *nt) | |
130 | { | |
131 | if (config_item_name(&nt->item)) | |
132 | config_item_get(&nt->item); | |
133 | } | |
134 | ||
135 | static void netconsole_target_put(struct netconsole_target *nt) | |
136 | { | |
137 | if (config_item_name(&nt->item)) | |
138 | config_item_put(&nt->item); | |
139 | } | |
140 | ||
141 | #else /* !CONFIG_NETCONSOLE_DYNAMIC */ | |
142 | ||
143 | static int __init dynamic_netconsole_init(void) | |
144 | { | |
145 | return 0; | |
146 | } | |
147 | ||
148 | static void __exit dynamic_netconsole_exit(void) | |
149 | { | |
150 | } | |
151 | ||
152 | /* | |
153 | * No danger of targets going away from under us when dynamic | |
154 | * reconfigurability is off. | |
155 | */ | |
156 | static void netconsole_target_get(struct netconsole_target *nt) | |
157 | { | |
158 | } | |
159 | ||
160 | static void netconsole_target_put(struct netconsole_target *nt) | |
161 | { | |
162 | } | |
163 | ||
164 | #endif /* CONFIG_NETCONSOLE_DYNAMIC */ | |
165 | ||
166 | /* Allocate new target (from boot/module param) and setup netpoll for it */ | |
167 | static struct netconsole_target *alloc_param_target(char *target_config) | |
b5427c27 SS |
168 | { |
169 | int err = -ENOMEM; | |
170 | struct netconsole_target *nt; | |
171 | ||
0bcc1816 SS |
172 | /* |
173 | * Allocate and initialize with defaults. | |
174 | * Note that these targets get their config_item fields zeroed-out. | |
175 | */ | |
b5427c27 | 176 | nt = kzalloc(sizeof(*nt), GFP_KERNEL); |
e404decb | 177 | if (!nt) |
b5427c27 | 178 | goto fail; |
b5427c27 SS |
179 | |
180 | nt->np.name = "netconsole"; | |
fb3ceec1 | 181 | strscpy(nt->np.dev_name, "eth0", IFNAMSIZ); |
b5427c27 SS |
182 | nt->np.local_port = 6665; |
183 | nt->np.remote_port = 6666; | |
1667c942 | 184 | eth_broadcast_addr(nt->np.remote_mac); |
b5427c27 | 185 | |
e2f15f9a TH |
186 | if (*target_config == '+') { |
187 | nt->extended = true; | |
188 | target_config++; | |
189 | } | |
190 | ||
b5427c27 SS |
191 | /* Parse parameters and setup netpoll */ |
192 | err = netpoll_parse_options(&nt->np, target_config); | |
193 | if (err) | |
194 | goto fail; | |
195 | ||
196 | err = netpoll_setup(&nt->np); | |
197 | if (err) | |
198 | goto fail; | |
199 | ||
698cf1c6 | 200 | nt->enabled = true; |
0bcc1816 | 201 | |
b5427c27 SS |
202 | return nt; |
203 | ||
204 | fail: | |
205 | kfree(nt); | |
206 | return ERR_PTR(err); | |
207 | } | |
208 | ||
0bcc1816 SS |
209 | /* Cleanup netpoll for given target (from boot/module param) and free it */ |
210 | static void free_param_target(struct netconsole_target *nt) | |
b5427c27 SS |
211 | { |
212 | netpoll_cleanup(&nt->np); | |
213 | kfree(nt); | |
214 | } | |
1da177e4 | 215 | |
0bcc1816 SS |
216 | #ifdef CONFIG_NETCONSOLE_DYNAMIC |
217 | ||
218 | /* | |
219 | * Our subsystem hierarchy is: | |
220 | * | |
221 | * /sys/kernel/config/netconsole/ | |
222 | * | | |
223 | * <target>/ | |
224 | * | enabled | |
225 | * | dev_name | |
226 | * | local_port | |
227 | * | remote_port | |
228 | * | local_ip | |
229 | * | remote_ip | |
230 | * | local_mac | |
231 | * | remote_mac | |
232 | * | | |
233 | * <target>/... | |
234 | */ | |
235 | ||
0bcc1816 SS |
236 | static struct netconsole_target *to_target(struct config_item *item) |
237 | { | |
238 | return item ? | |
239 | container_of(item, struct netconsole_target, item) : | |
240 | NULL; | |
241 | } | |
242 | ||
0bcc1816 SS |
243 | /* |
244 | * Attribute operations for netconsole_target. | |
245 | */ | |
246 | ||
ea9ed9cf | 247 | static ssize_t enabled_show(struct config_item *item, char *buf) |
0bcc1816 | 248 | { |
ea9ed9cf | 249 | return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->enabled); |
0bcc1816 SS |
250 | } |
251 | ||
ea9ed9cf | 252 | static ssize_t extended_show(struct config_item *item, char *buf) |
e2f15f9a | 253 | { |
ea9ed9cf | 254 | return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->extended); |
e2f15f9a TH |
255 | } |
256 | ||
ea9ed9cf | 257 | static ssize_t dev_name_show(struct config_item *item, char *buf) |
0bcc1816 | 258 | { |
ea9ed9cf | 259 | return snprintf(buf, PAGE_SIZE, "%s\n", to_target(item)->np.dev_name); |
0bcc1816 SS |
260 | } |
261 | ||
ea9ed9cf | 262 | static ssize_t local_port_show(struct config_item *item, char *buf) |
0bcc1816 | 263 | { |
ea9ed9cf | 264 | return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->np.local_port); |
0bcc1816 SS |
265 | } |
266 | ||
ea9ed9cf | 267 | static ssize_t remote_port_show(struct config_item *item, char *buf) |
0bcc1816 | 268 | { |
ea9ed9cf | 269 | return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->np.remote_port); |
0bcc1816 SS |
270 | } |
271 | ||
ea9ed9cf | 272 | static ssize_t local_ip_show(struct config_item *item, char *buf) |
0bcc1816 | 273 | { |
ea9ed9cf CH |
274 | struct netconsole_target *nt = to_target(item); |
275 | ||
b3d936f3 CW |
276 | if (nt->np.ipv6) |
277 | return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.local_ip.in6); | |
278 | else | |
b7394d24 | 279 | return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip); |
0bcc1816 SS |
280 | } |
281 | ||
ea9ed9cf | 282 | static ssize_t remote_ip_show(struct config_item *item, char *buf) |
0bcc1816 | 283 | { |
ea9ed9cf CH |
284 | struct netconsole_target *nt = to_target(item); |
285 | ||
b3d936f3 CW |
286 | if (nt->np.ipv6) |
287 | return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.remote_ip.in6); | |
288 | else | |
b7394d24 | 289 | return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip); |
0bcc1816 SS |
290 | } |
291 | ||
ea9ed9cf | 292 | static ssize_t local_mac_show(struct config_item *item, char *buf) |
0bcc1816 | 293 | { |
ea9ed9cf | 294 | struct net_device *dev = to_target(item)->np.dev; |
e174961c | 295 | static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
09538641 | 296 | |
e174961c | 297 | return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast); |
0bcc1816 SS |
298 | } |
299 | ||
ea9ed9cf | 300 | static ssize_t remote_mac_show(struct config_item *item, char *buf) |
0bcc1816 | 301 | { |
ea9ed9cf | 302 | return snprintf(buf, PAGE_SIZE, "%pM\n", to_target(item)->np.remote_mac); |
0bcc1816 SS |
303 | } |
304 | ||
305 | /* | |
306 | * This one is special -- targets created through the configfs interface | |
307 | * are not enabled (and the corresponding netpoll activated) by default. | |
308 | * The user is expected to set the desired parameters first (which | |
309 | * would enable him to dynamically add new netpoll targets for new | |
310 | * network interfaces as and when they come up). | |
311 | */ | |
ea9ed9cf CH |
312 | static ssize_t enabled_store(struct config_item *item, |
313 | const char *buf, size_t count) | |
0bcc1816 | 314 | { |
ea9ed9cf | 315 | struct netconsole_target *nt = to_target(item); |
45e526e8 | 316 | unsigned long flags; |
99f823f9 | 317 | int enabled; |
0bcc1816 | 318 | int err; |
0bcc1816 | 319 | |
ea9ed9cf | 320 | mutex_lock(&dynamic_netconsole_mutex); |
99f823f9 AD |
321 | err = kstrtoint(buf, 10, &enabled); |
322 | if (err < 0) | |
ea9ed9cf CH |
323 | goto out_unlock; |
324 | ||
325 | err = -EINVAL; | |
99f823f9 | 326 | if (enabled < 0 || enabled > 1) |
ea9ed9cf | 327 | goto out_unlock; |
698cf1c6 | 328 | if ((bool)enabled == nt->enabled) { |
22ded577 JP |
329 | pr_info("network logging has already %s\n", |
330 | nt->enabled ? "started" : "stopped"); | |
ea9ed9cf | 331 | goto out_unlock; |
d5123480 | 332 | } |
0bcc1816 | 333 | |
698cf1c6 | 334 | if (enabled) { /* true */ |
2c6b4b70 | 335 | if (nt->extended && !console_is_registered(&netconsole_ext)) |
e2f15f9a | 336 | register_console(&netconsole_ext); |
e2f15f9a | 337 | |
0bcc1816 SS |
338 | /* |
339 | * Skip netpoll_parse_options() -- all the attributes are | |
340 | * already configured via configfs. Just print them out. | |
341 | */ | |
342 | netpoll_print_options(&nt->np); | |
343 | ||
344 | err = netpoll_setup(&nt->np); | |
c7c6effd | 345 | if (err) |
ea9ed9cf | 346 | goto out_unlock; |
0bcc1816 | 347 | |
4a6a97e2 | 348 | pr_info("network logging started\n"); |
698cf1c6 | 349 | } else { /* false */ |
45e526e8 NA |
350 | /* We need to disable the netconsole before cleaning it up |
351 | * otherwise we might end up in write_msg() with | |
698cf1c6 | 352 | * nt->np.dev == NULL and nt->enabled == true |
45e526e8 NA |
353 | */ |
354 | spin_lock_irqsave(&target_list_lock, flags); | |
698cf1c6 | 355 | nt->enabled = false; |
45e526e8 | 356 | spin_unlock_irqrestore(&target_list_lock, flags); |
0bcc1816 SS |
357 | netpoll_cleanup(&nt->np); |
358 | } | |
359 | ||
360 | nt->enabled = enabled; | |
361 | ||
ea9ed9cf | 362 | mutex_unlock(&dynamic_netconsole_mutex); |
0bcc1816 | 363 | return strnlen(buf, count); |
ea9ed9cf CH |
364 | out_unlock: |
365 | mutex_unlock(&dynamic_netconsole_mutex); | |
366 | return err; | |
0bcc1816 SS |
367 | } |
368 | ||
ea9ed9cf CH |
369 | static ssize_t extended_store(struct config_item *item, const char *buf, |
370 | size_t count) | |
e2f15f9a | 371 | { |
ea9ed9cf | 372 | struct netconsole_target *nt = to_target(item); |
e2f15f9a TH |
373 | int extended; |
374 | int err; | |
375 | ||
ea9ed9cf | 376 | mutex_lock(&dynamic_netconsole_mutex); |
e2f15f9a TH |
377 | if (nt->enabled) { |
378 | pr_err("target (%s) is enabled, disable to update parameters\n", | |
379 | config_item_name(&nt->item)); | |
ea9ed9cf CH |
380 | err = -EINVAL; |
381 | goto out_unlock; | |
e2f15f9a TH |
382 | } |
383 | ||
384 | err = kstrtoint(buf, 10, &extended); | |
385 | if (err < 0) | |
ea9ed9cf CH |
386 | goto out_unlock; |
387 | if (extended < 0 || extended > 1) { | |
388 | err = -EINVAL; | |
389 | goto out_unlock; | |
390 | } | |
e2f15f9a TH |
391 | |
392 | nt->extended = extended; | |
393 | ||
ea9ed9cf | 394 | mutex_unlock(&dynamic_netconsole_mutex); |
e2f15f9a | 395 | return strnlen(buf, count); |
ea9ed9cf CH |
396 | out_unlock: |
397 | mutex_unlock(&dynamic_netconsole_mutex); | |
398 | return err; | |
e2f15f9a TH |
399 | } |
400 | ||
ea9ed9cf CH |
401 | static ssize_t dev_name_store(struct config_item *item, const char *buf, |
402 | size_t count) | |
0bcc1816 | 403 | { |
ea9ed9cf | 404 | struct netconsole_target *nt = to_target(item); |
0bcc1816 SS |
405 | size_t len; |
406 | ||
ea9ed9cf | 407 | mutex_lock(&dynamic_netconsole_mutex); |
0bcc1816 | 408 | if (nt->enabled) { |
22ded577 JP |
409 | pr_err("target (%s) is enabled, disable to update parameters\n", |
410 | config_item_name(&nt->item)); | |
ea9ed9cf | 411 | mutex_unlock(&dynamic_netconsole_mutex); |
0bcc1816 SS |
412 | return -EINVAL; |
413 | } | |
414 | ||
fb3ceec1 | 415 | strscpy(nt->np.dev_name, buf, IFNAMSIZ); |
0bcc1816 SS |
416 | |
417 | /* Get rid of possible trailing newline from echo(1) */ | |
418 | len = strnlen(nt->np.dev_name, IFNAMSIZ); | |
419 | if (nt->np.dev_name[len - 1] == '\n') | |
420 | nt->np.dev_name[len - 1] = '\0'; | |
421 | ||
ea9ed9cf | 422 | mutex_unlock(&dynamic_netconsole_mutex); |
0bcc1816 SS |
423 | return strnlen(buf, count); |
424 | } | |
425 | ||
ea9ed9cf CH |
426 | static ssize_t local_port_store(struct config_item *item, const char *buf, |
427 | size_t count) | |
0bcc1816 | 428 | { |
ea9ed9cf CH |
429 | struct netconsole_target *nt = to_target(item); |
430 | int rv = -EINVAL; | |
0bcc1816 | 431 | |
ea9ed9cf | 432 | mutex_lock(&dynamic_netconsole_mutex); |
0bcc1816 | 433 | if (nt->enabled) { |
22ded577 JP |
434 | pr_err("target (%s) is enabled, disable to update parameters\n", |
435 | config_item_name(&nt->item)); | |
ea9ed9cf | 436 | goto out_unlock; |
0bcc1816 SS |
437 | } |
438 | ||
99f823f9 AD |
439 | rv = kstrtou16(buf, 10, &nt->np.local_port); |
440 | if (rv < 0) | |
ea9ed9cf CH |
441 | goto out_unlock; |
442 | mutex_unlock(&dynamic_netconsole_mutex); | |
0bcc1816 | 443 | return strnlen(buf, count); |
ea9ed9cf CH |
444 | out_unlock: |
445 | mutex_unlock(&dynamic_netconsole_mutex); | |
446 | return rv; | |
0bcc1816 SS |
447 | } |
448 | ||
ea9ed9cf CH |
449 | static ssize_t remote_port_store(struct config_item *item, |
450 | const char *buf, size_t count) | |
0bcc1816 | 451 | { |
ea9ed9cf CH |
452 | struct netconsole_target *nt = to_target(item); |
453 | int rv = -EINVAL; | |
0bcc1816 | 454 | |
ea9ed9cf | 455 | mutex_lock(&dynamic_netconsole_mutex); |
0bcc1816 | 456 | if (nt->enabled) { |
22ded577 JP |
457 | pr_err("target (%s) is enabled, disable to update parameters\n", |
458 | config_item_name(&nt->item)); | |
ea9ed9cf | 459 | goto out_unlock; |
0bcc1816 SS |
460 | } |
461 | ||
99f823f9 AD |
462 | rv = kstrtou16(buf, 10, &nt->np.remote_port); |
463 | if (rv < 0) | |
ea9ed9cf CH |
464 | goto out_unlock; |
465 | mutex_unlock(&dynamic_netconsole_mutex); | |
0bcc1816 | 466 | return strnlen(buf, count); |
ea9ed9cf CH |
467 | out_unlock: |
468 | mutex_unlock(&dynamic_netconsole_mutex); | |
469 | return rv; | |
0bcc1816 SS |
470 | } |
471 | ||
ea9ed9cf CH |
472 | static ssize_t local_ip_store(struct config_item *item, const char *buf, |
473 | size_t count) | |
0bcc1816 | 474 | { |
ea9ed9cf CH |
475 | struct netconsole_target *nt = to_target(item); |
476 | ||
477 | mutex_lock(&dynamic_netconsole_mutex); | |
0bcc1816 | 478 | if (nt->enabled) { |
22ded577 JP |
479 | pr_err("target (%s) is enabled, disable to update parameters\n", |
480 | config_item_name(&nt->item)); | |
ea9ed9cf | 481 | goto out_unlock; |
0bcc1816 SS |
482 | } |
483 | ||
b3d936f3 CW |
484 | if (strnchr(buf, count, ':')) { |
485 | const char *end; | |
486 | if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) { | |
487 | if (*end && *end != '\n') { | |
22ded577 | 488 | pr_err("invalid IPv6 address at: <%c>\n", *end); |
ea9ed9cf | 489 | goto out_unlock; |
b3d936f3 CW |
490 | } |
491 | nt->np.ipv6 = true; | |
492 | } else | |
ea9ed9cf | 493 | goto out_unlock; |
b3d936f3 CW |
494 | } else { |
495 | if (!nt->np.ipv6) { | |
496 | nt->np.local_ip.ip = in_aton(buf); | |
497 | } else | |
ea9ed9cf | 498 | goto out_unlock; |
b3d936f3 | 499 | } |
0bcc1816 | 500 | |
ea9ed9cf | 501 | mutex_unlock(&dynamic_netconsole_mutex); |
0bcc1816 | 502 | return strnlen(buf, count); |
ea9ed9cf CH |
503 | out_unlock: |
504 | mutex_unlock(&dynamic_netconsole_mutex); | |
505 | return -EINVAL; | |
0bcc1816 SS |
506 | } |
507 | ||
ea9ed9cf CH |
508 | static ssize_t remote_ip_store(struct config_item *item, const char *buf, |
509 | size_t count) | |
0bcc1816 | 510 | { |
ea9ed9cf CH |
511 | struct netconsole_target *nt = to_target(item); |
512 | ||
513 | mutex_lock(&dynamic_netconsole_mutex); | |
0bcc1816 | 514 | if (nt->enabled) { |
22ded577 JP |
515 | pr_err("target (%s) is enabled, disable to update parameters\n", |
516 | config_item_name(&nt->item)); | |
ea9ed9cf | 517 | goto out_unlock; |
0bcc1816 SS |
518 | } |
519 | ||
b3d936f3 CW |
520 | if (strnchr(buf, count, ':')) { |
521 | const char *end; | |
522 | if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) { | |
523 | if (*end && *end != '\n') { | |
22ded577 | 524 | pr_err("invalid IPv6 address at: <%c>\n", *end); |
ea9ed9cf | 525 | goto out_unlock; |
b3d936f3 CW |
526 | } |
527 | nt->np.ipv6 = true; | |
528 | } else | |
ea9ed9cf | 529 | goto out_unlock; |
b3d936f3 CW |
530 | } else { |
531 | if (!nt->np.ipv6) { | |
532 | nt->np.remote_ip.ip = in_aton(buf); | |
533 | } else | |
ea9ed9cf | 534 | goto out_unlock; |
b3d936f3 | 535 | } |
0bcc1816 | 536 | |
ea9ed9cf | 537 | mutex_unlock(&dynamic_netconsole_mutex); |
0bcc1816 | 538 | return strnlen(buf, count); |
ea9ed9cf CH |
539 | out_unlock: |
540 | mutex_unlock(&dynamic_netconsole_mutex); | |
541 | return -EINVAL; | |
0bcc1816 SS |
542 | } |
543 | ||
ea9ed9cf CH |
544 | static ssize_t remote_mac_store(struct config_item *item, const char *buf, |
545 | size_t count) | |
0bcc1816 | 546 | { |
ea9ed9cf | 547 | struct netconsole_target *nt = to_target(item); |
0bcc1816 | 548 | u8 remote_mac[ETH_ALEN]; |
0bcc1816 | 549 | |
ea9ed9cf | 550 | mutex_lock(&dynamic_netconsole_mutex); |
0bcc1816 | 551 | if (nt->enabled) { |
22ded577 JP |
552 | pr_err("target (%s) is enabled, disable to update parameters\n", |
553 | config_item_name(&nt->item)); | |
ea9ed9cf | 554 | goto out_unlock; |
0bcc1816 SS |
555 | } |
556 | ||
4940fc88 | 557 | if (!mac_pton(buf, remote_mac)) |
ea9ed9cf | 558 | goto out_unlock; |
4940fc88 | 559 | if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n') |
ea9ed9cf | 560 | goto out_unlock; |
0bcc1816 SS |
561 | memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN); |
562 | ||
ea9ed9cf | 563 | mutex_unlock(&dynamic_netconsole_mutex); |
0bcc1816 | 564 | return strnlen(buf, count); |
ea9ed9cf CH |
565 | out_unlock: |
566 | mutex_unlock(&dynamic_netconsole_mutex); | |
567 | return -EINVAL; | |
0bcc1816 SS |
568 | } |
569 | ||
ea9ed9cf CH |
570 | CONFIGFS_ATTR(, enabled); |
571 | CONFIGFS_ATTR(, extended); | |
572 | CONFIGFS_ATTR(, dev_name); | |
573 | CONFIGFS_ATTR(, local_port); | |
574 | CONFIGFS_ATTR(, remote_port); | |
575 | CONFIGFS_ATTR(, local_ip); | |
576 | CONFIGFS_ATTR(, remote_ip); | |
577 | CONFIGFS_ATTR_RO(, local_mac); | |
578 | CONFIGFS_ATTR(, remote_mac); | |
0bcc1816 SS |
579 | |
580 | static struct configfs_attribute *netconsole_target_attrs[] = { | |
ea9ed9cf CH |
581 | &attr_enabled, |
582 | &attr_extended, | |
583 | &attr_dev_name, | |
584 | &attr_local_port, | |
585 | &attr_remote_port, | |
586 | &attr_local_ip, | |
587 | &attr_remote_ip, | |
588 | &attr_local_mac, | |
589 | &attr_remote_mac, | |
0bcc1816 SS |
590 | NULL, |
591 | }; | |
592 | ||
593 | /* | |
594 | * Item operations and type for netconsole_target. | |
595 | */ | |
596 | ||
597 | static void netconsole_target_release(struct config_item *item) | |
598 | { | |
599 | kfree(to_target(item)); | |
600 | } | |
601 | ||
0bcc1816 SS |
602 | static struct configfs_item_operations netconsole_target_item_ops = { |
603 | .release = netconsole_target_release, | |
0bcc1816 SS |
604 | }; |
605 | ||
0d4a4406 | 606 | static const struct config_item_type netconsole_target_type = { |
0bcc1816 SS |
607 | .ct_attrs = netconsole_target_attrs, |
608 | .ct_item_ops = &netconsole_target_item_ops, | |
609 | .ct_owner = THIS_MODULE, | |
610 | }; | |
611 | ||
612 | /* | |
613 | * Group operations and type for netconsole_subsys. | |
614 | */ | |
615 | ||
f89ab861 JB |
616 | static struct config_item *make_netconsole_target(struct config_group *group, |
617 | const char *name) | |
0bcc1816 SS |
618 | { |
619 | unsigned long flags; | |
620 | struct netconsole_target *nt; | |
621 | ||
622 | /* | |
623 | * Allocate and initialize with defaults. | |
698cf1c6 | 624 | * Target is disabled at creation (!enabled). |
0bcc1816 SS |
625 | */ |
626 | nt = kzalloc(sizeof(*nt), GFP_KERNEL); | |
e404decb | 627 | if (!nt) |
a6795e9e | 628 | return ERR_PTR(-ENOMEM); |
0bcc1816 SS |
629 | |
630 | nt->np.name = "netconsole"; | |
fb3ceec1 | 631 | strscpy(nt->np.dev_name, "eth0", IFNAMSIZ); |
0bcc1816 SS |
632 | nt->np.local_port = 6665; |
633 | nt->np.remote_port = 6666; | |
1667c942 | 634 | eth_broadcast_addr(nt->np.remote_mac); |
0bcc1816 SS |
635 | |
636 | /* Initialize the config_item member */ | |
637 | config_item_init_type_name(&nt->item, name, &netconsole_target_type); | |
638 | ||
639 | /* Adding, but it is disabled */ | |
640 | spin_lock_irqsave(&target_list_lock, flags); | |
641 | list_add(&nt->list, &target_list); | |
642 | spin_unlock_irqrestore(&target_list_lock, flags); | |
643 | ||
f89ab861 | 644 | return &nt->item; |
0bcc1816 SS |
645 | } |
646 | ||
647 | static void drop_netconsole_target(struct config_group *group, | |
648 | struct config_item *item) | |
649 | { | |
650 | unsigned long flags; | |
651 | struct netconsole_target *nt = to_target(item); | |
652 | ||
653 | spin_lock_irqsave(&target_list_lock, flags); | |
654 | list_del(&nt->list); | |
655 | spin_unlock_irqrestore(&target_list_lock, flags); | |
656 | ||
657 | /* | |
658 | * The target may have never been enabled, or was manually disabled | |
659 | * before being removed so netpoll may have already been cleaned up. | |
660 | */ | |
661 | if (nt->enabled) | |
662 | netpoll_cleanup(&nt->np); | |
663 | ||
664 | config_item_put(&nt->item); | |
665 | } | |
666 | ||
667 | static struct configfs_group_operations netconsole_subsys_group_ops = { | |
668 | .make_item = make_netconsole_target, | |
669 | .drop_item = drop_netconsole_target, | |
670 | }; | |
671 | ||
0d4a4406 | 672 | static const struct config_item_type netconsole_subsys_type = { |
0bcc1816 SS |
673 | .ct_group_ops = &netconsole_subsys_group_ops, |
674 | .ct_owner = THIS_MODULE, | |
675 | }; | |
676 | ||
677 | /* The netconsole configfs subsystem */ | |
678 | static struct configfs_subsystem netconsole_subsys = { | |
679 | .su_group = { | |
680 | .cg_item = { | |
681 | .ci_namebuf = "netconsole", | |
682 | .ci_type = &netconsole_subsys_type, | |
683 | }, | |
684 | }, | |
685 | }; | |
686 | ||
687 | #endif /* CONFIG_NETCONSOLE_DYNAMIC */ | |
688 | ||
17951f34 SS |
689 | /* Handle network interface device notifications */ |
690 | static int netconsole_netdev_event(struct notifier_block *this, | |
351638e7 | 691 | unsigned long event, void *ptr) |
17951f34 | 692 | { |
b5427c27 SS |
693 | unsigned long flags; |
694 | struct netconsole_target *nt; | |
351638e7 | 695 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
141dfba3 | 696 | bool stopped = false; |
17951f34 | 697 | |
0e34e931 | 698 | if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER || |
daf9209b | 699 | event == NETDEV_RELEASE || event == NETDEV_JOIN)) |
b5427c27 SS |
700 | goto done; |
701 | ||
702 | spin_lock_irqsave(&target_list_lock, flags); | |
3f315bef | 703 | restart: |
b5427c27 | 704 | list_for_each_entry(nt, &target_list, list) { |
0bcc1816 | 705 | netconsole_target_get(nt); |
b5427c27 SS |
706 | if (nt->np.dev == dev) { |
707 | switch (event) { | |
b5427c27 | 708 | case NETDEV_CHANGENAME: |
fb3ceec1 | 709 | strscpy(nt->np.dev_name, dev->name, IFNAMSIZ); |
b5427c27 | 710 | break; |
daf9209b | 711 | case NETDEV_RELEASE: |
8d8fc29d | 712 | case NETDEV_JOIN: |
2382b15b | 713 | case NETDEV_UNREGISTER: |
c71380ff | 714 | /* rtnl_lock already held |
3f315bef | 715 | * we might sleep in __netpoll_cleanup() |
3b410a31 | 716 | */ |
3f315bef | 717 | spin_unlock_irqrestore(&target_list_lock, flags); |
7a163bfb | 718 | |
3f315bef | 719 | __netpoll_cleanup(&nt->np); |
7a163bfb | 720 | |
3f315bef | 721 | spin_lock_irqsave(&target_list_lock, flags); |
d62607c3 | 722 | netdev_put(nt->np.dev, &nt->np.dev_tracker); |
3f315bef | 723 | nt->np.dev = NULL; |
698cf1c6 | 724 | nt->enabled = false; |
141dfba3 | 725 | stopped = true; |
3f315bef VF |
726 | netconsole_target_put(nt); |
727 | goto restart; | |
b5427c27 | 728 | } |
17951f34 | 729 | } |
0bcc1816 | 730 | netconsole_target_put(nt); |
17951f34 | 731 | } |
b5427c27 | 732 | spin_unlock_irqrestore(&target_list_lock, flags); |
8d8fc29d | 733 | if (stopped) { |
22ded577 | 734 | const char *msg = "had an event"; |
8d8fc29d AW |
735 | switch (event) { |
736 | case NETDEV_UNREGISTER: | |
22ded577 | 737 | msg = "unregistered"; |
8d8fc29d | 738 | break; |
daf9209b | 739 | case NETDEV_RELEASE: |
22ded577 | 740 | msg = "released slaves"; |
8d8fc29d AW |
741 | break; |
742 | case NETDEV_JOIN: | |
22ded577 | 743 | msg = "is joining a master device"; |
8d8fc29d AW |
744 | break; |
745 | } | |
22ded577 JP |
746 | pr_info("network logging stopped on interface %s as it %s\n", |
747 | dev->name, msg); | |
8d8fc29d | 748 | } |
17951f34 | 749 | |
b5427c27 | 750 | done: |
17951f34 SS |
751 | return NOTIFY_DONE; |
752 | } | |
753 | ||
754 | static struct notifier_block netconsole_netdev_notifier = { | |
755 | .notifier_call = netconsole_netdev_event, | |
756 | }; | |
757 | ||
e2f15f9a TH |
758 | /** |
759 | * send_ext_msg_udp - send extended log message to target | |
760 | * @nt: target to send message to | |
761 | * @msg: extended log message to send | |
762 | * @msg_len: length of message | |
763 | * | |
764 | * Transfer extended log @msg to @nt. If @msg is longer than | |
765 | * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with | |
766 | * ncfrag header field added to identify them. | |
767 | */ | |
768 | static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg, | |
769 | int msg_len) | |
770 | { | |
771 | static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */ | |
772 | const char *header, *body; | |
773 | int offset = 0; | |
774 | int header_len, body_len; | |
775 | ||
776 | if (msg_len <= MAX_PRINT_CHUNK) { | |
777 | netpoll_send_udp(&nt->np, msg, msg_len); | |
778 | return; | |
779 | } | |
780 | ||
781 | /* need to insert extra header fields, detect header and body */ | |
782 | header = msg; | |
783 | body = memchr(msg, ';', msg_len); | |
784 | if (WARN_ON_ONCE(!body)) | |
785 | return; | |
786 | ||
787 | header_len = body - header; | |
788 | body_len = msg_len - header_len - 1; | |
789 | body++; | |
790 | ||
791 | /* | |
792 | * Transfer multiple chunks with the following extra header. | |
793 | * "ncfrag=<byte-offset>/<total-bytes>" | |
794 | */ | |
795 | memcpy(buf, header, header_len); | |
796 | ||
797 | while (offset < body_len) { | |
798 | int this_header = header_len; | |
799 | int this_chunk; | |
800 | ||
801 | this_header += scnprintf(buf + this_header, | |
802 | sizeof(buf) - this_header, | |
803 | ",ncfrag=%d/%d;", offset, body_len); | |
804 | ||
805 | this_chunk = min(body_len - offset, | |
806 | MAX_PRINT_CHUNK - this_header); | |
807 | if (WARN_ON_ONCE(this_chunk <= 0)) | |
808 | return; | |
809 | ||
810 | memcpy(buf + this_header, body + offset, this_chunk); | |
811 | ||
812 | netpoll_send_udp(&nt->np, buf, this_header + this_chunk); | |
813 | ||
814 | offset += this_chunk; | |
815 | } | |
816 | } | |
817 | ||
818 | static void write_ext_msg(struct console *con, const char *msg, | |
819 | unsigned int len) | |
820 | { | |
821 | struct netconsole_target *nt; | |
822 | unsigned long flags; | |
823 | ||
824 | if ((oops_only && !oops_in_progress) || list_empty(&target_list)) | |
825 | return; | |
826 | ||
827 | spin_lock_irqsave(&target_list_lock, flags); | |
828 | list_for_each_entry(nt, &target_list, list) | |
829 | if (nt->extended && nt->enabled && netif_running(nt->np.dev)) | |
830 | send_ext_msg_udp(nt, msg, len); | |
831 | spin_unlock_irqrestore(&target_list_lock, flags); | |
832 | } | |
833 | ||
1da177e4 LT |
834 | static void write_msg(struct console *con, const char *msg, unsigned int len) |
835 | { | |
836 | int frag, left; | |
837 | unsigned long flags; | |
b5427c27 SS |
838 | struct netconsole_target *nt; |
839 | const char *tmp; | |
840 | ||
c1a60851 AW |
841 | if (oops_only && !oops_in_progress) |
842 | return; | |
b5427c27 SS |
843 | /* Avoid taking lock and disabling interrupts unnecessarily */ |
844 | if (list_empty(&target_list)) | |
845 | return; | |
846 | ||
847 | spin_lock_irqsave(&target_list_lock, flags); | |
848 | list_for_each_entry(nt, &target_list, list) { | |
e2f15f9a | 849 | if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) { |
b5427c27 SS |
850 | /* |
851 | * We nest this inside the for-each-target loop above | |
852 | * so that we're able to get as much logging out to | |
853 | * at least one target if we die inside here, instead | |
854 | * of unnecessarily keeping all targets in lock-step. | |
855 | */ | |
856 | tmp = msg; | |
857 | for (left = len; left;) { | |
858 | frag = min(left, MAX_PRINT_CHUNK); | |
859 | netpoll_send_udp(&nt->np, tmp, frag); | |
860 | tmp += frag; | |
861 | left -= frag; | |
862 | } | |
0cc120be | 863 | } |
1da177e4 | 864 | } |
b5427c27 | 865 | spin_unlock_irqrestore(&target_list_lock, flags); |
1da177e4 LT |
866 | } |
867 | ||
e2f15f9a TH |
868 | static struct console netconsole_ext = { |
869 | .name = "netcon_ext", | |
2c6b4b70 | 870 | .flags = CON_ENABLED | CON_EXTENDED, |
e2f15f9a TH |
871 | .write = write_ext_msg, |
872 | }; | |
873 | ||
1da177e4 | 874 | static struct console netconsole = { |
d39badf0 | 875 | .name = "netcon", |
0517deed | 876 | .flags = CON_ENABLED, |
d39badf0 | 877 | .write = write_msg, |
1da177e4 LT |
878 | }; |
879 | ||
d39badf0 | 880 | static int __init init_netconsole(void) |
1da177e4 | 881 | { |
0bcc1816 | 882 | int err; |
b5427c27 | 883 | struct netconsole_target *nt, *tmp; |
2c6b4b70 | 884 | bool extended = false; |
b5427c27 SS |
885 | unsigned long flags; |
886 | char *target_config; | |
887 | char *input = config; | |
b41848b6 | 888 | |
0bcc1816 SS |
889 | if (strnlen(input, MAX_PARAM_LENGTH)) { |
890 | while ((target_config = strsep(&input, ";"))) { | |
891 | nt = alloc_param_target(target_config); | |
892 | if (IS_ERR(nt)) { | |
893 | err = PTR_ERR(nt); | |
894 | goto fail; | |
895 | } | |
0517deed | 896 | /* Dump existing printks when we register */ |
2c6b4b70 JO |
897 | if (nt->extended) { |
898 | extended = true; | |
899 | netconsole_ext.flags |= CON_PRINTBUFFER; | |
900 | } else { | |
e2f15f9a | 901 | netconsole.flags |= CON_PRINTBUFFER; |
2c6b4b70 | 902 | } |
0517deed | 903 | |
0bcc1816 SS |
904 | spin_lock_irqsave(&target_list_lock, flags); |
905 | list_add(&nt->list, &target_list); | |
906 | spin_unlock_irqrestore(&target_list_lock, flags); | |
b5427c27 | 907 | } |
b5427c27 | 908 | } |
1da177e4 | 909 | |
17951f34 SS |
910 | err = register_netdevice_notifier(&netconsole_netdev_notifier); |
911 | if (err) | |
b5427c27 | 912 | goto fail; |
17951f34 | 913 | |
0bcc1816 SS |
914 | err = dynamic_netconsole_init(); |
915 | if (err) | |
916 | goto undonotifier; | |
917 | ||
2c6b4b70 | 918 | if (extended) |
e2f15f9a | 919 | register_console(&netconsole_ext); |
1da177e4 | 920 | register_console(&netconsole); |
22ded577 | 921 | pr_info("network logging started\n"); |
d39badf0 | 922 | |
d39badf0 | 923 | return err; |
b5427c27 | 924 | |
0bcc1816 SS |
925 | undonotifier: |
926 | unregister_netdevice_notifier(&netconsole_netdev_notifier); | |
927 | ||
b5427c27 | 928 | fail: |
22ded577 | 929 | pr_err("cleaning up\n"); |
b5427c27 SS |
930 | |
931 | /* | |
0bcc1816 SS |
932 | * Remove all targets and destroy them (only targets created |
933 | * from the boot/module option exist here). Skipping the list | |
b5427c27 SS |
934 | * lock is safe here, and netpoll_cleanup() will sleep. |
935 | */ | |
936 | list_for_each_entry_safe(nt, tmp, &target_list, list) { | |
937 | list_del(&nt->list); | |
0bcc1816 | 938 | free_param_target(nt); |
b5427c27 SS |
939 | } |
940 | ||
941 | return err; | |
1da177e4 LT |
942 | } |
943 | ||
d39badf0 | 944 | static void __exit cleanup_netconsole(void) |
1da177e4 | 945 | { |
b5427c27 | 946 | struct netconsole_target *nt, *tmp; |
df180e36 | 947 | |
2c6b4b70 JO |
948 | if (console_is_registered(&netconsole_ext)) |
949 | unregister_console(&netconsole_ext); | |
1da177e4 | 950 | unregister_console(&netconsole); |
0bcc1816 | 951 | dynamic_netconsole_exit(); |
17951f34 | 952 | unregister_netdevice_notifier(&netconsole_netdev_notifier); |
b5427c27 SS |
953 | |
954 | /* | |
0bcc1816 SS |
955 | * Targets created via configfs pin references on our module |
956 | * and would first be rmdir(2)'ed from userspace. We reach | |
957 | * here only when they are already destroyed, and only those | |
958 | * created from the boot/module option are left, so remove and | |
959 | * destroy them. Skipping the list lock is safe here, and | |
960 | * netpoll_cleanup() will sleep. | |
b5427c27 SS |
961 | */ |
962 | list_for_each_entry_safe(nt, tmp, &target_list, list) { | |
963 | list_del(&nt->list); | |
0bcc1816 | 964 | free_param_target(nt); |
b5427c27 | 965 | } |
1da177e4 LT |
966 | } |
967 | ||
97c7de05 LM |
968 | /* |
969 | * Use late_initcall to ensure netconsole is | |
970 | * initialized after network device driver if built-in. | |
971 | * | |
972 | * late_initcall() and module_init() are identical if built as module. | |
973 | */ | |
974 | late_initcall(init_netconsole); | |
1da177e4 | 975 | module_exit(cleanup_netconsole); |