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