common.c 21.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/* net/atm/common.c - ATM sockets (common part for PVC and SVC) */

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

5
#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14

#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/net.h>		/* struct socket, struct proto_ops */
#include <linux/atm.h>		/* ATM stuff */
#include <linux/atmdev.h>
#include <linux/socket.h>	/* SOL_SOCKET */
#include <linux/errno.h>	/* error codes */
#include <linux/capability.h>
J
Jesper Juhl 已提交
15
#include <linux/mm.h>
L
Linus Torvalds 已提交
16 17 18 19 20
#include <linux/sched.h>
#include <linux/time.h>		/* struct timeval */
#include <linux/skbuff.h>
#include <linux/bitops.h>
#include <linux/init.h>
21
#include <linux/slab.h>
L
Linus Torvalds 已提交
22
#include <net/sock.h>		/* struct sock */
23 24
#include <linux/uaccess.h>
#include <linux/poll.h>
L
Linus Torvalds 已提交
25

A
Arun Sharma 已提交
26
#include <linux/atomic.h>
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34

#include "resources.h"		/* atm_find_dev */
#include "common.h"		/* prototypes */
#include "protocols.h"		/* atm_init_<transport> */
#include "addr.h"		/* address registry */
#include "signaling.h"		/* for WAITING and sigd_attach */

struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
35 36
EXPORT_SYMBOL(vcc_hash);

L
Linus Torvalds 已提交
37
DEFINE_RWLOCK(vcc_sklist_lock);
38
EXPORT_SYMBOL(vcc_sklist_lock);
L
Linus Torvalds 已提交
39

40 41
static ATOMIC_NOTIFIER_HEAD(atm_dev_notify_chain);

L
Linus Torvalds 已提交
42 43 44
static void __vcc_insert_socket(struct sock *sk)
{
	struct atm_vcc *vcc = atm_sk(sk);
45
	struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
46
	sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55
	sk_add_node(sk, head);
}

void vcc_insert_socket(struct sock *sk)
{
	write_lock_irq(&vcc_sklist_lock);
	__vcc_insert_socket(sk);
	write_unlock_irq(&vcc_sklist_lock);
}
56
EXPORT_SYMBOL(vcc_insert_socket);
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64

static void vcc_remove_socket(struct sock *sk)
{
	write_lock_irq(&vcc_sklist_lock);
	sk_del_node_init(sk);
	write_unlock_irq(&vcc_sklist_lock);
}

65
static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size)
L
Linus Torvalds 已提交
66 67 68 69
{
	struct sk_buff *skb;
	struct sock *sk = sk_atm(vcc);

70
	if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
71
		pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
72
			 sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
L
Linus Torvalds 已提交
73 74
		return NULL;
	}
75 76
	while (!(skb = alloc_skb(size, GFP_KERNEL)))
		schedule();
77
	pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
L
Linus Torvalds 已提交
78 79 80 81 82 83 84
	atomic_add(skb->truesize, &sk->sk_wmem_alloc);
	return skb;
}

static void vcc_sock_destruct(struct sock *sk)
{
	if (atomic_read(&sk->sk_rmem_alloc))
85 86
		printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
		       __func__, atomic_read(&sk->sk_rmem_alloc));
L
Linus Torvalds 已提交
87 88

	if (atomic_read(&sk->sk_wmem_alloc))
89 90
		printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
		       __func__, atomic_read(&sk->sk_wmem_alloc));
L
Linus Torvalds 已提交
91 92 93 94
}

static void vcc_def_wakeup(struct sock *sk)
{
95 96 97 98
	struct socket_wq *wq;

	rcu_read_lock();
	wq = rcu_dereference(sk->sk_wq);
H
Herbert Xu 已提交
99
	if (skwq_has_sleeper(wq))
100 101
		wake_up(&wq->wait);
	rcu_read_unlock();
L
Linus Torvalds 已提交
102 103 104 105 106 107 108
}

