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); |
| 152 | /* FIXME: statistics */ |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 153 | //vty_out_rate_ctr_group(vty, " ", pdp->ctrg); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void vty_dump_mmctx(struct vty *vty, const char *pfx, |
| 157 | struct sgsn_mm_ctx *mm, int pdp) |
| 158 | { |
| 159 | vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s", |
| 160 | pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE); |
| 161 | vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn, |
| 162 | mm->tlli, VTY_NEWLINE); |
| 163 | vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, " |
| 164 | "Cell ID: %u%s", pfx, |
| 165 | get_value_string(gprs_mm_st_strs, mm->mm_state), |
| 166 | mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac, |
| 167 | mm->cell_id, VTY_NEWLINE); |
| 168 | |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 169 | vty_out_rate_ctr_group(vty, " ", mm->ctrg); |
| 170 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 171 | if (pdp) { |
| 172 | struct sgsn_pdp_ctx *pdp; |
| 173 | |
| 174 | llist_for_each_entry(pdp, &mm->pdp_list, list) |
| 175 | vty_dump_pdp(vty, " ", pdp); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn", |
| 180 | SHOW_STR "Display information about the SGSN") |
| 181 | { |
| 182 | /* FIXME: statistics */ |
| 183 | return CMD_SUCCESS; |
| 184 | } |
| 185 | |
| 186 | #define MMCTX_STR "MM Context\n" |
| 187 | #define INCLUDE_PDP_STR "Include PDP Context Information\n" |
| 188 | |
| 189 | #if 0 |
| 190 | DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd, |
| 191 | "show mm-context tlli HEX [pdp]", |
| 192 | SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR) |
| 193 | { |
| 194 | uint32_t tlli; |
| 195 | struct sgsn_mm_ctx *mm; |
| 196 | |
| 197 | tlli = strtoul(argv[0], NULL, 16); |
| 198 | mm = sgsn_mm_ctx_by_tlli(tlli); |
| 199 | if (!mm) { |
| 200 | vty_out(vty, "No MM context for TLLI %08x%s", |
| 201 | tlli, VTY_NEWLINE); |
| 202 | return CMD_WARNING; |
| 203 | } |
| 204 | vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0); |
| 205 | return CMD_SUCCESS; |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd, |
| 210 | "show mm-context imsi IMSI [pdp]", |
| 211 | SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n" |
| 212 | INCLUDE_PDP_STR) |
| 213 | { |
| 214 | struct sgsn_mm_ctx *mm; |
| 215 | |
| 216 | mm = sgsn_mm_ctx_by_imsi(argv[0]); |
| 217 | if (!mm) { |
| 218 | vty_out(vty, "No MM context for IMSI %s%s", |
| 219 | argv[0], VTY_NEWLINE); |
| 220 | return CMD_WARNING; |
| 221 | } |
| 222 | vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0); |
| 223 | return CMD_SUCCESS; |
| 224 | } |
| 225 | |
| 226 | DEFUN(swow_mmctx_all, show_mmctx_all_cmd, |
| 227 | "show mm-context all [pdp]", |
| 228 | SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR) |
| 229 | { |
| 230 | struct sgsn_mm_ctx *mm; |
| 231 | |
| 232 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) |
| 233 | vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0); |
| 234 | |
| 235 | return CMD_SUCCESS; |
| 236 | } |
| 237 | |
| 238 | DEFUN(show_ggsn, show_ggsn_cmd, |
| 239 | "show ggsn", |
| 240 | "") |
| 241 | { |
| 242 | |
| 243 | } |
| 244 | |
| 245 | DEFUN(show_pdpctx_all, show_pdpctx_all_cmd, |
| 246 | "show pdp-context all", |
| 247 | SHOW_STR "Display information on PDP Context\n") |
| 248 | { |
| 249 | struct sgsn_pdp_ctx *pdp; |
| 250 | |
| 251 | llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list) |
| 252 | vty_dump_pdp(vty, "", pdp); |
| 253 | |
| 254 | return CMD_SUCCESS; |
| 255 | } |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 256 | |
| 257 | int sgsn_vty_init(void) |
| 258 | { |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 259 | install_element_ve(&show_sgsn_cmd); |
| 260 | //install_element_ve(&show_mmctx_tlli_cmd); |
| 261 | install_element_ve(&show_mmctx_imsi_cmd); |
| 262 | install_element_ve(&show_mmctx_all_cmd); |
| 263 | install_element_ve(&show_pdpctx_all_cmd); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 264 | |
| 265 | install_element(CONFIG_NODE, &cfg_sgsn_cmd); |
| 266 | install_node(&sgsn_node, config_write_sgsn); |
| 267 | install_default(SGSN_NODE); |
Harald Welte | 62ab20c | 2010-05-14 18:59:17 +0200 | [diff] [blame] | 268 | install_element(SGSN_NODE, &ournode_exit_cmd); |
Harald Welte | 54f7424 | 2010-05-14 19:11:04 +0200 | [diff] [blame] | 269 | install_element(SGSN_NODE, &ournode_end_cmd); |
Harald Welte | e300d00 | 2010-06-02 12:41:34 +0200 | [diff] [blame] | 270 | install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 271 | install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd); |
| 272 | //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd); |
| 273 | install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg) |
| 279 | { |
| 280 | int rc; |
| 281 | |
| 282 | g_cfg = cfg; |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 283 | rc = vty_read_config_file(config_file, NULL); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 284 | if (rc < 0) { |
| 285 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 286 | return rc; |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |