Blame net/atm/ioctl.c

Packit 7b02f3
// SPDX-License-Identifier: GPL-2.0
Packit 7b02f3
/* ATM ioctl handling */
Packit 7b02f3
Packit 7b02f3
/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
Packit 7b02f3
/* 2003 John Levon  <levon@movementarian.org> */
Packit 7b02f3
Packit 7b02f3
#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
Packit 7b02f3
Packit 7b02f3
#include <linux/module.h>
Packit 7b02f3
#include <linux/kmod.h>
Packit 7b02f3
#include <linux/net.h>		/* struct socket, struct proto_ops */
Packit 7b02f3
#include <linux/atm.h>		/* ATM stuff */
Packit 7b02f3
#include <linux/atmdev.h>
Packit 7b02f3
#include <linux/atmclip.h>	/* CLIP_*ENCAP */
Packit 7b02f3
#include <linux/atmarp.h>	/* manifest constants */
Packit 7b02f3
#include <linux/capability.h>
Packit 7b02f3
#include <linux/sonet.h>	/* for ioctls */
Packit 7b02f3
#include <linux/atmsvc.h>
Packit 7b02f3
#include <linux/atmmpc.h>
Packit 7b02f3
#include <net/atmclip.h>
Packit 7b02f3
#include <linux/atmlec.h>
Packit 7b02f3
#include <linux/mutex.h>
Packit 7b02f3
#include <asm/ioctls.h>
Packit 7b02f3
#include <net/compat.h>
Packit 7b02f3
Packit 7b02f3
#include "resources.h"
Packit 7b02f3
#include "signaling.h"		/* for WAITING and sigd_attach */
Packit 7b02f3
#include "common.h"
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
static DEFINE_MUTEX(ioctl_mutex);
Packit 7b02f3
static LIST_HEAD(ioctl_list);
Packit 7b02f3
Packit 7b02f3
Packit 7b02f3
void register_atm_ioctl(struct atm_ioctl *ioctl)
Packit 7b02f3
{
Packit 7b02f3
	mutex_lock(&ioctl_mutex);
Packit 7b02f3
	list_add_tail(&ioctl->list, &ioctl_list);
Packit 7b02f3
	mutex_unlock(&ioctl_mutex);
Packit 7b02f3
}
Packit 7b02f3
EXPORT_SYMBOL(register_atm_ioctl);
Packit 7b02f3
Packit 7b02f3
void deregister_atm_ioctl(struct atm_ioctl *ioctl)
Packit 7b02f3
{
Packit 7b02f3
	mutex_lock(&ioctl_mutex);
Packit 7b02f3
	list_del(&ioctl->list);
Packit 7b02f3
	mutex_unlock(&ioctl_mutex);
Packit 7b02f3
}
Packit 7b02f3
EXPORT_SYMBOL(deregister_atm_ioctl);
Packit 7b02f3
Packit 7b02f3
static int do_vcc_ioctl(struct socket *sock, unsigned int cmd,
Packit 7b02f3
			unsigned long arg, int compat)
Packit 7b02f3
{
Packit 7b02f3
	struct sock *sk = sock->sk;
Packit 7b02f3
	struct atm_vcc *vcc;
Packit 7b02f3
	int error;
Packit 7b02f3
	struct list_head *pos;
Packit 7b02f3
	void __user *argp = (void __user *)arg;
Packit 7b02f3
Packit 7b02f3
	vcc = ATM_SD(sock);
Packit 7b02f3
	switch (cmd) {
Packit 7b02f3
	case SIOCOUTQ:
Packit 7b02f3
		if (sock->state != SS_CONNECTED ||
Packit 7b02f3
		    !test_bit(ATM_VF_READY, &vcc->flags)) {
Packit 7b02f3
			error =  -EINVAL;
Packit 7b02f3
			goto done;
Packit 7b02f3
		}
Packit 7b02f3
		error = put_user(sk->sk_sndbuf - sk_wmem_alloc_get(sk),
Packit 7b02f3
				 (int __user *)argp) ? -EFAULT : 0;
Packit 7b02f3
		goto done;
Packit 7b02f3
	case SIOCINQ:
Packit 7b02f3
	{
Packit 7b02f3
		struct sk_buff *skb;
Packit 7b02f3
Packit 7b02f3
		if (sock->state != SS_CONNECTED) {
Packit 7b02f3
			error = -EINVAL;
Packit 7b02f3
			goto done;
Packit 7b02f3
		}
Packit 7b02f3
		skb = skb_peek(&sk->sk_receive_queue);
Packit 7b02f3
		error = put_user(skb ? skb->len : 0,
Packit 7b02f3
				 (int __user *)argp) ? -EFAULT : 0;
Packit 7b02f3
		goto done;
Packit 7b02f3
	}
Packit 7b02f3
	case SIOCGSTAMP: /* borrowed from IP */
Packit 7b02f3
#ifdef CONFIG_COMPAT
Packit 7b02f3
		if (compat)
Packit 7b02f3
			error = compat_sock_get_timestamp(sk, argp);
Packit 7b02f3
		else
Packit 7b02f3
#endif
Packit 7b02f3
			error = sock_get_timestamp(sk, argp);
Packit 7b02f3
		goto done;
Packit 7b02f3
	case SIOCGSTAMPNS: /* borrowed from IP */
Packit 7b02f3
#ifdef CONFIG_COMPAT
Packit 7b02f3
		if (compat)
Packit 7b02f3
			error = compat_sock_get_timestampns(sk, argp);
Packit 7b02f3
		else
Packit 7b02f3
#endif
Packit 7b02f3
			error = sock_get_timestampns(sk, argp);
Packit 7b02f3
		goto done;
Packit 7b02f3
	case ATM_SETSC:
Packit 7b02f3
		net_warn_ratelimited("ATM_SETSC is obsolete; used by %s:%d\n",
Packit 7b02f3
				     current->comm, task_pid_nr(current));
Packit 7b02f3
		error = 0;
Packit 7b02f3
		goto done;
Packit 7b02f3
	case ATMSIGD_CTRL:
Packit 7b02f3
		if (!capable(CAP_NET_ADMIN)) {
Packit 7b02f3
			error = -EPERM;
Packit 7b02f3
			goto done;
Packit 7b02f3
		}
Packit 7b02f3
		/*
Packit 7b02f3
		 * The user/kernel protocol for exchanging signalling
Packit 7b02f3
		 * info uses kernel pointers as opaque references,
Packit 7b02f3
		 * so the holder of the file descriptor can scribble
Packit 7b02f3
		 * on the kernel... so we should make sure that we
Packit 7b02f3
		 * have the same privileges that /proc/kcore needs
Packit 7b02f3
		 */
Packit 7b02f3
		if (!capable(CAP_SYS_RAWIO)) {
Packit 7b02f3
			error = -EPERM;
Packit 7b02f3
			goto done;
Packit 7b02f3
		}
Packit 7b02f3
#ifdef CONFIG_COMPAT
Packit 7b02f3
		/* WTF? I don't even want to _think_ about making this
Packit 7b02f3
		   work for 32-bit userspace. TBH I don't really want
Packit 7b02f3
		   to think about it at all. dwmw2. */
Packit 7b02f3
		if (compat) {
Packit 7b02f3
			net_warn_ratelimited("32-bit task cannot be atmsigd\n");
Packit 7b02f3
			error = -EINVAL;
Packit 7b02f3
			goto done;
Packit 7b02f3
		}
Packit 7b02f3
#endif
Packit 7b02f3
		error = sigd_attach(vcc);
Packit 7b02f3
		if (!error)
Packit 7b02f3
			sock->state = SS_CONNECTED;
Packit 7b02f3
		goto done;
Packit 7b02f3
	case ATM_SETBACKEND:
Packit 7b02f3
	case ATM_NEWBACKENDIF:
Packit 7b02f3
	{
Packit 7b02f3
		atm_backend_t backend;
Packit 7b02f3
		error = get_user(backend, (atm_backend_t __user *)argp);
Packit 7b02f3
		if (error)
Packit 7b02f3
			goto done;
Packit 7b02f3
		switch (backend) {
Packit 7b02f3
		case ATM_BACKEND_PPP:
Packit 7b02f3
			request_module("pppoatm");
Packit 7b02f3
			break;
Packit 7b02f3
		case ATM_BACKEND_BR2684:
Packit 7b02f3
			request_module("br2684");
Packit 7b02f3
			break;
Packit 7b02f3
		}
Packit 7b02f3
		break;
Packit 7b02f3
	}
Packit 7b02f3
	case ATMMPC_CTRL:
Packit 7b02f3
	case ATMMPC_DATA:
Packit 7b02f3
		request_module("mpoa");
Packit 7b02f3
		break;
Packit 7b02f3
	case ATMARPD_CTRL:
Packit 7b02f3
		request_module("clip");
Packit 7b02f3
		break;
Packit 7b02f3
	case ATMLEC_CTRL:
Packit 7b02f3
		request_module("lec");
Packit 7b02f3
		break;
Packit 7b02f3
	}
Packit 7b02f3
Packit 7b02f3
	error = -ENOIOCTLCMD;
Packit 7b02f3
Packit 7b02f3
	mutex_lock(&ioctl_mutex);
Packit 7b02f3
	list_for_each(pos, &ioctl_list) {
Packit 7b02f3
		struct atm_ioctl *ic = list_entry(pos, struct atm_ioctl, list);
Packit 7b02f3
		if (try_module_get(ic->owner)) {
Packit 7b02f3
			error = ic->ioctl(sock, cmd, arg);
Packit 7b02f3
			module_put(ic->owner);
Packit 7b02f3
			if (error != -ENOIOCTLCMD)
Packit 7b02f3
				break;
Packit 7b02f3
		}
Packit 7b02f3
	}
Packit 7b02f3
	mutex_unlock(&ioctl_mutex);
Packit 7b02f3
Packit 7b02f3
	if (error != -ENOIOCTLCMD)
Packit 7b02f3
		goto done;
Packit 7b02f3
Packit 7b02f3
	error = atm_dev_ioctl(cmd, argp, compat);
Packit 7b02f3
Packit 7b02f3
done:
Packit 7b02f3
	return error;
Packit 7b02f3
}
Packit 7b02f3
Packit 7b02f3
int vcc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Packit 7b02f3
{
Packit 7b02f3
	return do_vcc_ioctl(sock, cmd, arg, 0);
Packit 7b02f3
}
Packit 7b02f3
Packit 7b02f3
#ifdef CONFIG_COMPAT
Packit 7b02f3
/*
Packit 7b02f3
 * FIXME:
Packit 7b02f3
 * The compat_ioctl handling is duplicated, using both these conversion
Packit 7b02f3
 * routines and the compat argument to the actual handlers. Both
Packit 7b02f3
 * versions are somewhat incomplete and should be merged, e.g. by
Packit 7b02f3
 * moving the ioctl number translation into the actual handlers and
Packit 7b02f3
 * killing the conversion code.
Packit 7b02f3
 *
Packit 7b02f3
 * -arnd, November 2009
Packit 7b02f3
 */
