writeback: split writeback_inodes_wb
[linux-2.6/next.git] / net / atm / common.c
blobb43feb1a3995f77a75d0c445859ab70030c2f477
1 /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
5 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
7 #include <linux/module.h>
8 #include <linux/kmod.h>
9 #include <linux/net.h> /* struct socket, struct proto_ops */
10 #include <linux/atm.h> /* ATM stuff */
11 #include <linux/atmdev.h>
12 #include <linux/socket.h> /* SOL_SOCKET */
13 #include <linux/errno.h> /* error codes */
14 #include <linux/capability.h>
15 #include <linux/mm.h>
16 #include <linux/sched.h>
17 #include <linux/time.h> /* struct timeval */
18 #include <linux/skbuff.h>
19 #include <linux/bitops.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <net/sock.h> /* struct sock */
23 #include <linux/uaccess.h>
24 #include <linux/poll.h>
26 #include <asm/atomic.h>
28 #include "resources.h" /* atm_find_dev */
29 #include "common.h" /* prototypes */
30 #include "protocols.h" /* atm_init_<transport> */
31 #include "addr.h" /* address registry */
32 #include "signaling.h" /* for WAITING and sigd_attach */
34 struct hlist_head vcc_hash[VCC_HTABLE_SIZE];
35 EXPORT_SYMBOL(vcc_hash);
37 DEFINE_RWLOCK(vcc_sklist_lock);
38 EXPORT_SYMBOL(vcc_sklist_lock);
40 static void __vcc_insert_socket(struct sock *sk)
42 struct atm_vcc *vcc = atm_sk(sk);
43 struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)];
44 sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1);
45 sk_add_node(sk, head);
48 void vcc_insert_socket(struct sock *sk)
50 write_lock_irq(&vcc_sklist_lock);
51 __vcc_insert_socket(sk);
52 write_unlock_irq(&vcc_sklist_lock);
54 EXPORT_SYMBOL(vcc_insert_socket);
56 static void vcc_remove_socket(struct sock *sk)
58 write_lock_irq(&vcc_sklist_lock);
59 sk_del_node_init(sk);
60 write_unlock_irq(&vcc_sklist_lock);
63 static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size)
65 struct sk_buff *skb;
66 struct sock *sk = sk_atm(vcc);
68 if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) {
69 pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n",
70 sk_wmem_alloc_get(sk), size, sk->sk_sndbuf);
71 return NULL;
73 while (!(skb = alloc_skb(size, GFP_KERNEL)))
74 schedule();
75 pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize);
76 atomic_add(skb->truesize, &sk->sk_wmem_alloc);
77 return skb;
80 static void vcc_sock_destruct(struct sock *sk)
82 if (atomic_read(&sk->sk_rmem_alloc))
83 printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n",
84 __func__, atomic_read(&sk->sk_rmem_alloc));
86 if (atomic_read(&sk->sk_wmem_alloc))
87 printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n",
88 __func__, atomic_read(&sk->sk_wmem_alloc));
91 static void vcc_def_wakeup(struct sock *sk)
93 struct socket_wq *wq;
95 rcu_read_lock();
96 wq = rcu_dereference(sk->sk_wq);
97 if (wq_has_sleeper(wq))
98 wake_up(&wq->wait);
99 rcu_read_unlock();
102 static inline int vcc_writable(struct sock *sk)
104 struct atm_vcc *vcc = atm_sk(sk);
106 return (vcc->qos.txtp.max_sdu +
107 atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf;
110 static void vcc_write_space(struct sock *sk)
112 struct socket_wq *wq;
114 rcu_read_lock();
116 if (vcc_writable(sk)) {
117 wq = rcu_dereference(sk->sk_wq);
118 if (wq_has_sleeper(wq))
119 wake_up_interruptible(&wq->wait);
121 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
124 rcu_read_unlock();
127 static struct proto vcc_proto = {
128 .name = "VCC",
129 .owner = THIS_MODULE,
130 .obj_size = sizeof(struct atm_vcc),
133 int vcc_create(struct net *net, struct socket *sock, int protocol, int family)
135 struct sock *sk;
136 struct atm_vcc *vcc;
138 sock->sk = NULL;
139 if (sock->type == SOCK_STREAM)
140 return -EINVAL;
141 sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto);
142 if (!sk)
143 return -ENOMEM;
144 sock_init_data(sock, sk);
145 sk->sk_state_change = vcc_def_wakeup;
146 sk->sk_write_space = vcc_write_space;
148 vcc = atm_sk(sk);
149 vcc->dev = NULL;
150 memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc));
151 memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc));
152 vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */
153 atomic_set(&sk->sk_wmem_alloc, 1);
154 atomic_set(&sk->sk_rmem_alloc, 0);
155 vcc->push = NULL;
156 vcc->pop = NULL;
157 vcc->push_oam = NULL;
158 vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */
159 vcc->atm_options = vcc->aal_options = 0;
160 sk->sk_destruct = vcc_sock_destruct;
161 return 0;
164 static void vcc_destroy_socket(struct sock *sk)
166 struct atm_vcc *vcc = atm_sk(sk);
167 struct sk_buff *skb;
169 set_bit(ATM_VF_CLOSE, &vcc->flags);
170 clear_bit(ATM_VF_READY, &vcc->flags);
171 if (vcc->dev) {
172 if (vcc->dev->ops->close)
173 vcc->dev->ops->close(vcc);
174 if (vcc->push)
175 vcc->push(vcc, NULL); /* atmarpd has no push */
177 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
178 atm_return(vcc, skb->truesize);
179 kfree_skb(skb);
182 module_put(vcc->dev->ops->owner);
183 atm_dev_put(vcc->dev);
186 vcc_remove_socket(sk);
189 int vcc_release(struct socket *sock)
191 struct sock *sk = sock->sk;
193 if (sk) {
194 lock_sock(sk);
195 vcc_destroy_socket(sock->sk);
196 release_sock(sk);
197 sock_put(sk);
200 return 0;
203 void vcc_release_async(struct atm_vcc *vcc, int reply)
205 struct sock *sk = sk_atm(vcc);
207 set_bit(ATM_VF_CLOSE, &vcc->flags);
208 sk->sk_shutdown |= RCV_SHUTDOWN;
209 sk->sk_err = -reply;
210 clear_bit(ATM_VF_WAITING, &vcc->flags);
211 sk->sk_state_change(sk);
213 EXPORT_SYMBOL(vcc_release_async);
216 void atm_dev_release_vccs(struct atm_dev *dev)
218 int i;
220 write_lock_irq(&vcc_sklist_lock);
221 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
222 struct hlist_head *head = &vcc_hash[i];
223 struct hlist_node *node, *tmp;
224 struct sock *s;
225 struct atm_vcc *vcc;
227 sk_for_each_safe(s, node, tmp, head) {
228 vcc = atm_sk(s);
229 if (vcc->dev == dev) {
230 vcc_release_async(vcc, -EPIPE);
231 sk_del_node_init(s);
235 write_unlock_irq(&vcc_sklist_lock);
238 static int adjust_tp(struct atm_trafprm *tp, unsigned char aal)
240 int max_sdu;
242 if (!tp->traffic_class)
243 return 0;
244 switch (aal) {
245 case ATM_AAL0:
246 max_sdu = ATM_CELL_SIZE-1;
247 break;
248 case ATM_AAL34:
249 max_sdu = ATM_MAX_AAL34_PDU;
250 break;
251 default:
252 pr_warning("AAL problems ... (%d)\n", aal);
253 /* fall through */
254 case ATM_AAL5:
255 max_sdu = ATM_MAX_AAL5_PDU;
257 if (!tp->max_sdu)
258 tp->max_sdu = max_sdu;
259 else if (tp->max_sdu > max_sdu)
260 return -EINVAL;
261 if (!tp->max_cdv)
262 tp->max_cdv = ATM_MAX_CDV;
263 return 0;
266 static int check_ci(const struct atm_vcc *vcc, short vpi, int vci)
268 struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)];
269 struct hlist_node *node;
270 struct sock *s;
271 struct atm_vcc *walk;
273 sk_for_each(s, node, head) {
274 walk = atm_sk(s);
275 if (walk->dev != vcc->dev)
276 continue;
277 if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi &&
278 walk->vci == vci && ((walk->qos.txtp.traffic_class !=
279 ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) ||
280 (walk->qos.rxtp.traffic_class != ATM_NONE &&
281 vcc->qos.rxtp.traffic_class != ATM_NONE)))
282 return -EADDRINUSE;
285 /* allow VCCs with same VPI/VCI iff they don't collide on
286 TX/RX (but we may refuse such sharing for other reasons,
287 e.g. if protocol requires to have both channels) */
289 return 0;
292 static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci)
294 static short p; /* poor man's per-device cache */
295 static int c;
296 short old_p;
297 int old_c;
298 int err;
300 if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) {
301 err = check_ci(vcc, *vpi, *vci);
302 return err;
304 /* last scan may have left values out of bounds for current device */
305 if (*vpi != ATM_VPI_ANY)
306 p = *vpi;
307 else if (p >= 1 << vcc->dev->ci_range.vpi_bits)
308 p = 0;
309 if (*vci != ATM_VCI_ANY)
310 c = *vci;
311 else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits)
312 c = ATM_NOT_RSV_VCI;
313 old_p = p;
314 old_c = c;
315 do {
316 if (!check_ci(vcc, p, c)) {
317 *vpi = p;
318 *vci = c;
319 return 0;
321 if (*vci == ATM_VCI_ANY) {
322 c++;
323 if (c >= 1 << vcc->dev->ci_range.vci_bits)
324 c = ATM_NOT_RSV_VCI;
326 if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) &&
327 *vpi == ATM_VPI_ANY) {
328 p++;
329 if (p >= 1 << vcc->dev->ci_range.vpi_bits)
330 p = 0;
332 } while (old_p != p || old_c != c);
333 return -EADDRINUSE;
336 static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi,
337 int vci)
339 struct sock *sk = sk_atm(vcc);
340 int error;
342 if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY &&
343 vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC &&
344 vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits))
345 return -EINVAL;
346 if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE))
347 return -EPERM;
348 error = -ENODEV;
349 if (!try_module_get(dev->ops->owner))
350 return error;
351 vcc->dev = dev;
352 write_lock_irq(&vcc_sklist_lock);
353 if (test_bit(ATM_DF_REMOVED, &dev->flags) ||
354 (error = find_ci(vcc, &vpi, &vci))) {
355 write_unlock_irq(&vcc_sklist_lock);
356 goto fail_module_put;
358 vcc->vpi = vpi;
359 vcc->vci = vci;
360 __vcc_insert_socket(sk);
361 write_unlock_irq(&vcc_sklist_lock);
362 switch (vcc->qos.aal) {
363 case ATM_AAL0:
364 error = atm_init_aal0(vcc);
365 vcc->stats = &dev->stats.aal0;
366 break;
367 case ATM_AAL34:
368 error = atm_init_aal34(vcc);
369 vcc->stats = &dev->stats.aal34;
370 break;
371 case ATM_NO_AAL:
372 /* ATM_AAL5 is also used in the "0 for default" case */
373 vcc->qos.aal = ATM_AAL5;
374 /* fall through */
375 case ATM_AAL5:
376 error = atm_init_aal5(vcc);
377 vcc->stats = &dev->stats.aal5;
378 break;
379 default:
380 error = -EPROTOTYPE;
382 if (!error)
383 error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal);
384 if (!error)
385 error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal);
386 if (error)
387 goto fail;
388 pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal);
389 pr_debug(" TX: %d, PCR %d..%d, SDU %d\n",
390 vcc->qos.txtp.traffic_class,
391 vcc->qos.txtp.min_pcr,
392 vcc->qos.txtp.max_pcr,
393 vcc->qos.txtp.max_sdu);
394 pr_debug(" RX: %d, PCR %d..%d, SDU %d\n",
395 vcc->qos.rxtp.traffic_class,
396 vcc->qos.rxtp.min_pcr,
397 vcc->qos.rxtp.max_pcr,
398 vcc->qos.rxtp.max_sdu);
400 if (dev->ops->open) {
401 error = dev->ops->open(vcc);
402 if (error)
403 goto fail;
405 return 0;
407 fail:
408 vcc_remove_socket(sk);
409 fail_module_put:
410 module_put(dev->ops->owner);
411 /* ensure we get dev module ref count correct */
412 vcc->dev = NULL;
413 return error;
416 int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
418 struct atm_dev *dev;
419 struct atm_vcc *vcc = ATM_SD(sock);
420 int error;
422 pr_debug("(vpi %d, vci %d)\n", vpi, vci);
423 if (sock->state == SS_CONNECTED)
424 return -EISCONN;
425 if (sock->state != SS_UNCONNECTED)
426 return -EINVAL;
427 if (!(vpi || vci))
428 return -EINVAL;
430 if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC)
431 clear_bit(ATM_VF_PARTIAL, &vcc->flags);
432 else
433 if (test_bit(ATM_VF_PARTIAL, &vcc->flags))
434 return -EINVAL;
435 pr_debug("(TX: cl %d,bw %d-%d,sdu %d; "
436 "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n",
437 vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr,
438 vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu,
439 vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr,
440 vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu,
441 vcc->qos.aal == ATM_AAL5 ? "" :
442 vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ",
443 vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal);
444 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
445 return -EBADFD;
446 if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS ||
447 vcc->qos.rxtp.traffic_class == ATM_ANYCLASS)
448 return -EINVAL;
449 if (likely(itf != ATM_ITF_ANY)) {
450 dev = try_then_request_module(atm_dev_lookup(itf),
451 "atm-device-%d", itf);
452 } else {
453 dev = NULL;
454 mutex_lock(&atm_dev_mutex);
455 if (!list_empty(&atm_devs)) {
456 dev = list_entry(atm_devs.next,
457 struct atm_dev, dev_list);
458 atm_dev_hold(dev);
460 mutex_unlock(&atm_dev_mutex);
462 if (!dev)
463 return -ENODEV;
464 error = __vcc_connect(vcc, dev, vpi, vci);
465 if (error) {
466 atm_dev_put(dev);
467 return error;
469 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC)
470 set_bit(ATM_VF_PARTIAL, &vcc->flags);
471 if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags))
472 sock->state = SS_CONNECTED;
473 return 0;
476 int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
477 size_t size, int flags)
479 struct sock *sk = sock->sk;
480 struct atm_vcc *vcc;
481 struct sk_buff *skb;
482 int copied, error = -EINVAL;
484 if (sock->state != SS_CONNECTED)
485 return -ENOTCONN;
486 if (flags & ~MSG_DONTWAIT) /* only handle MSG_DONTWAIT */
487 return -EOPNOTSUPP;
488 vcc = ATM_SD(sock);
489 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
490 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
491 !test_bit(ATM_VF_READY, &vcc->flags))
492 return 0;
494 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error);
495 if (!skb)
496 return error;
498 copied = skb->len;
499 if (copied > size) {
500 copied = size;
501 msg->msg_flags |= MSG_TRUNC;
504 error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
505 if (error)
506 return error;
507 sock_recv_ts_and_drops(msg, sk, skb);
508 pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc), skb->truesize);
509 atm_return(vcc, skb->truesize);
510 skb_free_datagram(sk, skb);
511 return copied;
514 int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
515 size_t total_len)
517 struct sock *sk = sock->sk;
518 DEFINE_WAIT(wait);
519 struct atm_vcc *vcc;
520 struct sk_buff *skb;
521 int eff, error;
522 const void __user *buff;
523 int size;
525 lock_sock(sk);
526 if (sock->state != SS_CONNECTED) {
527 error = -ENOTCONN;
528 goto out;
530 if (m->msg_name) {
531 error = -EISCONN;
532 goto out;
534 if (m->msg_iovlen != 1) {
535 error = -ENOSYS; /* fix this later @@@ */
536 goto out;
538 buff = m->msg_iov->iov_base;
539 size = m->msg_iov->iov_len;
540 vcc = ATM_SD(sock);
541 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
542 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
543 !test_bit(ATM_VF_READY, &vcc->flags)) {
544 error = -EPIPE;
545 send_sig(SIGPIPE, current, 0);
546 goto out;
548 if (!size) {
549 error = 0;
550 goto out;
552 if (size < 0 || size > vcc->qos.txtp.max_sdu) {
553 error = -EMSGSIZE;
554 goto out;
557 eff = (size+3) & ~3; /* align to word boundary */
558 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
559 error = 0;
560 while (!(skb = alloc_tx(vcc, eff))) {
561 if (m->msg_flags & MSG_DONTWAIT) {
562 error = -EAGAIN;
563 break;
565 schedule();
566 if (signal_pending(current)) {
567 error = -ERESTARTSYS;
568 break;
570 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
571 test_bit(ATM_VF_CLOSE, &vcc->flags) ||
572 !test_bit(ATM_VF_READY, &vcc->flags)) {
573 error = -EPIPE;
574 send_sig(SIGPIPE, current, 0);
575 break;
577 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
579 finish_wait(sk_sleep(sk), &wait);
580 if (error)
581 goto out;
582 skb->dev = NULL; /* for paths shared with net_device interfaces */
583 ATM_SKB(skb)->atm_options = vcc->atm_options;
584 if (copy_from_user(skb_put(skb, size), buff, size)) {
585 kfree_skb(skb);
586 error = -EFAULT;
587 goto out;
589 if (eff != size)
590 memset(skb->data + size, 0, eff-size);
591 error = vcc->dev->ops->send(vcc, skb);
592 error = error ? error : size;
593 out:
594 release_sock(sk);
595 return error;
598 unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait)
600 struct sock *sk = sock->sk;
601 struct atm_vcc *vcc;
602 unsigned int mask;
604 sock_poll_wait(file, sk_sleep(sk), wait);
605 mask = 0;
607 vcc = ATM_SD(sock);
609 /* exceptional events */
610 if (sk->sk_err)
611 mask = POLLERR;
613 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
614 test_bit(ATM_VF_CLOSE, &vcc->flags))
615 mask |= POLLHUP;
617 /* readable? */
618 if (!skb_queue_empty(&sk->sk_receive_queue))
619 mask |= POLLIN | POLLRDNORM;
621 /* writable? */
622 if (sock->state == SS_CONNECTING &&
623 test_bit(ATM_VF_WAITING, &vcc->flags))
624 return mask;
626 if (vcc->qos.txtp.traffic_class != ATM_NONE &&
627 vcc_writable(sk))
628 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
630 return mask;
633 static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos)
635 int error;
638 * Don't let the QoS change the already connected AAL type nor the
639 * traffic class.
641 if (qos->aal != vcc->qos.aal ||
642 qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class ||
643 qos->txtp.traffic_class != vcc->qos.txtp.traffic_class)
644 return -EINVAL;
645 error = adjust_tp(&qos->txtp, qos->aal);
646 if (!error)
647 error = adjust_tp(&qos->rxtp, qos->aal);
648 if (error)
649 return error;
650 if (!vcc->dev->ops->change_qos)
651 return -EOPNOTSUPP;
652 if (sk_atm(vcc)->sk_family == AF_ATMPVC)
653 return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET);
654 return svc_change_qos(vcc, qos);
657 static int check_tp(const struct atm_trafprm *tp)
659 /* @@@ Should be merged with adjust_tp */
660 if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS)
661 return 0;
662 if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr &&
663 !tp->max_pcr)
664 return -EINVAL;
665 if (tp->min_pcr == ATM_MAX_PCR)
666 return -EINVAL;
667 if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR &&
668 tp->min_pcr > tp->max_pcr)
669 return -EINVAL;
671 * We allow pcr to be outside [min_pcr,max_pcr], because later
672 * adjustment may still push it in the valid range.
674 return 0;
677 static int check_qos(const struct atm_qos *qos)
679 int error;
681 if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class)
682 return -EINVAL;
683 if (qos->txtp.traffic_class != qos->rxtp.traffic_class &&
684 qos->txtp.traffic_class && qos->rxtp.traffic_class &&
685 qos->txtp.traffic_class != ATM_ANYCLASS &&
686 qos->rxtp.traffic_class != ATM_ANYCLASS)
687 return -EINVAL;
688 error = check_tp(&qos->txtp);
689 if (error)
690 return error;
691 return check_tp(&qos->rxtp);
694 int vcc_setsockopt(struct socket *sock, int level, int optname,
695 char __user *optval, unsigned int optlen)
697 struct atm_vcc *vcc;
698 unsigned long value;
699 int error;
701 if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname))
702 return -EINVAL;
704 vcc = ATM_SD(sock);
705 switch (optname) {
706 case SO_ATMQOS:
708 struct atm_qos qos;
710 if (copy_from_user(&qos, optval, sizeof(qos)))
711 return -EFAULT;
712 error = check_qos(&qos);
713 if (error)
714 return error;
715 if (sock->state == SS_CONNECTED)
716 return atm_change_qos(vcc, &qos);
717 if (sock->state != SS_UNCONNECTED)
718 return -EBADFD;
719 vcc->qos = qos;
720 set_bit(ATM_VF_HASQOS, &vcc->flags);
721 return 0;
723 case SO_SETCLP:
724 if (get_user(value, (unsigned long __user *)optval))
725 return -EFAULT;
726 if (value)
727 vcc->atm_options |= ATM_ATMOPT_CLP;
728 else
729 vcc->atm_options &= ~ATM_ATMOPT_CLP;
730 return 0;
731 default:
732 if (level == SOL_SOCKET)
733 return -EINVAL;
734 break;
736 if (!vcc->dev || !vcc->dev->ops->setsockopt)
737 return -EINVAL;
738 return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen);
741 int vcc_getsockopt(struct socket *sock, int level, int optname,
742 char __user *optval, int __user *optlen)
744 struct atm_vcc *vcc;
745 int len;
747 if (get_user(len, optlen))
748 return -EFAULT;
749 if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname))
750 return -EINVAL;
752 vcc = ATM_SD(sock);
753 switch (optname) {
754 case SO_ATMQOS:
755 if (!test_bit(ATM_VF_HASQOS, &vcc->flags))
756 return -EINVAL;
757 return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos))
758 ? -EFAULT : 0;
759 case SO_SETCLP:
760 return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0,
761 (unsigned long __user *)optval) ? -EFAULT : 0;
762 case SO_ATMPVC:
764 struct sockaddr_atmpvc pvc;
766 if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags))
767 return -ENOTCONN;
768 pvc.sap_family = AF_ATMPVC;
769 pvc.sap_addr.itf = vcc->dev->number;
770 pvc.sap_addr.vpi = vcc->vpi;
771 pvc.sap_addr.vci = vcc->vci;
772 return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0;
774 default:
775 if (level == SOL_SOCKET)
776 return -EINVAL;
777 break;
779 if (!vcc->dev || !vcc->dev->ops->getsockopt)
780 return -EINVAL;
781 return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len);
784 static int __init atm_init(void)
786 int error;
788 error = proto_register(&vcc_proto, 0);
789 if (error < 0)
790 goto out;
791 error = atmpvc_init();
792 if (error < 0) {
793 pr_err("atmpvc_init() failed with %d\n", error);
794 goto out_unregister_vcc_proto;
796 error = atmsvc_init();
797 if (error < 0) {
798 pr_err("atmsvc_init() failed with %d\n", error);
799 goto out_atmpvc_exit;
801 error = atm_proc_init();
802 if (error < 0) {
803 pr_err("atm_proc_init() failed with %d\n", error);
804 goto out_atmsvc_exit;
806 error = atm_sysfs_init();
807 if (error < 0) {
808 pr_err("atm_sysfs_init() failed with %d\n", error);
809 goto out_atmproc_exit;
811 out:
812 return error;
813 out_atmproc_exit:
814 atm_proc_exit();
815 out_atmsvc_exit:
816 atmsvc_exit();
817 out_atmpvc_exit:
818 atmsvc_exit();
819 out_unregister_vcc_proto:
820 proto_unregister(&vcc_proto);
821 goto out;
824 static void __exit atm_exit(void)
826 atm_proc_exit();
827 atm_sysfs_exit();
828 atmsvc_exit();
829 atmpvc_exit();
830 proto_unregister(&vcc_proto);
833 subsys_initcall(atm_init);
835 module_exit(atm_exit);
837 MODULE_LICENSE("GPL");
838 MODULE_ALIAS_NETPROTO(PF_ATMPVC);
839 MODULE_ALIAS_NETPROTO(PF_ATMSVC);