blob: cc69847cf24071035b28cdd349dd771503f01a0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: socket.c,v 1.6 2002/02/08 03:57:14 davem Exp $
2 * socket.c: Socket syscall emulation for Solaris 2.6+
3 *
4 * Copyright (C) 1998 Jakub Jelinek (jj@ultra.linux.cz)
5 *
6 * 1999-08-19 Fixed socketpair code
7 * Jason Rappleye (rappleye@ccr.buffalo.edu)
8 */
9
10#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/mm.h>
12#include <linux/slab.h>
13#include <linux/socket.h>
14#include <linux/file.h>
15#include <linux/net.h>
16#include <linux/compat.h>
17#include <net/compat.h>
Andrew Mortond64d3872005-08-09 15:29:19 -070018#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/uaccess.h>
21#include <asm/string.h>
22#include <asm/oplib.h>
23#include <asm/idprom.h>
24
25#include "conv.h"
26
27#define SOCK_SOL_STREAM 2
28#define SOCK_SOL_DGRAM 1
29#define SOCK_SOL_RAW 4
30#define SOCK_SOL_RDM 5
31#define SOCK_SOL_SEQPACKET 6
32
33#define SOL_SO_SNDLOWAT 0x1003
34#define SOL_SO_RCVLOWAT 0x1004
35#define SOL_SO_SNDTIMEO 0x1005
36#define SOL_SO_RCVTIMEO 0x1006
37#define SOL_SO_STATE 0x2000
38
39#define SOL_SS_NDELAY 0x040
40#define SOL_SS_NONBLOCK 0x080
41#define SOL_SS_ASYNC 0x100
42
43#define SO_STATE 0x000e
44
45static int socket_check(int family, int type)
46{
47 if (family != PF_UNIX && family != PF_INET)
48 return -ESOCKTNOSUPPORT;
49 switch (type) {
50 case SOCK_SOL_STREAM: type = SOCK_STREAM; break;
51 case SOCK_SOL_DGRAM: type = SOCK_DGRAM; break;
52 case SOCK_SOL_RAW: type = SOCK_RAW; break;
53 case SOCK_SOL_RDM: type = SOCK_RDM; break;
54 case SOCK_SOL_SEQPACKET: type = SOCK_SEQPACKET; break;
55 default: return -EINVAL;
56 }
57 return type;
58}
59
60static int solaris_to_linux_sockopt(int optname)
61{
62 switch (optname) {
63 case SOL_SO_SNDLOWAT: optname = SO_SNDLOWAT; break;
64 case SOL_SO_RCVLOWAT: optname = SO_RCVLOWAT; break;
65 case SOL_SO_SNDTIMEO: optname = SO_SNDTIMEO; break;
66 case SOL_SO_RCVTIMEO: optname = SO_RCVTIMEO; break;
67 case SOL_SO_STATE: optname = SO_STATE; break;
68 };
69
70 return optname;
71}
72
73asmlinkage int solaris_socket(int family, int type, int protocol)
74{
75 int (*sys_socket)(int, int, int) =
76 (int (*)(int, int, int))SYS(socket);
77
78 type = socket_check (family, type);
79 if (type < 0) return type;
80 return sys_socket(family, type, protocol);
81}
82
83asmlinkage int solaris_socketpair(int *usockvec)
84{
85 int (*sys_socketpair)(int, int, int, int *) =
86 (int (*)(int, int, int, int *))SYS(socketpair);
87
88 /* solaris socketpair really only takes one arg at the syscall
89 * level, int * usockvec. The libs apparently take care of
90 * making sure that family==AF_UNIX and type==SOCK_STREAM. The
91 * pointer we really want ends up residing in the first (and
92 * supposedly only) argument.
93 */
94
95 return sys_socketpair(AF_UNIX, SOCK_STREAM, 0, (int *)usockvec);
96}
97
98asmlinkage int solaris_bind(int fd, struct sockaddr *addr, int addrlen)
99{
100 int (*sys_bind)(int, struct sockaddr *, int) =
101 (int (*)(int, struct sockaddr *, int))SUNOS(104);
102
103 return sys_bind(fd, addr, addrlen);
104}
105
106asmlinkage int solaris_setsockopt(int fd, int level, int optname, u32 optval, int optlen)
107{
108 int (*sunos_setsockopt)(int, int, int, u32, int) =
109 (int (*)(int, int, int, u32, int))SUNOS(105);
110
111 optname = solaris_to_linux_sockopt(optname);
112 if (optname < 0)
113 return optname;
114 if (optname == SO_STATE)
115 return 0;
116
117 return sunos_setsockopt(fd, level, optname, optval, optlen);
118}
119
120asmlinkage int solaris_getsockopt(int fd, int level, int optname, u32 optval, u32 optlen)
121{
122 int (*sunos_getsockopt)(int, int, int, u32, u32) =
123 (int (*)(int, int, int, u32, u32))SUNOS(118);
124
125 optname = solaris_to_linux_sockopt(optname);
126 if (optname < 0)
127 return optname;
128
129 if (optname == SO_STATE)
130 optname = SOL_SO_STATE;
131
132 return sunos_getsockopt(fd, level, optname, optval, optlen);
133}
134
135asmlinkage int solaris_connect(int fd, struct sockaddr __user *addr, int addrlen)
136{
137 int (*sys_connect)(int, struct sockaddr __user *, int) =
138 (int (*)(int, struct sockaddr __user *, int))SYS(connect);
139
140 return sys_connect(fd, addr, addrlen);
141}
142
143asmlinkage int solaris_accept(int fd, struct sockaddr __user *addr, int __user *addrlen)
144{
145 int (*sys_accept)(int, struct sockaddr __user *, int __user *) =
146 (int (*)(int, struct sockaddr __user *, int __user *))SYS(accept);
147
148 return sys_accept(fd, addr, addrlen);
149}
150
151asmlinkage int solaris_listen(int fd, int backlog)
152{
153 int (*sys_listen)(int, int) =
154 (int (*)(int, int))SUNOS(106);
155
156 return sys_listen(fd, backlog);
157}
158
159asmlinkage int solaris_shutdown(int fd, int how)
160{
161 int (*sys_shutdown)(int, int) =
162 (int (*)(int, int))SYS(shutdown);
163
164 return sys_shutdown(fd, how);
165}
166
167#define MSG_SOL_OOB 0x1
168#define MSG_SOL_PEEK 0x2
169#define MSG_SOL_DONTROUTE 0x4
170#define MSG_SOL_EOR 0x8
171#define MSG_SOL_CTRUNC 0x10
172#define MSG_SOL_TRUNC 0x20
173#define MSG_SOL_WAITALL 0x40
174#define MSG_SOL_DONTWAIT 0x80
175
176static int solaris_to_linux_msgflags(int flags)
177{
178 int fl = flags & (MSG_OOB|MSG_PEEK|MSG_DONTROUTE);
179
180 if (flags & MSG_SOL_EOR) fl |= MSG_EOR;
181 if (flags & MSG_SOL_CTRUNC) fl |= MSG_CTRUNC;
182 if (flags & MSG_SOL_TRUNC) fl |= MSG_TRUNC;
183 if (flags & MSG_SOL_WAITALL) fl |= MSG_WAITALL;
184 if (flags & MSG_SOL_DONTWAIT) fl |= MSG_DONTWAIT;
185 return fl;
186}
187
188static int linux_to_solaris_msgflags(int flags)
189{
190 int fl = flags & (MSG_OOB|MSG_PEEK|MSG_DONTROUTE);
191
192 if (flags & MSG_EOR) fl |= MSG_SOL_EOR;
193 if (flags & MSG_CTRUNC) fl |= MSG_SOL_CTRUNC;
194 if (flags & MSG_TRUNC) fl |= MSG_SOL_TRUNC;
195 if (flags & MSG_WAITALL) fl |= MSG_SOL_WAITALL;
196 if (flags & MSG_DONTWAIT) fl |= MSG_SOL_DONTWAIT;
197 return fl;
198}
199
200asmlinkage int solaris_recvfrom(int s, char __user *buf, int len, int flags, u32 from, u32 fromlen)
201{
202 int (*sys_recvfrom)(int, void __user *, size_t, unsigned, struct sockaddr __user *, int __user *) =
203 (int (*)(int, void __user *, size_t, unsigned, struct sockaddr __user *, int __user *))SYS(recvfrom);
204
205 return sys_recvfrom(s, buf, len, solaris_to_linux_msgflags(flags), A(from), A(fromlen));
206}
207
208asmlinkage int solaris_recv(int s, char __user *buf, int len, int flags)
209{
210 int (*sys_recvfrom)(int, void __user *, size_t, unsigned, struct sockaddr __user *, int __user *) =
211 (int (*)(int, void __user *, size_t, unsigned, struct sockaddr __user *, int __user *))SYS(recvfrom);
212
213 return sys_recvfrom(s, buf, len, solaris_to_linux_msgflags(flags), NULL, NULL);
214}
215
216asmlinkage int solaris_sendto(int s, char __user *buf, int len, int flags, u32 to, u32 tolen)
217{
218 int (*sys_sendto)(int, void __user *, size_t, unsigned, struct sockaddr __user *, int __user *) =
219 (int (*)(int, void __user *, size_t, unsigned, struct sockaddr __user *, int __user *))SYS(sendto);
220
221 return sys_sendto(s, buf, len, solaris_to_linux_msgflags(flags), A(to), A(tolen));
222}
223
224asmlinkage int solaris_send(int s, char *buf, int len, int flags)
225{
226 int (*sys_sendto)(int, void *, size_t, unsigned, struct sockaddr *, int *) =
227 (int (*)(int, void *, size_t, unsigned, struct sockaddr *, int *))SYS(sendto);
228
229 return sys_sendto(s, buf, len, solaris_to_linux_msgflags(flags), NULL, NULL);
230}
231
232asmlinkage int solaris_getpeername(int fd, struct sockaddr *addr, int *addrlen)
233{
234 int (*sys_getpeername)(int, struct sockaddr *, int *) =
235 (int (*)(int, struct sockaddr *, int *))SYS(getpeername);
236
237 return sys_getpeername(fd, addr, addrlen);
238}
239
240asmlinkage int solaris_getsockname(int fd, struct sockaddr *addr, int *addrlen)
241{
242 int (*sys_getsockname)(int, struct sockaddr *, int *) =
243 (int (*)(int, struct sockaddr *, int *))SYS(getsockname);
244
245 return sys_getsockname(fd, addr, addrlen);
246}
247
248/* XXX This really belongs in some header file... -DaveM */
249#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
250 16 for IP, 16 for IPX,
251 24 for IPv6,
252 about 80 for AX.25 */
253
254struct sol_nmsghdr {
255 u32 msg_name;
256 int msg_namelen;
257 u32 msg_iov;
258 u32 msg_iovlen;
259 u32 msg_control;
260 u32 msg_controllen;
261 u32 msg_flags;
262};
263
264struct sol_cmsghdr {
265 u32 cmsg_len;
266 int cmsg_level;
267 int cmsg_type;
268 unsigned char cmsg_data[0];
269};
270
271static inline int msghdr_from_user32_to_kern(struct msghdr *kmsg,
272 struct sol_nmsghdr __user *umsg)
273{
274 u32 tmp1, tmp2, tmp3;
275 int err;
276
277 err = get_user(tmp1, &umsg->msg_name);
278 err |= __get_user(tmp2, &umsg->msg_iov);
279 err |= __get_user(tmp3, &umsg->msg_control);
280 if (err)
281 return -EFAULT;
282
283 kmsg->msg_name = A(tmp1);
284 kmsg->msg_iov = A(tmp2);
285 kmsg->msg_control = A(tmp3);
286
287 err = get_user(kmsg->msg_namelen, &umsg->msg_namelen);
288 err |= get_user(kmsg->msg_controllen, &umsg->msg_controllen);
289 err |= get_user(kmsg->msg_flags, &umsg->msg_flags);
290
291 kmsg->msg_flags = solaris_to_linux_msgflags(kmsg->msg_flags);
292
293 return err;
294}
295
296asmlinkage int solaris_sendmsg(int fd, struct sol_nmsghdr __user *user_msg, unsigned user_flags)
297{
298 struct socket *sock;
299 char address[MAX_SOCK_ADDR];
Andrew Mortond64d3872005-08-09 15:29:19 -0700300 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned char ctl[sizeof(struct cmsghdr) + 20];
302 unsigned char *ctl_buf = ctl;
Andrew Mortond64d3872005-08-09 15:29:19 -0700303 struct msghdr msg_sys;
304 int err, ctl_len, iov_size, total_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Andrew Mortond64d3872005-08-09 15:29:19 -0700306 err = -EFAULT;
307 if (msghdr_from_user32_to_kern(&msg_sys, user_msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto out;
Andrew Mortond64d3872005-08-09 15:29:19 -0700309
310 sock = sockfd_lookup(fd, &err);
311 if (!sock)
312 goto out;
313
314 /* do not move before msg_sys is valid */
315 err = -EMSGSIZE;
316 if (msg_sys.msg_iovlen > UIO_MAXIOV)
317 goto out_put;
318
319 /* Check whether to allocate the iovec area*/
320 err = -ENOMEM;
321 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
322 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
323 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
324 if (!iov)
325 goto out_put;
326 }
327
328 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
329 if (err < 0)
330 goto out_freeiov;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 total_len = err;
332
Andrew Mortond64d3872005-08-09 15:29:19 -0700333 err = -ENOBUFS;
334 if (msg_sys.msg_controllen > INT_MAX)
335 goto out_freeiov;
336
337 ctl_len = msg_sys.msg_controllen;
338 if (ctl_len) {
339 struct sol_cmsghdr __user *ucmsg = msg_sys.msg_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned long *kcmsg;
341 compat_size_t cmlen;
342
Andrew Mortond64d3872005-08-09 15:29:19 -0700343 err = -EINVAL;
344 if (ctl_len <= sizeof(compat_size_t))
345 goto out_freeiov;
David S. Miller8005aba2005-06-21 15:39:22 -0700346
Andrew Mortond64d3872005-08-09 15:29:19 -0700347 if (ctl_len > sizeof(ctl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 err = -ENOBUFS;
Andrew Mortond64d3872005-08-09 15:29:19 -0700349 ctl_buf = kmalloc(ctl_len, GFP_KERNEL);
350 if (!ctl_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto out_freeiov;
352 }
353 __get_user(cmlen, &ucmsg->cmsg_len);
354 kcmsg = (unsigned long *) ctl_buf;
355 *kcmsg++ = (unsigned long)cmlen;
356 err = -EFAULT;
Andrew Mortond64d3872005-08-09 15:29:19 -0700357 if (copy_from_user(kcmsg, &ucmsg->cmsg_level,
358 ctl_len - sizeof(compat_size_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto out_freectl;
Andrew Mortond64d3872005-08-09 15:29:19 -0700360 msg_sys.msg_control = ctl_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Andrew Mortond64d3872005-08-09 15:29:19 -0700362 msg_sys.msg_flags = solaris_to_linux_msgflags(user_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Andrew Mortond64d3872005-08-09 15:29:19 -0700364 if (sock->file->f_flags & O_NONBLOCK)
365 msg_sys.msg_flags |= MSG_DONTWAIT;
366 err = sock_sendmsg(sock, &msg_sys, total_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368out_freectl:
Andrew Mortond64d3872005-08-09 15:29:19 -0700369 if (ctl_buf != ctl)
370 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371out_freeiov:
Andrew Mortond64d3872005-08-09 15:29:19 -0700372 if (iov != iovstack)
373 sock_kfree_s(sock->sk, iov, iov_size);
374out_put:
375 sockfd_put(sock);
376out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return err;
378}
379
380asmlinkage int solaris_recvmsg(int fd, struct sol_nmsghdr __user *user_msg, unsigned int user_flags)
381{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct socket *sock;
Andrew Mortond64d3872005-08-09 15:29:19 -0700383 struct iovec iovstack[UIO_FASTIOV];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct iovec *iov = iovstack;
Andrew Mortond64d3872005-08-09 15:29:19 -0700385 struct msghdr msg_sys;
386 unsigned long cmsg_ptr;
387 int err, iov_size, total_len, len;
388
389 /* kernel mode address */
390 char addr[MAX_SOCK_ADDR];
391
392 /* user mode address pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct sockaddr __user *uaddr;
394 int __user *uaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Andrew Mortond64d3872005-08-09 15:29:19 -0700396 if (msghdr_from_user32_to_kern(&msg_sys, user_msg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Andrew Mortond64d3872005-08-09 15:29:19 -0700399 sock = sockfd_lookup(fd, &err);
400 if (!sock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 goto out;
Andrew Mortond64d3872005-08-09 15:29:19 -0700402
403 err = -EMSGSIZE;
404 if (msg_sys.msg_iovlen > UIO_MAXIOV)
405 goto out_put;
406
407 /* Check whether to allocate the iovec area*/
408 err = -ENOMEM;
409 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
410 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
411 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
412 if (!iov)
413 goto out_put;
414 }
415
416 /*
417 * Save the user-mode address (verify_iovec will change the
418 * kernel msghdr to use the kernel address space)
419 */
420
421 uaddr = (void __user *) msg_sys.msg_name;
422 uaddr_len = &user_msg->msg_namelen;
423 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
424 if (err < 0)
425 goto out_freeiov;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 total_len = err;
427
Andrew Mortond64d3872005-08-09 15:29:19 -0700428 cmsg_ptr = (unsigned long) msg_sys.msg_control;
429 msg_sys.msg_flags = MSG_CMSG_COMPAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Andrew Mortond64d3872005-08-09 15:29:19 -0700431 if (sock->file->f_flags & O_NONBLOCK)
432 user_flags |= MSG_DONTWAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Andrew Mortond64d3872005-08-09 15:29:19 -0700434 err = sock_recvmsg(sock, &msg_sys, total_len, user_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if(err < 0)
Andrew Mortond64d3872005-08-09 15:29:19 -0700436 goto out_freeiov;
437
438 len = err;
439
440 if (uaddr != NULL) {
441 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
442 if (err < 0)
443 goto out_freeiov;
444 }
445 err = __put_user(linux_to_solaris_msgflags(msg_sys.msg_flags), &user_msg->msg_flags);
446 if (err)
447 goto out_freeiov;
448 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
449 &user_msg->msg_controllen);
450 if (err)
451 goto out_freeiov;
452 err = len;
453
454out_freeiov:
455 if (iov != iovstack)
456 sock_kfree_s(sock->sk, iov, iov_size);
457out_put:
458 sockfd_put(sock);
459out:
460 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}