Packit 7b02f3
#define ATM_GETLINKRATE32 _IOW('a', ATMIOC_ITF+1, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_GETNAMES32    _IOW('a', ATMIOC_ITF+3, struct compat_atm_iobuf)
Packit 7b02f3
#define ATM_GETTYPE32     _IOW('a', ATMIOC_ITF+4, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_GETESI32	  _IOW('a', ATMIOC_ITF+5, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_GETADDR32	  _IOW('a', ATMIOC_ITF+6, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_RSTADDR32	  _IOW('a', ATMIOC_ITF+7, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_ADDADDR32	  _IOW('a', ATMIOC_ITF+8, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_DELADDR32	  _IOW('a', ATMIOC_ITF+9, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_GETCIRANGE32  _IOW('a', ATMIOC_ITF+10, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_SETCIRANGE32  _IOW('a', ATMIOC_ITF+11, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_SETESI32      _IOW('a', ATMIOC_ITF+12, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_SETESIF32     _IOW('a', ATMIOC_ITF+13, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_GETSTAT32     _IOW('a', ATMIOC_SARCOM+0, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_GETSTATZ32    _IOW('a', ATMIOC_SARCOM+1, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_GETLOOP32	  _IOW('a', ATMIOC_SARCOM+2, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_SETLOOP32	  _IOW('a', ATMIOC_SARCOM+3, struct compat_atmif_sioc)
Packit 7b02f3
#define ATM_QUERYLOOP32	  _IOW('a', ATMIOC_SARCOM+4, struct compat_atmif_sioc)
Packit 7b02f3
Packit 7b02f3
static struct {
Packit 7b02f3
	unsigned int cmd32;
Packit 7b02f3
	unsigned int cmd;
Packit 7b02f3
} atm_ioctl_map[] = {
Packit 7b02f3
	{ ATM_GETLINKRATE32, ATM_GETLINKRATE },
Packit 7b02f3
	{ ATM_GETNAMES32,    ATM_GETNAMES },
Packit 7b02f3
	{ ATM_GETTYPE32,     ATM_GETTYPE },
Packit 7b02f3
	{ ATM_GETESI32,	     ATM_GETESI },
Packit 7b02f3
	{ ATM_GETADDR32,     ATM_GETADDR },
Packit 7b02f3
	{ ATM_RSTADDR32,     ATM_RSTADDR },
Packit 7b02f3
	{ ATM_ADDADDR32,     ATM_ADDADDR },
Packit 7b02f3
	{ ATM_DELADDR32,     ATM_DELADDR },
Packit 7b02f3
	{ ATM_GETCIRANGE32,  ATM_GETCIRANGE },
Packit 7b02f3
	{ ATM_SETCIRANGE32,  ATM_SETCIRANGE },
Packit 7b02f3
	{ ATM_SETESI32,	     ATM_SETESI },
Packit 7b02f3
	{ ATM_SETESIF32,     ATM_SETESIF },
Packit 7b02f3
	{ ATM_GETSTAT32,     ATM_GETSTAT },
Packit 7b02f3
	{ ATM_GETSTATZ32,    ATM_GETSTATZ },
Packit 7b02f3
	{ ATM_GETLOOP32,     ATM_GETLOOP },
Packit 7b02f3
	{ ATM_SETLOOP32,     ATM_SETLOOP },
Packit 7b02f3
	{ ATM_QUERYLOOP32,   ATM_QUERYLOOP },
Packit 7b02f3
};
Packit 7b02f3
Packit 7b02f3
#define NR_ATM_IOCTL ARRAY_SIZE(atm_ioctl_map)
Packit 7b02f3
Packit 7b02f3
static int do_atm_iobuf(struct socket *sock, unsigned int cmd,
Packit 7b02f3
			unsigned long arg)
