summaryrefslogtreecommitdiff
path: root/sys/compat/linux/common/linux_socket.c
blob: 2bfa15f5104818d393460b58e1d42ac0d90d576c (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
/*	$NetBSD: linux_socket.c,v 1.84 2007/12/08 18:36:10 dsl Exp $	*/

/*-
 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Frank van der Linden and Eric Haszlakiewicz.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the NetBSD
 *	Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Functions in multiarch:
 *	linux_sys_socketcall		: linux_socketcall.c
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.84 2007/12/08 18:36:10 dsl Exp $");

#if defined(_KERNEL_OPT)
#include "opt_inet.h"
#endif /* defined(_KERNEL_OPT) */

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/malloc.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/domain.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/mount.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/device.h>
#include <sys/protosw.h>
#include <sys/mbuf.h>
#include <sys/syslog.h>
#include <sys/exec.h>
#include <sys/kauth.h>

#include <sys/syscallargs.h>
#include <sys/ktrace.h>

#include <lib/libkern/libkern.h>

#ifdef INET6
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#endif

#include <compat/sys/socket.h>
#include <compat/sys/sockio.h>

#include <compat/linux/common/linux_types.h>
#include <compat/linux/common/linux_util.h>
#include <compat/linux/common/linux_signal.h>
#include <compat/linux/common/linux_ioctl.h>
#include <compat/linux/common/linux_socket.h>
#if !defined(__alpha__) && !defined(__amd64__)
#include <compat/linux/common/linux_socketcall.h>
#endif
#include <compat/linux/common/linux_sockio.h>
#include <compat/linux/common/linux_ipc.h>
#include <compat/linux/common/linux_sem.h>

#include <compat/linux/linux_syscallargs.h>

#ifdef DEBUG_LINUX
#define DPRINTF(a) uprintf a
#else
#define DPRINTF(a)
#endif

/*
 * The calls in this file are entered either via the linux_socketcall()
 * interface or, on the Alpha, as individual syscalls.  The
 * linux_socketcall function does any massaging of arguments so that all
 * the calls in here need not think that they are anything other
 * than a normal syscall.
 */

static int linux_to_bsd_domain(int);
static int bsd_to_linux_domain(int);
int linux_to_bsd_sopt_level(int);
int linux_to_bsd_so_sockopt(int);
int linux_to_bsd_ip_sockopt(int);
int linux_to_bsd_tcp_sockopt(int);
int linux_to_bsd_udp_sockopt(int);
int linux_getifhwaddr(struct lwp *, register_t *, u_int, void *);
static int linux_get_sa(struct lwp *, int, struct mbuf **,
		const struct osockaddr *, int);
static int linux_sa_put(struct osockaddr *osa);
static int linux_to_bsd_msg_flags(int);
static int bsd_to_linux_msg_flags(int);

static const int linux_to_bsd_domain_[LINUX_AF_MAX] = {
	AF_UNSPEC,
	AF_UNIX,
	AF_INET,
	AF_CCITT,	/* LINUX_AF_AX25 */
	AF_IPX,
	AF_APPLETALK,
	-1,		/* LINUX_AF_NETROM */
	-1,		/* LINUX_AF_BRIDGE */
	-1,		/* LINUX_AF_ATMPVC */
	AF_CCITT,	/* LINUX_AF_X25 */
	AF_INET6,
	-1,		/* LINUX_AF_ROSE */
	AF_DECnet,
	-1,		/* LINUX_AF_NETBEUI */
	-1,		/* LINUX_AF_SECURITY */
	pseudo_AF_KEY,
	AF_ROUTE,	/* LINUX_AF_NETLINK */
	-1,		/* LINUX_AF_PACKET */
	-1,		/* LINUX_AF_ASH */
	-1,		/* LINUX_AF_ECONET */
	-1,		/* LINUX_AF_ATMSVC */
	AF_SNA,
	/* rest up to LINUX_AF_MAX-1 is not allocated */
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};

static const int bsd_to_linux_domain_[AF_MAX] = {
	LINUX_AF_UNSPEC,
	LINUX_AF_UNIX,
	LINUX_AF_INET,
	-1,		/* AF_IMPLINK */
	-1,		/* AF_PUP */
	-1,		/* AF_CHAOS */
	-1,		/* AF_NS */
	-1,		/* AF_ISO */
	-1,		/* AF_ECMA */
	-1,		/* AF_DATAKIT */
	LINUX_AF_AX25,	/* AF_CCITT */
	LINUX_AF_SNA,
	LINUX_AF_DECnet,
	-1,		/* AF_DLI */
	-1,		/* AF_LAT */
	-1,		/* AF_HYLINK */
	LINUX_AF_APPLETALK,
	LINUX_AF_NETLINK,
	-1,		/* AF_LINK */
	-1,		/* AF_XTP */
	-1,		/* AF_COIP */
	-1,		/* AF_CNT */
	-1,		/* pseudo_AF_RTIP */
	LINUX_AF_IPX,
	LINUX_AF_INET6,
	-1,		/* pseudo_AF_PIP */
	-1,		/* AF_ISDN */
	-1,		/* AF_NATM */
	-1,		/* AF_ARP */
	LINUX_pseudo_AF_KEY,
	-1,		/* pseudo_AF_HDRCMPLT */
};

static const struct {
	int bfl;
	int lfl;
} bsd_to_linux_msg_flags_[] = {
	{MSG_OOB,		LINUX_MSG_OOB},
	{MSG_PEEK,		LINUX_MSG_PEEK},
	{MSG_DONTROUTE,		LINUX_MSG_DONTROUTE},
	{MSG_EOR,		LINUX_MSG_EOR},
	{MSG_TRUNC,		LINUX_MSG_TRUNC},
	{MSG_CTRUNC,		LINUX_MSG_CTRUNC},
	{MSG_WAITALL,		LINUX_MSG_WAITALL},
	{MSG_DONTWAIT,		LINUX_MSG_DONTWAIT},
	{MSG_BCAST,		0},		/* not supported, clear */
	{MSG_MCAST,		0},		/* not supported, clear */
	{-1, /* not supp */	LINUX_MSG_PROBE},
	{-1, /* not supp */	LINUX_MSG_FIN},
	{-1, /* not supp */	LINUX_MSG_SYN},
	{-1, /* not supp */	LINUX_MSG_CONFIRM},
	{-1, /* not supp */	LINUX_MSG_RST},
	{-1, /* not supp */	LINUX_MSG_ERRQUEUE},
	{-1, /* not supp */	LINUX_MSG_NOSIGNAL},
	{-1, /* not supp */	LINUX_MSG_MORE},
};

/*
 * Convert between Linux and BSD socket domain values
 */
static int
linux_to_bsd_domain(int ldom)
{
	if (ldom < 0 || ldom >= LINUX_AF_MAX)
		return (-1);

	return linux_to_bsd_domain_[ldom];
}

/*
 * Convert between BSD and Linux socket domain values
 */
static int
bsd_to_linux_domain(int bdom)
{
	if (bdom < 0 || bdom >= AF_MAX)
		return (-1);

	return bsd_to_linux_domain_[bdom];
}

static int
linux_to_bsd_msg_flags(int lflag)
{
	int i, lfl, bfl;
	int bflag = 0;

	if (lflag == 0)
		return (0);

	for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
		bfl = bsd_to_linux_msg_flags_[i].bfl;
		lfl = bsd_to_linux_msg_flags_[i].lfl;

		if (lfl == 0)
			continue;

		if (lflag & lfl) {
			if (bfl < 0)
				return (-1);

			bflag |= bfl;
		}
	}

	return (bflag);
}

