blob: 6b15bd06c0a26d867bb3d57fd1c8542897c486a4 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <errno.h>
25#include <ctype.h>
26
27#include <netinet/in.h>
28
Jonathan Santos5a45b152011-08-17 15:33:57 -040029#include <osmocom/core/linuxlist.h>
30#include <osmocom/core/talloc.h>
31#include <osmocom/gsm/gsm_utils.h>
32#include <osmocom/gsm/abis_nm.h>
33#include <osmocom/core/statistics.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040034
35#include <openbsc/gsm_data.h>
36#include <openbsc/osmo_msc_data.h>
37#include <openbsc/abis_nm.h>
38
39void *tall_bsc_ctx;
40
41static LLIST_HEAD(bts_models);
42
Jonathan Santos5a45b152011-08-17 15:33:57 -040043void set_ts_e1link(struct gsm_bts_trx_ts *ts, uint8_t e1_nr,
44 uint8_t e1_ts, uint8_t e1_ts_ss)
Jonathan Santos03fd8d02011-05-25 13:54:02 -040045{
46 ts->e1_link.e1_nr = e1_nr;
47 ts->e1_link.e1_ts = e1_ts;
48 ts->e1_link.e1_ts_ss = e1_ts_ss;
49}
50
Jonathan Santos03fd8d02011-05-25 13:54:02 -040051static struct gsm_bts_model *bts_model_find(enum gsm_bts_type type)
52{
53 struct gsm_bts_model *model;
54
55 llist_for_each_entry(model, &bts_models, list) {
56 if (model->type == type)
57 return model;
58 }
59
60 return NULL;
61}
62
63int gsm_bts_model_register(struct gsm_bts_model *model)
64{
65 if (bts_model_find(model->type))
66 return -EEXIST;
67
Jonathan Santos5a45b152011-08-17 15:33:57 -040068 tlv_def_patch(&model->nm_att_tlvdef, &abis_nm_att_tlvdef);
Jonathan Santos03fd8d02011-05-25 13:54:02 -040069 llist_add_tail(&model->list, &bts_models);
70 return 0;
71}
72
Jonathan Santos5a45b152011-08-17 15:33:57 -040073struct gsm_network *gsm_network_init(uint16_t country_code, uint16_t network_code,
Jonathan Santos03fd8d02011-05-25 13:54:02 -040074 int (*mncc_recv)(struct gsm_network *, struct msgb *))
75{
76 struct gsm_network *net;
77
78 net = talloc_zero(tall_bsc_ctx, struct gsm_network);
79 if (!net)
80 return NULL;
81
82 net->msc_data = talloc_zero(net, struct osmo_msc_data);
83 if (!net->msc_data) {
84 talloc_free(net);
85 return NULL;
86 }
87
Jonathan Santos5a45b152011-08-17 15:33:57 -040088 /* Init back pointer */
89 net->msc_data->network = net;
90
Jonathan Santos03fd8d02011-05-25 13:54:02 -040091 net->country_code = country_code;
92 net->network_code = network_code;
93 net->num_bts = 0;
94 net->reject_cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
95 net->T3101 = GSM_T3101_DEFAULT;
96 net->T3113 = GSM_T3113_DEFAULT;
97 /* FIXME: initialize all other timers! */
98
99 /* default set of handover parameters */
100 net->handover.win_rxlev_avg = 10;
101 net->handover.win_rxqual_avg = 1;
102 net->handover.win_rxlev_avg_neigh = 10;
103 net->handover.pwr_interval = 6;
104 net->handover.pwr_hysteresis = 3;
105 net->handover.max_distance = 9999;
106
107 INIT_LLIST_HEAD(&net->trans_list);
108 INIT_LLIST_HEAD(&net->upqueue);
109 INIT_LLIST_HEAD(&net->bts_list);
110
Jonathan Santos5a45b152011-08-17 15:33:57 -0400111 net->stats.chreq.total = osmo_counter_alloc("net.chreq.total");
112 net->stats.chreq.no_channel = osmo_counter_alloc("net.chreq.no_channel");
113 net->stats.handover.attempted = osmo_counter_alloc("net.handover.attempted");
114 net->stats.handover.no_channel = osmo_counter_alloc("net.handover.no_channel");
115 net->stats.handover.timeout = osmo_counter_alloc("net.handover.timeout");
116 net->stats.handover.completed = osmo_counter_alloc("net.handover.completed");
117 net->stats.handover.failed = osmo_counter_alloc("net.handover.failed");
118 net->stats.loc_upd_type.attach = osmo_counter_alloc("net.loc_upd_type.attach");
119 net->stats.loc_upd_type.normal = osmo_counter_alloc("net.loc_upd_type.normal");
120 net->stats.loc_upd_type.periodic = osmo_counter_alloc("net.loc_upd_type.periodic");
121 net->stats.loc_upd_type.detach = osmo_counter_alloc("net.imsi_detach.count");
122 net->stats.loc_upd_resp.reject = osmo_counter_alloc("net.loc_upd_resp.reject");
123 net->stats.loc_upd_resp.accept = osmo_counter_alloc("net.loc_upd_resp.accept");
124 net->stats.paging.attempted = osmo_counter_alloc("net.paging.attempted");
125 net->stats.paging.detached = osmo_counter_alloc("net.paging.detached");
126 net->stats.paging.completed = osmo_counter_alloc("net.paging.completed");
127 net->stats.paging.expired = osmo_counter_alloc("net.paging.expired");
128 net->stats.sms.submitted = osmo_counter_alloc("net.sms.submitted");
129 net->stats.sms.no_receiver = osmo_counter_alloc("net.sms.no_receiver");
130 net->stats.sms.delivered = osmo_counter_alloc("net.sms.delivered");
131 net->stats.sms.rp_err_mem = osmo_counter_alloc("net.sms.rp_err_mem");
132 net->stats.sms.rp_err_other = osmo_counter_alloc("net.sms.rp_err_other");
133 net->stats.call.mo_setup = osmo_counter_alloc("net.call.mo_setup");
134 net->stats.call.mo_connect_ack = osmo_counter_alloc("net.call.mo_connect_ack");
135 net->stats.call.mt_setup = osmo_counter_alloc("net.call.mt_setup");
136 net->stats.call.mt_connect = osmo_counter_alloc("net.call.mt_connect");
137 net->stats.chan.rf_fail = osmo_counter_alloc("net.chan.rf_fail");
138 net->stats.chan.rll_err = osmo_counter_alloc("net.chan.rll_err");
139 net->stats.bts.oml_fail = osmo_counter_alloc("net.bts.oml_fail");
140 net->stats.bts.rsl_fail = osmo_counter_alloc("net.bts.rsl_fail");
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400141
142 net->mncc_recv = mncc_recv;
143
Jonathan Santos5a45b152011-08-17 15:33:57 -0400144 INIT_LLIST_HEAD(&net->msc_data->dests);
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400145 net->msc_data->ping_timeout = 20;
146 net->msc_data->pong_timeout = 5;
147 net->msc_data->core_ncc = -1;
148 net->msc_data->core_mcc = -1;
149 net->msc_data->rtp_base = 4000;
150
151 gsm_net_update_ctype(net);
152
153 return net;
154}
155
156struct gsm_bts *gsm_bts_num(struct gsm_network *net, int num)
157{
158 struct gsm_bts *bts;
159
160 if (num >= net->num_bts)
161 return NULL;
162
163 llist_for_each_entry(bts, &net->bts_list, list) {
164 if (bts->nr == num)
165 return bts;
166 }
167
168 return NULL;
169}
170
171/* Get reference to a neighbor cell on a given BCCH ARFCN */
172struct gsm_bts *gsm_bts_neighbor(const struct gsm_bts *bts,
Jonathan Santos5a45b152011-08-17 15:33:57 -0400173 uint16_t arfcn, uint8_t bsic)
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400174{
175 struct gsm_bts *neigh;
176 /* FIXME: use some better heuristics here to determine which cell
177 * using this ARFCN really is closest to the target cell. For
178 * now we simply assume that each ARFCN will only be used by one
179 * cell */
180
181 llist_for_each_entry(neigh, &bts->network->bts_list, list) {
182 if (neigh->c0->arfcn == arfcn &&
183 neigh->bsic == bsic)
184 return neigh;
185 }
186
187 return NULL;
188}
189
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400190static const struct value_string bts_types[] = {
191 { GSM_BTS_TYPE_UNKNOWN, "unknown" },
192 { GSM_BTS_TYPE_BS11, "bs11" },
193 { GSM_BTS_TYPE_NANOBTS, "nanobts" },
194 { GSM_BTS_TYPE_RBS2000, "rbs2000" },
195 { GSM_BTS_TYPE_HSL_FEMTO, "hsl_femto" },
196 { 0, NULL }
197};
198
199enum gsm_bts_type parse_btstype(const char *arg)
200{
201 return get_string_value(bts_types, arg);
202}
203
204const char *btstype2str(enum gsm_bts_type type)
205{
206 return get_value_string(bts_types, type);
207}
208
209struct gsm_bts_trx *gsm_bts_trx_by_nr(struct gsm_bts *bts, int nr)
210{
211 struct gsm_bts_trx *trx;
212
213 llist_for_each_entry(trx, &bts->trx_list, list) {
214 if (trx->nr == nr)
215 return trx;
216 }
217 return NULL;
218}
219
220/* Search for a BTS in the given Location Area; optionally start searching
221 * with start_bts (for continuing to search after the first result) */
222struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac,
223 struct gsm_bts *start_bts)
224{
225 int i;
226 struct gsm_bts *bts;
227 int skip = 0;
228
229 if (start_bts)
230 skip = 1;
231
232 for (i = 0; i < net->num_bts; i++) {
233 bts = gsm_bts_num(net, i);
234
235 if (skip) {
236 if (start_bts == bts)
237 skip = 0;
238 continue;
239 }
240
241 if (lac == GSM_LAC_RESERVED_ALL_BTS || bts->location_area_code == lac)
242 return bts;
243 }
244 return NULL;
245}
246
247static const struct value_string auth_policy_names[] = {
248 { GSM_AUTH_POLICY_CLOSED, "closed" },
249 { GSM_AUTH_POLICY_ACCEPT_ALL, "accept-all" },
250 { GSM_AUTH_POLICY_TOKEN, "token" },
251 { 0, NULL }
252};
253
254enum gsm_auth_policy gsm_auth_policy_parse(const char *arg)
255{
256 return get_string_value(auth_policy_names, arg);
257}
258
259const char *gsm_auth_policy_name(enum gsm_auth_policy policy)
260{
261 return get_value_string(auth_policy_names, policy);
262}
263
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400264static const struct value_string rrlp_mode_names[] = {
265 { RRLP_MODE_NONE, "none" },
266 { RRLP_MODE_MS_BASED, "ms-based" },
267 { RRLP_MODE_MS_PREF, "ms-preferred" },
268 { RRLP_MODE_ASS_PREF, "ass-preferred" },
269 { 0, NULL }
270};
271
272enum rrlp_mode rrlp_mode_parse(const char *arg)
273{
274 return get_string_value(rrlp_mode_names, arg);
275}
276
277const char *rrlp_mode_name(enum rrlp_mode mode)
278{
279 return get_value_string(rrlp_mode_names, mode);
280}
281
282static const struct value_string bts_gprs_mode_names[] = {
283 { BTS_GPRS_NONE, "none" },
284 { BTS_GPRS_GPRS, "gprs" },
285 { BTS_GPRS_EGPRS, "egprs" },
286 { 0, NULL }
287};
288
289enum bts_gprs_mode bts_gprs_mode_parse(const char *arg)
290{
291 return get_string_value(bts_gprs_mode_names, arg);
292}
293
294const char *bts_gprs_mode_name(enum bts_gprs_mode mode)
295{
296 return get_value_string(bts_gprs_mode_names, mode);
297}
298
299struct gsm_meas_rep *lchan_next_meas_rep(struct gsm_lchan *lchan)
300{
301 struct gsm_meas_rep *meas_rep;
302
303 meas_rep = &lchan->meas_rep[lchan->meas_rep_idx];
304 memset(meas_rep, 0, sizeof(*meas_rep));
305 meas_rep->lchan = lchan;
306 lchan->meas_rep_idx = (lchan->meas_rep_idx + 1)
307 % ARRAY_SIZE(lchan->meas_rep);
308
309 return meas_rep;
310}
311
312int gsm_btsmodel_set_feature(struct gsm_bts_model *bts, enum gsm_bts_features feat)
313{
314 return bitvec_set_bit_pos(&bts->features, feat, 1);
315}
316
317int gsm_bts_has_feature(struct gsm_bts *bts, enum gsm_bts_features feat)
318{
319 return bitvec_get_bit_pos(&bts->model->features, feat);
320}
321
322int gsm_set_bts_type(struct gsm_bts *bts, enum gsm_bts_type type)
323{
324 struct gsm_bts_model *model;
325
326 model = bts_model_find(type);
327 if (!model)
328 return -EINVAL;
329
330 bts->type = type;
331 bts->model = model;
332
Jonathan Santos5a45b152011-08-17 15:33:57 -0400333 if (model->start && !model->started) {
334 int ret = model->start(bts->network);
335 if (ret < 0)
336 return ret;
337
338 model->started = true;
339 }
340
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400341 switch (bts->type) {
342 case GSM_BTS_TYPE_HSL_FEMTO:
343 bts->c0->rsl_tei = 0;
344 case GSM_BTS_TYPE_NANOBTS:
345 /* Set the default OML Stream ID to 0xff */
346 bts->oml_tei = 0xff;
347 bts->c0->nominal_power = 23;
348 break;
349 case GSM_BTS_TYPE_BS11:
350 case GSM_BTS_TYPE_UNKNOWN:
351 break;
352 case GSM_BTS_TYPE_RBS2000:
353 INIT_LLIST_HEAD(&bts->rbs2000.is.conn_groups);
354 INIT_LLIST_HEAD(&bts->rbs2000.con.conn_groups);
355 break;
356 }
357
358 return 0;
359}
Jonathan Santos5a45b152011-08-17 15:33:57 -0400360
361struct gsm_bts *gsm_bts_alloc_register(struct gsm_network *net, enum gsm_bts_type type,
362 uint8_t tsc, uint8_t bsic)
363{
364 struct gsm_bts_model *model = bts_model_find(type);
365 struct gsm_bts *bts;
366
367 if (!model && type != GSM_BTS_TYPE_UNKNOWN)
368 return NULL;
369
370 bts = gsm_bts_alloc(net);
371 if (!bts)
372 return NULL;
373
374 bts->network = net;
375 bts->nr = net->num_bts++;
376 bts->type = type;
377 bts->model = model;
378 bts->tsc = tsc;
379 bts->bsic = bsic;
380
381 bts->neigh_list_manual_mode = 0;
382 bts->si_common.cell_sel_par.cell_resel_hyst = 2; /* 4 dB */
383 bts->si_common.cell_sel_par.rxlev_acc_min = 0;
384 bts->si_common.neigh_list.data = bts->si_common.data.neigh_list;
385 bts->si_common.neigh_list.data_len =
386 sizeof(bts->si_common.data.neigh_list);
387 bts->si_common.si5_neigh_list.data = bts->si_common.data.si5_neigh_list;
388 bts->si_common.si5_neigh_list.data_len =
389 sizeof(bts->si_common.data.si5_neigh_list);
390 bts->si_common.cell_alloc.data = bts->si_common.data.cell_alloc;
391 bts->si_common.cell_alloc.data_len =
392 sizeof(bts->si_common.data.cell_alloc);
393 bts->si_common.rach_control.re = 1; /* no re-establishment */
394 bts->si_common.rach_control.tx_integer = 9; /* 12 slots spread - 217/115 slots delay */
395 bts->si_common.rach_control.max_trans = 3; /* 7 retransmissions */
396 bts->si_common.rach_control.t2 = 4; /* no emergency calls */
397
398 llist_add_tail(&bts->list, &net->bts_list);
399
400 INIT_LLIST_HEAD(&bts->abis_queue);
401
402 return bts;
403}
404
405void gprs_ra_id_by_bts(struct gprs_ra_id *raid, struct gsm_bts *bts)
406{
407 raid->mcc = bts->network->country_code;
408 raid->mnc = bts->network->network_code;
409 raid->lac = bts->location_area_code;
410 raid->rac = bts->gprs.rac;
411}
412
413int gsm48_ra_id_by_bts(uint8_t *buf, struct gsm_bts *bts)
414{
415 struct gprs_ra_id raid;
416
417 gprs_ra_id_by_bts(&raid, bts);
418
419 return gsm48_construct_ra(buf, &raid);
420}