1/* ATM ioctl handling */
2
3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4/* 2003 John Levon  <levon@movementarian.org> */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
7
8#include <linux/module.h>
9#include <linux/kmod.h>
10#include <linux/net.h>		/* struct socket, struct proto_ops */
11#include <linux/atm.h>		/* ATM stuff */
12#include <linux/atmdev.h>
13#include <linux/atmclip.h>	/* CLIP_*ENCAP */
14#include <linux/atmarp.h>	/* manifest constants */
15#include <linux/capability.h>
16#include <linux/sonet.h>	/* for ioctls */
17#include <linux/atmsvc.h>
18#include <linux/atmmpc.h>
19#include <net/atmclip.h>
20#include <linux/atmlec.h>
21#include <linux/mutex.h>
22#include <asm/ioctls.h>
23#include <net/compat.h>
24
25#include "resources.h"
26#include "signaling.h"		/* for WAITING and sigd_attach */
27#include "common.h"
28
29
30static DEFINE_MUTEX(ioctl_mutex);
31static LIST_HEAD(ioctl_list);
32
33
34void register_atm_ioctl(struct atm_ioctl *ioctl)
35{
36	mutex_lock(&ioctl_mutex);
37	list_add_tail(&ioctl->list, &ioctl_list);
38	mutex_unlock(&ioctl_mutex);
39}
40EXPORT_SYMBOL(register_atm_ioctl);
41
42void deregister_atm_ioctl(struct atm_ioctl *ioctl)
43{
44	mutex_lock(&ioctl_mutex);
45	list_del(&ioctl->list);
46	mutex_unlock(&ioctl_mutex);
47}
48EXPORT_SYMBOL(deregister_atm_ioctl);
49
50static int do_vcc_ioctl(struct socket *sock, unsigned int cmd,
51			unsigned long arg, int compat)
52{
53	struct sock *sk = sock->sk;
54	struct atm_vcc *vcc;
55	int error;
56	struct list_head *pos;
57	void __user *argp = (void __user *)arg;
58
59	vcc = ATM_SD(sock);
60	switch (cmd) {
61	case SIOCOUTQ:
62		if (sock->state != SS_CONNECTED ||
63		    !test_bit(ATM_VF_READY, &vcc->flags)) {
64			error =  -EINVAL;
65			goto done;
66		}
67		error = put_user(sk->sk_sndbuf - sk_wmem_alloc_get(sk),
68				 (int __user *)argp) ? -EFAULT : 0;
69		goto done;
70	case SIOCINQ:
71	{
72		struct sk_buff *skb;
73
74		if (sock->state != SS_CONNECTED) {
75			error = -EINVAL;
76			goto done;
77		}
78		skb = skb_peek(&sk->sk_receive_queue);
79		error = put_user(skb ? skb->len : 0,
80				 (int __user *)argp) ? -EFAULT : 0;
81		goto done;
82	}
83	case SIOCGSTAMP: /* borrowed from IP */
84#ifdef CONFIG_COMPAT
85		if (compat)
86			error = compat_sock_get_timestamp(sk, argp);
87		else
88#endif
89			error = sock_get_timestamp(sk, argp);
90		goto done;
91	case SIOCGSTAMPNS: /* borrowed from IP */
92#ifdef CONFIG_COMPAT
93		if (compat)
94			error = compat_sock_get_timestampns(sk, argp);
95		else
96#endif
97			error = sock_get_timestampns(sk, argp);
98		goto done;
99	case ATM_SETSC:
100		if (net_ratelimit())
101			pr_warning("ATM_SETSC is obsolete; used by %s:%d\n",
102				   current->comm, task_pid_nr(current));
103		error = 0;
104		goto done;
105	case ATMSIGD_CTRL:
106		if (!capable(CAP_NET_ADMIN)) {
107			error = -EPERM;
108			goto done;
109		}
110		/*
111		 * The user/kernel protocol for exchanging signalling
112		 * info uses kernel pointers as opaque references,
113		 * so the holder of the file descriptor can scribble
114		 * on the kernel... so we should make sure that we
115		 * have the same privileges that /proc/kcore needs
116		 */
117		if (!capable(CAP_SYS_RAWIO)) {
118			error = -EPERM;
119			goto done;
120		}
121#ifdef CONFIG_COMPAT
122		/* WTF? I don't even want to _think_ about making this
123		   work for 32-bit userspace. TBH I don't really want
124		   to think about it at all. dwmw2. */
125		if (compat) {
126			if (net_ratelimit())
127				pr_warning("32-bit task cannot be atmsigd\n");
128			error = -EINVAL;
129			goto done;
130		}
131#endif
132		error = sigd_attach(vcc);
133		if (!error)
134			sock->state = SS_CONNECTED;
135		goto done;
136	case ATM_SETBACKEND:
137	case ATM_NEWBACKENDIF:
138	{
139		atm_backend_t backend;
140		error = get_user(backend, (atm_backend_t __user *)argp);
141		if (error)
142			goto done;
143		switch (backend) {
144		case ATM_BACKEND_PPP:
145			request_module("pppoatm");
146			break;
147		case ATM_BACKEND_BR2684:
148			request_module("br2684");
149			break;
150		}
151		break;
152	}
153	case ATMMPC_CTRL:
154	case ATMMPC_DATA:
155		request_module("mpoa");
156		break;
157	case ATMARPD_CTRL:
158		request_module("clip");
159		break;
160	case ATMLEC_CTRL:
161		request_module("lec");
162		break;
163	}
164
165	error = -ENOIOCTLCMD;
166
167	mutex_lock(&ioctl_mutex);
168	list_for_each(pos, &ioctl_list) {
169		struct atm_ioctl *ic = list_entry(pos, struct atm_ioctl, list);
170		if (try_module_get(ic->owner)) {
171			error = ic->ioctl(sock, cmd, arg);
172			module_put(ic->owner);
173			if (error != -ENOIOCTLCMD)
174				break;
175		}
176	}
177	mutex_unlock(&ioctl_mutex);
178
179	if (error != -ENOIOCTLCMD)
180		goto done;
181
182	error = atm_dev_ioctl(cmd, argp, compat);
183
184done:
185	return error;
186}
187
188int vcc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
189{
190	return do_vcc_ioctl(sock, cmd, arg, 0);
191}
192
193#ifdef CONFIG_COMPAT
194#define ATM_GETLINKRATE32 _IOW('a', ATMIOC_ITF+1, struct compat_atmif_sioc)
195#define ATM_GETNAMES32    _IOW('a', ATMIOC_ITF+3, struct compat_atm_iobuf)
196#define ATM_GETTYPE32     _IOW('a', ATMIOC_ITF+4, struct compat_atmif_sioc)
197#define ATM_GETESI32	  _IOW('a', ATMIOC_ITF+5, struct compat_atmif_sioc)
198#define ATM_GETADDR32	  _IOW('a', ATMIOC_ITF+6, struct compat_atmif_sioc)
199#define ATM_RSTADDR32	  _IOW('a', ATMIOC_ITF+7, struct compat_atmif_sioc)
200#define ATM_ADDADDR32	  _IOW('a', ATMIOC_ITF+8, struct compat_atmif_sioc)
201#define ATM_DELADDR32	  _IOW('a', ATMIOC_ITF+9, struct compat_atmif_sioc)
202#define ATM_GETCIRANGE32  _IOW('a', ATMIOC_ITF+10, struct compat_atmif_sioc)
203#define ATM_SETCIRANGE32  _IOW('a', ATMIOC_ITF+11, struct compat_atmif_sioc)
204#define ATM_SETESI32      _IOW('a', ATMIOC_ITF+12, struct compat_atmif_sioc)
205#define ATM_SETESIF32     _IOW('a', ATMIOC_ITF+13, struct compat_atmif_sioc)
206#define ATM_GETSTAT32     _IOW('a', ATMIOC_SARCOM+0, struct compat_atmif_sioc)
207#define ATM_GETSTATZ32    _IOW('a', ATMIOC_SARCOM+1, struct compat_atmif_sioc)
208#define ATM_GETLOOP32	  _IOW('a', ATMIOC_SARCOM+2, struct compat_atmif_sioc)
209#define ATM_SETLOOP32	  _IOW('a', ATMIOC_SARCOM+3, struct compat_atmif_sioc)
210#define ATM_QUERYLOOP32	  _IOW('a', ATMIOC_SARCOM+4, struct compat_atmif_sioc)
211
212static struct {
213	unsigned int cmd32;
214	unsigned int cmd;
215} atm_ioctl_map[] = {
216	{ ATM_GETLINKRATE32, ATM_GETLINKRATE },
217	{ ATM_GETNAMES32,    ATM_GETNAMES },
218	{ ATM_GETTYPE32,     ATM_GETTYPE },
219	{ ATM_GETESI32,	     ATM_GETESI },
220	{ ATM_GETADDR32,     ATM_GETADDR },
221	{ ATM_RSTADDR32,     ATM_RSTADDR },
222	{ ATM_ADDADDR32,     ATM_ADDADDR },
223	{ ATM_DELADDR32,     ATM_DELADDR },
224	{ ATM_GETCIRANGE32,  ATM_GETCIRANGE },
225	{ ATM_SETCIRANGE32,  ATM_SETCIRANGE },
226	{ ATM_SETESI32,	     ATM_SETESI },
227	{ ATM_SETESIF32,     ATM_SETESIF },
228	{ ATM_GETSTAT32,     ATM_GETSTAT },
229	{ ATM_GETSTATZ32,    ATM_GETSTATZ },
230	{ ATM_GETLOOP32,     ATM_GETLOOP },
231	{ ATM_SETLOOP32,     ATM_SETLOOP },
232	{ ATM_QUERYLOOP32,   ATM_QUERYLOOP },
233};
234
235#define NR_ATM_IOCTL ARRAY_SIZE(atm_ioctl_map)
236
237static int do_atm_iobuf(struct socket *sock, unsigned int cmd,
238			unsigned long arg)
239{
240	struct atm_iobuf __user *iobuf;
241	struct compat_atm_iobuf __user *iobuf32;
242	u32 data;
243	void __user *datap;
244	int len, err;
245
246	iobuf = compat_alloc_user_space(sizeof(*iobuf));
247	iobuf32 = compat_ptr(arg);
248
249	if (get_user(len, &iobuf32->length) ||
250	    get_user(data, &iobuf32->buffer))
251		return -EFAULT;
252	datap = compat_ptr(data);
253	if (put_user(len, &iobuf->length) ||
254	    put_user(datap, &iobuf->buffer))
255		return -EFAULT;
256
257	err = do_vcc_ioctl(sock, cmd, (unsigned long) iobuf, 0);
258
259	if (!err) {
260		if (copy_in_user(&iobuf32->length, &iobuf->length,
261				 sizeof(int)))
262			err = -EFAULT;
263	}
264
265	return err;
266}
267
268static int do_atmif_sioc(struct socket *sock, unsigned int cmd,
269			 unsigned long arg)
270{
271	struct atmif_sioc __user *sioc;
272	struct compat_atmif_sioc __user *sioc32;
273	u32 data;
274	void __user *datap;
275	int err;
276
277	sioc = compat_alloc_user_space(sizeof(*sioc));
278	sioc32 = compat_ptr(arg);
279
280	if (copy_in_user(&sioc->number, &sioc32->number, 2 * sizeof(int)) ||
281	    get_user(data, &sioc32->arg))
282		return -EFAULT;
283	datap = compat_ptr(data);
284	if (put_user(datap, &sioc->arg))
285		return -EFAULT;
286
287	err = do_vcc_ioctl(sock, cmd, (unsigned long) sioc, 0);
288
289	if (!err) {
290		if (copy_in_user(&sioc32->length, &sioc->length,
291				 sizeof(int)))
292			err = -EFAULT;
293	}
294	return err;
295}
296
297static int do_atm_ioctl(struct socket *sock, unsigned int cmd32,
298			unsigned long arg)
299{
300	int i;
301	unsigned int cmd = 0;
302
303	switch (cmd32) {
304	case SONET_GETSTAT:
305	case SONET_GETSTATZ:
306	case SONET_GETDIAG:
307	case SONET_SETDIAG:
308	case SONET_CLRDIAG:
309	case SONET_SETFRAMING:
310	case SONET_GETFRAMING:
311	case SONET_GETFRSENSE:
312		return do_atmif_sioc(sock, cmd32, arg);
313	}
314
315	for (i = 0; i < NR_ATM_IOCTL; i++) {
316		if (cmd32 == atm_ioctl_map[i].cmd32) {
317			cmd = atm_ioctl_map[i].cmd;
318			break;
319		}
320	}
321	if (i == NR_ATM_IOCTL)
322		return -EINVAL;
323
324	switch (cmd) {
325	case ATM_GETNAMES:
326		return do_atm_iobuf(sock, cmd, arg);
327
328	case ATM_GETLINKRATE:
329	case ATM_GETTYPE:
330	case ATM_GETESI:
331	case ATM_GETADDR:
332	case ATM_RSTADDR:
333	case ATM_ADDADDR:
334	case ATM_DELADDR:
335	case ATM_GETCIRANGE:
336	case ATM_SETCIRANGE:
337	case ATM_SETESI:
338	case ATM_SETESIF:
339	case ATM_GETSTAT:
340	case ATM_GETSTATZ:
341	case ATM_GETLOOP:
342	case ATM_SETLOOP:
343	case ATM_QUERYLOOP:
344		return do_atmif_sioc(sock, cmd, arg);
345	}
346
347	return -EINVAL;
348}
349
350int vcc_compat_ioctl(struct socket *sock, unsigned int cmd,
351		     unsigned long arg)
352{
353	int ret;
354
355	ret = do_vcc_ioctl(sock, cmd, arg, 1);
356	if (ret != -ENOIOCTLCMD)
357		return ret;
358
359	return do_atm_ioctl(sock, cmd, arg);
360}
361#endif
362