Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* 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 Freyther | 72baef3 | 2009-07-07 12:40:07 +0200 | [diff] [blame] | 40 | #include <openbsc/signal.h> |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 41 | #include <openbsc/debug.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 42 | |
| 43 | static struct gsm_network *gsmnet; |
| 44 | |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 45 | static int net_listen_testnr; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 46 | static int restart; |
| 47 | static char *prim_oml_ip; |
| 48 | static char *unit_id; |
Harald Welte | 21460f0 | 2009-07-03 11:26:45 +0200 | [diff] [blame] | 49 | static u_int16_t nv_flags; |
| 50 | static u_int16_t nv_mask; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | static u_int8_t prim_oml_attr[] = { 0x95, 0x00, 7, 0x88, 192, 168, 100, 11, 0x00, 0x00 }; |
| 54 | static u_int8_t unit_id_attr[] = { 0x91, 0x00, 9, '2', '3', '4', '2', '/' , '0', '/', '0', 0x00 }; |
| 55 | */ |
| 56 | |
Holger Hans Peter Freyther | 72baef3 | 2009-07-07 12:40:07 +0200 | [diff] [blame] | 57 | /* |
| 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 | */ |
| 64 | static int ipacc_msg_nack(int mt) |
| 65 | { |
| 66 | fprintf(stderr, "Failure to set attribute. This seems fatal\n"); |
| 67 | exit(-1); |
| 68 | return 0; |
| 69 | } |
| 70 | |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 71 | struct ipacc_ferr_elem { |
| 72 | int16_t freq_err; |
| 73 | u_int8_t freq_qual; |
| 74 | u_int8_t arfcn; |
| 75 | } __attribute__((packed)); |
| 76 | |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 77 | struct ipacc_cusage_elem { |
| 78 | u_int16_t arfcn:10, |
| 79 | rxlev:6; |
| 80 | } __attribute__ ((packed)); |
| 81 | |
| 82 | static const char *ipacc_testres_names[] = { |
| 83 | [NM_IPACC_TESTRES_SUCCESS] = "SUCCESS", |
| 84 | [NM_IPACC_TESTRES_TIMEOUT] = "TIMEOUT", |
| 85 | [NM_IPACC_TESTRES_NO_CHANS] = "NO CHANNELS", |
| 86 | [NM_IPACC_TESTRES_PARTIAL] = "PARTIAL", |
| 87 | [NM_IPACC_TESTRES_STOPPED] = "STOPPED", |
| 88 | }; |
| 89 | |
| 90 | const char *ipacc_testres_name(u_int8_t res) |
| 91 | { |
| 92 | if (res < ARRAY_SIZE(ipacc_testres_names) && |
| 93 | ipacc_testres_names[res]) |
| 94 | return ipacc_testres_names[res]; |
| 95 | |
| 96 | return "unknown"; |
| 97 | } |
| 98 | |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 99 | static int test_rep(void *_msg) |
| 100 | { |
| 101 | struct msgb *msg = _msg; |
| 102 | struct abis_om_fom_hdr *foh = msgb_l3(msg); |
| 103 | u_int16_t test_rep_len, ferr_list_len; |
| 104 | struct ipacc_ferr_elem *ife; |
| 105 | int i; |
| 106 | |
| 107 | DEBUGP(DNM, "TEST REPORT: "); |
| 108 | |
| 109 | if (foh->data[0] != NM_ATT_TEST_NO || |
| 110 | foh->data[2] != NM_ATT_TEST_REPORT) |
| 111 | return -EINVAL; |
| 112 | |
| 113 | DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]); |
| 114 | /* data[2] == NM_ATT_TEST_REPORT */ |
| 115 | /* data[3..4]: test_rep_len */ |
| 116 | test_rep_len = ntohs(*(u_int16_t *) &foh->data[3]); |
| 117 | /* data[5]: ip.access test result */ |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 118 | DEBUGPC(DNM, "test_res=%s\n", ipacc_testres_name(foh->data[5])); |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 119 | |
| 120 | /* data[6]: ip.access nested IE. 3 == freq_err_list */ |
| 121 | switch (foh->data[6]) { |
| 122 | case 3: |
| 123 | /* data[7..8]: length of ferr_list */ |
| 124 | ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]); |
| 125 | |
| 126 | /* data[9...]: frequency error list elements */ |
| 127 | for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) { |
| 128 | ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i); |
| 129 | DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n", |
| 130 | ife->arfcn, ntohs(ife->freq_err)); |
| 131 | } |
| 132 | break; |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 133 | case 4: |
| 134 | /* data[7..8]: length of ferr_list */ |
| 135 | ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]); |
| 136 | |
| 137 | /* data[9...]: channel usage list elements */ |
| 138 | for (i = 0; i < ferr_list_len; i+= 2) { |
| 139 | u_int16_t *cu_ptr = (u_int16_t *)(foh->data + 9 + i); |
| 140 | u_int16_t cu = ntohs(*cu_ptr); |
| 141 | DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n", |
| 142 | cu & 0x3ff, cu >> 10); |
| 143 | } |
| 144 | break; |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Holger Hans Peter Freyther | 72baef3 | 2009-07-07 12:40:07 +0200 | [diff] [blame] | 152 | static int nm_sig_cb(unsigned int subsys, unsigned int signal, |
| 153 | void *handler_data, void *signal_data) |
| 154 | { |
| 155 | switch (signal) { |
| 156 | case S_NM_IPACC_NACK: |
| 157 | return ipacc_msg_nack((int)signal_data); |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 158 | case S_NM_TEST_REP: |
| 159 | return test_rep(signal_data); |
Holger Hans Peter Freyther | 72baef3 | 2009-07-07 12:40:07 +0200 | [diff] [blame] | 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 167 | static void bootstrap_om(struct gsm_bts *bts) |
| 168 | { |
| 169 | int len; |
| 170 | static u_int8_t buf[1024]; |
Harald Welte | 21460f0 | 2009-07-03 11:26:45 +0200 | [diff] [blame] | 171 | u_int8_t *cur = buf; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 172 | |
| 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 Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 189 | |
| 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 Welte | 4206d98 | 2009-07-12 09:33:54 +0200 | [diff] [blame] | 199 | *cur++ = NM_ATT_IPACC_PRIM_OML_CFG_LIST; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 200 | *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 Welte | 21460f0 | 2009-07-03 11:26:45 +0200 | [diff] [blame] | 210 | 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 Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 224 | |
| 225 | if (restart) { |
| 226 | printf("restarting BTS\n"); |
| 227 | abis_nm_ipaccess_restart(bts); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void 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 | |
| 255 | int 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 Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 258 | if (evt == EVT_STATECHG_OPER && |
| 259 | obj_class == NM_OC_RADIO_CARRIER && |
| 260 | new_state->availability == 3 && |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 261 | net_listen_testnr) { |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 262 | 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 Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 265 | net_listen_testnr, 1, |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 266 | phys_config, sizeof(phys_config)); |
| 267 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 271 | static void print_usage(void) |
| 272 | { |
| 273 | printf("Usage: ipaccess-config\n"); |
| 274 | } |
| 275 | |
| 276 | static 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 Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 281 | printf(" -n flags/mask\tSet NVRAM attributes.\n"); |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 282 | printf(" -l --listen testnr \tPerform speciified test number\n"); |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 283 | printf(" -h --help this text\n"); |
| 284 | } |
| 285 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 286 | int main(int argc, char **argv) |
| 287 | { |
| 288 | struct gsm_bts *bts; |
| 289 | struct sockaddr_in sin; |
| 290 | int rc, option_index = 0; |
| 291 | |
| 292 | printf("ipaccess-config (C) 2009 by Harald Welte\n"); |
| 293 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 294 | |
| 295 | while (1) { |
| 296 | int c; |
Harald Welte | 21460f0 | 2009-07-03 11:26:45 +0200 | [diff] [blame] | 297 | unsigned long ul; |
| 298 | char *slash; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 299 | static struct option long_options[] = { |
| 300 | { "unit-id", 1, 0, 'u' }, |
| 301 | { "oml-ip", 1, 0, 'o' }, |
| 302 | { "restart", 0, 0, 'r' }, |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 303 | { "help", 0, 0, 'h' }, |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 304 | { "listen", 1, 0, 'l' }, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 305 | }; |
| 306 | |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 307 | c = getopt_long(argc, argv, "u:o:rn:l:h", long_options, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 308 | &option_index); |
| 309 | |
| 310 | if (c == -1) |
| 311 | break; |
| 312 | |
| 313 | switch (c) { |
| 314 | case 'u': |
| 315 | unit_id = optarg; |
| 316 | break; |
| 317 | case 'o': |
| 318 | prim_oml_ip = optarg; |
| 319 | break; |
| 320 | case 'r': |
| 321 | restart = 1; |
| 322 | break; |
Harald Welte | 21460f0 | 2009-07-03 11:26:45 +0200 | [diff] [blame] | 323 | case 'n': |
| 324 | slash = strchr(optarg, '/'); |
| 325 | if (!slash) |
| 326 | exit(2); |
| 327 | ul = strtoul(optarg, NULL, 16); |
| 328 | nv_flags = ul & 0xffff; |
| 329 | ul = strtoul(slash+1, NULL, 16); |
| 330 | nv_mask = ul & 0xffff; |
| 331 | break; |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 332 | case 'l': |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 333 | net_listen_testnr = atoi(optarg); |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 334 | break; |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 335 | case 'h': |
| 336 | print_usage(); |
| 337 | print_help(); |
| 338 | exit(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 339 | } |
| 340 | }; |
| 341 | |
| 342 | if (optind >= argc) { |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 343 | fprintf(stderr, "you have to specify the IP address of the BTS. Use --help for more information\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 344 | exit(2); |
| 345 | } |
| 346 | |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 347 | gsmnet = gsm_network_init(1, 1, NULL); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 348 | if (!gsmnet) |
| 349 | exit(1); |
| 350 | |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 351 | bts = gsm_bts_alloc(gsmnet, GSM_BTS_TYPE_NANOBTS_900, HARDCODED_TSC, |
| 352 | HARDCODED_BSIC); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 353 | |
Holger Hans Peter Freyther | 72baef3 | 2009-07-07 12:40:07 +0200 | [diff] [blame] | 354 | register_signal_handler(SS_NM, nm_sig_cb, NULL); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 355 | printf("Trying to connect to ip.access BTS ...\n"); |
| 356 | |
| 357 | memset(&sin, 0, sizeof(sin)); |
| 358 | sin.sin_family = AF_INET; |
| 359 | inet_aton(argv[optind], &sin.sin_addr); |
| 360 | rc = ia_config_connect(bts, &sin); |
| 361 | if (rc < 0) { |
| 362 | perror("Error connecting to the BTS"); |
| 363 | exit(1); |
| 364 | } |
| 365 | |
| 366 | while (1) { |
| 367 | rc = bsc_select_main(0); |
| 368 | if (rc < 0) |
| 369 | exit(3); |
| 370 | } |
| 371 | |
| 372 | exit(0); |
| 373 | } |
| 374 | |