static int
bsd_to_linux_msg_flags(int bflag)
{
	int i, lfl, bfl;
	int lflag = 0;

	if (bflag == 0)
		return (0);

	for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
		bfl = bsd_to_linux_msg_flags_[i].bfl;
		lfl = bsd_to_linux_msg_flags_[i].lfl;

		if (bfl <= 0)
			continue;

		if (bflag & bfl) {
			if (lfl < 0)
				return (-1);

			lflag |= lfl;
		}
	}

	return (lflag);
}

int
linux_sys_socket(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_socket_args /* {
		syscallarg(int)	domain;
		syscallarg(int)	type;
		syscallarg(int) protocol;
	} */ *uap = v;
	struct compat_30_sys_socket_args bsa;
	int error;

	SCARG(&bsa, protocol) = SCARG(uap, protocol);
	SCARG(&bsa, type) = SCARG(uap, type);
	SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
	if (SCARG(&bsa, domain) == -1)
		return EINVAL;
	error = sys___socket30(l, &bsa, retval);

#ifdef INET6
	/*
	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by
	 * default and some apps depend on this. So, set V6ONLY to 0
	 * for Linux apps if the sysctl value is set to 1.
	 */
	if (!error && ip6_v6only && SCARG(&bsa, domain) == PF_INET6) {
		struct proc *p = l->l_proc;
		struct file *fp;

		if (getsock(p->p_fd, *retval, &fp) == 0) {
			struct mbuf *m;

			m = m_get(M_WAIT, MT_SOOPTS);
			m->m_len = sizeof(int);
			*mtod(m, int *) = 0;

			/* ignore error */
			(void) sosetopt((struct socket *)fp->f_data,
				IPPROTO_IPV6, IPV6_V6ONLY, m);

			FILE_UNUSE(fp, l);
		}
	}
#endif

	return (error);
}

int
linux_sys_socketpair(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_socketpair_args /* {
		syscallarg(int) domain;
		syscallarg(int) type;
		syscallarg(int) protocol;
		syscallarg(int *) rsv;
	} */ *uap = v;
	struct sys_socketpair_args bsa;

	SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
	if (SCARG(&bsa, domain) == -1)
		return EINVAL;
	SCARG(&bsa, type) = SCARG(uap, type);
	SCARG(&bsa, protocol) = SCARG(uap, protocol);
	SCARG(&bsa, rsv) = SCARG(uap, rsv);

	return sys_socketpair(l, &bsa, retval);
}

int
linux_sys_sendto(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_sendto_args /* {
		syscallarg(int)				s;
		syscallarg(void *)			msg;
		syscallarg(int)				len;
		syscallarg(int)				flags;
		syscallarg(struct osockaddr *)		to;
		syscallarg(int)				tolen;
	} */ *uap = v;
	struct msghdr   msg;
	struct iovec    aiov;
	struct mbuf *nam;
	int bflags;
	int error;

	/* Translate message flags.  */
	bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
	if (bflags < 0)
		/* Some supported flag */
		return EINVAL;

	/* Read in and convert the sockaddr */
	error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, to),
	    SCARG(uap, tolen));
	if (error)
		return (error);
	msg.msg_flags = MSG_NAMEMBUF;

	msg.msg_name = nam;
	msg.msg_namelen = SCARG(uap, tolen);
	msg.msg_iov = &aiov;
	msg.msg_iovlen = 1;
	msg.msg_control = 0;
	aiov.iov_base = __UNCONST(SCARG(uap, msg));
	aiov.iov_len = SCARG(uap, len);

	return do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
}

