1 // SPDX-License-Identifier: GPL-2.0-or-later
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 /****************************************************************
22 *
23 ****************************************************************/
24
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27 #include <linux/mm.h>
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/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/udp.h>
38 #include <linux/netpoll.h>
39 #include <linux/inet.h>
40 #include <linux/inetdevice.h>
41 #include <linux/unaligned.h>
42 #include <net/ip6_checksum.h>
43 #include <net/addrconf.h>
44 #include <linux/configfs.h>
45 #include <linux/etherdevice.h>
46 #include <linux/hex.h>
47 #include <linux/u64_stats_sync.h>
48 #include <linux/utsname.h>
49 #include <linux/rtnetlink.h>
50 #include <linux/workqueue.h>
51 #include <linux/delay.h>
52
53 MODULE_AUTHOR("Matt Mackall <mpm@selenic.com>");
54 MODULE_DESCRIPTION("Console driver for network interfaces");
55 MODULE_LICENSE("GPL");
56 MODULE_IMPORT_NS("NETDEV_INTERNAL");
57
58 #define MAX_PARAM_LENGTH 256
59 #define MAX_EXTRADATA_ENTRY_LEN 256
60 #define MAX_EXTRADATA_VALUE_LEN 200
61 /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
62 #define MAX_EXTRADATA_NAME_LEN (MAX_EXTRADATA_ENTRY_LEN - \
63 MAX_EXTRADATA_VALUE_LEN - 3)
64 #define MAX_USERDATA_ITEMS 256
65 #define MAX_PRINT_CHUNK 1000
66
67 /*
68 * Sizing for the per-target fallback skb pool consulted by find_skb()
69 * when its GFP_ATOMIC allocation fails so messages still get out under
70 * memory pressure.
71 */
72 #define MAX_UDP_CHUNK 1460
73 #define MAX_SKBS 32
74 #define MAX_SKB_SIZE \
75 (sizeof(struct ethhdr) + \
76 sizeof(struct iphdr) + \
77 sizeof(struct udphdr) + \
78 MAX_UDP_CHUNK)
79
80 static char config[MAX_PARAM_LENGTH];
81 module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
82 MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
83
84 static bool oops_only;
85 module_param(oops_only, bool, 0600);
86 MODULE_PARM_DESC(oops_only, "Only log oops messages");
87
88 #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
89
90 #ifndef MODULE
option_setup(char * opt)91 static int __init option_setup(char *opt)
92 {
93 strscpy(config, opt, MAX_PARAM_LENGTH);
94 return 1;
95 }
96 __setup("netconsole=", option_setup);
97 #endif /* MODULE */
98
99 /* Linked list of all configured targets */
100 static LIST_HEAD(target_list);
101 /* target_cleanup_list is used to track targets that need to be cleaned outside
102 * of target_list_lock. It should be cleaned in the same function it is
103 * populated.
104 */
105 static LIST_HEAD(target_cleanup_list);
106
107 /* This needs to be a spinlock because write_msg() cannot sleep */
108 static DEFINE_SPINLOCK(target_list_lock);
109 /* This needs to be a mutex because netpoll_cleanup might sleep */
110 static DEFINE_MUTEX(target_cleanup_list_lock);
111
112 static struct workqueue_struct *netconsole_wq;
113
114 /*
115 * Console driver for netconsoles. Register only consoles that have
116 * an associated target of the same type.
117 */
118 static struct console netconsole_ext, netconsole;
119
120 struct netconsole_target_stats {
121 u64_stats_t xmit_drop_count;
122 u64_stats_t enomem_count;
123 struct u64_stats_sync syncp;
124 };
125
126 enum console_type {
127 CONS_BASIC = BIT(0),
128 CONS_EXTENDED = BIT(1),
129 };
130
131 /* Features enabled in sysdata. Contrary to userdata, this data is populated by
132 * the kernel. The fields are designed as bitwise flags, allowing multiple
133 * features to be set in sysdata_fields.
134 */
135 enum sysdata_feature {
136 /* Populate the CPU that sends the message */
137 SYSDATA_CPU_NR = BIT(0),
138 /* Populate the task name (as in current->comm) in sysdata */
139 SYSDATA_TASKNAME = BIT(1),
140 /* Kernel release/version as part of sysdata */
141 SYSDATA_RELEASE = BIT(2),
142 /* Include a per-target message ID as part of sysdata */
143 SYSDATA_MSGID = BIT(3),
144 /* Sentinel: highest bit position */
145 MAX_SYSDATA_ITEMS = 4,
146 };
147
148 enum target_state {
149 STATE_DISABLED,
150 STATE_ENABLED,
151 STATE_DEACTIVATED,
152 };
153
154 /**
155 * struct netcons_userdata - Formatted userdata payload of a target.
156 * @rcu: Used to free the payload after a grace period.
157 * @length: Length of @data, excluding the NUL terminator.
158 * @data: Formatted " key=value\n" entries, NUL terminated.
159 *
160 * Immutable once published, so the transmit path never observes @data and
161 * @length disagreeing.
162 */
163 struct netcons_userdata {
164 struct rcu_head rcu;
165 size_t length;
166 char data[];
167 };
168
169 /**
170 * struct netconsole_target - Represents a configured netconsole target.
171 * @list: Links this target into the target_list.
172 * @group: Links us into the configfs subsystem hierarchy.
173 * @userdata_group: Links to the userdata configfs hierarchy
174 * @userdata: Cached, formatted userdata payload. RCU protected.
175 * @sysdata: Cached, formatted string of append
176 * @sysdata_fields: Sysdata features enabled.
177 * @msgcounter: Message sent counter.
178 * @stats: Packet send stats for the target. Used for debugging.
179 * @state: State of the target.
180 * Visible from userspace (read-write).
181 * From a userspace perspective, the target is either enabled or
182 * disabled. Internally, although both STATE_DISABLED and
183 * STATE_DEACTIVATED correspond to inactive targets, the latter is
184 * due to automatic interface state changes and will try
185 * recover automatically, if the interface comes back
186 * online.
187 * Also, other parameters of a target may be modified at
188 * runtime only when it is disabled (state != STATE_ENABLED).
189 * @extended: Denotes whether console is extended or not.
190 * @release: Denotes whether kernel release version should be prepended
191 * to the message. Depends on extended console.
192 * @np: The netpoll structure for this target.
193 * Contains the other userspace visible parameters:
194 * dev_name (read-write)
195 * local_mac (read-only)
196 * @local_ip: Source IP address of the target (read-write).
197 * @remote_ip: Destination IP address of the target (read-write).
198 * @ipv6: Whether the target addresses are IPv6 (read-write).
199 * @local_port: Source UDP port of the target (read-write).
200 * @remote_port: Destination UDP port of the target (read-write).
201 * @remote_mac: Destination ethernet address of the target (read-write).
202 * @buf: The buffer used to send the full msg to the network stack
203 * @resume_wq: Workqueue to resume deactivated target
204 * @skb_pool: Per-target fallback skb pool consulted by find_skb() when
205 * its GFP_ATOMIC allocation fails. Lifetime brackets a
206 * successful netpoll_setup() / netpoll_cleanup() pair on @np.
207 * @refill_wq: Work item that asynchronously tops @skb_pool back up to
208 * MAX_SKBS after find_skb() drains an entry.
209 */
210 struct netconsole_target {
211 struct list_head list;
212 #ifdef CONFIG_NETCONSOLE_DYNAMIC
213 struct config_group group;
214 struct config_group userdata_group;
215 struct netcons_userdata __rcu *userdata;
216 char sysdata[MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS];
217
218 /* bit-wise with sysdata_feature bits */
219 u32 sysdata_fields;
220 /* protected by target_list_lock */
221 u32 msgcounter;
222 #endif
223 struct netconsole_target_stats stats;
224 enum target_state state;
225 bool extended;
226 bool release;
227 struct netpoll np;
228 union inet_addr local_ip, remote_ip;
229 bool ipv6;
230 u16 local_port, remote_port;
231 u8 remote_mac[ETH_ALEN];
232 /* protected by target_list_lock; +1 gives scnprintf() room for its
233 * NUL terminator so a full MAX_PRINT_CHUNK payload is not truncated
234 */
235 char buf[MAX_PRINT_CHUNK + 1];
236 struct work_struct resume_wq;
237 struct sk_buff_head skb_pool;
238 struct work_struct refill_wq;
239 };
240
241 #ifdef CONFIG_NETCONSOLE_DYNAMIC
242
243 static struct configfs_subsystem netconsole_subsys;
244 static DEFINE_MUTEX(dynamic_netconsole_mutex);
245
dynamic_netconsole_init(void)246 static int __init dynamic_netconsole_init(void)
247 {
248 config_group_init(&netconsole_subsys.su_group);
249 mutex_init(&netconsole_subsys.su_mutex);
250 return configfs_register_subsystem(&netconsole_subsys);
251 }
252
dynamic_netconsole_exit(void)253 static void __exit dynamic_netconsole_exit(void)
254 {
255 configfs_unregister_subsystem(&netconsole_subsys);
256 }
257
258 /*
259 * Targets that were created by parsing the boot/module option string
260 * do not exist in the configfs hierarchy (and have NULL names) and will
261 * never go away, so make these a no-op for them.
262 */
netconsole_target_get(struct netconsole_target * nt)263 static void netconsole_target_get(struct netconsole_target *nt)
264 {
265 if (config_item_name(&nt->group.cg_item))
266 config_group_get(&nt->group);
267 }
268
netconsole_target_put(struct netconsole_target * nt)269 static void netconsole_target_put(struct netconsole_target *nt)
270 {
271 if (config_item_name(&nt->group.cg_item))
272 config_group_put(&nt->group);
273 }
274
dynamic_netconsole_mutex_lock(void)275 static void dynamic_netconsole_mutex_lock(void)
276 {
277 mutex_lock(&dynamic_netconsole_mutex);
278 }
279
dynamic_netconsole_mutex_unlock(void)280 static void dynamic_netconsole_mutex_unlock(void)
281 {
282 mutex_unlock(&dynamic_netconsole_mutex);
283 }
284
285 #else /* !CONFIG_NETCONSOLE_DYNAMIC */
286
dynamic_netconsole_init(void)287 static int __init dynamic_netconsole_init(void)
288 {
289 return 0;
290 }
291
dynamic_netconsole_exit(void)292 static void __exit dynamic_netconsole_exit(void)
293 {
294 }
295
296 /*
297 * No danger of targets going away from under us when dynamic
298 * reconfigurability is off.
299 */
netconsole_target_get(struct netconsole_target * nt)300 static void netconsole_target_get(struct netconsole_target *nt)
301 {
302 }
303
netconsole_target_put(struct netconsole_target * nt)304 static void netconsole_target_put(struct netconsole_target *nt)
305 {
306 }
307
populate_configfs_item(struct netconsole_target * nt,int cmdline_count)308 static void populate_configfs_item(struct netconsole_target *nt,
309 int cmdline_count)
310 {
311 }
312
dynamic_netconsole_mutex_lock(void)313 static void dynamic_netconsole_mutex_lock(void)
314 {
315 }
316
dynamic_netconsole_mutex_unlock(void)317 static void dynamic_netconsole_mutex_unlock(void)
318 {
319 }
320
321 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
322
323 /* Check if the target was bound by mac address. */
bound_by_mac(struct netconsole_target * nt)324 static bool bound_by_mac(struct netconsole_target *nt)
325 {
326 return is_valid_ether_addr(nt->np.dev_mac);
327 }
328
netcons_release_dev(struct netconsole_target * nt)329 static void netcons_release_dev(struct netconsole_target *nt)
330 {
331 do_netpoll_cleanup(&nt->np);
332 if (bound_by_mac(nt))
333 memset(&nt->np.dev_name, 0, IFNAMSIZ);
334 }
335
refill_skbs(struct netconsole_target * nt)336 static void refill_skbs(struct netconsole_target *nt)
337 {
338 struct sk_buff_head *skb_pool = &nt->skb_pool;
339 struct sk_buff *skb;
340
341 while (READ_ONCE(skb_pool->qlen) < MAX_SKBS) {
342 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC | __GFP_NOWARN);
343 if (!skb)
344 break;
345
346 skb_queue_tail(skb_pool, skb);
347 }
348 }
349
refill_skbs_work_handler(struct work_struct * work)350 static void refill_skbs_work_handler(struct work_struct *work)
351 {
352 struct netconsole_target *nt =
353 container_of(work, struct netconsole_target, refill_wq);
354
355 refill_skbs(nt);
356 }
357
358 /* Seed the per-target skb pool that find_skb() falls back to. The queue
359 * head and refill work are set up once in alloc_and_init(); this only
360 * (re)fills the pool. Pair with netconsole_skb_pool_flush().
361 */
netconsole_skb_pool_init(struct netconsole_target * nt)362 static void netconsole_skb_pool_init(struct netconsole_target *nt)
363 {
364 refill_skbs(nt);
365 }
366
netconsole_skb_pool_flush(struct netconsole_target * nt)367 static void netconsole_skb_pool_flush(struct netconsole_target *nt)
368 {
369 cancel_work_sync(&nt->refill_wq);
370 skb_queue_purge_reason(&nt->skb_pool, SKB_CONSUMED);
371 }
372
netcons_wait_carrier(struct netpoll * np,struct net_device * ndev)373 static void netcons_wait_carrier(struct netpoll *np, struct net_device *ndev)
374 {
375 unsigned long atmost;
376
377 atmost = jiffies + netpoll_get_carrier_timeout() * HZ;
378 while (!netif_carrier_ok(ndev)) {
379 if (time_after(jiffies, atmost)) {
380 np_notice(np, "timeout waiting for carrier\n");
381 break;
382 }
383 msleep(1);
384 }
385 }
386
387 /*
388 * Returns a pointer to a string representation of the identifier used
389 * to select the egress interface for the given netpoll instance. buf
390 * is used to format np->dev_mac when np->dev_name is empty; bufsz must
391 * be at least MAC_ADDR_STR_LEN + 1 to fit the formatted MAC address
392 * and its NUL terminator.
393 */
netcons_egress_dev(struct netpoll * np,char * buf,size_t bufsz)394 static char *netcons_egress_dev(struct netpoll *np, char *buf, size_t bufsz)
395 {
396 if (np->dev_name[0])
397 return np->dev_name;
398
399 snprintf(buf, bufsz, "%pM", np->dev_mac);
400 return buf;
401 }
402
403 /*
404 * Populate the target's local_ip with the IPv6 address from ndev.
405 */
netcons_take_ipv6(struct netconsole_target * nt,struct net_device * ndev)406 static int netcons_take_ipv6(struct netconsole_target *nt,
407 struct net_device *ndev)
408 {
409 char buf[MAC_ADDR_STR_LEN + 1];
410 struct netpoll *np = &nt->np;
411 int err = -EDESTADDRREQ;
412 struct inet6_dev *idev;
413
414 if (!IS_ENABLED(CONFIG_IPV6)) {
415 np_err(np, "IPv6 is not supported %s, aborting\n",
416 netcons_egress_dev(np, buf, sizeof(buf)));
417 return -EINVAL;
418 }
419
420 idev = __in6_dev_get(ndev);
421 if (idev) {
422 struct inet6_ifaddr *ifp;
423
424 read_lock_bh(&idev->lock);
425 list_for_each_entry(ifp, &idev->addr_list, if_list) {
426 if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
427 !!(ipv6_addr_type(&nt->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
428 continue;
429 /* Got the IP, let's return */
430 nt->local_ip.in6 = ifp->addr;
431 err = 0;
432 break;
433 }
434 read_unlock_bh(&idev->lock);
435 }
436 if (err) {
437 np_err(np, "no IPv6 address for %s, aborting\n",
438 netcons_egress_dev(np, buf, sizeof(buf)));
439 return err;
440 }
441
442 np_info(np, "local IPv6 %pI6c\n", &nt->local_ip.in6);
443 return 0;
444 }
445
446 /*
447 * Populate the target's local_ip with the IPv4 address from ndev.
448 */
netcons_take_ipv4(struct netconsole_target * nt,struct net_device * ndev)449 static int netcons_take_ipv4(struct netconsole_target *nt,
450 struct net_device *ndev)
451 {
452 char buf[MAC_ADDR_STR_LEN + 1];
453 struct netpoll *np = &nt->np;
454 const struct in_ifaddr *ifa;
455 struct in_device *in_dev;
456
457 in_dev = __in_dev_get_rtnl(ndev);
458 if (!in_dev) {
459 np_err(np, "no IP address for %s, aborting\n",
460 netcons_egress_dev(np, buf, sizeof(buf)));
461 return -EDESTADDRREQ;
462 }
463
464 ifa = rtnl_dereference(in_dev->ifa_list);
465 if (!ifa) {
466 np_err(np, "no IP address for %s, aborting\n",
467 netcons_egress_dev(np, buf, sizeof(buf)));
468 return -EDESTADDRREQ;
469 }
470
471 nt->local_ip.ip = ifa->ifa_local;
472 np_info(np, "local IP %pI4\n", &nt->local_ip.ip);
473
474 return 0;
475 }
476
477 /*
478 * Test whether the caller left nt->local_ip unset, so that
479 * netcons_netpoll_setup() should auto-populate it from the egress device.
480 *
481 * nt->local_ip is a union of __be32 (IPv4) and struct in6_addr (IPv6),
482 * so an IPv6 address whose first 4 bytes are zero (e.g. ::1, ::2,
483 * IPv4-mapped ::ffff:a.b.c.d) must not be tested via the IPv4 arm —
484 * doing so would misclassify a caller-supplied address as unset and
485 * silently overwrite it with whatever address the device exposes.
486 */
netcons_local_ip_unset(const struct netconsole_target * nt)487 static bool netcons_local_ip_unset(const struct netconsole_target *nt)
488 {
489 if (nt->ipv6)
490 return ipv6_addr_any(&nt->local_ip.in6);
491 return !nt->local_ip.ip;
492 }
493
netcons_netpoll_setup(struct netconsole_target * nt)494 static int netcons_netpoll_setup(struct netconsole_target *nt)
495 {
496 struct net *net = current->nsproxy->net_ns;
497 char buf[MAC_ADDR_STR_LEN + 1];
498 struct net_device *ndev = NULL;
499 struct netpoll *np = &nt->np;
500 bool ip_overwritten = false;
501 int err;
502
503 rtnl_lock();
504 if (np->dev_name[0])
505 ndev = __dev_get_by_name(net, np->dev_name);
506 else if (is_valid_ether_addr(np->dev_mac))
507 ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac);
508
509 if (!ndev) {
510 np_err(np, "%s doesn't exist, aborting\n",
511 netcons_egress_dev(np, buf, sizeof(buf)));
512 err = -ENODEV;
513 goto unlock;
514 }
515 netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
516
517 if (netdev_master_upper_dev_get(ndev)) {
518 np_err(np, "%s is a slave device, aborting\n",
519 netcons_egress_dev(np, buf, sizeof(buf)));
520 err = -EBUSY;
521 goto put;
522 }
523
524 if (!netif_running(ndev)) {
525 np_info(np, "device %s not up yet, forcing it\n",
526 netcons_egress_dev(np, buf, sizeof(buf)));
527
528 err = dev_open(ndev, NULL);
529 if (err) {
530 np_err(np, "failed to open %s\n", ndev->name);
531 goto put;
532 }
533
534 rtnl_unlock();
535 netcons_wait_carrier(np, ndev);
536 rtnl_lock();
537 }
538
539 if (netcons_local_ip_unset(nt)) {
540 if (!nt->ipv6) {
541 err = netcons_take_ipv4(nt, ndev);
542 if (err)
543 goto put;
544 } else {
545 err = netcons_take_ipv6(nt, ndev);
546 if (err)
547 goto put;
548 }
549 ip_overwritten = true;
550 }
551
552 err = __netpoll_setup(np, ndev);
553 if (err)
554 goto put;
555 rtnl_unlock();
556
557 /* Make sure all NAPI polls which started before dev->npinfo
558 * was visible have exited before we start calling NAPI poll.
559 * NAPI skips locking if dev->npinfo is NULL.
560 */
561 synchronize_rcu();
562
563 return 0;
564
565 put:
566 DEBUG_NET_WARN_ON_ONCE(np->dev);
567 if (ip_overwritten)
568 memset(&nt->local_ip, 0, sizeof(nt->local_ip));
569 netdev_put(ndev, &np->dev_tracker);
570 unlock:
571 rtnl_unlock();
572 return err;
573 }
574
575 /* Attempts to resume logging to a deactivated target. */
resume_target(struct netconsole_target * nt)576 static void resume_target(struct netconsole_target *nt)
577 {
578 /* Initialise the skb pool before netpoll_setup() makes nt->np.dev
579 * visible to target_list walkers (e.g. netconsole_netdev_event),
580 * which otherwise may move the target to the cleanup list and
581 * call netconsole_skb_pool_flush() on uninitialised state.
582 */
583 netconsole_skb_pool_init(nt);
584
585 if (netcons_netpoll_setup(nt)) {
586 /* netpoll fails setup once, do not try again. */
587 netconsole_skb_pool_flush(nt);
588 nt->state = STATE_DISABLED;
589 return;
590 }
591
592 nt->state = STATE_ENABLED;
593 pr_info("network logging resumed on interface %s\n", nt->np.dev_name);
594 }
595
596 /* Checks if a deactivated target matches a device. */
deactivated_target_match(struct netconsole_target * nt,struct net_device * ndev)597 static bool deactivated_target_match(struct netconsole_target *nt,
598 struct net_device *ndev)
599 {
600 if (nt->state != STATE_DEACTIVATED)
601 return false;
602
603 if (bound_by_mac(nt))
604 return !memcmp(nt->np.dev_mac, ndev->dev_addr, ETH_ALEN);
605 return !strncmp(nt->np.dev_name, ndev->name, IFNAMSIZ);
606 }
607
608 /* Process work scheduled for target resume. */
process_resume_target(struct work_struct * work)609 static void process_resume_target(struct work_struct *work)
610 {
611 struct netconsole_target *nt;
612 unsigned long flags;
613
614 nt = container_of(work, struct netconsole_target, resume_wq);
615
616 dynamic_netconsole_mutex_lock();
617
618 spin_lock_irqsave(&target_list_lock, flags);
619 /* Check if target is still deactivated as it may have been disabled
620 * while resume was being scheduled.
621 */
622 if (nt->state != STATE_DEACTIVATED) {
623 spin_unlock_irqrestore(&target_list_lock, flags);
624 goto out_unlock;
625 }
626
627 /* resume_target is IRQ unsafe, remove target from
628 * target_list in order to resume it with IRQ enabled.
629 */
630 list_del_init(&nt->list);
631 spin_unlock_irqrestore(&target_list_lock, flags);
632
633 resume_target(nt);
634
635 /* netpoll_setup() took a net_device reference and dropped the RTNL
636 * before returning, all while this target was off target_list and
637 * thus invisible to netconsole_netdev_event(). If the device was
638 * unregistered in that window the NETDEV_UNREGISTER notifier could not
639 * tear this target down, which would leak the reference and hang
640 * unregister_netdevice(). Re-check under the RTNL before re-publishing:
641 * taking it across the check and the list_add() serialises against the
642 * notifier (which also runs under the RTNL), so the device is either
643 * still registered (the notifier will find the re-added target) or
644 * already unregistering (we drop the reference here).
645 */
646 rtnl_lock();
647 if (nt->state == STATE_ENABLED && nt->np.dev &&
648 nt->np.dev->reg_state != NETREG_REGISTERED) {
649 netconsole_skb_pool_flush(nt);
650 netcons_release_dev(nt);
651 nt->state = STATE_DISABLED;
652 }
653
654 /* At this point the target is either enabled or disabled and
655 * was cleaned up before getting deactivated. Either way, add it
656 * back to target list.
657 */
658 spin_lock_irqsave(&target_list_lock, flags);
659 list_add(&nt->list, &target_list);
660 spin_unlock_irqrestore(&target_list_lock, flags);
661 rtnl_unlock();
662
663 out_unlock:
664 dynamic_netconsole_mutex_unlock();
665 }
666
667 /* Allocate and initialize with defaults.
668 * Note that these targets get their config_item fields zeroed-out.
669 */
alloc_and_init(void)670 static struct netconsole_target *alloc_and_init(void)
671 {
672 struct netconsole_target *nt;
673
674 nt = kzalloc_obj(*nt);
675 if (!nt)
676 return nt;
677
678 if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
679 nt->extended = true;
680 if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
681 nt->release = true;
682
683 nt->np.name = "netconsole";
684 strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
685 nt->local_port = 6665;
686 nt->remote_port = 6666;
687 eth_broadcast_addr(nt->remote_mac);
688 nt->state = STATE_DISABLED;
689 INIT_WORK(&nt->resume_wq, process_resume_target);
690 /* Set up the skb pool primitives once; enabling only refills it. */
691 skb_queue_head_init(&nt->skb_pool);
692 INIT_WORK(&nt->refill_wq, refill_skbs_work_handler);
693
694 return nt;
695 }
696
697 /* Clean up every target in the cleanup_list and move the clean targets back to
698 * the main target_list.
699 */
netconsole_process_cleanups_core(void)700 static void netconsole_process_cleanups_core(void)
701 {
702 struct netconsole_target *nt, *tmp;
703 unsigned long flags;
704
705 /* The cleanup needs RTNL locked */
706 ASSERT_RTNL();
707
708 mutex_lock(&target_cleanup_list_lock);
709 list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) {
710 /* all entries in the cleanup_list needs to be disabled */
711 WARN_ON_ONCE(nt->state == STATE_ENABLED);
712 netconsole_skb_pool_flush(nt);
713 netcons_release_dev(nt);
714 /* moved the cleaned target to target_list. Need to hold both
715 * locks
716 */
717 spin_lock_irqsave(&target_list_lock, flags);
718 list_move(&nt->list, &target_list);
719 spin_unlock_irqrestore(&target_list_lock, flags);
720 }
721 WARN_ON_ONCE(!list_empty(&target_cleanup_list));
722 mutex_unlock(&target_cleanup_list_lock);
723 }
724
netconsole_print_banner(struct netconsole_target * nt)725 static void netconsole_print_banner(struct netconsole_target *nt)
726 {
727 struct netpoll *np = &nt->np;
728
729 np_info(np, "local port %d\n", nt->local_port);
730 if (nt->ipv6)
731 np_info(np, "local IPv6 address %pI6c\n", &nt->local_ip.in6);
732 else
733 np_info(np, "local IPv4 address %pI4\n", &nt->local_ip.ip);
734 np_info(np, "interface name '%s'\n", np->dev_name);
735 np_info(np, "local ethernet address '%pM'\n", np->dev_mac);
736 np_info(np, "remote port %d\n", nt->remote_port);
737 if (nt->ipv6)
738 np_info(np, "remote IPv6 address %pI6c\n", &nt->remote_ip.in6);
739 else
740 np_info(np, "remote IPv4 address %pI4\n", &nt->remote_ip.ip);
741 np_info(np, "remote ethernet address %pM\n", nt->remote_mac);
742 }
743
744 /* Parse the string and populate the `inet_addr` union. Return 0 if IPv4 is
745 * populated, 1 if IPv6 is populated, and -1 upon failure.
746 */
netpoll_parse_ip_addr(const char * str,union inet_addr * addr)747 static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
748 {
749 const char *end = NULL;
750 int len;
751
752 len = strlen(str);
753 if (!len)
754 return -1;
755
756 if (str[len - 1] == '\n')
757 len -= 1;
758
759 if (in4_pton(str, len, (void *)addr, -1, &end) > 0 &&
760 (!end || *end == 0 || *end == '\n'))
761 return 0;
762
763 if (IS_ENABLED(CONFIG_IPV6) &&
764 in6_pton(str, len, (void *)addr, -1, &end) > 0 &&
765 (!end || *end == 0 || *end == '\n'))
766 return 1;
767
768 return -1;
769 }
770
771 #ifdef CONFIG_NETCONSOLE_DYNAMIC
772
773 /*
774 * Our subsystem hierarchy is:
775 *
776 * /sys/kernel/config/netconsole/
777 * |
778 * <target>/
779 * | enabled
780 * | release
781 * | dev_name
782 * | local_port
783 * | remote_port
784 * | local_ip
785 * | remote_ip
786 * | local_mac
787 * | remote_mac
788 * | transmit_errors
789 * | userdata/
790 * | <key>/
791 * | value
792 * | ...
793 * |
794 * <target>/...
795 */
796
to_target(struct config_item * item)797 static struct netconsole_target *to_target(struct config_item *item)
798 {
799 struct config_group *cfg_group;
800
801 cfg_group = to_config_group(item);
802 if (!cfg_group)
803 return NULL;
804 return container_of(to_config_group(item),
805 struct netconsole_target, group);
806 }
807
808 /* Do the list cleanup with the rtnl lock hold. rtnl lock is necessary because
809 * netdev might be cleaned-up by calling __netpoll_cleanup(),
810 */
netconsole_process_cleanups(void)811 static void netconsole_process_cleanups(void)
812 {
813 /* rtnl lock is called here, because it has precedence over
814 * target_cleanup_list_lock mutex and target_cleanup_list
815 */
816 rtnl_lock();
817 netconsole_process_cleanups_core();
818 rtnl_unlock();
819 }
820
821 /* Get rid of possible trailing newline, returning the new length */
trim_newline(char * s,size_t maxlen)822 static void trim_newline(char *s, size_t maxlen)
823 {
824 size_t len;
825
826 len = strnlen(s, maxlen);
827 if (!len)
828 return;
829 if (s[len - 1] == '\n')
830 s[len - 1] = '\0';
831 }
832
833 /*
834 * Attribute operations for netconsole_target.
835 */
836
enabled_show(struct config_item * item,char * buf)837 static ssize_t enabled_show(struct config_item *item, char *buf)
838 {
839 return sysfs_emit(buf, "%d\n", to_target(item)->state == STATE_ENABLED);
840 }
841
extended_show(struct config_item * item,char * buf)842 static ssize_t extended_show(struct config_item *item, char *buf)
843 {
844 return sysfs_emit(buf, "%d\n", to_target(item)->extended);
845 }
846
release_show(struct config_item * item,char * buf)847 static ssize_t release_show(struct config_item *item, char *buf)
848 {
849 return sysfs_emit(buf, "%d\n", to_target(item)->release);
850 }
851
dev_name_show(struct config_item * item,char * buf)852 static ssize_t dev_name_show(struct config_item *item, char *buf)
853 {
854 return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
855 }
856
local_port_show(struct config_item * item,char * buf)857 static ssize_t local_port_show(struct config_item *item, char *buf)
858 {
859 return sysfs_emit(buf, "%d\n", to_target(item)->local_port);
860 }
861
remote_port_show(struct config_item * item,char * buf)862 static ssize_t remote_port_show(struct config_item *item, char *buf)
863 {
864 return sysfs_emit(buf, "%d\n", to_target(item)->remote_port);
865 }
866
local_ip_show(struct config_item * item,char * buf)867 static ssize_t local_ip_show(struct config_item *item, char *buf)
868 {
869 struct netconsole_target *nt = to_target(item);
870
871 if (nt->ipv6)
872 return sysfs_emit(buf, "%pI6c\n", &nt->local_ip.in6);
873 else
874 return sysfs_emit(buf, "%pI4\n", &nt->local_ip);
875 }
876
remote_ip_show(struct config_item * item,char * buf)877 static ssize_t remote_ip_show(struct config_item *item, char *buf)
878 {
879 struct netconsole_target *nt = to_target(item);
880
881 if (nt->ipv6)
882 return sysfs_emit(buf, "%pI6c\n", &nt->remote_ip.in6);
883 else
884 return sysfs_emit(buf, "%pI4\n", &nt->remote_ip);
885 }
886
local_mac_show(struct config_item * item,char * buf)887 static ssize_t local_mac_show(struct config_item *item, char *buf)
888 {
889 struct net_device *dev = to_target(item)->np.dev;
890 static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
891
892 return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
893 }
894
remote_mac_show(struct config_item * item,char * buf)895 static ssize_t remote_mac_show(struct config_item *item, char *buf)
896 {
897 return sysfs_emit(buf, "%pM\n", to_target(item)->remote_mac);
898 }
899
transmit_errors_show(struct config_item * item,char * buf)900 static ssize_t transmit_errors_show(struct config_item *item, char *buf)
901 {
902 struct netconsole_target *nt = to_target(item);
903 u64 xmit_drop_count, enomem_count;
904 unsigned int start;
905
906 do {
907 start = u64_stats_fetch_begin(&nt->stats.syncp);
908 xmit_drop_count = u64_stats_read(&nt->stats.xmit_drop_count);
909 enomem_count = u64_stats_read(&nt->stats.enomem_count);
910 } while (u64_stats_fetch_retry(&nt->stats.syncp, start));
911
912 return sysfs_emit(buf, "%llu\n", xmit_drop_count + enomem_count);
913 }
914
915 /* configfs helper to display if cpu_nr sysdata feature is enabled */
sysdata_cpu_nr_enabled_show(struct config_item * item,char * buf)916 static ssize_t sysdata_cpu_nr_enabled_show(struct config_item *item, char *buf)
917 {
918 struct netconsole_target *nt = to_target(item->ci_parent);
919 bool cpu_nr_enabled;
920
921 dynamic_netconsole_mutex_lock();
922 cpu_nr_enabled = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
923 dynamic_netconsole_mutex_unlock();
924
925 return sysfs_emit(buf, "%d\n", cpu_nr_enabled);
926 }
927
928 /* configfs helper to display if taskname sysdata feature is enabled */
sysdata_taskname_enabled_show(struct config_item * item,char * buf)929 static ssize_t sysdata_taskname_enabled_show(struct config_item *item,
930 char *buf)
931 {
932 struct netconsole_target *nt = to_target(item->ci_parent);
933 bool taskname_enabled;
934
935 dynamic_netconsole_mutex_lock();
936 taskname_enabled = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
937 dynamic_netconsole_mutex_unlock();
938
939 return sysfs_emit(buf, "%d\n", taskname_enabled);
940 }
941
sysdata_release_enabled_show(struct config_item * item,char * buf)942 static ssize_t sysdata_release_enabled_show(struct config_item *item,
943 char *buf)
944 {
945 struct netconsole_target *nt = to_target(item->ci_parent);
946 bool release_enabled;
947
948 dynamic_netconsole_mutex_lock();
949 release_enabled = !!(nt->sysdata_fields & SYSDATA_RELEASE);
950 dynamic_netconsole_mutex_unlock();
951
952 return sysfs_emit(buf, "%d\n", release_enabled);
953 }
954
955 /* Iterate in the list of target, and make sure we don't have any console
956 * register without targets of the same type
957 */
unregister_netcons_consoles(void)958 static void unregister_netcons_consoles(void)
959 {
960 struct netconsole_target *nt;
961 u32 console_type_needed = 0;
962 unsigned long flags;
963
964 spin_lock_irqsave(&target_list_lock, flags);
965 list_for_each_entry(nt, &target_list, list) {
966 if (nt->extended)
967 console_type_needed |= CONS_EXTENDED;
968 else
969 console_type_needed |= CONS_BASIC;
970 }
971 spin_unlock_irqrestore(&target_list_lock, flags);
972
973 if (!(console_type_needed & CONS_EXTENDED) &&
974 console_is_registered(&netconsole_ext))
975 unregister_console(&netconsole_ext);
976
977 if (!(console_type_needed & CONS_BASIC) &&
978 console_is_registered(&netconsole))
979 unregister_console(&netconsole);
980 }
981
sysdata_msgid_enabled_show(struct config_item * item,char * buf)982 static ssize_t sysdata_msgid_enabled_show(struct config_item *item,
983 char *buf)
984 {
985 struct netconsole_target *nt = to_target(item->ci_parent);
986 bool msgid_enabled;
987
988 dynamic_netconsole_mutex_lock();
989 msgid_enabled = !!(nt->sysdata_fields & SYSDATA_MSGID);
990 dynamic_netconsole_mutex_unlock();
991
992 return sysfs_emit(buf, "%d\n", msgid_enabled);
993 }
994
995 /*
996 * This one is special -- targets created through the configfs interface
997 * are not enabled (and the corresponding netpoll activated) by default.
998 * The user is expected to set the desired parameters first (which
999 * would enable him to dynamically add new netpoll targets for new
1000 * network interfaces as and when they come up).
1001 */
enabled_store(struct config_item * item,const char * buf,size_t count)1002 static ssize_t enabled_store(struct config_item *item,
1003 const char *buf, size_t count)
1004 {
1005 struct netconsole_target *nt = to_target(item);
1006 bool enabled, current_enabled;
1007 unsigned long flags;
1008 ssize_t ret;
1009
1010 dynamic_netconsole_mutex_lock();
1011 ret = kstrtobool(buf, &enabled);
1012 if (ret)
1013 goto out_unlock;
1014
1015 /* When the user explicitly enables or disables a target that is
1016 * currently deactivated, reset its state to disabled. The DEACTIVATED
1017 * state only tracks interface-driven deactivation and should _not_
1018 * persist when the user manually changes the target's enabled state.
1019 */
1020 if (nt->state == STATE_DEACTIVATED)
1021 nt->state = STATE_DISABLED;
1022
1023 ret = -EINVAL;
1024 current_enabled = nt->state == STATE_ENABLED;
1025 if (enabled == current_enabled) {
1026 pr_info("network logging has already %s\n",
1027 current_enabled ? "started" : "stopped");
1028 goto out_unlock;
1029 }
1030
1031 if (enabled) { /* true */
1032 if (nt->release && !nt->extended) {
1033 pr_err("Not enabling netconsole. Release feature requires extended log message");
1034 goto out_unlock;
1035 }
1036
1037 if (nt->extended && !console_is_registered(&netconsole_ext)) {
1038 netconsole_ext.flags |= CON_ENABLED;
1039 register_console(&netconsole_ext);
1040 }
1041
1042 /* User might be enabling the basic format target for the very
1043 * first time, make sure the console is registered.
1044 */
1045 if (!nt->extended && !console_is_registered(&netconsole)) {
1046 netconsole.flags |= CON_ENABLED;
1047 register_console(&netconsole);
1048 }
1049
1050 /*
1051 * Skip netconsole_parser_cmdline() -- all the attributes are
1052 * already configured via configfs. Just print them out.
1053 */
1054 netconsole_print_banner(nt);
1055
1056 /* Initialise the skb pool before netpoll_setup() so the pool
1057 * is valid as soon as nt->np.dev becomes visible to
1058 * target_list walkers (netconsole_netdev_event), which would
1059 * otherwise call netconsole_skb_pool_flush() on uninitialised
1060 * state.
1061 */
1062 netconsole_skb_pool_init(nt);
1063
1064 ret = netcons_netpoll_setup(nt);
1065 if (ret) {
1066 netconsole_skb_pool_flush(nt);
1067 goto out_unlock;
1068 }
1069
1070 nt->state = STATE_ENABLED;
1071 pr_info("network logging started\n");
1072 } else { /* false */
1073 /* We need to disable the netconsole before cleaning it up
1074 * otherwise we might end up in write_msg() with
1075 * nt->np.dev == NULL and nt->state == STATE_ENABLED
1076 */
1077 mutex_lock(&target_cleanup_list_lock);
1078 spin_lock_irqsave(&target_list_lock, flags);
1079 nt->state = STATE_DISABLED;
1080 /* Remove the target from the list, while holding
1081 * target_list_lock
1082 */
1083 list_move(&nt->list, &target_cleanup_list);
1084 spin_unlock_irqrestore(&target_list_lock, flags);
1085 mutex_unlock(&target_cleanup_list_lock);
1086 /* Unregister consoles, whose the last target of that type got
1087 * disabled.
1088 */
1089 unregister_netcons_consoles();
1090 }
1091
1092 ret = count;
1093 /* Deferred cleanup */
1094 netconsole_process_cleanups();
1095 out_unlock:
1096 dynamic_netconsole_mutex_unlock();
1097 return ret;
1098 }
1099
release_store(struct config_item * item,const char * buf,size_t count)1100 static ssize_t release_store(struct config_item *item, const char *buf,
1101 size_t count)
1102 {
1103 struct netconsole_target *nt = to_target(item);
1104 bool release;
1105 ssize_t ret;
1106
1107 dynamic_netconsole_mutex_lock();
1108 if (nt->state == STATE_ENABLED) {
1109 pr_err("target (%s) is enabled, disable to update parameters\n",
1110 config_item_name(&nt->group.cg_item));
1111 ret = -EINVAL;
1112 goto out_unlock;
1113 }
1114
1115 ret = kstrtobool(buf, &release);
1116 if (ret)
1117 goto out_unlock;
1118
1119 nt->release = release;
1120
1121 ret = count;
1122 out_unlock:
1123 dynamic_netconsole_mutex_unlock();
1124 return ret;
1125 }
1126
extended_store(struct config_item * item,const char * buf,size_t count)1127 static ssize_t extended_store(struct config_item *item, const char *buf,
1128 size_t count)
1129 {
1130 struct netconsole_target *nt = to_target(item);
1131 bool extended;
1132 ssize_t ret;
1133
1134 dynamic_netconsole_mutex_lock();
1135 if (nt->state == STATE_ENABLED) {
1136 pr_err("target (%s) is enabled, disable to update parameters\n",
1137 config_item_name(&nt->group.cg_item));
1138 ret = -EINVAL;
1139 goto out_unlock;
1140 }
1141
1142 ret = kstrtobool(buf, &extended);
1143 if (ret)
1144 goto out_unlock;
1145
1146 nt->extended = extended;
1147 ret = count;
1148 out_unlock:
1149 dynamic_netconsole_mutex_unlock();
1150 return ret;
1151 }
1152
dev_name_store(struct config_item * item,const char * buf,size_t count)1153 static ssize_t dev_name_store(struct config_item *item, const char *buf,
1154 size_t count)
1155 {
1156 struct netconsole_target *nt = to_target(item);
1157 size_t len = count;
1158
1159 /* Account for a trailing newline appended by tools like echo */
1160 if (len && buf[len - 1] == '\n')
1161 len--;
1162 if (len >= IFNAMSIZ)
1163 return -ENAMETOOLONG;
1164
1165 dynamic_netconsole_mutex_lock();
1166 if (nt->state == STATE_ENABLED) {
1167 pr_err("target (%s) is enabled, disable to update parameters\n",
1168 config_item_name(&nt->group.cg_item));
1169 dynamic_netconsole_mutex_unlock();
1170 return -EINVAL;
1171 }
1172
1173 strscpy(nt->np.dev_name, buf, IFNAMSIZ);
1174 trim_newline(nt->np.dev_name, IFNAMSIZ);
1175
1176 dynamic_netconsole_mutex_unlock();
1177 return count;
1178 }
1179
local_port_store(struct config_item * item,const char * buf,size_t count)1180 static ssize_t local_port_store(struct config_item *item, const char *buf,
1181 size_t count)
1182 {
1183 struct netconsole_target *nt = to_target(item);
1184 ssize_t ret = -EINVAL;
1185
1186 dynamic_netconsole_mutex_lock();
1187 if (nt->state == STATE_ENABLED) {
1188 pr_err("target (%s) is enabled, disable to update parameters\n",
1189 config_item_name(&nt->group.cg_item));
1190 goto out_unlock;
1191 }
1192
1193 ret = kstrtou16(buf, 10, &nt->local_port);
1194 if (ret < 0)
1195 goto out_unlock;
1196 ret = count;
1197 out_unlock:
1198 dynamic_netconsole_mutex_unlock();
1199 return ret;
1200 }
1201
remote_port_store(struct config_item * item,const char * buf,size_t count)1202 static ssize_t remote_port_store(struct config_item *item,
1203 const char *buf, size_t count)
1204 {
1205 struct netconsole_target *nt = to_target(item);
1206 ssize_t ret = -EINVAL;
1207
1208 dynamic_netconsole_mutex_lock();
1209 if (nt->state == STATE_ENABLED) {
1210 pr_err("target (%s) is enabled, disable to update parameters\n",
1211 config_item_name(&nt->group.cg_item));
1212 goto out_unlock;
1213 }
1214
1215 ret = kstrtou16(buf, 10, &nt->remote_port);
1216 if (ret < 0)
1217 goto out_unlock;
1218 ret = count;
1219 out_unlock:
1220 dynamic_netconsole_mutex_unlock();
1221 return ret;
1222 }
1223
local_ip_store(struct config_item * item,const char * buf,size_t count)1224 static ssize_t local_ip_store(struct config_item *item, const char *buf,
1225 size_t count)
1226 {
1227 struct netconsole_target *nt = to_target(item);
1228 ssize_t ret = -EINVAL;
1229 int ipv6;
1230
1231 dynamic_netconsole_mutex_lock();
1232 if (nt->state == STATE_ENABLED) {
1233 pr_err("target (%s) is enabled, disable to update parameters\n",
1234 config_item_name(&nt->group.cg_item));
1235 goto out_unlock;
1236 }
1237
1238 ipv6 = netpoll_parse_ip_addr(buf, &nt->local_ip);
1239 if (ipv6 == -1)
1240 goto out_unlock;
1241 nt->ipv6 = !!ipv6;
1242
1243 ret = count;
1244 out_unlock:
1245 dynamic_netconsole_mutex_unlock();
1246 return ret;
1247 }
1248
remote_ip_store(struct config_item * item,const char * buf,size_t count)1249 static ssize_t remote_ip_store(struct config_item *item, const char *buf,
1250 size_t count)
1251 {
1252 struct netconsole_target *nt = to_target(item);
1253 ssize_t ret = -EINVAL;
1254 int ipv6;
1255
1256 dynamic_netconsole_mutex_lock();
1257 if (nt->state == STATE_ENABLED) {
1258 pr_err("target (%s) is enabled, disable to update parameters\n",
1259 config_item_name(&nt->group.cg_item));
1260 goto out_unlock;
1261 }
1262
1263 ipv6 = netpoll_parse_ip_addr(buf, &nt->remote_ip);
1264 if (ipv6 == -1)
1265 goto out_unlock;
1266 nt->ipv6 = !!ipv6;
1267
1268 ret = count;
1269 out_unlock:
1270 dynamic_netconsole_mutex_unlock();
1271 return ret;
1272 }
1273
1274 /* Count number of entries we have in userdata.
1275 * This is important because userdata only supports MAX_USERDATA_ITEMS
1276 * entries. Before enabling any new userdata feature, number of entries needs
1277 * to checked for available space.
1278 */
count_userdata_entries(struct netconsole_target * nt)1279 static size_t count_userdata_entries(struct netconsole_target *nt)
1280 {
1281 return list_count_nodes(&nt->userdata_group.cg_children);
1282 }
1283
remote_mac_store(struct config_item * item,const char * buf,size_t count)1284 static ssize_t remote_mac_store(struct config_item *item, const char *buf,
1285 size_t count)
1286 {
1287 struct netconsole_target *nt = to_target(item);
1288 u8 remote_mac[ETH_ALEN];
1289 ssize_t ret = -EINVAL;
1290
1291 dynamic_netconsole_mutex_lock();
1292 if (nt->state == STATE_ENABLED) {
1293 pr_err("target (%s) is enabled, disable to update parameters\n",
1294 config_item_name(&nt->group.cg_item));
1295 goto out_unlock;
1296 }
1297
1298 if (!mac_pton(buf, remote_mac))
1299 goto out_unlock;
1300 if (buf[MAC_ADDR_STR_LEN] && buf[MAC_ADDR_STR_LEN] != '\n')
1301 goto out_unlock;
1302 memcpy(nt->remote_mac, remote_mac, ETH_ALEN);
1303
1304 ret = count;
1305 out_unlock:
1306 dynamic_netconsole_mutex_unlock();
1307 return ret;
1308 }
1309
1310 struct userdatum {
1311 struct config_item item;
1312 char value[MAX_EXTRADATA_VALUE_LEN];
1313 };
1314
to_userdatum(struct config_item * item)1315 static struct userdatum *to_userdatum(struct config_item *item)
1316 {
1317 return container_of(item, struct userdatum, item);
1318 }
1319
1320 struct userdata {
1321 struct config_group group;
1322 };
1323
to_userdata(struct config_item * item)1324 static struct userdata *to_userdata(struct config_item *item)
1325 {
1326 return container_of(to_config_group(item), struct userdata, group);
1327 }
1328
userdata_to_target(struct userdata * ud)1329 static struct netconsole_target *userdata_to_target(struct userdata *ud)
1330 {
1331 struct config_group *netconsole_group;
1332
1333 netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
1334 return to_target(&netconsole_group->cg_item);
1335 }
1336
userdatum_value_show(struct config_item * item,char * buf)1337 static ssize_t userdatum_value_show(struct config_item *item, char *buf)
1338 {
1339 return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
1340 }
1341
1342 /* Navigate configfs and calculate the lentgh of the formatted string
1343 * representing userdata.
1344 * Must be called holding netconsole_subsys.su_mutex
1345 */
calc_userdata_len(struct netconsole_target * nt)1346 static int calc_userdata_len(struct netconsole_target *nt)
1347 {
1348 struct userdatum *udm_item;
1349 struct config_item *item;
1350 struct list_head *entry;
1351 int len = 0;
1352
1353 list_for_each(entry, &nt->userdata_group.cg_children) {
1354 item = container_of(entry, struct config_item, ci_entry);
1355 udm_item = to_userdatum(item);
1356 /* Skip userdata with no value set */
1357 if (udm_item->value[0]) {
1358 len += snprintf(NULL, 0, " %s=%s\n", item->ci_name,
1359 udm_item->value);
1360 }
1361 }
1362 return len;
1363 }
1364
update_userdata(struct netconsole_target * nt)1365 static int update_userdata(struct netconsole_target *nt)
1366 {
1367 struct netcons_userdata *new = NULL;
1368 struct netcons_userdata *old;
1369 struct userdatum *udm_item;
1370 struct config_item *item;
1371 struct list_head *entry;
1372 int offset = 0;
1373 int len;
1374
1375 /* Calculate required buffer size */
1376 len = calc_userdata_len(nt);
1377
1378 if (WARN_ON_ONCE(len > MAX_EXTRADATA_ENTRY_LEN * MAX_USERDATA_ITEMS))
1379 return -ENOSPC;
1380
1381 /* Allocate new buffer */
1382 if (len) {
1383 new = kmalloc_flex(*new, data, len + 1);
1384 if (!new)
1385 return -ENOMEM;
1386 }
1387
1388 /* Write userdata to new buffer */
1389 list_for_each(entry, &nt->userdata_group.cg_children) {
1390 item = container_of(entry, struct config_item, ci_entry);
1391 udm_item = to_userdatum(item);
1392 /* Skip userdata with no value set */
1393 if (udm_item->value[0]) {
1394 offset += scnprintf(&new->data[offset],
1395 len + 1 - offset,
1396 " %s=%s\n", item->ci_name,
1397 udm_item->value);
1398 }
1399 }
1400
1401 WARN_ON_ONCE(offset != len);
1402 if (new)
1403 new->length = offset;
1404
1405 /* Writers are serialized by dynamic_netconsole_mutex. */
1406 old = rcu_replace_pointer(nt->userdata, new,
1407 lockdep_is_held(&dynamic_netconsole_mutex));
1408 kfree_rcu(old, rcu);
1409
1410 return 0;
1411 }
1412
userdatum_value_store(struct config_item * item,const char * buf,size_t count)1413 static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
1414 size_t count)
1415 {
1416 struct userdatum *udm = to_userdatum(item);
1417 char old_value[MAX_EXTRADATA_VALUE_LEN];
1418 struct netconsole_target *nt;
1419 struct userdata *ud;
1420 ssize_t ret;
1421
1422 if (count >= MAX_EXTRADATA_VALUE_LEN)
1423 return -EMSGSIZE;
1424
1425 mutex_lock(&netconsole_subsys.su_mutex);
1426 dynamic_netconsole_mutex_lock();
1427 /* Snapshot for rollback if update_userdata() fails below */
1428 strscpy(old_value, udm->value, sizeof(old_value));
1429 /* count is bounded above, so strscpy() cannot truncate here */
1430 strscpy(udm->value, buf, sizeof(udm->value));
1431 trim_newline(udm->value, sizeof(udm->value));
1432
1433 ud = to_userdata(item->ci_parent);
1434 nt = userdata_to_target(ud);
1435 ret = update_userdata(nt);
1436 if (ret < 0) {
1437 /* Restore the previous value so it matches the live payload */
1438 strscpy(udm->value, old_value, sizeof(udm->value));
1439 goto out_unlock;
1440 }
1441 ret = count;
1442 out_unlock:
1443 dynamic_netconsole_mutex_unlock();
1444 mutex_unlock(&netconsole_subsys.su_mutex);
1445 return ret;
1446 }
1447
1448 /* disable_sysdata_feature - Disable sysdata feature and clean sysdata
1449 * @nt: target that is disabling the feature
1450 * @feature: feature being disabled
1451 */
disable_sysdata_feature(struct netconsole_target * nt,enum sysdata_feature feature)1452 static void disable_sysdata_feature(struct netconsole_target *nt,
1453 enum sysdata_feature feature)
1454 {
1455 nt->sysdata_fields &= ~feature;
1456 nt->sysdata[0] = 0;
1457 }
1458
sysdata_msgid_enabled_store(struct config_item * item,const char * buf,size_t count)1459 static ssize_t sysdata_msgid_enabled_store(struct config_item *item,
1460 const char *buf, size_t count)
1461 {
1462 struct netconsole_target *nt = to_target(item->ci_parent);
1463 bool msgid_enabled, curr;
1464 ssize_t ret;
1465
1466 ret = kstrtobool(buf, &msgid_enabled);
1467 if (ret)
1468 return ret;
1469
1470 mutex_lock(&netconsole_subsys.su_mutex);
1471 dynamic_netconsole_mutex_lock();
1472 curr = !!(nt->sysdata_fields & SYSDATA_MSGID);
1473 if (msgid_enabled == curr)
1474 goto unlock_ok;
1475
1476 if (msgid_enabled)
1477 nt->sysdata_fields |= SYSDATA_MSGID;
1478 else
1479 disable_sysdata_feature(nt, SYSDATA_MSGID);
1480
1481 unlock_ok:
1482 ret = count;
1483 dynamic_netconsole_mutex_unlock();
1484 mutex_unlock(&netconsole_subsys.su_mutex);
1485 return ret;
1486 }
1487
sysdata_release_enabled_store(struct config_item * item,const char * buf,size_t count)1488 static ssize_t sysdata_release_enabled_store(struct config_item *item,
1489 const char *buf, size_t count)
1490 {
1491 struct netconsole_target *nt = to_target(item->ci_parent);
1492 bool release_enabled, curr;
1493 ssize_t ret;
1494
1495 ret = kstrtobool(buf, &release_enabled);
1496 if (ret)
1497 return ret;
1498
1499 mutex_lock(&netconsole_subsys.su_mutex);
1500 dynamic_netconsole_mutex_lock();
1501 curr = !!(nt->sysdata_fields & SYSDATA_RELEASE);
1502 if (release_enabled == curr)
1503 goto unlock_ok;
1504
1505 if (release_enabled)
1506 nt->sysdata_fields |= SYSDATA_RELEASE;
1507 else
1508 disable_sysdata_feature(nt, SYSDATA_RELEASE);
1509
1510 unlock_ok:
1511 ret = count;
1512 dynamic_netconsole_mutex_unlock();
1513 mutex_unlock(&netconsole_subsys.su_mutex);
1514 return ret;
1515 }
1516
sysdata_taskname_enabled_store(struct config_item * item,const char * buf,size_t count)1517 static ssize_t sysdata_taskname_enabled_store(struct config_item *item,
1518 const char *buf, size_t count)
1519 {
1520 struct netconsole_target *nt = to_target(item->ci_parent);
1521 bool taskname_enabled, curr;
1522 ssize_t ret;
1523
1524 ret = kstrtobool(buf, &taskname_enabled);
1525 if (ret)
1526 return ret;
1527
1528 mutex_lock(&netconsole_subsys.su_mutex);
1529 dynamic_netconsole_mutex_lock();
1530 curr = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
1531 if (taskname_enabled == curr)
1532 goto unlock_ok;
1533
1534 if (taskname_enabled)
1535 nt->sysdata_fields |= SYSDATA_TASKNAME;
1536 else
1537 disable_sysdata_feature(nt, SYSDATA_TASKNAME);
1538
1539 unlock_ok:
1540 ret = count;
1541 dynamic_netconsole_mutex_unlock();
1542 mutex_unlock(&netconsole_subsys.su_mutex);
1543 return ret;
1544 }
1545
1546 /* configfs helper to sysdata cpu_nr feature */
sysdata_cpu_nr_enabled_store(struct config_item * item,const char * buf,size_t count)1547 static ssize_t sysdata_cpu_nr_enabled_store(struct config_item *item,
1548 const char *buf, size_t count)
1549 {
1550 struct netconsole_target *nt = to_target(item->ci_parent);
1551 bool cpu_nr_enabled, curr;
1552 ssize_t ret;
1553
1554 ret = kstrtobool(buf, &cpu_nr_enabled);
1555 if (ret)
1556 return ret;
1557
1558 mutex_lock(&netconsole_subsys.su_mutex);
1559 dynamic_netconsole_mutex_lock();
1560 curr = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
1561 if (cpu_nr_enabled == curr)
1562 /* no change requested */
1563 goto unlock_ok;
1564
1565 if (cpu_nr_enabled)
1566 nt->sysdata_fields |= SYSDATA_CPU_NR;
1567 else
1568 /* This is special because sysdata might have remaining data
1569 * from previous sysdata, and it needs to be cleaned.
1570 */
1571 disable_sysdata_feature(nt, SYSDATA_CPU_NR);
1572
1573 unlock_ok:
1574 ret = count;
1575 dynamic_netconsole_mutex_unlock();
1576 mutex_unlock(&netconsole_subsys.su_mutex);
1577 return ret;
1578 }
1579
1580 CONFIGFS_ATTR(userdatum_, value);
1581 CONFIGFS_ATTR(sysdata_, cpu_nr_enabled);
1582 CONFIGFS_ATTR(sysdata_, taskname_enabled);
1583 CONFIGFS_ATTR(sysdata_, release_enabled);
1584 CONFIGFS_ATTR(sysdata_, msgid_enabled);
1585
1586 static struct configfs_attribute *userdatum_attrs[] = {
1587 &userdatum_attr_value,
1588 NULL,
1589 };
1590
userdatum_release(struct config_item * item)1591 static void userdatum_release(struct config_item *item)
1592 {
1593 kfree(to_userdatum(item));
1594 }
1595
1596 static const struct configfs_item_operations userdatum_ops = {
1597 .release = userdatum_release,
1598 };
1599
1600 static const struct config_item_type userdatum_type = {
1601 .ct_item_ops = &userdatum_ops,
1602 .ct_attrs = userdatum_attrs,
1603 .ct_owner = THIS_MODULE,
1604 };
1605
userdatum_make_item(struct config_group * group,const char * name)1606 static struct config_item *userdatum_make_item(struct config_group *group,
1607 const char *name)
1608 {
1609 struct netconsole_target *nt;
1610 struct userdatum *udm;
1611 struct userdata *ud;
1612
1613 if (strlen(name) > MAX_EXTRADATA_NAME_LEN)
1614 return ERR_PTR(-ENAMETOOLONG);
1615
1616 ud = to_userdata(&group->cg_item);
1617 nt = userdata_to_target(ud);
1618 if (count_userdata_entries(nt) >= MAX_USERDATA_ITEMS)
1619 return ERR_PTR(-ENOSPC);
1620
1621 udm = kzalloc_obj(*udm);
1622 if (!udm)
1623 return ERR_PTR(-ENOMEM);
1624
1625 config_item_init_type_name(&udm->item, name, &userdatum_type);
1626 return &udm->item;
1627 }
1628
userdatum_drop(struct config_group * group,struct config_item * item)1629 static void userdatum_drop(struct config_group *group, struct config_item *item)
1630 {
1631 struct netconsole_target *nt;
1632 struct userdata *ud;
1633
1634 ud = to_userdata(&group->cg_item);
1635 nt = userdata_to_target(ud);
1636
1637 dynamic_netconsole_mutex_lock();
1638 update_userdata(nt);
1639 config_item_put(item);
1640 dynamic_netconsole_mutex_unlock();
1641 }
1642
1643 static struct configfs_attribute *userdata_attrs[] = {
1644 &sysdata_attr_cpu_nr_enabled,
1645 &sysdata_attr_taskname_enabled,
1646 &sysdata_attr_release_enabled,
1647 &sysdata_attr_msgid_enabled,
1648 NULL,
1649 };
1650
1651 static const struct configfs_group_operations userdata_ops = {
1652 .make_item = userdatum_make_item,
1653 .drop_item = userdatum_drop,
1654 };
1655
1656 static const struct config_item_type userdata_type = {
1657 .ct_item_ops = &userdatum_ops,
1658 .ct_group_ops = &userdata_ops,
1659 .ct_attrs = userdata_attrs,
1660 .ct_owner = THIS_MODULE,
1661 };
1662
1663 CONFIGFS_ATTR(, enabled);
1664 CONFIGFS_ATTR(, extended);
1665 CONFIGFS_ATTR(, dev_name);
1666 CONFIGFS_ATTR(, local_port);
1667 CONFIGFS_ATTR(, remote_port);
1668 CONFIGFS_ATTR(, local_ip);
1669 CONFIGFS_ATTR(, remote_ip);
1670 CONFIGFS_ATTR_RO(, local_mac);
1671 CONFIGFS_ATTR(, remote_mac);
1672 CONFIGFS_ATTR(, release);
1673 CONFIGFS_ATTR_RO(, transmit_errors);
1674
1675 static struct configfs_attribute *netconsole_target_attrs[] = {
1676 &attr_enabled,
1677 &attr_extended,
1678 &attr_release,
1679 &attr_dev_name,
1680 &attr_local_port,
1681 &attr_remote_port,
1682 &attr_local_ip,
1683 &attr_remote_ip,
1684 &attr_local_mac,
1685 &attr_remote_mac,
1686 &attr_transmit_errors,
1687 NULL,
1688 };
1689
1690 /*
1691 * Item operations and type for netconsole_target.
1692 */
1693
netconsole_target_release(struct config_item * item)1694 static void netconsole_target_release(struct config_item *item)
1695 {
1696 struct netconsole_target *nt = to_target(item);
1697
1698 kfree(rcu_access_pointer(nt->userdata));
1699 kfree(nt);
1700 }
1701
1702 static const struct configfs_item_operations netconsole_target_item_ops = {
1703 .release = netconsole_target_release,
1704 };
1705
1706 static const struct config_item_type netconsole_target_type = {
1707 .ct_attrs = netconsole_target_attrs,
1708 .ct_item_ops = &netconsole_target_item_ops,
1709 .ct_owner = THIS_MODULE,
1710 };
1711
init_target_config_group(struct netconsole_target * nt,const char * name)1712 static void init_target_config_group(struct netconsole_target *nt,
1713 const char *name)
1714 {
1715 config_group_init_type_name(&nt->group, name, &netconsole_target_type);
1716 config_group_init_type_name(&nt->userdata_group, "userdata",
1717 &userdata_type);
1718 configfs_add_default_group(&nt->userdata_group, &nt->group);
1719 }
1720
find_cmdline_target(const char * name)1721 static struct netconsole_target *find_cmdline_target(const char *name)
1722 {
1723 struct netconsole_target *nt, *ret = NULL;
1724 unsigned long flags;
1725
1726 spin_lock_irqsave(&target_list_lock, flags);
1727 list_for_each_entry(nt, &target_list, list) {
1728 if (!strcmp(nt->group.cg_item.ci_name, name)) {
1729 ret = nt;
1730 break;
1731 }
1732 }
1733 spin_unlock_irqrestore(&target_list_lock, flags);
1734
1735 return ret;
1736 }
1737
1738 /*
1739 * Group operations and type for netconsole_subsys.
1740 */
1741
make_netconsole_target(struct config_group * group,const char * name)1742 static struct config_group *make_netconsole_target(struct config_group *group,
1743 const char *name)
1744 {
1745 struct netconsole_target *nt;
1746 unsigned long flags;
1747
1748 /* Checking if a target by this name was created at boot time. If so,
1749 * attach a configfs entry to that target. This enables dynamic
1750 * control.
1751 */
1752 if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
1753 strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
1754 nt = find_cmdline_target(name);
1755 if (nt) {
1756 init_target_config_group(nt, name);
1757 return &nt->group;
1758 }
1759 }
1760
1761 nt = alloc_and_init();
1762 if (!nt)
1763 return ERR_PTR(-ENOMEM);
1764
1765 /* Initialize the config_group member */
1766 init_target_config_group(nt, name);
1767
1768 /* Adding, but it is disabled */
1769 spin_lock_irqsave(&target_list_lock, flags);
1770 list_add(&nt->list, &target_list);
1771 spin_unlock_irqrestore(&target_list_lock, flags);
1772
1773 return &nt->group;
1774 }
1775
drop_netconsole_target(struct config_group * group,struct config_item * item)1776 static void drop_netconsole_target(struct config_group *group,
1777 struct config_item *item)
1778 {
1779 struct netconsole_target *nt = to_target(item);
1780 unsigned long flags;
1781 bool needs_cleanup;
1782
1783 dynamic_netconsole_mutex_lock();
1784
1785 mutex_lock(&target_cleanup_list_lock);
1786 spin_lock_irqsave(&target_list_lock, flags);
1787 /* A target moved to target_cleanup_list by netconsole_netdev_event()
1788 * but not yet processed still owns a netpoll; unlinking it below hides
1789 * it from the cleanup worker, so this path must tear it down itself.
1790 * This covers NETDEV_UNREGISTER (STATE_DEACTIVATED) and
1791 * NETDEV_RELEASE / NETDEV_JOIN (STATE_DISABLED); key off nt->np.dev,
1792 * which stays set until the netpoll is cleaned up.
1793 */
1794 needs_cleanup = nt->state == STATE_ENABLED ||
1795 nt->state == STATE_DEACTIVATED || nt->np.dev;
1796 /* Disable deactivated target to prevent races between resume attempt
1797 * and target removal.
1798 */
1799 if (nt->state == STATE_DEACTIVATED)
1800 nt->state = STATE_DISABLED;
1801 list_del(&nt->list);
1802 spin_unlock_irqrestore(&target_list_lock, flags);
1803 mutex_unlock(&target_cleanup_list_lock);
1804
1805 dynamic_netconsole_mutex_unlock();
1806
1807 /* Now that the target has been marked disabled no further work
1808 * can be scheduled. Existing work will skip as targets are not
1809 * deactivated anymore. Cancel any scheduled resume and wait for
1810 * completion.
1811 */
1812 cancel_work_sync(&nt->resume_wq);
1813
1814 /*
1815 * The target may have never been enabled, or was manually disabled
1816 * before being removed so netpoll may have already been cleaned up.
1817 * netpoll_cleanup() is idempotent (it skips when np->dev is NULL), so
1818 * it is safe even if the cleanup worker already tore the netpoll down.
1819 */
1820 if (needs_cleanup) {
1821 netconsole_skb_pool_flush(nt);
1822 netpoll_cleanup(&nt->np);
1823 }
1824
1825 config_item_put(&nt->group.cg_item);
1826 }
1827
1828 static const struct configfs_group_operations netconsole_subsys_group_ops = {
1829 .make_group = make_netconsole_target,
1830 .drop_item = drop_netconsole_target,
1831 };
1832
1833 static const struct config_item_type netconsole_subsys_type = {
1834 .ct_group_ops = &netconsole_subsys_group_ops,
1835 .ct_owner = THIS_MODULE,
1836 };
1837
1838 /* The netconsole configfs subsystem */
1839 static struct configfs_subsystem netconsole_subsys = {
1840 .su_group = {
1841 .cg_item = {
1842 .ci_namebuf = "netconsole",
1843 .ci_type = &netconsole_subsys_type,
1844 },
1845 },
1846 };
1847
populate_configfs_item(struct netconsole_target * nt,int cmdline_count)1848 static void populate_configfs_item(struct netconsole_target *nt,
1849 int cmdline_count)
1850 {
1851 char target_name[16];
1852
1853 snprintf(target_name, sizeof(target_name), "%s%d",
1854 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
1855 init_target_config_group(nt, target_name);
1856 }
1857
sysdata_append_cpu_nr(struct netconsole_target * nt,int offset,struct nbcon_write_context * wctxt)1858 static int sysdata_append_cpu_nr(struct netconsole_target *nt, int offset,
1859 struct nbcon_write_context *wctxt)
1860 {
1861 return scnprintf(&nt->sysdata[offset],
1862 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
1863 wctxt->cpu);
1864 }
1865
sysdata_append_taskname(struct netconsole_target * nt,int offset,struct nbcon_write_context * wctxt)1866 static int sysdata_append_taskname(struct netconsole_target *nt, int offset,
1867 struct nbcon_write_context *wctxt)
1868 {
1869 return scnprintf(&nt->sysdata[offset],
1870 MAX_EXTRADATA_ENTRY_LEN, " taskname=%s\n",
1871 wctxt->comm);
1872 }
1873
sysdata_append_release(struct netconsole_target * nt,int offset)1874 static int sysdata_append_release(struct netconsole_target *nt, int offset)
1875 {
1876 return scnprintf(&nt->sysdata[offset],
1877 MAX_EXTRADATA_ENTRY_LEN, " release=%s\n",
1878 init_utsname()->release);
1879 }
1880
sysdata_append_msgid(struct netconsole_target * nt,int offset)1881 static int sysdata_append_msgid(struct netconsole_target *nt, int offset)
1882 {
1883 wrapping_assign_add(nt->msgcounter, 1);
1884 return scnprintf(&nt->sysdata[offset],
1885 MAX_EXTRADATA_ENTRY_LEN, " msgid=%u\n",
1886 nt->msgcounter);
1887 }
1888
1889 /*
1890 * prepare_sysdata - append sysdata in runtime
1891 * @nt: target to send message to
1892 * @wctxt: nbcon write context containing message metadata
1893 */
prepare_sysdata(struct netconsole_target * nt,struct nbcon_write_context * wctxt)1894 static int prepare_sysdata(struct netconsole_target *nt,
1895 struct nbcon_write_context *wctxt)
1896 {
1897 int sysdata_len = 0;
1898
1899 if (!nt->sysdata_fields)
1900 goto out;
1901
1902 if (nt->sysdata_fields & SYSDATA_CPU_NR)
1903 sysdata_len += sysdata_append_cpu_nr(nt, sysdata_len, wctxt);
1904 if (nt->sysdata_fields & SYSDATA_TASKNAME)
1905 sysdata_len += sysdata_append_taskname(nt, sysdata_len, wctxt);
1906 if (nt->sysdata_fields & SYSDATA_RELEASE)
1907 sysdata_len += sysdata_append_release(nt, sysdata_len);
1908 if (nt->sysdata_fields & SYSDATA_MSGID)
1909 sysdata_len += sysdata_append_msgid(nt, sysdata_len);
1910
1911 WARN_ON_ONCE(sysdata_len >
1912 MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS);
1913
1914 out:
1915 return sysdata_len;
1916 }
1917 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
1918
1919 /* Handle network interface device notifications */
netconsole_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)1920 static int netconsole_netdev_event(struct notifier_block *this,
1921 unsigned long event, void *ptr)
1922 {
1923 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1924 struct netconsole_target *nt, *tmp;
1925 bool stopped = false;
1926 unsigned long flags;
1927
1928 if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
1929 event == NETDEV_RELEASE || event == NETDEV_JOIN ||
1930 event == NETDEV_REGISTER))
1931 goto done;
1932
1933 mutex_lock(&target_cleanup_list_lock);
1934 spin_lock_irqsave(&target_list_lock, flags);
1935 list_for_each_entry_safe(nt, tmp, &target_list, list) {
1936 netconsole_target_get(nt);
1937 if (nt->np.dev == dev) {
1938 switch (event) {
1939 case NETDEV_CHANGENAME:
1940 strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
1941 break;
1942 case NETDEV_RELEASE:
1943 case NETDEV_JOIN:
1944 /* transition target to DISABLED instead of
1945 * DEACTIVATED when (de)enslaving devices as
1946 * their targets should not be automatically
1947 * resumed when the interface is brought up.
1948 */
1949 nt->state = STATE_DISABLED;
1950 list_move(&nt->list, &target_cleanup_list);
1951 stopped = true;
1952 break;
1953 case NETDEV_UNREGISTER:
1954 nt->state = STATE_DEACTIVATED;
1955 list_move(&nt->list, &target_cleanup_list);
1956 stopped = true;
1957 }
1958 }
1959 if ((event == NETDEV_REGISTER || event == NETDEV_CHANGENAME) &&
1960 deactivated_target_match(nt, dev))
1961 /* Schedule resume on a workqueue as it will attempt
1962 * to UP the device, which can't be done as part of this
1963 * notifier.
1964 */
1965 queue_work(netconsole_wq, &nt->resume_wq);
1966 netconsole_target_put(nt);
1967 }
1968 spin_unlock_irqrestore(&target_list_lock, flags);
1969 mutex_unlock(&target_cleanup_list_lock);
1970
1971 if (stopped) {
1972 const char *msg = "had an event";
1973
1974 switch (event) {
1975 case NETDEV_UNREGISTER:
1976 msg = "unregistered";
1977 break;
1978 case NETDEV_RELEASE:
1979 msg = "released slaves";
1980 break;
1981 case NETDEV_JOIN:
1982 msg = "is joining a master device";
1983 break;
1984 }
1985 pr_info("network logging stopped on interface %s as it %s\n",
1986 dev->name, msg);
1987 }
1988
1989 /* Process target_cleanup_list entries. By the end, target_cleanup_list
1990 * should be empty
1991 */
1992 netconsole_process_cleanups_core();
1993
1994 done:
1995 return NOTIFY_DONE;
1996 }
1997
1998 static struct notifier_block netconsole_netdev_notifier = {
1999 .notifier_call = netconsole_netdev_event,
2000 };
2001
2002 /* Pop a pre-allocated skb from the pool and request a refill.
2003 *
2004 * The pool is refilled with MAX_SKB_SIZE buffers, so a pooled skb cannot
2005 * satisfy a larger request. Return NULL in that case rather than handing
2006 * back a too-small skb that would later trip skb_over_panic() in skb_put();
2007 * the caller still polls and retries, and alloc_skb() itself can satisfy the
2008 * oversized request once memory frees up.
2009 *
2010 * The refill is requested via schedule_work(), which takes the workqueue
2011 * pool locks and is therefore not NMI-safe. Skip the refill when called
2012 * from NMI context; the next non-NMI caller will top the pool back up.
2013 */
netcons_skb_pop(struct netconsole_target * nt,int len)2014 static struct sk_buff *netcons_skb_pop(struct netconsole_target *nt, int len)
2015 {
2016 struct sk_buff *skb;
2017
2018 if (len > MAX_SKB_SIZE) {
2019 /* net_warn_ratelimited() pulls in printk machinery that is not
2020 * NMI-safe and could recurse into the nbcon console we are
2021 * servicing, so only warn outside NMI.
2022 */
2023 if (!in_nmi())
2024 net_warn_ratelimited("netconsole: dropping message, requested skb len %d exceeds pool buffer size %zu on %s\n",
2025 len, (size_t)MAX_SKB_SIZE,
2026 nt->np.dev->name);
2027 return NULL;
2028 }
2029
2030 skb = skb_dequeue(&nt->skb_pool);
2031 if (!in_nmi())
2032 schedule_work(&nt->refill_wq);
2033
2034 return skb;
2035 }
2036
find_skb(struct netconsole_target * nt,int len,int reserve)2037 static struct sk_buff *find_skb(struct netconsole_target *nt, int len,
2038 int reserve)
2039 {
2040 struct netpoll *np = &nt->np;
2041 int count = 0;
2042 struct sk_buff *skb;
2043
2044 netpoll_zap_completion_queue();
2045 repeat:
2046
2047 skb = alloc_skb(len, GFP_ATOMIC | __GFP_NOWARN);
2048 if (!skb)
2049 skb = netcons_skb_pop(nt, len);
2050
2051 if (!skb) {
2052 if (++count < 10) {
2053 netpoll_poll_dev(np->dev);
2054 goto repeat;
2055 }
2056 return NULL;
2057 }
2058
2059 refcount_set(&skb->users, 1);
2060 skb_reserve(skb, reserve);
2061 return skb;
2062 }
2063
netpoll_udp_checksum(struct netconsole_target * nt,struct sk_buff * skb,int len)2064 static void netpoll_udp_checksum(struct netconsole_target *nt,
2065 struct sk_buff *skb, int len)
2066 {
2067 struct udphdr *udph;
2068 int udp_len;
2069
2070 udp_len = len + sizeof(struct udphdr);
2071 udph = udp_hdr(skb);
2072
2073 /* check needs to be set, since it will be consumed in csum_partial */
2074 udph->check = 0;
2075 if (nt->ipv6)
2076 udph->check = csum_ipv6_magic(&nt->local_ip.in6,
2077 &nt->remote_ip.in6,
2078 udp_len, IPPROTO_UDP,
2079 csum_partial(udph, udp_len, 0));
2080 else
2081 udph->check = csum_tcpudp_magic(nt->local_ip.ip,
2082 nt->remote_ip.ip,
2083 udp_len, IPPROTO_UDP,
2084 csum_partial(udph, udp_len, 0));
2085 if (udph->check == 0)
2086 udph->check = CSUM_MANGLED_0;
2087 }
2088
push_udp(struct netconsole_target * nt,struct sk_buff * skb,int len)2089 static void push_udp(struct netconsole_target *nt, struct sk_buff *skb, int len)
2090 {
2091 struct udphdr *udph;
2092 int udp_len;
2093
2094 udp_len = len + sizeof(struct udphdr);
2095
2096 skb_push(skb, sizeof(struct udphdr));
2097 skb_reset_transport_header(skb);
2098
2099 udph = udp_hdr(skb);
2100 udph->source = htons(nt->local_port);
2101 udph->dest = htons(nt->remote_port);
2102 udp_set_len_short(udph, udp_len);
2103
2104 netpoll_udp_checksum(nt, skb, len);
2105 }
2106
push_eth(struct netconsole_target * nt,struct sk_buff * skb)2107 static void push_eth(struct netconsole_target *nt, struct sk_buff *skb)
2108 {
2109 struct netpoll *np = &nt->np;
2110 struct ethhdr *eth;
2111
2112 eth = skb_push(skb, ETH_HLEN);
2113 skb_reset_mac_header(skb);
2114 ether_addr_copy(eth->h_source, np->dev->dev_addr);
2115 ether_addr_copy(eth->h_dest, nt->remote_mac);
2116 if (nt->ipv6)
2117 eth->h_proto = htons(ETH_P_IPV6);
2118 else
2119 eth->h_proto = htons(ETH_P_IP);
2120 }
2121
push_ipv4(struct netconsole_target * nt,struct sk_buff * skb,int len)2122 static void push_ipv4(struct netconsole_target *nt, struct sk_buff *skb,
2123 int len)
2124 {
2125 static atomic_t ip_ident;
2126 struct iphdr *iph;
2127 int ip_len;
2128
2129 ip_len = len + sizeof(struct udphdr) + sizeof(struct iphdr);
2130
2131 skb_push(skb, sizeof(struct iphdr));
2132 skb_reset_network_header(skb);
2133 iph = ip_hdr(skb);
2134
2135 /* iph->version = 4; iph->ihl = 5; */
2136 *(unsigned char *)iph = 0x45;
2137 iph->tos = 0;
2138 put_unaligned(htons(ip_len), &iph->tot_len);
2139 iph->id = htons(atomic_inc_return(&ip_ident));
2140 iph->frag_off = 0;
2141 iph->ttl = 64;
2142 iph->protocol = IPPROTO_UDP;
2143 iph->check = 0;
2144 put_unaligned(nt->local_ip.ip, &iph->saddr);
2145 put_unaligned(nt->remote_ip.ip, &iph->daddr);
2146 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
2147 skb->protocol = htons(ETH_P_IP);
2148 }
2149
push_ipv6(struct netconsole_target * nt,struct sk_buff * skb,int len)2150 static void push_ipv6(struct netconsole_target *nt, struct sk_buff *skb,
2151 int len)
2152 {
2153 struct ipv6hdr *ip6h;
2154
2155 skb_push(skb, sizeof(struct ipv6hdr));
2156 skb_reset_network_header(skb);
2157 ip6h = ipv6_hdr(skb);
2158
2159 /* ip6h->version = 6; ip6h->priority = 0; */
2160 *(unsigned char *)ip6h = 0x60;
2161 ip6h->flow_lbl[0] = 0;
2162 ip6h->flow_lbl[1] = 0;
2163 ip6h->flow_lbl[2] = 0;
2164
2165 ip6h->payload_len = htons(sizeof(struct udphdr) + len);
2166 ip6h->nexthdr = IPPROTO_UDP;
2167 ip6h->hop_limit = 32;
2168 ip6h->saddr = nt->local_ip.in6;
2169 ip6h->daddr = nt->remote_ip.in6;
2170
2171 skb->protocol = htons(ETH_P_IPV6);
2172 }
2173
netpoll_send_udp(struct netconsole_target * nt,const char * msg,int len)2174 static int netpoll_send_udp(struct netconsole_target *nt, const char *msg,
2175 int len)
2176 {
2177 struct netpoll *np = &nt->np;
2178 int total_len, ip_len, udp_len;
2179 struct sk_buff *skb;
2180
2181 if (!IS_ENABLED(CONFIG_PREEMPT_RT))
2182 WARN_ON_ONCE(!irqs_disabled());
2183
2184 udp_len = len + sizeof(struct udphdr);
2185 if (nt->ipv6)
2186 ip_len = udp_len + sizeof(struct ipv6hdr);
2187 else
2188 ip_len = udp_len + sizeof(struct iphdr);
2189
2190 total_len = ip_len + LL_RESERVED_SPACE(np->dev);
2191
2192 skb = find_skb(nt, total_len + np->dev->needed_tailroom,
2193 total_len - len);
2194 if (!skb)
2195 return -ENOMEM;
2196
2197 skb_copy_to_linear_data(skb, msg, len);
2198 skb_put(skb, len);
2199
2200 push_udp(nt, skb, len);
2201 if (nt->ipv6)
2202 push_ipv6(nt, skb, len);
2203 else
2204 push_ipv4(nt, skb, len);
2205 push_eth(nt, skb);
2206 skb->dev = np->dev;
2207
2208 return (int)netpoll_send_skb(np, skb);
2209 }
2210
2211 /**
2212 * send_udp - Wrapper for netpoll_send_udp that counts errors
2213 * @nt: target to send message to
2214 * @msg: message to send
2215 * @len: length of message
2216 *
2217 * Calls netpoll_send_udp and classifies the return value. If an error
2218 * occurred it increments statistics in nt->stats accordingly.
2219 * Only calls netpoll_send_udp if CONFIG_NETCONSOLE_DYNAMIC is disabled.
2220 */
send_udp(struct netconsole_target * nt,const char * msg,int len)2221 static void send_udp(struct netconsole_target *nt, const char *msg, int len)
2222 {
2223 int result = netpoll_send_udp(nt, msg, len);
2224
2225 if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) {
2226 if (result == NET_XMIT_DROP) {
2227 u64_stats_update_begin(&nt->stats.syncp);
2228 u64_stats_inc(&nt->stats.xmit_drop_count);
2229 u64_stats_update_end(&nt->stats.syncp);
2230 } else if (result == -ENOMEM) {
2231 u64_stats_update_begin(&nt->stats.syncp);
2232 u64_stats_inc(&nt->stats.enomem_count);
2233 u64_stats_update_end(&nt->stats.syncp);
2234 }
2235 }
2236 }
2237
send_msg_no_fragmentation(struct netconsole_target * nt,const char * msg,int msg_len,int release_len,const struct netcons_userdata * userdata)2238 static void send_msg_no_fragmentation(struct netconsole_target *nt,
2239 const char *msg,
2240 int msg_len,
2241 int release_len,
2242 const struct netcons_userdata *userdata)
2243 {
2244 const char *sysdata = NULL;
2245 const char *release;
2246
2247 #ifdef CONFIG_NETCONSOLE_DYNAMIC
2248 sysdata = nt->sysdata;
2249 #endif
2250
2251 if (release_len) {
2252 release = init_utsname()->release;
2253
2254 scnprintf(nt->buf, sizeof(nt->buf), "%s,%.*s", release,
2255 msg_len, msg);
2256 msg_len += release_len;
2257 } else {
2258 memcpy(nt->buf, msg, msg_len);
2259 }
2260
2261 if (userdata)
2262 msg_len += scnprintf(&nt->buf[msg_len],
2263 sizeof(nt->buf) - msg_len, "%s",
2264 userdata->data);
2265
2266 if (sysdata)
2267 msg_len += scnprintf(&nt->buf[msg_len],
2268 sizeof(nt->buf) - msg_len, "%s",
2269 sysdata);
2270
2271 send_udp(nt, nt->buf, msg_len);
2272 }
2273
append_release(char * buf)2274 static void append_release(char *buf)
2275 {
2276 const char *release;
2277
2278 release = init_utsname()->release;
2279 scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
2280 }
2281
send_fragmented_body(struct netconsole_target * nt,const char * msgbody_ptr,int header_len,int msgbody_len,int sysdata_len,const struct netcons_userdata * userdata)2282 static void send_fragmented_body(struct netconsole_target *nt,
2283 const char *msgbody_ptr, int header_len,
2284 int msgbody_len, int sysdata_len,
2285 const struct netcons_userdata *userdata)
2286 {
2287 const char *userdata_ptr = NULL;
2288 const char *sysdata_ptr = NULL;
2289 int data_len, data_sent = 0;
2290 int userdata_offset = 0;
2291 int sysdata_offset = 0;
2292 int msgbody_offset = 0;
2293 int userdata_len = 0;
2294
2295 #ifdef CONFIG_NETCONSOLE_DYNAMIC
2296 sysdata_ptr = nt->sysdata;
2297 #endif
2298 if (userdata) {
2299 userdata_ptr = userdata->data;
2300 userdata_len = userdata->length;
2301 }
2302
2303 if (WARN_ON_ONCE(!sysdata_ptr && sysdata_len != 0))
2304 return;
2305
2306 /* data_len represents the number of bytes that will be sent. This is
2307 * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple
2308 * packets
2309 */
2310 data_len = msgbody_len + userdata_len + sysdata_len;
2311
2312 /* In each iteration of the while loop below, we send a packet
2313 * containing the header and a portion of the data. The data is
2314 * composed of three parts: msgbody, userdata, and sysdata.
2315 * We keep track of how many bytes have been sent from each part using
2316 * the *_offset variables.
2317 * We keep track of how many bytes have been sent overall using the
2318 * data_sent variable, which ranges from 0 to the total bytes to be
2319 * sent.
2320 */
2321 while (data_sent < data_len) {
2322 int userdata_left = userdata_len - userdata_offset;
2323 int sysdata_left = sysdata_len - sysdata_offset;
2324 int msgbody_left = msgbody_len - msgbody_offset;
2325 int buf_offset = 0;
2326 int this_chunk = 0;
2327
2328 /* header is already populated in nt->buf, just append to it */
2329 buf_offset = header_len;
2330
2331 buf_offset += scnprintf(nt->buf + buf_offset,
2332 MAX_PRINT_CHUNK - buf_offset,
2333 ",ncfrag=%d/%d;", data_sent,
2334 data_len);
2335
2336 /* append msgbody first */
2337 this_chunk = min(msgbody_left, MAX_PRINT_CHUNK - buf_offset);
2338 memcpy(nt->buf + buf_offset, msgbody_ptr + msgbody_offset,
2339 this_chunk);
2340 msgbody_offset += this_chunk;
2341 buf_offset += this_chunk;
2342 data_sent += this_chunk;
2343
2344 /* after msgbody, append userdata */
2345 if (userdata_ptr && userdata_left) {
2346 this_chunk = min(userdata_left,
2347 MAX_PRINT_CHUNK - buf_offset);
2348 memcpy(nt->buf + buf_offset,
2349 userdata_ptr + userdata_offset, this_chunk);
2350 userdata_offset += this_chunk;
2351 buf_offset += this_chunk;
2352 data_sent += this_chunk;
2353 }
2354
2355 /* after userdata, append sysdata */
2356 if (sysdata_ptr && sysdata_left) {
2357 this_chunk = min(sysdata_left,
2358 MAX_PRINT_CHUNK - buf_offset);
2359 memcpy(nt->buf + buf_offset,
2360 sysdata_ptr + sysdata_offset, this_chunk);
2361 sysdata_offset += this_chunk;
2362 buf_offset += this_chunk;
2363 data_sent += this_chunk;
2364 }
2365
2366 /* if all is good, send the packet out */
2367 if (WARN_ON_ONCE(data_sent > data_len))
2368 return;
2369
2370 send_udp(nt, nt->buf, buf_offset);
2371 }
2372 }
2373
send_msg_fragmented(struct netconsole_target * nt,const char * msg,int msg_len,int release_len,int sysdata_len,const struct netcons_userdata * userdata)2374 static void send_msg_fragmented(struct netconsole_target *nt,
2375 const char *msg,
2376 int msg_len,
2377 int release_len,
2378 int sysdata_len,
2379 const struct netcons_userdata *userdata)
2380 {
2381 int header_len, msgbody_len;
2382 const char *msgbody;
2383
2384 /* need to insert extra header fields, detect header and msgbody */
2385 msgbody = memchr(msg, ';', msg_len);
2386 if (WARN_ON_ONCE(!msgbody))
2387 return;
2388
2389 header_len = msgbody - msg;
2390 msgbody_len = msg_len - header_len - 1;
2391 msgbody++;
2392
2393 /*
2394 * Transfer multiple chunks with the following extra header.
2395 * "ncfrag=<byte-offset>/<total-bytes>"
2396 */
2397 if (release_len)
2398 append_release(nt->buf);
2399
2400 /* Copy the header into the buffer */
2401 memcpy(nt->buf + release_len, msg, header_len);
2402 header_len += release_len;
2403
2404 /* for now on, the header will be persisted, and the msgbody
2405 * will be replaced
2406 */
2407 send_fragmented_body(nt, msgbody, header_len, msgbody_len,
2408 sysdata_len, userdata);
2409 }
2410
2411 /**
2412 * send_ext_msg_udp - send extended log message to target
2413 * @nt: target to send message to
2414 * @wctxt: nbcon write context containing message and metadata
2415 *
2416 * Transfer extended log message to @nt. If message is longer than
2417 * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
2418 * ncfrag header field added to identify them.
2419 */
send_ext_msg_udp(struct netconsole_target * nt,struct nbcon_write_context * wctxt)2420 static void send_ext_msg_udp(struct netconsole_target *nt,
2421 struct nbcon_write_context *wctxt)
2422 {
2423 const struct netcons_userdata *userdata = NULL;
2424 int userdata_len = 0;
2425 int release_len = 0;
2426 int sysdata_len = 0;
2427 int len;
2428
2429 /* Keeps the payload picked below alive until the last send_udp(). */
2430 rcu_read_lock();
2431
2432 #ifdef CONFIG_NETCONSOLE_DYNAMIC
2433 sysdata_len = prepare_sysdata(nt, wctxt);
2434 userdata = rcu_dereference(nt->userdata);
2435 if (userdata)
2436 userdata_len = userdata->length;
2437 #endif
2438 if (nt->release)
2439 release_len = strlen(init_utsname()->release) + 1;
2440
2441 len = wctxt->len + release_len + sysdata_len + userdata_len;
2442 if (len <= MAX_PRINT_CHUNK)
2443 send_msg_no_fragmentation(nt, wctxt->outbuf, wctxt->len,
2444 release_len, userdata);
2445 else
2446 send_msg_fragmented(nt, wctxt->outbuf, wctxt->len, release_len,
2447 sysdata_len, userdata);
2448
2449 rcu_read_unlock();
2450 }
2451
send_msg_udp(struct netconsole_target * nt,const char * msg,unsigned int len)2452 static void send_msg_udp(struct netconsole_target *nt, const char *msg,
2453 unsigned int len)
2454 {
2455 const char *tmp = msg;
2456 int frag, left = len;
2457
2458 while (left > 0) {
2459 frag = min(left, MAX_PRINT_CHUNK);
2460 send_udp(nt, tmp, frag);
2461 tmp += frag;
2462 left -= frag;
2463 }
2464 }
2465
2466 /**
2467 * netconsole_write - Generic function to send a msg to all targets
2468 * @wctxt: nbcon write context
2469 * @extended: "true" for extended console mode
2470 *
2471 * Given an nbcon write context, send the message to the netconsole targets
2472 */
netconsole_write(struct nbcon_write_context * wctxt,bool extended)2473 static void netconsole_write(struct nbcon_write_context *wctxt, bool extended)
2474 {
2475 struct netconsole_target *nt;
2476
2477 if (oops_only && !oops_in_progress)
2478 return;
2479
2480 list_for_each_entry(nt, &target_list, list) {
2481 if (nt->extended != extended || nt->state != STATE_ENABLED ||
2482 !netif_running(nt->np.dev))
2483 continue;
2484
2485 /* If nbcon_enter_unsafe() fails, just return given netconsole
2486 * lost the ownership, and iterating over the targets will not
2487 * be able to re-acquire.
2488 */
2489 if (!nbcon_enter_unsafe(wctxt))
2490 return;
2491
2492 if (extended)
2493 send_ext_msg_udp(nt, wctxt);
2494 else
2495 send_msg_udp(nt, wctxt->outbuf, wctxt->len);
2496
2497 nbcon_exit_unsafe(wctxt);
2498 }
2499 }
2500
netconsole_write_ext(struct console * con __always_unused,struct nbcon_write_context * wctxt)2501 static void netconsole_write_ext(struct console *con __always_unused,
2502 struct nbcon_write_context *wctxt)
2503 {
2504 netconsole_write(wctxt, true);
2505 }
2506
netconsole_write_basic(struct console * con __always_unused,struct nbcon_write_context * wctxt)2507 static void netconsole_write_basic(struct console *con __always_unused,
2508 struct nbcon_write_context *wctxt)
2509 {
2510 netconsole_write(wctxt, false);
2511 }
2512
netconsole_device_lock(struct console * con __always_unused,unsigned long * flags)2513 static void netconsole_device_lock(struct console *con __always_unused,
2514 unsigned long *flags)
2515 __acquires(&target_list_lock)
2516 {
2517 spin_lock_irqsave(&target_list_lock, *flags);
2518 }
2519
netconsole_device_unlock(struct console * con __always_unused,unsigned long flags)2520 static void netconsole_device_unlock(struct console *con __always_unused,
2521 unsigned long flags)
2522 __releases(&target_list_lock)
2523 {
2524 spin_unlock_irqrestore(&target_list_lock, flags);
2525 }
2526
netconsole_parser_cmdline(struct netconsole_target * nt,char * opt)2527 static int netconsole_parser_cmdline(struct netconsole_target *nt, char *opt)
2528 {
2529 struct netpoll *np = &nt->np;
2530 bool ipversion_set = false;
2531 char *cur = opt;
2532 char *delim;
2533 int ipv6;
2534
2535 if (*cur != '@') {
2536 delim = strchr(cur, '@');
2537 if (!delim)
2538 goto parse_failed;
2539 *delim = 0;
2540 if (kstrtou16(cur, 10, &nt->local_port))
2541 goto parse_failed;
2542 cur = delim;
2543 }
2544 cur++;
2545
2546 if (*cur != '/') {
2547 ipversion_set = true;
2548 delim = strchr(cur, '/');
2549 if (!delim)
2550 goto parse_failed;
2551 *delim = 0;
2552 ipv6 = netpoll_parse_ip_addr(cur, &nt->local_ip);
2553 if (ipv6 < 0)
2554 goto parse_failed;
2555 else
2556 nt->ipv6 = (bool)ipv6;
2557 cur = delim;
2558 }
2559 cur++;
2560
2561 if (*cur != ',') {
2562 /* parse out dev_name or dev_mac */
2563 delim = strchr(cur, ',');
2564 if (!delim)
2565 goto parse_failed;
2566 *delim = 0;
2567
2568 np->dev_name[0] = '\0';
2569 eth_broadcast_addr(np->dev_mac);
2570 if (!strchr(cur, ':'))
2571 strscpy(np->dev_name, cur, sizeof(np->dev_name));
2572 else if (!mac_pton(cur, np->dev_mac))
2573 goto parse_failed;
2574
2575 cur = delim;
2576 }
2577 cur++;
2578
2579 if (*cur != '@') {
2580 /* dst port */
2581 delim = strchr(cur, '@');
2582 if (!delim)
2583 goto parse_failed;
2584 *delim = 0;
2585 if (*cur == ' ' || *cur == '\t')
2586 np_info(np, "warning: whitespace is not allowed\n");
2587 if (kstrtou16(cur, 10, &nt->remote_port))
2588 goto parse_failed;
2589 cur = delim;
2590 }
2591 cur++;
2592
2593 /* dst ip */
2594 delim = strchr(cur, '/');
2595 if (!delim)
2596 goto parse_failed;
2597 *delim = 0;
2598 ipv6 = netpoll_parse_ip_addr(cur, &nt->remote_ip);
2599 if (ipv6 < 0)
2600 goto parse_failed;
2601 else if (ipversion_set && nt->ipv6 != (bool)ipv6)
2602 goto parse_failed;
2603 else
2604 nt->ipv6 = (bool)ipv6;
2605 cur = delim + 1;
2606
2607 if (*cur != 0) {
2608 /* MAC address */
2609 if (!mac_pton(cur, nt->remote_mac))
2610 goto parse_failed;
2611 }
2612
2613 netconsole_print_banner(nt);
2614
2615 return 0;
2616
2617 parse_failed:
2618 np_info(np, "couldn't parse config at '%s'!\n", cur);
2619 return -1;
2620 }
2621
2622 /* Allocate new target (from boot/module param) and setup netpoll for it */
alloc_param_target(char * target_config,int cmdline_count)2623 static struct netconsole_target *alloc_param_target(char *target_config,
2624 int cmdline_count)
2625 {
2626 struct netconsole_target *nt;
2627 int err;
2628
2629 nt = alloc_and_init();
2630 if (!nt) {
2631 err = -ENOMEM;
2632 goto fail;
2633 }
2634
2635 if (*target_config == '+') {
2636 nt->extended = true;
2637 target_config++;
2638 }
2639
2640 if (*target_config == 'r') {
2641 if (!nt->extended) {
2642 pr_err("Netconsole configuration error. Release feature requires extended log message");
2643 err = -EINVAL;
2644 goto fail;
2645 }
2646 nt->release = true;
2647 target_config++;
2648 }
2649
2650 /* Parse parameters and setup netpoll */
2651 err = netconsole_parser_cmdline(nt, target_config);
2652 if (err)
2653 goto fail;
2654
2655 /* Initialise the skb pool before netpoll_setup() so the pool is
2656 * valid as soon as nt->np.dev becomes visible. The target is not
2657 * yet on target_list, so a netdev event cannot reach it here, but
2658 * mirror the configfs path for symmetry.
2659 */
2660 netconsole_skb_pool_init(nt);
2661
2662 err = netcons_netpoll_setup(nt);
2663 if (err) {
2664 pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n",
2665 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
2666 netconsole_skb_pool_flush(nt);
2667 if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
2668 /* only fail if dynamic reconfiguration is set,
2669 * otherwise, keep the target in the list, but disabled.
2670 */
2671 goto fail;
2672 } else {
2673 nt->state = STATE_ENABLED;
2674 }
2675 populate_configfs_item(nt, cmdline_count);
2676
2677 return nt;
2678
2679 fail:
2680 kfree(nt);
2681 return ERR_PTR(err);
2682 }
2683
2684 /* Cleanup netpoll for given target (from boot/module param) and free it */
free_param_target(struct netconsole_target * nt)2685 static void free_param_target(struct netconsole_target *nt)
2686 {
2687 cancel_work_sync(&nt->resume_wq);
2688 if (nt->state == STATE_ENABLED)
2689 netconsole_skb_pool_flush(nt);
2690 netpoll_cleanup(&nt->np);
2691 #ifdef CONFIG_NETCONSOLE_DYNAMIC
2692 kfree(rcu_access_pointer(nt->userdata));
2693 #endif
2694 kfree(nt);
2695 }
2696
2697 static struct console netconsole_ext = {
2698 .name = "netcon_ext",
2699 .flags = CON_ENABLED | CON_EXTENDED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE,
2700 .write_thread = netconsole_write_ext,
2701 .write_atomic = netconsole_write_ext,
2702 .device_lock = netconsole_device_lock,
2703 .device_unlock = netconsole_device_unlock,
2704 };
2705
2706 static struct console netconsole = {
2707 .name = "netcon",
2708 .flags = CON_ENABLED | CON_NBCON | CON_NBCON_ATOMIC_UNSAFE,
2709 .write_thread = netconsole_write_basic,
2710 .write_atomic = netconsole_write_basic,
2711 .device_lock = netconsole_device_lock,
2712 .device_unlock = netconsole_device_unlock,
2713 };
2714
init_netconsole(void)2715 static int __init init_netconsole(void)
2716 {
2717 int err;
2718 struct netconsole_target *nt, *tmp;
2719 u32 console_type_needed = 0;
2720 unsigned int count = 0;
2721 unsigned long flags;
2722 char *target_config;
2723 char *input = config;
2724
2725 if (strnlen(input, MAX_PARAM_LENGTH)) {
2726 while ((target_config = strsep(&input, ";"))) {
2727 nt = alloc_param_target(target_config, count);
2728 if (IS_ERR(nt)) {
2729 if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
2730 continue;
2731 err = PTR_ERR(nt);
2732 goto fail;
2733 }
2734 /* Dump existing printks when we register */
2735 if (nt->extended) {
2736 console_type_needed |= CONS_EXTENDED;
2737 netconsole_ext.flags |= CON_PRINTBUFFER;
2738 } else {
2739 console_type_needed |= CONS_BASIC;
2740 netconsole.flags |= CON_PRINTBUFFER;
2741 }
2742
2743 spin_lock_irqsave(&target_list_lock, flags);
2744 list_add(&nt->list, &target_list);
2745 spin_unlock_irqrestore(&target_list_lock, flags);
2746 count++;
2747 }
2748 }
2749
2750 netconsole_wq = alloc_workqueue("netconsole", WQ_UNBOUND, 0);
2751 if (!netconsole_wq) {
2752 err = -ENOMEM;
2753 goto fail;
2754 }
2755
2756 err = register_netdevice_notifier(&netconsole_netdev_notifier);
2757 if (err)
2758 goto fail;
2759
2760 err = dynamic_netconsole_init();
2761 if (err)
2762 goto undonotifier;
2763
2764 if (console_type_needed & CONS_EXTENDED)
2765 register_console(&netconsole_ext);
2766 if (console_type_needed & CONS_BASIC)
2767 register_console(&netconsole);
2768 pr_info("network logging started\n");
2769
2770 return err;
2771
2772 undonotifier:
2773 unregister_netdevice_notifier(&netconsole_netdev_notifier);
2774
2775 fail:
2776 pr_err("cleaning up\n");
2777
2778 if (netconsole_wq)
2779 flush_workqueue(netconsole_wq);
2780 /*
2781 * Remove all targets and destroy them (only targets created
2782 * from the boot/module option exist here). Skipping the list
2783 * lock is safe here, and netpoll_cleanup() will sleep.
2784 */
2785 list_for_each_entry_safe(nt, tmp, &target_list, list) {
2786 list_del(&nt->list);
2787 free_param_target(nt);
2788 }
2789
2790 if (netconsole_wq)
2791 destroy_workqueue(netconsole_wq);
2792
2793 return err;
2794 }
2795
cleanup_netconsole(void)2796 static void __exit cleanup_netconsole(void)
2797 {
2798 struct netconsole_target *nt, *tmp;
2799
2800 if (console_is_registered(&netconsole_ext))
2801 unregister_console(&netconsole_ext);
2802 if (console_is_registered(&netconsole))
2803 unregister_console(&netconsole);
2804 dynamic_netconsole_exit();
2805 unregister_netdevice_notifier(&netconsole_netdev_notifier);
2806 flush_workqueue(netconsole_wq);
2807
2808 /*
2809 * Targets created via configfs pin references on our module
2810 * and would first be rmdir(2)'ed from userspace. We reach
2811 * here only when they are already destroyed, and only those
2812 * created from the boot/module option are left, so remove and
2813 * destroy them. Skipping the list lock is safe here, and
2814 * netpoll_cleanup() will sleep.
2815 */
2816 list_for_each_entry_safe(nt, tmp, &target_list, list) {
2817 list_del(&nt->list);
2818 free_param_target(nt);
2819 }
2820
2821 destroy_workqueue(netconsole_wq);
2822 }
2823
2824 /*
2825 * Use late_initcall to ensure netconsole is
2826 * initialized after network device driver if built-in.
2827 *
2828 * late_initcall() and module_init() are identical if built as module.
2829 */
2830 late_initcall(init_netconsole);
2831 module_exit(cleanup_netconsole);
2832