blob: bff4d17de7b5f4476344600633fd9d13a0495fd0 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file control_cmd.c
2 * SNMP-like status interface. */
3/*
Daniel Willmann1264cb42010-10-21 15:00:36 +02004 * (C) 2010-2011 by Daniel Willmann <daniel@totalueberwachung.de>
5 * (C) 2010-2011 by On-Waves
Harald Weltee08da972017-11-13 01:00:26 +09006 * (C) 2014 by Harald Welte <laforge@gnumonks.org>
7 * (C) 2017 by sysmocom - s.f.m.c. GmbH
Daniel Willmann1264cb42010-10-21 15:00:36 +02008 *
9 * All Rights Reserved
10 *
Harald Weltee08da972017-11-13 01:00:26 +090011 * SPDX-License-Identifier: GPL-2.0+
12 *
Daniel Willmann1264cb42010-10-21 15:00:36 +020013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 */
28
29#include <ctype.h>
30#include <errno.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35#include <unistd.h>
36
Harald Welte1238cc62014-08-20 19:50:04 +020037#include <osmocom/ctrl/control_cmd.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020038
39#include <osmocom/core/msgb.h>
40#include <osmocom/core/talloc.h>
Max70c7d412017-02-24 13:59:14 +010041#include <osmocom/core/utils.h>
Daniel Willmann1264cb42010-10-21 15:00:36 +020042#include <osmocom/vty/command.h>
43#include <osmocom/vty/vector.h>
44
45extern vector ctrl_node_vec;
46
Max70c7d412017-02-24 13:59:14 +010047const struct value_string ctrl_type_vals[] = {
Neels Hofmeyr7c1ec8c2017-03-02 14:32:52 +010048 { CTRL_TYPE_UNKNOWN, "(unknown)" },
Max70c7d412017-02-24 13:59:14 +010049 { CTRL_TYPE_GET, "GET" },
50 { CTRL_TYPE_SET, "SET" },
51 { CTRL_TYPE_GET_REPLY, "GET_REPLY" },
52 { CTRL_TYPE_SET_REPLY, "SET_REPLY" },
53 { CTRL_TYPE_TRAP, "TRAP" },
54 { CTRL_TYPE_ERROR, "ERROR" },
Neels Hofmeyr7c1ec8c2017-03-02 14:32:52 +010055 { 0, NULL }
Daniel Willmann1264cb42010-10-21 15:00:36 +020056};
57
Daniel Willmann1264cb42010-10-21 15:00:36 +020058/* Functions from libosmocom */
59extern vector cmd_make_descvec(const char *string, const char *descstr);
60
61/* Get the ctrl_cmd_element that matches this command */
62static struct ctrl_cmd_element *ctrl_cmd_get_element_match(vector vline, vector node)
63{
64 int index, j;
65 const char *desc;
66 struct ctrl_cmd_element *cmd_el;
67 struct ctrl_cmd_struct *cmd_desc;
68 char *str;
69
70 for (index = 0; index < vector_active(node); index++) {
71 if ((cmd_el = vector_slot(node, index))) {
72 cmd_desc = &cmd_el->strcmd;
73 if (cmd_desc->nr_commands > vector_active(vline))
74 continue;
Holger Hans Peter Freyther5fb265e2015-04-05 14:36:31 +020075 for (j =0; j < vector_active(vline) && j < cmd_desc->nr_commands; j++) {
Daniel Willmann1264cb42010-10-21 15:00:36 +020076 str = vector_slot(vline, j);
77 desc = cmd_desc->command[j];
78 if (desc[0] == '*')
79 return cmd_el; /* Partial match */
80 if (strcmp(desc, str) != 0)
81 break;
82 }
83 /* We went through all the elements and all matched */
84 if (j == cmd_desc->nr_commands)
85 return cmd_el;
86 }
87 }
88
89 return NULL;
90}
91
Harald Welteb4186822018-05-26 17:25:11 +020092/*! Execute a given received command
93 * \param[in] vline vector representing the available/registered commands
94 * \param[inout] command parsed received command to be executed
95 * \param[in] node CTRL interface node
96 * \param[in] data opaque data passed to verify(), get() and set() call-backs
97 * \returns CTRL_CMD_HANDLED or CTRL_CMD_REPLY; CTRL_CMD_ERROR on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +020098int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data)
99{
100 int ret = CTRL_CMD_ERROR;
101 struct ctrl_cmd_element *cmd_el;
102
103 if ((command->type != CTRL_TYPE_GET) && (command->type != CTRL_TYPE_SET)) {
104 command->reply = "Trying to execute something not GET or SET";
105 goto out;
106 }
107 if ((command->type == CTRL_TYPE_SET) && (!command->value)) {
108 command->reply = "SET without a value";
109 goto out;
110 }
111
112 if (!vline)
113 goto out;
114
115 cmd_el = ctrl_cmd_get_element_match(vline, node);
116
117 if (!cmd_el) {
118 command->reply = "Command not found";
119 goto out;
120 }
121
122 if (command->type == CTRL_TYPE_SET) {
123 if (!cmd_el->set) {
124 command->reply = "SET not implemented";
125 goto out;
126 }
127 if (cmd_el->verify) {
128 if ((ret = cmd_el->verify(command, command->value, data))) {
129 ret = CTRL_CMD_ERROR;
Daniel Willmann9225ab12011-07-28 21:38:51 +0200130 /* If verify() set an appropriate error message, don't change it. */
131 if (!command->reply)
132 command->reply = "Value failed verification.";
Daniel Willmann1264cb42010-10-21 15:00:36 +0200133 goto out;
134 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200135 }
136 ret = cmd_el->set(command, data);
137 goto out;
138 } else if (command->type == CTRL_TYPE_GET) {
139 if (!cmd_el->get) {
140 command->reply = "GET not implemented";
141 goto out;
142 }
143 ret = cmd_el->get(command, data);
144 goto out;
145 }
146out:
147 if (ret == CTRL_CMD_REPLY) {
148 if (command->type == CTRL_TYPE_SET) {
149 command->type = CTRL_TYPE_SET_REPLY;
150 } else if (command->type == CTRL_TYPE_GET) {
151 command->type = CTRL_TYPE_GET_REPLY;
152 }
153 } else if (ret == CTRL_CMD_ERROR) {
154 command->type = CTRL_TYPE_ERROR;
155 }
156 return ret;
157}
158
159static void add_word(struct ctrl_cmd_struct *cmd,
160 const char *start, const char *end)
161{
162 if (!cmd->command) {
163 cmd->command = talloc_zero_array(tall_vty_vec_ctx,
164 char*, 1);
165 cmd->nr_commands = 0;
166 } else {
167 cmd->command = talloc_realloc(tall_vty_vec_ctx,
168 cmd->command, char*,
169 cmd->nr_commands + 1);
170 }
171
172 cmd->command[cmd->nr_commands++] = talloc_strndup(cmd->command,
173 start, end - start);
174}
175
176static void create_cmd_struct(struct ctrl_cmd_struct *cmd, const char *name)
177{
178 const char *cur, *word;
179
180 for (cur = name, word = NULL; cur[0] != '\0'; ++cur) {
181 /* warn about optionals */
182 if (cur[0] == '(' || cur[0] == ')' || cur[0] == '|') {
Harald Welte7fd0c832014-08-20 19:58:13 +0200183 LOGP(DLCTRL, LOGL_ERROR,
Daniel Willmann1264cb42010-10-21 15:00:36 +0200184 "Optionals are not supported in '%s'\n", name);
185 goto failure;
186 }
187
188 if (isspace(cur[0])) {
189 if (word) {
190 add_word(cmd, word, cur);
191 word = NULL;
192 }
193 continue;
194 }
195
196 if (!word)
197 word = cur;
198 }
199
200 if (word)
201 add_word(cmd, word, cur);
202
203 return;
204failure:
205 cmd->nr_commands = 0;
206 talloc_free(cmd->command);
207}
208
Harald Welteb4186822018-05-26 17:25:11 +0200209/*! Install a given command definition at a given CTRL node.
210 * \param[in] node CTRL node at whihc \a cmd is to be installed
211 * \param[in] cmd command definition to be installed
212 * \returns 0 on success; negative on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200213int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd)
214{
215 vector cmds_vec;
216
217 cmds_vec = vector_lookup_ensure(ctrl_node_vec, node);
218
219 if (!cmds_vec) {
220 cmds_vec = vector_init(5);
221 if (!cmds_vec) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200222 LOGP(DLCTRL, LOGL_ERROR, "vector_init failed.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200223 return -ENOMEM;
224 }
225 vector_set_index(ctrl_node_vec, node, cmds_vec);
226 }
227
228 vector_set(cmds_vec, cmd);
229
230 create_cmd_struct(&cmd->strcmd, cmd->name);
231 return 0;
232}
233
Harald Welteb4186822018-05-26 17:25:11 +0200234/*! Allocate a control command of given \a type.
235 * \param[in] ctx talloc context from which to allocate
236 * \param[in] type command type to set after allocation
237 * \returns callee-allocated \ref ctrl_cmd. Caller must talloc_free() it. */
Holger Hans Peter Freythera6b34012011-08-22 23:44:32 +0200238struct ctrl_cmd *ctrl_cmd_create(void *ctx, enum ctrl_type type)
239{
240 struct ctrl_cmd *cmd;
241
242 cmd = talloc_zero(ctx, struct ctrl_cmd);
243 if (!cmd)
244 return NULL;
245
246 cmd->type = type;
247 return cmd;
248}
249
Harald Welteb4186822018-05-26 17:25:11 +0200250/*! Perform a deepl copy of the given \a cmd, allocating memory from \a ctx.
251 * \param[in] ctx talloc context from which to allocate
252 * \param[in cmd CTRL command to be copied
253 * \returns deep copy of \a cmd on success; NULL on error */
Daniel Willmann8b7a9622011-03-17 15:37:54 +0100254struct ctrl_cmd *ctrl_cmd_cpy(void *ctx, struct ctrl_cmd *cmd)
255{
256 struct ctrl_cmd *cmd2;
257
258 cmd2 = talloc_zero(ctx, struct ctrl_cmd);
259 if (!cmd2)
260 return NULL;
261
262 cmd2->type = cmd->type;
263 if (cmd->id) {
264 cmd2->id = talloc_strdup(cmd2, cmd->id);
265 if (!cmd2->id)
266 goto err;
267 }
268 if (cmd->variable) {
269 cmd2->variable = talloc_strdup(cmd2, cmd->variable);
270 if (!cmd2->variable)
271 goto err;
272 }
273 if (cmd->value) {
274 cmd2->value = talloc_strdup(cmd2, cmd->value);
275 if (!cmd2->value)
276 goto err;
277 }
278 if (cmd->reply) {
279 cmd2->reply = talloc_strdup(cmd2, cmd->reply);
280 if (!cmd2->reply)
281 goto err;
282 }
283
284 return cmd2;
285err:
286 talloc_free(cmd2);
287 return NULL;
288}
289
Harald Welteb4186822018-05-26 17:25:11 +0200290/*! Parse/Decode CTRL from \ref msgb into command struct.
291 * \param[in] ctx talloc context from which to allocate
292 * \param[in] msg message buffer containing command to be decoded
293 * \returns callee-allocated decoded CTRL command; NULL on allocation or other failure
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100294 * The caller is responsible to talloc_free() the returned struct pointer. */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200295struct ctrl_cmd *ctrl_cmd_parse(void *ctx, struct msgb *msg)
296{
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100297 struct ctrl_cmd *res = ctrl_cmd_parse2(ctx, msg);
298 if (res->type == CTRL_TYPE_ERROR) {
299 talloc_free(res);
300 return NULL;
301 }
302 return res;
303}
304
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200305static bool id_str_valid(const char *str)
306{
307 for (;*str;str++) {
308 if (!isdigit(*str))
309 return false;
310 }
311 return true;
312}
313
Harald Welteb4186822018-05-26 17:25:11 +0200314/*! Parse/Decode CTRL from \ref msgb into command struct.
315 * \param[in] ctx talloc context from which to allocate
316 * \param[in] msg message buffer containing command to be decoded
317 * \returns callee-allocated decoded CTRL command; NULL on allocation failure,
318 * ctrl->type == CTRL_TYPE_ERROR and an error message in ctrl->reply on any error.
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100319 * The caller is responsible to talloc_free() the returned struct pointer. */
320struct ctrl_cmd *ctrl_cmd_parse2(void *ctx, struct msgb *msg)
321{
Daniel Willmann1264cb42010-10-21 15:00:36 +0200322 char *str, *tmp, *saveptr = NULL;
323 char *var, *val;
324 struct ctrl_cmd *cmd;
325
326 cmd = talloc_zero(ctx, struct ctrl_cmd);
327 if (!cmd) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200328 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200329 return NULL;
330 }
331
332 /* Make sure input is NULL terminated */
333 msgb_put_u8(msg, 0);
334 str = (char *) msg->l2h;
335
Harald Welteedf6fe72016-11-26 10:06:07 +0100336 OSMO_ASSERT(str);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200337 tmp = strtok_r(str, " ", &saveptr);
338 if (!tmp) {
339 cmd->type = CTRL_TYPE_ERROR;
340 cmd->id = "err";
341 cmd->reply = "Request malformed";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200342 LOGP(DLCTRL, LOGL_NOTICE, "Malformed request: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200343 goto err;
344 }
345
Max70c7d412017-02-24 13:59:14 +0100346 cmd->type = get_string_value(ctrl_type_vals, tmp);
Pau Espin Pedrol8aa85bd2017-06-23 12:43:31 +0200347 if ((int)cmd->type < 0 || cmd->type == CTRL_TYPE_UNKNOWN) {
Daniel Willmann1264cb42010-10-21 15:00:36 +0200348 cmd->type = CTRL_TYPE_ERROR;
349 cmd->id = "err";
350 cmd->reply = "Request type unknown";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200351 LOGP(DLCTRL, LOGL_NOTICE, "Request type unknown: \"%s\"\n", osmo_escape_str(str, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200352 goto err;
353 }
354
355 tmp = strtok_r(NULL, " ", &saveptr);
356
357 if (!tmp) {
358 cmd->type = CTRL_TYPE_ERROR;
359 cmd->id = "err";
360 cmd->reply = "Missing ID";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200361 LOGP(DLCTRL, LOGL_NOTICE, "Missing ID: \"%s\"\n", osmo_escape_str(str, -1));
362 goto err;
363 }
364
365 if (!id_str_valid(tmp)) {
366 cmd->type = CTRL_TYPE_ERROR;
367 cmd->id = "err";
368 cmd->reply = "Invalid message ID number";
369 LOGP(DLCTRL, LOGL_NOTICE, "Invalid message ID number: \"%s\"\n", osmo_escape_str(tmp, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200370 goto err;
371 }
372 cmd->id = talloc_strdup(cmd, tmp);
373 if (!cmd->id)
374 goto oom;
375
376 switch (cmd->type) {
377 case CTRL_TYPE_GET:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200378 var = strtok_r(NULL, " \n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200379 if (!var) {
380 cmd->type = CTRL_TYPE_ERROR;
381 cmd->reply = "GET incomplete";
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200382 LOGP(DLCTRL, LOGL_NOTICE, "GET Command incomplete: \"%s\"\n",
383 osmo_escape_str(str, -1));
384 goto err;
385 }
386 if (!osmo_separated_identifiers_valid(var, ".")) {
387 cmd->type = CTRL_TYPE_ERROR;
388 cmd->reply = "GET variable contains invalid characters";
389 LOGP(DLCTRL, LOGL_NOTICE, "GET variable contains invalid characters: \"%s\"\n",
390 osmo_escape_str(var, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200391 goto err;
392 }
393 cmd->variable = talloc_strdup(cmd, var);
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200394 var = strtok_r(NULL, "", &saveptr);
395 if (var) {
396 cmd->type = CTRL_TYPE_ERROR;
397 cmd->reply = "GET with trailing characters";
398 LOGP(DLCTRL, LOGL_NOTICE, "GET with trailing characters: \"%s\"\n",
399 osmo_escape_str(var, -1));
400 goto err;
401 }
Harald Welte7fd0c832014-08-20 19:58:13 +0200402 LOGP(DLCTRL, LOGL_DEBUG, "Command: GET %s\n", cmd->variable);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200403 break;
404 case CTRL_TYPE_SET:
405 var = strtok_r(NULL, " ", &saveptr);
Holger Hans Peter Freyther5ad742d2014-08-08 19:41:28 +0200406 val = strtok_r(NULL, "\n", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200407 if (!var || !val) {
408 cmd->type = CTRL_TYPE_ERROR;
409 cmd->reply = "SET incomplete";
Harald Welte7fd0c832014-08-20 19:58:13 +0200410 LOGP(DLCTRL, LOGL_NOTICE, "SET Command incomplete\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200411 goto err;
412 }
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200413 if (!osmo_separated_identifiers_valid(var, ".")) {
414 cmd->type = CTRL_TYPE_ERROR;
415 cmd->reply = "SET variable contains invalid characters";
416 LOGP(DLCTRL, LOGL_NOTICE, "SET variable contains invalid characters: \"%s\"\n",
417 osmo_escape_str(var, -1));
418 goto err;
419 }
Daniel Willmann1264cb42010-10-21 15:00:36 +0200420 cmd->variable = talloc_strdup(cmd, var);
421 cmd->value = talloc_strdup(cmd, val);
422 if (!cmd->variable || !cmd->value)
423 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200424
425 var = strtok_r(NULL, "", &saveptr);
426 if (var) {
427 cmd->type = CTRL_TYPE_ERROR;
428 cmd->reply = "SET with trailing characters";
429 LOGP(DLCTRL, LOGL_NOTICE, "SET with trailing characters: \"%s\"\n",
430 osmo_escape_str(var, -1));
431 goto err;
432 }
433
434 LOGP(DLCTRL, LOGL_DEBUG, "Command: SET %s = \"%s\"\n", cmd->variable,
435 osmo_escape_str(cmd->value, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200436 break;
Neels Hofmeyra5e21622017-12-16 01:18:53 +0100437#define REPLY_CASE(TYPE, NAME) \
438 case TYPE: \
439 var = strtok_r(NULL, " ", &saveptr); \
440 val = strtok_r(NULL, "", &saveptr); \
441 if (!var) { \
442 cmd->type = CTRL_TYPE_ERROR; \
443 cmd->reply = NAME " incomplete"; \
444 LOGP(DLCTRL, LOGL_NOTICE, NAME " incomplete\n"); \
445 goto err; \
446 } \
447 if (!osmo_separated_identifiers_valid(var, ".")) { \
448 cmd->type = CTRL_TYPE_ERROR; \
449 cmd->reply = NAME " variable contains invalid characters"; \
450 LOGP(DLCTRL, LOGL_NOTICE, NAME " variable contains invalid characters: \"%s\"\n", \
451 osmo_escape_str(var, -1)); \
452 goto err; \
453 } \
454 cmd->variable = talloc_strdup(cmd, var); \
455 cmd->reply = talloc_strdup(cmd, val); \
456 if (!cmd->variable || !cmd->reply) \
457 goto oom; \
458 LOGP(DLCTRL, LOGL_DEBUG, "Command: " NAME " %s: %s\n", cmd->variable, \
459 osmo_escape_str(cmd->reply, -1)); \
460 break
461 REPLY_CASE(CTRL_TYPE_GET_REPLY, "GET REPLY");
462 REPLY_CASE(CTRL_TYPE_SET_REPLY, "SET REPLY");
463 REPLY_CASE(CTRL_TYPE_TRAP, "TRAP");
464#undef REPLY_CASE
Daniel Willmann1264cb42010-10-21 15:00:36 +0200465 case CTRL_TYPE_ERROR:
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200466 var = strtok_r(NULL, "", &saveptr);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200467 if (!var) {
468 cmd->reply = "";
469 goto err;
470 }
471 cmd->reply = talloc_strdup(cmd, var);
472 if (!cmd->reply)
473 goto oom;
Neels Hofmeyr3da9aa62017-09-26 14:21:44 +0200474 LOGP(DLCTRL, LOGL_DEBUG, "Command: ERROR \"%s\"\n",
475 osmo_escape_str(cmd->reply, -1));
Daniel Willmann1264cb42010-10-21 15:00:36 +0200476 break;
477 case CTRL_TYPE_UNKNOWN:
478 default:
479 cmd->type = CTRL_TYPE_ERROR;
480 cmd->reply = "Unknown type";
481 goto err;
482 }
483
484 return cmd;
485oom:
486 cmd->type = CTRL_TYPE_ERROR;
487 cmd->id = "err";
488 cmd->reply = "OOM";
489err:
Neels Hofmeyrf2e83ad2017-12-16 01:05:25 +0100490 return cmd;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200491}
492
Harald Welteb4186822018-05-26 17:25:11 +0200493/*! Encode a given CTRL command from its parsed form into a message buffer.
494 * \param[in] cmd decoded/parsed form of to-be-encoded command
495 * \returns callee-allocated message buffer containing the encoded \a cmd; NULL on error */
Daniel Willmann1264cb42010-10-21 15:00:36 +0200496struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
497{
498 struct msgb *msg;
Max70c7d412017-02-24 13:59:14 +0100499 const char *type;
500 char *tmp;
Daniel Willmann1264cb42010-10-21 15:00:36 +0200501
502 if (!cmd->id)
503 return NULL;
504
505 msg = msgb_alloc_headroom(4096, 128, "ctrl command make");
506 if (!msg)
507 return NULL;
508
Max70c7d412017-02-24 13:59:14 +0100509 type = get_value_string(ctrl_type_vals, cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200510
511 switch (cmd->type) {
512 case CTRL_TYPE_GET:
513 if (!cmd->variable)
514 goto err;
515
516 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id, cmd->variable);
517 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200518 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200519 goto err;
520 }
521
522 msg->l2h = msgb_put(msg, strlen(tmp));
523 memcpy(msg->l2h, tmp, strlen(tmp));
524 talloc_free(tmp);
525 break;
526 case CTRL_TYPE_SET:
527 if (!cmd->variable || !cmd->value)
528 goto err;
529
530 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
531 cmd->value);
532 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200533 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200534 goto err;
535 }
536
537 msg->l2h = msgb_put(msg, strlen(tmp));
538 memcpy(msg->l2h, tmp, strlen(tmp));
539 talloc_free(tmp);
540 break;
541 case CTRL_TYPE_GET_REPLY:
542 case CTRL_TYPE_SET_REPLY:
543 case CTRL_TYPE_TRAP:
544 if (!cmd->variable || !cmd->reply)
545 goto err;
546
547 tmp = talloc_asprintf(cmd, "%s %s %s %s", type, cmd->id, cmd->variable,
548 cmd->reply);
549 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200550 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200551 goto err;
552 }
553
554 msg->l2h = msgb_put(msg, strlen(tmp));
555 memcpy(msg->l2h, tmp, strlen(tmp));
556 talloc_free(tmp);
557 break;
558 case CTRL_TYPE_ERROR:
559 if (!cmd->reply)
560 goto err;
561
562 tmp = talloc_asprintf(cmd, "%s %s %s", type, cmd->id,
563 cmd->reply);
564 if (!tmp) {
Harald Welte7fd0c832014-08-20 19:58:13 +0200565 LOGP(DLCTRL, LOGL_ERROR, "Failed to allocate cmd.\n");
Daniel Willmann1264cb42010-10-21 15:00:36 +0200566 goto err;
567 }
568
569 msg->l2h = msgb_put(msg, strlen(tmp));
570 memcpy(msg->l2h, tmp, strlen(tmp));
571 talloc_free(tmp);
572 break;
573 default:
Harald Welte7fd0c832014-08-20 19:58:13 +0200574 LOGP(DLCTRL, LOGL_NOTICE, "Unknown command type %i\n", cmd->type);
Daniel Willmann1264cb42010-10-21 15:00:36 +0200575 goto err;
576 break;
577 }
578
579 return msg;
580
581err:
582 msgb_free(msg);
583 return NULL;
584}
Harald Welte39c9e7b2014-08-22 00:28:51 +0200585
Harald Welteb4186822018-05-26 17:25:11 +0200586/*! Build a deferred control command state and keep it the per-connection list of deferred commands.
587 * This function is typically called by a ctrl command handler that wishes to defer returning a
588 * response. The reutnred state can later be used to check if the deferred command is still alive,
589 * and to respond to the specific command. This only works to defer the response to GET and SET.
590 * \param[in] ctx talloc context from whihc to allocate the ctrl_cmd_def
591 * \param[in] cmd the control command whose response is deferred
592 * \param[in] data opaque, user-defined pointer
593 * \param[in] secs number of seconds until the command times out
594 * \returns callee-allocated ctrl_cmd_def */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200595struct ctrl_cmd_def *
596ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs)
597{
598 struct ctrl_cmd_def *cd;
599
600 if (!cmd->ccon)
601 return NULL;
602
603 cd = talloc_zero(ctx, struct ctrl_cmd_def);
604
Neels Hofmeyrcdbc9af2018-04-03 16:51:49 +0200605 cmd->defer = cd;
Harald Welte39c9e7b2014-08-22 00:28:51 +0200606 cd->cmd = cmd;
607 cd->data = data;
608
609 /* add to per-connection list of deferred commands */
610 llist_add(&cd->list, &cmd->ccon->def_cmds);
611
612 return cd;
613}
614
Harald Welteb4186822018-05-26 17:25:11 +0200615/*! Determine if the given deferred control command is still alive or a zombie.
616 * \param[in] cd deferred ctrl command state
617 * \returns 0 is \a cd is still alive; 1 if it's a zombie */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200618int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd)
619{
620 /* luckily we're still alive */
621 if (cd->cmd)
622 return 0;
623
624 /* if we are a zombie, make sure we really die */
625 llist_del(&cd->list);
626 talloc_free(cd);
627
628 return 1;
629}
630
Harald Welteb4186822018-05-26 17:25:11 +0200631/*! Send the response to a deferred ctrl command.
632 * The command can only be a resply to a SET or a GET operation.
633 * \param[in] cd deferred ctrl command state
634 * \returns 0 if command sent successfully; negative on error */
Harald Welte39c9e7b2014-08-22 00:28:51 +0200635int ctrl_cmd_def_send(struct ctrl_cmd_def *cd)
636{
637 struct ctrl_cmd *cmd = cd->cmd;
638
639 int rc;
640
641 /* Deferred commands can only be responses to GET/SET or ERROR, but
642 * never TRAP or anything else */
643 switch (cmd->type) {
644 case CTRL_TYPE_GET:
645 cmd->type = CTRL_TYPE_GET_REPLY;
646 break;
647 case CTRL_TYPE_SET:
648 cmd->type = CTRL_TYPE_SET_REPLY;
649 break;
650 default:
651 cmd->type = CTRL_TYPE_ERROR;
652 }
653
654 rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd);
655
656 talloc_free(cmd);
657 llist_del(&cd->list);
658 talloc_free(cd);
659
660 return rc;
661}