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