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