blob: 997ff7b2509b49a3da6d3183fe65512f8f4caa38 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09006 * screw up. It might even work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +090018 * mar/20/00 Daniela Squassoni Disabling/enabling of facilities
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * negotiation.
Shaun Pereiraebc3f642005-06-22 22:16:17 -070020 * apr/14/05 Shaun Pereira - Allow fast select with no restriction
21 * on response.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
wangweidongb73e9e32013-12-06 19:24:33 +080024#define pr_fmt(fmt) "X25: " fmt
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/string.h>
28#include <linux/skbuff.h>
29#include <net/sock.h>
30#include <net/x25.h>
31
andrew hendry95c30432011-02-07 00:08:15 +000032/**
33 * x25_parse_facilities - Parse facilities from skb into the facilities structs
34 *
35 * @skb: sk_buff to parse
Lucas De Marchi25985ed2011-03-30 22:57:33 -030036 * @facilities: Regular facilities, updated as facilities are found
andrew hendry95c30432011-02-07 00:08:15 +000037 * @dte_facs: ITU DTE facilities, updated as DTE facilities are found
38 * @vc_fac_mask: mask is updated with all facilities found
39 *
40 * Return codes:
41 * -1 - Parsing error, caller should drop call and clean up
42 * 0 - Parse OK, this skb has no facilities
43 * >0 - Parse OK, returns the length of the facilities header
44 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -080046int x25_parse_facilities(struct sk_buff *skb, struct x25_facilities *facilities,
47 struct x25_dte_facilities *dte_facs, unsigned long *vc_fac_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Matthew Daleycb101ed2011-10-14 18:45:04 +000049 unsigned char *p;
John Hughesf5eb9172010-04-07 21:29:25 -070050 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 *vc_fac_mask = 0;
53
Shaun Pereiraa64b7b92006-03-22 00:01:31 -080054 /*
55 * The kernel knows which facilities were set on an incoming call but
56 * currently this information is not available to userspace. Here we
57 * give userspace who read incoming call facilities 0 length to indicate
58 * it wasn't set.
59 */
60 dte_facs->calling_len = 0;
61 dte_facs->called_len = 0;
62 memset(dte_facs->called_ae, '\0', sizeof(dte_facs->called_ae));
63 memset(dte_facs->calling_ae, '\0', sizeof(dte_facs->calling_ae));
64
Matthew Daleycb101ed2011-10-14 18:45:04 +000065 if (!pskb_may_pull(skb, 1))
John Hughesf5eb9172010-04-07 21:29:25 -070066 return 0;
67
Matthew Daleycb101ed2011-10-14 18:45:04 +000068 len = skb->data[0];
John Hughesf5eb9172010-04-07 21:29:25 -070069
Matthew Daleycb101ed2011-10-14 18:45:04 +000070 if (!pskb_may_pull(skb, 1 + len))
John Hughesf5eb9172010-04-07 21:29:25 -070071 return -1;
72
Matthew Daleycb101ed2011-10-14 18:45:04 +000073 p = skb->data + 1;
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 while (len > 0) {
76 switch (*p & X25_FAC_CLASS_MASK) {
77 case X25_FAC_CLASS_A:
Dan Rosenberg5ef41302010-11-12 12:44:42 -080078 if (len < 2)
andrew hendry95c30432011-02-07 00:08:15 +000079 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 switch (*p) {
81 case X25_FAC_REVERSE:
Shaun Pereiraebc3f642005-06-22 22:16:17 -070082 if((p[1] & 0x81) == 0x81) {
83 facilities->reverse = p[1] & 0x81;
84 *vc_fac_mask |= X25_MASK_REVERSE;
85 break;
86 }
87
88 if((p[1] & 0x01) == 0x01) {
89 facilities->reverse = p[1] & 0x01;
90 *vc_fac_mask |= X25_MASK_REVERSE;
91 break;
92 }
93
94 if((p[1] & 0x80) == 0x80) {
95 facilities->reverse = p[1] & 0x80;
96 *vc_fac_mask |= X25_MASK_REVERSE;
97 break;
98 }
99
100 if(p[1] == 0x00) {
101 facilities->reverse
102 = X25_DEFAULT_REVERSE;
103 *vc_fac_mask |= X25_MASK_REVERSE;
104 break;
105 }
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 case X25_FAC_THROUGHPUT:
108 facilities->throughput = p[1];
109 *vc_fac_mask |= X25_MASK_THROUGHPUT;
110 break;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800111 case X25_MARKER:
112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 default:
wangweidongb73e9e32013-12-06 19:24:33 +0800114 pr_debug("unknown facility "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 "%02X, value %02X\n",
116 p[0], p[1]);
117 break;
118 }
119 p += 2;
120 len -= 2;
121 break;
122 case X25_FAC_CLASS_B:
Dan Rosenberg5ef41302010-11-12 12:44:42 -0800123 if (len < 3)
andrew hendry95c30432011-02-07 00:08:15 +0000124 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 switch (*p) {
126 case X25_FAC_PACKET_SIZE:
127 facilities->pacsize_in = p[1];
128 facilities->pacsize_out = p[2];
129 *vc_fac_mask |= X25_MASK_PACKET_SIZE;
130 break;
131 case X25_FAC_WINDOW_SIZE:
132 facilities->winsize_in = p[1];
133 facilities->winsize_out = p[2];
134 *vc_fac_mask |= X25_MASK_WINDOW_SIZE;
135 break;
136 default:
wangweidongb73e9e32013-12-06 19:24:33 +0800137 pr_debug("unknown facility "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 "%02X, values %02X, %02X\n",
139 p[0], p[1], p[2]);
140 break;
141 }
142 p += 3;
143 len -= 3;
144 break;
145 case X25_FAC_CLASS_C:
Dan Rosenberg5ef41302010-11-12 12:44:42 -0800146 if (len < 4)
andrew hendry95c30432011-02-07 00:08:15 +0000147 return -1;
wangweidongb73e9e32013-12-06 19:24:33 +0800148 pr_debug("unknown facility %02X, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 "values %02X, %02X, %02X\n",
150 p[0], p[1], p[2], p[3]);
151 p += 4;
152 len -= 4;
153 break;
154 case X25_FAC_CLASS_D:
Dan Rosenberg5ef41302010-11-12 12:44:42 -0800155 if (len < p[1] + 2)
andrew hendry95c30432011-02-07 00:08:15 +0000156 return -1;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800157 switch (*p) {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900158 case X25_FAC_CALLING_AE:
andrew hendrya6331d62010-11-03 12:54:53 +0000159 if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1)
andrew hendry95c30432011-02-07 00:08:15 +0000160 return -1;
Dan Carpenter80aa4e12013-09-03 12:03:40 +0300161 if (p[2] > X25_MAX_AE_LEN)
162 return -1;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800163 dte_facs->calling_len = p[2];
164 memcpy(dte_facs->calling_ae, &p[3], p[1] - 1);
165 *vc_fac_mask |= X25_MASK_CALLING_AE;
166 break;
167 case X25_FAC_CALLED_AE:
andrew hendrya6331d62010-11-03 12:54:53 +0000168 if (p[1] > X25_MAX_DTE_FACIL_LEN || p[1] <= 1)
andrew hendry95c30432011-02-07 00:08:15 +0000169 return -1;
Dan Carpenter80aa4e12013-09-03 12:03:40 +0300170 if (p[2] > X25_MAX_AE_LEN)
171 return -1;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800172 dte_facs->called_len = p[2];
173 memcpy(dte_facs->called_ae, &p[3], p[1] - 1);
174 *vc_fac_mask |= X25_MASK_CALLED_AE;
175 break;
176 default:
wangweidongb73e9e32013-12-06 19:24:33 +0800177 pr_debug("unknown facility %02X,"
Dan Rosenberg5ef41302010-11-12 12:44:42 -0800178 "length %d\n", p[0], p[1]);
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800179 break;
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 len -= p[1] + 2;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800182 p += p[1] + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
184 }
185 }
186
187 return p - skb->data;
188}
189
190/*
191 * Create a set of facilities.
192 */
193int x25_create_facilities(unsigned char *buffer,
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800194 struct x25_facilities *facilities,
195 struct x25_dte_facilities *dte_facs, unsigned long facil_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 unsigned char *p = buffer + 1;
198 int len;
199
200 if (!facil_mask) {
201 /*
202 * Length of the facilities field in call_req or
203 * call_accept packets
204 */
205 buffer[0] = 0;
206 len = 1; /* 1 byte for the length field */
207 return len;
208 }
209
210 if (facilities->reverse && (facil_mask & X25_MASK_REVERSE)) {
211 *p++ = X25_FAC_REVERSE;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700212 *p++ = facilities->reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
215 if (facilities->throughput && (facil_mask & X25_MASK_THROUGHPUT)) {
216 *p++ = X25_FAC_THROUGHPUT;
217 *p++ = facilities->throughput;
218 }
219
220 if ((facilities->pacsize_in || facilities->pacsize_out) &&
221 (facil_mask & X25_MASK_PACKET_SIZE)) {
222 *p++ = X25_FAC_PACKET_SIZE;
223 *p++ = facilities->pacsize_in ? : facilities->pacsize_out;
224 *p++ = facilities->pacsize_out ? : facilities->pacsize_in;
225 }
226
227 if ((facilities->winsize_in || facilities->winsize_out) &&
228 (facil_mask & X25_MASK_WINDOW_SIZE)) {
229 *p++ = X25_FAC_WINDOW_SIZE;
230 *p++ = facilities->winsize_in ? : facilities->winsize_out;
231 *p++ = facilities->winsize_out ? : facilities->winsize_in;
232 }
233
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800234 if (facil_mask & (X25_MASK_CALLING_AE|X25_MASK_CALLED_AE)) {
235 *p++ = X25_MARKER;
236 *p++ = X25_DTE_SERVICES;
237 }
238
239 if (dte_facs->calling_len && (facil_mask & X25_MASK_CALLING_AE)) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000240 unsigned int bytecount = (dte_facs->calling_len + 1) >> 1;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800241 *p++ = X25_FAC_CALLING_AE;
242 *p++ = 1 + bytecount;
243 *p++ = dte_facs->calling_len;
244 memcpy(p, dte_facs->calling_ae, bytecount);
245 p += bytecount;
246 }
247
248 if (dte_facs->called_len && (facil_mask & X25_MASK_CALLED_AE)) {
Eric Dumazet95c96172012-04-15 05:58:06 +0000249 unsigned int bytecount = (dte_facs->called_len % 2) ?
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800250 dte_facs->called_len / 2 + 1 :
251 dte_facs->called_len / 2;
252 *p++ = X25_FAC_CALLED_AE;
253 *p++ = 1 + bytecount;
254 *p++ = dte_facs->called_len;
255 memcpy(p, dte_facs->called_ae, bytecount);
256 p+=bytecount;
257 }
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 len = p - buffer;
260 buffer[0] = len - 1;
261
262 return len;
263}
264
265/*
266 * Try to reach a compromise on a set of facilities.
267 *
268 * The only real problem is with reverse charging.
269 */
270int x25_negotiate_facilities(struct sk_buff *skb, struct sock *sk,
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800271 struct x25_facilities *new, struct x25_dte_facilities *dte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct x25_sock *x25 = x25_sk(sk);
274 struct x25_facilities *ours = &x25->facilities;
275 struct x25_facilities theirs;
276 int len;
277
278 memset(&theirs, 0, sizeof(theirs));
279 memcpy(new, ours, sizeof(*new));
Kangjie Lu79e48652016-05-08 12:10:14 -0400280 memset(dte, 0, sizeof(*dte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800282 len = x25_parse_facilities(skb, &theirs, dte, &x25->vc_facil_mask);
John Hughesf5eb9172010-04-07 21:29:25 -0700283 if (len < 0)
284 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /*
287 * They want reverse charging, we won't accept it.
288 */
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700289 if ((theirs.reverse & 0x01 ) && (ours->reverse & 0x01)) {
Andrew Hendryd2e75432007-01-04 17:00:56 -0800290 SOCK_DEBUG(sk, "X.25: rejecting reverse charging request\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -1;
292 }
293
294 new->reverse = theirs.reverse;
295
296 if (theirs.throughput) {
John Hughesddd04512010-04-04 06:48:10 +0000297 int theirs_in = theirs.throughput & 0x0f;
298 int theirs_out = theirs.throughput & 0xf0;
299 int ours_in = ours->throughput & 0x0f;
300 int ours_out = ours->throughput & 0xf0;
301 if (!ours_in || theirs_in < ours_in) {
302 SOCK_DEBUG(sk, "X.25: inbound throughput negotiated\n");
303 new->throughput = (new->throughput & 0xf0) | theirs_in;
304 }
305 if (!ours_out || theirs_out < ours_out) {
306 SOCK_DEBUG(sk,
307 "X.25: outbound throughput negotiated\n");
308 new->throughput = (new->throughput & 0x0f) | theirs_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 }
311
312 if (theirs.pacsize_in && theirs.pacsize_out) {
313 if (theirs.pacsize_in < ours->pacsize_in) {
Andrew Hendryd2e75432007-01-04 17:00:56 -0800314 SOCK_DEBUG(sk, "X.25: packet size inwards negotiated down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 new->pacsize_in = theirs.pacsize_in;
316 }
317 if (theirs.pacsize_out < ours->pacsize_out) {
Andrew Hendryd2e75432007-01-04 17:00:56 -0800318 SOCK_DEBUG(sk, "X.25: packet size outwards negotiated down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 new->pacsize_out = theirs.pacsize_out;
320 }
321 }
322
323 if (theirs.winsize_in && theirs.winsize_out) {
324 if (theirs.winsize_in < ours->winsize_in) {
Andrew Hendryd2e75432007-01-04 17:00:56 -0800325 SOCK_DEBUG(sk, "X.25: window size inwards negotiated down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 new->winsize_in = theirs.winsize_in;
327 }
328 if (theirs.winsize_out < ours->winsize_out) {
Andrew Hendryd2e75432007-01-04 17:00:56 -0800329 SOCK_DEBUG(sk, "X.25: window size outwards negotiated down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 new->winsize_out = theirs.winsize_out;
331 }
332 }
333
334 return len;
335}
336
337/*
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900338 * Limit values of certain facilities according to the capability of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * currently attached x25 link.
340 */
341void x25_limit_facilities(struct x25_facilities *facilities,
342 struct x25_neigh *nb)
343{
344
345 if (!nb->extended) {
346 if (facilities->winsize_in > 7) {
wangweidongb73e9e32013-12-06 19:24:33 +0800347 pr_debug("incoming winsize limited to 7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 facilities->winsize_in = 7;
349 }
350 if (facilities->winsize_out > 7) {
351 facilities->winsize_out = 7;
wangweidongb73e9e32013-12-06 19:24:33 +0800352 pr_debug("outgoing winsize limited to 7\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 }
355}