static inline int vcc_writable(struct sock *sk)
{
	struct atm_vcc *vcc = atm_sk(sk);

	return (vcc->qos.txtp.max_sdu +
109
		atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
L
Linus Torvalds 已提交
110 111 112
}

static void vcc_write_space(struct sock *sk)
113
{
114 115 116
	struct socket_wq *wq;

	rcu_read_lock();
L
Linus Torvalds 已提交
117 118

	if (vcc_writable(sk)) {
119
		wq = rcu_dereference(sk->sk_wq);
H
Herbert Xu 已提交
120
		if (skwq_has_sleeper(wq))
121
			wake_up_interruptible(&wq->wait);
L
Linus Torvalds 已提交
122

123
		sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
L
Linus Torvalds 已提交
124 125
	}

126
	rcu_read_unlock();
L
Linus Torvalds 已提交
127 128
}

129 130 131 132 133 134 135 136
static void vcc_release_cb(struct sock *sk)
{
	struct atm_vcc *vcc = atm_sk(sk);

	if (vcc->release_cb)
		vcc->release_cb(vcc);
}

L
Linus Torvalds 已提交
137 138 139 140
static struct proto vcc_proto = {
	.name	  = "VCC",
	.owner	  = THIS_MODULE,
	.obj_size = sizeof(struct atm_vcc),
141
	.release_cb = vcc_release_cb,
L
Linus Torvalds 已提交
142
};
143

144
int vcc_create(struct net *net, struct socket *sock, int protocol, int family, int kern)
L
Linus Torvalds 已提交
145 146 147 148 149 150 151
{
	struct sock *sk;
	struct atm_vcc *vcc;

	sock->sk = NULL;
	if (sock->type == SOCK_STREAM)
		return -EINVAL;
152
	sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto, kern);
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160
	if (!sk)
		return -ENOMEM;
	sock_init_data(sock, sk);
	sk->sk_state_change = vcc_def_wakeup;
	sk->sk_write_space = vcc_write_space;

	vcc = atm_sk(sk);
	vcc->dev = NULL;
161 162
	memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
	memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
L
Linus Torvalds 已提交
163
	vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
164
	atomic_set(&sk->sk_wmem_alloc, 1);
L
Linus Torvalds 已提交
165 166 167
	atomic_set(&sk->sk_rmem_alloc, 0);
	vcc->push = NULL;
	vcc->pop = NULL;
168
	vcc->owner = NULL;
L
Linus Torvalds 已提交
169
	vcc->push_oam = NULL;
170
	vcc->release_cb = NULL;
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
	vcc->atm_options = vcc->aal_options = 0;
	sk->sk_destruct = vcc_sock_destruct;
	return 0;
}

static void vcc_destroy_socket(struct sock *sk)
{
	struct atm_vcc *vcc = atm_sk(sk);
	struct sk_buff *skb;

	set_bit(ATM_VF_CLOSE, &vcc->flags);
	clear_bit(ATM_VF_READY, &vcc->flags);
	if (vcc->dev) {
		if (vcc->dev->ops->close)
			vcc->dev->ops->close(vcc);
		if (vcc->push)
			vcc->push(vcc, NULL); /* atmarpd has no push */
189
		module_put(vcc->owner);
L
Linus Torvalds 已提交
190 191

		while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
192
			atm_return(vcc, skb->truesize);
L
Linus Torvalds 已提交
193 194 195 196 197 198
			kfree_skb(skb);
		}

		module_put(vcc->dev->ops->owner);
		atm_dev_put(vcc->dev);
	}
199 200

	vcc_remove_socket(sk);
L
Linus Torvalds 已提交
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
}

int vcc_release(struct socket *sock)
{
	struct sock *sk = sock->sk;

	if (sk) {
		lock_sock(sk);
		vcc_destroy_socket(sock->sk);
		release_sock(sk);
		sock_put(sk);
	}

	return 0;
}

