blob: e96b1abacef5f9398c0ab3bc001d0294154bccb1 [file] [log] [blame]
Harald Welte25de9912009-04-30 15:53:07 +00001/* ip.access nanoBTS configuration tool */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherf413a082009-12-28 03:11:33 +01004 * (C) 2009 by Holger Hans Peter Freyther
5 * (C) 2009 by On Waves
Harald Welte25de9912009-04-30 15:53:07 +00006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <unistd.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <getopt.h>
29#include <sys/types.h>
30
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34
35
36#include <openbsc/select.h>
37#include <openbsc/timer.h>
38#include <openbsc/ipaccess.h>
39#include <openbsc/gsm_data.h>
40#include <openbsc/e1_input.h>
41#include <openbsc/abis_nm.h>
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +020042#include <openbsc/signal.h>
Harald Weltec7310382009-08-08 00:02:36 +020043#include <openbsc/debug.h>
Harald Welte25de9912009-04-30 15:53:07 +000044
45static struct gsm_network *gsmnet;
46
Harald Weltef1676e52009-08-10 11:39:47 +020047static int net_listen_testnr;
Harald Welte25de9912009-04-30 15:53:07 +000048static int restart;
49static char *prim_oml_ip;
50static char *unit_id;
Harald Welte684b1a82009-07-03 11:26:45 +020051static u_int16_t nv_flags;
52static u_int16_t nv_mask;
Holger Hans Peter Freytherf413a082009-12-28 03:11:33 +010053static char *software = NULL;
Harald Welte25de9912009-04-30 15:53:07 +000054
55/*
56static u_int8_t prim_oml_attr[] = { 0x95, 0x00, 7, 0x88, 192, 168, 100, 11, 0x00, 0x00 };
57static u_int8_t unit_id_attr[] = { 0x91, 0x00, 9, '2', '3', '4', '2', '/' , '0', '/', '0', 0x00 };
58*/
59
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +020060/*
61 * Callback function for NACK on the OML NM
62 *
63 * Currently we send the config requests but don't check the
64 * result. The nanoBTS will send us a NACK when we did something the
65 * BTS didn't like.
66 */
Harald Welted8cfc902009-11-17 06:09:56 +010067static int ipacc_msg_nack(u_int8_t mt)
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +020068{
69 fprintf(stderr, "Failure to set attribute. This seems fatal\n");
70 exit(-1);
71 return 0;
72}
73
Harald Weltec7310382009-08-08 00:02:36 +020074struct ipacc_ferr_elem {
75 int16_t freq_err;
76 u_int8_t freq_qual;
77 u_int8_t arfcn;
78} __attribute__((packed));
79
Harald Weltef1676e52009-08-10 11:39:47 +020080struct ipacc_cusage_elem {
81 u_int16_t arfcn:10,
82 rxlev:6;
83} __attribute__ ((packed));
84
Harald Weltec7310382009-08-08 00:02:36 +020085static int test_rep(void *_msg)
86{
87 struct msgb *msg = _msg;
88 struct abis_om_fom_hdr *foh = msgb_l3(msg);
89 u_int16_t test_rep_len, ferr_list_len;
90 struct ipacc_ferr_elem *ife;
Harald Weltecf01b8f2009-11-13 14:43:15 +010091 struct ipac_bcch_info binfo;
92 int i, rc;
Harald Weltec7310382009-08-08 00:02:36 +020093
94 DEBUGP(DNM, "TEST REPORT: ");
95
96 if (foh->data[0] != NM_ATT_TEST_NO ||
97 foh->data[2] != NM_ATT_TEST_REPORT)
98 return -EINVAL;
99
100 DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]);
101 /* data[2] == NM_ATT_TEST_REPORT */
102 /* data[3..4]: test_rep_len */
103 test_rep_len = ntohs(*(u_int16_t *) &foh->data[3]);
104 /* data[5]: ip.access test result */
Harald Weltef1676e52009-08-10 11:39:47 +0200105 DEBUGPC(DNM, "test_res=%s\n", ipacc_testres_name(foh->data[5]));
Harald Weltec7310382009-08-08 00:02:36 +0200106
107 /* data[6]: ip.access nested IE. 3 == freq_err_list */
108 switch (foh->data[6]) {
Harald Weltecf01b8f2009-11-13 14:43:15 +0100109 case NM_IPAC_EIE_FREQ_ERR_LIST:
Harald Weltec7310382009-08-08 00:02:36 +0200110 /* data[7..8]: length of ferr_list */
111 ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]);
112
113 /* data[9...]: frequency error list elements */
114 for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) {
115 ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i);
116 DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n",
117 ife->arfcn, ntohs(ife->freq_err));
118 }
119 break;
Harald Weltecf01b8f2009-11-13 14:43:15 +0100120 case NM_IPAC_EIE_CHAN_USE_LIST:
Harald Weltef1676e52009-08-10 11:39:47 +0200121 /* data[7..8]: length of ferr_list */
122 ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]);
123
124 /* data[9...]: channel usage list elements */
125 for (i = 0; i < ferr_list_len; i+= 2) {
126 u_int16_t *cu_ptr = (u_int16_t *)(foh->data + 9 + i);
127 u_int16_t cu = ntohs(*cu_ptr);
128 DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n",
129 cu & 0x3ff, cu >> 10);
130 }
131 break;
Harald Weltecf01b8f2009-11-13 14:43:15 +0100132 case NM_IPAC_EIE_BCCH_INFO_TYPE:
133 break;
134 case NM_IPAC_EIE_BCCH_INFO:
135 rc = ipac_parse_bcch_info(&binfo, foh->data+6);
136 if (rc < 0) {
137 DEBUGP(DNM, "BCCH Info parsing failed\n");
138 break;
139 }
140 DEBUGP(DNM, "==> ARFCN %u, RxLev %2u, RxQual %2u: %3d-%d, LAC %d CI %d\n",
141 binfo.arfcn, binfo.rx_lev, binfo.rx_qual,
142 binfo.cgi.mcc, binfo.cgi.mnc,
143 binfo.cgi.lac, binfo.cgi.ci);
144 break;
Harald Weltec7310382009-08-08 00:02:36 +0200145 default:
146 break;
147 }
148
149 return 0;
150}
151
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +0200152static int nm_sig_cb(unsigned int subsys, unsigned int signal,
153 void *handler_data, void *signal_data)
154{
Harald Welted8cfc902009-11-17 06:09:56 +0100155 u_int8_t *msg_type;
156
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +0200157 switch (signal) {
158 case S_NM_IPACC_NACK:
Harald Welted8cfc902009-11-17 06:09:56 +0100159 msg_type = signal_data;
160 return ipacc_msg_nack(*msg_type);
Harald Weltec7310382009-08-08 00:02:36 +0200161 case S_NM_TEST_REP:
162 return test_rep(signal_data);
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +0200163 default:
164 break;
165 }
166
167 return 0;
168}
169
Holger Hans Peter Freytherf413a082009-12-28 03:11:33 +0100170/* callback function passed to the ABIS OML code */
171static int percent;
172static int percent_old;
173static int swload_cbfn(unsigned int hook, unsigned int event, struct msgb *msg,
174 void *data, void *param)
175{
176 struct gsm_bts *bts;
177
178 if (hook != GSM_HOOK_NM_SWLOAD)
179 return 0;
180
181 bts = (struct gsm_bts *) data;
182
183 switch (event) {
184 case NM_MT_LOAD_INIT_ACK:
185 fprintf(stdout, "Software Load Initiate ACK\n");
186 break;
187 case NM_MT_LOAD_INIT_NACK:
188 fprintf(stderr, "ERROR: Software Load Initiate NACK\n");
189 exit(5);
190 break;
191 case NM_MT_LOAD_END_ACK:
192 fprintf(stderr, "LOAD END ACK...");
193#if 0
194 if (data) {
195 /* we did a safety load and must activate it */
196 abis_nm_software_activate(g_bts, fname_safety,
197 swload_cbfn, bts);
198 sleep(5);
199 }
200#endif
201 break;
202 case NM_MT_LOAD_END_NACK:
203 fprintf(stderr, "ERROR: Software Load End NACK\n");
204 exit(3);
205 break;
206 case NM_MT_ACTIVATE_SW_NACK:
207 fprintf(stderr, "ERROR: Activate Software NACK\n");
208 exit(4);
209 break;
210 case NM_MT_ACTIVATE_SW_ACK:
211 break;
212 case NM_MT_LOAD_SEG_ACK:
213 percent = abis_nm_software_load_status(bts);
214 if (percent > percent_old)
215 printf("Software Download Progress: %d%%\n", percent);
216 percent_old = percent;
217 break;
Holger Hans Peter Freytherc7aabca2009-12-28 12:23:02 +0100218 case NM_MT_LOAD_ABORT:
219 fprintf(stderr, "ERROR: Load aborted by the BTS.\n");
220 exit(6);
221 break;
Holger Hans Peter Freytherf413a082009-12-28 03:11:33 +0100222 }
223 return 0;
224}
225
Harald Welte25de9912009-04-30 15:53:07 +0000226static void bootstrap_om(struct gsm_bts *bts)
227{
228 int len;
229 static u_int8_t buf[1024];
Harald Welte684b1a82009-07-03 11:26:45 +0200230 u_int8_t *cur = buf;
Harald Welte25de9912009-04-30 15:53:07 +0000231
232 printf("OML link established\n");
233
234 if (unit_id) {
235 len = strlen(unit_id);
236 if (len > sizeof(buf)-10)
237 return;
238 buf[0] = NM_ATT_IPACC_UNIT_ID;
239 buf[1] = (len+1) >> 8;
240 buf[2] = (len+1) & 0xff;
241 memcpy(buf+3, unit_id, len);
242 buf[3+len] = 0;
243 printf("setting Unit ID to '%s'\n", unit_id);
244 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len+1);
245 }
246 if (prim_oml_ip) {
247 struct in_addr ia;
Harald Welte25de9912009-04-30 15:53:07 +0000248
249 if (!inet_aton(prim_oml_ip, &ia)) {
250 fprintf(stderr, "invalid IP address: %s\n",
251 prim_oml_ip);
252 return;
253 }
254
255 /* 0x88 + IP + port */
256 len = 1 + sizeof(ia) + 2;
257
Harald Welte0efe9b72009-07-12 09:33:54 +0200258 *cur++ = NM_ATT_IPACC_PRIM_OML_CFG_LIST;
Harald Welte25de9912009-04-30 15:53:07 +0000259 *cur++ = (len) >> 8;
260 *cur++ = (len) & 0xff;
261 *cur++ = 0x88;
262 memcpy(cur, &ia, sizeof(ia));
263 cur += sizeof(ia);
264 *cur++ = 0;
265 *cur++ = 0;
Harald Welte4802b882009-04-30 16:23:45 +0000266 printf("setting primary OML link IP to '%s'\n", inet_ntoa(ia));
Harald Welte25de9912009-04-30 15:53:07 +0000267 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
268 }
Harald Welte684b1a82009-07-03 11:26:45 +0200269 if (nv_mask) {
270 len = 4;
271
272 *cur++ = NM_ATT_IPACC_NV_FLAGS;
273 *cur++ = (len) >> 8;
274 *cur++ = (len) & 0xff;
275 *cur++ = nv_flags & 0xff;
276 *cur++ = nv_mask & 0xff;
277 *cur++ = nv_flags >> 8;
278 *cur++ = nv_mask >> 8;
279 printf("setting NV Flags/Mask to 0x%04x/0x%04x\n",
280 nv_flags, nv_mask);
281 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
282 }
Harald Welte25de9912009-04-30 15:53:07 +0000283
284 if (restart) {
285 printf("restarting BTS\n");
286 abis_nm_ipaccess_restart(bts);
287 }
Holger Hans Peter Freytherf413a082009-12-28 03:11:33 +0100288
Harald Welte25de9912009-04-30 15:53:07 +0000289}
290
291void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
292{
293 switch (event) {
294 case EVT_E1_TEI_UP:
295 switch (type) {
296 case E1INP_SIGN_OML:
297 bootstrap_om(trx->bts);
298 break;
299 case E1INP_SIGN_RSL:
300 /* FIXME */
301 break;
302 default:
303 break;
304 }
305 break;
306 case EVT_E1_TEI_DN:
307 fprintf(stderr, "Lost some E1 TEI link\n");
308 /* FIXME: deal with TEI or L1 link loss */
309 break;
310 default:
311 break;
312 }
313}
314
315int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
316 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
317{
Harald Weltec7310382009-08-08 00:02:36 +0200318 if (evt == EVT_STATECHG_OPER &&
319 obj_class == NM_OC_RADIO_CARRIER &&
Holger Hans Peter Freytherab2c8b12009-12-28 07:27:43 +0100320 new_state->availability == 3) {
Harald Weltec7310382009-08-08 00:02:36 +0200321 struct gsm_bts_trx *trx = obj;
Holger Hans Peter Freytherab2c8b12009-12-28 07:27:43 +0100322
323 if (net_listen_testnr) {
324 u_int8_t phys_config[] = { 0x02, 0x0a, 0x00, 0x01, 0x02 };
325 abis_nm_perform_test(trx->bts, 2, 0, 0, 0xff,
326 net_listen_testnr, 1,
327 phys_config, sizeof(phys_config));
328 } else if (software) {
329 int rc;
330 printf("Attempting software upload with '%s'\n", software);
331 rc = abis_nm_software_load(trx->bts, software, 19, 0, swload_cbfn, trx->bts);
332 if (rc < 0) {
333 fprintf(stderr, "Failed to start software load\n");
334 exit(-3);
335 }
336 }
Harald Weltec7310382009-08-08 00:02:36 +0200337 }
Harald Welte25de9912009-04-30 15:53:07 +0000338 return 0;
339}
340
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200341static void print_usage(void)
342{
343 printf("Usage: ipaccess-config\n");
344}
345
346static void print_help(void)
347{
348 printf(" -u --unit-id UNIT_ID\n");
349 printf(" -o --oml-ip ip\n");
350 printf(" -r --restart\n");
Harald Weltec7310382009-08-08 00:02:36 +0200351 printf(" -n flags/mask\tSet NVRAM attributes.\n");
Holger Hans Peter Freyther057fec52009-11-16 19:53:08 +0100352 printf(" -l --listen testnr \tPerform specified test number\n");
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200353 printf(" -h --help this text\n");
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100354 printf(" -s --stream-id ID\n");
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200355}
356
Harald Welte25de9912009-04-30 15:53:07 +0000357int main(int argc, char **argv)
358{
359 struct gsm_bts *bts;
360 struct sockaddr_in sin;
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100361 int rc, option_index = 0, stream_id = 0xff;
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100362 struct debug_target *stderr_target;
363
364 debug_init();
365 stderr_target = debug_target_create_stderr();
366 debug_add_target(stderr_target);
367 debug_set_all_filter(stderr_target, 1);
Harald Welte25de9912009-04-30 15:53:07 +0000368
369 printf("ipaccess-config (C) 2009 by Harald Welte\n");
370 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
371
372 while (1) {
373 int c;
Harald Welte684b1a82009-07-03 11:26:45 +0200374 unsigned long ul;
375 char *slash;
Harald Welte25de9912009-04-30 15:53:07 +0000376 static struct option long_options[] = {
377 { "unit-id", 1, 0, 'u' },
378 { "oml-ip", 1, 0, 'o' },
379 { "restart", 0, 0, 'r' },
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200380 { "help", 0, 0, 'h' },
Harald Weltef1676e52009-08-10 11:39:47 +0200381 { "listen", 1, 0, 'l' },
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100382 { "stream-id", 1, 0, 's' },
Holger Hans Peter Freytherf413a082009-12-28 03:11:33 +0100383 { "software", 1, 0, 'd' },
Harald Welte25de9912009-04-30 15:53:07 +0000384 };
385
Holger Hans Peter Freytherf413a082009-12-28 03:11:33 +0100386 c = getopt_long(argc, argv, "u:o:rn:l:hs:d:", long_options,
Harald Welte25de9912009-04-30 15:53:07 +0000387 &option_index);
388
389 if (c == -1)
390 break;
391
392 switch (c) {
393 case 'u':
394 unit_id = optarg;
395 break;
396 case 'o':
397 prim_oml_ip = optarg;
398 break;
399 case 'r':
400 restart = 1;
401 break;
Harald Welte684b1a82009-07-03 11:26:45 +0200402 case 'n':
403 slash = strchr(optarg, '/');
404 if (!slash)
405 exit(2);
406 ul = strtoul(optarg, NULL, 16);
407 nv_flags = ul & 0xffff;
408 ul = strtoul(slash+1, NULL, 16);
409 nv_mask = ul & 0xffff;
410 break;
Harald Weltec7310382009-08-08 00:02:36 +0200411 case 'l':
Harald Weltef1676e52009-08-10 11:39:47 +0200412 net_listen_testnr = atoi(optarg);
Harald Weltec7310382009-08-08 00:02:36 +0200413 break;
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100414 case 's':
415 stream_id = atoi(optarg);
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100416 break;
Holger Hans Peter Freytherf413a082009-12-28 03:11:33 +0100417 case 'd':
418 software = strdup(optarg);
419 break;
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200420 case 'h':
421 print_usage();
422 print_help();
423 exit(0);
Harald Welte25de9912009-04-30 15:53:07 +0000424 }
425 };
426
427 if (optind >= argc) {
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200428 fprintf(stderr, "you have to specify the IP address of the BTS. Use --help for more information\n");
Harald Welte25de9912009-04-30 15:53:07 +0000429 exit(2);
430 }
431
Harald Weltee441d9c2009-06-21 16:17:15 +0200432 gsmnet = gsm_network_init(1, 1, NULL);
Harald Welte25de9912009-04-30 15:53:07 +0000433 if (!gsmnet)
434 exit(1);
435
Mike Habene2d82272009-10-02 12:19:34 +0100436 bts = gsm_bts_alloc(gsmnet, GSM_BTS_TYPE_NANOBTS, HARDCODED_TSC,
Harald Weltee441d9c2009-06-21 16:17:15 +0200437 HARDCODED_BSIC);
Holger Hans Peter Freyther55aac6e2009-10-27 03:41:09 +0100438 bts->oml_tei = stream_id;
Harald Welte25de9912009-04-30 15:53:07 +0000439
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +0200440 register_signal_handler(SS_NM, nm_sig_cb, NULL);
Harald Welte25de9912009-04-30 15:53:07 +0000441 printf("Trying to connect to ip.access BTS ...\n");
442
443 memset(&sin, 0, sizeof(sin));
444 sin.sin_family = AF_INET;
445 inet_aton(argv[optind], &sin.sin_addr);
446 rc = ia_config_connect(bts, &sin);
447 if (rc < 0) {
448 perror("Error connecting to the BTS");
449 exit(1);
450 }
451
452 while (1) {
Harald Welte04d3c922009-05-23 06:07:04 +0000453 rc = bsc_select_main(0);
Harald Welte25de9912009-04-30 15:53:07 +0000454 if (rc < 0)
455 exit(3);
456 }
457
458 exit(0);
459}
460