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