void vcc_release_async(struct atm_vcc *vcc, int reply)
{
	struct sock *sk = sk_atm(vcc);

	set_bit(ATM_VF_CLOSE, &vcc->flags);
	sk->sk_shutdown |= RCV_SHUTDOWN;
	sk->sk_err = -reply;
	clear_bit(ATM_VF_WAITING, &vcc->flags);
	sk->sk_state_change(sk);
}
EXPORT_SYMBOL(vcc_release_async);

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
void vcc_process_recv_queue(struct atm_vcc *vcc)
{
	struct sk_buff_head queue, *rq;
	struct sk_buff *skb, *tmp;
	unsigned long flags;

	__skb_queue_head_init(&queue);
	rq = &sk_atm(vcc)->sk_receive_queue;

	spin_lock_irqsave(&rq->lock, flags);
	skb_queue_splice_init(rq, &queue);
	spin_unlock_irqrestore(&rq->lock, flags);

	skb_queue_walk_safe(&queue, skb, tmp) {
		__skb_unlink(skb, &queue);
		vcc->push(vcc, skb);
	}
}
EXPORT_SYMBOL(vcc_process_recv_queue);

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
void atm_dev_signal_change(struct atm_dev *dev, char signal)
{
	pr_debug("%s signal=%d dev=%p number=%d dev->signal=%d\n",
		__func__, signal, dev, dev->number, dev->signal);

	/* atm driver sending invalid signal */
	WARN_ON(signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND);

	if (dev->signal == signal)
		return; /* no change */

	dev->signal = signal;

	atomic_notifier_call_chain(&atm_dev_notify_chain, signal, dev);
}
EXPORT_SYMBOL(atm_dev_signal_change);
L
Linus Torvalds 已提交
265

266 267 268 269 270 271 272
void atm_dev_release_vccs(struct atm_dev *dev)
{
	int i;

	write_lock_irq(&vcc_sklist_lock);
	for (i = 0; i < VCC_HTABLE_SIZE; i++) {
		struct hlist_head *head = &vcc_hash[i];
273
		struct hlist_node *tmp;
274 275 276
		struct sock *s;
		struct atm_vcc *vcc;

277
		sk_for_each_safe(s, tmp, head) {
278 279 280 281 282 283 284 285 286
			vcc = atm_sk(s);
			if (vcc->dev == dev) {
				vcc_release_async(vcc, -EPIPE);
				sk_del_node_init(s);
			}
		}
	}
	write_unlock_irq(&vcc_sklist_lock);
}
287
EXPORT_SYMBOL(atm_dev_release_vccs);
288

289
static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
L
Linus Torvalds 已提交
290 291 292
{
	int max_sdu;

293 294
	if (!tp->traffic_class)
		return 0;
L
Linus Torvalds 已提交
295
	switch (aal) {
296 297 298 299 300 301 302
	case ATM_AAL0:
		max_sdu = ATM_CELL_SIZE-1;
		break;
	case ATM_AAL34:
		max_sdu = ATM_MAX_AAL34_PDU;
		break;
	default:
J
Joe Perches 已提交
303
		pr_warn("AAL problems ... (%d)\n", aal);
304 305 306
		/* fall through */
	case ATM_AAL5:
		max_sdu = ATM_MAX_AAL5_PDU;
L
Linus Torvalds 已提交
307
	}
308 309 310 311 312 313
	if (!tp->max_sdu)
		tp->max_sdu = max_sdu;
	else if (tp->max_sdu > max_sdu)
		return -EINVAL;
	if (!tp->max_cdv)
		tp->max_cdv = ATM_MAX_CDV;
L
Linus Torvalds 已提交
314 315 316
	return 0;
}

