blob: 5ccbaa70c4421405ef9604a850739e5335c2036b [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>
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 Freythera7cd9fc2009-07-07 12:40:07 +020040#include <openbsc/signal.h>
Harald Welte25de9912009-04-30 15:53:07 +000041
42static struct gsm_network *gsmnet;
43
44static int restart;
45static char *prim_oml_ip;
46static char *unit_id;
Harald Welte684b1a82009-07-03 11:26:45 +020047static u_int16_t nv_flags;
48static u_int16_t nv_mask;
Harald Welte25de9912009-04-30 15:53:07 +000049
50/*
51static u_int8_t prim_oml_attr[] = { 0x95, 0x00, 7, 0x88, 192, 168, 100, 11, 0x00, 0x00 };
52static u_int8_t unit_id_attr[] = { 0x91, 0x00, 9, '2', '3', '4', '2', '/' , '0', '/', '0', 0x00 };
53*/
54
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +020055/*
56 * Callback function for NACK on the OML NM
57 *
58 * Currently we send the config requests but don't check the
59 * result. The nanoBTS will send us a NACK when we did something the
60 * BTS didn't like.
61 */
62static int ipacc_msg_nack(int mt)
63{
64 fprintf(stderr, "Failure to set attribute. This seems fatal\n");
65 exit(-1);
66 return 0;
67}
68
69static int nm_sig_cb(unsigned int subsys, unsigned int signal,
70 void *handler_data, void *signal_data)
71{
72 switch (signal) {
73 case S_NM_IPACC_NACK:
74 return ipacc_msg_nack((int)signal_data);
75 default:
76 break;
77 }
78
79 return 0;
80}
81
Harald Welte25de9912009-04-30 15:53:07 +000082static void bootstrap_om(struct gsm_bts *bts)
83{
84 int len;
85 static u_int8_t buf[1024];
Harald Welte684b1a82009-07-03 11:26:45 +020086 u_int8_t *cur = buf;
Harald Welte25de9912009-04-30 15:53:07 +000087
88 printf("OML link established\n");
89
90 if (unit_id) {
91 len = strlen(unit_id);
92 if (len > sizeof(buf)-10)
93 return;
94 buf[0] = NM_ATT_IPACC_UNIT_ID;
95 buf[1] = (len+1) >> 8;
96 buf[2] = (len+1) & 0xff;
97 memcpy(buf+3, unit_id, len);
98 buf[3+len] = 0;
99 printf("setting Unit ID to '%s'\n", unit_id);
100 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len+1);
101 }
102 if (prim_oml_ip) {
103 struct in_addr ia;
Harald Welte25de9912009-04-30 15:53:07 +0000104
105 if (!inet_aton(prim_oml_ip, &ia)) {
106 fprintf(stderr, "invalid IP address: %s\n",
107 prim_oml_ip);
108 return;
109 }
110
111 /* 0x88 + IP + port */
112 len = 1 + sizeof(ia) + 2;
113
Harald Welte0efe9b72009-07-12 09:33:54 +0200114 *cur++ = NM_ATT_IPACC_PRIM_OML_CFG_LIST;
Harald Welte25de9912009-04-30 15:53:07 +0000115 *cur++ = (len) >> 8;
116 *cur++ = (len) & 0xff;
117 *cur++ = 0x88;
118 memcpy(cur, &ia, sizeof(ia));
119 cur += sizeof(ia);
120 *cur++ = 0;
121 *cur++ = 0;
Harald Welte4802b882009-04-30 16:23:45 +0000122 printf("setting primary OML link IP to '%s'\n", inet_ntoa(ia));
Harald Welte25de9912009-04-30 15:53:07 +0000123 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
124 }
Harald Welte684b1a82009-07-03 11:26:45 +0200125 if (nv_mask) {
126 len = 4;
127
128 *cur++ = NM_ATT_IPACC_NV_FLAGS;
129 *cur++ = (len) >> 8;
130 *cur++ = (len) & 0xff;
131 *cur++ = nv_flags & 0xff;
132 *cur++ = nv_mask & 0xff;
133 *cur++ = nv_flags >> 8;
134 *cur++ = nv_mask >> 8;
135 printf("setting NV Flags/Mask to 0x%04x/0x%04x\n",
136 nv_flags, nv_mask);
137 abis_nm_ipaccess_set_nvattr(bts, buf, 3+len);
138 }
Harald Welte25de9912009-04-30 15:53:07 +0000139
140 if (restart) {
141 printf("restarting BTS\n");
142 abis_nm_ipaccess_restart(bts);
143 }
144}
145
146void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx)
147{
148 switch (event) {
149 case EVT_E1_TEI_UP:
150 switch (type) {
151 case E1INP_SIGN_OML:
152 bootstrap_om(trx->bts);
153 break;
154 case E1INP_SIGN_RSL:
155 /* FIXME */
156 break;
157 default:
158 break;
159 }
160 break;
161 case EVT_E1_TEI_DN:
162 fprintf(stderr, "Lost some E1 TEI link\n");
163 /* FIXME: deal with TEI or L1 link loss */
164 break;
165 default:
166 break;
167 }
168}
169
170int nm_state_event(enum nm_evt evt, u_int8_t obj_class, void *obj,
171 struct gsm_nm_state *old_state, struct gsm_nm_state *new_state)
172{
173 return 0;
174}
175
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200176static void print_usage(void)
177{
178 printf("Usage: ipaccess-config\n");
179}
180
181static void print_help(void)
182{
183 printf(" -u --unit-id UNIT_ID\n");
184 printf(" -o --oml-ip ip\n");
185 printf(" -r --restart\n");
186 printf(" -n flags/mask Set NVRAM attributes.\n");
187 printf(" -h --help this text\n");
188}
189
Harald Welte25de9912009-04-30 15:53:07 +0000190int main(int argc, char **argv)
191{
192 struct gsm_bts *bts;
193 struct sockaddr_in sin;
194 int rc, option_index = 0;
195
196 printf("ipaccess-config (C) 2009 by Harald Welte\n");
197 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
198
199 while (1) {
200 int c;
Harald Welte684b1a82009-07-03 11:26:45 +0200201 unsigned long ul;
202 char *slash;
Harald Welte25de9912009-04-30 15:53:07 +0000203 static struct option long_options[] = {
204 { "unit-id", 1, 0, 'u' },
205 { "oml-ip", 1, 0, 'o' },
206 { "restart", 0, 0, 'r' },
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200207 { "help", 0, 0, 'h' },
Harald Welte25de9912009-04-30 15:53:07 +0000208 };
209
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200210 c = getopt_long(argc, argv, "u:o:rn:h", long_options,
Harald Welte25de9912009-04-30 15:53:07 +0000211 &option_index);
212
213 if (c == -1)
214 break;
215
216 switch (c) {
217 case 'u':
218 unit_id = optarg;
219 break;
220 case 'o':
221 prim_oml_ip = optarg;
222 break;
223 case 'r':
224 restart = 1;
225 break;
Harald Welte684b1a82009-07-03 11:26:45 +0200226 case 'n':
227 slash = strchr(optarg, '/');
228 if (!slash)
229 exit(2);
230 ul = strtoul(optarg, NULL, 16);
231 nv_flags = ul & 0xffff;
232 ul = strtoul(slash+1, NULL, 16);
233 nv_mask = ul & 0xffff;
234 break;
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200235 case 'h':
236 print_usage();
237 print_help();
238 exit(0);
Harald Welte25de9912009-04-30 15:53:07 +0000239 }
240 };
241
242 if (optind >= argc) {
Holger Hans Peter Freyther42b40072009-07-04 11:53:10 +0200243 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 +0000244 exit(2);
245 }
246
Harald Weltee441d9c2009-06-21 16:17:15 +0200247 gsmnet = gsm_network_init(1, 1, NULL);
Harald Welte25de9912009-04-30 15:53:07 +0000248 if (!gsmnet)
249 exit(1);
250
Harald Weltee441d9c2009-06-21 16:17:15 +0200251 bts = gsm_bts_alloc(gsmnet, GSM_BTS_TYPE_NANOBTS_900, HARDCODED_TSC,
252 HARDCODED_BSIC);
Harald Welte25de9912009-04-30 15:53:07 +0000253
Holger Hans Peter Freythera7cd9fc2009-07-07 12:40:07 +0200254 register_signal_handler(SS_NM, nm_sig_cb, NULL);
Harald Welte25de9912009-04-30 15:53:07 +0000255 printf("Trying to connect to ip.access BTS ...\n");
256
257 memset(&sin, 0, sizeof(sin));
258 sin.sin_family = AF_INET;
259 inet_aton(argv[optind], &sin.sin_addr);
260 rc = ia_config_connect(bts, &sin);
261 if (rc < 0) {
262 perror("Error connecting to the BTS");
263 exit(1);
264 }
265
266 while (1) {
Harald Welte04d3c922009-05-23 06:07:04 +0000267 rc = bsc_select_main(0);
Harald Welte25de9912009-04-30 15:53:07 +0000268 if (rc < 0)
269 exit(3);
270 }
271
272 exit(0);
273}
274