Annotation of src/sys/compat/linux/common/linux_socket.c, revision 1.36
1.36 ! christos 1: /* $NetBSD: linux_socket.c,v 1.35 2001/11/13 02:08:59 lukem 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
1.32 jdolecek 42: *
43: * XXX Note: Linux CMSG_ALIGN() uses (sizeof(long)-1). For architectures
44: * where our CMSG_ALIGN() differs (like powerpc, sparc, sparc64), the passed
45: * control structure would need to be adjusted accordingly in sendmsg() and
46: * recvmsg().
1.16 erh 47: */
1.35 lukem 48:
49: #include <sys/cdefs.h>
1.36 ! christos 50: __KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.35 2001/11/13 02:08:59 lukem Exp $");
1.16 erh 51:
1.32 jdolecek 52: #if defined(_KERNEL_OPT)
53: #include "opt_inet.h"
54: #endif
55:
1.1 fvdl 56: #include <sys/param.h>
57: #include <sys/kernel.h>
58: #include <sys/systm.h>
59: #include <sys/buf.h>
60: #include <sys/malloc.h>
61: #include <sys/ioctl.h>
62: #include <sys/tty.h>
63: #include <sys/file.h>
64: #include <sys/filedesc.h>
65: #include <sys/select.h>
66: #include <sys/socket.h>
67: #include <sys/socketvar.h>
68: #include <net/if.h>
1.21 jtk 69: #include <net/if_dl.h>
70: #include <net/if_types.h>
1.1 fvdl 71: #include <netinet/in.h>
1.7 mycroft 72: #include <netinet/tcp.h>
1.1 fvdl 73: #include <sys/mount.h>
74: #include <sys/proc.h>
75: #include <sys/vnode.h>
76: #include <sys/device.h>
1.19 sommerfe 77: #include <sys/protosw.h>
1.32 jdolecek 78: #include <sys/mbuf.h>
79: #include <sys/syslog.h>
1.1 fvdl 80:
81: #include <sys/syscallargs.h>
82:
1.17 christos 83: #include <compat/linux/common/linux_types.h>
84: #include <compat/linux/common/linux_util.h>
85: #include <compat/linux/common/linux_signal.h>
86: #include <compat/linux/common/linux_ioctl.h>
87: #include <compat/linux/common/linux_socket.h>
88: #include <compat/linux/common/linux_socketcall.h>
89: #include <compat/linux/common/linux_sockio.h>
90:
1.1 fvdl 91: #include <compat/linux/linux_syscallargs.h>
1.21 jtk 92:
1.1 fvdl 93: /*
1.16 erh 94: * The calls in this file are entered either via the linux_socketcall()
95: * interface or, on the Alpha, as individual syscalls. The
96: * linux_socketcall function does any massaging of arguments so that all
97: * the calls in here need not think that they are anything other
98: * than a normal syscall.
1.1 fvdl 99: */
1.14 christos 100:
1.32 jdolecek 101: static int linux_to_bsd_domain __P((int));
102: static int bsd_to_linux_domain __P((int));
1.14 christos 103: int linux_to_bsd_sopt_level __P((int));
104: int linux_to_bsd_so_sockopt __P((int));
105: int linux_to_bsd_ip_sockopt __P((int));
106: int linux_to_bsd_tcp_sockopt __P((int));
107: int linux_to_bsd_udp_sockopt __P((int));
1.21 jtk 108: int linux_getifhwaddr __P((struct proc *, register_t *, u_int, void *));
1.36 ! christos 109: static int linux_sa_get __P((struct proc *, caddr_t *sgp, struct sockaddr **sap,
1.34 fvdl 110: const struct osockaddr *osa, int *osalen));
1.32 jdolecek 111: static int linux_sa_put __P((struct osockaddr *osa));
112:
113: static const int linux_to_bsd_domain_[LINUX_AF_MAX] = {
114: AF_UNSPEC,
115: AF_UNIX,
116: AF_INET,
117: AF_CCITT, /* LINUX_AF_AX25 */
118: AF_IPX,
119: AF_APPLETALK,
120: -1, /* LINUX_AF_NETROM */
121: -1, /* LINUX_AF_BRIDGE */
122: -1, /* LINUX_AF_ATMPVC */
123: AF_CCITT, /* LINUX_AF_X25 */
124: AF_INET6,
125: -1, /* LINUX_AF_ROSE */
126: AF_DECnet,
127: -1, /* LINUX_AF_NETBEUI */
128: -1, /* LINUX_AF_SECURITY */
129: pseudo_AF_KEY,
130: AF_ROUTE, /* LINUX_AF_NETLINK */
131: -1, /* LINUX_AF_PACKET */
132: -1, /* LINUX_AF_ASH */
133: -1, /* LINUX_AF_ECONET */
134: -1, /* LINUX_AF_ATMSVC */
135: AF_SNA,
136: /* rest up to LINUX_AF_MAX-1 is not allocated */
137: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
138: };
139:
140: static const int bsd_to_linux_domain_[AF_MAX] = {
141: LINUX_AF_UNSPEC,
142: LINUX_AF_UNIX,
143: LINUX_AF_INET,
144: -1, /* AF_IMPLINK */
145: -1, /* AF_PUP */
146: -1, /* AF_CHAOS */
147: -1, /* AF_NS */
148: -1, /* AF_ISO */
149: -1, /* AF_ECMA */
150: -1, /* AF_DATAKIT */
151: LINUX_AF_AX25, /* AF_CCITT */
152: LINUX_AF_SNA,
153: LINUX_AF_DECnet,
154: -1, /* AF_DLI */
155: -1, /* AF_LAT */
156: -1, /* AF_HYLINK */
157: LINUX_AF_APPLETALK,
158: LINUX_AF_NETLINK,
159: -1, /* AF_LINK */
160: -1, /* AF_XTP */
161: -1, /* AF_COIP */
162: -1, /* AF_CNT */
163: -1, /* pseudo_AF_RTIP */
164: LINUX_AF_IPX,
165: LINUX_AF_INET6,
166: -1, /* pseudo_AF_PIP */
167: -1, /* AF_ISDN */
168: -1, /* AF_NATM */
169: -1, /* AF_ARP */
170: LINUX_pseudo_AF_KEY,
171: -1, /* pseudo_AF_HDRCMPLT */
172: };
1.1 fvdl 173:
174: /*
175: * Convert between Linux and BSD socket domain values
176: */
1.32 jdolecek 177: static int
1.1 fvdl 178: linux_to_bsd_domain(ldom)
179: int ldom;
180: {
1.32 jdolecek 181: if (ldom < 0 || ldom >= LINUX_AF_MAX)
182: return (-1);
183:
184: return linux_to_bsd_domain_[ldom];
185: }
186:
187: /*
188: * Convert between BSD and Linux socket domain values
189: */
190: static int
191: bsd_to_linux_domain(bdom)
192: int bdom;
193: {
194: if (bdom < 0 || bdom >= AF_MAX)
195: return (-1);
1.12 mycroft 196:
1.32 jdolecek 197: return bsd_to_linux_domain_[bdom];
1.1 fvdl 198: }
199:
1.2 mycroft 200: int
1.16 erh 201: linux_sys_socket(p, v, retval)
1.1 fvdl 202: struct proc *p;
1.16 erh 203: void *v;
204: register_t *retval;
205: {
206: struct linux_sys_socket_args /* {
1.1 fvdl 207: syscallarg(int) domain;
208: syscallarg(int) type;
209: syscallarg(int) protocol;
1.16 erh 210: } */ *uap = v;
1.12 mycroft 211: struct sys_socket_args bsa;
1.1 fvdl 212:
1.16 erh 213: SCARG(&bsa, protocol) = SCARG(uap, protocol);
214: SCARG(&bsa, type) = SCARG(uap, type);
215: SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1 fvdl 216: if (SCARG(&bsa, domain) == -1)
217: return EINVAL;
1.12 mycroft 218: return sys_socket(p, &bsa, retval);
1.1 fvdl 219: }
220:
1.2 mycroft 221: int
1.16 erh 222: linux_sys_socketpair(p, v, retval)
1.1 fvdl 223: struct proc *p;
1.16 erh 224: void *v;
1.1 fvdl 225: register_t *retval;
226: {
1.16 erh 227: struct linux_sys_socketpair_args /* {
1.1 fvdl 228: syscallarg(int) domain;
229: syscallarg(int) type;
230: syscallarg(int) protocol;
231: syscallarg(int *) rsv;
1.16 erh 232: } */ *uap = v;
1.12 mycroft 233: struct sys_socketpair_args bsa;
1.1 fvdl 234:
1.16 erh 235: SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1 fvdl 236: if (SCARG(&bsa, domain) == -1)
237: return EINVAL;
1.16 erh 238: SCARG(&bsa, type) = SCARG(uap, type);
239: SCARG(&bsa, protocol) = SCARG(uap, protocol);
240: SCARG(&bsa, rsv) = SCARG(uap, rsv);
1.1 fvdl 241:
1.12 mycroft 242: return sys_socketpair(p, &bsa, retval);
1.1 fvdl 243: }
244:
1.2 mycroft 245: int
1.16 erh 246: linux_sys_sendto(p, v, retval)
1.1 fvdl 247: struct proc *p;
1.16 erh 248: void *v;
1.1 fvdl 249: register_t *retval;
250: {
1.16 erh 251: struct linux_sys_sendto_args /* {
1.32 jdolecek 252: syscallarg(int) s;
253: syscallarg(void *) msg;
254: syscallarg(int) len;
255: syscallarg(int) flags;
256: syscallarg(struct osockaddr *) to;
257: syscallarg(int) tolen;
1.16 erh 258: } */ *uap = v;
1.12 mycroft 259: struct sys_sendto_args bsa;
1.34 fvdl 260: int tolen;
1.1 fvdl 261:
1.16 erh 262: SCARG(&bsa, s) = SCARG(uap, s);
263: SCARG(&bsa, buf) = SCARG(uap, msg);
1.32 jdolecek 264: SCARG(&bsa, len) = (size_t) SCARG(uap, len);
1.16 erh 265: SCARG(&bsa, flags) = SCARG(uap, flags);
1.34 fvdl 266: tolen = SCARG(uap, tolen);
1.32 jdolecek 267: if (SCARG(uap, to)) {
268: struct sockaddr *sa;
269: int error;
1.36 ! christos 270: caddr_t sg = stackgap_init(p, 0);
1.32 jdolecek 271:
1.36 ! christos 272: if ((error = linux_sa_get(p, &sg, &sa, SCARG(uap, to), &tolen)))
1.32 jdolecek 273: return (error);
274:
275: SCARG(&bsa, to) = sa;
276: } else
277: SCARG(&bsa, to) = NULL;
1.34 fvdl 278: SCARG(&bsa, tolen) = tolen;
1.1 fvdl 279:
1.32 jdolecek 280: return (sys_sendto(p, &bsa, retval));
281: }
282:
283: int
284: linux_sys_sendmsg(p, v, retval)
285: struct proc *p;
286: void *v;
287: register_t *retval;
288: {
289: struct linux_sys_sendmsg_args /* {
290: syscallarg(int) s;
291: syscallarg(struct msghdr *) msg;
292: syscallarg(u_int) flags;
293: } */ *uap = v;
294: struct msghdr msg;
295: int error;
296: struct sys_sendmsg_args bsa;
297: struct msghdr *nmsg = NULL;
298:
299: error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg));
300: if (error)
301: return (error);
302:
303: if (msg.msg_name) {
304: struct sockaddr *sa;
1.36 ! christos 305: caddr_t sg = stackgap_init(p, 0);
1.32 jdolecek 306:
1.36 ! christos 307: nmsg = (struct msghdr *) stackgap_alloc(p, &sg,
1.32 jdolecek 308: sizeof(struct msghdr));
309: if (!nmsg)
310: return (ENOMEM);
311:
1.36 ! christos 312: error = linux_sa_get(p, &sg, &sa,
1.34 fvdl 313: (struct osockaddr *) msg.msg_name, &msg.msg_namelen);
1.32 jdolecek 314: if (error)
315: return (error);
316:
317: msg.msg_name = (struct sockaddr *) sa;
318: if ((error = copyout(&msg, nmsg, sizeof(struct msghdr))))
319: return (error);
320: }
321:
322: /*
323: * XXX handle different alignment of cmsg data on architectures where
324: * the Linux alignment is different (powerpc, sparc, sparc64).
325: */
326:
327: SCARG(&bsa, s) = SCARG(uap, s);
328: SCARG(&bsa, msg) = nmsg;
329: SCARG(&bsa, flags) = SCARG(uap, flags);
330:
331: if ((error = sys_sendmsg(p, &bsa, retval)))
332: return (error);
333:
334: return (0);
1.1 fvdl 335: }
336:
1.32 jdolecek 337:
1.2 mycroft 338: int
1.16 erh 339: linux_sys_recvfrom(p, v, retval)
1.1 fvdl 340: struct proc *p;
1.16 erh 341: void *v;
342: register_t *retval;
343: {
344: struct linux_sys_recvfrom_args /* {
1.1 fvdl 345: syscallarg(int) s;
346: syscallarg(void *) buf;
347: syscallarg(int) len;
348: syscallarg(int) flags;
1.32 jdolecek 349: syscallarg(struct osockaddr *) from;
350: syscallarg(int *) fromlenaddr;
1.16 erh 351: } */ *uap = v;
1.32 jdolecek 352: int error;
353: struct sys_recvfrom_args bra;
1.1 fvdl 354:
1.16 erh 355: SCARG(&bra, s) = SCARG(uap, s);
356: SCARG(&bra, buf) = SCARG(uap, buf);
357: SCARG(&bra, len) = SCARG(uap, len);
358: SCARG(&bra, flags) = SCARG(uap, flags);
1.32 jdolecek 359: SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from);
360: SCARG(&bra, fromlenaddr) = SCARG(uap, fromlenaddr);
1.1 fvdl 361:
1.32 jdolecek 362: if ((error = sys_recvfrom(p, &bra, retval)))
363: return (error);
364:
365: if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from))))
366: return (error);
367:
368: return (0);
369: }
370:
371: int
372: linux_sys_recvmsg(p, v, retval)
373: struct proc *p;
374: void *v;
375: register_t *retval;
376: {
377: struct linux_sys_recvmsg_args /* {
378: syscallarg(int) s;
379: syscallarg(struct msghdr *) msg;
380: syscallarg(u_int) flags;
381: } */ *uap = v;
382: struct msghdr msg;
383: int error;
384:
385: if ((error = sys_recvmsg(p, v, retval)))
386: return (error);
387:
388: error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
389: sizeof(msg));
390:
391: if (!error && msg.msg_name && msg.msg_namelen > 2)
392: error = linux_sa_put(msg.msg_name);
393:
394: /*
395: * XXX handle different alignment of cmsg data on architectures where
396: * the Linux alignment is different (powerpc, sparc, sparc64).
397: */
398:
399: return (error);
1.1 fvdl 400: }
401:
402: /*
403: * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
404: * is different, the rest matches IPPROTO_* on both systems.
405: */
1.2 mycroft 406: int
1.1 fvdl 407: linux_to_bsd_sopt_level(llevel)
408: int llevel;
409: {
1.12 mycroft 410:
1.1 fvdl 411: switch (llevel) {
412: case LINUX_SOL_SOCKET:
413: return SOL_SOCKET;
1.3 mycroft 414: case LINUX_SOL_IP:
415: return IPPROTO_IP;
416: case LINUX_SOL_TCP:
417: return IPPROTO_TCP;
418: case LINUX_SOL_UDP:
419: return IPPROTO_UDP;
1.1 fvdl 420: default:
421: return -1;
422: }
423: }
424:
425: /*
426: * Convert Linux socket level socket option numbers to NetBSD values.
427: */
1.2 mycroft 428: int
1.1 fvdl 429: linux_to_bsd_so_sockopt(lopt)
430: int lopt;
431: {
1.12 mycroft 432:
1.1 fvdl 433: switch (lopt) {
434: case LINUX_SO_DEBUG:
435: return SO_DEBUG;
436: case LINUX_SO_REUSEADDR:
437: return SO_REUSEADDR;
438: case LINUX_SO_TYPE:
439: return SO_TYPE;
440: case LINUX_SO_ERROR:
441: return SO_ERROR;
442: case LINUX_SO_DONTROUTE:
443: return SO_DONTROUTE;
444: case LINUX_SO_BROADCAST:
445: return SO_BROADCAST;
446: case LINUX_SO_SNDBUF:
447: return SO_SNDBUF;
448: case LINUX_SO_RCVBUF:
449: return SO_RCVBUF;
450: case LINUX_SO_KEEPALIVE:
451: return SO_KEEPALIVE;
452: case LINUX_SO_OOBINLINE:
453: return SO_OOBINLINE;
454: case LINUX_SO_LINGER:
455: return SO_LINGER;
456: case LINUX_SO_PRIORITY:
457: case LINUX_SO_NO_CHECK:
458: default:
459: return -1;
460: }
461: }
462:
463: /*
1.3 mycroft 464: * Convert Linux IP level socket option number to NetBSD values.
465: */
466: int
467: linux_to_bsd_ip_sockopt(lopt)
468: int lopt;
469: {
1.12 mycroft 470:
1.3 mycroft 471: switch (lopt) {
472: case LINUX_IP_TOS:
473: return IP_TOS;
474: case LINUX_IP_TTL:
475: return IP_TTL;
476: case LINUX_IP_MULTICAST_TTL:
477: return IP_MULTICAST_TTL;
478: case LINUX_IP_MULTICAST_LOOP:
479: return IP_MULTICAST_LOOP;
480: case LINUX_IP_MULTICAST_IF:
481: return IP_MULTICAST_IF;
482: case LINUX_IP_ADD_MEMBERSHIP:
483: return IP_ADD_MEMBERSHIP;
484: case LINUX_IP_DROP_MEMBERSHIP:
485: return IP_DROP_MEMBERSHIP;
486: default:
487: return -1;
488: }
489: }
490:
491: /*
492: * Convert Linux TCP level socket option number to NetBSD values.
493: */
494: int
495: linux_to_bsd_tcp_sockopt(lopt)
496: int lopt;
497: {
1.12 mycroft 498:
1.3 mycroft 499: switch (lopt) {
1.6 mycroft 500: case LINUX_TCP_NODELAY:
501: return TCP_NODELAY;
502: case LINUX_TCP_MAXSEG:
503: return TCP_MAXSEG;
1.3 mycroft 504: default:
1.6 mycroft 505: return -1;
1.3 mycroft 506: }
507: }
508:
509: /*
510: * Convert Linux UDP level socket option number to NetBSD values.
511: */
512: int
513: linux_to_bsd_udp_sockopt(lopt)
514: int lopt;
515: {
1.12 mycroft 516:
1.3 mycroft 517: switch (lopt) {
518: default:
1.6 mycroft 519: return -1;
1.3 mycroft 520: }
521: }
522:
523: /*
1.5 mycroft 524: * Another reasonably straightforward function: setsockopt(2).
1.1 fvdl 525: * The level and option numbers are converted; the values passed
526: * are not (yet) converted, the ones currently implemented don't
527: * need conversion, as they are the same on both systems.
528: */
1.2 mycroft 529: int
1.16 erh 530: linux_sys_setsockopt(p, v, retval)
1.1 fvdl 531: struct proc *p;
1.16 erh 532: void *v;
533: register_t *retval;
534: {
535: struct linux_sys_setsockopt_args /* {
1.1 fvdl 536: syscallarg(int) s;
537: syscallarg(int) level;
538: syscallarg(int) optname;
539: syscallarg(void *) optval;
540: syscallarg(int) optlen;
1.16 erh 541: } */ *uap = v;
1.12 mycroft 542: struct sys_setsockopt_args bsa;
1.16 erh 543: int name;
1.1 fvdl 544:
1.16 erh 545: SCARG(&bsa, s) = SCARG(uap, s);
546: SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
547: SCARG(&bsa, val) = SCARG(uap, optval);
548: SCARG(&bsa, valsize) = SCARG(uap, optlen);
1.1 fvdl 549:
550: switch (SCARG(&bsa, level)) {
551: case SOL_SOCKET:
1.16 erh 552: name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
1.1 fvdl 553: break;
554: case IPPROTO_IP:
1.16 erh 555: name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
1.1 fvdl 556: break;
1.3 mycroft 557: case IPPROTO_TCP:
1.16 erh 558: name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
1.3 mycroft 559: break;
560: case IPPROTO_UDP:
1.16 erh 561: name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
1.3 mycroft 562: break;
1.1 fvdl 563: default:
564: return EINVAL;
565: }
566:
567: if (name == -1)
568: return EINVAL;
569: SCARG(&bsa, name) = name;
570:
1.12 mycroft 571: return sys_setsockopt(p, &bsa, retval);
1.1 fvdl 572: }
573:
574: /*
575: * getsockopt(2) is very much the same as setsockopt(2) (see above)
576: */
1.2 mycroft 577: int
1.16 erh 578: linux_sys_getsockopt(p, v, retval)
1.1 fvdl 579: struct proc *p;
1.16 erh 580: void *v;
581: register_t *retval;
582: {
583: struct linux_sys_getsockopt_args /* {
1.1 fvdl 584: syscallarg(int) s;
585: syscallarg(int) level;
586: syscallarg(int) optname;
587: syscallarg(void *) optval;
1.16 erh 588: syscallarg(int *) optlen;
589: } */ *uap = v;
1.12 mycroft 590: struct sys_getsockopt_args bga;
1.16 erh 591: int name;
1.1 fvdl 592:
1.16 erh 593: SCARG(&bga, s) = SCARG(uap, s);
594: SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
595: SCARG(&bga, val) = SCARG(uap, optval);
596: SCARG(&bga, avalsize) = SCARG(uap, optlen);
1.1 fvdl 597:
598: switch (SCARG(&bga, level)) {
599: case SOL_SOCKET:
1.16 erh 600: name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
1.1 fvdl 601: break;
602: case IPPROTO_IP:
1.16 erh 603: name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
1.3 mycroft 604: break;
605: case IPPROTO_TCP:
1.16 erh 606: name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
1.3 mycroft 607: break;
608: case IPPROTO_UDP:
1.16 erh 609: name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
1.1 fvdl 610: break;
611: default:
612: return EINVAL;
613: }
614:
615: if (name == -1)
616: return EINVAL;
1.5 mycroft 617: SCARG(&bga, name) = name;
1.1 fvdl 618:
1.12 mycroft 619: return sys_getsockopt(p, &bga, retval);
1.13 mycroft 620: }
621:
1.21 jtk 622: #define IF_NAME_LEN 16
623:
624: int
625: linux_getifhwaddr(p, retval, fd, data)
626: struct proc *p;
627: register_t *retval;
628: u_int fd;
629: void *data;
630: {
631: /* Not the full structure, just enough to map what we do here */
632: struct linux_ifreq {
633: char if_name[IF_NAME_LEN];
634: struct osockaddr hwaddr;
635: } lreq;
636: struct filedesc *fdp;
637: struct file *fp;
638: struct ifaddr *ifa;
639: struct ifnet *ifp;
640: struct sockaddr_dl *sadl;
641: int error, found;
642: int index, ifnum;
643:
644: /*
645: * We can't emulate this ioctl by calling sys_ioctl() to run
646: * SIOCGIFCONF, because the user buffer is not of the right
647: * type to take those results. We can't use kernel buffers to
648: * receive the results, as the implementation of sys_ioctl()
649: * and ifconf() [which implements SIOCGIFCONF] use
650: * copyin()/copyout() which will fail on kernel addresses.
651: *
652: * So, we must duplicate code from sys_ioctl() and ifconf(). Ugh.
653: */
654:
655: fdp = p->p_fd;
1.30 thorpej 656: if ((fp = fd_getfile(fdp, fd)) == NULL)
1.21 jtk 657: return (EBADF);
658:
659: FILE_USE(fp);
660: if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
661: error = EBADF;
662: goto out;
663: }
664:
665: error = copyin(data, (caddr_t)&lreq, sizeof(lreq));
666: if (error)
667: goto out;
668: lreq.if_name[IF_NAME_LEN-1] = '\0'; /* just in case */
669:
670: /*
1.22 jdolecek 671: * Try real interface name first, then fake "ethX"
1.21 jtk 672: */
1.22 jdolecek 673: for (ifp = ifnet.tqh_first, found = 0;
674: ifp != 0 && !found;
675: ifp = ifp->if_list.tqe_next) {
676: if (strcmp(lreq.if_name, ifp->if_xname))
677: /* not this interface */
678: continue;
679: found=1;
680: if ((ifa = ifp->if_addrlist.tqh_first) != 0) {
681: for (; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
682: sadl = (struct sockaddr_dl *)ifa->ifa_addr;
683: /* only return ethernet addresses */
684: /* XXX what about FDDI, etc. ? */
685: if (sadl->sdl_family != AF_LINK ||
686: sadl->sdl_type != IFT_ETHER)
687: continue;
688: memcpy((caddr_t)&lreq.hwaddr.sa_data,
689: LLADDR(sadl),
690: MIN(sadl->sdl_alen,
691: sizeof(lreq.hwaddr.sa_data)));
692: lreq.hwaddr.sa_family =
693: sadl->sdl_family;
694: error = copyout((caddr_t)&lreq, data,
695: sizeof(lreq));
696: goto out;
697: }
698: } else {
699: error = ENODEV;
700: goto out;
701: }
702: }
703:
1.31 jdolecek 704: if (strncmp(lreq.if_name, "eth", 3) == 0) {
1.21 jtk 705: for (ifnum = 0, index = 3;
706: lreq.if_name[index] != '\0' && index < IF_NAME_LEN;
707: index++) {
708: ifnum *= 10;
709: ifnum += lreq.if_name[index] - '0';
710: }
711:
712: error = EINVAL; /* in case we don't find one */
713: for (ifp = ifnet.tqh_first, found = 0;
714: ifp != 0 && !found;
715: ifp = ifp->if_list.tqe_next) {
716: memcpy(lreq.if_name, ifp->if_xname,
717: MIN(IF_NAME_LEN, IFNAMSIZ));
718: if ((ifa = ifp->if_addrlist.tqh_first) == 0)
719: /* no addresses on this interface */
720: continue;
721: else
722: for (; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
723: sadl = (struct sockaddr_dl *)ifa->ifa_addr;
724: /* only return ethernet addresses */
725: /* XXX what about FDDI, etc. ? */
726: if (sadl->sdl_family != AF_LINK ||
727: sadl->sdl_type != IFT_ETHER)
728: continue;
729: if (ifnum--)
730: /* not the reqested iface */
731: continue;
732: memcpy((caddr_t)&lreq.hwaddr.sa_data,
733: LLADDR(sadl),
734: MIN(sadl->sdl_alen,
735: sizeof(lreq.hwaddr.sa_data)));
736: lreq.hwaddr.sa_family =
737: sadl->sdl_family;
738: error = copyout((caddr_t)&lreq, data,
739: sizeof(lreq));
740: found = 1;
741: break;
742: }
743: }
1.22 jdolecek 744: } else {
745: /* unknown interface, not even an "eth*" name */
746: error = ENODEV;
747: }
1.21 jtk 748:
749: out:
750: FILE_UNUSE(fp, p);
751: return error;
752: }
1.24 thorpej 753: #undef IF_NAME_LEN
1.21 jtk 754:
1.13 mycroft 755: int
756: linux_ioctl_socket(p, uap, retval)
1.23 augustss 757: struct proc *p;
758: struct linux_sys_ioctl_args /* {
1.13 mycroft 759: syscallarg(int) fd;
760: syscallarg(u_long) com;
761: syscallarg(caddr_t) data;
762: } */ *uap;
763: register_t *retval;
764: {
765: u_long com;
1.25 fvdl 766: int error = 0, isdev = 0, dosys = 1;
1.13 mycroft 767: struct sys_ioctl_args ia;
1.25 fvdl 768: struct file *fp;
769: struct filedesc *fdp;
770: struct vnode *vp;
771: int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
772: struct ioctl_pt pt;
773:
774: fdp = p->p_fd;
1.30 thorpej 775: if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
1.25 fvdl 776: return (EBADF);
777:
778: FILE_USE(fp);
779:
780: if (fp->f_type == DTYPE_VNODE) {
781: vp = (struct vnode *)fp->f_data;
782: isdev = vp->v_type == VCHR;
783: }
784:
785: /*
786: * Don't try to interpret socket ioctl calls that are done
787: * on a device filedescriptor, just pass them through, to
788: * emulate Linux behaviour. Use PTIOCLINUX so that the
789: * device will only handle these if it's prepared to do
790: * so, to avoid unexpected things from happening.
791: */
792: if (isdev) {
1.26 fvdl 793: dosys = 0;
1.25 fvdl 794: ioctlf = fp->f_ops->fo_ioctl;
795: pt.com = SCARG(uap, com);
796: pt.data = SCARG(uap, data);
797: error = ioctlf(fp, PTIOCLINUX, (caddr_t)&pt, p);
1.27 fvdl 798: /*
799: * XXX hack: if the function returns EJUSTRETURN,
800: * it has stuffed a sysctl return value in pt.data.
801: */
802: if (error == EJUSTRETURN) {
803: retval[0] = (register_t)pt.data;
804: error = 0;
805: }
1.25 fvdl 806: goto out;
807: }
1.13 mycroft 808:
809: com = SCARG(uap, com);
810: retval[0] = 0;
811:
812: switch (com) {
813: case LINUX_SIOCGIFCONF:
814: SCARG(&ia, com) = OSIOCGIFCONF;
815: break;
816: case LINUX_SIOCGIFFLAGS:
817: SCARG(&ia, com) = SIOCGIFFLAGS;
818: break;
1.25 fvdl 819: case LINUX_SIOCSIFFLAGS:
820: SCARG(&ia, com) = SIOCSIFFLAGS;
821: break;
1.13 mycroft 822: case LINUX_SIOCGIFADDR:
823: SCARG(&ia, com) = OSIOCGIFADDR;
824: break;
825: case LINUX_SIOCGIFDSTADDR:
826: SCARG(&ia, com) = OSIOCGIFDSTADDR;
827: break;
828: case LINUX_SIOCGIFBRDADDR:
829: SCARG(&ia, com) = OSIOCGIFBRDADDR;
830: break;
831: case LINUX_SIOCGIFNETMASK:
832: SCARG(&ia, com) = OSIOCGIFNETMASK;
833: break;
834: case LINUX_SIOCADDMULTI:
835: SCARG(&ia, com) = SIOCADDMULTI;
836: break;
837: case LINUX_SIOCDELMULTI:
838: SCARG(&ia, com) = SIOCDELMULTI;
839: break;
1.21 jtk 840: case LINUX_SIOCGIFHWADDR:
1.25 fvdl 841: error = linux_getifhwaddr(p, retval, SCARG(uap, fd),
1.21 jtk 842: SCARG(uap, data));
1.29 fvdl 843: dosys = 0;
1.25 fvdl 844: break;
1.13 mycroft 845: default:
1.25 fvdl 846: error = EINVAL;
847: }
848:
849: out:
850: FILE_UNUSE(fp, p);
851:
852: if (error ==0 && dosys) {
853: SCARG(&ia, fd) = SCARG(uap, fd);
854: SCARG(&ia, data) = SCARG(uap, data);
855: error = sys_ioctl(p, &ia, retval);
1.13 mycroft 856: }
857:
1.25 fvdl 858: return error;
1.1 fvdl 859: }
1.19 sommerfe 860:
861: int
862: linux_sys_connect(p, v, retval)
1.23 augustss 863: struct proc *p;
1.19 sommerfe 864: void *v;
865: register_t *retval;
866: {
1.32 jdolecek 867: struct linux_sys_connect_args /* {
1.19 sommerfe 868: syscallarg(int) s;
869: syscallarg(const struct sockaddr *) name;
1.32 jdolecek 870: syscallarg(int) namelen;
1.19 sommerfe 871: } */ *uap = v;
1.32 jdolecek 872: int error;
873: struct sockaddr *sa;
874: struct sys_connect_args bca;
1.36 ! christos 875: caddr_t sg = stackgap_init(p, 0);
1.34 fvdl 876: int namlen;
1.32 jdolecek 877:
1.34 fvdl 878: namlen = SCARG(uap, namelen);
1.36 ! christos 879: error = linux_sa_get(p, &sg, &sa, SCARG(uap, name), &namlen);
1.32 jdolecek 880: if (error)
881: return (error);
1.19 sommerfe 882:
1.32 jdolecek 883: SCARG(&bca, s) = SCARG(uap, s);
884: SCARG(&bca, name) = sa;
1.34 fvdl 885: SCARG(&bca, namelen) = (unsigned int) namlen;
1.32 jdolecek 886:
887: error = sys_connect(p, &bca, retval);
1.19 sommerfe 888:
889: if (error == EISCONN) {
890: struct file *fp;
1.23 augustss 891: struct socket *so;
1.19 sommerfe 892: int s, state, prflags;
893:
1.20 thorpej 894: /* getsock() will use the descriptor for us */
895: if (getsock(p->p_fd, SCARG(uap, s), &fp) != 0)
1.19 sommerfe 896: return EISCONN;
897:
898: s = splsoftnet();
899: so = (struct socket *)fp->f_data;
900: state = so->so_state;
901: prflags = so->so_proto->pr_flags;
902: splx(s);
1.20 thorpej 903: FILE_UNUSE(fp, p);
1.19 sommerfe 904: /*
905: * We should only let this call succeed once per
906: * non-blocking connect; however we don't have
907: * a convenient place to keep that state..
908: */
909: if ((state & SS_NBIO) && (state & SS_ISCONNECTED) &&
910: (prflags & PR_CONNREQUIRED))
911: return 0;
912: }
913:
1.32 jdolecek 914: return (error);
915: }
916:
917: int
918: linux_sys_bind(p, v, retval)
919: struct proc *p;
920: void *v;
921: register_t *retval;
922: {
923: struct linux_sys_bind_args /* {
924: syscallarg(int) s;
925: syscallarg(const struct osockaddr *) name;
926: syscallarg(int) namelen;
927: } */ *uap = v;
1.34 fvdl 928: int error, namlen;
1.32 jdolecek 929: struct sys_bind_args bsa;
930:
1.34 fvdl 931: namlen = SCARG(uap, namelen);
1.32 jdolecek 932: SCARG(&bsa, s) = SCARG(uap, s);
933: if (SCARG(uap, name)) {
934: struct sockaddr *sa;
1.36 ! christos 935: caddr_t sg = stackgap_init(p, 0);
1.32 jdolecek 936:
1.36 ! christos 937: error = linux_sa_get(p, &sg, &sa, SCARG(uap, name), &namlen);
1.32 jdolecek 938: if (error)
939: return (error);
940:
941: SCARG(&bsa, name) = sa;
942: } else
943: SCARG(&bsa, name) = NULL;
1.34 fvdl 944: SCARG(&bsa, namelen) = namlen;
1.32 jdolecek 945:
946: return (sys_bind(p, &bsa, retval));
947: }
948:
949: int
950: linux_sys_getsockname(p, v, retval)
951: struct proc *p;
952: void *v;
953: register_t *retval;
954: {
955: struct linux_sys_getsockname_args /* {
956: syscallarg(int) fdes;
957: syscallarg(caddr_t) asa;
958: syscallarg(int *) alen;
959: } */ *uap = v;
960: int error;
961:
962: if ((error = sys_getsockname(p, uap, retval)) != 0)
963: return (error);
964:
965: if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
966: return (error);
967:
968: return (0);
969: }
970:
971: int
972: linux_sys_getpeername(p, v, retval)
973: struct proc *p;
974: void *v;
975: register_t *retval;
976: {
977: struct sys_getpeername_args /* {
978: syscallarg(int) fdes;
979: syscallarg(caddr_t) asa;
980: syscallarg(int *) alen;
981: } */ *uap = v;
982: int error;
983:
984: if ((error = sys_getpeername(p, uap, retval)) != 0)
985: return (error);
986:
987: if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
988: return (error);
989:
990: return (0);
991: }
992:
993: /*
994: * Copy the osockaddr structure pointed to by osa to kernel, adjust
995: * family and convert to sockaddr, allocate stackgap and put the
996: * the converted structure there, address on stackgap returned in sap.
997: */
998: static int
1.36 ! christos 999: linux_sa_get(p, sgp, sap, osa, osalen)
! 1000: struct proc *p;
1.32 jdolecek 1001: caddr_t *sgp;
1002: struct sockaddr **sap;
1.33 jdolecek 1003: const struct osockaddr *osa;
1.34 fvdl 1004: int *osalen;
1.32 jdolecek 1005: {
1006: int error=0, bdom;
1007: struct sockaddr *sa, *usa;
1008: struct osockaddr *kosa = (struct osockaddr *) &sa;
1.34 fvdl 1009: int alloclen;
1010: #ifdef INET6
1011: int oldv6size;
1012: struct sockaddr_in6 *sin6;
1013: #endif
1.32 jdolecek 1014:
1.34 fvdl 1015: if (*osalen < 2 || *osalen > UCHAR_MAX || !osa)
1.32 jdolecek 1016: return (EINVAL);
1017:
1.34 fvdl 1018: alloclen = *osalen;
1019: #ifdef INET6
1020: oldv6size = 0;
1021: /*
1022: * Check for old (pre-RFC2553) sockaddr_in6. We may accept it
1023: * if it's a v4-mapped address, so reserve the proper space
1024: * for it.
1025: */
1026: if (alloclen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
1027: alloclen = sizeof (struct sockaddr_in6);
1028: oldv6size = 1;
1029: }
1030: #endif
1.32 jdolecek 1031:
1.34 fvdl 1032: kosa = (struct osockaddr *) malloc(alloclen, M_TEMP, M_WAITOK);
1033:
1034: if ((error = copyin(osa, (caddr_t) kosa, *osalen)))
1.32 jdolecek 1035: goto out;
1036:
1037: bdom = linux_to_bsd_domain(kosa->sa_family);
1038: if (bdom == -1) {
1039: error = EINVAL;
1040: goto out;
1041: }
1042:
1043: #ifdef INET6
1044: /*
1045: * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
1046: * which lacks the scope id compared with RFC2553 one. If we detect
1047: * the situation, reject the address and write a message to system log.
1.34 fvdl 1048: *
1049: * Still accept addresses for which the scope id is not used.
1.32 jdolecek 1050: */
1.34 fvdl 1051: if (oldv6size && bdom == AF_INET6) {
1052: sin6 = (struct sockaddr_in6 *)kosa;
1053: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ||
1054: (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
1055: !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
1056: !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) &&
1057: !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
1058: !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
1059: sin6->sin6_scope_id = 0;
1060: } else {
1061: struct proc *p = curproc; /* XXX */
1062: int uid = p->p_cred && p->p_ucred ?
1063: p->p_ucred->cr_uid : -1;
1064:
1065: log(LOG_DEBUG,
1066: "pid %d (%s), uid %d: obsolete pre-RFC2553 "
1067: "sockaddr_in6 rejected",
1068: p->p_pid, p->p_comm, uid);
1069: error = EINVAL;
1070: goto out;
1071: }
1.32 jdolecek 1072: }
1073: #endif
1074:
1075: sa = (struct sockaddr *) kosa;
1076: sa->sa_family = bdom;
1.34 fvdl 1077: sa->sa_len = alloclen;
1.32 jdolecek 1078:
1.36 ! christos 1079: usa = (struct sockaddr *) stackgap_alloc(p, sgp, alloclen);
1.32 jdolecek 1080: if (!usa) {
1081: error = ENOMEM;
1082: goto out;
1083: }
1084:
1.34 fvdl 1085: if ((error = copyout(sa, usa, alloclen)))
1.32 jdolecek 1086: goto out;
1087:
1088: *sap = usa;
1089:
1090: out:
1.34 fvdl 1091: *osalen = alloclen;
1.32 jdolecek 1092: free(kosa, M_TEMP);
1093: return (error);
1094: }
1095:
1096: static int
1097: linux_sa_put(osa)
1098: struct osockaddr *osa;
1099: {
1100: struct sockaddr sa;
1101: struct osockaddr *kosa;
1102: int error, bdom, len;
1103:
1104: /*
1105: * Only read/write the sockaddr family and length part, the rest is
1106: * not changed.
1107: */
1108: len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
1109:
1110: error = copyin((caddr_t) osa, (caddr_t) &sa, len);
1111: if (error)
1112: return (error);
1113:
1114: bdom = bsd_to_linux_domain(sa.sa_family);
1115: if (bdom == -1)
1116: return (EINVAL);
1117:
1118: /* Note: we convert from sockaddr to osockaddr here, too */
1119: kosa = (struct osockaddr *) &sa;
1120: kosa->sa_family = bdom;
1121: error = copyout(kosa, osa, len);
1122: if (error)
1123: return (error);
1124:
1125: return (0);
1126: }
1127:
1128: int
1129: linux_sys_recv(p, v, retval)
1130: struct proc *p;
1131: void *v;
1132: register_t *retval;
1133: {
1134: struct linux_sys_recv_args /* {
1135: syscallarg(int) s;
1136: syscallarg(void *) buf;
1137: syscallarg(int) len;
1138: syscallarg(int) flags;
1139: } */ *uap = v;
1140: struct sys_recvfrom_args bra;
1141:
1142:
1143: SCARG(&bra, s) = SCARG(uap, s);
1144: SCARG(&bra, buf) = SCARG(uap, buf);
1145: SCARG(&bra, len) = (size_t) SCARG(uap, len);
1146: SCARG(&bra, flags) = SCARG(uap, flags);
1147: SCARG(&bra, from) = NULL;
1148: SCARG(&bra, fromlenaddr) = NULL;
1149:
1150: return (sys_recvfrom(p, &bra, retval));
1151: }
1152:
1153: int
1154: linux_sys_send(p, v, retval)
1155: struct proc *p;
1156: void *v;
1157: register_t *retval;
1158: {
1159: struct linux_sys_send_args /* {
1160: syscallarg(int) s;
1161: syscallarg(caddr_t) buf;
1162: syscallarg(int) len;
1163: syscallarg(int) flags;
1164: } */ *uap = v;
1165: struct sys_sendto_args bsa;
1166:
1167: SCARG(&bsa, s) = SCARG(uap, s);
1168: SCARG(&bsa, buf) = SCARG(uap, buf);
1169: SCARG(&bsa, len) = SCARG(uap, len);
1170: SCARG(&bsa, flags) = SCARG(uap, flags);
1171: SCARG(&bsa, to) = NULL;
1172: SCARG(&bsa, tolen) = 0;
1173:
1174: return (sys_sendto(p, &bsa, retval));
1175: }
1176:
1177: int
1178: linux_sys_accept(p, v, retval)
1179: struct proc *p;
1180: void *v;
1181: register_t *retval;
1182: {
1183: struct linux_sys_accept_args /* {
1184: syscallarg(int) s;
1185: syscallarg(struct osockaddr *) name;
1186: syscallarg(int *) anamelen;
1187: } */ *uap = v;
1188: int error;
1189: struct sys_accept_args baa;
1190:
1191: SCARG(&baa, s) = SCARG(uap, s);
1192: SCARG(&baa, name) = (struct sockaddr *) SCARG(uap, name);
1193: SCARG(&baa, anamelen) = (unsigned int *) SCARG(uap, anamelen);
1194:
1195: if ((error = sys_accept(p, &baa, retval)))
1196: return (error);
1197:
1198: if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
1199: return (error);
1200:
1201: return (0);
1.19 sommerfe 1202: }
CVSweb <webmaster@jp.NetBSD.org>