blob: 58c209f073ab5cc309dfbf733696f845d3d5bc91 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* A hackish minimal BSC (+MSC +HLR) implementation */
2
Harald Welte32201c12009-03-10 12:15:10 +00003/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Freyther219518d2009-01-02 22:04:43 +00004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte52b1f982008-12-23 20:25:15 +00005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Weltef6b7a902008-12-26 00:05:11 +000023#include <unistd.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <stdarg.h>
27#include <time.h>
28#include <string.h>
Harald Weltead384642008-12-26 10:20:07 +000029#include <errno.h>
Harald Welted1252502009-01-01 01:50:32 +000030#include <signal.h>
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000031#include <fcntl.h>
32#include <sys/stat.h>
Harald Welte52b1f982008-12-23 20:25:15 +000033
Holger Freytherb332f612008-12-27 12:46:51 +000034#define _GNU_SOURCE
35#include <getopt.h>
36
Harald Welte255539c2008-12-28 02:26:27 +000037#include <openbsc/db.h>
38#include <openbsc/timer.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/gsm_data.h>
Harald Welte255539c2008-12-28 02:26:27 +000040#include <openbsc/gsm_04_08.h>
Harald Weltead384642008-12-26 10:20:07 +000041#include <openbsc/select.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/abis_rsl.h>
43#include <openbsc/abis_nm.h>
Harald Welte702d8702008-12-26 20:25:35 +000044#include <openbsc/debug.h>
Holger Freyther5677ae32008-12-27 09:41:03 +000045#include <openbsc/misdn.h>
Holger Freyther219518d2009-01-02 22:04:43 +000046#include <openbsc/telnet_interface.h>
Harald Welte38c2f132009-01-06 23:10:57 +000047#include <openbsc/paging.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000048#include <openbsc/e1_input.h>
Harald Welteb4630602009-05-01 15:43:22 +000049#include <openbsc/signal.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020050#include <openbsc/talloc.h>
51
Harald Welte52b1f982008-12-23 20:25:15 +000052/* global pointer to the gsm network data structure */
Harald Welte879c85a2009-05-01 15:00:20 +000053static struct gsm_network *gsmnet;
Harald Welte52b1f982008-12-23 20:25:15 +000054
Holger Freytherefde7fb2008-12-28 14:14:56 +000055/* MCC and MNC for the Location Area Identifier */
56static int MCC = 1;
57static int MNC = 1;
Holger Freyther0a173bf2009-04-22 22:07:07 +000058static int LAC = 1;
Harald Welte98981882009-01-06 18:59:11 +000059static int ARFCN = HARDCODED_ARFCN;
Holger Freytherdda22c12009-04-22 22:07:31 +000060static int cardnr = 0;
Holger Freytherb5c00f52009-04-22 22:08:07 +000061static int release_l2 = 0;
Harald Welte8c1d0e42009-02-15 03:38:12 +000062static enum gsm_bts_type BTS_TYPE = GSM_BTS_TYPE_BS11;
Harald Weltefcd24452009-06-20 18:15:19 +020063static enum gsm_band BAND = GSM_BAND_900;
Holger Freytherbde36102008-12-28 22:51:39 +000064static const char *database_name = "hlr.sqlite3";
Holger Freytherefde7fb2008-12-28 14:14:56 +000065
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +020066struct nano_bts_id {
67 struct llist_head entry;
68 int site_id;
69 int bts_id;
70};
71
72static LLIST_HEAD(nanobts_ids);
73
74
Harald Welte52b1f982008-12-23 20:25:15 +000075/* The following definitions are for OM and NM packets that we cannot yet
76 * generate by code but we just pass on */
77
78// BTS Site Manager, SET ATTRIBUTES
79
80/*
81 Object Class: BTS Site Manager
82 Instance 1: FF
83 Instance 2: FF
84 Instance 3: FF
85SET ATTRIBUTES
86 sAbisExternalTime: 2007/09/08 14:36:11
87 omLAPDRelTimer: 30sec
88 shortLAPDIntTimer: 5sec
89 emergencyTimer1: 10 minutes
90 emergencyTimer2: 0 minutes
91*/
92
93unsigned char msg_1[] =
94{
Harald Weltea865f1b2009-06-20 10:42:17 +020095 NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER, 0xFF, 0xFF, 0xFF,
Harald Welte110c0ab2009-05-23 16:27:05 +000096 NM_ATT_BS11_ABIS_EXT_TIME, 0x07,
97 0xD7, 0x09, 0x08, 0x0E, 0x24, 0x0B, 0xCE,
98 0x02,
99 0x00, 0x1E,
100 NM_ATT_BS11_SH_LAPD_INT_TIMER,
101 0x01, 0x05,
Harald Weltecd993872009-02-15 16:16:28 +0000102 0x42, 0x02, 0x00, 0x0A,
103 0x44, 0x02, 0x00, 0x00
Harald Welte52b1f982008-12-23 20:25:15 +0000104};
105
106// BTS, SET BTS ATTRIBUTES
107
108/*
109 Object Class: BTS
110 BTS relat. Number: 0
111 Instance 2: FF
112 Instance 3: FF
113SET BTS ATTRIBUTES
114 bsIdentityCode / BSIC:
115 PLMN_colour_code: 7h
116 BS_colour_code: 7h
117 BTS Air Timer T3105: 4 ,unit 10 ms
118 btsIsHopping: FALSE
Harald Welte83282292009-02-01 16:22:19 +0000119 periodCCCHLoadIndication: 1sec
Holger Freyther3b910432009-02-11 00:43:48 +0000120 thresholdCCCHLoadIndication: 0%
Harald Welte52b1f982008-12-23 20:25:15 +0000121 cellAllocationNumber: 00h = GSM 900
122 enableInterferenceClass: 00h = Disabled
123 fACCHQual: 6 (FACCH stealing flags minus 1)
124 intaveParameter: 31 SACCH multiframes
125 interferenceLevelBoundaries:
126 Interference Boundary 1: 0Ah
127 Interference Boundary 2: 0Fh
128 Interference Boundary 3: 14h
129 Interference Boundary 4: 19h
130 Interference Boundary 5: 1Eh
131 mSTxPwrMax: 11
132 GSM range: 2=39dBm, 15=13dBm, stepsize 2 dBm
133 DCS1800 range: 0=30dBm, 15=0dBm, stepsize 2 dBm
134 PCS1900 range: 0=30dBm, 15=0dBm, stepsize 2 dBm
135 30=33dBm, 31=32dBm
136 ny1:
137 Maximum number of repetitions for PHYSICAL INFORMATION message (GSM 04.08): 20
138 powerOutputThresholds:
139 Out Power Fault Threshold: -10 dB
140 Red Out Power Threshold: - 6 dB
141 Excessive Out Power Threshold: 5 dB
142 rACHBusyThreshold: -127 dBm
143 rACHLoadAveragingSlots: 250 ,number of RACH burst periods
144 rfResourceIndicationPeriod: 125 SACCH multiframes
145 T200:
146 SDCCH: 044 in 5 ms
147 FACCH/Full rate: 031 in 5 ms
148 FACCH/Half rate: 041 in 5 ms
149 SACCH with TCH SAPI0: 090 in 10 ms
150 SACCH with SDCCH: 090 in 10 ms
151 SDCCH with SAPI3: 090 in 5 ms
152 SACCH with TCH SAPI3: 135 in 10 ms
153 tSync: 9000 units of 10 msec
154 tTrau: 9000 units of 10 msec
155 enableUmLoopTest: 00h = disabled
156 enableExcessiveDistance: 00h = Disabled
157 excessiveDistance: 64km
158 hoppingMode: 00h = baseband hopping
159 cellType: 00h = Standard Cell
160 BCCH ARFCN / bCCHFrequency: 1
161*/
162
Harald Weltea865f1b2009-06-20 10:42:17 +0200163static unsigned char bs11_attr_bts[] =
Harald Welte52b1f982008-12-23 20:25:15 +0000164{
Harald Welte060f6df2009-05-23 17:50:53 +0000165 NM_ATT_BSIC, HARDCODED_BSIC,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000166 NM_ATT_BTS_AIR_TIMER, 0x04,
Harald Weltecd993872009-02-15 16:16:28 +0000167 NM_ATT_BS11_BTSLS_HOPPING, 0x00,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000168 NM_ATT_CCCH_L_I_P, 0x01,
169 NM_ATT_CCCH_L_T, 0x00,
Harald Welte7b26bcb2009-05-28 11:39:21 +0000170 NM_ATT_BS11_CELL_ALLOC_NR, NM_BS11_CANR_GSM,
171 NM_ATT_BS11_ENA_INTERF_CLASS, 0x01,
Harald Weltecd993872009-02-15 16:16:28 +0000172 NM_ATT_BS11_FACCH_QUAL, 0x06,
Harald Weltefe609d82009-05-23 18:14:31 +0000173 /* interference avg. period in numbers of SACCH multifr */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000174 NM_ATT_INTAVE_PARAM, 0x1F,
175 NM_ATT_INTERF_BOUND, 0x0A, 0x0F, 0x14, 0x19, 0x1E, 0x7B,
176 NM_ATT_CCCH_L_T, 0x23,
177 NM_ATT_GSM_TIME, 0x28, 0x00,
178 NM_ATT_ADM_STATE, 0x03,
179 NM_ATT_RACH_B_THRESH, 0x7F,
180 NM_ATT_LDAVG_SLOTS, 0x00, 0xFA,
Harald Weltecd993872009-02-15 16:16:28 +0000181 NM_ATT_BS11_RF_RES_IND_PER, 0x7D,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000182 NM_ATT_T200, 0x2C, 0x1F, 0x29, 0x5A, 0x5A, 0x5A, 0x87,
Harald Weltecd993872009-02-15 16:16:28 +0000183 NM_ATT_BS11_TSYNC, 0x23, 0x28,
184 NM_ATT_BS11_TTRAU, 0x23, 0x28,
185 NM_ATT_TEST_DUR, 0x01, 0x00,
186 NM_ATT_OUTST_ALARM, 0x01, 0x00,
187 NM_ATT_BS11_EXCESSIVE_DISTANCE, 0x01, 0x40,
188 NM_ATT_BS11_HOPPING_MODE, 0x01, 0x00,
189 NM_ATT_BS11_PLL, 0x01, 0x00,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000190 NM_ATT_BCCH_ARFCN, 0x00, HARDCODED_ARFCN/*0x01*/,
Harald Welte52b1f982008-12-23 20:25:15 +0000191};
192
193// Handover Recognition, SET ATTRIBUTES
194
195/*
196Illegal Contents GSM Formatted O&M Msg
197 Object Class: Handover Recognition
198 BTS relat. Number: 0
199 Instance 2: FF
200 Instance 3: FF
201SET ATTRIBUTES
202 enableDelayPowerBudgetHO: 00h = Disabled
203 enableDistanceHO: 00h = Disabled
204 enableInternalInterCellHandover: 00h = Disabled
205 enableInternalIntraCellHandover: 00h = Disabled
206 enablePowerBudgetHO: 00h = Disabled
207 enableRXLEVHO: 00h = Disabled
208 enableRXQUALHO: 00h = Disabled
209 hoAveragingDistance: 8 SACCH multiframes
210 hoAveragingLev:
211 A_LEV_HO: 8 SACCH multiframes
212 W_LEV_HO: 1 SACCH multiframes
213 hoAveragingPowerBudget: 16 SACCH multiframes
214 hoAveragingQual:
215 A_QUAL_HO: 8 SACCH multiframes
216 W_QUAL_HO: 2 SACCH multiframes
217 hoLowerThresholdLevDL: (10 - 110) dBm
218 hoLowerThresholdLevUL: (5 - 110) dBm
219 hoLowerThresholdQualDL: 06h = 6.4% < BER < 12.8%
220 hoLowerThresholdQualUL: 06h = 6.4% < BER < 12.8%
221 hoThresholdLevDLintra : (20 - 110) dBm
222 hoThresholdLevULintra: (20 - 110) dBm
223 hoThresholdMsRangeMax: 20 km
224 nCell: 06h
225 timerHORequest: 3 ,unit 2 SACCH multiframes
226*/
227
228unsigned char msg_3[] =
229{
Harald Weltea865f1b2009-06-20 10:42:17 +0200230 NM_MT_BS11_SET_ATTR, NM_OC_BS11_HANDOVER, 0x00, 0xFF, 0xFF,
Harald Weltecd993872009-02-15 16:16:28 +0000231 0xD0, 0x00,
232 0x64, 0x00,
233 0x67, 0x00,
234 0x68, 0x00,
235 0x6A, 0x00,
236 0x6C, 0x00,
237 0x6D, 0x00,
238 0x6F, 0x08,
239 0x70, 0x08, 0x01,
240 0x71, 0x10, 0x10, 0x10,
241 0x72, 0x08, 0x02,
242 0x73, 0x0A,
243 0x74, 0x05,
244 0x75, 0x06,
245 0x76, 0x06,
246 0x78, 0x14,
247 0x79, 0x14,
248 0x7A, 0x14,
249 0x7D, 0x06,
250 0x92, 0x03, 0x20, 0x01, 0x00,
251 0x45, 0x01, 0x00,
252 0x48, 0x01, 0x00,
253 0x5A, 0x01, 0x00,
254 0x5B, 0x01, 0x05,
255 0x5E, 0x01, 0x1A,
256 0x5F, 0x01, 0x20,
257 0x9D, 0x01, 0x00,
258 0x47, 0x01, 0x00,
259 0x5C, 0x01, 0x64,
260 0x5D, 0x01, 0x1E,
261 0x97, 0x01, 0x20,
262 0xF7, 0x01, 0x3C,
Harald Welte52b1f982008-12-23 20:25:15 +0000263};
264
265// Power Control, SET ATTRIBUTES
266
267/*
268 Object Class: Power Control
269 BTS relat. Number: 0
270 Instance 2: FF
271 Instance 3: FF
272SET ATTRIBUTES
273 enableMsPowerControl: 00h = Disabled
274 enablePowerControlRLFW: 00h = Disabled
275 pcAveragingLev:
276 A_LEV_PC: 4 SACCH multiframes
277 W_LEV_PC: 1 SACCH multiframes
278 pcAveragingQual:
279 A_QUAL_PC: 4 SACCH multiframes
280 W_QUAL_PC: 2 SACCH multiframes
281 pcLowerThresholdLevDL: 0Fh
282 pcLowerThresholdLevUL: 0Ah
283 pcLowerThresholdQualDL: 05h = 3.2% < BER < 6.4%
284 pcLowerThresholdQualUL: 05h = 3.2% < BER < 6.4%
285 pcRLFThreshold: 0Ch
286 pcUpperThresholdLevDL: 14h
287 pcUpperThresholdLevUL: 0Fh
288 pcUpperThresholdQualDL: 04h = 1.6% < BER < 3.2%
289 pcUpperThresholdQualUL: 04h = 1.6% < BER < 3.2%
290 powerConfirm: 2 ,unit 2 SACCH multiframes
291 powerControlInterval: 2 ,unit 2 SACCH multiframes
292 powerIncrStepSize: 02h = 4 dB
293 powerRedStepSize: 01h = 2 dB
294 radioLinkTimeoutBs: 64 SACCH multiframes
295 enableBSPowerControl: 00h = disabled
296*/
297
298unsigned char msg_4[] =
299{
Harald Weltea865f1b2009-06-20 10:42:17 +0200300 NM_MT_BS11_SET_ATTR, NM_OC_BS11_PWR_CTRL, 0x00, 0xFF, 0xFF,
Harald Weltecd993872009-02-15 16:16:28 +0000301 NM_ATT_BS11_ENA_MS_PWR_CTRL, 0x00,
302 NM_ATT_BS11_ENA_PWR_CTRL_RLFW, 0x00,
303 0x7E, 0x04, 0x01,
304 0x7F, 0x04, 0x02,
305 0x80, 0x0F,
306 0x81, 0x0A,
307 0x82, 0x05,
308 0x83, 0x05,
309 0x84, 0x0C,
310 0x85, 0x14,
311 0x86, 0x0F,
312 0x87, 0x04,
313 0x88, 0x04,
314 0x89, 0x02,
315 0x8A, 0x02,
316 0x8B, 0x02,
317 0x8C, 0x01,
318 0x8D, 0x40,
319 0x65, 0x01, 0x00 // set to 0x01 to enable BSPowerControl
Harald Welte52b1f982008-12-23 20:25:15 +0000320};
321
322
323// Transceiver, SET TRX ATTRIBUTES (TRX 0)
324
325/*
326 Object Class: Transceiver
327 BTS relat. Number: 0
328 Tranceiver number: 0
329 Instance 3: FF
330SET TRX ATTRIBUTES
331 aRFCNList (HEX): 0001
Harald Weltecd993872009-02-15 16:16:28 +0000332 txPwrMaxReduction: 00h = 30dB
Harald Welte52b1f982008-12-23 20:25:15 +0000333 radioMeasGran: 254 SACCH multiframes
334 radioMeasRep: 01h = enabled
335 memberOfEmergencyConfig: 01h = TRUE
336 trxArea: 00h = TRX doesn't belong to a concentric cell
337*/
338
Harald Weltea865f1b2009-06-20 10:42:17 +0200339static unsigned char bs11_attr_radio[] =
Harald Welte52b1f982008-12-23 20:25:15 +0000340{
Harald Weltecd993872009-02-15 16:16:28 +0000341 NM_ATT_ARFCN_LIST, 0x01, 0x00, HARDCODED_ARFCN /*0x01*/,
Harald Welte311d0cf2009-02-17 17:45:59 +0000342 NM_ATT_RF_MAXPOWR_R, 0x00,
Harald Weltecd993872009-02-15 16:16:28 +0000343 NM_ATT_BS11_RADIO_MEAS_GRAN, 0x01, 0xFE,
344 NM_ATT_BS11_RADIO_MEAS_REP, 0x01, 0x01,
345 NM_ATT_BS11_EMRG_CFG_MEMBER, 0x01, 0x01,
346 NM_ATT_BS11_TRX_AREA, 0x01, 0x00,
Harald Welte52b1f982008-12-23 20:25:15 +0000347};
348
Harald Welte8c1d0e42009-02-15 03:38:12 +0000349static unsigned char nanobts_attr_bts[] = {
350 NM_ATT_INTERF_BOUND, 0x55, 0x5b, 0x61, 0x67, 0x6d, 0x73,
Harald Weltefe609d82009-05-23 18:14:31 +0000351 /* interference avg. period in numbers of SACCH multifr */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000352 NM_ATT_INTAVE_PARAM, 0x06,
Harald Welted0fbab52009-06-09 20:04:44 +0000353 /* conn fail based on SACCH error rate */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000354 NM_ATT_CONN_FAIL_CRIT, 0x00, 0x02, 0x01, 0x10,
355 NM_ATT_T200, 0x1e, 0x24, 0x24, 0xa8, 0x34, 0x21, 0xa8,
356 NM_ATT_MAX_TA, 0x3f,
Harald Welte311d0cf2009-02-17 17:45:59 +0000357 NM_ATT_OVERL_PERIOD, 0x00, 0x01, 10, /* seconds */
358 NM_ATT_CCCH_L_T, 10, /* percent */
359 NM_ATT_CCCH_L_I_P, 1, /* seconds */
Harald Weltefe609d82009-05-23 18:14:31 +0000360 NM_ATT_RACH_B_THRESH, 10, /* busy threshold in - dBm */
Harald Welted0fbab52009-06-09 20:04:44 +0000361 NM_ATT_LDAVG_SLOTS, 0x03, 0xe8, /* rach load averaging 1000 slots */
Harald Weltefe609d82009-05-23 18:14:31 +0000362 NM_ATT_BTS_AIR_TIMER, 128, /* miliseconds */
363 NM_ATT_NY1, 10, /* 10 retransmissions of physical config */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000364 NM_ATT_BCCH_ARFCN, HARDCODED_ARFCN >> 8, HARDCODED_ARFCN & 0xff,
Harald Welte060f6df2009-05-23 17:50:53 +0000365 NM_ATT_BSIC, HARDCODED_BSIC,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000366};
Harald Welte52b1f982008-12-23 20:25:15 +0000367
Harald Welte8c1d0e42009-02-15 03:38:12 +0000368static unsigned char nanobts_attr_radio[] = {
Harald Welted0fbab52009-06-09 20:04:44 +0000369 NM_ATT_RF_MAXPOWR_R, 0x0c, /* number of -2dB reduction steps / Pn */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000370 NM_ATT_ARFCN_LIST, 0x00, 0x02, HARDCODED_ARFCN >> 8, HARDCODED_ARFCN & 0xff,
371};
372
Harald Welte5c1e4582009-02-15 11:57:29 +0000373static unsigned char nanobts_attr_e0[] = {
Harald Welte0efe9b72009-07-12 09:33:54 +0200374 NM_ATT_IPACC_STREAM_ID, 0x00,
375 NM_ATT_IPACC_DST_IP_PORT, 0x0b, 0xbb, /* TCP PORT for RSL */
Harald Welte5c1e4582009-02-15 11:57:29 +0000376};
377
Harald Welteb4630602009-05-01 15:43:22 +0000378/* Callback function to be called whenever we get a GSM 12.21 state change event */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000379int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
380 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
381{
382 struct gsm_bts *bts;
383 struct gsm_bts_trx *trx;
384 struct gsm_bts_trx_ts *ts;
385
386 /* This is currently only required on nanoBTS */
387
388 switch (evt) {
389 case EVT_STATECHG_OPER:
390 switch (obj_class) {
391 case NM_OC_SITE_MANAGER:
392 bts = container_of(obj, struct gsm_bts, site_mgr);
393 if (old_state->operational != 2 && new_state->operational == 2) {
394 abis_nm_opstart(bts, NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
395 }
396 break;
397 case NM_OC_BTS:
398 bts = obj;
399 if (new_state->availability == 5) {
400 abis_nm_set_bts_attr(bts, nanobts_attr_bts,
401 sizeof(nanobts_attr_bts));
402 abis_nm_opstart(bts, NM_OC_BTS,
Harald Welte191280d2009-05-01 13:20:04 +0000403 bts->bts_nr, 0xff, 0xff);
Harald Welte8c1d0e42009-02-15 03:38:12 +0000404 abis_nm_chg_adm_state(bts, NM_OC_BTS,
Harald Welte191280d2009-05-01 13:20:04 +0000405 bts->bts_nr, 0xff, 0xff,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000406 NM_STATE_UNLOCKED);
407 }
408 break;
Harald Welte8c1d0e42009-02-15 03:38:12 +0000409 case NM_OC_CHANNEL:
410 ts = obj;
411 trx = ts->trx;
412 if (new_state->availability == 5) {
413 if (ts->nr == 0 && trx == trx->bts->c0)
Harald Weltee1bd2412009-02-15 14:40:09 +0000414 abis_nm_set_channel_attr(ts, NM_CHANC_BCCH_CBCH);
Harald Welte8c1d0e42009-02-15 03:38:12 +0000415 else
416 abis_nm_set_channel_attr(ts, NM_CHANC_TCHFull);
417 abis_nm_opstart(trx->bts, NM_OC_CHANNEL,
Harald Welte191280d2009-05-01 13:20:04 +0000418 trx->bts->bts_nr, trx->nr, ts->nr);
Harald Welte8c1d0e42009-02-15 03:38:12 +0000419 abis_nm_chg_adm_state(trx->bts, NM_OC_CHANNEL,
Harald Welte191280d2009-05-01 13:20:04 +0000420 trx->bts->bts_nr, trx->nr, ts->nr,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000421 NM_STATE_UNLOCKED);
422 }
423 break;
Harald Weltea994a482009-05-01 15:54:23 +0000424 default:
Harald Welte8c1d0e42009-02-15 03:38:12 +0000425 break;
426 }
427 break;
Harald Weltea994a482009-05-01 15:54:23 +0000428 default:
429 //DEBUGP(DMM, "Unhandled state change in %s:%d\n", __func__, __LINE__);
Holger Freytherff9592f2009-03-09 16:17:14 +0000430 break;
Harald Welte8c1d0e42009-02-15 03:38:12 +0000431 }
432 return 0;
433}
434
Harald Welteb4630602009-05-01 15:43:22 +0000435/* Callback function to be called every time we receive a 12.21 SW activated report */
436static int sw_activ_rep(struct msgb *mb)
437{
438 struct abis_om_fom_hdr *foh = msgb_l3(mb);
439 struct gsm_bts_trx *trx = mb->trx;
440
441 switch (foh->obj_class) {
442 case NM_OC_BASEB_TRANSC:
443 /* TRX software is active, tell it to initiate RSL Link */
444 abis_nm_ipaccess_msg(trx->bts, 0xe0, NM_OC_BASEB_TRANSC,
445 trx->bts->bts_nr, trx->nr, 0xff,
446 nanobts_attr_e0, sizeof(nanobts_attr_e0));
447 abis_nm_opstart(trx->bts, NM_OC_BASEB_TRANSC,
448 trx->bts->bts_nr, trx->nr, 0xff);
449 abis_nm_chg_adm_state(trx->bts, NM_OC_BASEB_TRANSC,
450 trx->bts->bts_nr, trx->nr, 0xff,
451 NM_STATE_UNLOCKED);
452 break;
453 case NM_OC_RADIO_CARRIER:
454 abis_nm_set_radio_attr(trx, nanobts_attr_radio,
455 sizeof(nanobts_attr_radio));
456 abis_nm_opstart(trx->bts, NM_OC_RADIO_CARRIER,
457 trx->bts->bts_nr, trx->nr, 0xff);
458 abis_nm_chg_adm_state(trx->bts, NM_OC_RADIO_CARRIER,
459 trx->bts->bts_nr, trx->nr, 0xff,
460 NM_STATE_UNLOCKED);
461 break;
462 }
463 return 0;
464}
465
Holger Hans Peter Freyther500f3ca2009-06-10 10:48:14 +0200466/* Callback function for NACK on the OML NM */
467static int oml_msg_nack(int mt)
468{
469 if (mt == NM_MT_SET_BTS_ATTR_NACK) {
470 fprintf(stderr, "Failed to set BTS attributes. That is fatal. "
471 "Was the bts type and frequency properly specified?\n");
472 exit(-1);
473 }
474
475 return 0;
476}
477
Harald Welteb4630602009-05-01 15:43:22 +0000478/* Callback function to be called every time we receive a signal from NM */
479static int nm_sig_cb(unsigned int subsys, unsigned int signal,
480 void *handler_data, void *signal_data)
481{
482 switch (signal) {
483 case S_NM_SW_ACTIV_REP:
484 return sw_activ_rep(signal_data);
Holger Hans Peter Freyther500f3ca2009-06-10 10:48:14 +0200485 case S_NM_NACK:
486 return oml_msg_nack((int)signal_data);
Harald Welteb4630602009-05-01 15:43:22 +0000487 default:
488 break;
489 }
490 return 0;
491}
492
Harald Welte8c1d0e42009-02-15 03:38:12 +0000493static void bootstrap_om_nanobts(struct gsm_bts *bts)
494{
Harald Weltee1bd2412009-02-15 14:40:09 +0000495 /* We don't do callback based bootstrapping, but event driven (see above) */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000496}
497
498static void bootstrap_om_bs11(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000499{
Harald Weltee441d9c2009-06-21 16:17:15 +0200500 struct gsm_bts_trx *trx = bts->c0;
Harald Welte52b1f982008-12-23 20:25:15 +0000501
502 /* stop sending event reports */
503 abis_nm_event_reports(bts, 0);
504
505 /* begin DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000506 abis_nm_bs11_db_transmission(bts, 1);
Harald Welte52b1f982008-12-23 20:25:15 +0000507
Harald Welte702d8702008-12-26 20:25:35 +0000508 /* end DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000509 abis_nm_bs11_db_transmission(bts, 0);
Harald Welte702d8702008-12-26 20:25:35 +0000510
511 /* Reset BTS Site manager resource */
Harald Welte78374892009-01-18 19:09:22 +0000512 abis_nm_bs11_reset_resource(bts);
Harald Welte702d8702008-12-26 20:25:35 +0000513
514 /* begin DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000515 abis_nm_bs11_db_transmission(bts, 1);
Harald Welte702d8702008-12-26 20:25:35 +0000516
Harald Welte52b1f982008-12-23 20:25:15 +0000517 abis_nm_raw_msg(bts, sizeof(msg_1), msg_1); /* set BTS SiteMgr attr*/
Harald Weltea865f1b2009-06-20 10:42:17 +0200518 abis_nm_set_bts_attr(bts, bs11_attr_bts, sizeof(bs11_attr_bts));
Harald Welte52b1f982008-12-23 20:25:15 +0000519 abis_nm_raw_msg(bts, sizeof(msg_3), msg_3); /* set BTS handover attr */
520 abis_nm_raw_msg(bts, sizeof(msg_4), msg_4); /* set BTS power control attr */
521
522 /* Connect signalling of bts0/trx0 to e1_0/ts1/64kbps */
523 abis_nm_conn_terr_sign(trx, 0, 1, 0xff);
Harald Weltea865f1b2009-06-20 10:42:17 +0200524 abis_nm_set_radio_attr(trx, bs11_attr_radio, sizeof(bs11_attr_radio));
Harald Welte52b1f982008-12-23 20:25:15 +0000525
526 /* Use TEI 1 for signalling */
527 abis_nm_establish_tei(bts, 0, 0, 1, 0xff, 0x01);
528 abis_nm_set_channel_attr(&trx->ts[0], NM_CHANC_SDCCH_CBCH);
Harald Weltecd06bfb2009-02-10 17:33:56 +0000529
530#ifdef HAVE_TRX1
Harald Welte52b1f982008-12-23 20:25:15 +0000531 /* TRX 1 */
532 abis_nm_conn_terr_sign(&bts->trx[1], 0, 1, 0xff);
533 /* FIXME: TRX ATTRIBUTE */
534 abis_nm_establish_tei(bts, 0, 0, 1, 0xff, 0x02);
535#endif
536
537 /* SET CHANNEL ATTRIBUTE TS1 */
Harald Welte23887b62009-02-18 03:37:20 +0000538 abis_nm_set_channel_attr(&trx->ts[1], NM_CHANC_TCHFull);
Harald Welte52b1f982008-12-23 20:25:15 +0000539 /* Connect traffic of bts0/trx0/ts1 to e1_0/ts2/b */
540 abis_nm_conn_terr_traf(&trx->ts[1], 0, 2, 1);
541
542 /* SET CHANNEL ATTRIBUTE TS2 */
Harald Welte23887b62009-02-18 03:37:20 +0000543 abis_nm_set_channel_attr(&trx->ts[2], NM_CHANC_TCHFull);
Harald Welte52b1f982008-12-23 20:25:15 +0000544 /* Connect traffic of bts0/trx0/ts2 to e1_0/ts2/c */
545 abis_nm_conn_terr_traf(&trx->ts[2], 0, 2, 2);
546
547 /* SET CHANNEL ATTRIBUTE TS3 */
Harald Welte23887b62009-02-18 03:37:20 +0000548 abis_nm_set_channel_attr(&trx->ts[3], NM_CHANC_TCHFull);
Harald Welte52b1f982008-12-23 20:25:15 +0000549 /* Connect traffic of bts0/trx0/ts3 to e1_0/ts2/d */
550 abis_nm_conn_terr_traf(&trx->ts[3], 0, 2, 3);
551
552 /* SET CHANNEL ATTRIBUTE TS4 */
Harald Welte23887b62009-02-18 03:37:20 +0000553 abis_nm_set_channel_attr(&trx->ts[4], NM_CHANC_TCHFull);
Harald Welte52b1f982008-12-23 20:25:15 +0000554 /* Connect traffic of bts0/trx0/ts4 to e1_0/ts3/a */
555 abis_nm_conn_terr_traf(&trx->ts[4], 0, 3, 0);
556
557 /* SET CHANNEL ATTRIBUTE TS5 */
Harald Welte23887b62009-02-18 03:37:20 +0000558 abis_nm_set_channel_attr(&trx->ts[5], NM_CHANC_TCHFull);
Harald Welte52b1f982008-12-23 20:25:15 +0000559 /* Connect traffic of bts0/trx0/ts5 to e1_0/ts3/b */
560 abis_nm_conn_terr_traf(&trx->ts[5], 0, 3, 1);
561
562 /* SET CHANNEL ATTRIBUTE TS6 */
Harald Welte23887b62009-02-18 03:37:20 +0000563 abis_nm_set_channel_attr(&trx->ts[6], NM_CHANC_TCHFull);
Harald Welte52b1f982008-12-23 20:25:15 +0000564 /* Connect traffic of bts0/trx0/ts6 to e1_0/ts3/c */
565 abis_nm_conn_terr_traf(&trx->ts[6], 0, 3, 2);
566
567 /* SET CHANNEL ATTRIBUTE TS7 */
Harald Welte23887b62009-02-18 03:37:20 +0000568 abis_nm_set_channel_attr(&trx->ts[7], NM_CHANC_TCHFull);
Harald Welte52b1f982008-12-23 20:25:15 +0000569 /* Connect traffic of bts0/trx0/ts7 to e1_0/ts3/d */
570 abis_nm_conn_terr_traf(&trx->ts[7], 0, 3, 3);
571
572 /* end DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000573 abis_nm_bs11_db_transmission(bts, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000574
575 /* Reset BTS Site manager resource */
Harald Welte78374892009-01-18 19:09:22 +0000576 abis_nm_bs11_reset_resource(bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000577
578 /* restart sending event reports */
579 abis_nm_event_reports(bts, 1);
580}
581
Harald Welte8c1d0e42009-02-15 03:38:12 +0000582static void bootstrap_om(struct gsm_bts *bts)
583{
Harald Welteedb37782009-05-01 14:59:07 +0000584 fprintf(stdout, "bootstrapping OML for BTS %u\n", bts->nr);
Harald Welte8c1d0e42009-02-15 03:38:12 +0000585
586 switch (bts->type) {
587 case GSM_BTS_TYPE_BS11:
588 bootstrap_om_bs11(bts);
589 break;
590 case GSM_BTS_TYPE_NANOBTS_900:
591 case GSM_BTS_TYPE_NANOBTS_1800:
592 bootstrap_om_nanobts(bts);
593 break;
594 default:
595 fprintf(stderr, "Unable to bootstrap OML: Unknown BTS type %d\n", bts->type);
596 }
597}
598
Harald Welted1252502009-01-01 01:50:32 +0000599static int shutdown_om(struct gsm_bts *bts)
600{
601 /* stop sending event reports */
602 abis_nm_event_reports(bts, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000603
Harald Welted1252502009-01-01 01:50:32 +0000604 /* begin DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000605 abis_nm_bs11_db_transmission(bts, 1);
Harald Welted1252502009-01-01 01:50:32 +0000606
607 /* end DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000608 abis_nm_bs11_db_transmission(bts, 0);
Harald Welted1252502009-01-01 01:50:32 +0000609
610 /* Reset BTS Site manager resource */
Harald Welte78374892009-01-18 19:09:22 +0000611 abis_nm_bs11_reset_resource(bts);
Harald Welted1252502009-01-01 01:50:32 +0000612
613 return 0;
614}
615
616static int shutdown_net(struct gsm_network *net)
617{
Harald Weltee441d9c2009-06-21 16:17:15 +0200618 struct gsm_bts *bts;
619
620 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welted1252502009-01-01 01:50:32 +0000621 int rc;
Harald Weltee441d9c2009-06-21 16:17:15 +0200622 rc = shutdown_om(bts);
Harald Welted1252502009-01-01 01:50:32 +0000623 if (rc < 0)
624 return rc;
625 }
626
627 return 0;
628}
Harald Welte52b1f982008-12-23 20:25:15 +0000629
630struct bcch_info {
631 u_int8_t type;
632 u_int8_t len;
633 const u_int8_t *data;
634};
635
636/*
637SYSTEM INFORMATION TYPE 1
638 Cell channel description
639 Format-ID bit map 0
640 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
641 RACH Control Parameters
642 maximum 7 retransmissions
643 8 slots used to spread transmission
644 cell not barred for access
645 call reestablishment not allowed
646 Access Control Class = 0000
647*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000648static u_int8_t si1[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000649 /* header */0x55, 0x06, 0x19,
650 /* ccdesc */0x04 /*0x00*/, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
651 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /*0x01*/,
652 /* rach */0xD5, 0x00, 0x00,
653 /* s1 reset*/0x2B
Harald Welte52b1f982008-12-23 20:25:15 +0000654};
655
656/*
657 SYSTEM INFORMATION TYPE 2
658 Neighbour Cells Description
659 EXT-IND: Carries the complete BA
660 BA-IND = 0
661 Format-ID bit map 0
662 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
663 NCC permitted (NCC) = FF
664 RACH Control Parameters
665 maximum 7 retransmissions
666 8 slots used to spread transmission
667 cell not barred for access
668 call reestablishment not allowed
669 Access Control Class = 0000
670*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000671static u_int8_t si2[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000672 /* header */0x59, 0x06, 0x1A,
673 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
674 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
675 /* ncc */0xFF,
676 /* rach*/0xD5, 0x00, 0x00
Harald Welte52b1f982008-12-23 20:25:15 +0000677};
678
679/*
680SYSTEM INFORMATION TYPE 3
681 Cell identity = 00001 (1h)
682 Location area identification
683 Mobile Country Code (MCC): 001
684 Mobile Network Code (MNC): 01
685 Location Area Code (LAC): 00001 (1h)
686 Control Channel Description
687 Attach-detach: MSs in the cell are not allowed to apply IMSI attach /detach
688 0 blocks reserved for access grant
689 1 channel used for CCCH, with SDCCH
690 5 multiframes period for PAGING REQUEST
691 Time-out T3212 = 0
692 Cell Options BCCH
693 Power control indicator: not set
694 MSs shall not use uplink DTX
695 Radio link timeout = 36
696 Cell Selection Parameters
697 Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
Harald Welte3b2ec422008-12-29 04:11:14 +0000698 max.TX power level MS may use for CCH = 2 <- according to GSM05.05 39dBm (max)
Harald Welte52b1f982008-12-23 20:25:15 +0000699 Additional Reselect Parameter Indication (ACS) = only SYSTEM INFO 4: The SI rest octets, if present, shall be used to derive the value of PI and possibly C2 parameters
700 Half rate support (NECI): New establishment causes are not supported
701 min.RX signal level for MS = 0
702 RACH Control Parameters
703 maximum 7 retransmissions
704 8 slots used to spread transmission
705 cell not barred for access
706 call reestablishment not allowed
707 Access Control Class = 0000
Harald Welte53833f62009-07-05 13:41:40 +0200708 SI 3 Rest Octets (not present)
Harald Welte52b1f982008-12-23 20:25:15 +0000709*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000710static u_int8_t si3[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000711 /* header */0x49, 0x06, 0x1B,
712 /* cell */0x00, 0x01,
713 /* lai */0x00, 0xF1, 0x10, 0x00, 0x01,
714 /* desc */0x01, 0x03, 0x00,
715 /* option*/0x28,
716 /* selection*/0x62, 0x00,
717 /* rach */0xD5, 0x00, 0x00,
Harald Welte53833f62009-07-05 13:41:40 +0200718 /* rest */ 0x2B, 0x2B, 0x2B, 0x2B
Harald Welte52b1f982008-12-23 20:25:15 +0000719};
720
721/*
722SYSTEM INFORMATION TYPE 4
723 Location area identification
724 Mobile Country Code (MCC): 001
725 Mobile Network Code (MNC): 01
726 Location Area Code (LAC): 00001 (1h)
727 Cell Selection Parameters
728 Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
729 max.TX power level MS may use for CCH = 2
730 Additional Reselect Parameter Indication (ACS) = only SYSTEM INFO 4: The SI rest octets, if present, shall be used to derive the value of PI and possibly C2 parameters
731 Half rate support (NECI): New establishment causes are not supported
732 min.RX signal level for MS = 0
733 RACH Control Parameters
734 maximum 7 retransmissions
735 8 slots used to spread transmission
736 cell not barred for access
737 call reestablishment not allowed
738 Access Control Class = 0000
Harald Welte53833f62009-07-05 13:41:40 +0200739 CBCH Channel Description
Harald Welte52b1f982008-12-23 20:25:15 +0000740 Type = SDCCH/4[2]
741 Timeslot Number: 0
742 Training Sequence Code: 7h
743 ARFCN: 1
Harald Welte53833f62009-07-05 13:41:40 +0200744 SI Rest Octets (not present)
Harald Welte52b1f982008-12-23 20:25:15 +0000745*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000746static u_int8_t si4[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000747 /* header */0x41, 0x06, 0x1C,
748 /* lai */0x00, 0xF1, 0x10, 0x00, 0x01,
749 /* sel */0x62, 0x00,
750 /* rach*/0xD5, 0x00, 0x00,
Harald Welte53833f62009-07-05 13:41:40 +0200751 /* cbch chan desc */ 0x64, 0x30, 0xE0, HARDCODED_ARFCN/*0x01*/,
752 /* rest octets */ 0x2B, 0x2B, 0x2B, 0x2B, 0x2B, 0x2B
Harald Welte52b1f982008-12-23 20:25:15 +0000753};
754
755/*
756 SYSTEM INFORMATION TYPE 5
757 Neighbour Cells Description
758 EXT-IND: Carries the complete BA
759 BA-IND = 0
760 Format-ID bit map 0
761 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
762*/
763
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000764static u_int8_t si5[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000765 /* header without l2 len*/0x06, 0x1D,
766 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
767 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Harald Welte52b1f982008-12-23 20:25:15 +0000768};
769
770// SYSTEM INFORMATION TYPE 6
771
772/*
773SACCH FILLING
774 System Info Type: SYSTEM INFORMATION 6
775 L3 Information (Hex): 06 1E 00 01 xx xx 10 00 01 28 FF
776
777SYSTEM INFORMATION TYPE 6
778 Cell identity = 00001 (1h)
779 Location area identification
780 Mobile Country Code (MCC): 001
781 Mobile Network Code (MNC): 01
782 Location Area Code (LAC): 00001 (1h)
783 Cell Options SACCH
784 Power control indicator: not set
785 MSs shall not use uplink DTX on a TCH-F. MS shall not use uplink DTX on TCH-H.
786 Radio link timeout = 36
787 NCC permitted (NCC) = FF
788*/
789
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000790static u_int8_t si6[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000791 /* header */0x06, 0x1E,
792 /* cell id*/ 0x00, 0x01,
793 /* lai */ 0x00, 0xF1, 0x10, 0x00, 0x01,
794 /* options */ 0x28,
795 /* ncc */ 0xFF,
Harald Welte52b1f982008-12-23 20:25:15 +0000796};
797
798
799
800static const struct bcch_info bcch_infos[] = {
801 {
802 .type = RSL_SYSTEM_INFO_1,
803 .len = sizeof(si1),
804 .data = si1,
805 }, {
806 .type = RSL_SYSTEM_INFO_2,
807 .len = sizeof(si2),
808 .data = si2,
809 }, {
810 .type = RSL_SYSTEM_INFO_3,
811 .len = sizeof(si3),
812 .data = si3,
813 }, {
814 .type = RSL_SYSTEM_INFO_4,
815 .len = sizeof(si4),
816 .data = si4,
817 },
818};
819
Holger Freyther24287b62008-12-28 16:32:41 +0000820static_assert(sizeof(si1) == sizeof(struct gsm48_system_information_type_1), type1)
821static_assert(sizeof(si2) == sizeof(struct gsm48_system_information_type_2), type2)
822static_assert(sizeof(si3) == sizeof(struct gsm48_system_information_type_3), type3)
823static_assert(sizeof(si4) >= sizeof(struct gsm48_system_information_type_4), type4)
Harald Welte104604e2008-12-28 16:36:11 +0000824static_assert(sizeof(si5) == sizeof(struct gsm48_system_information_type_5), type5)
825static_assert(sizeof(si6) >= sizeof(struct gsm48_system_information_type_6), type6)
Holger Freyther24287b62008-12-28 16:32:41 +0000826
Harald Welte52b1f982008-12-23 20:25:15 +0000827/* set all system information types */
Harald Weltee79769b2009-02-07 00:48:17 +0000828static int set_system_infos(struct gsm_bts_trx *trx)
Harald Welte52b1f982008-12-23 20:25:15 +0000829{
830 int i;
831
832 for (i = 0; i < ARRAY_SIZE(bcch_infos); i++) {
Harald Weltee79769b2009-02-07 00:48:17 +0000833 rsl_bcch_info(trx, bcch_infos[i].type,
Harald Welte52b1f982008-12-23 20:25:15 +0000834 bcch_infos[i].data,
835 bcch_infos[i].len);
836 }
Harald Weltee79769b2009-02-07 00:48:17 +0000837 rsl_sacch_filling(trx, RSL_SYSTEM_INFO_5, si5, sizeof(si5));
838 rsl_sacch_filling(trx, RSL_SYSTEM_INFO_6, si6, sizeof(si6));
Harald Weltead384642008-12-26 10:20:07 +0000839
840 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000841}
842
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000843/*
844 * Patch the various SYSTEM INFORMATION tables to update
845 * the LAI
846 */
847static void patch_tables(struct gsm_bts *bts)
848{
Harald Weltee441d9c2009-06-21 16:17:15 +0200849 u_int8_t arfcn_low = bts->c0->arfcn & 0xff;
850 u_int8_t arfcn_high = (bts->c0->arfcn >> 8) & 0x0f;
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000851 /* covert the raw packet to the struct */
852 struct gsm48_system_information_type_3 *type_3 =
853 (struct gsm48_system_information_type_3*)&si3;
854 struct gsm48_system_information_type_4 *type_4 =
855 (struct gsm48_system_information_type_4*)&si4;
856 struct gsm48_system_information_type_6 *type_6 =
857 (struct gsm48_system_information_type_6*)&si6;
Harald Welteb84e2f42008-12-28 23:42:04 +0000858 struct gsm48_loc_area_id lai;
859
860 gsm0408_generate_lai(&lai, bts->network->country_code,
Harald Welte110c0ab2009-05-23 16:27:05 +0000861 bts->network->network_code,
862 bts->location_area_code);
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000863
864 /* assign the MCC and MNC */
Harald Welteb84e2f42008-12-28 23:42:04 +0000865 type_3->lai = lai;
866 type_4->lai = lai;
867 type_6->lai = lai;
Harald Welte98981882009-01-06 18:59:11 +0000868
Harald Welte94009322009-02-15 15:38:42 +0000869 /* patch ARFCN into BTS Attributes */
Harald Weltea865f1b2009-06-20 10:42:17 +0200870 bs11_attr_bts[69] &= 0xf0;
871 bs11_attr_bts[69] |= arfcn_high;
872 bs11_attr_bts[70] = arfcn_low;
Harald Welte94009322009-02-15 15:38:42 +0000873 nanobts_attr_bts[42] &= 0xf0;
874 nanobts_attr_bts[42] |= arfcn_high;
875 nanobts_attr_bts[43] = arfcn_low;
Harald Welte98981882009-01-06 18:59:11 +0000876
Harald Welte94009322009-02-15 15:38:42 +0000877 /* patch ARFCN into TRX Attributes */
Harald Weltea865f1b2009-06-20 10:42:17 +0200878 bs11_attr_radio[2] &= 0xf0;
879 bs11_attr_radio[2] |= arfcn_high;
880 bs11_attr_radio[3] = arfcn_low;
Harald Welte94009322009-02-15 15:38:42 +0000881 nanobts_attr_radio[5] &= 0xf0;
882 nanobts_attr_radio[5] |= arfcn_high;
883 nanobts_attr_radio[6] = arfcn_low;
Harald Welte98981882009-01-06 18:59:11 +0000884
885 type_4->data[2] &= 0xf0;
886 type_4->data[2] |= arfcn_high;
887 type_4->data[3] = arfcn_low;
Holger Freyther1adb4ff2009-02-04 00:04:52 +0000888
889 /* patch Control Channel Description 10.5.2.11 */
890 type_3->control_channel_desc = bts->chan_desc;
Harald Welte78f2f502009-05-23 16:56:52 +0000891
892 /* patch BSIC */
Harald Weltea865f1b2009-06-20 10:42:17 +0200893 bs11_attr_bts[1] = bts->bsic;
Harald Welte78f2f502009-05-23 16:56:52 +0000894 nanobts_attr_bts[sizeof(nanobts_attr_bts)-1] = bts->bsic;
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000895}
896
897
Harald Weltee79769b2009-02-07 00:48:17 +0000898static void bootstrap_rsl(struct gsm_bts_trx *trx)
Harald Welte52b1f982008-12-23 20:25:15 +0000899{
Harald Welteedb37782009-05-01 14:59:07 +0000900 fprintf(stdout, "bootstrapping RSL for BTS/TRX (%u/%u) "
901 "using MCC=%u MNC=%u\n", trx->nr, trx->bts->nr, MCC, MNC);
Harald Weltee79769b2009-02-07 00:48:17 +0000902 set_system_infos(trx);
Harald Welte52b1f982008-12-23 20:25:15 +0000903}
904
Harald Welte1fa60c82009-02-09 18:13:26 +0000905void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
Harald Weltead384642008-12-26 10:20:07 +0000906{
907 switch (event) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000908 case EVT_E1_TEI_UP:
909 switch (type) {
910 case E1INP_SIGN_OML:
911 bootstrap_om(trx->bts);
912 break;
913 case E1INP_SIGN_RSL:
914 bootstrap_rsl(trx);
915 break;
916 default:
917 break;
918 }
Harald Weltead384642008-12-26 10:20:07 +0000919 break;
Harald Welte1fa60c82009-02-09 18:13:26 +0000920 case EVT_E1_TEI_DN:
921 fprintf(stderr, "Lost some E1 TEI link\n");
922 /* FIXME: deal with TEI or L1 link loss */
Harald Weltead384642008-12-26 10:20:07 +0000923 break;
924 default:
Harald Weltead384642008-12-26 10:20:07 +0000925 break;
926 }
927}
928
Harald Welteedb37782009-05-01 14:59:07 +0000929static int bootstrap_bts(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000930{
Harald Weltefcd24452009-06-20 18:15:19 +0200931 bts->band = BAND;
Holger Freyther0a173bf2009-04-22 22:07:07 +0000932 bts->location_area_code = LAC;
Harald Weltee441d9c2009-06-21 16:17:15 +0200933 bts->c0->arfcn = ARFCN;
Holger Freyther1adb4ff2009-02-04 00:04:52 +0000934
935 /* Control Channel Description */
936 memset(&bts->chan_desc, 0, sizeof(struct gsm48_control_channel_descr));
Harald Welte41fbf442009-02-24 22:34:22 +0000937 bts->chan_desc.att = 1;
Holger Freyther1adb4ff2009-02-04 00:04:52 +0000938 bts->chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_C;
939 bts->chan_desc.bs_pa_mfrms = RSL_BS_PA_MFRMS_5;
940 bts->chan_desc.t3212 = 0;
941
Harald Welte98981882009-01-06 18:59:11 +0000942 patch_tables(bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000943
Holger Freyther1fd34142009-02-09 23:42:03 +0000944 paging_init(bts);
Harald Welte38c2f132009-01-06 23:10:57 +0000945
Holger Freytherb8be6542009-06-02 03:25:14 +0000946 if (bts->type == GSM_BTS_TYPE_BS11) {
Harald Weltee441d9c2009-06-21 16:17:15 +0200947 struct gsm_bts_trx *trx = bts->c0;
Holger Freytherb8be6542009-06-02 03:25:14 +0000948 set_ts_e1link(&trx->ts[0], 0, 1, 0xff);
949 set_ts_e1link(&trx->ts[1], 0, 2, 1);
950 set_ts_e1link(&trx->ts[2], 0, 2, 2);
951 set_ts_e1link(&trx->ts[3], 0, 2, 3);
952 set_ts_e1link(&trx->ts[4], 0, 3, 0);
953 set_ts_e1link(&trx->ts[5], 0, 3, 1);
954 set_ts_e1link(&trx->ts[6], 0, 3, 2);
955 set_ts_e1link(&trx->ts[7], 0, 3, 3);
956#ifdef HAVE_TRX1
957 /* TRX 1 */
958 trx = &bts->trx[1];
959 set_ts_e1link(&trx->ts[0], 0, 1, 0xff);
960 set_ts_e1link(&trx->ts[1], 0, 2, 1);
961 set_ts_e1link(&trx->ts[2], 0, 2, 2);
962 set_ts_e1link(&trx->ts[3], 0, 2, 3);
963 set_ts_e1link(&trx->ts[4], 0, 3, 0);
964 set_ts_e1link(&trx->ts[5], 0, 3, 1);
965 set_ts_e1link(&trx->ts[6], 0, 3, 2);
966 set_ts_e1link(&trx->ts[7], 0, 3, 3);
967#endif
968 }
969
Harald Welteedb37782009-05-01 14:59:07 +0000970 return 0;
971}
972
973static int bootstrap_network(void)
974{
Holger Hans Peter Freythere91f5782009-06-10 10:20:16 +0200975 switch(BTS_TYPE) {
976 case GSM_BTS_TYPE_NANOBTS_1800:
977 if (ARFCN < 512 || ARFCN > 885) {
978 fprintf(stderr, "GSM1800 channel must be between 512-885.\n");
979 return -EINVAL;
980 }
981 break;
982 case GSM_BTS_TYPE_BS11:
983 case GSM_BTS_TYPE_NANOBTS_900:
984 /* Assume we have a P-GSM900 here */
985 if (ARFCN < 1 || ARFCN > 124) {
986 fprintf(stderr, "GSM900 channel must be between 1-124.\n");
987 return -EINVAL;
988 }
989 break;
990 case GSM_BTS_TYPE_UNKNOWN:
991 fprintf(stderr, "Unknown BTS. Please use the --bts-type switch\n");
992 return -EINVAL;
993 }
994
Harald Welteedb37782009-05-01 14:59:07 +0000995 /* initialize our data structures */
Harald Weltee441d9c2009-06-21 16:17:15 +0200996 gsmnet = gsm_network_init(MCC, MNC, mncc_recv);
Harald Welteedb37782009-05-01 14:59:07 +0000997 if (!gsmnet)
998 return -ENOMEM;
999
1000 gsmnet->name_long = "OpenBSC";
1001 gsmnet->name_short = "OpenBSC";
1002
Holger Freytherc7b86f92009-06-06 13:54:20 +00001003 if (db_init(database_name)) {
Holger Freytheref7f7ce2009-04-19 06:35:12 +00001004 printf("DB: Failed to init database. Please check the option settings.\n");
1005 return -1;
1006 }
1007 printf("DB: Database initialized.\n");
1008
1009 if (db_prepare()) {
1010 printf("DB: Failed to prepare database.\n");
1011 return -1;
1012 }
1013 printf("DB: Database prepared.\n");
1014
Holger Freyther219518d2009-01-02 22:04:43 +00001015 telnet_init(gsmnet, 4242);
Harald Weltead384642008-12-26 10:20:07 +00001016
Harald Welteb4630602009-05-01 15:43:22 +00001017 register_signal_handler(SS_NM, nm_sig_cb, NULL);
1018
Harald Welte1fa60c82009-02-09 18:13:26 +00001019 /* E1 mISDN input setup */
Harald Welteedb37782009-05-01 14:59:07 +00001020 if (BTS_TYPE == GSM_BTS_TYPE_BS11) {
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +02001021 struct gsm_bts *bts = gsm_bts_alloc(gsmnet, BTS_TYPE, HARDCODED_TSC, HARDCODED_BSIC);
1022 bootstrap_bts(bts);
1023
Harald Welteedb37782009-05-01 14:59:07 +00001024 gsmnet->num_bts = 1;
Holger Freytherb5c00f52009-04-22 22:08:07 +00001025 return e1_config(bts, cardnr, release_l2);
Harald Welteedb37782009-05-01 14:59:07 +00001026 } else {
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +02001027 struct nano_bts_id *bts_id;
1028 struct gsm_bts *bts;
Harald Weltefcd24452009-06-20 18:15:19 +02001029
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +02001030 if (llist_empty(&nanobts_ids)) {
1031 fprintf(stderr, "You need to specify -i DEVICE_1 -i DEVICE_2 for nanoBTS.\n");
1032 return -EINVAL;
1033 }
1034
1035 llist_for_each_entry(bts_id, &nanobts_ids, entry) {
1036 bts = gsm_bts_alloc(gsmnet, BTS_TYPE, HARDCODED_TSC, HARDCODED_BSIC);
1037 bootstrap_bts(bts);
1038 bts->ip_access.site_id = bts_id->site_id;
1039 bts->ip_access.bts_id = 0;
1040 }
1041
Harald Weltecf559782009-05-01 15:43:49 +00001042 return ipaccess_setup(gsmnet);
Harald Welteedb37782009-05-01 14:59:07 +00001043 }
Harald Welte52b1f982008-12-23 20:25:15 +00001044}
Harald Weltef6b7a902008-12-26 00:05:11 +00001045
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00001046static void create_pcap_file(char *file)
1047{
1048 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1049 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
1050
1051 if (fd < 0) {
1052 perror("Failed to open file for pcap");
1053 return;
1054 }
1055
Holger Freyther0469cf62009-03-31 12:14:16 +00001056 e1_set_pcap_fd(fd);
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00001057}
1058
Holger Freytherb332f612008-12-27 12:46:51 +00001059static void print_usage()
1060{
1061 printf("Usage: bsc_hack\n");
1062}
1063
1064static void print_help()
1065{
1066 printf(" Some useful help...\n");
1067 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Holger Freytherefde7fb2008-12-28 14:14:56 +00001068 printf(" -s --disable-color\n");
1069 printf(" -n --network-code number(MNC) \n");
1070 printf(" -c --country-code number (MCC) \n");
Holger Freyther0a173bf2009-04-22 22:07:07 +00001071 printf(" -L --location-area-code number (LAC) \n");
Harald Welte98981882009-01-06 18:59:11 +00001072 printf(" -f --arfcn number The frequency ARFCN\n");
Holger Freytherbde36102008-12-28 22:51:39 +00001073 printf(" -l --database db-name The database to use\n");
Holger Freyther89824fc2008-12-30 16:18:18 +00001074 printf(" -a --authorize-everyone Allow everyone into the network.\n");
Holger Freythere97f7fb2008-12-31 18:52:11 +00001075 printf(" -r --reject-cause number The reject cause for LOCATION UPDATING REJECT.\n");
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00001076 printf(" -p --pcap file The filename of the pcap file\n");
Harald Weltee1bd2412009-02-15 14:40:09 +00001077 printf(" -t --bts-type type The BTS type (bs11, nanobts900, nanobts1800)\n");
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +02001078 printf(" -i --bts-id=NUMBER The known nanoBTS device numbers. Can be specified multiple times.\n");
Holger Freytherdda22c12009-04-22 22:07:31 +00001079 printf(" -C --cardnr number For bs11 select E1 card number other than 0\n");
Holger Freytherb5c00f52009-04-22 22:08:07 +00001080 printf(" -R --release-l2 Releases mISDN layer 2 after exit, to unload driver.\n");
Holger Freytherb332f612008-12-27 12:46:51 +00001081 printf(" -h --help this text\n");
1082}
1083
1084static void handle_options(int argc, char** argv)
1085{
1086 while (1) {
Harald Welte2cf161b2009-06-20 22:36:41 +02001087 int option_index = 0, c;
Holger Freytherb332f612008-12-27 12:46:51 +00001088 static struct option long_options[] = {
1089 {"help", 0, 0, 'h'},
1090 {"debug", 1, 0, 'd'},
Holger Freytherefde7fb2008-12-28 14:14:56 +00001091 {"disable-color", 0, 0, 's'},
1092 {"network-code", 1, 0, 'n'},
1093 {"country-code", 1, 0, 'c'},
Holger Freyther0a173bf2009-04-22 22:07:07 +00001094 {"location-area-code", 1, 0, 'L'},
Holger Freytherbde36102008-12-28 22:51:39 +00001095 {"database", 1, 0, 'l'},
Holger Freyther89824fc2008-12-30 16:18:18 +00001096 {"authorize-everyone", 0, 0, 'a'},
Holger Freythere97f7fb2008-12-31 18:52:11 +00001097 {"reject-cause", 1, 0, 'r'},
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00001098 {"pcap", 1, 0, 'p'},
Harald Welte98981882009-01-06 18:59:11 +00001099 {"arfcn", 1, 0, 'f'},
Harald Welte8c1d0e42009-02-15 03:38:12 +00001100 {"bts-type", 1, 0, 't'},
Holger Freytherdda22c12009-04-22 22:07:31 +00001101 {"cardnr", 1, 0, 'C'},
Holger Freytherb5c00f52009-04-22 22:08:07 +00001102 {"release-l2", 0, 0, 'R'},
Harald Welted3ff51d2009-06-09 20:21:57 +00001103 {"timestamp", 0, 0, 'T'},
Harald Weltefcd24452009-06-20 18:15:19 +02001104 {"band", 0, 0, 'b'},
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +02001105 {"bts-id", 1, 0, 'i'},
Holger Freytherb332f612008-12-27 12:46:51 +00001106 {0, 0, 0, 0}
1107 };
1108
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +02001109 c = getopt_long(argc, argv, "hc:n:d:sar:p:f:t:C:RL:l:Tb:i:",
Holger Freytherb332f612008-12-27 12:46:51 +00001110 long_options, &option_index);
1111 if (c == -1)
1112 break;
1113
1114 switch (c) {
1115 case 'h':
1116 print_usage();
1117 print_help();
1118 exit(0);
Holger Freytherefde7fb2008-12-28 14:14:56 +00001119 case 's':
Holger Freytherb332f612008-12-27 12:46:51 +00001120 debug_use_color(0);
1121 break;
1122 case 'd':
1123 debug_parse_category_mask(optarg);
1124 break;
Holger Freytherefde7fb2008-12-28 14:14:56 +00001125 case 'n':
1126 MNC = atoi(optarg);
1127 break;
1128 case 'c':
1129 MCC = atoi(optarg);
1130 break;
Holger Freyther0a173bf2009-04-22 22:07:07 +00001131 case 'L':
1132 LAC = atoi(optarg);
1133 break;
Harald Welte98981882009-01-06 18:59:11 +00001134 case 'f':
1135 ARFCN = atoi(optarg);
1136 break;
Harald Welte8965da42009-01-06 18:09:02 +00001137 case 'l':
Holger Freytherbde36102008-12-28 22:51:39 +00001138 database_name = strdup(optarg);
1139 break;
Holger Freyther89824fc2008-12-30 16:18:18 +00001140 case 'a':
1141 gsm0408_allow_everyone(1);
1142 break;
Holger Freythere97f7fb2008-12-31 18:52:11 +00001143 case 'r':
1144 gsm0408_set_reject_cause(atoi(optarg));
1145 break;
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00001146 case 'p':
1147 create_pcap_file(optarg);
1148 break;
Harald Welte8c1d0e42009-02-15 03:38:12 +00001149 case 't':
1150 BTS_TYPE = parse_btstype(optarg);
1151 break;
Holger Freytherdda22c12009-04-22 22:07:31 +00001152 case 'C':
1153 cardnr = atoi(optarg);
1154 break;
Holger Freytherb5c00f52009-04-22 22:08:07 +00001155 case 'R':
1156 release_l2 = 1;
1157 break;
Harald Welted3ff51d2009-06-09 20:21:57 +00001158 case 'T':
1159 debug_timestamp(1);
1160 break;
Harald Weltefcd24452009-06-20 18:15:19 +02001161 case 'b':
1162 BAND = gsm_band_parse(atoi(optarg));
1163 break;
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +02001164 case 'i': {
1165 struct nano_bts_id *bts_id = talloc_zero(tall_bsc_ctx, struct nano_bts_id);
1166 if (!bts_id) {
1167 fprintf(stderr, "Failed to allocate bts id\n");
1168 exit(-1);
1169 }
1170
1171 bts_id->site_id = atoi(optarg);
1172 llist_add(&bts_id->entry, &nanobts_ids);
1173 break;
1174 }
Holger Freytherb332f612008-12-27 12:46:51 +00001175 default:
1176 /* ignore */
1177 break;
1178 }
1179 }
1180}
1181
Harald Welted1252502009-01-01 01:50:32 +00001182static void signal_handler(int signal)
1183{
1184 fprintf(stdout, "signal %u received\n", signal);
1185
1186 switch (signal) {
1187 case SIGHUP:
1188 case SIGABRT:
1189 shutdown_net(gsmnet);
1190 break;
Harald Welte2cf161b2009-06-20 22:36:41 +02001191 case SIGUSR1:
1192 talloc_report_full(tall_bsc_ctx, stderr);
1193 break;
Harald Welted1252502009-01-01 01:50:32 +00001194 default:
1195 break;
1196 }
1197}
1198
Harald Weltef6b7a902008-12-26 00:05:11 +00001199int main(int argc, char **argv)
1200{
Harald Welte1fa60c82009-02-09 18:13:26 +00001201 int rc;
1202
Harald Welte2cf161b2009-06-20 22:36:41 +02001203 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
1204
Holger Freytherb332f612008-12-27 12:46:51 +00001205 /* parse options */
1206 handle_options(argc, argv);
1207
Harald Welte65ccf882009-02-24 22:36:20 +00001208 /* seed the PRNG */
1209 srand(time(NULL));
1210
Harald Welte1fa60c82009-02-09 18:13:26 +00001211 rc = bootstrap_network();
1212 if (rc < 0)
1213 exit(1);
Harald Weltef6b7a902008-12-26 00:05:11 +00001214
Harald Welted1252502009-01-01 01:50:32 +00001215 signal(SIGHUP, &signal_handler);
1216 signal(SIGABRT, &signal_handler);
Harald Welte2cf161b2009-06-20 22:36:41 +02001217 signal(SIGUSR1, &signal_handler);
Harald Welted1252502009-01-01 01:50:32 +00001218
Harald Weltef6b7a902008-12-26 00:05:11 +00001219 while (1) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001220 bsc_upqueue(gsmnet);
Harald Welte04d3c922009-05-23 06:07:04 +00001221 bsc_select_main(0);
Harald Weltef6b7a902008-12-26 00:05:11 +00001222 }
1223}