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