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 static DECLARE_WAIT_QUEUE_HEAD(sigd_sleep);
35 
36 
sigd_put_skb(struct sk_buff * skb)37 static void sigd_put_skb(struct sk_buff *skb)
38 {
39 #ifdef WAIT_FOR_DEMON
40 	static unsigned long silence = 0;
41 	DECLARE_WAITQUEUE(wait,current);
42 
43 	add_wait_queue(&sigd_sleep,&wait);
44 	while (!sigd) {
45 		set_current_state(TASK_UNINTERRUPTIBLE);
46 		if (time_after(jiffies, silence) || silence == 0) {
47 			printk(KERN_INFO "atmsvc: waiting for signaling demon "
48 			    "...\n");
49 			silence = (jiffies+30*HZ)|1;
50 		}
51 		schedule();
52 	}
53 	current->state = TASK_RUNNING;
54 	remove_wait_queue(&sigd_sleep,&wait);
55 #else
56 	if (!sigd) {
57 		printk(KERN_WARNING "atmsvc: no signaling demon\n");
58 		kfree_skb(skb);
59 		return;
60 	}
61 #endif
62 	atm_force_charge(sigd,skb->truesize);
63 	skb_queue_tail(&sigd->sk->receive_queue,skb);
64 	wake_up(&sigd->sleep);
65 }
66 
67 
modify_qos(struct atm_vcc * vcc,struct atmsvc_msg * msg)68 static void modify_qos(struct atm_vcc *vcc,struct atmsvc_msg *msg)
69 {
70 	struct sk_buff *skb;
71 
72 	if (test_bit(ATM_VF_RELEASED,&vcc->flags) ||
73 	    !test_bit(ATM_VF_READY,&vcc->flags))
74 		return;
75 	msg->type = as_error;
76 	if (!vcc->dev->ops->change_qos) msg->reply = -EOPNOTSUPP;
77 	else {
78 		/* should lock VCC */
79 		msg->reply = vcc->dev->ops->change_qos(vcc,&msg->qos,
80 		    msg->reply);
81 		if (!msg->reply) msg->type = as_okay;
82 	}
83 	/*
84 	 * Should probably just turn around the old skb. But the, the buffer
85 	 * space accounting needs to follow the change too. Maybe later.
86 	 */
87 	while (!(skb = alloc_skb(sizeof(struct atmsvc_msg),GFP_KERNEL)))
88 		schedule();
89 	*(struct atmsvc_msg *) skb_put(skb,sizeof(struct atmsvc_msg)) = *msg;
90 	sigd_put_skb(skb);
91 }
92 
93 
sigd_send(struct atm_vcc * vcc,struct sk_buff * skb)94 static int sigd_send(struct atm_vcc *vcc,struct sk_buff *skb)
95 {
96 	struct atmsvc_msg *msg;
97 	struct atm_vcc *session_vcc;
98 
99 	msg = (struct atmsvc_msg *) skb->data;
100 	atomic_sub(skb->truesize, &vcc->sk->wmem_alloc);
101 	DPRINTK("sigd_send %d (0x%lx)\n",(int) msg->type,
102 	  (unsigned long) msg->vcc);
103 	vcc = *(struct atm_vcc **) &msg->vcc;
104 	switch (msg->type) {
105 		case as_okay:
106 			vcc->reply = msg->reply;
107 			if (!*vcc->local.sas_addr.prv &&
108 			    !*vcc->local.sas_addr.pub) {
109 				vcc->local.sas_family = AF_ATMSVC;
110 				memcpy(vcc->local.sas_addr.prv,
111 				    msg->local.sas_addr.prv,ATM_ESA_LEN);
112 				memcpy(vcc->local.sas_addr.pub,
113 				    msg->local.sas_addr.pub,ATM_E164_LEN+1);
114 			}
115 			session_vcc = vcc->session ? vcc->session : vcc;
116 			if (session_vcc->vpi || session_vcc->vci) break;
117 			session_vcc->itf = msg->pvc.sap_addr.itf;
118 			session_vcc->vpi = msg->pvc.sap_addr.vpi;
119 			session_vcc->vci = msg->pvc.sap_addr.vci;
120 			if (session_vcc->vpi || session_vcc->vci)
121 				session_vcc->qos = msg->qos;
122 			break;
123 		case as_error:
124 			clear_bit(ATM_VF_REGIS,&vcc->flags);
125 			clear_bit(ATM_VF_READY,&vcc->flags);
126 			vcc->reply = msg->reply;
127 			vcc->sk->err = -msg->reply;
128 			break;
129 		case as_indicate:
130 			vcc = *(struct atm_vcc **) &msg->listen_vcc;
131 			DPRINTK("as_indicate!!!\n");
132 			lock_sock(vcc->sk);
133 			if (vcc->sk->ack_backlog == vcc->sk->max_ack_backlog) {
134 				sigd_enq(0,as_reject,vcc,NULL,NULL);
135 				goto as_indicate_complete;
136 			}
137 			vcc->sk->ack_backlog++;
138 			skb_queue_tail(&vcc->sk->receive_queue,skb);
139 			if (vcc->callback) {
140 				DPRINTK("waking vcc->sleep 0x%p\n",
141 				    &vcc->sleep);
142 				vcc->callback(vcc);
143 			}
144 as_indicate_complete:
145 			release_sock(vcc->sk);
146 			return 0;
147 		case as_close:
148 			set_bit(ATM_VF_RELEASED,&vcc->flags);
149 			clear_bit(ATM_VF_READY,&vcc->flags);
150 			vcc->reply = msg->reply;
151 			vcc->sk->err = -msg->reply;
152 			break;
153 		case as_modify:
154 			modify_qos(vcc,msg);
155 			break;
156 		default:
157 			printk(KERN_ALERT "sigd_send: bad message type %d\n",
158 			    (int) msg->type);
159 			return -EINVAL;
160 	}
161 	if (vcc->callback) vcc->callback(vcc);
162 	dev_kfree_skb(skb);
163 	return 0;
164 }
165 
166 
sigd_enq2(struct atm_vcc * vcc,enum atmsvc_msg_type type,struct atm_vcc * listen_vcc,const struct sockaddr_atmpvc * pvc,const struct sockaddr_atmsvc * svc,const struct atm_qos * qos,int reply)167 void sigd_enq2(struct atm_vcc *vcc,enum atmsvc_msg_type type,
168     struct atm_vcc *listen_vcc,const struct sockaddr_atmpvc *pvc,
169     const struct sockaddr_atmsvc *svc,const struct atm_qos *qos,int reply)
170 {
171 	struct sk_buff *skb;
172 	struct atmsvc_msg *msg;
173 
174 	DPRINTK("sigd_enq %d (0x%p)\n",(int) type,vcc);
175 	while (!(skb = alloc_skb(sizeof(struct atmsvc_msg),GFP_KERNEL)))
176 		schedule();
177 	msg = (struct atmsvc_msg *) skb_put(skb,sizeof(struct atmsvc_msg));
178 	memset(msg,0,sizeof(*msg));
179 	msg->type = type;
180 	*(struct atm_vcc **) &msg->vcc = vcc;
181 	*(struct atm_vcc **) &msg->listen_vcc = listen_vcc;
182 	msg->reply = reply;
183 	if (qos) msg->qos = *qos;
184 	if (vcc) msg->sap = vcc->sap;
185 	if (svc) msg->svc = *svc;
186 	if (vcc) msg->local = vcc->local;
187 	if (pvc) msg->pvc = *pvc;
188 	sigd_put_skb(skb);
189 	if (vcc) set_bit(ATM_VF_REGIS,&vcc->flags);
190 }
191 
192 
sigd_enq(struct atm_vcc * vcc,enum atmsvc_msg_type type,struct atm_vcc * listen_vcc,const struct sockaddr_atmpvc * pvc,const struct sockaddr_atmsvc * svc)193 void sigd_enq(struct atm_vcc *vcc,enum atmsvc_msg_type type,
194     struct atm_vcc *listen_vcc,const struct sockaddr_atmpvc *pvc,
195     const struct sockaddr_atmsvc *svc)
196 {
197 	sigd_enq2(vcc,type,listen_vcc,pvc,svc,vcc ? &vcc->qos : NULL,0);
198 	/* other ISP applications may use "reply" */
199 }
200 
201 
purge_vcc(struct atm_vcc * vcc)202 static void purge_vcc(struct atm_vcc *vcc)
203 {
204 	if (vcc->sk->family == PF_ATMSVC &&
205 	    !test_bit(ATM_VF_META, &vcc->flags)) {
206 		set_bit(ATM_VF_RELEASED, &vcc->flags);
207 		vcc->reply = -EUNATCH;
208 		vcc->sk->err = EUNATCH;
209 		wake_up(&vcc->sleep);
210 	}
211 }
212 
213 
sigd_close(struct atm_vcc * vcc)214 static void sigd_close(struct atm_vcc *vcc)
215 {
216 	struct sock *s;
217 
218 	DPRINTK("sigd_close\n");
219 	sigd = NULL;
220 	if (skb_peek(&vcc->sk->receive_queue))
221 		printk(KERN_ERR "sigd_close: closing with requests pending\n");
222 	skb_queue_purge(&vcc->sk->receive_queue);
223 
224 	read_lock(&vcc_sklist_lock);
225 	for(s = vcc_sklist; s; s = s->next) {
226 		struct atm_vcc *vcc = s->protinfo.af_atm;
227 
228 		if (vcc->dev)
229 			purge_vcc(vcc);
230 	}
231 	read_unlock(&vcc_sklist_lock);
232 }
233 
234 
235 static struct atmdev_ops sigd_dev_ops = {
236 	.close =	sigd_close,
237 	.send =		sigd_send
238 };
239 
240 
241 static struct atm_dev sigd_dev = {
242 	.ops =		&sigd_dev_ops,
243 	.type =		"sig",
244 	.number =	999,
245 	.lock =		SPIN_LOCK_UNLOCKED
246 };
247 
248 
sigd_attach(struct atm_vcc * vcc)249 int sigd_attach(struct atm_vcc *vcc)
250 {
251 	if (sigd) return -EADDRINUSE;
252 	DPRINTK("sigd_attach\n");
253 	sigd = vcc;
254 	vcc->dev = &sigd_dev;
255 	vcc_insert_socket(vcc->sk);
256 	set_bit(ATM_VF_META,&vcc->flags);
257 	set_bit(ATM_VF_READY,&vcc->flags);
258 	wake_up(&sigd_sleep);
259 	return 0;
260 }
261