summaryrefslogtreecommitdiffstats
path: root/include/openbsc/gsm_data.h
blob: 715ff1bdddd00a0100d0a943ba7da9b72b215b5d (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#ifndef _GSM_DATA_H
#define _GSM_DATA_H

#include <stdint.h>

#include <osmocom/core/timer.h>
#include <osmocom/core/select.h>

#include <openbsc/rest_octets.h>

#define OBSC_NM_W_ACK_CB(__msgb) (__msgb)->cb[3]

/* the data structure stored in msgb->cb for openbsc apps */
struct openbsc_msgb_cb {
	unsigned char *bssgph;
	unsigned char *llch;

	/* Cell Identifier */
	unsigned char *bssgp_cell_id;

	/* Identifiers of a BTS, equal to 'struct bssgp_bts_ctx' */
	uint16_t nsei;
	uint16_t bvci;

	/* Identifier of a MS (inside BTS), equal to 'struct sgsn_mm_ctx' */
	uint32_t tlli;
} __attribute__((packed));
#define OBSC_MSGB_CB(__msgb)	((struct openbsc_msgb_cb *)&((__msgb)->cb[0]))
#define msgb_tlli(__x)		OBSC_MSGB_CB(__x)->tlli
#define msgb_nsei(__x)		OBSC_MSGB_CB(__x)->nsei
#define msgb_bvci(__x)		OBSC_MSGB_CB(__x)->bvci
#define msgb_gmmh(__x)		(__x)->l3h
#define msgb_bssgph(__x)	OBSC_MSGB_CB(__x)->bssgph
#define msgb_bssgp_len(__x)	((__x)->tail - (uint8_t *)msgb_bssgph(__x))
#define msgb_bcid(__x)		OBSC_MSGB_CB(__x)->bssgp_cell_id
#define msgb_llch(__x)		OBSC_MSGB_CB(__x)->llch

#define OBSC_LINKID_CB(__msgb)	(__msgb)->cb[3]

enum gsm_security_event {
	GSM_SECURITY_NOAVAIL,
	GSM_SECURITY_AUTH_FAILED,
	GSM_SECURITY_SUCCEEDED,
};

struct msgb;
typedef int gsm_cbfn(unsigned int hooknum,
		     unsigned int event,
		     struct msgb *msg,
		     void *data, void *param);

/* Real authentication information containing Ki */
enum gsm_auth_algo {
	AUTH_ALGO_NONE,
	AUTH_ALGO_XOR,
	AUTH_ALGO_COMP128v1,
};

struct gsm_auth_info {
	enum gsm_auth_algo auth_algo;
	unsigned int a3a8_ki_len;
	uint8_t a3a8_ki[16];
};

struct gsm_auth_tuple {
	int use_count;
	int key_seq;
	uint8_t rand[16];
	uint8_t sres[4];
	uint8_t kc[8];
};
#define GSM_KEY_SEQ_INVAL	7	/* GSM 04.08 - 10.5.1.2 */

/*
 * LOCATION UPDATING REQUEST state
 *
 * Our current operation is:
 *	- Get imei/tmsi
 *	- Accept/Reject according to global policy
 */
struct gsm_loc_updating_operation {
        struct osmo_timer_list updating_timer;
	unsigned int waiting_for_imsi : 1;
	unsigned int waiting_for_imei : 1;
	unsigned int key_seq : 4;
};

/*
 * AUTHENTICATION/CIPHERING state
 */
struct gsm_security_operation {
	struct gsm_auth_tuple atuple;
	gsm_cbfn *cb;
	void *cb_data;
};

/*
 * A dummy to keep a connection up for at least
 * a couple of seconds to work around MSC issues.
 */
struct gsm_anchor_operation {
	struct osmo_timer_list timeout;
};

/* Maximum number of neighbor cells whose average we track */
#define MAX_NEIGH_MEAS		10
/* Maximum size of the averaging window for neighbor cells */
#define MAX_WIN_NEIGH_AVG	10

/* processed neighbor measurements for one cell */
struct neigh_meas_proc {
	uint16_t arfcn;
	uint8_t bsic;
	uint8_t rxlev[MAX_WIN_NEIGH_AVG];
	unsigned int rxlev_cnt;
	uint8_t last_seen_nr;
};

/* the per subscriber data for lchan */
struct gsm_subscriber_connection {
	struct llist_head entry;

	/* To whom we are allocated at the moment */
	struct gsm_subscriber *subscr;

	/*
	 * Operations that have a state and might be pending
	 */
	struct gsm_loc_updating_operation *loc_operation;
	struct gsm_security_operation *sec_operation;
	struct gsm_anchor_operation *anch_operation;

	/* Are we part of a special "silent" call */
	int silent_call;
	int put_channel;

	/* bsc structures */
	struct osmo_bsc_sccp_con *sccp_con;

	/* back pointers */
	int in_release;
	struct gsm_lchan *lchan;
	struct gsm_lchan *ho_lchan;
	struct gsm_bts *bts;

	/* for assignment handling */
	struct osmo_timer_list T10;
	struct gsm_lchan *secondary_lchan;

};


#define ROLE_BSC
#include "gsm_data_shared.h"


/* Some statistics of our network */
struct gsmnet_stats {
	struct {
		struct osmo_counter *total;
		struct osmo_counter *no_channel;
	} chreq;
	struct {
		struct osmo_counter *attempted;
		struct osmo_counter *no_channel;	/* no channel available */
		struct osmo_counter *timeout;		/* T3103 timeout */
		struct osmo_counter *completed;	/* HO COMPL received */
		struct osmo_counter *failed;		/* HO FAIL received */
	} handover;
	struct {
		struct osmo_counter *attach;
		struct osmo_counter *normal;
		struct osmo_counter *periodic;
		struct osmo_counter *detach;
	} loc_upd_type;
	struct {
		struct osmo_counter *reject;
		struct osmo_counter *accept;
	} loc_upd_resp;
	struct {
		struct osmo_counter *attempted;
		struct osmo_counter *detached;
		struct osmo_counter *completed;
		struct osmo_counter *expired;
	} paging;
	struct {
		struct osmo_counter *submitted; /* MO SMS submissions */
		struct osmo_counter *no_receiver;
		struct osmo_counter *delivered; /* MT SMS deliveries */
		struct osmo_counter *rp_err_mem;
		struct osmo_counter *rp_err_other;
	} sms;
	struct {
		struct osmo_counter *mo_setup;
		struct osmo_counter *mo_connect_ack;
		struct osmo_counter *mt_setup;
		struct osmo_counter *mt_connect;
	} call;
	struct {
		struct osmo_counter *rf_fail;
		struct osmo_counter *rll_err;
	} chan;
	struct {
		struct osmo_counter *oml_fail;
		struct osmo_counter *rsl_fail;
	} bts;
};

enum gsm_auth_policy {
	GSM_AUTH_POLICY_CLOSED, /* only subscribers authorized in DB */
	GSM_AUTH_POLICY_ACCEPT_ALL, /* accept everyone, even if not authorized in DB */
	GSM_AUTH_POLICY_TOKEN, /* accept first, send token per sms, then revoke authorization */
};

#define GSM_T3101_DEFAULT 10
#define GSM_T3113_DEFAULT 60

struct gsm_network {
	/* global parameters */
	uint16_t country_code;
	uint16_t network_code;
	char *name_long;
	char *name_short;
	enum gsm_auth_policy auth_policy;
	enum gsm48_reject_value reject_cause;
	int a5_encryption;
	int neci;
	int send_mm_info;
	struct {
		int active;
		/* Window RXLEV averaging */
		unsigned int win_rxlev_avg;	/* number of SACCH frames */
		/* Window RXQUAL averaging */
		unsigned int win_rxqual_avg;	/* number of SACCH frames */
		/* Window RXLEV neighbouring cells averaging */
		unsigned int win_rxlev_avg_neigh; /* number of SACCH frames */

		/* how often should we check for power budget HO */
		unsigned int pwr_interval;	/* SACCH frames */
		/* how much better does a neighbor cell have to be ? */
		unsigned int pwr_hysteresis;	/* dBm */
		/* maximum distacne before we try a handover */
		unsigned int max_distance;	/* TA values */
	} handover;

	struct gsmnet_stats stats;

	/* layer 4 */
	int (*mncc_recv) (struct gsm_network *net, struct msgb *msg);
	struct llist_head upqueue;
	struct llist_head trans_list;
	struct bsc_api *bsc_api;

	unsigned int num_bts;
	struct llist_head bts_list;

	/* timer values */
	int T3101;
	int T3103;
	int T3105;
	int T3107;
	int T3109;
	int T3111;
	int T3113;
	int T3115;
	int T3117;
	int T3119;
	int T3122;
	int T3141;

	/* Radio Resource Location Protocol (TS 04.31) */
	struct {
		enum rrlp_mode mode;
	} rrlp;

	/* enable the DTXu and DTXd for this network */
	int dtx_enabled;

	enum gsm_chan_t ctype_by_chreq[16];

	/* Use a TCH for handling requests of type paging any */
	int pag_any_tch;

	/* MSC data in case we are a true BSC */
	struct osmo_msc_data *msc_data;
	int hardcoded_rtp_payload;

	/* subscriber related features */
	int keep_subscr;
	struct gsm_sms_queue *sms_queue;
};

#define SMS_HDR_SIZE	128
#define SMS_TEXT_SIZE	256
struct gsm_sms {
	unsigned long long id;
	struct gsm_subscriber *sender;
	struct gsm_subscriber *receiver;

	unsigned long validity_minutes;
	uint8_t reply_path_req;
	uint8_t status_rep_req;
	uint8_t ud_hdr_ind;
	uint8_t protocol_id;
	uint8_t data_coding_scheme;
	uint8_t msg_ref;
	char dest_addr[20+1];	/* DA LV is 12 bytes max, i.e. 10 bytes
				 * BCD == 20 bytes string */
	uint8_t user_data_len;
	uint8_t user_data[SMS_TEXT_SIZE];

	char text[SMS_TEXT_SIZE];
};

struct gsm_network *gsm_network_init(uint16_t country_code, uint16_t network_code,
				     int (*mncc_recv)(struct gsm_network *, struct msgb *));
int gsm_set_bts_type(struct gsm_bts *bts, enum gsm_bts_type type);

struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num);

