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