Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
f8e1d201 | 6 | * screw up. It might even work. |
1da177e4 LT |
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 | |
f8e1d201 | 18 | * mar/20/00 Daniela Squassoni Disabling/enabling of facilities |
1da177e4 | 19 | * negotiation. |
ebc3f64b SP |
20 | * apr/14/05 Shaun Pereira - Allow fast select with no restriction |
21 | * on response. | |
1da177e4 LT |
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/x25.h> | |
29 | ||
95c30430 | 30 | /** |
31 | * x25_parse_facilities - Parse facilities from skb into the facilities structs | |
32 | * | |
33 | * @skb: sk_buff to parse | |
25985edc | 34 | * @facilities: Regular facilities, updated as facilities are found |
95c30430 | 35 | * @dte_facs: ITU DTE facilities, updated as DTE facilities are found |
36 | * @vc_fac_mask: mask is updated with all facilities found | |
37 | * | |
38 | * Return codes: | |
39 | * -1 - Parsing error, caller should drop call and clean up | |
40 | * 0 - Parse OK, this skb has no facilities | |
41 | * >0 - Parse OK, returns the length of the facilities header | |
42 | * | |
1da177e4 | 43 | */ |
a64b7b93 SP |
44 | int x25_parse_facilities(struct sk_buff *skb, struct x25_facilities *facilities, |
45 | struct x25_dte_facilities *dte_facs, unsigned long *vc_fac_mask) | |
1da177e4 | 46 | { |
cb101ed2 | 47 | unsigned char *p; |
f5eb917b | 48 | unsigned int len; |
1da177e4 LT |
49 | |
50 | *vc_fac_mask = 0; | |
51 | ||
a64b7b93 SP |
52 | /* |
53 | * The kernel knows which facilities were set on an incoming call but | |
54 | * currently this information is not available to userspace. Here we | |
55 | * give userspace who read incoming call facilities 0 length to indicate | |
56 | * it wasn't set. | |
57 | */ | |
58 | dte_facs->calling_len = 0; | |
59 | dte_facs->called_len = 0; | |
60 | memset(dte_facs->called_ae, '\0', sizeof(dte_facs->called_ae)); | |
61 | memset(dte_facs->calling_ae, '\0', sizeof(dte_facs->calling_ae)); | |
62 | ||
cb101ed2 | 63 | if (!pskb_may_pull(skb, 1)) |
f5eb917b JH |
64 | return 0; |
65 | ||
cb101ed2 | 66 | len = skb->data[0]; |
f5eb917b | 67 | |
cb101ed2 | 68 | if (!pskb_may_pull(skb, 1 + len)) |
f5eb917b JH |
69 | return -1; |
70 | ||
cb101ed2 MD |
71 | p = skb->data + 1; |
72 | ||
1da177e4 LT |
73 | while (len > 0) { |
74 | switch (*p & X25_FAC_CLASS_MASK) { | |
75 | case X25_FAC_CLASS_A: | |
5ef41308 | 76 | if (len < 2) |
95c30430 | 77 | return -1; |
1da177e4 LT |
78 | switch (*p) { |
79 | case X25_FAC_REVERSE: | |
ebc3f64b SP |
80 | if((p[1] & 0x81) == 0x81) { |
81 | facilities->reverse = p[1] & 0x81; | |
82 | *vc_fac_mask |= X25_MASK_REVERSE; | |
83 | break; | |
84 | } | |
85 | ||
86 | if((p[1] & 0x01) == 0x01) { | |
87 | facilities->reverse = p[1] & 0x01; | |
88 | *vc_fac_mask |= X25_MASK_REVERSE; | |
89 | break; | |
90 | } | |
91 | ||
92 | if((p[1] & 0x80) == 0x80) { | |
93 | facilities->reverse = p[1] & 0x80; | |
94 | *vc_fac_mask |= X25_MASK_REVERSE; | |
95 | break; | |
96 | } | |
97 | ||
98 | if(p[1] == 0x00) { | |
99 | facilities->reverse | |
100 | = X25_DEFAULT_REVERSE; | |
101 | *vc_fac_mask |= X25_MASK_REVERSE; | |
102 | break; | |
103 | } | |
104 | ||
1da177e4 LT |
105 | case X25_FAC_THROUGHPUT: |
106 | facilities->throughput = p[1]; | |
107 | *vc_fac_mask |= X25_MASK_THROUGHPUT; | |
108 | break; | |
a64b7b93 SP |
109 | case X25_MARKER: |
110 | break; | |
1da177e4 LT |
111 | default: |
112 | printk(KERN_DEBUG "X.25: unknown facility " | |
113 | "%02X, value %02X\n", | |
114 | p[0], p[1]); | |
115 | break; | |
116 | } | |
117 | p += 2; | |
118 | len -= 2; | |
119 | break; | |
120 | case X25_FAC_CLASS_B: | |
5ef41308 | 121 | if (len < 3) |
95c30430 | 122 | return -1; |
1da177e4 LT |
123 | switch (*p) { |
124 | case X25_FAC_PACKET_SIZE: | |
125 | facilities->pacsize_in = p[1]; | |
126 | facilities->pacsize_out = p[2]; | |
127 | *vc_fac_mask |= X25_MASK_PACKET_SIZE; | |
128 | break; | |
129 | case X25_FAC_WINDOW_SIZE: | |
130 | facilities->winsize_in = p[1]; | |
131 | facilities->winsize_out = p[2]; | |
132 | *vc_fac_mask |= X25_MASK_WINDOW_SIZE; | |
133 | break; | |
134 | default: | |
135 | printk(KERN_DEBUG "X.25: unknown facility " | |
136 | "%02X, values %02X, %02X\n", | |
137 | p[0], p[1], p[2]); | |
138 | break; | |
139 | } | |
140 | p += 3; | |
141 | len -= 3; | |
142 | break; | |
143 | case X25_FAC_CLASS_C: | |
5ef41308 | 144 | if (len < 4) |
95c30430 | 145 | return -1; |
1da177e4 LT |
146 | printk(KERN_DEBUG "X.25: unknown facility %02X, " |
147 | "values %02X, %02X, %02X\n", | |
148 | p[0], p[1], p[2], p[3]); | |
149 | p += 4; | |
150 | len -= 4; | |
151 | break; | |
152 | case X25_FAC_CLASS_D: | |
5ef41308 | 153 | if (len < p[1] + 2) |
95c30430 | 154 | return -1; |
a64b7b93 | 155 | switch (*p) { |
f8e1d201 | 156 | case X25_FAC_CALLING_AE: |
a6331d6f | 157 | if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1) |
95c30430 | 158 | return -1; |
80aa4e10 DC |
159 | if (p[2] > X25_MAX_AE_LEN) |
160 | return -1; | |
a64b7b93 SP |
161 | dte_facs->calling_len = p[2]; |
162 | memcpy(dte_facs->calling_ae, &p[3], p[1] - 1); | |
163 | *vc_fac_mask |= X25_MASK_CALLING_AE; | |
164 | break; | |
165 | case X25_FAC_CALLED_AE: | |
a6331d6f | 166 | if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1) |
95c30430 | 167 | return -1; |
80aa4e10 DC |
168 | if (p[2] > X25_MAX_AE_LEN) |
169 | return -1; | |
a64b7b93 SP |
170 | dte_facs->called_len = p[2]; |
171 | memcpy(dte_facs->called_ae, &p[3], p[1] - 1); | |
172 | *vc_fac_mask |= X25_MASK_CALLED_AE; | |
173 | break; | |
174 | default: | |
175 | printk(KERN_DEBUG "X.25: unknown facility %02X," | |
5ef41308 | 176 | "length %d\n", p[0], p[1]); |
a64b7b93 SP |
177 | break; |
178 | } | |
1da177e4 | 179 | len -= p[1] + 2; |
a64b7b93 | 180 | p += p[1] + 2; |
1da177e4 LT |
181 | break; |
182 | } | |
183 | } | |
184 | ||
185 | return p - skb->data; | |
186 | } | |
187 | ||
188 | /* | |
189 | * Create a set of facilities. | |
190 | */ | |
191 | int x25_create_facilities(unsigned char *buffer, | |
a64b7b93 SP |
192 | struct x25_facilities *facilities, |
193 | struct x25_dte_facilities *dte_facs, unsigned long facil_mask) | |
1da177e4 LT |
194 | { |
195 | unsigned char *p = buffer + 1; | |
196 | int len; | |
197 | ||
198 | if (!facil_mask) { | |
199 | /* | |
200 | * Length of the facilities field in call_req or | |
201 | * call_accept packets | |
202 | */ | |
203 | buffer[0] = 0; | |
204 | len = 1; /* 1 byte for the length field */ | |
205 | return len; | |
206 | } | |
207 | ||
208 | if (facilities->reverse && (facil_mask & X25_MASK_REVERSE)) { | |
209 | *p++ = X25_FAC_REVERSE; | |
ebc3f64b | 210 | *p++ = facilities->reverse; |
1da177e4 LT |
211 | } |
212 | ||
213 | if (facilities->throughput && (facil_mask & X25_MASK_THROUGHPUT)) { | |
214 | *p++ = X25_FAC_THROUGHPUT; | |
215 | *p++ = facilities->throughput; | |
216 | } | |
217 | ||
218 | if ((facilities->pacsize_in || facilities->pacsize_out) && | |
219 | (facil_mask & X25_MASK_PACKET_SIZE)) { | |
220 | *p++ = X25_FAC_PACKET_SIZE; | |
221 | *p++ = facilities->pacsize_in ? : facilities->pacsize_out; | |
222 | *p++ = facilities->pacsize_out ? : facilities->pacsize_in; | |
223 | } | |
224 | ||
225 | if ((facilities->winsize_in || facilities->winsize_out) && | |
226 | (facil_mask & X25_MASK_WINDOW_SIZE)) { | |
227 | *p++ = X25_FAC_WINDOW_SIZE; | |
228 | *p++ = facilities->winsize_in ? : facilities->winsize_out; | |
229 | *p++ = facilities->winsize_out ? : facilities->winsize_in; | |
230 | } | |
231 | ||
a64b7b93 SP |
232 | if (facil_mask & (X25_MASK_CALLING_AE|X25_MASK_CALLED_AE)) { |
233 | *p++ = X25_MARKER; | |
234 | *p++ = X25_DTE_SERVICES; | |
235 | } | |
236 | ||
237 | if (dte_facs->calling_len && (facil_mask & X25_MASK_CALLING_AE)) { | |
95c96174 | 238 | unsigned int bytecount = (dte_facs->calling_len + 1) >> 1; |
a64b7b93 SP |
239 | *p++ = X25_FAC_CALLING_AE; |
240 | *p++ = 1 + bytecount; | |
241 | *p++ = dte_facs->calling_len; | |
242 | memcpy(p, dte_facs->calling_ae, bytecount); | |
243 | p += bytecount; | |
244 | } | |
245 | ||
246 | if (dte_facs->called_len && (facil_mask & X25_MASK_CALLED_AE)) { | |
95c96174 | 247 | unsigned int bytecount = (dte_facs->called_len % 2) ? |
a64b7b93 SP |
248 | dte_facs->called_len / 2 + 1 : |
249 | dte_facs->called_len / 2; | |
250 | *p++ = X25_FAC_CALLED_AE; | |
251 | *p++ = 1 + bytecount; | |
252 | *p++ = dte_facs->called_len; | |
253 | memcpy(p, dte_facs->called_ae, bytecount); | |
254 | p+=bytecount; | |
255 | } | |
256 | ||
1da177e4 LT |
257 | len = p - buffer; |
258 | buffer[0] = len - 1; | |
259 | ||
260 | return len; | |
261 | } | |
262 | ||
263 | /* | |
264 | * Try to reach a compromise on a set of facilities. | |
265 | * | |
266 | * The only real problem is with reverse charging. | |
267 | */ | |
268 | int x25_negotiate_facilities(struct sk_buff *skb, struct sock *sk, | |
a64b7b93 | 269 | struct x25_facilities *new, struct x25_dte_facilities *dte) |
1da177e4 LT |
270 | { |
271 | struct x25_sock *x25 = x25_sk(sk); | |
272 | struct x25_facilities *ours = &x25->facilities; | |
273 | struct x25_facilities theirs; | |
274 | int len; | |
275 | ||
276 | memset(&theirs, 0, sizeof(theirs)); | |
277 | memcpy(new, ours, sizeof(*new)); | |
278 | ||
a64b7b93 | 279 | len = x25_parse_facilities(skb, &theirs, dte, &x25->vc_facil_mask); |
f5eb917b JH |
280 | if (len < 0) |
281 | return len; | |
1da177e4 LT |
282 | |
283 | /* | |
284 | * They want reverse charging, we won't accept it. | |
285 | */ | |
ebc3f64b | 286 | if ((theirs.reverse & 0x01 ) && (ours->reverse & 0x01)) { |
d2e7543c | 287 | SOCK_DEBUG(sk, "X.25: rejecting reverse charging request\n"); |
1da177e4 LT |
288 | return -1; |
289 | } | |
290 | ||
291 | new->reverse = theirs.reverse; | |
292 | ||
293 | if (theirs.throughput) { | |
ddd0451f JH |
294 | int theirs_in = theirs.throughput & 0x0f; |
295 | int theirs_out = theirs.throughput & 0xf0; | |
296 | int ours_in = ours->throughput & 0x0f; | |
297 | int ours_out = ours->throughput & 0xf0; | |
298 | if (!ours_in || theirs_in < ours_in) { | |
299 | SOCK_DEBUG(sk, "X.25: inbound throughput negotiated\n"); | |
300 | new->throughput = (new->throughput & 0xf0) | theirs_in; | |
301 | } | |
302 | if (!ours_out || theirs_out < ours_out) { | |
303 | SOCK_DEBUG(sk, | |
304 | "X.25: outbound throughput negotiated\n"); | |
305 | new->throughput = (new->throughput & 0x0f) | theirs_out; | |
1da177e4 LT |
306 | } |
307 | } | |
308 | ||
309 | if (theirs.pacsize_in && theirs.pacsize_out) { | |
310 | if (theirs.pacsize_in < ours->pacsize_in) { | |
d2e7543c | 311 | SOCK_DEBUG(sk, "X.25: packet size inwards negotiated down\n"); |
1da177e4 LT |
312 | new->pacsize_in = theirs.pacsize_in; |
313 | } | |
314 | if (theirs.pacsize_out < ours->pacsize_out) { | |
d2e7543c | 315 | SOCK_DEBUG(sk, "X.25: packet size outwards negotiated down\n"); |
1da177e4 LT |
316 | new->pacsize_out = theirs.pacsize_out; |
317 | } | |
318 | } | |
319 | ||
320 | if (theirs.winsize_in && theirs.winsize_out) { | |
321 | if (theirs.winsize_in < ours->winsize_in) { | |
d2e7543c | 322 | SOCK_DEBUG(sk, "X.25: window size inwards negotiated down\n"); |
1da177e4 LT |
323 | new->winsize_in = theirs.winsize_in; |
324 | } | |
325 | if (theirs.winsize_out < ours->winsize_out) { | |
d2e7543c | 326 | SOCK_DEBUG(sk, "X.25: window size outwards negotiated down\n"); |
1da177e4 LT |
327 | new->winsize_out = theirs.winsize_out; |
328 | } | |
329 | } | |
330 | ||
331 | return len; | |
332 | } | |
333 | ||
334 | /* | |
f8e1d201 | 335 | * Limit values of certain facilities according to the capability of the |
1da177e4 LT |
336 | * currently attached x25 link. |
337 | */ | |
338 | void x25_limit_facilities(struct x25_facilities *facilities, | |
339 | struct x25_neigh *nb) | |
340 | { | |
341 | ||
342 | if (!nb->extended) { | |
343 | if (facilities->winsize_in > 7) { | |
344 | printk(KERN_DEBUG "X.25: incoming winsize limited to 7\n"); | |
345 | facilities->winsize_in = 7; | |
346 | } | |
347 | if (facilities->winsize_out > 7) { | |
348 | facilities->winsize_out = 7; | |
349 | printk( KERN_DEBUG "X.25: outgoing winsize limited to 7\n"); | |
350 | } | |
351 | } | |
352 | } |