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