blob: d2d0b06d16e0373d008100b0310c622d722ce6dd [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* A hackish minimal BSC (+MSC +HLR) implementation */
2
3/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
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
23#include <unistd.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <stdarg.h>
27#include <time.h>
28#include <string.h>
29#include <errno.h>
30#include <signal.h>
31#include <fcntl.h>
32#include <sys/stat.h>
33
34#define _GNU_SOURCE
35#include <getopt.h>
36
37#include <openbsc/db.h>
38#include <openbsc/timer.h>
39#include <openbsc/gsm_data.h>
40#include <openbsc/gsm_04_08.h>
41#include <openbsc/select.h>
42#include <openbsc/abis_rsl.h>
43#include <openbsc/abis_nm.h>
44#include <openbsc/debug.h>
45#include <openbsc/misdn.h>
46#include <openbsc/telnet_interface.h>
47#include <openbsc/paging.h>
48#include <openbsc/e1_input.h>
49#include <openbsc/signal.h>
Harald Weltea8379772009-06-20 22:36:41 +020050#include <openbsc/talloc.h>
51
Harald Welte59b04682009-06-10 05:40:52 +080052/* global pointer to the gsm network data structure */
53static struct gsm_network *gsmnet;
54
55/* MCC and MNC for the Location Area Identifier */
56static int MCC = 1;
57static int MNC = 1;
58static int LAC = 1;
59static int ARFCN = HARDCODED_ARFCN;
60static int cardnr = 0;
61static int release_l2 = 0;
62static enum gsm_bts_type BTS_TYPE = GSM_BTS_TYPE_BS11;
Harald Welte1d50e082009-06-20 18:15:19 +020063static enum gsm_band BAND = GSM_BAND_900;
Harald Welte59b04682009-06-10 05:40:52 +080064static const char *database_name = "hlr.sqlite3";
65
66/* The following definitions are for OM and NM packets that we cannot yet
67 * generate by code but we just pass on */
68
69// BTS Site Manager, SET ATTRIBUTES
70
71/*
72 Object Class: BTS Site Manager
73 Instance 1: FF
74 Instance 2: FF
75 Instance 3: FF
76SET ATTRIBUTES
77 sAbisExternalTime: 2007/09/08 14:36:11
78 omLAPDRelTimer: 30sec
79 shortLAPDIntTimer: 5sec
80 emergencyTimer1: 10 minutes
81 emergencyTimer2: 0 minutes
82*/
83
84unsigned char msg_1[] =
85{
Harald Weltea4b54d82009-06-20 10:42:17 +020086 NM_MT_BS11_SET_ATTR, NM_OC_SITE_MANAGER, 0xFF, 0xFF, 0xFF,
Harald Welte59b04682009-06-10 05:40:52 +080087 NM_ATT_BS11_ABIS_EXT_TIME, 0x07,
88 0xD7, 0x09, 0x08, 0x0E, 0x24, 0x0B, 0xCE,
89 0x02,
90 0x00, 0x1E,
91 NM_ATT_BS11_SH_LAPD_INT_TIMER,
92 0x01, 0x05,
93 0x42, 0x02, 0x00, 0x0A,
94 0x44, 0x02, 0x00, 0x00
95};
96
97// BTS, SET BTS ATTRIBUTES
98
99/*
100 Object Class: BTS
101 BTS relat. Number: 0
102 Instance 2: FF
103 Instance 3: FF
104SET BTS ATTRIBUTES
105 bsIdentityCode / BSIC:
106 PLMN_colour_code: 7h
107 BS_colour_code: 7h
108 BTS Air Timer T3105: 4 ,unit 10 ms
109 btsIsHopping: FALSE
110 periodCCCHLoadIndication: 1sec
111 thresholdCCCHLoadIndication: 0%
112 cellAllocationNumber: 00h = GSM 900
113 enableInterferenceClass: 00h = Disabled
114 fACCHQual: 6 (FACCH stealing flags minus 1)
115 intaveParameter: 31 SACCH multiframes
116 interferenceLevelBoundaries:
117 Interference Boundary 1: 0Ah
118 Interference Boundary 2: 0Fh
119 Interference Boundary 3: 14h
120 Interference Boundary 4: 19h
121 Interference Boundary 5: 1Eh
122 mSTxPwrMax: 11
123 GSM range: 2=39dBm, 15=13dBm, stepsize 2 dBm
124 DCS1800 range: 0=30dBm, 15=0dBm, stepsize 2 dBm
125 PCS1900 range: 0=30dBm, 15=0dBm, stepsize 2 dBm
126 30=33dBm, 31=32dBm
127 ny1:
128 Maximum number of repetitions for PHYSICAL INFORMATION message (GSM 04.08): 20
129 powerOutputThresholds:
130 Out Power Fault Threshold: -10 dB
131 Red Out Power Threshold: - 6 dB
132 Excessive Out Power Threshold: 5 dB
133 rACHBusyThreshold: -127 dBm
134 rACHLoadAveragingSlots: 250 ,number of RACH burst periods
135 rfResourceIndicationPeriod: 125 SACCH multiframes
136 T200:
137 SDCCH: 044 in 5 ms
138 FACCH/Full rate: 031 in 5 ms
139 FACCH/Half rate: 041 in 5 ms
140 SACCH with TCH SAPI0: 090 in 10 ms
141 SACCH with SDCCH: 090 in 10 ms
142 SDCCH with SAPI3: 090 in 5 ms
143 SACCH with TCH SAPI3: 135 in 10 ms
144 tSync: 9000 units of 10 msec
145 tTrau: 9000 units of 10 msec
146 enableUmLoopTest: 00h = disabled
147 enableExcessiveDistance: 00h = Disabled
148 excessiveDistance: 64km
149 hoppingMode: 00h = baseband hopping
150 cellType: 00h = Standard Cell
151 BCCH ARFCN / bCCHFrequency: 1
152*/
153
Harald Weltea4b54d82009-06-20 10:42:17 +0200154static unsigned char bs11_attr_bts[] =
Harald Welte59b04682009-06-10 05:40:52 +0800155{
Harald Welte59b04682009-06-10 05:40:52 +0800156 NM_ATT_BSIC, HARDCODED_BSIC,
157 NM_ATT_BTS_AIR_TIMER, 0x04,
158 NM_ATT_BS11_BTSLS_HOPPING, 0x00,
159 NM_ATT_CCCH_L_I_P, 0x01,
160 NM_ATT_CCCH_L_T, 0x00,
161 NM_ATT_BS11_CELL_ALLOC_NR, NM_BS11_CANR_GSM,
162 NM_ATT_BS11_ENA_INTERF_CLASS, 0x01,
163 NM_ATT_BS11_FACCH_QUAL, 0x06,
164 /* interference avg. period in numbers of SACCH multifr */
165 NM_ATT_INTAVE_PARAM, 0x1F,
166 NM_ATT_INTERF_BOUND, 0x0A, 0x0F, 0x14, 0x19, 0x1E, 0x7B,
167 NM_ATT_CCCH_L_T, 0x23,
168 NM_ATT_GSM_TIME, 0x28, 0x00,
169 NM_ATT_ADM_STATE, 0x03,
170 NM_ATT_RACH_B_THRESH, 0x7F,
171 NM_ATT_LDAVG_SLOTS, 0x00, 0xFA,
172 NM_ATT_BS11_RF_RES_IND_PER, 0x7D,
173 NM_ATT_T200, 0x2C, 0x1F, 0x29, 0x5A, 0x5A, 0x5A, 0x87,
174 NM_ATT_BS11_TSYNC, 0x23, 0x28,
175 NM_ATT_BS11_TTRAU, 0x23, 0x28,
176 NM_ATT_TEST_DUR, 0x01, 0x00,
177 NM_ATT_OUTST_ALARM, 0x01, 0x00,
178 NM_ATT_BS11_EXCESSIVE_DISTANCE, 0x01, 0x40,
179 NM_ATT_BS11_HOPPING_MODE, 0x01, 0x00,
180 NM_ATT_BS11_PLL, 0x01, 0x00,
181 NM_ATT_BCCH_ARFCN, 0x00, HARDCODED_ARFCN/*0x01*/,
182};
183
184// Handover Recognition, SET ATTRIBUTES
185
186/*
187Illegal Contents GSM Formatted O&M Msg
188 Object Class: Handover Recognition
189 BTS relat. Number: 0
190 Instance 2: FF
191 Instance 3: FF
192SET ATTRIBUTES
193 enableDelayPowerBudgetHO: 00h = Disabled
194 enableDistanceHO: 00h = Disabled
195 enableInternalInterCellHandover: 00h = Disabled
196 enableInternalIntraCellHandover: 00h = Disabled
197 enablePowerBudgetHO: 00h = Disabled
198 enableRXLEVHO: 00h = Disabled
199 enableRXQUALHO: 00h = Disabled
200 hoAveragingDistance: 8 SACCH multiframes
201 hoAveragingLev:
202 A_LEV_HO: 8 SACCH multiframes
203 W_LEV_HO: 1 SACCH multiframes
204 hoAveragingPowerBudget: 16 SACCH multiframes
205 hoAveragingQual:
206 A_QUAL_HO: 8 SACCH multiframes
207 W_QUAL_HO: 2 SACCH multiframes
208 hoLowerThresholdLevDL: (10 - 110) dBm
209 hoLowerThresholdLevUL: (5 - 110) dBm
210 hoLowerThresholdQualDL: 06h = 6.4% < BER < 12.8%
211 hoLowerThresholdQualUL: 06h = 6.4% < BER < 12.8%
212 hoThresholdLevDLintra : (20 - 110) dBm
213 hoThresholdLevULintra: (20 - 110) dBm
214 hoThresholdMsRangeMax: 20 km
215 nCell: 06h
216 timerHORequest: 3 ,unit 2 SACCH multiframes
217*/
218
219unsigned char msg_3[] =
220{
Harald Weltea4b54d82009-06-20 10:42:17 +0200221 NM_MT_BS11_SET_ATTR, NM_OC_BS11_HANDOVER, 0x00, 0xFF, 0xFF,
Harald Welte59b04682009-06-10 05:40:52 +0800222 0xD0, 0x00,
223 0x64, 0x00,
224 0x67, 0x00,
225 0x68, 0x00,
226 0x6A, 0x00,
227 0x6C, 0x00,
228 0x6D, 0x00,
229 0x6F, 0x08,
230 0x70, 0x08, 0x01,
231 0x71, 0x10, 0x10, 0x10,
232 0x72, 0x08, 0x02,
233 0x73, 0x0A,
234 0x74, 0x05,
235 0x75, 0x06,
236 0x76, 0x06,
237 0x78, 0x14,
238 0x79, 0x14,
239 0x7A, 0x14,
240 0x7D, 0x06,
241 0x92, 0x03, 0x20, 0x01, 0x00,
242 0x45, 0x01, 0x00,
243 0x48, 0x01, 0x00,
244 0x5A, 0x01, 0x00,
245 0x5B, 0x01, 0x05,
246 0x5E, 0x01, 0x1A,
247 0x5F, 0x01, 0x20,
248 0x9D, 0x01, 0x00,
249 0x47, 0x01, 0x00,
250 0x5C, 0x01, 0x64,
251 0x5D, 0x01, 0x1E,
252 0x97, 0x01, 0x20,
253 0xF7, 0x01, 0x3C,
254};
255
256// Power Control, SET ATTRIBUTES
257
258/*
259 Object Class: Power Control
260 BTS relat. Number: 0
261 Instance 2: FF
262 Instance 3: FF
263SET ATTRIBUTES
264 enableMsPowerControl: 00h = Disabled
265 enablePowerControlRLFW: 00h = Disabled
266 pcAveragingLev:
267 A_LEV_PC: 4 SACCH multiframes
268 W_LEV_PC: 1 SACCH multiframes
269 pcAveragingQual:
270 A_QUAL_PC: 4 SACCH multiframes
271 W_QUAL_PC: 2 SACCH multiframes
272 pcLowerThresholdLevDL: 0Fh
273 pcLowerThresholdLevUL: 0Ah
274 pcLowerThresholdQualDL: 05h = 3.2% < BER < 6.4%
275 pcLowerThresholdQualUL: 05h = 3.2% < BER < 6.4%
276 pcRLFThreshold: 0Ch
277 pcUpperThresholdLevDL: 14h
278 pcUpperThresholdLevUL: 0Fh
279 pcUpperThresholdQualDL: 04h = 1.6% < BER < 3.2%
280 pcUpperThresholdQualUL: 04h = 1.6% < BER < 3.2%
281 powerConfirm: 2 ,unit 2 SACCH multiframes
282 powerControlInterval: 2 ,unit 2 SACCH multiframes
283 powerIncrStepSize: 02h = 4 dB
284 powerRedStepSize: 01h = 2 dB
285 radioLinkTimeoutBs: 64 SACCH multiframes
286 enableBSPowerControl: 00h = disabled
287*/
288
289unsigned char msg_4[] =
290{
Harald Weltea4b54d82009-06-20 10:42:17 +0200291 NM_MT_BS11_SET_ATTR, NM_OC_BS11_PWR_CTRL, 0x00, 0xFF, 0xFF,
Harald Welte59b04682009-06-10 05:40:52 +0800292 NM_ATT_BS11_ENA_MS_PWR_CTRL, 0x00,
293 NM_ATT_BS11_ENA_PWR_CTRL_RLFW, 0x00,
294 0x7E, 0x04, 0x01,
295 0x7F, 0x04, 0x02,
296 0x80, 0x0F,
297 0x81, 0x0A,
298 0x82, 0x05,
299 0x83, 0x05,
300 0x84, 0x0C,
301 0x85, 0x14,
302 0x86, 0x0F,
303 0x87, 0x04,
304 0x88, 0x04,
305 0x89, 0x02,
306 0x8A, 0x02,
307 0x8B, 0x02,
308 0x8C, 0x01,
309 0x8D, 0x40,
310 0x65, 0x01, 0x00 // set to 0x01 to enable BSPowerControl
311};
312
313
314// Transceiver, SET TRX ATTRIBUTES (TRX 0)
315
316/*
317 Object Class: Transceiver
318 BTS relat. Number: 0
319 Tranceiver number: 0
320 Instance 3: FF
321SET TRX ATTRIBUTES
322 aRFCNList (HEX): 0001
323 txPwrMaxReduction: 00h = 30dB
324 radioMeasGran: 254 SACCH multiframes
325 radioMeasRep: 01h = enabled
326 memberOfEmergencyConfig: 01h = TRUE
327 trxArea: 00h = TRX doesn't belong to a concentric cell
328*/
329
Harald Weltea4b54d82009-06-20 10:42:17 +0200330static unsigned char bs11_attr_radio[] =
Harald Welte59b04682009-06-10 05:40:52 +0800331{
Harald Welte59b04682009-06-10 05:40:52 +0800332 NM_ATT_ARFCN_LIST, 0x01, 0x00, HARDCODED_ARFCN /*0x01*/,
333 NM_ATT_RF_MAXPOWR_R, 0x00,
334 NM_ATT_BS11_RADIO_MEAS_GRAN, 0x01, 0xFE,
335 NM_ATT_BS11_RADIO_MEAS_REP, 0x01, 0x01,
336 NM_ATT_BS11_EMRG_CFG_MEMBER, 0x01, 0x01,
337 NM_ATT_BS11_TRX_AREA, 0x01, 0x00,
338};
339
340static unsigned char nanobts_attr_bts[] = {
341 NM_ATT_INTERF_BOUND, 0x55, 0x5b, 0x61, 0x67, 0x6d, 0x73,
342 /* interference avg. period in numbers of SACCH multifr */
343 NM_ATT_INTAVE_PARAM, 0x06,
344 /* conn fail based on SACCH error rate */
345 NM_ATT_CONN_FAIL_CRIT, 0x00, 0x02, 0x01, 0x10,
346 NM_ATT_T200, 0x1e, 0x24, 0x24, 0xa8, 0x34, 0x21, 0xa8,
347 NM_ATT_MAX_TA, 0x3f,
348 NM_ATT_OVERL_PERIOD, 0x00, 0x01, 10, /* seconds */
349 NM_ATT_CCCH_L_T, 10, /* percent */
350 NM_ATT_CCCH_L_I_P, 1, /* seconds */
351 NM_ATT_RACH_B_THRESH, 10, /* busy threshold in - dBm */
352 NM_ATT_LDAVG_SLOTS, 0x03, 0xe8, /* rach load averaging 1000 slots */
353 NM_ATT_BTS_AIR_TIMER, 128, /* miliseconds */
354 NM_ATT_NY1, 10, /* 10 retransmissions of physical config */
355 NM_ATT_BCCH_ARFCN, HARDCODED_ARFCN >> 8, HARDCODED_ARFCN & 0xff,
356 NM_ATT_BSIC, HARDCODED_BSIC,
357};
358
359static unsigned char nanobts_attr_radio[] = {
360 NM_ATT_RF_MAXPOWR_R, 0x0c, /* number of -2dB reduction steps / Pn */
361 NM_ATT_ARFCN_LIST, 0x00, 0x02, HARDCODED_ARFCN >> 8, HARDCODED_ARFCN & 0xff,
362};
363
364static unsigned char nanobts_attr_e0[] = {
365 0x85, 0x00,
366 0x81, 0x0b, 0xbb, /* TCP PORT for RSL */
367};
368
369/* Callback function to be called whenever we get a GSM 12.21 state change event */
370int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
371 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
372{
373 struct gsm_bts *bts;
374 struct gsm_bts_trx *trx;
375 struct gsm_bts_trx_ts *ts;
376
377 /* This is currently only required on nanoBTS */
378
379 switch (evt) {
380 case EVT_STATECHG_OPER:
381 switch (obj_class) {
382 case NM_OC_SITE_MANAGER:
383 bts = container_of(obj, struct gsm_bts, site_mgr);
384 if (old_state->operational != 2 && new_state->operational == 2) {
385 abis_nm_opstart(bts, NM_OC_SITE_MANAGER, 0xff, 0xff, 0xff);
386 }
387 break;
388 case NM_OC_BTS:
389 bts = obj;
390 if (new_state->availability == 5) {
391 abis_nm_set_bts_attr(bts, nanobts_attr_bts,
392 sizeof(nanobts_attr_bts));
393 abis_nm_opstart(bts, NM_OC_BTS,
394 bts->bts_nr, 0xff, 0xff);
395 abis_nm_chg_adm_state(bts, NM_OC_BTS,
396 bts->bts_nr, 0xff, 0xff,
397 NM_STATE_UNLOCKED);
398 }
399 break;
400 case NM_OC_CHANNEL:
401 ts = obj;
402 trx = ts->trx;
403 if (new_state->availability == 5) {
404 if (ts->nr == 0 && trx == trx->bts->c0)
405 abis_nm_set_channel_attr(ts, NM_CHANC_BCCH_CBCH);
406 else
407 abis_nm_set_channel_attr(ts, NM_CHANC_TCHFull);
408 abis_nm_opstart(trx->bts, NM_OC_CHANNEL,
409 trx->bts->bts_nr, trx->nr, ts->nr);
410 abis_nm_chg_adm_state(trx->bts, NM_OC_CHANNEL,
411 trx->bts->bts_nr, trx->nr, ts->nr,
412 NM_STATE_UNLOCKED);
413 }
414 break;
415 default:
416 break;
417 }
418 break;
419 default:
420 //DEBUGP(DMM, "Unhandled state change in %s:%d\n", __func__, __LINE__);
421 break;
422 }
423 return 0;
424}
425
426/* Callback function to be called every time we receive a 12.21 SW activated report */
427static int sw_activ_rep(struct msgb *mb)
428{
429 struct abis_om_fom_hdr *foh = msgb_l3(mb);
430 struct gsm_bts_trx *trx = mb->trx;
431
432 switch (foh->obj_class) {
433 case NM_OC_BASEB_TRANSC:
434 /* TRX software is active, tell it to initiate RSL Link */
435 abis_nm_ipaccess_msg(trx->bts, 0xe0, NM_OC_BASEB_TRANSC,
436 trx->bts->bts_nr, trx->nr, 0xff,
437 nanobts_attr_e0, sizeof(nanobts_attr_e0));
438 abis_nm_opstart(trx->bts, NM_OC_BASEB_TRANSC,
439 trx->bts->bts_nr, trx->nr, 0xff);
440 abis_nm_chg_adm_state(trx->bts, NM_OC_BASEB_TRANSC,
441 trx->bts->bts_nr, trx->nr, 0xff,
442 NM_STATE_UNLOCKED);
443 break;
444 case NM_OC_RADIO_CARRIER:
445 abis_nm_set_radio_attr(trx, nanobts_attr_radio,
446 sizeof(nanobts_attr_radio));
447 abis_nm_opstart(trx->bts, NM_OC_RADIO_CARRIER,
448 trx->bts->bts_nr, trx->nr, 0xff);
449 abis_nm_chg_adm_state(trx->bts, NM_OC_RADIO_CARRIER,
450 trx->bts->bts_nr, trx->nr, 0xff,
451 NM_STATE_UNLOCKED);
452 break;
453 }
454 return 0;
455}
456
Holger Hans Peter Freytherefedf942009-06-10 10:48:14 +0200457/* Callback function for NACK on the OML NM */
458static int oml_msg_nack(int mt)
459{
460 if (mt == NM_MT_SET_BTS_ATTR_NACK) {
461 fprintf(stderr, "Failed to set BTS attributes. That is fatal. "
462 "Was the bts type and frequency properly specified?\n");
463 exit(-1);
464 }
465
466 return 0;
467}
468
Harald Welte59b04682009-06-10 05:40:52 +0800469/* Callback function to be called every time we receive a signal from NM */
470static int nm_sig_cb(unsigned int subsys, unsigned int signal,
471 void *handler_data, void *signal_data)
472{
473 switch (signal) {
474 case S_NM_SW_ACTIV_REP:
475 return sw_activ_rep(signal_data);
Holger Hans Peter Freytherefedf942009-06-10 10:48:14 +0200476 case S_NM_NACK:
477 return oml_msg_nack((int)signal_data);
Harald Welte59b04682009-06-10 05:40:52 +0800478 default:
479 break;
480 }
481 return 0;
482}
483
484static void bootstrap_om_nanobts(struct gsm_bts *bts)
485{
486 /* We don't do callback based bootstrapping, but event driven (see above) */
487}
488
489static void bootstrap_om_bs11(struct gsm_bts *bts)
490{
Harald Weltee712a5f2009-06-21 16:17:15 +0200491 struct gsm_bts_trx *trx = bts->c0;
Harald Welte59b04682009-06-10 05:40:52 +0800492
493 /* stop sending event reports */
494 abis_nm_event_reports(bts, 0);
495
496 /* begin DB transmission */
497 abis_nm_bs11_db_transmission(bts, 1);
498
499 /* end DB transmission */
500 abis_nm_bs11_db_transmission(bts, 0);
501
502 /* Reset BTS Site manager resource */
503 abis_nm_bs11_reset_resource(bts);
504
505 /* begin DB transmission */
506 abis_nm_bs11_db_transmission(bts, 1);
507
508 abis_nm_raw_msg(bts, sizeof(msg_1), msg_1); /* set BTS SiteMgr attr*/
Harald Weltea4b54d82009-06-20 10:42:17 +0200509 abis_nm_set_bts_attr(bts, bs11_attr_bts, sizeof(bs11_attr_bts));
Harald Welte59b04682009-06-10 05:40:52 +0800510 abis_nm_raw_msg(bts, sizeof(msg_3), msg_3); /* set BTS handover attr */
511 abis_nm_raw_msg(bts, sizeof(msg_4), msg_4); /* set BTS power control attr */
512
513 /* Connect signalling of bts0/trx0 to e1_0/ts1/64kbps */
514 abis_nm_conn_terr_sign(trx, 0, 1, 0xff);
Harald Weltea4b54d82009-06-20 10:42:17 +0200515 abis_nm_set_radio_attr(trx, bs11_attr_radio, sizeof(bs11_attr_radio));
Harald Welte59b04682009-06-10 05:40:52 +0800516
517 /* Use TEI 1 for signalling */
518 abis_nm_establish_tei(bts, 0, 0, 1, 0xff, 0x01);
519 abis_nm_set_channel_attr(&trx->ts[0], NM_CHANC_SDCCH_CBCH);
520
521#ifdef HAVE_TRX1
522 /* TRX 1 */
523 abis_nm_conn_terr_sign(&bts->trx[1], 0, 1, 0xff);
524 /* FIXME: TRX ATTRIBUTE */
525 abis_nm_establish_tei(bts, 0, 0, 1, 0xff, 0x02);
526#endif
527
528 /* SET CHANNEL ATTRIBUTE TS1 */
529 abis_nm_set_channel_attr(&trx->ts[1], NM_CHANC_TCHFull);
530 /* Connect traffic of bts0/trx0/ts1 to e1_0/ts2/b */
531 abis_nm_conn_terr_traf(&trx->ts[1], 0, 2, 1);
532
533 /* SET CHANNEL ATTRIBUTE TS2 */
534 abis_nm_set_channel_attr(&trx->ts[2], NM_CHANC_TCHFull);
535 /* Connect traffic of bts0/trx0/ts2 to e1_0/ts2/c */
536 abis_nm_conn_terr_traf(&trx->ts[2], 0, 2, 2);
537
538 /* SET CHANNEL ATTRIBUTE TS3 */
539 abis_nm_set_channel_attr(&trx->ts[3], NM_CHANC_TCHFull);
540 /* Connect traffic of bts0/trx0/ts3 to e1_0/ts2/d */
541 abis_nm_conn_terr_traf(&trx->ts[3], 0, 2, 3);
542
543 /* SET CHANNEL ATTRIBUTE TS4 */
544 abis_nm_set_channel_attr(&trx->ts[4], NM_CHANC_TCHFull);
545 /* Connect traffic of bts0/trx0/ts4 to e1_0/ts3/a */
546 abis_nm_conn_terr_traf(&trx->ts[4], 0, 3, 0);
547
548 /* SET CHANNEL ATTRIBUTE TS5 */
549 abis_nm_set_channel_attr(&trx->ts[5], NM_CHANC_TCHFull);
550 /* Connect traffic of bts0/trx0/ts5 to e1_0/ts3/b */
551 abis_nm_conn_terr_traf(&trx->ts[5], 0, 3, 1);
552
553 /* SET CHANNEL ATTRIBUTE TS6 */
554 abis_nm_set_channel_attr(&trx->ts[6], NM_CHANC_TCHFull);
555 /* Connect traffic of bts0/trx0/ts6 to e1_0/ts3/c */
556 abis_nm_conn_terr_traf(&trx->ts[6], 0, 3, 2);
557
558 /* SET CHANNEL ATTRIBUTE TS7 */
559 abis_nm_set_channel_attr(&trx->ts[7], NM_CHANC_TCHFull);
560 /* Connect traffic of bts0/trx0/ts7 to e1_0/ts3/d */
561 abis_nm_conn_terr_traf(&trx->ts[7], 0, 3, 3);
562
563 /* end DB transmission */
564 abis_nm_bs11_db_transmission(bts, 0);
565
566 /* Reset BTS Site manager resource */
567 abis_nm_bs11_reset_resource(bts);
568
569 /* restart sending event reports */
570 abis_nm_event_reports(bts, 1);
571}
572
573static void bootstrap_om(struct gsm_bts *bts)
574{
575 fprintf(stdout, "bootstrapping OML for BTS %u\n", bts->nr);
576
577 switch (bts->type) {
578 case GSM_BTS_TYPE_BS11:
579 bootstrap_om_bs11(bts);
580 break;
581 case GSM_BTS_TYPE_NANOBTS_900:
582 case GSM_BTS_TYPE_NANOBTS_1800:
583 bootstrap_om_nanobts(bts);
584 break;
585 default:
586 fprintf(stderr, "Unable to bootstrap OML: Unknown BTS type %d\n", bts->type);
587 }
588}
589
590static int shutdown_om(struct gsm_bts *bts)
591{
592 /* stop sending event reports */
593 abis_nm_event_reports(bts, 0);
594
595 /* begin DB transmission */
596 abis_nm_bs11_db_transmission(bts, 1);
597
598 /* end DB transmission */
599 abis_nm_bs11_db_transmission(bts, 0);
600
601 /* Reset BTS Site manager resource */
602 abis_nm_bs11_reset_resource(bts);
603
604 return 0;
605}
606
607static int shutdown_net(struct gsm_network *net)
608{
Harald Weltee712a5f2009-06-21 16:17:15 +0200609 struct gsm_bts *bts;
610
611 llist_for_each_entry(bts, &net->bts_list, list) {
Harald Welte59b04682009-06-10 05:40:52 +0800612 int rc;
Harald Weltee712a5f2009-06-21 16:17:15 +0200613 rc = shutdown_om(bts);
Harald Welte59b04682009-06-10 05:40:52 +0800614 if (rc < 0)
615 return rc;
616 }
617
618 return 0;
619}
620
621struct bcch_info {
622 u_int8_t type;
623 u_int8_t len;
624 const u_int8_t *data;
625};
626
627/*
628SYSTEM INFORMATION TYPE 1
629 Cell channel description
630 Format-ID bit map 0
631 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
632 RACH Control Parameters
633 maximum 7 retransmissions
634 8 slots used to spread transmission
635 cell not barred for access
636 call reestablishment not allowed
637 Access Control Class = 0000
638*/
639static u_int8_t si1[] = {
640 /* header */0x55, 0x06, 0x19,
641 /* ccdesc */0x04 /*0x00*/, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
642 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /*0x01*/,
643 /* rach */0xD5, 0x00, 0x00,
644 /* s1 reset*/0x2B
645};
646
647/*
648 SYSTEM INFORMATION TYPE 2
649 Neighbour Cells Description
650 EXT-IND: Carries the complete BA
651 BA-IND = 0
652 Format-ID bit map 0
653 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
654 NCC permitted (NCC) = FF
655 RACH Control Parameters
656 maximum 7 retransmissions
657 8 slots used to spread transmission
658 cell not barred for access
659 call reestablishment not allowed
660 Access Control Class = 0000
661*/
662static u_int8_t si2[] = {
663 /* header */0x59, 0x06, 0x1A,
664 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
665 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
666 /* ncc */0xFF,
667 /* rach*/0xD5, 0x00, 0x00
668};
669
670/*
671SYSTEM INFORMATION TYPE 3
672 Cell identity = 00001 (1h)
673 Location area identification
674 Mobile Country Code (MCC): 001
675 Mobile Network Code (MNC): 01
676 Location Area Code (LAC): 00001 (1h)
677 Control Channel Description
678 Attach-detach: MSs in the cell are not allowed to apply IMSI attach /detach
679 0 blocks reserved for access grant
680 1 channel used for CCCH, with SDCCH
681 5 multiframes period for PAGING REQUEST
682 Time-out T3212 = 0
683 Cell Options BCCH
684 Power control indicator: not set
685 MSs shall not use uplink DTX
686 Radio link timeout = 36
687 Cell Selection Parameters
688 Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
689 max.TX power level MS may use for CCH = 2 <- according to GSM05.05 39dBm (max)
690 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
691 Half rate support (NECI): New establishment causes are not supported
692 min.RX signal level for MS = 0
693 RACH Control Parameters
694 maximum 7 retransmissions
695 8 slots used to spread transmission
696 cell not barred for access
697 call reestablishment not allowed
698 Access Control Class = 0000
699 SI 3 Rest Octets
700 Cell Bar Qualify (CBQ): 0
701 Cell Reselect Offset = 0 dB
702 Temporary Offset = 0 dB
703 Penalty Time = 20 s
704 System Information 2ter Indicator (2TI): 0 = not available
705 Early Classmark Sending Control (ECSC): 0 = forbidden
706 Scheduling Information is not sent in SYSTEM INFORMATION TYPE 9 on the BCCH
707*/
708static u_int8_t si3[] = {
709 /* header */0x49, 0x06, 0x1B,
710 /* cell */0x00, 0x01,
711 /* lai */0x00, 0xF1, 0x10, 0x00, 0x01,
712 /* desc */0x01, 0x03, 0x00,
713 /* option*/0x28,
714 /* selection*/0x62, 0x00,
715 /* rach */0xD5, 0x00, 0x00,
716 /* reset*/0x80, 0x00, 0x00, 0x2B
717};
718
719/*
720SYSTEM INFORMATION TYPE 4
721 Location area identification
722 Mobile Country Code (MCC): 001
723 Mobile Network Code (MNC): 01
724 Location Area Code (LAC): 00001 (1h)
725 Cell Selection Parameters
726 Cell reselect hysteresis = 6 dB RXLEV hysteresis for LA re-selection
727 max.TX power level MS may use for CCH = 2
728 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
729 Half rate support (NECI): New establishment causes are not supported
730 min.RX signal level for MS = 0
731 RACH Control Parameters
732 maximum 7 retransmissions
733 8 slots used to spread transmission
734 cell not barred for access
735 call reestablishment not allowed
736 Access Control Class = 0000
737 Channel Description
738 Type = SDCCH/4[2]
739 Timeslot Number: 0
740 Training Sequence Code: 7h
741 ARFCN: 1
742 SI Rest Octets
743 Cell Bar Qualify (CBQ): 0
744 Cell Reselect Offset = 0 dB
745 Temporary Offset = 0 dB
746 Penalty Time = 20 s
747*/
748static u_int8_t si4[] = {
749 /* header */0x41, 0x06, 0x1C,
750 /* lai */0x00, 0xF1, 0x10, 0x00, 0x01,
751 /* sel */0x62, 0x00,
752 /* rach*/0xD5, 0x00, 0x00,
753 /* var */0x64, 0x30, 0xE0, HARDCODED_ARFCN/*0x01*/, 0x80, 0x00, 0x00,
754 0x2B, 0x2B, 0x2B
755};
756
757/*
758 SYSTEM INFORMATION TYPE 5
759 Neighbour Cells Description
760 EXT-IND: Carries the complete BA
761 BA-IND = 0
762 Format-ID bit map 0
763 CA-ARFCN Bit 124...001 (Hex): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
764*/
765
766static u_int8_t si5[] = {
767 /* header without l2 len*/0x06, 0x1D,
768 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
769 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
770};
771
772// SYSTEM INFORMATION TYPE 6
773
774/*
775SACCH FILLING
776 System Info Type: SYSTEM INFORMATION 6
777 L3 Information (Hex): 06 1E 00 01 xx xx 10 00 01 28 FF
778
779SYSTEM INFORMATION TYPE 6
780 Cell identity = 00001 (1h)
781 Location area identification
782 Mobile Country Code (MCC): 001
783 Mobile Network Code (MNC): 01
784 Location Area Code (LAC): 00001 (1h)
785 Cell Options SACCH
786 Power control indicator: not set
787 MSs shall not use uplink DTX on a TCH-F. MS shall not use uplink DTX on TCH-H.
788 Radio link timeout = 36
789 NCC permitted (NCC) = FF
790*/
791
792static u_int8_t si6[] = {
793 /* header */0x06, 0x1E,
794 /* cell id*/ 0x00, 0x01,
795 /* lai */ 0x00, 0xF1, 0x10, 0x00, 0x01,
796 /* options */ 0x28,
797 /* ncc */ 0xFF,
798};
799
800
801
802static const struct bcch_info bcch_infos[] = {
803 {
804 .type = RSL_SYSTEM_INFO_1,
805 .len = sizeof(si1),
806 .data = si1,
807 }, {
808 .type = RSL_SYSTEM_INFO_2,
809 .len = sizeof(si2),
810 .data = si2,
811 }, {
812 .type = RSL_SYSTEM_INFO_3,
813 .len = sizeof(si3),
814 .data = si3,
815 }, {
816 .type = RSL_SYSTEM_INFO_4,
817 .len = sizeof(si4),
818 .data = si4,
819 },
820};
821
822static_assert(sizeof(si1) == sizeof(struct gsm48_system_information_type_1), type1)
823static_assert(sizeof(si2) == sizeof(struct gsm48_system_information_type_2), type2)
824static_assert(sizeof(si3) == sizeof(struct gsm48_system_information_type_3), type3)
825static_assert(sizeof(si4) >= sizeof(struct gsm48_system_information_type_4), type4)
826static_assert(sizeof(si5) == sizeof(struct gsm48_system_information_type_5), type5)
827static_assert(sizeof(si6) >= sizeof(struct gsm48_system_information_type_6), type6)
828
829/* set all system information types */
830static int set_system_infos(struct gsm_bts_trx *trx)
831{
832 int i;
833
834 for (i = 0; i < ARRAY_SIZE(bcch_infos); i++) {
835 rsl_bcch_info(trx, bcch_infos[i].type,
836 bcch_infos[i].data,
837 bcch_infos[i].len);
838 }
839 rsl_sacch_filling(trx, RSL_SYSTEM_INFO_5, si5, sizeof(si5));
840 rsl_sacch_filling(trx, RSL_SYSTEM_INFO_6, si6, sizeof(si6));
841
842 return 0;
843}
844
845/*
846 * Patch the various SYSTEM INFORMATION tables to update
847 * the LAI
848 */
849static void patch_tables(struct gsm_bts *bts)
850{
Harald Weltee712a5f2009-06-21 16:17:15 +0200851 u_int8_t arfcn_low = bts->c0->arfcn & 0xff;
852 u_int8_t arfcn_high = (bts->c0->arfcn >> 8) & 0x0f;
Harald Welte59b04682009-06-10 05:40:52 +0800853 /* covert the raw packet to the struct */
854 struct gsm48_system_information_type_3 *type_3 =
855 (struct gsm48_system_information_type_3*)&si3;
856 struct gsm48_system_information_type_4 *type_4 =
857 (struct gsm48_system_information_type_4*)&si4;
858 struct gsm48_system_information_type_6 *type_6 =
859 (struct gsm48_system_information_type_6*)&si6;
860 struct gsm48_loc_area_id lai;
861
862 gsm0408_generate_lai(&lai, bts->network->country_code,
863 bts->network->network_code,
864 bts->location_area_code);
865
866 /* assign the MCC and MNC */
867 type_3->lai = lai;
868 type_4->lai = lai;
869 type_6->lai = lai;
870
871 /* patch ARFCN into BTS Attributes */
Harald Weltea4b54d82009-06-20 10:42:17 +0200872 bs11_attr_bts[69] &= 0xf0;
873 bs11_attr_bts[69] |= arfcn_high;
874 bs11_attr_bts[70] = arfcn_low;
Harald Welte59b04682009-06-10 05:40:52 +0800875 nanobts_attr_bts[42] &= 0xf0;
876 nanobts_attr_bts[42] |= arfcn_high;
877 nanobts_attr_bts[43] = arfcn_low;
878
879 /* patch ARFCN into TRX Attributes */
Harald Weltea4b54d82009-06-20 10:42:17 +0200880 bs11_attr_radio[2] &= 0xf0;
881 bs11_attr_radio[2] |= arfcn_high;
882 bs11_attr_radio[3] = arfcn_low;
Harald Welte59b04682009-06-10 05:40:52 +0800883 nanobts_attr_radio[5] &= 0xf0;
884 nanobts_attr_radio[5] |= arfcn_high;
885 nanobts_attr_radio[6] = arfcn_low;
886
887 type_4->data[2] &= 0xf0;
888 type_4->data[2] |= arfcn_high;
889 type_4->data[3] = arfcn_low;
890
891 /* patch Control Channel Description 10.5.2.11 */
892 type_3->control_channel_desc = bts->chan_desc;
893
894 /* patch BSIC */
Harald Weltea4b54d82009-06-20 10:42:17 +0200895 bs11_attr_bts[1] = bts->bsic;
Harald Welte59b04682009-06-10 05:40:52 +0800896 nanobts_attr_bts[sizeof(nanobts_attr_bts)-1] = bts->bsic;
897}
898
899
900static void bootstrap_rsl(struct gsm_bts_trx *trx)
901{
902 fprintf(stdout, "bootstrapping RSL for BTS/TRX (%u/%u) "
903 "using MCC=%u MNC=%u\n", trx->nr, trx->bts->nr, MCC, MNC);
904 set_system_infos(trx);
905}
906
907void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
908{
909 switch (event) {
910 case EVT_E1_TEI_UP:
911 switch (type) {
912 case E1INP_SIGN_OML:
913 bootstrap_om(trx->bts);
914 break;
915 case E1INP_SIGN_RSL:
916 bootstrap_rsl(trx);
917 break;
918 default:
919 break;
920 }
921 break;
922 case EVT_E1_TEI_DN:
923 fprintf(stderr, "Lost some E1 TEI link\n");
924 /* FIXME: deal with TEI or L1 link loss */
925 break;
926 default:
927 break;
928 }
929}
930
931static int bootstrap_bts(struct gsm_bts *bts)
932{
Harald Welte1d50e082009-06-20 18:15:19 +0200933 bts->band = BAND;
Harald Welte59b04682009-06-10 05:40:52 +0800934 bts->location_area_code = LAC;
Harald Weltee712a5f2009-06-21 16:17:15 +0200935 bts->c0->arfcn = ARFCN;
Harald Welte59b04682009-06-10 05:40:52 +0800936
937 /* Control Channel Description */
938 memset(&bts->chan_desc, 0, sizeof(struct gsm48_control_channel_descr));
939 bts->chan_desc.att = 1;
940 bts->chan_desc.ccch_conf = RSL_BCCH_CCCH_CONF_1_C;
941 bts->chan_desc.bs_pa_mfrms = RSL_BS_PA_MFRMS_5;
942 bts->chan_desc.t3212 = 0;
943
944 patch_tables(bts);
945
946 paging_init(bts);
947
948 if (bts->type == GSM_BTS_TYPE_BS11) {
Harald Weltee712a5f2009-06-21 16:17:15 +0200949 struct gsm_bts_trx *trx = bts->c0;
Harald Welte59b04682009-06-10 05:40:52 +0800950 set_ts_e1link(&trx->ts[0], 0, 1, 0xff);
951 set_ts_e1link(&trx->ts[1], 0, 2, 1);
952 set_ts_e1link(&trx->ts[2], 0, 2, 2);
953 set_ts_e1link(&trx->ts[3], 0, 2, 3);
954 set_ts_e1link(&trx->ts[4], 0, 3, 0);
955 set_ts_e1link(&trx->ts[5], 0, 3, 1);
956 set_ts_e1link(&trx->ts[6], 0, 3, 2);
957 set_ts_e1link(&trx->ts[7], 0, 3, 3);
958#ifdef HAVE_TRX1
959 /* TRX 1 */
960 trx = &bts->trx[1];
961 set_ts_e1link(&trx->ts[0], 0, 1, 0xff);
962 set_ts_e1link(&trx->ts[1], 0, 2, 1);
963 set_ts_e1link(&trx->ts[2], 0, 2, 2);
964 set_ts_e1link(&trx->ts[3], 0, 2, 3);
965 set_ts_e1link(&trx->ts[4], 0, 3, 0);
966 set_ts_e1link(&trx->ts[5], 0, 3, 1);
967 set_ts_e1link(&trx->ts[6], 0, 3, 2);
968 set_ts_e1link(&trx->ts[7], 0, 3, 3);
969#endif
970 }
971
972 return 0;
973}
974
975static int bootstrap_network(void)
976{
977 struct gsm_bts *bts;
978
Holger Hans Peter Freytheref5f4182009-06-10 10:20:16 +0200979 switch(BTS_TYPE) {
980 case GSM_BTS_TYPE_NANOBTS_1800:
981 if (ARFCN < 512 || ARFCN > 885) {
982 fprintf(stderr, "GSM1800 channel must be between 512-885.\n");
983 return -EINVAL;
984 }
985 break;
986 case GSM_BTS_TYPE_BS11:
987 case GSM_BTS_TYPE_NANOBTS_900:
988 /* Assume we have a P-GSM900 here */
989 if (ARFCN < 1 || ARFCN > 124) {
990 fprintf(stderr, "GSM900 channel must be between 1-124.\n");
991 return -EINVAL;
992 }
993 break;
994 case GSM_BTS_TYPE_UNKNOWN:
995 fprintf(stderr, "Unknown BTS. Please use the --bts-type switch\n");
996 return -EINVAL;
997 }
998
Harald Welte59b04682009-06-10 05:40:52 +0800999 /* initialize our data structures */
Harald Weltee712a5f2009-06-21 16:17:15 +02001000 gsmnet = gsm_network_init(MCC, MNC, mncc_recv);
Harald Welte59b04682009-06-10 05:40:52 +08001001 if (!gsmnet)
1002 return -ENOMEM;
1003
1004 gsmnet->name_long = "OpenBSC";
1005 gsmnet->name_short = "OpenBSC";
1006
Harald Weltee712a5f2009-06-21 16:17:15 +02001007 bts = gsm_bts_alloc(gsmnet, BTS_TYPE, HARDCODED_TSC, HARDCODED_BSIC);
Harald Welte59b04682009-06-10 05:40:52 +08001008 bootstrap_bts(bts);
1009
1010 if (db_init(database_name)) {
1011 printf("DB: Failed to init database. Please check the option settings.\n");
1012 return -1;
1013 }
1014 printf("DB: Database initialized.\n");
1015
1016 if (db_prepare()) {
1017 printf("DB: Failed to prepare database.\n");
1018 return -1;
1019 }
1020 printf("DB: Database prepared.\n");
1021
1022 telnet_init(gsmnet, 4242);
1023
1024 register_signal_handler(SS_NM, nm_sig_cb, NULL);
1025
1026 /* E1 mISDN input setup */
1027 if (BTS_TYPE == GSM_BTS_TYPE_BS11) {
1028 gsmnet->num_bts = 1;
1029 return e1_config(bts, cardnr, release_l2);
1030 } else {
1031 /* FIXME: do this dynamic */
1032 bts->ip_access.site_id = 1801;
1033 bts->ip_access.bts_id = 0;
Harald Welte1d50e082009-06-20 18:15:19 +02001034
Harald Weltee712a5f2009-06-21 16:17:15 +02001035 bts = gsm_bts_alloc(gsmnet, BTS_TYPE, HARDCODED_TSC, HARDCODED_BSIC);
Harald Welte59b04682009-06-10 05:40:52 +08001036 bootstrap_bts(bts);
1037 bts->ip_access.site_id = 1800;
1038 bts->ip_access.bts_id = 0;
1039 return ipaccess_setup(gsmnet);
1040 }
1041}
1042
1043static void create_pcap_file(char *file)
1044{
1045 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1046 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
1047
1048 if (fd < 0) {
1049 perror("Failed to open file for pcap");
1050 return;
1051 }
1052
1053 e1_set_pcap_fd(fd);
1054}
1055
1056static void print_usage()
1057{
1058 printf("Usage: bsc_hack\n");
1059}
1060
1061static void print_help()
1062{
1063 printf(" Some useful help...\n");
1064 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
1065 printf(" -s --disable-color\n");
1066 printf(" -n --network-code number(MNC) \n");
1067 printf(" -c --country-code number (MCC) \n");
1068 printf(" -L --location-area-code number (LAC) \n");
1069 printf(" -f --arfcn number The frequency ARFCN\n");
1070 printf(" -l --database db-name The database to use\n");
1071 printf(" -a --authorize-everyone Allow everyone into the network.\n");
1072 printf(" -r --reject-cause number The reject cause for LOCATION UPDATING REJECT.\n");
1073 printf(" -p --pcap file The filename of the pcap file\n");
1074 printf(" -t --bts-type type The BTS type (bs11, nanobts900, nanobts1800)\n");
1075 printf(" -C --cardnr number For bs11 select E1 card number other than 0\n");
1076 printf(" -R --release-l2 Releases mISDN layer 2 after exit, to unload driver.\n");
1077 printf(" -h --help this text\n");
1078}
1079
1080static void handle_options(int argc, char** argv)
1081{
1082 while (1) {
Harald Welte1d50e082009-06-20 18:15:19 +02001083 int tmp, option_index = 0, c;
Harald Welte59b04682009-06-10 05:40:52 +08001084 static struct option long_options[] = {
1085 {"help", 0, 0, 'h'},
1086 {"debug", 1, 0, 'd'},
1087 {"disable-color", 0, 0, 's'},
1088 {"network-code", 1, 0, 'n'},
1089 {"country-code", 1, 0, 'c'},
1090 {"location-area-code", 1, 0, 'L'},
1091 {"database", 1, 0, 'l'},
1092 {"authorize-everyone", 0, 0, 'a'},
1093 {"reject-cause", 1, 0, 'r'},
1094 {"pcap", 1, 0, 'p'},
1095 {"arfcn", 1, 0, 'f'},
1096 {"bts-type", 1, 0, 't'},
1097 {"cardnr", 1, 0, 'C'},
1098 {"release-l2", 0, 0, 'R'},
1099 {"timestamp", 0, 0, 'T'},
Harald Welte1d50e082009-06-20 18:15:19 +02001100 {"band", 0, 0, 'b'},
Harald Welte59b04682009-06-10 05:40:52 +08001101 {0, 0, 0, 0}
1102 };
1103
Harald Welte1d50e082009-06-20 18:15:19 +02001104 c = getopt_long(argc, argv, "hc:n:d:sar:p:f:t:C:RL:l:Tb:",
Harald Welte59b04682009-06-10 05:40:52 +08001105 long_options, &option_index);
1106 if (c == -1)
1107 break;
1108
1109 switch (c) {
1110 case 'h':
1111 print_usage();
1112 print_help();
1113 exit(0);
1114 case 's':
1115 debug_use_color(0);
1116 break;
1117 case 'd':
1118 debug_parse_category_mask(optarg);
1119 break;
1120 case 'n':
1121 MNC = atoi(optarg);
1122 break;
1123 case 'c':
1124 MCC = atoi(optarg);
1125 break;
1126 case 'L':
1127 LAC = atoi(optarg);
1128 break;
1129 case 'f':
1130 ARFCN = atoi(optarg);
1131 break;
1132 case 'l':
1133 database_name = strdup(optarg);
1134 break;
1135 case 'a':
1136 gsm0408_allow_everyone(1);
1137 break;
1138 case 'r':
1139 gsm0408_set_reject_cause(atoi(optarg));
1140 break;
1141 case 'p':
1142 create_pcap_file(optarg);
1143 break;
1144 case 't':
1145 BTS_TYPE = parse_btstype(optarg);
1146 break;
1147 case 'C':
1148 cardnr = atoi(optarg);
1149 break;
1150 case 'R':
1151 release_l2 = 1;
1152 break;
1153 case 'T':
1154 debug_timestamp(1);
1155 break;
Harald Welte1d50e082009-06-20 18:15:19 +02001156 case 'b':
1157 BAND = gsm_band_parse(atoi(optarg));
1158 break;
Harald Welte59b04682009-06-10 05:40:52 +08001159 default:
1160 /* ignore */
1161 break;
1162 }
1163 }
1164}
1165
1166static void signal_handler(int signal)
1167{
1168 fprintf(stdout, "signal %u received\n", signal);
1169
1170 switch (signal) {
1171 case SIGHUP:
1172 case SIGABRT:
1173 shutdown_net(gsmnet);
1174 break;
Harald Weltea8379772009-06-20 22:36:41 +02001175 case SIGUSR1:
1176 talloc_report_full(tall_bsc_ctx, stderr);
1177 break;
Harald Welte59b04682009-06-10 05:40:52 +08001178 default:
1179 break;
1180 }
1181}
1182
1183int main(int argc, char **argv)
1184{
1185 int rc;
1186
Harald Weltea8379772009-06-20 22:36:41 +02001187 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
1188
Harald Welte59b04682009-06-10 05:40:52 +08001189 /* parse options */
1190 handle_options(argc, argv);
1191
1192 /* seed the PRNG */
1193 srand(time(NULL));
1194
1195 rc = bootstrap_network();
1196 if (rc < 0)
1197 exit(1);
1198
1199 signal(SIGHUP, &signal_handler);
1200 signal(SIGABRT, &signal_handler);
Harald Weltea8379772009-06-20 22:36:41 +02001201 signal(SIGUSR1, &signal_handler);
Harald Welte59b04682009-06-10 05:40:52 +08001202
1203 while (1) {
Harald Welte03740842009-06-10 23:11:52 +08001204 bsc_upqueue(gsmnet);
Harald Welte59b04682009-06-10 05:40:52 +08001205 bsc_select_main(0);
1206 }
1207}