proc->thread stage 2: MAJOR revamping of system calls, ucred, jail API,
[dragonfly.git] / sys / emulation / linux / linux_socket.c
1 /*-
2  * Copyright (c) 1995 Søren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer 
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software withough specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/compat/linux/linux_socket.c,v 1.19.2.8 2001/11/07 20:33:55 marcel Exp $
29  * $DragonFly: src/sys/emulation/linux/linux_socket.c,v 1.3 2003/06/23 17:55:26 dillon Exp $
30  */
31
32 /* XXX we use functions that might not exist. */
33 #include "opt_compat.h"
34
35 #ifndef COMPAT_43
36 #error "Unable to compile Linux-emulator due to missing COMPAT_43 option!"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/proc.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/fcntl.h>
44 #include <sys/file.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/uio.h>
48
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52
53 #include <machine/../linux/linux.h>
54 #include <machine/../linux/linux_proto.h>
55 #include <compat/linux/linux_socket.h>
56 #include <compat/linux/linux_util.h>
57
58 /*
59  * FreeBSD's socket calls require the sockaddr struct length to agree
60  * with the address family.  Linux does not, so we must force it.
61  */
62 static int
63 linux_to_bsd_namelen(caddr_t name, int namelen)
64 {
65         uint16_t        family; /* XXX must match Linux sockaddr */
66
67         if (copyin(name, &family, sizeof(family)))
68                 return namelen;
69
70         switch (family) {
71                 case AF_INET:
72                         return sizeof(struct sockaddr_in);
73                 case AF_INET6:
74                         return sizeof(struct sockaddr_in6);
75         }
76         return namelen;
77 }
78
79 #ifndef __alpha__
80 static int
81 linux_to_bsd_domain(int domain)
82 {
83
84         switch (domain) {
85         case LINUX_AF_UNSPEC:
86                 return (AF_UNSPEC);
87         case LINUX_AF_UNIX:
88                 return (AF_LOCAL);
89         case LINUX_AF_INET:
90                 return (AF_INET);
91         case LINUX_AF_AX25:
92                 return (AF_CCITT);
93         case LINUX_AF_IPX:
94                 return (AF_IPX);
95         case LINUX_AF_APPLETALK:
96                 return (AF_APPLETALK);
97         }
98         return (-1);
99 }
100
101 static int
102 linux_to_bsd_sockopt_level(int level)
103 {
104
105         switch (level) {
106         case LINUX_SOL_SOCKET:
107                 return (SOL_SOCKET);
108         }
109         return (level);
110 }
111
112 static int
113 linux_to_bsd_ip_sockopt(int opt)
114 {
115
116         switch (opt) {
117         case LINUX_IP_TOS:
118                 return (IP_TOS);
119         case LINUX_IP_TTL:
120                 return (IP_TTL);
121         case LINUX_IP_OPTIONS:
122                 return (IP_OPTIONS);
123         case LINUX_IP_MULTICAST_IF:
124                 return (IP_MULTICAST_IF);
125         case LINUX_IP_MULTICAST_TTL:
126                 return (IP_MULTICAST_TTL);
127         case LINUX_IP_MULTICAST_LOOP:
128                 return (IP_MULTICAST_LOOP);
129         case LINUX_IP_ADD_MEMBERSHIP:
130                 return (IP_ADD_MEMBERSHIP);
131         case LINUX_IP_DROP_MEMBERSHIP:
132                 return (IP_DROP_MEMBERSHIP);
133         case LINUX_IP_HDRINCL:
134                 return (IP_HDRINCL);
135         }
136         return (-1);
137 }
138
139 static int
140 linux_to_bsd_so_sockopt(int opt)
141 {
142
143         switch (opt) {
144         case LINUX_SO_DEBUG:
145                 return (SO_DEBUG);
146         case LINUX_SO_REUSEADDR:
147                 return (SO_REUSEADDR);
148         case LINUX_SO_TYPE:
149                 return (SO_TYPE);
150         case LINUX_SO_ERROR:
151                 return (SO_ERROR);
152         case LINUX_SO_DONTROUTE:
153                 return (SO_DONTROUTE);
154         case LINUX_SO_BROADCAST:
155                 return (SO_BROADCAST);
156         case LINUX_SO_SNDBUF:
157                 return (SO_SNDBUF);
158         case LINUX_SO_RCVBUF:
159                 return (SO_RCVBUF);
160         case LINUX_SO_KEEPALIVE:
161                 return (SO_KEEPALIVE);
162         case LINUX_SO_OOBINLINE:
163                 return (SO_OOBINLINE);
164         case LINUX_SO_LINGER:
165                 return (SO_LINGER);
166         }
167         return (-1);
168 }
169
170 static int
171 linux_to_bsd_msg_flags(int flags)
172 {
173         int ret_flags = 0;
174
175         if (flags & LINUX_MSG_OOB)
176                 ret_flags |= MSG_OOB;
177         if (flags & LINUX_MSG_PEEK)
178                 ret_flags |= MSG_PEEK;
179         if (flags & LINUX_MSG_DONTROUTE)
180                 ret_flags |= MSG_DONTROUTE;
181         if (flags & LINUX_MSG_CTRUNC)
182                 ret_flags |= MSG_CTRUNC;
183         if (flags & LINUX_MSG_TRUNC)
184                 ret_flags |= MSG_TRUNC;
185         if (flags & LINUX_MSG_DONTWAIT)
186                 ret_flags |= MSG_DONTWAIT;
187         if (flags & LINUX_MSG_EOR)
188                 ret_flags |= MSG_EOR;
189         if (flags & LINUX_MSG_WAITALL)
190                 ret_flags |= MSG_WAITALL;
191 #if 0 /* not handled */
192         if (flags & LINUX_MSG_PROXY)
193                 ;
194         if (flags & LINUX_MSG_FIN)
195                 ;
196         if (flags & LINUX_MSG_SYN)
197                 ;
198         if (flags & LINUX_MSG_CONFIRM)
199                 ;
200         if (flags & LINUX_MSG_RST)
201                 ;
202         if (flags & LINUX_MSG_ERRQUEUE)
203                 ;
204         if (flags & LINUX_MSG_NOSIGNAL)
205                 ;
206 #endif
207         return ret_flags;
208 }
209
210 /* Return 0 if IP_HDRINCL is set for the given socket. */
211 static int
212 linux_check_hdrincl(int s)
213 {
214         struct getsockopt_args /* {
215                 int s;
216                 int level;
217                 int name;
218                 caddr_t val;
219                 int *avalsize;
220         } */ bsd_args;
221         int error;
222         caddr_t sg, val, valsize;
223         int size_val = sizeof val;
224         int optval;
225
226         sg = stackgap_init();
227         val = stackgap_alloc(&sg, sizeof(int));
228         valsize = stackgap_alloc(&sg, sizeof(int));
229
230         if ((error = copyout(&size_val, valsize, sizeof(size_val))))
231                 return (error);
232
233         bsd_args.s = s;
234         bsd_args.level = IPPROTO_IP;
235         bsd_args.name = IP_HDRINCL;
236         bsd_args.val = val;
237         bsd_args.avalsize = (int *)valsize;
238         if ((error = getsockopt(&bsd_args)))
239                 return (error);
240
241         if ((error = copyin(val, &optval, sizeof(optval))))
242                 return (error);
243
244         return (optval == 0);
245 }
246
247 /*
248  * Updated sendto() when IP_HDRINCL is set:
249  * tweak endian-dependent fields in the IP packet.
250  */
251 static int
252 linux_sendto_hdrincl(struct sendto_args *bsd_args)
253 {
254 /*
255  * linux_ip_copysize defines how many bytes we should copy
256  * from the beginning of the IP packet before we customize it for BSD.
257  * It should include all the fields we modify (ip_len and ip_off)
258  * and be as small as possible to minimize copying overhead.
259  */
260 #define linux_ip_copysize       8
261
262         caddr_t sg;
263         struct ip *packet;
264         struct msghdr *msg;
265         struct iovec *iov;
266
267         int error;
268         struct  sendmsg_args /* {
269                 int s;
270                 caddr_t msg;
271                 int flags;
272         } */ sendmsg_args;
273
274         /* Check the packet isn't too small before we mess with it */
275         if (bsd_args->len < linux_ip_copysize)
276                 return (EINVAL);
277
278         /*
279          * Tweaking the user buffer in place would be bad manners.
280          * We create a corrected IP header with just the needed length,
281          * then use an iovec to glue it to the rest of the user packet
282          * when calling sendmsg().
283          */
284         sg = stackgap_init();
285         packet = (struct ip *)stackgap_alloc(&sg, linux_ip_copysize);
286         msg = (struct msghdr *)stackgap_alloc(&sg, sizeof(*msg));
287         iov = (struct iovec *)stackgap_alloc(&sg, sizeof(*iov)*2);
288
289         /* Make a copy of the beginning of the packet to be sent */
290         if ((error = copyin(bsd_args->buf, packet, linux_ip_copysize)))
291                 return (error);
292
293         /* Convert fields from Linux to BSD raw IP socket format */
294         packet->ip_len = bsd_args->len;
295         packet->ip_off = ntohs(packet->ip_off);
296
297         /* Prepare the msghdr and iovec structures describing the new packet */
298         msg->msg_name = bsd_args->to;
299         msg->msg_namelen = bsd_args->tolen;
300         msg->msg_iov = iov;
301         msg->msg_iovlen = 2;
302         msg->msg_control = NULL;
303         msg->msg_controllen = 0;
304         msg->msg_flags = 0;
305         iov[0].iov_base = (char *)packet;
306         iov[0].iov_len = linux_ip_copysize;
307         iov[1].iov_base = (char *)(bsd_args->buf) + linux_ip_copysize;
308         iov[1].iov_len = bsd_args->len - linux_ip_copysize;
309
310         sendmsg_args.s = bsd_args->s;
311         sendmsg_args.msg = (caddr_t)msg;
312         sendmsg_args.flags = bsd_args->flags;
313         return (sendmsg(&sendmsg_args));
314 }
315
316 struct linux_socket_args {
317         int domain;
318         int type;
319         int protocol;
320 };
321
322 static int
323 linux_socket(struct linux_socket_args *args)
324 {
325         struct proc *p = curproc;
326         struct linux_socket_args linux_args;
327         struct socket_args /* {
328                 int domain;
329                 int type;
330                 int protocol;
331         } */ bsd_args;
332         int error;
333         int retval_socket;
334
335         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
336                 return (error);
337
338         bsd_args.protocol = linux_args.protocol;
339         bsd_args.type = linux_args.type;
340         bsd_args.domain = linux_to_bsd_domain(linux_args.domain);
341         if (bsd_args.domain == -1)
342                 return (EINVAL);
343
344         retval_socket = socket(&bsd_args);
345         if (bsd_args.type == SOCK_RAW
346             && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
347             && bsd_args.domain == AF_INET
348             && retval_socket >= 0) {
349                 /* It's a raw IP socket: set the IP_HDRINCL option. */
350                 struct setsockopt_args /* {
351                         int s;
352                         int level;
353                         int name;
354                         caddr_t val;
355                         int valsize;
356                 } */ bsd_setsockopt_args;
357                 caddr_t sg;
358                 int *hdrincl;
359
360                 sg = stackgap_init();
361                 hdrincl = (int *)stackgap_alloc(&sg, sizeof(*hdrincl));
362                 *hdrincl = 1;
363                 bsd_setsockopt_args.s = p->p_retval[0];
364                 bsd_setsockopt_args.level = IPPROTO_IP;
365                 bsd_setsockopt_args.name = IP_HDRINCL;
366                 bsd_setsockopt_args.val = (caddr_t)hdrincl;
367                 bsd_setsockopt_args.valsize = sizeof(*hdrincl);
368                 /* We ignore any error returned by setsockopt() */
369                 setsockopt(&bsd_setsockopt_args);
370                 /* Copy back the return value from socket() */
371                 p->p_retval[0] = bsd_setsockopt_args.s;
372         }
373
374         return (retval_socket);
375 }
376
377 struct linux_bind_args {
378         int s;
379         struct sockaddr *name;
380         int namelen;
381 };
382
383 static int
384 linux_bind(struct linux_bind_args *args)
385 {
386         struct linux_bind_args linux_args;
387         struct bind_args /* {
388                 int s;
389                 caddr_t name;
390                 int namelen;
391         } */ bsd_args;
392         int error;
393
394         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
395                 return (error);
396
397         bsd_args.s = linux_args.s;
398         bsd_args.name = (caddr_t)linux_args.name;
399         bsd_args.namelen = linux_to_bsd_namelen(bsd_args.name,
400             linux_args.namelen);
401         return (bind(&bsd_args));
402 }
403
404 struct linux_connect_args {
405         int s;
406         struct sockaddr * name;
407         int namelen;
408 };
409 int linux_connect(struct linux_connect_args *);
410 #endif /* !__alpha__*/
411
412 int
413 linux_connect(struct linux_connect_args *args)
414 {
415         struct proc *p = curproc;
416         struct linux_connect_args linux_args;
417         struct connect_args /* {
418                 int s;
419                 caddr_t name;
420                 int namelen;
421         } */ bsd_args;
422         struct socket *so;
423         struct file *fp;
424         int error;
425
426 #ifdef __alpha__
427         bcopy(args, &linux_args, sizeof(linux_args));
428 #else
429         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
430                 return (error);
431 #endif /* __alpha__ */
432
433         bsd_args.s = linux_args.s;
434         bsd_args.name = (caddr_t)linux_args.name;
435         bsd_args.namelen = linux_to_bsd_namelen(bsd_args.name,
436             linux_args.namelen);
437         error = connect(&bsd_args);
438         if (error != EISCONN)
439                 return (error);
440
441         /*
442          * Linux doesn't return EISCONN the first time it occurs,
443          * when on a non-blocking socket. Instead it returns the
444          * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
445          */
446         error = holdsock(p->p_fd, linux_args.s, &fp);
447         if (error)
448                 return (error);
449         error = EISCONN;
450         if (fp->f_flag & FNONBLOCK) {
451                 so = (struct socket *)fp->f_data;
452                 if (so->so_emuldata == 0)
453                         error = so->so_error;
454                 so->so_emuldata = (void *)1;
455         }
456         fdrop(fp, p);
457         return (error);
458 }
459
460 #ifndef __alpha__
461
462 struct linux_listen_args {
463         int s;
464         int backlog;
465 };
466
467 static int
468 linux_listen(struct linux_listen_args *args)
469 {
470         struct linux_listen_args linux_args;
471         struct listen_args /* {
472                 int s;
473                 int backlog;
474         } */ bsd_args;
475         int error;
476
477         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
478                 return (error);
479
480         bsd_args.s = linux_args.s;
481         bsd_args.backlog = linux_args.backlog;
482         return (listen(&bsd_args));
483 }
484
485 struct linux_accept_args {
486         int s;
487         struct sockaddr *addr;
488         int *namelen;
489 };
490
491 static int
492 linux_accept(struct linux_accept_args *args)
493 {
494         struct proc *p = curproc;
495         struct linux_accept_args linux_args;
496         struct accept_args /* {
497                 int s;
498                 caddr_t name;
499                 int *anamelen;
500         } */ bsd_args;
501         struct fcntl_args /* {
502                 int fd;
503                 int cmd;
504                 long arg;
505         } */ f_args;
506         int error;
507
508         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
509                 return (error);
510
511         bsd_args.s = linux_args.s;
512         bsd_args.name = (caddr_t)linux_args.addr;
513         bsd_args.anamelen = linux_args.namelen;
514         error = oaccept(&bsd_args);
515         if (error)
516                 return (error);
517
518         /*
519          * linux appears not to copy flags from the parent socket to the
520          * accepted one, so we must clear the flags in the new descriptor.
521          * Ignore any errors, because we already have an open fd.
522          */
523         f_args.fd = p->p_retval[0];
524         f_args.cmd = F_SETFL;
525         f_args.arg = 0;
526         (void)fcntl(&f_args);
527         p->p_retval[0] = f_args.fd;
528         return (0);
529 }
530
531 struct linux_getsockname_args {
532         int s;
533         struct sockaddr *addr;
534         int *namelen;
535 };
536
537 static int
538 linux_getsockname(struct linux_getsockname_args *args)
539 {
540         struct linux_getsockname_args linux_args;
541         struct getsockname_args /* {
542                 int fdes;
543                 caddr_t asa;
544                 int *alen;
545         } */ bsd_args;
546         int error;
547
548         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
549                 return (error);
550
551         bsd_args.fdes = linux_args.s;
552         bsd_args.asa = (caddr_t) linux_args.addr;
553         bsd_args.alen = linux_args.namelen;
554         return (ogetsockname(&bsd_args));
555 }
556
557 struct linux_getpeername_args {
558         int s;
559         struct sockaddr *addr;
560         int *namelen;
561 };
562
563 static int
564 linux_getpeername(struct linux_getpeername_args *args)
565 {
566         struct linux_getpeername_args linux_args;
567         struct ogetpeername_args /* {
568                 int fdes;
569                 caddr_t asa;
570                 int *alen;
571         } */ bsd_args;
572         int error;
573
574         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
575                 return (error);
576
577         bsd_args.fdes = linux_args.s;
578         bsd_args.asa = (caddr_t) linux_args.addr;
579         bsd_args.alen = linux_args.namelen;
580         return (ogetpeername(&bsd_args));
581 }
582
583 struct linux_socketpair_args {
584         int domain;
585         int type;
586         int protocol;
587         int *rsv;
588 };
589
590 static int
591 linux_socketpair(struct linux_socketpair_args *args)
592 {
593         struct linux_socketpair_args linux_args;
594         struct socketpair_args /* {
595                 int domain;
596                 int type;
597                 int protocol;
598                 int *rsv;
599         } */ bsd_args;
600         int error;
601
602         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
603                 return (error);
604
605         bsd_args.domain = linux_to_bsd_domain(linux_args.domain);
606         if (bsd_args.domain == -1)
607                 return (EINVAL);
608
609         bsd_args.type = linux_args.type;
610         bsd_args.protocol = linux_args.protocol;
611         bsd_args.rsv = linux_args.rsv;
612         return (socketpair(&bsd_args));
613 }
614
615 struct linux_send_args {
616         int s;
617         void *msg;
618         int len;
619         int flags;
620 };
621
622 static int
623 linux_send(struct linux_send_args *args)
624 {
625         struct linux_send_args linux_args;
626         struct osend_args /* {
627                 int s;
628                 caddr_t buf;
629                 int len;
630                 int flags;
631         } */ bsd_args;
632         int error;
633
634         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
635                 return (error);
636
637         bsd_args.s = linux_args.s;
638         bsd_args.buf = linux_args.msg;
639         bsd_args.len = linux_args.len;
640         bsd_args.flags = linux_args.flags;
641         return (osend(&bsd_args));
642 }
643
644 struct linux_recv_args {
645         int s;
646         void *msg;
647         int len;
648         int flags;
649 };
650
651 static int
652 linux_recv(struct linux_recv_args *args)
653 {
654         struct linux_recv_args linux_args;
655         struct orecv_args /* {
656                 int s;
657                 caddr_t buf;
658                 int len;
659                 int flags;
660         } */ bsd_args;
661         int error;
662
663         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
664                 return (error);
665
666         bsd_args.s = linux_args.s;
667         bsd_args.buf = linux_args.msg;
668         bsd_args.len = linux_args.len;
669         bsd_args.flags = linux_args.flags;
670         return (orecv(&bsd_args));
671 }
672
673 struct linux_sendto_args {
674         int s;
675         void *msg;
676         int len;
677         int flags;
678         caddr_t to;
679         int tolen;
680 };
681
682 static int
683 linux_sendto(struct linux_sendto_args *args)
684 {
685         struct linux_sendto_args linux_args;
686         struct sendto_args /* {
687                 int s;
688                 caddr_t buf;
689                 size_t len;
690                 int flags;
691                 caddr_t to;
692                 int tolen;
693         } */ bsd_args;
694         int error;
695
696         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
697                 return (error);
698
699         bsd_args.s = linux_args.s;
700         bsd_args.buf = linux_args.msg;
701         bsd_args.len = linux_args.len;
702         bsd_args.flags = linux_args.flags;
703         bsd_args.to = linux_args.to;
704         bsd_args.tolen = linux_args.tolen;
705
706         if (linux_check_hdrincl(linux_args.s) == 0)
707                 /* IP_HDRINCL set, tweak the packet before sending */
708                 return (linux_sendto_hdrincl(&bsd_args));
709
710         return (sendto(&bsd_args));
711 }
712
713 struct linux_recvfrom_args {
714         int s;
715         void *buf;
716         int len;
717         int flags;
718         caddr_t from;
719         int *fromlen;
720 };
721
722 static int
723 linux_recvfrom(struct linux_recvfrom_args *args)
724 {
725         struct linux_recvfrom_args linux_args;
726         struct recvfrom_args /* {
727                 int s;
728                 caddr_t buf;
729                 size_t len;
730                 int flags;
731                 caddr_t from;
732                 int *fromlenaddr;
733         } */ bsd_args;
734         int error;
735
736         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
737                 return (error);
738
739         bsd_args.s = linux_args.s;
740         bsd_args.buf = linux_args.buf;
741         bsd_args.len = linux_args.len;
742         bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
743         bsd_args.from = linux_args.from;
744         bsd_args.fromlenaddr = linux_args.fromlen;
745         return (orecvfrom(&bsd_args));
746 }
747
748 struct linux_recvmsg_args {
749         int s;
750         struct msghdr *msg;
751         int flags;
752 };
753
754 static int
755 linux_recvmsg(struct linux_recvmsg_args *args)
756 {
757         struct linux_recvmsg_args linux_args;
758         struct recvmsg_args /* {
759                 int     s;
760                 struct  msghdr *msg;
761                 int     flags;
762         } */ bsd_args;
763         int error;
764
765         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
766                 return (error);
767
768         bsd_args.s = linux_args.s;
769         bsd_args.msg = linux_args.msg;
770         bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
771         return (recvmsg(&bsd_args));
772 }
773
774 struct linux_shutdown_args {
775         int s;
776         int how;
777 };
778
779 static int
780 linux_shutdown(struct linux_shutdown_args *args)
781 {
782         struct linux_shutdown_args linux_args;
783         struct shutdown_args /* {
784                 int s;
785                 int how;
786         } */ bsd_args;
787         int error;
788
789         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
790                 return (error);
791
792         bsd_args.s = linux_args.s;
793         bsd_args.how = linux_args.how;
794         return (shutdown(&bsd_args));
795 }
796
797 struct linux_setsockopt_args {
798         int s;
799         int level;
800         int optname;
801         void *optval;
802         int optlen;
803 };
804
805 static int
806 linux_setsockopt(struct linux_setsockopt_args *args)
807 {
808         struct linux_setsockopt_args linux_args;
809         struct setsockopt_args /* {
810                 int s;
811                 int level;
812                 int name;
813                 caddr_t val;
814                 int valsize;
815         } */ bsd_args;
816         int error, name;
817
818         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
819                 return (error);
820
821         bsd_args.s = linux_args.s;
822         bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
823         switch (bsd_args.level) {
824         case SOL_SOCKET:
825                 name = linux_to_bsd_so_sockopt(linux_args.optname);
826                 break;
827         case IPPROTO_IP:
828                 name = linux_to_bsd_ip_sockopt(linux_args.optname);
829                 break;
830         case IPPROTO_TCP:
831                 /* Linux TCP option values match BSD's */
832                 name = linux_args.optname;
833                 break;
834         default:
835                 name = -1;
836                 break;
837         }
838         if (name == -1)
839                 return (EINVAL);
840
841         bsd_args.name = name;
842         bsd_args.val = linux_args.optval;
843         bsd_args.valsize = linux_args.optlen;
844         return (setsockopt(&bsd_args));
845 }
846
847 struct linux_getsockopt_args {
848         int s;
849         int level;
850         int optname;
851         void *optval;
852         int *optlen;
853 };
854
855 static int
856 linux_getsockopt(struct linux_getsockopt_args *args)
857 {
858         struct linux_getsockopt_args linux_args;
859         struct getsockopt_args /* {
860                 int s;
861                 int level;
862                 int name;
863                 caddr_t val;
864                 int *avalsize;
865         } */ bsd_args;
866         int error, name;
867
868         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
869                 return (error);
870
871         bsd_args.s = linux_args.s;
872         bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
873         switch (bsd_args.level) {
874         case SOL_SOCKET:
875                 name = linux_to_bsd_so_sockopt(linux_args.optname);
876                 break;
877         case IPPROTO_IP:
878                 name = linux_to_bsd_ip_sockopt(linux_args.optname);
879                 break;
880         case IPPROTO_TCP:
881                 /* Linux TCP option values match BSD's */
882                 name = linux_args.optname;
883                 break;
884         default:
885                 name = -1;
886                 break;
887         }
888         if (name == -1)
889                 return (EINVAL);
890
891         bsd_args.name = name;
892         bsd_args.val = linux_args.optval;
893         bsd_args.avalsize = linux_args.optlen;
894         return (getsockopt(&bsd_args));
895 }
896
897 int
898 linux_socketcall(struct linux_socketcall_args *args)
899 {
900         void *arg = (void *)args->args;
901
902         switch (args->what) {
903         case LINUX_SOCKET:
904                 return (linux_socket(arg));
905         case LINUX_BIND:
906                 return (linux_bind(arg));
907         case LINUX_CONNECT:
908                 return (linux_connect(arg));
909         case LINUX_LISTEN:
910                 return (linux_listen(arg));
911         case LINUX_ACCEPT:
912                 return (linux_accept(arg));
913         case LINUX_GETSOCKNAME:
914                 return (linux_getsockname(arg));
915         case LINUX_GETPEERNAME:
916                 return (linux_getpeername(arg));
917         case LINUX_SOCKETPAIR:
918                 return (linux_socketpair(arg));
919         case LINUX_SEND:
920                 return (linux_send(arg));
921         case LINUX_RECV:
922                 return (linux_recv(arg));
923         case LINUX_SENDTO:
924                 return (linux_sendto(arg));
925         case LINUX_RECVFROM:
926                 return (linux_recvfrom(arg));
927         case LINUX_SHUTDOWN:
928                 return (linux_shutdown(arg));
929         case LINUX_SETSOCKOPT:
930                 return (linux_setsockopt(arg));
931         case LINUX_GETSOCKOPT:
932                 return (linux_getsockopt(arg));
933         case LINUX_SENDMSG:
934                 do {
935                         int error;
936                         int level;
937                         caddr_t control;
938                         struct {
939                                 int s;
940                                 const struct msghdr *msg;
941                                 int flags;
942                         } *uap = arg;
943
944                         error = copyin(&uap->msg->msg_control, &control,
945                             sizeof(caddr_t));
946                         if (error)
947                                 return (error);
948
949                         if (control == NULL)
950                                 goto done;
951
952                         error = copyin(&((struct cmsghdr*)control)->cmsg_level,
953                             &level, sizeof(int));
954                         if (error)
955                                 return (error);
956
957                         if (level == 1) {
958                                 /*
959                                  * Linux thinks that SOL_SOCKET is 1; we know
960                                  * that it's really 0xffff, of course.
961                                  */
962                                 level = SOL_SOCKET;
963                                 error = copyout(&level,
964                                     &((struct cmsghdr *)control)->cmsg_level,
965                                     sizeof(int));
966                                 if (error)
967                                         return (error);
968                         }
969                 done:
970                         return (sendmsg(arg));
971                 } while (0);
972         case LINUX_RECVMSG:
973                 return (linux_recvmsg(arg));
974         }
975
976         uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
977         return (ENOSYS);
978 }
979 #endif  /*!__alpha__*/