vserver 2.0 rc7
[linux-2.6.git] / net / x25 / x25_subr.c
1 /*
2  *      X.25 Packet Layer release 002
3  *
4  *      This is ALPHA test software. This code may break your machine,
5  *      randomly fail to work with new releases, misbehave and/or generally
6  *      screw up. It might even work.
7  *
8  *      This code REQUIRES 2.1.15 or higher
9  *
10  *      This module:
11  *              This module is free software; you can redistribute it and/or
12  *              modify it under the terms of the GNU General Public License
13  *              as published by the Free Software Foundation; either version
14  *              2 of the License, or (at your option) any later version.
15  *
16  *      History
17  *      X.25 001        Jonathan Naylor   Started coding.
18  *      X.25 002        Jonathan Naylor   Centralised disconnection processing.
19  *      mar/20/00       Daniela Squassoni Disabling/enabling of facilities
20  *                                        negotiation.
21  *      jun/24/01       Arnaldo C. Melo   use skb_queue_purge, cleanups
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/skbuff.h>
27 #include <net/sock.h>
28 #include <net/tcp.h>
29 #include <net/x25.h>
30
31 /*
32  *      This routine purges all of the queues of frames.
33  */
34 void x25_clear_queues(struct sock *sk)
35 {
36         struct x25_sock *x25 = x25_sk(sk);
37
38         skb_queue_purge(&sk->sk_write_queue);
39         skb_queue_purge(&x25->ack_queue);
40         skb_queue_purge(&x25->interrupt_in_queue);
41         skb_queue_purge(&x25->interrupt_out_queue);
42         skb_queue_purge(&x25->fragment_queue);
43 }
44
45
46 /*
47  * This routine purges the input queue of those frames that have been
48  * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
49  * SDL diagram.
50 */
51 void x25_frames_acked(struct sock *sk, unsigned short nr)
52 {
53         struct sk_buff *skb;
54         struct x25_sock *x25 = x25_sk(sk);
55         int modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
56
57         /*
58          * Remove all the ack-ed frames from the ack queue.
59          */
60         if (x25->va != nr)
61                 while (skb_peek(&x25->ack_queue) && x25->va != nr) {
62                         skb = skb_dequeue(&x25->ack_queue);
63                         kfree_skb(skb);
64                         x25->va = (x25->va + 1) % modulus;
65                 }
66 }
67
68 void x25_requeue_frames(struct sock *sk)
69 {
70         struct sk_buff *skb, *skb_prev = NULL;
71
72         /*
73          * Requeue all the un-ack-ed frames on the output queue to be picked
74          * up by x25_kick. This arrangement handles the possibility of an empty
75          * output queue.
76          */
77         while ((skb = skb_dequeue(&x25_sk(sk)->ack_queue)) != NULL) {
78                 if (!skb_prev)
79                         skb_queue_head(&sk->sk_write_queue, skb);
80                 else
81                         skb_append(skb_prev, skb);
82                 skb_prev = skb;
83         }
84 }
85
86 /*
87  *      Validate that the value of nr is between va and vs. Return true or
88  *      false for testing.
89  */
90 int x25_validate_nr(struct sock *sk, unsigned short nr)
91 {
92         struct x25_sock *x25 = x25_sk(sk);
93         unsigned short vc = x25->va;
94         int modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
95
96         while (vc != x25->vs) {
97                 if (nr == vc)
98                         return 1;
99                 vc = (vc + 1) % modulus;
100         }
101
102         return nr == x25->vs ? 1 : 0;
103 }
104
105 /*
106  *  This routine is called when the packet layer internally generates a
107  *  control frame.
108  */
109 void x25_write_internal(struct sock *sk, int frametype)
110 {
111         struct x25_sock *x25 = x25_sk(sk);
112         struct sk_buff *skb;
113         unsigned char  *dptr;
114         unsigned char  facilities[X25_MAX_FAC_LEN];
115         unsigned char  addresses[1 + X25_ADDR_LEN];
116         unsigned char  lci1, lci2;
117         /*
118          *      Default safe frame size.
119          */
120         int len = X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
121
122         /*
123          *      Adjust frame size.
124          */
125         switch (frametype) {
126                 case X25_CALL_REQUEST:
127                         len += 1 + X25_ADDR_LEN + X25_MAX_FAC_LEN +
128                                X25_MAX_CUD_LEN;
129                         break;
130                 case X25_CALL_ACCEPTED:
131                         len += 1 + X25_MAX_FAC_LEN + X25_MAX_CUD_LEN;
132                         break;
133                 case X25_CLEAR_REQUEST:
134                 case X25_RESET_REQUEST:
135                         len += 2;
136                         break;
137                 case X25_RR:
138                 case X25_RNR:
139                 case X25_REJ:
140                 case X25_CLEAR_CONFIRMATION:
141                 case X25_INTERRUPT_CONFIRMATION:
142                 case X25_RESET_CONFIRMATION:
143                         break;
144                 default:
145                         printk(KERN_ERR "X.25: invalid frame type %02X\n",
146                                frametype);
147                         return;
148         }
149
150         if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
151                 return;
152
153         /*
154          *      Space for Ethernet and 802.2 LLC headers.
155          */
156         skb_reserve(skb, X25_MAX_L2_LEN);
157
158         /*
159          *      Make space for the GFI and LCI, and fill them in.
160          */
161         dptr = skb_put(skb, 2);
162
163         lci1 = (x25->lci >> 8) & 0x0F;
164         lci2 = (x25->lci >> 0) & 0xFF;
165
166         if (x25->neighbour->extended) {
167                 *dptr++ = lci1 | X25_GFI_EXTSEQ;
168                 *dptr++ = lci2;
169         } else {
170                 *dptr++ = lci1 | X25_GFI_STDSEQ;
171                 *dptr++ = lci2;
172         }
173
174         /*
175          *      Now fill in the frame type specific information.
176          */
177         switch (frametype) {
178
179                 case X25_CALL_REQUEST:
180                         dptr    = skb_put(skb, 1);
181                         *dptr++ = X25_CALL_REQUEST;
182                         len     = x25_addr_aton(addresses, &x25->dest_addr,
183                                                 &x25->source_addr);
184                         dptr    = skb_put(skb, len);
185                         memcpy(dptr, addresses, len);
186                         len     = x25_create_facilities(facilities,
187                                                         &x25->facilities,
188                                              x25->neighbour->global_facil_mask);
189                         dptr    = skb_put(skb, len);
190                         memcpy(dptr, facilities, len);
191                         dptr = skb_put(skb, x25->calluserdata.cudlength);
192                         memcpy(dptr, x25->calluserdata.cuddata,
193                                x25->calluserdata.cudlength);
194                         x25->calluserdata.cudlength = 0;
195                         break;
196
197                 case X25_CALL_ACCEPTED:
198                         dptr    = skb_put(skb, 2);
199                         *dptr++ = X25_CALL_ACCEPTED;
200                         *dptr++ = 0x00;         /* Address lengths */
201                         len     = x25_create_facilities(facilities,
202                                                         &x25->facilities,
203                                                         x25->vc_facil_mask);
204                         dptr    = skb_put(skb, len);
205                         memcpy(dptr, facilities, len);
206                         dptr = skb_put(skb, x25->calluserdata.cudlength);
207                         memcpy(dptr, x25->calluserdata.cuddata,
208                                x25->calluserdata.cudlength);
209                         x25->calluserdata.cudlength = 0;
210                         break;
211
212                 case X25_CLEAR_REQUEST:
213                 case X25_RESET_REQUEST:
214                         dptr    = skb_put(skb, 3);
215                         *dptr++ = frametype;
216                         *dptr++ = 0x00;         /* XXX */
217                         *dptr++ = 0x00;         /* XXX */
218                         break;
219
220                 case X25_RR:
221                 case X25_RNR:
222                 case X25_REJ:
223                         if (x25->neighbour->extended) {
224                                 dptr     = skb_put(skb, 2);
225                                 *dptr++  = frametype;
226                                 *dptr++  = (x25->vr << 1) & 0xFE;
227                         } else {
228                                 dptr     = skb_put(skb, 1);
229                                 *dptr    = frametype;
230                                 *dptr++ |= (x25->vr << 5) & 0xE0;
231                         }
232                         break;
233
234                 case X25_CLEAR_CONFIRMATION:
235                 case X25_INTERRUPT_CONFIRMATION:
236                 case X25_RESET_CONFIRMATION:
237                         dptr  = skb_put(skb, 1);
238                         *dptr = frametype;
239                         break;
240         }
241
242         x25_transmit_link(skb, x25->neighbour);
243 }
244
245 /*
246  *      Unpick the contents of the passed X.25 Packet Layer frame.
247  */
248 int x25_decode(struct sock *sk, struct sk_buff *skb, int *ns, int *nr, int *q,
249                int *d, int *m)
250 {
251         struct x25_sock *x25 = x25_sk(sk);
252         unsigned char *frame = skb->data;
253
254         *ns = *nr = *q = *d = *m = 0;
255
256         switch (frame[2]) {
257                 case X25_CALL_REQUEST:
258                 case X25_CALL_ACCEPTED:
259                 case X25_CLEAR_REQUEST:
260                 case X25_CLEAR_CONFIRMATION:
261                 case X25_INTERRUPT:
262                 case X25_INTERRUPT_CONFIRMATION:
263                 case X25_RESET_REQUEST:
264                 case X25_RESET_CONFIRMATION:
265                 case X25_RESTART_REQUEST:
266                 case X25_RESTART_CONFIRMATION:
267                 case X25_REGISTRATION_REQUEST:
268                 case X25_REGISTRATION_CONFIRMATION:
269                 case X25_DIAGNOSTIC:
270                         return frame[2];
271         }
272
273         if (x25->neighbour->extended) {
274                 if (frame[2] == X25_RR  ||
275                     frame[2] == X25_RNR ||
276                     frame[2] == X25_REJ) {
277                         *nr = (frame[3] >> 1) & 0x7F;
278                         return frame[2];
279                 }
280         } else {
281                 if ((frame[2] & 0x1F) == X25_RR  ||
282                     (frame[2] & 0x1F) == X25_RNR ||
283                     (frame[2] & 0x1F) == X25_REJ) {
284                         *nr = (frame[2] >> 5) & 0x07;
285                         return frame[2] & 0x1F;
286                 }
287         }
288
289         if (x25->neighbour->extended) {
290                 if ((frame[2] & 0x01) == X25_DATA) {
291                         *q  = (frame[0] & X25_Q_BIT) == X25_Q_BIT;
292                         *d  = (frame[0] & X25_D_BIT) == X25_D_BIT;
293                         *m  = (frame[3] & X25_EXT_M_BIT) == X25_EXT_M_BIT;
294                         *nr = (frame[3] >> 1) & 0x7F;
295                         *ns = (frame[2] >> 1) & 0x7F;
296                         return X25_DATA;
297                 }
298         } else {
299                 if ((frame[2] & 0x01) == X25_DATA) {
300                         *q  = (frame[0] & X25_Q_BIT) == X25_Q_BIT;
301                         *d  = (frame[0] & X25_D_BIT) == X25_D_BIT;
302                         *m  = (frame[2] & X25_STD_M_BIT) == X25_STD_M_BIT;
303                         *nr = (frame[2] >> 5) & 0x07;
304                         *ns = (frame[2] >> 1) & 0x07;
305                         return X25_DATA;
306                 }
307         }
308
309         printk(KERN_DEBUG "X.25: invalid PLP frame %02X %02X %02X\n",
310                frame[0], frame[1], frame[2]);
311
312         return X25_ILLEGAL;
313 }
314
315 void x25_disconnect(struct sock *sk, int reason, unsigned char cause,
316                     unsigned char diagnostic)
317 {
318         struct x25_sock *x25 = x25_sk(sk);
319
320         x25_clear_queues(sk);
321         x25_stop_timer(sk);
322
323         x25->lci   = 0;
324         x25->state = X25_STATE_0;
325
326         x25->causediag.cause      = cause;
327         x25->causediag.diagnostic = diagnostic;
328
329         sk->sk_state     = TCP_CLOSE;
330         sk->sk_err       = reason;
331         sk->sk_shutdown |= SEND_SHUTDOWN;
332
333         if (!sock_flag(sk, SOCK_DEAD)) {
334                 sk->sk_state_change(sk);
335                 sock_set_flag(sk, SOCK_DEAD);
336         }
337 }
338
339 /*
340  * Clear an own-rx-busy condition and tell the peer about this, provided
341  * that there is a significant amount of free receive buffer space available.
342  */
343 void x25_check_rbuf(struct sock *sk)
344 {
345         struct x25_sock *x25 = x25_sk(sk);
346
347         if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
348             (x25->condition & X25_COND_OWN_RX_BUSY)) {
349                 x25->condition &= ~X25_COND_OWN_RX_BUSY;
350                 x25->condition &= ~X25_COND_ACK_PENDING;
351                 x25->vl         = x25->vr;
352                 x25_write_internal(sk, X25_RR);
353                 x25_stop_timer(sk);
354         }
355 }
356
357 /*
358  * Compare 2 calluserdata structures, used to find correct listening sockets
359  * when call user data is used.
360  */
361 int x25_check_calluserdata(struct x25_calluserdata *ours, struct x25_calluserdata *theirs)
362 {
363         int i;
364         if (ours->cudlength != theirs->cudlength)
365                 return 0;
366
367         for (i=0;i<ours->cudlength;i++) {
368                 if (ours->cuddata[i] != theirs->cuddata[i]) {
369                         return 0;
370                 }
371         }
372         return 1;
373 }
374