int
linux_sys_sendmsg(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_sendmsg_args /* {
		syscallarg(int) s;
		syscallarg(struct msghdr *) msg;
		syscallarg(u_int) flags;
	} */ *uap = v;
	struct msghdr	msg;
	int		error;
	int		bflags;
	struct mbuf     *nam;
	u_int8_t	*control;
	struct mbuf     *ctl_mbuf = NULL;

	msg.msg_flags = MSG_IOVUSRSPACE;

	/*
	 * Translate message flags.
	 */
	bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
	if (bflags < 0)
		/* Some supported flag */
		return EINVAL;

	if (msg.msg_name) {
		/* Read in and convert the sockaddr */
		error = linux_get_sa(l, SCARG(uap, s), &nam, msg.msg_name,
		    msg.msg_namelen);
		if (error)
			return (error);
		msg.msg_flags |= MSG_NAMEMBUF;
		msg.msg_name = nam;
	}

	/*
	 * Handle cmsg if there is any.
	 */
	if (CMSG_FIRSTHDR(&msg)) {
		struct linux_cmsghdr l_cmsg, *l_cc;
		struct cmsghdr *cmsg;
		ssize_t resid = msg.msg_controllen;
		size_t clen, cidx = 0, cspace;

		ctl_mbuf = m_get(M_WAIT, MT_CONTROL);
		clen = MLEN;
		control = mtod(ctl_mbuf, void *);

		l_cc = LINUX_CMSG_FIRSTHDR(&msg);
		do {
			error = copyin(l_cc, &l_cmsg, sizeof(l_cmsg));
			if (error)
				goto done;

			/*
			 * Sanity check the control message length.
			 */
			if (l_cmsg.cmsg_len > resid
			    || l_cmsg.cmsg_len < sizeof l_cmsg) {
				error = EINVAL;
				goto done;
			}

			/*
			 * Refuse unsupported control messages, and
			 * translate fields as appropriate.
			 */
			switch (l_cmsg.cmsg_level) {
			case LINUX_SOL_SOCKET:
				/* It only differs on some archs */
				if (LINUX_SOL_SOCKET != SOL_SOCKET)
					l_cmsg.cmsg_level = SOL_SOCKET;

				switch(l_cmsg.cmsg_type) {
				case LINUX_SCM_RIGHTS:
					/* Linux SCM_RIGHTS is same as NetBSD */
					break;

				default:
					/* other types not supported */
					error = EINVAL;
					goto done;
				}
				break;
			default:
				/* pray and leave intact */
				break;
			}

			cspace = CMSG_SPACE(l_cmsg.cmsg_len - sizeof(l_cmsg));

			/* Check the buffer is big enough */
			if (__predict_false(cidx + cspace > clen)) {
				u_int8_t *nc;

				clen = cidx + cspace;
				if (clen >= PAGE_SIZE) {
					error = EINVAL;
					goto done;
				}
				nc = realloc(clen <= MLEN ? NULL : control,
						clen, M_TEMP, M_WAITOK);
				if (!nc) {
					error = ENOMEM;
					goto done;
				}
				if (cidx <= MLEN)
					/* Old buffer was in mbuf... */
					memcpy(nc, control, cidx);
				control = nc;
			}

			/* Copy header */
			cmsg = (void *)&control[cidx];
			cmsg->cmsg_len = l_cmsg.cmsg_len + LINUX_CMSG_ALIGN_DELTA;
			cmsg->cmsg_level = l_cmsg.cmsg_level;
			cmsg->cmsg_type = l_cmsg.cmsg_type;

			/* Zero are between header and data */
			memset(cmsg + 1, 0, 
				CMSG_ALIGN(sizeof(cmsg)) - sizeof(cmsg));

			/* Copyin the data */
			error = copyin(LINUX_CMSG_DATA(l_cc),
				CMSG_DATA(control),
				l_cmsg.cmsg_len - sizeof(l_cmsg));
			if (error)
				goto done;

			resid -= LINUX_CMSG_ALIGN(l_cmsg.cmsg_len);
			cidx += cspace;
		} while ((l_cc = LINUX_CMSG_NXTHDR(&msg, l_cc)) && resid > 0);

		/* If we allocated a buffer, attach to mbuf */
		if (cidx > MLEN) {
			MEXTADD(ctl_mbuf, control, clen, M_MBUF, NULL, NULL);
			ctl_mbuf->m_flags |= M_EXT_RW;
		}
		control = NULL;
		ctl_mbuf->m_len = cidx;

		msg.msg_control = ctl_mbuf;
		msg.msg_flags |= MSG_CONTROLMBUF;
	}

	error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
	/* Freed internally */
	ctl_mbuf = NULL;

done:
	if (ctl_mbuf != NULL) {
		if (control != NULL && control != mtod(ctl_mbuf, void *))
			free(control, M_MBUF);
		m_free(ctl_mbuf);
	}
	return (error);
}

