blob: 509c8ac02b63cdb91677049d6c304ee4c9f10399 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "resources.h"
20#include "signaling.h"
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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 Torvalds1da177e2005-04-16 15:20:36 -070027struct atm_vcc *sigd = NULL;
28#ifdef WAIT_FOR_DEMON
29static DECLARE_WAIT_QUEUE_HEAD(sigd_sleep);
30#endif
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static void sigd_put_skb(struct sk_buff *skb)
33{
34#ifdef WAIT_FOR_DEMON
Joe Perches0ec96e62010-01-26 11:40:17 +000035 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Joe Perches0ec96e62010-01-26 11:40:17 +000037 add_wait_queue(&sigd_sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 while (!sigd) {
39 set_current_state(TASK_UNINTERRUPTIBLE);
Joe Perches99824462010-01-26 11:40:00 +000040 pr_debug("atmsvc: waiting for signaling daemon...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 schedule();
42 }
43 current->state = TASK_RUNNING;
Joe Perches0ec96e62010-01-26 11:40:17 +000044 remove_wait_queue(&sigd_sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#else
46 if (!sigd) {
Joe Perches99824462010-01-26 11:40:00 +000047 pr_debug("atmsvc: no signaling daemon\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 kfree_skb(skb);
49 return;
50 }
51#endif
Joe Perches0ec96e62010-01-26 11:40:17 +000052 atm_force_charge(sigd, skb->truesize);
53 skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 sk_atm(sigd)->sk_data_ready(sk_atm(sigd), skb->len);
55}
56
Joe Perches0ec96e62010-01-26 11:40:17 +000057static void modify_qos(struct atm_vcc *vcc, struct atmsvc_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 struct sk_buff *skb;
60
Joe Perches0ec96e62010-01-26 11:40:17 +000061 if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
62 !test_bit(ATM_VF_READY, &vcc->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return;
64 msg->type = as_error;
Joe Perches0ec96e62010-01-26 11:40:17 +000065 if (!vcc->dev->ops->change_qos)
66 msg->reply = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 else {
68 /* should lock VCC */
Joe Perches0ec96e62010-01-26 11:40:17 +000069 msg->reply = vcc->dev->ops->change_qos(vcc, &msg->qos,
70 msg->reply);
71 if (!msg->reply)
72 msg->type = as_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
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 Perches0ec96e62010-01-26 11:40:17 +000078 while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 schedule();
Joe Perches0ec96e62010-01-26 11:40:17 +000080 *(struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg)) = *msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 sigd_put_skb(skb);
82}
83
Joe Perches0ec96e62010-01-26 11:40:17 +000084static int sigd_send(struct atm_vcc *vcc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
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 Torvalds1da177e2005-04-16 15:20:36 -070092 vcc = *(struct atm_vcc **) &msg->vcc;
Joe Perches99824462010-01-26 11:40:00 +000093 pr_debug("%d (0x%lx)\n", (int)msg->type, (unsigned long)vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 sk = sk_atm(vcc);
95
96 switch (msg->type) {
Joe Perches0ec96e62010-01-26 11:40:17 +000097 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 Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
Joe Perches0ec96e62010-01-26 11:40:17 +0000110 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 Dumazetaa395142010-04-20 13:03:51 +0000134 pr_debug("waking sk_sleep(sk) 0x%p\n", sk_sleep(sk));
Joe Perches0ec96e62010-01-26 11:40:17 +0000135 sk->sk_state_change(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136as_indicate_complete:
Joe Perches0ec96e62010-01-26 11:40:17 +0000137 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 Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 sk->sk_state_change(sk);
157out:
158 dev_kfree_skb(skb);
159 return 0;
160}
161
Joe Perches0ec96e62010-01-26 11:40:17 +0000162void 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 Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct sk_buff *skb;
168 struct atmsvc_msg *msg;
169 static unsigned session = 0;
170
Joe Perches99824462010-01-26 11:40:00 +0000171 pr_debug("%d (0x%p)\n", (int)type, vcc);
Joe Perches0ec96e62010-01-26 11:40:17 +0000172 while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 schedule();
Joe Perches0ec96e62010-01-26 11:40:17 +0000174 msg = (struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg));
175 memset(msg, 0, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 msg->type = type;
177 *(struct atm_vcc **) &msg->vcc = vcc;
178 *(struct atm_vcc **) &msg->listen_vcc = listen_vcc;
179 msg->reply = reply;
Joe Perches0ec96e62010-01-26 11:40:17 +0000180 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 Torvalds1da177e2005-04-16 15:20:36 -0700190 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 Perches0ec96e62010-01-26 11:40:17 +0000196 if (vcc)
197 set_bit(ATM_VF_REGIS, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Joe Perches0ec96e62010-01-26 11:40:17 +0000200void 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 Torvalds1da177e2005-04-16 15:20:36 -0700203{
Joe Perches0ec96e62010-01-26 11:40:17 +0000204 sigd_enq2(vcc, type, listen_vcc, pvc, svc, vcc ? &vcc->qos : NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* other ISP applications may use "reply" */
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static void purge_vcc(struct atm_vcc *vcc)
209{
210 if (sk_atm(vcc)->sk_family == PF_ATMSVC &&
Chas Williams9301e322005-09-28 16:35:01 -0700211 !test_bit(ATM_VF_META, &vcc->flags)) {
212 set_bit(ATM_VF_RELEASED, &vcc->flags);
213 clear_bit(ATM_VF_REGIS, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 vcc_release_async(vcc, -EUNATCH);
215 }
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218static void sigd_close(struct atm_vcc *vcc)
219{
220 struct hlist_node *node;
221 struct sock *s;
222 int i;
223
Joe Perches99824462010-01-26 11:40:00 +0000224 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 sigd = NULL;
226 if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
Joe Perches99824462010-01-26 11:40:00 +0000227 pr_err("closing with requests pending\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
229
230 read_lock(&vcc_sklist_lock);
Joe Perches0ec96e62010-01-26 11:40:17 +0000231 for (i = 0; i < VCC_HTABLE_SIZE; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct hlist_head *head = &vcc_hash[i];
233
234 sk_for_each(s, node, head) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700235 vcc = atm_sk(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Chas Williams9301e322005-09-28 16:35:01 -0700237 purge_vcc(vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 }
240 read_unlock(&vcc_sklist_lock);
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static struct atmdev_ops sigd_dev_ops = {
244 .close = sigd_close,
245 .send = sigd_send
246};
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static struct atm_dev sigd_dev = {
249 .ops = &sigd_dev_ops,
250 .type = "sig",
251 .number = 999,
Milind Arun Choudhary4ef8d0a2007-04-26 01:37:44 -0700252 .lock = __SPIN_LOCK_UNLOCKED(sigd_dev.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253};
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255int sigd_attach(struct atm_vcc *vcc)
256{
Joe Perches0ec96e62010-01-26 11:40:17 +0000257 if (sigd)
258 return -EADDRINUSE;
Joe Perches99824462010-01-26 11:40:00 +0000259 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 sigd = vcc;
261 vcc->dev = &sigd_dev;
262 vcc_insert_socket(sk_atm(vcc));
Joe Perches0ec96e62010-01-26 11:40:17 +0000263 set_bit(ATM_VF_META, &vcc->flags);
264 set_bit(ATM_VF_READY, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#ifdef WAIT_FOR_DEMON
266 wake_up(&sigd_sleep);
267#endif
268 return 0;
269}