blob: ad1d28ae512bcfc66a89f693c4e855ad0580783f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* net/atm/signaling.c - ATM signaling */
2
3/* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
Joe Perches99824462010-01-26 11:40:00 +00005#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
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 Torvalds1da177e2005-04-16 15:20:36 -070021#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 Torvalds1da177e2005-04-16 15:20:36 -070026struct atm_vcc *sigd = NULL;
27#ifdef WAIT_FOR_DEMON
28static DECLARE_WAIT_QUEUE_HEAD(sigd_sleep);
29#endif
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static void sigd_put_skb(struct sk_buff *skb)
32{
33#ifdef WAIT_FOR_DEMON
Joe Perches0ec96e62010-01-26 11:40:17 +000034 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Joe Perches0ec96e62010-01-26 11:40:17 +000036 add_wait_queue(&sigd_sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 while (!sigd) {
38 set_current_state(TASK_UNINTERRUPTIBLE);
Joe Perches99824462010-01-26 11:40:00 +000039 pr_debug("atmsvc: waiting for signaling daemon...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 schedule();
41 }
42 current->state = TASK_RUNNING;
Joe Perches0ec96e62010-01-26 11:40:17 +000043 remove_wait_queue(&sigd_sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#else
45 if (!sigd) {
Joe Perches99824462010-01-26 11:40:00 +000046 pr_debug("atmsvc: no signaling daemon\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 kfree_skb(skb);
48 return;
49 }
50#endif
Joe Perches0ec96e62010-01-26 11:40:17 +000051 atm_force_charge(sigd, skb->truesize);
52 skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 sk_atm(sigd)->sk_data_ready(sk_atm(sigd), skb->len);
54}
55
Joe Perches0ec96e62010-01-26 11:40:17 +000056static void modify_qos(struct atm_vcc *vcc, struct atmsvc_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 struct sk_buff *skb;
59
Joe Perches0ec96e62010-01-26 11:40:17 +000060 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
61 !test_bit(ATM_VF_READY, &vcc->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return;
63 msg->type = as_error;
Joe Perches0ec96e62010-01-26 11:40:17 +000064 if (!vcc->dev->ops->change_qos)
65 msg->reply = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 else {
67 /* should lock VCC */
Joe Perches0ec96e62010-01-26 11:40:17 +000068 msg->reply = vcc->dev->ops->change_qos(vcc, &msg->qos,
69 msg->reply);
70 if (!msg->reply)
71 msg->type = as_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
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 Perches0ec96e62010-01-26 11:40:17 +000077 while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 schedule();
Joe Perches0ec96e62010-01-26 11:40:17 +000079 *(struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg)) = *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 sigd_put_skb(skb);
81}
82
Joe Perches0ec96e62010-01-26 11:40:17 +000083static int sigd_send(struct atm_vcc *vcc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
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 Torvalds1da177e2005-04-16 15:20:36 -070091 vcc = *(struct atm_vcc **) &msg->vcc;
Joe Perches99824462010-01-26 11:40:00 +000092 pr_debug("%d (0x%lx)\n", (int)msg->type, (unsigned long)vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 sk = sk_atm(vcc);
94
95 switch (msg->type) {
Joe Perches0ec96e62010-01-26 11:40:17 +000096 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 Torvalds1da177e2005-04-16 15:20:36 -0700108 break;
Joe Perches0ec96e62010-01-26 11:40:17 +0000109 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 Torvalds1da177e2005-04-16 15:20:36 -0700135as_indicate_complete:
Joe Perches0ec96e62010-01-26 11:40:17 +0000136 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 Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 sk->sk_state_change(sk);
156out:
157 dev_kfree_skb(skb);
158 return 0;
159}
160
Joe Perches0ec96e62010-01-26 11:40:17 +0000161void 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 Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct sk_buff *skb;
167 struct atmsvc_msg *msg;
168 static unsigned session = 0;
169
Joe Perches99824462010-01-26 11:40:00 +0000170 pr_debug("%d (0x%p)\n", (int)type, vcc);
Joe Perches0ec96e62010-01-26 11:40:17 +0000171 while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 schedule();
Joe Perches0ec96e62010-01-26 11:40:17 +0000173 msg = (struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg));
174 memset(msg, 0, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 msg->type = type;
176 *(struct atm_vcc **) &msg->vcc = vcc;
177 *(struct atm_vcc **) &msg->listen_vcc = listen_vcc;
178 msg->reply = reply;
Joe Perches0ec96e62010-01-26 11:40:17 +0000179 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 Torvalds1da177e2005-04-16 15:20:36 -0700189 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 Perches0ec96e62010-01-26 11:40:17 +0000195 if (vcc)
196 set_bit(ATM_VF_REGIS, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Joe Perches0ec96e62010-01-26 11:40:17 +0000199void 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 Torvalds1da177e2005-04-16 15:20:36 -0700202{
Joe Perches0ec96e62010-01-26 11:40:17 +0000203 sigd_enq2(vcc, type, listen_vcc, pvc, svc, vcc ? &vcc->qos : NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* other ISP applications may use "reply" */
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static void purge_vcc(struct atm_vcc *vcc)
208{
209 if (sk_atm(vcc)->sk_family == PF_ATMSVC &&
Chas Williams9301e322005-09-28 16:35:01 -0700210 !test_bit(ATM_VF_META, &vcc->flags)) {
211 set_bit(ATM_VF_RELEASED, &vcc->flags);
212 clear_bit(ATM_VF_REGIS, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 vcc_release_async(vcc, -EUNATCH);
214 }
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static void sigd_close(struct atm_vcc *vcc)
218{
219 struct hlist_node *node;
220 struct sock *s;
221 int i;
222
Joe Perches99824462010-01-26 11:40:00 +0000223 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 sigd = NULL;
225 if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
Joe Perches99824462010-01-26 11:40:00 +0000226 pr_err("closing with requests pending\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
228
229 read_lock(&vcc_sklist_lock);
Joe Perches0ec96e62010-01-26 11:40:17 +0000230 for (i = 0; i < VCC_HTABLE_SIZE; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct hlist_head *head = &vcc_hash[i];
232
233 sk_for_each(s, node, head) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700234 vcc = atm_sk(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Chas Williams9301e322005-09-28 16:35:01 -0700236 purge_vcc(vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 }
239 read_unlock(&vcc_sklist_lock);
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static struct atmdev_ops sigd_dev_ops = {
243 .close = sigd_close,
244 .send = sigd_send
245};
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247static struct atm_dev sigd_dev = {
248 .ops = &sigd_dev_ops,
249 .type = "sig",
250 .number = 999,
Milind Arun Choudhary4ef8d0a2007-04-26 01:37:44 -0700251 .lock = __SPIN_LOCK_UNLOCKED(sigd_dev.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254int sigd_attach(struct atm_vcc *vcc)
255{
Joe Perches0ec96e62010-01-26 11:40:17 +0000256 if (sigd)
257 return -EADDRINUSE;
Joe Perches99824462010-01-26 11:40:00 +0000258 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 sigd = vcc;
260 vcc->dev = &sigd_dev;
261 vcc_insert_socket(sk_atm(vcc));
Joe Perches0ec96e62010-01-26 11:40:17 +0000262 set_bit(ATM_VF_META, &vcc->flags);
263 set_bit(ATM_VF_READY, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#ifdef WAIT_FOR_DEMON
265 wake_up(&sigd_sleep);
266#endif
267 return 0;
268}