blob: 28df4edf9ca4d182f08ed63ea7bcdf40b2a4e6b8 [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
21
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 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
32
33static void sigd_put_skb(struct sk_buff *skb)
34{
35#ifdef WAIT_FOR_DEMON
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 DECLARE_WAITQUEUE(wait,current);
37
38 add_wait_queue(&sigd_sleep,&wait);
39 while (!sigd) {
40 set_current_state(TASK_UNINTERRUPTIBLE);
Joe Perches99824462010-01-26 11:40:00 +000041 pr_debug("atmsvc: waiting for signaling daemon...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 schedule();
43 }
44 current->state = TASK_RUNNING;
45 remove_wait_queue(&sigd_sleep,&wait);
46#else
47 if (!sigd) {
Joe Perches99824462010-01-26 11:40:00 +000048 pr_debug("atmsvc: no signaling daemon\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 kfree_skb(skb);
50 return;
51 }
52#endif
53 atm_force_charge(sigd,skb->truesize);
54 skb_queue_tail(&sk_atm(sigd)->sk_receive_queue,skb);
55 sk_atm(sigd)->sk_data_ready(sk_atm(sigd), skb->len);
56}
57
58
59static void modify_qos(struct atm_vcc *vcc,struct atmsvc_msg *msg)
60{
61 struct sk_buff *skb;
62
63 if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
64 !test_bit(ATM_VF_READY,&vcc->flags))
65 return;
66 msg->type = as_error;
67 if (!vcc->dev->ops->change_qos) msg->reply = -EOPNOTSUPP;
68 else {
69 /* should lock VCC */
70 msg->reply = vcc->dev->ops->change_qos(vcc,&msg->qos,
71 msg->reply);
72 if (!msg->reply) msg->type = as_okay;
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 */
78 while (!(skb = alloc_skb(sizeof(struct atmsvc_msg),GFP_KERNEL)))
79 schedule();
80 *(struct atmsvc_msg *) skb_put(skb,sizeof(struct atmsvc_msg)) = *msg;
81 sigd_put_skb(skb);
82}
83
84
85static int sigd_send(struct atm_vcc *vcc,struct sk_buff *skb)
86{
87 struct atmsvc_msg *msg;
88 struct atm_vcc *session_vcc;
89 struct sock *sk;
90
91 msg = (struct atmsvc_msg *) skb->data;
92 atomic_sub(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 vcc = *(struct atm_vcc **) &msg->vcc;
Joe Perches99824462010-01-26 11:40:00 +000094 pr_debug("%d (0x%lx)\n", (int)msg->type, (unsigned long)vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 sk = sk_atm(vcc);
96
97 switch (msg->type) {
98 case as_okay:
99 sk->sk_err = -msg->reply;
100 clear_bit(ATM_VF_WAITING, &vcc->flags);
101 if (!*vcc->local.sas_addr.prv &&
102 !*vcc->local.sas_addr.pub) {
103 vcc->local.sas_family = AF_ATMSVC;
104 memcpy(vcc->local.sas_addr.prv,
105 msg->local.sas_addr.prv,ATM_ESA_LEN);
106 memcpy(vcc->local.sas_addr.pub,
107 msg->local.sas_addr.pub,ATM_E164_LEN+1);
108 }
109 session_vcc = vcc->session ? vcc->session : vcc;
110 if (session_vcc->vpi || session_vcc->vci) break;
111 session_vcc->itf = msg->pvc.sap_addr.itf;
112 session_vcc->vpi = msg->pvc.sap_addr.vpi;
113 session_vcc->vci = msg->pvc.sap_addr.vci;
114 if (session_vcc->vpi || session_vcc->vci)
115 session_vcc->qos = msg->qos;
116 break;
117 case as_error:
118 clear_bit(ATM_VF_REGIS,&vcc->flags);
119 clear_bit(ATM_VF_READY,&vcc->flags);
120 sk->sk_err = -msg->reply;
121 clear_bit(ATM_VF_WAITING, &vcc->flags);
122 break;
123 case as_indicate:
124 vcc = *(struct atm_vcc **) &msg->listen_vcc;
Chas Williams208a6df2005-04-21 16:44:57 -0700125 sk = sk_atm(vcc);
Stephen Hemminger52240062007-08-28 15:22:09 -0700126 pr_debug("as_indicate!!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 lock_sock(sk);
128 if (sk_acceptq_is_full(sk)) {
129 sigd_enq(NULL,as_reject,vcc,NULL,NULL);
130 dev_kfree_skb(skb);
131 goto as_indicate_complete;
132 }
133 sk->sk_ack_backlog++;
134 skb_queue_tail(&sk->sk_receive_queue, skb);
Stephen Hemminger52240062007-08-28 15:22:09 -0700135 pr_debug("waking sk->sk_sleep 0x%p\n", sk->sk_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 sk->sk_state_change(sk);
137as_indicate_complete:
138 release_sock(sk);
139 return 0;
140 case as_close:
141 set_bit(ATM_VF_RELEASED,&vcc->flags);
142 vcc_release_async(vcc, msg->reply);
143 goto out;
144 case as_modify:
145 modify_qos(vcc,msg);
146 break;
147 case as_addparty:
148 case as_dropparty:
149 sk->sk_err_soft = msg->reply; /* < 0 failure, otherwise ep_ref */
150 clear_bit(ATM_VF_WAITING, &vcc->flags);
151 break;
152 default:
Joe Perches99824462010-01-26 11:40:00 +0000153 pr_alert("bad message type %d\n", (int)msg->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return -EINVAL;
155 }
156 sk->sk_state_change(sk);
157out:
158 dev_kfree_skb(skb);
159 return 0;
160}
161
162
163void sigd_enq2(struct atm_vcc *vcc,enum atmsvc_msg_type type,
164 struct atm_vcc *listen_vcc,const struct sockaddr_atmpvc *pvc,
165 const struct sockaddr_atmsvc *svc,const struct atm_qos *qos,int reply)
166{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 while (!(skb = alloc_skb(sizeof(struct atmsvc_msg),GFP_KERNEL)))
173 schedule();
174 msg = (struct atmsvc_msg *) skb_put(skb,sizeof(struct atmsvc_msg));
175 memset(msg,0,sizeof(*msg));
176 msg->type = type;
177 *(struct atm_vcc **) &msg->vcc = vcc;
178 *(struct atm_vcc **) &msg->listen_vcc = listen_vcc;
179 msg->reply = reply;
180 if (qos) msg->qos = *qos;
181 if (vcc) msg->sap = vcc->sap;
182 if (svc) msg->svc = *svc;
183 if (vcc) msg->local = vcc->local;
184 if (pvc) msg->pvc = *pvc;
185 if (vcc) {
186 if (type == as_connect && test_bit(ATM_VF_SESSION, &vcc->flags))
187 msg->session = ++session;
188 /* every new pmp connect gets the next session number */
189 }
190 sigd_put_skb(skb);
191 if (vcc) set_bit(ATM_VF_REGIS,&vcc->flags);
192}
193
194
195void sigd_enq(struct atm_vcc *vcc,enum atmsvc_msg_type type,
196 struct atm_vcc *listen_vcc,const struct sockaddr_atmpvc *pvc,
197 const struct sockaddr_atmsvc *svc)
198{
199 sigd_enq2(vcc,type,listen_vcc,pvc,svc,vcc ? &vcc->qos : NULL,0);
200 /* other ISP applications may use "reply" */
201}
202
203
204static void purge_vcc(struct atm_vcc *vcc)
205{
206 if (sk_atm(vcc)->sk_family == PF_ATMSVC &&
Chas Williams9301e322005-09-28 16:35:01 -0700207 !test_bit(ATM_VF_META, &vcc->flags)) {
208 set_bit(ATM_VF_RELEASED, &vcc->flags);
209 clear_bit(ATM_VF_REGIS, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 vcc_release_async(vcc, -EUNATCH);
211 }
212}
213
214
215static void sigd_close(struct atm_vcc *vcc)
216{
217 struct hlist_node *node;
218 struct sock *s;
219 int i;
220
Joe Perches99824462010-01-26 11:40:00 +0000221 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 sigd = NULL;
223 if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
Joe Perches99824462010-01-26 11:40:00 +0000224 pr_err("closing with requests pending\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
226
227 read_lock(&vcc_sklist_lock);
228 for(i = 0; i < VCC_HTABLE_SIZE; ++i) {
229 struct hlist_head *head = &vcc_hash[i];
230
231 sk_for_each(s, node, head) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700232 vcc = atm_sk(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Chas Williams9301e322005-09-28 16:35:01 -0700234 purge_vcc(vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 }
237 read_unlock(&vcc_sklist_lock);
238}
239
240
241static struct atmdev_ops sigd_dev_ops = {
242 .close = sigd_close,
243 .send = sigd_send
244};
245
246
247static 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
254
255int sigd_attach(struct atm_vcc *vcc)
256{
257 if (sigd) 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));
262 set_bit(ATM_VF_META,&vcc->flags);
263 set_bit(ATM_VF_READY,&vcc->flags);
264#ifdef WAIT_FOR_DEMON
265 wake_up(&sigd_sleep);
266#endif
267 return 0;
268}