-
Notifications
You must be signed in to change notification settings - Fork 1
/
net.c
1696 lines (1476 loc) · 46.2 KB
/
net.c
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
/* net.c - core analysis suite
*
* Copyright (C) 1999, 2000, 2001, 2002 Mission Critical Linux, Inc.
* Copyright (C) 2002-2016 David Anderson
* Copyright (C) 2002-2016 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "defs.h"
#include <netinet/in.h>
#include <netdb.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
/*
* Cache values we need that can change based on OS version, or any other
* variables static to this file. These are setup in net_init(). Dump
* the table during runtime via "help -n".
*/
struct net_table {
ulong flags;
char *netdevice; /* name of net device */
char *dev_name_t; /* readmem ID's */
char *dev_type_t;
char *dev_addr_t;
long dev_name;
long dev_next;
long dev_type;
long dev_addr_len;
long dev_ip_ptr;
long in_device_ifa_list;
long in_ifaddr_ifa_next;
long in_ifaddr_ifa_address;
int net_device_name_index;
} net_table = { 0 };
struct net_table *net = &net_table;
#define NETDEV_INIT (0x1)
#define STRUCT_DEVICE (0x2)
#define STRUCT_NET_DEVICE (0x4)
#define SOCK_V1 (0x8)
#define SOCK_V2 (0x10)
#define NO_INET_SOCK (0x20)
#define DEV_NAME_MAX 100
struct devinfo {
char dev_name[DEV_NAME_MAX];
unsigned char dev_addr_len;
short dev_type;
};
#define BYTES_IP_ADDR 15 /* bytes to print IP addr (xxx.xxx.xxx.xxx) */
#define BYTES_PORT_NUM 5 /* bytes to print port number */
/* bytes needed for <ip address>:<port> notation */
#define BYTES_IP_TUPLE (BYTES_IP_ADDR + BYTES_PORT_NUM + 1)
static void show_net_devices(ulong);
static void show_net_devices_v2(ulong);
static void show_net_devices_v3(ulong);
static void print_neighbour_q(ulong, int);
static void get_netdev_info(ulong, struct devinfo *);
static void get_device_name(ulong, char *);
static long get_device_address(ulong, char **, long);
static void get_sock_info(ulong, char *);
static void dump_arp(void);
static void arp_state_to_flags(unsigned char);
static void dump_ether_hw(unsigned char *, int);
static void dump_sockets(ulong, struct reference *);
static int sym_socket_dump(ulong, int, int, ulong, struct reference *);
static void dump_hw_addr(unsigned char *, int);
static char *dump_in6_addr_port(uint16_t *, uint16_t, char *, int *);
#define MK_TYPE_T(f,s,m) \
do { \
(f) = malloc(strlen(s) + strlen(m) + 2); \
if ((f) == NULL) { \
error(WARNING, "malloc fail for type %s.%s", (s), (m)); \
} else { \
sprintf((f), "%s %s", (s), (m)); \
} \
} while(0)
void
net_init(void)
{
/*
* Note the order of the following checks. The device struct was
* renamed to net_device in 2.3, but there may be another struct
* called 'device' so we check for the new one first.
*/
STRUCT_SIZE_INIT(net_device, "net_device");
if (VALID_STRUCT(net_device)) {
net->netdevice = "net_device";
net->dev_next = MEMBER_OFFSET_INIT(net_device_next,
"net_device", "next");
net->dev_name = MEMBER_OFFSET_INIT(net_device_name,
"net_device", "name");
net->dev_type = MEMBER_OFFSET_INIT(net_device_type,
"net_device", "type");
net->dev_addr_len = MEMBER_OFFSET_INIT(net_device_addr_len,
"net_device", "addr_len");
net->dev_ip_ptr = MEMBER_OFFSET_INIT(net_device_ip_ptr,
"net_device", "ip_ptr");
MEMBER_OFFSET_INIT(net_device_dev_list, "net_device", "dev_list");
MEMBER_OFFSET_INIT(net_dev_base_head, "net", "dev_base_head");
ARRAY_LENGTH_INIT(net->net_device_name_index,
net_device_name, "net_device.name", NULL, sizeof(char));
net->flags |= (NETDEV_INIT|STRUCT_NET_DEVICE);
} else {
STRUCT_SIZE_INIT(device, "device");
if (VALID_STRUCT(device)) {
net->netdevice = "device";
net->dev_next = MEMBER_OFFSET_INIT(device_next,
"device", "next");
net->dev_name = MEMBER_OFFSET_INIT(device_name,
"device", "name");
net->dev_type = MEMBER_OFFSET_INIT(device_type,
"device", "type");
net->dev_ip_ptr = MEMBER_OFFSET_INIT(device_ip_ptr,
"device", "ip_ptr");
net->dev_addr_len = MEMBER_OFFSET_INIT(device_addr_len,
"device", "addr_len");
net->flags |= (NETDEV_INIT|STRUCT_DEVICE);
} else
error(WARNING,
"net_init: unknown device type for net device");
}
if (VALID_MEMBER(task_struct_nsproxy))
MEMBER_OFFSET_INIT(nsproxy_net_ns, "nsproxy", "net_ns");
if (net->flags & NETDEV_INIT) {
MK_TYPE_T(net->dev_name_t, net->netdevice, "name");
MK_TYPE_T(net->dev_type_t, net->netdevice, "type");
MK_TYPE_T(net->dev_addr_t, net->netdevice, "addr_len");
MEMBER_OFFSET_INIT(socket_sk, "socket", "sk");
MEMBER_OFFSET_INIT(neighbour_next, "neighbour", "next");
MEMBER_OFFSET_INIT(neighbour_primary_key,
"neighbour", "primary_key");
MEMBER_OFFSET_INIT(neighbour_ha, "neighbour", "ha");
MEMBER_OFFSET_INIT(neighbour_dev, "neighbour", "dev");
MEMBER_OFFSET_INIT(neighbour_nud_state,
"neighbour", "nud_state");
MEMBER_OFFSET_INIT(neigh_table_nht_ptr, "neigh_table", "nht");
if (VALID_MEMBER(neigh_table_nht_ptr)) {
MEMBER_OFFSET_INIT(neigh_table_hash_mask,
"neigh_hash_table", "hash_mask");
MEMBER_OFFSET_INIT(neigh_table_hash_shift,
"neigh_hash_table", "hash_shift");
MEMBER_OFFSET_INIT(neigh_table_hash_buckets,
"neigh_hash_table", "hash_buckets");
} else {
MEMBER_OFFSET_INIT(neigh_table_hash_buckets,
"neigh_table", "hash_buckets");
MEMBER_OFFSET_INIT(neigh_table_hash_mask,
"neigh_table", "hash_mask");
}
MEMBER_OFFSET_INIT(neigh_table_key_len,
"neigh_table", "key_len");
MEMBER_OFFSET_INIT(in_device_ifa_list,
"in_device", "ifa_list");
MEMBER_OFFSET_INIT(in_ifaddr_ifa_next,
"in_ifaddr", "ifa_next");
MEMBER_OFFSET_INIT(in_ifaddr_ifa_address,
"in_ifaddr", "ifa_address");
STRUCT_SIZE_INIT(sock, "sock");
MEMBER_OFFSET_INIT(sock_family, "sock", "family");
if (VALID_MEMBER(sock_family)) {
MEMBER_OFFSET_INIT(sock_daddr, "sock", "daddr");
MEMBER_OFFSET_INIT(sock_rcv_saddr, "sock", "rcv_saddr");
MEMBER_OFFSET_INIT(sock_dport, "sock", "dport");
MEMBER_OFFSET_INIT(sock_sport, "sock", "sport");
MEMBER_OFFSET_INIT(sock_num, "sock", "num");
MEMBER_OFFSET_INIT(sock_type, "sock", "type");
net->flags |= SOCK_V1;
} else {
/*
* struct sock {
* struct sock_common __sk_common;
* #define sk_family __sk_common.skc_family
* ...
*/
MEMBER_OFFSET_INIT(sock_common_skc_family,
"sock_common", "skc_family");
MEMBER_OFFSET_INIT(sock_sk_type, "sock", "sk_type");
/*
* struct inet_sock {
* struct sock sk;
* struct ipv6_pinfo *pinet6;
* struct inet_opt inet;
* };
*/
STRUCT_SIZE_INIT(inet_sock, "inet_sock");
STRUCT_SIZE_INIT(socket, "socket");
if (STRUCT_EXISTS("inet_opt")) {
MEMBER_OFFSET_INIT(inet_sock_inet, "inet_sock", "inet");
MEMBER_OFFSET_INIT(inet_opt_daddr, "inet_opt", "daddr");
MEMBER_OFFSET_INIT(inet_opt_rcv_saddr, "inet_opt", "rcv_saddr");
MEMBER_OFFSET_INIT(inet_opt_dport, "inet_opt", "dport");
MEMBER_OFFSET_INIT(inet_opt_sport, "inet_opt", "sport");
MEMBER_OFFSET_INIT(inet_opt_num, "inet_opt", "num");
} else { /* inet_opt moved to inet_sock */
ASSIGN_OFFSET(inet_sock_inet) = 0;
if (MEMBER_EXISTS("inet_sock", "daddr")) {
MEMBER_OFFSET_INIT(inet_opt_daddr, "inet_sock", "daddr");
MEMBER_OFFSET_INIT(inet_opt_rcv_saddr, "inet_sock", "rcv_saddr");
MEMBER_OFFSET_INIT(inet_opt_dport, "inet_sock", "dport");
MEMBER_OFFSET_INIT(inet_opt_sport, "inet_sock", "sport");
MEMBER_OFFSET_INIT(inet_opt_num, "inet_sock", "num");
} else if (MEMBER_EXISTS("inet_sock", "inet_daddr")) {
MEMBER_OFFSET_INIT(inet_opt_daddr, "inet_sock", "inet_daddr");
MEMBER_OFFSET_INIT(inet_opt_rcv_saddr, "inet_sock", "inet_rcv_saddr");
MEMBER_OFFSET_INIT(inet_opt_dport, "inet_sock", "inet_dport");
MEMBER_OFFSET_INIT(inet_opt_sport, "inet_sock", "inet_sport");
MEMBER_OFFSET_INIT(inet_opt_num, "inet_sock", "inet_num");
} else if ((MEMBER_OFFSET("inet_sock", "sk") == 0) &&
(MEMBER_OFFSET("sock", "__sk_common") == 0)) {
MEMBER_OFFSET_INIT(inet_opt_daddr, "sock_common", "skc_daddr");
if (INVALID_MEMBER(inet_opt_daddr))
ANON_MEMBER_OFFSET_INIT(inet_opt_daddr, "sock_common",
"skc_daddr");
MEMBER_OFFSET_INIT(inet_opt_rcv_saddr, "sock_common", "skc_rcv_saddr");
if (INVALID_MEMBER(inet_opt_rcv_saddr))
ANON_MEMBER_OFFSET_INIT(inet_opt_rcv_saddr, "sock_common",
"skc_rcv_saddr");
MEMBER_OFFSET_INIT(inet_opt_dport, "inet_sock", "inet_dport");
if (INVALID_MEMBER(inet_opt_dport)) {
MEMBER_OFFSET_INIT(inet_opt_dport, "sock_common",
"skc_dport");
if (INVALID_MEMBER(inet_opt_dport))
ANON_MEMBER_OFFSET_INIT(inet_opt_dport, "sock_common",
"skc_dport");
}
MEMBER_OFFSET_INIT(inet_opt_sport, "inet_sock", "inet_sport");
MEMBER_OFFSET_INIT(inet_opt_num, "inet_sock", "inet_num");
if (INVALID_MEMBER(inet_opt_num)) {
MEMBER_OFFSET_INIT(inet_opt_num, "sock_common", "skc_num");
if (INVALID_MEMBER(inet_opt_num))
ANON_MEMBER_OFFSET_INIT(inet_opt_num, "sock_common",
"skc_num");
}
}
}
if (VALID_STRUCT(inet_sock) &&
INVALID_MEMBER(inet_sock_inet)) {
/*
* gdb can't seem to figure out the inet_sock
* in later 2.6 kernels, returning this:
*
* struct inet_sock {
* <no data fields>
* }
*
* It does know the struct size, so kludge it
* to subtract the size of the inet_opt struct
* from the size of the containing inet_sock.
*/
net->flags |= NO_INET_SOCK;
ASSIGN_OFFSET(inet_sock_inet) =
SIZE(inet_sock) - STRUCT_SIZE("inet_opt");
}
/*
* If necessary, set inet_sock size and inet_sock_inet offset,
* accounting for the configuration-dependent, intervening,
* struct ipv6_pinfo pointer located in between the sock and
* inet_opt members of the inet_sock.
*/
if (!VALID_STRUCT(inet_sock))
{
if (symbol_exists("tcpv6_protocol") &&
symbol_exists("udpv6_protocol")) {
ASSIGN_SIZE(inet_sock) = SIZE(sock) +
sizeof(void *) + STRUCT_SIZE("inet_opt");
ASSIGN_OFFSET(inet_sock_inet) = SIZE(sock) +
sizeof(void *);
} else {
ASSIGN_SIZE(inet_sock) = SIZE(sock) +
STRUCT_SIZE("inet_opt");
ASSIGN_OFFSET(inet_sock_inet) = SIZE(sock);
}
}
MEMBER_OFFSET_INIT(ipv6_pinfo_rcv_saddr, "ipv6_pinfo", "rcv_saddr");
MEMBER_OFFSET_INIT(ipv6_pinfo_daddr, "ipv6_pinfo", "daddr");
STRUCT_SIZE_INIT(in6_addr, "in6_addr");
MEMBER_OFFSET_INIT(socket_alloc_vfs_inode, "socket_alloc", "vfs_inode");
net->flags |= SOCK_V2;
}
}
}
/*
* The net command...
*/
#define NETOPTS "N:asSR:xdn"
#define s_FLAG FOREACH_s_FLAG
#define S_FLAG FOREACH_S_FLAG
#define x_FLAG FOREACH_x_FLAG
#define d_FLAG FOREACH_d_FLAG
#define NET_REF_FOUND (0x1)
#define NET_REF_HEXNUM (0x2)
#define NET_REF_DECNUM (0x4)
#define NET_TASK_HEADER_PRINTED (0x8)
#define NET_SOCK_HEADER_PRINTED (0x10)
#define NET_REF_FOUND_ITEM (0x20)
#define NET_REFERENCE_CHECK(X) (X)
#define NET_REFERENCE_FOUND(X) ((X) && ((X)->cmdflags & NET_REF_FOUND))
void
cmd_net(void)
{
int c;
ulong sflag, nflag, aflag;
ulong value;
ulong task;
struct task_context *tc = NULL;
struct in_addr in_addr;
struct reference reference, *ref;
if (!(net->flags & NETDEV_INIT))
error(FATAL, "net subsystem not initialized!");
ref = NULL;
sflag = nflag = aflag = 0;
task = pid_to_task(0);
while ((c = getopt(argcnt, args, NETOPTS)) != EOF) {
switch (c) {
case 'R':
if (ref)
error(INFO, "only one -R option allowed\n");
else {
ref = &reference;
BZERO(ref, sizeof(struct reference));
ref->str = optarg;
}
break;
case 'a':
dump_arp();
aflag++;
break;
case 'N':
value = stol(optarg, FAULT_ON_ERROR, NULL);
in_addr.s_addr = (in_addr_t)value;
fprintf(fp, "%s\n", inet_ntoa(in_addr));
return;
case 's':
if (sflag & S_FLAG)
error(INFO,
"only one -s or -S option allowed\n");
else
sflag |= s_FLAG;
break;
case 'S':
if (sflag & s_FLAG)
error(INFO,
"only one -s or -S option allowed\n");
else
sflag |= S_FLAG;
break;
case 'x':
if (sflag & d_FLAG)
error(FATAL,
"-d and -x are mutually exclusive\n");
sflag |= x_FLAG;
break;
case 'd':
if (sflag & x_FLAG)
error(FATAL,
"-d and -x are mutually exclusive\n");
sflag |= d_FLAG;
break;
case 'n':
nflag = 1;
task = CURRENT_TASK();
if (args[optind]) {
switch (str_to_context(args[optind],
&value, &tc)) {
case STR_PID:
case STR_TASK:
task = tc->task;
}
}
break;
default:
argerrs++;
break;
}
}
if (argerrs)
cmd_usage(pc->curcmd, SYNOPSIS);
if (sflag & (s_FLAG|S_FLAG))
dump_sockets(sflag, ref);
else {
if ((argcnt == 1) || nflag)
show_net_devices(task);
else if (!aflag)
cmd_usage(pc->curcmd, SYNOPSIS);
}
}
/*
* Just display the address and name of each net device.
*/
static void
show_net_devices(ulong task)
{
ulong next;
long flen;
char *buf;
long buflen = BUFSIZE;
if (symbol_exists("dev_base_head")) {
show_net_devices_v2(task);
return;
} else if (symbol_exists("init_net")) {
show_net_devices_v3(task);
return;
}
if (!symbol_exists("dev_base"))
error(FATAL, "dev_base, dev_base_head or init_net do not exist!\n");
get_symbol_data("dev_base", sizeof(void *), &next);
if (!net->netdevice || !next)
return;
buf = GETBUF(buflen);
flen = MAX(VADDR_PRLEN, strlen(net->netdevice));
fprintf(fp, "%s NAME IP ADDRESS(ES)\n",
mkstring(upper_case(net->netdevice, buf),
flen, CENTER|LJUST, NULL));
do {
fprintf(fp, "%s ",
mkstring(buf, flen, CENTER|RJUST|LONG_HEX, MKSTR(next)));
get_device_name(next, buf);
fprintf(fp, "%-6s ", buf);
buflen = get_device_address(next, &buf, buflen);
fprintf(fp, "%s\n", buf);
readmem(next+net->dev_next, KVADDR, &next,
sizeof(void *), "(net_)device.next", FAULT_ON_ERROR);
} while (next);
FREEBUF(buf);
}
static void
show_net_devices_v2(ulong task)
{
struct list_data list_data, *ld;
char *net_device_buf;
char *buf;
long buflen = BUFSIZE;
int ndevcnt, i;
long flen;
if (!net->netdevice) /* initialized in net_init() */
return;
buf = GETBUF(buflen);
flen = MAX(VADDR_PRLEN, strlen(net->netdevice));
fprintf(fp, "%s NAME IP ADDRESS(ES)\n",
mkstring(upper_case(net->netdevice, buf),
flen, CENTER|LJUST, NULL));
net_device_buf = GETBUF(SIZE(net_device));
ld = &list_data;
BZERO(ld, sizeof(struct list_data));
ld->flags |= LIST_ALLOCATE;
get_symbol_data("dev_base_head", sizeof(void *), &ld->start);
ld->end = symbol_value("dev_base_head");
ld->list_head_offset = OFFSET(net_device_dev_list);
ndevcnt = do_list(ld);
for (i = 0; i < ndevcnt; ++i) {
readmem(ld->list_ptr[i], KVADDR, net_device_buf,
SIZE(net_device), "net_device buffer",
FAULT_ON_ERROR);
fprintf(fp, "%s ",
mkstring(buf, flen, CENTER|RJUST|LONG_HEX,
MKSTR(ld->list_ptr[i])));
get_device_name(ld->list_ptr[i], buf);
fprintf(fp, "%-6s ", buf);
buflen = get_device_address(ld->list_ptr[i], &buf, buflen);
fprintf(fp, "%s\n", buf);
}
FREEBUF(ld->list_ptr);
FREEBUF(net_device_buf);
FREEBUF(buf);
}
static void
show_net_devices_v3(ulong task)
{
ulong nsproxy_p, net_ns_p;
struct list_data list_data, *ld;
char *net_device_buf;
char *buf;
long buflen = BUFSIZE;
int ndevcnt, i;
long flen;
if (!net->netdevice) /* initialized in net_init() */
return;
buf = GETBUF(buflen);
flen = MAX(VADDR_PRLEN, strlen(net->netdevice));
fprintf(fp, "%s NAME IP ADDRESS(ES)\n",
mkstring(upper_case(net->netdevice, buf),
flen, CENTER|LJUST, NULL));
net_device_buf = GETBUF(SIZE(net_device));
ld = &list_data;
BZERO(ld, sizeof(struct list_data));
ld->flags |= LIST_ALLOCATE;
if (VALID_MEMBER(nsproxy_net_ns)) {
readmem(task + OFFSET(task_struct_nsproxy), KVADDR, &nsproxy_p,
sizeof(ulong), "task_struct.nsproxy", FAULT_ON_ERROR);
if (!readmem(nsproxy_p + OFFSET(nsproxy_net_ns), KVADDR, &net_ns_p,
sizeof(ulong), "nsproxy.net_ns", RETURN_ON_ERROR|QUIET))
error(FATAL, "cannot determine net_namespace location!\n");
} else
net_ns_p = symbol_value("init_net");
ld->start = ld->end = net_ns_p + OFFSET(net_dev_base_head);
ld->list_head_offset = OFFSET(net_device_dev_list);
ndevcnt = do_list(ld);
/*
* Skip the first entry (init_net).
*/
for (i = 1; i < ndevcnt; ++i) {
readmem(ld->list_ptr[i], KVADDR, net_device_buf,
SIZE(net_device), "net_device buffer",
FAULT_ON_ERROR);
fprintf(fp, "%s ",
mkstring(buf, flen, CENTER|RJUST|LONG_HEX,
MKSTR(ld->list_ptr[i])));
get_device_name(ld->list_ptr[i], buf);
fprintf(fp, "%-6s ", buf);
buflen = get_device_address(ld->list_ptr[i], &buf, buflen);
fprintf(fp, "%s\n", buf);
}
FREEBUF(ld->list_ptr);
FREEBUF(net_device_buf);
FREEBUF(buf);
}
/*
* Perform the actual work of dumping the ARP table...
*/
#define ARP_HEADING \
"NEIGHBOUR IP ADDRESS HW TYPE HW ADDRESS DEVICE STATE"
static void
dump_arp(void)
{
ulong arp_tbl; /* address of arp_tbl */
ulong *hash_buckets;
ulong hash;
long hash_bytes;
int nhash_buckets = 0;
int key_len;
int i;
int header_printed = 0;
int hash_mask = 0;
ulong nht;
if (!symbol_exists("arp_tbl"))
error(FATAL, "arp_tbl does not exist in this kernel\n");
arp_tbl = symbol_value("arp_tbl");
/*
* NOTE: 2.6.8 -> 2.6.9 neigh_table struct changed from:
*
* struct neighbour *hash_buckets[32];
* to
* struct neighbour **hash_buckets;
*
* Use 'hash_mask' as indicator to decide if we're dealing
* with an array or a pointer.
*
* Around 2.6.37 neigh_hash_table struct has been introduced
* and pointer to it has been added to neigh_table.
*/
if (VALID_MEMBER(neigh_table_nht_ptr)) {
readmem(arp_tbl + OFFSET(neigh_table_nht_ptr),
KVADDR, &nht, sizeof(nht),
"neigh_table nht", FAULT_ON_ERROR);
/* NB! Re-use of offsets like neigh_table_hash_mask
* with neigh_hash_table structure */
if (VALID_MEMBER(neigh_table_hash_mask)) {
readmem(nht + OFFSET(neigh_table_hash_mask),
KVADDR, &hash_mask, sizeof(hash_mask),
"neigh_hash_table hash_mask", FAULT_ON_ERROR);
nhash_buckets = hash_mask + 1;
} else if (VALID_MEMBER(neigh_table_hash_shift)) {
readmem(nht + OFFSET(neigh_table_hash_shift),
KVADDR, &hash_mask, sizeof(hash_mask),
"neigh_hash_table hash_shift", FAULT_ON_ERROR);
nhash_buckets = 1U << hash_mask;
}
} else if (VALID_MEMBER(neigh_table_hash_mask)) {
readmem(arp_tbl + OFFSET(neigh_table_hash_mask),
KVADDR, &hash_mask, sizeof(hash_mask),
"neigh_table hash_mask", FAULT_ON_ERROR);
nhash_buckets = hash_mask + 1;
} else
nhash_buckets = (i = ARRAY_LENGTH(neigh_table_hash_buckets)) ?
i : get_array_length("neigh_table.hash_buckets",
NULL, sizeof(void *));
if (nhash_buckets == 0) {
option_not_supported('a');
return;
}
hash_bytes = nhash_buckets * sizeof(*hash_buckets);
hash_buckets = (ulong *)GETBUF(hash_bytes);
readmem(arp_tbl + OFFSET(neigh_table_key_len),
KVADDR, &key_len, sizeof(key_len),
"neigh_table key_len", FAULT_ON_ERROR);
if (VALID_MEMBER(neigh_table_nht_ptr)) {
readmem(nht + OFFSET(neigh_table_hash_buckets),
KVADDR, &hash, sizeof(hash),
"neigh_hash_table hash_buckets ptr", FAULT_ON_ERROR);
readmem(hash, KVADDR, hash_buckets, hash_bytes,
"neigh_hash_table hash_buckets", FAULT_ON_ERROR);
} else if (hash_mask) {
readmem(arp_tbl + OFFSET(neigh_table_hash_buckets),
KVADDR, &hash, sizeof(hash),
"neigh_table hash_buckets pointer", FAULT_ON_ERROR);
readmem(hash,
KVADDR, hash_buckets, hash_bytes,
"neigh_table hash_buckets", FAULT_ON_ERROR);
} else
readmem(arp_tbl + OFFSET(neigh_table_hash_buckets),
KVADDR, hash_buckets, hash_bytes,
"neigh_table hash_buckets", FAULT_ON_ERROR);
for (i = 0; i < nhash_buckets; i++) {
if (hash_buckets[i] != (ulong)NULL) {
if (!header_printed) {
fprintf(fp, "%s\n", ARP_HEADING);
header_printed = 1;
}
print_neighbour_q(hash_buckets[i], key_len);
}
}
fflush(fp);
FREEBUF(hash_buckets);
}
/*
* Dump out the relevant information of a neighbour structure for the
* ARP table.
*/
static void
print_neighbour_q(ulong addr, int key_len)
{
int i;
ulong dev; /* dev address of this struct */
unsigned char *ha_buf; /* buffer for hardware address */
uint ha_size; /* size of HW address */
uint ipaddr; /* hold ipaddr (aka primary_key) */
struct devinfo dinfo;
unsigned char state; /* state of ARP entry */
struct in_addr in_addr;
ha_size = (i = ARRAY_LENGTH(neighbour_ha)) ?
i : get_array_length("neighbour.ha", NULL, sizeof(char));
ha_buf = (unsigned char *)GETBUF(ha_size);
while (addr) {
readmem(addr + OFFSET(neighbour_primary_key), KVADDR,
&ipaddr, sizeof(ipaddr), "neighbour primary_key",
FAULT_ON_ERROR);
readmem(addr + OFFSET(neighbour_ha), KVADDR, ha_buf, ha_size,
"neighbour ha", FAULT_ON_ERROR);
readmem(addr + OFFSET(neighbour_dev), KVADDR, &dev, sizeof(dev),
"neighbour dev", FAULT_ON_ERROR);
get_netdev_info(dev, &dinfo);
readmem(addr + OFFSET(neighbour_nud_state), KVADDR,
&state, sizeof(state), "neighbour nud_state",
FAULT_ON_ERROR);
in_addr.s_addr = ipaddr;
fprintf(fp, "%-16lx %-16s", addr, inet_ntoa(in_addr));
switch (dinfo.dev_type) {
case ARPHRD_ETHER:
/*
* Use the actual HW address size in the device struct
* rather than the max size of the array (as was done
* during the readmem() call above....
*/
fprintf(fp, "%-10s ", "ETHER");
dump_ether_hw(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_NETROM:
fprintf(fp, "%-10s ", "NETROM");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_EETHER:
fprintf(fp, "%-10s ", "EETHER");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_AX25:
fprintf(fp, "%-10s ", "AX25");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_PRONET:
fprintf(fp, "%-10s ", "PRONET");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_CHAOS:
fprintf(fp, "%-10s ", "CHAOS");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_IEEE802:
fprintf(fp, "%-10s ", "IEEE802");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_ARCNET:
fprintf(fp, "%-10s ", "ARCNET");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_APPLETLK:
fprintf(fp, "%-10s ", "APPLETLK");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_DLCI:
fprintf(fp, "%-10s ", "DLCI");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
case ARPHRD_METRICOM:
fprintf(fp, "%-10s ", "METRICOM");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
default:
fprintf(fp, "%-10s ", "UNKNOWN");
dump_hw_addr(ha_buf, dinfo.dev_addr_len);
break;
}
fprintf(fp, " %-6s ", dinfo.dev_name);
arp_state_to_flags(state);
readmem(addr + OFFSET(neighbour_next), KVADDR,
&addr, sizeof(addr), "neighbour next", FAULT_ON_ERROR);
}
FREEBUF(ha_buf);
}
/*
* read netdevice info....
*/
static void
get_netdev_info(ulong devaddr, struct devinfo *dip)
{
short dev_type;
get_device_name(devaddr, dip->dev_name);
readmem(devaddr + net->dev_type, KVADDR,
&dev_type, sizeof(dev_type), net->dev_type_t, FAULT_ON_ERROR);
dip->dev_type = dev_type;
readmem(devaddr + net->dev_addr_len, KVADDR,
&dip->dev_addr_len, sizeof(dip->dev_addr_len), net->dev_addr_t,
FAULT_ON_ERROR);
}
/*
* Get the device name.
*/
static void
get_device_name(ulong devaddr, char *buf)
{
ulong name_addr;
switch (net->flags & (STRUCT_DEVICE|STRUCT_NET_DEVICE))
{
case STRUCT_NET_DEVICE:
if (net->net_device_name_index > 0) {
readmem(devaddr + net->dev_name, KVADDR,
buf, net->net_device_name_index,
net->dev_name_t, FAULT_ON_ERROR);
return;
}
/* fallthrough */
case STRUCT_DEVICE:
readmem(devaddr + net->dev_name, KVADDR,
&name_addr, sizeof(name_addr), net->dev_name_t,
FAULT_ON_ERROR);
read_string(name_addr, buf, DEV_NAME_MAX);
break;
}
}
/*
* Get the device address.
*
* {net_}device->ip_ptr points to in_device.
* in_device->in_ifaddr points to in_ifaddr list.
* in_ifaddr->ifa_address contains the address.
* in_ifaddr->ifa_next points to the next in_ifaddr in the list (if any).
*
*/
static long
get_device_address(ulong devaddr, char **bufp, long buflen)
{
ulong ip_ptr, ifa_list;
struct in_addr ifa_address;
char *buf;
char buf2[BUFSIZE];
long pos = 0;
buf = *bufp;
BZERO(buf, buflen);
BZERO(buf2, BUFSIZE);
readmem(devaddr + net->dev_ip_ptr, KVADDR,
&ip_ptr, sizeof(ulong), "ip_ptr", FAULT_ON_ERROR);
if (!ip_ptr)
return buflen;
readmem(ip_ptr + OFFSET(in_device_ifa_list), KVADDR,
&ifa_list, sizeof(ulong), "ifa_list", FAULT_ON_ERROR);
while (ifa_list) {
readmem(ifa_list + OFFSET(in_ifaddr_ifa_address), KVADDR,
&ifa_address, sizeof(struct in_addr), "ifa_address",
FAULT_ON_ERROR);
sprintf(buf2, "%s%s", pos ? ", " : "", inet_ntoa(ifa_address));
if (pos + strlen(buf2) >= buflen) {
RESIZEBUF(*bufp, buflen, buflen * 2);
buf = *bufp;
BZERO(buf + buflen, buflen);
buflen *= 2;
}
BCOPY(buf2, &buf[pos], strlen(buf2));
pos += strlen(buf2);
readmem(ifa_list + OFFSET(in_ifaddr_ifa_next), KVADDR,
&ifa_list, sizeof(ulong), "ifa_next", FAULT_ON_ERROR);
}
return buflen;
}
/*
* Get the family, type, local and destination address/port pairs.
*/
static void
get_sock_info(ulong sock, char *buf)
{
uint32_t daddr, rcv_saddr;
uint16_t dport, sport;
ushort family, type;
ushort num ATTRIBUTE_UNUSED;
char *sockbuf, *inet_sockbuf;
ulong ipv6_pinfo, ipv6_rcv_saddr, ipv6_daddr;
uint16_t u6_addr16_src[8];
uint16_t u6_addr16_dest[8];
char buf2[BUFSIZE];
struct in_addr in_addr;
int len;
BZERO(buf, BUFSIZE);
BZERO(buf2, BUFSIZE);
sockbuf = inet_sockbuf = NULL;
rcv_saddr = daddr = 0;
dport = sport = 0;
family = type = 0;
ipv6_pinfo = 0;
switch (net->flags & (SOCK_V1|SOCK_V2))
{
case SOCK_V1:
sockbuf = GETBUF(SIZE(sock));
readmem(sock, KVADDR, sockbuf, SIZE(sock),
"sock buffer", FAULT_ON_ERROR);
daddr = UINT(sockbuf + OFFSET(sock_daddr));
rcv_saddr = UINT(sockbuf + OFFSET(sock_rcv_saddr));
dport = USHORT(sockbuf + OFFSET(sock_dport));
sport = USHORT(sockbuf + OFFSET(sock_sport));
num = USHORT(sockbuf + OFFSET(sock_num));
family = USHORT(sockbuf + OFFSET(sock_family));
type = USHORT(sockbuf + OFFSET(sock_type));
break;
case SOCK_V2:
inet_sockbuf = GETBUF(SIZE(inet_sock));
readmem(sock, KVADDR, inet_sockbuf, SIZE(inet_sock),
"inet_sock buffer", FAULT_ON_ERROR);
daddr = UINT(inet_sockbuf + OFFSET(inet_sock_inet) +
OFFSET(inet_opt_daddr));
rcv_saddr = UINT(inet_sockbuf + OFFSET(inet_sock_inet) +
OFFSET(inet_opt_rcv_saddr));
dport = USHORT(inet_sockbuf + OFFSET(inet_sock_inet) +
OFFSET(inet_opt_dport));
sport = USHORT(inet_sockbuf + OFFSET(inet_sock_inet) +
OFFSET(inet_opt_sport));
num = USHORT(inet_sockbuf + OFFSET(inet_sock_inet) +
OFFSET(inet_opt_num));
family = USHORT(inet_sockbuf + OFFSET(sock_common_skc_family));
type = USHORT(inet_sockbuf + OFFSET(sock_sk_type));
ipv6_pinfo = ULONG(inet_sockbuf + SIZE(sock));
break;
}
switch (family)
{
case AF_UNSPEC:
sprintf(buf, "UNSPEC:"); break;
case AF_UNIX:
sprintf(buf, "UNIX:"); break;
case AF_INET:
sprintf(buf, "INET:"); break;
case AF_AX25:
sprintf(buf, "AX25:"); break;
case AF_IPX:
sprintf(buf, "IPX:"); break;
case AF_APPLETALK: