Merge master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-drm-fsl-dcu.git] / arch / sparc64 / solaris / socksys.c
1 /* $Id: socksys.c,v 1.21 2002/02/08 03:57:14 davem Exp $
2  * socksys.c: /dev/inet/ stuff for Solaris emulation.
3  *
4  * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5  * Copyright (C) 1997, 1998 Patrik Rak (prak3264@ss1000.ms.mff.cuni.cz)
6  * Copyright (C) 1995, 1996 Mike Jagdis (jaggy@purplet.demon.co.uk)
7  */
8
9 /*
10  *  Dave, _please_ give me specifications on this fscking mess so that I
11  * could at least get it into the state when it wouldn't screw the rest of
12  * the kernel over.  socksys.c and timod.c _stink_ and we are not talking
13  * H2S here, it's isopropilmercaptan in concentrations way over LD50. -- AV
14  */
15
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/smp.h>
20 #include <linux/ioctl.h>
21 #include <linux/fs.h>
22 #include <linux/file.h>
23 #include <linux/init.h>
24 #include <linux/poll.h>
25 #include <linux/slab.h>
26 #include <linux/syscalls.h>
27 #include <linux/in.h>
28
29 #include <net/sock.h>
30
31 #include <asm/uaccess.h>
32 #include <asm/termios.h>
33
34 #include "conv.h"
35 #include "socksys.h"
36
37 static int af_inet_protocols[] = {
38 IPPROTO_ICMP, IPPROTO_ICMP, IPPROTO_IGMP, IPPROTO_IPIP, IPPROTO_TCP,
39 IPPROTO_EGP, IPPROTO_PUP, IPPROTO_UDP, IPPROTO_IDP, IPPROTO_RAW,
40 0, 0, 0, 0, 0, 0,
41 };
42
43 #ifndef DEBUG_SOLARIS_KMALLOC
44
45 #define mykmalloc kmalloc
46 #define mykfree kfree
47
48 #else
49
50 extern void * mykmalloc(size_t s, gfp_t gfp);
51 extern void mykfree(void *);
52
53 #endif
54
55 static unsigned int (*sock_poll)(struct file *, poll_table *);
56
57 static struct file_operations socksys_file_ops = {
58         /* Currently empty */
59 };
60
61 static int socksys_open(struct inode * inode, struct file * filp)
62 {
63         int family, type, protocol, fd;
64         struct dentry *dentry;
65         int (*sys_socket)(int,int,int) =
66                 (int (*)(int,int,int))SUNOS(97);
67         struct sol_socket_struct * sock;
68         
69         family = ((iminor(inode) >> 4) & 0xf);
70         switch (family) {
71         case AF_UNIX:
72                 type = SOCK_STREAM;
73                 protocol = 0;
74                 break;
75         case AF_INET:
76                 protocol = af_inet_protocols[iminor(inode) & 0xf];
77                 switch (protocol) {
78                 case IPPROTO_TCP: type = SOCK_STREAM; break;
79                 case IPPROTO_UDP: type = SOCK_DGRAM; break;
80                 default: type = SOCK_RAW; break;
81                 }
82                 break;
83         default:
84                 type = SOCK_RAW;
85                 protocol = 0;
86                 break;
87         }
88
89         fd = sys_socket(family, type, protocol);
90         if (fd < 0)
91                 return fd;
92         /*
93          * N.B. The following operations are not legal!
94          *
95          * No shit.  WTF is it supposed to do, anyway?
96          *
97          * Try instead:
98          * d_delete(filp->f_path.dentry), then d_instantiate with sock inode
99          */
100         dentry = filp->f_path.dentry;
101         filp->f_path.dentry = dget(fcheck(fd)->f_path.dentry);
102         filp->f_path.dentry->d_inode->i_rdev = inode->i_rdev;
103         filp->f_path.dentry->d_inode->i_flock = inode->i_flock;
104         SOCKET_I(filp->f_path.dentry->d_inode)->file = filp;
105         filp->f_op = &socksys_file_ops;
106         sock = (struct sol_socket_struct*) 
107                 mykmalloc(sizeof(struct sol_socket_struct), GFP_KERNEL);
108         if (!sock) return -ENOMEM;
109         SOLDD(("sock=%016lx(%016lx)\n", sock, filp));
110         sock->magic = SOLARIS_SOCKET_MAGIC;
111         sock->modcount = 0;
112         sock->state = TS_UNBND;
113         sock->offset = 0;
114         sock->pfirst = sock->plast = NULL;
115         filp->private_data = sock;
116         SOLDD(("filp->private_data %016lx\n", filp->private_data));
117
118         sys_close(fd);
119         dput(dentry);
120         return 0;
121 }
122
123 static int socksys_release(struct inode * inode, struct file * filp)
124 {
125         struct sol_socket_struct * sock;
126         struct T_primsg *it;
127
128         /* XXX: check this */
129         sock = (struct sol_socket_struct *)filp->private_data;
130         SOLDD(("sock release %016lx(%016lx)\n", sock, filp));
131         it = sock->pfirst;
132         while (it) {
133                 struct T_primsg *next = it->next;
134                 
135                 SOLDD(("socksys_release %016lx->%016lx\n", it, next));
136                 mykfree((char*)it);
137                 it = next;
138         }
139         filp->private_data = NULL;
140         SOLDD(("socksys_release %016lx\n", sock));
141         mykfree((char*)sock);
142         return 0;
143 }
144
145 static unsigned int socksys_poll(struct file * filp, poll_table * wait)
146 {
147         struct inode *ino;
148         unsigned int mask = 0;
149
150         ino=filp->f_path.dentry->d_inode;
151         if (ino && S_ISSOCK(ino->i_mode)) {
152                 struct sol_socket_struct *sock;
153                 sock = (struct sol_socket_struct*)filp->private_data;
154                 if (sock && sock->pfirst) {
155                         mask |= POLLIN | POLLRDNORM;
156                         if (sock->pfirst->pri == MSG_HIPRI)
157                                 mask |= POLLPRI;
158                 }
159         }
160         if (sock_poll)
161                 mask |= (*sock_poll)(filp, wait);
162         return mask;
163 }
164         
165 static const struct file_operations socksys_fops = {
166         .open =         socksys_open,
167         .release =      socksys_release,
168 };
169
170 int __init init_socksys(void)
171 {
172         int ret;
173         struct file * file;
174         int (*sys_socket)(int,int,int) =
175                 (int (*)(int,int,int))SUNOS(97);
176         int (*sys_close)(unsigned int) = 
177                 (int (*)(unsigned int))SYS(close);
178         
179         ret = register_chrdev (30, "socksys", &socksys_fops);
180         if (ret < 0) {
181                 printk ("Couldn't register socksys character device\n");
182                 return ret;
183         }
184         ret = sys_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
185         if (ret < 0) {
186                 printk ("Couldn't create socket\n");
187                 return ret;
188         }
189
190         file = fcheck(ret);
191         /* N.B. Is this valid? Suppose the f_ops are in a module ... */
192         socksys_file_ops = *file->f_op;
193         sys_close(ret);
194         sock_poll = socksys_file_ops.poll;
195         socksys_file_ops.poll = socksys_poll;
196         socksys_file_ops.release = socksys_release;
197         return 0;
198 }
199
200 void __exit cleanup_socksys(void)
201 {
202         if (unregister_chrdev(30, "socksys"))
203                 printk ("Couldn't unregister socksys character device\n");
204 }