aboutsummaryrefslogtreecommitdiffstats
path: root/src/gprs/gprs_llc_xid.c
blob: fe631715a3502ccff38d49a15779c1c83e9a2d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* GPRS LLC XID field encoding/decoding as per 3GPP TS 44.064 */

/* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * All Rights Reserved
 *
 * Author: Philipp Maier
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>

#include <osmocom/core/utils.h>
#include <osmocom/core/linuxlist.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/talloc.h>

#include <openbsc/debug.h>
#include <openbsc/gprs_llc.h>
#include <openbsc/sgsn.h>
#include <openbsc/gprs_llc_xid.h>

const struct value_string gprs_llc_xid_type_names[] = {
	{ GPRS_LLC_XID_T_VERSION,	"VERSION"},
	{ GPRS_LLC_XID_T_IOV_UI,	"IOV_UI"},
	{ GPRS_LLC_XID_T_IOV_I,		"IOV_I"},
	{ GPRS_LLC_XID_T_T200,		"T200"},
	{ GPRS_LLC_XID_T_N200,		"N200"},
	{ GPRS_LLC_XID_T_N201_U,	"N201_"},
	{ GPRS_LLC_XID_T_N201_I,	"N201_I"},
	{ GPRS_LLC_XID_T_mD,		"mD"},
	{ GPRS_LLC_XID_T_mU,		"mU"},
	{ GPRS_LLC_XID_T_kD,		"kD"},
	{ GPRS_LLC_XID_T_kU,		"kU"},
	{ GPRS_LLC_XID_T_L3_PAR,	"L3_PAR"},
	{ GPRS_LLC_XID_T_RESET,		"RESET"},
	{ 0, NULL },
};

/* Parse XID parameter field */
static int decode_xid_field(struct gprs_llc_xid_field *xid_field,
			    const uint8_t *src, uint8_t src_len)
{
	uint8_t xl;
	uint8_t type;
	uint8_t len;
	int src_counter = 0;

	/* Exit immediately if it is clear that no
	 * parseable data is present */
	if (src_len < 1 || !src)
		return -EINVAL;

	/* Extract header info */
	xl = (*src >> 7) & 1;
	type = (*src >> 2) & 0x1F;

	/* Extract length field */
	len = (*src) & 0x3;
	src++;
	src_counter++;
	if (xl) {
		if (src_len < 2)
			return -EINVAL;
		len = (len << 6) & 0xC0;
		len |= ((*src) >> 2) & 0x3F;
		src++;
		src_counter++;
	}

	/* Fill out struct */
	xid_field->type = type;
	xid_field->data_len = len;
	if (len > 0) {
		if (src_len < src_counter + len)
			return -EINVAL;
		xid_field->data =
			talloc_memdup(xid_field,src,xid_field->data_len);
	} else
		xid_field->data = NULL;

	/* Return consumed length */
	return src_counter + len;
}

/* Encode XID parameter field */
static int encode_xid_field(uint8_t *dst, int dst_maxlen,
			    const struct gprs_llc_xid_field *xid_field)
{
	int xl = 0;

	/* When the length does not fit into 2 bits,
	 * we need extended length fields */
	if (xid_field->data_len > 3)
		xl = 1;

	/* Exit immediately if it is clear that no
	 * encoding result can be stored */
	if (dst_maxlen < xid_field->data_len + 1 + xl)
		return -EINVAL;

	/* There are only 5 bits reserved for the type, exit on exceed */
	if (xid_field->type > 31)
		return -EINVAL;

	/* Encode header */
	memset(dst, 0, dst_maxlen);
	if (xl)
		dst[0] |= 0x80;
	dst[0] |= (((xid_field->type) & 0x1F) << 2);

	if (xl) {
		dst[0] |= (((xid_field->data_len) >> 6) & 0x03);
		dst[1] = ((xid_field->data_len) << 2) & 0xFC;
	} else
		dst[0] |= ((xid_field->data_len) & 0x03);

	/* Append payload data */
	if (xid_field->data && xid_field->data_len)
		memcpy(dst + 1 + xl, xid_field->data, xid_field->data_len);

	/* Return generated length */
	return xid_field->data_len + 1 + xl;
}