317
static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
L
Linus Torvalds 已提交
318
{
319
	struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
L
Linus Torvalds 已提交
320 321 322
	struct sock *s;
	struct atm_vcc *walk;

323
	sk_for_each(s, head) {
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
		walk = atm_sk(s);
		if (walk->dev != vcc->dev)
			continue;
		if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
		    walk->vci == vci && ((walk->qos.txtp.traffic_class !=
		    ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
		    (walk->qos.rxtp.traffic_class != ATM_NONE &&
		    vcc->qos.rxtp.traffic_class != ATM_NONE)))
			return -EADDRINUSE;
	}

	/* allow VCCs with same VPI/VCI iff they don't collide on
	   TX/RX (but we may refuse such sharing for other reasons,
	   e.g. if protocol requires to have both channels) */

	return 0;
}

342
static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
L
Linus Torvalds 已提交
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
{
	static short p;        /* poor man's per-device cache */
	static int c;
	short old_p;
	int old_c;
	int err;

	if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
		err = check_ci(vcc, *vpi, *vci);
		return err;
	}
	/* last scan may have left values out of bounds for current device */
	if (*vpi != ATM_VPI_ANY)
		p = *vpi;
	else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
		p = 0;
	if (*vci != ATM_VCI_ANY)
		c = *vci;
	else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
			c = ATM_NOT_RSV_VCI;
	old_p = p;
	old_c = c;
	do {
		if (!check_ci(vcc, p, c)) {
			*vpi = p;
			*vci = c;
			return 0;
		}
		if (*vci == ATM_VCI_ANY) {
			c++;
			if (c >= 1 << vcc->dev->ci_range.vci_bits)
				c = ATM_NOT_RSV_VCI;
		}
		if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
		    *vpi == ATM_VPI_ANY) {
			p++;
379 380
			if (p >= 1 << vcc->dev->ci_range.vpi_bits)
				p = 0;
L
Linus Torvalds 已提交
381
		}
382
	} while (old_p != p || old_c != c);
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	return -EADDRINUSE;
}

static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
			 int vci)
{
	struct sock *sk = sk_atm(vcc);
	int error;

	if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
	    vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
	    vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
		return -EINVAL;
	if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
		return -EPERM;
398
	error = -ENODEV;
L
Linus Torvalds 已提交
399
	if (!try_module_get(dev->ops->owner))
400
		return error;
L
Linus Torvalds 已提交
401 402
	vcc->dev = dev;
	write_lock_irq(&vcc_sklist_lock);
403
	if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
404
	    (error = find_ci(vcc, &vpi, &vci))) {
L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412
		write_unlock_irq(&vcc_sklist_lock);
		goto fail_module_put;
	}
	vcc->vpi = vpi;
	vcc->vci = vci;
	__vcc_insert_socket(sk);
	write_unlock_irq(&vcc_sklist_lock);
	switch (vcc->qos.aal) {
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	case ATM_AAL0:
		error = atm_init_aal0(vcc);
		vcc->stats = &dev->stats.aal0;
		break;
	case ATM_AAL34:
		error = atm_init_aal34(vcc);
		vcc->stats = &dev->stats.aal34;
		break;
	case ATM_NO_AAL:
		/* ATM_AAL5 is also used in the "0 for default" case */
		vcc->qos.aal = ATM_AAL5;
		/* fall through */
	case ATM_AAL5:
		error = atm_init_aal5(vcc);
		vcc->stats = &dev->stats.aal5;
		break;
	default:
		error = -EPROTOTYPE;
L
Linus Torvalds 已提交
431
	}
432 433 434 435
	if (!error)
		error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
	if (!error)
		error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
L
Linus Torvalds 已提交
436 437
	if (error)
		goto fail;
438 439 440 441 442 443 444 445 446 447 448
	pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
	pr_debug("  TX: %d, PCR %d..%d, SDU %d\n",
		 vcc->qos.txtp.traffic_class,
		 vcc->qos.txtp.min_pcr,
		 vcc->qos.txtp.max_pcr,
		 vcc->qos.txtp.max_sdu);
	pr_debug("  RX: %d, PCR %d..%d, SDU %d\n",
		 vcc->qos.rxtp.traffic_class,
		 vcc->qos.rxtp.min_pcr,
		 vcc->qos.rxtp.max_pcr,
		 vcc->qos.rxtp.max_sdu);
