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