aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc/src/gprs/gprs_sndcp_comp.c
blob: cae0039a39316675346aa656dbe45c16c17d936c (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* GPRS SNDCP header compression entity management tools */

/* (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 <math.h>
#include <errno.h>
#include <stdbool.h>

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

#include <openbsc/debug.h>
#include <openbsc/gprs_sndcp_xid.h>
#include <openbsc/gprs_sndcp_comp.h>
#include <openbsc/gprs_sndcp_pcomp.h>
#include <openbsc/gprs_sndcp_dcomp.h>

/* Create a new compression entity from a XID-Field */
static struct gprs_sndcp_comp *gprs_sndcp_comp_create(const void *ctx,
						      const struct
						      gprs_sndcp_comp_field
						      *comp_field)
{
	struct gprs_sndcp_comp *comp_entity;
	comp_entity = talloc_zero(ctx, struct gprs_sndcp_comp);

	/* Copy relevant information from the SNDCP-XID field */
	comp_entity->entity = comp_field->entity;
	comp_entity->comp_len = comp_field->comp_len;
	memcpy(comp_entity->comp, comp_field->comp, sizeof(comp_entity->comp));

	if (comp_field->rfc1144_params) {
		comp_entity->nsapi_len = comp_field->rfc1144_params->nsapi_len;
		memcpy(comp_entity->nsapi,
		       comp_field->rfc1144_params->nsapi,
		       sizeof(comp_entity->nsapi));
	} else if (comp_field->rfc2507_params) {
		comp_entity->nsapi_len = comp_field->rfc2507_params->nsapi_len;
		memcpy(comp_entity->nsapi,
		       comp_field->rfc2507_params->nsapi,
		       sizeof(comp_entity->nsapi));
	} else if (comp_field->rohc_params) {
		comp_entity->nsapi_len = comp_field->rohc_params->nsapi_len;
		memcpy(comp_entity->nsapi, comp_field->rohc_params->nsapi,
		       sizeof(comp_entity->nsapi));
	} else if (comp_field->v42bis_params) {
		comp_entity->nsapi_len = comp_field->v42bis_params->nsapi_len;
		memcpy(comp_entity->nsapi,
		       comp_field->v42bis_params->nsapi,
		       sizeof(comp_entity->nsapi));
	} else if (comp_field->v44_params) {
		comp_entity->nsapi_len = comp_field->v44_params->nsapi_len;
		memcpy(comp_entity->nsapi,
		       comp_field->v42bis_params->nsapi,
		       sizeof(comp_entity->nsapi));
	} else {
		/* The caller is expected to check carefully if the all
		 * data fields required for compression entity creation
		 * are present. Otherwise we blow an assertion here */
		OSMO_ASSERT(false);
	}
	comp_entity->algo = comp_field->algo;

	/* Check if an NSAPI is selected, if not, it does not make sense
	 * to create the compression entity, since the caller should
	 * have checked the presence of the NSAPI, we blow an assertion
	 * in case of missing NSAPIs */
	OSMO_ASSERT(comp_entity->nsapi_len > 0);

	/* Determine of which class our compression entity will be
	 * (Protocol or Data compresson ?) */
	comp_entity->compclass = gprs_sndcp_get_compression_class(comp_field);

	OSMO_ASSERT(comp_entity->compclass != -1);

	/* Create an algorithm specific compression context */
	if (comp_entity->compclass == SNDCP_XID_PROTOCOL_COMPRESSION) {
		if (gprs_sndcp_pcomp_init(ctx, comp_entity, comp_field) != 0) {
			talloc_free(comp_entity);
			comp_entity = NULL;
		}
	} else {
		if (gprs_sndcp_dcomp_init(ctx, comp_entity, comp_field) != 0) {
			talloc_free(comp_entity);
			comp_entity = NULL;
		}
	}

	/* Display info message */
	if (comp_entity == NULL) {
		LOGP(DSNDCP, LOGL_ERROR,
		     "Compression entity (%d) creation failed!\n",
		     comp_entity->entity);
		return NULL;
	}
	if (comp_entity->compclass == SNDCP_XID_PROTOCOL_COMPRESSION) {
		LOGP(DSNDCP, LOGL_INFO,
		     "New header compression entity (%d) created.\n",
		     comp_entity->entity);
	} else {
		LOGP(DSNDCP, LOGL_INFO,
		     "New data compression entity (%d) created.\n",
		     comp_entity->entity);
	}

	return comp_entity;
}

/* Allocate a compression enitiy list */
struct llist_head *gprs_sndcp_comp_alloc(const void *ctx)
{
	struct llist_head *lh;

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

	return lh;
}

/* Free a compression entitiy list */
void gprs_sndcp_comp_free(struct llist_head *comp_entities)
{
	struct gprs_sndcp_comp *comp_entity;

	/* We expect the caller to take care of allocating a
	 * compression entity list properly. Attempting to
	 * free a non existing list clearly points out
	 * a malfunction. */
	OSMO_ASSERT(comp_entities);

	llist_for_each_entry(comp_entity, comp_entities, list) {
		/* Free compression entity */
		if (comp_entity->compclass == SNDCP_XID_PROTOCOL_COMPRESSION) {
			LOGP(DSNDCP, LOGL_INFO,
			     "Deleting header compression entity %d ...\n",
			     comp_entity->entity);
			gprs_sndcp_pcomp_term(comp_entity);
		} else {
			LOGP(DSNDCP, LOGL_INFO,
			     "Deleting data compression entity %d ...\n",
			     comp_entity->entity);
			gprs_sndcp_dcomp_term(comp_entity);
		}
	}

	talloc_free(comp_entities);
}

/* Delete a compression entity */
void gprs_sndcp_comp_delete(struct llist_head *comp_entities,
			    unsigned int entity)
{
	struct gprs_sndcp_comp *comp_entity;
	struct gprs_sndcp_comp *comp_entity_to_delete = NULL;

	OSMO_ASSERT(comp_entities);

	llist_for_each_entry(comp_entity, comp_entities, list) {
		if (comp_entity->entity == entity) {
			comp_entity_to_delete = comp_entity;
			break;
		}
	}

	if (!comp_entity_to_delete)
		return;

	if (comp_entity_to_delete->compclass == SNDCP_XID_PROTOCOL_COMPRESSION) {
		LOGP(DSNDCP, LOGL_INFO,
		     "Deleting header compression entity %d ...\n",
		     comp_entity_to_delete->entity);
		gprs_sndcp_pcomp_term(comp_entity_to_delete);
	} else {
		LOGP(DSNDCP, LOGL_INFO,
		     "Deleting data compression entity %d ...\n",
		     comp_entity_to_delete->entity);
	}

	/* Delete compression entity */
	llist_del(&comp_entity_to_delete->list);
	talloc_free(comp_entity_to_delete);
}

/* Create and Add a new compression entity
 * (returns a pointer to the compression entity that has just been created) */
struct gprs_sndcp_comp *gprs_sndcp_comp_add(const void *ctx,
					    struct llist_head *comp_entities,
					    const struct gprs_sndcp_comp_field
					    *comp_field)
{
	struct gprs_sndcp_comp *comp_entity;

	OSMO_ASSERT(comp_entities);
	OSMO_ASSERT(comp_field);

	/* Just to be sure, if the entity is already in
	 * the list it will be deleted now */
	gprs_sndcp_comp_delete(comp_entities, comp_field->entity);

	/* Create and add a new entity to the list */
	comp_entity = gprs_sndcp_comp_create(ctx, comp_field);

	if (!comp_entity)
		return NULL;

	llist_add(&comp_entity->list, comp_entities);
	return comp_entity;
}

/* Find which compression entity handles the specified pcomp/dcomp */
struct gprs_sndcp_comp *gprs_sndcp_comp_by_comp(const struct llist_head
						*comp_entities, uint8_t comp)
{
	struct gprs_sndcp_comp *comp_entity;
	int i;

	OSMO_ASSERT(comp_entities);

	llist_for_each_entry(comp_entity, comp_entities, list) {
		for (i = 0; i < comp_entity->comp_len; i++) {
			if (comp_entity->comp[i] == comp)
				return comp_entity;
		}
	}

	LOGP(DSNDCP, LOGL_ERROR,
	     "Could not find a matching compression entity for given pcomp/dcomp value %d.\n",
	     comp);
	return NULL;
}

/* Find which compression entity handles the specified nsapi */
struct gprs_sndcp_comp *gprs_sndcp_comp_by_nsapi(const struct llist_head
						 *comp_entities, uint8_t nsapi)
{
	struct gprs_sndcp_comp *comp_entity;
	int i;

	OSMO_ASSERT(comp_entities);

	llist_for_each_entry(comp_entity, comp_entities, list) {
		for (i = 0; i < comp_entity->nsapi_len; i++) {
			if (comp_entity->nsapi[i] == nsapi)
				return comp_entity;
		}
	}

	return NULL;
}

/* Find a comp_index for a given pcomp/dcomp value */
uint8_t gprs_sndcp_comp_get_idx(const struct gprs_sndcp_comp *comp_entity,
				uint8_t comp)
{
	/* Note: This function returns a normalized version of the comp value,
	 * which matches up with the position of the comp field. Since comp=0
	 * is reserved for "no compression", the index value starts counting
	 * at one. The return value is the PCOMPn/DCOMPn value one can find
	 * in the Specification (see e.g. 3GPP TS 44.065, 6.5.3.2, Table 7) */

	int i;
	OSMO_ASSERT(comp_entity);

	/* A pcomp/dcomp value of zero is reserved for "no comproession",
	 * So we just bail and return zero in this case */
	if (comp == 0)
		return 0;

	/* Look in the pcomp/dcomp list for the index */
	for (i = 0; i < comp_entity->comp_len; i++) {
		if (comp_entity->comp[i] == comp)
			return i + 1;
	}

	LOGP(DSNDCP, LOGL_ERROR,
	     "Could not find a matching comp_index for given pcomp/dcomp value %d\n",
	     comp);
	return 0;
}

/* Find a pcomp/dcomp value for a given comp_index */
uint8_t gprs_sndcp_comp_get_comp(const struct gprs_sndcp_comp *comp_entity,
			         uint8_t comp_index)
{
	OSMO_ASSERT(comp_entity);

	/* A comp_index of zero translates to zero right away. */
	if (comp_index == 0)
		return 0;

	if (comp_index > comp_entity->comp_len) {
		LOGP(DSNDCP, LOGL_ERROR,
		     "Could not find a matching pcomp/dcomp value for given comp_index value %d.\n",
		     comp_index);
		return 0;
	}

	/* Look in the pcomp/dcomp list for the comp_index, see
	 * note in gprs_sndcp_comp_get_idx() */
	return comp_entity->comp[comp_index - 1];
}