int
linux_sys_recvfrom(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_recvfrom_args /* {
		syscallarg(int) s;
		syscallarg(void *) buf;
		syscallarg(int) len;
		syscallarg(int) flags;
		syscallarg(struct osockaddr *) from;
		syscallarg(int *) fromlenaddr;
	} */ *uap = v;
	int		error;
	struct sys_recvfrom_args bra;

	SCARG(&bra, s) = SCARG(uap, s);
	SCARG(&bra, buf) = SCARG(uap, buf);
	SCARG(&bra, len) = SCARG(uap, len);
	SCARG(&bra, flags) = SCARG(uap, flags);
	SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from);
	SCARG(&bra, fromlenaddr) = (socklen_t *)SCARG(uap, fromlenaddr);

	if ((error = sys_recvfrom(l, &bra, retval)))
		return (error);

	if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from))))
		return (error);

	return (0);
}

static int
linux_copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
{
	int dlen, error = 0;
	struct cmsghdr *cmsg;
	struct linux_cmsghdr linux_cmsg;
	struct mbuf *m;
	char *q, *q_end;

	if (mp->msg_controllen <= 0 || control == 0) {
		mp->msg_controllen = 0;
		free_control_mbuf(l, control, control);
		return 0;
	}

	q = (char *)mp->msg_control;
	q_end = q + mp->msg_controllen;

	for (m = control; m != NULL; ) {
		cmsg = mtod(m, struct cmsghdr *);

		/*
		 * Fixup cmsg. We handle two things:
		 * 0. different sizeof cmsg_len.
		 * 1. different values for level/type on some archs
		 * 2. different alignment of CMSG_DATA on some archs
		 */
		linux_cmsg.cmsg_len = cmsg->cmsg_len - LINUX_CMSG_ALIGN_DELTA;
		linux_cmsg.cmsg_level = cmsg->cmsg_level;
		linux_cmsg.cmsg_type = cmsg->cmsg_type;

		dlen = q_end - q;
		if (linux_cmsg.cmsg_len > dlen) {
			/* Not enough room for the parameter */
			dlen -= sizeof linux_cmsg;
			if (dlen <= 0)
				/* Discard if header wont fit */
				break;
			mp->msg_flags |= MSG_CTRUNC;
			if (linux_cmsg.cmsg_level == SOL_SOCKET
			    && linux_cmsg.cmsg_type == SCM_RIGHTS)
				/* Do not truncate me ... */
				break;
		} else
			dlen = linux_cmsg.cmsg_len - sizeof linux_cmsg;

		switch (linux_cmsg.cmsg_level) {
		case SOL_SOCKET:
			linux_cmsg.cmsg_level = LINUX_SOL_SOCKET;
			switch (linux_cmsg.cmsg_type) {
			case SCM_RIGHTS:
				/* Linux SCM_RIGHTS is same as NetBSD */
				break;

			default:
				/* other types not supported */
				error = EINVAL;
				goto done;
			}
			/* machine dependant ! */
			break;
		default:
			/* pray and leave intact */
			break;
		}

		/* There can be padding between the header and data... */
		error = copyout(&linux_cmsg, q, sizeof *cmsg);
		if (error != 0) {
			error = copyout(CCMSG_DATA(cmsg), q + sizeof linux_cmsg,
			    dlen);
		}
		if (error != 0) {
			/* We must free all the SCM_RIGHTS */
			m = control;
			break;
		}
		m = m->m_next;
		if (m == NULL || q + LINUX_CMSG_ALIGN(dlen) > q_end) {
			q += dlen;
			break;
		}
		q += LINUX_CMSG_ALIGN(dlen);
	}

  done:
	free_control_mbuf(l, control, m);

	mp->msg_controllen = q - (char *)mp->msg_control;
	return error;
}

int
linux_sys_recvmsg(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_recvmsg_args /* {
		syscallarg(int) s;
		syscallarg(struct msghdr *) msg;
		syscallarg(u_int) flags;
	} */ *uap = v;
	struct msghdr	msg;
	int		error;
	struct mbuf	*from, *control;

	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
	if (error)
		return (error);

	msg.msg_flags = linux_to_bsd_msg_flags(SCARG(uap, flags));
	if (msg.msg_flags < 0) {
		/* Some unsupported flag */
		return (EINVAL);
	}
	msg.msg_flags |= MSG_IOVUSRSPACE;

	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
	    msg.msg_control != NULL ? &control : NULL, retval);
	if (error != 0)
		return error;

	if (msg.msg_control != NULL)
		error = linux_copyout_msg_control(l, &msg, control);

	if (error == 0 && from != 0) {
		mtod(from, struct osockaddr *)->sa_family =
		    bsd_to_linux_domain(mtod(from, struct sockaddr *)->sa_family);
		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
			from);
	} else
		msg.msg_namelen = 0;

	if (from != NULL)
		m_free(from);

	if (error == 0) {
		msg.msg_flags = bsd_to_linux_msg_flags(msg.msg_flags);
		if (msg.msg_flags < 0)
			/* Some flag unsupported by Linux */
			error = EINVAL;
		else
			error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
	}

	return (error);
}

/*
 * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
 * is different, the rest matches IPPROTO_* on both systems.
 */
int
linux_to_bsd_sopt_level(int llevel)
{

	switch (llevel) {
	case LINUX_SOL_SOCKET:
		return SOL_SOCKET;
	case LINUX_SOL_IP:
		return IPPROTO_IP;
	case LINUX_SOL_TCP:
		return IPPROTO_TCP;
	case LINUX_SOL_UDP:
		return IPPROTO_UDP;
	default:
		return -1;
	}
}

/*
 * Convert Linux socket level socket option numbers to NetBSD values.
 */
