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
|
/* $NetBSD: linux_socket.c,v 1.21 1999/07/17 22:03:55 jtk 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/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 <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/syscallargs.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>
#include <compat/linux/common/linux_socketcall.h>
#include <compat/linux/common/linux_sockio.h>
#include <compat/linux/linux_syscallargs.h>
#ifndef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))
#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.
*/
int linux_to_bsd_domain __P((int));
int linux_to_bsd_sopt_level __P((int));
int linux_to_bsd_so_sockopt __P((int));
int linux_to_bsd_ip_sockopt __P((int));
int linux_to_bsd_tcp_sockopt __P((int));
int linux_to_bsd_udp_sockopt __P((int));
int linux_getifhwaddr __P((struct proc *, register_t *, u_int, void *));
/*
* Convert between Linux and BSD socket domain values
*/
int
linux_to_bsd_domain(ldom)
int ldom;
{
switch (ldom) {
case LINUX_AF_UNSPEC:
return AF_UNSPEC;
case LINUX_AF_UNIX:
return AF_LOCAL;
case LINUX_AF_INET:
return AF_INET;
case LINUX_AF_AX25:
return AF_CCITT;
case LINUX_AF_IPX:
return AF_IPX;
case LINUX_AF_APPLETALK:
return AF_APPLETALK;
case LINUX_AF_X25:
return AF_CCITT;
case LINUX_AF_INET6:
return AF_INET6;
case LINUX_AF_DECnet:
return AF_DECnet;
case LINUX_AF_NETLINK:
return AF_ROUTE;
/* NETROM, BRIDGE, ATMPVC, ROSE, NETBEUI, SECURITY, */
/* pseudo_AF_KEY, PACKET, ASH, ECONET, ATMSVC, SNA */
default:
return -1;
}
}
int
linux_sys_socket(p, v, retval)
struct proc *p;
void *v;
register_t *retval;
{
struct linux_sys_socket_args /* {
syscallarg(int) domain;
syscallarg(int) type;
syscallarg(int) protocol;
} */ *uap = v;
struct sys_socket_args bsa;
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;
return sys_socket(p, &bsa, retval);
}
int
linux_sys_socketpair(p, v, retval)
struct proc *p;
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(p, &bsa, retval);
}
int
linux_sys_sendto(p, v, retval)
struct proc *p;
void *v;
register_t *retval;
{
struct linux_sys_sendto_args /* {
syscallarg(int) s;
syscallarg(void *) msg;
syscallarg(int) len;
syscallarg(int) flags;
syscallarg(sockaddr *) to;
syscallarg(int) tolen;
} */ *uap = v;
struct sys_sendto_args bsa;
SCARG(&bsa, s) = SCARG(uap, s);
SCARG(&bsa, buf) = SCARG(uap, msg);
SCARG(&bsa, len) = SCARG(uap, len);
SCARG(&bsa, flags) = SCARG(uap, flags);
SCARG(&bsa, to) = (void *) SCARG(uap, to);
SCARG(&bsa, tolen) = SCARG(uap, tolen);
return sys_sendto(p, &bsa, retval);
}
int
linux_sys_recvfrom(p, v, retval)
struct proc *p;
void *v;
register_t *retval;
{
struct linux_sys_recvfrom_args /* {
syscallarg(int) s;
syscallarg(void *) buf;
syscallarg(int) len;
syscallarg(int) flags;
syscallarg(struct sockaddr *) from;
syscallarg(int *) fromlen;
} */ *uap = v;
struct compat_43_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) = (caddr_t) SCARG(uap, from);
SCARG(&bra, fromlenaddr) = SCARG(uap, fromlen);
return compat_43_sys_recvfrom(p, &bra, retval);
}
/*
* 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(llevel)
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(lopt)
int lopt;
{
switch (lopt) {
case LINUX_SO_DEBUG:
return SO_DEBUG;
case LINUX_SO_REUSEADDR:
return SO_REUSEADDR;
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(lopt)
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(lopt)
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(lopt)
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(p, v, retval)
struct proc *p;
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 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);
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(p, &bsa, retval);
}
/*
* getsockopt(2) is very much the same as setsockopt(2) (see above)
*/
int
linux_sys_getsockopt(p, v, retval)
struct proc *p;
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) = 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(p, &bga, retval);
}
#define IF_NAME_LEN 16
int
linux_getifhwaddr(p, retval, fd, data)
struct proc *p;
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 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 (fd >= fdp->fd_nfiles ||
(fp = fdp->fd_ofiles[fd]) == NULL ||
(fp->f_iflags & FIF_WANTCLOSE) != 0)
return (EBADF);
FILE_USE(fp);
if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
error = EBADF;
goto out;
}
error = copyin(data, (caddr_t)&lreq, sizeof(lreq));
if (error)
goto out;
lreq.if_name[IF_NAME_LEN-1] = '\0'; /* just in case */
/*
* Only support finding addresses for "ethX". Should we
* do otherwise? XXX
*/
if (lreq.if_name[0] == 'e' &&
lreq.if_name[1] == 't' &&
lreq.if_name[2] == 'h') {
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 */
for (ifp = ifnet.tqh_first, found = 0;
ifp != 0 && !found;
ifp = ifp->if_list.tqe_next) {
memcpy(lreq.if_name, ifp->if_xname,
MIN(IF_NAME_LEN, IFNAMSIZ));
if ((ifa = ifp->if_addrlist.tqh_first) == 0)
/* no addresses on this interface */
continue;
else
for (; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
sadl = (struct sockaddr_dl *)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((caddr_t)&lreq.hwaddr.sa_data,
LLADDR(sadl),
MIN(sadl->sdl_alen,
sizeof(lreq.hwaddr.sa_data)));
lreq.hwaddr.sa_family =
sadl->sdl_family;
error = copyout((caddr_t)&lreq, data,
sizeof(lreq));
found = 1;
break;
}
}
} else
/* not an "eth*" name */
error = EINVAL;
out:
FILE_UNUSE(fp, p);
return error;
}
#undef IF_NAME_LEN 16
int
linux_ioctl_socket(p, uap, retval)
register struct proc *p;
register struct linux_sys_ioctl_args /* {
syscallarg(int) fd;
syscallarg(u_long) com;
syscallarg(caddr_t) data;
} */ *uap;
register_t *retval;
{
u_long com;
struct sys_ioctl_args ia;
com = SCARG(uap, com);
retval[0] = 0;
switch (com) {
case LINUX_SIOCGIFCONF:
SCARG(&ia, com) = OSIOCGIFCONF;
break;
case LINUX_SIOCGIFFLAGS:
SCARG(&ia, com) = SIOCGIFFLAGS;
break;
case LINUX_SIOCGIFADDR:
SCARG(&ia, com) = OSIOCGIFADDR;
break;
case LINUX_SIOCGIFDSTADDR:
SCARG(&ia, com) = OSIOCGIFDSTADDR;
break;
case LINUX_SIOCGIFBRDADDR:
SCARG(&ia, com) = OSIOCGIFBRDADDR;
break;
case LINUX_SIOCGIFNETMASK:
SCARG(&ia, com) = OSIOCGIFNETMASK;
break;
case LINUX_SIOCADDMULTI:
SCARG(&ia, com) = SIOCADDMULTI;
break;
case LINUX_SIOCDELMULTI:
SCARG(&ia, com) = SIOCDELMULTI;
break;
case LINUX_SIOCGIFHWADDR:
return linux_getifhwaddr(p, retval, SCARG(uap, fd),
SCARG(uap, data));
default:
return EINVAL;
}
SCARG(&ia, fd) = SCARG(uap, fd);
SCARG(&ia, data) = SCARG(uap, data);
return sys_ioctl(p, &ia, retval);
}
int
linux_sys_connect(p, v, retval)
register struct proc *p;
void *v;
register_t *retval;
{
int error;
register struct sys_connect_args /* {
syscallarg(int) s;
syscallarg(const struct sockaddr *) name;
syscallarg(unsigned int) namelen;
} */ *uap = v;
error = sys_connect (p, v, retval);
if (error == EISCONN) {
struct file *fp;
register struct socket *so;
int s, state, prflags;
/* getsock() will use the descriptor for us */
if (getsock(p->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, p);
/*
* 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;
}
|