Packit 7b02f3
{
Packit 7b02f3
	struct atm_iobuf __user *iobuf;
Packit 7b02f3
	struct compat_atm_iobuf __user *iobuf32;
Packit 7b02f3
	u32 data;
Packit 7b02f3
	void __user *datap;
Packit 7b02f3
	int len, err;
Packit 7b02f3
Packit 7b02f3
	iobuf = compat_alloc_user_space(sizeof(*iobuf));
Packit 7b02f3
	iobuf32 = compat_ptr(arg);
Packit 7b02f3
Packit 7b02f3
	if (get_user(len, &iobuf32->length) ||
Packit 7b02f3
	    get_user(data, &iobuf32->buffer))
Packit 7b02f3
		return -EFAULT;
Packit 7b02f3
	datap = compat_ptr(data);
Packit 7b02f3
	if (put_user(len, &iobuf->length) ||
Packit 7b02f3
	    put_user(datap, &iobuf->buffer))
Packit 7b02f3
		return -EFAULT;
Packit 7b02f3
Packit 7b02f3
	err = do_vcc_ioctl(sock, cmd, (unsigned long) iobuf, 0);
Packit 7b02f3
Packit 7b02f3
	if (!err) {
Packit 7b02f3
		if (copy_in_user(&iobuf32->length, &iobuf->length,
Packit 7b02f3
				 sizeof(int)))
Packit 7b02f3
			err = -EFAULT;
Packit 7b02f3
	}
Packit 7b02f3
Packit 7b02f3
	return err;
Packit 7b02f3
}
Packit 7b02f3
Packit 7b02f3
static int do_atmif_sioc(struct socket *sock, unsigned int cmd,
Packit 7b02f3
			 unsigned long arg)
