blob: 005c93dd5447ffcf101eef4a3c33dffd8ee8c9b3 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* ip.access nanoBTS configuration tool */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <unistd.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <getopt.h>
27#include <sys/types.h>
28
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32
33
34#include <openbsc/select.h>
35#include <openbsc/timer.h>
36#include <openbsc/ipaccess.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/e1_input.h>
39#include <openbsc/abis_nm.h>
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +020040#include <openbsc/signal.h>
Harald Welte0bf8e302009-08-08 00:02:36 +020041#include <openbsc/debug.h>
Harald Welte59b04682009-06-10 05:40:52 +080042
43static struct gsm_network *gsmnet;
44
Harald Welte12a721e2009-08-10 11:39:47 +020045static int net_listen_testnr;
Harald Welte59b04682009-06-10 05:40:52 +080046static int restart;
47static char *prim_oml_ip;
48static char *unit_id;
Harald Welte21460f02009-07-03 11:26:45 +020049static u_int16_t nv_flags;
50static u_int16_t nv_mask;
Harald Welte59b04682009-06-10 05:40:52 +080051
52/*
53static u_int8_t prim_oml_attr[] = { 0x95, 0x00, 7, 0x88, 192, 168, 100, 11, 0x00, 0x00 };
54static u_int8_t unit_id_attr[] = { 0x91, 0x00, 9, '2', '3', '4', '2', '/' , '0', '/', '0', 0x00 };
55*/
56
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +020057/*
58 * Callback function for NACK on the OML NM
59 *
60 * Currently we send the config requests but don't check the
61 * result. The nanoBTS will send us a NACK when we did something the
62 * BTS didn't like.
63 */
Harald Welte6a21c732009-11-17 06:09:56 +010064static int ipacc_msg_nack(u_int8_t mt)
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +020065{
66 fprintf(stderr, "Failure to set attribute. This seems fatal\n");
67 exit(-1);
68 return 0;
69}
70
Harald Welte0bf8e302009-08-08 00:02:36 +020071struct ipacc_ferr_elem {
72 int16_t freq_err;
73 u_int8_t freq_qual;
74 u_int8_t arfcn;
75} __attribute__((packed));
76
Harald Welte12a721e2009-08-10 11:39:47 +020077struct ipacc_cusage_elem {
78 u_int16_t arfcn:10,
79 rxlev:6;
80} __attribute__ ((packed));
81
Harald Welte0bf8e302009-08-08 00:02:36 +020082static int test_rep(void *_msg)
83{
84 struct msgb *msg = _msg;
85 struct abis_om_fom_hdr *foh = msgb_l3(msg);
86 u_int16_t test_rep_len, ferr_list_len;
87 struct ipacc_ferr_elem *ife;
Harald Weltec73d9832009-11-13 14:43:15 +010088 struct ipac_bcch_info binfo;
89 int i, rc;
Harald Welte0bf8e302009-08-08 00:02:36 +020090
91 DEBUGP(DNM, "TEST REPORT: ");
92
93 if (foh->data[0] != NM_ATT_TEST_NO ||
94 foh->data[2] != NM_ATT_TEST_REPORT)
95 return -EINVAL;
96
97 DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]);
98 /* data[2] == NM_ATT_TEST_REPORT */
99 /* data[3..4]: test_rep_len */
100 test_rep_len = ntohs(*(u_int16_t *) &foh->data[3]);
101 /* data[5]: ip.access test result */
Harald Welte12a721e2009-08-10 11:39:47 +0200102 DEBUGPC(DNM, "test_res=%s\n", ipacc_testres_name(foh->data[5]));
Harald Welte0bf8e302009-08-08 00:02:36 +0200103
104 /* data[6]: ip.access nested IE. 3 == freq_err_list */
105 switch (foh->data[6]) {
Harald Weltec73d9832009-11-13 14:43:15 +0100106 case NM_IPAC_EIE_FREQ_ERR_LIST:
Harald Welte0bf8e302009-08-08 00:02:36 +0200107 /* data[7..8]: length of ferr_list */
108 ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]);
109
110 /* data[9...]: frequency error list elements */
111 for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) {
112 ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i);
113 DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n",
114 ife->arfcn, ntohs(ife->freq_err));
115 }
116 break;
Harald Weltec73d9832009-11-13 14:43:15 +0100117 case NM_IPAC_EIE_CHAN_USE_LIST:
Harald Welte12a721e2009-08-10 11:39:47 +0200118 /* data[7..8]: length of ferr_list */
119 ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]);
120
121 /* data[9...]: channel usage list elements */
122 for (i = 0; i < ferr_list_len; i+= 2) {
123 u_int16_t *cu_ptr = (u_int16_t *)(foh->data + 9 + i);
124 u_int16_t cu = ntohs(*cu_ptr);
125 DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n",
126 cu & 0x3ff, cu >> 10);
127 }
128 break;
Harald Weltec73d9832009-11-13 14:43:15 +0100129 case NM_IPAC_EIE_BCCH_INFO_TYPE:
130 break;
131 case NM_IPAC_EIE_BCCH_INFO:
132 rc = ipac_parse_bcch_info(&binfo, foh->data+6);
133 if (rc < 0) {
134 DEBUGP(DNM, "BCCH Info parsing failed\n");
135 break;
136 }
137 DEBUGP(DNM, "==> ARFCN %u, RxLev %2u, RxQual %2u: %3d-%d, LAC %d CI %d\n",
138 binfo.arfcn, binfo.rx_lev, binfo.rx_qual,
139 binfo.cgi.mcc, binfo.cgi.mnc,
140 binfo.cgi.lac, binfo.cgi.ci);
141 break;
Harald Welte0bf8e302009-08-08 00:02:36 +0200142 default:
143 break;
144 }
145
146 return 0;
147}
148
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +0200149static int nm_sig_cb(unsigned int subsys, unsigned int signal,
150 void *handler_data, void *signal_data)
151{
Harald Welte6a21c732009-11-17 06:09:56 +0100152 u_int8_t *msg_type;
153
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +0200154 switch (signal) {
155 case S_NM_IPACC_NACK:
Harald Welte6a21c732009-11-17 06:09:56 +0100156 msg_type = signal_data;
157 return ipacc_msg_nack(*msg_type);
Harald Welte0bf8e302009-08-08 00:02:36 +0200158 case S_NM_TEST_REP:
159 return test_rep(signal_data);
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +0200160 default:
161 break;
162 }
163
164 return 0;
165}
166
Harald Welte59b04682009-06-10 05:40:52 +0800167static void bootstrap_om(struct gsm_bts *bts)
168{
169 int len;
170 static u_int8_t buf[1024];
Harald Welte21460f02009-07-03 11:26:45 +0200171 u_int8_t *cur = buf;
Harald Welte59b04682009-06-10 05:40:52 +0800172
173 printf("OML link established\n");
174
175 if (unit_id) {
176 len = strlen(unit_id);
177 if (len > sizeof(buf)-10)
178 return;
179 buf[0] = NM_ATT_IPACC_UNIT_ID;
180 buf[1] = (len+1) >> 8;
181 buf[2] = (len+1) & 0xff;
182 memcpy(buf+3, unit_id, len);
183 buf[3+len] = 0;
184 printf("setting Unit ID to '%s'\n", unit_id);
185 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len+1);
186 }
187 if (prim_oml_ip) {
188 struct in_addr ia;
Harald Welte59b04682009-06-10 05:40:52 +0800189
190 if (!inet_aton(prim_oml_ip, &ia)) {
191 fprintf(stderr, "invalid IP address: %s\n",
192 prim_oml_ip);
193 return;
194 }
195
196 /* 0x88 + IP + port */
197 len = 1 + sizeof(ia) + 2;
198
Harald Welte4206d982009-07-12 09:33:54 +0200199 *cur++ = NM_ATT_IPACC_PRIM_OML_CFG_LIST;
Harald Welte59b04682009-06-10 05:40:52 +0800200 *cur++ = (len) >> 8;
201 *cur++ = (len) & 0xff;
202 *cur++ = 0x88;
203 memcpy(cur, &ia, sizeof(ia));
204 cur += sizeof(ia);
205 *cur++ = 0;
206 *cur++ = 0;
207 printf("setting primary OML link IP to '%s'\n", inet_ntoa(ia));
208 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
209 }
Harald Welte21460f02009-07-03 11:26:45 +0200210 if (nv_mask) {
211 len = 4;
212
213 *cur++ = NM_ATT_IPACC_NV_FLAGS;
214 *cur++ = (len) >> 8;
215 *cur++ = (len) & 0xff;
216 *cur++ = nv_flags & 0xff;
217 *cur++ = nv_mask & 0xff;
218 *cur++ = nv_flags >> 8;
219 *cur++ = nv_mask >> 8;
220 printf("setting NV Flags/Mask to 0x%04x/0x%04x\n",
221 nv_flags, nv_mask);
222 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
223 }
Harald Welte59b04682009-06-10 05:40:52 +0800224
225 if (restart) {
226 printf("restarting BTS\n");
227 abis_nm_ipaccess_restart(bts);
228 }
229}
230
231void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
232{
233 switch (event) {
234 case EVT_E1_TEI_UP:
235 switch (type) {
236 case E1INP_SIGN_OML:
237 bootstrap_om(trx->bts);
238 break;
239 case E1INP_SIGN_RSL:
240 /* FIXME */
241 break;
242 default:
243 break;
244 }
245 break;
246 case EVT_E1_TEI_DN:
247 fprintf(stderr, "Lost some E1 TEI link\n");
248 /* FIXME: deal with TEI or L1 link loss */
249 break;
250 default:
251 break;
252 }
253}
254
255int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
256 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
257{
Harald Welte0bf8e302009-08-08 00:02:36 +0200258 if (evt == EVT_STATECHG_OPER &&
259 obj_class == NM_OC_RADIO_CARRIER &&
260 new_state->availability == 3 &&
Harald Welte12a721e2009-08-10 11:39:47 +0200261 net_listen_testnr) {
Harald Welte0bf8e302009-08-08 00:02:36 +0200262 struct gsm_bts_trx *trx = obj;
263 u_int8_t phys_config[] = { 0x02, 0x0a, 0x00, 0x01, 0x02 };
264 abis_nm_perform_test(trx->bts, 2, 0, 0, 0xff,
Harald Welte12a721e2009-08-10 11:39:47 +0200265 net_listen_testnr, 1,
Harald Welte0bf8e302009-08-08 00:02:36 +0200266 phys_config, sizeof(phys_config));
267 }
Harald Welte59b04682009-06-10 05:40:52 +0800268 return 0;
269}
270
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200271static void print_usage(void)
272{
273 printf("Usage: ipaccess-config\n");
274}
275
276static void print_help(void)
277{
278 printf(" -u --unit-id UNIT_ID\n");
279 printf(" -o --oml-ip ip\n");
280 printf(" -r --restart\n");
Harald Welte0bf8e302009-08-08 00:02:36 +0200281 printf(" -n flags/mask\tSet NVRAM attributes.\n");
Holger Hans Peter Freythera4434c22009-11-16 19:53:08 +0100282 printf(" -l --listen testnr \tPerform specified test number\n");
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200283 printf(" -h --help this text\n");
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100284 printf(" -s --stream-id ID\n");
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200285}
286
Harald Welte59b04682009-06-10 05:40:52 +0800287int main(int argc, char **argv)
288{
289 struct gsm_bts *bts;
290 struct sockaddr_in sin;
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100291 int rc, option_index = 0, stream_id = 0xff;
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100292 struct debug_target *stderr_target;
293
294 debug_init();
295 stderr_target = debug_target_create_stderr();
296 debug_add_target(stderr_target);
297 debug_set_all_filter(stderr_target, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800298
299 printf("ipaccess-config (C) 2009 by Harald Welte\n");
300 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
301
302 while (1) {
303 int c;
Harald Welte21460f02009-07-03 11:26:45 +0200304 unsigned long ul;
305 char *slash;
Harald Welte59b04682009-06-10 05:40:52 +0800306 static struct option long_options[] = {
307 { "unit-id", 1, 0, 'u' },
308 { "oml-ip", 1, 0, 'o' },
309 { "restart", 0, 0, 'r' },
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200310 { "help", 0, 0, 'h' },
Harald Welte12a721e2009-08-10 11:39:47 +0200311 { "listen", 1, 0, 'l' },
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100312 { "stream-id", 1, 0, 's' },
Harald Welte59b04682009-06-10 05:40:52 +0800313 };
314
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100315 c = getopt_long(argc, argv, "u:o:rn:l:hs:", long_options,
Harald Welte59b04682009-06-10 05:40:52 +0800316 &option_index);
317
318 if (c == -1)
319 break;
320
321 switch (c) {
322 case 'u':
323 unit_id = optarg;
324 break;
325 case 'o':
326 prim_oml_ip = optarg;
327 break;
328 case 'r':
329 restart = 1;
330 break;
Harald Welte21460f02009-07-03 11:26:45 +0200331 case 'n':
332 slash = strchr(optarg, '/');
333 if (!slash)
334 exit(2);
335 ul = strtoul(optarg, NULL, 16);
336 nv_flags = ul & 0xffff;
337 ul = strtoul(slash+1, NULL, 16);
338 nv_mask = ul & 0xffff;
339 break;
Harald Welte0bf8e302009-08-08 00:02:36 +0200340 case 'l':
Harald Welte12a721e2009-08-10 11:39:47 +0200341 net_listen_testnr = atoi(optarg);
Harald Welte0bf8e302009-08-08 00:02:36 +0200342 break;
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100343 case 's':
344 stream_id = atoi(optarg);
345 printf("foo: %d\n", stream_id);
346 break;
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200347 case 'h':
348 print_usage();
349 print_help();
350 exit(0);
Harald Welte59b04682009-06-10 05:40:52 +0800351 }
352 };
353
354 if (optind >= argc) {
Holger Hans Peter Freyther5bcfb4f2009-07-04 11:53:10 +0200355 fprintf(stderr, "you have to specify the IP address of the BTS. Use --help for more information\n");
Harald Welte59b04682009-06-10 05:40:52 +0800356 exit(2);
357 }
358
Harald Weltee712a5f2009-06-21 16:17:15 +0200359 gsmnet = gsm_network_init(1, 1, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800360 if (!gsmnet)
361 exit(1);
362
Mike Haben66e0ba02009-10-02 12:19:34 +0100363 bts = gsm_bts_alloc(gsmnet, GSM_BTS_TYPE_NANOBTS, HARDCODED_TSC,
Harald Weltee712a5f2009-06-21 16:17:15 +0200364 HARDCODED_BSIC);
Holger Hans Peter Freyther2e021c62009-10-27 03:41:09 +0100365 bts->oml_tei = stream_id;
Harald Welte59b04682009-06-10 05:40:52 +0800366
Holger Hans Peter Freyther72baef32009-07-07 12:40:07 +0200367 register_signal_handler(SS_NM, nm_sig_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800368 printf("Trying to connect to ip.access BTS ...\n");
369
370 memset(&sin, 0, sizeof(sin));
371 sin.sin_family = AF_INET;
372 inet_aton(argv[optind], &sin.sin_addr);
373 rc = ia_config_connect(bts, &sin);
374 if (rc < 0) {
375 perror("Error connecting to the BTS");
376 exit(1);
377 }
378
379 while (1) {
380 rc = bsc_select_main(0);
381 if (rc < 0)
382 exit(3);
383 }
384
385 exit(0);
386}
387