blob: a607b10bc72cb7fb982b41657b58489b6d190e36 [file] [log] [blame]
Daniel Willmann1264cb42010-10-21 15:00:36 +02001/* SNMP-like status interface
2 *
3 * (C) 2010-2011 by Daniel Willmann <daniel@totalueberwachung.de>
4 * (C) 2010-2011 by On-Waves
5 *
6 * 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 <errno.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <time.h>
29#include <unistd.h>
30
31#include <arpa/inet.h>
32
33#include <netinet/tcp.h>
34
35#include <sys/fcntl.h>
36#include <sys/ioctl.h>
37#include <sys/socket.h>
38#include <sys/types.h>
39
40#include <openbsc/control_cmd.h>
41#include <openbsc/debug.h>
42#include <openbsc/e1_input.h>
43#include <openbsc/gsm_data.h>
44#include <openbsc/ipaccess.h>
45#include <openbsc/socket.h>
46#include <openbsc/subchan_demux.h>
47
48#include <openbsc/abis_rsl.h>
49#include <openbsc/abis_nm.h>
50
51#include <osmocom/core/msgb.h>
52#include <osmocom/core/select.h>
53#include <osmocom/core/talloc.h>
54
55#include <osmocom/gsm/tlv.h>
56
57#include <osmocom/vty/command.h>
58#include <osmocom/vty/vector.h>
59
60struct ctrl_handle {
61 struct osmo_fd listen_fd;
62 struct gsm_network *gsmnet;
63};
64
65vector ctrl_node_vec;
66
67int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd)
68{
69 int ret;
70 struct msgb *msg;
71
72 msg = ctrl_cmd_make(cmd);
73 if (!msg) {
74 LOGP(DINP, LOGL_ERROR, "Could not generate msg\n");
75 return -1;
76 }
77
78 ipaccess_prepend_header_ext(msg, IPAC_PROTO_EXT_CTRL);
79 ipaccess_prepend_header(msg, IPAC_PROTO_OSMO);
80
81 ret = osmo_wqueue_enqueue(queue, msg);
82 if (ret != 0) {
83 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the command.\n");
84 msgb_free(msg);
85 }
86 return ret;
87}
88
89int ctrl_cmd_handle(struct ctrl_cmd *cmd, void *data)
90{
91 char *token, *request;
92 int num, i, j, ret, node;
93 struct gsm_network *gsmnet = data;
94
95 struct gsm_network *net = NULL;
96 struct gsm_bts *bts = NULL;
97 struct gsm_bts_trx *trx = NULL;
98 struct gsm_bts_trx_ts *ts = NULL;
99 vector vline, cmdvec, cmds_vec;
100
101 ret = CTRL_CMD_ERROR;
102 cmd->reply = "Someone forgot to fill in the reply.";
103 cmd->node = NULL;
104 node = CTRL_NODE_ROOT;
105
106 request = talloc_strdup(tall_bsc_ctx, cmd->variable);
107 if (!request)
108 goto err;
109
110 for (i=0;i<strlen(request);i++) {
111 if (request[i] == '.')
112 request[i] = ' ';
113 }
114
115 vline = cmd_make_strvec(request);
116 talloc_free(request);
117 if (!vline)
118 goto err;
119
120 for (i=0;i<vector_active(vline);i++) {
121 token = vector_slot(vline, i);
122 /* TODO: We need to make sure that the following chars are digits
123 * and/or use strtol to check if number conversion was successful
124 * Right now something like net.bts_stats will not work */
125 if (!strcmp(token, "net")) {
126 net = gsmnet;
127 if (!net)
128 break;
129 cmd->node = net;
130 node = CTRL_NODE_NET;
131 } else if (!strncmp(token, "bts", 3)) {
132 if (!net)
133 break;
134 num = atoi(&token[3]);
135 bts = gsm_bts_num(net, num);
136 if (!bts)
137 break;
138 cmd->node = bts;
139 node = CTRL_NODE_BTS;
140 } else if (!strncmp(token, "trx", 3)) {
141 if (!bts)
142 break;
143 num = atoi(&token[3]);
144 trx = gsm_bts_trx_num(bts, num);
145 if (!trx)
146 break;
147 cmd->node = trx;
148 node = CTRL_NODE_TRX;
149 } else if (!strncmp(token, "ts", 2)) {
150 if (!trx)
151 break;
152 num = atoi(&token[2]);
153 if ((num >= 0) && (num < TRX_NR_TS))
154 ts = &trx->ts[num];
155 if (!ts)
156 break;
157 cmd->node = ts;
158 node = CTRL_NODE_TS;
159 } else {
160 /* If we're here the rest must be the command */
161 cmdvec = vector_init(vector_active(vline)-i);
162 for (j=i; j<vector_active(vline); j++) {
163 vector_set(cmdvec, vector_slot(vline, j));
164 }
165
166 /* Get the command vector of the right node */
167 cmds_vec = vector_lookup(ctrl_node_vec, node);
168
169 if (!cmds_vec) {
170 cmd->reply = "Command not found";
171 vector_free(cmdvec);
172 break;
173 }
174
175 ret = ctrl_cmd_exec(cmdvec, cmd, cmds_vec, data);
176
177 vector_free(cmdvec);
178 break;
179 }
180 }
181
182 cmd_free_strvec(vline);
183
184err:
185 if (ret == CTRL_CMD_ERROR)
186 cmd->type = CTRL_TYPE_ERROR;
187 return ret;
188}
189
190static void control_close_conn(struct ctrl_connection *ccon)
191{
192 close(ccon->write_queue.bfd.fd);
193 osmo_fd_unregister(&ccon->write_queue.bfd);
194 if (ccon->closed_cb)
195 ccon->closed_cb(ccon);
196 talloc_free(ccon);
197}
198
199static int handle_control_read(struct osmo_fd * bfd)
200{
201 int ret = -1, error;
202 struct osmo_wqueue *queue;
203 struct ctrl_connection *ccon;
204 struct ipaccess_head *iph;
205 struct ipaccess_head_ext *iph_ext;
206 struct msgb *msg;
207 struct ctrl_cmd *cmd;
208 struct ctrl_handle *ctrl = bfd->data;
209
210 queue = container_of(bfd, struct osmo_wqueue, bfd);
211 ccon = container_of(queue, struct ctrl_connection, write_queue);
212
213 msg = ipaccess_read_msg(bfd, &error);
214
215 if (!msg) {
216 if (error == 0)
217 LOGP(DINP, LOGL_INFO, "The control connection was closed\n");
218 else
219 LOGP(DINP, LOGL_ERROR, "Failed to parse ip access message: %d\n", error);
220
221 goto err;
222 }
223
224 if (msg->len < sizeof(*iph) + sizeof(*iph_ext)) {
225 LOGP(DINP, LOGL_ERROR, "The message is too short.\n");
226 goto err;
227 }
228
229 iph = (struct ipaccess_head *) msg->data;
230 if (iph->proto != IPAC_PROTO_OSMO) {
231 LOGP(DINP, LOGL_ERROR, "Protocol mismatch. We got 0x%x\n", iph->proto);
232 goto err;
233 }
234
235 iph_ext = (struct ipaccess_head_ext *) iph->data;
236 if (iph_ext->proto != IPAC_PROTO_EXT_CTRL) {
237 LOGP(DINP, LOGL_ERROR, "Extended protocol mismatch. We got 0x%x\n", iph_ext->proto);
238 goto err;
239 }
240
241 msg->l2h = iph_ext->data;
242
243 cmd = ctrl_cmd_parse(ccon, msg);
244
245 if (cmd) {
246 cmd->ccon = ccon;
247 if (ctrl_cmd_handle(cmd, ctrl->gsmnet) != CTRL_CMD_HANDLED) {
248 ctrl_cmd_send(queue, cmd);
249 talloc_free(cmd);
250 }
251 } else {
252 cmd = talloc_zero(ccon, struct ctrl_cmd);
253 if (!cmd)
254 goto err;
255 LOGP(DINP, LOGL_ERROR, "Command parser error.\n");
256 cmd->type = CTRL_TYPE_ERROR;
257 cmd->id = "err";
258 cmd->reply = "Command parser error.";
259 ctrl_cmd_send(queue, cmd);
260 talloc_free(cmd);
261 }
262
263 msgb_free(msg);
264 return 0;
265
266err:
267 control_close_conn(ccon);
268 msgb_free(msg);
269 return ret;
270}
271
272static int control_write_cb(struct osmo_fd *bfd, struct msgb *msg)
273{
274 int rc;
275
276 rc = write(bfd->fd, msg->data, msg->len);
277 if (rc != msg->len)
278 LOGP(DINP, LOGL_ERROR, "Failed to write message to the control connection.\n");
279
280 return rc;
281}
282
283static struct ctrl_connection *ctrl_connection_alloc(void *ctx)
284{
285 struct ctrl_connection *ccon = talloc_zero(ctx, struct ctrl_connection);
286 if (!ccon)
287 return NULL;
288
289 osmo_wqueue_init(&ccon->write_queue, 100);
290 /* Error handling here? */
291
292 INIT_LLIST_HEAD(&ccon->cmds);
293 return ccon;
294}
295
296static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
297{
298 int ret, fd, on;
299 struct ctrl_connection *ccon;
300 struct sockaddr_in sa;
301 socklen_t sa_len = sizeof(sa);
302
303
304 if (!(what & BSC_FD_READ))
305 return 0;
306
307 fd = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
308 if (fd < 0) {
309 perror("accept");
310 return fd;
311 }
312 LOGP(DINP, LOGL_INFO, "accept()ed new control connection from %s\n",
313 inet_ntoa(sa.sin_addr));
314
315 on = 1;
316 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
317 if (ret != 0) {
318 LOGP(DNAT, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
319 close(fd);
320 return ret;
321 }
322 ccon = ctrl_connection_alloc(listen_bfd->data);
323 if (!ccon) {
324 LOGP(DINP, LOGL_ERROR, "Failed to allocate.\n");
325 close(fd);
326 return -1;
327 }
328
329 ccon->write_queue.bfd.data = listen_bfd->data;
330 ccon->write_queue.bfd.fd = fd;
331 ccon->write_queue.bfd.when = BSC_FD_READ;
332 ccon->write_queue.read_cb = handle_control_read;
333 ccon->write_queue.write_cb = control_write_cb;
334
335 ret = osmo_fd_register(&ccon->write_queue.bfd);
336 if (ret < 0) {
337 LOGP(DINP, LOGL_ERROR, "Could not register FD.\n");
338 close(ccon->write_queue.bfd.fd);
339 talloc_free(ccon);
340 }
341
342 return ret;
343}
344
345int controlif_setup(struct gsm_network *gsmnet, uint16_t port)
346{
347 int ret;
348 struct ctrl_handle *ctrl;
349
350 ctrl = talloc_zero(tall_bsc_ctx, struct ctrl_handle);
351 if (!ctrl)
352 return -ENOMEM;
353
354 ctrl->gsmnet = gsmnet;
355
356 ctrl_node_vec = vector_init(5);
357 if (!ctrl_node_vec)
358 return -ENOMEM;
359
360 /* Listen for control connections */
361 ret = make_sock(&ctrl->listen_fd, IPPROTO_TCP, 0, port,
362 0, listen_fd_cb, ctrl);
363 if (ret < 0) {
364 talloc_free(ctrl);
365 return ret;
366 }
367
368 return ret;
369}