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