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