forked from illumos/illumos-gate
-
Notifications
You must be signed in to change notification settings - Fork 111
/
lx_socket.c
4907 lines (4347 loc) · 128 KB
/
lx_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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
* Copyright 2022 Joyent, Inc.
* Copyright 2024 Carlos Neira <cneirabustos@gmail.com>
*/
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/cmn_err.h>
#include <sys/sockio.h>
#include <sys/thread.h>
#include <sys/stropts.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/kmem.h>
#include <sys/un.h>
#include <sys/sunddi.h>
#include <sys/cred.h>
#include <sys/ucred.h>
#include <sys/model.h>
#include <sys/brand.h>
#include <sys/vmsystm.h>
#include <sys/limits.h>
#include <sys/fcntl.h>
#include <sys/sysmacros.h>
#include <netpacket/packet.h>
#include <sockcommon.h>
#include <socktpi_impl.h>
#include <netinet/udp.h>
#include <sys/sdt.h>
#include <netinet/tcp.h>
#include <netinet/igmp.h>
#include <netinet/icmp6.h>
#include <inet/cc.h>
#include <inet/tcp_impl.h>
#include <lx_errno.h>
#include <sys/lx_brand.h>
#include <sys/lx_socket.h>
#include <sys/lx_types.h>
#include <sys/lx_impl.h>
#include <sys/time.h>
/* From uts/common/fs/sockfs/socksyscalls.c */
extern int listen(int, int, int);
extern int shutdown(int, int, int);
typedef struct lx_ucred {
pid_t lxu_pid;
lx_uid_t lxu_uid;
lx_gid_t lxu_gid;
} lx_ucred_t;
typedef struct lx_socket_aux_data
{
kmutex_t lxsad_lock;
enum lxsad_status_t {
LXSS_NONE = 0,
LXSS_CONNECTING,
LXSS_CONNECTED
} lxsad_status;
uint_t lxsad_flags;
} lx_socket_aux_data_t;
#define LX_SS_MAXSIZE 128
typedef struct lx_sockaddr_storage {
unsigned short lxss_family;
char lxdata[LX_SS_MAXSIZE - sizeof (unsigned short)];
} lx_sockaddr_storage_t;
typedef struct lx_group_req {
uint32_t lxgr_interface;
#ifdef _LP64
/* On 64-bit linux kernels, gr_interface is padded by 4 bytes. */
uint32_t _lxgr_pad;
#endif
lx_sockaddr_storage_t lxgr_group;
} lx_group_req_t;
#if defined(_SYSCALL32_IMPL)
typedef struct lx_group_req32 {
uint32_t lxgr_interface;
lx_sockaddr_storage_t lxgr_group;
} lx_group_req32_t;
#endif /* defined(_SYSCALL32_IMPL) */
/* lxsad_flags */
#define LXSAD_FL_STRCRED 0x1
#define LXSAD_FL_EMULSEQPKT 0x2
/* These two work together to implement Linux SO_REUSEADDR semantics. */
#define LXSAD_FL_EMULRUADDR 0x4
#define LXSAD_FL_EMULRUPORT 0x8
static lx_socket_aux_data_t *lx_sad_acquire(vnode_t *);
/* VSD key for lx-specific socket information */
static uint_t lx_socket_vsd = 0;
/* Convenience enum to enforce translation direction */
typedef enum lx_xlate_dir {
SUNOS_TO_LX,
LX_TO_SUNOS
} lx_xlate_dir_t;
/* enum for getpeername/getsockname handling */
typedef enum lx_getname_type {
LX_GETPEERNAME,
LX_GETSOCKNAME
} lx_getname_type_t;
/*
* What follows are a series of tables we use to translate Linux constants
* into equivalent Illumos constants and back again. I wish this were
* cleaner, more programmatic, and generally nicer. Sadly, life is messy,
* and Unix networking even more so.
*/
static const int ltos_family[LX_AF_MAX + 1] = {
AF_UNSPEC, /* LX_AF_UNSPEC */
AF_UNIX, /* LX_AF_UNIX */
AF_INET, /* LX_AF_INET */
AF_NOTSUPPORTED, /* LX_AF_AX25 */
AF_NOTSUPPORTED, /* LX_AF_IPX */
AF_NOTSUPPORTED, /* LX_AF_APPLETALK */
AF_NOTSUPPORTED, /* LX_AF_NETROM */
AF_NOTSUPPORTED, /* LX_AF_BRIDGE */
AF_NOTSUPPORTED, /* LX_AF_ATMPVC */
AF_NOTSUPPORTED, /* LX_AF_X25 */
AF_INET6, /* LX_AF_INET6 */
AF_NOTSUPPORTED, /* LX_AF_ROSE */
AF_NOTSUPPORTED, /* LX_AF_DECNET */
AF_NOTSUPPORTED, /* LX_AF_NETBEUI */
AF_NOTSUPPORTED, /* LX_AF_SECURITY */
AF_NOTSUPPORTED, /* LX_AF_KEY */
AF_LX_NETLINK, /* LX_AF_NETLINK */
AF_PACKET, /* LX_AF_PACKET */
AF_NOTSUPPORTED, /* LX_AF_ASH */
AF_NOTSUPPORTED, /* LX_AF_ECONET */
AF_NOTSUPPORTED, /* LX_AF_ATMSVC */
AF_NOTSUPPORTED, /* LX_AF_RDS */
AF_NOTSUPPORTED, /* LX_AF_SNA */
AF_NOTSUPPORTED, /* LX_AF_IRDA */
AF_NOTSUPPORTED, /* LX_AF_PPOX */
AF_NOTSUPPORTED, /* LX_AF_WANPIPE */
AF_NOTSUPPORTED, /* LX_AF_LLC */
AF_NOTSUPPORTED, /* NONE */
AF_NOTSUPPORTED, /* NONE */
AF_NOTSUPPORTED, /* LX_AF_CAN */
AF_NOTSUPPORTED, /* LX_AF_TIPC */
AF_NOTSUPPORTED, /* LX_AF_BLUETOOTH */
AF_NOTSUPPORTED, /* LX_AF_IUCV */
AF_NOTSUPPORTED /* LX_AF_RXRPC */
/* LX_AF_ISDN */
/* LX_AF_PHONET */
/* LX_AF_IEEE802154 */
/* LX_AF_CAIF */
/* LX_AF_ALG */
/* LX_AF_NFC */
/* LX_AF_VSOCK */
};
static const int stol_family[LX_AF_MAX + 1] = {
AF_UNSPEC, /* AF_UNSPEC */
AF_UNIX, /* AF_UNIX */
AF_INET, /* AF_INET */
AF_NOTSUPPORTED, /* AF_IMPLINK */
AF_NOTSUPPORTED, /* AF_PUP */
AF_NOTSUPPORTED, /* AF_CHAOS */
AF_NOTSUPPORTED, /* AF_NS */
AF_NOTSUPPORTED, /* AF_NBS */
AF_NOTSUPPORTED, /* AF_ECMA */
AF_NOTSUPPORTED, /* AF_DATAKIT */
AF_NOTSUPPORTED, /* AF_CCITT */
AF_NOTSUPPORTED, /* AF_SNA */
AF_NOTSUPPORTED, /* AF_DECNET */
AF_NOTSUPPORTED, /* AF_DLI */
AF_NOTSUPPORTED, /* AF_LAT */
AF_NOTSUPPORTED, /* AF_HYLINK */
AF_NOTSUPPORTED, /* AF_APPLETALK */
AF_NOTSUPPORTED, /* AF_NIT */
AF_NOTSUPPORTED, /* AF_802 */
AF_NOTSUPPORTED, /* AF_OSI */
AF_NOTSUPPORTED, /* AF_X25 */
AF_NOTSUPPORTED, /* AF_OSINET */
AF_NOTSUPPORTED, /* AF_GOSIP */
AF_NOTSUPPORTED, /* AF_IPX */
AF_NOTSUPPORTED, /* AF_ROUTE */
AF_NOTSUPPORTED, /* AF_LINK */
LX_AF_INET6, /* AF_INET6 */
AF_NOTSUPPORTED, /* AF_KEY */
AF_NOTSUPPORTED, /* AF_NCA */
AF_NOTSUPPORTED, /* AF_POLICY */
AF_NOTSUPPORTED, /* AF_INET_OFFLOAD */
AF_NOTSUPPORTED, /* AF_TRILL */
LX_AF_PACKET, /* AF_PACKET */
LX_AF_NETLINK /* AF_LX_NETLINK */
};
#define LTOS_FAMILY(d) ((d) <= LX_AF_MAX ? ltos_family[(d)] : AF_INVAL)
#define STOL_FAMILY(d) ((d) <= LX_AF_MAX ? stol_family[(d)] : AF_INVAL)
static const int ltos_socktype[LX_SOCK_PACKET + 1] = {
SOCK_NOTSUPPORTED, SOCK_STREAM, SOCK_DGRAM, SOCK_RAW,
SOCK_RDM, SOCK_SEQPACKET, SOCK_NOTSUPPORTED, SOCK_NOTSUPPORTED,
SOCK_NOTSUPPORTED, SOCK_NOTSUPPORTED, SOCK_NOTSUPPORTED
};
static const int stol_socktype[SOCK_SEQPACKET + 1] = {
SOCK_NOTSUPPORTED, LX_SOCK_DGRAM, LX_SOCK_STREAM, SOCK_NOTSUPPORTED,
LX_SOCK_RAW, LX_SOCK_RDM, LX_SOCK_SEQPACKET
};
#define LTOS_SOCKTYPE(t) \
((t) <= LX_SOCK_PACKET ? ltos_socktype[(t)] : SOCK_INVAL)
#define STOL_SOCKTYPE(t) \
((t) <= SOCK_SEQPACKET ? stol_socktype[(t)] : SOCK_INVAL)
/*
* illumos represents tcp states with ranges between -6 to 6, where:
* tcp states > 0 define connections that have been stablished.
* tcp states < 0 define connections that have not yet stablished.
* Where Linux defines their tcp states as a range from 1 to 12.
* the macro STOL_TCPSTATE translate our illumos tcp states to a Linux one,
* in case there is no tcp state returned by OS that matches Linux, the state
* LX_TCP_ESTABLISHED will be returned, except for the following:
* for TCPS_BOUND we will return LX_TCP_LISTEN.
* for TCPS_IDLE we will return LX_TCP_LISTEN.
*/
/* Table based on -6 to 6 being mapped on to 0 to 12 */
static const int stol_tcp_state[LX_TCP_NEW_SYN_RECV + 1] = {
LX_TCP_CLOSE, /* -6 ==> 0 */
LX_TCP_ESTABLISHED, /* -5 ==> 1, no TCPS_IDLE in Linux */
LX_TCP_LISTEN, /* -4 ==> 2, no TCPS_BOUND in Linux */
LX_TCP_LISTEN, /* -3 ==> 3 */
LX_TCP_SYN_SENT, /* -2 ==> 4 */
LX_TCP_SYN_RECV, /* -1 ==> 5 */
LX_TCP_ESTABLISHED, /* 0 ==> 6 */
LX_TCP_CLOSE_WAIT, /* 1 ==> 7 */
LX_TCP_FIN_WAIT1, /* 2 ==> 8 */
LX_TCP_CLOSING, /* 3 ==> 9 */
LX_TCP_LAST_ACK, /* 4 ==> 10 */
LX_TCP_FIN_WAIT2, /* 5 ==> 11 */
LX_TCP_TIME_WAIT /* 6 ==> 12 */
};
#define STOL_TCPSTATE(t)\
(((t) <= LX_OS_MAX_TCP_STATE && (t) >= LX_OS_MIN_TCP_STATE) ? \
stol_tcp_state[(t) + 6] : LX_TCP_ESTABLISHED)
/*
* This string is used to prefix all abstract namespace Unix sockets, ie all
* abstract namespace sockets are converted to regular sockets in the /tmp
* directory with .ABSK_ prefixed to their names.
*/
#define ABST_PRFX "/tmp/.ABSK_"
#define ABST_PRFX_LEN (sizeof (ABST_PRFX) - 1)
#define DATAFILT "datafilt"
typedef enum {
lxa_none,
lxa_abstract,
lxa_devlog
} lx_addr_type_t;
static int
ltos_pkt_proto(int protocol)
{
switch (ntohs(protocol)) {
case LX_ETH_P_802_2:
return (ETH_P_802_2);
case LX_ETH_P_IP:
return (ETH_P_IP);
case LX_ETH_P_ARP:
return (ETH_P_ARP);
case LX_ETH_P_IPV6:
return (ETH_P_IPV6);
case LX_ETH_P_ALL:
case LX_ETH_P_802_3:
return (ETH_P_ALL);
default:
return (-1);
}
}
typedef struct lx_flag_map {
enum {
LXFM_MAP,
LXFM_IGNORE,
LXFM_UNSUP
} lxfm_action;
int lxfm_sunos_flag;
int lxfm_linux_flag;
char *lxfm_name;
} lx_flag_map_t;
static lx_flag_map_t lx_flag_map_tbl[] = {
{ LXFM_MAP, MSG_OOB, LX_MSG_OOB, NULL },
{ LXFM_MAP, MSG_PEEK, LX_MSG_PEEK, NULL },
{ LXFM_MAP, MSG_DONTROUTE, LX_MSG_DONTROUTE, NULL },
{ LXFM_MAP, MSG_CTRUNC, LX_MSG_CTRUNC, NULL },
{ LXFM_MAP, MSG_TRUNC, LX_MSG_TRUNC, NULL },
{ LXFM_MAP, MSG_DONTWAIT, LX_MSG_DONTWAIT, NULL },
{ LXFM_MAP, MSG_EOR, LX_MSG_EOR, NULL },
{ LXFM_MAP, MSG_WAITALL, LX_MSG_WAITALL, NULL },
/* MSG_CONFIRM is safe to ignore */
{ LXFM_IGNORE, 0, LX_MSG_CONFIRM, NULL },
/*
* The NOSIGNAL and CMSG_CLOEXEC flags are handled by the emulation
* outside of the flag-conversion routine.
*/
{ LXFM_IGNORE, 0, LX_MSG_NOSIGNAL, NULL },
{ LXFM_IGNORE, 0, LX_MSG_CMSG_CLOEXEC, NULL },
{ LXFM_UNSUP, LX_MSG_PROXY, 0, "MSG_PROXY" },
{ LXFM_UNSUP, LX_MSG_FIN, 0, "MSG_FIN" },
{ LXFM_UNSUP, LX_MSG_SYN, 0, "MSG_SYN" },
{ LXFM_UNSUP, LX_MSG_RST, 0, "MSG_RST" },
{ LXFM_UNSUP, LX_MSG_ERRQUEUE, 0, "MSG_ERRQUEUE" },
{ LXFM_UNSUP, LX_MSG_MORE, 0, "MSG_MORE" },
{ LXFM_UNSUP, LX_MSG_WAITFORONE, 0, "MSG_WAITFORONE" },
{ LXFM_UNSUP, LX_MSG_FASTOPEN, 0, "MSG_FASTOPEN" },
};
#define LX_FLAG_MAP_MAX \
(sizeof (lx_flag_map_tbl) / sizeof (lx_flag_map_tbl[0]))
#define LX_UNSUP_BUFSZ 64
static int
lx_xlate_sock_flags(int inflags, lx_xlate_dir_t dir)
{
int i, outflags = 0;
char buf[LX_UNSUP_BUFSZ];
VERIFY(dir == SUNOS_TO_LX || dir == LX_TO_SUNOS);
for (i = 0; i < LX_FLAG_MAP_MAX; i++) {
lx_flag_map_t *map = &lx_flag_map_tbl[i];
int match, out;
if (dir == SUNOS_TO_LX) {
match = inflags & map->lxfm_sunos_flag;
out = map->lxfm_linux_flag;
} else {
match = inflags & map->lxfm_linux_flag;
out = map->lxfm_sunos_flag;
}
switch (map->lxfm_action) {
case LXFM_MAP:
if (match != 0) {
inflags &= ~(match);
outflags |= out;
}
break;
case LXFM_IGNORE:
if (match != 0) {
inflags &= ~(match);
}
break;
case LXFM_UNSUP:
if (match != 0) {
(void) snprintf(buf, LX_UNSUP_BUFSZ,
"unsupported sock flag %s", map->lxfm_name);
lx_unsupported(buf);
}
}
}
if (inflags != 0) {
(void) snprintf(buf, LX_UNSUP_BUFSZ,
"unsupported sock flags 0x%08x", inflags);
lx_unsupported(buf);
}
return (outflags);
}
typedef enum lx_sun_type {
LX_SUN_NORMAL,
LX_SUN_ABSTRACT,
} lx_sun_type_t;
static void
ltos_sockaddr_ux(const struct sockaddr *inaddr, const socklen_t inlen,
struct sockaddr **outaddr, socklen_t *outlen, lx_sun_type_t *sun_type)
{
struct sockaddr_un buf;
/* Calculate size of (sun_family + any padding) in sockaddr */
int sizediff = (sizeof (buf) - sizeof (buf.sun_path));
int len = inlen - sizediff;
VERIFY(len > 0);
VERIFY(len <= sizeof (buf.sun_path));
bzero(&buf, sizeof (buf));
if (inaddr->sa_data[0] == '\0') {
/*
* Linux supports abstract Unix sockets, which are simply
* sockets that do not exist on the file system. These sockets
* are denoted by beginning the path with a NULL character. To
* support these, we strip out the leading NULL character and
* change the path to point to a real place in /tmp directory,
* by prepending ABST_PRFX and replacing all illegal characters
* with * '_'.
*
* Since these sockets are supposed to exist outside the
* filesystem, they must be cleaned up after use. This removal
* is performed during bind().
*/
int idx, odx;
/* Add our abstract prefix */
(void) strcpy(buf.sun_path, ABST_PRFX);
for (idx = 1, odx = ABST_PRFX_LEN;
idx < len && odx < sizeof (buf.sun_path);
idx++, odx++) {
char c = inaddr->sa_data[idx];
if (c == '\0' || c == '/') {
buf.sun_path[odx] = '_';
} else {
buf.sun_path[odx] = c;
}
}
/*
* Since abstract socket addresses might not be NUL terminated,
* we must explicitly NUL terminate the translated path.
* Care is taken not to overflow the buffer.
*/
if (odx == sizeof (buf.sun_path)) {
buf.sun_path[odx - 1] = '\0';
} else {
buf.sun_path[odx] = '\0';
}
if (sun_type != NULL) {
*sun_type = LX_SUN_ABSTRACT;
}
} else {
/* Copy the address directly, minding termination */
(void) strncpy(buf.sun_path, inaddr->sa_data, len);
len = strnlen(buf.sun_path, len);
if (len == sizeof (buf.sun_path)) {
buf.sun_path[len - 1] = '\0';
} else {
VERIFY(len < sizeof (buf.sun_path));
buf.sun_path[len] = '\0';
}
if (sun_type != NULL) {
*sun_type = LX_SUN_NORMAL;
}
}
buf.sun_family = AF_UNIX;
*outlen = strlen(buf.sun_path) + 1 + sizediff;
VERIFY(*outlen <= sizeof (struct sockaddr_un));
*outaddr = kmem_alloc(*outlen, KM_SLEEP);
bcopy(&buf, *outaddr, *outlen);
}
/*
* Copy in a Linux-native socket address from userspace and convert it into
* illumos format. When successful, it will allocate an appropriately sized
* struct to be freed by the caller.
*/
static long
ltos_sockaddr_copyin(const struct sockaddr *inaddr, const socklen_t inlen,
struct sockaddr **outaddr, socklen_t *outlen, lx_sun_type_t *sun_type)
{
sa_family_t family;
struct sockaddr *laddr;
struct sockaddr_ll *sal;
int proto, error = 0;
VERIFY(inaddr != NULL);
if (inlen < sizeof (sa_family_t) ||
inlen > sizeof (struct sockaddr_storage)) {
return (EINVAL);
}
laddr = kmem_alloc(inlen, KM_SLEEP);
if (copyin(inaddr, laddr, inlen) != 0) {
kmem_free(laddr, inlen);
return (EFAULT);
}
family = LTOS_FAMILY(laddr->sa_family);
switch (family) {
case (sa_family_t)AF_NOTSUPPORTED:
error = EPROTONOSUPPORT;
break;
case (sa_family_t)AF_INVAL:
error = EAFNOSUPPORT;
break;
case AF_UNIX:
if (inlen < sizeof (sa_family_t) + 2 ||
inlen > sizeof (struct sockaddr_un)) {
error = EINVAL;
break;
}
ltos_sockaddr_ux(laddr, inlen, outaddr, outlen,
sun_type);
/* AF_UNIX bypasses the standard copy logic */
kmem_free(laddr, inlen);
return (0);
case AF_PACKET:
if (inlen < sizeof (struct sockaddr_ll)) {
error = EINVAL;
break;
}
*outlen = sizeof (struct sockaddr_ll);
/* sll_protocol must be translated */
/* LINTED: alignment */
sal = (struct sockaddr_ll *)laddr;
proto = ltos_pkt_proto(sal->sll_protocol);
if (proto < 0) {
error = EINVAL;
}
sal->sll_protocol = proto;
break;
case AF_INET:
if (inlen < sizeof (struct sockaddr)) {
error = EINVAL;
break;
}
*outlen = sizeof (struct sockaddr);
break;
case AF_INET6:
/*
* The illumos sockaddr_in6 has one more 32-bit field
* than the Linux version. We simply zero that field
* via kmem_zalloc.
*/
if (inlen < sizeof (lx_sockaddr_in6_t)) {
error = EINVAL;
break;
}
*outlen = sizeof (struct sockaddr_in6);
*outaddr = (struct sockaddr *)kmem_zalloc(*outlen,
KM_SLEEP);
bcopy(laddr, *outaddr, sizeof (lx_sockaddr_in6_t));
(*outaddr)->sa_family = AF_INET6;
/* AF_INET6 bypasses the standard copy logic */
kmem_free(laddr, inlen);
return (0);
default:
*outlen = inlen;
}
if (error == 0) {
/*
* For most address families, just copying into a sockaddr of
* the correct size and updating sa_family is adequate.
*/
VERIFY(inlen >= *outlen);
*outaddr = (struct sockaddr *)kmem_zalloc(*outlen, KM_SLEEP);
bcopy(laddr, *outaddr, *outlen);
(*outaddr)->sa_family = family;
}
kmem_free(laddr, inlen);
return (error);
}
/*
* Convert an illumos-native socket address into Linux format and copy it out
* to userspace.
*/
static long
stol_sockaddr_copyout(struct sockaddr *inaddr, socklen_t inlen,
struct sockaddr *outaddr, void *outlenp, socklen_t orig)
{
socklen_t size = inlen;
struct sockaddr_storage buf;
struct sockaddr *bufaddr;
/*
* Either we were passed a valid sockaddr (with length) or the length
* is set to 0.
*/
VERIFY(inaddr != NULL || inlen == 0);
if (inlen == 0) {
goto finish;
}
switch (inaddr->sa_family) {
case AF_INET:
if (inlen != sizeof (struct sockaddr)) {
return (EINVAL);
}
break;
case AF_INET6:
if (inlen != sizeof (struct sockaddr_in6)) {
return (EINVAL);
}
/*
* The linux sockaddr_in6 is shorter than illumos.
* Truncate the extra field on the way out.
*/
size = (sizeof (lx_sockaddr_in6_t));
inlen = (sizeof (lx_sockaddr_in6_t));
break;
case AF_UNIX:
if (inlen > sizeof (struct sockaddr_un)) {
return (EINVAL);
}
/*
* On Linux an empty AF_UNIX address is returned as NULL, which
* means setting the returned length to only encompass the
* address family part of the buffer. However, some code also
* references the address portion of the buffer and uses it,
* even though the returned length has been shortened. Thus, we
* clear the buffer to ensure that the address portion is NULL.
*/
if (inaddr->sa_data[0] == '\0') {
bzero(&buf, sizeof (buf));
inlen = sizeof (inaddr->sa_family);
}
break;
case (sa_family_t)AF_NOTSUPPORTED:
return (EPROTONOSUPPORT);
case (sa_family_t)AF_INVAL:
return (EAFNOSUPPORT);
default:
break;
}
/*
* The input should be smaller than sockaddr_storage, the largest
* sockaddr we support.
*/
VERIFY(inlen <= sizeof (buf));
bufaddr = (struct sockaddr *)&buf;
bcopy(inaddr, bufaddr, inlen);
bufaddr->sa_family = STOL_FAMILY(bufaddr->sa_family);
/*
* It is possible that userspace passed us a smaller buffer than we
* hope to output. When this is the case, we will truncate our output
* to the max size of their buffer but report the true size of the
* sockaddr when outputting the outlen value.
*/
size = (orig < size) ? orig : size;
if (copyout(bufaddr, outaddr, size) != 0) {
return (EFAULT);
}
finish:
#if defined(_LP64)
if (get_udatamodel() != DATAMODEL_NATIVE) {
int32_t len32 = (int32_t)inlen;
if (copyout(&len32, outlenp, sizeof (len32)) != 0) {
return (EFAULT);
}
} else
#endif /* defined(_LP64) */
{
if (copyout(&inlen, outlenp, sizeof (inlen)) != 0) {
return (EFAULT);
}
}
return (0);
}
typedef struct lx_cmsg_xlate {
int lcx_sunos_level;
int lcx_sunos_type;
int (*lcx_stol_conv)(struct cmsghdr *, struct cmsghdr *);
int lcx_linux_level;
int lcx_linux_type;
int (*lcx_ltos_conv)(struct cmsghdr *, struct cmsghdr *);
} lx_cmsg_xlate_t;
static int cmsg_conv_generic(struct cmsghdr *, struct cmsghdr *);
static int stol_conv_ucred(struct cmsghdr *, struct cmsghdr *);
static int ltos_conv_ucred(struct cmsghdr *, struct cmsghdr *);
static int stol_conv_recvttl(struct cmsghdr *, struct cmsghdr *);
/*
* Table describing SunOS <-> Linux cmsg translation mappings.
* Certain types (IP_RECVTTL) are only converted in one direction and are
* indicated by one of the translation functions being set to NULL.
*/
static lx_cmsg_xlate_t lx_cmsg_xlate_tbl[] = {
{ SOL_SOCKET, SCM_RIGHTS, cmsg_conv_generic,
LX_SOL_SOCKET, LX_SCM_RIGHTS, cmsg_conv_generic },
{ SOL_SOCKET, SCM_UCRED, stol_conv_ucred,
LX_SOL_SOCKET, LX_SCM_CRED, ltos_conv_ucred },
{ SOL_SOCKET, SCM_TIMESTAMP, cmsg_conv_generic,
LX_SOL_SOCKET, LX_SCM_TIMESTAMP, cmsg_conv_generic },
{ IPPROTO_IP, IP_PKTINFO, cmsg_conv_generic,
LX_IPPROTO_IP, LX_IP_PKTINFO, cmsg_conv_generic },
{ IPPROTO_IP, IP_RECVTTL, stol_conv_recvttl,
LX_IPPROTO_IP, LX_IP_TTL, NULL },
{ IPPROTO_IP, IP_RECVTOS, cmsg_conv_generic,
LX_IPPROTO_IP, LX_IP_TOS, cmsg_conv_generic },
{ IPPROTO_IP, IP_TTL, cmsg_conv_generic,
LX_IPPROTO_IP, LX_IP_TTL, cmsg_conv_generic },
{ IPPROTO_IPV6, IPV6_HOPLIMIT, cmsg_conv_generic,
LX_IPPROTO_IPV6, LX_IPV6_HOPLIMIT, cmsg_conv_generic },
{ IPPROTO_IPV6, IPV6_PKTINFO, cmsg_conv_generic,
LX_IPPROTO_IPV6, LX_IPV6_PKTINFO, cmsg_conv_generic },
{ IPPROTO_IPV6, IPV6_TCLASS, cmsg_conv_generic,
LX_IPPROTO_IPV6, LX_IPV6_TCLASS, cmsg_conv_generic }
};
#define LX_MAX_CMSG_XLATE \
(sizeof (lx_cmsg_xlate_tbl) / sizeof (lx_cmsg_xlate_tbl[0]))
#if defined(_LP64)
typedef struct {
int64_t cmsg_len;
int32_t cmsg_level;
int32_t cmsg_type;
} lx_cmsghdr64_t;
/* The alignment/padding for 64bit Linux cmsghdr is not the same. */
#define LX_CMSG64_ALIGNMENT 8
#define ISALIGNED_LX_CMSG64(addr) \
(((uintptr_t)(addr) & (LX_CMSG64_ALIGNMENT - 1)) == 0)
#define ROUNDUP_LX_CMSG64_LEN(len) \
(((len) + LX_CMSG64_ALIGNMENT - 1) & ~(LX_CMSG64_ALIGNMENT - 1))
#define LX_CMSG64_IS_ALIGNED(m) \
(((uintptr_t)(m) & (_CMSG_DATA_ALIGNMENT - 1)) == 0)
#define LX_CMSG64_DATA(c) ((unsigned char *)(((lx_cmsghdr64_t *)(c)) + 1))
/*
* LX_CMSG64_VALID is closely derived from CMSG_VALID with one particularly
* important addition. Since cmsg_len is 64bit, (cmsg + cmsg_len) is checked
* against the start address as well. This prevents bogus inputs from wrapping
* around the address space.
*/
#define LX_CMSG64_VALID(cmsg, start, end) \
(ISALIGNED_LX_CMSG64(cmsg) && \
((uintptr_t)(cmsg) >= (uintptr_t)(start)) && \
((uintptr_t)(cmsg) < (uintptr_t)(end)) && \
((cmsg)->cmsg_len >= sizeof (lx_cmsghdr64_t)) && \
((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)) && \
((uintptr_t)(cmsg) + (cmsg)->cmsg_len >= (uintptr_t)(start)))
#define LX_CMSG64_NEXT(cmsg) \
(lx_cmsghdr64_t *)((uintptr_t)(cmsg) + \
ROUNDUP_LX_CMSG64_LEN((cmsg)->cmsg_len))
#define LX_CMSG64_DIFF sizeof (uint32_t)
#endif /* defined(_LP64) */
/*
* convert ucred_s to lx_ucred.
*/
static int
stol_conv_ucred(struct cmsghdr *inmsg, struct cmsghdr *omsg)
{
/*
* Format the data correctly in the omsg buffer.
*/
if (omsg != NULL) {
struct ucred_s *scred;
prcred_t *cr;
lx_ucred_t lcred;
scred = (struct ucred_s *)CMSG_CONTENT(inmsg);
lcred.lxu_pid = scred->uc_pid;
/* LINTED: alignment */
cr = UCCRED(scred);
if (cr != NULL) {
lcred.lxu_uid = cr->pr_euid;
lcred.lxu_gid = cr->pr_egid;
} else {
lcred.lxu_uid = lcred.lxu_gid = 0;
}
bcopy(&lcred, CMSG_CONTENT(omsg), sizeof (lx_ucred_t));
}
return (sizeof (struct cmsghdr) + sizeof (lx_ucred_t));
}
static int
ltos_conv_ucred(struct cmsghdr *inmsg, struct cmsghdr *omsg)
{
if (omsg != NULL) {
struct ucred_s *uc;
prcred_t *pc;
lx_ucred_t *lcred;
uc = (struct ucred_s *)CMSG_CONTENT(omsg);
/* LINTED: alignment */
pc = (prcred_t *)((char *)uc + sizeof (struct ucred_s));
uc->uc_credoff = sizeof (struct ucred_s);
lcred = (lx_ucred_t *)CMSG_CONTENT(inmsg);
uc->uc_pid = lcred->lxu_pid;
pc->pr_euid = lcred->lxu_uid;
pc->pr_egid = lcred->lxu_gid;
}
return (sizeof (struct cmsghdr) + sizeof (struct ucred_s) +
sizeof (prcred_t));
}
static int
stol_conv_recvttl(struct cmsghdr *inmsg, struct cmsghdr *omsg)
{
/*
* SunOS communicates the TTL of incoming packets via IP_RECVTTL using
* a uint8_t value instead of IP_TTL using an int. This conversion is
* only needed in the one direction since Linux does not handle
* IP_RECVTTL in the sendmsg path.
*/
if (omsg != NULL) {
uint8_t *inttl = (uint8_t *)CMSG_CONTENT(inmsg);
int *ottl = (int *)CMSG_CONTENT(omsg);
*ottl = (int)*inttl;
}
return (sizeof (struct cmsghdr) + sizeof (int));
}
static int
cmsg_conv_generic(struct cmsghdr *inmsg, struct cmsghdr *omsg)
{
if (omsg != NULL) {
size_t data_len;
data_len = inmsg->cmsg_len - sizeof (struct cmsghdr);
bcopy(CMSG_CONTENT(inmsg), CMSG_CONTENT(omsg), data_len);
}
return (inmsg->cmsg_len);
}
static int
lx_xlate_cmsg(struct cmsghdr *inmsg, struct cmsghdr *omsg, lx_xlate_dir_t dir)
{
int i;
int len;
VERIFY(dir == SUNOS_TO_LX || dir == LX_TO_SUNOS);
for (i = 0; i < LX_MAX_CMSG_XLATE; i++) {
lx_cmsg_xlate_t *xlate = &lx_cmsg_xlate_tbl[i];
if (dir == LX_TO_SUNOS &&
inmsg->cmsg_level == xlate->lcx_linux_level &&
inmsg->cmsg_type == xlate->lcx_linux_type &&
xlate->lcx_ltos_conv != NULL) {
len = xlate->lcx_ltos_conv(inmsg, omsg);
if (omsg != NULL) {
omsg->cmsg_len = len;
omsg->cmsg_level = xlate->lcx_sunos_level;
omsg->cmsg_type = xlate->lcx_sunos_type;
}
return (len);
} else if (dir == SUNOS_TO_LX &&
inmsg->cmsg_level == xlate->lcx_sunos_level &&
inmsg->cmsg_type == xlate->lcx_sunos_type &&
xlate->lcx_stol_conv != NULL) {
len = xlate->lcx_stol_conv(inmsg, omsg);
if (omsg != NULL) {
omsg->cmsg_len = len;
omsg->cmsg_level = xlate->lcx_linux_level;
omsg->cmsg_type = xlate->lcx_linux_type;
}
return (len);
}
}
/*
* The Linux man page for sendmsg does not define a specific error for
* unsupported cmsgs. While it is meant to indicated bad values for
* passed flags, EOPNOTSUPP appears to be the next closest choice.
*/
return (-EOPNOTSUPP);
}
static long
ltos_cmsgs_copyin(void *addr, socklen_t inlen, void **outmsg,
socklen_t *outlenp)
{
void *inbuf, *obuf;
struct cmsghdr *inmsg, *omsg;
int slen = 0;
if (inlen < sizeof (struct cmsghdr) || inlen > SO_MAXARGSIZE) {
return (EINVAL);
}
#if defined(_LP64)
if (get_udatamodel() == DATAMODEL_NATIVE &&
inlen < sizeof (lx_cmsghdr64_t)) {
/* The size requirements are more strict for 64bit. */
return (EINVAL);
}
#endif /* defined(_LP64) */
inbuf = kmem_alloc(inlen, KM_SLEEP);
if (copyin(addr, inbuf, inlen) != 0) {
kmem_free(inbuf, inlen);
return (EFAULT);
}
#if defined(_LP64)
if (get_udatamodel() == DATAMODEL_NATIVE) {
/*
* Linux cmsg headers are longer than illumos under x86_64.
* Convert to regular cmsgs first.
*/
lx_cmsghdr64_t *lmsg;
struct cmsghdr *smsg;
void *newbuf;
int len = 0;
/* Inventory the new cmsg size */
for (lmsg = (lx_cmsghdr64_t *)inbuf;
LX_CMSG64_VALID(lmsg, inbuf, (uintptr_t)inbuf + inlen) != 0;
lmsg = LX_CMSG64_NEXT(lmsg)) {
len += ROUNDUP_cmsglen(lmsg->cmsg_len - LX_CMSG64_DIFF);
}
VERIFY(len < inlen);
if (len == 0) {
/* Input was bogus, so we can give up early. */
kmem_free(inbuf, inlen);
*outmsg = NULL;
*outlenp = 0;
return (EINVAL);
}
newbuf = kmem_alloc(len, KM_SLEEP);
for (lmsg = (lx_cmsghdr64_t *)inbuf,
smsg = (struct cmsghdr *)newbuf;
LX_CMSG64_VALID(lmsg, inbuf, (uintptr_t)inbuf + inlen) != 0;
lmsg = LX_CMSG64_NEXT(lmsg), smsg = CMSG_NEXT(smsg)) {
smsg->cmsg_level = lmsg->cmsg_level;
smsg->cmsg_type = lmsg->cmsg_type;
smsg->cmsg_len = lmsg->cmsg_len - LX_CMSG64_DIFF;
/* The above length measurement should ensure this */
ASSERT(CMSG_VALID(smsg, newbuf,
(uintptr_t)newbuf + len));
bcopy(LX_CMSG64_DATA(lmsg), CMSG_CONTENT(smsg),
smsg->cmsg_len - sizeof (*smsg));
}
kmem_free(inbuf, inlen);
inbuf = newbuf;
inlen = len;
}