blob: 64dcd157b133980c4c8d654eabaf143634fcaefb [file] [log] [blame]
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +02001/* A hackish minimal BSC (+MSC +HLR) implementation */
2
Harald Weltedcccb182010-05-16 20:52:23 +02003/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther8b457fb2009-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 Welte9af6ddf2011-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 Freyther8b457fb2009-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 Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020016 *
Harald Welte9af6ddf2011-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 Freyther8b457fb2009-08-17 06:55:10 +020019 *
20 */
21
22#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010023#include <osmocom/gsm/gsm_utils.h>
Holger Hans Peter Freyther8b457fb2009-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 Welte4b037e42010-05-19 19:45:32 +020029#include <osmocom/vty/telnet_interface.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020030#include <osmocom/vty/ports.h>
Harald Weltea43f7892009-12-01 18:04:30 +053031#include <openbsc/system_information.h>
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020032#include <openbsc/paging.h>
33#include <openbsc/signal.h>
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +020034#include <openbsc/chan_alloc.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010035#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther39e6ba02010-11-15 20:43:02 +010036#include <openbsc/ipaccess.h>
Alexander Huemere1d2e1c2011-09-06 00:09:50 +020037#include <osmocom/gsm/sysinfo.h>
38#include <openbsc/e1_config.h>
Neels Hofmeyr2d521a02016-05-14 00:57:04 +020039#include <openbsc/common_bsc.h>
Philipp Maierb4999b62016-10-26 15:19:41 +020040#include <openbsc/pcu_if.h>
Harald Welte7b423ed2016-06-19 18:06:02 +020041#include <openbsc/osmo_msc.h>
Philipp Maierb4999b62016-10-26 15:19:41 +020042#include <limits.h>
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020043
44/* global pointer to the gsm network data structure */
45extern struct gsm_network *bsc_gsmnet;
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020046
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020047/* Callback function for NACK on the OML NM */
Holger Hans Peter Freyther6d2b66e2010-07-14 02:08:35 +080048static int oml_msg_nack(struct nm_nack_signal_data *nack)
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020049{
Max88d4fc72017-03-16 13:57:47 +010050 if (nack->mt == NM_MT_GET_ATTR_NACK) {
51 LOGP(DNM, LOGL_ERROR, "BTS%u does not support Get Attributes "
52 "OML message.\n", nack->bts->nr);
53 return 0;
Holger Hans Peter Freytherdf82b722011-05-21 00:05:09 +020054 }
55
Max88d4fc72017-03-16 13:57:47 +010056 if (nack->mt == NM_MT_SET_BTS_ATTR_NACK)
57 LOGP(DNM, LOGL_ERROR, "Failed to set BTS attributes. That is fatal. "
58 "Was the bts type and frequency properly specified?\n");
59 else
60 LOGP(DNM, LOGL_ERROR, "Got %s NACK going to drop the OML links.\n",
61 abis_nm_nack_name(nack->mt));
Holger Hans Peter Freytherdf82b722011-05-21 00:05:09 +020062
Holger Hans Peter Freytherde1674a2012-11-11 18:26:23 +010063 if (!nack->bts) {
64 LOGP(DNM, LOGL_ERROR, "Unknown bts. Can not drop it.\n");
Harald Welted9173c32012-10-05 15:48:54 +020065 return 0;
Holger Hans Peter Freytherde1674a2012-11-11 18:26:23 +010066 }
Harald Welted9173c32012-10-05 15:48:54 +020067
Holger Hans Peter Freytherde1674a2012-11-11 18:26:23 +010068 if (is_ipaccess_bts(nack->bts))
69 ipaccess_drop_oml(nack->bts);
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020070
71 return 0;
72}
73
74/* Callback function to be called every time we receive a signal from NM */
75static int nm_sig_cb(unsigned int subsys, unsigned int signal,
76 void *handler_data, void *signal_data)
77{
Holger Hans Peter Freyther6d2b66e2010-07-14 02:08:35 +080078 struct nm_nack_signal_data *nack;
Harald Welted8cfc902009-11-17 06:09:56 +010079
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020080 switch (signal) {
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020081 case S_NM_NACK:
Holger Hans Peter Freyther6d2b66e2010-07-14 02:08:35 +080082 nack = signal_data;
83 return oml_msg_nack(nack);
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020084 default:
85 break;
86 }
87 return 0;
88}
89
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020090int bsc_shutdown_net(struct gsm_network *net)
91{
92 struct gsm_bts *bts;
93
94 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Weltef338a032011-01-14 15:55:42 +010095 LOGP(DNM, LOGL_NOTICE, "shutting down OML for BTS %u\n", bts->nr);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020096 osmo_signal_dispatch(SS_L_GLOBAL, S_GLOBAL_BTS_CLOSE_OM, bts);
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020097 }
98
99 return 0;
100}
101
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100102static int rsl_si(struct gsm_bts_trx *trx, enum osmo_sysinfo_type i, int si_len)
Harald Welte7401ae62010-06-15 16:44:12 +0200103{
104 struct gsm_bts *bts = trx->bts;
Max70fdd242017-06-15 15:10:53 +0200105 int rc, j;
Harald Welte7401ae62010-06-15 16:44:12 +0200106
Harald Welte80cffaf2011-05-24 15:02:20 +0200107 DEBUGP(DRR, "SI%s: %s\n", get_value_string(osmo_sitype_strs, i),
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200108 osmo_hexdump(GSM_BTS_SI(bts, i), GSM_MACBLOCK_LEN));
Harald Welte7401ae62010-06-15 16:44:12 +0200109
110 switch (i) {
111 case SYSINFO_TYPE_5:
112 case SYSINFO_TYPE_5bis:
113 case SYSINFO_TYPE_5ter:
114 case SYSINFO_TYPE_6:
Holger Hans Peter Freytherf0167dd2013-07-03 16:06:20 +0200115 rc = rsl_sacch_filling(trx, osmo_sitype2rsl(i),
116 GSM_BTS_SI(bts, i), si_len);
Harald Welte7401ae62010-06-15 16:44:12 +0200117 break;
Max70fdd242017-06-15 15:10:53 +0200118 case SYSINFO_TYPE_2quater:
119 for (j = 0; j <= bts->si2q_count; j++)
120 rc = rsl_bcch_info(trx, i, (const uint8_t *)GSM_BTS_SI2Q(bts, j), GSM_MACBLOCK_LEN);
121 break;
Harald Welte7401ae62010-06-15 16:44:12 +0200122 default:
Max8b1a2f82017-06-15 14:59:20 +0200123 rc = rsl_bcch_info(trx, i, GSM_BTS_SI(bts, i), si_len);
Harald Welte7401ae62010-06-15 16:44:12 +0200124 break;
125 }
126
127 return rc;
128}
129
Alexander Chemerisc36a13b2015-05-30 14:40:54 -0400130/* set all system information types for a TRX */
Holger Hans Peter Freythera49b2c02014-11-21 11:18:45 +0100131int gsm_bts_trx_set_system_infos(struct gsm_bts_trx *trx)
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200132{
Harald Weltea43f7892009-12-01 18:04:30 +0530133 int i, rc;
Harald Welte73d4fce2009-12-21 23:12:19 +0100134 struct gsm_bts *bts = trx->bts;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100135 uint8_t gen_si[_MAX_SYSINFO_TYPE], n_si = 0, n;
136 int si_len[_MAX_SYSINFO_TYPE];
Harald Welte73d4fce2009-12-21 23:12:19 +0100137
138 bts->si_common.cell_sel_par.ms_txpwr_max_ccch =
139 ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
140 bts->si_common.cell_sel_par.neci = bts->network->neci;
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200141
Max8b1a2f82017-06-15 14:59:20 +0200142 /* Zero, forget the state of the SIs */
143 bts->si_valid = 0;
144
Harald Welte9fbff4a2010-07-30 11:50:09 +0200145 /* First, we determine which of the SI messages we actually need */
146
Harald Welte7401ae62010-06-15 16:44:12 +0200147 if (trx == bts->c0) {
Harald Welte9fbff4a2010-07-30 11:50:09 +0200148 /* 1...4 are always present on a C0 TRX */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100149 gen_si[n_si++] = SYSINFO_TYPE_1;
150 gen_si[n_si++] = SYSINFO_TYPE_2;
151 gen_si[n_si++] = SYSINFO_TYPE_2bis;
152 gen_si[n_si++] = SYSINFO_TYPE_2ter;
Maxf3f35052016-04-15 16:04:44 +0200153 gen_si[n_si++] = SYSINFO_TYPE_2quater;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100154 gen_si[n_si++] = SYSINFO_TYPE_3;
155 gen_si[n_si++] = SYSINFO_TYPE_4;
Harald Welte9fbff4a2010-07-30 11:50:09 +0200156
157 /* 13 is always present on a C0 TRX of a GPRS BTS */
158 if (bts->gprs.mode != BTS_GPRS_NONE)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100159 gen_si[n_si++] = SYSINFO_TYPE_13;
Harald Welte6a22c012009-12-21 17:02:32 +0100160 }
161
Harald Welte9fbff4a2010-07-30 11:50:09 +0200162 /* 5 and 6 are always present on every TRX */
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100163 gen_si[n_si++] = SYSINFO_TYPE_5;
164 gen_si[n_si++] = SYSINFO_TYPE_5bis;
165 gen_si[n_si++] = SYSINFO_TYPE_5ter;
166 gen_si[n_si++] = SYSINFO_TYPE_6;
Harald Weltea43f7892009-12-01 18:04:30 +0530167
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100168 /* Second, we generate the selected SI via RSL */
169
170 for (n = 0; n < n_si; n++) {
171 i = gen_si[n];
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100172 /* Only generate SI if this SI is not in "static" (user-defined) mode */
173 if (!(bts->si_mode_static & (1 << i))) {
Andreas Eversbergf2621e52013-01-08 19:33:42 +0100174 /* Set SI as being valid. gsm_generate_si() might unset
175 * it, if SI is not required. */
176 bts->si_valid |= (1 << i);
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100177 rc = gsm_generate_si(bts, i);
178 if (rc < 0)
179 goto err_out;
180 si_len[i] = rc;
181 } else {
182 if (i == SYSINFO_TYPE_5 || i == SYSINFO_TYPE_5bis
183 || i == SYSINFO_TYPE_5ter)
184 si_len[i] = 18;
185 else if (i == SYSINFO_TYPE_6)
186 si_len[i] = 11;
187 else
188 si_len[i] = 23;
189 }
190 }
191
192 /* Third, we send the selected SI via RSL */
193
Andreas Eversbergf2621e52013-01-08 19:33:42 +0100194 for (n = 0; n < n_si; n++) {
195 i = gen_si[n];
Max9b97b002017-06-02 10:58:26 +0200196 if (!GSM_BTS_HAS_SI(bts, i))
Harald Welte9fbff4a2010-07-30 11:50:09 +0200197 continue;
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100198 rc = rsl_si(trx, i, si_len[i]);
Harald Welte9fbff4a2010-07-30 11:50:09 +0200199 if (rc < 0)
Andreas Eversbergc50543b2012-02-29 09:04:07 +0100200 return rc;
Harald Welte9fbff4a2010-07-30 11:50:09 +0200201 }
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200202
Philipp Maierb4999b62016-10-26 15:19:41 +0200203 /* Make sure the PCU is aware (in case anything GPRS related has
204 * changed in SI */
205 pcu_info_update(bts);
206
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200207 return 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530208err_out:
Max34be86b2016-12-16 18:45:51 +0100209 LOGP(DRR, LOGL_ERROR, "Cannot generate SI%s for BTS %u: error <%s>, "
Max59a1bf32016-04-15 16:04:46 +0200210 "most likely a problem with neighbor cell list generation\n",
211 get_value_string(osmo_sitype_strs, i), bts->nr, strerror(-rc));
Harald Weltea43f7892009-12-01 18:04:30 +0530212 return rc;
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200213}
214
Alexander Chemerisc36a13b2015-05-30 14:40:54 -0400215/* set all system information types for a BTS */
216int gsm_bts_set_system_infos(struct gsm_bts *bts)
217{
218 struct gsm_bts_trx *trx;
219
220 /* Generate a new ID */
221 bts->bcch_change_mark += 1;
222 bts->bcch_change_mark %= 0x7;
223
224 llist_for_each_entry(trx, &bts->trx_list, list) {
225 int rc;
226
227 rc = gsm_bts_trx_set_system_infos(trx);
228 if (rc != 0)
229 return rc;
230 }
231
232 return 0;
233}
234
Harald Weltefd370892010-06-15 08:52:12 +0200235/* Produce a MA as specified in 10.5.2.21 */
236static int generate_ma_for_ts(struct gsm_bts_trx_ts *ts)
237{
238 /* we have three bitvecs: the per-timeslot ARFCNs, the cell chan ARFCNs
239 * and the MA */
240 struct bitvec *cell_chan = &ts->trx->bts->si_common.cell_alloc;
241 struct bitvec *ts_arfcn = &ts->hopping.arfcns;
242 struct bitvec *ma = &ts->hopping.ma;
Harald Welte29aca172010-06-15 16:45:51 +0200243 unsigned int num_cell_arfcns, bitnum, n_chan;
Harald Weltefd370892010-06-15 08:52:12 +0200244 int i;
245
246 /* re-set the MA to all-zero */
247 ma->cur_bit = 0;
Harald Welte29aca172010-06-15 16:45:51 +0200248 ts->hopping.ma_len = 0;
Harald Weltefd370892010-06-15 08:52:12 +0200249 memset(ma->data, 0, ma->data_len);
250
251 if (!ts->hopping.enabled)
252 return 0;
253
Harald Welte29aca172010-06-15 16:45:51 +0200254 /* count the number of ARFCNs in the cell channel allocation */
255 num_cell_arfcns = 0;
Harald Welte1fe73a12012-01-29 13:24:12 +0100256 for (i = 0; i < 1024; i++) {
Harald Welte29aca172010-06-15 16:45:51 +0200257 if (bitvec_get_bit_pos(cell_chan, i))
258 num_cell_arfcns++;
259 }
260
261 /* pad it to octet-aligned number of bits */
262 ts->hopping.ma_len = num_cell_arfcns / 8;
263 if (num_cell_arfcns % 8)
264 ts->hopping.ma_len++;
265
266 n_chan = 0;
Harald Welte1fe73a12012-01-29 13:24:12 +0100267 for (i = 0; i < 1024; i++) {
Harald Weltefd370892010-06-15 08:52:12 +0200268 if (!bitvec_get_bit_pos(cell_chan, i))
269 continue;
Harald Welte29aca172010-06-15 16:45:51 +0200270 /* set the corresponding bit in the MA */
271 bitnum = (ts->hopping.ma_len * 8) - 1 - n_chan;
Harald Weltefd370892010-06-15 08:52:12 +0200272 if (bitvec_get_bit_pos(ts_arfcn, i))
Harald Welte29aca172010-06-15 16:45:51 +0200273 bitvec_set_bit_pos(ma, bitnum, 1);
Harald Weltefd370892010-06-15 08:52:12 +0200274 else
Harald Welte29aca172010-06-15 16:45:51 +0200275 bitvec_set_bit_pos(ma, bitnum, 0);
laforgeabeb9512010-06-20 15:56:50 +0200276 n_chan++;
Harald Weltefd370892010-06-15 08:52:12 +0200277 }
278
279 /* ARFCN 0 is special: It is coded last in the bitmask */
280 if (bitvec_get_bit_pos(cell_chan, 0)) {
Harald Welte29aca172010-06-15 16:45:51 +0200281 n_chan++;
282 /* set the corresponding bit in the MA */
283 bitnum = (ts->hopping.ma_len * 8) - 1 - n_chan;
Harald Weltefd370892010-06-15 08:52:12 +0200284 if (bitvec_get_bit_pos(ts_arfcn, 0))
Harald Welte29aca172010-06-15 16:45:51 +0200285 bitvec_set_bit_pos(ma, bitnum, 1);
Harald Weltefd370892010-06-15 08:52:12 +0200286 else
Harald Welte29aca172010-06-15 16:45:51 +0200287 bitvec_set_bit_pos(ma, bitnum, 0);
Harald Weltefd370892010-06-15 08:52:12 +0200288 }
289
290 return 0;
291}
292
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200293static void bootstrap_rsl(struct gsm_bts_trx *trx)
294{
Harald Weltefd370892010-06-15 08:52:12 +0200295 unsigned int i;
296
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100297 LOGP(DRSL, LOGL_NOTICE, "bootstrapping RSL for BTS/TRX (%u/%u) "
Harald Weltea2bbc5e2015-11-20 10:43:31 +0100298 "on ARFCN %u using MCC=%u MNC=%u LAC=%u CID=%u BSIC=%u\n",
Harald Welte19a3f0b2009-12-24 13:39:34 +0100299 trx->bts->nr, trx->nr, trx->arfcn, bsc_gsmnet->country_code,
300 bsc_gsmnet->network_code, trx->bts->location_area_code,
Harald Weltea2bbc5e2015-11-20 10:43:31 +0100301 trx->bts->cell_identity, trx->bts->bsic);
Dieter Spaar16646022011-07-28 00:01:50 +0200302
303 if (trx->bts->type == GSM_BTS_TYPE_NOKIA_SITE) {
304 rsl_nokia_si_begin(trx);
305 }
306
Holger Hans Peter Freythera49b2c02014-11-21 11:18:45 +0100307 gsm_bts_trx_set_system_infos(trx);
Harald Weltefd370892010-06-15 08:52:12 +0200308
Dieter Spaar16646022011-07-28 00:01:50 +0200309 if (trx->bts->type == GSM_BTS_TYPE_NOKIA_SITE) {
310 /* channel unspecific, power reduction in 2 dB steps */
311 rsl_bs_power_control(trx, 0xFF, trx->max_power_red / 2);
312 rsl_nokia_si_end(trx);
313 }
314
Harald Weltefd370892010-06-15 08:52:12 +0200315 for (i = 0; i < ARRAY_SIZE(trx->ts); i++)
316 generate_ma_for_ts(&trx->ts[i]);
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200317}
318
Harald Weltef338a032011-01-14 15:55:42 +0100319/* Callback function to be called every time we receive a signal from INPUT */
320static int inp_sig_cb(unsigned int subsys, unsigned int signal,
321 void *handler_data, void *signal_data)
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200322{
Harald Weltef338a032011-01-14 15:55:42 +0100323 struct input_signal_data *isd = signal_data;
324 struct gsm_bts_trx *trx = isd->trx;
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +0200325 int ts_no, lchan_no;
Max33e13572017-05-29 11:48:29 +0200326 /* N. B: we rely on attribute order when parsing response in abis_nm_rx_get_attr_resp() */
327 const uint8_t bts_attr[] = { NM_ATT_MANUF_ID, NM_ATT_SW_CONFIG, };
328 const uint8_t trx_attr[] = { NM_ATT_MANUF_STATE, NM_ATT_SW_CONFIG, };
Maxdefb6c92017-05-15 10:29:54 +0200329
330 /* we should not request more attributes than we're ready to handle */
Max33e13572017-05-29 11:48:29 +0200331 OSMO_ASSERT(sizeof(bts_attr) < MAX_BTS_ATTR);
332 OSMO_ASSERT(sizeof(trx_attr) < MAX_BTS_ATTR);
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +0200333
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200334 if (subsys != SS_L_INPUT)
Harald Weltef338a032011-01-14 15:55:42 +0100335 return -EINVAL;
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +0200336
Harald Welte5f72f6f2017-05-25 19:16:40 +0200337 LOGP(DLMI, LOGL_DEBUG, "%s(): Input signal '%s' received\n", __func__,
338 get_value_string(e1inp_signal_names, signal));
Harald Weltef338a032011-01-14 15:55:42 +0100339 switch (signal) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200340 case S_L_INP_TEI_UP:
Dieter Spaar16646022011-07-28 00:01:50 +0200341 if (isd->link_type == E1INP_SIGN_OML) {
342 /* TODO: this is required for the Nokia BTS, hopping is configured
343 during OML, other MA is not set. */
344 struct gsm_bts_trx *cur_trx;
345 /* was static in system_information.c */
346 extern int generate_cell_chan_list(uint8_t *chan_list, struct gsm_bts *bts);
347 uint8_t ca[20];
348 /* has to be called before generate_ma_for_ts to
349 set bts->si_common.cell_alloc */
350 generate_cell_chan_list(ca, trx->bts);
351
Max33e13572017-05-29 11:48:29 +0200352 /* Request generic BTS-level attributes */
353 abis_nm_get_attr(trx->bts, NM_OC_BTS, trx->bts->nr, trx->nr, 0xFF, bts_attr, sizeof(bts_attr));
354
Dieter Spaar16646022011-07-28 00:01:50 +0200355 llist_for_each_entry(cur_trx, &trx->bts->trx_list, list) {
356 int i;
Max33e13572017-05-29 11:48:29 +0200357 /* Request TRX-level attributes */
358 abis_nm_get_attr(cur_trx->bts, NM_OC_BASEB_TRANSC, cur_trx->bts->nr, cur_trx->nr, 0xFF,
359 trx_attr, sizeof(trx_attr));
Neels Hofmeyrb8afb5f2016-06-02 01:18:02 +0200360 for (i = 0; i < ARRAY_SIZE(cur_trx->ts); i++)
Dieter Spaar16646022011-07-28 00:01:50 +0200361 generate_ma_for_ts(&cur_trx->ts[i]);
362 }
363 }
Harald Weltef338a032011-01-14 15:55:42 +0100364 if (isd->link_type == E1INP_SIGN_RSL)
365 bootstrap_rsl(trx);
366 break;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200367 case S_L_INP_TEI_DN:
368 LOGP(DLMI, LOGL_ERROR, "Lost some E1 TEI link: %d %p\n", isd->link_type, trx);
Harald Weltef338a032011-01-14 15:55:42 +0100369
370 if (isd->link_type == E1INP_SIGN_OML)
Alexander Couzensb847a212016-08-02 11:34:11 +0200371 rate_ctr_inc(&trx->bts->network->bsc_ctrs->ctr[BSC_CTR_BTS_OML_FAIL]);
Harald Weltef338a032011-01-14 15:55:42 +0100372 else if (isd->link_type == E1INP_SIGN_RSL)
Alexander Couzensb847a212016-08-02 11:34:11 +0200373 rate_ctr_inc(&trx->bts->network->bsc_ctrs->ctr[BSC_CTR_BTS_RSL_FAIL]);
Holger Hans Peter Freytherbb110f92010-04-12 10:45:52 +0200374
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +0200375 /*
376 * free all allocated channels. change the nm_state so the
377 * trx and trx_ts becomes unusable and chan_alloc.c can not
378 * allocate from it.
379 */
380 for (ts_no = 0; ts_no < ARRAY_SIZE(trx->ts); ++ts_no) {
381 struct gsm_bts_trx_ts *ts = &trx->ts[ts_no];
382
383 for (lchan_no = 0; lchan_no < ARRAY_SIZE(ts->lchan); ++lchan_no) {
Holger Hans Peter Freyther4dc025b2010-08-25 12:29:24 +0800384 if (ts->lchan[lchan_no].state != LCHAN_S_NONE)
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +0200385 lchan_free(&ts->lchan[lchan_no]);
386 lchan_reset(&ts->lchan[lchan_no]);
387 }
Holger Hans Peter Freyther135f7972010-04-15 11:21:02 +0200388 }
389
Harald Welteb7849982011-06-29 16:49:03 +0200390 gsm_bts_mo_reset(trx->bts);
Holger Hans Peter Freyther6f615552010-11-15 20:50:42 +0100391
392 abis_nm_clear_queue(trx->bts);
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200393 break;
394 default:
395 break;
396 }
Harald Weltef338a032011-01-14 15:55:42 +0100397
398 return 0;
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200399}
400
401static int bootstrap_bts(struct gsm_bts *bts)
402{
Sylvain Munaut50f81b02010-04-28 21:31:29 +0200403 int i, n;
404
Max687f0482016-04-08 11:52:34 +0200405 if (!bts->model)
406 return -EFAULT;
407
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +0200408 if (bts->model->start && !bts->model->started) {
409 int ret = bts->model->start(bts->network);
410 if (ret < 0)
411 return ret;
412
413 bts->model->started = true;
414 }
415
Harald Welte71b822b2010-05-26 17:14:42 +0200416 /* FIXME: What about secondary TRX of a BTS? What about a BTS that has TRX
417 * in different bands? Why is 'band' a parameter of the BTS and not of the TRX? */
Mike Habene2d82272009-10-02 12:19:34 +0100418 switch (bts->band) {
419 case GSM_BAND_1800:
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200420 if (bts->c0->arfcn < 512 || bts->c0->arfcn > 885) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100421 LOGP(DNM, LOGL_ERROR, "GSM1800 channel must be between 512-885.\n");
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200422 return -EINVAL;
423 }
424 break;
Mike Habene2d82272009-10-02 12:19:34 +0100425 case GSM_BAND_1900:
426 if (bts->c0->arfcn < 512 || bts->c0->arfcn > 810) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100427 LOGP(DNM, LOGL_ERROR, "GSM1900 channel must be between 512-810.\n");
Mike Habene2d82272009-10-02 12:19:34 +0100428 return -EINVAL;
429 }
430 break;
431 case GSM_BAND_900:
Holger Hans Peter Freyther57ee7802015-06-20 18:45:35 +0200432 if ((bts->c0->arfcn > 124 && bts->c0->arfcn < 955) ||
Holger Hans Peter Freytherfb0b6fc2010-02-09 15:44:14 +0100433 bts->c0->arfcn > 1023) {
Michael McTernanc58a1e52015-06-19 20:51:43 +0200434 LOGP(DNM, LOGL_ERROR, "GSM900 channel must be between 0-124, 955-1023.\n");
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200435 return -EINVAL;
436 }
437 break;
Harald Welte71b822b2010-05-26 17:14:42 +0200438 case GSM_BAND_850:
439 if (bts->c0->arfcn < 128 || bts->c0->arfcn > 251) {
440 LOGP(DNM, LOGL_ERROR, "GSM850 channel must be between 128-251.\n");
441 return -EINVAL;
442 }
443 break;
Mike Habene2d82272009-10-02 12:19:34 +0100444 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100445 LOGP(DNM, LOGL_ERROR, "Unsupported frequency band.\n");
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200446 return -EINVAL;
447 }
448
Harald Welted6aa5242009-12-12 13:44:19 +0100449 if (bts->network->auth_policy == GSM_AUTH_POLICY_ACCEPT_ALL &&
Harald Welte71355012009-12-21 23:08:18 +0100450 !bts->si_common.rach_control.cell_bar)
Harald Weltea992a362009-12-21 23:36:45 +0100451 LOGP(DNM, LOGL_ERROR, "\nWARNING: You are running an 'accept-all' "
Harald Welted6aa5242009-12-12 13:44:19 +0100452 "network on a BTS that is not barred. This "
453 "configuration is likely to interfere with production "
454 "GSM networks and should only be used in a RF "
455 "shielded environment such as a faraday cage!\n\n");
456
Andreas Eversberg2ee7ecd2012-10-13 07:27:47 +0200457 /* Control Channel Description is set from vty/config */
Harald Welteaace9fc2011-01-13 23:20:45 +0100458
Holger Hans Peter Freythera5811362010-04-30 13:32:05 +0800459 /* T3212 is set from vty/config */
Sylvain Munaut50f81b02010-04-28 21:31:29 +0200460
Holger Hans Peter Freythera5811362010-04-30 13:32:05 +0800461 /* Set ccch config by looking at ts config */
Sylvain Munaut50f81b02010-04-28 21:31:29 +0200462 for (n=0, i=0; i<8; i++)
463 n += bts->c0->ts[i].pchan == GSM_PCHAN_CCCH ? 1 : 0;
464
Neels Hofmeyr48a0c6a2017-02-22 02:27:59 +0100465 /* Indicate R99 MSC in SI3 */
466 bts->si_common.chan_desc.mscr = 1;
467
Sylvain Munaut50f81b02010-04-28 21:31:29 +0200468 switch (n) {
469 case 0:
470 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_C;
Maxb6591072016-10-12 12:48:50 +0200471 /* Limit reserved block to 2 on combined channel according to
472 3GPP TS 44.018 Table 10.5.2.11.1 */
473 if (bts->si_common.chan_desc.bs_ag_blks_res > 2) {
474 LOGP(DNM, LOGL_NOTICE, "CCCH is combined with SDCCHs, "
475 "reducing BS-AG-BLKS-RES value %d -> 2\n",
476 bts->si_common.chan_desc.bs_ag_blks_res);
Andreas Eversberg2ee7ecd2012-10-13 07:27:47 +0200477 bts->si_common.chan_desc.bs_ag_blks_res = 2;
Maxb6591072016-10-12 12:48:50 +0200478 }
Sylvain Munaut50f81b02010-04-28 21:31:29 +0200479 break;
480 case 1:
481 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_NC;
482 break;
483 case 2:
484 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_2_NC;
485 break;
486 case 3:
487 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_3_NC;
488 break;
489 case 4:
490 bts->si_common.chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_4_NC;
491 break;
492 default:
493 LOGP(DNM, LOGL_ERROR, "Unsupported CCCH timeslot configuration\n");
494 return -EINVAL;
495 }
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200496
Harald Weltea43f7892009-12-01 18:04:30 +0530497 bts->si_common.cell_options.pwrc = 0; /* PWRC not set */
498
Harald Weltea43f7892009-12-01 18:04:30 +0530499 bts->si_common.cell_sel_par.acs = 0;
Harald Weltea43f7892009-12-01 18:04:30 +0530500
501 bts->si_common.ncc_permitted = 0xff;
502
Holger Hans Peter Freytherfe1ca352011-07-18 11:26:07 +0200503 /* Initialize the BTS state */
504 gsm_bts_mo_reset(bts);
505
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200506 return 0;
507}
508
Neels Hofmeyre2354412016-05-11 14:12:00 +0200509int bsc_network_alloc(mncc_recv_cb_t mncc_recv)
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200510{
Philipp Maierb4999b62016-10-26 15:19:41 +0200511 /* initialize our data structures */
Neels Hofmeyr27681a32016-05-14 00:45:26 +0200512 bsc_gsmnet = bsc_network_init(tall_bsc_ctx, 1, 1, mncc_recv);
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200513 if (!bsc_gsmnet)
514 return -ENOMEM;
515
516 bsc_gsmnet->name_long = talloc_strdup(bsc_gsmnet, "OpenBSC");
517 bsc_gsmnet->name_short = talloc_strdup(bsc_gsmnet, "OpenBSC");
518
Neels Hofmeyre2354412016-05-11 14:12:00 +0200519 return 0;
520}
521
522int bsc_network_configure(const char *config_file)
523{
524 struct gsm_bts *bts;
525 int rc;
526
Neels Hofmeyr0ce98c72016-05-11 13:39:33 +0200527 rc = vty_read_config_file(config_file, NULL);
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200528 if (rc < 0) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100529 LOGP(DNM, LOGL_FATAL, "Failed to parse the config file: '%s'\n", config_file);
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200530 return rc;
531 }
532
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100533 /* start telnet after reading config for vty_get_bind_addr() */
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100534 rc = telnet_init_dynif(tall_bsc_ctx, bsc_gsmnet, vty_get_bind_addr(),
535 OSMO_VTY_PORT_NITB_BSC);
Harald Weltedcccb182010-05-16 20:52:23 +0200536 if (rc < 0)
537 return rc;
538
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200539 osmo_signal_register_handler(SS_NM, nm_sig_cb, NULL);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200540 osmo_signal_register_handler(SS_L_INPUT, inp_sig_cb, NULL);
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200541
542 llist_for_each_entry(bts, &bsc_gsmnet->bts_list, list) {
Harald Weltefd355a32011-03-04 13:41:31 +0100543 rc = bootstrap_bts(bts);
Pablo Neira Ayuso6e52b882011-05-14 11:32:46 +0200544 if (rc < 0) {
545 LOGP(DNM, LOGL_FATAL, "Error bootstrapping BTS\n");
546 return rc;
547 }
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200548 rc = e1_reconfig_bts(bts);
Harald Welte889f16e2011-02-05 15:41:24 +0100549 if (rc < 0) {
Pablo Neira Ayuso6e52b882011-05-14 11:32:46 +0200550 LOGP(DNM, LOGL_FATAL, "Error enabling E1 input driver\n");
551 return rc;
Harald Welte889f16e2011-02-05 15:41:24 +0100552 }
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200553 }
Philipp Maierb4999b62016-10-26 15:19:41 +0200554
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200555 return 0;
556}