int
linux_to_bsd_so_sockopt(int lopt)
{

	switch (lopt) {
	case LINUX_SO_DEBUG:
		return SO_DEBUG;
	case LINUX_SO_REUSEADDR:
		/*
		 * Linux does not implement SO_REUSEPORT, but allows reuse of a
		 * host:port pair through SO_REUSEADDR even if the address is not a
		 * multicast-address.  Effectively, this means that we should use
		 * SO_REUSEPORT to allow Linux applications to not exit with
		 * EADDRINUSE
		 */
		return SO_REUSEPORT;
	case LINUX_SO_TYPE:
		return SO_TYPE;
	case LINUX_SO_ERROR:
		return SO_ERROR;
	case LINUX_SO_DONTROUTE:
		return SO_DONTROUTE;
	case LINUX_SO_BROADCAST:
		return SO_BROADCAST;
	case LINUX_SO_SNDBUF:
		return SO_SNDBUF;
	case LINUX_SO_RCVBUF:
		return SO_RCVBUF;
	case LINUX_SO_KEEPALIVE:
		return SO_KEEPALIVE;
	case LINUX_SO_OOBINLINE:
		return SO_OOBINLINE;
	case LINUX_SO_LINGER:
		return SO_LINGER;
	case LINUX_SO_PRIORITY:
	case LINUX_SO_NO_CHECK:
	default:
		return -1;
	}
}

/*
 * Convert Linux IP level socket option number to NetBSD values.
 */
int
linux_to_bsd_ip_sockopt(int lopt)
{

	switch (lopt) {
	case LINUX_IP_TOS:
		return IP_TOS;
	case LINUX_IP_TTL:
		return IP_TTL;
	case LINUX_IP_MULTICAST_TTL:
		return IP_MULTICAST_TTL;
	case LINUX_IP_MULTICAST_LOOP:
		return IP_MULTICAST_LOOP;
	case LINUX_IP_MULTICAST_IF:
		return IP_MULTICAST_IF;
	case LINUX_IP_ADD_MEMBERSHIP:
		return IP_ADD_MEMBERSHIP;
	case LINUX_IP_DROP_MEMBERSHIP:
		return IP_DROP_MEMBERSHIP;
	default:
		return -1;
	}
}

/*
 * Convert Linux TCP level socket option number to NetBSD values.
 */
int
linux_to_bsd_tcp_sockopt(int lopt)
{

	switch (lopt) {
	case LINUX_TCP_NODELAY:
		return TCP_NODELAY;
	case LINUX_TCP_MAXSEG:
		return TCP_MAXSEG;
	default:
		return -1;
	}
}

/*
 * Convert Linux UDP level socket option number to NetBSD values.
 */
int
linux_to_bsd_udp_sockopt(int lopt)
{

	switch (lopt) {
	default:
		return -1;
	}
}

/*
 * Another reasonably straightforward function: setsockopt(2).
 * The level and option numbers are converted; the values passed
 * are not (yet) converted, the ones currently implemented don't
 * need conversion, as they are the same on both systems.
 */
int
linux_sys_setsockopt(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_setsockopt_args /* {
		syscallarg(int) s;
		syscallarg(int) level;
		syscallarg(int) optname;
		syscallarg(void *) optval;
		syscallarg(int) optlen;
	} */ *uap = v;
	struct proc *p = l->l_proc;
	struct sys_setsockopt_args bsa;
	int name;

	SCARG(&bsa, s) = SCARG(uap, s);
	SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
	SCARG(&bsa, val) = SCARG(uap, optval);
	SCARG(&bsa, valsize) = SCARG(uap, optlen);

	/*
	 * Linux supports only SOL_SOCKET for AF_LOCAL domain sockets
	 * and returns EOPNOTSUPP for other levels
	 */
	if (SCARG(&bsa, level) != SOL_SOCKET) {
		struct file *fp;
		struct socket *so;
		int error, s, family;

		/* getsock() will use the descriptor for us */
	    	if ((error = getsock(p->p_fd, SCARG(&bsa, s), &fp)) != 0)
		    	return error;

		s = splsoftnet();
		so = (struct socket *)fp->f_data;
		family = so->so_proto->pr_domain->dom_family;
		splx(s);
		FILE_UNUSE(fp, l);

		if (family == AF_LOCAL)
			return EOPNOTSUPP;
	}

	switch (SCARG(&bsa, level)) {
	case SOL_SOCKET:
		name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
		break;
	case IPPROTO_IP:
		name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
		break;
	case IPPROTO_TCP:
		name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
		break;
	case IPPROTO_UDP:
		name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
		break;
	default:
		return EINVAL;
	}

	if (name == -1)
		return EINVAL;
	SCARG(&bsa, name) = name;

	return sys_setsockopt(l, &bsa, retval);
}

/*
 * getsockopt(2) is very much the same as setsockopt(2) (see above)
 */
int
linux_sys_getsockopt(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_getsockopt_args /* {
		syscallarg(int) s;
		syscallarg(int) level;
		syscallarg(int) optname;
		syscallarg(void *) optval;
		syscallarg(int *) optlen;
	} */ *uap = v;
	struct sys_getsockopt_args bga;
	int name;

	SCARG(&bga, s) = SCARG(uap, s);
	SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
	SCARG(&bga, val) = SCARG(uap, optval);
	SCARG(&bga, avalsize) = (socklen_t *)SCARG(uap, optlen);

	switch (SCARG(&bga, level)) {
	case SOL_SOCKET:
		name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
		break;
	case IPPROTO_IP:
		name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
		break;
	case IPPROTO_TCP:
		name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
		break;
	case IPPROTO_UDP:
		name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
		break;
	default:
		return EINVAL;
	}

	if (name == -1)
		return EINVAL;
	SCARG(&bga, name) = name;

	return sys_getsockopt(l, &bga, retval);
}