L
Linus Torvalds 已提交
449 450

	if (dev->ops->open) {
451 452
		error = dev->ops->open(vcc);
		if (error)
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
			goto fail;
	}
	return 0;

fail:
	vcc_remove_socket(sk);
fail_module_put:
	module_put(dev->ops->owner);
	/* ensure we get dev module ref count correct */
	vcc->dev = NULL;
	return error;
}

int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
{
	struct atm_dev *dev;
	struct atm_vcc *vcc = ATM_SD(sock);
	int error;

472
	pr_debug("(vpi %d, vci %d)\n", vpi, vci);
L
Linus Torvalds 已提交
473 474 475 476 477 478 479 480
	if (sock->state == SS_CONNECTED)
		return -EISCONN;
	if (sock->state != SS_UNCONNECTED)
		return -EINVAL;
	if (!(vpi || vci))
		return -EINVAL;

	if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
481
		clear_bit(ATM_VF_PARTIAL, &vcc->flags);
L
Linus Torvalds 已提交
482
	else
483
		if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
L
Linus Torvalds 已提交
484
			return -EINVAL;
485 486 487 488 489
	pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
		 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
		 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
		 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
		 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
490
		 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
491 492 493
		 vcc->qos.aal == ATM_AAL5 ? "" :
		 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
		 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
L
Linus Torvalds 已提交
494 495 496 497 498
	if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
		return -EBADFD;
	if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
	    vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
		return -EINVAL;
499
	if (likely(itf != ATM_ITF_ANY)) {
500 501
		dev = try_then_request_module(atm_dev_lookup(itf),
					      "atm-device-%d", itf);
L
Linus Torvalds 已提交
502 503
	} else {
		dev = NULL;
I
Ingo Molnar 已提交
504
		mutex_lock(&atm_dev_mutex);
505
		if (!list_empty(&atm_devs)) {
506 507
			dev = list_entry(atm_devs.next,
					 struct atm_dev, dev_list);
L
Linus Torvalds 已提交
508 509
			atm_dev_hold(dev);
		}
I
Ingo Molnar 已提交
510
		mutex_unlock(&atm_dev_mutex);
511 512 513 514 515 516 517
	}
	if (!dev)
		return -ENODEV;
	error = __vcc_connect(vcc, dev, vpi, vci);
	if (error) {
		atm_dev_put(dev);
		return error;
L
Linus Torvalds 已提交
518 519
	}
	if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
520 521
		set_bit(ATM_VF_PARTIAL, &vcc->flags);
	if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
L
Linus Torvalds 已提交
522 523 524 525
		sock->state = SS_CONNECTED;
	return 0;
}

526 527
int vcc_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
		int flags)
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535
{
	struct sock *sk = sock->sk;
	struct atm_vcc *vcc;
	struct sk_buff *skb;
	int copied, error = -EINVAL;

	if (sock->state != SS_CONNECTED)
		return -ENOTCONN;
536 537 538

	/* only handle MSG_DONTWAIT and MSG_PEEK */
	if (flags & ~(MSG_DONTWAIT | MSG_PEEK))
L
Linus Torvalds 已提交
539
		return -EOPNOTSUPP;
540

L
Linus Torvalds 已提交
541
	vcc = ATM_SD(sock);
542 543
	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
L
Linus Torvalds 已提交
544 545 546 547 548 549 550
	    !test_bit(ATM_VF_READY, &vcc->flags))
		return 0;

	skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
	if (!skb)
		return error;

551
	copied = skb->len;
L
Linus Torvalds 已提交
552
	if (copied > size) {
553
		copied = size;
L
Linus Torvalds 已提交
554 555 556
		msg->msg_flags |= MSG_TRUNC;
	}

557
	error = skb_copy_datagram_msg(skb, 0, msg, copied);
558 559
	if (error)
		return error;
560
	sock_recv_ts_and_drops(msg, sk, skb);
