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 | #undef WAIT_FOR_DEMON /* #define this if system calls on SVC sockets |
| 23 | should block until the demon runs. |
| 24 | Danger: may cause nasty hangs if the demon |
| 25 | crashes. */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | struct atm_vcc *sigd = NULL; |
| 28 | #ifdef WAIT_FOR_DEMON |
| 29 | static DECLARE_WAIT_QUEUE_HEAD(sigd_sleep); |
| 30 | #endif |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static void sigd_put_skb(struct sk_buff *skb) |
| 33 | { |
| 34 | #ifdef WAIT_FOR_DEMON |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 35 | DECLARE_WAITQUEUE(wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 37 | add_wait_queue(&sigd_sleep, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | while (!sigd) { |
| 39 | set_current_state(TASK_UNINTERRUPTIBLE); |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 40 | pr_debug("atmsvc: waiting for signaling daemon...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | schedule(); |
| 42 | } |
| 43 | current->state = TASK_RUNNING; |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 44 | remove_wait_queue(&sigd_sleep, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #else |
| 46 | if (!sigd) { |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 47 | pr_debug("atmsvc: no signaling daemon\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | kfree_skb(skb); |
| 49 | return; |
| 50 | } |
| 51 | #endif |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 52 | atm_force_charge(sigd, skb->truesize); |
| 53 | skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | sk_atm(sigd)->sk_data_ready(sk_atm(sigd), skb->len); |
| 55 | } |
| 56 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 57 | static void modify_qos(struct atm_vcc *vcc, struct atmsvc_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | struct sk_buff *skb; |
| 60 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 61 | if (test_bit(ATM_VF_RELEASED, &vcc->flags) || |
| 62 | !test_bit(ATM_VF_READY, &vcc->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return; |
| 64 | msg->type = as_error; |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 65 | if (!vcc->dev->ops->change_qos) |
| 66 | msg->reply = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | else { |
| 68 | /* should lock VCC */ |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 69 | msg->reply = vcc->dev->ops->change_qos(vcc, &msg->qos, |
| 70 | msg->reply); |
| 71 | if (!msg->reply) |
| 72 | msg->type = as_okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | /* |
| 75 | * Should probably just turn around the old skb. But the, the buffer |
| 76 | * space accounting needs to follow the change too. Maybe later. |
| 77 | */ |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 78 | while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | schedule(); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 80 | *(struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg)) = *msg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | sigd_put_skb(skb); |
| 82 | } |
| 83 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 84 | static int sigd_send(struct atm_vcc *vcc, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | struct atmsvc_msg *msg; |
| 87 | struct atm_vcc *session_vcc; |
| 88 | struct sock *sk; |
| 89 | |
| 90 | msg = (struct atmsvc_msg *) skb->data; |
| 91 | atomic_sub(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | vcc = *(struct atm_vcc **) &msg->vcc; |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 93 | pr_debug("%d (0x%lx)\n", (int)msg->type, (unsigned long)vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | sk = sk_atm(vcc); |
| 95 | |
| 96 | switch (msg->type) { |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 97 | case as_okay: |
| 98 | sk->sk_err = -msg->reply; |
| 99 | clear_bit(ATM_VF_WAITING, &vcc->flags); |
| 100 | if (!*vcc->local.sas_addr.prv && !*vcc->local.sas_addr.pub) { |
| 101 | vcc->local.sas_family = AF_ATMSVC; |
| 102 | memcpy(vcc->local.sas_addr.prv, |
| 103 | msg->local.sas_addr.prv, ATM_ESA_LEN); |
| 104 | memcpy(vcc->local.sas_addr.pub, |
| 105 | msg->local.sas_addr.pub, ATM_E164_LEN + 1); |
| 106 | } |
| 107 | session_vcc = vcc->session ? vcc->session : vcc; |
| 108 | if (session_vcc->vpi || session_vcc->vci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | break; |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 110 | session_vcc->itf = msg->pvc.sap_addr.itf; |
| 111 | session_vcc->vpi = msg->pvc.sap_addr.vpi; |
| 112 | session_vcc->vci = msg->pvc.sap_addr.vci; |
| 113 | if (session_vcc->vpi || session_vcc->vci) |
| 114 | session_vcc->qos = msg->qos; |
| 115 | break; |
| 116 | case as_error: |
| 117 | clear_bit(ATM_VF_REGIS, &vcc->flags); |
| 118 | clear_bit(ATM_VF_READY, &vcc->flags); |
| 119 | sk->sk_err = -msg->reply; |
| 120 | clear_bit(ATM_VF_WAITING, &vcc->flags); |
| 121 | break; |
| 122 | case as_indicate: |
| 123 | vcc = *(struct atm_vcc **)&msg->listen_vcc; |
| 124 | sk = sk_atm(vcc); |
| 125 | pr_debug("as_indicate!!!\n"); |
| 126 | lock_sock(sk); |
| 127 | if (sk_acceptq_is_full(sk)) { |
| 128 | sigd_enq(NULL, as_reject, vcc, NULL, NULL); |
| 129 | dev_kfree_skb(skb); |
| 130 | goto as_indicate_complete; |
| 131 | } |
| 132 | sk->sk_ack_backlog++; |
| 133 | skb_queue_tail(&sk->sk_receive_queue, skb); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 134 | pr_debug("waking sk_sleep(sk) 0x%p\n", sk_sleep(sk)); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 135 | sk->sk_state_change(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | as_indicate_complete: |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 137 | release_sock(sk); |
| 138 | return 0; |
| 139 | case as_close: |
| 140 | set_bit(ATM_VF_RELEASED, &vcc->flags); |
| 141 | vcc_release_async(vcc, msg->reply); |
| 142 | goto out; |
| 143 | case as_modify: |
| 144 | modify_qos(vcc, msg); |
| 145 | break; |
| 146 | case as_addparty: |
| 147 | case as_dropparty: |
| 148 | sk->sk_err_soft = msg->reply; |
| 149 | /* < 0 failure, otherwise ep_ref */ |
| 150 | clear_bit(ATM_VF_WAITING, &vcc->flags); |
| 151 | break; |
| 152 | default: |
| 153 | pr_alert("bad message type %d\n", (int)msg->type); |
| 154 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | sk->sk_state_change(sk); |
| 157 | out: |
| 158 | dev_kfree_skb(skb); |
| 159 | return 0; |
| 160 | } |
| 161 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 162 | void sigd_enq2(struct atm_vcc *vcc, enum atmsvc_msg_type type, |
| 163 | struct atm_vcc *listen_vcc, const struct sockaddr_atmpvc *pvc, |
| 164 | const struct sockaddr_atmsvc *svc, const struct atm_qos *qos, |
| 165 | int reply) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
| 167 | struct sk_buff *skb; |
| 168 | struct atmsvc_msg *msg; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 169 | static unsigned int session = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 171 | pr_debug("%d (0x%p)\n", (int)type, vcc); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 172 | while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | schedule(); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 174 | msg = (struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg)); |
| 175 | memset(msg, 0, sizeof(*msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | msg->type = type; |
| 177 | *(struct atm_vcc **) &msg->vcc = vcc; |
| 178 | *(struct atm_vcc **) &msg->listen_vcc = listen_vcc; |
| 179 | msg->reply = reply; |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 180 | if (qos) |
| 181 | msg->qos = *qos; |
| 182 | if (vcc) |
| 183 | msg->sap = vcc->sap; |
| 184 | if (svc) |
| 185 | msg->svc = *svc; |
| 186 | if (vcc) |
| 187 | msg->local = vcc->local; |
| 188 | if (pvc) |
| 189 | msg->pvc = *pvc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (vcc) { |
| 191 | if (type == as_connect && test_bit(ATM_VF_SESSION, &vcc->flags)) |
| 192 | msg->session = ++session; |
| 193 | /* every new pmp connect gets the next session number */ |
| 194 | } |
| 195 | sigd_put_skb(skb); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 196 | if (vcc) |
| 197 | set_bit(ATM_VF_REGIS, &vcc->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 200 | void sigd_enq(struct atm_vcc *vcc, enum atmsvc_msg_type type, |
| 201 | struct atm_vcc *listen_vcc, const struct sockaddr_atmpvc *pvc, |
| 202 | const struct sockaddr_atmsvc *svc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 204 | 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] | 205 | /* other ISP applications may use "reply" */ |
| 206 | } |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | static void purge_vcc(struct atm_vcc *vcc) |
| 209 | { |
| 210 | if (sk_atm(vcc)->sk_family == PF_ATMSVC && |
Chas Williams | 9301e32 | 2005-09-28 16:35:01 -0700 | [diff] [blame] | 211 | !test_bit(ATM_VF_META, &vcc->flags)) { |
| 212 | set_bit(ATM_VF_RELEASED, &vcc->flags); |
| 213 | clear_bit(ATM_VF_REGIS, &vcc->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | vcc_release_async(vcc, -EUNATCH); |
| 215 | } |
| 216 | } |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | static void sigd_close(struct atm_vcc *vcc) |
| 219 | { |
| 220 | struct hlist_node *node; |
| 221 | struct sock *s; |
| 222 | int i; |
| 223 | |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 224 | pr_debug("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | sigd = NULL; |
| 226 | if (skb_peek(&sk_atm(vcc)->sk_receive_queue)) |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 227 | pr_err("closing with requests pending\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | skb_queue_purge(&sk_atm(vcc)->sk_receive_queue); |
| 229 | |
| 230 | read_lock(&vcc_sklist_lock); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 231 | for (i = 0; i < VCC_HTABLE_SIZE; ++i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | struct hlist_head *head = &vcc_hash[i]; |
| 233 | |
| 234 | sk_for_each(s, node, head) { |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 235 | vcc = atm_sk(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Chas Williams | 9301e32 | 2005-09-28 16:35:01 -0700 | [diff] [blame] | 237 | purge_vcc(vcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | read_unlock(&vcc_sklist_lock); |
| 241 | } |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | static struct atmdev_ops sigd_dev_ops = { |
| 244 | .close = sigd_close, |
| 245 | .send = sigd_send |
| 246 | }; |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | static struct atm_dev sigd_dev = { |
| 249 | .ops = &sigd_dev_ops, |
| 250 | .type = "sig", |
| 251 | .number = 999, |
Milind Arun Choudhary | 4ef8d0a | 2007-04-26 01:37:44 -0700 | [diff] [blame] | 252 | .lock = __SPIN_LOCK_UNLOCKED(sigd_dev.lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | int sigd_attach(struct atm_vcc *vcc) |
| 256 | { |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 257 | if (sigd) |
| 258 | return -EADDRINUSE; |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 259 | pr_debug("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | sigd = vcc; |
| 261 | vcc->dev = &sigd_dev; |
| 262 | vcc_insert_socket(sk_atm(vcc)); |
Joe Perches | 0ec96e6 | 2010-01-26 11:40:17 +0000 | [diff] [blame] | 263 | set_bit(ATM_VF_META, &vcc->flags); |
| 264 | set_bit(ATM_VF_READY, &vcc->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | #ifdef WAIT_FOR_DEMON |
| 266 | wake_up(&sigd_sleep); |
| 267 | #endif |
| 268 | return 0; |
| 269 | } |