Annotation of src/sys/compat/linux/common/linux_socket.c, revision 1.16
1.16 ! erh 1: /* $NetBSD: linux_socket.c,v 1.15 1996/12/22 23:02:26 fvdl Exp $ */
! 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:
97: #include <compat/linux/linux_types.h>
98: #include <compat/linux/linux_util.h>
1.10 mycroft 99: #include <compat/linux/linux_signal.h>
1.1 fvdl 100: #include <compat/linux/linux_syscallargs.h>
101: #include <compat/linux/linux_ioctl.h>
102: #include <compat/linux/linux_socket.h>
103: #include <compat/linux/linux_socketcall.h>
104: #include <compat/linux/linux_sockio.h>
105:
106: /*
1.16 ! erh 107: * The calls in this file are entered either via the linux_socketcall()
! 108: * interface or, on the Alpha, as individual syscalls. The
! 109: * linux_socketcall function does any massaging of arguments so that all
! 110: * the calls in here need not think that they are anything other
! 111: * than a normal syscall.
1.1 fvdl 112: */
1.14 christos 113:
114: int linux_to_bsd_domain __P((int));
115: int linux_to_bsd_sopt_level __P((int));
116: int linux_to_bsd_so_sockopt __P((int));
117: int linux_to_bsd_ip_sockopt __P((int));
118: int linux_to_bsd_tcp_sockopt __P((int));
119: int linux_to_bsd_udp_sockopt __P((int));
1.1 fvdl 120:
121: /*
122: * Convert between Linux and BSD socket domain values
123: */
1.2 mycroft 124: int
1.1 fvdl 125: linux_to_bsd_domain(ldom)
126: int ldom;
127: {
1.12 mycroft 128:
1.1 fvdl 129: switch (ldom) {
130: case LINUX_AF_UNSPEC:
131: return AF_UNSPEC;
132: case LINUX_AF_UNIX:
133: return AF_LOCAL;
134: case LINUX_AF_INET:
135: return AF_INET;
136: case LINUX_AF_AX25:
137: return AF_CCITT;
138: case LINUX_AF_IPX:
139: return AF_IPX;
140: case LINUX_AF_APPLETALK:
141: return AF_APPLETALK;
1.16 ! erh 142: case LINUX_AF_X25:
! 143: return AF_CCITT;
! 144: case LINUX_AF_INET6:
! 145: return AF_INET6;
! 146: case LINUX_AF_DECnet:
! 147: return AF_DECnet;
! 148: case LINUX_AF_NETLINK:
! 149: return AF_ROUTE;
! 150: /* NETROM, BRIDGE, ATMPVC, ROSE, NETBEUI, SECURITY, */
! 151: /* pseudo_AF_KEY, PACKET, ASH, ECONET, ATMSVC, SNA */
1.1 fvdl 152: default:
153: return -1;
154: }
155: }
156:
1.2 mycroft 157: int
1.16 ! erh 158: linux_sys_socket(p, v, retval)
1.1 fvdl 159: struct proc *p;
1.16 ! erh 160: void *v;
! 161: register_t *retval;
! 162: {
! 163: struct linux_sys_socket_args /* {
1.1 fvdl 164: syscallarg(int) domain;
165: syscallarg(int) type;
166: syscallarg(int) protocol;
1.16 ! erh 167: } */ *uap = v;
1.12 mycroft 168: struct sys_socket_args bsa;
1.1 fvdl 169:
1.16 ! erh 170: SCARG(&bsa, protocol) = SCARG(uap, protocol);
! 171: SCARG(&bsa, type) = SCARG(uap, type);
! 172: SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1 fvdl 173: if (SCARG(&bsa, domain) == -1)
174: return EINVAL;
1.12 mycroft 175: return sys_socket(p, &bsa, retval);
1.1 fvdl 176: }
177:
1.2 mycroft 178: int
1.16 ! erh 179: linux_sys_socketpair(p, v, retval)
1.1 fvdl 180: struct proc *p;
1.16 ! erh 181: void *v;
1.1 fvdl 182: register_t *retval;
183: {
1.16 ! erh 184: struct linux_sys_socketpair_args /* {
1.1 fvdl 185: syscallarg(int) domain;
186: syscallarg(int) type;
187: syscallarg(int) protocol;
188: syscallarg(int *) rsv;
1.16 ! erh 189: } */ *uap = v;
1.12 mycroft 190: struct sys_socketpair_args bsa;
1.1 fvdl 191:
1.16 ! erh 192: SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1 fvdl 193: if (SCARG(&bsa, domain) == -1)
194: return EINVAL;
1.16 ! erh 195: SCARG(&bsa, type) = SCARG(uap, type);
! 196: SCARG(&bsa, protocol) = SCARG(uap, protocol);
! 197: SCARG(&bsa, rsv) = SCARG(uap, rsv);
1.1 fvdl 198:
1.12 mycroft 199: return sys_socketpair(p, &bsa, retval);
1.1 fvdl 200: }
201:
1.2 mycroft 202: int
1.16 ! erh 203: linux_sys_sendto(p, v, retval)
1.1 fvdl 204: struct proc *p;
1.16 ! erh 205: void *v;
1.1 fvdl 206: register_t *retval;
207: {
1.16 ! erh 208: struct linux_sys_sendto_args /* {
1.1 fvdl 209: syscallarg(int) s;
210: syscallarg(void *) msg;
211: syscallarg(int) len;
212: syscallarg(int) flags;
213: syscallarg(sockaddr *) to;
214: syscallarg(int) tolen;
1.16 ! erh 215: } */ *uap = v;
1.12 mycroft 216: struct sys_sendto_args bsa;
1.1 fvdl 217:
1.16 ! erh 218: SCARG(&bsa, s) = SCARG(uap, s);
! 219: SCARG(&bsa, buf) = SCARG(uap, msg);
! 220: SCARG(&bsa, len) = SCARG(uap, len);
! 221: SCARG(&bsa, flags) = SCARG(uap, flags);
! 222: SCARG(&bsa, to) = (void *) SCARG(uap, to);
! 223: SCARG(&bsa, tolen) = SCARG(uap, tolen);
1.1 fvdl 224:
1.12 mycroft 225: return sys_sendto(p, &bsa, retval);
1.1 fvdl 226: }
227:
1.2 mycroft 228: int
1.16 ! erh 229: linux_sys_recvfrom(p, v, retval)
1.1 fvdl 230: struct proc *p;
1.16 ! erh 231: void *v;
! 232: register_t *retval;
! 233: {
! 234: struct linux_sys_recvfrom_args /* {
1.1 fvdl 235: syscallarg(int) s;
236: syscallarg(void *) buf;
237: syscallarg(int) len;
238: syscallarg(int) flags;
239: syscallarg(struct sockaddr *) from;
240: syscallarg(int *) fromlen;
1.16 ! erh 241: } */ *uap = v;
1.12 mycroft 242: struct compat_43_sys_recvfrom_args bra;
1.1 fvdl 243:
1.16 ! erh 244: SCARG(&bra, s) = SCARG(uap, s);
! 245: SCARG(&bra, buf) = SCARG(uap, buf);
! 246: SCARG(&bra, len) = SCARG(uap, len);
! 247: SCARG(&bra, flags) = SCARG(uap, flags);
! 248: SCARG(&bra, from) = (caddr_t) SCARG(uap, from);
! 249: SCARG(&bra, fromlenaddr) = SCARG(uap, fromlen);
1.1 fvdl 250:
1.12 mycroft 251: return compat_43_sys_recvfrom(p, &bra, retval);
1.1 fvdl 252: }
253:
254: /*
255: * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
256: * is different, the rest matches IPPROTO_* on both systems.
257: */
1.2 mycroft 258: int
1.1 fvdl 259: linux_to_bsd_sopt_level(llevel)
260: int llevel;
261: {
1.12 mycroft 262:
1.1 fvdl 263: switch (llevel) {
264: case LINUX_SOL_SOCKET:
265: return SOL_SOCKET;
1.3 mycroft 266: case LINUX_SOL_IP:
267: return IPPROTO_IP;
268: case LINUX_SOL_TCP:
269: return IPPROTO_TCP;
270: case LINUX_SOL_UDP:
271: return IPPROTO_UDP;
1.1 fvdl 272: default:
273: return -1;
274: }
275: }
276:
277: /*
278: * Convert Linux socket level socket option numbers to NetBSD values.
279: */
1.2 mycroft 280: int
1.1 fvdl 281: linux_to_bsd_so_sockopt(lopt)
282: int lopt;
283: {
1.12 mycroft 284:
1.1 fvdl 285: switch (lopt) {
286: case LINUX_SO_DEBUG:
287: return SO_DEBUG;
288: case LINUX_SO_REUSEADDR:
289: return SO_REUSEADDR;
290: case LINUX_SO_TYPE:
291: return SO_TYPE;
292: case LINUX_SO_ERROR:
293: return SO_ERROR;
294: case LINUX_SO_DONTROUTE:
295: return SO_DONTROUTE;
296: case LINUX_SO_BROADCAST:
297: return SO_BROADCAST;
298: case LINUX_SO_SNDBUF:
299: return SO_SNDBUF;
300: case LINUX_SO_RCVBUF:
301: return SO_RCVBUF;
302: case LINUX_SO_KEEPALIVE:
303: return SO_KEEPALIVE;
304: case LINUX_SO_OOBINLINE:
305: return SO_OOBINLINE;
306: case LINUX_SO_LINGER:
307: return SO_LINGER;
308: case LINUX_SO_PRIORITY:
309: case LINUX_SO_NO_CHECK:
310: default:
311: return -1;
312: }
313: }
314:
315: /*
1.3 mycroft 316: * Convert Linux IP level socket option number to NetBSD values.
317: */
318: int
319: linux_to_bsd_ip_sockopt(lopt)
320: int lopt;
321: {
1.12 mycroft 322:
1.3 mycroft 323: switch (lopt) {
324: case LINUX_IP_TOS:
325: return IP_TOS;
326: case LINUX_IP_TTL:
327: return IP_TTL;
328: case LINUX_IP_MULTICAST_TTL:
329: return IP_MULTICAST_TTL;
330: case LINUX_IP_MULTICAST_LOOP:
331: return IP_MULTICAST_LOOP;
332: case LINUX_IP_MULTICAST_IF:
333: return IP_MULTICAST_IF;
334: case LINUX_IP_ADD_MEMBERSHIP:
335: return IP_ADD_MEMBERSHIP;
336: case LINUX_IP_DROP_MEMBERSHIP:
337: return IP_DROP_MEMBERSHIP;
338: default:
339: return -1;
340: }
341: }
342:
343: /*
344: * Convert Linux TCP level socket option number to NetBSD values.
345: */
346: int
347: linux_to_bsd_tcp_sockopt(lopt)
348: int lopt;
349: {
1.12 mycroft 350:
1.3 mycroft 351: switch (lopt) {
1.6 mycroft 352: case LINUX_TCP_NODELAY:
353: return TCP_NODELAY;
354: case LINUX_TCP_MAXSEG:
355: return TCP_MAXSEG;
1.3 mycroft 356: default:
1.6 mycroft 357: return -1;
1.3 mycroft 358: }
359: }
360:
361: /*
362: * Convert Linux UDP level socket option number to NetBSD values.
363: */
364: int
365: linux_to_bsd_udp_sockopt(lopt)
366: int lopt;
367: {
1.12 mycroft 368:
1.3 mycroft 369: switch (lopt) {
370: default:
1.6 mycroft 371: return -1;
1.3 mycroft 372: }
373: }
374:
375: /*
1.5 mycroft 376: * Another reasonably straightforward function: setsockopt(2).
1.1 fvdl 377: * The level and option numbers are converted; the values passed
378: * are not (yet) converted, the ones currently implemented don't
379: * need conversion, as they are the same on both systems.
380: */
1.2 mycroft 381: int
1.16 ! erh 382: linux_sys_setsockopt(p, v, retval)
1.1 fvdl 383: struct proc *p;
1.16 ! erh 384: void *v;
! 385: register_t *retval;
! 386: {
! 387: struct linux_sys_setsockopt_args /* {
1.1 fvdl 388: syscallarg(int) s;
389: syscallarg(int) level;
390: syscallarg(int) optname;
391: syscallarg(void *) optval;
392: syscallarg(int) optlen;
1.16 ! erh 393: } */ *uap = v;
1.12 mycroft 394: struct sys_setsockopt_args bsa;
1.16 ! erh 395: int name;
1.1 fvdl 396:
1.16 ! erh 397: SCARG(&bsa, s) = SCARG(uap, s);
! 398: SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
! 399: SCARG(&bsa, val) = SCARG(uap, optval);
! 400: SCARG(&bsa, valsize) = SCARG(uap, optlen);
1.1 fvdl 401:
402: switch (SCARG(&bsa, level)) {
403: case SOL_SOCKET:
1.16 ! erh 404: name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
1.1 fvdl 405: break;
406: case IPPROTO_IP:
1.16 ! erh 407: name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
1.1 fvdl 408: break;
1.3 mycroft 409: case IPPROTO_TCP:
1.16 ! erh 410: name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
1.3 mycroft 411: break;
412: case IPPROTO_UDP:
1.16 ! erh 413: name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
1.3 mycroft 414: break;
1.1 fvdl 415: default:
416: return EINVAL;
417: }
418:
419: if (name == -1)
420: return EINVAL;
421: SCARG(&bsa, name) = name;
422:
1.12 mycroft 423: return sys_setsockopt(p, &bsa, retval);
1.1 fvdl 424: }
425:
426: /*
427: * getsockopt(2) is very much the same as setsockopt(2) (see above)
428: */
1.2 mycroft 429: int
1.16 ! erh 430: linux_sys_getsockopt(p, v, retval)
1.1 fvdl 431: struct proc *p;
1.16 ! erh 432: void *v;
! 433: register_t *retval;
! 434: {
! 435: struct linux_sys_getsockopt_args /* {
1.1 fvdl 436: syscallarg(int) s;
437: syscallarg(int) level;
438: syscallarg(int) optname;
439: syscallarg(void *) optval;
1.16 ! erh 440: syscallarg(int *) optlen;
! 441: } */ *uap = v;
1.12 mycroft 442: struct sys_getsockopt_args bga;
1.16 ! erh 443: int name;
1.1 fvdl 444:
1.16 ! erh 445: SCARG(&bga, s) = SCARG(uap, s);
! 446: SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
! 447: SCARG(&bga, val) = SCARG(uap, optval);
! 448: SCARG(&bga, avalsize) = SCARG(uap, optlen);
1.1 fvdl 449:
450: switch (SCARG(&bga, level)) {
451: case SOL_SOCKET:
1.16 ! erh 452: name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
1.1 fvdl 453: break;
454: case IPPROTO_IP:
1.16 ! erh 455: name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
1.3 mycroft 456: break;
457: case IPPROTO_TCP:
1.16 ! erh 458: name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
1.3 mycroft 459: break;
460: case IPPROTO_UDP:
1.16 ! erh 461: name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
1.1 fvdl 462: break;
463: default:
464: return EINVAL;
465: }
466:
467: if (name == -1)
468: return EINVAL;
1.5 mycroft 469: SCARG(&bga, name) = name;
1.1 fvdl 470:
1.12 mycroft 471: return sys_getsockopt(p, &bga, retval);
1.13 mycroft 472: }
473:
474: int
475: linux_ioctl_socket(p, uap, retval)
476: register struct proc *p;
477: register struct linux_sys_ioctl_args /* {
478: syscallarg(int) fd;
479: syscallarg(u_long) com;
480: syscallarg(caddr_t) data;
481: } */ *uap;
482: register_t *retval;
483: {
484: u_long com;
485: struct sys_ioctl_args ia;
486:
487: com = SCARG(uap, com);
488: retval[0] = 0;
489:
490: switch (com) {
491: case LINUX_SIOCGIFCONF:
492: SCARG(&ia, com) = OSIOCGIFCONF;
493: break;
494: case LINUX_SIOCGIFFLAGS:
495: SCARG(&ia, com) = SIOCGIFFLAGS;
496: break;
497: case LINUX_SIOCGIFADDR:
498: SCARG(&ia, com) = OSIOCGIFADDR;
499: break;
500: case LINUX_SIOCGIFDSTADDR:
501: SCARG(&ia, com) = OSIOCGIFDSTADDR;
502: break;
503: case LINUX_SIOCGIFBRDADDR:
504: SCARG(&ia, com) = OSIOCGIFBRDADDR;
505: break;
506: case LINUX_SIOCGIFNETMASK:
507: SCARG(&ia, com) = OSIOCGIFNETMASK;
508: break;
509: case LINUX_SIOCADDMULTI:
510: SCARG(&ia, com) = SIOCADDMULTI;
511: break;
512: case LINUX_SIOCDELMULTI:
513: SCARG(&ia, com) = SIOCDELMULTI;
514: break;
515: default:
516: return EINVAL;
517: }
518:
519: SCARG(&ia, fd) = SCARG(uap, fd);
520: SCARG(&ia, data) = SCARG(uap, data);
521: return sys_ioctl(p, &ia, retval);
1.1 fvdl 522: }
CVSweb <webmaster@jp.NetBSD.org>