resources.c 8.96 KB
Newer Older
Steven Cole's avatar
Steven Cole committed
1
/* net/atm/resources.c - Statically allocated resources */
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4

/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */

5 6 7 8 9
/* Fixes
 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 * 2002/01 - don't free the whole struct sock on sk->destruct time,
 * 	     use the default destruct function initialized by sock_init_data */

Linus Torvalds's avatar
Linus Torvalds committed
10 11 12 13 14

#include <linux/config.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/atmdev.h>
15
#include <linux/sonet.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19 20 21 22
#include <linux/kernel.h> /* for barrier */
#include <linux/module.h>
#include <linux/bitops.h>
#include <net/sock.h>	 /* for struct sock */

#include "common.h"
#include "resources.h"
23
#include "addr.h"
Linus Torvalds's avatar
Linus Torvalds committed
24 25


26
LIST_HEAD(atm_devs);
27
spinlock_t atm_dev_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
28

David S. Miller's avatar
David S. Miller committed
29
static struct atm_dev *__alloc_atm_dev(const char *type)
Linus Torvalds's avatar
Linus Torvalds committed
30 31 32
{
	struct atm_dev *dev;

David S. Miller's avatar
David S. Miller committed
33 34 35 36
	dev = kmalloc(sizeof(*dev), GFP_ATOMIC);
	if (!dev)
		return NULL;
	memset(dev, 0, sizeof(*dev));
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39
	dev->type = type;
	dev->signal = ATM_PHY_SIG_UNKNOWN;
	dev->link_rate = ATM_OC3_PCR;
40
	spin_lock_init(&dev->lock);
David S. Miller's avatar
David S. Miller committed
41

Linus Torvalds's avatar
Linus Torvalds committed
42 43 44
	return dev;
}

David S. Miller's avatar
David S. Miller committed
45
static void __free_atm_dev(struct atm_dev *dev)
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49
{
	kfree(dev);
}

50
static struct atm_dev *__atm_dev_lookup(int number)
Linus Torvalds's avatar
Linus Torvalds committed
51 52
{
	struct atm_dev *dev;
53
	struct list_head *p;
Linus Torvalds's avatar
Linus Torvalds committed
54

55 56
	list_for_each(p, &atm_devs) {
		dev = list_entry(p, struct atm_dev, dev_list);
57 58
		if ((dev->ops) && (dev->number == number)) {
			atm_dev_hold(dev);
David S. Miller's avatar
David S. Miller committed
59
			return dev;
60
		}
61
	}
Linus Torvalds's avatar
Linus Torvalds committed
62 63 64
	return NULL;
}

65
struct atm_dev *atm_dev_lookup(int number)
Linus Torvalds's avatar
Linus Torvalds committed
66 67 68
{
	struct atm_dev *dev;

David S. Miller's avatar
David S. Miller committed
69
	spin_lock(&atm_dev_lock);
70 71 72 73 74 75 76 77 78
	dev = __atm_dev_lookup(number);
	spin_unlock(&atm_dev_lock);
	return dev;
}

struct atm_dev *atm_dev_register(const char *type, const struct atmdev_ops *ops,
				 int number, unsigned long *flags)
{
	struct atm_dev *dev, *inuse;
David S. Miller's avatar
David S. Miller committed
79 80

	dev = __alloc_atm_dev(type);
Linus Torvalds's avatar
Linus Torvalds committed
81 82 83
	if (!dev) {
		printk(KERN_ERR "atm_dev_register: no space for dev %s\n",
		    type);
84
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
85
	}
86
	spin_lock(&atm_dev_lock);
Linus Torvalds's avatar
Linus Torvalds committed
87
	if (number != -1) {
88
		if ((inuse = __atm_dev_lookup(number))) {
89
			atm_dev_put(inuse);
90
			spin_unlock(&atm_dev_lock);
David S. Miller's avatar
David S. Miller committed
91
			__free_atm_dev(dev);
92
			return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
93 94
		}
		dev->number = number;
David S. Miller's avatar
David S. Miller committed
95
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
96
		dev->number = 0;
97
		while ((inuse = __atm_dev_lookup(dev->number))) {
98
			atm_dev_put(inuse);
David S. Miller's avatar
David S. Miller committed
99
			dev->number++;
100
		}
Linus Torvalds's avatar
Linus Torvalds committed
101
	}
102

Linus Torvalds's avatar
Linus Torvalds committed
103
	dev->ops = ops;
David S. Miller's avatar
David S. Miller committed
104 105 106 107 108
	if (flags)
		dev->flags = *flags;
	else
		memset(&dev->flags, 0, sizeof(dev->flags));
	memset(&dev->stats, 0, sizeof(dev->stats));
109 110 111
	atomic_set(&dev->refcnt, 1);
	list_add_tail(&dev->dev_list, &atm_devs);
	spin_unlock(&atm_dev_lock);
David S. Miller's avatar
David S. Miller committed
112

Linus Torvalds's avatar
Linus Torvalds committed
113
#ifdef CONFIG_PROC_FS
David S. Miller's avatar
David S. Miller committed
114
	if (ops->proc_read) {
Linus Torvalds's avatar
Linus Torvalds committed
115 116
		if (atm_proc_dev_register(dev) < 0) {
			printk(KERN_ERR "atm_dev_register: "
David S. Miller's avatar
David S. Miller committed
117 118
			       "atm_proc_dev_register failed for dev %s\n",
			       type);
119 120 121
			spin_lock(&atm_dev_lock);
			list_del(&dev->dev_list);
			spin_unlock(&atm_dev_lock);
David S. Miller's avatar
David S. Miller committed
122
			__free_atm_dev(dev);
123
			return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
124
		}
David S. Miller's avatar
David S. Miller committed
125
	}
Linus Torvalds's avatar
Linus Torvalds committed
126
#endif
David S. Miller's avatar
David S. Miller committed
127

Linus Torvalds's avatar
Linus Torvalds committed
128 129 130 131 132 133
	return dev;
}


