blob: db47bfc789772f8da22afe3e4d898667a51d70b9 [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 Welte66b6a8d2009-08-09 14:45:18 +020040#include <openbsc/gsm_utils.h>
Harald Welte255539c2008-12-28 02:26:27 +000041#include <openbsc/gsm_04_08.h>
Harald Weltead384642008-12-26 10:20:07 +000042#include <openbsc/select.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/abis_rsl.h>
44#include <openbsc/abis_nm.h>
Harald Welte702d8702008-12-26 20:25:35 +000045#include <openbsc/debug.h>
Holger Freyther5677ae32008-12-27 09:41:03 +000046#include <openbsc/misdn.h>
Holger Freyther219518d2009-01-02 22:04:43 +000047#include <openbsc/telnet_interface.h>
Harald Welte38c2f132009-01-06 23:10:57 +000048#include <openbsc/paging.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000049#include <openbsc/e1_input.h>
Harald Welteb4630602009-05-01 15:43:22 +000050#include <openbsc/signal.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020051#include <openbsc/talloc.h>
52
Harald Welte52b1f982008-12-23 20:25:15 +000053/* global pointer to the gsm network data structure */
Harald Welte879c85a2009-05-01 15:00:20 +000054static struct gsm_network *gsmnet;
Harald Welte52b1f982008-12-23 20:25:15 +000055
Holger Freytherefde7fb2008-12-28 14:14:56 +000056/* MCC and MNC for the Location Area Identifier */
57static int MCC = 1;
58static int MNC = 1;
Holger Freytherdda22c12009-04-22 22:07:31 +000059static int cardnr = 0;
Holger Freytherb5c00f52009-04-22 22:08:07 +000060static int release_l2 = 0;
Harald Welte8c1d0e42009-02-15 03:38:12 +000061static enum gsm_bts_type BTS_TYPE = GSM_BTS_TYPE_BS11;
Holger Freytherbde36102008-12-28 22:51:39 +000062static const char *database_name = "hlr.sqlite3";
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +020063static const char *config_file = "openbsc.cfg";
Harald Welte805f6442009-07-28 18:25:29 +020064extern int ipacc_rtp_direct;
Holger Freytherefde7fb2008-12-28 14:14:56 +000065
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +020066
Harald Welte52b1f982008-12-23 20:25:15 +000067/* The following definitions are for OM and NM packets that we cannot yet
68 * generate by code but we just pass on */
69
70// BTS Site Manager, SET ATTRIBUTES
71
72/*
73 Object Class: BTS Site Manager
74 Instance 1: FF
75 Instance 2: FF
76 Instance 3: FF
77SET ATTRIBUTES
78 sAbisExternalTime: 2007/09/08 14:36:11
79 omLAPDRelTimer: 30sec
80 shortLAPDIntTimer: 5sec
81 emergencyTimer1: 10 minutes
82 emergencyTimer2: 0 minutes
83*/
84
85unsigned char msg_1[] =
86{
Harald Weltea865f1b2009-06-20 10:42:17 +020087 NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER, 0xFF, 0xFF, 0xFF,
Harald Welte110c0ab2009-05-23 16:27:05 +000088 NM_ATT_BS11_ABIS_EXT_TIME, 0x07,
89 0xD7, 0x09, 0x08, 0x0E, 0x24, 0x0B, 0xCE,
90 0x02,
91 0x00, 0x1E,
92 NM_ATT_BS11_SH_LAPD_INT_TIMER,
93 0x01, 0x05,
Harald Weltecd993872009-02-15 16:16:28 +000094 0x42, 0x02, 0x00, 0x0A,
95 0x44, 0x02, 0x00, 0x00
Harald Welte52b1f982008-12-23 20:25:15 +000096};
97
98// BTS, SET BTS ATTRIBUTES
99
100/*
101 Object Class: BTS
102 BTS relat. Number: 0
103 Instance 2: FF
104 Instance 3: FF
105SET BTS ATTRIBUTES
106 bsIdentityCode / BSIC:
107 PLMN_colour_code: 7h
108 BS_colour_code: 7h
109 BTS Air Timer T3105: 4 ,unit 10 ms
110 btsIsHopping: FALSE
Harald Welte83282292009-02-01 16:22:19 +0000111 periodCCCHLoadIndication: 1sec
Holger Freyther3b910432009-02-11 00:43:48 +0000112 thresholdCCCHLoadIndication: 0%
Harald Welte52b1f982008-12-23 20:25:15 +0000113 cellAllocationNumber: 00h = GSM 900
114 enableInterferenceClass: 00h = Disabled
115 fACCHQual: 6 (FACCH stealing flags minus 1)
116 intaveParameter: 31 SACCH multiframes
117 interferenceLevelBoundaries:
118 Interference Boundary 1: 0Ah
119 Interference Boundary 2: 0Fh
120 Interference Boundary 3: 14h
121 Interference Boundary 4: 19h
122 Interference Boundary 5: 1Eh
123 mSTxPwrMax: 11
124 GSM range: 2=39dBm, 15=13dBm, stepsize 2 dBm
125 DCS1800 range: 0=30dBm, 15=0dBm, stepsize 2 dBm
126 PCS1900 range: 0=30dBm, 15=0dBm, stepsize 2 dBm
127 30=33dBm, 31=32dBm
128 ny1:
129 Maximum number of repetitions for PHYSICAL INFORMATION message (GSM 04.08): 20
130 powerOutputThresholds:
131 Out Power Fault Threshold: -10 dB
132 Red Out Power Threshold: - 6 dB
133 Excessive Out Power Threshold: 5 dB
134 rACHBusyThreshold: -127 dBm
135 rACHLoadAveragingSlots: 250 ,number of RACH burst periods
136 rfResourceIndicationPeriod: 125 SACCH multiframes
137 T200:
138 SDCCH: 044 in 5 ms
139 FACCH/Full rate: 031 in 5 ms
140 FACCH/Half rate: 041 in 5 ms
141 SACCH with TCH SAPI0: 090 in 10 ms
142 SACCH with SDCCH: 090 in 10 ms
143 SDCCH with SAPI3: 090 in 5 ms
144 SACCH with TCH SAPI3: 135 in 10 ms
145 tSync: 9000 units of 10 msec
146 tTrau: 9000 units of 10 msec
147 enableUmLoopTest: 00h = disabled
148 enableExcessiveDistance: 00h = Disabled
149 excessiveDistance: 64km
150 hoppingMode: 00h = baseband hopping
151 cellType: 00h = Standard Cell
152 BCCH ARFCN / bCCHFrequency: 1
153*/
154
Harald Weltea865f1b2009-06-20 10:42:17 +0200155static unsigned char bs11_attr_bts[] =
Harald Welte52b1f982008-12-23 20:25:15 +0000156{
Harald Welte060f6df2009-05-23 17:50:53 +0000157 NM_ATT_BSIC, HARDCODED_BSIC,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000158 NM_ATT_BTS_AIR_TIMER, 0x04,
Harald Weltecd993872009-02-15 16:16:28 +0000159 NM_ATT_BS11_BTSLS_HOPPING, 0x00,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000160 NM_ATT_CCCH_L_I_P, 0x01,
161 NM_ATT_CCCH_L_T, 0x00,
Harald Welte7b26bcb2009-05-28 11:39:21 +0000162 NM_ATT_BS11_CELL_ALLOC_NR, NM_BS11_CANR_GSM,
163 NM_ATT_BS11_ENA_INTERF_CLASS, 0x01,
Harald Weltecd993872009-02-15 16:16:28 +0000164 NM_ATT_BS11_FACCH_QUAL, 0x06,
Harald Weltefe609d82009-05-23 18:14:31 +0000165 /* interference avg. period in numbers of SACCH multifr */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000166 NM_ATT_INTAVE_PARAM, 0x1F,
167 NM_ATT_INTERF_BOUND, 0x0A, 0x0F, 0x14, 0x19, 0x1E, 0x7B,
168 NM_ATT_CCCH_L_T, 0x23,
169 NM_ATT_GSM_TIME, 0x28, 0x00,
170 NM_ATT_ADM_STATE, 0x03,
171 NM_ATT_RACH_B_THRESH, 0x7F,
172 NM_ATT_LDAVG_SLOTS, 0x00, 0xFA,
Harald Weltecd993872009-02-15 16:16:28 +0000173 NM_ATT_BS11_RF_RES_IND_PER, 0x7D,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000174 NM_ATT_T200, 0x2C, 0x1F, 0x29, 0x5A, 0x5A, 0x5A, 0x87,
Harald Weltecd993872009-02-15 16:16:28 +0000175 NM_ATT_BS11_TSYNC, 0x23, 0x28,
176 NM_ATT_BS11_TTRAU, 0x23, 0x28,
177 NM_ATT_TEST_DUR, 0x01, 0x00,
178 NM_ATT_OUTST_ALARM, 0x01, 0x00,
179 NM_ATT_BS11_EXCESSIVE_DISTANCE, 0x01, 0x40,
180 NM_ATT_BS11_HOPPING_MODE, 0x01, 0x00,
181 NM_ATT_BS11_PLL, 0x01, 0x00,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000182 NM_ATT_BCCH_ARFCN, 0x00, HARDCODED_ARFCN/*0x01*/,
Harald Welte52b1f982008-12-23 20:25:15 +0000183};
184
185// Handover Recognition, SET ATTRIBUTES
186
187/*
188Illegal Contents GSM Formatted O&M Msg
189 Object Class: Handover Recognition
190 BTS relat. Number: 0
191 Instance 2: FF
192 Instance 3: FF
193SET ATTRIBUTES
194 enableDelayPowerBudgetHO: 00h = Disabled
195 enableDistanceHO: 00h = Disabled
196 enableInternalInterCellHandover: 00h = Disabled
197 enableInternalIntraCellHandover: 00h = Disabled
198 enablePowerBudgetHO: 00h = Disabled
199 enableRXLEVHO: 00h = Disabled
200 enableRXQUALHO: 00h = Disabled
201 hoAveragingDistance: 8 SACCH multiframes
202 hoAveragingLev:
203 A_LEV_HO: 8 SACCH multiframes
204 W_LEV_HO: 1 SACCH multiframes
205 hoAveragingPowerBudget: 16 SACCH multiframes
206 hoAveragingQual:
207 A_QUAL_HO: 8 SACCH multiframes
208 W_QUAL_HO: 2 SACCH multiframes
209 hoLowerThresholdLevDL: (10 - 110) dBm
210 hoLowerThresholdLevUL: (5 - 110) dBm
211 hoLowerThresholdQualDL: 06h = 6.4% < BER < 12.8%
212 hoLowerThresholdQualUL: 06h = 6.4% < BER < 12.8%
213 hoThresholdLevDLintra : (20 - 110) dBm
214 hoThresholdLevULintra: (20 - 110) dBm
215 hoThresholdMsRangeMax: 20 km
216 nCell: 06h
217 timerHORequest: 3 ,unit 2 SACCH multiframes
218*/
219
220unsigned char msg_3[] =
221{
Harald Weltea865f1b2009-06-20 10:42:17 +0200222 NM_MT_BS11_SET_ATTR, NM_OC_BS11_HANDOVER, 0x00, 0xFF, 0xFF,
Harald Welte56554712009-07-18 16:18:11 +0200223 0xD0, 0x00, /* enableDelayPowerBudgetHO */
224 0x64, 0x00, /* enableDistanceHO */
225 0x67, 0x00, /* enableInternalInterCellHandover */
226 0x68, 0x00, /* enableInternalInterCellHandover */
227 0x6A, 0x00, /* enablePowerBudgetHO */
228 0x6C, 0x00, /* enableRXLEVHO */
229 0x6D, 0x00, /* enableRXQUALHO */
230 0x6F, 0x08, /* hoAveragingDistance */
231 0x70, 0x08, 0x01, /* hoAveragingLev */
Harald Weltecd993872009-02-15 16:16:28 +0000232 0x71, 0x10, 0x10, 0x10,
Harald Welte56554712009-07-18 16:18:11 +0200233 0x72, 0x08, 0x02, /* hoAveragingQual */
234 0x73, 0x0A, /* hoLowerThresholdLevDL */
235 0x74, 0x05, /* hoLowerThresholdLevUL */
236 0x75, 0x06, /* hoLowerThresholdQualDL */
237 0x76, 0x06, /* hoLowerThresholdQualUL */
238 0x78, 0x14, /* hoThresholdLevDLintra */
239 0x79, 0x14, /* hoThresholdLevULintra */
240 0x7A, 0x14, /* hoThresholdMsRangeMax */
241 0x7D, 0x06, /* nCell */
242 NM_ATT_BS11_TIMER_HO_REQUEST, 0x03,
243 0x20, 0x01, 0x00,
Harald Weltecd993872009-02-15 16:16:28 +0000244 0x45, 0x01, 0x00,
245 0x48, 0x01, 0x00,
246 0x5A, 0x01, 0x00,
247 0x5B, 0x01, 0x05,
248 0x5E, 0x01, 0x1A,
249 0x5F, 0x01, 0x20,
250 0x9D, 0x01, 0x00,
251 0x47, 0x01, 0x00,
252 0x5C, 0x01, 0x64,
253 0x5D, 0x01, 0x1E,
254 0x97, 0x01, 0x20,
255 0xF7, 0x01, 0x3C,
Harald Welte52b1f982008-12-23 20:25:15 +0000256};
257
258// Power Control, SET ATTRIBUTES
259
260/*
261 Object Class: Power Control
262 BTS relat. Number: 0
263 Instance 2: FF
264 Instance 3: FF
265SET ATTRIBUTES
266 enableMsPowerControl: 00h = Disabled
267 enablePowerControlRLFW: 00h = Disabled
268 pcAveragingLev:
269 A_LEV_PC: 4 SACCH multiframes
270 W_LEV_PC: 1 SACCH multiframes
271 pcAveragingQual:
272 A_QUAL_PC: 4 SACCH multiframes
273 W_QUAL_PC: 2 SACCH multiframes
274 pcLowerThresholdLevDL: 0Fh
275 pcLowerThresholdLevUL: 0Ah
276 pcLowerThresholdQualDL: 05h = 3.2% < BER < 6.4%
277 pcLowerThresholdQualUL: 05h = 3.2% < BER < 6.4%
278 pcRLFThreshold: 0Ch
279 pcUpperThresholdLevDL: 14h
280 pcUpperThresholdLevUL: 0Fh
281 pcUpperThresholdQualDL: 04h = 1.6% < BER < 3.2%
282 pcUpperThresholdQualUL: 04h = 1.6% < BER < 3.2%
283 powerConfirm: 2 ,unit 2 SACCH multiframes
284 powerControlInterval: 2 ,unit 2 SACCH multiframes
285 powerIncrStepSize: 02h = 4 dB
286 powerRedStepSize: 01h = 2 dB
287 radioLinkTimeoutBs: 64 SACCH multiframes
288 enableBSPowerControl: 00h = disabled
289*/
290
291unsigned char msg_4[] =
292{
Harald Weltea865f1b2009-06-20 10:42:17 +0200293 NM_MT_BS11_SET_ATTR, NM_OC_BS11_PWR_CTRL, 0x00, 0xFF, 0xFF,
Harald Weltecd993872009-02-15 16:16:28 +0000294 NM_ATT_BS11_ENA_MS_PWR_CTRL, 0x00,
295 NM_ATT_BS11_ENA_PWR_CTRL_RLFW, 0x00,
Harald Welte56554712009-07-18 16:18:11 +0200296 0x7E, 0x04, 0x01, /* pcAveragingLev */
297 0x7F, 0x04, 0x02, /* pcAveragingQual */
298 0x80, 0x0F, /* pcLowerThresholdLevDL */
299 0x81, 0x0A, /* pcLowerThresholdLevUL */
300 0x82, 0x05, /* pcLowerThresholdQualDL */
301 0x83, 0x05, /* pcLowerThresholdQualUL */
302 0x84, 0x0C, /* pcRLFThreshold */
303 0x85, 0x14, /* pcUpperThresholdLevDL */
304 0x86, 0x0F, /* pcUpperThresholdLevUL */
305 0x87, 0x04, /* pcUpperThresholdQualDL */
306 0x88, 0x04, /* pcUpperThresholdQualUL */
307 0x89, 0x02, /* powerConfirm */
308 0x8A, 0x02, /* powerConfirmInterval */
309 0x8B, 0x02, /* powerIncrStepSize */
310 0x8C, 0x01, /* powerRedStepSize */
311 0x8D, 0x40, /* radioLinkTimeoutBs */
Harald Weltecd993872009-02-15 16:16:28 +0000312 0x65, 0x01, 0x00 // set to 0x01 to enable BSPowerControl
Harald Welte52b1f982008-12-23 20:25:15 +0000313};
314
315
316// Transceiver, SET TRX ATTRIBUTES (TRX 0)
317
318/*
319 Object Class: Transceiver
320 BTS relat. Number: 0
321 Tranceiver number: 0
322 Instance 3: FF
323SET TRX ATTRIBUTES
324 aRFCNList (HEX): 0001
Harald Weltecd993872009-02-15 16:16:28 +0000325 txPwrMaxReduction: 00h = 30dB
Harald Welte52b1f982008-12-23 20:25:15 +0000326 radioMeasGran: 254 SACCH multiframes
327 radioMeasRep: 01h = enabled
328 memberOfEmergencyConfig: 01h = TRUE
329 trxArea: 00h = TRX doesn't belong to a concentric cell
330*/
331
Harald Weltea865f1b2009-06-20 10:42:17 +0200332static unsigned char bs11_attr_radio[] =
Harald Welte52b1f982008-12-23 20:25:15 +0000333{
Harald Weltecd993872009-02-15 16:16:28 +0000334 NM_ATT_ARFCN_LIST, 0x01, 0x00, HARDCODED_ARFCN /*0x01*/,
Harald Welte311d0cf2009-02-17 17:45:59 +0000335 NM_ATT_RF_MAXPOWR_R, 0x00,
Harald Weltecd993872009-02-15 16:16:28 +0000336 NM_ATT_BS11_RADIO_MEAS_GRAN, 0x01, 0xFE,
337 NM_ATT_BS11_RADIO_MEAS_REP, 0x01, 0x01,
338 NM_ATT_BS11_EMRG_CFG_MEMBER, 0x01, 0x01,
339 NM_ATT_BS11_TRX_AREA, 0x01, 0x00,
Harald Welte52b1f982008-12-23 20:25:15 +0000340};
341
Harald Welte8c1d0e42009-02-15 03:38:12 +0000342static unsigned char nanobts_attr_bts[] = {
343 NM_ATT_INTERF_BOUND, 0x55, 0x5b, 0x61, 0x67, 0x6d, 0x73,
Harald Weltefe609d82009-05-23 18:14:31 +0000344 /* interference avg. period in numbers of SACCH multifr */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000345 NM_ATT_INTAVE_PARAM, 0x06,
Harald Welted0fbab52009-06-09 20:04:44 +0000346 /* conn fail based on SACCH error rate */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000347 NM_ATT_CONN_FAIL_CRIT, 0x00, 0x02, 0x01, 0x10,
348 NM_ATT_T200, 0x1e, 0x24, 0x24, 0xa8, 0x34, 0x21, 0xa8,
349 NM_ATT_MAX_TA, 0x3f,
Harald Welte311d0cf2009-02-17 17:45:59 +0000350 NM_ATT_OVERL_PERIOD, 0x00, 0x01, 10, /* seconds */
351 NM_ATT_CCCH_L_T, 10, /* percent */
352 NM_ATT_CCCH_L_I_P, 1, /* seconds */
Harald Weltefe609d82009-05-23 18:14:31 +0000353 NM_ATT_RACH_B_THRESH, 10, /* busy threshold in - dBm */
Harald Welted0fbab52009-06-09 20:04:44 +0000354 NM_ATT_LDAVG_SLOTS, 0x03, 0xe8, /* rach load averaging 1000 slots */
Harald Weltefe609d82009-05-23 18:14:31 +0000355 NM_ATT_BTS_AIR_TIMER, 128, /* miliseconds */
356 NM_ATT_NY1, 10, /* 10 retransmissions of physical config */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000357 NM_ATT_BCCH_ARFCN, HARDCODED_ARFCN >> 8, HARDCODED_ARFCN & 0xff,
Harald Welte060f6df2009-05-23 17:50:53 +0000358 NM_ATT_BSIC, HARDCODED_BSIC,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000359};
Harald Welte52b1f982008-12-23 20:25:15 +0000360
Harald Welte8c1d0e42009-02-15 03:38:12 +0000361static unsigned char nanobts_attr_radio[] = {
Harald Welted0fbab52009-06-09 20:04:44 +0000362 NM_ATT_RF_MAXPOWR_R, 0x0c, /* number of -2dB reduction steps / Pn */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000363 NM_ATT_ARFCN_LIST, 0x00, 0x02, HARDCODED_ARFCN >> 8, HARDCODED_ARFCN & 0xff,
364};
365
Harald Welte5c1e4582009-02-15 11:57:29 +0000366static unsigned char nanobts_attr_e0[] = {
Harald Welte0efe9b72009-07-12 09:33:54 +0200367 NM_ATT_IPACC_STREAM_ID, 0x00,
368 NM_ATT_IPACC_DST_IP_PORT, 0x0b, 0xbb, /* TCP PORT for RSL */
Harald Welte5c1e4582009-02-15 11:57:29 +0000369};
370
Harald Welteb4630602009-05-01 15:43:22 +0000371/* Callback function to be called whenever we get a GSM 12.21 state change event */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000372int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
373 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
374{
375 struct gsm_bts *bts;
376 struct gsm_bts_trx *trx;
377 struct gsm_bts_trx_ts *ts;
378
379 /* This is currently only required on nanoBTS */
380
381 switch (evt) {
382 case EVT_STATECHG_OPER:
383 switch (obj_class) {
384 case NM_OC_SITE_MANAGER:
385 bts = container_of(obj, struct gsm_bts, site_mgr);
386 if (old_state->operational != 2 && new_state->operational == 2) {
387 abis_nm_opstart(bts, NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
388 }
389 break;
390 case NM_OC_BTS:
391 bts = obj;
392 if (new_state->availability == 5) {
393 abis_nm_set_bts_attr(bts, nanobts_attr_bts,
394 sizeof(nanobts_attr_bts));
395 abis_nm_opstart(bts, NM_OC_BTS,
Harald Welte191280d2009-05-01 13:20:04 +0000396 bts->bts_nr, 0xff, 0xff);
Harald Welte8c1d0e42009-02-15 03:38:12 +0000397 abis_nm_chg_adm_state(bts, NM_OC_BTS,
Harald Welte191280d2009-05-01 13:20:04 +0000398 bts->bts_nr, 0xff, 0xff,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000399 NM_STATE_UNLOCKED);
400 }
401 break;
Harald Welte8c1d0e42009-02-15 03:38:12 +0000402 case NM_OC_CHANNEL:
403 ts = obj;
404 trx = ts->trx;
405 if (new_state->availability == 5) {
406 if (ts->nr == 0 && trx == trx->bts->c0)
Harald Welte89e9d592009-08-09 22:01:26 +0200407 abis_nm_set_channel_attr(ts, NM_CHANC_BCCHComb);
Harald Welte8c1d0e42009-02-15 03:38:12 +0000408 else
409 abis_nm_set_channel_attr(ts, NM_CHANC_TCHFull);
410 abis_nm_opstart(trx->bts, NM_OC_CHANNEL,
Harald Welte191280d2009-05-01 13:20:04 +0000411 trx->bts->bts_nr, trx->nr, ts->nr);
Harald Welte8c1d0e42009-02-15 03:38:12 +0000412 abis_nm_chg_adm_state(trx->bts, NM_OC_CHANNEL,
Harald Welte191280d2009-05-01 13:20:04 +0000413 trx->bts->bts_nr, trx->nr, ts->nr,
Harald Welte8c1d0e42009-02-15 03:38:12 +0000414 NM_STATE_UNLOCKED);
415 }
416 break;
Harald Weltea994a482009-05-01 15:54:23 +0000417 default:
Harald Welte8c1d0e42009-02-15 03:38:12 +0000418 break;
419 }
420 break;
Harald Weltea994a482009-05-01 15:54:23 +0000421 default:
422 //DEBUGP(DMM, "Unhandled state change in %s:%d\n", __func__, __LINE__);
Holger Freytherff9592f2009-03-09 16:17:14 +0000423 break;
Harald Welte8c1d0e42009-02-15 03:38:12 +0000424 }
425 return 0;
426}
427
Harald Welteb4630602009-05-01 15:43:22 +0000428/* Callback function to be called every time we receive a 12.21 SW activated report */
429static int sw_activ_rep(struct msgb *mb)
430{
431 struct abis_om_fom_hdr *foh = msgb_l3(mb);
432 struct gsm_bts_trx *trx = mb->trx;
433
434 switch (foh->obj_class) {
435 case NM_OC_BASEB_TRANSC:
436 /* TRX software is active, tell it to initiate RSL Link */
437 abis_nm_ipaccess_msg(trx->bts, 0xe0, NM_OC_BASEB_TRANSC,
438 trx->bts->bts_nr, trx->nr, 0xff,
439 nanobts_attr_e0, sizeof(nanobts_attr_e0));
440 abis_nm_opstart(trx->bts, NM_OC_BASEB_TRANSC,
441 trx->bts->bts_nr, trx->nr, 0xff);
442 abis_nm_chg_adm_state(trx->bts, NM_OC_BASEB_TRANSC,
443 trx->bts->bts_nr, trx->nr, 0xff,
444 NM_STATE_UNLOCKED);
445 break;
446 case NM_OC_RADIO_CARRIER:
447 abis_nm_set_radio_attr(trx, nanobts_attr_radio,
448 sizeof(nanobts_attr_radio));
449 abis_nm_opstart(trx->bts, NM_OC_RADIO_CARRIER,
450 trx->bts->bts_nr, trx->nr, 0xff);
451 abis_nm_chg_adm_state(trx->bts, NM_OC_RADIO_CARRIER,
452 trx->bts->bts_nr, trx->nr, 0xff,
453 NM_STATE_UNLOCKED);
454 break;
455 }
456 return 0;
457}
458
Holger Hans Peter Freyther500f3ca2009-06-10 10:48:14 +0200459/* Callback function for NACK on the OML NM */
460static int oml_msg_nack(int mt)
461{
462 if (mt == NM_MT_SET_BTS_ATTR_NACK) {
463 fprintf(stderr, "Failed to set BTS attributes. That is fatal. "
464 "Was the bts type and frequency properly specified?\n");
465 exit(-1);
466 }
467
468 return 0;
469}
470
Harald Welteb4630602009-05-01 15:43:22 +0000471/* Callback function to be called every time we receive a signal from NM */
472static int nm_sig_cb(unsigned int subsys, unsigned int signal,
473 void *handler_data, void *signal_data)
474{
475 switch (signal) {
476 case S_NM_SW_ACTIV_REP:
477 return sw_activ_rep(signal_data);
Holger Hans Peter Freyther500f3ca2009-06-10 10:48:14 +0200478 case S_NM_NACK:
479 return oml_msg_nack((int)signal_data);
Harald Welteb4630602009-05-01 15:43:22 +0000480 default:
481 break;
482 }
483 return 0;
484}
485
Harald Welte8c1d0e42009-02-15 03:38:12 +0000486static void bootstrap_om_nanobts(struct gsm_bts *bts)
487{
Harald Weltee1bd2412009-02-15 14:40:09 +0000488 /* We don't do callback based bootstrapping, but event driven (see above) */
Harald Welte8c1d0e42009-02-15 03:38:12 +0000489}
490
Harald Welte349aba62009-08-10 12:31:31 +0200491static void nm_reconfig_ts(struct gsm_bts_trx_ts *ts)
Harald Welte52b1f982008-12-23 20:25:15 +0000492{
Harald Welte349aba62009-08-10 12:31:31 +0200493 enum abis_nm_chan_comb ccomb = abis_nm_chcomb4pchan(ts->pchan);
494 struct gsm_e1_subslot *e1l = &ts->e1_link;
Harald Welteac56e8b2009-08-06 17:40:24 +0200495
Harald Welte349aba62009-08-10 12:31:31 +0200496 abis_nm_set_channel_attr(ts, ccomb);
497
498 if (is_ipaccess_bts(ts->trx->bts))
Harald Welteac56e8b2009-08-06 17:40:24 +0200499 return;
Harald Welte349aba62009-08-10 12:31:31 +0200500
501 switch (ts->pchan) {
502 case GSM_PCHAN_TCH_F:
503 case GSM_PCHAN_TCH_H:
504 abis_nm_conn_terr_traf(ts, e1l->e1_nr, e1l->e1_ts,
505 e1l->e1_ts_ss);
506 break;
507 default:
508 break;
509 }
510}
511
512static void nm_reconfig_trx(struct gsm_bts_trx *trx)
513{
514 struct gsm_e1_subslot *e1l = &trx->rsl_e1_link;
515 int i;
516
517 switch (trx->bts->type) {
518 case GSM_BTS_TYPE_BS11:
519 abis_nm_conn_terr_sign(trx, e1l->e1_nr, e1l->e1_ts,
520 e1l->e1_ts_ss);
521 abis_nm_establish_tei(trx->bts, trx->nr, e1l->e1_nr,
522 e1l->e1_ts, e1l->e1_ts_ss, trx->rsl_tei);
523
524 /* Set Radio Attributes */
525 if (trx == trx->bts->c0)
526 abis_nm_set_radio_attr(trx, bs11_attr_radio,
527 sizeof(bs11_attr_radio));
528 else {
529 u_int8_t trx1_attr_radio[sizeof(bs11_attr_radio)];
530 u_int8_t arfcn_low = trx->arfcn & 0xff;
531 u_int8_t arfcn_high = (trx->arfcn >> 8) & 0x0f;
532 memcpy(trx1_attr_radio, bs11_attr_radio,
533 sizeof(trx1_attr_radio));
534
535 /* patch ARFCN into TRX Attributes */
536 trx1_attr_radio[2] &= 0xf0;
537 trx1_attr_radio[2] |= arfcn_high;
538 trx1_attr_radio[3] = arfcn_low;
539
540 abis_nm_set_radio_attr(trx, trx1_attr_radio,
541 sizeof(trx1_attr_radio));
542 }
543 break;
544 default:
545 break;
Harald Welteac56e8b2009-08-06 17:40:24 +0200546 }
Harald Welte52b1f982008-12-23 20:25:15 +0000547
Harald Welte349aba62009-08-10 12:31:31 +0200548 for (i = 0; i < TRX_NR_TS; i++)
549 nm_reconfig_ts(&trx->ts[i]);
550}
551
552static void nm_reconfig_bts(struct gsm_bts *bts)
553{
554 struct gsm_bts_trx *trx;
555
556 switch (bts->type) {
557 case GSM_BTS_TYPE_BS11:
558 abis_nm_raw_msg(bts, sizeof(msg_1), msg_1); /* set BTS SiteMgr attr*/
559 abis_nm_set_bts_attr(bts, bs11_attr_bts, sizeof(bs11_attr_bts));
560 abis_nm_raw_msg(bts, sizeof(msg_3), msg_3); /* set BTS handover attr */
561 abis_nm_raw_msg(bts, sizeof(msg_4), msg_4); /* set BTS power control attr */
562 break;
563 default:
564 break;
565 }
566
567 llist_for_each_entry(trx, &bts->trx_list, list)
568 nm_reconfig_trx(trx);
569}
570
571static void bootstrap_om_bs11(struct gsm_bts *bts)
572{
Harald Welte52b1f982008-12-23 20:25:15 +0000573 /* stop sending event reports */
574 abis_nm_event_reports(bts, 0);
575
576 /* begin DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000577 abis_nm_bs11_db_transmission(bts, 1);
Harald Welte52b1f982008-12-23 20:25:15 +0000578
Harald Welte702d8702008-12-26 20:25:35 +0000579 /* end DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000580 abis_nm_bs11_db_transmission(bts, 0);
Harald Welte702d8702008-12-26 20:25:35 +0000581
582 /* Reset BTS Site manager resource */
Harald Welte78374892009-01-18 19:09:22 +0000583 abis_nm_bs11_reset_resource(bts);
Harald Welte702d8702008-12-26 20:25:35 +0000584
585 /* begin DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000586 abis_nm_bs11_db_transmission(bts, 1);
Harald Welte702d8702008-12-26 20:25:35 +0000587
Harald Welte349aba62009-08-10 12:31:31 +0200588 /* reconfigure BTS with all TRX and all TS */
589 nm_reconfig_bts(bts);
Harald Welte67b4c302009-07-29 16:42:16 +0200590
Harald Welte52b1f982008-12-23 20:25:15 +0000591 /* end DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000592 abis_nm_bs11_db_transmission(bts, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000593
594 /* Reset BTS Site manager resource */
Harald Welte78374892009-01-18 19:09:22 +0000595 abis_nm_bs11_reset_resource(bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000596
597 /* restart sending event reports */
598 abis_nm_event_reports(bts, 1);
599}
600
Harald Welte8c1d0e42009-02-15 03:38:12 +0000601static void bootstrap_om(struct gsm_bts *bts)
602{
Harald Welteedb37782009-05-01 14:59:07 +0000603 fprintf(stdout, "bootstrapping OML for BTS %u\n", bts->nr);
Harald Welte8c1d0e42009-02-15 03:38:12 +0000604
605 switch (bts->type) {
606 case GSM_BTS_TYPE_BS11:
607 bootstrap_om_bs11(bts);
608 break;
609 case GSM_BTS_TYPE_NANOBTS_900:
610 case GSM_BTS_TYPE_NANOBTS_1800:
611 bootstrap_om_nanobts(bts);
612 break;
613 default:
614 fprintf(stderr, "Unable to bootstrap OML: Unknown BTS type %d\n", bts->type);
615 }
616}
617
Harald Welted1252502009-01-01 01:50:32 +0000618static int shutdown_om(struct gsm_bts *bts)
619{
Harald Weltef294f452009-08-06 17:43:50 +0200620 fprintf(stdout, "shutting down OML for BTS %u\n", bts->nr);
621
Harald Welted1252502009-01-01 01:50:32 +0000622 /* stop sending event reports */
623 abis_nm_event_reports(bts, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000624
Harald Welted1252502009-01-01 01:50:32 +0000625 /* begin DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000626 abis_nm_bs11_db_transmission(bts, 1);
Harald Welted1252502009-01-01 01:50:32 +0000627
628 /* end DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000629 abis_nm_bs11_db_transmission(bts, 0);
Harald Welted1252502009-01-01 01:50:32 +0000630
631 /* Reset BTS Site manager resource */
Harald Welte78374892009-01-18 19:09:22 +0000632 abis_nm_bs11_reset_resource(bts);
Harald Welted1252502009-01-01 01:50:32 +0000633
634 return 0;
635}
636
637static int shutdown_net(struct gsm_network *net)
638{
Harald Weltee441d9c2009-06-21 16:17:15 +0200639 struct gsm_bts *bts;
640
641 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welted1252502009-01-01 01:50:32 +0000642 int rc;
Harald Weltee441d9c2009-06-21 16:17:15 +0200643 rc = shutdown_om(bts);
Harald Welted1252502009-01-01 01:50:32 +0000644 if (rc < 0)
645 return rc;
646 }
647
648 return 0;
649}
Harald Welte52b1f982008-12-23 20:25:15 +0000650
651struct bcch_info {
652 u_int8_t type;
653 u_int8_t len;
654 const u_int8_t *data;
655};
656
657/*
658SYSTEM INFORMATION TYPE 1
659 Cell channel description
660 Format-ID bit map 0
661 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
662 RACH Control Parameters
663 maximum 7 retransmissions
664 8 slots used to spread transmission
665 cell not barred for access
666 call reestablishment not allowed
667 Access Control Class = 0000
668*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000669static u_int8_t si1[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000670 /* header */0x55, 0x06, 0x19,
671 /* ccdesc */0x04 /*0x00*/, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
672 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /*0x01*/,
673 /* rach */0xD5, 0x00, 0x00,
674 /* s1 reset*/0x2B
Harald Welte52b1f982008-12-23 20:25:15 +0000675};
676
677/*
678 SYSTEM INFORMATION TYPE 2
679 Neighbour Cells Description
680 EXT-IND: Carries the complete BA
681 BA-IND = 0
682 Format-ID bit map 0
683 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
684 NCC permitted (NCC) = FF
685 RACH Control Parameters
686 maximum 7 retransmissions
687 8 slots used to spread transmission
688 cell not barred for access
689 call reestablishment not allowed
690 Access Control Class = 0000
691*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000692static u_int8_t si2[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000693 /* header */0x59, 0x06, 0x1A,
694 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
695 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
696 /* ncc */0xFF,
697 /* rach*/0xD5, 0x00, 0x00
Harald Welte52b1f982008-12-23 20:25:15 +0000698};
699
700/*
701SYSTEM INFORMATION TYPE 3
702 Cell identity = 00001 (1h)
703 Location area identification
704 Mobile Country Code (MCC): 001
705 Mobile Network Code (MNC): 01
706 Location Area Code (LAC): 00001 (1h)
707 Control Channel Description
708 Attach-detach: MSs in the cell are not allowed to apply IMSI attach /detach
709 0 blocks reserved for access grant
710 1 channel used for CCCH, with SDCCH
711 5 multiframes period for PAGING REQUEST
712 Time-out T3212 = 0
713 Cell Options BCCH
714 Power control indicator: not set
715 MSs shall not use uplink DTX
716 Radio link timeout = 36
717 Cell Selection Parameters
718 Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
Harald Welte3b2ec422008-12-29 04:11:14 +0000719 max.TX power level MS may use for CCH = 2 <- according to GSM05.05 39dBm (max)
Harald Welte52b1f982008-12-23 20:25:15 +0000720 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
721 Half rate support (NECI): New establishment causes are not supported
722 min.RX signal level for MS = 0
723 RACH Control Parameters
724 maximum 7 retransmissions
725 8 slots used to spread transmission
726 cell not barred for access
727 call reestablishment not allowed
728 Access Control Class = 0000
Harald Welte53833f62009-07-05 13:41:40 +0200729 SI 3 Rest Octets (not present)
Harald Welte52b1f982008-12-23 20:25:15 +0000730*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000731static u_int8_t si3[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000732 /* header */0x49, 0x06, 0x1B,
733 /* cell */0x00, 0x01,
734 /* lai */0x00, 0xF1, 0x10, 0x00, 0x01,
735 /* desc */0x01, 0x03, 0x00,
736 /* option*/0x28,
737 /* selection*/0x62, 0x00,
738 /* rach */0xD5, 0x00, 0x00,
Harald Welte53833f62009-07-05 13:41:40 +0200739 /* rest */ 0x2B, 0x2B, 0x2B, 0x2B
Harald Welte52b1f982008-12-23 20:25:15 +0000740};
741
742/*
743SYSTEM INFORMATION TYPE 4
744 Location area identification
745 Mobile Country Code (MCC): 001
746 Mobile Network Code (MNC): 01
747 Location Area Code (LAC): 00001 (1h)
748 Cell Selection Parameters
749 Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
750 max.TX power level MS may use for CCH = 2
751 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
752 Half rate support (NECI): New establishment causes are not supported
753 min.RX signal level for MS = 0
754 RACH Control Parameters
755 maximum 7 retransmissions
756 8 slots used to spread transmission
757 cell not barred for access
758 call reestablishment not allowed
759 Access Control Class = 0000
Harald Welte53833f62009-07-05 13:41:40 +0200760 CBCH Channel Description
Harald Welte52b1f982008-12-23 20:25:15 +0000761 Type = SDCCH/4[2]
762 Timeslot Number: 0
763 Training Sequence Code: 7h
764 ARFCN: 1
Harald Welte53833f62009-07-05 13:41:40 +0200765 SI Rest Octets (not present)
Harald Welte52b1f982008-12-23 20:25:15 +0000766*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000767static u_int8_t si4[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000768 /* header */0x41, 0x06, 0x1C,
769 /* lai */0x00, 0xF1, 0x10, 0x00, 0x01,
770 /* sel */0x62, 0x00,
771 /* rach*/0xD5, 0x00, 0x00,
Harald Welte53833f62009-07-05 13:41:40 +0200772 /* cbch chan desc */ 0x64, 0x30, 0xE0, HARDCODED_ARFCN/*0x01*/,
773 /* rest octets */ 0x2B, 0x2B, 0x2B, 0x2B, 0x2B, 0x2B
Harald Welte52b1f982008-12-23 20:25:15 +0000774};
775
776/*
777 SYSTEM INFORMATION TYPE 5
778 Neighbour Cells Description
779 EXT-IND: Carries the complete BA
780 BA-IND = 0
781 Format-ID bit map 0
782 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
783*/
784
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000785static u_int8_t si5[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000786 /* header without l2 len*/0x06, 0x1D,
787 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
788 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Harald Welte52b1f982008-12-23 20:25:15 +0000789};
790
791// SYSTEM INFORMATION TYPE 6
792
793/*
794SACCH FILLING
795 System Info Type: SYSTEM INFORMATION 6
796 L3 Information (Hex): 06 1E 00 01 xx xx 10 00 01 28 FF
797
798SYSTEM INFORMATION TYPE 6
799 Cell identity = 00001 (1h)
800 Location area identification
801 Mobile Country Code (MCC): 001
802 Mobile Network Code (MNC): 01
803 Location Area Code (LAC): 00001 (1h)
804 Cell Options SACCH
805 Power control indicator: not set
806 MSs shall not use uplink DTX on a TCH-F. MS shall not use uplink DTX on TCH-H.
807 Radio link timeout = 36
808 NCC permitted (NCC) = FF
809*/
810
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000811static u_int8_t si6[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000812 /* header */0x06, 0x1E,
813 /* cell id*/ 0x00, 0x01,
814 /* lai */ 0x00, 0xF1, 0x10, 0x00, 0x01,
815 /* options */ 0x28,
816 /* ncc */ 0xFF,
Harald Welte52b1f982008-12-23 20:25:15 +0000817};
818
819
820
821static const struct bcch_info bcch_infos[] = {
822 {
823 .type = RSL_SYSTEM_INFO_1,
824 .len = sizeof(si1),
825 .data = si1,
826 }, {
827 .type = RSL_SYSTEM_INFO_2,
828 .len = sizeof(si2),
829 .data = si2,
830 }, {
831 .type = RSL_SYSTEM_INFO_3,
832 .len = sizeof(si3),
833 .data = si3,
834 }, {
835 .type = RSL_SYSTEM_INFO_4,
836 .len = sizeof(si4),
837 .data = si4,
838 },
839};
840
Holger Freyther24287b62008-12-28 16:32:41 +0000841static_assert(sizeof(si1) == sizeof(struct gsm48_system_information_type_1), type1)
842static_assert(sizeof(si2) == sizeof(struct gsm48_system_information_type_2), type2)
843static_assert(sizeof(si3) == sizeof(struct gsm48_system_information_type_3), type3)
844static_assert(sizeof(si4) >= sizeof(struct gsm48_system_information_type_4), type4)
Harald Welte104604e2008-12-28 16:36:11 +0000845static_assert(sizeof(si5) == sizeof(struct gsm48_system_information_type_5), type5)
846static_assert(sizeof(si6) >= sizeof(struct gsm48_system_information_type_6), type6)
Holger Freyther24287b62008-12-28 16:32:41 +0000847
Harald Welte52b1f982008-12-23 20:25:15 +0000848/* set all system information types */
Harald Weltee79769b2009-02-07 00:48:17 +0000849static int set_system_infos(struct gsm_bts_trx *trx)
Harald Welte52b1f982008-12-23 20:25:15 +0000850{
851 int i;
852
Harald Welted1586052009-08-06 17:41:19 +0200853 if (trx == trx->bts->c0) {
854 for (i = 0; i < ARRAY_SIZE(bcch_infos); i++) {
855 rsl_bcch_info(trx, bcch_infos[i].type,
856 bcch_infos[i].data,
857 bcch_infos[i].len);
858 }
Harald Welte52b1f982008-12-23 20:25:15 +0000859 }
Harald Weltee79769b2009-02-07 00:48:17 +0000860 rsl_sacch_filling(trx, RSL_SYSTEM_INFO_5, si5, sizeof(si5));
861 rsl_sacch_filling(trx, RSL_SYSTEM_INFO_6, si6, sizeof(si6));
Harald Weltead384642008-12-26 10:20:07 +0000862
863 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000864}
865
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000866/*
867 * Patch the various SYSTEM INFORMATION tables to update
868 * the LAI
869 */
870static void patch_tables(struct gsm_bts *bts)
871{
Harald Weltee441d9c2009-06-21 16:17:15 +0200872 u_int8_t arfcn_low = bts->c0->arfcn & 0xff;
873 u_int8_t arfcn_high = (bts->c0->arfcn >> 8) & 0x0f;
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000874 /* covert the raw packet to the struct */
875 struct gsm48_system_information_type_3 *type_3 =
876 (struct gsm48_system_information_type_3*)&si3;
877 struct gsm48_system_information_type_4 *type_4 =
878 (struct gsm48_system_information_type_4*)&si4;
879 struct gsm48_system_information_type_6 *type_6 =
880 (struct gsm48_system_information_type_6*)&si6;
Harald Welteb84e2f42008-12-28 23:42:04 +0000881 struct gsm48_loc_area_id lai;
882
883 gsm0408_generate_lai(&lai, bts->network->country_code,
Harald Welte110c0ab2009-05-23 16:27:05 +0000884 bts->network->network_code,
885 bts->location_area_code);
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000886
887 /* assign the MCC and MNC */
Harald Welteb84e2f42008-12-28 23:42:04 +0000888 type_3->lai = lai;
889 type_4->lai = lai;
890 type_6->lai = lai;
Harald Welte98981882009-01-06 18:59:11 +0000891
Harald Welte94009322009-02-15 15:38:42 +0000892 /* patch ARFCN into BTS Attributes */
Harald Weltea865f1b2009-06-20 10:42:17 +0200893 bs11_attr_bts[69] &= 0xf0;
894 bs11_attr_bts[69] |= arfcn_high;
895 bs11_attr_bts[70] = arfcn_low;
Harald Welte94009322009-02-15 15:38:42 +0000896 nanobts_attr_bts[42] &= 0xf0;
897 nanobts_attr_bts[42] |= arfcn_high;
898 nanobts_attr_bts[43] = arfcn_low;
Harald Welte98981882009-01-06 18:59:11 +0000899
Harald Welte94009322009-02-15 15:38:42 +0000900 /* patch ARFCN into TRX Attributes */
Harald Weltea865f1b2009-06-20 10:42:17 +0200901 bs11_attr_radio[2] &= 0xf0;
902 bs11_attr_radio[2] |= arfcn_high;
903 bs11_attr_radio[3] = arfcn_low;
Harald Welte94009322009-02-15 15:38:42 +0000904 nanobts_attr_radio[5] &= 0xf0;
905 nanobts_attr_radio[5] |= arfcn_high;
906 nanobts_attr_radio[6] = arfcn_low;
Harald Welte98981882009-01-06 18:59:11 +0000907
908 type_4->data[2] &= 0xf0;
909 type_4->data[2] |= arfcn_high;
910 type_4->data[3] = arfcn_low;
Holger Freyther1adb4ff2009-02-04 00:04:52 +0000911
912 /* patch Control Channel Description 10.5.2.11 */
913 type_3->control_channel_desc = bts->chan_desc;
Harald Welte78f2f502009-05-23 16:56:52 +0000914
915 /* patch BSIC */
Harald Weltea865f1b2009-06-20 10:42:17 +0200916 bs11_attr_bts[1] = bts->bsic;
Harald Welte78f2f502009-05-23 16:56:52 +0000917 nanobts_attr_bts[sizeof(nanobts_attr_bts)-1] = bts->bsic;
Harald Weltef8d536d2009-07-21 22:12:23 +0200918
919 /* patch TSC */
920 si4[15] &= ~0xe0;
921 si4[15] |= (bts->tsc & 7) << 5;
Harald Welte31f03a62009-08-09 14:38:49 +0200922
923 /* patch MS max power for CCH */
924 type_4->cell_sel_par.ms_txpwr_max_ccch =
Harald Welte66b6a8d2009-08-09 14:45:18 +0200925 ms_pwr_ctl_lvl(bts->band, 20 /* dBm == 100mW */);
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000926}
927
928
Harald Weltee79769b2009-02-07 00:48:17 +0000929static void bootstrap_rsl(struct gsm_bts_trx *trx)
Harald Welte52b1f982008-12-23 20:25:15 +0000930{
Harald Welteedb37782009-05-01 14:59:07 +0000931 fprintf(stdout, "bootstrapping RSL for BTS/TRX (%u/%u) "
Harald Weltef8d536d2009-07-21 22:12:23 +0200932 "using MCC=%u MNC=%u BSIC=%u TSC=%u\n",
Harald Welte42581822009-08-08 16:12:58 +0200933 trx->bts->nr, trx->nr, gsmnet->country_code,
934 gsmnet->network_code, trx->bts->bsic, trx->bts->tsc);
Harald Weltee79769b2009-02-07 00:48:17 +0000935 set_system_infos(trx);
Harald Welte52b1f982008-12-23 20:25:15 +0000936}
937
Harald Welte1fa60c82009-02-09 18:13:26 +0000938void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
Harald Weltead384642008-12-26 10:20:07 +0000939{
940 switch (event) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000941 case EVT_E1_TEI_UP:
942 switch (type) {
943 case E1INP_SIGN_OML:
944 bootstrap_om(trx->bts);
945 break;
946 case E1INP_SIGN_RSL:
947 bootstrap_rsl(trx);
948 break;
949 default:
950 break;
951 }
Harald Weltead384642008-12-26 10:20:07 +0000952 break;
Harald Welte1fa60c82009-02-09 18:13:26 +0000953 case EVT_E1_TEI_DN:
954 fprintf(stderr, "Lost some E1 TEI link\n");
955 /* FIXME: deal with TEI or L1 link loss */
Harald Weltead384642008-12-26 10:20:07 +0000956 break;
957 default:
Harald Weltead384642008-12-26 10:20:07 +0000958 break;
959 }
960}
961
Harald Welteedb37782009-05-01 14:59:07 +0000962static int bootstrap_bts(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000963{
Harald Welte42581822009-08-08 16:12:58 +0200964 switch (bts->type) {
965 case GSM_BTS_TYPE_NANOBTS_1800:
966 if (bts->c0->arfcn < 512 || bts->c0->arfcn > 885) {
967 fprintf(stderr, "GSM1800 channel must be between 512-885.\n");
968 return -EINVAL;
969 }
970 break;
971 case GSM_BTS_TYPE_BS11:
972 case GSM_BTS_TYPE_NANOBTS_900:
973 /* Assume we have a P-GSM900 here */
974 if (bts->c0->arfcn < 1 || bts->c0->arfcn > 124) {
975 fprintf(stderr, "GSM900 channel must be between 1-124.\n");
976 return -EINVAL;
977 }
978 break;
979 case GSM_BTS_TYPE_UNKNOWN:
980 fprintf(stderr, "Unknown BTS. Please specify\n");
981 return -EINVAL;
982 }
Holger Freyther1adb4ff2009-02-04 00:04:52 +0000983
984 /* Control Channel Description */
985 memset(&bts->chan_desc, 0, sizeof(struct gsm48_control_channel_descr));
Harald Welte41fbf442009-02-24 22:34:22 +0000986 bts->chan_desc.att = 1;
Holger Freyther1adb4ff2009-02-04 00:04:52 +0000987 bts->chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_C;
988 bts->chan_desc.bs_pa_mfrms = RSL_BS_PA_MFRMS_5;
989 bts->chan_desc.t3212 = 0;
990
Harald Welte98981882009-01-06 18:59:11 +0000991 patch_tables(bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000992
Holger Freyther1fd34142009-02-09 23:42:03 +0000993 paging_init(bts);
Harald Welte38c2f132009-01-06 23:10:57 +0000994
Harald Welteedb37782009-05-01 14:59:07 +0000995 return 0;
996}
997
998static int bootstrap_network(void)
999{
Harald Welte42581822009-08-08 16:12:58 +02001000 struct gsm_bts *bts;
Harald Welte51460062009-08-06 17:54:21 +02001001 int rc;
1002
Harald Welteedb37782009-05-01 14:59:07 +00001003 /* initialize our data structures */
Harald Weltee441d9c2009-06-21 16:17:15 +02001004 gsmnet = gsm_network_init(MCC, MNC, mncc_recv);
Harald Welteedb37782009-05-01 14:59:07 +00001005 if (!gsmnet)
1006 return -ENOMEM;
1007
Harald Welte42581822009-08-08 16:12:58 +02001008 gsmnet->name_long = talloc_strdup(gsmnet, "OpenBSC");
1009 gsmnet->name_short = talloc_strdup(gsmnet, "OpenBSC");
Harald Welteedb37782009-05-01 14:59:07 +00001010
Holger Freytherc7b86f92009-06-06 13:54:20 +00001011 if (db_init(database_name)) {
Holger Freytheref7f7ce2009-04-19 06:35:12 +00001012 printf("DB: Failed to init database. Please check the option settings.\n");
1013 return -1;
1014 }
1015 printf("DB: Database initialized.\n");
1016
1017 if (db_prepare()) {
1018 printf("DB: Failed to prepare database.\n");
1019 return -1;
1020 }
1021 printf("DB: Database prepared.\n");
1022
Holger Freyther219518d2009-01-02 22:04:43 +00001023 telnet_init(gsmnet, 4242);
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +02001024 rc = vty_read_config_file(config_file);
Holger Hans Peter Freyther100325a2009-08-10 10:35:24 +02001025 if (rc < 0) {
1026 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
Harald Welte42581822009-08-08 16:12:58 +02001027 return rc;
Holger Hans Peter Freyther100325a2009-08-10 10:35:24 +02001028 }
Harald Weltead384642008-12-26 10:20:07 +00001029
Harald Welteb4630602009-05-01 15:43:22 +00001030 register_signal_handler(SS_NM, nm_sig_cb, NULL);
1031
Harald Welte42581822009-08-08 16:12:58 +02001032 llist_for_each_entry(bts, &gsmnet->bts_list, list) {
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +02001033 bootstrap_bts(bts);
Harald Welte42581822009-08-08 16:12:58 +02001034 if (is_ipaccess_bts(bts))
1035 rc = ipaccess_setup(bts);
1036 else
1037 rc = e1_reconfig_bts(bts);
Holger Hans Peter Freyther03582a82009-07-16 15:24:27 +02001038
Harald Welte42581822009-08-08 16:12:58 +02001039 if (rc < 0)
1040 exit (1);
Harald Welteedb37782009-05-01 14:59:07 +00001041 }
Harald Welte42581822009-08-08 16:12:58 +02001042
1043 return 0;
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");
Harald Welte42581822009-08-08 16:12:58 +02001067 printf(" -h --help this text\n");
Holger Freytherb332f612008-12-27 12:46:51 +00001068 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Holger Freytherefde7fb2008-12-28 14:14:56 +00001069 printf(" -s --disable-color\n");
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +02001070 printf(" -c --config-file filename The config file to use.\n");
Holger Freytherbde36102008-12-28 22:51:39 +00001071 printf(" -l --database db-name The database to use\n");
Holger Freyther89824fc2008-12-30 16:18:18 +00001072 printf(" -a --authorize-everyone Allow everyone into the network.\n");
Holger Freythere97f7fb2008-12-31 18:52:11 +00001073 printf(" -r --reject-cause number The reject cause for LOCATION UPDATING REJECT.\n");
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00001074 printf(" -p --pcap file The filename of the pcap file\n");
Holger Freytherdda22c12009-04-22 22:07:31 +00001075 printf(" -C --cardnr number For bs11 select E1 card number other than 0\n");
Holger Freytherb5c00f52009-04-22 22:08:07 +00001076 printf(" -R --release-l2 Releases mISDN layer 2 after exit, to unload driver.\n");
Holger Freytherb332f612008-12-27 12:46:51 +00001077}
1078
1079static void handle_options(int argc, char** argv)
1080{
1081 while (1) {
Harald Welte2cf161b2009-06-20 22:36:41 +02001082 int option_index = 0, c;
Holger Freytherb332f612008-12-27 12:46:51 +00001083 static struct option long_options[] = {
1084 {"help", 0, 0, 'h'},
1085 {"debug", 1, 0, 'd'},
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +02001086 {"config-file", 1, 0, 'c'},
Holger Freytherefde7fb2008-12-28 14:14:56 +00001087 {"disable-color", 0, 0, 's'},
Holger Freytherbde36102008-12-28 22:51:39 +00001088 {"database", 1, 0, 'l'},
Holger Freyther89824fc2008-12-30 16:18:18 +00001089 {"authorize-everyone", 0, 0, 'a'},
Holger Freythere97f7fb2008-12-31 18:52:11 +00001090 {"reject-cause", 1, 0, 'r'},
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00001091 {"pcap", 1, 0, 'p'},
Holger Freytherdda22c12009-04-22 22:07:31 +00001092 {"cardnr", 1, 0, 'C'},
Holger Freytherb5c00f52009-04-22 22:08:07 +00001093 {"release-l2", 0, 0, 'R'},
Harald Welted3ff51d2009-06-09 20:21:57 +00001094 {"timestamp", 0, 0, 'T'},
Harald Welte805f6442009-07-28 18:25:29 +02001095 {"rtp-proxy", 0, 0, 'P'},
Holger Freytherb332f612008-12-27 12:46:51 +00001096 {0, 0, 0, 0}
1097 };
1098
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +02001099 c = getopt_long(argc, argv, "hd:sl:ar:p:C:RTPc:",
Holger Freytherb332f612008-12-27 12:46:51 +00001100 long_options, &option_index);
1101 if (c == -1)
1102 break;
1103
1104 switch (c) {
1105 case 'h':
1106 print_usage();
1107 print_help();
1108 exit(0);
Holger Freytherefde7fb2008-12-28 14:14:56 +00001109 case 's':
Holger Freytherb332f612008-12-27 12:46:51 +00001110 debug_use_color(0);
1111 break;
1112 case 'd':
1113 debug_parse_category_mask(optarg);
1114 break;
Harald Welte8965da42009-01-06 18:09:02 +00001115 case 'l':
Holger Freytherbde36102008-12-28 22:51:39 +00001116 database_name = strdup(optarg);
1117 break;
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +02001118 case 'c':
1119 config_file = strdup(optarg);
1120 break;
Holger Freyther89824fc2008-12-30 16:18:18 +00001121 case 'a':
1122 gsm0408_allow_everyone(1);
1123 break;
Holger Freythere97f7fb2008-12-31 18:52:11 +00001124 case 'r':
1125 gsm0408_set_reject_cause(atoi(optarg));
1126 break;
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00001127 case 'p':
1128 create_pcap_file(optarg);
1129 break;
Harald Welte8c1d0e42009-02-15 03:38:12 +00001130 case 't':
1131 BTS_TYPE = parse_btstype(optarg);
1132 break;
Holger Freytherdda22c12009-04-22 22:07:31 +00001133 case 'C':
1134 cardnr = atoi(optarg);
1135 break;
Holger Freytherb5c00f52009-04-22 22:08:07 +00001136 case 'R':
1137 release_l2 = 1;
1138 break;
Harald Welted3ff51d2009-06-09 20:21:57 +00001139 case 'T':
1140 debug_timestamp(1);
1141 break;
Harald Welte805f6442009-07-28 18:25:29 +02001142 case 'P':
1143 ipacc_rtp_direct = 0;
1144 break;
Holger Freytherb332f612008-12-27 12:46:51 +00001145 default:
1146 /* ignore */
1147 break;
1148 }
1149 }
1150}
1151
Harald Welted1252502009-01-01 01:50:32 +00001152static void signal_handler(int signal)
1153{
1154 fprintf(stdout, "signal %u received\n", signal);
1155
1156 switch (signal) {
Harald Weltef294f452009-08-06 17:43:50 +02001157 case SIGINT:
Harald Welted1252502009-01-01 01:50:32 +00001158 shutdown_net(gsmnet);
Harald Weltef294f452009-08-06 17:43:50 +02001159 sleep(3);
1160 exit(0);
Harald Welted1252502009-01-01 01:50:32 +00001161 break;
Harald Welte31c3d342009-08-07 00:29:44 +02001162 case SIGABRT:
1163 /* in case of abort, we want to obtain a talloc report
1164 * and then return to the caller, who will abort the process */
Harald Welte2cf161b2009-06-20 22:36:41 +02001165 case SIGUSR1:
1166 talloc_report_full(tall_bsc_ctx, stderr);
1167 break;
Harald Welted1252502009-01-01 01:50:32 +00001168 default:
1169 break;
1170 }
1171}
1172
Harald Weltef6b7a902008-12-26 00:05:11 +00001173int main(int argc, char **argv)
1174{
Harald Welte1fa60c82009-02-09 18:13:26 +00001175 int rc;
1176
Harald Welte2cf161b2009-06-20 22:36:41 +02001177 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
1178
Holger Freytherb332f612008-12-27 12:46:51 +00001179 /* parse options */
1180 handle_options(argc, argv);
1181
Harald Welte65ccf882009-02-24 22:36:20 +00001182 /* seed the PRNG */
1183 srand(time(NULL));
1184
Harald Welte1fa60c82009-02-09 18:13:26 +00001185 rc = bootstrap_network();
1186 if (rc < 0)
1187 exit(1);
Harald Weltef6b7a902008-12-26 00:05:11 +00001188
Harald Weltef294f452009-08-06 17:43:50 +02001189 signal(SIGINT, &signal_handler);
Harald Welted1252502009-01-01 01:50:32 +00001190 signal(SIGABRT, &signal_handler);
Harald Welte2cf161b2009-06-20 22:36:41 +02001191 signal(SIGUSR1, &signal_handler);
Harald Welted1252502009-01-01 01:50:32 +00001192
Harald Weltef6b7a902008-12-26 00:05:11 +00001193 while (1) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001194 bsc_upqueue(gsmnet);
Harald Welte04d3c922009-05-23 06:07:04 +00001195 bsc_select_main(0);
Harald Weltef6b7a902008-12-26 00:05:11 +00001196 }
1197}