1 /* $Id: sunos_ioctl32.c,v 1.11 2000/07/30 23:12:24 davem Exp $
2  * sunos_ioctl32.c: SunOS ioctl compatability on sparc64.
3  *
4  * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
5  * Copyright (C) 1995, 1996, 1997 David S. Miller (davem@caip.rutgers.edu)
6  */
7 
8 #include <asm/uaccess.h>
9 
10 #include <linux/sched.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/termios.h>
14 #include <linux/ioctl.h>
15 #include <linux/route.h>
16 #include <linux/sockios.h>
17 #include <linux/if.h>
18 #include <linux/netdevice.h>
19 #include <linux/if_arp.h>
20 #include <linux/fs.h>
21 #include <linux/file.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <asm/kbio.h>
26 
27 /* Use this to get at 32-bit user passed pointers. */
28 #define A(__x)				\
29 ({	unsigned long __ret;		\
30 	__asm__ ("srl	%0, 0, %0"	\
31 		 : "=r" (__ret)		\
32 		 : "0" (__x));		\
33 	__ret;				\
34 })
35 
36 #define SUNOS_NR_OPEN	256
37 
38 struct rtentry32 {
39         u32   		rt_pad1;
40         struct sockaddr rt_dst;         /* target address               */
41         struct sockaddr rt_gateway;     /* gateway addr (RTF_GATEWAY)   */
42         struct sockaddr rt_genmask;     /* target network mask (IP)     */
43         unsigned short  rt_flags;
44         short           rt_pad2;
45         u32   		rt_pad3;
46         unsigned char   rt_tos;
47         unsigned char   rt_class;
48         short           rt_pad4;
49         short           rt_metric;      /* +1 for binary compatibility! */
50         /* char * */ u32 rt_dev;        /* forcing the device at add    */
51         u32   		rt_mtu;         /* per route MTU/Window         */
52         u32   		rt_window;      /* Window clamping              */
53         unsigned short  rt_irtt;        /* Initial RTT                  */
54 
55 };
56 
57 struct ifmap32 {
58 	u32 mem_start;
59 	u32 mem_end;
60 	unsigned short base_addr;
61 	unsigned char irq;
62 	unsigned char dma;
63 	unsigned char port;
64 };
65 
66 struct ifreq32 {
67 #define IFHWADDRLEN     6
68 #define IFNAMSIZ        16
69         union {
70                 char    ifrn_name[IFNAMSIZ];            /* if name, e.g. "en0" */
71         } ifr_ifrn;
72         union {
73                 struct  sockaddr ifru_addr;
74                 struct  sockaddr ifru_dstaddr;
75                 struct  sockaddr ifru_broadaddr;
76                 struct  sockaddr ifru_netmask;
77                 struct  sockaddr ifru_hwaddr;
78                 short   ifru_flags;
79                 int     ifru_ivalue;
80                 int     ifru_mtu;
81                 struct  ifmap32 ifru_map;
82                 char    ifru_slave[IFNAMSIZ];   /* Just fits the size */
83                 __kernel_caddr_t32 ifru_data;
84         } ifr_ifru;
85 };
86 
87 struct ifconf32 {
88         int     ifc_len;                        /* size of buffer       */
89         __kernel_caddr_t32  ifcbuf;
90 };
91 
92 extern asmlinkage int sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg);
93 
94 extern asmlinkage int sys32_ioctl(unsigned int, unsigned int, u32);
95 extern asmlinkage int sys_setsid(void);
96 
sunos_ioctl(int fd,u32 cmd,u32 arg)97 asmlinkage int sunos_ioctl (int fd, u32 cmd, u32 arg)
98 {
99 	int ret = -EBADF;
100 
101 	if(fd >= SUNOS_NR_OPEN)
102 		goto out;
103 	if(!fcheck(fd))
104 		goto out;
105 
106 	if(cmd == TIOCSETD) {
107 		mm_segment_t old_fs = get_fs();
108 		int *p, ntty = N_TTY;
109 		int tmp;
110 
111 		p = (int *)A(arg);
112 		ret = -EFAULT;
113 		if(get_user(tmp, p))
114 			goto out;
115 		if(tmp == 2) {
116 			set_fs(KERNEL_DS);
117 			ret = sys_ioctl(fd, cmd, (unsigned long) &ntty);
118 			set_fs(old_fs);
119 			ret = (ret == -EINVAL ? -EOPNOTSUPP : ret);
120 			goto out;
121 		}
122 	}
123 	if(cmd == TIOCNOTTY) {
124 		ret = sys_setsid();
125 		goto out;
126 	}
127 	switch(cmd) {
128 	case _IOW('r', 10, struct rtentry32):
129 		ret = sys32_ioctl(fd, SIOCADDRT, arg);
130 		goto out;
131 	case _IOW('r', 11, struct rtentry32):
132 		ret = sys32_ioctl(fd, SIOCDELRT, arg);
133 		goto out;
134 
135 	case _IOW('i', 12, struct ifreq32):
136 		ret = sys32_ioctl(fd, SIOCSIFADDR, arg);
137 		goto out;
138 	case _IOWR('i', 13, struct ifreq32):
139 		ret = sys32_ioctl(fd, SIOCGIFADDR, arg);
140 		goto out;
141 	case _IOW('i', 14, struct ifreq32):
142 		ret = sys32_ioctl(fd, SIOCSIFDSTADDR, arg);
143 		goto out;
144 	case _IOWR('i', 15, struct ifreq32):
145 		ret = sys32_ioctl(fd, SIOCGIFDSTADDR, arg);
146 		goto out;
147 	case _IOW('i', 16, struct ifreq32):
148 		ret = sys32_ioctl(fd, SIOCSIFFLAGS, arg);
149 		goto out;
150 	case _IOWR('i', 17, struct ifreq32):
151 		ret = sys32_ioctl(fd, SIOCGIFFLAGS, arg);
152 		goto out;
153 	case _IOW('i', 18, struct ifreq32):
154 		ret = sys32_ioctl(fd, SIOCSIFMEM, arg);
155 		goto out;
156 	case _IOWR('i', 19, struct ifreq32):
157 		ret = sys32_ioctl(fd, SIOCGIFMEM, arg);
158 		goto out;
159 
160 	case _IOWR('i', 20, struct ifconf32):
161 		ret = sys32_ioctl(fd, SIOCGIFCONF, arg);
162 		goto out;
163 
164 	case _IOW('i', 21, struct ifreq): /* SIOCSIFMTU */
165 		ret = sys_ioctl(fd, SIOCSIFMTU, arg);
166 		goto out;
167 	case _IOWR('i', 22, struct ifreq): /* SIOCGIFMTU */
168 		ret = sys_ioctl(fd, SIOCGIFMTU, arg);
169 		goto out;
170 
171 	case _IOWR('i', 23, struct ifreq32):
172 		ret = sys32_ioctl(fd, SIOCGIFBRDADDR, arg);
173 		goto out;
174 	case _IOW('i', 24, struct ifreq32):
175 		ret = sys32_ioctl(fd, SIOCSIFBRDADDR, arg);
176 		goto out;
177 	case _IOWR('i', 25, struct ifreq32):
178 		ret = sys32_ioctl(fd, SIOCGIFNETMASK, arg);
179 		goto out;
180 	case _IOW('i', 26, struct ifreq32):
181 		ret = sys32_ioctl(fd, SIOCSIFNETMASK, arg);
182 		goto out;
183 	case _IOWR('i', 27, struct ifreq32):
184 		ret = sys32_ioctl(fd, SIOCGIFMETRIC, arg);
185 		goto out;
186 	case _IOW('i', 28, struct ifreq32):
187 		ret = sys32_ioctl(fd, SIOCSIFMETRIC, arg);
188 		goto out;
189 
190 	case _IOW('i', 30, struct arpreq):
191 		ret = sys32_ioctl(fd, SIOCSARP, arg);
192 		goto out;
193 	case _IOWR('i', 31, struct arpreq):
194 		ret = sys32_ioctl(fd, SIOCGARP, arg);
195 		goto out;
196 	case _IOW('i', 32, struct arpreq):
197 		ret = sys32_ioctl(fd, SIOCDARP, arg);
198 		goto out;
199 
200 	case _IOW('i', 40, struct ifreq32): /* SIOCUPPER */
201 	case _IOW('i', 41, struct ifreq32): /* SIOCLOWER */
202 	case _IOW('i', 44, struct ifreq32): /* SIOCSETSYNC */
203 	case _IOW('i', 45, struct ifreq32): /* SIOCGETSYNC */
204 	case _IOW('i', 46, struct ifreq32): /* SIOCSSDSTATS */
205 	case _IOW('i', 47, struct ifreq32): /* SIOCSSESTATS */
206 	case _IOW('i', 48, struct ifreq32): /* SIOCSPROMISC */
207 		ret = -EOPNOTSUPP;
208 		goto out;
209 
210 	case _IOW('i', 49, struct ifreq32):
211 		ret = sys32_ioctl(fd, SIOCADDMULTI, arg);
212 		goto out;
213 	case _IOW('i', 50, struct ifreq32):
214 		ret = sys32_ioctl(fd, SIOCDELMULTI, arg);
215 		goto out;
216 
217 	/* FDDI interface ioctls, unsupported. */
218 
219 	case _IOW('i', 51, struct ifreq32): /* SIOCFDRESET */
220 	case _IOW('i', 52, struct ifreq32): /* SIOCFDSLEEP */
221 	case _IOW('i', 53, struct ifreq32): /* SIOCSTRTFMWAR */
222 	case _IOW('i', 54, struct ifreq32): /* SIOCLDNSTRTFW */
223 	case _IOW('i', 55, struct ifreq32): /* SIOCGETFDSTAT */
224 	case _IOW('i', 56, struct ifreq32): /* SIOCFDNMIINT */
225 	case _IOW('i', 57, struct ifreq32): /* SIOCFDEXUSER */
226 	case _IOW('i', 58, struct ifreq32): /* SIOCFDGNETMAP */
227 	case _IOW('i', 59, struct ifreq32): /* SIOCFDGIOCTL */
228 		printk("FDDI ioctl, returning EOPNOTSUPP\n");
229 		ret = -EOPNOTSUPP;
230 		goto out;
231 
232 	case _IOW('t', 125, int):
233 		/* More stupid tty sunos ioctls, just
234 		 * say it worked.
235 		 */
236 		ret = 0;
237 		goto out;
238 
239 	/* Non posix grp */
240 	case _IOW('t', 118, int): {
241 		int oldval, newval, *ptr;
242 
243 		cmd = TIOCSPGRP;
244 		ptr = (int *) A(arg);
245 		ret = -EFAULT;
246 		if(get_user(oldval, ptr))
247 			goto out;
248 		ret = sys32_ioctl(fd, cmd, arg);
249 		__get_user(newval, ptr);
250 		if(newval == -1) {
251 			__put_user(oldval, ptr);
252 			ret = -EIO;
253 		}
254 		if(ret == -ENOTTY)
255 			ret = -EIO;
256 		goto out;
257 	}
258 
259 	case _IOR('t', 119, int): {
260 		int oldval, newval, *ptr;
261 
262 		cmd = TIOCGPGRP;
263 		ptr = (int *) A(arg);
264 		ret = -EFAULT;
265 		if(get_user(oldval, ptr))
266 			goto out;
267 		ret = sys32_ioctl(fd, cmd, arg);
268 		__get_user(newval, ptr);
269 		if(newval == -1) {
270 			__put_user(oldval, ptr);
271 			ret = -EIO;
272 		}
273 		if(ret == -ENOTTY)
274 			ret = -EIO;
275 		goto out;
276 	}
277 	};
278 
279 	ret = sys32_ioctl(fd, cmd, arg);
280 	/* so stupid... */
281 	ret = (ret == -EINVAL ? -EOPNOTSUPP : ret);
282 out:
283 	return ret;
284 }
285