1 /* $Id: socket.c,v 1.5 2001/02/13 01:16:44 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>
11 #include <linux/smp_lock.h>
12 #include <linux/mm.h>
13 #include <linux/slab.h>
14 #include <linux/socket.h>
15 #include <linux/file.h>
16
17 #include <asm/uaccess.h>
18 #include <asm/string.h>
19 #include <asm/oplib.h>
20 #include <asm/idprom.h>
21
22 #include "conv.h"
23
24 #define SOCK_SOL_STREAM 2
25 #define SOCK_SOL_DGRAM 1
26 #define SOCK_SOL_RAW 4
27 #define SOCK_SOL_RDM 5
28 #define SOCK_SOL_SEQPACKET 6
29
30 #define SOL_SO_SNDLOWAT 0x1003
31 #define SOL_SO_RCVLOWAT 0x1004
32 #define SOL_SO_SNDTIMEO 0x1005
33 #define SOL_SO_RCVTIMEO 0x1006
34 #define SOL_SO_STATE 0x2000
35
36 #define SOL_SS_NDELAY 0x040
37 #define SOL_SS_NONBLOCK 0x080
38 #define SOL_SS_ASYNC 0x100
39
40 #define SO_STATE 0x000e
41
socket_check(int family,int type)42 static int socket_check(int family, int type)
43 {
44 if (family != PF_UNIX && family != PF_INET)
45 return -ESOCKTNOSUPPORT;
46 switch (type) {
47 case SOCK_SOL_STREAM: type = SOCK_STREAM; break;
48 case SOCK_SOL_DGRAM: type = SOCK_DGRAM; break;
49 case SOCK_SOL_RAW: type = SOCK_RAW; break;
50 case SOCK_SOL_RDM: type = SOCK_RDM; break;
51 case SOCK_SOL_SEQPACKET: type = SOCK_SEQPACKET; break;
52 default: return -EINVAL;
53 }
54 return type;
55 }
56
solaris_to_linux_sockopt(int optname)57 static int solaris_to_linux_sockopt(int optname)
58 {
59 switch (optname) {
60 case SOL_SO_SNDLOWAT: optname = SO_SNDLOWAT; break;
61 case SOL_SO_RCVLOWAT: optname = SO_RCVLOWAT; break;
62 case SOL_SO_SNDTIMEO: optname = SO_SNDTIMEO; break;
63 case SOL_SO_RCVTIMEO: optname = SO_RCVTIMEO; break;
64 case SOL_SO_STATE: optname = SO_STATE; break;
65 };
66
67 return optname;
68 }
69
solaris_socket(int family,int type,int protocol)70 asmlinkage int solaris_socket(int family, int type, int protocol)
71 {
72 int (*sys_socket)(int, int, int) =
73 (int (*)(int, int, int))SYS(socket);
74
75 type = socket_check (family, type);
76 if (type < 0) return type;
77 return sys_socket(family, type, protocol);
78 }
79
solaris_socketpair(int * usockvec)80 asmlinkage int solaris_socketpair(int *usockvec)
81 {
82 int (*sys_socketpair)(int, int, int, int *) =
83 (int (*)(int, int, int, int *))SYS(socketpair);
84
85 /* solaris socketpair really only takes one arg at the syscall
86 * level, int * usockvec. The libs apparently take care of
87 * making sure that family==AF_UNIX and type==SOCK_STREAM. The
88 * pointer we really want ends up residing in the first (and
89 * supposedly only) argument.
90 */
91
92 return sys_socketpair(AF_UNIX, SOCK_STREAM, 0, (int *)usockvec);
93 }
94
solaris_bind(int fd,struct sockaddr * addr,int addrlen)95 asmlinkage int solaris_bind(int fd, struct sockaddr *addr, int addrlen)
96 {
97 int (*sys_bind)(int, struct sockaddr *, int) =
98 (int (*)(int, struct sockaddr *, int))SUNOS(104);
99
100 return sys_bind(fd, addr, addrlen);
101 }
102
solaris_setsockopt(int fd,int level,int optname,u32 optval,int optlen)103 asmlinkage int solaris_setsockopt(int fd, int level, int optname, u32 optval, int optlen)
104 {
105 int (*sunos_setsockopt)(int, int, int, u32, int) =
106 (int (*)(int, int, int, u32, int))SUNOS(105);
107
108 optname = solaris_to_linux_sockopt(optname);
109 if (optname < 0)
110 return optname;
111 if (optname == SO_STATE)
112 return 0;
113
114 return sunos_setsockopt(fd, level, optname, optval, optlen);
115 }
116
solaris_getsockopt(int fd,int level,int optname,u32 optval,u32 optlen)117 asmlinkage int solaris_getsockopt(int fd, int level, int optname, u32 optval, u32 optlen)
118 {
119 int (*sunos_getsockopt)(int, int, int, u32, u32) =
120 (int (*)(int, int, int, u32, u32))SUNOS(118);
121
122 optname = solaris_to_linux_sockopt(optname);
123 if (optname < 0)
124 return optname;
125
126 if (optname == SO_STATE)
127 optname = SOL_SO_STATE;
128
129 return sunos_getsockopt(fd, level, optname, optval, optlen);
130 }
131
solaris_connect(int fd,struct sockaddr * addr,int addrlen)132 asmlinkage int solaris_connect(int fd, struct sockaddr *addr, int addrlen)
133 {
134 int (*sys_connect)(int, struct sockaddr *, int) =
135 (int (*)(int, struct sockaddr *, int))SYS(connect);
136
137 return sys_connect(fd, addr, addrlen);
138 }
139
solaris_accept(int fd,struct sockaddr * addr,int * addrlen)140 asmlinkage int solaris_accept(int fd, struct sockaddr *addr, int *addrlen)
141 {
142 int (*sys_accept)(int, struct sockaddr *, int *) =
143 (int (*)(int, struct sockaddr *, int *))SYS(accept);
144
145 return sys_accept(fd, addr, addrlen);
146 }
147
solaris_listen(int fd,int backlog)148 asmlinkage int solaris_listen(int fd, int backlog)
149 {
150 int (*sys_listen)(int, int) =
151 (int (*)(int, int))SUNOS(106);
152
153 return sys_listen(fd, backlog);
154 }
155
solaris_shutdown(int fd,int how)156 asmlinkage int solaris_shutdown(int fd, int how)
157 {
158 int (*sys_shutdown)(int, int) =
159 (int (*)(int, int))SYS(shutdown);
160
161 return sys_shutdown(fd, how);
162 }
163
164 #define MSG_SOL_OOB 0x1
165 #define MSG_SOL_PEEK 0x2
166 #define MSG_SOL_DONTROUTE 0x4
167 #define MSG_SOL_EOR 0x8
168 #define MSG_SOL_CTRUNC 0x10
169 #define MSG_SOL_TRUNC 0x20
170 #define MSG_SOL_WAITALL 0x40
171 #define MSG_SOL_DONTWAIT 0x80
172
solaris_to_linux_msgflags(int flags)173 static int solaris_to_linux_msgflags(int flags)
174 {
175 int fl = flags & (MSG_OOB|MSG_PEEK|MSG_DONTROUTE);
176
177 if (flags & MSG_SOL_EOR) fl |= MSG_EOR;
178 if (flags & MSG_SOL_CTRUNC) fl |= MSG_CTRUNC;
179 if (flags & MSG_SOL_TRUNC) fl |= MSG_TRUNC;
180 if (flags & MSG_SOL_WAITALL) fl |= MSG_WAITALL;
181 if (flags & MSG_SOL_DONTWAIT) fl |= MSG_DONTWAIT;
182 return fl;
183 }
184
linux_to_solaris_msgflags(int flags)185 static int linux_to_solaris_msgflags(int flags)
186 {
187 int fl = flags & (MSG_OOB|MSG_PEEK|MSG_DONTROUTE);
188
189 if (flags & MSG_EOR) fl |= MSG_SOL_EOR;
190 if (flags & MSG_CTRUNC) fl |= MSG_SOL_CTRUNC;
191 if (flags & MSG_TRUNC) fl |= MSG_SOL_TRUNC;
192 if (flags & MSG_WAITALL) fl |= MSG_SOL_WAITALL;
193 if (flags & MSG_DONTWAIT) fl |= MSG_SOL_DONTWAIT;
194 return fl;
195 }
196
solaris_recvfrom(int s,char * buf,int len,int flags,u32 from,u32 fromlen)197 asmlinkage int solaris_recvfrom(int s, char *buf, int len, int flags, u32 from, u32 fromlen)
198 {
199 int (*sys_recvfrom)(int, void *, size_t, unsigned, struct sockaddr *, int *) =
200 (int (*)(int, void *, size_t, unsigned, struct sockaddr *, int *))SYS(recvfrom);
201
202 return sys_recvfrom(s, buf, len, solaris_to_linux_msgflags(flags), (struct sockaddr *)A(from), (int *)A(fromlen));
203 }
204
solaris_recv(int s,char * buf,int len,int flags)205 asmlinkage int solaris_recv(int s, char *buf, int len, int flags)
206 {
207 int (*sys_recvfrom)(int, void *, size_t, unsigned, struct sockaddr *, int *) =
208 (int (*)(int, void *, size_t, unsigned, struct sockaddr *, int *))SYS(recvfrom);
209
210 return sys_recvfrom(s, buf, len, solaris_to_linux_msgflags(flags), NULL, NULL);
211 }
212
solaris_sendto(int s,char * buf,int len,int flags,u32 to,u32 tolen)213 asmlinkage int solaris_sendto(int s, char *buf, int len, int flags, u32 to, u32 tolen)
214 {
215 int (*sys_sendto)(int, void *, size_t, unsigned, struct sockaddr *, int *) =
216 (int (*)(int, void *, size_t, unsigned, struct sockaddr *, int *))SYS(sendto);
217
218 return sys_sendto(s, buf, len, solaris_to_linux_msgflags(flags), (struct sockaddr *)A(to), (int *)A(tolen));
219 }
220
solaris_send(int s,char * buf,int len,int flags)221 asmlinkage int solaris_send(int s, char *buf, int len, int flags)
222 {
223 int (*sys_sendto)(int, void *, size_t, unsigned, struct sockaddr *, int *) =
224 (int (*)(int, void *, size_t, unsigned, struct sockaddr *, int *))SYS(sendto);
225
226 return sys_sendto(s, buf, len, solaris_to_linux_msgflags(flags), NULL, NULL);
227 }
228
solaris_getpeername(int fd,struct sockaddr * addr,int * addrlen)229 asmlinkage int solaris_getpeername(int fd, struct sockaddr *addr, int *addrlen)
230 {
231 int (*sys_getpeername)(int, struct sockaddr *, int *) =
232 (int (*)(int, struct sockaddr *, int *))SYS(getpeername);
233
234 return sys_getpeername(fd, addr, addrlen);
235 }
236
solaris_getsockname(int fd,struct sockaddr * addr,int * addrlen)237 asmlinkage int solaris_getsockname(int fd, struct sockaddr *addr, int *addrlen)
238 {
239 int (*sys_getsockname)(int, struct sockaddr *, int *) =
240 (int (*)(int, struct sockaddr *, int *))SYS(getsockname);
241
242 return sys_getsockname(fd, addr, addrlen);
243 }
244
245 /* XXX This really belongs in some header file... -DaveM */
246 #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
247 16 for IP, 16 for IPX,
248 24 for IPv6,
249 about 80 for AX.25 */
250
251 /* XXX These as well... */
socki_lookup(struct inode * inode)252 extern __inline__ struct socket *socki_lookup(struct inode *inode)
253 {
254 return &inode->u.socket_i;
255 }
256
sockfd_lookup(int fd,int * err)257 extern __inline__ struct socket *sockfd_lookup(int fd, int *err)
258 {
259 struct file *file;
260 struct inode *inode;
261
262 if (!(file = fget(fd))) {
263 *err = -EBADF;
264 return NULL;
265 }
266
267 inode = file->f_dentry->d_inode;
268 if (!inode->i_sock || !socki_lookup(inode)) {
269 *err = -ENOTSOCK;
270 fput(file);
271 return NULL;
272 }
273
274 return socki_lookup(inode);
275 }
276
sockfd_put(struct socket * sock)277 extern __inline__ void sockfd_put(struct socket *sock)
278 {
279 fput(sock->file);
280 }
281
282 struct sol_nmsghdr {
283 u32 msg_name;
284 int msg_namelen;
285 u32 msg_iov;
286 u32 msg_iovlen;
287 u32 msg_control;
288 u32 msg_controllen;
289 u32 msg_flags;
290 };
291
292 struct sol_cmsghdr {
293 u32 cmsg_len;
294 int cmsg_level;
295 int cmsg_type;
296 unsigned char cmsg_data[0];
297 };
298
299 struct iovec32 {
300 u32 iov_base;
301 u32 iov_len;
302 };
303
iov_from_user32_to_kern(struct iovec * kiov,struct iovec32 * uiov32,int niov)304 static inline int iov_from_user32_to_kern(struct iovec *kiov,
305 struct iovec32 *uiov32,
306 int niov)
307 {
308 int tot_len = 0;
309
310 while(niov > 0) {
311 u32 len, buf;
312
313 if(get_user(len, &uiov32->iov_len) ||
314 get_user(buf, &uiov32->iov_base)) {
315 tot_len = -EFAULT;
316 break;
317 }
318 tot_len += len;
319 kiov->iov_base = (void *)A(buf);
320 kiov->iov_len = (__kernel_size_t) len;
321 uiov32++;
322 kiov++;
323 niov--;
324 }
325 return tot_len;
326 }
327
msghdr_from_user32_to_kern(struct msghdr * kmsg,struct sol_nmsghdr * umsg)328 static inline int msghdr_from_user32_to_kern(struct msghdr *kmsg,
329 struct sol_nmsghdr *umsg)
330 {
331 u32 tmp1, tmp2, tmp3;
332 int err;
333
334 err = get_user(tmp1, &umsg->msg_name);
335 err |= __get_user(tmp2, &umsg->msg_iov);
336 err |= __get_user(tmp3, &umsg->msg_control);
337 if (err)
338 return -EFAULT;
339
340 kmsg->msg_name = (void *)A(tmp1);
341 kmsg->msg_iov = (struct iovec *)A(tmp2);
342 kmsg->msg_control = (void *)A(tmp3);
343
344 err = get_user(kmsg->msg_namelen, &umsg->msg_namelen);
345 err |= get_user(kmsg->msg_controllen, &umsg->msg_controllen);
346 err |= get_user(kmsg->msg_flags, &umsg->msg_flags);
347
348 kmsg->msg_flags = solaris_to_linux_msgflags(kmsg->msg_flags);
349
350 return err;
351 }
352
353 /* I've named the args so it is easy to tell whose space the pointers are in. */
verify_iovec32(struct msghdr * kern_msg,struct iovec * kern_iov,char * kern_address,int mode)354 static int verify_iovec32(struct msghdr *kern_msg, struct iovec *kern_iov,
355 char *kern_address, int mode)
356 {
357 int tot_len;
358
359 if(kern_msg->msg_namelen) {
360 if(mode==VERIFY_READ) {
361 int err = move_addr_to_kernel(kern_msg->msg_name,
362 kern_msg->msg_namelen,
363 kern_address);
364 if(err < 0)
365 return err;
366 }
367 kern_msg->msg_name = kern_address;
368 } else
369 kern_msg->msg_name = NULL;
370
371 if(kern_msg->msg_iovlen > UIO_FASTIOV) {
372 kern_iov = kmalloc(kern_msg->msg_iovlen * sizeof(struct iovec),
373 GFP_KERNEL);
374 if(!kern_iov)
375 return -ENOMEM;
376 }
377
378 tot_len = iov_from_user32_to_kern(kern_iov,
379 (struct iovec32 *)kern_msg->msg_iov,
380 kern_msg->msg_iovlen);
381 if(tot_len >= 0)
382 kern_msg->msg_iov = kern_iov;
383 else if(kern_msg->msg_iovlen > UIO_FASTIOV)
384 kfree(kern_iov);
385
386 return tot_len;
387 }
388
solaris_sendmsg(int fd,struct sol_nmsghdr * user_msg,unsigned user_flags)389 asmlinkage int solaris_sendmsg(int fd, struct sol_nmsghdr *user_msg, unsigned user_flags)
390 {
391 struct socket *sock;
392 char address[MAX_SOCK_ADDR];
393 struct iovec iov[UIO_FASTIOV];
394 unsigned char ctl[sizeof(struct cmsghdr) + 20];
395 unsigned char *ctl_buf = ctl;
396 struct msghdr kern_msg;
397 int err, total_len;
398
399 if(msghdr_from_user32_to_kern(&kern_msg, user_msg))
400 return -EFAULT;
401 if(kern_msg.msg_iovlen > UIO_MAXIOV)
402 return -EINVAL;
403 err = verify_iovec32(&kern_msg, iov, address, VERIFY_READ);
404 if (err < 0)
405 goto out;
406 total_len = err;
407
408 if(kern_msg.msg_controllen) {
409 struct sol_cmsghdr *ucmsg = (struct sol_cmsghdr *)kern_msg.msg_control;
410 unsigned long *kcmsg;
411 __kernel_size_t32 cmlen;
412
413 if (kern_msg.msg_controllen <= sizeof(__kernel_size_t32))
414 return -EINVAL;
415
416 if(kern_msg.msg_controllen > sizeof(ctl)) {
417 err = -ENOBUFS;
418 ctl_buf = kmalloc(kern_msg.msg_controllen, GFP_KERNEL);
419 if(!ctl_buf)
420 goto out_freeiov;
421 }
422 __get_user(cmlen, &ucmsg->cmsg_len);
423 kcmsg = (unsigned long *) ctl_buf;
424 *kcmsg++ = (unsigned long)cmlen;
425 err = -EFAULT;
426 if(copy_from_user(kcmsg, &ucmsg->cmsg_level,
427 kern_msg.msg_controllen - sizeof(__kernel_size_t32)))
428 goto out_freectl;
429 kern_msg.msg_control = ctl_buf;
430 }
431 kern_msg.msg_flags = solaris_to_linux_msgflags(user_flags);
432
433 lock_kernel();
434 sock = sockfd_lookup(fd, &err);
435 if (sock != NULL) {
436 if (sock->file->f_flags & O_NONBLOCK)
437 kern_msg.msg_flags |= MSG_DONTWAIT;
438 err = sock_sendmsg(sock, &kern_msg, total_len);
439 sockfd_put(sock);
440 }
441 unlock_kernel();
442
443 out_freectl:
444 /* N.B. Use kfree here, as kern_msg.msg_controllen might change? */
445 if(ctl_buf != ctl)
446 kfree(ctl_buf);
447 out_freeiov:
448 if(kern_msg.msg_iov != iov)
449 kfree(kern_msg.msg_iov);
450 out:
451 return err;
452 }
453
solaris_recvmsg(int fd,struct sol_nmsghdr * user_msg,unsigned int user_flags)454 asmlinkage int solaris_recvmsg(int fd, struct sol_nmsghdr *user_msg, unsigned int user_flags)
455 {
456 struct iovec iovstack[UIO_FASTIOV];
457 struct msghdr kern_msg;
458 char addr[MAX_SOCK_ADDR];
459 struct socket *sock;
460 struct iovec *iov = iovstack;
461 struct sockaddr *uaddr;
462 int *uaddr_len;
463 unsigned long cmsg_ptr;
464 int err, total_len, len = 0;
465
466 if(msghdr_from_user32_to_kern(&kern_msg, user_msg))
467 return -EFAULT;
468 if(kern_msg.msg_iovlen > UIO_MAXIOV)
469 return -EINVAL;
470
471 uaddr = kern_msg.msg_name;
472 uaddr_len = &user_msg->msg_namelen;
473 err = verify_iovec32(&kern_msg, iov, addr, VERIFY_WRITE);
474 if (err < 0)
475 goto out;
476 total_len = err;
477
478 cmsg_ptr = (unsigned long) kern_msg.msg_control;
479 kern_msg.msg_flags = 0;
480
481 lock_kernel();
482 sock = sockfd_lookup(fd, &err);
483 if (sock != NULL) {
484 if (sock->file->f_flags & O_NONBLOCK)
485 user_flags |= MSG_DONTWAIT;
486 err = sock_recvmsg(sock, &kern_msg, total_len, user_flags);
487 if(err >= 0)
488 len = err;
489 sockfd_put(sock);
490 }
491 unlock_kernel();
492
493 if(uaddr != NULL && err >= 0)
494 err = move_addr_to_user(addr, kern_msg.msg_namelen, uaddr, uaddr_len);
495 if(err >= 0) {
496 err = __put_user(linux_to_solaris_msgflags(kern_msg.msg_flags), &user_msg->msg_flags);
497 if(!err) {
498 /* XXX Convert cmsg back into userspace 32-bit format... */
499 err = __put_user((unsigned long)kern_msg.msg_control - cmsg_ptr,
500 &user_msg->msg_controllen);
501 }
502 }
503
504 if(kern_msg.msg_iov != iov)
505 kfree(kern_msg.msg_iov);
506 out:
507 if(err < 0)
508 return err;
509 return len;
510 }
511