/* Get reference to a neighbor cell on a given BCCH ARFCN */
struct gsm_bts *gsm_bts_neighbor(const struct gsm_bts *bts,
				 uint16_t arfcn, uint8_t bsic);

enum gsm_bts_type parse_btstype(const char *arg);
const char *btstype2str(enum gsm_bts_type type);
struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
				struct gsm_bts *start_bts);

extern void *tall_bsc_ctx;
extern int ipacc_rtp_direct;

static inline int is_ipaccess_bts(struct gsm_bts *bts)
{
	switch (bts->type) {
	case GSM_BTS_TYPE_NANOBTS:
		return 1;
	default:
		break;
	}
	return 0;
}

static inline int is_siemens_bts(struct gsm_bts *bts)
{
	switch (bts->type) {
	case GSM_BTS_TYPE_BS11:
		return 1;
	default:
		break;
	}

	return 0;
}

enum gsm_auth_policy gsm_auth_policy_parse(const char *arg);
const char *gsm_auth_policy_name(enum gsm_auth_policy policy);

enum rrlp_mode rrlp_mode_parse(const char *arg);
const char *rrlp_mode_name(enum rrlp_mode mode);

enum bts_gprs_mode bts_gprs_mode_parse(const char *arg);
const char *bts_gprs_mode_name(enum bts_gprs_mode mode);

int gsm48_ra_id_by_bts(uint8_t *buf, struct gsm_bts *bts);
void gprs_ra_id_by_bts(struct gprs_ra_id *raid, struct gsm_bts *bts);
struct gsm_meas_rep *lchan_next_meas_rep(struct gsm_lchan *lchan);

int gsm_btsmodel_set_feature(struct gsm_bts_model *model, enum gsm_bts_features feat);
int gsm_bts_model_register(struct gsm_bts_model *model);

struct gsm_subscriber_connection *subscr_con_allocate(struct gsm_lchan *lchan);
void subscr_con_free(struct gsm_subscriber_connection *conn);

struct gsm_bts *gsm_bts_alloc_register(struct gsm_network *net,
					enum gsm_bts_type type,
					uint8_t tsc, uint8_t bsic);
void set_ts_e1link(struct gsm_bts_trx_ts *ts, uint8_t e1_nr,
		   uint8_t e1_ts, uint8_t e1_ts_ss);

void gsm_trx_lock_rf(struct gsm_bts_trx *trx, int locked);
int gsm_bts_has_feature(struct gsm_bts *bts, enum gsm_bts_features feat);
struct gsm_bts_trx *gsm_bts_trx_by_nr(struct gsm_bts *bts, int nr);

#endif /* _GSM_DATA_H */