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