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