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