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 | |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 82 | static 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; |
| 88 | int i; |
| 89 | |
| 90 | DEBUGP(DNM, "TEST REPORT: "); |
| 91 | |
| 92 | if (foh->data[0] != NM_ATT_TEST_NO || |
| 93 | foh->data[2] != NM_ATT_TEST_REPORT) |
| 94 | return -EINVAL; |
| 95 | |
| 96 | DEBUGPC(DNM, "test_no=0x%02x ", foh->data[1]); |
| 97 | /* data[2] == NM_ATT_TEST_REPORT */ |
| 98 | /* data[3..4]: test_rep_len */ |
| 99 | test_rep_len = ntohs(*(u_int16_t *) &foh->data[3]); |
| 100 | /* data[5]: ip.access test result */ |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 101 | DEBUGPC(DNM, "test_res=%s\n", ipacc_testres_name(foh->data[5])); |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 102 | |
| 103 | /* data[6]: ip.access nested IE. 3 == freq_err_list */ |
| 104 | switch (foh->data[6]) { |
| 105 | case 3: |
| 106 | /* data[7..8]: length of ferr_list */ |
| 107 | ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]); |
| 108 | |
| 109 | /* data[9...]: frequency error list elements */ |
| 110 | for (i = 0; i < ferr_list_len; i+= sizeof(*ife)) { |
| 111 | ife = (struct ipacc_ferr_elem *) (foh->data + 9 + i); |
| 112 | DEBUGP(DNM, "==> ARFCN %4u, Frequency Error %6hd\n", |
| 113 | ife->arfcn, ntohs(ife->freq_err)); |
| 114 | } |
| 115 | break; |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 116 | case 4: |
| 117 | /* data[7..8]: length of ferr_list */ |
| 118 | ferr_list_len = ntohs(*(u_int16_t *) &foh->data[7]); |
| 119 | |
| 120 | /* data[9...]: channel usage list elements */ |
| 121 | for (i = 0; i < ferr_list_len; i+= 2) { |
| 122 | u_int16_t *cu_ptr = (u_int16_t *)(foh->data + 9 + i); |
| 123 | u_int16_t cu = ntohs(*cu_ptr); |
| 124 | DEBUGP(DNM, "==> ARFCN %4u, RxLev %2u\n", |
| 125 | cu & 0x3ff, cu >> 10); |
| 126 | } |
| 127 | break; |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 128 | default: |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Holger Hans Peter Freyther | 72baef3 | 2009-07-07 12:40:07 +0200 | [diff] [blame] | 135 | static int nm_sig_cb(unsigned int subsys, unsigned int signal, |
| 136 | void *handler_data, void *signal_data) |
| 137 | { |
| 138 | switch (signal) { |
| 139 | case S_NM_IPACC_NACK: |
| 140 | return ipacc_msg_nack((int)signal_data); |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 141 | case S_NM_TEST_REP: |
| 142 | return test_rep(signal_data); |
Holger Hans Peter Freyther | 72baef3 | 2009-07-07 12:40:07 +0200 | [diff] [blame] | 143 | default: |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 150 | static void bootstrap_om(struct gsm_bts *bts) |
| 151 | { |
| 152 | int len; |
| 153 | static u_int8_t buf[1024]; |
Harald Welte | 21460f0 | 2009-07-03 11:26:45 +0200 | [diff] [blame] | 154 | u_int8_t *cur = buf; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 155 | |
| 156 | printf("OML link established\n"); |
| 157 | |
| 158 | if (unit_id) { |
| 159 | len = strlen(unit_id); |
| 160 | if (len > sizeof(buf)-10) |
| 161 | return; |
| 162 | buf[0] = NM_ATT_IPACC_UNIT_ID; |
| 163 | buf[1] = (len+1) >> 8; |
| 164 | buf[2] = (len+1) & 0xff; |
| 165 | memcpy(buf+3, unit_id, len); |
| 166 | buf[3+len] = 0; |
| 167 | printf("setting Unit ID to '%s'\n", unit_id); |
| 168 | abis_nm_ipaccess_set_nvattr(bts, buf, 3+len+1); |
| 169 | } |
| 170 | if (prim_oml_ip) { |
| 171 | struct in_addr ia; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 172 | |
| 173 | if (!inet_aton(prim_oml_ip, &ia)) { |
| 174 | fprintf(stderr, "invalid IP address: %s\n", |
| 175 | prim_oml_ip); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | /* 0x88 + IP + port */ |
| 180 | len = 1 + sizeof(ia) + 2; |
| 181 | |
Harald Welte | 4206d98 | 2009-07-12 09:33:54 +0200 | [diff] [blame] | 182 | *cur++ = NM_ATT_IPACC_PRIM_OML_CFG_LIST; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 183 | *cur++ = (len) >> 8; |
| 184 | *cur++ = (len) & 0xff; |
| 185 | *cur++ = 0x88; |
| 186 | memcpy(cur, &ia, sizeof(ia)); |
| 187 | cur += sizeof(ia); |
| 188 | *cur++ = 0; |
| 189 | *cur++ = 0; |
| 190 | printf("setting primary OML link IP to '%s'\n", inet_ntoa(ia)); |
| 191 | abis_nm_ipaccess_set_nvattr(bts, buf, 3+len); |
| 192 | } |
Harald Welte | 21460f0 | 2009-07-03 11:26:45 +0200 | [diff] [blame] | 193 | if (nv_mask) { |
| 194 | len = 4; |
| 195 | |
| 196 | *cur++ = NM_ATT_IPACC_NV_FLAGS; |
| 197 | *cur++ = (len) >> 8; |
| 198 | *cur++ = (len) & 0xff; |
| 199 | *cur++ = nv_flags & 0xff; |
| 200 | *cur++ = nv_mask & 0xff; |
| 201 | *cur++ = nv_flags >> 8; |
| 202 | *cur++ = nv_mask >> 8; |
| 203 | printf("setting NV Flags/Mask to 0x%04x/0x%04x\n", |
| 204 | nv_flags, nv_mask); |
| 205 | abis_nm_ipaccess_set_nvattr(bts, buf, 3+len); |
| 206 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 207 | |
| 208 | if (restart) { |
| 209 | printf("restarting BTS\n"); |
| 210 | abis_nm_ipaccess_restart(bts); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx) |
| 215 | { |
| 216 | switch (event) { |
| 217 | case EVT_E1_TEI_UP: |
| 218 | switch (type) { |
| 219 | case E1INP_SIGN_OML: |
| 220 | bootstrap_om(trx->bts); |
| 221 | break; |
| 222 | case E1INP_SIGN_RSL: |
| 223 | /* FIXME */ |
| 224 | break; |
| 225 | default: |
| 226 | break; |
| 227 | } |
| 228 | break; |
| 229 | case EVT_E1_TEI_DN: |
| 230 | fprintf(stderr, "Lost some E1 TEI link\n"); |
| 231 | /* FIXME: deal with TEI or L1 link loss */ |
| 232 | break; |
| 233 | default: |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj, |
| 239 | struct gsm_nm_state *old_state, struct gsm_nm_state *new_state) |
| 240 | { |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 241 | if (evt == EVT_STATECHG_OPER && |
| 242 | obj_class == NM_OC_RADIO_CARRIER && |
| 243 | new_state->availability == 3 && |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 244 | net_listen_testnr) { |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 245 | struct gsm_bts_trx *trx = obj; |
| 246 | u_int8_t phys_config[] = { 0x02, 0x0a, 0x00, 0x01, 0x02 }; |
| 247 | abis_nm_perform_test(trx->bts, 2, 0, 0, 0xff, |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 248 | net_listen_testnr, 1, |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 249 | phys_config, sizeof(phys_config)); |
| 250 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 254 | static void print_usage(void) |
| 255 | { |
| 256 | printf("Usage: ipaccess-config\n"); |
| 257 | } |
| 258 | |
| 259 | static void print_help(void) |
| 260 | { |
| 261 | printf(" -u --unit-id UNIT_ID\n"); |
| 262 | printf(" -o --oml-ip ip\n"); |
| 263 | printf(" -r --restart\n"); |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 264 | printf(" -n flags/mask\tSet NVRAM attributes.\n"); |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 265 | printf(" -l --listen testnr \tPerform speciified test number\n"); |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 266 | printf(" -h --help this text\n"); |
Holger Hans Peter Freyther | 2e021c6 | 2009-10-27 03:41:09 +0100 | [diff] [blame] | 267 | printf(" -s --stream-id ID\n"); |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 270 | int main(int argc, char **argv) |
| 271 | { |
| 272 | struct gsm_bts *bts; |
| 273 | struct sockaddr_in sin; |
Holger Hans Peter Freyther | 2e021c6 | 2009-10-27 03:41:09 +0100 | [diff] [blame] | 274 | int rc, option_index = 0, stream_id = 0xff; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 275 | |
| 276 | printf("ipaccess-config (C) 2009 by Harald Welte\n"); |
| 277 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 278 | |
| 279 | while (1) { |
| 280 | int c; |
Harald Welte | 21460f0 | 2009-07-03 11:26:45 +0200 | [diff] [blame] | 281 | unsigned long ul; |
| 282 | char *slash; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 283 | static struct option long_options[] = { |
| 284 | { "unit-id", 1, 0, 'u' }, |
| 285 | { "oml-ip", 1, 0, 'o' }, |
| 286 | { "restart", 0, 0, 'r' }, |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 287 | { "help", 0, 0, 'h' }, |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 288 | { "listen", 1, 0, 'l' }, |
Holger Hans Peter Freyther | 2e021c6 | 2009-10-27 03:41:09 +0100 | [diff] [blame] | 289 | { "stream-id", 1, 0, 's' }, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 290 | }; |
| 291 | |
Holger Hans Peter Freyther | 2e021c6 | 2009-10-27 03:41:09 +0100 | [diff] [blame] | 292 | c = getopt_long(argc, argv, "u:o:rn:l:hs:", long_options, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 293 | &option_index); |
| 294 | |
| 295 | if (c == -1) |
| 296 | break; |
| 297 | |
| 298 | switch (c) { |
| 299 | case 'u': |
| 300 | unit_id = optarg; |
| 301 | break; |
| 302 | case 'o': |
| 303 | prim_oml_ip = optarg; |
| 304 | break; |
| 305 | case 'r': |
| 306 | restart = 1; |
| 307 | break; |
Harald Welte | 21460f0 | 2009-07-03 11:26:45 +0200 | [diff] [blame] | 308 | case 'n': |
| 309 | slash = strchr(optarg, '/'); |
| 310 | if (!slash) |
| 311 | exit(2); |
| 312 | ul = strtoul(optarg, NULL, 16); |
| 313 | nv_flags = ul & 0xffff; |
| 314 | ul = strtoul(slash+1, NULL, 16); |
| 315 | nv_mask = ul & 0xffff; |
| 316 | break; |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 317 | case 'l': |
Harald Welte | 12a721e | 2009-08-10 11:39:47 +0200 | [diff] [blame] | 318 | net_listen_testnr = atoi(optarg); |
Harald Welte | 0bf8e30 | 2009-08-08 00:02:36 +0200 | [diff] [blame] | 319 | break; |
Holger Hans Peter Freyther | 2e021c6 | 2009-10-27 03:41:09 +0100 | [diff] [blame] | 320 | case 's': |
| 321 | stream_id = atoi(optarg); |
| 322 | printf("foo: %d\n", stream_id); |
| 323 | break; |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 324 | case 'h': |
| 325 | print_usage(); |
| 326 | print_help(); |
| 327 | exit(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 328 | } |
| 329 | }; |
| 330 | |
| 331 | if (optind >= argc) { |
Holger Hans Peter Freyther | 5bcfb4f | 2009-07-04 11:53:10 +0200 | [diff] [blame] | 332 | 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] | 333 | exit(2); |
| 334 | } |
| 335 | |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 336 | gsmnet = gsm_network_init(1, 1, NULL); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 337 | if (!gsmnet) |
| 338 | exit(1); |
| 339 | |
Mike Haben | 66e0ba0 | 2009-10-02 12:19:34 +0100 | [diff] [blame] | 340 | bts = gsm_bts_alloc(gsmnet, GSM_BTS_TYPE_NANOBTS, HARDCODED_TSC, |
Harald Welte | e712a5f | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 341 | HARDCODED_BSIC); |
Holger Hans Peter Freyther | 2e021c6 | 2009-10-27 03:41:09 +0100 | [diff] [blame] | 342 | bts->oml_tei = stream_id; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 343 | |
Holger Hans Peter Freyther | 72baef3 | 2009-07-07 12:40:07 +0200 | [diff] [blame] | 344 | register_signal_handler(SS_NM, nm_sig_cb, NULL); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 345 | printf("Trying to connect to ip.access BTS ...\n"); |
| 346 | |
| 347 | memset(&sin, 0, sizeof(sin)); |
| 348 | sin.sin_family = AF_INET; |
| 349 | inet_aton(argv[optind], &sin.sin_addr); |
| 350 | rc = ia_config_connect(bts, &sin); |
| 351 | if (rc < 0) { |
| 352 | perror("Error connecting to the BTS"); |
| 353 | exit(1); |
| 354 | } |
| 355 | |
| 356 | while (1) { |
| 357 | rc = bsc_select_main(0); |
| 358 | if (rc < 0) |
| 359 | exit(3); |
| 360 | } |
| 361 | |
| 362 | exit(0); |
| 363 | } |
| 364 | |