Annotation of src/sys/compat/linux/common/linux_socket.c, revision 1.102
1.102 ! njoly 1: /* $NetBSD: linux_socket.c,v 1.101 2009/06/16 15:56:10 njoly Exp $ */
1.16 erh 2:
3: /*-
1.87 ad 4: * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
1.16 erh 5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
1.18 fvdl 8: * by Frank van der Linden and Eric Haszlakiewicz.
1.16 erh 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: *
19: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29: * POSSIBILITY OF SUCH DAMAGE.
1.1 fvdl 30: */
31:
1.16 erh 32: /*
33: * Functions in multiarch:
34: * linux_sys_socketcall : linux_socketcall.c
35: */
1.35 lukem 36:
37: #include <sys/cdefs.h>
1.102 ! njoly 38: __KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.101 2009/06/16 15:56:10 njoly Exp $");
1.16 erh 39:
1.74 yamt 40: #if defined(_KERNEL_OPT)
1.32 jdolecek 41: #include "opt_inet.h"
1.74 yamt 42: #endif /* defined(_KERNEL_OPT) */
1.32 jdolecek 43:
1.1 fvdl 44: #include <sys/param.h>
45: #include <sys/kernel.h>
46: #include <sys/systm.h>
47: #include <sys/buf.h>
48: #include <sys/malloc.h>
49: #include <sys/ioctl.h>
50: #include <sys/tty.h>
51: #include <sys/file.h>
52: #include <sys/filedesc.h>
53: #include <sys/select.h>
54: #include <sys/socket.h>
55: #include <sys/socketvar.h>
1.53 jdolecek 56: #include <sys/domain.h>
1.1 fvdl 57: #include <net/if.h>
1.21 jtk 58: #include <net/if_dl.h>
59: #include <net/if_types.h>
1.1 fvdl 60: #include <netinet/in.h>
1.7 mycroft 61: #include <netinet/tcp.h>
1.1 fvdl 62: #include <sys/mount.h>
63: #include <sys/proc.h>
64: #include <sys/vnode.h>
65: #include <sys/device.h>
1.55 perry 66: #include <sys/protosw.h>
1.32 jdolecek 67: #include <sys/mbuf.h>
68: #include <sys/syslog.h>
1.49 jdolecek 69: #include <sys/exec.h>
1.62 elad 70: #include <sys/kauth.h>
1.1 fvdl 71: #include <sys/syscallargs.h>
1.73 dsl 72: #include <sys/ktrace.h>
1.1 fvdl 73:
1.60 dyoung 74: #include <lib/libkern/libkern.h>
75:
1.42 fvdl 76: #include <netinet/ip6.h>
77: #include <netinet6/ip6_var.h>
78:
1.57 christos 79: #include <compat/sys/socket.h>
1.72 christos 80: #include <compat/sys/sockio.h>
1.57 christos 81:
1.17 christos 82: #include <compat/linux/common/linux_types.h>
83: #include <compat/linux/common/linux_util.h>
84: #include <compat/linux/common/linux_signal.h>
85: #include <compat/linux/common/linux_ioctl.h>
86: #include <compat/linux/common/linux_socket.h>
1.59 manu 87: #if !defined(__alpha__) && !defined(__amd64__)
1.17 christos 88: #include <compat/linux/common/linux_socketcall.h>
1.59 manu 89: #endif
1.17 christos 90: #include <compat/linux/common/linux_sockio.h>
1.80 njoly 91: #include <compat/linux/common/linux_ipc.h>
92: #include <compat/linux/common/linux_sem.h>
1.17 christos 93:
1.1 fvdl 94: #include <compat/linux/linux_syscallargs.h>
1.21 jtk 95:
1.37 christos 96: #ifdef DEBUG_LINUX
97: #define DPRINTF(a) uprintf a
98: #else
99: #define DPRINTF(a)
100: #endif
101:
1.1 fvdl 102: /*
1.16 erh 103: * The calls in this file are entered either via the linux_socketcall()
104: * interface or, on the Alpha, as individual syscalls. The
105: * linux_socketcall function does any massaging of arguments so that all
106: * the calls in here need not think that they are anything other
107: * than a normal syscall.
1.1 fvdl 108: */
1.14 christos 109:
1.81 dsl 110: static int linux_to_bsd_domain(int);
111: static int bsd_to_linux_domain(int);
112: int linux_to_bsd_sopt_level(int);
113: int linux_to_bsd_so_sockopt(int);
114: int linux_to_bsd_ip_sockopt(int);
115: int linux_to_bsd_tcp_sockopt(int);
116: int linux_to_bsd_udp_sockopt(int);
1.97 njoly 117: int linux_getifconf(struct lwp *, register_t *, void *);
1.81 dsl 118: int linux_getifhwaddr(struct lwp *, register_t *, u_int, void *);
1.73 dsl 119: static int linux_get_sa(struct lwp *, int, struct mbuf **,
1.93 njoly 120: const struct osockaddr *, unsigned int);
1.81 dsl 121: static int linux_sa_put(struct osockaddr *osa);
122: static int linux_to_bsd_msg_flags(int);
123: static int bsd_to_linux_msg_flags(int);
1.32 jdolecek 124:
125: static const int linux_to_bsd_domain_[LINUX_AF_MAX] = {
126: AF_UNSPEC,
127: AF_UNIX,
128: AF_INET,
129: AF_CCITT, /* LINUX_AF_AX25 */
130: AF_IPX,
131: AF_APPLETALK,
132: -1, /* LINUX_AF_NETROM */
133: -1, /* LINUX_AF_BRIDGE */
134: -1, /* LINUX_AF_ATMPVC */
135: AF_CCITT, /* LINUX_AF_X25 */
136: AF_INET6,
137: -1, /* LINUX_AF_ROSE */
138: AF_DECnet,
139: -1, /* LINUX_AF_NETBEUI */
140: -1, /* LINUX_AF_SECURITY */
141: pseudo_AF_KEY,
142: AF_ROUTE, /* LINUX_AF_NETLINK */
143: -1, /* LINUX_AF_PACKET */
144: -1, /* LINUX_AF_ASH */
145: -1, /* LINUX_AF_ECONET */
146: -1, /* LINUX_AF_ATMSVC */
147: AF_SNA,
148: /* rest up to LINUX_AF_MAX-1 is not allocated */
149: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
150: };
1.55 perry 151:
1.32 jdolecek 152: static const int bsd_to_linux_domain_[AF_MAX] = {
153: LINUX_AF_UNSPEC,
154: LINUX_AF_UNIX,
155: LINUX_AF_INET,
156: -1, /* AF_IMPLINK */
157: -1, /* AF_PUP */
158: -1, /* AF_CHAOS */
159: -1, /* AF_NS */
160: -1, /* AF_ISO */
161: -1, /* AF_ECMA */
162: -1, /* AF_DATAKIT */
163: LINUX_AF_AX25, /* AF_CCITT */
164: LINUX_AF_SNA,
165: LINUX_AF_DECnet,
166: -1, /* AF_DLI */
167: -1, /* AF_LAT */
168: -1, /* AF_HYLINK */
169: LINUX_AF_APPLETALK,
170: LINUX_AF_NETLINK,
171: -1, /* AF_LINK */
172: -1, /* AF_XTP */
173: -1, /* AF_COIP */
174: -1, /* AF_CNT */
175: -1, /* pseudo_AF_RTIP */
176: LINUX_AF_IPX,
177: LINUX_AF_INET6,
178: -1, /* pseudo_AF_PIP */
179: -1, /* AF_ISDN */
180: -1, /* AF_NATM */
181: -1, /* AF_ARP */
182: LINUX_pseudo_AF_KEY,
183: -1, /* pseudo_AF_HDRCMPLT */
184: };
1.1 fvdl 185:
1.60 dyoung 186: static const struct {
187: int bfl;
188: int lfl;
189: } bsd_to_linux_msg_flags_[] = {
190: {MSG_OOB, LINUX_MSG_OOB},
191: {MSG_PEEK, LINUX_MSG_PEEK},
192: {MSG_DONTROUTE, LINUX_MSG_DONTROUTE},
193: {MSG_EOR, LINUX_MSG_EOR},
194: {MSG_TRUNC, LINUX_MSG_TRUNC},
195: {MSG_CTRUNC, LINUX_MSG_CTRUNC},
196: {MSG_WAITALL, LINUX_MSG_WAITALL},
197: {MSG_DONTWAIT, LINUX_MSG_DONTWAIT},
198: {MSG_BCAST, 0}, /* not supported, clear */
199: {MSG_MCAST, 0}, /* not supported, clear */
200: {-1, /* not supp */ LINUX_MSG_PROBE},
201: {-1, /* not supp */ LINUX_MSG_FIN},
202: {-1, /* not supp */ LINUX_MSG_SYN},
203: {-1, /* not supp */ LINUX_MSG_CONFIRM},
204: {-1, /* not supp */ LINUX_MSG_RST},
205: {-1, /* not supp */ LINUX_MSG_ERRQUEUE},
206: {-1, /* not supp */ LINUX_MSG_NOSIGNAL},
207: {-1, /* not supp */ LINUX_MSG_MORE},
1.49 jdolecek 208: };
209:
1.1 fvdl 210: /*
211: * Convert between Linux and BSD socket domain values
212: */
1.32 jdolecek 213: static int
1.84 dsl 214: linux_to_bsd_domain(int ldom)
1.1 fvdl 215: {
1.32 jdolecek 216: if (ldom < 0 || ldom >= LINUX_AF_MAX)
217: return (-1);
218:
219: return linux_to_bsd_domain_[ldom];
220: }
221:
222: /*
223: * Convert between BSD and Linux socket domain values
224: */
225: static int
1.84 dsl 226: bsd_to_linux_domain(int bdom)
1.32 jdolecek 227: {
228: if (bdom < 0 || bdom >= AF_MAX)
229: return (-1);
1.12 mycroft 230:
1.32 jdolecek 231: return bsd_to_linux_domain_[bdom];
1.1 fvdl 232: }
233:
1.49 jdolecek 234: static int
1.84 dsl 235: linux_to_bsd_msg_flags(int lflag)
1.49 jdolecek 236: {
237: int i, lfl, bfl;
238: int bflag = 0;
239:
240: if (lflag == 0)
241: return (0);
242:
1.60 dyoung 243: for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
244: bfl = bsd_to_linux_msg_flags_[i].bfl;
245: lfl = bsd_to_linux_msg_flags_[i].lfl;
1.49 jdolecek 246:
247: if (lfl == 0)
248: continue;
249:
250: if (lflag & lfl) {
251: if (bfl < 0)
252: return (-1);
253:
254: bflag |= bfl;
255: }
256: }
257:
258: return (bflag);
259: }
260:
261: static int
1.84 dsl 262: bsd_to_linux_msg_flags(int bflag)
1.49 jdolecek 263: {
264: int i, lfl, bfl;
265: int lflag = 0;
266:
267: if (bflag == 0)
268: return (0);
269:
1.60 dyoung 270: for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
271: bfl = bsd_to_linux_msg_flags_[i].bfl;
272: lfl = bsd_to_linux_msg_flags_[i].lfl;
1.49 jdolecek 273:
274: if (bfl <= 0)
275: continue;
276:
277: if (bflag & bfl) {
278: if (lfl < 0)
279: return (-1);
280:
281: lflag |= lfl;
282: }
283: }
284:
285: return (lflag);
286: }
287:
1.2 mycroft 288: int
1.85 dsl 289: linux_sys_socket(struct lwp *l, const struct linux_sys_socket_args *uap, register_t *retval)
1.16 erh 290: {
1.85 dsl 291: /* {
1.1 fvdl 292: syscallarg(int) domain;
293: syscallarg(int) type;
294: syscallarg(int) protocol;
1.85 dsl 295: } */
296: struct sys___socket30_args bsa;
1.42 fvdl 297: int error;
1.1 fvdl 298:
1.16 erh 299: SCARG(&bsa, protocol) = SCARG(uap, protocol);
300: SCARG(&bsa, type) = SCARG(uap, type);
301: SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1 fvdl 302: if (SCARG(&bsa, domain) == -1)
303: return EINVAL;
1.64 pavel 304: error = sys___socket30(l, &bsa, retval);
1.41 itojun 305:
306: #ifdef INET6
307: /*
1.42 fvdl 308: * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by
309: * default and some apps depend on this. So, set V6ONLY to 0
310: * for Linux apps if the sysctl value is set to 1.
1.41 itojun 311: */
1.42 fvdl 312: if (!error && ip6_v6only && SCARG(&bsa, domain) == PF_INET6) {
1.89 ad 313: struct socket *so;
1.42 fvdl 314:
1.89 ad 315: if (fd_getsock(*retval, &so) == 0) {
1.98 plunky 316: int val = 0;
1.42 fvdl 317:
318: /* ignore error */
1.98 plunky 319: (void)so_setsockopt(l, so, IPPROTO_IPV6, IPV6_V6ONLY,
320: &val, sizeof(val));
1.42 fvdl 321:
1.87 ad 322: fd_putfile(*retval);
1.41 itojun 323: }
324: }
325: #endif
326:
1.42 fvdl 327: return (error);
1.1 fvdl 328: }
329:
1.2 mycroft 330: int
1.85 dsl 331: linux_sys_socketpair(struct lwp *l, const struct linux_sys_socketpair_args *uap, register_t *retval)
1.1 fvdl 332: {
1.85 dsl 333: /* {
1.1 fvdl 334: syscallarg(int) domain;
335: syscallarg(int) type;
336: syscallarg(int) protocol;
337: syscallarg(int *) rsv;
1.85 dsl 338: } */
1.12 mycroft 339: struct sys_socketpair_args bsa;
1.1 fvdl 340:
1.16 erh 341: SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1 fvdl 342: if (SCARG(&bsa, domain) == -1)
343: return EINVAL;
1.16 erh 344: SCARG(&bsa, type) = SCARG(uap, type);
345: SCARG(&bsa, protocol) = SCARG(uap, protocol);
346: SCARG(&bsa, rsv) = SCARG(uap, rsv);
1.1 fvdl 347:
1.43 thorpej 348: return sys_socketpair(l, &bsa, retval);
1.1 fvdl 349: }
350:
1.2 mycroft 351: int
1.85 dsl 352: linux_sys_sendto(struct lwp *l, const struct linux_sys_sendto_args *uap, register_t *retval)
1.1 fvdl 353: {
1.85 dsl 354: /* {
1.32 jdolecek 355: syscallarg(int) s;
356: syscallarg(void *) msg;
357: syscallarg(int) len;
358: syscallarg(int) flags;
359: syscallarg(struct osockaddr *) to;
360: syscallarg(int) tolen;
1.85 dsl 361: } */
1.73 dsl 362: struct msghdr msg;
363: struct iovec aiov;
364: struct mbuf *nam;
365: int bflags;
366: int error;
1.1 fvdl 367:
1.73 dsl 368: /* Translate message flags. */
369: bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
370: if (bflags < 0)
371: /* Some supported flag */
372: return EINVAL;
1.32 jdolecek 373:
1.95 njoly 374: msg.msg_flags = 0;
375: msg.msg_name = NULL;
376: msg.msg_control = NULL;
377:
378: if (SCARG(uap, tolen)) {
379: /* Read in and convert the sockaddr */
380: error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, to),
381: SCARG(uap, tolen));
382: if (error)
383: return (error);
384: msg.msg_flags |= MSG_NAMEMBUF;
385: msg.msg_name = nam;
386: msg.msg_namelen = SCARG(uap, tolen);
387: }
1.32 jdolecek 388:
1.73 dsl 389: msg.msg_iov = &aiov;
390: msg.msg_iovlen = 1;
391: aiov.iov_base = __UNCONST(SCARG(uap, msg));
392: aiov.iov_len = SCARG(uap, len);
1.1 fvdl 393:
1.73 dsl 394: return do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
1.32 jdolecek 395: }
396:
397: int
1.85 dsl 398: linux_sys_sendmsg(struct lwp *l, const struct linux_sys_sendmsg_args *uap, register_t *retval)
1.32 jdolecek 399: {
1.85 dsl 400: /* {
1.32 jdolecek 401: syscallarg(int) s;
402: syscallarg(struct msghdr *) msg;
403: syscallarg(u_int) flags;
1.85 dsl 404: } */
1.32 jdolecek 405: struct msghdr msg;
406: int error;
1.49 jdolecek 407: int bflags;
1.73 dsl 408: struct mbuf *nam;
409: u_int8_t *control;
410: struct mbuf *ctl_mbuf = NULL;
1.32 jdolecek 411:
1.100 njoly 412: error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
413: if (error)
414: return error;
415:
1.73 dsl 416: msg.msg_flags = MSG_IOVUSRSPACE;
1.32 jdolecek 417:
1.49 jdolecek 418: /*
419: * Translate message flags.
420: */
421: bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
1.73 dsl 422: if (bflags < 0)
1.49 jdolecek 423: /* Some supported flag */
1.73 dsl 424: return EINVAL;
425:
426: if (msg.msg_name) {
427: /* Read in and convert the sockaddr */
428: error = linux_get_sa(l, SCARG(uap, s), &nam, msg.msg_name,
429: msg.msg_namelen);
430: if (error)
431: return (error);
432: msg.msg_flags |= MSG_NAMEMBUF;
433: msg.msg_name = nam;
1.32 jdolecek 434: }
435:
436: /*
1.49 jdolecek 437: * Handle cmsg if there is any.
1.32 jdolecek 438: */
1.49 jdolecek 439: if (CMSG_FIRSTHDR(&msg)) {
1.76 dsl 440: struct linux_cmsghdr l_cmsg, *l_cc;
441: struct cmsghdr *cmsg;
1.49 jdolecek 442: ssize_t resid = msg.msg_controllen;
443: size_t clen, cidx = 0, cspace;
444:
1.73 dsl 445: ctl_mbuf = m_get(M_WAIT, MT_CONTROL);
446: clen = MLEN;
447: control = mtod(ctl_mbuf, void *);
1.49 jdolecek 448:
1.76 dsl 449: l_cc = LINUX_CMSG_FIRSTHDR(&msg);
1.49 jdolecek 450: do {
1.76 dsl 451: error = copyin(l_cc, &l_cmsg, sizeof(l_cmsg));
1.49 jdolecek 452: if (error)
453: goto done;
454:
455: /*
456: * Sanity check the control message length.
457: */
1.76 dsl 458: if (l_cmsg.cmsg_len > resid
459: || l_cmsg.cmsg_len < sizeof l_cmsg) {
1.49 jdolecek 460: error = EINVAL;
461: goto done;
462: }
463:
464: /*
465: * Refuse unsupported control messages, and
466: * translate fields as appropriate.
467: */
1.76 dsl 468: switch (l_cmsg.cmsg_level) {
1.49 jdolecek 469: case LINUX_SOL_SOCKET:
470: /* It only differs on some archs */
1.73 dsl 471: if (LINUX_SOL_SOCKET != SOL_SOCKET)
1.76 dsl 472: l_cmsg.cmsg_level = SOL_SOCKET;
1.49 jdolecek 473:
1.76 dsl 474: switch(l_cmsg.cmsg_type) {
1.49 jdolecek 475: case LINUX_SCM_RIGHTS:
476: /* Linux SCM_RIGHTS is same as NetBSD */
477: break;
478:
479: default:
480: /* other types not supported */
481: error = EINVAL;
482: goto done;
483: }
484: break;
485: default:
486: /* pray and leave intact */
487: break;
488: }
489:
1.76 dsl 490: cspace = CMSG_SPACE(l_cmsg.cmsg_len - sizeof(l_cmsg));
1.49 jdolecek 491:
492: /* Check the buffer is big enough */
493: if (__predict_false(cidx + cspace > clen)) {
494: u_int8_t *nc;
495:
496: clen = cidx + cspace;
1.73 dsl 497: if (clen >= PAGE_SIZE) {
498: error = EINVAL;
499: goto done;
500: }
501: nc = realloc(clen <= MLEN ? NULL : control,
502: clen, M_TEMP, M_WAITOK);
1.49 jdolecek 503: if (!nc) {
504: error = ENOMEM;
505: goto done;
506: }
1.73 dsl 507: if (cidx <= MLEN)
508: /* Old buffer was in mbuf... */
509: memcpy(nc, control, cidx);
1.49 jdolecek 510: control = nc;
511: }
512:
513: /* Copy header */
1.76 dsl 514: cmsg = (void *)&control[cidx];
515: cmsg->cmsg_len = l_cmsg.cmsg_len + LINUX_CMSG_ALIGN_DELTA;
516: cmsg->cmsg_level = l_cmsg.cmsg_level;
517: cmsg->cmsg_type = l_cmsg.cmsg_type;
1.49 jdolecek 518:
519: /* Zero are between header and data */
1.76 dsl 520: memset(cmsg + 1, 0,
1.49 jdolecek 521: CMSG_ALIGN(sizeof(cmsg)) - sizeof(cmsg));
522:
523: /* Copyin the data */
1.76 dsl 524: error = copyin(LINUX_CMSG_DATA(l_cc),
1.49 jdolecek 525: CMSG_DATA(control),
1.76 dsl 526: l_cmsg.cmsg_len - sizeof(l_cmsg));
1.49 jdolecek 527: if (error)
528: goto done;
529:
1.76 dsl 530: resid -= LINUX_CMSG_ALIGN(l_cmsg.cmsg_len);
1.49 jdolecek 531: cidx += cspace;
1.76 dsl 532: } while ((l_cc = LINUX_CMSG_NXTHDR(&msg, l_cc)) && resid > 0);
1.49 jdolecek 533:
1.73 dsl 534: /* If we allocated a buffer, attach to mbuf */
535: if (cidx > MLEN) {
536: MEXTADD(ctl_mbuf, control, clen, M_MBUF, NULL, NULL);
537: ctl_mbuf->m_flags |= M_EXT_RW;
1.49 jdolecek 538: }
1.73 dsl 539: control = NULL;
540: ctl_mbuf->m_len = cidx;
1.49 jdolecek 541:
1.73 dsl 542: msg.msg_control = ctl_mbuf;
543: msg.msg_flags |= MSG_CONTROLMBUF;
1.101 njoly 544:
545: ktrkuser("msgcontrol", mtod(ctl_mbuf, void *),
546: msg.msg_controllen);
1.49 jdolecek 547: }
1.32 jdolecek 548:
1.73 dsl 549: error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
550: /* Freed internally */
551: ctl_mbuf = NULL;
1.32 jdolecek 552:
1.49 jdolecek 553: done:
1.73 dsl 554: if (ctl_mbuf != NULL) {
555: if (control != NULL && control != mtod(ctl_mbuf, void *))
556: free(control, M_MBUF);
557: m_free(ctl_mbuf);
558: }
1.49 jdolecek 559: return (error);
1.1 fvdl 560: }
561:
1.2 mycroft 562: int
1.85 dsl 563: linux_sys_recvfrom(struct lwp *l, const struct linux_sys_recvfrom_args *uap, register_t *retval)
1.16 erh 564: {
1.85 dsl 565: /* {
1.1 fvdl 566: syscallarg(int) s;
567: syscallarg(void *) buf;
568: syscallarg(int) len;
569: syscallarg(int) flags;
1.32 jdolecek 570: syscallarg(struct osockaddr *) from;
571: syscallarg(int *) fromlenaddr;
1.85 dsl 572: } */
1.32 jdolecek 573: int error;
574: struct sys_recvfrom_args bra;
1.1 fvdl 575:
1.16 erh 576: SCARG(&bra, s) = SCARG(uap, s);
577: SCARG(&bra, buf) = SCARG(uap, buf);
578: SCARG(&bra, len) = SCARG(uap, len);
579: SCARG(&bra, flags) = SCARG(uap, flags);
1.32 jdolecek 580: SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from);
1.61 mrg 581: SCARG(&bra, fromlenaddr) = (socklen_t *)SCARG(uap, fromlenaddr);
1.1 fvdl 582:
1.43 thorpej 583: if ((error = sys_recvfrom(l, &bra, retval)))
1.32 jdolecek 584: return (error);
585:
586: if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from))))
587: return (error);
588:
589: return (0);
590: }
591:
1.76 dsl 592: static int
593: linux_copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
594: {
595: int dlen, error = 0;
596: struct cmsghdr *cmsg;
597: struct linux_cmsghdr linux_cmsg;
598: struct mbuf *m;
599: char *q, *q_end;
600:
601: if (mp->msg_controllen <= 0 || control == 0) {
602: mp->msg_controllen = 0;
603: free_control_mbuf(l, control, control);
604: return 0;
605: }
606:
1.101 njoly 607: ktrkuser("msgcontrol", mtod(control, void *), mp->msg_controllen);
608:
1.76 dsl 609: q = (char *)mp->msg_control;
610: q_end = q + mp->msg_controllen;
611:
612: for (m = control; m != NULL; ) {
613: cmsg = mtod(m, struct cmsghdr *);
614:
615: /*
616: * Fixup cmsg. We handle two things:
617: * 0. different sizeof cmsg_len.
618: * 1. different values for level/type on some archs
619: * 2. different alignment of CMSG_DATA on some archs
620: */
621: linux_cmsg.cmsg_len = cmsg->cmsg_len - LINUX_CMSG_ALIGN_DELTA;
622: linux_cmsg.cmsg_level = cmsg->cmsg_level;
623: linux_cmsg.cmsg_type = cmsg->cmsg_type;
624:
625: dlen = q_end - q;
626: if (linux_cmsg.cmsg_len > dlen) {
627: /* Not enough room for the parameter */
628: dlen -= sizeof linux_cmsg;
629: if (dlen <= 0)
630: /* Discard if header wont fit */
631: break;
632: mp->msg_flags |= MSG_CTRUNC;
633: if (linux_cmsg.cmsg_level == SOL_SOCKET
634: && linux_cmsg.cmsg_type == SCM_RIGHTS)
635: /* Do not truncate me ... */
636: break;
637: } else
638: dlen = linux_cmsg.cmsg_len - sizeof linux_cmsg;
639:
640: switch (linux_cmsg.cmsg_level) {
641: case SOL_SOCKET:
642: linux_cmsg.cmsg_level = LINUX_SOL_SOCKET;
643: switch (linux_cmsg.cmsg_type) {
644: case SCM_RIGHTS:
645: /* Linux SCM_RIGHTS is same as NetBSD */
646: break;
647:
648: default:
649: /* other types not supported */
650: error = EINVAL;
651: goto done;
652: }
653: /* machine dependant ! */
654: break;
655: default:
656: /* pray and leave intact */
657: break;
658: }
659:
660: /* There can be padding between the header and data... */
1.102 ! njoly 661: error = copyout(&linux_cmsg, q, sizeof linux_cmsg);
1.76 dsl 662: if (error != 0) {
663: error = copyout(CCMSG_DATA(cmsg), q + sizeof linux_cmsg,
664: dlen);
665: }
666: if (error != 0) {
667: /* We must free all the SCM_RIGHTS */
668: m = control;
669: break;
670: }
671: m = m->m_next;
672: if (m == NULL || q + LINUX_CMSG_ALIGN(dlen) > q_end) {
673: q += dlen;
674: break;
675: }
676: q += LINUX_CMSG_ALIGN(dlen);
677: }
678:
679: done:
680: free_control_mbuf(l, control, m);
681:
682: mp->msg_controllen = q - (char *)mp->msg_control;
683: return error;
684: }
685:
1.32 jdolecek 686: int
1.85 dsl 687: linux_sys_recvmsg(struct lwp *l, const struct linux_sys_recvmsg_args *uap, register_t *retval)
1.32 jdolecek 688: {
1.85 dsl 689: /* {
1.32 jdolecek 690: syscallarg(int) s;
691: syscallarg(struct msghdr *) msg;
692: syscallarg(u_int) flags;
1.85 dsl 693: } */
1.32 jdolecek 694: struct msghdr msg;
695: int error;
1.76 dsl 696: struct mbuf *from, *control;
1.32 jdolecek 697:
1.76 dsl 698: error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
699: if (error)
700: return (error);
1.49 jdolecek 701:
1.76 dsl 702: msg.msg_flags = linux_to_bsd_msg_flags(SCARG(uap, flags));
703: if (msg.msg_flags < 0) {
1.49 jdolecek 704: /* Some unsupported flag */
705: return (EINVAL);
706: }
1.76 dsl 707: msg.msg_flags |= MSG_IOVUSRSPACE;
1.49 jdolecek 708:
1.76 dsl 709: error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
710: msg.msg_control != NULL ? &control : NULL, retval);
711: if (error != 0)
712: return error;
713:
714: if (msg.msg_control != NULL)
715: error = linux_copyout_msg_control(l, &msg, control);
716:
717: if (error == 0 && from != 0) {
718: mtod(from, struct osockaddr *)->sa_family =
719: bsd_to_linux_domain(mtod(from, struct sockaddr *)->sa_family);
720: error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
721: from);
722: } else
723: msg.msg_namelen = 0;
724:
725: if (from != NULL)
726: m_free(from);
727:
728: if (error == 0) {
729: msg.msg_flags = bsd_to_linux_msg_flags(msg.msg_flags);
730: if (msg.msg_flags < 0)
731: /* Some flag unsupported by Linux */
732: error = EINVAL;
1.101 njoly 733: else {
734: ktrkuser("msghdr", &msg, sizeof(msg));
1.76 dsl 735: error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
1.101 njoly 736: }
1.49 jdolecek 737: }
738:
1.32 jdolecek 739: return (error);
1.1 fvdl 740: }
741:
742: /*
743: * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
744: * is different, the rest matches IPPROTO_* on both systems.
745: */
1.2 mycroft 746: int
1.84 dsl 747: linux_to_bsd_sopt_level(int llevel)
1.1 fvdl 748: {
1.12 mycroft 749:
1.1 fvdl 750: switch (llevel) {
751: case LINUX_SOL_SOCKET:
752: return SOL_SOCKET;
1.3 mycroft 753: case LINUX_SOL_IP:
754: return IPPROTO_IP;
755: case LINUX_SOL_TCP:
756: return IPPROTO_TCP;
757: case LINUX_SOL_UDP:
758: return IPPROTO_UDP;
1.1 fvdl 759: default:
760: return -1;
761: }
762: }
763:
764: /*
765: * Convert Linux socket level socket option numbers to NetBSD values.
766: */
1.2 mycroft 767: int
1.84 dsl 768: linux_to_bsd_so_sockopt(int lopt)
1.1 fvdl 769: {
1.12 mycroft 770:
1.1 fvdl 771: switch (lopt) {
772: case LINUX_SO_DEBUG:
773: return SO_DEBUG;
774: case LINUX_SO_REUSEADDR:
1.55 perry 775: /*
1.39 jschauma 776: * Linux does not implement SO_REUSEPORT, but allows reuse of a
777: * host:port pair through SO_REUSEADDR even if the address is not a
778: * multicast-address. Effectively, this means that we should use
779: * SO_REUSEPORT to allow Linux applications to not exit with
780: * EADDRINUSE
781: */
782: return SO_REUSEPORT;
1.1 fvdl 783: case LINUX_SO_TYPE:
784: return SO_TYPE;
785: case LINUX_SO_ERROR:
786: return SO_ERROR;
787: case LINUX_SO_DONTROUTE:
788: return SO_DONTROUTE;
789: case LINUX_SO_BROADCAST:
790: return SO_BROADCAST;
791: case LINUX_SO_SNDBUF:
792: return SO_SNDBUF;
793: case LINUX_SO_RCVBUF:
794: return SO_RCVBUF;
795: case LINUX_SO_KEEPALIVE:
796: return SO_KEEPALIVE;
797: case LINUX_SO_OOBINLINE:
798: return SO_OOBINLINE;
799: case LINUX_SO_LINGER:
800: return SO_LINGER;
801: case LINUX_SO_PRIORITY:
802: case LINUX_SO_NO_CHECK:
803: default:
804: return -1;
805: }
806: }
807:
808: /*
1.3 mycroft 809: * Convert Linux IP level socket option number to NetBSD values.
810: */
811: int
1.84 dsl 812: linux_to_bsd_ip_sockopt(int lopt)
1.3 mycroft 813: {
1.12 mycroft 814:
1.3 mycroft 815: switch (lopt) {
816: case LINUX_IP_TOS:
817: return IP_TOS;
818: case LINUX_IP_TTL:
819: return IP_TTL;
820: case LINUX_IP_MULTICAST_TTL:
821: return IP_MULTICAST_TTL;
822: case LINUX_IP_MULTICAST_LOOP:
823: return IP_MULTICAST_LOOP;
824: case LINUX_IP_MULTICAST_IF:
825: return IP_MULTICAST_IF;
826: case LINUX_IP_ADD_MEMBERSHIP:
827: return IP_ADD_MEMBERSHIP;
828: case LINUX_IP_DROP_MEMBERSHIP:
829: return IP_DROP_MEMBERSHIP;
830: default:
831: return -1;
832: }
833: }
834:
835: /*
836: * Convert Linux TCP level socket option number to NetBSD values.
837: */
838: int
1.84 dsl 839: linux_to_bsd_tcp_sockopt(int lopt)
1.3 mycroft 840: {
1.12 mycroft 841:
1.3 mycroft 842: switch (lopt) {
1.6 mycroft 843: case LINUX_TCP_NODELAY:
844: return TCP_NODELAY;
845: case LINUX_TCP_MAXSEG:
846: return TCP_MAXSEG;
1.3 mycroft 847: default:
1.6 mycroft 848: return -1;
1.3 mycroft 849: }
850: }
851:
852: /*
853: * Convert Linux UDP level socket option number to NetBSD values.
854: */
855: int
1.84 dsl 856: linux_to_bsd_udp_sockopt(int lopt)
1.3 mycroft 857: {
1.12 mycroft 858:
1.3 mycroft 859: switch (lopt) {
860: default:
1.6 mycroft 861: return -1;
1.3 mycroft 862: }
863: }
864:
865: /*
1.5 mycroft 866: * Another reasonably straightforward function: setsockopt(2).
1.1 fvdl 867: * The level and option numbers are converted; the values passed
868: * are not (yet) converted, the ones currently implemented don't
869: * need conversion, as they are the same on both systems.
870: */
1.2 mycroft 871: int
1.85 dsl 872: linux_sys_setsockopt(struct lwp *l, const struct linux_sys_setsockopt_args *uap, register_t *retval)
1.16 erh 873: {
1.85 dsl 874: /* {
1.1 fvdl 875: syscallarg(int) s;
876: syscallarg(int) level;
877: syscallarg(int) optname;
878: syscallarg(void *) optval;
879: syscallarg(int) optlen;
1.85 dsl 880: } */
1.12 mycroft 881: struct sys_setsockopt_args bsa;
1.16 erh 882: int name;
1.1 fvdl 883:
1.16 erh 884: SCARG(&bsa, s) = SCARG(uap, s);
885: SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
886: SCARG(&bsa, val) = SCARG(uap, optval);
887: SCARG(&bsa, valsize) = SCARG(uap, optlen);
1.1 fvdl 888:
1.70 mlelstv 889: /*
890: * Linux supports only SOL_SOCKET for AF_LOCAL domain sockets
891: * and returns EOPNOTSUPP for other levels
892: */
893: if (SCARG(&bsa, level) != SOL_SOCKET) {
894: struct socket *so;
1.89 ad 895: int error, family;
1.70 mlelstv 896:
1.96 ad 897: /* fd_getsock() will use the descriptor for us */
1.89 ad 898: if ((error = fd_getsock(SCARG(&bsa, s), &so)) != 0)
1.70 mlelstv 899: return error;
900: family = so->so_proto->pr_domain->dom_family;
1.87 ad 901: fd_putfile(SCARG(&bsa, s));
1.70 mlelstv 902:
903: if (family == AF_LOCAL)
904: return EOPNOTSUPP;
905: }
906:
1.1 fvdl 907: switch (SCARG(&bsa, level)) {
1.40 itojun 908: case SOL_SOCKET:
909: name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
910: break;
911: case IPPROTO_IP:
912: name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
913: break;
914: case IPPROTO_TCP:
915: name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
916: break;
917: case IPPROTO_UDP:
918: name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
919: break;
920: default:
921: return EINVAL;
1.1 fvdl 922: }
923:
924: if (name == -1)
925: return EINVAL;
926: SCARG(&bsa, name) = name;
927:
1.43 thorpej 928: return sys_setsockopt(l, &bsa, retval);
1.1 fvdl 929: }
930:
931: /*
932: * getsockopt(2) is very much the same as setsockopt(2) (see above)
933: */
1.2 mycroft 934: int
1.85 dsl 935: linux_sys_getsockopt(struct lwp *l, const struct linux_sys_getsockopt_args *uap, register_t *retval)
1.16 erh 936: {
1.85 dsl 937: /* {
1.1 fvdl 938: syscallarg(int) s;
939: syscallarg(int) level;
940: syscallarg(int) optname;
941: syscallarg(void *) optval;
1.16 erh 942: syscallarg(int *) optlen;
1.85 dsl 943: } */
1.12 mycroft 944: struct sys_getsockopt_args bga;
1.16 erh 945: int name;
1.1 fvdl 946:
1.16 erh 947: SCARG(&bga, s) = SCARG(uap, s);
948: SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
949: SCARG(&bga, val) = SCARG(uap, optval);
1.61 mrg 950: SCARG(&bga, avalsize) = (socklen_t *)SCARG(uap, optlen);
1.1 fvdl 951:
952: switch (SCARG(&bga, level)) {
1.40 itojun 953: case SOL_SOCKET:
954: name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
955: break;
956: case IPPROTO_IP:
957: name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
958: break;
959: case IPPROTO_TCP:
960: name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
961: break;
962: case IPPROTO_UDP:
963: name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
964: break;
965: default:
966: return EINVAL;
1.1 fvdl 967: }
968:
969: if (name == -1)
970: return EINVAL;
1.5 mycroft 971: SCARG(&bga, name) = name;
1.1 fvdl 972:
1.43 thorpej 973: return sys_getsockopt(l, &bga, retval);
1.13 mycroft 974: }
975:
1.97 njoly 976: int
977: linux_getifconf(struct lwp *l, register_t *retval, void *data)
978: {
979: struct linux_ifreq ifr, *ifrp;
980: struct ifconf *ifc = data;
981: struct ifnet *ifp;
982: struct ifaddr *ifa;
983: struct sockaddr *sa;
984: struct osockaddr *osa;
985: int space, error = 0;
986: const int sz = (int)sizeof(ifr);
987:
988: ifrp = (struct linux_ifreq *)ifc->ifc_req;
989: if (ifrp == NULL)
990: space = 0;
991: else
992: space = ifc->ifc_len;
993:
994: IFNET_FOREACH(ifp) {
995: (void)strncpy(ifr.ifr_name, ifp->if_xname,
996: sizeof(ifr.ifr_name));
997: if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
998: return ENAMETOOLONG;
999: if (IFADDR_EMPTY(ifp))
1000: continue;
1001: IFADDR_FOREACH(ifa, ifp) {
1002: sa = ifa->ifa_addr;
1003: if (sa->sa_family != AF_INET ||
1004: sa->sa_len > sizeof(*osa))
1005: continue;
1006: memcpy(&ifr.ifr_addr, sa, sa->sa_len);
1007: osa = (struct osockaddr *)&ifr.ifr_addr;
1008: osa->sa_family = sa->sa_family;
1009: if (space >= sz) {
1010: error = copyout(&ifr, ifrp, sz);
1011: if (error != 0)
1012: return error;
1013: ifrp++;
1014: }
1015: space -= sz;
1016: }
1017: }
1018:
1019: if (ifrp != NULL)
1020: ifc->ifc_len -= space;
1021: else
1022: ifc->ifc_len = -space;
1023:
1024: return 0;
1025: }
1.21 jtk 1026:
1027: int
1.68 christos 1028: linux_getifhwaddr(struct lwp *l, register_t *retval, u_int fd,
1.67 christos 1029: void *data)
1.21 jtk 1030: {
1031: /* Not the full structure, just enough to map what we do here */
1.97 njoly 1032: struct linux_ifreq lreq;
1.87 ad 1033: file_t *fp;
1.21 jtk 1034: struct ifaddr *ifa;
1035: struct ifnet *ifp;
1036: struct sockaddr_dl *sadl;
1037: int error, found;
1038: int index, ifnum;
1039:
1040: /*
1041: * We can't emulate this ioctl by calling sys_ioctl() to run
1042: * SIOCGIFCONF, because the user buffer is not of the right
1043: * type to take those results. We can't use kernel buffers to
1044: * receive the results, as the implementation of sys_ioctl()
1045: * and ifconf() [which implements SIOCGIFCONF] use
1046: * copyin()/copyout() which will fail on kernel addresses.
1047: *
1048: * So, we must duplicate code from sys_ioctl() and ifconf(). Ugh.
1049: */
1050:
1.87 ad 1051: if ((fp = fd_getfile(fd)) == NULL)
1.21 jtk 1052: return (EBADF);
1053:
1.90 ad 1054: KERNEL_LOCK(1, NULL);
1055:
1.21 jtk 1056: if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
1057: error = EBADF;
1058: goto out;
1059: }
1060:
1.73 dsl 1061: error = copyin(data, &lreq, sizeof(lreq));
1.21 jtk 1062: if (error)
1063: goto out;
1.97 njoly 1064: lreq.ifr_name[LINUX_IFNAMSIZ-1] = '\0'; /* just in case */
1.21 jtk 1065:
1066: /*
1.22 jdolecek 1067: * Try real interface name first, then fake "ethX"
1.21 jtk 1068: */
1.82 dyoung 1069: found = 0;
1070: IFNET_FOREACH(ifp) {
1071: if (found)
1072: break;
1.97 njoly 1073: if (strcmp(lreq.ifr_name, ifp->if_xname))
1.22 jdolecek 1074: /* not this interface */
1075: continue;
1.55 perry 1076: found=1;
1.83 dyoung 1077: if (IFADDR_EMPTY(ifp)) {
1.22 jdolecek 1078: error = ENODEV;
1079: goto out;
1080: }
1.82 dyoung 1081: IFADDR_FOREACH(ifa, ifp) {
1082: sadl = satosdl(ifa->ifa_addr);
1083: /* only return ethernet addresses */
1084: /* XXX what about FDDI, etc. ? */
1085: if (sadl->sdl_family != AF_LINK ||
1086: sadl->sdl_type != IFT_ETHER)
1087: continue;
1.97 njoly 1088: memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl),
1.82 dyoung 1089: MIN(sadl->sdl_alen,
1.97 njoly 1090: sizeof(lreq.ifr_hwaddr.sa_data)));
1091: lreq.ifr_hwaddr.sa_family =
1.82 dyoung 1092: sadl->sdl_family;
1093: error = copyout(&lreq, data, sizeof(lreq));
1094: goto out;
1095: }
1.22 jdolecek 1096: }
1097:
1.97 njoly 1098: if (strncmp(lreq.ifr_name, "eth", 3) == 0) {
1.21 jtk 1099: for (ifnum = 0, index = 3;
1.97 njoly 1100: lreq.ifr_name[index] != '\0' && index < LINUX_IFNAMSIZ;
1.21 jtk 1101: index++) {
1102: ifnum *= 10;
1.97 njoly 1103: ifnum += lreq.ifr_name[index] - '0';
1.21 jtk 1104: }
1105:
1106: error = EINVAL; /* in case we don't find one */
1.82 dyoung 1107: found = 0;
1108: IFNET_FOREACH(ifp) {
1109: if (found)
1110: break;
1.97 njoly 1111: memcpy(lreq.ifr_name, ifp->if_xname,
1112: MIN(LINUX_IFNAMSIZ, IFNAMSIZ));
1.82 dyoung 1113: IFADDR_FOREACH(ifa, ifp) {
1114: sadl = satosdl(ifa->ifa_addr);
1115: /* only return ethernet addresses */
1116: /* XXX what about FDDI, etc. ? */
1117: if (sadl->sdl_family != AF_LINK ||
1118: sadl->sdl_type != IFT_ETHER)
1119: continue;
1120: if (ifnum--)
1121: /* not the reqested iface */
1122: continue;
1.97 njoly 1123: memcpy(&lreq.ifr_hwaddr.sa_data,
1.82 dyoung 1124: CLLADDR(sadl),
1125: MIN(sadl->sdl_alen,
1.97 njoly 1126: sizeof(lreq.ifr_hwaddr.sa_data)));
1127: lreq.ifr_hwaddr.sa_family =
1.82 dyoung 1128: sadl->sdl_family;
1129: error = copyout(&lreq, data, sizeof(lreq));
1130: found = 1;
1131: break;
1132: }
1.21 jtk 1133: }
1.22 jdolecek 1134: } else {
1135: /* unknown interface, not even an "eth*" name */
1136: error = ENODEV;
1137: }
1.55 perry 1138:
1.21 jtk 1139: out:
1.90 ad 1140: KERNEL_UNLOCK_ONE(NULL);
1.87 ad 1141: fd_putfile(fd);
1.21 jtk 1142: return error;
1143: }
1144:
1.13 mycroft 1145: int
1.85 dsl 1146: linux_ioctl_socket(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
1147: {
1148: /* {
1.13 mycroft 1149: syscallarg(int) fd;
1150: syscallarg(u_long) com;
1.71 christos 1151: syscallarg(void *) data;
1.85 dsl 1152: } */
1.13 mycroft 1153: u_long com;
1.25 fvdl 1154: int error = 0, isdev = 0, dosys = 1;
1.13 mycroft 1155: struct sys_ioctl_args ia;
1.87 ad 1156: file_t *fp;
1.25 fvdl 1157: struct vnode *vp;
1.87 ad 1158: int (*ioctlf)(file_t *, u_long, void *);
1.25 fvdl 1159: struct ioctl_pt pt;
1160:
1.87 ad 1161: if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
1.25 fvdl 1162: return (EBADF);
1163:
1164: if (fp->f_type == DTYPE_VNODE) {
1165: vp = (struct vnode *)fp->f_data;
1166: isdev = vp->v_type == VCHR;
1167: }
1168:
1169: /*
1170: * Don't try to interpret socket ioctl calls that are done
1171: * on a device filedescriptor, just pass them through, to
1172: * emulate Linux behaviour. Use PTIOCLINUX so that the
1173: * device will only handle these if it's prepared to do
1174: * so, to avoid unexpected things from happening.
1175: */
1176: if (isdev) {
1.26 fvdl 1177: dosys = 0;
1.25 fvdl 1178: ioctlf = fp->f_ops->fo_ioctl;
1179: pt.com = SCARG(uap, com);
1180: pt.data = SCARG(uap, data);
1.87 ad 1181: error = ioctlf(fp, PTIOCLINUX, &pt);
1.27 fvdl 1182: /*
1.55 perry 1183: * XXX hack: if the function returns EJUSTRETURN,
1.27 fvdl 1184: * it has stuffed a sysctl return value in pt.data.
1185: */
1186: if (error == EJUSTRETURN) {
1187: retval[0] = (register_t)pt.data;
1188: error = 0;
1189: }
1.25 fvdl 1190: goto out;
1191: }
1.13 mycroft 1192:
1193: com = SCARG(uap, com);
1194: retval[0] = 0;
1195:
1196: switch (com) {
1197: case LINUX_SIOCGIFCONF:
1.97 njoly 1198: error = linux_getifconf(l, retval, SCARG(uap, data));
1199: dosys = 0;
1.13 mycroft 1200: break;
1201: case LINUX_SIOCGIFFLAGS:
1.72 christos 1202: SCARG(&ia, com) = OSIOCGIFFLAGS;
1.13 mycroft 1203: break;
1.25 fvdl 1204: case LINUX_SIOCSIFFLAGS:
1.72 christos 1205: SCARG(&ia, com) = OSIOCSIFFLAGS;
1.25 fvdl 1206: break;
1.13 mycroft 1207: case LINUX_SIOCGIFADDR:
1.72 christos 1208: SCARG(&ia, com) = OOSIOCGIFADDR;
1.13 mycroft 1209: break;
1210: case LINUX_SIOCGIFDSTADDR:
1.72 christos 1211: SCARG(&ia, com) = OOSIOCGIFDSTADDR;
1.13 mycroft 1212: break;
1213: case LINUX_SIOCGIFBRDADDR:
1.72 christos 1214: SCARG(&ia, com) = OOSIOCGIFBRDADDR;
1.13 mycroft 1215: break;
1216: case LINUX_SIOCGIFNETMASK:
1.72 christos 1217: SCARG(&ia, com) = OOSIOCGIFNETMASK;
1.13 mycroft 1218: break;
1219: case LINUX_SIOCADDMULTI:
1.72 christos 1220: SCARG(&ia, com) = OSIOCADDMULTI;
1.13 mycroft 1221: break;
1222: case LINUX_SIOCDELMULTI:
1.72 christos 1223: SCARG(&ia, com) = OSIOCDELMULTI;
1.13 mycroft 1224: break;
1.21 jtk 1225: case LINUX_SIOCGIFHWADDR:
1.73 dsl 1226: error = linux_getifhwaddr(l, retval, SCARG(uap, fd),
1.87 ad 1227: SCARG(uap, data));
1.29 fvdl 1228: dosys = 0;
1.25 fvdl 1229: break;
1.13 mycroft 1230: default:
1.25 fvdl 1231: error = EINVAL;
1232: }
1233:
1.87 ad 1234: out:
1235: fd_putfile(SCARG(uap, fd));
1.25 fvdl 1236:
1237: if (error ==0 && dosys) {
1238: SCARG(&ia, fd) = SCARG(uap, fd);
1239: SCARG(&ia, data) = SCARG(uap, data);
1.43 thorpej 1240: error = sys_ioctl(curlwp, &ia, retval);
1.13 mycroft 1241: }
1242:
1.25 fvdl 1243: return error;
1.1 fvdl 1244: }
1.19 sommerfe 1245:
1246: int
1.85 dsl 1247: linux_sys_connect(struct lwp *l, const struct linux_sys_connect_args *uap, register_t *retval)
1.19 sommerfe 1248: {
1.85 dsl 1249: /* {
1.19 sommerfe 1250: syscallarg(int) s;
1251: syscallarg(const struct sockaddr *) name;
1.32 jdolecek 1252: syscallarg(int) namelen;
1.85 dsl 1253: } */
1.32 jdolecek 1254: int error;
1.73 dsl 1255: struct mbuf *nam;
1256:
1257: error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, name),
1258: SCARG(uap, namelen));
1.32 jdolecek 1259: if (error)
1260: return (error);
1.55 perry 1261:
1.73 dsl 1262: error = do_sys_connect(l, SCARG(uap, s), nam);
1.19 sommerfe 1263:
1264: if (error == EISCONN) {
1.23 augustss 1265: struct socket *so;
1.91 ad 1266: int state, prflags, nbio;
1.55 perry 1267:
1.96 ad 1268: /* fd_getsock() will use the descriptor for us */
1.89 ad 1269: if (fd_getsock(SCARG(uap, s), &so) != 0)
1.19 sommerfe 1270: return EISCONN;
1271:
1.91 ad 1272: solock(so);
1.19 sommerfe 1273: state = so->so_state;
1.86 ad 1274: nbio = so->so_nbio;
1.19 sommerfe 1275: prflags = so->so_proto->pr_flags;
1.91 ad 1276: sounlock(so);
1.87 ad 1277: fd_putfile(SCARG(uap, s));
1.19 sommerfe 1278: /*
1279: * We should only let this call succeed once per
1280: * non-blocking connect; however we don't have
1281: * a convenient place to keep that state..
1282: */
1.86 ad 1283: if (nbio && (state & SS_ISCONNECTED) &&
1.19 sommerfe 1284: (prflags & PR_CONNREQUIRED))
1285: return 0;
1286: }
1287:
1.32 jdolecek 1288: return (error);
1289: }
1290:
1291: int
1.85 dsl 1292: linux_sys_bind(struct lwp *l, const struct linux_sys_bind_args *uap, register_t *retval)
1.32 jdolecek 1293: {
1.85 dsl 1294: /* {
1.32 jdolecek 1295: syscallarg(int) s;
1296: syscallarg(const struct osockaddr *) name;
1297: syscallarg(int) namelen;
1.85 dsl 1298: } */
1.61 mrg 1299: int error;
1.73 dsl 1300: struct mbuf *nam;
1.32 jdolecek 1301:
1.73 dsl 1302: error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, name),
1303: SCARG(uap, namelen));
1304: if (error)
1305: return (error);
1.32 jdolecek 1306:
1.73 dsl 1307: return do_sys_bind(l, SCARG(uap, s), nam);
1.32 jdolecek 1308: }
1309:
1310: int
1.85 dsl 1311: linux_sys_getsockname(struct lwp *l, const struct linux_sys_getsockname_args *uap, register_t *retval)
1.32 jdolecek 1312: {
1.85 dsl 1313: /* {
1.32 jdolecek 1314: syscallarg(int) fdes;
1.71 christos 1315: syscallarg(void *) asa;
1.32 jdolecek 1316: syscallarg(int *) alen;
1.85 dsl 1317: } */
1.32 jdolecek 1318: int error;
1319:
1.85 dsl 1320: if ((error = sys_getsockname(l, (const void *)uap, retval)) != 0)
1.32 jdolecek 1321: return (error);
1322:
1323: if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
1324: return (error);
1325:
1326: return (0);
1327: }
1328:
1329: int
1.85 dsl 1330: linux_sys_getpeername(struct lwp *l, const struct linux_sys_getpeername_args *uap, register_t *retval)
1.32 jdolecek 1331: {
1.85 dsl 1332: /* {
1.32 jdolecek 1333: syscallarg(int) fdes;
1.71 christos 1334: syscallarg(void *) asa;
1.32 jdolecek 1335: syscallarg(int *) alen;
1.85 dsl 1336: } */
1.32 jdolecek 1337: int error;
1338:
1.85 dsl 1339: if ((error = sys_getpeername(l, (const void *)uap, retval)) != 0)
1.32 jdolecek 1340: return (error);
1341:
1342: if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
1343: return (error);
1344:
1345: return (0);
1346: }
1347:
1348: /*
1.73 dsl 1349: * Copy the osockaddr structure pointed to by osa to mbuf, adjust
1350: * family and convert to sockaddr.
1.32 jdolecek 1351: */
1352: static int
1.93 njoly 1353: linux_get_sa(struct lwp *l, int s, struct mbuf **mp,
1354: const struct osockaddr *osa, unsigned int salen)
1.73 dsl 1355: {
1356: int error, bdom;
1357: struct sockaddr *sa;
1358: struct osockaddr *kosa;
1359: struct mbuf *m;
1.32 jdolecek 1360:
1.73 dsl 1361: if (salen == 1 || salen > UCHAR_MAX) {
1362: DPRINTF(("bad osa=%p salen=%d\n", osa, salen));
1363: return EINVAL;
1.34 fvdl 1364: }
1.32 jdolecek 1365:
1.73 dsl 1366: /* We'll need the address in an mbuf later, so copy into one here */
1367: m = m_get(M_WAIT, MT_SONAME);
1368: if (salen > MLEN)
1369: MEXTMALLOC(m, salen, M_WAITOK);
1370:
1371: m->m_len = salen;
1372:
1.94 njoly 1373: if (salen == 0) {
1374: *mp = m;
1.73 dsl 1375: return 0;
1.94 njoly 1376: }
1.73 dsl 1377:
1378: kosa = mtod(m, void *);
1379: if ((error = copyin(osa, kosa, salen))) {
1380: DPRINTF(("error %d copying osa %p len %d\n",
1381: error, osa, salen));
1382: goto bad;
1.37 christos 1383: }
1.77 ad 1384:
1385: ktrkuser("linux sockaddr", kosa, salen);
1.32 jdolecek 1386:
1387: bdom = linux_to_bsd_domain(kosa->sa_family);
1388: if (bdom == -1) {
1.37 christos 1389: DPRINTF(("bad linux family=%d\n", kosa->sa_family));
1.32 jdolecek 1390: error = EINVAL;
1.73 dsl 1391: goto bad;
1392: }
1393:
1394: /*
1395: * If the family is unspecified, use address family of the socket.
1396: * This avoid triggering strict family checks in netinet/in_pcb.c et.al.
1397: */
1398: if (bdom == AF_UNSPEC) {
1399: struct socket *so;
1400:
1.96 ad 1401: /* fd_getsock() will use the descriptor for us */
1.88 ad 1402: if ((error = fd_getsock(s, &so)) != 0)
1.73 dsl 1403: goto bad;
1404:
1405: bdom = so->so_proto->pr_domain->dom_family;
1.88 ad 1406: fd_putfile(s);
1.73 dsl 1407:
1408: DPRINTF(("AF_UNSPEC family adjusted to %d\n", bdom));
1.32 jdolecek 1409: }
1410:
1411: /*
1412: * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
1413: * which lacks the scope id compared with RFC2553 one. If we detect
1414: * the situation, reject the address and write a message to system log.
1.34 fvdl 1415: *
1416: * Still accept addresses for which the scope id is not used.
1.32 jdolecek 1417: */
1.73 dsl 1418: if (bdom == AF_INET6 && salen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
1419: struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)kosa;
1420: if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) &&
1421: (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1422: IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) ||
1423: IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) ||
1424: IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
1425: IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
1426: struct proc *p = l->l_proc;
1.65 ad 1427: int uid = l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1;
1.34 fvdl 1428:
1429: log(LOG_DEBUG,
1430: "pid %d (%s), uid %d: obsolete pre-RFC2553 "
1431: "sockaddr_in6 rejected",
1432: p->p_pid, p->p_comm, uid);
1433: error = EINVAL;
1.73 dsl 1434: goto bad;
1.34 fvdl 1435: }
1.73 dsl 1436: salen = sizeof (struct sockaddr_in6);
1437: sin6->sin6_scope_id = 0;
1.53 jdolecek 1438: }
1439:
1.73 dsl 1440: if (bdom == AF_INET)
1441: salen = sizeof(struct sockaddr_in);
1.32 jdolecek 1442:
1443: sa = (struct sockaddr *) kosa;
1444: sa->sa_family = bdom;
1.73 dsl 1445: sa->sa_len = salen;
1446: m->m_len = salen;
1.77 ad 1447: ktrkuser("new sockaddr", kosa, salen);
1.73 dsl 1448:
1.37 christos 1449: #ifdef DEBUG_LINUX
1450: DPRINTF(("family %d, len = %d [ ", sa->sa_family, sa->sa_len));
1451: for (bdom = 0; bdom < sizeof(sa->sa_data); bdom++)
1.51 jdolecek 1452: DPRINTF(("%02x ", (unsigned char) sa->sa_data[bdom]));
1.37 christos 1453: DPRINTF(("\n"));
1454: #endif
1.32 jdolecek 1455:
1.73 dsl 1456: *mp = m;
1457: return 0;
1.32 jdolecek 1458:
1.73 dsl 1459: bad:
1460: m_free(m);
1461: return error;
1.32 jdolecek 1462: }
1463:
1464: static int
1.84 dsl 1465: linux_sa_put(struct osockaddr *osa)
1.32 jdolecek 1466: {
1467: struct sockaddr sa;
1468: struct osockaddr *kosa;
1469: int error, bdom, len;
1470:
1471: /*
1472: * Only read/write the sockaddr family and length part, the rest is
1473: * not changed.
1474: */
1475: len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
1476:
1.73 dsl 1477: error = copyin(osa, &sa, len);
1.32 jdolecek 1478: if (error)
1479: return (error);
1480:
1481: bdom = bsd_to_linux_domain(sa.sa_family);
1482: if (bdom == -1)
1483: return (EINVAL);
1484:
1485: /* Note: we convert from sockaddr to osockaddr here, too */
1486: kosa = (struct osockaddr *) &sa;
1487: kosa->sa_family = bdom;
1488: error = copyout(kosa, osa, len);
1489: if (error)
1490: return (error);
1491:
1492: return (0);
1493: }
1494:
1.56 manu 1495: #ifndef __amd64__
1.32 jdolecek 1496: int
1.85 dsl 1497: linux_sys_recv(struct lwp *l, const struct linux_sys_recv_args *uap, register_t *retval)
1.32 jdolecek 1498: {
1.85 dsl 1499: /* {
1.32 jdolecek 1500: syscallarg(int) s;
1501: syscallarg(void *) buf;
1502: syscallarg(int) len;
1503: syscallarg(int) flags;
1.85 dsl 1504: } */
1.32 jdolecek 1505: struct sys_recvfrom_args bra;
1506:
1507:
1508: SCARG(&bra, s) = SCARG(uap, s);
1509: SCARG(&bra, buf) = SCARG(uap, buf);
1510: SCARG(&bra, len) = (size_t) SCARG(uap, len);
1511: SCARG(&bra, flags) = SCARG(uap, flags);
1512: SCARG(&bra, from) = NULL;
1513: SCARG(&bra, fromlenaddr) = NULL;
1514:
1.43 thorpej 1515: return (sys_recvfrom(l, &bra, retval));
1.32 jdolecek 1516: }
1517:
1518: int
1.85 dsl 1519: linux_sys_send(struct lwp *l, const struct linux_sys_send_args *uap, register_t *retval)
1.32 jdolecek 1520: {
1.85 dsl 1521: /* {
1.32 jdolecek 1522: syscallarg(int) s;
1.71 christos 1523: syscallarg(void *) buf;
1.32 jdolecek 1524: syscallarg(int) len;
1525: syscallarg(int) flags;
1.85 dsl 1526: } */
1.32 jdolecek 1527: struct sys_sendto_args bsa;
1528:
1529: SCARG(&bsa, s) = SCARG(uap, s);
1530: SCARG(&bsa, buf) = SCARG(uap, buf);
1531: SCARG(&bsa, len) = SCARG(uap, len);
1532: SCARG(&bsa, flags) = SCARG(uap, flags);
1533: SCARG(&bsa, to) = NULL;
1534: SCARG(&bsa, tolen) = 0;
1535:
1.43 thorpej 1536: return (sys_sendto(l, &bsa, retval));
1.32 jdolecek 1537: }
1.59 manu 1538: #endif
1.32 jdolecek 1539:
1540: int
1.85 dsl 1541: linux_sys_accept(struct lwp *l, const struct linux_sys_accept_args *uap, register_t *retval)
1.32 jdolecek 1542: {
1.85 dsl 1543: /* {
1.32 jdolecek 1544: syscallarg(int) s;
1545: syscallarg(struct osockaddr *) name;
1546: syscallarg(int *) anamelen;
1.85 dsl 1547: } */
1.32 jdolecek 1548: int error;
1549: struct sys_accept_args baa;
1550:
1551: SCARG(&baa, s) = SCARG(uap, s);
1552: SCARG(&baa, name) = (struct sockaddr *) SCARG(uap, name);
1553: SCARG(&baa, anamelen) = (unsigned int *) SCARG(uap, anamelen);
1554:
1.43 thorpej 1555: if ((error = sys_accept(l, &baa, retval)))
1.32 jdolecek 1556: return (error);
1557:
1558: if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
1559: return (error);
1560:
1561: return (0);
1.19 sommerfe 1562: }
CVSweb <webmaster@jp.NetBSD.org>