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