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