void atm_dev_deregister(struct atm_dev *dev)
{
134 135
	unsigned long warning_time;

Linus Torvalds's avatar
Linus Torvalds committed
136
#ifdef CONFIG_PROC_FS
David S. Miller's avatar
David S. Miller committed
137 138
	if (dev->ops->proc_read)
		atm_proc_dev_deregister(dev);
Linus Torvalds's avatar
Linus Torvalds committed
139
#endif
David S. Miller's avatar
David S. Miller committed
140
	spin_lock(&atm_dev_lock);
141
	list_del(&dev->dev_list);
David S. Miller's avatar
David S. Miller committed
142
	spin_unlock(&atm_dev_lock);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

        warning_time = jiffies;
        while (atomic_read(&dev->refcnt) != 1) {
                current->state = TASK_INTERRUPTIBLE;
                schedule_timeout(HZ / 4);
                current->state = TASK_RUNNING;
                if ((jiffies - warning_time) > 10 * HZ) {
                        printk(KERN_EMERG "atm_dev_deregister: waiting for "
                               "dev %d to become free. Usage count = %d\n",
                               dev->number, atomic_read(&dev->refcnt));
                        warning_time = jiffies;
                }
        }

	__free_atm_dev(dev);
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160 161
}

void shutdown_atm_dev(struct atm_dev *dev)
{
162
	if (atomic_read(&dev->refcnt) > 1) {
David S. Miller's avatar
David S. Miller committed
163
		set_bit(ATM_DF_CLOSE, &dev->flags);
Linus Torvalds's avatar
Linus Torvalds committed
164 165
		return;
	}
David S. Miller's avatar
David S. Miller committed
166 167
	if (dev->ops->dev_close)
		dev->ops->dev_close(dev);
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170
	atm_dev_deregister(dev);
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

static void copy_aal_stats(struct k_atm_aal_stats *from,
    struct atm_aal_stats *to)
{
#define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
	__AAL_STAT_ITEMS
#undef __HANDLE_ITEM
}


static void subtract_aal_stats(struct k_atm_aal_stats *from,
    struct atm_aal_stats *to)
{
#define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
	__AAL_STAT_ITEMS
#undef __HANDLE_ITEM
}


static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats *arg, int zero)
{
	struct atm_dev_stats tmp;
	int error = 0;

	copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
	copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
	copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
	if (arg)
		error = copy_to_user(arg, &tmp, sizeof(tmp));
	if (zero && !error) {
		subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
		subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
		subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
	}
	return error ? -EFAULT : 0;
}


