net/atm/atm_sysfs.c: checkpatch cleanups
[linux-2.6.git] / net / atm / signaling.c
1 /* net/atm/signaling.c - ATM signaling */
2
3 /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
4
5 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
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
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
27 struct atm_vcc *sigd = NULL;
28 #ifdef WAIT_FOR_DEMON
29 static DECLARE_WAIT_QUEUE_HEAD(sigd_sleep);
30 #endif
31
32
33 static void sigd_put_skb(struct sk_buff *skb)
34 {
35 #ifdef WAIT_FOR_DEMON
36         DECLARE_WAITQUEUE(wait,current);
37
38         add_wait_queue(&sigd_sleep,&wait);
39         while (!sigd) {
40                 set_current_state(TASK_UNINTERRUPTIBLE);
41                 pr_debug("atmsvc: waiting for signaling daemon...\n");
42                 schedule();
43         }
44         current->state = TASK_RUNNING;
45         remove_wait_queue(&sigd_sleep,&wait);
46 #else
47         if (!sigd) {
48                 pr_debug("atmsvc: no signaling daemon\n");
49                 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
59 static 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
85 static 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);
93         vcc = *(struct atm_vcc **) &msg->vcc;
94         pr_debug("%d (0x%lx)\n", (int)msg->type, (unsigned long)vcc);
95         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;
125                         sk = sk_atm(vcc);
126                         pr_debug("as_indicate!!!\n");
127                         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);
135                         pr_debug("waking sk->sk_sleep 0x%p\n", sk->sk_sleep);
136                         sk->sk_state_change(sk);
137 as_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:
153                         pr_alert("bad message type %d\n", (int)msg->type);
154                         return -EINVAL;
155         }
156         sk->sk_state_change(sk);
157 out:
158         dev_kfree_skb(skb);
159         return 0;
160 }
161
162
163 void 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
171         pr_debug("%d (0x%p)\n", (int)type, vcc);
172         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
195 void 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
204 static void purge_vcc(struct atm_vcc *vcc)
205 {
206         if (sk_atm(vcc)->sk_family == PF_ATMSVC &&
207             !test_bit(ATM_VF_META, &vcc->flags)) {
208                 set_bit(ATM_VF_RELEASED, &vcc->flags);
209                 clear_bit(ATM_VF_REGIS, &vcc->flags);
210                 vcc_release_async(vcc, -EUNATCH);
211         }
212 }
213
214
215 static void sigd_close(struct atm_vcc *vcc)
216 {
217         struct hlist_node *node;
218         struct sock *s;
219         int i;
220
221         pr_debug("\n");
222         sigd = NULL;
223         if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
224                 pr_err("closing with requests pending\n");
225         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) {
232                         vcc = atm_sk(s);
233
234                         purge_vcc(vcc);
235                 }
236         }
237         read_unlock(&vcc_sklist_lock);
238 }
239
240
241 static struct atmdev_ops sigd_dev_ops = {
242         .close = sigd_close,
243         .send = sigd_send
244 };
245
246
247 static struct atm_dev sigd_dev = {
248         .ops =          &sigd_dev_ops,
249         .type =         "sig",
250         .number =       999,
251         .lock =         __SPIN_LOCK_UNLOCKED(sigd_dev.lock)
252 };
253
254
255 int sigd_attach(struct atm_vcc *vcc)
256 {
257         if (sigd) return -EADDRINUSE;
258         pr_debug("\n");
259         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 }