Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* net/atm/signaling.c - ATM signaling */ |
| 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 | |
| 8 | #include <linux/errno.h> /* error codes */ |
| 9 | #include <linux/kernel.h> /* printk */ |
| 10 | #include <linux/skbuff.h> |
| 11 | #include <linux/wait.h> |
| 12 | #include <linux/sched.h> /* jiffies and HZ */ |
| 13 | #include <linux/atm.h> /* ATM stuff */ |
| 14 | #include <linux/atmsap.h> |
| 15 | #include <linux/atmsvc.h> |
| 16 | #include <linux/atmdev.h> |
| 17 | #include <linux/bitops.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include "resources.h" |
| 21 | #include "signaling.h" |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | struct atm_vcc *sigd = NULL; |
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 | static void sigd_put_skb(struct sk_buff *skb) |
| 26 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | if (!sigd) { |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 28 | pr_debug("atmsvc: no signaling daemon\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | kfree_skb(skb); |
| 30 | return; |
| 31 | } |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 32 | atm_force_charge(sigd, skb->truesize); |
| 33 | skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb); |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 34 | sk_atm(sigd)->sk_data_ready(sk_atm(sigd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 37 | static void modify_qos(struct atm_vcc *vcc, struct atmsvc_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
| 39 | struct sk_buff *skb; |
| 40 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 41 | if (test_bit(ATM_VF_RELEASED, &vcc->flags) || |
| 42 | !test_bit(ATM_VF_READY, &vcc->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | return; |
| 44 | msg->type = as_error; |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 45 | if (!vcc->dev->ops->change_qos) |
| 46 | msg->reply = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | else { |
| 48 | /* should lock VCC */ |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 49 | msg->reply = vcc->dev->ops->change_qos(vcc, &msg->qos, |
| 50 | msg->reply); |
| 51 | if (!msg->reply) |
| 52 | msg->type = as_okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | /* |
Randy Dunlap | 563f63e | 2020-09-17 21:35:20 -0700 | [diff] [blame] | 55 | * Should probably just turn around the old skb. But then, the buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * space accounting needs to follow the change too. Maybe later. |
| 57 | */ |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 58 | while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | schedule(); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 60 | *(struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg)) = *msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | sigd_put_skb(skb); |
| 62 | } |
| 63 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 64 | static int sigd_send(struct atm_vcc *vcc, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
| 66 | struct atmsvc_msg *msg; |
| 67 | struct atm_vcc *session_vcc; |
| 68 | struct sock *sk; |
| 69 | |
| 70 | msg = (struct atmsvc_msg *) skb->data; |
Reshetova, Elena | 14afee4 | 2017-06-30 13:08:00 +0300 | [diff] [blame] | 71 | WARN_ON(refcount_sub_and_test(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | vcc = *(struct atm_vcc **) &msg->vcc; |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 73 | pr_debug("%d (0x%lx)\n", (int)msg->type, (unsigned long)vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | sk = sk_atm(vcc); |
| 75 | |
| 76 | switch (msg->type) { |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 77 | case as_okay: |
| 78 | sk->sk_err = -msg->reply; |
| 79 | clear_bit(ATM_VF_WAITING, &vcc->flags); |
| 80 | if (!*vcc->local.sas_addr.prv && !*vcc->local.sas_addr.pub) { |
| 81 | vcc->local.sas_family = AF_ATMSVC; |
| 82 | memcpy(vcc->local.sas_addr.prv, |
| 83 | msg->local.sas_addr.prv, ATM_ESA_LEN); |
| 84 | memcpy(vcc->local.sas_addr.pub, |
| 85 | msg->local.sas_addr.pub, ATM_E164_LEN + 1); |
| 86 | } |
| 87 | session_vcc = vcc->session ? vcc->session : vcc; |
| 88 | if (session_vcc->vpi || session_vcc->vci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | break; |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 90 | session_vcc->itf = msg->pvc.sap_addr.itf; |
| 91 | session_vcc->vpi = msg->pvc.sap_addr.vpi; |
| 92 | session_vcc->vci = msg->pvc.sap_addr.vci; |
| 93 | if (session_vcc->vpi || session_vcc->vci) |
| 94 | session_vcc->qos = msg->qos; |
| 95 | break; |
| 96 | case as_error: |
| 97 | clear_bit(ATM_VF_REGIS, &vcc->flags); |
| 98 | clear_bit(ATM_VF_READY, &vcc->flags); |
| 99 | sk->sk_err = -msg->reply; |
| 100 | clear_bit(ATM_VF_WAITING, &vcc->flags); |
| 101 | break; |
| 102 | case as_indicate: |
| 103 | vcc = *(struct atm_vcc **)&msg->listen_vcc; |
| 104 | sk = sk_atm(vcc); |
| 105 | pr_debug("as_indicate!!!\n"); |
| 106 | lock_sock(sk); |
| 107 | if (sk_acceptq_is_full(sk)) { |
| 108 | sigd_enq(NULL, as_reject, vcc, NULL, NULL); |
| 109 | dev_kfree_skb(skb); |
| 110 | goto as_indicate_complete; |
| 111 | } |
Eric Dumazet | 7976a11 | 2019-11-05 14:11:52 -0800 | [diff] [blame] | 112 | sk_acceptq_added(sk); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 113 | skb_queue_tail(&sk->sk_receive_queue, skb); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 114 | pr_debug("waking sk_sleep(sk) 0x%p\n", sk_sleep(sk)); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 115 | sk->sk_state_change(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | as_indicate_complete: |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 117 | release_sock(sk); |
| 118 | return 0; |
| 119 | case as_close: |
| 120 | set_bit(ATM_VF_RELEASED, &vcc->flags); |
| 121 | vcc_release_async(vcc, msg->reply); |
| 122 | goto out; |
| 123 | case as_modify: |
| 124 | modify_qos(vcc, msg); |
| 125 | break; |
| 126 | case as_addparty: |
| 127 | case as_dropparty: |
Stefan Hajnoczi | c685293 | 2016-05-18 17:42:13 -0700 | [diff] [blame] | 128 | sk->sk_err_soft = -msg->reply; |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 129 | /* < 0 failure, otherwise ep_ref */ |
| 130 | clear_bit(ATM_VF_WAITING, &vcc->flags); |
| 131 | break; |
| 132 | default: |
| 133 | pr_alert("bad message type %d\n", (int)msg->type); |
| 134 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | sk->sk_state_change(sk); |
| 137 | out: |
| 138 | dev_kfree_skb(skb); |
| 139 | return 0; |
| 140 | } |
| 141 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 142 | void sigd_enq2(struct atm_vcc *vcc, enum atmsvc_msg_type type, |
| 143 | struct atm_vcc *listen_vcc, const struct sockaddr_atmpvc *pvc, |
| 144 | const struct sockaddr_atmsvc *svc, const struct atm_qos *qos, |
| 145 | int reply) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | struct sk_buff *skb; |
| 148 | struct atmsvc_msg *msg; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 149 | static unsigned int session = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 151 | pr_debug("%d (0x%p)\n", (int)type, vcc); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 152 | while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | schedule(); |
Johannes Berg | b080db5 | 2017-06-16 14:29:19 +0200 | [diff] [blame] | 154 | msg = skb_put_zero(skb, sizeof(struct atmsvc_msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | msg->type = type; |
| 156 | *(struct atm_vcc **) &msg->vcc = vcc; |
| 157 | *(struct atm_vcc **) &msg->listen_vcc = listen_vcc; |
| 158 | msg->reply = reply; |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 159 | if (qos) |
| 160 | msg->qos = *qos; |
| 161 | if (vcc) |
| 162 | msg->sap = vcc->sap; |
| 163 | if (svc) |
| 164 | msg->svc = *svc; |
| 165 | if (vcc) |
| 166 | msg->local = vcc->local; |
| 167 | if (pvc) |
| 168 | msg->pvc = *pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | if (vcc) { |
| 170 | if (type == as_connect && test_bit(ATM_VF_SESSION, &vcc->flags)) |
| 171 | msg->session = ++session; |
| 172 | /* every new pmp connect gets the next session number */ |
| 173 | } |
| 174 | sigd_put_skb(skb); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 175 | if (vcc) |
| 176 | set_bit(ATM_VF_REGIS, &vcc->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 179 | void sigd_enq(struct atm_vcc *vcc, enum atmsvc_msg_type type, |
| 180 | struct atm_vcc *listen_vcc, const struct sockaddr_atmpvc *pvc, |
| 181 | const struct sockaddr_atmsvc *svc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 183 | sigd_enq2(vcc, type, listen_vcc, pvc, svc, vcc ? &vcc->qos : NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | /* other ISP applications may use "reply" */ |
| 185 | } |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | static void purge_vcc(struct atm_vcc *vcc) |
| 188 | { |
| 189 | if (sk_atm(vcc)->sk_family == PF_ATMSVC && |
Chas Williams | 9301e32 | 2005-09-28 16:35:01 -0700 | [diff] [blame] | 190 | !test_bit(ATM_VF_META, &vcc->flags)) { |
| 191 | set_bit(ATM_VF_RELEASED, &vcc->flags); |
| 192 | clear_bit(ATM_VF_REGIS, &vcc->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | vcc_release_async(vcc, -EUNATCH); |
| 194 | } |
| 195 | } |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | static void sigd_close(struct atm_vcc *vcc) |
| 198 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | struct sock *s; |
| 200 | int i; |
| 201 | |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 202 | pr_debug("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | sigd = NULL; |
| 204 | if (skb_peek(&sk_atm(vcc)->sk_receive_queue)) |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 205 | pr_err("closing with requests pending\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | skb_queue_purge(&sk_atm(vcc)->sk_receive_queue); |
| 207 | |
| 208 | read_lock(&vcc_sklist_lock); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 209 | for (i = 0; i < VCC_HTABLE_SIZE; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | struct hlist_head *head = &vcc_hash[i]; |
| 211 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 212 | sk_for_each(s, head) { |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 213 | vcc = atm_sk(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Chas Williams | 9301e32 | 2005-09-28 16:35:01 -0700 | [diff] [blame] | 215 | purge_vcc(vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | read_unlock(&vcc_sklist_lock); |
| 219 | } |
| 220 | |
Bhumika Goyal | 800bb47 | 2017-08-09 15:02:08 +0530 | [diff] [blame] | 221 | static const struct atmdev_ops sigd_dev_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | .close = sigd_close, |
| 223 | .send = sigd_send |
| 224 | }; |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | static struct atm_dev sigd_dev = { |
| 227 | .ops = &sigd_dev_ops, |
| 228 | .type = "sig", |
| 229 | .number = 999, |
Milind Arun Choudhary | 4ef8d0a | 2007-04-26 01:37:44 -0700 | [diff] [blame] | 230 | .lock = __SPIN_LOCK_UNLOCKED(sigd_dev.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | int sigd_attach(struct atm_vcc *vcc) |
| 234 | { |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 235 | if (sigd) |
| 236 | return -EADDRINUSE; |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 237 | pr_debug("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | sigd = vcc; |
| 239 | vcc->dev = &sigd_dev; |
| 240 | vcc_insert_socket(sk_atm(vcc)); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 241 | set_bit(ATM_VF_META, &vcc->flags); |
| 242 | set_bit(ATM_VF_READY, &vcc->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return 0; |
| 244 | } |