Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1 | /* Quagga VTY implementation to manage identity of neighboring BSS cells for inter-BSC handover. */ |
| 2 | /* (C) 2018 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 7 | * Author: Stefan Sperling <ssperling@sysmocom.de> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include <osmocom/vty/command.h> |
| 29 | #include <osmocom/gsm/gsm0808.h> |
| 30 | #include <osmocom/sigtran/osmo_ss7.h> |
| 31 | #include <osmocom/sigtran/sccp_helpers.h> |
| 32 | |
| 33 | #include <osmocom/msc/vty.h> |
| 34 | #include <osmocom/msc/neighbor_ident.h> |
| 35 | #include <osmocom/msc/gsm_data.h> |
| 36 | #include <osmocom/msc/ran_infra.h> |
| 37 | #include <osmocom/msc/cell_id_list.h> |
| 38 | |
| 39 | #define NEIGHBOR_ADD_CMD "neighbor" |
| 40 | #define NEIGHBOR_ADD_DOC "Add Handover target configuration\n" |
| 41 | |
| 42 | #define NEIGHBOR_DEL_CMD "no neighbor" |
| 43 | #define NEIGHBOR_DEL_DOC NO_STR "Remove Handover target\n" |
| 44 | |
| 45 | #define NEIGHBOR_SHOW_CMD "show neighbor" |
| 46 | #define NEIGHBOR_SHOW_DOC SHOW_STR "Show Handover targets\n" |
| 47 | |
| 48 | #define RAN_TYPE_PARAMS "(a|iu)" |
| 49 | #define RAN_TYPE_DOC "Neighbor on GERAN-A\n" "Neighbor on UTRAN-Iu\n" |
| 50 | |
| 51 | #define RAN_PC_TOKEN "ran-pc" |
| 52 | #define MSC_IPA_NAME_TOKEN "msc-ipa-name" |
| 53 | #define HO_TARGET_PARAMS "("RAN_PC_TOKEN"|"MSC_IPA_NAME_TOKEN") RAN_PC_OR_MSC_IPA_NAME" |
| 54 | #define HO_TARGET_DOC "SCCP point code of RAN peer\n" "GSUP IPA name of target MSC\n" "Point code or MSC IPA name value\n" |
| 55 | |
| 56 | #define LAC_PARAMS "lac <0-65535>" |
| 57 | #define LAC_ARGC 1 |
| 58 | #define LAC_DOC "Handover target cell by LAC\n" "LAC\n" |
| 59 | |
| 60 | #define LAC_CI_PARAMS "lac-ci <0-65535> <0-65535>" |
| 61 | #define LAC_CI_ARGC 2 |
| 62 | #define LAC_CI_DOC "Handover target cell by LAC and CI\n" "LAC\n" "CI\n" |
| 63 | |
| 64 | #define CGI_PARAMS "cgi <0-999> <0-999> <0-65535> <0-65535>" |
| 65 | #define CGI_ARGC 4 |
| 66 | #define CGI_DOC "Handover target cell by Cell-Global Identifier (MCC, MNC, LAC, CI)\n" "MCC\n" "MNC\n" "LAC\n" "CI\n" |
| 67 | |
| 68 | static struct gsm_network *gsmnet = NULL; |
| 69 | |
| 70 | static void write_neighbor_ident_cell(struct vty *vty, const struct neighbor_ident_entry *e, |
| 71 | const struct gsm0808_cell_id *cid) |
| 72 | { |
| 73 | vty_out(vty, " " NEIGHBOR_ADD_CMD " "); |
| 74 | |
| 75 | switch (e->addr.ran_type) { |
| 76 | case OSMO_RAT_GERAN_A: |
| 77 | vty_out(vty, "a"); |
| 78 | break; |
| 79 | case OSMO_RAT_UTRAN_IU: |
| 80 | vty_out(vty, "iu"); |
| 81 | break; |
| 82 | default: |
| 83 | vty_out(vty, "<Unsupported-RAN-type>"); |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | vty_out(vty, " "); |
| 88 | |
| 89 | switch (cid->id_discr) { |
| 90 | case CELL_IDENT_LAC: |
| 91 | vty_out(vty, "lac %u", cid->id.lac); |
| 92 | break; |
| 93 | case CELL_IDENT_LAC_AND_CI: |
| 94 | vty_out(vty, "lac-ci %u %u", |
| 95 | cid->id.lac_and_ci.lac, |
| 96 | cid->id.lac_and_ci.ci); |
| 97 | break; |
| 98 | case CELL_IDENT_WHOLE_GLOBAL: |
| 99 | vty_out(vty, "cgi %s %s %u %u", |
| 100 | osmo_mcc_name(cid->id.global.lai.plmn.mcc), |
| 101 | osmo_mnc_name(cid->id.global.lai.plmn.mnc, cid->id.global.lai.plmn.mnc_3_digits), |
| 102 | cid->id.global.lai.lac, |
| 103 | cid->id.global.cell_identity); |
| 104 | break; |
| 105 | default: |
| 106 | vty_out(vty, "<Unsupported-Cell-Identity>"); |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | vty_out(vty, " "); |
| 111 | |
| 112 | switch (e->addr.type) { |
| 113 | case MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER: |
| 114 | vty_out(vty, RAN_PC_TOKEN " %s", e->addr.local_ran_peer_pc_str); |
| 115 | break; |
| 116 | case MSC_NEIGHBOR_TYPE_REMOTE_MSC: |
| 117 | vty_out(vty, MSC_IPA_NAME_TOKEN " %s", osmo_escape_str(e->addr.remote_msc_ipa_name.buf, |
| 118 | e->addr.remote_msc_ipa_name.len)); |
| 119 | break; |
| 120 | default: |
| 121 | vty_out(vty, "<Unsupported-target-type>"); |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | vty_out(vty, "%s", VTY_NEWLINE); |
| 126 | } |
| 127 | |
| 128 | static void write_neighbor_ident_entry(struct vty *vty, const struct neighbor_ident_entry *e) |
| 129 | { |
| 130 | struct cell_id_list_entry *le; |
| 131 | |
| 132 | llist_for_each_entry(le, &e->cell_ids, entry) { |
| 133 | write_neighbor_ident_cell(vty, e, &le->cell_id); |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | static void write_neighbor_ident_entry_by_cell(struct vty *vty, const struct neighbor_ident_entry *e, |
| 139 | const struct gsm0808_cell_id *cid) |
| 140 | { |
| 141 | struct cell_id_list_entry *le; |
| 142 | |
| 143 | llist_for_each_entry(le, &e->cell_ids, entry) { |
| 144 | if (!gsm0808_cell_ids_match(&le->cell_id, cid, false)) |
| 145 | continue; |
| 146 | write_neighbor_ident_cell(vty, e, &le->cell_id); |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | void neighbor_ident_vty_write(struct vty *vty) |
| 152 | { |
| 153 | const struct neighbor_ident_entry *e; |
| 154 | |
| 155 | llist_for_each_entry(e, &gsmnet->neighbor_ident_list, entry) { |
| 156 | write_neighbor_ident_entry(vty, e); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void neighbor_ident_vty_write_by_ran_type(struct vty *vty, enum osmo_rat_type ran_type) |
| 161 | { |
| 162 | const struct neighbor_ident_entry *e; |
| 163 | |
| 164 | llist_for_each_entry(e, &gsmnet->neighbor_ident_list, entry) { |
| 165 | if (e->addr.ran_type != ran_type) |
| 166 | continue; |
| 167 | write_neighbor_ident_entry(vty, e); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void neighbor_ident_vty_write_by_cell(struct vty *vty, enum osmo_rat_type ran_type, const struct gsm0808_cell_id *cid) |
| 172 | { |
| 173 | struct neighbor_ident_entry *e; |
| 174 | |
| 175 | llist_for_each_entry(e, &gsmnet->neighbor_ident_list, entry) { |
| 176 | if (ran_type != OSMO_RAT_UNKNOWN |
| 177 | && e->addr.ran_type != ran_type) |
| 178 | continue; |
| 179 | write_neighbor_ident_entry_by_cell(vty, e, cid); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static struct gsm0808_cell_id *parse_lac(struct vty *vty, const char **argv) |
| 184 | { |
| 185 | static struct gsm0808_cell_id cell_id; |
| 186 | cell_id = (struct gsm0808_cell_id){ |
| 187 | .id_discr = CELL_IDENT_LAC, |
| 188 | .id.lac = atoi(argv[0]), |
| 189 | }; |
| 190 | return &cell_id; |
| 191 | } |
| 192 | |
| 193 | static struct gsm0808_cell_id *parse_lac_ci(struct vty *vty, const char **argv) |
| 194 | { |
| 195 | static struct gsm0808_cell_id cell_id; |
| 196 | cell_id = (struct gsm0808_cell_id){ |
| 197 | .id_discr = CELL_IDENT_LAC_AND_CI, |
| 198 | .id.lac_and_ci = { |
| 199 | .lac = atoi(argv[0]), |
| 200 | .ci = atoi(argv[1]), |
| 201 | }, |
| 202 | }; |
| 203 | return &cell_id; |
| 204 | } |
| 205 | |
| 206 | static struct gsm0808_cell_id *parse_cgi(struct vty *vty, const char **argv) |
| 207 | { |
| 208 | static struct gsm0808_cell_id cell_id; |
| 209 | cell_id = (struct gsm0808_cell_id){ |
| 210 | .id_discr = CELL_IDENT_WHOLE_GLOBAL, |
| 211 | }; |
| 212 | struct osmo_cell_global_id *cgi = &cell_id.id.global; |
| 213 | const char *mcc = argv[0]; |
| 214 | const char *mnc = argv[1]; |
| 215 | const char *lac = argv[2]; |
| 216 | const char *ci = argv[3]; |
| 217 | |
| 218 | if (osmo_mcc_from_str(mcc, &cgi->lai.plmn.mcc)) { |
| 219 | vty_out(vty, "%% Error decoding MCC: %s%s", mcc, VTY_NEWLINE); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | if (osmo_mnc_from_str(mnc, &cgi->lai.plmn.mnc, &cgi->lai.plmn.mnc_3_digits)) { |
| 224 | vty_out(vty, "%% Error decoding MNC: %s%s", mnc, VTY_NEWLINE); |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | cgi->lai.lac = atoi(lac); |
| 229 | cgi->cell_identity = atoi(ci); |
| 230 | return &cell_id; |
| 231 | } |
| 232 | |
| 233 | static int add_neighbor(struct vty *vty, struct neighbor_ident_addr *addr, const struct gsm0808_cell_id *cell_id) |
| 234 | { |
| 235 | if (!neighbor_ident_add(&gsmnet->neighbor_ident_list, addr, cell_id)) { |
| 236 | vty_out(vty, "%% Error: cannot add cell %s to neighbor %s%s", |
| 237 | gsm0808_cell_id_name(cell_id), neighbor_ident_addr_name(addr), |
| 238 | VTY_NEWLINE); |
| 239 | return CMD_WARNING; |
| 240 | } |
| 241 | return CMD_SUCCESS; |
| 242 | } |
| 243 | |
| 244 | static enum osmo_rat_type parse_ran_type(struct vty *vty, const char *ran_type_str) |
| 245 | { |
| 246 | if (!strcmp(ran_type_str, "a")) |
| 247 | return OSMO_RAT_GERAN_A; |
| 248 | else if (!strcmp(ran_type_str, "iu")) |
| 249 | return OSMO_RAT_UTRAN_IU; |
| 250 | vty_out(vty, "%% Error: cannot parse RAN type argument %s%s", |
| 251 | osmo_quote_str(ran_type_str, -1), VTY_NEWLINE); |
| 252 | return OSMO_RAT_UNKNOWN; |
| 253 | } |
| 254 | |
| 255 | static enum msc_neighbor_type parse_target_type(struct vty *vty, const char *target_type_str) |
| 256 | { |
| 257 | if (osmo_str_startswith(RAN_PC_TOKEN, target_type_str)) |
| 258 | return MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER; |
| 259 | if (osmo_str_startswith(MSC_IPA_NAME_TOKEN, target_type_str)) |
| 260 | return MSC_NEIGHBOR_TYPE_REMOTE_MSC; |
| 261 | vty_out(vty, "%% Unknown Handover target type: %s%s\n", |
| 262 | osmo_quote_str(target_type_str, -1), VTY_NEWLINE); |
| 263 | return MSC_NEIGHBOR_TYPE_NONE; |
| 264 | } |
| 265 | |
| 266 | static int parse_ho_target_addr(struct vty *vty, |
| 267 | struct neighbor_ident_addr *nia, |
| 268 | enum osmo_rat_type ran_type, |
| 269 | const char **argv) |
| 270 | { |
| 271 | const char *target_type_str = argv[0]; |
| 272 | const char *arg_str = argv[1]; |
| 273 | int rc; |
| 274 | |
| 275 | *nia = (struct neighbor_ident_addr){ |
| 276 | .type = parse_target_type(vty, target_type_str), |
| 277 | .ran_type = ran_type, |
| 278 | }; |
| 279 | if (nia->ran_type == OSMO_RAT_UNKNOWN) |
| 280 | return -1; |
| 281 | |
| 282 | switch (nia->type) { |
| 283 | case MSC_NEIGHBOR_TYPE_LOCAL_RAN_PEER: |
| 284 | rc = osmo_strlcpy(nia->local_ran_peer_pc_str, arg_str, sizeof(nia->local_ran_peer_pc_str)); |
| 285 | if (rc < 1 || rc >= sizeof(nia->local_ran_peer_pc_str)) { |
| 286 | vty_out(vty, "%% Invalid RAN peer point-code string: %s%s", osmo_quote_str(arg_str, -1), VTY_NEWLINE); |
| 287 | return -1; |
| 288 | } |
| 289 | return 0; |
| 290 | case MSC_NEIGHBOR_TYPE_REMOTE_MSC: |
| 291 | if (msc_ipa_name_from_str(&nia->remote_msc_ipa_name, arg_str)) { |
| 292 | vty_out(vty, "%% Invalid MSC IPA name: %s%s", osmo_quote_str(arg_str, -1), VTY_NEWLINE); |
| 293 | return -1; |
| 294 | } |
| 295 | return 0; |
| 296 | default: |
| 297 | return -1; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | #define DEFUN_CELL(id_name, ID_NAME) \ |
| 302 | \ |
| 303 | DEFUN(cfg_neighbor_add_##id_name, cfg_neighbor_add_##id_name##_cmd, \ |
| 304 | NEIGHBOR_ADD_CMD " "RAN_TYPE_PARAMS " " ID_NAME##_PARAMS " " HO_TARGET_PARAMS, \ |
| 305 | NEIGHBOR_ADD_DOC RAN_TYPE_DOC ID_NAME##_DOC HO_TARGET_DOC) \ |
| 306 | { \ |
| 307 | struct neighbor_ident_addr addr; \ |
| 308 | if (parse_ho_target_addr(vty, &addr, \ |
| 309 | parse_ran_type(vty, argv[0]), \ |
| 310 | argv + 1 + ID_NAME##_ARGC)) \ |
| 311 | return CMD_WARNING; \ |
| 312 | return add_neighbor(vty, &addr, parse_##id_name(vty, argv + 1)); \ |
| 313 | } \ |
| 314 | \ |
| 315 | DEFUN(show_neighbor_ran_##id_name, show_neighbor_ran_##id_name##_cmd, \ |
| 316 | NEIGHBOR_SHOW_CMD " " RAN_TYPE_PARAMS " " ID_NAME##_PARAMS, \ |
| 317 | NEIGHBOR_SHOW_DOC RAN_TYPE_DOC ID_NAME##_DOC RAN_TYPE_DOC) \ |
| 318 | { \ |
| 319 | neighbor_ident_vty_write_by_cell(vty, \ |
| 320 | parse_ran_type(vty, argv[0]), \ |
| 321 | parse_##id_name(vty, argv + 1)); \ |
| 322 | return CMD_SUCCESS; \ |
| 323 | } \ |
| 324 | \ |
| 325 | DEFUN(show_neighbor_##id_name, show_neighbor_##id_name##_cmd, \ |
| 326 | NEIGHBOR_SHOW_CMD " "ID_NAME##_PARAMS, \ |
| 327 | NEIGHBOR_SHOW_DOC ID_NAME##_DOC) \ |
| 328 | { \ |
| 329 | neighbor_ident_vty_write_by_cell(vty, OSMO_RAT_UNKNOWN, parse_##id_name(vty, argv)); \ |
| 330 | return CMD_SUCCESS; \ |
| 331 | } |
| 332 | |
| 333 | DEFUN_CELL(lac, LAC) |
| 334 | DEFUN_CELL(lac_ci, LAC_CI) |
| 335 | DEFUN_CELL(cgi, CGI) |
| 336 | |
| 337 | static int del_by_addr(struct vty *vty, const struct neighbor_ident_addr *addr) |
| 338 | { |
| 339 | const struct neighbor_ident_entry *e = neighbor_ident_find_by_addr(&gsmnet->neighbor_ident_list, addr); |
| 340 | |
| 341 | if (!e) { |
| 342 | vty_out(vty, "%% Cannot remove, no such neighbor: %s%s", |
| 343 | neighbor_ident_addr_name(addr), VTY_NEWLINE); |
| 344 | return CMD_WARNING; |
| 345 | } |
| 346 | |
| 347 | neighbor_ident_del(e); |
| 348 | vty_out(vty, "%% Removed neighbor %s%s", neighbor_ident_addr_name(addr), VTY_NEWLINE); |
| 349 | |
| 350 | return CMD_SUCCESS; |
| 351 | } |
| 352 | |
| 353 | DEFUN(cfg_del_neighbor, cfg_del_neighbor_cmd, |
| 354 | NEIGHBOR_DEL_CMD " " RAN_TYPE_PARAMS " "HO_TARGET_PARAMS, |
| 355 | NEIGHBOR_DEL_DOC RAN_TYPE_DOC HO_TARGET_DOC) |
| 356 | { |
| 357 | struct neighbor_ident_addr addr; |
| 358 | if (parse_ho_target_addr(vty, &addr, |
| 359 | parse_ran_type(vty, argv[0]), |
| 360 | argv + 1)) |
| 361 | return CMD_WARNING; |
| 362 | |
| 363 | return del_by_addr(vty, &addr); |
| 364 | } |
| 365 | |
| 366 | DEFUN(show_neighbor_all, show_neighbor_all_cmd, |
| 367 | NEIGHBOR_SHOW_CMD, |
| 368 | NEIGHBOR_SHOW_DOC) |
| 369 | { |
| 370 | neighbor_ident_vty_write(vty); |
| 371 | return CMD_SUCCESS; |
| 372 | } |
| 373 | |
| 374 | DEFUN(show_neighbor_ran, show_neighbor_ran_cmd, |
| 375 | NEIGHBOR_SHOW_CMD " " RAN_TYPE_PARAMS, |
| 376 | NEIGHBOR_SHOW_DOC RAN_TYPE_DOC) |
| 377 | { |
| 378 | neighbor_ident_vty_write_by_ran_type(vty, parse_ran_type(vty, argv[0])); |
| 379 | return CMD_SUCCESS; |
| 380 | } |
| 381 | |
| 382 | DEFUN(show_neighbor, show_neighbor_cmd, |
| 383 | NEIGHBOR_SHOW_CMD " "RAN_TYPE_PARAMS " " HO_TARGET_PARAMS, |
| 384 | NEIGHBOR_SHOW_DOC RAN_TYPE_DOC HO_TARGET_DOC) |
| 385 | { |
| 386 | const struct neighbor_ident_entry *e; |
| 387 | struct neighbor_ident_addr addr; |
| 388 | if (parse_ho_target_addr(vty, &addr, |
| 389 | parse_ran_type(vty, argv[0]), |
| 390 | argv + 1)) |
| 391 | return CMD_WARNING; |
| 392 | |
| 393 | e = neighbor_ident_find_by_addr(&gsmnet->neighbor_ident_list, &addr); |
| 394 | if (e) |
| 395 | write_neighbor_ident_entry(vty, e); |
| 396 | else |
| 397 | vty_out(vty, "%% No such neighbor target%s", VTY_NEWLINE); |
| 398 | |
| 399 | return CMD_SUCCESS; |
| 400 | } |
| 401 | |
| 402 | void neighbor_ident_vty_init(struct gsm_network *net) |
| 403 | { |
| 404 | gsmnet = net; |
| 405 | |
| 406 | install_element(MSC_NODE, &cfg_neighbor_add_lac_cmd); |
| 407 | install_element(MSC_NODE, &cfg_neighbor_add_lac_ci_cmd); |
| 408 | install_element(MSC_NODE, &cfg_neighbor_add_cgi_cmd); |
| 409 | install_element(MSC_NODE, &cfg_del_neighbor_cmd); |
| 410 | install_element_ve(&show_neighbor_all_cmd); |
| 411 | install_element_ve(&show_neighbor_cmd); |
| 412 | install_element_ve(&show_neighbor_ran_cmd); |
| 413 | |
| 414 | install_element_ve(&show_neighbor_ran_lac_cmd); |
| 415 | install_element_ve(&show_neighbor_ran_lac_ci_cmd); |
| 416 | install_element_ve(&show_neighbor_ran_cgi_cmd); |
| 417 | |
| 418 | install_element_ve(&show_neighbor_lac_cmd); |
| 419 | install_element_ve(&show_neighbor_lac_ci_cmd); |
| 420 | install_element_ve(&show_neighbor_cgi_cmd); |
| 421 | } |