net/atm/signaling.c: checkpatch cleanups
[linux-3.10.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 #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 struct atm_vcc *sigd = NULL;
27 #ifdef WAIT_FOR_DEMON
28 static DECLARE_WAIT_QUEUE_HEAD(sigd_sleep);
29 #endif
30
31 static void sigd_put_skb(struct sk_buff *skb)
32 {
33 #ifdef WAIT_FOR_DEMON
34         DECLARE_WAITQUEUE(wait, current);
35
36         add_wait_queue(&sigd_sleep, &wait);
37         while (!sigd) {
38                 set_current_state(TASK_UNINTERRUPTIBLE);
39                 pr_debug("atmsvc: waiting for signaling daemon...\n");
40                 schedule();
41         }
42         current->state = TASK_RUNNING;
43         remove_wait_queue(&sigd_sleep, &wait);
44 #else
45         if (!sigd) {
46                 pr_debug("atmsvc: no signaling daemon\n");
47                 kfree_skb(skb);
48                 return;
49         }
50 #endif
51         atm_force_charge(sigd, skb->truesize);
52         skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb);
53         sk_atm(sigd)->sk_data_ready(sk_atm(sigd), skb->len);
54 }
55
56 static void modify_qos(struct atm_vcc *vcc, struct atmsvc_msg *msg)
57 {
58         struct sk_buff *skb;
59
60         if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
61             !test_bit(ATM_VF_READY, &vcc->flags))
62                 return;
63         msg->type = as_error;
64         if (!vcc->dev->ops->change_qos)
65                 msg->reply = -EOPNOTSUPP;
66         else {
67                 /* should lock VCC */
68                 msg->reply = vcc->dev->ops->change_qos(vcc, &msg->qos,
69                                                        msg->reply);
70                 if (!msg->reply)
71                         msg->type = as_okay;
72         }
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          */
77         while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL)))
78                 schedule();
79         *(struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg)) = *msg;
80         sigd_put_skb(skb);
81 }
82
83 static int sigd_send(struct atm_vcc *vcc, struct sk_buff *skb)
84 {
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);
91         vcc = *(struct atm_vcc **) &msg->vcc;
92         pr_debug("%d (0x%lx)\n", (int)msg->type, (unsigned long)vcc);
93         sk = sk_atm(vcc);
94
95         switch (msg->type) {
96         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)
108                         break;
109                 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);
135 as_indicate_complete:
136                 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;
154         }
155         sk->sk_state_change(sk);
156 out:
157         dev_kfree_skb(skb);
158         return 0;
159 }
160
161 void 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)
165 {
166         struct sk_buff *skb;
167         struct atmsvc_msg *msg;
168         static unsigned session = 0;
169
170         pr_debug("%d (0x%p)\n", (int)type, vcc);
171         while (!(skb = alloc_skb(sizeof(struct atmsvc_msg), GFP_KERNEL)))
172                 schedule();
173         msg = (struct atmsvc_msg *)skb_put(skb, sizeof(struct atmsvc_msg));
174         memset(msg, 0, sizeof(*msg));
175         msg->type = type;
176         *(struct atm_vcc **) &msg->vcc = vcc;
177         *(struct atm_vcc **) &msg->listen_vcc = listen_vcc;
178         msg->reply = reply;
179         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;
189         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);
195         if (vcc)
196                 set_bit(ATM_VF_REGIS, &vcc->flags);
197 }
198
199 void 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)
202 {
203         sigd_enq2(vcc, type, listen_vcc, pvc, svc, vcc ? &vcc->qos : NULL, 0);
204         /* other ISP applications may use "reply" */
205 }
206
207 static void purge_vcc(struct atm_vcc *vcc)
208 {
209         if (sk_atm(vcc)->sk_family == PF_ATMSVC &&
210             !test_bit(ATM_VF_META, &vcc->flags)) {
211                 set_bit(ATM_VF_RELEASED, &vcc->flags);
212                 clear_bit(ATM_VF_REGIS, &vcc->flags);
213                 vcc_release_async(vcc, -EUNATCH);
214         }
215 }
216
217 static void sigd_close(struct atm_vcc *vcc)
218 {
219         struct hlist_node *node;
220         struct sock *s;
221         int i;
222
223         pr_debug("\n");
224         sigd = NULL;
225         if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
226                 pr_err("closing with requests pending\n");
227         skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
228
229         read_lock(&vcc_sklist_lock);
230         for (i = 0; i < VCC_HTABLE_SIZE; ++i) {
231                 struct hlist_head *head = &vcc_hash[i];
232
233                 sk_for_each(s, node, head) {
234                         vcc = atm_sk(s);
235
236                         purge_vcc(vcc);
237                 }
238         }
239         read_unlock(&vcc_sklist_lock);
240 }
241
242 static struct atmdev_ops sigd_dev_ops = {
243         .close = sigd_close,
244         .send = sigd_send
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 int sigd_attach(struct atm_vcc *vcc)
255 {
256         if (sigd)
257                 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 }