ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / x25 / x25_facilities.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        Split from x25_subr.c
18  *      mar/20/00       Daniela Squassoni Disabling/enabling of facilities 
19  *                                        negotiation.
20  */
21
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/socket.h>
25 #include <linux/in.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/timer.h>
29 #include <linux/string.h>
30 #include <linux/sockios.h>
31 #include <linux/net.h>
32 #include <linux/inet.h>
33 #include <linux/netdevice.h>
34 #include <linux/skbuff.h>
35 #include <net/sock.h>
36 #include <asm/system.h>
37 #include <linux/fcntl.h>
38 #include <linux/mm.h>
39 #include <linux/interrupt.h>
40 #include <net/x25.h>
41
42 /*
43  *      Parse a set of facilities into the facilities structure. Unrecognised
44  *      facilities are written to the debug log file.
45  */
46 int x25_parse_facilities(struct sk_buff *skb,
47                          struct x25_facilities *facilities,
48                          unsigned long *vc_fac_mask)
49 {
50         unsigned char *p = skb->data;
51         unsigned int len = *p++;
52
53         *vc_fac_mask = 0;
54
55         while (len > 0) {
56                 switch (*p & X25_FAC_CLASS_MASK) {
57                 case X25_FAC_CLASS_A:
58                         switch (*p) {
59                         case X25_FAC_REVERSE:
60                                 facilities->reverse = p[1] & 0x01;
61                                 *vc_fac_mask |= X25_MASK_REVERSE;
62                                 break;
63                         case X25_FAC_THROUGHPUT:
64                                 facilities->throughput = p[1];
65                                 *vc_fac_mask |= X25_MASK_THROUGHPUT;
66                                 break;
67                         default:
68                                 printk(KERN_DEBUG "X.25: unknown facility "
69                                        "%02X, value %02X\n",
70                                        p[0], p[1]);
71                                 break;
72                         }
73                         p   += 2;
74                         len -= 2;
75                         break;
76                 case X25_FAC_CLASS_B:
77                         switch (*p) {
78                         case X25_FAC_PACKET_SIZE:
79                                 facilities->pacsize_in  = p[1];
80                                 facilities->pacsize_out = p[2];
81                                 *vc_fac_mask |= X25_MASK_PACKET_SIZE;
82                                 break;
83                         case X25_FAC_WINDOW_SIZE:
84                                 facilities->winsize_in  = p[1];
85                                 facilities->winsize_out = p[2];
86                                 *vc_fac_mask |= X25_MASK_WINDOW_SIZE;
87                                 break;
88                         default:
89                                 printk(KERN_DEBUG "X.25: unknown facility "
90                                        "%02X, values %02X, %02X\n",
91                                        p[0], p[1], p[2]);
92                                 break;
93                         }
94                         p   += 3;
95                         len -= 3;
96                         break;
97                 case X25_FAC_CLASS_C:
98                         printk(KERN_DEBUG "X.25: unknown facility %02X, "
99                                "values %02X, %02X, %02X\n",
100                                p[0], p[1], p[2], p[3]);
101                         p   += 4;
102                         len -= 4;
103                         break;
104                 case X25_FAC_CLASS_D:
105                         printk(KERN_DEBUG "X.25: unknown facility %02X, "
106                                "length %d, values %02X, %02X, %02X, %02X\n",
107                                p[0], p[1], p[2], p[3], p[4], p[5]);
108                         len -= p[1] + 2;
109                         p   += p[1] + 2;
110                         break;
111                 }
112         }
113
114         return p - skb->data;
115 }
116
117 /*
118  *      Create a set of facilities.
119  */
120 int x25_create_facilities(unsigned char *buffer,
121                           struct x25_facilities *facilities,
122                           unsigned long facil_mask)
123 {
124         unsigned char *p = buffer + 1;
125         int len;
126
127         if (!facil_mask) {
128                 /*
129                  * Length of the facilities field in call_req or
130                  * call_accept packets
131                  */
132                 buffer[0] = 0;
133                 len = 1; /* 1 byte for the length field */
134                 return len;
135         }
136
137         if (facilities->reverse && (facil_mask & X25_MASK_REVERSE)) {
138                 *p++ = X25_FAC_REVERSE;
139                 *p++ = !!facilities->reverse;
140         }
141
142         if (facilities->throughput && (facil_mask & X25_MASK_THROUGHPUT)) {
143                 *p++ = X25_FAC_THROUGHPUT;
144                 *p++ = facilities->throughput;
145         }
146
147         if ((facilities->pacsize_in || facilities->pacsize_out) &&
148             (facil_mask & X25_MASK_PACKET_SIZE)) {
149                 *p++ = X25_FAC_PACKET_SIZE;
150                 *p++ = facilities->pacsize_in ? : facilities->pacsize_out;
151                 *p++ = facilities->pacsize_out ? : facilities->pacsize_in;
152         }
153
154         if ((facilities->winsize_in || facilities->winsize_out) &&
155             (facil_mask & X25_MASK_WINDOW_SIZE)) {
156                 *p++ = X25_FAC_WINDOW_SIZE;
157                 *p++ = facilities->winsize_in ? : facilities->winsize_out;
158                 *p++ = facilities->winsize_out ? : facilities->winsize_in;
159         }
160
161         len       = p - buffer;
162         buffer[0] = len - 1;
163
164         return len;
165 }
166
167 /*
168  *      Try to reach a compromise on a set of facilities.
169  *
170  *      The only real problem is with reverse charging.
171  */
172 int x25_negotiate_facilities(struct sk_buff *skb, struct sock *sk,
173                              struct x25_facilities *new)
174 {
175         struct x25_opt *x25 = x25_sk(sk);
176         struct x25_facilities *ours = &x25->facilities;
177         struct x25_facilities theirs;
178         int len;
179
180         memset(&theirs, 0, sizeof(theirs));
181         memcpy(new, ours, sizeof(*new));
182
183         len = x25_parse_facilities(skb, &theirs, &x25->vc_facil_mask);
184
185         /*
186          *      They want reverse charging, we won't accept it.
187          */
188         if (theirs.reverse && ours->reverse) {
189                 SOCK_DEBUG(sk, "X.25: rejecting reverse charging request");
190                 return -1;
191         }
192
193         new->reverse = theirs.reverse;
194
195         if (theirs.throughput) {
196                 if (theirs.throughput < ours->throughput) {
197                         SOCK_DEBUG(sk, "X.25: throughput negotiated down");
198                         new->throughput = theirs.throughput;
199                 }
200         }
201
202         if (theirs.pacsize_in && theirs.pacsize_out) {
203                 if (theirs.pacsize_in < ours->pacsize_in) {
204                         SOCK_DEBUG(sk, "X.25: packet size inwards negotiated down");
205                         new->pacsize_in = theirs.pacsize_in;
206                 }
207                 if (theirs.pacsize_out < ours->pacsize_out) {
208                         SOCK_DEBUG(sk, "X.25: packet size outwards negotiated down");
209                         new->pacsize_out = theirs.pacsize_out;
210                 }
211         }
212
213         if (theirs.winsize_in && theirs.winsize_out) {
214                 if (theirs.winsize_in < ours->winsize_in) {
215                         SOCK_DEBUG(sk, "X.25: window size inwards negotiated down");
216                         new->winsize_in = theirs.winsize_in;
217                 }
218                 if (theirs.winsize_out < ours->winsize_out) {
219                         SOCK_DEBUG(sk, "X.25: window size outwards negotiated down");
220                         new->winsize_out = theirs.winsize_out;
221                 }
222         }
223
224         return len;
225 }
226
227 /*
228  *      Limit values of certain facilities according to the capability of the 
229  *      currently attached x25 link.
230  */
231 void x25_limit_facilities(struct x25_facilities *facilities,
232                           struct x25_neigh *nb)
233 {
234
235         if (!nb->extended) {
236                 if (facilities->winsize_in  > 7) {
237                         printk(KERN_DEBUG "X.25: incoming winsize limited to 7\n");
238                         facilities->winsize_in = 7;
239                 }
240                 if (facilities->winsize_out > 7) {
241                         facilities->winsize_out = 7;
242                         printk( KERN_DEBUG "X.25: outgoing winsize limited to 7\n");
243                 }
244         }
245 }