Repository navigation
Expand file tree
/
Copy pathnetconsole.c
More file actions
2829 lines (2409 loc) · 74.2 KB
/
Copy pathnetconsole.c
File metadata and controls
2829 lines (2409 loc) · 74.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* linux/drivers/net/netconsole.c
*
* Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
*
* This file contains the implementation of an IRQ-safe, crash-safe
* kernel console implementation that outputs kernel messages to the
* network.
*
* Modification history:
*
* 2001-09-17 started by Ingo Molnar.
* 2003-08-11 2.6 port by Matt Mackall
* simplified options
* generic card hooks
* works non-modular
* 2003-09-07 rewritten with netpoll api
*/
/****************************************************************
*
****************************************************************/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/console.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/udp.h>
#include <linux/netpoll.h>
#include <linux/inet.h>
#include <linux/inetdevice.h>
#include <linux/unaligned.h>
#include <net/ip6_checksum.h>
#include <net/addrconf.h>
#include <linux/configfs.h>
#include <linux/etherdevice.h>
#include <linux/hex.h>
#include <linux/u64_stats_sync.h>
#include <linux/utsname.h>
#include <linux/rtnetlink.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
MODULE_AUTHOR("Matt Mackall <mpm@selenic.com>");
MODULE_DESCRIPTION("Console driver for network interfaces");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("NETDEV_INTERNAL");
#define MAX_PARAM_LENGTH 256
#define MAX_EXTRADATA_ENTRY_LEN 256
#define MAX_EXTRADATA_VALUE_LEN 200
/* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
#define MAX_EXTRADATA_NAME_LEN (MAX_EXTRADATA_ENTRY_LEN - \
MAX_EXTRADATA_VALUE_LEN - 3)
#define MAX_USERDATA_ITEMS 256
#define MAX_PRINT_CHUNK 1000
/*
* Sizing for the per-target fallback skb pool consulted by find_skb()
* when its GFP_ATOMIC allocation fails so messages still get out under
* memory pressure.
*/
#define MAX_UDP_CHUNK 1460
#define MAX_SKBS 32
#define MAX_SKB_SIZE \
(sizeof(struct ethhdr) + \
sizeof(struct iphdr) + \
sizeof(struct udphdr) + \
MAX_UDP_CHUNK)
static char config[MAX_PARAM_LENGTH];
module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
static bool oops_only;
module_param(oops_only, bool, 0600);
MODULE_PARM_DESC(oops_only, "Only log oops messages");
#define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
#ifndef MODULE
static int __init option_setup(char *opt)
{
strscpy(config, opt, MAX_PARAM_LENGTH);
return 1;
}
__setup("netconsole=", option_setup);
#endif /* MODULE */
/* Linked list of all configured targets */
static LIST_HEAD(target_list);
/* target_cleanup_list is used to track targets that need to be cleaned outside
* of target_list_lock. It should be cleaned in the same function it is
* populated.
*/
static LIST_HEAD(target_cleanup_list);
/* This needs to be a spinlock because write_msg() cannot sleep */
static DEFINE_SPINLOCK(target_list_lock);
/* This needs to be a mutex because netpoll_cleanup might sleep */
static DEFINE_MUTEX(target_cleanup_list_lock);
static struct workqueue_struct *netconsole_wq;
/*
* Console driver for netconsoles. Register only consoles that have
* an associated target of the same type.
*/
static struct console netconsole_ext, netconsole;
struct netconsole_target_stats {
u64_stats_t xmit_drop_count;
u64_stats_t enomem_count;
struct u64_stats_sync syncp;
};
enum console_type {
CONS_BASIC = BIT(0),
CONS_EXTENDED = BIT(1),
};
/* Features enabled in sysdata. Contrary to userdata, this data is populated by
* the kernel. The fields are designed as bitwise flags, allowing multiple
* features to be set in sysdata_fields.
*/
enum sysdata_feature {
/* Populate the CPU that sends the message */
SYSDATA_CPU_NR = BIT(0),
/* Populate the task name (as in current->comm) in sysdata */
SYSDATA_TASKNAME = BIT(1),
/* Kernel release/version as part of sysdata */
SYSDATA_RELEASE = BIT(2),
/* Include a per-target message ID as part of sysdata */
SYSDATA_MSGID = BIT(3),
/* Sentinel: highest bit position */
MAX_SYSDATA_ITEMS = 4,
};
enum target_state {
STATE_DISABLED,
STATE_ENABLED,
STATE_DEACTIVATED,
};
struct inet_addr {
/* Address family: AF_UNSPEC when unset, else AF_INET or AF_INET6 */
u8 family;
union {
__be32 ip;
struct in6_addr in6;
};
};
/**
* struct netcons_userdata - Formatted userdata payload of a target.
* @rcu: Used to free the payload after a grace period.
* @length: Length of @data, excluding the NUL terminator.
* @data: Formatted " key=value\n" entries, NUL terminated.
*
* Immutable once published, so the transmit path never observes @data and
* @length disagreeing.
*/
struct netcons_userdata {
struct rcu_head rcu;
size_t length;
char data[];
};
/**
* struct netconsole_target - Represents a configured netconsole target.
* @list: Links this target into the target_list.
* @group: Links us into the configfs subsystem hierarchy.
* @userdata_group: Links to the userdata configfs hierarchy
* @userdata: Cached, formatted userdata payload. RCU protected.
* @sysdata: Cached, formatted string of append
* @sysdata_fields: Sysdata features enabled.
* @msgcounter: Message sent counter.
* @stats: Packet send stats for the target. Used for debugging.
* @state: State of the target.
* Visible from userspace (read-write).
* From a userspace perspective, the target is either enabled or
* disabled. Internally, although both STATE_DISABLED and
* STATE_DEACTIVATED correspond to inactive targets, the latter is
* due to automatic interface state changes and will try
* recover automatically, if the interface comes back
* online.
* Also, other parameters of a target may be modified at
* runtime only when it is disabled (state != STATE_ENABLED).
* @extended: Denotes whether console is extended or not.
* @release: Denotes whether kernel release version should be prepended
* to the message. Depends on extended console.
* @np: The netpoll structure for this target.
* Contains the other userspace visible parameters:
* dev_name (read-write)
* local_mac (read-only)
* @local_ip: Source IP address of the target (read-write).
* @remote_ip: Destination IP address of the target (read-write).
* @local_port: Source UDP port of the target (read-write).
* @remote_port: Destination UDP port of the target (read-write).
* @remote_mac: Destination ethernet address of the target (read-write).
* @buf: The buffer used to send the full msg to the network stack
* @resume_wq: Workqueue to resume deactivated target
* @skb_pool: Per-target fallback skb pool consulted by find_skb() when
* its GFP_ATOMIC allocation fails. Lifetime brackets a
* successful netpoll_setup() / netpoll_cleanup() pair on @np.
* @refill_wq: Work item that asynchronously tops @skb_pool back up to
* MAX_SKBS after find_skb() drains an entry.
*/
struct netconsole_target {
struct list_head list;
#ifdef CONFIG_NETCONSOLE_DYNAMIC
struct config_group group;
struct config_group userdata_group;
struct netcons_userdata __rcu *userdata;
char sysdata[MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS];
/* bit-wise with sysdata_feature bits */
u32 sysdata_fields;
/* protected by target_list_lock */
u32 msgcounter;
#endif
struct netconsole_target_stats stats;
enum target_state state;
bool extended;
bool release;
struct netpoll np;
struct inet_addr local_ip, remote_ip;
u16 local_port, remote_port;
u8 remote_mac[ETH_ALEN];
/* protected by target_list_lock; +1 gives scnprintf() room for its
* NUL terminator so a full MAX_PRINT_CHUNK payload is not truncated
*/
char buf[MAX_PRINT_CHUNK + 1];
struct work_struct resume_wq;
struct sk_buff_head skb_pool;
struct work_struct refill_wq;
};
#ifdef CONFIG_NETCONSOLE_DYNAMIC
static struct configfs_subsystem netconsole_subsys;
static DEFINE_MUTEX(dynamic_netconsole_mutex);
static int __init dynamic_netconsole_init(void)
{
config_group_init(&netconsole_subsys.su_group);
mutex_init(&netconsole_subsys.su_mutex);
return configfs_register_subsystem(&netconsole_subsys);
}
static void __exit dynamic_netconsole_exit(void)
{
configfs_unregister_subsystem(&netconsole_subsys);
}
/*
* Targets that were created by parsing the boot/module option string
* do not exist in the configfs hierarchy (and have NULL names) and will
* never go away, so make these a no-op for them.
*/
static void netconsole_target_get(struct netconsole_target *nt)
{
if (config_item_name(&nt->group.cg_item))
config_group_get(&nt->group);
}
static void netconsole_target_put(struct netconsole_target *nt)
{
if (config_item_name(&nt->group.cg_item))
config_group_put(&nt->group);
}
static void dynamic_netconsole_mutex_lock(void)
{
mutex_lock(&dynamic_netconsole_mutex);
}
static void dynamic_netconsole_mutex_unlock(void)
{
mutex_unlock(&dynamic_netconsole_mutex);
}
#else /* !CONFIG_NETCONSOLE_DYNAMIC */
static int __init dynamic_netconsole_init(void)
{
return 0;
}
static void __exit dynamic_netconsole_exit(void)
{
}
/*
* No danger of targets going away from under us when dynamic
* reconfigurability is off.
*/
static void netconsole_target_get(struct netconsole_target *nt)
{
}
static void netconsole_target_put(struct netconsole_target *nt)
{
}
static void populate_configfs_item(struct netconsole_target *nt,
int cmdline_count)
{
}
static void dynamic_netconsole_mutex_lock(void)
{
}
static void dynamic_netconsole_mutex_unlock(void)
{
}
#endif /* CONFIG_NETCONSOLE_DYNAMIC */
/* Check if the target was bound by mac address. */
static bool bound_by_mac(struct netconsole_target *nt)
{
return is_valid_ether_addr(nt->np.dev_mac);
}
static void netcons_release_dev(struct netconsole_target *nt)
{
do_netpoll_cleanup(&nt->np);
if (bound_by_mac(nt))
memset(&nt->np.dev_name, 0, IFNAMSIZ);
}
static void refill_skbs(struct netconsole_target *nt)
{
struct sk_buff_head *skb_pool = &nt->skb_pool;
struct sk_buff *skb;
while (READ_ONCE(skb_pool->qlen) < MAX_SKBS) {
skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC | __GFP_NOWARN);
if (!skb)
break;
skb_queue_tail(skb_pool, skb);
}
}
static void refill_skbs_work_handler(struct work_struct *work)
{
struct netconsole_target *nt =
container_of(work, struct netconsole_target, refill_wq);
refill_skbs(nt);
}
/* Seed the per-target skb pool that find_skb() falls back to. The queue
* head and refill work are set up once in alloc_and_init(); this only
* (re)fills the pool. Pair with netconsole_skb_pool_flush().
*/
static void netconsole_skb_pool_init(struct netconsole_target *nt)
{
refill_skbs(nt);
}
static void netconsole_skb_pool_flush(struct netconsole_target *nt)
{
cancel_work_sync(&nt->refill_wq);
skb_queue_purge_reason(&nt->skb_pool, SKB_CONSUMED);
}
static void netcons_wait_carrier(struct netpoll *np, struct net_device *ndev)
{
unsigned long atmost;
atmost = jiffies + netpoll_get_carrier_timeout() * HZ;
while (!netif_carrier_ok(ndev)) {
if (time_after(jiffies, atmost)) {
np_notice(np, "timeout waiting for carrier\n");
break;
}
msleep(1);
}
}
/*
* Returns a pointer to a string representation of the identifier used
* to select the egress interface for the given netpoll instance. buf
* is used to format np->dev_mac when np->dev_name is empty; bufsz must
* be at least MAC_ADDR_STR_LEN + 1 to fit the formatted MAC address
* and its NUL terminator.
*/
static char *netcons_egress_dev(struct netpoll *np, char *buf, size_t bufsz)
{
if (np->dev_name[0])
return np->dev_name;
snprintf(buf, bufsz, "%pM", np->dev_mac);
return buf;
}
/*
* Populate the target's local_ip with the IPv6 address from ndev.
*/
static int netcons_take_ipv6(struct netconsole_target *nt,
struct net_device *ndev)
{
char buf[MAC_ADDR_STR_LEN + 1];
struct netpoll *np = &nt->np;
int err = -EDESTADDRREQ;
struct inet6_dev *idev;
if (!IS_ENABLED(CONFIG_IPV6)) {
np_err(np, "IPv6 is not supported %s, aborting\n",
netcons_egress_dev(np, buf, sizeof(buf)));
return -EINVAL;
}
idev = __in6_dev_get(ndev);
if (idev) {
struct inet6_ifaddr *ifp;
read_lock_bh(&idev->lock);
list_for_each_entry(ifp, &idev->addr_list, if_list) {
if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
!!(ipv6_addr_type(&nt->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
continue;
/* Got the IP, let's return */
nt->local_ip.in6 = ifp->addr;
nt->local_ip.family = AF_INET6;
err = 0;
break;
}
read_unlock_bh(&idev->lock);
}
if (err) {
np_err(np, "no IPv6 address for %s, aborting\n",
netcons_egress_dev(np, buf, sizeof(buf)));
return err;
}
np_info(np, "local IPv6 %pI6c\n", &nt->local_ip.in6);
return 0;
}
/*
* Populate the target's local_ip with the IPv4 address from ndev.
*/
static int netcons_take_ipv4(struct netconsole_target *nt,
struct net_device *ndev)
{
char buf[MAC_ADDR_STR_LEN + 1];
struct netpoll *np = &nt->np;
const struct in_ifaddr *ifa;
struct in_device *in_dev;
in_dev = __in_dev_get_rtnl(ndev);
if (!in_dev) {
np_err(np, "no IP address for %s, aborting\n",
netcons_egress_dev(np, buf, sizeof(buf)));
return -EDESTADDRREQ;
}
ifa = rtnl_dereference(in_dev->ifa_list);
if (!ifa) {
np_err(np, "no IP address for %s, aborting\n",
netcons_egress_dev(np, buf, sizeof(buf)));
return -EDESTADDRREQ;
}
nt->local_ip.ip = ifa->ifa_local;
nt->local_ip.family = AF_INET;
np_info(np, "local IP %pI4\n", &nt->local_ip.ip);
return 0;
}
static int netcons_netpoll_setup(struct netconsole_target *nt)
{
struct net *net = current->nsproxy->net_ns;
char buf[MAC_ADDR_STR_LEN + 1];
struct net_device *ndev = NULL;
struct netpoll *np = &nt->np;
bool ip_overwritten = false;
int err;
if (nt->remote_ip.family == AF_UNSPEC) {
np_err(np, "remote IP address not configured, aborting\n");
return -EDESTADDRREQ;
}
if (nt->local_ip.family != AF_UNSPEC &&
nt->local_ip.family != nt->remote_ip.family) {
np_err(np, "local and remote IP address families differ, aborting\n");
return -EINVAL;
}
rtnl_lock();
if (np->dev_name[0])
ndev = __dev_get_by_name(net, np->dev_name);
else if (is_valid_ether_addr(np->dev_mac))
ndev = dev_getbyhwaddr(net, ARPHRD_ETHER, np->dev_mac);
if (!ndev) {
np_err(np, "%s doesn't exist, aborting\n",
netcons_egress_dev(np, buf, sizeof(buf)));
err = -ENODEV;
goto unlock;
}
netdev_hold(ndev, &np->dev_tracker, GFP_KERNEL);
if (netdev_master_upper_dev_get(ndev)) {
np_err(np, "%s is a slave device, aborting\n",
netcons_egress_dev(np, buf, sizeof(buf)));
err = -EBUSY;
goto put;
}
if (!netif_running(ndev)) {
np_info(np, "device %s not up yet, forcing it\n",
netcons_egress_dev(np, buf, sizeof(buf)));
err = dev_open(ndev, NULL);
if (err) {
np_err(np, "failed to open %s\n", ndev->name);
goto put;
}
rtnl_unlock();
netcons_wait_carrier(np, ndev);
rtnl_lock();
}
if (nt->local_ip.family == AF_UNSPEC) {
if (nt->remote_ip.family == AF_INET6)
err = netcons_take_ipv6(nt, ndev);
else
err = netcons_take_ipv4(nt, ndev);
if (err)
goto put;
ip_overwritten = true;
}
err = __netpoll_setup(np, ndev);
if (err)
goto put;
rtnl_unlock();
/* Make sure all NAPI polls which started before dev->npinfo
* was visible have exited before we start calling NAPI poll.
* NAPI skips locking if dev->npinfo is NULL.
*/
synchronize_rcu();
return 0;
put:
DEBUG_NET_WARN_ON_ONCE(np->dev);
if (ip_overwritten)
memset(&nt->local_ip, 0, sizeof(nt->local_ip));
netdev_put(ndev, &np->dev_tracker);
unlock:
rtnl_unlock();
return err;
}
/* Attempts to resume logging to a deactivated target. */
static void resume_target(struct netconsole_target *nt)
{
/* Initialise the skb pool before netpoll_setup() makes nt->np.dev
* visible to target_list walkers (e.g. netconsole_netdev_event),
* which otherwise may move the target to the cleanup list and
* call netconsole_skb_pool_flush() on uninitialised state.
*/
netconsole_skb_pool_init(nt);
if (netcons_netpoll_setup(nt)) {
/* netpoll fails setup once, do not try again. */
netconsole_skb_pool_flush(nt);
nt->state = STATE_DISABLED;
return;
}
nt->state = STATE_ENABLED;
pr_info("network logging resumed on interface %s\n", nt->np.dev_name);
}
/* Checks if a deactivated target matches a device. */
static bool deactivated_target_match(struct netconsole_target *nt,
struct net_device *ndev)
{
if (nt->state != STATE_DEACTIVATED)
return false;
if (bound_by_mac(nt))
return !memcmp(nt->np.dev_mac, ndev->dev_addr, ETH_ALEN);
return !strncmp(nt->np.dev_name, ndev->name, IFNAMSIZ);
}
/* Process work scheduled for target resume. */
static void process_resume_target(struct work_struct *work)
{
struct netconsole_target *nt;
unsigned long flags;
nt = container_of(work, struct netconsole_target, resume_wq);
dynamic_netconsole_mutex_lock();
spin_lock_irqsave(&target_list_lock, flags);
/* Check if target is still deactivated as it may have been disabled
* while resume was being scheduled.
*/
if (nt->state != STATE_DEACTIVATED) {
spin_unlock_irqrestore(&target_list_lock, flags);
goto out_unlock;
}
/* resume_target is IRQ unsafe, remove target from
* target_list in order to resume it with IRQ enabled.
*/
list_del_init(&nt->list);
spin_unlock_irqrestore(&target_list_lock, flags);
resume_target(nt);
/* netpoll_setup() took a net_device reference and dropped the RTNL
* before returning, all while this target was off target_list and
* thus invisible to netconsole_netdev_event(). If the device was
* unregistered in that window the NETDEV_UNREGISTER notifier could not
* tear this target down, which would leak the reference and hang
* unregister_netdevice(). Re-check under the RTNL before re-publishing:
* taking it across the check and the list_add() serialises against the
* notifier (which also runs under the RTNL), so the device is either
* still registered (the notifier will find the re-added target) or
* already unregistering (we drop the reference here).
*/
rtnl_lock();
if (nt->state == STATE_ENABLED && nt->np.dev &&
nt->np.dev->reg_state != NETREG_REGISTERED) {
netconsole_skb_pool_flush(nt);
netcons_release_dev(nt);
nt->state = STATE_DISABLED;
}
/* At this point the target is either enabled or disabled and
* was cleaned up before getting deactivated. Either way, add it
* back to target list.
*/
spin_lock_irqsave(&target_list_lock, flags);
list_add(&nt->list, &target_list);
spin_unlock_irqrestore(&target_list_lock, flags);
rtnl_unlock();
out_unlock:
dynamic_netconsole_mutex_unlock();
}
/* Allocate and initialize with defaults.
* Note that these targets get their config_item fields zeroed-out.
*/
static struct netconsole_target *alloc_and_init(void)
{
struct netconsole_target *nt;
nt = kzalloc_obj(*nt);
if (!nt)
return nt;
if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
nt->extended = true;
if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
nt->release = true;
nt->np.name = "netconsole";
strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
nt->local_port = 6665;
nt->remote_port = 6666;
eth_broadcast_addr(nt->remote_mac);
nt->state = STATE_DISABLED;
INIT_WORK(&nt->resume_wq, process_resume_target);
/* Set up the skb pool primitives once; enabling only refills it. */
skb_queue_head_init(&nt->skb_pool);
INIT_WORK(&nt->refill_wq, refill_skbs_work_handler);
return nt;
}
/* Clean up every target in the cleanup_list and move the clean targets back to
* the main target_list.
*/
static void netconsole_process_cleanups_core(void)
{
struct netconsole_target *nt, *tmp;
unsigned long flags;
/* The cleanup needs RTNL locked */
ASSERT_RTNL();
mutex_lock(&target_cleanup_list_lock);
list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) {
/* all entries in the cleanup_list needs to be disabled */
WARN_ON_ONCE(nt->state == STATE_ENABLED);
netconsole_skb_pool_flush(nt);
netcons_release_dev(nt);
/* moved the cleaned target to target_list. Need to hold both
* locks
*/
spin_lock_irqsave(&target_list_lock, flags);
list_move(&nt->list, &target_list);
spin_unlock_irqrestore(&target_list_lock, flags);
}
WARN_ON_ONCE(!list_empty(&target_cleanup_list));
mutex_unlock(&target_cleanup_list_lock);
}
static void netconsole_print_banner(struct netconsole_target *nt)
{
struct netpoll *np = &nt->np;
np_info(np, "local port %d\n", nt->local_port);
if (nt->local_ip.family == AF_UNSPEC)
np_info(np, "local IP unset\n");
else if (nt->local_ip.family == AF_INET6)
np_info(np, "local IPv6 address %pI6c\n", &nt->local_ip.in6);
else
np_info(np, "local IPv4 address %pI4\n", &nt->local_ip.ip);
np_info(np, "interface name '%s'\n", np->dev_name);
np_info(np, "local ethernet address '%pM'\n", np->dev_mac);
np_info(np, "remote port %d\n", nt->remote_port);
if (nt->remote_ip.family == AF_UNSPEC)
np_info(np, "remote IP unset\n");
else if (nt->remote_ip.family == AF_INET6)
np_info(np, "remote IPv6 address %pI6c\n", &nt->remote_ip.in6);
else
np_info(np, "remote IPv4 address %pI4\n", &nt->remote_ip.ip);
np_info(np, "remote ethernet address %pM\n", nt->remote_mac);
}
/* Parse the string and populate the `inet_addr` struct. Return 0 on success
* and -1 upon failure.
*/
static int netpoll_parse_ip_addr(const char *str, struct inet_addr *addr)
{
const char *end = NULL;
int len;
len = strlen(str);
if (!len)
return -1;
if (str[len - 1] == '\n')
len -= 1;
if (in4_pton(str, len, (void *)&addr->ip, -1, &end) > 0 &&
(!end || *end == 0 || *end == '\n')) {
addr->family = AF_INET;
return 0;
}
if (IS_ENABLED(CONFIG_IPV6) &&
in6_pton(str, len, (void *)&addr->in6, -1, &end) > 0 &&
(!end || *end == 0 || *end == '\n')) {
addr->family = AF_INET6;
return 0;
}
return -1;
}
#ifdef CONFIG_NETCONSOLE_DYNAMIC
/*
* Our subsystem hierarchy is:
*
* /sys/kernel/config/netconsole/
* |
* <target>/
* | enabled
* | release
* | dev_name
* | local_port
* | remote_port
* | local_ip
* | remote_ip
* | local_mac
* | remote_mac
* | transmit_errors
* | userdata/
* | <key>/
* | value
* | ...
* |
* <target>/...
*/
static struct netconsole_target *to_target(struct config_item *item)
{
struct config_group *cfg_group;
cfg_group = to_config_group(item);
if (!cfg_group)
return NULL;
return container_of(to_config_group(item),
struct netconsole_target, group);
}
/* Do the list cleanup with the rtnl lock hold. rtnl lock is necessary because
* netdev might be cleaned-up by calling __netpoll_cleanup(),
*/
static void netconsole_process_cleanups(void)
{
/* rtnl lock is called here, because it has precedence over
* target_cleanup_list_lock mutex and target_cleanup_list
*/
rtnl_lock();
netconsole_process_cleanups_core();
rtnl_unlock();
}
/* Get rid of possible trailing newline, returning the new length */
static void trim_newline(char *s, size_t maxlen)
{
size_t len;
len = strnlen(s, maxlen);
if (!len)
return;
if (s[len - 1] == '\n')
s[len - 1] = '\0';
}
/*
* Attribute operations for netconsole_target.
*/
static ssize_t enabled_show(struct config_item *item, char *buf)
{
return sysfs_emit(buf, "%d\n", to_target(item)->state == STATE_ENABLED);
}
static ssize_t extended_show(struct config_item *item, char *buf)
{
return sysfs_emit(buf, "%d\n", to_target(item)->extended);
}
static ssize_t release_show(struct config_item *item, char *buf)
{
return sysfs_emit(buf, "%d\n", to_target(item)->release);
}
static ssize_t dev_name_show(struct config_item *item, char *buf)
{
return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
}
static ssize_t local_port_show(struct config_item *item, char *buf)
{
return sysfs_emit(buf, "%d\n", to_target(item)->local_port);
}
static ssize_t remote_port_show(struct config_item *item, char *buf)
{
return sysfs_emit(buf, "%d\n", to_target(item)->remote_port);
}
static ssize_t local_ip_show(struct config_item *item, char *buf)
{
struct netconsole_target *nt = to_target(item);
if (nt->local_ip.family == AF_UNSPEC)
return sysfs_emit(buf, "\n");
if (nt->local_ip.family == AF_INET6)
return sysfs_emit(buf, "%pI6c\n", &nt->local_ip.in6);
else
return sysfs_emit(buf, "%pI4\n", &nt->local_ip.ip);
}
static ssize_t remote_ip_show(struct config_item *item, char *buf)
{
struct netconsole_target *nt = to_target(item);
if (nt->remote_ip.family == AF_UNSPEC)
return sysfs_emit(buf, "\n");
if (nt->remote_ip.family == AF_INET6)
return sysfs_emit(buf, "%pI6c\n", &nt->remote_ip.in6);
else
return sysfs_emit(buf, "%pI4\n", &nt->remote_ip.ip);
}
static ssize_t local_mac_show(struct config_item *item, char *buf)
{
struct net_device *dev = to_target(item)->np.dev;
static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
}
static ssize_t remote_mac_show(struct config_item *item, char *buf)
{
return sysfs_emit(buf, "%pM\n", to_target(item)->remote_mac);
}
static ssize_t transmit_errors_show(struct config_item *item, char *buf)
{
struct netconsole_target *nt = to_target(item);
u64 xmit_drop_count, enomem_count;
unsigned int start;
do {
start = u64_stats_fetch_begin(&nt->stats.syncp);
xmit_drop_count = u64_stats_read(&nt->stats.xmit_drop_count);
enomem_count = u64_stats_read(&nt->stats.enomem_count);
} while (u64_stats_fetch_retry(&nt->stats.syncp, start));
return sysfs_emit(buf, "%llu\n", xmit_drop_count + enomem_count);
}
/* configfs helper to display if cpu_nr sysdata feature is enabled */
static ssize_t sysdata_cpu_nr_enabled_show(struct config_item *item, char *buf)
{
struct netconsole_target *nt = to_target(item->ci_parent);
bool cpu_nr_enabled;
dynamic_netconsole_mutex_lock();
cpu_nr_enabled = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
dynamic_netconsole_mutex_unlock();
return sysfs_emit(buf, "%d\n", cpu_nr_enabled);
}
/* configfs helper to display if taskname sysdata feature is enabled */
static ssize_t sysdata_taskname_enabled_show(struct config_item *item,
char *buf)
{
struct netconsole_target *nt = to_target(item->ci_parent);
bool taskname_enabled;
dynamic_netconsole_mutex_lock();
taskname_enabled = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
dynamic_netconsole_mutex_unlock();
return sysfs_emit(buf, "%d\n", taskname_enabled);
}
static ssize_t sysdata_release_enabled_show(struct config_item *item,
char *buf)
{
struct netconsole_target *nt = to_target(item->ci_parent);
bool release_enabled;
dynamic_netconsole_mutex_lock();
release_enabled = !!(nt->sysdata_fields & SYSDATA_RELEASE);
dynamic_netconsole_mutex_unlock();
return sysfs_emit(buf, "%d\n", release_enabled);
}
/* Iterate in the list of target, and make sure we don't have any console
* register without targets of the same type
*/
static void unregister_netcons_consoles(void)
{
struct netconsole_target *nt;
u32 console_type_needed = 0;
unsigned long flags;
spin_lock_irqsave(&target_list_lock, flags);
list_for_each_entry(nt, &target_list, list) {
if (nt->extended)
console_type_needed |= CONS_EXTENDED;
else
console_type_needed |= CONS_BASIC;
}
spin_unlock_irqrestore(&target_list_lock, flags);
if (!(console_type_needed & CONS_EXTENDED) &&
console_is_registered(&netconsole_ext))
unregister_console(&netconsole_ext);
if (!(console_type_needed & CONS_BASIC) &&
console_is_registered(&netconsole))
unregister_console(&netconsole);
}
static ssize_t sysdata_msgid_enabled_show(struct config_item *item,
char *buf)
{
struct netconsole_target *nt = to_target(item->ci_parent);
bool msgid_enabled;
dynamic_netconsole_mutex_lock();