#define IF_NAME_LEN 16

int
linux_getifhwaddr(struct lwp *l, register_t *retval, u_int fd,
    void *data)
{
	/* Not the full structure, just enough to map what we do here */
	struct linux_ifreq {
		char if_name[IF_NAME_LEN];
		struct osockaddr hwaddr;
	} lreq;
	struct proc *p = l->l_proc;
	struct filedesc *fdp;
	struct file *fp;
	struct ifaddr *ifa;
	struct ifnet *ifp;
	struct sockaddr_dl *sadl;
	int error, found;
	int index, ifnum;

	/*
	 * We can't emulate this ioctl by calling sys_ioctl() to run
	 * SIOCGIFCONF, because the user buffer is not of the right
	 * type to take those results.  We can't use kernel buffers to
	 * receive the results, as the implementation of sys_ioctl()
	 * and ifconf() [which implements SIOCGIFCONF] use
	 * copyin()/copyout() which will fail on kernel addresses.
	 *
	 * So, we must duplicate code from sys_ioctl() and ifconf().  Ugh.
	 */

	fdp = p->p_fd;
	if ((fp = fd_getfile(fdp, fd)) == NULL)
		return (EBADF);

	FILE_USE(fp);
	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
		error = EBADF;
		goto out;
	}

	error = copyin(data, &lreq, sizeof(lreq));
	if (error)
		goto out;
	lreq.if_name[IF_NAME_LEN-1] = '\0';		/* just in case */

	/*
	 * Try real interface name first, then fake "ethX"
	 */
	found = 0;
	IFNET_FOREACH(ifp) {
		if (found)
			break;
		if (strcmp(lreq.if_name, ifp->if_xname))
			/* not this interface */
			continue;
		found=1;
		if (IFADDR_EMPTY(ifp)) {
			error = ENODEV;
			goto out;
		}
		IFADDR_FOREACH(ifa, ifp) {
			sadl = satosdl(ifa->ifa_addr);
			/* only return ethernet addresses */
			/* XXX what about FDDI, etc. ? */
			if (sadl->sdl_family != AF_LINK ||
			    sadl->sdl_type != IFT_ETHER)
				continue;
			memcpy(&lreq.hwaddr.sa_data, CLLADDR(sadl),
			       MIN(sadl->sdl_alen,
				   sizeof(lreq.hwaddr.sa_data)));
			lreq.hwaddr.sa_family =
				sadl->sdl_family;
			error = copyout(&lreq, data, sizeof(lreq));
			goto out;
		}
	}

	if (strncmp(lreq.if_name, "eth", 3) == 0) {
		for (ifnum = 0, index = 3;
		     lreq.if_name[index] != '\0' && index < IF_NAME_LEN;
		     index++) {
			ifnum *= 10;
			ifnum += lreq.if_name[index] - '0';
		}

		error = EINVAL;			/* in case we don't find one */
		found = 0;
		IFNET_FOREACH(ifp) {
			if (found)
				break;
			memcpy(lreq.if_name, ifp->if_xname,
			       MIN(IF_NAME_LEN, IFNAMSIZ));
			IFADDR_FOREACH(ifa, ifp) {
				sadl = satosdl(ifa->ifa_addr);
				/* only return ethernet addresses */
				/* XXX what about FDDI, etc. ? */
				if (sadl->sdl_family != AF_LINK ||
				    sadl->sdl_type != IFT_ETHER)
					continue;
				if (ifnum--)
					/* not the reqested iface */
					continue;
				memcpy(&lreq.hwaddr.sa_data,
				       CLLADDR(sadl),
				       MIN(sadl->sdl_alen,
					   sizeof(lreq.hwaddr.sa_data)));
				lreq.hwaddr.sa_family =
					sadl->sdl_family;
				error = copyout(&lreq, data, sizeof(lreq));
				found = 1;
				break;
			}
		}
	} else {
		/* unknown interface, not even an "eth*" name */
		error = ENODEV;
	}

out:
	FILE_UNUSE(fp, l);
	return error;
}
#undef IF_NAME_LEN

