-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsocket.c
1808 lines (1452 loc) · 46.5 KB
/
socket.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
/*
chronyd/chronyc - Programs for keeping computer clocks accurate.
**********************************************************************
* Copyright (C) Richard P. Curnow 1997-2003
* Copyright (C) Timo Teras 2009
* Copyright (C) Miroslav Lichvar 2009, 2013-2020
* Copyright (C) Luke Valenta 2023
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
**********************************************************************
=======================================================================
This file implements socket operations.
*/
#include "config.h"
#include "sysincl.h"
#ifdef HAVE_LINUX_TIMESTAMPING
#include <linux/errqueue.h>
#include <linux/net_tstamp.h>
#endif
#include "socket.h"
#include "array.h"
#include "logging.h"
#include "privops.h"
#include "ptp.h"
#include "util.h"
#define INVALID_SOCK_FD (-4)
#define CMSG_BUF_SIZE 256
union sockaddr_all {
struct sockaddr_in in4;
#ifdef FEAT_IPV6
struct sockaddr_in6 in6;
#endif
struct sockaddr_un un;
struct sockaddr sa;
};
struct Message {
union sockaddr_all name;
struct iovec iov;
/* Buffer of sufficient length for all expected messages */
struct {
/* Extra space for Ethernet, IPv4/IPv6, and UDP headers in
timestamped messages received from the Linux error queue */
uint8_t l234_headers[64];
union {
NTP_Packet ntp_msg;
PTP_NtpMessage ptp_msg;
CMD_Request cmd_request;
CMD_Reply cmd_reply;
} msg;
} msg_buf;
/* Aligned buffer for control messages */
struct cmsghdr cmsg_buf[CMSG_BUF_SIZE / sizeof (struct cmsghdr)];
};
#ifdef HAVE_RECVMMSG
#define MAX_RECV_MESSAGES 16
#define MessageHeader mmsghdr
#else
/* Compatible with mmsghdr */
struct MessageHeader {
struct msghdr msg_hdr;
unsigned int msg_len;
};
#define MAX_RECV_MESSAGES 1
#endif
static int initialised;
static int first_reusable_fd;
static int reusable_fds;
/* Flags indicating in which IP families sockets can be requested */
static int ip4_enabled;
static int ip6_enabled;
/* Flags supported by socket() */
static int supported_socket_flags;
/* Arrays of Message, MessageHeader, and SCK_Message */
static ARR_Instance recv_messages;
static ARR_Instance recv_headers;
static ARR_Instance recv_sck_messages;
static unsigned int received_messages;
static int (*priv_bind_function)(int sock_fd, struct sockaddr *address,
socklen_t address_len);
/* ================================================== */
static void
prepare_buffers(unsigned int n)
{
struct MessageHeader *hdr;
struct Message *msg;
unsigned int i;
for (i = 0; i < n; i++) {
msg = ARR_GetElement(recv_messages, i);
hdr = ARR_GetElement(recv_headers, i);
msg->iov.iov_base = &msg->msg_buf;
msg->iov.iov_len = sizeof (msg->msg_buf);
hdr->msg_hdr.msg_name = &msg->name;
hdr->msg_hdr.msg_namelen = sizeof (msg->name);
hdr->msg_hdr.msg_iov = &msg->iov;
hdr->msg_hdr.msg_iovlen = 1;
hdr->msg_hdr.msg_control = msg->cmsg_buf;
hdr->msg_hdr.msg_controllen = sizeof (msg->cmsg_buf);
hdr->msg_hdr.msg_flags = 0;
hdr->msg_len = 0;
}
}
/* ================================================== */
static const char *
domain_to_string(int domain)
{
switch (domain) {
case AF_INET:
return "IPv4";
#ifdef AF_INET6
case AF_INET6:
return "IPv6";
#endif
case AF_UNIX:
return "Unix";
case AF_UNSPEC:
return "UNSPEC";
default:
return "?";
}
}
/* ================================================== */
static int
get_reusable_socket(int type, IPSockAddr *spec)
{
#ifdef LINUX
union sockaddr_all sa;
IPSockAddr ip_sa;
int sock_fd, opt;
socklen_t l;
/* Abort early if not an IPv4/IPv6 server socket */
if (!spec || spec->ip_addr.family == IPADDR_UNSPEC || spec->port == 0)
return INVALID_SOCK_FD;
/* Loop over available reusable sockets */
for (sock_fd = first_reusable_fd; sock_fd < first_reusable_fd + reusable_fds; sock_fd++) {
/* Check that types match */
l = sizeof (opt);
if (getsockopt(sock_fd, SOL_SOCKET, SO_TYPE, &opt, &l) < 0 ||
l != sizeof (opt) || opt != type)
continue;
/* Get sockaddr for reusable socket */
l = sizeof (sa);
if (getsockname(sock_fd, &sa.sa, &l) < 0 || l < sizeof (sa_family_t))
continue;
SCK_SockaddrToIPSockAddr(&sa.sa, l, &ip_sa);
/* Check that reusable socket matches specification */
if (ip_sa.port != spec->port || UTI_CompareIPs(&ip_sa.ip_addr, &spec->ip_addr, NULL) != 0)
continue;
/* Check that STREAM socket is listening */
l = sizeof (opt);
if (type == SOCK_STREAM && (getsockopt(sock_fd, SOL_SOCKET, SO_ACCEPTCONN, &opt, &l) < 0 ||
l != sizeof (opt) || opt == 0))
continue;
#if defined(FEAT_IPV6) && defined(IPV6_V6ONLY)
if (spec->ip_addr.family == IPADDR_INET6 &&
(!SCK_GetIntOption(sock_fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt) || opt != 1))
LOG(LOGS_WARN, "Reusable IPv6 socket missing IPV6_V6ONLY option");
#endif
return sock_fd;
}
#endif
return INVALID_SOCK_FD;
}
/* ================================================== */
#if defined(SOCK_CLOEXEC) || defined(SOCK_NONBLOCK)
static int
check_socket_flag(int sock_flag, int fd_flag, int fs_flag)
{
int sock_fd, fd_flags, fs_flags;
sock_fd = socket(AF_INET, SOCK_DGRAM | sock_flag, 0);
if (sock_fd < 0)
return 0;
fd_flags = fcntl(sock_fd, F_GETFD);
fs_flags = fcntl(sock_fd, F_GETFL);
close(sock_fd);
if (fd_flags == -1 || (fd_flags & fd_flag) != fd_flag ||
fs_flags == -1 || (fs_flags & fs_flag) != fs_flag)
return 0;
return 1;
}
#endif
/* ================================================== */
static int
set_socket_nonblock(int sock_fd)
{
if (fcntl(sock_fd, F_SETFL, O_NONBLOCK) < 0) {
DEBUG_LOG("Could not set O_NONBLOCK : %s", strerror(errno));
return 0;
}
return 1;
}
/* ================================================== */
static int
get_open_flags(int flags)
{
int r = supported_socket_flags;
#ifdef SOCK_NONBLOCK
if (flags & SCK_FLAG_BLOCK)
r &= ~SOCK_NONBLOCK;
#endif
return r;
}
/* ================================================== */
static int
set_socket_flags(int sock_fd, int flags)
{
/* Close the socket automatically on exec */
if (!SCK_IsReusable(sock_fd) &&
#ifdef SOCK_CLOEXEC
(supported_socket_flags & SOCK_CLOEXEC) == 0 &&
#endif
!UTI_FdSetCloexec(sock_fd))
return 0;
/* Enable non-blocking mode */
if ((flags & SCK_FLAG_BLOCK) == 0 &&
#ifdef SOCK_NONBLOCK
(SCK_IsReusable(sock_fd) || (supported_socket_flags & SOCK_NONBLOCK) == 0) &&
#endif
!set_socket_nonblock(sock_fd))
return 0;
return 1;
}
/* ================================================== */
static int
open_socket(int domain, int type, int flags)
{
int sock_fd;
sock_fd = socket(domain, type | get_open_flags(flags), 0);
if (sock_fd < 0) {
DEBUG_LOG("Could not open %s socket : %s",
domain_to_string(domain), strerror(errno));
return INVALID_SOCK_FD;
}
if (!set_socket_flags(sock_fd, flags)) {
close(sock_fd);
return INVALID_SOCK_FD;
}
return sock_fd;
}
/* ================================================== */
static int
open_socket_pair(int domain, int type, int flags, int *other_fd)
{
int sock_fds[2];
if (socketpair(domain, type | get_open_flags(flags), 0, sock_fds) < 0) {
DEBUG_LOG("Could not open %s socket : %s",
domain_to_string(domain), strerror(errno));
return INVALID_SOCK_FD;
}
if (!set_socket_flags(sock_fds[0], flags) || !set_socket_flags(sock_fds[1], flags)) {
close(sock_fds[0]);
close(sock_fds[1]);
return INVALID_SOCK_FD;
}
*other_fd = sock_fds[1];
return sock_fds[0];
}
/* ================================================== */
static int
get_ip_socket(int domain, int type, int flags, IPSockAddr *ip_sa)
{
int sock_fd;
/* Check if there is a matching reusable socket */
sock_fd = get_reusable_socket(type, ip_sa);
if (sock_fd < 0) {
sock_fd = open_socket(domain, type, flags);
/* Unexpected, but make sure the new socket is not in the reusable range */
if (SCK_IsReusable(sock_fd))
LOG_FATAL("Could not open %s socket : file descriptor in reusable range",
domain_to_string(domain));
} else {
/* Set socket flags on reusable socket */
if (!set_socket_flags(sock_fd, flags))
return INVALID_SOCK_FD;
}
return sock_fd;
}
/* ================================================== */
static int
set_socket_options(int sock_fd, int flags)
{
/* Make the socket capable of sending broadcast packets if requested */
if (flags & SCK_FLAG_BROADCAST && !SCK_SetIntOption(sock_fd, SOL_SOCKET, SO_BROADCAST, 1))
;
return 1;
}
/* ================================================== */
static int
set_ip_options(int sock_fd, int family, int flags)
{
#if defined(FEAT_IPV6) && defined(IPV6_V6ONLY)
/* Receive only IPv6 packets on an IPv6 socket, but do not attempt
to set this option on pre-initialised reuseable sockets */
if (family == IPADDR_INET6 && !SCK_IsReusable(sock_fd) &&
!SCK_SetIntOption(sock_fd, IPPROTO_IPV6, IPV6_V6ONLY, 1))
return 0;
#endif
/* Provide destination address of received packets if requested */
if (flags & SCK_FLAG_RX_DEST_ADDR) {
if (family == IPADDR_INET4) {
#ifdef HAVE_IN_PKTINFO
if (!SCK_SetIntOption(sock_fd, IPPROTO_IP, IP_PKTINFO, 1))
;
#elif defined(IP_RECVDSTADDR)
if (!SCK_SetIntOption(sock_fd, IPPROTO_IP, IP_RECVDSTADDR, 1))
;
#endif
}
#ifdef FEAT_IPV6
else if (family == IPADDR_INET6) {
#ifdef HAVE_IN6_PKTINFO
#ifdef IPV6_RECVPKTINFO
if (!SCK_SetIntOption(sock_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, 1))
;
#else
if (!SCK_SetIntOption(sock_fd, IPPROTO_IPV6, IPV6_PKTINFO, 1))
;
#endif
#endif
}
#endif
}
return 1;
}
/* ================================================== */
static int
is_any_address(IPAddr *addr)
{
IPAddr any_addr;
SCK_GetAnyLocalIPAddress(addr->family, &any_addr);
return UTI_CompareIPs(&any_addr, addr, NULL) == 0;
}
/* ================================================== */
static int
bind_device(int sock_fd, const char *iface)
{
#ifdef SO_BINDTODEVICE
if (setsockopt(sock_fd, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) < 0) {
DEBUG_LOG("Could not bind socket to %s : %s", iface, strerror(errno));
return 0;
}
return 1;
#else
DEBUG_LOG("Could not bind socket to %s : %s", iface, "Not supported");
return 0;
#endif
}
/* ================================================== */
static int
bind_ip_address(int sock_fd, IPSockAddr *addr, int flags)
{
union sockaddr_all saddr;
socklen_t saddr_len;
int s;
/* Make the socket capable of re-using an old address if binding to a specific port */
if (addr->port > 0 && !SCK_SetIntOption(sock_fd, SOL_SOCKET, SO_REUSEADDR, 1))
;
#if defined(LINUX) && defined(SO_REUSEPORT)
/* Allow multiple instances to bind to the same port in order to enable load
balancing. Don't enable this option on non-Linux systems as it has
a slightly different meaning there (with some important implications). */
if (addr->port > 0 && !SCK_SetIntOption(sock_fd, SOL_SOCKET, SO_REUSEPORT, 1))
;
#endif
#ifdef IP_FREEBIND
/* Allow binding to an address that doesn't exist yet */
if (!SCK_SetIntOption(sock_fd, IPPROTO_IP, IP_FREEBIND, 1))
;
#endif
/* Do not attempt to bind pre-initialised reusable socket */
if (SCK_IsReusable(sock_fd))
return 1;
saddr_len = SCK_IPSockAddrToSockaddr(addr, (struct sockaddr *)&saddr, sizeof (saddr));
if (saddr_len == 0)
return 0;
if (flags & SCK_FLAG_PRIV_BIND && priv_bind_function)
s = priv_bind_function(sock_fd, &saddr.sa, saddr_len);
else
s = bind(sock_fd, &saddr.sa, saddr_len);
if (s < 0) {
DEBUG_LOG("Could not bind socket to %s : %s",
UTI_IPSockAddrToString(addr), strerror(errno));
return 0;
}
return 1;
}
/* ================================================== */
static int
connect_ip_address(int sock_fd, IPSockAddr *addr)
{
union sockaddr_all saddr;
socklen_t saddr_len;
saddr_len = SCK_IPSockAddrToSockaddr(addr, (struct sockaddr *)&saddr, sizeof (saddr));
if (saddr_len == 0)
return 0;
if (connect(sock_fd, &saddr.sa, saddr_len) < 0 && errno != EINPROGRESS) {
DEBUG_LOG("Could not connect socket to %s : %s",
UTI_IPSockAddrToString(addr), strerror(errno));
return 0;
}
return 1;
}
/* ================================================== */
static int
open_ip_socket(IPSockAddr *remote_addr, IPSockAddr *local_addr, const char *iface,
int type, int flags)
{
int domain, family, sock_fd;
if (local_addr)
family = local_addr->ip_addr.family;
else if (remote_addr)
family = remote_addr->ip_addr.family;
else
family = IPADDR_INET4;
switch (family) {
case IPADDR_INET4:
if (!ip4_enabled)
return INVALID_SOCK_FD;
domain = AF_INET;
break;
#ifdef FEAT_IPV6
case IPADDR_INET6:
if (!ip6_enabled)
return INVALID_SOCK_FD;
domain = AF_INET6;
break;
#endif
default:
DEBUG_LOG("Unspecified family");
return INVALID_SOCK_FD;
}
sock_fd = get_ip_socket(domain, type, flags, local_addr);
if (sock_fd < 0)
return INVALID_SOCK_FD;
if (!set_socket_options(sock_fd, flags))
goto error;
if (!set_ip_options(sock_fd, family, flags))
goto error;
if (iface && !bind_device(sock_fd, iface))
goto error;
/* Bind the socket if a non-any local address/port was specified */
if (local_addr && local_addr->ip_addr.family != IPADDR_UNSPEC &&
(local_addr->port != 0 || !is_any_address(&local_addr->ip_addr)) &&
!bind_ip_address(sock_fd, local_addr, flags))
goto error;
/* Connect the socket if a remote address was specified */
if (remote_addr && remote_addr->ip_addr.family != IPADDR_UNSPEC &&
!connect_ip_address(sock_fd, remote_addr))
goto error;
if (remote_addr || local_addr)
DEBUG_LOG("%s %s%s socket fd=%d%s%s%s%s",
SCK_IsReusable(sock_fd) ? "Reusing" : "Opened",
type == SOCK_DGRAM ? "UDP" : type == SOCK_STREAM ? "TCP" : "?",
family == IPADDR_INET4 ? "v4" : "v6",
sock_fd,
remote_addr ? " remote=" : "",
remote_addr ? UTI_IPSockAddrToString(remote_addr) : "",
local_addr ? " local=" : "",
local_addr ? UTI_IPSockAddrToString(local_addr) : "");
return sock_fd;
error:
SCK_CloseSocket(sock_fd);
return INVALID_SOCK_FD;
}
/* ================================================== */
static socklen_t
set_unix_sockaddr(struct sockaddr_un *sun, const char *addr)
{
size_t len = strlen(addr);
if (len + 1 > sizeof (sun->sun_path)) {
DEBUG_LOG("Unix socket path %s too long", addr);
return 0;
}
memset(sun, 0, sizeof (*sun));
sun->sun_family = AF_UNIX;
memcpy(sun->sun_path, addr, len);
return offsetof(struct sockaddr_un, sun_path) + len + 1;
}
/* ================================================== */
static int
bind_unix_address(int sock_fd, const char *addr, int flags)
{
union sockaddr_all saddr;
socklen_t saddr_len;
saddr_len = set_unix_sockaddr(&saddr.un, addr);
if (saddr_len == 0)
return 0;
if (unlink(addr) < 0)
DEBUG_LOG("Could not remove %s : %s", addr, strerror(errno));
/* PRV_BindSocket() doesn't support Unix sockets yet */
if (bind(sock_fd, &saddr.sa, saddr_len) < 0) {
DEBUG_LOG("Could not bind Unix socket to %s : %s", addr, strerror(errno));
return 0;
}
/* Allow access to everyone with access to the directory if requested */
if (flags & SCK_FLAG_ALL_PERMISSIONS && chmod(addr, 0666) < 0) {
DEBUG_LOG("Could not change permissions of %s : %s", addr, strerror(errno));
return 0;
}
return 1;
}
/* ================================================== */
static int
connect_unix_address(int sock_fd, const char *addr)
{
union sockaddr_all saddr;
socklen_t saddr_len;
saddr_len = set_unix_sockaddr(&saddr.un, addr);
if (saddr_len == 0)
return 0;
if (connect(sock_fd, &saddr.sa, saddr_len) < 0) {
DEBUG_LOG("Could not connect Unix socket to %s : %s", addr, strerror(errno));
return 0;
}
return 1;
}
/* ================================================== */
static int
open_unix_socket(const char *remote_addr, const char *local_addr, int type, int flags)
{
int sock_fd;
sock_fd = open_socket(AF_UNIX, type, flags);
if (sock_fd < 0)
return INVALID_SOCK_FD;
if (!set_socket_options(sock_fd, flags))
goto error;
/* Bind the socket if a local address was specified */
if (local_addr && !bind_unix_address(sock_fd, local_addr, flags))
goto error;
/* Connect the socket if a remote address was specified */
if (remote_addr && !connect_unix_address(sock_fd, remote_addr))
goto error;
DEBUG_LOG("Opened Unix socket fd=%d%s%s%s%s",
sock_fd,
remote_addr ? " remote=" : "", remote_addr ? remote_addr : "",
local_addr ? " local=" : "", local_addr ? local_addr : "");
return sock_fd;
error:
SCK_RemoveSocket(sock_fd);
SCK_CloseSocket(sock_fd);
return INVALID_SOCK_FD;
}
/* ================================================== */
static int
open_unix_socket_pair(int type, int flags, int *other_fd)
{
int sock_fd;
sock_fd = open_socket_pair(AF_UNIX, type, flags, other_fd);
if (sock_fd < 0)
return INVALID_SOCK_FD;
DEBUG_LOG("Opened Unix socket pair fd1=%d fd2=%d", sock_fd, *other_fd);
return sock_fd;
}
/* ================================================== */
static int
get_recv_flags(int flags)
{
int recv_flags = 0;
if (flags & SCK_FLAG_MSG_ERRQUEUE) {
#ifdef MSG_ERRQUEUE
recv_flags |= MSG_ERRQUEUE;
#else
assert(0);
#endif
}
return recv_flags;
}
/* ================================================== */
static void
handle_recv_error(int sock_fd, int flags)
{
#ifdef MSG_ERRQUEUE
/* If reading from the error queue failed, the select() exception should
be for a socket error. Clear the error to avoid a busy loop. */
if (flags & SCK_FLAG_MSG_ERRQUEUE) {
int error = 0;
if (SCK_GetIntOption(sock_fd, SOL_SOCKET, SO_ERROR, &error))
errno = error;
}
#endif
DEBUG_LOG("Could not receive message fd=%d : %s", sock_fd, strerror(errno));
}
/* ================================================== */
static void
log_message(int sock_fd, int direction, SCK_Message *message, const char *prefix,
const char *error)
{
const char *local_addr, *remote_addr;
char if_index[20], tss[10], tsif[20], tslen[20];
if (DEBUG <= 0 || log_min_severity > LOGS_DEBUG)
return;
remote_addr = NULL;
local_addr = NULL;
if_index[0] = '\0';
tss[0] = '\0';
tsif[0] = '\0';
tslen[0] = '\0';
switch (message->addr_type) {
case SCK_ADDR_IP:
if (message->remote_addr.ip.ip_addr.family != IPADDR_UNSPEC)
remote_addr = UTI_IPSockAddrToString(&message->remote_addr.ip);
if (message->local_addr.ip.family != IPADDR_UNSPEC)
local_addr = UTI_IPToString(&message->local_addr.ip);
break;
case SCK_ADDR_UNIX:
remote_addr = message->remote_addr.path;
break;
default:
break;
}
if (message->if_index != INVALID_IF_INDEX)
snprintf(if_index, sizeof (if_index), " if=%d", message->if_index);
if (direction > 0) {
if (!UTI_IsZeroTimespec(&message->timestamp.kernel) ||
!UTI_IsZeroTimespec(&message->timestamp.hw))
snprintf(tss, sizeof (tss), " tss=%s%s",
!UTI_IsZeroTimespec(&message->timestamp.kernel) ? "K" : "",
!UTI_IsZeroTimespec(&message->timestamp.hw) ? "H" : "");
if (message->timestamp.if_index != INVALID_IF_INDEX)
snprintf(tsif, sizeof (tsif), " tsif=%d", message->timestamp.if_index);
if (message->timestamp.l2_length != 0)
snprintf(tslen, sizeof (tslen), " tslen=%d", message->timestamp.l2_length);
}
DEBUG_LOG("%s message%s%s%s%s fd=%d len=%d%s%s%s%s%s%s",
prefix,
remote_addr ? (direction > 0 ? " from " : " to ") : "",
remote_addr ? remote_addr : "",
local_addr ? (direction > 0 ? " to " : " from ") : "",
local_addr ? local_addr : "",
sock_fd, message->length, if_index,
tss, tsif, tslen,
error ? " : " : "", error ? error : "");
}
/* ================================================== */
static void
init_message_addresses(SCK_Message *message, SCK_AddressType addr_type)
{
message->addr_type = addr_type;
switch (addr_type) {
case SCK_ADDR_UNSPEC:
break;
case SCK_ADDR_IP:
message->remote_addr.ip.ip_addr.family = IPADDR_UNSPEC;
message->remote_addr.ip.port = 0;
message->local_addr.ip.family = IPADDR_UNSPEC;
break;
case SCK_ADDR_UNIX:
message->remote_addr.path = NULL;
break;
default:
assert(0);
}
}
/* ================================================== */
static void
init_message_nonaddress(SCK_Message *message)
{
message->data = NULL;
message->length = 0;
message->if_index = INVALID_IF_INDEX;
UTI_ZeroTimespec(&message->timestamp.kernel);
UTI_ZeroTimespec(&message->timestamp.hw);
message->timestamp.if_index = INVALID_IF_INDEX;
message->timestamp.l2_length = 0;
message->timestamp.tx_flags = 0;
message->descriptor = INVALID_SOCK_FD;
}
/* ================================================== */
static int
match_cmsg(struct cmsghdr *cmsg, int level, int type, size_t length)
{
if (cmsg->cmsg_type == type && cmsg->cmsg_level == level &&
(length == 0 || cmsg->cmsg_len == CMSG_LEN(length)))
return 1;
return 0;
}
/* ================================================== */
static int
process_header(struct msghdr *msg, int msg_length, int sock_fd, int flags,
SCK_Message *message)
{
struct cmsghdr *cmsg;
int r = 1;
if (msg->msg_namelen <= sizeof (union sockaddr_all) &&
msg->msg_namelen > sizeof (((struct sockaddr *)msg->msg_name)->sa_family)) {
switch (((struct sockaddr *)msg->msg_name)->sa_family) {
case AF_INET:
#ifdef FEAT_IPV6
case AF_INET6:
#endif
init_message_addresses(message, SCK_ADDR_IP);
SCK_SockaddrToIPSockAddr(msg->msg_name, msg->msg_namelen, &message->remote_addr.ip);
break;
case AF_UNIX:
init_message_addresses(message, SCK_ADDR_UNIX);
message->remote_addr.path = ((struct sockaddr_un *)msg->msg_name)->sun_path;
break;
default:
init_message_addresses(message, SCK_ADDR_UNSPEC);
DEBUG_LOG("Unexpected address");
r = 0;
break;
}
} else {
init_message_addresses(message, SCK_ADDR_UNSPEC);
if (msg->msg_namelen > sizeof (union sockaddr_all)) {
DEBUG_LOG("Truncated source address");
r = 0;
}
}
init_message_nonaddress(message);
if (msg->msg_iovlen == 1) {
message->data = msg->msg_iov[0].iov_base;
message->length = msg_length;
} else {
DEBUG_LOG("Unexpected iovlen");
r = 0;
}
if (msg->msg_flags & MSG_TRUNC) {
log_message(sock_fd, 1, message, "Truncated", NULL);
r = 0;
}
if (msg->msg_flags & MSG_CTRUNC) {
log_message(sock_fd, 1, message, "Truncated cmsg in", NULL);
r = 0;
}
for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
if (0) {
}
#ifdef HAVE_IN_PKTINFO
else if (match_cmsg(cmsg, IPPROTO_IP, IP_PKTINFO, sizeof (struct in_pktinfo))) {
struct in_pktinfo ipi;
if (message->addr_type != SCK_ADDR_IP)
init_message_addresses(message, SCK_ADDR_IP);
memcpy(&ipi, CMSG_DATA(cmsg), sizeof (ipi));
message->local_addr.ip.addr.in4 = ntohl(ipi.ipi_addr.s_addr);
message->local_addr.ip.family = IPADDR_INET4;
message->if_index = ipi.ipi_ifindex;
}
#elif defined(IP_RECVDSTADDR)
else if (match_cmsg(cmsg, IPPROTO_IP, IP_RECVDSTADDR, sizeof (struct in_addr))) {
struct in_addr addr;
if (message->addr_type != SCK_ADDR_IP)
init_message_addresses(message, SCK_ADDR_IP);
memcpy(&addr, CMSG_DATA(cmsg), sizeof (addr));
message->local_addr.ip.addr.in4 = ntohl(addr.s_addr);
message->local_addr.ip.family = IPADDR_INET4;
}
#endif
#ifdef HAVE_IN6_PKTINFO
else if (match_cmsg(cmsg, IPPROTO_IPV6, IPV6_PKTINFO, sizeof (struct in6_pktinfo))) {
struct in6_pktinfo ipi;
if (message->addr_type != SCK_ADDR_IP)
init_message_addresses(message, SCK_ADDR_IP);
memcpy(&ipi, CMSG_DATA(cmsg), sizeof (ipi));
memcpy(&message->local_addr.ip.addr.in6, &ipi.ipi6_addr.s6_addr,
sizeof (message->local_addr.ip.addr.in6));
message->local_addr.ip.family = IPADDR_INET6;
message->if_index = ipi.ipi6_ifindex;
}
#endif
#ifdef SCM_TIMESTAMP
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_TIMESTAMP, sizeof (struct timeval))) {
struct timeval tv;
memcpy(&tv, CMSG_DATA(cmsg), sizeof (tv));
UTI_TimevalToTimespec(&tv, &message->timestamp.kernel);
}
#endif
#ifdef SCM_TIMESTAMPNS
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof (message->timestamp.kernel))) {
memcpy(&message->timestamp.kernel, CMSG_DATA(cmsg), sizeof (message->timestamp.kernel));
}
#endif
#ifdef SCM_REALTIME
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_REALTIME, sizeof (message->timestamp.kernel))) {
memcpy(&message->timestamp.kernel, CMSG_DATA(cmsg), sizeof (message->timestamp.kernel));
}
#endif
#ifdef HAVE_LINUX_TIMESTAMPING
#ifdef HAVE_LINUX_TIMESTAMPING_OPT_PKTINFO
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
sizeof (struct scm_ts_pktinfo))) {
struct scm_ts_pktinfo ts_pktinfo;
memcpy(&ts_pktinfo, CMSG_DATA(cmsg), sizeof (ts_pktinfo));
message->timestamp.if_index = ts_pktinfo.if_index;
message->timestamp.l2_length = ts_pktinfo.pkt_length;
}
#endif
else if (match_cmsg(cmsg, SOL_SOCKET, SCM_TIMESTAMPING,
sizeof (struct scm_timestamping))) {
struct scm_timestamping ts3;
memcpy(&ts3, CMSG_DATA(cmsg), sizeof (ts3));
message->timestamp.kernel = ts3.ts[0];
message->timestamp.hw = ts3.ts[2];
}
else if ((match_cmsg(cmsg, SOL_IP, IP_RECVERR, 0) ||
match_cmsg(cmsg, SOL_IPV6, IPV6_RECVERR, 0)) &&
cmsg->cmsg_len >= CMSG_LEN(sizeof (struct sock_extended_err))) {
struct sock_extended_err err;