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