Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */ |
| 2 | |
| 3 | /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */ |
| 4 | |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 5 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Jesper Juhl | e49332b | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 15 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <net/sock.h> /* struct sock */ |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/poll.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 26 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 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 */ |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct hlist_head vcc_hash[VCC_HTABLE_SIZE]; |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 35 | EXPORT_SYMBOL(vcc_hash); |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | DEFINE_RWLOCK(vcc_sklist_lock); |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 38 | EXPORT_SYMBOL(vcc_sklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Karl Hiramoto | 7313bb8 | 2010-07-08 20:55:30 +0000 | [diff] [blame] | 40 | static ATOMIC_NOTIFIER_HEAD(atm_dev_notify_chain); |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static void __vcc_insert_socket(struct sock *sk) |
| 43 | { |
| 44 | struct atm_vcc *vcc = atm_sk(sk); |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 45 | struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)]; |
Eric Dumazet | 81c3d54 | 2005-10-03 14:13:38 -0700 | [diff] [blame] | 46 | sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | sk_add_node(sk, head); |
| 48 | } |
| 49 | |
| 50 | void vcc_insert_socket(struct sock *sk) |
| 51 | { |
| 52 | write_lock_irq(&vcc_sklist_lock); |
| 53 | __vcc_insert_socket(sk); |
| 54 | write_unlock_irq(&vcc_sklist_lock); |
| 55 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 56 | EXPORT_SYMBOL(vcc_insert_socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | static void vcc_remove_socket(struct sock *sk) |
| 59 | { |
| 60 | write_lock_irq(&vcc_sklist_lock); |
| 61 | sk_del_node_init(sk); |
| 62 | write_unlock_irq(&vcc_sklist_lock); |
| 63 | } |
| 64 | |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 65 | static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | struct sk_buff *skb; |
| 68 | struct sock *sk = sk_atm(vcc); |
| 69 | |
Eric Dumazet | 81e2a3d | 2009-06-17 19:06:12 -0700 | [diff] [blame] | 70 | if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) { |
Stephen Hemminger | 5224006 | 2007-08-28 15:22:09 -0700 | [diff] [blame] | 71 | pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n", |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 72 | sk_wmem_alloc_get(sk), size, sk->sk_sndbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
Eric Dumazet | 81e2a3d | 2009-06-17 19:06:12 -0700 | [diff] [blame] | 75 | while (!(skb = alloc_skb(size, GFP_KERNEL))) |
| 76 | schedule(); |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 77 | pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | atomic_add(skb->truesize, &sk->sk_wmem_alloc); |
| 79 | return skb; |
| 80 | } |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | static void vcc_sock_destruct(struct sock *sk) |
| 83 | { |
| 84 | if (atomic_read(&sk->sk_rmem_alloc)) |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 85 | printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n", |
| 86 | __func__, atomic_read(&sk->sk_rmem_alloc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | if (atomic_read(&sk->sk_wmem_alloc)) |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 89 | printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n", |
| 90 | __func__, atomic_read(&sk->sk_wmem_alloc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void vcc_def_wakeup(struct sock *sk) |
| 94 | { |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 95 | struct socket_wq *wq; |
| 96 | |
| 97 | rcu_read_lock(); |
| 98 | wq = rcu_dereference(sk->sk_wq); |
| 99 | if (wq_has_sleeper(wq)) |
| 100 | wake_up(&wq->wait); |
| 101 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static inline int vcc_writable(struct sock *sk) |
| 105 | { |
| 106 | struct atm_vcc *vcc = atm_sk(sk); |
| 107 | |
| 108 | return (vcc->qos.txtp.max_sdu + |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 109 | atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void vcc_write_space(struct sock *sk) |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 113 | { |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 114 | struct socket_wq *wq; |
| 115 | |
| 116 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | if (vcc_writable(sk)) { |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 119 | wq = rcu_dereference(sk->sk_wq); |
| 120 | if (wq_has_sleeper(wq)) |
| 121 | wake_up_interruptible(&wq->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Pavel Emelyanov | 8d8ad9d | 2007-11-26 20:10:50 +0800 | [diff] [blame] | 123 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 126 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
David Woodhouse | c971f08 | 2012-11-28 00:03:11 +0000 | [diff] [blame] | 129 | static void vcc_release_cb(struct sock *sk) |
| 130 | { |
| 131 | struct atm_vcc *vcc = atm_sk(sk); |
| 132 | |
| 133 | if (vcc->release_cb) |
| 134 | vcc->release_cb(vcc); |
| 135 | } |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | static struct proto vcc_proto = { |
| 138 | .name = "VCC", |
| 139 | .owner = THIS_MODULE, |
| 140 | .obj_size = sizeof(struct atm_vcc), |
David Woodhouse | c971f08 | 2012-11-28 00:03:11 +0000 | [diff] [blame] | 141 | .release_cb = vcc_release_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | }; |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 143 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 144 | int vcc_create(struct net *net, struct socket *sock, int protocol, int family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | struct sock *sk; |
| 147 | struct atm_vcc *vcc; |
| 148 | |
| 149 | sock->sk = NULL; |
| 150 | if (sock->type == SOCK_STREAM) |
| 151 | return -EINVAL; |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 152 | sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | if (!sk) |
| 154 | return -ENOMEM; |
| 155 | sock_init_data(sock, sk); |
| 156 | sk->sk_state_change = vcc_def_wakeup; |
| 157 | sk->sk_write_space = vcc_write_space; |
| 158 | |
| 159 | vcc = atm_sk(sk); |
| 160 | vcc->dev = NULL; |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 161 | memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc)); |
| 162 | memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */ |
Eric Dumazet | 81e2a3d | 2009-06-17 19:06:12 -0700 | [diff] [blame] | 164 | atomic_set(&sk->sk_wmem_alloc, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | atomic_set(&sk->sk_rmem_alloc, 0); |
| 166 | vcc->push = NULL; |
| 167 | vcc->pop = NULL; |
Krzysztof Mazur | ec809bd | 2012-11-06 23:16:57 +0100 | [diff] [blame] | 168 | vcc->owner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | vcc->push_oam = NULL; |
David Woodhouse | c971f08 | 2012-11-28 00:03:11 +0000 | [diff] [blame] | 170 | vcc->release_cb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */ |
| 172 | vcc->atm_options = vcc->aal_options = 0; |
| 173 | sk->sk_destruct = vcc_sock_destruct; |
| 174 | return 0; |
| 175 | } |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | static void vcc_destroy_socket(struct sock *sk) |
| 178 | { |
| 179 | struct atm_vcc *vcc = atm_sk(sk); |
| 180 | struct sk_buff *skb; |
| 181 | |
| 182 | set_bit(ATM_VF_CLOSE, &vcc->flags); |
| 183 | clear_bit(ATM_VF_READY, &vcc->flags); |
| 184 | if (vcc->dev) { |
| 185 | if (vcc->dev->ops->close) |
| 186 | vcc->dev->ops->close(vcc); |
| 187 | if (vcc->push) |
| 188 | vcc->push(vcc, NULL); /* atmarpd has no push */ |
Krzysztof Mazur | ec809bd | 2012-11-06 23:16:57 +0100 | [diff] [blame] | 189 | module_put(vcc->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 192 | atm_return(vcc, skb->truesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | kfree_skb(skb); |
| 194 | } |
| 195 | |
| 196 | module_put(vcc->dev->ops->owner); |
| 197 | atm_dev_put(vcc->dev); |
| 198 | } |
Chas Williams | 9301e32 | 2005-09-28 16:35:01 -0700 | [diff] [blame] | 199 | |
| 200 | vcc_remove_socket(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | int vcc_release(struct socket *sock) |
| 204 | { |
| 205 | struct sock *sk = sock->sk; |
| 206 | |
| 207 | if (sk) { |
| 208 | lock_sock(sk); |
| 209 | vcc_destroy_socket(sock->sk); |
| 210 | release_sock(sk); |
| 211 | sock_put(sk); |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | void vcc_release_async(struct atm_vcc *vcc, int reply) |
| 218 | { |
| 219 | struct sock *sk = sk_atm(vcc); |
| 220 | |
| 221 | set_bit(ATM_VF_CLOSE, &vcc->flags); |
| 222 | sk->sk_shutdown |= RCV_SHUTDOWN; |
| 223 | sk->sk_err = -reply; |
| 224 | clear_bit(ATM_VF_WAITING, &vcc->flags); |
| 225 | sk->sk_state_change(sk); |
| 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | EXPORT_SYMBOL(vcc_release_async); |
| 228 | |
Jorge Boncompte [DTI2] | 4e55f57 | 2011-11-21 10:25:57 +0000 | [diff] [blame] | 229 | void vcc_process_recv_queue(struct atm_vcc *vcc) |
| 230 | { |
| 231 | struct sk_buff_head queue, *rq; |
| 232 | struct sk_buff *skb, *tmp; |
| 233 | unsigned long flags; |
| 234 | |
| 235 | __skb_queue_head_init(&queue); |
| 236 | rq = &sk_atm(vcc)->sk_receive_queue; |
| 237 | |
| 238 | spin_lock_irqsave(&rq->lock, flags); |
| 239 | skb_queue_splice_init(rq, &queue); |
| 240 | spin_unlock_irqrestore(&rq->lock, flags); |
| 241 | |
| 242 | skb_queue_walk_safe(&queue, skb, tmp) { |
| 243 | __skb_unlink(skb, &queue); |
| 244 | vcc->push(vcc, skb); |
| 245 | } |
| 246 | } |
| 247 | EXPORT_SYMBOL(vcc_process_recv_queue); |
| 248 | |
Karl Hiramoto | 7313bb8 | 2010-07-08 20:55:30 +0000 | [diff] [blame] | 249 | void atm_dev_signal_change(struct atm_dev *dev, char signal) |
| 250 | { |
| 251 | pr_debug("%s signal=%d dev=%p number=%d dev->signal=%d\n", |
| 252 | __func__, signal, dev, dev->number, dev->signal); |
| 253 | |
| 254 | /* atm driver sending invalid signal */ |
| 255 | WARN_ON(signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND); |
| 256 | |
| 257 | if (dev->signal == signal) |
| 258 | return; /* no change */ |
| 259 | |
| 260 | dev->signal = signal; |
| 261 | |
| 262 | atomic_notifier_call_chain(&atm_dev_notify_chain, signal, dev); |
| 263 | } |
| 264 | EXPORT_SYMBOL(atm_dev_signal_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 266 | void atm_dev_release_vccs(struct atm_dev *dev) |
| 267 | { |
| 268 | int i; |
| 269 | |
| 270 | write_lock_irq(&vcc_sklist_lock); |
| 271 | for (i = 0; i < VCC_HTABLE_SIZE; i++) { |
| 272 | struct hlist_head *head = &vcc_hash[i]; |
| 273 | struct hlist_node *node, *tmp; |
| 274 | struct sock *s; |
| 275 | struct atm_vcc *vcc; |
| 276 | |
| 277 | sk_for_each_safe(s, node, tmp, head) { |
| 278 | vcc = atm_sk(s); |
| 279 | if (vcc->dev == dev) { |
| 280 | vcc_release_async(vcc, -EPIPE); |
| 281 | sk_del_node_init(s); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | write_unlock_irq(&vcc_sklist_lock); |
| 286 | } |
Philip A. Prindeville | c031235 | 2011-03-30 13:17:04 +0000 | [diff] [blame] | 287 | EXPORT_SYMBOL(atm_dev_release_vccs); |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 288 | |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 289 | static int adjust_tp(struct atm_trafprm *tp, unsigned char aal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | int max_sdu; |
| 292 | |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 293 | if (!tp->traffic_class) |
| 294 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | switch (aal) { |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 296 | case ATM_AAL0: |
| 297 | max_sdu = ATM_CELL_SIZE-1; |
| 298 | break; |
| 299 | case ATM_AAL34: |
| 300 | max_sdu = ATM_MAX_AAL34_PDU; |
| 301 | break; |
| 302 | default: |
| 303 | pr_warning("AAL problems ... (%d)\n", aal); |
| 304 | /* fall through */ |
| 305 | case ATM_AAL5: |
| 306 | max_sdu = ATM_MAX_AAL5_PDU; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 308 | if (!tp->max_sdu) |
| 309 | tp->max_sdu = max_sdu; |
| 310 | else if (tp->max_sdu > max_sdu) |
| 311 | return -EINVAL; |
| 312 | if (!tp->max_cdv) |
| 313 | tp->max_cdv = ATM_MAX_CDV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
Mitchell Blank Jr | 61c33e0 | 2008-06-17 16:20:06 -0700 | [diff] [blame] | 317 | static int check_ci(const struct atm_vcc *vcc, short vpi, int vci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 319 | struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | struct hlist_node *node; |
| 321 | struct sock *s; |
| 322 | struct atm_vcc *walk; |
| 323 | |
| 324 | sk_for_each(s, node, head) { |
| 325 | walk = atm_sk(s); |
| 326 | if (walk->dev != vcc->dev) |
| 327 | continue; |
| 328 | if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi && |
| 329 | walk->vci == vci && ((walk->qos.txtp.traffic_class != |
| 330 | ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) || |
| 331 | (walk->qos.rxtp.traffic_class != ATM_NONE && |
| 332 | vcc->qos.rxtp.traffic_class != ATM_NONE))) |
| 333 | return -EADDRINUSE; |
| 334 | } |
| 335 | |
| 336 | /* allow VCCs with same VPI/VCI iff they don't collide on |
| 337 | TX/RX (but we may refuse such sharing for other reasons, |
| 338 | e.g. if protocol requires to have both channels) */ |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Mitchell Blank Jr | 61c33e0 | 2008-06-17 16:20:06 -0700 | [diff] [blame] | 343 | static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
| 345 | static short p; /* poor man's per-device cache */ |
| 346 | static int c; |
| 347 | short old_p; |
| 348 | int old_c; |
| 349 | int err; |
| 350 | |
| 351 | if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) { |
| 352 | err = check_ci(vcc, *vpi, *vci); |
| 353 | return err; |
| 354 | } |
| 355 | /* last scan may have left values out of bounds for current device */ |
| 356 | if (*vpi != ATM_VPI_ANY) |
| 357 | p = *vpi; |
| 358 | else if (p >= 1 << vcc->dev->ci_range.vpi_bits) |
| 359 | p = 0; |
| 360 | if (*vci != ATM_VCI_ANY) |
| 361 | c = *vci; |
| 362 | else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits) |
| 363 | c = ATM_NOT_RSV_VCI; |
| 364 | old_p = p; |
| 365 | old_c = c; |
| 366 | do { |
| 367 | if (!check_ci(vcc, p, c)) { |
| 368 | *vpi = p; |
| 369 | *vci = c; |
| 370 | return 0; |
| 371 | } |
| 372 | if (*vci == ATM_VCI_ANY) { |
| 373 | c++; |
| 374 | if (c >= 1 << vcc->dev->ci_range.vci_bits) |
| 375 | c = ATM_NOT_RSV_VCI; |
| 376 | } |
| 377 | if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) && |
| 378 | *vpi == ATM_VPI_ANY) { |
| 379 | p++; |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 380 | if (p >= 1 << vcc->dev->ci_range.vpi_bits) |
| 381 | p = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 383 | } while (old_p != p || old_c != c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return -EADDRINUSE; |
| 385 | } |
| 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi, |
| 388 | int vci) |
| 389 | { |
| 390 | struct sock *sk = sk_atm(vcc); |
| 391 | int error; |
| 392 | |
| 393 | if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY && |
| 394 | vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC && |
| 395 | vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits)) |
| 396 | return -EINVAL; |
| 397 | if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE)) |
| 398 | return -EPERM; |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 399 | error = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (!try_module_get(dev->ops->owner)) |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 401 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | vcc->dev = dev; |
| 403 | write_lock_irq(&vcc_sklist_lock); |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 404 | if (test_bit(ATM_DF_REMOVED, &dev->flags) || |
Stanislaw Gruszka | 64bf69d | 2005-11-29 16:16:41 -0800 | [diff] [blame] | 405 | (error = find_ci(vcc, &vpi, &vci))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | write_unlock_irq(&vcc_sklist_lock); |
| 407 | goto fail_module_put; |
| 408 | } |
| 409 | vcc->vpi = vpi; |
| 410 | vcc->vci = vci; |
| 411 | __vcc_insert_socket(sk); |
| 412 | write_unlock_irq(&vcc_sklist_lock); |
| 413 | switch (vcc->qos.aal) { |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 414 | case ATM_AAL0: |
| 415 | error = atm_init_aal0(vcc); |
| 416 | vcc->stats = &dev->stats.aal0; |
| 417 | break; |
| 418 | case ATM_AAL34: |
| 419 | error = atm_init_aal34(vcc); |
| 420 | vcc->stats = &dev->stats.aal34; |
| 421 | break; |
| 422 | case ATM_NO_AAL: |
| 423 | /* ATM_AAL5 is also used in the "0 for default" case */ |
| 424 | vcc->qos.aal = ATM_AAL5; |
| 425 | /* fall through */ |
| 426 | case ATM_AAL5: |
| 427 | error = atm_init_aal5(vcc); |
| 428 | vcc->stats = &dev->stats.aal5; |
| 429 | break; |
| 430 | default: |
| 431 | error = -EPROTOTYPE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 433 | if (!error) |
| 434 | error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal); |
| 435 | if (!error) |
| 436 | error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | if (error) |
| 438 | goto fail; |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 439 | pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal); |
| 440 | pr_debug(" TX: %d, PCR %d..%d, SDU %d\n", |
| 441 | vcc->qos.txtp.traffic_class, |
| 442 | vcc->qos.txtp.min_pcr, |
| 443 | vcc->qos.txtp.max_pcr, |
| 444 | vcc->qos.txtp.max_sdu); |
| 445 | pr_debug(" RX: %d, PCR %d..%d, SDU %d\n", |
| 446 | vcc->qos.rxtp.traffic_class, |
| 447 | vcc->qos.rxtp.min_pcr, |
| 448 | vcc->qos.rxtp.max_pcr, |
| 449 | vcc->qos.rxtp.max_sdu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | if (dev->ops->open) { |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 452 | error = dev->ops->open(vcc); |
| 453 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | goto fail; |
| 455 | } |
| 456 | return 0; |
| 457 | |
| 458 | fail: |
| 459 | vcc_remove_socket(sk); |
| 460 | fail_module_put: |
| 461 | module_put(dev->ops->owner); |
| 462 | /* ensure we get dev module ref count correct */ |
| 463 | vcc->dev = NULL; |
| 464 | return error; |
| 465 | } |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | int vcc_connect(struct socket *sock, int itf, short vpi, int vci) |
| 468 | { |
| 469 | struct atm_dev *dev; |
| 470 | struct atm_vcc *vcc = ATM_SD(sock); |
| 471 | int error; |
| 472 | |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 473 | pr_debug("(vpi %d, vci %d)\n", vpi, vci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (sock->state == SS_CONNECTED) |
| 475 | return -EISCONN; |
| 476 | if (sock->state != SS_UNCONNECTED) |
| 477 | return -EINVAL; |
| 478 | if (!(vpi || vci)) |
| 479 | return -EINVAL; |
| 480 | |
| 481 | if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC) |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 482 | clear_bit(ATM_VF_PARTIAL, &vcc->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | else |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 484 | if (test_bit(ATM_VF_PARTIAL, &vcc->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return -EINVAL; |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 486 | pr_debug("(TX: cl %d,bw %d-%d,sdu %d; " |
| 487 | "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n", |
| 488 | vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr, |
| 489 | vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu, |
| 490 | vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr, |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 491 | vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu, |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 492 | vcc->qos.aal == ATM_AAL5 ? "" : |
| 493 | vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ", |
| 494 | vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) |
| 496 | return -EBADFD; |
| 497 | if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS || |
| 498 | vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) |
| 499 | return -EINVAL; |
Mitchell Blank Jr | c9933d0 | 2005-11-29 16:13:32 -0800 | [diff] [blame] | 500 | if (likely(itf != ATM_ITF_ANY)) { |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 501 | dev = try_then_request_module(atm_dev_lookup(itf), |
| 502 | "atm-device-%d", itf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | dev = NULL; |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 505 | mutex_lock(&atm_dev_mutex); |
Mitchell Blank Jr | c9933d0 | 2005-11-29 16:13:32 -0800 | [diff] [blame] | 506 | if (!list_empty(&atm_devs)) { |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 507 | dev = list_entry(atm_devs.next, |
| 508 | struct atm_dev, dev_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | atm_dev_hold(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 511 | mutex_unlock(&atm_dev_mutex); |
Mitchell Blank Jr | c9933d0 | 2005-11-29 16:13:32 -0800 | [diff] [blame] | 512 | } |
| 513 | if (!dev) |
| 514 | return -ENODEV; |
| 515 | error = __vcc_connect(vcc, dev, vpi, vci); |
| 516 | if (error) { |
| 517 | atm_dev_put(dev); |
| 518 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 521 | set_bit(ATM_VF_PARTIAL, &vcc->flags); |
| 522 | if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | sock->state = SS_CONNECTED; |
| 524 | return 0; |
| 525 | } |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, |
| 528 | size_t size, int flags) |
| 529 | { |
| 530 | struct sock *sk = sock->sk; |
| 531 | struct atm_vcc *vcc; |
| 532 | struct sk_buff *skb; |
| 533 | int copied, error = -EINVAL; |
| 534 | |
| 535 | if (sock->state != SS_CONNECTED) |
| 536 | return -ENOTCONN; |
Jorge Boncompte [DTI2] | 40ba849 | 2011-11-21 10:25:58 +0000 | [diff] [blame] | 537 | |
| 538 | /* only handle MSG_DONTWAIT and MSG_PEEK */ |
| 539 | if (flags & ~(MSG_DONTWAIT | MSG_PEEK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return -EOPNOTSUPP; |
Jorge Boncompte [DTI2] | 40ba849 | 2011-11-21 10:25:58 +0000 | [diff] [blame] | 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | vcc = ATM_SD(sock); |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 543 | if (test_bit(ATM_VF_RELEASED, &vcc->flags) || |
| 544 | test_bit(ATM_VF_CLOSE, &vcc->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | !test_bit(ATM_VF_READY, &vcc->flags)) |
| 546 | return 0; |
| 547 | |
| 548 | skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error); |
| 549 | if (!skb) |
| 550 | return error; |
| 551 | |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 552 | copied = skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | if (copied > size) { |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 554 | copied = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | msg->msg_flags |= MSG_TRUNC; |
| 556 | } |
| 557 | |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 558 | error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 559 | if (error) |
| 560 | return error; |
Neil Horman | 3b88578 | 2009-10-12 13:26:31 -0700 | [diff] [blame] | 561 | sock_recv_ts_and_drops(msg, sk, skb); |
Jorge Boncompte [DTI2] | 40ba849 | 2011-11-21 10:25:58 +0000 | [diff] [blame] | 562 | |
| 563 | if (!(flags & MSG_PEEK)) { |
| 564 | pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc), |
| 565 | skb->truesize); |
| 566 | atm_return(vcc, skb->truesize); |
| 567 | } |
| 568 | |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 569 | skb_free_datagram(sk, skb); |
| 570 | return copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, |
| 574 | size_t total_len) |
| 575 | { |
| 576 | struct sock *sk = sock->sk; |
| 577 | DEFINE_WAIT(wait); |
| 578 | struct atm_vcc *vcc; |
| 579 | struct sk_buff *skb; |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 580 | int eff, error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | const void __user *buff; |
| 582 | int size; |
| 583 | |
| 584 | lock_sock(sk); |
| 585 | if (sock->state != SS_CONNECTED) { |
| 586 | error = -ENOTCONN; |
| 587 | goto out; |
| 588 | } |
| 589 | if (m->msg_name) { |
| 590 | error = -EISCONN; |
| 591 | goto out; |
| 592 | } |
| 593 | if (m->msg_iovlen != 1) { |
| 594 | error = -ENOSYS; /* fix this later @@@ */ |
| 595 | goto out; |
| 596 | } |
| 597 | buff = m->msg_iov->iov_base; |
| 598 | size = m->msg_iov->iov_len; |
| 599 | vcc = ATM_SD(sock); |
| 600 | if (test_bit(ATM_VF_RELEASED, &vcc->flags) || |
| 601 | test_bit(ATM_VF_CLOSE, &vcc->flags) || |
| 602 | !test_bit(ATM_VF_READY, &vcc->flags)) { |
| 603 | error = -EPIPE; |
| 604 | send_sig(SIGPIPE, current, 0); |
| 605 | goto out; |
| 606 | } |
| 607 | if (!size) { |
| 608 | error = 0; |
| 609 | goto out; |
| 610 | } |
| 611 | if (size < 0 || size > vcc->qos.txtp.max_sdu) { |
| 612 | error = -EMSGSIZE; |
| 613 | goto out; |
| 614 | } |
Jesper Juhl | e49332b | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | eff = (size+3) & ~3; /* align to word boundary */ |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 617 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | error = 0; |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 619 | while (!(skb = alloc_tx(vcc, eff))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | if (m->msg_flags & MSG_DONTWAIT) { |
| 621 | error = -EAGAIN; |
| 622 | break; |
| 623 | } |
| 624 | schedule(); |
| 625 | if (signal_pending(current)) { |
| 626 | error = -ERESTARTSYS; |
| 627 | break; |
| 628 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 629 | if (test_bit(ATM_VF_RELEASED, &vcc->flags) || |
| 630 | test_bit(ATM_VF_CLOSE, &vcc->flags) || |
| 631 | !test_bit(ATM_VF_READY, &vcc->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | error = -EPIPE; |
| 633 | send_sig(SIGPIPE, current, 0); |
| 634 | break; |
| 635 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 636 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 638 | finish_wait(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | if (error) |
| 640 | goto out; |
| 641 | skb->dev = NULL; /* for paths shared with net_device interfaces */ |
| 642 | ATM_SKB(skb)->atm_options = vcc->atm_options; |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 643 | if (copy_from_user(skb_put(skb, size), buff, size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | kfree_skb(skb); |
| 645 | error = -EFAULT; |
| 646 | goto out; |
| 647 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 648 | if (eff != size) |
| 649 | memset(skb->data + size, 0, eff-size); |
| 650 | error = vcc->dev->ops->send(vcc, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | error = error ? error : size; |
| 652 | out: |
| 653 | release_sock(sk); |
| 654 | return error; |
| 655 | } |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait) |
| 658 | { |
| 659 | struct sock *sk = sock->sk; |
| 660 | struct atm_vcc *vcc; |
| 661 | unsigned int mask; |
| 662 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 663 | sock_poll_wait(file, sk_sleep(sk), wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | mask = 0; |
| 665 | |
| 666 | vcc = ATM_SD(sock); |
| 667 | |
| 668 | /* exceptional events */ |
| 669 | if (sk->sk_err) |
| 670 | mask = POLLERR; |
| 671 | |
| 672 | if (test_bit(ATM_VF_RELEASED, &vcc->flags) || |
| 673 | test_bit(ATM_VF_CLOSE, &vcc->flags)) |
| 674 | mask |= POLLHUP; |
| 675 | |
| 676 | /* readable? */ |
| 677 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 678 | mask |= POLLIN | POLLRDNORM; |
| 679 | |
| 680 | /* writable? */ |
| 681 | if (sock->state == SS_CONNECTING && |
| 682 | test_bit(ATM_VF_WAITING, &vcc->flags)) |
| 683 | return mask; |
| 684 | |
| 685 | if (vcc->qos.txtp.traffic_class != ATM_NONE && |
| 686 | vcc_writable(sk)) |
| 687 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
| 688 | |
| 689 | return mask; |
| 690 | } |
| 691 | |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 692 | static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
| 694 | int error; |
| 695 | |
| 696 | /* |
| 697 | * Don't let the QoS change the already connected AAL type nor the |
| 698 | * traffic class. |
| 699 | */ |
| 700 | if (qos->aal != vcc->qos.aal || |
| 701 | qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class || |
| 702 | qos->txtp.traffic_class != vcc->qos.txtp.traffic_class) |
| 703 | return -EINVAL; |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 704 | error = adjust_tp(&qos->txtp, qos->aal); |
| 705 | if (!error) |
| 706 | error = adjust_tp(&qos->rxtp, qos->aal); |
| 707 | if (error) |
| 708 | return error; |
| 709 | if (!vcc->dev->ops->change_qos) |
| 710 | return -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | if (sk_atm(vcc)->sk_family == AF_ATMPVC) |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 712 | return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET); |
| 713 | return svc_change_qos(vcc, qos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Mitchell Blank Jr | 61c33e0 | 2008-06-17 16:20:06 -0700 | [diff] [blame] | 716 | static int check_tp(const struct atm_trafprm *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
| 718 | /* @@@ Should be merged with adjust_tp */ |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 719 | if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) |
| 720 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr && |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 722 | !tp->max_pcr) |
| 723 | return -EINVAL; |
| 724 | if (tp->min_pcr == ATM_MAX_PCR) |
| 725 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR && |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 727 | tp->min_pcr > tp->max_pcr) |
| 728 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | /* |
| 730 | * We allow pcr to be outside [min_pcr,max_pcr], because later |
| 731 | * adjustment may still push it in the valid range. |
| 732 | */ |
| 733 | return 0; |
| 734 | } |
| 735 | |
Mitchell Blank Jr | 61c33e0 | 2008-06-17 16:20:06 -0700 | [diff] [blame] | 736 | static int check_qos(const struct atm_qos *qos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
| 738 | int error; |
| 739 | |
| 740 | if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class) |
YOSHIFUJI Hideaki | f7d5745 | 2007-02-09 23:24:29 +0900 | [diff] [blame] | 741 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | if (qos->txtp.traffic_class != qos->rxtp.traffic_class && |
| 743 | qos->txtp.traffic_class && qos->rxtp.traffic_class && |
| 744 | qos->txtp.traffic_class != ATM_ANYCLASS && |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 745 | qos->rxtp.traffic_class != ATM_ANYCLASS) |
| 746 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | error = check_tp(&qos->txtp); |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 748 | if (error) |
| 749 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | return check_tp(&qos->rxtp); |
| 751 | } |
| 752 | |
| 753 | int vcc_setsockopt(struct socket *sock, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 754 | char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | { |
| 756 | struct atm_vcc *vcc; |
| 757 | unsigned long value; |
| 758 | int error; |
| 759 | |
| 760 | if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname)) |
| 761 | return -EINVAL; |
| 762 | |
| 763 | vcc = ATM_SD(sock); |
| 764 | switch (optname) { |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 765 | case SO_ATMQOS: |
| 766 | { |
| 767 | struct atm_qos qos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 769 | if (copy_from_user(&qos, optval, sizeof(qos))) |
| 770 | return -EFAULT; |
| 771 | error = check_qos(&qos); |
| 772 | if (error) |
| 773 | return error; |
| 774 | if (sock->state == SS_CONNECTED) |
| 775 | return atm_change_qos(vcc, &qos); |
| 776 | if (sock->state != SS_UNCONNECTED) |
| 777 | return -EBADFD; |
| 778 | vcc->qos = qos; |
| 779 | set_bit(ATM_VF_HASQOS, &vcc->flags); |
| 780 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 782 | case SO_SETCLP: |
| 783 | if (get_user(value, (unsigned long __user *)optval)) |
| 784 | return -EFAULT; |
| 785 | if (value) |
| 786 | vcc->atm_options |= ATM_ATMOPT_CLP; |
| 787 | else |
| 788 | vcc->atm_options &= ~ATM_ATMOPT_CLP; |
| 789 | return 0; |
| 790 | default: |
| 791 | if (level == SOL_SOCKET) |
| 792 | return -EINVAL; |
| 793 | break; |
| 794 | } |
| 795 | if (!vcc->dev || !vcc->dev->ops->setsockopt) |
| 796 | return -EINVAL; |
| 797 | return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | int vcc_getsockopt(struct socket *sock, int level, int optname, |
| 801 | char __user *optval, int __user *optlen) |
| 802 | { |
| 803 | struct atm_vcc *vcc; |
| 804 | int len; |
| 805 | |
| 806 | if (get_user(len, optlen)) |
| 807 | return -EFAULT; |
| 808 | if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname)) |
| 809 | return -EINVAL; |
| 810 | |
| 811 | vcc = ATM_SD(sock); |
| 812 | switch (optname) { |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 813 | case SO_ATMQOS: |
| 814 | if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) |
| 815 | return -EINVAL; |
| 816 | return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos)) |
| 817 | ? -EFAULT : 0; |
| 818 | case SO_SETCLP: |
| 819 | return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0, |
| 820 | (unsigned long __user *)optval) ? -EFAULT : 0; |
| 821 | case SO_ATMPVC: |
| 822 | { |
| 823 | struct sockaddr_atmpvc pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 825 | if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags)) |
| 826 | return -ENOTCONN; |
Mathias Krause | e862f1a | 2012-08-15 11:31:44 +0000 | [diff] [blame] | 827 | memset(&pvc, 0, sizeof(pvc)); |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 828 | pvc.sap_family = AF_ATMPVC; |
| 829 | pvc.sap_addr.itf = vcc->dev->number; |
| 830 | pvc.sap_addr.vpi = vcc->vpi; |
| 831 | pvc.sap_addr.vci = vcc->vci; |
| 832 | return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0; |
| 833 | } |
| 834 | default: |
| 835 | if (level == SOL_SOCKET) |
| 836 | return -EINVAL; |
Julia Lawall | 510a05e | 2010-08-05 10:19:00 +0000 | [diff] [blame] | 837 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 839 | if (!vcc->dev || !vcc->dev->ops->getsockopt) |
| 840 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len); |
| 842 | } |
| 843 | |
Karl Hiramoto | 7313bb8 | 2010-07-08 20:55:30 +0000 | [diff] [blame] | 844 | int register_atmdevice_notifier(struct notifier_block *nb) |
| 845 | { |
| 846 | return atomic_notifier_chain_register(&atm_dev_notify_chain, nb); |
| 847 | } |
| 848 | EXPORT_SYMBOL_GPL(register_atmdevice_notifier); |
| 849 | |
| 850 | void unregister_atmdevice_notifier(struct notifier_block *nb) |
| 851 | { |
| 852 | atomic_notifier_chain_unregister(&atm_dev_notify_chain, nb); |
| 853 | } |
| 854 | EXPORT_SYMBOL_GPL(unregister_atmdevice_notifier); |
| 855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | static int __init atm_init(void) |
| 857 | { |
| 858 | int error; |
| 859 | |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 860 | error = proto_register(&vcc_proto, 0); |
| 861 | if (error < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | goto out; |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 863 | error = atmpvc_init(); |
| 864 | if (error < 0) { |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 865 | pr_err("atmpvc_init() failed with %d\n", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | goto out_unregister_vcc_proto; |
| 867 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 868 | error = atmsvc_init(); |
| 869 | if (error < 0) { |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 870 | pr_err("atmsvc_init() failed with %d\n", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | goto out_atmpvc_exit; |
| 872 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 873 | error = atm_proc_init(); |
| 874 | if (error < 0) { |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 875 | pr_err("atm_proc_init() failed with %d\n", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | goto out_atmsvc_exit; |
| 877 | } |
Joe Perches | a8147d7 | 2010-01-26 11:40:06 +0000 | [diff] [blame] | 878 | error = atm_sysfs_init(); |
| 879 | if (error < 0) { |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 880 | pr_err("atm_sysfs_init() failed with %d\n", error); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 881 | goto out_atmproc_exit; |
| 882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | out: |
| 884 | return error; |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 885 | out_atmproc_exit: |
| 886 | atm_proc_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | out_atmsvc_exit: |
| 888 | atmsvc_exit(); |
| 889 | out_atmpvc_exit: |
| 890 | atmsvc_exit(); |
| 891 | out_unregister_vcc_proto: |
| 892 | proto_unregister(&vcc_proto); |
| 893 | goto out; |
| 894 | } |
| 895 | |
| 896 | static void __exit atm_exit(void) |
| 897 | { |
| 898 | atm_proc_exit(); |
Roman Kagan | 656d98b | 2006-06-29 12:36:34 -0700 | [diff] [blame] | 899 | atm_sysfs_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | atmsvc_exit(); |
| 901 | atmpvc_exit(); |
| 902 | proto_unregister(&vcc_proto); |
| 903 | } |
| 904 | |
Daniel Walker | 84ff602 | 2007-02-05 18:04:06 -0800 | [diff] [blame] | 905 | subsys_initcall(atm_init); |
| 906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | module_exit(atm_exit); |
| 908 | |
| 909 | MODULE_LICENSE("GPL"); |
| 910 | MODULE_ALIAS_NETPROTO(PF_ATMPVC); |
| 911 | MODULE_ALIAS_NETPROTO(PF_ATMSVC); |