/* Transform a list with XID fields into a XID message (dst) */
int gprs_llc_compile_xid(uint8_t *dst, int dst_maxlen,
			 const struct llist_head *xid_fields)
{
	struct gprs_llc_xid_field *xid_field;
	int rc;
	int byte_counter = 0;

	OSMO_ASSERT(xid_fields);
	OSMO_ASSERT(dst);

	llist_for_each_entry_reverse(xid_field, xid_fields, list) {
		/* Encode XID-Field */
		rc = encode_xid_field(dst, dst_maxlen, xid_field);
		if (rc < 0)
			return -EINVAL;

		/* Advance pointer and lower maxlen for the
		 * next encoding round */
		dst += rc;
		byte_counter += rc;
		dst_maxlen -= rc;
	}

	/* Return generated length */
	return byte_counter;
}

/* Transform a XID message (dst) into a list of XID fields */
struct llist_head *gprs_llc_parse_xid(const void *ctx, const uint8_t *src,
				      int src_len)
{
	struct gprs_llc_xid_field *xid_field;
	struct llist_head *xid_fields;

	int rc;
	int max_loops = src_len;

	OSMO_ASSERT(src);

	xid_fields = talloc_zero(ctx, struct llist_head);
	INIT_LLIST_HEAD(xid_fields);

	while (1) {
		/* Bail in case decode_xid_field() constantly returns zero */
		if (max_loops <= 0) {
			talloc_free(xid_fields);
			return NULL;
		}

		/* Decode XID field */
		xid_field = talloc_zero(xid_fields, struct gprs_llc_xid_field);
		rc = decode_xid_field(xid_field, src, src_len);

		/* Immediately stop on error */
		if (rc < 0) {
			talloc_free(xid_fields);
			return NULL;
		}

		/* Add parsed XID field to list */
		llist_add(&xid_field->list, xid_fields);

		/* Advance pointer and lower dst_len for the next
		 * decoding round */
		src += rc;
		src_len -= rc;

		/* We are (scuccessfully) done when no further byes are left */
		if (src_len == 0)
			return xid_fields;

		max_loops--;
	}
}

/* Create a duplicate of an XID-Field */
struct gprs_llc_xid_field *gprs_llc_dup_xid_field(const void *ctx, const struct
						  gprs_llc_xid_field
						  *xid_field)
{
	struct gprs_llc_xid_field *dup;

	OSMO_ASSERT(xid_field);

	/* Create a copy of the XID field in memory */
	dup = talloc_memdup(ctx, xid_field, sizeof(*xid_field));
	dup->data = talloc_memdup(ctx, xid_field->data, xid_field->data_len);

	/* Unlink duplicate from source list */
	INIT_LLIST_HEAD(&dup->list);

	return dup;
}

/* Copy an llist with xid fields */
struct llist_head *gprs_llc_copy_xid(const void *ctx,
				     const struct llist_head *xid_fields)
{
	struct gprs_llc_xid_field *xid_field;
	struct llist_head *xid_fields_copy;

	OSMO_ASSERT(xid_fields);

	xid_fields_copy = talloc_zero(ctx, struct llist_head);
	INIT_LLIST_HEAD(xid_fields_copy);

	/* Create duplicates and add them to the target list */
	llist_for_each_entry(xid_field, xid_fields, list) {
		llist_add(&gprs_llc_dup_xid_field(ctx, xid_field)->list,
			  xid_fields_copy);
	}

	return xid_fields_copy;
}

/* Dump a list with XID fields (Debug) */
void gprs_llc_dump_xid_fields(const struct llist_head *xid_fields,
			      unsigned int logl)
{
	struct gprs_llc_xid_field *xid_field;

	OSMO_ASSERT(xid_fields);

	llist_for_each_entry(xid_field, xid_fields, list) {
		if (xid_field->data_len) {
			OSMO_ASSERT(xid_field->data);
			LOGP(DLLC, logl,
			     "XID: type %s, data_len=%d, data=%s\n",
			     get_value_string(gprs_llc_xid_type_names,
					      xid_field->type),
			     xid_field->data_len,
			     osmo_hexdump_nospc(xid_field->data,
						xid_field->data_len));
		} else {
			LOGP(DLLC, logl,
			     "XID: type=%d, data_len=%d, data=NULL\n",
			     xid_field->type, xid_field->data_len);
		}
	}
}