Harald Welte | 288be16 | 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 |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <netinet/in.h> |
| 25 | #include <arpa/inet.h> |
| 26 | |
| 27 | #include <osmocore/talloc.h> |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 28 | #include <osmocore/utils.h> |
Harald Welte | cd4dd4d | 2010-05-18 17:20:49 +0200 | [diff] [blame] | 29 | #include <osmocore/rate_ctr.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 30 | |
| 31 | #include <openbsc/debug.h> |
| 32 | #include <openbsc/sgsn.h> |
| 33 | #include <openbsc/gprs_ns.h> |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 34 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | 62ab20c | 2010-05-14 18:59:17 +0200 | [diff] [blame] | 35 | #include <openbsc/vty.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 36 | |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 37 | #include <osmocom/vty/command.h> |
| 38 | #include <osmocom/vty/vty.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 39 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 40 | #include <pdp.h> |
| 41 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 42 | static struct sgsn_config *g_cfg = NULL; |
| 43 | |
| 44 | static struct cmd_node sgsn_node = { |
| 45 | SGSN_NODE, |
| 46 | "%s(sgsn)#", |
| 47 | 1, |
| 48 | }; |
| 49 | |
| 50 | static int config_write_sgsn(struct vty *vty) |
| 51 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 52 | struct sgsn_ggsn_ctx *gctx; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 53 | |
| 54 | vty_out(vty, "sgsn%s", VTY_NEWLINE); |
| 55 | |
Harald Welte | e300d00 | 2010-06-02 12:41:34 +0200 | [diff] [blame] | 56 | vty_out(vty, " gtp local-ip %s%s", |
| 57 | inet_ntoa(g_cfg->gtp_listenaddr.sin_addr), VTY_NEWLINE); |
| 58 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 59 | llist_for_each_entry(gctx, &sgsn_ggsn_ctxts, list) { |
Harald Welte | ff3bde8 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 60 | vty_out(vty, " ggsn %u remote-ip %s%s", gctx->id, |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 61 | inet_ntoa(gctx->remote_addr), VTY_NEWLINE); |
Harald Welte | ff3bde8 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 62 | vty_out(vty, " ggsn %u gtp-version %u%s", gctx->id, |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 63 | gctx->gtp_version, VTY_NEWLINE); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | return CMD_SUCCESS; |
| 67 | } |
| 68 | |
Harald Welte | e300d00 | 2010-06-02 12:41:34 +0200 | [diff] [blame] | 69 | #define SGSN_STR "Configure the SGSN" |
| 70 | |
| 71 | DEFUN(cfg_sgsn, cfg_sgsn_cmd, |
| 72 | "sgsn", |
| 73 | SGSN_STR) |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 74 | { |
| 75 | vty->node = SGSN_NODE; |
| 76 | return CMD_SUCCESS; |
| 77 | } |
| 78 | |
Harald Welte | e300d00 | 2010-06-02 12:41:34 +0200 | [diff] [blame] | 79 | DEFUN(cfg_sgsn_bind_addr, cfg_sgsn_bind_addr_cmd, |
| 80 | "gtp local-ip A.B.C.D", |
| 81 | "GTP Parameters\n" |
| 82 | "Set the IP address for the local GTP bind\n") |
| 83 | { |
| 84 | inet_aton(argv[0], &g_cfg->gtp_listenaddr.sin_addr); |
| 85 | |
| 86 | return CMD_SUCCESS; |
| 87 | } |
| 88 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 89 | DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd, |
| 90 | "ggsn <0-255> remote-ip A.B.C.D", |
| 91 | "") |
| 92 | { |
| 93 | uint32_t id = atoi(argv[0]); |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 94 | struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 95 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 96 | inet_aton(argv[1], &ggc->remote_addr); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 97 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 98 | return CMD_SUCCESS; |
| 99 | } |
| 100 | |
| 101 | #if 0 |
| 102 | DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd, |
| 103 | "ggsn <0-255> remote-port <0-65535>", |
| 104 | "") |
| 105 | { |
| 106 | uint32_t id = atoi(argv[0]); |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 107 | struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 108 | uint16_t port = atoi(argv[1]); |
| 109 | |
| 110 | } |
| 111 | #endif |
| 112 | |
| 113 | DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd, |
| 114 | "ggsn <0-255> gtp-version (0|1)", |
| 115 | "") |
| 116 | { |
| 117 | uint32_t id = atoi(argv[0]); |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 118 | struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 119 | |
| 120 | if (atoi(argv[1])) |
| 121 | ggc->gtp_version = 1; |
| 122 | else |
| 123 | ggc->gtp_version = 0; |
| 124 | |
| 125 | return CMD_SUCCESS; |
| 126 | } |
| 127 | |
| 128 | #if 0 |
| 129 | DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd, |
| 130 | "apn APNAME ggsn <0-255>", |
| 131 | "") |
| 132 | { |
| 133 | struct apn_ctx ** |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | const struct value_string gprs_mm_st_strs[] = { |
| 138 | { GMM_DEREGISTERED, "DEREGISTERED" }, |
| 139 | { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" }, |
| 140 | { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" }, |
Harald Welte | bffeff8 | 2010-06-09 15:50:45 +0200 | [diff] [blame] | 141 | { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" }, |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 142 | { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" }, |
| 143 | { 0, NULL } |
| 144 | }; |
| 145 | |
| 146 | static void vty_dump_pdp(struct vty *vty, const char *pfx, |
| 147 | struct sgsn_pdp_ctx *pdp) |
| 148 | { |
| 149 | vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s", |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 150 | pfx, pdp->mm->imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 151 | vty_out(vty, "%s APN: %s\n", pfx, pdp->lib->apn_use.v); |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 152 | vty_out_rate_ctr_group(vty, " ", pdp->ctrg); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void vty_dump_mmctx(struct vty *vty, const char *pfx, |
| 156 | struct sgsn_mm_ctx *mm, int pdp) |
| 157 | { |
| 158 | vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s", |
| 159 | pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE); |
| 160 | vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn, |
| 161 | mm->tlli, VTY_NEWLINE); |
| 162 | vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, " |
| 163 | "Cell ID: %u%s", pfx, |
| 164 | get_value_string(gprs_mm_st_strs, mm->mm_state), |
| 165 | mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac, |
| 166 | mm->cell_id, VTY_NEWLINE); |
| 167 | |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 168 | vty_out_rate_ctr_group(vty, " ", mm->ctrg); |
| 169 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 170 | if (pdp) { |
| 171 | struct sgsn_pdp_ctx *pdp; |
| 172 | |
| 173 | llist_for_each_entry(pdp, &mm->pdp_list, list) |
| 174 | vty_dump_pdp(vty, " ", pdp); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn", |
| 179 | SHOW_STR "Display information about the SGSN") |
| 180 | { |
| 181 | /* FIXME: statistics */ |
| 182 | return CMD_SUCCESS; |
| 183 | } |
| 184 | |
| 185 | #define MMCTX_STR "MM Context\n" |
| 186 | #define INCLUDE_PDP_STR "Include PDP Context Information\n" |
| 187 | |
| 188 | #if 0 |
| 189 | DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd, |
| 190 | "show mm-context tlli HEX [pdp]", |
| 191 | SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR) |
| 192 | { |
| 193 | uint32_t tlli; |
| 194 | struct sgsn_mm_ctx *mm; |
| 195 | |
| 196 | tlli = strtoul(argv[0], NULL, 16); |
| 197 | mm = sgsn_mm_ctx_by_tlli(tlli); |
| 198 | if (!mm) { |
| 199 | vty_out(vty, "No MM context for TLLI %08x%s", |
| 200 | tlli, VTY_NEWLINE); |
| 201 | return CMD_WARNING; |
| 202 | } |
| 203 | vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0); |
| 204 | return CMD_SUCCESS; |
| 205 | } |
| 206 | #endif |
| 207 | |
| 208 | DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd, |
| 209 | "show mm-context imsi IMSI [pdp]", |
| 210 | SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n" |
| 211 | INCLUDE_PDP_STR) |
| 212 | { |
| 213 | struct sgsn_mm_ctx *mm; |
| 214 | |
| 215 | mm = sgsn_mm_ctx_by_imsi(argv[0]); |
| 216 | if (!mm) { |
| 217 | vty_out(vty, "No MM context for IMSI %s%s", |
| 218 | argv[0], VTY_NEWLINE); |
| 219 | return CMD_WARNING; |
| 220 | } |
| 221 | vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0); |
| 222 | return CMD_SUCCESS; |
| 223 | } |
| 224 | |
| 225 | DEFUN(swow_mmctx_all, show_mmctx_all_cmd, |
| 226 | "show mm-context all [pdp]", |
| 227 | SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR) |
| 228 | { |
| 229 | struct sgsn_mm_ctx *mm; |
| 230 | |
| 231 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) |
| 232 | vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0); |
| 233 | |
| 234 | return CMD_SUCCESS; |
| 235 | } |
| 236 | |
| 237 | DEFUN(show_ggsn, show_ggsn_cmd, |
| 238 | "show ggsn", |
| 239 | "") |
| 240 | { |
| 241 | |
| 242 | } |
| 243 | |
| 244 | DEFUN(show_pdpctx_all, show_pdpctx_all_cmd, |
| 245 | "show pdp-context all", |
| 246 | SHOW_STR "Display information on PDP Context\n") |
| 247 | { |
| 248 | struct sgsn_pdp_ctx *pdp; |
| 249 | |
| 250 | llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list) |
| 251 | vty_dump_pdp(vty, "", pdp); |
| 252 | |
| 253 | return CMD_SUCCESS; |
| 254 | } |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 255 | |
| 256 | int sgsn_vty_init(void) |
| 257 | { |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 258 | install_element_ve(&show_sgsn_cmd); |
| 259 | //install_element_ve(&show_mmctx_tlli_cmd); |
| 260 | install_element_ve(&show_mmctx_imsi_cmd); |
| 261 | install_element_ve(&show_mmctx_all_cmd); |
| 262 | install_element_ve(&show_pdpctx_all_cmd); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 263 | |
| 264 | install_element(CONFIG_NODE, &cfg_sgsn_cmd); |
| 265 | install_node(&sgsn_node, config_write_sgsn); |
| 266 | install_default(SGSN_NODE); |
Harald Welte | 62ab20c | 2010-05-14 18:59:17 +0200 | [diff] [blame] | 267 | install_element(SGSN_NODE, &ournode_exit_cmd); |
Harald Welte | 54f7424 | 2010-05-14 19:11:04 +0200 | [diff] [blame] | 268 | install_element(SGSN_NODE, &ournode_end_cmd); |
Harald Welte | e300d00 | 2010-06-02 12:41:34 +0200 | [diff] [blame] | 269 | install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 270 | install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd); |
| 271 | //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd); |
| 272 | install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg) |
| 278 | { |
| 279 | int rc; |
| 280 | |
| 281 | g_cfg = cfg; |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 282 | rc = vty_read_config_file(config_file, NULL); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 283 | if (rc < 0) { |
| 284 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 285 | return rc; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |