blob: 04452f745d80f05952ba439bef9324f0989e0da7 [file] [log] [blame]
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +02001/* A hackish minimal BSC (+MSC +HLR) implementation */
2
Harald Welte40152872010-05-16 20:52:23 +02003/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +02004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte0e3e88e2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020016 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020019 *
20 */
21
22#include <openbsc/gsm_data.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010023#include <osmocom/gsm/gsm_utils.h>
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020024#include <openbsc/gsm_04_08.h>
25#include <openbsc/abis_rsl.h>
26#include <openbsc/abis_nm.h>
27#include <openbsc/debug.h>
28#include <openbsc/misdn.h>
Harald Weltebd9591f2010-05-19 19:45:32 +020029#include <osmocom/vty/telnet_interface.h>
Harald Welte6bcd3032014-08-24 17:54:49 +020030#include <osmocom/vty/ports.h>
Harald Weltea54a2bb2009-12-01 18:04:30 +053031#include <openbsc/system_information.h>
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020032#include <openbsc/paging.h>
33#include <openbsc/signal.h>
Holger Hans Peter Freyther5a8b2b42010-04-15 11:21:02 +020034#include <openbsc/chan_alloc.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010035#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther82260a62010-11-15 20:43:02 +010036#include <openbsc/ipaccess.h>
Alexander Huemer6489afd2011-09-06 00:09:50 +020037#include <osmocom/gsm/sysinfo.h>
38#include <openbsc/e1_config.h>
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020039
40/* global pointer to the gsm network data structure */
41extern struct gsm_network *bsc_gsmnet;
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020042
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020043/* Callback function for NACK on the OML NM */
Holger Hans Peter Freytherdfea6c82010-07-14 02:08:35 +080044static int oml_msg_nack(struct nm_nack_signal_data *nack)
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020045{
Holger Hans Peter Freytherdfea6c82010-07-14 02:08:35 +080046 if (nack->mt == NM_MT_SET_BTS_ATTR_NACK) {
Holger Hans Peter Freyther82260a62010-11-15 20:43:02 +010047
Holger Hans Peter Freyther860efdf2011-05-21 00:05:09 +020048 LOGP(DNM, LOGL_ERROR, "Failed to set BTS attributes. That is fatal. "
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020049 "Was the bts type and frequency properly specified?\n");
Holger Hans Peter Freyther860efdf2011-05-21 00:05:09 +020050 goto drop_bts;
Holger Hans Peter Freyther82260a62010-11-15 20:43:02 +010051 } else {
52 LOGP(DNM, LOGL_ERROR, "Got a NACK going to drop the OML links.\n");
Holger Hans Peter Freyther860efdf2011-05-21 00:05:09 +020053 goto drop_bts;
54 }
55
56 return 0;
57
58drop_bts:
Holger Hans Peter Freyther70f1e472012-11-11 18:26:23 +010059 if (!nack->bts) {
60 LOGP(DNM, LOGL_ERROR, "Unknown bts. Can not drop it.\n");
Harald Weltef8878b32012-10-05 15:48:54 +020061 return 0;
Holger Hans Peter Freyther70f1e472012-11-11 18:26:23 +010062 }
Harald Weltef8878b32012-10-05 15:48:54 +020063
Holger Hans Peter Freyther70f1e472012-11-11 18:26:23 +010064 if (is_ipaccess_bts(nack->bts))
65 ipaccess_drop_oml(nack->bts);
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020066
67 return 0;
68}
69
70/* Callback function to be called every time we receive a signal from NM */
71static int nm_sig_cb(unsigned int subsys, unsigned int signal,
72 void *handler_data, void *signal_data)
73{
Holger Hans Peter Freytherdfea6c82010-07-14 02:08:35 +080074 struct nm_nack_signal_data *nack;
Harald Welte6a21c732009-11-17 06:09:56 +010075
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020076 switch (signal) {
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020077 case S_NM_NACK:
Holger Hans Peter Freytherdfea6c82010-07-14 02:08:35 +080078 nack = signal_data;
79 return oml_msg_nack(nack);
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020080 default:
81 break;
82 }
83 return 0;
84}
85
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020086int bsc_shutdown_net(struct gsm_network *net)
87{
88 struct gsm_bts *bts;
89
90 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welte4c826f72011-01-14 15:55:42 +010091 LOGP(DNM, LOGL_NOTICE, "shutting down OML for BTS %u\n", bts->nr);
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020092 osmo_signal_dispatch(SS_L_GLOBAL, S_GLOBAL_BTS_CLOSE_OM, bts);
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020093 }
94
95 return 0;
96}
97
Andreas Eversberg02de47d2012-02-29 09:04:07 +010098static int rsl_si(struct gsm_bts_trx *trx, enum osmo_sysinfo_type i, int si_len)
Harald Welte99bed522010-06-15 16:44:12 +020099{
100 struct gsm_bts *bts = trx->bts;
Holger Hans Peter Freyther1b0af512013-07-03 16:06:20 +0200101 int rc;
Harald Welte99bed522010-06-15 16:44:12 +0200102
Harald Welte6b77f0f2011-05-24 15:02:20 +0200103 DEBUGP(DRR, "SI%s: %s\n", get_value_string(osmo_sitype_strs, i),
Pablo Neira Ayusob1d5a692011-05-07 12:12:48 +0200104 osmo_hexdump(GSM_BTS_SI(bts, i), GSM_MACBLOCK_LEN));
Harald Welte99bed522010-06-15 16:44:12 +0200105
106 switch (i) {
107 case SYSINFO_TYPE_5:
108 case SYSINFO_TYPE_5bis:
109 case SYSINFO_TYPE_5ter:
110 case SYSINFO_TYPE_6:
Holger Hans Peter Freyther1b0af512013-07-03 16:06:20 +0200111 rc = rsl_sacch_filling(trx, osmo_sitype2rsl(i),
112 GSM_BTS_SI(bts, i), si_len);
Harald Welte99bed522010-06-15 16:44:12 +0200113 break;
114 default:
Harald Welte6b77f0f2011-05-24 15:02:20 +0200115 rc = rsl_bcch_info(trx, osmo_sitype2rsl(i),
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100116 GSM_BTS_SI(bts, i), si_len);
Harald Welte99bed522010-06-15 16:44:12 +0200117 break;
118 }
119
120 return rc;
121}
122
Alexander Chemeris143ac5f2015-05-30 14:40:54 -0400123/* set all system information types for a TRX */
Holger Hans Peter Freytherd0284c82014-11-21 11:18:45 +0100124int gsm_bts_trx_set_system_infos(struct gsm_bts_trx *trx)
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200125{
Harald Weltea54a2bb2009-12-01 18:04:30 +0530126 int i, rc;
Harald Welte36e5a792009-12-21 23:12:19 +0100127 struct gsm_bts *bts = trx->bts;
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100128 uint8_t gen_si[_MAX_SYSINFO_TYPE], n_si = 0, n;
129 int si_len[_MAX_SYSINFO_TYPE];
Harald Welte36e5a792009-12-21 23:12:19 +0100130
131 bts->si_common.cell_sel_par.ms_txpwr_max_ccch =
132 ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
133 bts->si_common.cell_sel_par.neci = bts->network->neci;
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200134
Harald Welted8acf142010-07-30 11:50:09 +0200135 /* First, we determine which of the SI messages we actually need */
136
Harald Welte99bed522010-06-15 16:44:12 +0200137 if (trx == bts->c0) {
Harald Welted8acf142010-07-30 11:50:09 +0200138 /* 1...4 are always present on a C0 TRX */
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100139 gen_si[n_si++] = SYSINFO_TYPE_1;
140 gen_si[n_si++] = SYSINFO_TYPE_2;
141 gen_si[n_si++] = SYSINFO_TYPE_2bis;
142 gen_si[n_si++] = SYSINFO_TYPE_2ter;
Maxcab5beb2016-04-15 16:04:44 +0200143 gen_si[n_si++] = SYSINFO_TYPE_2quater;
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100144 gen_si[n_si++] = SYSINFO_TYPE_3;
145 gen_si[n_si++] = SYSINFO_TYPE_4;
Harald Welted8acf142010-07-30 11:50:09 +0200146
147 /* 13 is always present on a C0 TRX of a GPRS BTS */
148 if (bts->gprs.mode != BTS_GPRS_NONE)
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100149 gen_si[n_si++] = SYSINFO_TYPE_13;
Harald Welte28fc9632009-12-21 17:02:32 +0100150 }
151
Harald Welted8acf142010-07-30 11:50:09 +0200152 /* 5 and 6 are always present on every TRX */
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100153 gen_si[n_si++] = SYSINFO_TYPE_5;
154 gen_si[n_si++] = SYSINFO_TYPE_5bis;
155 gen_si[n_si++] = SYSINFO_TYPE_5ter;
156 gen_si[n_si++] = SYSINFO_TYPE_6;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530157
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100158 /* Second, we generate the selected SI via RSL */
159
160 for (n = 0; n < n_si; n++) {
161 i = gen_si[n];
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100162 /* Only generate SI if this SI is not in "static" (user-defined) mode */
163 if (!(bts->si_mode_static & (1 << i))) {
Andreas Eversbergaaf29702013-01-08 19:33:42 +0100164 /* Set SI as being valid. gsm_generate_si() might unset
165 * it, if SI is not required. */
166 bts->si_valid |= (1 << i);
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100167 rc = gsm_generate_si(bts, i);
168 if (rc < 0)
169 goto err_out;
170 si_len[i] = rc;
171 } else {
172 if (i == SYSINFO_TYPE_5 || i == SYSINFO_TYPE_5bis
173 || i == SYSINFO_TYPE_5ter)
174 si_len[i] = 18;
175 else if (i == SYSINFO_TYPE_6)
176 si_len[i] = 11;
177 else
178 si_len[i] = 23;
179 }
180 }
181
182 /* Third, we send the selected SI via RSL */
183
Andreas Eversbergaaf29702013-01-08 19:33:42 +0100184 for (n = 0; n < n_si; n++) {
185 i = gen_si[n];
Harald Welted8acf142010-07-30 11:50:09 +0200186 if (!(bts->si_valid & (1 << i)))
187 continue;
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100188 rc = rsl_si(trx, i, si_len[i]);
Harald Welted8acf142010-07-30 11:50:09 +0200189 if (rc < 0)
Andreas Eversberg02de47d2012-02-29 09:04:07 +0100190 return rc;
Harald Welted8acf142010-07-30 11:50:09 +0200191 }
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200192
193 return 0;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530194err_out:
Max88ddcaa2016-04-15 16:04:46 +0200195 LOGP(DRR, LOGL_ERROR, "Cannot generate SI%s for BTS %u: error <%s>,"
196 "most likely a problem with neighbor cell list generation\n",
197 get_value_string(osmo_sitype_strs, i), bts->nr, strerror(-rc));
Harald Weltea54a2bb2009-12-01 18:04:30 +0530198 return rc;
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200199}
200
Alexander Chemeris143ac5f2015-05-30 14:40:54 -0400201/* set all system information types for a BTS */
202int gsm_bts_set_system_infos(struct gsm_bts *bts)
203{
204 struct gsm_bts_trx *trx;
205
206 /* Generate a new ID */
207 bts->bcch_change_mark += 1;
208 bts->bcch_change_mark %= 0x7;
209
210 llist_for_each_entry(trx, &bts->trx_list, list) {
211 int rc;
212
213 rc = gsm_bts_trx_set_system_infos(trx);
214 if (rc != 0)
215 return rc;
216 }
217
218 return 0;
219}
220
Harald Weltebf6624d2010-06-15 08:52:12 +0200221/* Produce a MA as specified in 10.5.2.21 */
222static int generate_ma_for_ts(struct gsm_bts_trx_ts *ts)
223{
224 /* we have three bitvecs: the per-timeslot ARFCNs, the cell chan ARFCNs
225 * and the MA */
226 struct bitvec *cell_chan = &ts->trx->bts->si_common.cell_alloc;
227 struct bitvec *ts_arfcn = &ts->hopping.arfcns;
228 struct bitvec *ma = &ts->hopping.ma;
Harald Welte6332a8a2010-06-15 16:45:51 +0200229 unsigned int num_cell_arfcns, bitnum, n_chan;
Harald Weltebf6624d2010-06-15 08:52:12 +0200230 int i;
231
232 /* re-set the MA to all-zero */
233 ma->cur_bit = 0;
Harald Welte6332a8a2010-06-15 16:45:51 +0200234 ts->hopping.ma_len = 0;
Harald Weltebf6624d2010-06-15 08:52:12 +0200235 memset(ma->data, 0, ma->data_len);
236
237 if (!ts->hopping.enabled)
238 return 0;
239
Harald Welte6332a8a2010-06-15 16:45:51 +0200240 /* count the number of ARFCNs in the cell channel allocation */
241 num_cell_arfcns = 0;
Harald Welte09e4fad2012-01-29 13:24:12 +0100242 for (i = 0; i < 1024; i++) {
Harald Welte6332a8a2010-06-15 16:45:51 +0200243 if (bitvec_get_bit_pos(cell_chan, i))
244 num_cell_arfcns++;
245 }
246
247 /* pad it to octet-aligned number of bits */
248 ts->hopping.ma_len = num_cell_arfcns / 8;
249 if (num_cell_arfcns % 8)
250 ts->hopping.ma_len++;
251
252 n_chan = 0;
Harald Welte09e4fad2012-01-29 13:24:12 +0100253 for (i = 0; i < 1024; i++) {
Harald Weltebf6624d2010-06-15 08:52:12 +0200254 if (!bitvec_get_bit_pos(cell_chan, i))
255 continue;
Harald Welte6332a8a2010-06-15 16:45:51 +0200256 /* set the corresponding bit in the MA */
257 bitnum = (ts->hopping.ma_len * 8) - 1 - n_chan;
Harald Weltebf6624d2010-06-15 08:52:12 +0200258 if (bitvec_get_bit_pos(ts_arfcn, i))
Harald Welte6332a8a2010-06-15 16:45:51 +0200259 bitvec_set_bit_pos(ma, bitnum, 1);
Harald Weltebf6624d2010-06-15 08:52:12 +0200260 else
Harald Welte6332a8a2010-06-15 16:45:51 +0200261 bitvec_set_bit_pos(ma, bitnum, 0);
laforge1dcdc952010-06-20 15:56:50 +0200262 n_chan++;
Harald Weltebf6624d2010-06-15 08:52:12 +0200263 }
264
265 /* ARFCN 0 is special: It is coded last in the bitmask */
266 if (bitvec_get_bit_pos(cell_chan, 0)) {
Harald Welte6332a8a2010-06-15 16:45:51 +0200267 n_chan++;
268 /* set the corresponding bit in the MA */
269 bitnum = (ts->hopping.ma_len * 8) - 1 - n_chan;
Harald Weltebf6624d2010-06-15 08:52:12 +0200270 if (bitvec_get_bit_pos(ts_arfcn, 0))
Harald Welte6332a8a2010-06-15 16:45:51 +0200271 bitvec_set_bit_pos(ma, bitnum, 1);
Harald Weltebf6624d2010-06-15 08:52:12 +0200272 else
Harald Welte6332a8a2010-06-15 16:45:51 +0200273 bitvec_set_bit_pos(ma, bitnum, 0);
Harald Weltebf6624d2010-06-15 08:52:12 +0200274 }
275
276 return 0;
277}
278
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200279static void bootstrap_rsl(struct gsm_bts_trx *trx)
280{
Harald Weltebf6624d2010-06-15 08:52:12 +0200281 unsigned int i;
282
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100283 LOGP(DRSL, LOGL_NOTICE, "bootstrapping RSL for BTS/TRX (%u/%u) "
Harald Welte7bb55f32015-11-20 10:43:31 +0100284 "on ARFCN %u using MCC=%u MNC=%u LAC=%u CID=%u BSIC=%u\n",
Harald Welte424873e2009-12-24 13:39:34 +0100285 trx->bts->nr, trx->nr, trx->arfcn, bsc_gsmnet->country_code,
286 bsc_gsmnet->network_code, trx->bts->location_area_code,
Harald Welte7bb55f32015-11-20 10:43:31 +0100287 trx->bts->cell_identity, trx->bts->bsic);
Dieter Spaar49c843e2011-07-28 00:01:50 +0200288
289 if (trx->bts->type == GSM_BTS_TYPE_NOKIA_SITE) {
290 rsl_nokia_si_begin(trx);
291 }
292
Holger Hans Peter Freytherd0284c82014-11-21 11:18:45 +0100293 gsm_bts_trx_set_system_infos(trx);
Harald Weltebf6624d2010-06-15 08:52:12 +0200294
Dieter Spaar49c843e2011-07-28 00:01:50 +0200295 if (trx->bts->type == GSM_BTS_TYPE_NOKIA_SITE) {
296 /* channel unspecific, power reduction in 2 dB steps */
297 rsl_bs_power_control(trx, 0xFF, trx->max_power_red / 2);
298 rsl_nokia_si_end(trx);
299 }
300
Harald Weltebf6624d2010-06-15 08:52:12 +0200301 for (i = 0; i < ARRAY_SIZE(trx->ts); i++)
302 generate_ma_for_ts(&trx->ts[i]);
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200303}
304
Harald Welte4c826f72011-01-14 15:55:42 +0100305/* Callback function to be called every time we receive a signal from INPUT */
306static int inp_sig_cb(unsigned int subsys, unsigned int signal,
307 void *handler_data, void *signal_data)
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200308{
Harald Welte4c826f72011-01-14 15:55:42 +0100309 struct input_signal_data *isd = signal_data;
310 struct gsm_bts_trx *trx = isd->trx;
Holger Hans Peter Freyther5a8b2b42010-04-15 11:21:02 +0200311 int ts_no, lchan_no;
312
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +0200313 if (subsys != SS_L_INPUT)
Harald Welte4c826f72011-01-14 15:55:42 +0100314 return -EINVAL;
Holger Hans Peter Freyther5a8b2b42010-04-15 11:21:02 +0200315
Harald Welte4c826f72011-01-14 15:55:42 +0100316 switch (signal) {
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +0200317 case S_L_INP_TEI_UP:
Dieter Spaar49c843e2011-07-28 00:01:50 +0200318 if (isd->link_type == E1INP_SIGN_OML) {
319 /* TODO: this is required for the Nokia BTS, hopping is configured
320 during OML, other MA is not set. */
321 struct gsm_bts_trx *cur_trx;
322 /* was static in system_information.c */
323 extern int generate_cell_chan_list(uint8_t *chan_list, struct gsm_bts *bts);
324 uint8_t ca[20];
325 /* has to be called before generate_ma_for_ts to
326 set bts->si_common.cell_alloc */
327 generate_cell_chan_list(ca, trx->bts);
328
329 llist_for_each_entry(cur_trx, &trx->bts->trx_list, list) {
330 int i;
331
Andreas Eversberg37c3a612013-10-11 13:32:30 +0200332 for (i = 0; i < ARRAY_SIZE(cur_trx->ts); i++) {
Dieter Spaar49c843e2011-07-28 00:01:50 +0200333 generate_ma_for_ts(&cur_trx->ts[i]);
Andreas Eversberg37c3a612013-10-11 13:32:30 +0200334 cur_trx->ts[i].flags |= TS_F_PDCH_MODE;
335 }
Dieter Spaar49c843e2011-07-28 00:01:50 +0200336 }
337 }
Harald Welte4c826f72011-01-14 15:55:42 +0100338 if (isd->link_type == E1INP_SIGN_RSL)
339 bootstrap_rsl(trx);
340 break;
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +0200341 case S_L_INP_TEI_DN:
342 LOGP(DLMI, LOGL_ERROR, "Lost some E1 TEI link: %d %p\n", isd->link_type, trx);
Harald Welte4c826f72011-01-14 15:55:42 +0100343
344 if (isd->link_type == E1INP_SIGN_OML)
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200345 osmo_counter_inc(trx->bts->network->stats.bts.oml_fail);
Harald Welte4c826f72011-01-14 15:55:42 +0100346 else if (isd->link_type == E1INP_SIGN_RSL)
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200347 osmo_counter_inc(trx->bts->network->stats.bts.rsl_fail);
Holger Hans Peter Freyther3e09b3f2010-04-12 10:45:52 +0200348
Holger Hans Peter Freyther5a8b2b42010-04-15 11:21:02 +0200349 /*
350 * free all allocated channels. change the nm_state so the
351 * trx and trx_ts becomes unusable and chan_alloc.c can not
352 * allocate from it.
353 */
354 for (ts_no = 0; ts_no < ARRAY_SIZE(trx->ts); ++ts_no) {
355 struct gsm_bts_trx_ts *ts = &trx->ts[ts_no];
356
357 for (lchan_no = 0; lchan_no < ARRAY_SIZE(ts->lchan); ++lchan_no) {
Holger Hans Peter Freytherade87dd2010-08-25 12:29:24 +0800358 if (ts->lchan[lchan_no].state != LCHAN_S_NONE)
Holger Hans Peter Freyther5a8b2b42010-04-15 11:21:02 +0200359 lchan_free(&ts->lchan[lchan_no]);
360 lchan_reset(&ts->lchan[lchan_no]);
361 }
Holger Hans Peter Freyther5a8b2b42010-04-15 11:21:02 +0200362 }
363
Harald Welte452119d2011-06-29 16:49:03 +0200364 gsm_bts_mo_reset(trx->bts);
Holger Hans Peter Freyther2a6bffe2010-11-15 20:50:42 +0100365
366 abis_nm_clear_queue(trx->bts);
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200367 break;
368 default:
369 break;
370 }
Harald Welte4c826f72011-01-14 15:55:42 +0100371
372 return 0;
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200373}
374
375static int bootstrap_bts(struct gsm_bts *bts)
376{
Sylvain Munaut63e014e2010-04-28 21:31:29 +0200377 int i, n;
378
Max0ae71532016-04-08 11:52:34 +0200379 if (!bts->model)
380 return -EFAULT;
381
Pablo Neira Ayuso5bd96a72011-05-14 11:32:48 +0200382 if (bts->model->start && !bts->model->started) {
383 int ret = bts->model->start(bts->network);
384 if (ret < 0)
385 return ret;
386
387 bts->model->started = true;
388 }
389
Harald Welteeee698b2010-05-26 17:14:42 +0200390 /* FIXME: What about secondary TRX of a BTS? What about a BTS that has TRX
391 * in different bands? Why is 'band' a parameter of the BTS and not of the TRX? */
Mike Haben66e0ba02009-10-02 12:19:34 +0100392 switch (bts->band) {
393 case GSM_BAND_1800:
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200394 if (bts->c0->arfcn < 512 || bts->c0->arfcn > 885) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100395 LOGP(DNM, LOGL_ERROR, "GSM1800 channel must be between 512-885.\n");
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200396 return -EINVAL;
397 }
398 break;
Mike Haben66e0ba02009-10-02 12:19:34 +0100399 case GSM_BAND_1900:
400 if (bts->c0->arfcn < 512 || bts->c0->arfcn > 810) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100401 LOGP(DNM, LOGL_ERROR, "GSM1900 channel must be between 512-810.\n");
Mike Haben66e0ba02009-10-02 12:19:34 +0100402 return -EINVAL;
403 }
404 break;
405 case GSM_BAND_900:
Holger Hans Peter Freyther1abf6532015-06-20 18:45:35 +0200406 if ((bts->c0->arfcn > 124 && bts->c0->arfcn < 955) ||
Holger Hans Peter Freyther2ba2b332010-02-09 15:44:14 +0100407 bts->c0->arfcn > 1023) {
Michael McTernan85b67312015-06-19 20:51:43 +0200408 LOGP(DNM, LOGL_ERROR, "GSM900 channel must be between 0-124, 955-1023.\n");
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200409 return -EINVAL;
410 }
411 break;
Harald Welteeee698b2010-05-26 17:14:42 +0200412 case GSM_BAND_850:
413 if (bts->c0->arfcn < 128 || bts->c0->arfcn > 251) {
414 LOGP(DNM, LOGL_ERROR, "GSM850 channel must be between 128-251.\n");
415 return -EINVAL;
416 }
417 break;
Mike Haben66e0ba02009-10-02 12:19:34 +0100418 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100419 LOGP(DNM, LOGL_ERROR, "Unsupported frequency band.\n");
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200420 return -EINVAL;
421 }
422
Harald Welte395cb6b2009-12-12 13:44:19 +0100423 if (bts->network->auth_policy == GSM_AUTH_POLICY_ACCEPT_ALL &&
Harald Welte8c973ba2009-12-21 23:08:18 +0100424 !bts->si_common.rach_control.cell_bar)
Harald Welte36ab5452009-12-21 23:36:45 +0100425 LOGP(DNM, LOGL_ERROR, "\nWARNING: You are running an 'accept-all' "
Harald Welte395cb6b2009-12-12 13:44:19 +0100426 "network on a BTS that is not barred. This "
427 "configuration is likely to interfere with production "
428 "GSM networks and should only be used in a RF "
429 "shielded environment such as a faraday cage!\n\n");
430
Andreas Eversberg14e12f22012-10-13 07:27:47 +0200431 /* Control Channel Description is set from vty/config */
Harald Welte961ac532011-01-13 23:20:45 +0100432
Holger Hans Peter Freyther8c994342010-04-30 13:32:05 +0800433 /* T3212 is set from vty/config */
Sylvain Munaut63e014e2010-04-28 21:31:29 +0200434
Holger Hans Peter Freyther8c994342010-04-30 13:32:05 +0800435 /* Set ccch config by looking at ts config */
Sylvain Munaut63e014e2010-04-28 21:31:29 +0200436 for (n=0, i=0; i<8; i++)
437 n += bts->c0->ts[i].pchan == GSM_PCHAN_CCCH ? 1 : 0;
438
439 switch (n) {
440 case 0:
441 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_C;
Andreas Eversberg14e12f22012-10-13 07:27:47 +0200442 /* Limit reserved block to 2 on combined channel */
443 if (bts->si_common.chan_desc.bs_ag_blks_res > 2)
444 bts->si_common.chan_desc.bs_ag_blks_res = 2;
Sylvain Munaut63e014e2010-04-28 21:31:29 +0200445 break;
446 case 1:
447 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_NC;
448 break;
449 case 2:
450 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_2_NC;
451 break;
452 case 3:
453 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_3_NC;
454 break;
455 case 4:
456 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_4_NC;
457 break;
458 default:
459 LOGP(DNM, LOGL_ERROR, "Unsupported CCCH timeslot configuration\n");
460 return -EINVAL;
461 }
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200462
Harald Weltea54a2bb2009-12-01 18:04:30 +0530463 bts->si_common.cell_options.pwrc = 0; /* PWRC not set */
464
Harald Weltea54a2bb2009-12-01 18:04:30 +0530465 bts->si_common.cell_sel_par.acs = 0;
Harald Weltea54a2bb2009-12-01 18:04:30 +0530466
467 bts->si_common.ncc_permitted = 0xff;
468
Holger Hans Peter Freyther29cd72a2011-07-18 11:26:07 +0200469 /* Initialize the BTS state */
470 gsm_bts_mo_reset(bts);
471
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200472 return 0;
473}
474
Harald Welte5c05ed32010-12-23 01:07:46 +0100475int bsc_bootstrap_network(int (*mncc_recv)(struct gsm_network *, struct msgb *),
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200476 const char *config_file)
477{
Harald Welte40152872010-05-16 20:52:23 +0200478 struct telnet_connection dummy_conn;
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200479 struct gsm_bts *bts;
480 int rc;
481
482 /* initialize our data structures */
483 bsc_gsmnet = gsm_network_init(1, 1, mncc_recv);
484 if (!bsc_gsmnet)
485 return -ENOMEM;
486
487 bsc_gsmnet->name_long = talloc_strdup(bsc_gsmnet, "OpenBSC");
488 bsc_gsmnet->name_short = talloc_strdup(bsc_gsmnet, "OpenBSC");
489
Harald Welte40152872010-05-16 20:52:23 +0200490 /* our vty command code expects vty->priv to point to a telnet_connection */
491 dummy_conn.priv = bsc_gsmnet;
492 rc = vty_read_config_file(config_file, &dummy_conn);
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200493 if (rc < 0) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100494 LOGP(DNM, LOGL_FATAL, "Failed to parse the config file: '%s'\n", config_file);
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200495 return rc;
496 }
497
Neels Hofmeyr737252c2016-02-23 14:09:38 +0100498 /* start telnet after reading config for vty_get_bind_addr() */
499 LOGP(DNM, LOGL_NOTICE, "VTY at %s %d\n",
500 vty_get_bind_addr(), OSMO_VTY_PORT_NITB_BSC);
501 rc = telnet_init_dynif(tall_bsc_ctx, bsc_gsmnet, vty_get_bind_addr(),
502 OSMO_VTY_PORT_NITB_BSC);
Harald Welte40152872010-05-16 20:52:23 +0200503 if (rc < 0)
504 return rc;
505
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200506 osmo_signal_register_handler(SS_NM, nm_sig_cb, NULL);
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +0200507 osmo_signal_register_handler(SS_L_INPUT, inp_sig_cb, NULL);
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200508
509 llist_for_each_entry(bts, &bsc_gsmnet->bts_list, list) {
Harald Welte08011e22011-03-04 13:41:31 +0100510 rc = bootstrap_bts(bts);
Pablo Neira Ayuso4726e7e2011-05-14 11:32:46 +0200511 if (rc < 0) {
512 LOGP(DNM, LOGL_FATAL, "Error bootstrapping BTS\n");
513 return rc;
514 }
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +0200515 rc = e1_reconfig_bts(bts);
Harald Weltea8497822011-02-05 15:41:24 +0100516 if (rc < 0) {
Pablo Neira Ayuso4726e7e2011-05-14 11:32:46 +0200517 LOGP(DNM, LOGL_FATAL, "Error enabling E1 input driver\n");
518 return rc;
Harald Weltea8497822011-02-05 15:41:24 +0100519 }
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200520 }
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200521 return 0;
522}