blob: 523bce72f698ef2a34cfc66da4dbe69cde954fe9 [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);
David S. Miller676d2362014-04-11 16:15:36 -040054 sk_atm(sigd)->sk_data_ready(sk_atm(sigd));
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
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;
Eric Dumazet95c96172012-04-15 05:58:06 +0000169 static unsigned int session = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct sock *s;
221 int i;
222
Joe Perches99824462010-01-26 11:40:00 +0000223 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 sigd = NULL;
225 if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
Joe Perches99824462010-01-26 11:40:00 +0000226 pr_err("closing with requests pending\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
228
229 read_lock(&vcc_sklist_lock);
Joe Perches0ec96e62010-01-26 11:40:17 +0000230 for (i = 0; i < VCC_HTABLE_SIZE; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct hlist_head *head = &vcc_hash[i];
232
Sasha Levinb67bfe02013-02-27 17:06:00 -0800233 sk_for_each(s, head) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700234 vcc = atm_sk(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Chas Williams9301e322005-09-28 16:35:01 -0700236 purge_vcc(vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 }
239 read_unlock(&vcc_sklist_lock);
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static struct atmdev_ops sigd_dev_ops = {
243 .close = sigd_close,
244 .send = sigd_send
245};
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254int sigd_attach(struct atm_vcc *vcc)
255{
Joe Perches0ec96e62010-01-26 11:40:17 +0000256 if (sigd)
257 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));
Joe Perches0ec96e62010-01-26 11:40:17 +0000262 set_bit(ATM_VF_META, &vcc->flags);
263 set_bit(ATM_VF_READY, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#ifdef WAIT_FOR_DEMON
265 wake_up(&sigd_sleep);
266#endif
267 return 0;
268}