int atm_dev_ioctl(unsigned int cmd, unsigned long arg)
{
	void *buf;
	int error, len, number, size = 0;
	struct atm_dev *dev;
	struct list_head *p;
	int *tmp_buf, *tmp_p;

	switch (cmd) {
		case ATM_GETNAMES:
			if (get_user(buf, &((struct atm_iobuf *) arg)->buffer))
				return -EFAULT;
			if (get_user(len, &((struct atm_iobuf *) arg)->length))
				return -EFAULT;
			spin_lock(&atm_dev_lock);
			list_for_each(p, &atm_devs)
				size += sizeof(int);
			if (size > len) {
				spin_unlock(&atm_dev_lock);
				return -E2BIG;
			}
			tmp_buf = kmalloc(size, GFP_ATOMIC);
			if (!tmp_buf) {
				spin_unlock(&atm_dev_lock);
				return -ENOMEM;
			}
			tmp_p = tmp_buf;
			list_for_each(p, &atm_devs) {
				dev = list_entry(p, struct atm_dev, dev_list);
				*tmp_p++ = dev->number;
			}
			spin_unlock(&atm_dev_lock);
		        error = ((copy_to_user(buf, tmp_buf, size)) ||
					put_user(size, &((struct atm_iobuf *) arg)->length))
						? -EFAULT : 0;
			kfree(tmp_buf);
			return error;
		default:
			break;
	}

	if (get_user(buf, &((struct atmif_sioc *) arg)->arg))
		return -EFAULT;
	if (get_user(len, &((struct atmif_sioc *) arg)->length))
		return -EFAULT;
	if (get_user(number, &((struct atmif_sioc *) arg)->number))
		return -EFAULT;

	if (!(dev = atm_dev_lookup(number)))
		return -ENODEV;
	
	switch (cmd) {
		case ATM_GETTYPE:
			size = strlen(dev->type) + 1;
			if (copy_to_user(buf, dev->type, size)) {
				error = -EFAULT;
				goto done;
			}
			break;
		case ATM_GETESI:
			size = ESI_LEN;
			if (copy_to_user(buf, dev->esi, size)) {
				error = -EFAULT;
				goto done;
			}
			break;
		case ATM_SETESI:
			{
				int i;

				for (i = 0; i < ESI_LEN; i++)
					if (dev->esi[i]) {
						error = -EEXIST;
						goto done;
					}
			}
			/* fall through */
		case ATM_SETESIF:
			{
				unsigned char esi[ESI_LEN];

				if (!capable(CAP_NET_ADMIN)) {
					error = -EPERM;
					goto done;
				}
				if (copy_from_user(esi, buf, ESI_LEN)) {
					error = -EFAULT;
					goto done;
				}
				memcpy(dev->esi, esi, ESI_LEN);
				error =  ESI_LEN;
				goto done;
			}
		case ATM_GETSTATZ:
			if (!capable(CAP_NET_ADMIN)) {
				error = -EPERM;
				goto done;
			}
			/* fall through */
		case ATM_GETSTAT:
			size = sizeof(struct atm_dev_stats);
			error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
			if (error)
				goto done;
			break;
		case ATM_GETCIRANGE:
			size = sizeof(struct atm_cirange);
			if (copy_to_user(buf, &dev->ci_range, size)) {
				error = -EFAULT;
				goto done;
			}
			break;
		case ATM_GETLINKRATE:
			size = sizeof(int);
			if (copy_to_user(buf, &dev->link_rate, size)) {
				error = -EFAULT;
				goto done;
			}
			break;
		case ATM_RSTADDR:
			if (!capable(CAP_NET_ADMIN)) {
				error = -EPERM;
				goto done;
			}
			atm_reset_addr(dev);
			break;
		case ATM_ADDADDR:
		case ATM_DELADDR:
			if (!capable(CAP_NET_ADMIN)) {
				error = -EPERM;
				goto done;
			}
			{
				struct sockaddr_atmsvc addr;

				if (copy_from_user(&addr, buf, sizeof(addr))) {
					error = -EFAULT;
					goto done;
				}
				if (cmd == ATM_ADDADDR)
					error = atm_add_addr(dev, &addr);
				else
					error = atm_del_addr(dev, &addr);
				goto done;
			}
		case ATM_GETADDR:
			error = atm_get_addr(dev, buf, len);
			if (error < 0)
				goto done;
			size = error;
			/* may return 0, but later on size == 0 means "don't
			   write the length" */
			error = put_user(size, &((struct atmif_sioc *) arg)->length)
				? -EFAULT : 0;
			goto done;
		case ATM_SETLOOP:
			if (__ATM_LM_XTRMT((int) (long) buf) &&
			    __ATM_LM_XTLOC((int) (long) buf) >
			    __ATM_LM_XTRMT((int) (long) buf)) {
				error = -EINVAL;
				goto done;
			}
			/* fall through */
		case ATM_SETCIRANGE:
		case SONET_GETSTATZ:
		case SONET_SETDIAG:
		case SONET_CLRDIAG:
		case SONET_SETFRAMING:
			if (!capable(CAP_NET_ADMIN)) {
				error = -EPERM;
				goto done;
			}
			/* fall through */
		default:
			if (!dev->ops->ioctl) {
				error = -EINVAL;
				goto done;
			}
			size = dev->ops->ioctl(dev, cmd, buf);
			if (size < 0) {
				error = (size == -ENOIOCTLCMD ? -EINVAL : size);
				goto done;
			}
	}
	
	if (size)
		error = put_user(size, &((struct atmif_sioc *) arg)->length)
			? -EFAULT : 0;
	else
		error = 0;
done:
400
	atm_dev_put(dev);
401 402 403 404
	return error;
}


Linus Torvalds's avatar
Linus Torvalds committed
405 406
EXPORT_SYMBOL(atm_dev_register);
EXPORT_SYMBOL(atm_dev_deregister);
407
EXPORT_SYMBOL(atm_dev_lookup);
Linus Torvalds's avatar
Linus Torvalds committed
408
EXPORT_SYMBOL(shutdown_atm_dev);