forked from lsof-org/lsof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsock.c
More file actions
5024 lines (4654 loc) · 156 KB
/
dsock.c
File metadata and controls
5024 lines (4654 loc) · 156 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
/*
* dsock.c - Linux socket processing functions for /proc-based lsof
*/
/*
* Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana
* 47907. All rights reserved.
*
* Written by Victor A. Abell
*
* This software is not subject to any license of the American Telephone
* and Telegraph Company or the Regents of the University of California.
*
* Permission is granted to anyone to use this software for any purpose on
* any computer system, and to alter it and redistribute it freely, subject
* to the following restrictions:
*
* 1. Neither the authors nor Purdue University are responsible for any
* consequences of the use of this software.
*
* 2. The origin of this software must not be misrepresented, either by
* explicit claim or by omission. Credit to the authors and Purdue
* University must appear in documentation and sources.
*
* 3. Altered versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 4. This notice may not be removed or altered.
*/
#include "common.h"
#include <sys/xattr.h>
#include "hash.h"
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
/*
* UNIX endpoint definitions
*/
# include <sys/socket.h> /* for AF_NETLINK */
# include <linux/rtnetlink.h> /* for NETLINK_INET_DIAG */
# include <linux/sock_diag.h> /* for SOCK_DIAG_BY_FAMILY */
# include <linux/unix_diag.h> /* for unix_diag_req */
# include <string.h> /* memset */
# include <stdint.h> /* for unt8_t */
# include <unistd.h> /* for getpagesize */
# define SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L)
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
#if defined(HASSOSTATE)
# include <linux/net.h> /* for SS_* */
#endif /* defined(HASSOSTATE) */
/*
* Local definitions
*/
#define INOBUCKS \
128 /* inode hash bucket count -- must be \
* a power of two */
#define INOHASH(ino) ((int)((ino * 31415) >> 3) & (INOBUCKS - 1))
#define TCPUDPHASH(ino) ((int)((ino * 31415) >> 3) & (TcpUdp_bucks - 1))
#define TCPUDP6HASH(ino) ((int)((ino * 31415) >> 3) & (TcpUdp6_bucks - 1))
#define IPCBUCKS \
4096 /* IPC hash bucket count -- must be \
* a power of two */
/* If a socket is used for IPC, we store both end points for the socket
* to the same hash bucket. This makes searching the counter part of
* an end point easier. See get_netpeeri(). */
#define TCPUDP_IPC_HASH(tp) \
((int)(((((tp)->faddr * 0x109 + (tp)->laddr * 0x109 + \
(tp)->fport * 0x121 + (tp)->lport * 0x121 + \
(tp)->proto * 0x181) * \
31415) >> \
3) & \
(IPCBUCKS - 1)))
#define TCPUDP6_IPC_ADDR_INT32(a, n) (((a)->s6_addr32[n]))
#define TCPUDP6_IPC_ADDR_MK_INT(a) \
((int)TCPUDP6_IPC_ADDR_INT32(a, 0x0) * 0x123 + \
(int)TCPUDP6_IPC_ADDR_INT32(a, 0x1) * 0x111 + \
(int)TCPUDP6_IPC_ADDR_INT32(a, 0x2) * 0x149 + \
(int)TCPUDP6_IPC_ADDR_INT32(a, 0x3) * 0x185)
#define TCPUDP6_IPC_HASH(tp) \
((int)((((TCPUDP6_IPC_ADDR_MK_INT(&(tp)->faddr) + \
TCPUDP6_IPC_ADDR_MK_INT(&(tp)->laddr) + (tp)->fport * 0x109 + \
(tp)->lport * 0x109 + (tp)->proto * 0x141) * \
31415) >> \
3) & \
(IPCBUCKS - 1)))
/*
* Local structures
*/
struct ax25sin { /* AX25 socket information */
char *da; /* destination address */
char *dev_ch; /* device characters */
char *sa; /* source address */
INODETYPE inode;
unsigned long sq, rq; /* send and receive queue values */
unsigned char sqs, rqs; /* send and receive queue states */
int state;
struct ax25sin *next;
};
struct icmpin {
INODETYPE inode; /* node number */
char *la; /* local address */
char *ra; /* remote address */
MALLOC_S lal; /* strlen(la) */
MALLOC_S ral; /* strlen(ra) */
struct icmpin *next;
};
struct ipxsin { /* IPX socket information */
INODETYPE inode;
char *la; /* local address */
char *ra; /* remote address */
int state;
unsigned long txq, rxq; /* transmit and receive queue values */
struct ipxsin *next;
};
struct nlksin { /* Netlink socket information */
INODETYPE inode; /* node number */
unsigned int pr; /* protocol */
struct nlksin *next;
};
struct packin { /* packet information */
INODETYPE inode;
int ty; /* socket type */
int pr; /* protocol */
struct packin *next;
};
struct rawsin { /* raw socket information */
INODETYPE inode;
char *la; /* local address */
char *ra; /* remote address */
char *sp; /* state characters */
MALLOC_S lal; /* strlen(la) */
MALLOC_S ral; /* strlen(ra) */
MALLOC_S spl; /* strlen(sp) */
struct rawsin *next;
};
struct sctpsin { /* SCTP socket information */
INODETYPE inode;
int type; /* type: 0 = assoc
* 1 = eps
* 2 assoc and eps */
char *addr; /* association or endpoint address */
char *assocID; /* association ID */
char *lport; /* local port */
char *rport; /* remote port */
char *laddrs; /* local address */
char *raddrs; /* remote address */
struct sctpsin *next;
};
struct tcp_udp { /* IPv4 TCP and UDP socket
* information */
INODETYPE inode;
unsigned long faddr, laddr; /* foreign & local IPv4 addresses */
int fport, lport; /* foreign & local ports */
unsigned long txq, rxq; /* transmit & receive queue values */
int proto; /* 0 = TCP, 1 = UDP, 2 = UDPLITE */
int state; /* protocol state */
struct tcp_udp *next; /* in TcpUdp inode hash table */
#if defined(HASEPTOPTS)
pxinfo_t *pxinfo; /* inode information */
struct tcp_udp *ipc_next; /* in TcpUdp local ipc hash table */
struct tcp_udp *ipc_peer; /* locally connected peer(s) info */
#endif /* defined(HASEPTOPTS) */
};
#if defined(HASIPv6)
struct tcp_udp6 { /* IPv6 TCP and UDP socket
* information */
INODETYPE inode;
struct in6_addr faddr, laddr; /* foreign & local IPv6 addresses */
int fport, lport; /* foreign & local ports */
unsigned long txq, rxq; /* transmit & receive queue values */
int proto; /* 0 = TCP, 1 = UDP, 2 = UDPLITE */
int state; /* protocol state */
struct tcp_udp6 *next;
# if defined(HASEPTOPTS)
pxinfo_t *pxinfo; /* inode information */
struct tcp_udp6 *ipc_next; /* in TcpUdp6 local ipc hash table */
struct tcp_udp6 *ipc_peer; /* locally connected peer(s) info */
# endif /* defined(HASEPTOPTS) */
};
#endif /* defined(HASIPv6) */
typedef struct uxsin { /* UNIX socket information */
INODETYPE inode; /* node number */
char *pcb; /* protocol control block */
char *path; /* file path */
unsigned char sb_def; /* stat(2) buffer definitions */
dev_t sb_dev; /* stat(2) buffer device */
INODETYPE sb_ino; /* stat(2) buffer node number */
dev_t sb_rdev; /* stat(2) raw device number */
uint32_t ty; /* socket type */
unsigned int opt; /* socket options */
unsigned int ss; /* socket state */
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
struct uxsin *icons; /* incoming socket connections */
unsigned int icstat; /* incoming connection status
* 0 == none */
pxinfo_t *pxinfo; /* inode information */
struct uxsin *peer; /* connected peer(s) info */
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
struct uxsin *next;
} uxsin_t;
/*
* Local static values
*/
static char *AX25path = (char *)NULL; /* path to AX25 /proc information */
/* AX25 socket info, hashed by inode */
static struct ax25sin **AX25sin = (struct ax25sin **)NULL;
static char *ax25st[] = {
"LISTENING", /* 0 */
"SABM SENT", /* 1 */
"DISC SENT", /* 2 */
"ESTABLISHED", /* 3 */
"RECOVERY" /* 4 */
};
#define NAX25ST (sizeof(ax25st) / sizeof(char *))
static char *ICMPpath = (char *)NULL; /* path to ICMP /proc information */
/* ICMP socket info, hashed by inode */
static struct icmpin **Icmpin = (struct icmpin **)NULL;
static char *Ipxpath = (char *)NULL; /* path to IPX /proc information */
/* IPX socket info, hashed by inode */
static struct ipxsin **Ipxsin = (struct ipxsin **)NULL;
static char *Nlkpath = (char *)NULL; /* path to Netlink /proc information */
/* Netlink socket info, hashed by
* inode */
static struct nlksin **Nlksin = (struct nlksin **)NULL;
/* packet info, hashed by inode */
static struct packin **Packin = (struct packin **)NULL;
static char *Packpath = (char *)NULL; /* path to packet /proc information */
static char *Rawpath = (char *)NULL; /* path to raw socket /proc
* information */
/* raw socket info, hashed by inode */
static struct rawsin **Rawsin = (struct rawsin **)NULL;
static char *SCTPPath[] = {
/* paths to /proc/net STCP info */
(char *)NULL, /* 0 = /proc/net/sctp/assocs */
(char *)NULL /* 1 = /proc/net/sctp/eps */
};
#define NSCTPPATHS sizeof(SCTPPath) / sizeof(char *)
static char *SCTPSfx[] = {
/* /proc/net suffixes */
"sctp/assocs", /* 0 = /proc/net/sctp/assocs */
"sctp/eps" /* 1 = /proc/net/sctp/eps */
};
/* SCTP info, hashed by inode */
static struct sctpsin **SCTPsin = (struct sctpsin **)NULL;
/* path to /proc/net socket status */
static char *SockStatPath = (char *)NULL;
static char *TCPpath = (char *)NULL; /* path to TCP /proc information */
/* IPv4 TCP & UDP info, hashed by
* inode */
static struct tcp_udp **TcpUdp = (struct tcp_udp **)NULL;
static int TcpUdp_bucks = 0; /* dynamically sized hash bucket
* count for TCP and UDP -- will
* be a power of two */
#if defined(HASEPTOPTS)
/* IPv4 TCP & UDP info for socket used
for IPC, hashed by (addr, port paris
and protocol */
static struct tcp_udp **TcpUdpIPC = (struct tcp_udp **)NULL;
#endif /* defined(HASEPTOPTS) */
#if defined(HASIPv6)
static char *Raw6path = (char *)NULL; /* path to raw IPv6 /proc information */
/* IPv6 raw socket info, hashed by
* inode */
static struct rawsin **Rawsin6 = (struct rawsin **)NULL;
/* path to /proc/net IPv6 socket
* status */
static char *SockStatPath6 = (char *)NULL;
static char *TCP6path = (char *)NULL; /* path to IPv6 TCP /proc information */
/* IPv6 TCP & UDP info, hashed by
* inode */
static struct tcp_udp6 **TcpUdp6 = (struct tcp_udp6 **)NULL;
static int TcpUdp6_bucks = 0; /* dynamically sized hash bucket
* count for IPv6 TCP and UDP -- will
* be a power of two */
static char *UDP6path = (char *)NULL; /* path to IPv6 UDP /proc information */
/* path to IPv6 UDPLITE /proc
* information */
static char *UDPLITE6path = (char *)NULL;
# if defined(HASEPTOPTS)
/* IPv4 TCP & UDP info for socket used
for IPC, hashed by (addr, port paris
and protocol */
static struct tcp_udp6 **TcpUdp6IPC = (struct tcp_udp6 **)NULL;
# endif /* defined(HASEPTOPTS) */
#endif /* defined(HASIPv6) */
static char *UDPpath = (char *)NULL; /* path to UDP /proc information */
/* path to UDPLITE /proc information */
static char *UDPLITEpath = (char *)NULL;
static char *UNIXpath = (char *)NULL; /* path to UNIX /proc information */
/* UNIX socket info, hashed by inode */
static uxsin_t **Uxsin = (uxsin_t **)NULL;
/*
* Local function prototypes
*/
static struct ax25sin *check_ax25(struct lsof_context *ctx, INODETYPE i);
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
static void enter_uxsinfo(struct lsof_context *ctx, uxsin_t *up);
static void fill_uxicino(struct lsof_context *ctx, INODETYPE si, INODETYPE sc);
static void fill_uxpino(struct lsof_context *ctx, INODETYPE si, INODETYPE pi);
static int get_diagmsg(int sockfd);
static void get_uxpeeri(struct lsof_context *ctx);
static void parse_diag(struct lsof_context *ctx, struct unix_diag_msg *dm,
int len);
static void prt_uxs(struct lsof_context *ctx, uxsin_t *p, int mk);
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
#if defined(HASEPTOPTS)
static void enter_netsinfo(struct lsof_context *ctx, struct tcp_udp *tp);
static void get_netpeeri(struct lsof_context *ctx);
#endif /* defined(HASEPTOPTS) */
#if defined(HASIPv6)
# if defined(HASEPTOPTS)
static void enter_nets6info(struct lsof_context *ctx, struct tcp_udp6 *tp);
static void get_net6peeri(struct lsof_context *ctx);
# endif /* defined(HASEPTOPTS) */
#endif /* defined(HASIPv6) */
static struct icmpin *check_icmp(struct lsof_context *ctx, INODETYPE i);
static struct ipxsin *check_ipx(struct lsof_context *ctx, INODETYPE i);
static struct nlksin *check_netlink(struct lsof_context *ctx, INODETYPE i);
static struct packin *check_pack(struct lsof_context *ctx, INODETYPE i);
static struct rawsin *check_raw(struct lsof_context *ctx, INODETYPE i);
static struct sctpsin *check_sctp(struct lsof_context *ctx, INODETYPE i);
static struct tcp_udp *check_tcpudp(struct lsof_context *ctx, INODETYPE i,
char **p);
static uxsin_t *check_unix(struct lsof_context *ctx, INODETYPE i);
static void get_ax25(struct lsof_context *ctx, char *p);
static void get_icmp(struct lsof_context *ctx, char *p);
static void get_ipx(struct lsof_context *ctx, char *p);
static void get_netlink(struct lsof_context *ctx, char *p);
static void get_pack(struct lsof_context *ctx, char *p);
static void get_raw(struct lsof_context *ctx, char *p);
static void get_sctp(struct lsof_context *ctx);
static char *get_sctpaddrs(char **fp, int i, int nf, int *x);
static void get_tcpudp(struct lsof_context *ctx, char *p, int pr, int clr);
static void get_unix(struct lsof_context *ctx, char *p);
static int isainb(char *a, char *b);
static void print_ax25info(struct lsof_context *ctx, struct ax25sin *ap);
static void print_ipxinfo(struct lsof_context *ctx, struct ipxsin *ip);
static char *socket_type_to_str(uint32_t ty, int *rf);
static char *netlink_proto_to_str(unsigned int pr);
#if defined(HASSOSTATE)
static char *socket_state_to_str(struct lsof_context *ctx, unsigned int ss);
#endif /* defined(HASSOSTATE) */
static char *ethernet_proto_to_str(unsigned int pr);
#if defined(HASIPv6)
static struct rawsin *check_raw6(struct lsof_context *ctx, INODETYPE i);
static struct tcp_udp6 *check_tcpudp6(struct lsof_context *ctx, INODETYPE i,
char **p);
static void get_raw6(struct lsof_context *ctx, char *p);
static void get_tcpudp6(struct lsof_context *ctx, char *p, int pr, int clr);
static int hex_ipv6_to_in6(char *as, struct in6_addr *ad);
#endif /* defined(HASIPv6) */
/*
* build_IPstates() -- build the TCP and UDP state tables
*/
void build_IPstates(struct lsof_context *ctx) {
if (!TcpSt) {
(void)enter_IPstate(ctx, "TCP", "ESTABLISHED", TCP_ESTABLISHED);
(void)enter_IPstate(ctx, "TCP", "SYN_SENT", TCP_SYN_SENT);
(void)enter_IPstate(ctx, "TCP", "SYN_RECV", TCP_SYN_RECV);
(void)enter_IPstate(ctx, "TCP", "FIN_WAIT1", TCP_FIN_WAIT1);
(void)enter_IPstate(ctx, "TCP", "FIN_WAIT2", TCP_FIN_WAIT2);
(void)enter_IPstate(ctx, "TCP", "TIME_WAIT", TCP_TIME_WAIT);
(void)enter_IPstate(ctx, "TCP", "CLOSE", TCP_CLOSE);
(void)enter_IPstate(ctx, "TCP", "CLOSE_WAIT", TCP_CLOSE_WAIT);
(void)enter_IPstate(ctx, "TCP", "LAST_ACK", TCP_LAST_ACK);
(void)enter_IPstate(ctx, "TCP", "LISTEN", TCP_LISTEN);
(void)enter_IPstate(ctx, "TCP", "CLOSING", TCP_CLOSING);
(void)enter_IPstate(ctx, "TCP", "CLOSED", 0);
(void)enter_IPstate(ctx, "TCP", (char *)NULL, 0);
}
if (!UdpSt) {
/*
* Linux /proc/net/udp reuses TCP state enum values:
* TCP_ESTABLISHED (1) for connected UDP sockets,
* TCP_CLOSE (7) for unconnected ones.
* Only register ESTABLISHED — unconnected (CLOSE) is the
* default state and not useful to display.
*/
(void)enter_IPstate(ctx, "UDP", "ESTABLISHED", TCP_ESTABLISHED);
(void)enter_IPstate(ctx, "UDP", (char *)NULL, 0);
}
}
/*
* check_ax25() - check for AX25 socket file
*/
static struct ax25sin *check_ax25(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(AX25sin, INOHASH, struct ax25sin, inode, i);
}
/*
* check_icmp() - check for ICMP socket
*/
static struct icmpin *check_icmp(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Icmpin, INOHASH, struct icmpin, inode, i);
}
/*
* check_ipx() - check for IPX socket file
*/
static struct ipxsin *check_ipx(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Ipxsin, INOHASH, struct ipxsin, inode, i);
}
/*
* check_netlink() - check for Netlink socket file
*/
static struct nlksin *
check_netlink(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Nlksin, INOHASH, struct nlksin, inode, i);
}
/*
* check_pack() - check for packet file
*/
static struct packin *check_pack(struct lsof_context *ctx,
INODETYPE i) /* packet file's inode number */
{
return HASH_FIND_ELEMENT(Packin, INOHASH, struct packin, inode, i);
}
/*
* check_raw() - check for raw socket file
*/
static struct rawsin *check_raw(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Rawsin, INOHASH, struct rawsin, inode, i);
}
/*
* check_sctp() - check for SCTP socket file
*/
static struct sctpsin *check_sctp(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(SCTPsin, INOHASH, struct sctpsin, inode, i);
}
/*
* check_tcpudp() - check for IPv4 TCP or UDP socket file
*/
static struct tcp_udp *
check_tcpudp(struct lsof_context *ctx,
INODETYPE i, /* socket file's inode number */
char **p) /* protocol return */
{
struct tcp_udp *tp;
tp = HASH_FIND_ELEMENT(TcpUdp, TCPUDPHASH, struct tcp_udp, inode, i);
if (tp) {
switch (tp->proto) {
case 0:
*p = "TCP";
break;
case 1:
*p = "UDP";
break;
case 2:
*p = "UDPLITE";
break;
default:
*p = "unknown";
}
}
return tp;
}
/*
* check_inet() - check for locally used INET domain socket
*/
static struct tcp_udp *check_inet(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(TcpUdp, TCPUDPHASH, struct tcp_udp, inode, i);
}
/*
* clear_netsinfo -- clear allocated INET socket info
*/
void clear_netsinfo(struct lsof_context *ctx) {
int h; /* hash index */
struct tcp_udp *ti, *tp; /* temporary pointers */
#if defined(HASEPTOPTS)
pxinfo_t *pp, *pnp;
#endif /* defined(HASEPTOPTS) */
if (TcpUdp) {
for (h = 0; h < TcpUdp_bucks; h++) {
if ((ti = TcpUdp[h])) {
do {
tp = ti->next;
#if defined(HASEPTOPTS)
for (pp = ti->pxinfo; pp; pp = pnp) {
pnp = pp->next;
(void)free((FREE_P *)pp);
}
#endif /* defined(HASEPTOPTS) */
(void)free((FREE_P *)ti);
ti = tp;
} while (ti);
TcpUdp[h] = (struct tcp_udp *)NULL;
}
}
}
if (TcpUdpIPC) {
for (h = 0; h < IPCBUCKS; h++)
TcpUdpIPC[h] = (struct tcp_udp *)NULL;
}
}
#if defined(HASIPv6)
/*
* check_raw6() - check for raw IPv6 socket file
*/
static struct rawsin *check_raw6(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Rawsin6, INOHASH, struct rawsin, inode, i);
}
/*
* check_tcpudp6() - check for IPv6 TCP or UDP socket file
*/
static struct tcp_udp6 *
check_tcpudp6(struct lsof_context *ctx,
INODETYPE i, /* socket file's inode number */
char **p) /* protocol return */
{
struct tcp_udp6 *tp6;
tp6 = HASH_FIND_ELEMENT(TcpUdp6, TCPUDP6HASH, struct tcp_udp6, inode, i);
if (tp6) {
switch (tp6->proto) {
case 0:
*p = "TCP";
break;
case 1:
*p = "UDP";
break;
case 2:
*p = "UDPLITE";
break;
default:
*p = "unknown";
}
}
return tp6;
}
/*
* check_inet6() - check for locally used INET6 domain socket
*/
static struct tcp_udp6 *
check_inet6(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(TcpUdp6, TCPUDP6HASH, struct tcp_udp6, inode, i);
}
/*
* clear_nets6info -- clear allocated INET6 socket info
*/
void clear_nets6info(struct lsof_context *ctx) {
int h; /* hash index */
struct tcp_udp6 *ti, *tp; /* temporary pointers */
# if defined(HASEPTOPTS)
pxinfo_t *pp, *pnp;
# endif /* defined(HASEPTOPTS) */
if (TcpUdp6) {
for (h = 0; h < TcpUdp6_bucks; h++) {
if ((ti = TcpUdp6[h])) {
do {
tp = ti->next;
# if defined(HASEPTOPTS)
for (pp = ti->pxinfo; pp; pp = pnp) {
pnp = pp->next;
(void)free((FREE_P *)pp);
}
# endif /* defined(HASEPTOPTS) */
(void)free((FREE_P *)ti);
ti = tp;
} while (ti);
TcpUdp6[h] = (struct tcp_udp6 *)NULL;
}
}
}
if (TcpUdp6IPC) {
for (h = 0; h < IPCBUCKS; h++)
TcpUdp6IPC[h] = (struct tcp_udp6 *)NULL;
}
}
#endif /* defined(HASIPv6) */
/*
* check_unix() - check for UNIX domain socket
*/
static uxsin_t *check_unix(struct lsof_context *ctx,
INODETYPE i) /* socket file's inode number */
{
return HASH_FIND_ELEMENT(Uxsin, INOHASH, uxsin_t, inode, i);
}
/*
* clear_uxsinfo -- clear allocated UNIX socket info
*/
void clear_uxsinfo(struct lsof_context *ctx) {
int h; /* hash index */
uxsin_t *ui, *up; /* remporary pointers */
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
pxinfo_t *pp, *pnp;
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
if (!Uxsin)
return;
for (h = 0; h < INOBUCKS; h++) {
if ((ui = Uxsin[h])) {
do {
up = ui->next;
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
for (pp = ui->pxinfo; pp; pp = pnp) {
pnp = pp->next;
(void)free((FREE_P *)pp);
}
#endif /* defined(HASEPTOPTS) && defined(HASUXSOCKEPT) */
if (ui->path)
(void)free((FREE_P *)ui->path);
if (ui->pcb)
(void)free((FREE_P *)ui->pcb);
(void)free((FREE_P *)ui);
ui = up;
} while (ui);
Uxsin[h] = (uxsin_t *)NULL;
}
}
}
/*
* get_ax25() - get /proc/net/ax25 info
*/
static void get_ax25(struct lsof_context *ctx,
char *p) /* /proc/net/ax25 path */
{
struct ax25sin *ap, *np;
FILE *as;
char buf[MAXPATHLEN], *da, *dev_ch, *ep, **fp, *sa;
int h;
INODETYPE inode;
unsigned long rq, sq, state;
MALLOC_S len;
unsigned char rqs, sqs;
static char *vbuf = (char *)NULL;
static size_t vsz = (size_t)0;
/*
* Do second time cleanup or first time setup.
*/
if (AX25sin) {
for (h = 0; h < INOBUCKS; h++) {
for (ap = AX25sin[h]; ap; ap = np) {
np = ap->next;
if (ap->da)
(void)free((FREE_P *)ap->da);
if (ap->dev_ch)
(void)free((FREE_P *)ap->dev_ch);
if (ap->sa)
(void)free((FREE_P *)ap->sa);
(void)free((FREE_P *)ap);
}
AX25sin[h] = (struct ax25sin *)NULL;
}
} else {
AX25sin = (struct ax25sin **)calloc(INOBUCKS, sizeof(struct ax25sin *));
if (!AX25sin) {
(void)fprintf(stderr,
"%s: can't allocate %d AX25 hash pointer bytes\n", Pn,
(int)(INOBUCKS * sizeof(struct ax25sin *)));
Error(ctx);
}
}
/*
* Open the /proc/net/ax25 file, assign a page size buffer to the stream,
* and read it. Store AX25 socket info in the AX25sin[] hash buckets.
*/
if (!(as = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
return;
while (fgets(buf, sizeof(buf) - 1, as)) {
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 24)
continue;
/*
* /proc/net/ax25 has no title line, a very poor deficiency in its
* implementation.
*
* The ax25_info_show() function in kern module .../net/ax25/af_ax25.c
* says the format of the lines in the file is:
*
* magic dev src_addr dest_addr,digi1,digi2,.. st vs vr va t1 t1 \
* t2 t2 t3 t3 idle idle n2 n2 rtt window paclen Snd-Q Rcv-Q \
* inode
*
* The code in this function is forced to assume that format is in
* effect..
*/
/*
* Assemble the inode number and see if it has already been recorded.
* If it has, skip this line.
*/
ep = (char *)NULL;
if (!fp[23] || !*fp[23] ||
(inode = strtoull(fp[23], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/* Skip if already exists in hash table */
if (HASH_FIND_ELEMENT(AX25sin, INOHASH, struct ax25sin, inode, inode))
continue;
/*
* Assemble the send and receive queue values and the state.
*/
ep = (char *)NULL;
if (!fp[21] || !*fp[21] ||
(sq = strtoul(fp[21], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
sqs = (unsigned char)1;
ep = (char *)NULL;
if (!fp[22] || !*fp[22] ||
(rq = strtoul(fp[22], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
rqs = (unsigned char)1;
ep = (char *)NULL;
if (!fp[4] || !*fp[4] ||
(state = strtoul(fp[4], &ep, 0)) == ULONG_MAX || !ep || *ep)
continue;
/*
* Allocate space for the destination address.
*/
if (!fp[3] || !*fp[3])
da = (char *)NULL;
else if ((len = strlen(fp[3]))) {
if (!(da = (char *)malloc(len + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d destination AX25 addr bytes: %s\n",
Pn, (int)(len + 1), fp[3]);
Error(ctx);
}
(void)snpf(da, len + 1, "%s", fp[3]);
} else
da = (char *)NULL;
/*
* Allocate space for the source address.
*/
if (!fp[2] || !*fp[2])
sa = (char *)NULL;
else if ((len = strlen(fp[2]))) {
if (!(sa = (char *)malloc(len + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d source AX25 address bytes: %s\n", Pn,
(int)(len + 1), fp[2]);
Error(ctx);
}
(void)snpf(sa, len + 1, "%s", fp[2]);
} else
sa = (char *)NULL;
/*
* Allocate space for the device characters.
*/
if (!fp[1] || !*fp[1])
dev_ch = (char *)NULL;
else if ((len = strlen(fp[1]))) {
if (!(dev_ch = (char *)malloc(len + 1))) {
(void)fprintf(
stderr,
"%s: can't allocate %d destination AX25 dev bytes: %s\n",
Pn, (int)(len + 1), fp[1]);
Error(ctx);
}
(void)snpf(dev_ch, len + 1, "%s", fp[1]);
} else
dev_ch = (char *)NULL;
/*
* Allocate space for an ax25sin entry, fill it, and link it to its
* hash bucket.
*/
if (!(ap = (struct ax25sin *)malloc(sizeof(struct ax25sin)))) {
(void)fprintf(stderr,
"%s: can't allocate %d byte ax25sin structure\n", Pn,
(int)sizeof(struct ax25sin));
Error(ctx);
}
ap->da = da;
ap->dev_ch = dev_ch;
ap->inode = inode;
ap->rq = rq;
ap->rqs = rqs;
ap->sa = sa;
ap->sq = sq;
ap->sqs = sqs;
ap->state = (int)state;
HASH_INSERT_ELEMENT(AX25sin, INOHASH, ap, inode);
}
(void)fclose(as);
}
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
/*
* enter_uxsinfo() -- enter unix socket info
* entry Lf = local file structure pointer
* Lp = local process structure pointer
*/
static void enter_uxsinfo(struct lsof_context *ctx, uxsin_t *up) {
pxinfo_t *pi; /* pxinfo_t structure pointer */
struct lfile *lf; /* local file structure pointer */
struct lproc *lp; /* local proc structure pointer */
pxinfo_t *np; /* new pxinfo_t structure pointer */
for (pi = up->pxinfo; pi; pi = pi->next) {
lf = pi->lf;
lp = &Lproc[pi->lpx];
if (pi->ino == Lf->inode) {
if ((lp->pid == Lp->pid) && (lf->fd_type == Lf->fd_type) &&
(lf->fd_num == Lf->fd_num))
return;
}
}
if (!(np = (pxinfo_t *)malloc(sizeof(pxinfo_t)))) {
(void)fprintf(stderr, "%s: no space for pipeinfo in uxsinfo, PID %d\n",
Pn, Lp->pid);
Error(ctx);
}
np->ino = Lf->inode;
np->lf = Lf;
np->lpx = Lp - Lproc;
np->next = up->pxinfo;
up->pxinfo = np;
}
/*
* fill_uxicino() -- fill incoming connection inode number
*/
static void fill_uxicino(struct lsof_context *ctx,
INODETYPE si, /* UNIX socket inode number */
INODETYPE ic) /* incoming UNIX socket connection
* inode number */
{
uxsin_t *psi; /* pointer to socket's information */
uxsin_t *pic; /* pointer to incoming connection's
* information */
if ((psi = check_unix(ctx, si))) {
if (psi->icstat || psi->icons)
return;
if ((pic = check_unix(ctx, ic))) {
psi->icstat = 1;
psi->icons = pic;
}
}
}
/*
* fill_uxpino() -- fill in UNIX socket's peer inode number
*/
static void fill_uxpino(struct lsof_context *ctx,
INODETYPE si, /* UNIX socket inode number */
INODETYPE pi) /* UNIX socket peer's inode number */
{
uxsin_t *pp, *up;
if ((up = check_unix(ctx, si))) {
if (!up->peer) {
if ((pp = check_unix(ctx, pi)))
up->peer = pp;
}
}
}
/*
* find_uxepti(lf) -- find UNIX socket endpoint info
*/
uxsin_t *find_uxepti(struct lsof_context *ctx,
struct lfile *lf) /* pipe's lfile */
{
uxsin_t *up;
up = check_unix(ctx, lf->inode);
return (up ? up->peer : (uxsin_t *)NULL);
}
/*
* get_diagmsg() -- get UNIX socket's diag message
*/
static int get_diagmsg(int sockfd) /* socket's file descriptor */
{
struct msghdr msg; /* message header */
struct nlmsghdr nlh; /* header length */
struct unix_diag_req creq; /* connection request */
struct sockaddr_nl sa; /* netlink socket address */
struct iovec iov[2]; /* I/O vector */
/*
* Build and send message to socket's file descriptor, asking for its
* diagnostic message.
*/
zeromem((char *)&msg, sizeof(msg));
zeromem((char *)&sa, sizeof(sa));
zeromem((char *)&nlh, sizeof(nlh));
zeromem((char *)&creq, sizeof(creq));
sa.nl_family = AF_NETLINK;
creq.sdiag_family = AF_UNIX;
creq.sdiag_protocol = 0;
memset((void *)&creq.udiag_states, -1, sizeof(creq.udiag_states));
creq.udiag_ino = (INODETYPE)0;
creq.udiag_show = UDIAG_SHOW_PEER | UDIAG_SHOW_ICONS;
nlh.nlmsg_len = NLMSG_LENGTH(sizeof(creq));
nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
iov[0].iov_base = (void *)&nlh;
iov[0].iov_len = sizeof(nlh);
iov[1].iov_base = (void *)&creq;
iov[1].iov_len = sizeof(creq);
msg.msg_name = (void *)&sa;
msg.msg_namelen = sizeof(sa);
msg.msg_iov = iov;
msg.msg_iovlen = 2;
return (sendmsg(sockfd, &msg, 0));
}
/*
* get_uxpeeri() - get UNIX socket peer inode information
*/
static void get_uxpeeri(struct lsof_context *ctx) {
struct unix_diag_msg *dm; /* pointer to diag message */
struct nlmsghdr *hp; /* netlink structure header pointer */
int nb = 0; /* number of bytes */
int ns = 0; /* netlink socket */
uint8_t rb[SOCKET_BUFFER_SIZE]; /* receive buffer */
int rl = 0; /* route info length */
/*
* Get a netlink socket.
*/
if ((ns = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_SOCK_DIAG)) == -1) {
(void)fprintf(stderr, "%s: netlink socket error: %s\n", Pn,
strerror(errno));
Error(ctx);
}
/*
* Request peer information.
*/