blob: a1943e64a986fa7c114fcf1d6bb75b5a8982ba69 [file] [log] [blame]
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +02001/* E1 vty interface */
2/* (C) 2011 by Harald Welte <laforge@gnumonks.org>
3 * All Rights Reserved
4 *
Harald Welte323d39d2017-11-13 01:09:21 +09005 * SPDX-License-Identifier: AGPL-3.0+
6 *
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +02007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21#include "internal.h"
22
23#include <stdlib.h>
24#include <unistd.h>
Harald Welted4f8f682011-08-19 13:28:48 +020025#include <string.h>
Stefan Sperlingb24efa52018-08-28 14:03:41 +020026#include <sys/un.h>
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020027
Harald Weltef2737fc2011-08-16 14:30:10 +020028#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/talloc.h>
30#include <osmocom/core/utils.h>
31#include <osmocom/gsm/gsm_utils.h>
32
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020033#include <osmocom/vty/command.h>
34#include <osmocom/vty/buffer.h>
35#include <osmocom/vty/vty.h>
36#include <osmocom/vty/logging.h>
Harald Weltef2737fc2011-08-16 14:30:10 +020037#include <osmocom/vty/misc.h>
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020038#include <osmocom/vty/telnet_interface.h>
39
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020040#include <osmocom/abis/e1_input.h>
Harald Weltef2737fc2011-08-16 14:30:10 +020041
42/* CONFIG */
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020043
Sylvain Munautb559a532019-05-09 11:14:26 +020044#define E1_DRIVER_NAMES "(misdn|misdn_lapd|dahdi|e1d|ipa|unixsocket)"
Harald Weltece307b42011-08-21 01:18:05 +020045#define E1_DRIVER_HELP "mISDN supported E1 Card (kernel LAPD)\n" \
46 "mISDN supported E1 Card (userspace LAPD)\n" \
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020047 "DAHDI supported E1/T1/J1 Card\n" \
Holger Hans Peter Freyther36bac9a2012-07-20 23:49:52 +020048 "IPA TCP/IP input\n" \
Alexander Couzensbeb10ef2016-11-01 22:05:13 +010049 "HSL TCP/IP input\n" \
50 "Unix socket input\n"
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020051
Harald Welte601a9c72011-08-16 14:17:49 +020052#define E1_LINE_HELP "Configure E1/T1/J1 Line\n" "Line Number\n"
53
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020054DEFUN(cfg_e1line_driver, cfg_e1_line_driver_cmd,
55 "e1_line <0-255> driver " E1_DRIVER_NAMES,
Harald Welte601a9c72011-08-16 14:17:49 +020056 E1_LINE_HELP "Set driver for this line\n"
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020057 E1_DRIVER_HELP)
58{
59 struct e1inp_line *line;
60 int e1_nr = atoi(argv[0]);
61
Pablo Neira Ayuso3832c4f2011-07-07 17:47:26 +020062 line = e1inp_line_find(e1_nr);
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +020063 if (line) {
64 vty_out(vty, "%% Line %d already exists%s", e1_nr, VTY_NEWLINE);
65 return CMD_WARNING;
66 }
67 line = e1inp_line_create(e1_nr, argv[1]);
68 if (!line) {
69 vty_out(vty, "%% Error creating line %d%s", e1_nr, VTY_NEWLINE);
70 return CMD_WARNING;
71 }
72
73 return CMD_SUCCESS;
74}
75
Harald Weltec2889512011-09-13 23:49:04 +010076DEFUN(cfg_e1line_port, cfg_e1_line_port_cmd,
Harald Welte356918f2011-09-26 22:54:51 +020077 "e1_line <0-255> port <0-255>",
78 E1_LINE_HELP "Set physical port/span/card number\n"
Harald Weltec2889512011-09-13 23:49:04 +010079 "E1/T1 Port/Span/Card number\n")
80{
81 struct e1inp_line *line;
82 int e1_nr = atoi(argv[0]);
83
84 line = e1inp_line_find(e1_nr);
85 if (!line) {
86 vty_out(vty, "%% Line %d doesn't exist%s", e1_nr, VTY_NEWLINE);
87 return CMD_WARNING;
88 }
89
90 line->port_nr = atoi(argv[1]);
91
92 return CMD_SUCCESS;
93}
94
Alexander Couzensbeb10ef2016-11-01 22:05:13 +010095DEFUN(cfg_e1line_socket, cfg_e1_line_socket_cmd,
96 "e1_line <0-255> socket .SOCKET",
97 E1_LINE_HELP "Set socket path for unixsocket\n"
98 "socket path\n")
99{
100 struct e1inp_line *line;
101 int e1_nr = atoi(argv[0]);
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200102 struct sockaddr_un sun;
103
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200104 /* Don't exceed the maximum unix socket path length, including a NUL byte. See the unix(7) man page.*/
105 if (strlen(argv[1]) > sizeof(sun.sun_path) - 1) {
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200106 vty_out(vty, "%% Socket path length exceeds %zd bytes: '%s'%s",
Stefan Sperling0d7d0b02018-09-20 17:34:31 +0200107 sizeof(sun.sun_path) - 1, argv[1], VTY_NEWLINE);
Stefan Sperlingb24efa52018-08-28 14:03:41 +0200108 return CMD_WARNING;
109 }
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100110
111 line = e1inp_line_find(e1_nr);
112 if (!line) {
113 vty_out(vty, "%% Line %d doesn't exist%s", e1_nr, VTY_NEWLINE);
114 return CMD_WARNING;
115 }
116
117 line->sock_path = talloc_strdup(line, argv[1]);
118
119 return CMD_SUCCESS;
120}
121
Jacob Erlbeck86dae842014-01-16 18:10:37 +0100122#define KEEPALIVE_HELP "Enable keep-alive probing\n"
123static int set_keepalive_params(struct vty *vty, int e1_nr,
124 int idle, int num_probes, int probe_interval)
125{
126 struct e1inp_line *line = e1inp_line_find(e1_nr);
127
128 if (!line) {
129 vty_out(vty, "%% Line %d doesn't exist%s", e1_nr, VTY_NEWLINE);
130 return CMD_WARNING;
131 }
132 if (!line->driver->has_keepalive && num_probes != 0) {
133 vty_out(vty, "%% Driver '%s' does not support keep alive%s",
134 line->driver->name, VTY_NEWLINE);
135 return CMD_WARNING;
136 }
137
138 line->keepalive_idle_timeout = idle;
139 line->keepalive_num_probes = num_probes;
140 line->keepalive_probe_interval = probe_interval;
141
142 return CMD_SUCCESS;
143}
144
145DEFUN(cfg_e1line_keepalive, cfg_e1_line_keepalive_cmd,
146 "e1_line <0-255> keepalive",
147 E1_LINE_HELP KEEPALIVE_HELP)
148{
149 return set_keepalive_params(vty, atoi(argv[0]),
150 E1INP_USE_DEFAULT, E1INP_USE_DEFAULT,
151 E1INP_USE_DEFAULT);
152}
153
154DEFUN(cfg_e1line_keepalive_params, cfg_e1_line_keepalive_params_cmd,
155 "e1_line <0-255> keepalive <1-300> <1-20> <1-300>",
156 E1_LINE_HELP KEEPALIVE_HELP
157 "Idle interval in seconds before probes are sent\n"
158 "Number of probes to sent\n"
159 "Delay between probe packets in seconds\n")
160{
161 return set_keepalive_params(vty, atoi(argv[0]),
162 atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
163}
164
165DEFUN(cfg_e1line_no_keepalive, cfg_e1_line_no_keepalive_cmd,
166 "no e1_line <0-255> keepalive",
167 NO_STR E1_LINE_HELP KEEPALIVE_HELP)
168{
169 return set_keepalive_params(vty, atoi(argv[0]), 0, 0, 0);
170}
171
Harald Welte601a9c72011-08-16 14:17:49 +0200172DEFUN(cfg_e1line_name, cfg_e1_line_name_cmd,
173 "e1_line <0-255> name .LINE",
174 E1_LINE_HELP "Set name for this line\n" "Human readable name\n")
175{
176 struct e1inp_line *line;
177 int e1_nr = atoi(argv[0]);
178
179 line = e1inp_line_find(e1_nr);
180 if (!line) {
181 vty_out(vty, "%% Line %d doesn't exist%s", e1_nr, VTY_NEWLINE);
182 return CMD_WARNING;
183 }
184 if (line->name) {
185 talloc_free((void *)line->name);
186 line->name = NULL;
187 }
188 line->name = talloc_strdup(line, argv[1]);
189
190 return CMD_SUCCESS;
191}
192
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +0200193DEFUN(cfg_e1inp, cfg_e1inp_cmd,
194 "e1_input",
195 "Configure E1/T1/J1 TDM input\n")
196{
Harald Weltecc2241b2011-07-19 16:06:06 +0200197 vty->node = L_E1INP_NODE;
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +0200198
199 return CMD_SUCCESS;
200}
201
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100202DEFUN(cfg_ipa_bind,
203 cfg_ipa_bind_cmd,
204 "ipa bind A.B.C.D",
205 "ipa driver config\n"
206 "Set ipa local bind address\n"
207 "Listen on this IP address (default 0.0.0.0)\n")
208{
209 e1inp_ipa_set_bind_addr(argv[0]);
210 return CMD_SUCCESS;
211}
212
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +0200213static int e1inp_config_write(struct vty *vty)
214{
215 struct e1inp_line *line;
216
217 if (llist_empty(&e1inp_line_list))
218 return CMD_SUCCESS;
219
220 vty_out(vty, "e1_input%s", VTY_NEWLINE);
221
222 llist_for_each_entry(line, &e1inp_line_list, list) {
223 vty_out(vty, " e1_line %u driver %s%s", line->num,
224 line->driver->name, VTY_NEWLINE);
Harald Weltec2889512011-09-13 23:49:04 +0100225 vty_out(vty, " e1_line %u port %u%s", line->num,
226 line->port_nr, VTY_NEWLINE);
Harald Welte601a9c72011-08-16 14:17:49 +0200227 if (line->name)
228 vty_out(vty, " e1_line %u name %s%s", line->num,
229 line->name, VTY_NEWLINE);
Jacob Erlbeck86dae842014-01-16 18:10:37 +0100230 if (!line->keepalive_num_probes)
231 vty_out(vty, " no e1_line %u keepalive%s", line->num,
232 VTY_NEWLINE);
233 else if (line->keepalive_idle_timeout == E1INP_USE_DEFAULT &&
234 line->keepalive_num_probes == E1INP_USE_DEFAULT &&
235 line->keepalive_probe_interval == E1INP_USE_DEFAULT)
236 vty_out(vty, " e1_line %u keepalive%s", line->num,
237 VTY_NEWLINE);
238 else
239 vty_out(vty, " e1_line %u keepalive %d %d %d%s",
240 line->num,
241 line->keepalive_idle_timeout,
242 line->keepalive_num_probes,
243 line->keepalive_probe_interval,
244 VTY_NEWLINE);
245
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +0200246 }
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100247
248 const char *ipa_bind = e1inp_ipa_get_bind_addr();
249 if (ipa_bind && (strcmp(ipa_bind, "0.0.0.0") != 0))
250 vty_out(vty, " ipa bind %s%s",
251 ipa_bind, VTY_NEWLINE);
252
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +0200253 return CMD_SUCCESS;
254}
255
Harald Weltef2737fc2011-08-16 14:30:10 +0200256/* SHOW */
257
Harald Welte6e37c592011-08-11 12:43:31 +0200258static void e1drv_dump_vty(struct vty *vty, struct e1inp_driver *drv)
259{
260 vty_out(vty, "E1 Input Driver %s%s", drv->name, VTY_NEWLINE);
261}
262
263DEFUN(show_e1drv,
264 show_e1drv_cmd,
265 "show e1_driver",
266 SHOW_STR "Display information about available E1 drivers\n")
267{
268 struct e1inp_driver *drv;
269
270 llist_for_each_entry(drv, &e1inp_driver_list, list)
271 e1drv_dump_vty(vty, drv);
272
273 return CMD_SUCCESS;
274}
275
Harald Weltef2737fc2011-08-16 14:30:10 +0200276static void e1line_dump_vty(struct vty *vty, struct e1inp_line *line,
277 int stats)
Harald Welte6e37c592011-08-11 12:43:31 +0200278{
279 vty_out(vty, "E1 Line Number %u, Name %s, Driver %s%s",
280 line->num, line->name ? line->name : "",
281 line->driver->name, VTY_NEWLINE);
Harald Weltefe05cf52011-09-26 23:18:41 +0200282 if (line->driver->vty_show)
283 line->driver->vty_show(vty, line);
Harald Weltef2737fc2011-08-16 14:30:10 +0200284 if (stats)
285 vty_out_rate_ctr_group(vty, " ", line->rate_ctr);
Harald Welte6e37c592011-08-11 12:43:31 +0200286}
287
288DEFUN(show_e1line,
289 show_e1line_cmd,
Harald Weltef2737fc2011-08-16 14:30:10 +0200290 "show e1_line [line_nr] [stats]",
Harald Welte6e37c592011-08-11 12:43:31 +0200291 SHOW_STR "Display information about a E1 line\n"
Holger Hans Peter Freyther9e8f1c02012-07-21 00:09:52 +0200292 "E1 Line Number\n" "Include statistics\n")
Harald Welte6e37c592011-08-11 12:43:31 +0200293{
294 struct e1inp_line *line;
Harald Weltef2737fc2011-08-16 14:30:10 +0200295 int stats = 0;
Harald Welte6e37c592011-08-11 12:43:31 +0200296
Harald Weltef2737fc2011-08-16 14:30:10 +0200297 if (argc >= 1 && strcmp(argv[0], "stats")) {
Harald Welte6e37c592011-08-11 12:43:31 +0200298 int num = atoi(argv[0]);
Harald Weltef2737fc2011-08-16 14:30:10 +0200299 if (argc >= 2)
300 stats = 1;
Harald Welte6e37c592011-08-11 12:43:31 +0200301 llist_for_each_entry(line, &e1inp_line_list, list) {
302 if (line->num == num) {
Harald Weltef2737fc2011-08-16 14:30:10 +0200303 e1line_dump_vty(vty, line, stats);
Harald Welte6e37c592011-08-11 12:43:31 +0200304 return CMD_SUCCESS;
305 }
306 }
307 return CMD_WARNING;
308 }
309
Harald Weltef2737fc2011-08-16 14:30:10 +0200310 if (argc >= 1 && !strcmp(argv[0], "stats"))
311 stats = 1;
312
Harald Welte6e37c592011-08-11 12:43:31 +0200313 llist_for_each_entry(line, &e1inp_line_list, list)
Harald Weltef2737fc2011-08-16 14:30:10 +0200314 e1line_dump_vty(vty, line, stats);
Harald Welte6e37c592011-08-11 12:43:31 +0200315
316 return CMD_SUCCESS;
317}
318
319static void e1ts_dump_vty(struct vty *vty, struct e1inp_ts *ts)
320{
321 if (ts->type == E1INP_TS_TYPE_NONE)
322 return;
323 vty_out(vty, "E1 Timeslot %2u of Line %u is Type %s%s",
324 ts->num, ts->line->num, e1inp_tstype_name(ts->type),
325 VTY_NEWLINE);
326}
327
328DEFUN(show_e1ts,
329 show_e1ts_cmd,
330 "show e1_timeslot [line_nr] [ts_nr]",
331 SHOW_STR "Display information about a E1 timeslot\n"
332 "E1 Line Number\n" "E1 Timeslot Number\n")
333{
334 struct e1inp_line *line = NULL;
335 struct e1inp_ts *ts;
336 int ts_nr;
337
Holger Hans Peter Freyther8cbd9f42013-07-04 20:00:33 +0200338 if (argc <= 0) {
Harald Welte6e37c592011-08-11 12:43:31 +0200339 llist_for_each_entry(line, &e1inp_line_list, list) {
Harald Weltec2889512011-09-13 23:49:04 +0100340 for (ts_nr = 0; ts_nr < line->num_ts; ts_nr++) {
Harald Welte6e37c592011-08-11 12:43:31 +0200341 ts = &line->ts[ts_nr];
342 e1ts_dump_vty(vty, ts);
343 }
344 }
345 return CMD_SUCCESS;
346 }
347 if (argc >= 1) {
348 int num = atoi(argv[0]);
349 struct e1inp_line *l;
350 llist_for_each_entry(l, &e1inp_line_list, list) {
351 if (l->num == num) {
352 line = l;
353 break;
354 }
355 }
356 if (!line) {
357 vty_out(vty, "E1 line %s is invalid%s",
358 argv[0], VTY_NEWLINE);
359 return CMD_WARNING;
360 }
361 }
362 if (argc >= 2) {
363 ts_nr = atoi(argv[1]);
Harald Weltec2889512011-09-13 23:49:04 +0100364 if (ts_nr >= line->num_ts) {
Harald Welte6e37c592011-08-11 12:43:31 +0200365 vty_out(vty, "E1 timeslot %s is invalid%s",
366 argv[1], VTY_NEWLINE);
367 return CMD_WARNING;
368 }
369 ts = &line->ts[ts_nr];
370 e1ts_dump_vty(vty, ts);
371 return CMD_SUCCESS;
372 } else {
Harald Weltec2889512011-09-13 23:49:04 +0100373 for (ts_nr = 0; ts_nr < line->num_ts; ts_nr++) {
Harald Welte6e37c592011-08-11 12:43:31 +0200374 ts = &line->ts[ts_nr];
375 e1ts_dump_vty(vty, ts);
376 }
377 return CMD_SUCCESS;
378 }
379 return CMD_SUCCESS;
380}
381
382
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +0200383struct cmd_node e1inp_node = {
Harald Weltecc2241b2011-07-19 16:06:06 +0200384 L_E1INP_NODE,
Jacob Erlbeck1c9dcc12013-11-11 14:13:13 +0100385 "%s(config-e1_input)# ",
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +0200386 1,
387};
388
389int e1inp_vty_init(void)
390{
391 install_element(CONFIG_NODE, &cfg_e1inp_cmd);
392 install_node(&e1inp_node, e1inp_config_write);
Jacob Erlbeck37f06952013-11-11 14:13:12 +0100393
Harald Weltecc2241b2011-07-19 16:06:06 +0200394 install_element(L_E1INP_NODE, &cfg_e1_line_driver_cmd);
Harald Weltec2889512011-09-13 23:49:04 +0100395 install_element(L_E1INP_NODE, &cfg_e1_line_port_cmd);
Alexander Couzensbeb10ef2016-11-01 22:05:13 +0100396 install_element(L_E1INP_NODE, &cfg_e1_line_socket_cmd);
Harald Welte601a9c72011-08-16 14:17:49 +0200397 install_element(L_E1INP_NODE, &cfg_e1_line_name_cmd);
Jacob Erlbeck86dae842014-01-16 18:10:37 +0100398 install_element(L_E1INP_NODE, &cfg_e1_line_keepalive_cmd);
399 install_element(L_E1INP_NODE, &cfg_e1_line_keepalive_params_cmd);
400 install_element(L_E1INP_NODE, &cfg_e1_line_no_keepalive_cmd);
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +0200401
Neels Hofmeyr0db1d432016-02-22 13:29:09 +0100402 install_element(L_E1INP_NODE, &cfg_ipa_bind_cmd);
403
Harald Welte6e37c592011-08-11 12:43:31 +0200404 install_element_ve(&show_e1drv_cmd);
405 install_element_ve(&show_e1line_cmd);
406 install_element_ve(&show_e1ts_cmd);
407
Pablo Neira Ayuso262aee82011-07-05 19:17:08 +0200408 return 0;
409}