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