Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2010 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <arpa/inet.h> |
| 25 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 26 | #include <osmocom/core/talloc.h> |
| 27 | #include <osmocom/core/utils.h> |
| 28 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 29 | |
| 30 | #include <openbsc/debug.h> |
| 31 | #include <openbsc/sgsn.h> |
| 32 | #include <openbsc/gprs_ns.h> |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 33 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | 58ed1cb | 2010-05-14 18:59:17 +0200 | [diff] [blame] | 34 | #include <openbsc/vty.h> |
Harald Welte | 493ba62 | 2010-07-02 22:47:59 +0200 | [diff] [blame] | 35 | #include <openbsc/gsm_04_08_gprs.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 36 | |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 37 | #include <osmocom/vty/command.h> |
| 38 | #include <osmocom/vty/vty.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 39 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 40 | #include <pdp.h> |
| 41 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 42 | static struct sgsn_config *g_cfg = NULL; |
| 43 | |
Harald Welte | 493ba62 | 2010-07-02 22:47:59 +0200 | [diff] [blame] | 44 | |
| 45 | #define GSM48_MAX_APN_LEN 102 /* 10.5.6.1 */ |
| 46 | static char *gprs_apn2str(uint8_t *apn, unsigned int len) |
| 47 | { |
| 48 | static char apnbuf[GSM48_MAX_APN_LEN+1]; |
| 49 | unsigned int i; |
| 50 | |
| 51 | if (!apn) |
| 52 | return ""; |
| 53 | |
| 54 | if (len > sizeof(apnbuf)-1) |
| 55 | len = sizeof(apnbuf)-1; |
| 56 | |
| 57 | memcpy(apnbuf, apn, len); |
| 58 | apnbuf[len] = '\0'; |
| 59 | |
| 60 | /* replace the domain name step sizes with dots */ |
| 61 | while (i < len) { |
| 62 | unsigned int step = apnbuf[i]; |
| 63 | apnbuf[i] = '.'; |
| 64 | i += step+1; |
| 65 | } |
| 66 | |
| 67 | return apnbuf+1; |
| 68 | } |
| 69 | |
| 70 | static char *gprs_pdpaddr2str(uint8_t *pdpa, uint8_t len) |
| 71 | { |
| 72 | static char str[INET6_ADDRSTRLEN + 10]; |
| 73 | |
| 74 | if (!pdpa || len < 2) |
| 75 | return "none"; |
| 76 | |
| 77 | switch (pdpa[0] & 0x0f) { |
| 78 | case PDP_TYPE_ORG_IETF: |
| 79 | switch (pdpa[1]) { |
| 80 | case PDP_TYPE_N_IETF_IPv4: |
| 81 | if (len < 2 + 4) |
| 82 | break; |
| 83 | strcpy(str, "IPv4 "); |
| 84 | inet_ntop(AF_INET, pdpa+2, str+5, sizeof(str)-5); |
| 85 | return str; |
| 86 | case PDP_TYPE_N_IETF_IPv6: |
| 87 | if (len < 2 + 8) |
| 88 | break; |
| 89 | strcpy(str, "IPv6 "); |
| 90 | inet_ntop(AF_INET6, pdpa+2, str+5, sizeof(str)-5); |
| 91 | return str; |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | break; |
| 96 | case PDP_TYPE_ORG_ETSI: |
| 97 | if (pdpa[1] == PDP_TYPE_N_ETSI_PPP) |
| 98 | return "PPP"; |
| 99 | break; |
| 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | return "invalid"; |
| 105 | } |
| 106 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 107 | static struct cmd_node sgsn_node = { |
| 108 | SGSN_NODE, |
| 109 | "%s(sgsn)#", |
| 110 | 1, |
| 111 | }; |
| 112 | |
| 113 | static int config_write_sgsn(struct vty *vty) |
| 114 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 115 | struct sgsn_ggsn_ctx *gctx; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 116 | |
| 117 | vty_out(vty, "sgsn%s", VTY_NEWLINE); |
| 118 | |
Harald Welte | e0aea39 | 2010-06-02 12:41:34 +0200 | [diff] [blame] | 119 | vty_out(vty, " gtp local-ip %s%s", |
| 120 | inet_ntoa(g_cfg->gtp_listenaddr.sin_addr), VTY_NEWLINE); |
| 121 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 122 | llist_for_each_entry(gctx, &sgsn_ggsn_ctxts, list) { |
Harald Welte | 31f0a23 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 123 | vty_out(vty, " ggsn %u remote-ip %s%s", gctx->id, |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 124 | inet_ntoa(gctx->remote_addr), VTY_NEWLINE); |
Harald Welte | 31f0a23 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 125 | vty_out(vty, " ggsn %u gtp-version %u%s", gctx->id, |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 126 | gctx->gtp_version, VTY_NEWLINE); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | return CMD_SUCCESS; |
| 130 | } |
| 131 | |
Harald Welte | e0aea39 | 2010-06-02 12:41:34 +0200 | [diff] [blame] | 132 | #define SGSN_STR "Configure the SGSN" |
| 133 | |
| 134 | DEFUN(cfg_sgsn, cfg_sgsn_cmd, |
| 135 | "sgsn", |
| 136 | SGSN_STR) |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 137 | { |
| 138 | vty->node = SGSN_NODE; |
| 139 | return CMD_SUCCESS; |
| 140 | } |
| 141 | |
Harald Welte | e0aea39 | 2010-06-02 12:41:34 +0200 | [diff] [blame] | 142 | DEFUN(cfg_sgsn_bind_addr, cfg_sgsn_bind_addr_cmd, |
| 143 | "gtp local-ip A.B.C.D", |
| 144 | "GTP Parameters\n" |
| 145 | "Set the IP address for the local GTP bind\n") |
| 146 | { |
| 147 | inet_aton(argv[0], &g_cfg->gtp_listenaddr.sin_addr); |
| 148 | |
| 149 | return CMD_SUCCESS; |
| 150 | } |
| 151 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 152 | DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd, |
| 153 | "ggsn <0-255> remote-ip A.B.C.D", |
| 154 | "") |
| 155 | { |
| 156 | uint32_t id = atoi(argv[0]); |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 157 | struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 158 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 159 | inet_aton(argv[1], &ggc->remote_addr); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 160 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 161 | return CMD_SUCCESS; |
| 162 | } |
| 163 | |
| 164 | #if 0 |
| 165 | DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd, |
| 166 | "ggsn <0-255> remote-port <0-65535>", |
| 167 | "") |
| 168 | { |
| 169 | uint32_t id = atoi(argv[0]); |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 170 | struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 171 | uint16_t port = atoi(argv[1]); |
| 172 | |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd, |
| 177 | "ggsn <0-255> gtp-version (0|1)", |
| 178 | "") |
| 179 | { |
| 180 | uint32_t id = atoi(argv[0]); |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 181 | struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 182 | |
| 183 | if (atoi(argv[1])) |
| 184 | ggc->gtp_version = 1; |
| 185 | else |
| 186 | ggc->gtp_version = 0; |
| 187 | |
| 188 | return CMD_SUCCESS; |
| 189 | } |
| 190 | |
| 191 | #if 0 |
| 192 | DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd, |
| 193 | "apn APNAME ggsn <0-255>", |
| 194 | "") |
| 195 | { |
| 196 | struct apn_ctx ** |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | const struct value_string gprs_mm_st_strs[] = { |
| 201 | { GMM_DEREGISTERED, "DEREGISTERED" }, |
| 202 | { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" }, |
| 203 | { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" }, |
Harald Welte | 3ba2ce1 | 2010-06-09 15:50:45 +0200 | [diff] [blame] | 204 | { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" }, |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 205 | { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" }, |
| 206 | { 0, NULL } |
| 207 | }; |
| 208 | |
| 209 | static void vty_dump_pdp(struct vty *vty, const char *pfx, |
| 210 | struct sgsn_pdp_ctx *pdp) |
| 211 | { |
| 212 | vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s", |
Harald Welte | 29d838e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 213 | pfx, pdp->mm->imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE); |
Harald Welte | 493ba62 | 2010-07-02 22:47:59 +0200 | [diff] [blame] | 214 | vty_out(vty, "%s APN: %s%s", pfx, |
| 215 | gprs_apn2str(pdp->lib->apn_use.v, pdp->lib->apn_use.l), |
| 216 | VTY_NEWLINE); |
| 217 | vty_out(vty, "%s PDP Address: %s%s", pfx, |
| 218 | gprs_pdpaddr2str(pdp->lib->eua.v, pdp->lib->eua.l), |
| 219 | VTY_NEWLINE); |
Harald Welte | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 220 | vty_out_rate_ctr_group(vty, " ", pdp->ctrg); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static void vty_dump_mmctx(struct vty *vty, const char *pfx, |
| 224 | struct sgsn_mm_ctx *mm, int pdp) |
| 225 | { |
| 226 | vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s", |
| 227 | pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE); |
| 228 | vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn, |
| 229 | mm->tlli, VTY_NEWLINE); |
| 230 | vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, " |
| 231 | "Cell ID: %u%s", pfx, |
| 232 | get_value_string(gprs_mm_st_strs, mm->mm_state), |
| 233 | mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac, |
| 234 | mm->cell_id, VTY_NEWLINE); |
| 235 | |
Harald Welte | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 236 | vty_out_rate_ctr_group(vty, " ", mm->ctrg); |
| 237 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 238 | if (pdp) { |
| 239 | struct sgsn_pdp_ctx *pdp; |
| 240 | |
| 241 | llist_for_each_entry(pdp, &mm->pdp_list, list) |
| 242 | vty_dump_pdp(vty, " ", pdp); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn", |
| 247 | SHOW_STR "Display information about the SGSN") |
| 248 | { |
| 249 | /* FIXME: statistics */ |
| 250 | return CMD_SUCCESS; |
| 251 | } |
| 252 | |
| 253 | #define MMCTX_STR "MM Context\n" |
| 254 | #define INCLUDE_PDP_STR "Include PDP Context Information\n" |
| 255 | |
| 256 | #if 0 |
| 257 | DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd, |
| 258 | "show mm-context tlli HEX [pdp]", |
| 259 | SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR) |
| 260 | { |
| 261 | uint32_t tlli; |
| 262 | struct sgsn_mm_ctx *mm; |
| 263 | |
| 264 | tlli = strtoul(argv[0], NULL, 16); |
| 265 | mm = sgsn_mm_ctx_by_tlli(tlli); |
| 266 | if (!mm) { |
| 267 | vty_out(vty, "No MM context for TLLI %08x%s", |
| 268 | tlli, VTY_NEWLINE); |
| 269 | return CMD_WARNING; |
| 270 | } |
| 271 | vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0); |
| 272 | return CMD_SUCCESS; |
| 273 | } |
| 274 | #endif |
| 275 | |
| 276 | DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd, |
| 277 | "show mm-context imsi IMSI [pdp]", |
| 278 | SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n" |
| 279 | INCLUDE_PDP_STR) |
| 280 | { |
| 281 | struct sgsn_mm_ctx *mm; |
| 282 | |
| 283 | mm = sgsn_mm_ctx_by_imsi(argv[0]); |
| 284 | if (!mm) { |
| 285 | vty_out(vty, "No MM context for IMSI %s%s", |
| 286 | argv[0], VTY_NEWLINE); |
| 287 | return CMD_WARNING; |
| 288 | } |
| 289 | vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0); |
| 290 | return CMD_SUCCESS; |
| 291 | } |
| 292 | |
| 293 | DEFUN(swow_mmctx_all, show_mmctx_all_cmd, |
| 294 | "show mm-context all [pdp]", |
| 295 | SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR) |
| 296 | { |
| 297 | struct sgsn_mm_ctx *mm; |
| 298 | |
| 299 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) |
| 300 | vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0); |
| 301 | |
| 302 | return CMD_SUCCESS; |
| 303 | } |
| 304 | |
| 305 | DEFUN(show_ggsn, show_ggsn_cmd, |
| 306 | "show ggsn", |
| 307 | "") |
| 308 | { |
| 309 | |
| 310 | } |
| 311 | |
| 312 | DEFUN(show_pdpctx_all, show_pdpctx_all_cmd, |
| 313 | "show pdp-context all", |
| 314 | SHOW_STR "Display information on PDP Context\n") |
| 315 | { |
| 316 | struct sgsn_pdp_ctx *pdp; |
| 317 | |
| 318 | llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list) |
| 319 | vty_dump_pdp(vty, "", pdp); |
| 320 | |
| 321 | return CMD_SUCCESS; |
| 322 | } |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 323 | |
| 324 | int sgsn_vty_init(void) |
| 325 | { |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 326 | install_element_ve(&show_sgsn_cmd); |
| 327 | //install_element_ve(&show_mmctx_tlli_cmd); |
| 328 | install_element_ve(&show_mmctx_imsi_cmd); |
| 329 | install_element_ve(&show_mmctx_all_cmd); |
| 330 | install_element_ve(&show_pdpctx_all_cmd); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 331 | |
| 332 | install_element(CONFIG_NODE, &cfg_sgsn_cmd); |
| 333 | install_node(&sgsn_node, config_write_sgsn); |
| 334 | install_default(SGSN_NODE); |
Harald Welte | 58ed1cb | 2010-05-14 18:59:17 +0200 | [diff] [blame] | 335 | install_element(SGSN_NODE, &ournode_exit_cmd); |
Harald Welte | dc82b9b | 2010-05-14 19:11:04 +0200 | [diff] [blame] | 336 | install_element(SGSN_NODE, &ournode_end_cmd); |
Harald Welte | e0aea39 | 2010-06-02 12:41:34 +0200 | [diff] [blame] | 337 | install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 338 | install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd); |
| 339 | //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd); |
| 340 | install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg) |
| 346 | { |
| 347 | int rc; |
| 348 | |
| 349 | g_cfg = cfg; |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 350 | rc = vty_read_config_file(config_file, NULL); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 351 | if (rc < 0) { |
| 352 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 353 | return rc; |
| 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |