blob: cadf41cf6ebe222c23faa1a9fcb6bbacf9818a80 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* A hackish minimal BSC (+MSC +HLR) implementation */
2
3/* (C) 2008 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 Welte52b1f982008-12-23 20:25:15 +000048
49/* global pointer to the gsm network data structure */
50static struct gsm_network *gsmnet;
51
Holger Freytherefde7fb2008-12-28 14:14:56 +000052/* MCC and MNC for the Location Area Identifier */
53static int MCC = 1;
54static int MNC = 1;
Harald Welte98981882009-01-06 18:59:11 +000055static int ARFCN = HARDCODED_ARFCN;
Holger Freytherbde36102008-12-28 22:51:39 +000056static const char *database_name = "hlr.sqlite3";
Holger Freytherefde7fb2008-12-28 14:14:56 +000057
Harald Welte52b1f982008-12-23 20:25:15 +000058/* The following definitions are for OM and NM packets that we cannot yet
59 * generate by code but we just pass on */
60
61// BTS Site Manager, SET ATTRIBUTES
62
63/*
64 Object Class: BTS Site Manager
65 Instance 1: FF
66 Instance 2: FF
67 Instance 3: FF
68SET ATTRIBUTES
69 sAbisExternalTime: 2007/09/08 14:36:11
70 omLAPDRelTimer: 30sec
71 shortLAPDIntTimer: 5sec
72 emergencyTimer1: 10 minutes
73 emergencyTimer2: 0 minutes
74*/
75
76unsigned char msg_1[] =
77{
78 0xD0, 0x00, 0xFF, 0xFF, 0xFF, 0x91, 0x07, 0xD7, 0x09, 0x08, 0x0E, 0x24,
79 0x0B, 0xCE, 0x02, 0x00, 0x1E, 0xE8, 0x01, 0x05, 0x42, 0x02, 0x00, 0x0A, 0x44,
80 0x02, 0x00, 0x00
81};
82
83// BTS, SET BTS ATTRIBUTES
84
85/*
86 Object Class: BTS
87 BTS relat. Number: 0
88 Instance 2: FF
89 Instance 3: FF
90SET BTS ATTRIBUTES
91 bsIdentityCode / BSIC:
92 PLMN_colour_code: 7h
93 BS_colour_code: 7h
94 BTS Air Timer T3105: 4 ,unit 10 ms
95 btsIsHopping: FALSE
Harald Welte83282292009-02-01 16:22:19 +000096 periodCCCHLoadIndication: 1sec
Harald Welte0bbb8b22009-02-06 12:49:11 +000097 thresholdCCCHLoadIndication: 50%
Harald Welte52b1f982008-12-23 20:25:15 +000098 cellAllocationNumber: 00h = GSM 900
99 enableInterferenceClass: 00h = Disabled
100 fACCHQual: 6 (FACCH stealing flags minus 1)
101 intaveParameter: 31 SACCH multiframes
102 interferenceLevelBoundaries:
103 Interference Boundary 1: 0Ah
104 Interference Boundary 2: 0Fh
105 Interference Boundary 3: 14h
106 Interference Boundary 4: 19h
107 Interference Boundary 5: 1Eh
108 mSTxPwrMax: 11
109 GSM range: 2=39dBm, 15=13dBm, stepsize 2 dBm
110 DCS1800 range: 0=30dBm, 15=0dBm, stepsize 2 dBm
111 PCS1900 range: 0=30dBm, 15=0dBm, stepsize 2 dBm
112 30=33dBm, 31=32dBm
113 ny1:
114 Maximum number of repetitions for PHYSICAL INFORMATION message (GSM 04.08): 20
115 powerOutputThresholds:
116 Out Power Fault Threshold: -10 dB
117 Red Out Power Threshold: - 6 dB
118 Excessive Out Power Threshold: 5 dB
119 rACHBusyThreshold: -127 dBm
120 rACHLoadAveragingSlots: 250 ,number of RACH burst periods
121 rfResourceIndicationPeriod: 125 SACCH multiframes
122 T200:
123 SDCCH: 044 in 5 ms
124 FACCH/Full rate: 031 in 5 ms
125 FACCH/Half rate: 041 in 5 ms
126 SACCH with TCH SAPI0: 090 in 10 ms
127 SACCH with SDCCH: 090 in 10 ms
128 SDCCH with SAPI3: 090 in 5 ms
129 SACCH with TCH SAPI3: 135 in 10 ms
130 tSync: 9000 units of 10 msec
131 tTrau: 9000 units of 10 msec
132 enableUmLoopTest: 00h = disabled
133 enableExcessiveDistance: 00h = Disabled
134 excessiveDistance: 64km
135 hoppingMode: 00h = baseband hopping
136 cellType: 00h = Standard Cell
137 BCCH ARFCN / bCCHFrequency: 1
138*/
139
140unsigned char msg_2[] =
141{
142 0x41, 0x01, 0x00, 0xFF, 0xFF, 0x09, 0x3F, 0x0A, 0x04, 0x61, 0x00, 0x0B,
Harald Welte0bbb8b22009-02-06 12:49:11 +0000143 0x01, 0x0C, 0x32, 0x62, 0x00, 0x66, 0x00, 0x6E, 0x06, 0x18, 0x1F, 0x19,
Harald Welte52b1f982008-12-23 20:25:15 +0000144 0x0A, 0x0F, 0x14, 0x19, 0x1E, 0x7B, 0x0B, 0x23, 0x14, 0x28, 0x00, 0x04,
145 0x03, 0x2A, 0x7F, 0x2B, 0x00, 0xFA, 0x8F, 0x7D, 0x33, 0x2C, 0x1F, 0x29,
146 0x5A, 0x5A, 0x5A, 0x87, 0x94, 0x23, 0x28, 0x95, 0x23, 0x28, 0x35, 0x01,
147 0x00, 0x46, 0x01, 0x00, 0x58, 0x01, 0x40, 0xC5, 0x01, 0x00, 0xF2, 0x01,
148 0x00, 0x08, 0x00, HARDCODED_ARFCN/*0x01*/,
149};
150
151// Handover Recognition, SET ATTRIBUTES
152
153/*
154Illegal Contents GSM Formatted O&M Msg
155 Object Class: Handover Recognition
156 BTS relat. Number: 0
157 Instance 2: FF
158 Instance 3: FF
159SET ATTRIBUTES
160 enableDelayPowerBudgetHO: 00h = Disabled
161 enableDistanceHO: 00h = Disabled
162 enableInternalInterCellHandover: 00h = Disabled
163 enableInternalIntraCellHandover: 00h = Disabled
164 enablePowerBudgetHO: 00h = Disabled
165 enableRXLEVHO: 00h = Disabled
166 enableRXQUALHO: 00h = Disabled
167 hoAveragingDistance: 8 SACCH multiframes
168 hoAveragingLev:
169 A_LEV_HO: 8 SACCH multiframes
170 W_LEV_HO: 1 SACCH multiframes
171 hoAveragingPowerBudget: 16 SACCH multiframes
172 hoAveragingQual:
173 A_QUAL_HO: 8 SACCH multiframes
174 W_QUAL_HO: 2 SACCH multiframes
175 hoLowerThresholdLevDL: (10 - 110) dBm
176 hoLowerThresholdLevUL: (5 - 110) dBm
177 hoLowerThresholdQualDL: 06h = 6.4% < BER < 12.8%
178 hoLowerThresholdQualUL: 06h = 6.4% < BER < 12.8%
179 hoThresholdLevDLintra : (20 - 110) dBm
180 hoThresholdLevULintra: (20 - 110) dBm
181 hoThresholdMsRangeMax: 20 km
182 nCell: 06h
183 timerHORequest: 3 ,unit 2 SACCH multiframes
184*/
185
186unsigned char msg_3[] =
187{
188 0xD0, 0xA1, 0x00, 0xFF, 0xFF, 0xD0, 0x00, 0x64, 0x00, 0x67, 0x00, 0x68,
189 0x00, 0x6A, 0x00, 0x6C, 0x00, 0x6D, 0x00, 0x6F, 0x08, 0x70, 0x08, 0x01,
190 0x71, 0x10, 0x10, 0x10, 0x72, 0x08, 0x02, 0x73, 0x0A, 0x74, 0x05, 0x75,
191 0x06, 0x76, 0x06, 0x78, 0x14, 0x79, 0x14, 0x7A, 0x14, 0x7D, 0x06, 0x92,
192 0x03, 0x20, 0x01, 0x00, 0x45, 0x01, 0x00, 0x48, 0x01, 0x00, 0x5A, 0x01,
193 0x00, 0x5B, 0x01, 0x05, 0x5E, 0x01, 0x1A, 0x5F, 0x01, 0x20, 0x9D, 0x01,
194 0x00, 0x47, 0x01, 0x00, 0x5C, 0x01, 0x64, 0x5D, 0x01, 0x1E, 0x97, 0x01,
195 0x20, 0xF7, 0x01, 0x3C,
196};
197
198// Power Control, SET ATTRIBUTES
199
200/*
201 Object Class: Power Control
202 BTS relat. Number: 0
203 Instance 2: FF
204 Instance 3: FF
205SET ATTRIBUTES
206 enableMsPowerControl: 00h = Disabled
207 enablePowerControlRLFW: 00h = Disabled
208 pcAveragingLev:
209 A_LEV_PC: 4 SACCH multiframes
210 W_LEV_PC: 1 SACCH multiframes
211 pcAveragingQual:
212 A_QUAL_PC: 4 SACCH multiframes
213 W_QUAL_PC: 2 SACCH multiframes
214 pcLowerThresholdLevDL: 0Fh
215 pcLowerThresholdLevUL: 0Ah
216 pcLowerThresholdQualDL: 05h = 3.2% < BER < 6.4%
217 pcLowerThresholdQualUL: 05h = 3.2% < BER < 6.4%
218 pcRLFThreshold: 0Ch
219 pcUpperThresholdLevDL: 14h
220 pcUpperThresholdLevUL: 0Fh
221 pcUpperThresholdQualDL: 04h = 1.6% < BER < 3.2%
222 pcUpperThresholdQualUL: 04h = 1.6% < BER < 3.2%
223 powerConfirm: 2 ,unit 2 SACCH multiframes
224 powerControlInterval: 2 ,unit 2 SACCH multiframes
225 powerIncrStepSize: 02h = 4 dB
226 powerRedStepSize: 01h = 2 dB
227 radioLinkTimeoutBs: 64 SACCH multiframes
228 enableBSPowerControl: 00h = disabled
229*/
230
231unsigned char msg_4[] =
232{
233 0xD0, 0xA2, 0x00, 0xFF, 0xFF, 0x69, 0x00, 0x6B, 0x00, 0x7E, 0x04, 0x01,
234 0x7F, 0x04, 0x02, 0x80, 0x0F, 0x81, 0x0A, 0x82, 0x05, 0x83, 0x05, 0x84,
235 0x0C, 0x85, 0x14, 0x86, 0x0F, 0x87, 0x04, 0x88, 0x04, 0x89, 0x02, 0x8A,
236 0x02, 0x8B, 0x02, 0x8C, 0x01, 0x8D, 0x40, 0x65, 0x01, 0x00 // set to 0x01 to enable BSPowerControl
237};
238
239
240// Transceiver, SET TRX ATTRIBUTES (TRX 0)
241
242/*
243 Object Class: Transceiver
244 BTS relat. Number: 0
245 Tranceiver number: 0
246 Instance 3: FF
247SET TRX ATTRIBUTES
248 aRFCNList (HEX): 0001
249 txPwrMaxReduction: 00h = 0dB
250 radioMeasGran: 254 SACCH multiframes
251 radioMeasRep: 01h = enabled
252 memberOfEmergencyConfig: 01h = TRUE
253 trxArea: 00h = TRX doesn't belong to a concentric cell
254*/
255
256unsigned char msg_6[] =
257{
258 0x44, 0x02, 0x00, 0x00, 0xFF, 0x05, 0x01, 0x00, HARDCODED_ARFCN /*0x01*/, 0x2D,
259 0x00, 0xDC, 0x01, 0xFE, 0xDD, 0x01, 0x01, 0x9B, 0x01, 0x01, 0x9F, 0x01, 0x00,
260};
261
262
263static void bootstrap_om(struct gsm_bts *bts)
264{
265 struct gsm_bts_trx *trx = &bts->trx[0];
266
Harald Weltead384642008-12-26 10:20:07 +0000267 fprintf(stdout, "bootstrapping OML\n");
268
Harald Welte52b1f982008-12-23 20:25:15 +0000269 /* stop sending event reports */
270 abis_nm_event_reports(bts, 0);
271
272 /* begin DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000273 abis_nm_bs11_db_transmission(bts, 1);
Harald Welte52b1f982008-12-23 20:25:15 +0000274
Harald Welte702d8702008-12-26 20:25:35 +0000275 /* end DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000276 abis_nm_bs11_db_transmission(bts, 0);
Harald Welte702d8702008-12-26 20:25:35 +0000277
278 /* Reset BTS Site manager resource */
Harald Welte78374892009-01-18 19:09:22 +0000279 abis_nm_bs11_reset_resource(bts);
Harald Welte702d8702008-12-26 20:25:35 +0000280
281 /* begin DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000282 abis_nm_bs11_db_transmission(bts, 1);
Harald Welte702d8702008-12-26 20:25:35 +0000283
Harald Welte52b1f982008-12-23 20:25:15 +0000284 abis_nm_raw_msg(bts, sizeof(msg_1), msg_1); /* set BTS SiteMgr attr*/
285 abis_nm_raw_msg(bts, sizeof(msg_2), msg_2); /* set BTS attr */
286 abis_nm_raw_msg(bts, sizeof(msg_3), msg_3); /* set BTS handover attr */
287 abis_nm_raw_msg(bts, sizeof(msg_4), msg_4); /* set BTS power control attr */
288
289 /* Connect signalling of bts0/trx0 to e1_0/ts1/64kbps */
290 abis_nm_conn_terr_sign(trx, 0, 1, 0xff);
291 abis_nm_raw_msg(bts, sizeof(msg_6), msg_6); /* SET TRX ATTRIBUTES */
292
293 /* Use TEI 1 for signalling */
294 abis_nm_establish_tei(bts, 0, 0, 1, 0xff, 0x01);
295 abis_nm_set_channel_attr(&trx->ts[0], NM_CHANC_SDCCH_CBCH);
296#if 0
297 /* TRX 1 */
298 abis_nm_conn_terr_sign(&bts->trx[1], 0, 1, 0xff);
299 /* FIXME: TRX ATTRIBUTE */
300 abis_nm_establish_tei(bts, 0, 0, 1, 0xff, 0x02);
301#endif
302
303 /* SET CHANNEL ATTRIBUTE TS1 */
304 abis_nm_set_channel_attr(&trx->ts[1], 0x09);
305 /* Connect traffic of bts0/trx0/ts1 to e1_0/ts2/b */
306 abis_nm_conn_terr_traf(&trx->ts[1], 0, 2, 1);
307
308 /* SET CHANNEL ATTRIBUTE TS2 */
309 abis_nm_set_channel_attr(&trx->ts[2], 0x09);
310 /* Connect traffic of bts0/trx0/ts2 to e1_0/ts2/c */
311 abis_nm_conn_terr_traf(&trx->ts[2], 0, 2, 2);
312
313 /* SET CHANNEL ATTRIBUTE TS3 */
314 abis_nm_set_channel_attr(&trx->ts[3], 0x09);
315 /* Connect traffic of bts0/trx0/ts3 to e1_0/ts2/d */
316 abis_nm_conn_terr_traf(&trx->ts[3], 0, 2, 3);
317
318 /* SET CHANNEL ATTRIBUTE TS4 */
319 abis_nm_set_channel_attr(&trx->ts[4], 0x09);
320 /* Connect traffic of bts0/trx0/ts4 to e1_0/ts3/a */
321 abis_nm_conn_terr_traf(&trx->ts[4], 0, 3, 0);
322
323 /* SET CHANNEL ATTRIBUTE TS5 */
324 abis_nm_set_channel_attr(&trx->ts[5], 0x09);
325 /* Connect traffic of bts0/trx0/ts5 to e1_0/ts3/b */
326 abis_nm_conn_terr_traf(&trx->ts[5], 0, 3, 1);
327
328 /* SET CHANNEL ATTRIBUTE TS6 */
329 abis_nm_set_channel_attr(&trx->ts[6], 0x09);
330 /* Connect traffic of bts0/trx0/ts6 to e1_0/ts3/c */
331 abis_nm_conn_terr_traf(&trx->ts[6], 0, 3, 2);
332
333 /* SET CHANNEL ATTRIBUTE TS7 */
334 abis_nm_set_channel_attr(&trx->ts[7], 0x09);
335 /* Connect traffic of bts0/trx0/ts7 to e1_0/ts3/d */
336 abis_nm_conn_terr_traf(&trx->ts[7], 0, 3, 3);
337
338 /* end DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000339 abis_nm_bs11_db_transmission(bts, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000340
341 /* Reset BTS Site manager resource */
Harald Welte78374892009-01-18 19:09:22 +0000342 abis_nm_bs11_reset_resource(bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000343
344 /* restart sending event reports */
345 abis_nm_event_reports(bts, 1);
346}
347
Harald Welted1252502009-01-01 01:50:32 +0000348static int shutdown_om(struct gsm_bts *bts)
349{
350 /* stop sending event reports */
351 abis_nm_event_reports(bts, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000352
Harald Welted1252502009-01-01 01:50:32 +0000353 /* begin DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000354 abis_nm_bs11_db_transmission(bts, 1);
Harald Welted1252502009-01-01 01:50:32 +0000355
356 /* end DB transmission */
Harald Welte05188ee2009-01-18 11:39:08 +0000357 abis_nm_bs11_db_transmission(bts, 0);
Harald Welted1252502009-01-01 01:50:32 +0000358
359 /* Reset BTS Site manager resource */
Harald Welte78374892009-01-18 19:09:22 +0000360 abis_nm_bs11_reset_resource(bts);
Harald Welted1252502009-01-01 01:50:32 +0000361
362 return 0;
363}
364
365static int shutdown_net(struct gsm_network *net)
366{
367 int i;
368 for (i = 0; i < net->num_bts; i++) {
369 int rc;
370 rc = shutdown_om(&net->bts[i]);
371 if (rc < 0)
372 return rc;
373 }
374
375 return 0;
376}
Harald Welte52b1f982008-12-23 20:25:15 +0000377
378struct bcch_info {
379 u_int8_t type;
380 u_int8_t len;
381 const u_int8_t *data;
382};
383
384/*
385SYSTEM INFORMATION TYPE 1
386 Cell channel description
387 Format-ID bit map 0
388 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
389 RACH Control Parameters
390 maximum 7 retransmissions
391 8 slots used to spread transmission
392 cell not barred for access
393 call reestablishment not allowed
394 Access Control Class = 0000
395*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000396static u_int8_t si1[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000397 /* header */0x55, 0x06, 0x19,
398 /* ccdesc */0x04 /*0x00*/, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
399 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /*0x01*/,
400 /* rach */0xD5, 0x00, 0x00,
401 /* s1 reset*/0x2B
Harald Welte52b1f982008-12-23 20:25:15 +0000402};
403
404/*
405 SYSTEM INFORMATION TYPE 2
406 Neighbour Cells Description
407 EXT-IND: Carries the complete BA
408 BA-IND = 0
409 Format-ID bit map 0
410 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
411 NCC permitted (NCC) = FF
412 RACH Control Parameters
413 maximum 7 retransmissions
414 8 slots used to spread transmission
415 cell not barred for access
416 call reestablishment not allowed
417 Access Control Class = 0000
418*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000419static u_int8_t si2[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000420 /* header */0x59, 0x06, 0x1A,
421 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
422 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
423 /* ncc */0xFF,
424 /* rach*/0xD5, 0x00, 0x00
Harald Welte52b1f982008-12-23 20:25:15 +0000425};
426
427/*
428SYSTEM INFORMATION TYPE 3
429 Cell identity = 00001 (1h)
430 Location area identification
431 Mobile Country Code (MCC): 001
432 Mobile Network Code (MNC): 01
433 Location Area Code (LAC): 00001 (1h)
434 Control Channel Description
435 Attach-detach: MSs in the cell are not allowed to apply IMSI attach /detach
436 0 blocks reserved for access grant
437 1 channel used for CCCH, with SDCCH
438 5 multiframes period for PAGING REQUEST
439 Time-out T3212 = 0
440 Cell Options BCCH
441 Power control indicator: not set
442 MSs shall not use uplink DTX
443 Radio link timeout = 36
444 Cell Selection Parameters
445 Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
Harald Welte3b2ec422008-12-29 04:11:14 +0000446 max.TX power level MS may use for CCH = 2 <- according to GSM05.05 39dBm (max)
Harald Welte52b1f982008-12-23 20:25:15 +0000447 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
448 Half rate support (NECI): New establishment causes are not supported
449 min.RX signal level for MS = 0
450 RACH Control Parameters
451 maximum 7 retransmissions
452 8 slots used to spread transmission
453 cell not barred for access
454 call reestablishment not allowed
455 Access Control Class = 0000
456 SI 3 Rest Octets
457 Cell Bar Qualify (CBQ): 0
458 Cell Reselect Offset = 0 dB
459 Temporary Offset = 0 dB
460 Penalty Time = 20 s
461 System Information 2ter Indicator (2TI): 0 = not available
462 Early Classmark Sending Control (ECSC): 0 = forbidden
463 Scheduling Information is not sent in SYSTEM INFORMATION TYPE 9 on the BCCH
464*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000465static u_int8_t si3[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000466 /* header */0x49, 0x06, 0x1B,
467 /* cell */0x00, 0x01,
468 /* lai */0x00, 0xF1, 0x10, 0x00, 0x01,
469 /* desc */0x01, 0x03, 0x00,
470 /* option*/0x28,
471 /* selection*/0x62, 0x00,
472 /* rach */0xD5, 0x00, 0x00,
473 /* reset*/0x80, 0x00, 0x00, 0x2B
Harald Welte52b1f982008-12-23 20:25:15 +0000474};
475
476/*
477SYSTEM INFORMATION TYPE 4
478 Location area identification
479 Mobile Country Code (MCC): 001
480 Mobile Network Code (MNC): 01
481 Location Area Code (LAC): 00001 (1h)
482 Cell Selection Parameters
483 Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
484 max.TX power level MS may use for CCH = 2
485 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
486 Half rate support (NECI): New establishment causes are not supported
487 min.RX signal level for MS = 0
488 RACH Control Parameters
489 maximum 7 retransmissions
490 8 slots used to spread transmission
491 cell not barred for access
492 call reestablishment not allowed
493 Access Control Class = 0000
494 Channel Description
495 Type = SDCCH/4[2]
496 Timeslot Number: 0
497 Training Sequence Code: 7h
498 ARFCN: 1
499 SI Rest Octets
500 Cell Bar Qualify (CBQ): 0
501 Cell Reselect Offset = 0 dB
502 Temporary Offset = 0 dB
503 Penalty Time = 20 s
504*/
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000505static u_int8_t si4[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000506 /* header */0x41, 0x06, 0x1C,
507 /* lai */0x00, 0xF1, 0x10, 0x00, 0x01,
508 /* sel */0x62, 0x00,
509 /* rach*/0xD5, 0x00, 0x00,
510 /* var */0x64, 0x30, 0xE0, HARDCODED_ARFCN/*0x01*/, 0x80, 0x00, 0x00,
Harald Welte52b1f982008-12-23 20:25:15 +0000511 0x2B, 0x2B, 0x2B
512};
513
514/*
515 SYSTEM INFORMATION TYPE 5
516 Neighbour Cells Description
517 EXT-IND: Carries the complete BA
518 BA-IND = 0
519 Format-ID bit map 0
520 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
521*/
522
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000523static u_int8_t si5[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000524 /* header without l2 len*/0x06, 0x1D,
525 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
526 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Harald Welte52b1f982008-12-23 20:25:15 +0000527};
528
529// SYSTEM INFORMATION TYPE 6
530
531/*
532SACCH FILLING
533 System Info Type: SYSTEM INFORMATION 6
534 L3 Information (Hex): 06 1E 00 01 xx xx 10 00 01 28 FF
535
536SYSTEM INFORMATION TYPE 6
537 Cell identity = 00001 (1h)
538 Location area identification
539 Mobile Country Code (MCC): 001
540 Mobile Network Code (MNC): 01
541 Location Area Code (LAC): 00001 (1h)
542 Cell Options SACCH
543 Power control indicator: not set
544 MSs shall not use uplink DTX on a TCH-F. MS shall not use uplink DTX on TCH-H.
545 Radio link timeout = 36
546 NCC permitted (NCC) = FF
547*/
548
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000549static u_int8_t si6[] = {
Holger Freyther4d505472008-12-28 16:32:42 +0000550 /* header */0x06, 0x1E,
551 /* cell id*/ 0x00, 0x01,
552 /* lai */ 0x00, 0xF1, 0x10, 0x00, 0x01,
553 /* options */ 0x28,
554 /* ncc */ 0xFF,
Harald Welte52b1f982008-12-23 20:25:15 +0000555};
556
557
558
559static const struct bcch_info bcch_infos[] = {
560 {
561 .type = RSL_SYSTEM_INFO_1,
562 .len = sizeof(si1),
563 .data = si1,
564 }, {
565 .type = RSL_SYSTEM_INFO_2,
566 .len = sizeof(si2),
567 .data = si2,
568 }, {
569 .type = RSL_SYSTEM_INFO_3,
570 .len = sizeof(si3),
571 .data = si3,
572 }, {
573 .type = RSL_SYSTEM_INFO_4,
574 .len = sizeof(si4),
575 .data = si4,
576 },
577};
578
Holger Freyther24287b62008-12-28 16:32:41 +0000579static_assert(sizeof(si1) == sizeof(struct gsm48_system_information_type_1), type1)
580static_assert(sizeof(si2) == sizeof(struct gsm48_system_information_type_2), type2)
581static_assert(sizeof(si3) == sizeof(struct gsm48_system_information_type_3), type3)
582static_assert(sizeof(si4) >= sizeof(struct gsm48_system_information_type_4), type4)
Harald Welte104604e2008-12-28 16:36:11 +0000583static_assert(sizeof(si5) == sizeof(struct gsm48_system_information_type_5), type5)
584static_assert(sizeof(si6) >= sizeof(struct gsm48_system_information_type_6), type6)
Holger Freyther24287b62008-12-28 16:32:41 +0000585
Harald Welte52b1f982008-12-23 20:25:15 +0000586/* set all system information types */
587static int set_system_infos(struct gsm_bts *bts)
588{
589 int i;
590
591 for (i = 0; i < ARRAY_SIZE(bcch_infos); i++) {
592 rsl_bcch_info(bts, bcch_infos[i].type,
593 bcch_infos[i].data,
594 bcch_infos[i].len);
595 }
596 rsl_sacch_filling(bts, RSL_SYSTEM_INFO_5, si5, sizeof(si5));
597 rsl_sacch_filling(bts, RSL_SYSTEM_INFO_6, si6, sizeof(si6));
Harald Weltead384642008-12-26 10:20:07 +0000598
599 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000600}
601
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000602/*
Harald Welte38c2f132009-01-06 23:10:57 +0000603 * Inform anyone...
604 */
605static void bsc_hack_channel_allocated(struct gsm_lchan *lchan) {
606}
607
608/*
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000609 * Patch the various SYSTEM INFORMATION tables to update
610 * the LAI
611 */
612static void patch_tables(struct gsm_bts *bts)
613{
Harald Welte98981882009-01-06 18:59:11 +0000614 u_int8_t arfcn_low = ARFCN & 0xff;
615 u_int8_t arfcn_high = (ARFCN >> 8) & 0x0f;
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000616 /* covert the raw packet to the struct */
617 struct gsm48_system_information_type_3 *type_3 =
618 (struct gsm48_system_information_type_3*)&si3;
619 struct gsm48_system_information_type_4 *type_4 =
620 (struct gsm48_system_information_type_4*)&si4;
621 struct gsm48_system_information_type_6 *type_6 =
622 (struct gsm48_system_information_type_6*)&si6;
Harald Welteb84e2f42008-12-28 23:42:04 +0000623 struct gsm48_loc_area_id lai;
624
625 gsm0408_generate_lai(&lai, bts->network->country_code,
626 bts->network->network_code, bts->location_area_code);
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000627
628 /* assign the MCC and MNC */
Harald Welteb84e2f42008-12-28 23:42:04 +0000629 type_3->lai = lai;
630 type_4->lai = lai;
631 type_6->lai = lai;
Harald Welte98981882009-01-06 18:59:11 +0000632
633 /* patch ARFCN */
634 msg_2[74] &= 0xf0;
635 msg_2[74] |= arfcn_high;
636 msg_2[75] = arfcn_low;
637
638 msg_6[7] &= 0xf0;
639 msg_6[7] |= arfcn_high;
640 msg_6[8] = arfcn_low;
641
642 type_4->data[2] &= 0xf0;
643 type_4->data[2] |= arfcn_high;
644 type_4->data[3] = arfcn_low;
Holger Freyther1adb4ff2009-02-04 00:04:52 +0000645
646 /* patch Control Channel Description 10.5.2.11 */
647 type_3->control_channel_desc = bts->chan_desc;
Holger Freytherb9ddfd02008-12-28 16:32:45 +0000648}
649
650
Harald Weltead384642008-12-26 10:20:07 +0000651static void bootstrap_rsl(struct gsm_bts *bts)
Harald Welte52b1f982008-12-23 20:25:15 +0000652{
Harald Welteb84e2f42008-12-28 23:42:04 +0000653 fprintf(stdout, "bootstrapping RSL MCC=%u MNC=%u\n", MCC, MNC);
Harald Welte52b1f982008-12-23 20:25:15 +0000654 set_system_infos(bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000655}
656
Harald Weltead384642008-12-26 10:20:07 +0000657static void mi_cb(int event, struct gsm_bts *bts)
658{
659 switch (event) {
660 case EVT_E1_OML_UP:
661 bootstrap_om(bts);
662 break;
663 case EVT_E1_RSL_UP:
664 bootstrap_rsl(bts);
665 break;
666 default:
667 /* FIXME: deal with TEI or L1 link loss */
668 break;
669 }
670}
671
672static int bootstrap_network(void)
Harald Welte52b1f982008-12-23 20:25:15 +0000673{
674 struct gsm_bts *bts;
675
676 /* initialize our data structures */
Holger Freytherefde7fb2008-12-28 14:14:56 +0000677 gsmnet = gsm_network_init(1, MCC, MNC);
Harald Weltead384642008-12-26 10:20:07 +0000678 if (!gsmnet)
679 return -ENOMEM;
Harald Weltef5cbab72008-12-30 18:00:15 +0000680
681 gsmnet->name_short = "25C3";
682 gsmnet->name_long = "25C3 GSM";
Harald Welte52b1f982008-12-23 20:25:15 +0000683 bts = &gsmnet->bts[0];
684 bts->location_area_code = 1;
Harald Welte98981882009-01-06 18:59:11 +0000685 bts->trx[0].arfcn = ARFCN;
Holger Freyther1adb4ff2009-02-04 00:04:52 +0000686
687 /* Control Channel Description */
688 memset(&bts->chan_desc, 0, sizeof(struct gsm48_control_channel_descr));
689 bts->chan_desc.att = 0;
690 bts->chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_C;
691 bts->chan_desc.bs_pa_mfrms = RSL_BS_PA_MFRMS_5;
692 bts->chan_desc.t3212 = 0;
693
Harald Welte98981882009-01-06 18:59:11 +0000694 patch_tables(bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000695
Holger Freytherceb59b72009-02-06 18:54:00 +0000696 page_init(bts);
697 bts->paging.channel_allocated = bsc_hack_channel_allocated;
Harald Welte38c2f132009-01-06 23:10:57 +0000698
Holger Freyther219518d2009-01-02 22:04:43 +0000699 telnet_init(gsmnet, 4242);
Harald Weltead384642008-12-26 10:20:07 +0000700 if (mi_setup(bts, 0, mi_cb) < 0)
701 return -EIO;
702
703 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000704}
Harald Weltef6b7a902008-12-26 00:05:11 +0000705
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000706
707static void create_pcap_file(char *file)
708{
709 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
710 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
711
712 if (fd < 0) {
713 perror("Failed to open file for pcap");
714 return;
715 }
716
717 mi_set_pcap_fd(fd);
718}
719
Holger Freytherb332f612008-12-27 12:46:51 +0000720static void print_usage()
721{
722 printf("Usage: bsc_hack\n");
723}
724
725static void print_help()
726{
727 printf(" Some useful help...\n");
728 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Holger Freytherefde7fb2008-12-28 14:14:56 +0000729 printf(" -s --disable-color\n");
730 printf(" -n --network-code number(MNC) \n");
731 printf(" -c --country-code number (MCC) \n");
Harald Welte98981882009-01-06 18:59:11 +0000732 printf(" -f --arfcn number The frequency ARFCN\n");
Holger Freytherbde36102008-12-28 22:51:39 +0000733 printf(" -l --database db-name The database to use\n");
Holger Freyther89824fc2008-12-30 16:18:18 +0000734 printf(" -a --authorize-everyone Allow everyone into the network.\n");
Holger Freythere97f7fb2008-12-31 18:52:11 +0000735 printf(" -r --reject-cause number The reject cause for LOCATION UPDATING REJECT.\n");
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000736 printf(" -p --pcap file The filename of the pcap file\n");
Holger Freytherb332f612008-12-27 12:46:51 +0000737 printf(" -h --help this text\n");
738}
739
740static void handle_options(int argc, char** argv)
741{
742 while (1) {
743 int option_index = 0, c;
744 static struct option long_options[] = {
745 {"help", 0, 0, 'h'},
746 {"debug", 1, 0, 'd'},
Holger Freytherefde7fb2008-12-28 14:14:56 +0000747 {"disable-color", 0, 0, 's'},
748 {"network-code", 1, 0, 'n'},
749 {"country-code", 1, 0, 'c'},
Holger Freytherbde36102008-12-28 22:51:39 +0000750 {"database", 1, 0, 'l'},
Holger Freyther89824fc2008-12-30 16:18:18 +0000751 {"authorize-everyone", 0, 0, 'a'},
Holger Freythere97f7fb2008-12-31 18:52:11 +0000752 {"reject-cause", 1, 0, 'r'},
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000753 {"pcap", 1, 0, 'p'},
Harald Welte98981882009-01-06 18:59:11 +0000754 {"arfcn", 1, 0, 'f'},
Holger Freytherb332f612008-12-27 12:46:51 +0000755 {0, 0, 0, 0}
756 };
757
Harald Welte98981882009-01-06 18:59:11 +0000758 c = getopt_long(argc, argv, "hc:n:d:sar:p:f:",
Holger Freytherb332f612008-12-27 12:46:51 +0000759 long_options, &option_index);
760 if (c == -1)
761 break;
762
763 switch (c) {
764 case 'h':
765 print_usage();
766 print_help();
767 exit(0);
Holger Freytherefde7fb2008-12-28 14:14:56 +0000768 case 's':
Holger Freytherb332f612008-12-27 12:46:51 +0000769 debug_use_color(0);
770 break;
771 case 'd':
772 debug_parse_category_mask(optarg);
773 break;
Holger Freytherefde7fb2008-12-28 14:14:56 +0000774 case 'n':
775 MNC = atoi(optarg);
776 break;
777 case 'c':
778 MCC = atoi(optarg);
779 break;
Harald Welte98981882009-01-06 18:59:11 +0000780 case 'f':
781 ARFCN = atoi(optarg);
782 break;
Harald Welte8965da42009-01-06 18:09:02 +0000783 case 'l':
Holger Freytherbde36102008-12-28 22:51:39 +0000784 database_name = strdup(optarg);
785 break;
Holger Freyther89824fc2008-12-30 16:18:18 +0000786 case 'a':
787 gsm0408_allow_everyone(1);
788 break;
Holger Freythere97f7fb2008-12-31 18:52:11 +0000789 case 'r':
790 gsm0408_set_reject_cause(atoi(optarg));
791 break;
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000792 case 'p':
793 create_pcap_file(optarg);
794 break;
Holger Freytherb332f612008-12-27 12:46:51 +0000795 default:
796 /* ignore */
797 break;
798 }
799 }
800}
801
Harald Welted1252502009-01-01 01:50:32 +0000802static void signal_handler(int signal)
803{
804 fprintf(stdout, "signal %u received\n", signal);
805
806 switch (signal) {
807 case SIGHUP:
808 case SIGABRT:
809 shutdown_net(gsmnet);
810 break;
811 default:
812 break;
813 }
814}
815
Harald Weltef6b7a902008-12-26 00:05:11 +0000816int main(int argc, char **argv)
817{
Holger Freytherb332f612008-12-27 12:46:51 +0000818 /* parse options */
819 handle_options(argc, argv);
820
Holger Freytherbde36102008-12-28 22:51:39 +0000821 if (db_init(database_name)) {
Harald Welte75a983f2008-12-27 21:34:06 +0000822 printf("DB: Failed to init database. Please check the option settings.\n");
823 return 1;
824 }
825 printf("DB: Database initialized.\n");
826
827 if (db_prepare()) {
828 printf("DB: Failed to prepare database.\n");
829 return 1;
830 }
831 printf("DB: Database prepared.\n");
832
Harald Weltef6b7a902008-12-26 00:05:11 +0000833 bootstrap_network();
834
Harald Welted1252502009-01-01 01:50:32 +0000835 signal(SIGHUP, &signal_handler);
836 signal(SIGABRT, &signal_handler);
837
Harald Weltef6b7a902008-12-26 00:05:11 +0000838 while (1) {
839 bsc_select_main();
840 }
841}