xref: /openbmc/linux/net/atm/signaling.c (revision 95e9fd10)
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 #include <linux/slab.h>
18 
19 #include "resources.h"
20 #include "signaling.h"
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 static void sigd_put_skb(struct sk_buff *skb)
33 {
34 #ifdef WAIT_FOR_DEMON
35 	DECLARE_WAITQUEUE(wait, current);
36 
37 	add_wait_queue(&sigd_sleep, &wait);
38 	while (!sigd) {
39 		set_current_state(TASK_UNINTERRUPTIBLE);
40 		pr_debug("atmsvc: waiting for signaling daemon...\n");
41 		schedule();
42 	}
43 	current->state = TASK_RUNNING;
44 	remove_wait_queue(&sigd_sleep, &wait);
45 #else
46 	if (!sigd) {
47 		pr_debug("atmsvc: no signaling daemon\n");
48 		kfree_skb(skb);
49 		return;
50 	}
51 #endif
52 	atm_force_charge(sigd, skb->truesize);
53 	skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb);
54 	sk_atm(sigd)->sk_data_ready(sk_atm(sigd), skb->len);
55 }
56 
57 static void modify_qos(struct atm_vcc *vcc, struct atmsvc_msg *msg)
58 {
59 	struct sk_buff *skb;
60 
61 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
62 	    !test_bit(ATM_VF_READY, &vcc->flags))
63 		return;
64 	msg->type = as_error;
65 	if (!vcc->dev->ops->change_qos)
66 		msg->reply = -EOPNOTSUPP;
67 	else {
68 		/* should lock VCC */
69 		msg->reply = vcc->dev->ops->change_qos(vcc, &msg->qos,
70 						       msg->reply);
71 		if (!msg->reply)
72 			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 static int sigd_send(struct atm_vcc *vcc, struct sk_buff *skb)
85 {
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);
92 	vcc = *(struct atm_vcc **) &msg->vcc;
93 	pr_debug("%d (0x%lx)\n", (int)msg->type, (unsigned long)vcc);
94 	sk = sk_atm(vcc);
95 
96 	switch (msg->type) {
97 	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)
109 			break;
110 		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);
134 		pr_debug("waking sk_sleep(sk) 0x%p\n", sk_sleep(sk));
135 		sk->sk_state_change(sk);
136 as_indicate_complete:
137 		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;
155 	}
156 	sk->sk_state_change(sk);
157 out:
158 	dev_kfree_skb(skb);
159 	return 0;
160 }
161 
162 void 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)
166 {
167 	struct sk_buff *skb;
168 	struct atmsvc_msg *msg;
169 	static unsigned int 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)
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;
190 	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);
196 	if (vcc)
197 		set_bit(ATM_VF_REGIS, &vcc->flags);
198 }
199 
200 void 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)
203 {
204 	sigd_enq2(vcc, type, listen_vcc, pvc, svc, vcc ? &vcc->qos : NULL, 0);
205 	/* other ISP applications may use "reply" */
206 }
207 
208 static void purge_vcc(struct atm_vcc *vcc)
209 {
210 	if (sk_atm(vcc)->sk_family == PF_ATMSVC &&
211 	    !test_bit(ATM_VF_META, &vcc->flags)) {
212 		set_bit(ATM_VF_RELEASED, &vcc->flags);
213 		clear_bit(ATM_VF_REGIS, &vcc->flags);
214 		vcc_release_async(vcc, -EUNATCH);
215 	}
216 }
217 
218 static void sigd_close(struct atm_vcc *vcc)
219 {
220 	struct hlist_node *node;
221 	struct sock *s;
222 	int i;
223 
224 	pr_debug("\n");
225 	sigd = NULL;
226 	if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
227 		pr_err("closing with requests pending\n");
228 	skb_queue_purge(&sk_atm(vcc)->sk_receive_queue);
229 
230 	read_lock(&vcc_sklist_lock);
231 	for (i = 0; i < VCC_HTABLE_SIZE; ++i) {
232 		struct hlist_head *head = &vcc_hash[i];
233 
234 		sk_for_each(s, node, head) {
235 			vcc = atm_sk(s);
236 
237 			purge_vcc(vcc);
238 		}
239 	}
240 	read_unlock(&vcc_sklist_lock);
241 }
242 
243 static struct atmdev_ops sigd_dev_ops = {
244 	.close = sigd_close,
245 	.send =	sigd_send
246 };
247 
248 static struct atm_dev sigd_dev = {
249 	.ops =		&sigd_dev_ops,
250 	.type =		"sig",
251 	.number =	999,
252 	.lock =		__SPIN_LOCK_UNLOCKED(sigd_dev.lock)
253 };
254 
255 int sigd_attach(struct atm_vcc *vcc)
256 {
257 	if (sigd)
258 		return -EADDRINUSE;
259 	pr_debug("\n");
260 	sigd = vcc;
261 	vcc->dev = &sigd_dev;
262 	vcc_insert_socket(sk_atm(vcc));
263 	set_bit(ATM_VF_META, &vcc->flags);
264 	set_bit(ATM_VF_READY, &vcc->flags);
265 #ifdef WAIT_FOR_DEMON
266 	wake_up(&sigd_sleep);
267 #endif
268 	return 0;
269 }
270