561 562 563 564 565 566 567

	if (!(flags & MSG_PEEK)) {
		pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc),
			 skb->truesize);
		atm_return(vcc, skb->truesize);
	}

568 569
	skb_free_datagram(sk, skb);
	return copied;
L
Linus Torvalds 已提交
570 571
}

572
int vcc_sendmsg(struct socket *sock, struct msghdr *m, size_t size)
L
Linus Torvalds 已提交
573 574 575 576 577
{
	struct sock *sk = sock->sk;
	DEFINE_WAIT(wait);
	struct atm_vcc *vcc;
	struct sk_buff *skb;
578
	int eff, error;
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

	lock_sock(sk);
	if (sock->state != SS_CONNECTED) {
		error = -ENOTCONN;
		goto out;
	}
	if (m->msg_name) {
		error = -EISCONN;
		goto out;
	}
	vcc = ATM_SD(sock);
	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
	    !test_bit(ATM_VF_READY, &vcc->flags)) {
		error = -EPIPE;
		send_sig(SIGPIPE, current, 0);
		goto out;
	}
	if (!size) {
		error = 0;
		goto out;
	}
601
	if (size > vcc->qos.txtp.max_sdu) {
L
Linus Torvalds 已提交
602 603 604
		error = -EMSGSIZE;
		goto out;
	}
J
Jesper Juhl 已提交
605

L
Linus Torvalds 已提交
606
	eff = (size+3) & ~3; /* align to word boundary */
E
Eric Dumazet 已提交
607
	prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
608
	error = 0;
609
	while (!(skb = alloc_tx(vcc, eff))) {
L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617 618
		if (m->msg_flags & MSG_DONTWAIT) {
			error = -EAGAIN;
			break;
		}
		schedule();
		if (signal_pending(current)) {
			error = -ERESTARTSYS;
			break;
		}
619 620 621
		if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
		    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
		    !test_bit(ATM_VF_READY, &vcc->flags)) {
L
Linus Torvalds 已提交
622 623 624 625
			error = -EPIPE;
			send_sig(SIGPIPE, current, 0);
			break;
		}
E
Eric Dumazet 已提交
626
		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
627
	}
E
Eric Dumazet 已提交
628
	finish_wait(sk_sleep(sk), &wait);
L
Linus Torvalds 已提交
629 630 631 632
	if (error)
		goto out;
	skb->dev = NULL; /* for paths shared with net_device interfaces */
	ATM_SKB(skb)->atm_options = vcc->atm_options;
A
Al Viro 已提交
633
	if (copy_from_iter(skb_put(skb, size), size, &m->msg_iter) != size) {
L
Linus Torvalds 已提交
634 635 636 637
		kfree_skb(skb);
		error = -EFAULT;
		goto out;
	}
638 639 640
	if (eff != size)
		memset(skb->data + size, 0, eff-size);
	error = vcc->dev->ops->send(vcc, skb);
L
Linus Torvalds 已提交
641 642 643 644 645 646 647 648 649 650 651 652
	error = error ? error : size;
out:
	release_sock(sk);
	return error;
}

unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
{
	struct sock *sk = sock->sk;
	struct atm_vcc *vcc;
	unsigned int mask;

E
Eric Dumazet 已提交
653
	sock_poll_wait(file, sk_sleep(sk), wait);
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
	mask = 0;

	vcc = ATM_SD(sock);

	/* exceptional events */
	if (sk->sk_err)
		mask = POLLERR;

	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
	    test_bit(ATM_VF_CLOSE, &vcc->flags))
		mask |= POLLHUP;

	/* readable? */
	if (!skb_queue_empty(&sk->sk_receive_queue))
		mask |= POLLIN | POLLRDNORM;

	/* writable? */
	if (sock->state == SS_CONNECTING &&
	    test_bit(ATM_VF_WAITING, &vcc->flags))
		return mask;

	if (vcc->qos.txtp.traffic_class != ATM_NONE &&
	    vcc_writable(sk))
		mask |= POLLOUT | POLLWRNORM | POLLWRBAND;

	return mask;
}