int
linux_ioctl_socket(l, uap, retval)
	struct lwp *l;
	struct linux_sys_ioctl_args /* {
		syscallarg(int) fd;
		syscallarg(u_long) com;
		syscallarg(void *) data;
	} */ *uap;
	register_t *retval;
{
	struct proc *p = l->l_proc;
	u_long com;
	int error = 0, isdev = 0, dosys = 1;
	struct sys_ioctl_args ia;
	struct file *fp;
	struct filedesc *fdp;
	struct vnode *vp;
	int (*ioctlf)(struct file *, u_long, void *, struct lwp *);
	struct ioctl_pt pt;

	fdp = p->p_fd;
	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
		return (EBADF);

	FILE_USE(fp);

	if (fp->f_type == DTYPE_VNODE) {
		vp = (struct vnode *)fp->f_data;
		isdev = vp->v_type == VCHR;
	}

	/*
	 * Don't try to interpret socket ioctl calls that are done
	 * on a device filedescriptor, just pass them through, to
	 * emulate Linux behaviour. Use PTIOCLINUX so that the
	 * device will only handle these if it's prepared to do
	 * so, to avoid unexpected things from happening.
	 */
	if (isdev) {
		dosys = 0;
		ioctlf = fp->f_ops->fo_ioctl;
		pt.com = SCARG(uap, com);
		pt.data = SCARG(uap, data);
		error = ioctlf(fp, PTIOCLINUX, (void *)&pt, l);
		/*
		 * XXX hack: if the function returns EJUSTRETURN,
		 * it has stuffed a sysctl return value in pt.data.
		 */
		if (error == EJUSTRETURN) {
			retval[0] = (register_t)pt.data;
			error = 0;
		}
		goto out;
	}

	com = SCARG(uap, com);
	retval[0] = 0;

	switch (com) {
	case LINUX_SIOCGIFCONF:
		SCARG(&ia, com) = OOSIOCGIFCONF;
		break;
	case LINUX_SIOCGIFFLAGS:
		SCARG(&ia, com) = OSIOCGIFFLAGS;
		break;
	case LINUX_SIOCSIFFLAGS:
		SCARG(&ia, com) = OSIOCSIFFLAGS;
		break;
	case LINUX_SIOCGIFADDR:
		SCARG(&ia, com) = OOSIOCGIFADDR;
		break;
	case LINUX_SIOCGIFDSTADDR:
		SCARG(&ia, com) = OOSIOCGIFDSTADDR;
		break;
	case LINUX_SIOCGIFBRDADDR:
		SCARG(&ia, com) = OOSIOCGIFBRDADDR;
		break;
	case LINUX_SIOCGIFNETMASK:
		SCARG(&ia, com) = OOSIOCGIFNETMASK;
		break;
	case LINUX_SIOCADDMULTI:
		SCARG(&ia, com) = OSIOCADDMULTI;
		break;
	case LINUX_SIOCDELMULTI:
		SCARG(&ia, com) = OSIOCDELMULTI;
		break;
	case LINUX_SIOCGIFHWADDR:
		error = linux_getifhwaddr(l, retval, SCARG(uap, fd),
					 SCARG(uap, data));
		dosys = 0;
		break;
	default:
		error = EINVAL;
	}

out:
	FILE_UNUSE(fp, l);

	if (error ==0 && dosys) {
		SCARG(&ia, fd) = SCARG(uap, fd);
		SCARG(&ia, data) = SCARG(uap, data);
		/* XXX NJWLWP */
		error = sys_ioctl(curlwp, &ia, retval);
	}

	return error;
}

int
linux_sys_connect(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_connect_args /* {
		syscallarg(int) s;
		syscallarg(const struct sockaddr *) name;
		syscallarg(int) namelen;
	} */ *uap = v;
	int		error;
	struct mbuf *nam;

	error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, name),
	    SCARG(uap, namelen));
	if (error)
		return (error);

	error = do_sys_connect(l, SCARG(uap, s), nam);

	if (error == EISCONN) {
		struct file *fp;
		struct socket *so;
		int s, state, prflags;

		/* getsock() will use the descriptor for us */
	    	if (getsock(l->l_proc->p_fd, SCARG(uap, s), &fp) != 0)
		    	return EISCONN;

		s = splsoftnet();
		so = (struct socket *)fp->f_data;
		state = so->so_state;
		prflags = so->so_proto->pr_flags;
		splx(s);
		FILE_UNUSE(fp, l);
		/*
		 * We should only let this call succeed once per
		 * non-blocking connect; however we don't have
		 * a convenient place to keep that state..
		 */
		if ((state & SS_NBIO) && (state & SS_ISCONNECTED) &&
		    (prflags & PR_CONNREQUIRED))
			return 0;
	}

	return (error);
}

int
linux_sys_bind(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_bind_args /* {
		syscallarg(int) s;
		syscallarg(const struct osockaddr *) name;
		syscallarg(int) namelen;
	} */ *uap = v;
	int		error;
	struct mbuf     *nam;

	error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, name),
	    SCARG(uap, namelen));
	if (error)
		return (error);

	return do_sys_bind(l, SCARG(uap, s), nam);
}

int
linux_sys_getsockname(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_getsockname_args /* {
		syscallarg(int) fdes;
		syscallarg(void *) asa;
		syscallarg(int *) alen;
	} */ *uap = v;
	int error;

	if ((error = sys_getsockname(l, uap, retval)) != 0)
		return (error);

	if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
		return (error);

	return (0);
}

int
linux_sys_getpeername(struct lwp *l, void *v, register_t *retval)
{
	struct sys_getpeername_args /* {
		syscallarg(int) fdes;
		syscallarg(void *) asa;
		syscallarg(int *) alen;
	} */ *uap = v;
	int error;

	if ((error = sys_getpeername(l, uap, retval)) != 0)
		return (error);

	if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
		return (error);

	return (0);
}

/*
 * Copy the osockaddr structure pointed to by osa to mbuf, adjust
 * family and convert to sockaddr.
 */
