Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns2_vty.c |
| 2 | * VTY interface for our GPRS Networks Service (NS) implementation. */ |
| 3 | |
| 4 | /* (C) 2009-2014 by Harald Welte <laforge@gnumonks.org> |
| 5 | * (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 6 | * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 7 | * Author: Alexander Couzens <lynxis@fe80.eu> |
| 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
| 11 | * SPDX-License-Identifier: GPL-2.0+ |
| 12 | * |
| 13 | * 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 |
| 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <unistd.h> |
| 30 | #include <errno.h> |
| 31 | #include <stdint.h> |
| 32 | |
| 33 | #include <arpa/inet.h> |
| 34 | |
| 35 | #include <osmocom/core/msgb.h> |
| 36 | #include <osmocom/core/byteswap.h> |
| 37 | #include <osmocom/core/talloc.h> |
| 38 | #include <osmocom/core/select.h> |
| 39 | #include <osmocom/core/rate_ctr.h> |
| 40 | #include <osmocom/core/socket.h> |
| 41 | #include <osmocom/core/sockaddr_str.h> |
| 42 | #include <osmocom/core/linuxlist.h> |
| 43 | #include <osmocom/core/socket.h> |
| 44 | #include <osmocom/gprs/gprs_ns2.h> |
| 45 | #include <osmocom/gsm/tlv.h> |
| 46 | #include <osmocom/vty/vty.h> |
| 47 | #include <osmocom/vty/command.h> |
| 48 | #include <osmocom/vty/logging.h> |
| 49 | #include <osmocom/vty/telnet_interface.h> |
| 50 | #include <osmocom/vty/misc.h> |
| 51 | |
| 52 | #include "gprs_ns2_internal.h" |
| 53 | |
| 54 | struct ns2_vty_priv { |
| 55 | /* global listen */ |
| 56 | struct osmo_sockaddr_str udp; |
| 57 | struct osmo_sockaddr_str frgreaddr; |
| 58 | int dscp; |
| 59 | enum gprs_ns2_vc_mode vc_mode; |
| 60 | /* force vc mode if another configuration forces |
| 61 | * the vc mode. E.g. SNS configuration */ |
| 62 | bool force_vc_mode; |
Daniel Willmann | 4fb27a8 | 2020-09-25 15:39:46 +0200 | [diff] [blame] | 63 | const char *force_vc_mode_reason; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 64 | bool frgre; |
| 65 | |
| 66 | struct llist_head vtyvc; |
| 67 | }; |
| 68 | |
| 69 | struct ns2_vty_vc { |
| 70 | struct llist_head list; |
| 71 | |
| 72 | struct osmo_sockaddr_str remote; |
| 73 | enum gprs_ns_ll ll; |
| 74 | |
| 75 | /* old vty code doesnt support multiple NSVCI per NSEI */ |
| 76 | uint16_t nsei; |
| 77 | uint16_t nsvci; |
| 78 | uint16_t frdlci; |
| 79 | |
| 80 | bool remote_end_is_sgsn; |
| 81 | bool configured; |
| 82 | }; |
| 83 | |
| 84 | static struct gprs_ns2_inst *vty_nsi = NULL; |
| 85 | static struct ns2_vty_priv priv; |
| 86 | |
| 87 | /* FIXME: this should go to some common file as it is copied |
| 88 | * in vty_interface.c of the BSC */ |
| 89 | static const struct value_string gprs_ns_timer_strs[] = { |
| 90 | { 0, "tns-block" }, |
| 91 | { 1, "tns-block-retries" }, |
| 92 | { 2, "tns-reset" }, |
| 93 | { 3, "tns-reset-retries" }, |
| 94 | { 4, "tns-test" }, |
| 95 | { 5, "tns-alive" }, |
| 96 | { 6, "tns-alive-retries" }, |
| 97 | { 7, "tsns-prov" }, |
| 98 | { 0, NULL } |
| 99 | }; |
| 100 | |
| 101 | static void log_set_nsvc_filter(struct log_target *target, |
| 102 | struct gprs_ns2_vc *nsvc) |
| 103 | { |
| 104 | if (nsvc) { |
| 105 | target->filter_map |= (1 << LOG_FLT_GB_NSVC); |
| 106 | target->filter_data[LOG_FLT_GB_NSVC] = nsvc; |
| 107 | } else if (target->filter_data[LOG_FLT_GB_NSVC]) { |
| 108 | target->filter_map = ~(1 << LOG_FLT_GB_NSVC); |
| 109 | target->filter_data[LOG_FLT_GB_NSVC] = NULL; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static struct cmd_node ns_node = { |
| 114 | L_NS_NODE, |
| 115 | "%s(config-ns)# ", |
| 116 | 1, |
| 117 | }; |
| 118 | |
| 119 | static struct ns2_vty_vc *vtyvc_alloc(uint16_t nsei) { |
| 120 | struct ns2_vty_vc *vtyvc = talloc_zero(vty_nsi, struct ns2_vty_vc); |
| 121 | if (!vtyvc) |
| 122 | return vtyvc; |
| 123 | |
| 124 | vtyvc->nsei = nsei; |
| 125 | |
| 126 | llist_add(&vtyvc->list, &priv.vtyvc); |
| 127 | |
| 128 | return vtyvc; |
| 129 | } |
| 130 | |
| 131 | static void ns2_vc_free(struct ns2_vty_vc *vtyvc) { |
| 132 | if (!vtyvc) |
| 133 | return; |
| 134 | |
| 135 | llist_del(&vtyvc->list); |
| 136 | talloc_free(vtyvc); |
| 137 | } |
| 138 | |
| 139 | static struct ns2_vty_vc *vtyvc_by_nsei(uint16_t nsei, bool alloc_missing) { |
| 140 | struct ns2_vty_vc *vtyvc; |
Alexander Couzens | 1f0625f | 2020-09-23 18:22:20 +0200 | [diff] [blame] | 141 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 142 | llist_for_each_entry(vtyvc, &priv.vtyvc, list) { |
| 143 | if (vtyvc->nsei == nsei) |
| 144 | return vtyvc; |
| 145 | } |
| 146 | |
Alexander Couzens | 1f0625f | 2020-09-23 18:22:20 +0200 | [diff] [blame] | 147 | if (!alloc_missing) |
| 148 | return NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 149 | |
Alexander Couzens | 1f0625f | 2020-09-23 18:22:20 +0200 | [diff] [blame] | 150 | vtyvc = vtyvc_alloc(nsei); |
| 151 | if (!vtyvc) |
| 152 | return vtyvc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 153 | |
Alexander Couzens | 1f0625f | 2020-09-23 18:22:20 +0200 | [diff] [blame] | 154 | vtyvc->nsei = nsei; |
| 155 | return vtyvc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int config_write_ns(struct vty *vty) |
| 159 | { |
| 160 | struct ns2_vty_vc *vtyvc; |
| 161 | unsigned int i; |
| 162 | struct osmo_sockaddr_str sockstr; |
| 163 | |
| 164 | vty_out(vty, "ns%s", VTY_NEWLINE); |
| 165 | |
| 166 | /* global configuration must be written first, as some of it may be |
| 167 | * relevant when creating the NSE/NSVC later below */ |
| 168 | |
| 169 | vty_out(vty, " encapsulation framerelay-gre enabled %u%s", |
| 170 | priv.frgre ? 1 : 0, VTY_NEWLINE); |
| 171 | |
| 172 | if (priv.frgre) { |
| 173 | if (strlen(priv.frgreaddr.ip)) { |
| 174 | vty_out(vty, " encapsulation framerelay-gre local-ip %s%s", |
| 175 | sockstr.ip, VTY_NEWLINE); |
| 176 | } |
| 177 | } else { |
| 178 | if (strlen(priv.udp.ip)) { |
| 179 | vty_out(vty, " encapsulation udp local-ip %s%s", |
| 180 | priv.udp.ip, VTY_NEWLINE); |
| 181 | } |
| 182 | |
| 183 | if (priv.udp.port) |
| 184 | vty_out(vty, " encapsulation udp local-port %u%s", |
| 185 | priv.udp.port, VTY_NEWLINE); |
| 186 | } |
| 187 | |
| 188 | if (priv.dscp) |
| 189 | vty_out(vty, " encapsulation udp dscp %d%s", |
| 190 | priv.dscp, VTY_NEWLINE); |
| 191 | |
| 192 | vty_out(vty, " encapsulation udp use-reset-block-unblock %s%s", |
| 193 | priv.vc_mode == NS2_VC_MODE_BLOCKRESET ? "enabled" : "disabled", VTY_NEWLINE); |
| 194 | |
| 195 | llist_for_each_entry(vtyvc, &priv.vtyvc, list) { |
| 196 | vty_out(vty, " nse %u nsvci %u%s", |
| 197 | vtyvc->nsei, vtyvc->nsvci, VTY_NEWLINE); |
| 198 | |
| 199 | vty_out(vty, " nse %u remote-role %s%s", |
| 200 | vtyvc->nsei, vtyvc->remote_end_is_sgsn ? "sgsn" : "bss", |
| 201 | VTY_NEWLINE); |
| 202 | |
| 203 | switch (vtyvc->ll) { |
| 204 | case GPRS_NS_LL_UDP: |
| 205 | vty_out(vty, " nse %u encapsulation udp%s", vtyvc->nsei, VTY_NEWLINE); |
| 206 | vty_out(vty, " nse %u remote-ip %s%s", |
| 207 | vtyvc->nsei, |
| 208 | vtyvc->remote.ip, |
| 209 | VTY_NEWLINE); |
| 210 | vty_out(vty, " nse %u remote-port %u%s", |
| 211 | vtyvc->nsei, vtyvc->remote.port, |
| 212 | VTY_NEWLINE); |
| 213 | break; |
| 214 | case GPRS_NS_LL_FR_GRE: |
| 215 | vty_out(vty, " nse %u encapsulation framerelay-gre%s", |
| 216 | vtyvc->nsei, VTY_NEWLINE); |
| 217 | vty_out(vty, " nse %u remote-ip %s%s", |
| 218 | vtyvc->nsei, |
| 219 | vtyvc->remote.ip, |
| 220 | VTY_NEWLINE); |
| 221 | vty_out(vty, " nse %u fr-dlci %u%s", |
| 222 | vtyvc->nsei, vtyvc->frdlci, |
| 223 | VTY_NEWLINE); |
| 224 | break; |
| 225 | default: |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | for (i = 0; i < ARRAY_SIZE(vty_nsi->timeout); i++) |
| 231 | vty_out(vty, " timer %s %u%s", |
| 232 | get_value_string(gprs_ns_timer_strs, i), |
| 233 | vty_nsi->timeout[i], VTY_NEWLINE); |
| 234 | |
| 235 | return CMD_SUCCESS; |
| 236 | } |
| 237 | |
| 238 | DEFUN(cfg_ns, cfg_ns_cmd, |
| 239 | "ns", |
| 240 | "Configure the GPRS Network Service") |
| 241 | { |
| 242 | vty->node = L_NS_NODE; |
| 243 | return CMD_SUCCESS; |
| 244 | } |
| 245 | |
| 246 | static void dump_nsvc(struct vty *vty, struct gprs_ns2_vc *nsvc, bool stats) |
| 247 | { |
| 248 | struct osmo_sockaddr_str remote; |
| 249 | struct osmo_sockaddr_str local; |
| 250 | struct osmo_sockaddr *sockaddr; |
| 251 | |
| 252 | switch (nsvc->ll) { |
| 253 | case GPRS_NS_LL_UDP: { |
| 254 | sockaddr = gprs_ns2_ip_vc_sockaddr(nsvc); |
| 255 | if (!sockaddr) { |
| 256 | vty_out(vty, "unknown"); |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | if (osmo_sockaddr_str_from_sockaddr( |
| 261 | &remote, |
| 262 | &sockaddr->u.sas)) { |
| 263 | vty_out(vty, "unknown"); |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | vty_out(vty, "%s:%u <> %s:%u", local.ip, local.port, remote.ip, remote.port); |
| 268 | break; |
| 269 | } |
| 270 | case GPRS_NS_LL_FR_GRE: |
| 271 | /* TODO: implement dump_nse for FR GRE */ |
| 272 | case GPRS_NS_LL_E1: |
| 273 | /* TODO: implement dump_nse for E1 */ |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | vty_out(vty, "Remote: %s ", |
| 278 | gprs_ns2_ll_str(nsvc)); |
| 279 | |
| 280 | vty_out(vty, "%s%s", nsvc->ll == GPRS_NS_LL_UDP ? "UDP" : "FR-GRE", VTY_NEWLINE); |
| 281 | |
| 282 | if (stats) { |
| 283 | vty_out_rate_ctr_group(vty, " ", nsvc->ctrg); |
| 284 | vty_out_stat_item_group(vty, " ", nsvc->statg); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static void dump_nse(struct vty *vty, const struct gprs_ns2_nse *nse, bool stats, bool persistent_only) |
| 289 | { |
| 290 | struct gprs_ns2_vc *nsvc; |
| 291 | |
| 292 | vty_out(vty, "NSEI %5u%s", |
| 293 | nse->nsei, VTY_NEWLINE); |
| 294 | |
| 295 | gprs_ns2_sns_dump_vty(vty, nse, stats); |
| 296 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 297 | if (persistent_only) { |
| 298 | if (nsvc->persistent) |
| 299 | dump_nsvc(vty, nsvc, stats); |
| 300 | } else { |
| 301 | dump_nsvc(vty, nsvc, stats); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Alexander Couzens | 22f3471 | 2020-10-02 02:34:39 +0200 | [diff] [blame] | 306 | static void dump_bind(struct vty *vty, const struct gprs_ns2_vc_bind *bind, bool stats) |
| 307 | { |
| 308 | if (bind->dump_vty) |
| 309 | bind->dump_vty(bind, vty, stats); |
| 310 | } |
| 311 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 312 | static void dump_ns(struct vty *vty, const struct gprs_ns2_inst *nsi, bool stats, bool persistent_only) |
| 313 | { |
Alexander Couzens | 22f3471 | 2020-10-02 02:34:39 +0200 | [diff] [blame] | 314 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 315 | struct gprs_ns2_nse *nse; |
| 316 | |
Alexander Couzens | 22f3471 | 2020-10-02 02:34:39 +0200 | [diff] [blame] | 317 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 318 | dump_bind(vty, bind, stats); |
| 319 | } |
| 320 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 321 | llist_for_each_entry(nse, &nsi->nse, list) { |
| 322 | dump_nse(vty, nse, stats, persistent_only); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | } |
| 326 | |
| 327 | DEFUN(show_ns, show_ns_cmd, "show ns", |
| 328 | SHOW_STR "Display information about the NS protocol") |
| 329 | { |
| 330 | dump_ns(vty, vty_nsi, false, false); |
| 331 | return CMD_SUCCESS; |
| 332 | } |
| 333 | |
| 334 | DEFUN(show_ns_stats, show_ns_stats_cmd, "show ns stats", |
| 335 | SHOW_STR |
| 336 | "Display information about the NS protocol\n" |
| 337 | "Include statistics\n") |
| 338 | { |
| 339 | dump_ns(vty, vty_nsi, true, false); |
| 340 | return CMD_SUCCESS; |
| 341 | } |
| 342 | |
| 343 | DEFUN(show_ns_pers, show_ns_pers_cmd, "show ns persistent", |
| 344 | SHOW_STR |
| 345 | "Display information about the NS protocol\n" |
| 346 | "Show only persistent NS\n") |
| 347 | { |
| 348 | dump_ns(vty, vty_nsi, true, true); |
| 349 | return CMD_SUCCESS; |
| 350 | } |
| 351 | |
| 352 | DEFUN(show_nse, show_nse_cmd, "show ns (nsei|nsvc) <0-65535> [stats]", |
| 353 | SHOW_STR "Display information about the NS protocol\n" |
| 354 | "Select one NSE by its NSE Identifier\n" |
| 355 | "Select one NSE by its NS-VC Identifier\n" |
| 356 | "The Identifier of selected type\n" |
| 357 | "Include Statistics\n") |
| 358 | { |
| 359 | struct gprs_ns2_inst *nsi = vty_nsi; |
| 360 | struct gprs_ns2_nse *nse; |
| 361 | struct gprs_ns2_vc *nsvc; |
| 362 | uint16_t id = atoi(argv[1]); |
| 363 | bool show_stats = false; |
| 364 | |
| 365 | if (argc >= 3) |
| 366 | show_stats = true; |
| 367 | |
| 368 | if (!strcmp(argv[0], "nsei")) { |
| 369 | nse = gprs_ns2_nse_by_nsei(nsi, id); |
| 370 | if (!nse) { |
| 371 | return CMD_WARNING; |
| 372 | } |
| 373 | |
| 374 | dump_nse(vty, nse, show_stats, false); |
| 375 | } else { |
| 376 | nsvc = gprs_ns2_nsvc_by_nsvci(nsi, id); |
| 377 | |
| 378 | if (!nsvc) { |
| 379 | vty_out(vty, "No such NS Entity%s", VTY_NEWLINE); |
| 380 | return CMD_WARNING; |
| 381 | } |
| 382 | |
| 383 | dump_nsvc(vty, nsvc, show_stats); |
| 384 | } |
| 385 | |
| 386 | return CMD_SUCCESS; |
| 387 | } |
| 388 | |
| 389 | #define NSE_CMD_STR "Persistent NS Entity\n" "NS Entity ID (NSEI)\n" |
| 390 | |
| 391 | DEFUN(cfg_nse_nsvc, cfg_nse_nsvci_cmd, |
| 392 | "nse <0-65535> nsvci <0-65535>", |
| 393 | NSE_CMD_STR |
| 394 | "NS Virtual Connection\n" |
| 395 | "NS Virtual Connection ID (NSVCI)\n" |
| 396 | ) |
| 397 | { |
| 398 | struct ns2_vty_vc *vtyvc; |
| 399 | |
| 400 | uint16_t nsei = atoi(argv[0]); |
| 401 | uint16_t nsvci = atoi(argv[1]); |
| 402 | |
| 403 | vtyvc = vtyvc_by_nsei(nsei, true); |
| 404 | if (!vtyvc) { |
| 405 | vty_out(vty, "Can not allocate space %s", VTY_NEWLINE); |
| 406 | return CMD_WARNING; |
| 407 | } |
| 408 | |
| 409 | vtyvc->nsvci = nsvci; |
| 410 | |
| 411 | return CMD_SUCCESS; |
| 412 | } |
| 413 | |
| 414 | DEFUN(cfg_nse_remoteip, cfg_nse_remoteip_cmd, |
| 415 | "nse <0-65535> remote-ip " VTY_IPV46_CMD, |
| 416 | NSE_CMD_STR |
| 417 | "Remote IP Address\n" |
Alexander Couzens | c82c40a | 2020-09-24 05:55:48 +0200 | [diff] [blame] | 418 | "Remote IPv4 Address\n" |
| 419 | "Remote IPv6 Address\n") |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 420 | { |
| 421 | uint16_t nsei = atoi(argv[0]); |
| 422 | struct ns2_vty_vc *vtyvc; |
| 423 | |
| 424 | vtyvc = vtyvc_by_nsei(nsei, true); |
| 425 | if (!vtyvc) { |
| 426 | vty_out(vty, "Can not allocate space %s", VTY_NEWLINE); |
| 427 | return CMD_WARNING; |
| 428 | } |
| 429 | |
| 430 | osmo_sockaddr_str_from_str2(&vtyvc->remote, argv[1]); |
| 431 | |
| 432 | return CMD_SUCCESS; |
| 433 | } |
| 434 | |
| 435 | DEFUN(cfg_nse_remoteport, cfg_nse_remoteport_cmd, |
| 436 | "nse <0-65535> remote-port <0-65535>", |
| 437 | NSE_CMD_STR |
| 438 | "Remote UDP Port\n" |
| 439 | "Remote UDP Port Number\n") |
| 440 | { |
| 441 | uint16_t nsei = atoi(argv[0]); |
| 442 | uint16_t port = atoi(argv[1]); |
| 443 | struct ns2_vty_vc *vtyvc; |
| 444 | |
| 445 | vtyvc = vtyvc_by_nsei(nsei, true); |
| 446 | if (!vtyvc) { |
| 447 | vty_out(vty, "Can not allocate space %s", VTY_NEWLINE); |
| 448 | return CMD_WARNING; |
| 449 | } |
| 450 | |
| 451 | vtyvc->remote.port = port; |
| 452 | |
| 453 | return CMD_SUCCESS; |
| 454 | } |
| 455 | |
| 456 | DEFUN(cfg_nse_fr_dlci, cfg_nse_fr_dlci_cmd, |
| 457 | "nse <0-65535> fr-dlci <16-1007>", |
| 458 | NSE_CMD_STR |
| 459 | "Frame Relay DLCI\n" |
| 460 | "Frame Relay DLCI Number\n") |
| 461 | { |
| 462 | uint16_t nsei = atoi(argv[0]); |
| 463 | uint16_t dlci = atoi(argv[1]); |
| 464 | struct ns2_vty_vc *vtyvc; |
| 465 | |
| 466 | vtyvc = vtyvc_by_nsei(nsei, true); |
| 467 | if (!vtyvc) { |
| 468 | vty_out(vty, "Can not allocate space %s", VTY_NEWLINE); |
| 469 | return CMD_WARNING; |
| 470 | } |
| 471 | |
| 472 | if (vtyvc->ll != GPRS_NS_LL_FR_GRE) { |
| 473 | vty_out(vty, "Warning: seting FR DLCI on non-FR NSE%s", |
| 474 | VTY_NEWLINE); |
| 475 | } |
| 476 | |
| 477 | vtyvc->frdlci = dlci; |
| 478 | |
| 479 | return CMD_SUCCESS; |
| 480 | } |
| 481 | |
| 482 | DEFUN(cfg_nse_encaps, cfg_nse_encaps_cmd, |
| 483 | "nse <0-65535> encapsulation (udp|framerelay-gre)", |
| 484 | NSE_CMD_STR |
| 485 | "Encapsulation for NS\n" |
| 486 | "UDP/IP Encapsulation\n" "Frame-Relay/GRE/IP Encapsulation\n") |
| 487 | { |
| 488 | uint16_t nsei = atoi(argv[0]); |
| 489 | struct ns2_vty_vc *vtyvc; |
| 490 | |
| 491 | vtyvc = vtyvc_by_nsei(nsei, true); |
| 492 | if (!vtyvc) { |
| 493 | vty_out(vty, "Can not allocate space %s", VTY_NEWLINE); |
| 494 | return CMD_WARNING; |
| 495 | } |
| 496 | |
| 497 | if (!strcmp(argv[1], "udp")) |
| 498 | vtyvc->ll = GPRS_NS_LL_UDP; |
| 499 | else |
| 500 | vtyvc->ll = GPRS_NS_LL_FR_GRE; |
| 501 | |
| 502 | return CMD_SUCCESS; |
| 503 | } |
| 504 | |
| 505 | DEFUN(cfg_nse_remoterole, cfg_nse_remoterole_cmd, |
| 506 | "nse <0-65535> remote-role (sgsn|bss)", |
| 507 | NSE_CMD_STR |
| 508 | "Remote NSE Role\n" |
| 509 | "Remote Peer is SGSN\n" |
| 510 | "Remote Peer is BSS\n") |
| 511 | { |
| 512 | uint16_t nsei = atoi(argv[0]); |
| 513 | struct ns2_vty_vc *vtyvc; |
| 514 | |
| 515 | vtyvc = vtyvc_by_nsei(nsei, true); |
| 516 | if (!vtyvc) { |
| 517 | vty_out(vty, "Can not allocate space %s", VTY_NEWLINE); |
| 518 | return CMD_WARNING; |
| 519 | } |
| 520 | |
| 521 | if (!strcmp(argv[1], "sgsn")) |
| 522 | vtyvc->remote_end_is_sgsn = 1; |
| 523 | else |
| 524 | vtyvc->remote_end_is_sgsn = 0; |
| 525 | |
| 526 | return CMD_SUCCESS; |
| 527 | } |
| 528 | |
| 529 | DEFUN(cfg_no_nse, cfg_no_nse_cmd, |
| 530 | "no nse <0-65535>", |
| 531 | "Delete Persistent NS Entity\n" |
| 532 | "Delete " NSE_CMD_STR) |
| 533 | { |
| 534 | uint16_t nsei = atoi(argv[0]); |
| 535 | struct ns2_vty_vc *vtyvc; |
| 536 | |
| 537 | vtyvc = vtyvc_by_nsei(nsei, false); |
| 538 | if (!vtyvc) { |
| 539 | vty_out(vty, "The NSE %d does not exists.%s", nsei, VTY_NEWLINE); |
| 540 | return CMD_WARNING; |
| 541 | } |
| 542 | |
| 543 | ns2_vc_free(vtyvc); |
| 544 | |
| 545 | return CMD_SUCCESS; |
| 546 | } |
| 547 | |
| 548 | DEFUN(cfg_ns_timer, cfg_ns_timer_cmd, |
| 549 | "timer " NS_TIMERS " <0-65535>", |
| 550 | "Network Service Timer\n" |
| 551 | NS_TIMERS_HELP "Timer Value\n") |
| 552 | { |
| 553 | int idx = get_string_value(gprs_ns_timer_strs, argv[0]); |
| 554 | int val = atoi(argv[1]); |
| 555 | |
| 556 | if (idx < 0 || idx >= ARRAY_SIZE(vty_nsi->timeout)) |
| 557 | return CMD_WARNING; |
| 558 | |
| 559 | vty_nsi->timeout[idx] = val; |
| 560 | |
| 561 | return CMD_SUCCESS; |
| 562 | } |
| 563 | |
| 564 | #define ENCAPS_STR "NS encapsulation options\n" |
| 565 | |
| 566 | DEFUN(cfg_nsip_local_ip, cfg_nsip_local_ip_cmd, |
| 567 | "encapsulation udp local-ip " VTY_IPV46_CMD, |
| 568 | ENCAPS_STR "NS over UDP Encapsulation\n" |
| 569 | "Set the IP address on which we listen for NS/UDP\n" |
Alexander Couzens | c82c40a | 2020-09-24 05:55:48 +0200 | [diff] [blame] | 570 | "IPv4 Address\n" |
| 571 | "IPv6 Address\n") |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 572 | { |
| 573 | osmo_sockaddr_str_from_str2(&priv.udp, argv[0]); |
| 574 | |
| 575 | return CMD_SUCCESS; |
| 576 | } |
| 577 | |
| 578 | DEFUN(cfg_nsip_local_port, cfg_nsip_local_port_cmd, |
| 579 | "encapsulation udp local-port <0-65535>", |
| 580 | ENCAPS_STR "NS over UDP Encapsulation\n" |
| 581 | "Set the UDP port on which we listen for NS/UDP\n" |
| 582 | "UDP port number\n") |
| 583 | { |
| 584 | unsigned int port = atoi(argv[0]); |
| 585 | |
| 586 | priv.udp.port = port; |
| 587 | |
| 588 | return CMD_SUCCESS; |
| 589 | } |
| 590 | |
| 591 | DEFUN(cfg_nsip_dscp, cfg_nsip_dscp_cmd, |
| 592 | "encapsulation udp dscp <0-255>", |
| 593 | ENCAPS_STR "NS over UDP Encapsulation\n" |
| 594 | "Set DSCP/TOS on the UDP socket\n" "DSCP Value\n") |
| 595 | { |
| 596 | int dscp = atoi(argv[0]); |
| 597 | struct gprs_ns2_vc_bind *bind; |
| 598 | |
| 599 | priv.dscp = dscp; |
| 600 | |
| 601 | llist_for_each_entry(bind, &vty_nsi->binding, list) { |
| 602 | if (gprs_ns2_is_ip_bind(bind)) |
| 603 | gprs_ns2_ip_bind_set_dscp(bind, dscp); |
| 604 | } |
| 605 | |
| 606 | return CMD_SUCCESS; |
| 607 | } |
| 608 | |
| 609 | DEFUN(cfg_nsip_res_block_unblock, cfg_nsip_res_block_unblock_cmd, |
| 610 | "encapsulation udp use-reset-block-unblock (enabled|disabled)", |
| 611 | ENCAPS_STR "NS over UDP Encapsulation\n" |
| 612 | "Use NS-{RESET,BLOCK,UNBLOCK} procedures in violation of 3GPP TS 48.016\n" |
| 613 | "Enable NS-{RESET,BLOCK,UNBLOCK}\n" |
| 614 | "Disable NS-{RESET,BLOCK,UNBLOCK}\n") |
| 615 | { |
| 616 | enum gprs_ns2_vc_mode vc_mode; |
| 617 | struct gprs_ns2_vc_bind *bind; |
| 618 | |
| 619 | if (!strcmp(argv[0], "enabled")) |
| 620 | vc_mode = NS2_VC_MODE_BLOCKRESET; |
| 621 | else |
| 622 | vc_mode = NS2_VC_MODE_ALIVE; |
| 623 | |
| 624 | if (priv.force_vc_mode) { |
| 625 | if (priv.vc_mode != vc_mode) |
| 626 | { |
| 627 | vty_out(vty, "Ignoring use-reset-block because it's already set by %s.%s", |
| 628 | priv.force_vc_mode_reason, VTY_NEWLINE); |
| 629 | return CMD_WARNING; |
| 630 | } |
| 631 | |
| 632 | return CMD_SUCCESS; |
| 633 | } |
| 634 | |
| 635 | priv.vc_mode = vc_mode; |
| 636 | |
| 637 | llist_for_each_entry(bind, &vty_nsi->binding, list) { |
| 638 | gprs_ns2_bind_set_mode(bind, priv.vc_mode); |
| 639 | } |
| 640 | |
| 641 | return CMD_SUCCESS; |
| 642 | } |
| 643 | |
| 644 | DEFUN(cfg_frgre_local_ip, cfg_frgre_local_ip_cmd, |
| 645 | "encapsulation framerelay-gre local-ip " VTY_IPV46_CMD, |
| 646 | ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n" |
| 647 | "Set the IP address on which we listen for NS/FR/GRE\n" |
Alexander Couzens | c82c40a | 2020-09-24 05:55:48 +0200 | [diff] [blame] | 648 | "IPv4 Address\n" |
| 649 | "IPv6 Address\n") |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 650 | { |
| 651 | osmo_sockaddr_str_from_str2(&priv.frgreaddr, argv[0]); |
| 652 | |
| 653 | return CMD_SUCCESS; |
| 654 | } |
| 655 | |
| 656 | DEFUN(cfg_frgre_enable, cfg_frgre_enable_cmd, |
| 657 | "encapsulation framerelay-gre enabled (1|0)", |
| 658 | ENCAPS_STR "NS over Frame Relay over GRE Encapsulation\n" |
| 659 | "Enable or disable Frame Relay over GRE\n" |
| 660 | "Enable\n" "Disable\n") |
| 661 | { |
| 662 | int enabled = atoi(argv[0]); |
| 663 | |
| 664 | priv.frgre = enabled; |
| 665 | |
| 666 | return CMD_SUCCESS; |
| 667 | } |
| 668 | |
| 669 | /* TODO: allow vty to reset/block/unblock nsvc/nsei */ |
| 670 | |
| 671 | /* TODO: add filter for NSEI as ns1 code does */ |
| 672 | /* TODO: add filter for single connection by description */ |
| 673 | DEFUN(logging_fltr_nsvc, |
| 674 | logging_fltr_nsvc_cmd, |
| 675 | "logging filter nsvc nsvci <0-65535>", |
| 676 | LOGGING_STR FILTER_STR |
| 677 | "Filter based on NS Virtual Connection\n" |
| 678 | "Identify NS-VC by NSVCI\n" |
| 679 | "Numeric identifier\n") |
| 680 | { |
| 681 | struct log_target *tgt; |
| 682 | struct gprs_ns2_vc *nsvc; |
| 683 | uint16_t id = atoi(argv[1]); |
| 684 | |
| 685 | log_tgt_mutex_lock(); |
| 686 | tgt = osmo_log_vty2tgt(vty); |
| 687 | if (!tgt) { |
| 688 | log_tgt_mutex_unlock(); |
| 689 | return CMD_WARNING; |
| 690 | } |
| 691 | |
| 692 | nsvc = gprs_ns2_nsvc_by_nsvci(vty_nsi, id); |
| 693 | if (!nsvc) { |
| 694 | vty_out(vty, "No NS-VC by that identifier%s", VTY_NEWLINE); |
| 695 | log_tgt_mutex_unlock(); |
| 696 | return CMD_WARNING; |
| 697 | } |
| 698 | |
| 699 | log_set_nsvc_filter(tgt, nsvc); |
| 700 | log_tgt_mutex_unlock(); |
| 701 | return CMD_SUCCESS; |
| 702 | } |
| 703 | |
Alexander Couzens | 1fac6f7 | 2020-10-01 19:08:38 +0200 | [diff] [blame] | 704 | /** |
| 705 | * gprs_ns2_vty_init initialize the vty |
| 706 | * \param[inout] nsi |
| 707 | * \param[in] default_bind set the default address to bind to. Can be NULL. |
| 708 | * \return 0 on success |
| 709 | */ |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 710 | int gprs_ns2_vty_init(struct gprs_ns2_inst *nsi, |
| 711 | const struct osmo_sockaddr_str *default_bind) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 712 | { |
| 713 | static bool vty_elements_installed = false; |
| 714 | |
| 715 | vty_nsi = nsi; |
| 716 | memset(&priv, 0, sizeof(struct ns2_vty_priv)); |
| 717 | INIT_LLIST_HEAD(&priv.vtyvc); |
| 718 | priv.vc_mode = NS2_VC_MODE_BLOCKRESET; |
Alexander Couzens | 1fac6f7 | 2020-10-01 19:08:38 +0200 | [diff] [blame] | 719 | if (default_bind) |
| 720 | memcpy(&priv.udp, default_bind, sizeof(*default_bind)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 721 | |
| 722 | /* Regression test code may call this function repeatedly, so make sure |
| 723 | * that VTY elements are not duplicated, which would assert. */ |
| 724 | if (vty_elements_installed) |
| 725 | return 0; |
| 726 | vty_elements_installed = true; |
| 727 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 728 | install_lib_element_ve(&show_ns_cmd); |
| 729 | install_lib_element_ve(&show_ns_stats_cmd); |
| 730 | install_lib_element_ve(&show_ns_pers_cmd); |
| 731 | install_lib_element_ve(&show_nse_cmd); |
| 732 | install_lib_element_ve(&logging_fltr_nsvc_cmd); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 733 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 734 | install_lib_element(CFG_LOG_NODE, &logging_fltr_nsvc_cmd); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 735 | |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 736 | install_lib_element(CONFIG_NODE, &cfg_ns_cmd); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 737 | install_node(&ns_node, config_write_ns); |
Vadim Yanitskiy | 8e7c496 | 2020-10-04 15:37:31 +0700 | [diff] [blame] | 738 | install_lib_element(L_NS_NODE, &cfg_nse_nsvci_cmd); |
| 739 | install_lib_element(L_NS_NODE, &cfg_nse_remoteip_cmd); |
| 740 | install_lib_element(L_NS_NODE, &cfg_nse_remoteport_cmd); |
| 741 | install_lib_element(L_NS_NODE, &cfg_nse_fr_dlci_cmd); |
| 742 | install_lib_element(L_NS_NODE, &cfg_nse_encaps_cmd); |
| 743 | install_lib_element(L_NS_NODE, &cfg_nse_remoterole_cmd); |
| 744 | install_lib_element(L_NS_NODE, &cfg_no_nse_cmd); |
| 745 | install_lib_element(L_NS_NODE, &cfg_ns_timer_cmd); |
| 746 | install_lib_element(L_NS_NODE, &cfg_nsip_local_ip_cmd); |
| 747 | install_lib_element(L_NS_NODE, &cfg_nsip_local_port_cmd); |
| 748 | install_lib_element(L_NS_NODE, &cfg_nsip_dscp_cmd); |
| 749 | install_lib_element(L_NS_NODE, &cfg_nsip_res_block_unblock_cmd); |
| 750 | install_lib_element(L_NS_NODE, &cfg_frgre_enable_cmd); |
| 751 | install_lib_element(L_NS_NODE, &cfg_frgre_local_ip_cmd); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 752 | |
| 753 | /* TODO: nsvc/nsei command to reset states or reset/block/unblock nsei/nsvcs */ |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | /*! |
| 759 | * \brief gprs_ns2_vty_create parse the vty tree into ns nodes |
| 760 | * It has to be in different steps to ensure the bind is created before creating VCs. |
| 761 | * \return 0 on success |
| 762 | */ |
| 763 | int gprs_ns2_vty_create() { |
| 764 | struct ns2_vty_vc *vtyvc; |
| 765 | struct gprs_ns2_vc_bind *bind; |
| 766 | struct gprs_ns2_nse *nse; |
| 767 | struct gprs_ns2_vc *nsvc; |
| 768 | struct osmo_sockaddr sockaddr; |
| 769 | |
| 770 | if (!vty_nsi) |
| 771 | return -1; |
| 772 | |
| 773 | /* create binds, only support a single bind. either FR or UDP */ |
| 774 | if (priv.frgre) { |
| 775 | /* TODO not yet supported !*/ |
| 776 | return -1; |
| 777 | } else { |
| 778 | /* UDP */ |
| 779 | osmo_sockaddr_str_to_sockaddr(&priv.udp, &sockaddr.u.sas); |
Alexander Couzens | 477ffb0 | 2020-10-01 19:05:28 +0200 | [diff] [blame] | 780 | if (gprs_ns2_ip_bind(vty_nsi, &sockaddr, priv.dscp, &bind)) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 781 | /* TODO: could not bind on the specific address */ |
| 782 | return -1; |
| 783 | } |
| 784 | gprs_ns2_bind_set_mode(bind, priv.vc_mode); |
| 785 | } |
| 786 | |
| 787 | /* create vcs */ |
| 788 | llist_for_each_entry(vtyvc, &priv.vtyvc, list) { |
| 789 | if (strlen(vtyvc->remote.ip) == 0) { |
| 790 | /* Invalid IP for VC */ |
| 791 | continue; |
| 792 | } |
| 793 | |
| 794 | if (!vtyvc->remote.port) { |
| 795 | /* Invalid port for VC */ |
| 796 | continue; |
| 797 | } |
| 798 | |
| 799 | if (osmo_sockaddr_str_to_sockaddr(&vtyvc->remote, &sockaddr.u.sas)) { |
| 800 | /* Invalid sockaddr for VC */ |
| 801 | continue; |
| 802 | } |
| 803 | |
| 804 | nse = gprs_ns2_nse_by_nsei(vty_nsi, vtyvc->nsei); |
| 805 | if (!nse) { |
| 806 | nse = gprs_ns2_create_nse(vty_nsi, vtyvc->nsei); |
| 807 | if (!nse) { |
| 808 | /* Could not create NSE for VTY */ |
| 809 | continue; |
| 810 | } |
| 811 | } |
| 812 | nse->persistent = true; |
| 813 | |
Alexander Couzens | d745a0e | 2020-10-07 00:50:00 +0200 | [diff] [blame] | 814 | nsvc = gprs_ns2_ip_connect(bind, |
| 815 | &sockaddr, |
| 816 | nse, |
| 817 | vtyvc->nsvci); |
| 818 | if (!nsvc) { |
| 819 | /* Could not create NSVC, connect failed */ |
| 820 | continue; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 821 | } |
Alexander Couzens | d745a0e | 2020-10-07 00:50:00 +0200 | [diff] [blame] | 822 | nsvc->persistent = true; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | /*! |
| 830 | * \brief ns2_vty_bind_apply will be called when a new bind is created to apply vty settings |
| 831 | * \param bind |
| 832 | * \return |
| 833 | */ |
| 834 | void ns2_vty_bind_apply(struct gprs_ns2_vc_bind *bind) |
| 835 | { |
| 836 | gprs_ns2_bind_set_mode(bind, priv.vc_mode); |
| 837 | } |
| 838 | |
| 839 | /*! |
| 840 | * \brief ns2_vty_force_vc_mode force a mode and prevents the vty from overwriting it. |
| 841 | * \param force if true mode and reason will be set. false to allow modification via vty. |
| 842 | * \param mode |
| 843 | * \param reason A description shown to the user when a vty command wants to change the mode. |
| 844 | */ |
Daniel Willmann | 4fb27a8 | 2020-09-25 15:39:46 +0200 | [diff] [blame] | 845 | void gprs_ns2_vty_force_vc_mode(bool force, enum gprs_ns2_vc_mode mode, const char *reason) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 846 | { |
| 847 | priv.force_vc_mode = force; |
| 848 | |
| 849 | if (force) { |
| 850 | priv.vc_mode = mode; |
| 851 | priv.force_vc_mode_reason = reason; |
| 852 | } |
| 853 | } |