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