682
static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
L
Linus Torvalds 已提交
683 684 685 686 687 688 689 690 691 692 693
{
	int error;

	/*
	 * Don't let the QoS change the already connected AAL type nor the
	 * traffic class.
	 */
	if (qos->aal != vcc->qos.aal ||
	    qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
	    qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
		return -EINVAL;
694 695 696 697 698 699 700
	error = adjust_tp(&qos->txtp, qos->aal);
	if (!error)
		error = adjust_tp(&qos->rxtp, qos->aal);
	if (error)
		return error;
	if (!vcc->dev->ops->change_qos)
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
701
	if (sk_atm(vcc)->sk_family == AF_ATMPVC)
702 703
		return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
	return svc_change_qos(vcc, qos);
L
Linus Torvalds 已提交
704 705
}

706
static int check_tp(const struct atm_trafprm *tp)
L
Linus Torvalds 已提交
707 708
{
	/* @@@ Should be merged with adjust_tp */
709 710
	if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
		return 0;
L
Linus Torvalds 已提交
711
	if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
712 713 714 715
	    !tp->max_pcr)
		return -EINVAL;
	if (tp->min_pcr == ATM_MAX_PCR)
		return -EINVAL;
L
Linus Torvalds 已提交
716
	if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
717 718
	    tp->min_pcr > tp->max_pcr)
		return -EINVAL;
L
Linus Torvalds 已提交
719 720 721 722 723 724 725
	/*
	 * We allow pcr to be outside [min_pcr,max_pcr], because later
	 * adjustment may still push it in the valid range.
	 */
	return 0;
}

726
static int check_qos(const struct atm_qos *qos)
L
Linus Torvalds 已提交
727 728 729 730
{
	int error;

	if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
731
		return -EINVAL;
L
Linus Torvalds 已提交
732 733 734
	if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
	    qos->txtp.traffic_class && qos->rxtp.traffic_class &&
	    qos->txtp.traffic_class != ATM_ANYCLASS &&
735 736
	    qos->rxtp.traffic_class != ATM_ANYCLASS)
		return -EINVAL;
L
Linus Torvalds 已提交
737
	error = check_tp(&qos->txtp);
738 739
	if (error)
		return error;
L
Linus Torvalds 已提交
740 741 742 743
	return check_tp(&qos->rxtp);
}

int vcc_setsockopt(struct socket *sock, int level, int optname,
744
		   char __user *optval, unsigned int optlen)
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753 754
{
	struct atm_vcc *vcc;
	unsigned long value;
	int error;

	if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
		return -EINVAL;

	vcc = ATM_SD(sock);
	switch (optname) {
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	case SO_ATMQOS:
	{
		struct atm_qos qos;

		if (copy_from_user(&qos, optval, sizeof(qos)))
			return -EFAULT;
		error = check_qos(&qos);
		if (error)
			return error;
		if (sock->state == SS_CONNECTED)
			return atm_change_qos(vcc, &qos);
		if (sock->state != SS_UNCONNECTED)
			return -EBADFD;
		vcc->qos = qos;
		set_bit(ATM_VF_HASQOS, &vcc->flags);
		return 0;
L
Linus Torvalds 已提交
771
	}
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
	case SO_SETCLP:
		if (get_user(value, (unsigned long __user *)optval))
			return -EFAULT;
		if (value)
			vcc->atm_options |= ATM_ATMOPT_CLP;
		else
			vcc->atm_options &= ~ATM_ATMOPT_CLP;
		return 0;
	default:
		if (level == SOL_SOCKET)
			return -EINVAL;
		break;
	}
	if (!vcc->dev || !vcc->dev->ops->setsockopt)
		return -EINVAL;
	return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
L
Linus Torvalds 已提交
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
}