static int
linux_get_sa(struct lwp *l, int s, struct mbuf **mp, const struct osockaddr *osa, int salen)
{
	int error, bdom;
	struct sockaddr *sa;
	struct osockaddr *kosa;
	struct mbuf *m;

	if (salen == 1 || salen > UCHAR_MAX) {
		DPRINTF(("bad osa=%p salen=%d\n", osa, salen));
		return EINVAL;
	}

	/* We'll need the address in an mbuf later, so copy into one here */
	m = m_get(M_WAIT, MT_SONAME);
	if (salen > MLEN)
		MEXTMALLOC(m, salen, M_WAITOK);

	m->m_len = salen;

	if (salen == 0)
		return 0;

	kosa = mtod(m, void *);
	if ((error = copyin(osa, kosa, salen))) {
		DPRINTF(("error %d copying osa %p len %d\n",
				error, osa, salen));
		goto bad;
	}

	ktrkuser("linux sockaddr", kosa, salen);

	bdom = linux_to_bsd_domain(kosa->sa_family);
	if (bdom == -1) {
		DPRINTF(("bad linux family=%d\n", kosa->sa_family));
		error = EINVAL;
		goto bad;
	}

	/*
	 * If the family is unspecified, use address family of the socket.
	 * This avoid triggering strict family checks in netinet/in_pcb.c et.al.
	 */
	if (bdom == AF_UNSPEC) {
		struct file *fp;
		struct socket *so;

		/* getsock() will use the descriptor for us */
		if ((error = getsock(l->l_proc->p_fd, s, &fp)) != 0)
			goto bad;

		so = (struct socket *)fp->f_data;
		bdom = so->so_proto->pr_domain->dom_family;

		FILE_UNUSE(fp, l);

		DPRINTF(("AF_UNSPEC family adjusted to %d\n", bdom));
	}

#ifdef INET6
	/*
	 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
	 * which lacks the scope id compared with RFC2553 one. If we detect
	 * the situation, reject the address and write a message to system log.
	 *
	 * Still accept addresses for which the scope id is not used.
	 */
	if (bdom == AF_INET6 && salen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)kosa;
		if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) &&
		    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
		     IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) ||
		     IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) ||
		     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
		     IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
			struct proc *p = l->l_proc;
			int uid = l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1;

			log(LOG_DEBUG,
			    "pid %d (%s), uid %d: obsolete pre-RFC2553 "
			    "sockaddr_in6 rejected",
			    p->p_pid, p->p_comm, uid);
			error = EINVAL;
			goto bad;
		}
		salen = sizeof (struct sockaddr_in6);
		sin6->sin6_scope_id = 0;
	}
#endif

	if (bdom == AF_INET)
		salen = sizeof(struct sockaddr_in);

	sa = (struct sockaddr *) kosa;
	sa->sa_family = bdom;
	sa->sa_len = salen;
	m->m_len = salen;
	ktrkuser("new sockaddr", kosa, salen);

#ifdef DEBUG_LINUX
	DPRINTF(("family %d, len = %d [ ", sa->sa_family, sa->sa_len));
	for (bdom = 0; bdom < sizeof(sa->sa_data); bdom++)
	    DPRINTF(("%02x ", (unsigned char) sa->sa_data[bdom]));
	DPRINTF(("\n"));
#endif

	*mp = m;
	return 0;

    bad:
	m_free(m);
	return error;
}

static int
linux_sa_put(struct osockaddr *osa)
{
	struct sockaddr sa;
	struct osockaddr *kosa;
	int error, bdom, len;

	/*
	 * Only read/write the sockaddr family and length part, the rest is
	 * not changed.
	 */
	len = sizeof(sa.sa_len) + sizeof(sa.sa_family);

	error = copyin(osa, &sa, len);
	if (error)
		return (error);

	bdom = bsd_to_linux_domain(sa.sa_family);
	if (bdom == -1)
		return (EINVAL);

	/* Note: we convert from sockaddr to osockaddr here, too */
	kosa = (struct osockaddr *) &sa;
	kosa->sa_family = bdom;
	error = copyout(kosa, osa, len);
	if (error)
		return (error);

	return (0);
}

#ifndef __amd64__
int
linux_sys_recv(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_recv_args /* {
		syscallarg(int) s;
		syscallarg(void *) buf;
		syscallarg(int) len;
		syscallarg(int) flags;
	} */ *uap = v;
	struct sys_recvfrom_args bra;


	SCARG(&bra, s) = SCARG(uap, s);
	SCARG(&bra, buf) = SCARG(uap, buf);
	SCARG(&bra, len) = (size_t) SCARG(uap, len);
	SCARG(&bra, flags) = SCARG(uap, flags);
	SCARG(&bra, from) = NULL;
	SCARG(&bra, fromlenaddr) = NULL;

	return (sys_recvfrom(l, &bra, retval));
}

int
linux_sys_send(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_send_args /* {
		syscallarg(int) s;
		syscallarg(void *) buf;
		syscallarg(int) len;
		syscallarg(int) flags;
	} */ *uap = v;
	struct sys_sendto_args bsa;

	SCARG(&bsa, s)		= SCARG(uap, s);
	SCARG(&bsa, buf)	= SCARG(uap, buf);
	SCARG(&bsa, len)	= SCARG(uap, len);
	SCARG(&bsa, flags)	= SCARG(uap, flags);
	SCARG(&bsa, to)		= NULL;
	SCARG(&bsa, tolen)	= 0;

	return (sys_sendto(l, &bsa, retval));
}
#endif

int
linux_sys_accept(struct lwp *l, void *v, register_t *retval)
{
	struct linux_sys_accept_args /* {
		syscallarg(int) s;
		syscallarg(struct osockaddr *) name;
		syscallarg(int *) anamelen;
	} */ *uap = v;
	int error;
	struct sys_accept_args baa;

	SCARG(&baa, s)		= SCARG(uap, s);
	SCARG(&baa, name)	= (struct sockaddr *) SCARG(uap, name);
	SCARG(&baa, anamelen)	= (unsigned int *) SCARG(uap, anamelen);

	if ((error = sys_accept(l, &baa, retval)))
		return (error);

	if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
		return (error);

	return (0);
}