blob: 848fa833faf4b10a43c10717140c26a29edb96d5 [file] [log] [blame]
Harald Welte288be162010-05-01 16:48:27 +02001/*
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 Welte9af6ddf2011-01-01 15:25:50 +01007 * 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 Welte288be162010-05-01 16:48:27 +02009 * (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 Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Harald Welte288be162010-05-01 16:48:27 +020015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * 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 Welte288be162010-05-01 16:48:27 +020018 *
19 */
20
Harald Welte288be162010-05-01 16:48:27 +020021#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010025#include <osmocom/core/talloc.h>
26#include <osmocom/core/utils.h>
27#include <osmocom/core/rate_ctr.h>
Harald Welte288be162010-05-01 16:48:27 +020028
29#include <openbsc/debug.h>
30#include <openbsc/sgsn.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080031#include <osmocom/gprs/gprs_ns.h>
Harald Welted193cb32010-05-17 22:58:03 +020032#include <openbsc/gprs_sgsn.h>
Harald Welte62ab20c2010-05-14 18:59:17 +020033#include <openbsc/vty.h>
Harald Weltec5d4a0c2010-07-02 22:47:59 +020034#include <openbsc/gsm_04_08_gprs.h>
Harald Welte288be162010-05-01 16:48:27 +020035
Harald Welte4b037e42010-05-19 19:45:32 +020036#include <osmocom/vty/command.h>
37#include <osmocom/vty/vty.h>
Pablo Neira Ayuso6110a3f2011-03-28 19:35:00 +020038#include <osmocom/vty/misc.h>
Harald Welte288be162010-05-01 16:48:27 +020039
Harald Welted193cb32010-05-17 22:58:03 +020040#include <pdp.h>
41
Harald Welte288be162010-05-01 16:48:27 +020042static struct sgsn_config *g_cfg = NULL;
43
Harald Weltec5d4a0c2010-07-02 22:47:59 +020044
45#define GSM48_MAX_APN_LEN 102 /* 10.5.6.1 */
46static char *gprs_apn2str(uint8_t *apn, unsigned int len)
47{
48 static char apnbuf[GSM48_MAX_APN_LEN+1];
Holger Hans Peter Freyther80e03652013-07-04 18:44:16 +020049 unsigned int i = 0;
Harald Weltec5d4a0c2010-07-02 22:47:59 +020050
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
70static 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 Welte288be162010-05-01 16:48:27 +0200107static struct cmd_node sgsn_node = {
108 SGSN_NODE,
Harald Welte570ce242012-08-17 13:16:10 +0200109 "%s(config-sgsn)# ",
Harald Welte288be162010-05-01 16:48:27 +0200110 1,
111};
112
113static int config_write_sgsn(struct vty *vty)
114{
Harald Welte77289c22010-05-18 14:32:29 +0200115 struct sgsn_ggsn_ctx *gctx;
Harald Welte288be162010-05-01 16:48:27 +0200116
117 vty_out(vty, "sgsn%s", VTY_NEWLINE);
118
Harald Weltee300d002010-06-02 12:41:34 +0200119 vty_out(vty, " gtp local-ip %s%s",
120 inet_ntoa(g_cfg->gtp_listenaddr.sin_addr), VTY_NEWLINE);
121
Harald Welted193cb32010-05-17 22:58:03 +0200122 llist_for_each_entry(gctx, &sgsn_ggsn_ctxts, list) {
Harald Welteff3bde82010-05-19 15:09:09 +0200123 vty_out(vty, " ggsn %u remote-ip %s%s", gctx->id,
Harald Welted193cb32010-05-17 22:58:03 +0200124 inet_ntoa(gctx->remote_addr), VTY_NEWLINE);
Harald Welteff3bde82010-05-19 15:09:09 +0200125 vty_out(vty, " ggsn %u gtp-version %u%s", gctx->id,
Harald Welted193cb32010-05-17 22:58:03 +0200126 gctx->gtp_version, VTY_NEWLINE);
Harald Welte288be162010-05-01 16:48:27 +0200127 }
128
129 return CMD_SUCCESS;
130}
131
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100132#define SGSN_STR "Configure the SGSN\n"
133#define GGSN_STR "Configure the GGSN information\n"
Harald Weltee300d002010-06-02 12:41:34 +0200134
135DEFUN(cfg_sgsn, cfg_sgsn_cmd,
136 "sgsn",
137 SGSN_STR)
Harald Welte288be162010-05-01 16:48:27 +0200138{
139 vty->node = SGSN_NODE;
140 return CMD_SUCCESS;
141}
142
Harald Weltee300d002010-06-02 12:41:34 +0200143DEFUN(cfg_sgsn_bind_addr, cfg_sgsn_bind_addr_cmd,
144 "gtp local-ip A.B.C.D",
145 "GTP Parameters\n"
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100146 "Set the IP address for the local GTP bind\n"
147 "IPv4 Address\n")
Harald Weltee300d002010-06-02 12:41:34 +0200148{
149 inet_aton(argv[0], &g_cfg->gtp_listenaddr.sin_addr);
150
151 return CMD_SUCCESS;
152}
153
Harald Welted193cb32010-05-17 22:58:03 +0200154DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd,
155 "ggsn <0-255> remote-ip A.B.C.D",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100156 GGSN_STR "GGSN Number\n" IP_STR "IPv4 Address\n")
Harald Welted193cb32010-05-17 22:58:03 +0200157{
158 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200159 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welte288be162010-05-01 16:48:27 +0200160
Harald Welted193cb32010-05-17 22:58:03 +0200161 inet_aton(argv[1], &ggc->remote_addr);
Harald Welte288be162010-05-01 16:48:27 +0200162
Harald Welted193cb32010-05-17 22:58:03 +0200163 return CMD_SUCCESS;
164}
165
166#if 0
167DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd,
168 "ggsn <0-255> remote-port <0-65535>",
169 "")
170{
171 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200172 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200173 uint16_t port = atoi(argv[1]);
174
175}
176#endif
177
178DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd,
179 "ggsn <0-255> gtp-version (0|1)",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100180 GGSN_STR "GGSN Number\n" "GTP Version\n"
181 "Version 0\n" "Version 1\n")
Harald Welted193cb32010-05-17 22:58:03 +0200182{
183 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200184 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200185
186 if (atoi(argv[1]))
187 ggc->gtp_version = 1;
188 else
189 ggc->gtp_version = 0;
190
191 return CMD_SUCCESS;
192}
193
194#if 0
195DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd,
196 "apn APNAME ggsn <0-255>",
197 "")
198{
199 struct apn_ctx **
200}
201#endif
202
203const struct value_string gprs_mm_st_strs[] = {
204 { GMM_DEREGISTERED, "DEREGISTERED" },
205 { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" },
206 { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" },
Harald Weltebffeff82010-06-09 15:50:45 +0200207 { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" },
Harald Welted193cb32010-05-17 22:58:03 +0200208 { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" },
209 { 0, NULL }
210};
211
212static void vty_dump_pdp(struct vty *vty, const char *pfx,
213 struct sgsn_pdp_ctx *pdp)
214{
215 vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s",
Harald Welte6abf94e2010-05-18 10:35:06 +0200216 pfx, pdp->mm->imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200217 vty_out(vty, "%s APN: %s%s", pfx,
218 gprs_apn2str(pdp->lib->apn_use.v, pdp->lib->apn_use.l),
219 VTY_NEWLINE);
220 vty_out(vty, "%s PDP Address: %s%s", pfx,
221 gprs_pdpaddr2str(pdp->lib->eua.v, pdp->lib->eua.l),
222 VTY_NEWLINE);
Harald Welteefbdee92010-06-10 00:20:12 +0200223 vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200224}
225
226static void vty_dump_mmctx(struct vty *vty, const char *pfx,
227 struct sgsn_mm_ctx *mm, int pdp)
228{
229 vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s",
230 pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE);
231 vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn,
232 mm->tlli, VTY_NEWLINE);
233 vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, "
234 "Cell ID: %u%s", pfx,
235 get_value_string(gprs_mm_st_strs, mm->mm_state),
236 mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac,
237 mm->cell_id, VTY_NEWLINE);
238
Harald Welte8acd88f2010-05-18 10:57:45 +0200239 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
240
Harald Welted193cb32010-05-17 22:58:03 +0200241 if (pdp) {
242 struct sgsn_pdp_ctx *pdp;
243
244 llist_for_each_entry(pdp, &mm->pdp_list, list)
245 vty_dump_pdp(vty, " ", pdp);
246 }
247}
248
249DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn",
250 SHOW_STR "Display information about the SGSN")
251{
252 /* FIXME: statistics */
253 return CMD_SUCCESS;
254}
255
256#define MMCTX_STR "MM Context\n"
257#define INCLUDE_PDP_STR "Include PDP Context Information\n"
258
259#if 0
260DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd,
261 "show mm-context tlli HEX [pdp]",
262 SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR)
263{
264 uint32_t tlli;
265 struct sgsn_mm_ctx *mm;
266
267 tlli = strtoul(argv[0], NULL, 16);
268 mm = sgsn_mm_ctx_by_tlli(tlli);
269 if (!mm) {
270 vty_out(vty, "No MM context for TLLI %08x%s",
271 tlli, VTY_NEWLINE);
272 return CMD_WARNING;
273 }
274 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
275 return CMD_SUCCESS;
276}
277#endif
278
279DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd,
280 "show mm-context imsi IMSI [pdp]",
281 SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n"
282 INCLUDE_PDP_STR)
283{
284 struct sgsn_mm_ctx *mm;
285
286 mm = sgsn_mm_ctx_by_imsi(argv[0]);
287 if (!mm) {
288 vty_out(vty, "No MM context for IMSI %s%s",
289 argv[0], VTY_NEWLINE);
290 return CMD_WARNING;
291 }
292 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
293 return CMD_SUCCESS;
294}
295
296DEFUN(swow_mmctx_all, show_mmctx_all_cmd,
297 "show mm-context all [pdp]",
298 SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR)
299{
300 struct sgsn_mm_ctx *mm;
301
302 llist_for_each_entry(mm, &sgsn_mm_ctxts, list)
303 vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0);
304
305 return CMD_SUCCESS;
306}
307
308DEFUN(show_ggsn, show_ggsn_cmd,
309 "show ggsn",
310 "")
311{
312
313}
314
315DEFUN(show_pdpctx_all, show_pdpctx_all_cmd,
316 "show pdp-context all",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100317 SHOW_STR "Display information on PDP Context\n" "Show everything\n")
Harald Welted193cb32010-05-17 22:58:03 +0200318{
319 struct sgsn_pdp_ctx *pdp;
320
321 llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list)
322 vty_dump_pdp(vty, "", pdp);
323
324 return CMD_SUCCESS;
325}
Harald Welte288be162010-05-01 16:48:27 +0200326
327int sgsn_vty_init(void)
328{
Harald Welted193cb32010-05-17 22:58:03 +0200329 install_element_ve(&show_sgsn_cmd);
330 //install_element_ve(&show_mmctx_tlli_cmd);
331 install_element_ve(&show_mmctx_imsi_cmd);
332 install_element_ve(&show_mmctx_all_cmd);
333 install_element_ve(&show_pdpctx_all_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200334
335 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
336 install_node(&sgsn_node, config_write_sgsn);
337 install_default(SGSN_NODE);
Harald Welte62ab20c2010-05-14 18:59:17 +0200338 install_element(SGSN_NODE, &ournode_exit_cmd);
Harald Welte54f74242010-05-14 19:11:04 +0200339 install_element(SGSN_NODE, &ournode_end_cmd);
Harald Weltee300d002010-06-02 12:41:34 +0200340 install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd);
Harald Welted193cb32010-05-17 22:58:03 +0200341 install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd);
342 //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd);
343 install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200344
345 return 0;
346}
347
348int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
349{
350 int rc;
351
352 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200353 rc = vty_read_config_file(config_file, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200354 if (rc < 0) {
355 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
356 return rc;
357 }
358
359 return 0;
360}