int vcc_getsockopt(struct socket *sock, int level, int optname,
		   char __user *optval, int __user *optlen)
{
	struct atm_vcc *vcc;
	int len;

	if (get_user(len, optlen))
		return -EFAULT;
	if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
		return -EINVAL;

	vcc = ATM_SD(sock);
	switch (optname) {
803 804 805 806 807 808 809 810 811 812 813 814 815 816
	case SO_ATMQOS:
		if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
			return -EINVAL;
		return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
			? -EFAULT : 0;
	case SO_SETCLP:
		return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
				(unsigned long __user *)optval) ? -EFAULT : 0;
	case SO_ATMPVC:
	{
		struct sockaddr_atmpvc pvc;

		if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
			return -ENOTCONN;
817
		memset(&pvc, 0, sizeof(pvc));
818 819 820 821 822 823 824 825 826
		pvc.sap_family = AF_ATMPVC;
		pvc.sap_addr.itf = vcc->dev->number;
		pvc.sap_addr.vpi = vcc->vpi;
		pvc.sap_addr.vci = vcc->vci;
		return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
	}
	default:
		if (level == SOL_SOCKET)
			return -EINVAL;
827
		break;
L
Linus Torvalds 已提交
828
	}
829 830
	if (!vcc->dev || !vcc->dev->ops->getsockopt)
		return -EINVAL;
L
Linus Torvalds 已提交
831 832 833
	return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
}

834 835 836 837 838 839 840 841 842 843 844 845
int register_atmdevice_notifier(struct notifier_block *nb)
{
	return atomic_notifier_chain_register(&atm_dev_notify_chain, nb);
}
EXPORT_SYMBOL_GPL(register_atmdevice_notifier);

void unregister_atmdevice_notifier(struct notifier_block *nb)
{
	atomic_notifier_chain_unregister(&atm_dev_notify_chain, nb);
}
EXPORT_SYMBOL_GPL(unregister_atmdevice_notifier);

L
Linus Torvalds 已提交
846 847 848 849
static int __init atm_init(void)
{
	int error;

850 851
	error = proto_register(&vcc_proto, 0);
	if (error < 0)
L
Linus Torvalds 已提交
852
		goto out;
853 854
	error = atmpvc_init();
	if (error < 0) {
855
		pr_err("atmpvc_init() failed with %d\n", error);
L
Linus Torvalds 已提交
856 857
		goto out_unregister_vcc_proto;
	}
858 859
	error = atmsvc_init();
	if (error < 0) {
860
		pr_err("atmsvc_init() failed with %d\n", error);
L
Linus Torvalds 已提交
861 862
		goto out_atmpvc_exit;
	}
863 864
	error = atm_proc_init();
	if (error < 0) {
865
		pr_err("atm_proc_init() failed with %d\n", error);
L
Linus Torvalds 已提交
866 867
		goto out_atmsvc_exit;
	}
868 869
	error = atm_sysfs_init();
	if (error < 0) {
870
		pr_err("atm_sysfs_init() failed with %d\n", error);
871 872
		goto out_atmproc_exit;
	}
L
Linus Torvalds 已提交
873 874
out:
	return error;
875 876
out_atmproc_exit:
	atm_proc_exit();
L
Linus Torvalds 已提交
877 878 879 880 881 882 883 884 885 886 887 888
out_atmsvc_exit:
	atmsvc_exit();
out_atmpvc_exit:
	atmsvc_exit();
out_unregister_vcc_proto:
	proto_unregister(&vcc_proto);
	goto out;
}

static void __exit atm_exit(void)
{
	atm_proc_exit();
889
	atm_sysfs_exit();
L
Linus Torvalds 已提交
890 891 892 893 894
	atmsvc_exit();
	atmpvc_exit();
	proto_unregister(&vcc_proto);
}

895 896
subsys_initcall(atm_init);

L
Linus Torvalds 已提交
897 898 899 900 901
module_exit(atm_exit);

MODULE_LICENSE("GPL");
MODULE_ALIAS_NETPROTO(PF_ATMPVC);
MODULE_ALIAS_NETPROTO(PF_ATMSVC);