Packit 7b02f3
{
Packit 7b02f3
	struct atmif_sioc __user *sioc;
Packit 7b02f3
	struct compat_atmif_sioc __user *sioc32;
Packit 7b02f3
	u32 data;
Packit 7b02f3
	void __user *datap;
Packit 7b02f3
	int err;
Packit 7b02f3
Packit 7b02f3
	sioc = compat_alloc_user_space(sizeof(*sioc));
Packit 7b02f3
	sioc32 = compat_ptr(arg);
Packit 7b02f3
Packit 7b02f3
	if (copy_in_user(&sioc->number, &sioc32->number, 2 * sizeof(int)) ||
Packit 7b02f3
	    get_user(data, &sioc32->arg))
Packit 7b02f3
		return -EFAULT;
Packit 7b02f3
	datap = compat_ptr(data);
Packit 7b02f3
	if (put_user(datap, &sioc->arg))
Packit 7b02f3
		return -EFAULT;
Packit 7b02f3
Packit 7b02f3
	err = do_vcc_ioctl(sock, cmd, (unsigned long) sioc, 0);
Packit 7b02f3
Packit 7b02f3
	if (!err) {
Packit 7b02f3
		if (copy_in_user(&sioc32->length, &sioc->length,
Packit 7b02f3
				 sizeof(int)))
Packit 7b02f3
			err = -EFAULT;
Packit 7b02f3
	}
Packit 7b02f3
	return err;
Packit 7b02f3
}
Packit 7b02f3
Packit 7b02f3
static int do_atm_ioctl(struct socket *sock, unsigned int cmd32,
Packit 7b02f3
			unsigned long arg)
Packit 7b02f3
{
Packit 7b02f3
	int i;
Packit 7b02f3
	unsigned int cmd = 0;
Packit 7b02f3
Packit 7b02f3
	switch (cmd32) {
Packit 7b02f3
	case SONET_GETSTAT:
Packit 7b02f3
	case SONET_GETSTATZ:
Packit 7b02f3
	case SONET_GETDIAG:
Packit 7b02f3
	case SONET_SETDIAG:
Packit 7b02f3
	case SONET_CLRDIAG:
Packit 7b02f3
	case SONET_SETFRAMING:
Packit 7b02f3
	case SONET_GETFRAMING:
Packit 7b02f3
	case SONET_GETFRSENSE:
Packit 7b02f3
		return do_atmif_sioc(sock, cmd32, arg);
Packit 7b02f3
	}
Packit 7b02f3
Packit 7b02f3
	for (i = 0; i < NR_ATM_IOCTL; i++) {
Packit 7b02f3
		if (cmd32 == atm_ioctl_map[i].cmd32) {
Packit 7b02f3
			cmd = atm_ioctl_map[i].cmd;
Packit 7b02f3
			break;
Packit 7b02f3
		}
Packit 7b02f3
	}
Packit 7b02f3
	if (i == NR_ATM_IOCTL)
Packit 7b02f3
		return -EINVAL;
Packit 7b02f3
Packit 7b02f3
	switch (cmd) {
Packit 7b02f3
	case ATM_GETNAMES:
Packit 7b02f3
		return do_atm_iobuf(sock, cmd, arg);
Packit 7b02f3
Packit 7b02f3
	case ATM_GETLINKRATE:
Packit 7b02f3
	case ATM_GETTYPE:
Packit 7b02f3
	case ATM_GETESI:
Packit 7b02f3
	case ATM_GETADDR:
Packit 7b02f3
	case ATM_RSTADDR:
Packit 7b02f3
	case ATM_ADDADDR:
Packit 7b02f3
	case ATM_DELADDR:
Packit 7b02f3
	case ATM_GETCIRANGE:
Packit 7b02f3
	case ATM_SETCIRANGE:
Packit 7b02f3
	case ATM_SETESI:
Packit 7b02f3
	case ATM_SETESIF:
Packit 7b02f3
	case ATM_GETSTAT:
Packit 7b02f3
	case ATM_GETSTATZ:
Packit 7b02f3
	case ATM_GETLOOP:
Packit 7b02f3
	case ATM_SETLOOP:
Packit 7b02f3
	case ATM_QUERYLOOP:
Packit 7b02f3
		return do_atmif_sioc(sock, cmd, arg);
Packit 7b02f3
	}
Packit 7b02f3
Packit 7b02f3
	return -EINVAL;
Packit 7b02f3
}
Packit 7b02f3
Packit 7b02f3
int vcc_compat_ioctl(struct socket *sock, unsigned int cmd,
Packit 7b02f3
		     unsigned long arg)
Packit 7b02f3
{
Packit 7b02f3
	int ret;
Packit 7b02f3
Packit 7b02f3
	ret = do_vcc_ioctl(sock, cmd, arg, 1);
Packit 7b02f3
	if (ret != -ENOIOCTLCMD)
Packit 7b02f3
		return ret;
Packit 7b02f3
Packit 7b02f3
	return do_atm_ioctl(sock, cmd, arg);
Packit 7b02f3
}
Packit 7b02f3
#endif