Annotation of src/sys/compat/linux/common/linux_socket.c, revision 1.17
1.17 ! christos 1: /* $NetBSD: linux_socket.c,v 1.16 1998/10/01 03:33:18 erh Exp $ */
1.16 erh 2:
3: /*-
4: * Copyright (c) 1998 The NetBSD Foundation, Inc.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Eric Haszlakiewicz.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the NetBSD
21: * Foundation, Inc. and its contributors.
22: * 4. Neither the name of The NetBSD Foundation nor the names of its
23: * contributors may be used to endorse or promote products derived
24: * from this software without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36: * POSSIBILITY OF SUCH DAMAGE.
37: */
1.1 fvdl 38:
39: /*
40: * Copyright (c) 1995 Frank van der Linden
41: * All rights reserved.
42: *
43: * Redistribution and use in source and binary forms, with or without
44: * modification, are permitted provided that the following conditions
45: * are met:
46: * 1. Redistributions of source code must retain the above copyright
47: * notice, this list of conditions and the following disclaimer.
48: * 2. Redistributions in binary form must reproduce the above copyright
49: * notice, this list of conditions and the following disclaimer in the
50: * documentation and/or other materials provided with the distribution.
51: * 3. All advertising materials mentioning features or use of this software
52: * must display the following acknowledgement:
53: * This product includes software developed for the NetBSD Project
54: * by Frank van der Linden
55: * 4. The name of the author may not be used to endorse or promote products
56: * derived from this software without specific prior written permission
57: *
58: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68: */
69:
1.16 erh 70: /*
71: * Functions in multiarch:
72: * linux_sys_socketcall : linux_socketcall.c
73: */
74:
1.1 fvdl 75: #include <sys/param.h>
76: #include <sys/kernel.h>
77: #include <sys/systm.h>
78: #include <sys/buf.h>
79: #include <sys/malloc.h>
80: #include <sys/ioctl.h>
81: #include <sys/tty.h>
82: #include <sys/file.h>
83: #include <sys/filedesc.h>
84: #include <sys/select.h>
85: #include <sys/socket.h>
86: #include <sys/socketvar.h>
87: #include <net/if.h>
88: #include <netinet/in.h>
1.7 mycroft 89: #include <netinet/tcp.h>
1.1 fvdl 90: #include <sys/mount.h>
91: #include <sys/proc.h>
92: #include <sys/vnode.h>
93: #include <sys/device.h>
94:
95: #include <sys/syscallargs.h>
96:
1.17 ! christos 97: #include <compat/linux/common/linux_types.h>
! 98: #include <compat/linux/common/linux_util.h>
! 99: #include <compat/linux/common/linux_signal.h>
! 100: #include <compat/linux/common/linux_ioctl.h>
! 101: #include <compat/linux/common/linux_socket.h>
! 102: #include <compat/linux/common/linux_socketcall.h>
! 103: #include <compat/linux/common/linux_sockio.h>
! 104:
1.1 fvdl 105: #include <compat/linux/linux_syscallargs.h>
106:
107: /*
1.16 erh 108: * The calls in this file are entered either via the linux_socketcall()
109: * interface or, on the Alpha, as individual syscalls. The
110: * linux_socketcall function does any massaging of arguments so that all
111: * the calls in here need not think that they are anything other
112: * than a normal syscall.
1.1 fvdl 113: */
1.14 christos 114:
115: int linux_to_bsd_domain __P((int));
116: int linux_to_bsd_sopt_level __P((int));
117: int linux_to_bsd_so_sockopt __P((int));
118: int linux_to_bsd_ip_sockopt __P((int));
119: int linux_to_bsd_tcp_sockopt __P((int));
120: int linux_to_bsd_udp_sockopt __P((int));
1.1 fvdl 121:
122: /*
123: * Convert between Linux and BSD socket domain values
124: */
1.2 mycroft 125: int
1.1 fvdl 126: linux_to_bsd_domain(ldom)
127: int ldom;
128: {
1.12 mycroft 129:
1.1 fvdl 130: switch (ldom) {
131: case LINUX_AF_UNSPEC:
132: return AF_UNSPEC;
133: case LINUX_AF_UNIX:
134: return AF_LOCAL;
135: case LINUX_AF_INET:
136: return AF_INET;
137: case LINUX_AF_AX25:
138: return AF_CCITT;
139: case LINUX_AF_IPX:
140: return AF_IPX;
141: case LINUX_AF_APPLETALK:
142: return AF_APPLETALK;
1.16 erh 143: case LINUX_AF_X25:
144: return AF_CCITT;
145: case LINUX_AF_INET6:
146: return AF_INET6;
147: case LINUX_AF_DECnet:
148: return AF_DECnet;
149: case LINUX_AF_NETLINK:
150: return AF_ROUTE;
151: /* NETROM, BRIDGE, ATMPVC, ROSE, NETBEUI, SECURITY, */
152: /* pseudo_AF_KEY, PACKET, ASH, ECONET, ATMSVC, SNA */
1.1 fvdl 153: default:
154: return -1;
155: }
156: }
157:
1.2 mycroft 158: int
1.16 erh 159: linux_sys_socket(p, v, retval)
1.1 fvdl 160: struct proc *p;
1.16 erh 161: void *v;
162: register_t *retval;
163: {
164: struct linux_sys_socket_args /* {
1.1 fvdl 165: syscallarg(int) domain;
166: syscallarg(int) type;
167: syscallarg(int) protocol;
1.16 erh 168: } */ *uap = v;
1.12 mycroft 169: struct sys_socket_args bsa;
1.1 fvdl 170:
1.16 erh 171: SCARG(&bsa, protocol) = SCARG(uap, protocol);
172: SCARG(&bsa, type) = SCARG(uap, type);
173: SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1 fvdl 174: if (SCARG(&bsa, domain) == -1)
175: return EINVAL;
1.12 mycroft 176: return sys_socket(p, &bsa, retval);
1.1 fvdl 177: }
178:
1.2 mycroft 179: int
1.16 erh 180: linux_sys_socketpair(p, v, retval)
1.1 fvdl 181: struct proc *p;
1.16 erh 182: void *v;
1.1 fvdl 183: register_t *retval;
184: {
1.16 erh 185: struct linux_sys_socketpair_args /* {
1.1 fvdl 186: syscallarg(int) domain;
187: syscallarg(int) type;
188: syscallarg(int) protocol;
189: syscallarg(int *) rsv;
1.16 erh 190: } */ *uap = v;
1.12 mycroft 191: struct sys_socketpair_args bsa;
1.1 fvdl 192:
1.16 erh 193: SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1 fvdl 194: if (SCARG(&bsa, domain) == -1)
195: return EINVAL;
1.16 erh 196: SCARG(&bsa, type) = SCARG(uap, type);
197: SCARG(&bsa, protocol) = SCARG(uap, protocol);
198: SCARG(&bsa, rsv) = SCARG(uap, rsv);
1.1 fvdl 199:
1.12 mycroft 200: return sys_socketpair(p, &bsa, retval);
1.1 fvdl 201: }
202:
1.2 mycroft 203: int
1.16 erh 204: linux_sys_sendto(p, v, retval)
1.1 fvdl 205: struct proc *p;
1.16 erh 206: void *v;
1.1 fvdl 207: register_t *retval;
208: {
1.16 erh 209: struct linux_sys_sendto_args /* {
1.1 fvdl 210: syscallarg(int) s;
211: syscallarg(void *) msg;
212: syscallarg(int) len;
213: syscallarg(int) flags;
214: syscallarg(sockaddr *) to;
215: syscallarg(int) tolen;
1.16 erh 216: } */ *uap = v;
1.12 mycroft 217: struct sys_sendto_args bsa;
1.1 fvdl 218:
1.16 erh 219: SCARG(&bsa, s) = SCARG(uap, s);
220: SCARG(&bsa, buf) = SCARG(uap, msg);
221: SCARG(&bsa, len) = SCARG(uap, len);
222: SCARG(&bsa, flags) = SCARG(uap, flags);
223: SCARG(&bsa, to) = (void *) SCARG(uap, to);
224: SCARG(&bsa, tolen) = SCARG(uap, tolen);
1.1 fvdl 225:
1.12 mycroft 226: return sys_sendto(p, &bsa, retval);
1.1 fvdl 227: }
228:
1.2 mycroft 229: int
1.16 erh 230: linux_sys_recvfrom(p, v, retval)
1.1 fvdl 231: struct proc *p;
1.16 erh 232: void *v;
233: register_t *retval;
234: {
235: struct linux_sys_recvfrom_args /* {
1.1 fvdl 236: syscallarg(int) s;
237: syscallarg(void *) buf;
238: syscallarg(int) len;
239: syscallarg(int) flags;
240: syscallarg(struct sockaddr *) from;
241: syscallarg(int *) fromlen;
1.16 erh 242: } */ *uap = v;
1.12 mycroft 243: struct compat_43_sys_recvfrom_args bra;
1.1 fvdl 244:
1.16 erh 245: SCARG(&bra, s) = SCARG(uap, s);
246: SCARG(&bra, buf) = SCARG(uap, buf);
247: SCARG(&bra, len) = SCARG(uap, len);
248: SCARG(&bra, flags) = SCARG(uap, flags);
249: SCARG(&bra, from) = (caddr_t) SCARG(uap, from);
250: SCARG(&bra, fromlenaddr) = SCARG(uap, fromlen);
1.1 fvdl 251:
1.12 mycroft 252: return compat_43_sys_recvfrom(p, &bra, retval);
1.1 fvdl 253: }
254:
255: /*
256: * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
257: * is different, the rest matches IPPROTO_* on both systems.
258: */
1.2 mycroft 259: int
1.1 fvdl 260: linux_to_bsd_sopt_level(llevel)
261: int llevel;
262: {
1.12 mycroft 263:
1.1 fvdl 264: switch (llevel) {
265: case LINUX_SOL_SOCKET:
266: return SOL_SOCKET;
1.3 mycroft 267: case LINUX_SOL_IP:
268: return IPPROTO_IP;
269: case LINUX_SOL_TCP:
270: return IPPROTO_TCP;
271: case LINUX_SOL_UDP:
272: return IPPROTO_UDP;
1.1 fvdl 273: default:
274: return -1;
275: }
276: }
277:
278: /*
279: * Convert Linux socket level socket option numbers to NetBSD values.
280: */
1.2 mycroft 281: int
1.1 fvdl 282: linux_to_bsd_so_sockopt(lopt)
283: int lopt;
284: {
1.12 mycroft 285:
1.1 fvdl 286: switch (lopt) {
287: case LINUX_SO_DEBUG:
288: return SO_DEBUG;
289: case LINUX_SO_REUSEADDR:
290: return SO_REUSEADDR;
291: case LINUX_SO_TYPE:
292: return SO_TYPE;
293: case LINUX_SO_ERROR:
294: return SO_ERROR;
295: case LINUX_SO_DONTROUTE:
296: return SO_DONTROUTE;
297: case LINUX_SO_BROADCAST:
298: return SO_BROADCAST;
299: case LINUX_SO_SNDBUF:
300: return SO_SNDBUF;
301: case LINUX_SO_RCVBUF:
302: return SO_RCVBUF;
303: case LINUX_SO_KEEPALIVE:
304: return SO_KEEPALIVE;
305: case LINUX_SO_OOBINLINE:
306: return SO_OOBINLINE;
307: case LINUX_SO_LINGER:
308: return SO_LINGER;
309: case LINUX_SO_PRIORITY:
310: case LINUX_SO_NO_CHECK:
311: default:
312: return -1;
313: }
314: }
315:
316: /*
1.3 mycroft 317: * Convert Linux IP level socket option number to NetBSD values.
318: */
319: int
320: linux_to_bsd_ip_sockopt(lopt)
321: int lopt;
322: {
1.12 mycroft 323:
1.3 mycroft 324: switch (lopt) {
325: case LINUX_IP_TOS:
326: return IP_TOS;
327: case LINUX_IP_TTL:
328: return IP_TTL;
329: case LINUX_IP_MULTICAST_TTL:
330: return IP_MULTICAST_TTL;
331: case LINUX_IP_MULTICAST_LOOP:
332: return IP_MULTICAST_LOOP;
333: case LINUX_IP_MULTICAST_IF:
334: return IP_MULTICAST_IF;
335: case LINUX_IP_ADD_MEMBERSHIP:
336: return IP_ADD_MEMBERSHIP;
337: case LINUX_IP_DROP_MEMBERSHIP:
338: return IP_DROP_MEMBERSHIP;
339: default:
340: return -1;
341: }
342: }
343:
344: /*
345: * Convert Linux TCP level socket option number to NetBSD values.
346: */
347: int
348: linux_to_bsd_tcp_sockopt(lopt)
349: int lopt;
350: {
1.12 mycroft 351:
1.3 mycroft 352: switch (lopt) {
1.6 mycroft 353: case LINUX_TCP_NODELAY:
354: return TCP_NODELAY;
355: case LINUX_TCP_MAXSEG:
356: return TCP_MAXSEG;
1.3 mycroft 357: default:
1.6 mycroft 358: return -1;
1.3 mycroft 359: }
360: }
361:
362: /*
363: * Convert Linux UDP level socket option number to NetBSD values.
364: */
365: int
366: linux_to_bsd_udp_sockopt(lopt)
367: int lopt;
368: {
1.12 mycroft 369:
1.3 mycroft 370: switch (lopt) {
371: default:
1.6 mycroft 372: return -1;
1.3 mycroft 373: }
374: }
375:
376: /*
1.5 mycroft 377: * Another reasonably straightforward function: setsockopt(2).
1.1 fvdl 378: * The level and option numbers are converted; the values passed
379: * are not (yet) converted, the ones currently implemented don't
380: * need conversion, as they are the same on both systems.
381: */
1.2 mycroft 382: int
1.16 erh 383: linux_sys_setsockopt(p, v, retval)
1.1 fvdl 384: struct proc *p;
1.16 erh 385: void *v;
386: register_t *retval;
387: {
388: struct linux_sys_setsockopt_args /* {
1.1 fvdl 389: syscallarg(int) s;
390: syscallarg(int) level;
391: syscallarg(int) optname;
392: syscallarg(void *) optval;
393: syscallarg(int) optlen;
1.16 erh 394: } */ *uap = v;
1.12 mycroft 395: struct sys_setsockopt_args bsa;
1.16 erh 396: int name;
1.1 fvdl 397:
1.16 erh 398: SCARG(&bsa, s) = SCARG(uap, s);
399: SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
400: SCARG(&bsa, val) = SCARG(uap, optval);
401: SCARG(&bsa, valsize) = SCARG(uap, optlen);
1.1 fvdl 402:
403: switch (SCARG(&bsa, level)) {
404: case SOL_SOCKET:
1.16 erh 405: name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
1.1 fvdl 406: break;
407: case IPPROTO_IP:
1.16 erh 408: name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
1.1 fvdl 409: break;
1.3 mycroft 410: case IPPROTO_TCP:
1.16 erh 411: name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
1.3 mycroft 412: break;
413: case IPPROTO_UDP:
1.16 erh 414: name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
1.3 mycroft 415: break;
1.1 fvdl 416: default:
417: return EINVAL;
418: }
419:
420: if (name == -1)
421: return EINVAL;
422: SCARG(&bsa, name) = name;
423:
1.12 mycroft 424: return sys_setsockopt(p, &bsa, retval);
1.1 fvdl 425: }
426:
427: /*
428: * getsockopt(2) is very much the same as setsockopt(2) (see above)
429: */
1.2 mycroft 430: int
1.16 erh 431: linux_sys_getsockopt(p, v, retval)
1.1 fvdl 432: struct proc *p;
1.16 erh 433: void *v;
434: register_t *retval;
435: {
436: struct linux_sys_getsockopt_args /* {
1.1 fvdl 437: syscallarg(int) s;
438: syscallarg(int) level;
439: syscallarg(int) optname;
440: syscallarg(void *) optval;
1.16 erh 441: syscallarg(int *) optlen;
442: } */ *uap = v;
1.12 mycroft 443: struct sys_getsockopt_args bga;
1.16 erh 444: int name;
1.1 fvdl 445:
1.16 erh 446: SCARG(&bga, s) = SCARG(uap, s);
447: SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
448: SCARG(&bga, val) = SCARG(uap, optval);
449: SCARG(&bga, avalsize) = SCARG(uap, optlen);
1.1 fvdl 450:
451: switch (SCARG(&bga, level)) {
452: case SOL_SOCKET:
1.16 erh 453: name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
1.1 fvdl 454: break;
455: case IPPROTO_IP:
1.16 erh 456: name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
1.3 mycroft 457: break;
458: case IPPROTO_TCP:
1.16 erh 459: name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
1.3 mycroft 460: break;
461: case IPPROTO_UDP:
1.16 erh 462: name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
1.1 fvdl 463: break;
464: default:
465: return EINVAL;
466: }
467:
468: if (name == -1)
469: return EINVAL;
1.5 mycroft 470: SCARG(&bga, name) = name;
1.1 fvdl 471:
1.12 mycroft 472: return sys_getsockopt(p, &bga, retval);
1.13 mycroft 473: }
474:
475: int
476: linux_ioctl_socket(p, uap, retval)
477: register struct proc *p;
478: register struct linux_sys_ioctl_args /* {
479: syscallarg(int) fd;
480: syscallarg(u_long) com;
481: syscallarg(caddr_t) data;
482: } */ *uap;
483: register_t *retval;
484: {
485: u_long com;
486: struct sys_ioctl_args ia;
487:
488: com = SCARG(uap, com);
489: retval[0] = 0;
490:
491: switch (com) {
492: case LINUX_SIOCGIFCONF:
493: SCARG(&ia, com) = OSIOCGIFCONF;
494: break;
495: case LINUX_SIOCGIFFLAGS:
496: SCARG(&ia, com) = SIOCGIFFLAGS;
497: break;
498: case LINUX_SIOCGIFADDR:
499: SCARG(&ia, com) = OSIOCGIFADDR;
500: break;
501: case LINUX_SIOCGIFDSTADDR:
502: SCARG(&ia, com) = OSIOCGIFDSTADDR;
503: break;
504: case LINUX_SIOCGIFBRDADDR:
505: SCARG(&ia, com) = OSIOCGIFBRDADDR;
506: break;
507: case LINUX_SIOCGIFNETMASK:
508: SCARG(&ia, com) = OSIOCGIFNETMASK;
509: break;
510: case LINUX_SIOCADDMULTI:
511: SCARG(&ia, com) = SIOCADDMULTI;
512: break;
513: case LINUX_SIOCDELMULTI:
514: SCARG(&ia, com) = SIOCDELMULTI;
515: break;
516: default:
517: return EINVAL;
518: }
519:
520: SCARG(&ia, fd) = SCARG(uap, fd);
521: SCARG(&ia, data) = SCARG(uap, data);
522: return sys_ioctl(p, &ia, retval);
1.1 fvdl 523: }
CVSweb <webmaster@jp.NetBSD.org>