blob: 74669ffb7d8b33c649db2c421e52e497883645db [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
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <arpa/inet.h>
25
26#include <osmocore/talloc.h>
Harald Welted193cb32010-05-17 22:58:03 +020027#include <osmocore/utils.h>
Harald Weltecd4dd4d2010-05-18 17:20:49 +020028#include <osmocore/rate_ctr.h>
Harald Welte288be162010-05-01 16:48:27 +020029
30#include <openbsc/debug.h>
31#include <openbsc/sgsn.h>
32#include <openbsc/gprs_ns.h>
Harald Welted193cb32010-05-17 22:58:03 +020033#include <openbsc/gprs_sgsn.h>
Harald Welte62ab20c2010-05-14 18:59:17 +020034#include <openbsc/vty.h>
Harald Weltec5d4a0c2010-07-02 22:47:59 +020035#include <openbsc/gsm_04_08_gprs.h>
Harald Welte288be162010-05-01 16:48:27 +020036
Harald Welte4b037e42010-05-19 19:45:32 +020037#include <osmocom/vty/command.h>
38#include <osmocom/vty/vty.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];
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
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,
109 "%s(sgsn)#",
110 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
Harald Weltee300d002010-06-02 12:41:34 +0200132#define SGSN_STR "Configure the SGSN"
133
134DEFUN(cfg_sgsn, cfg_sgsn_cmd,
135 "sgsn",
136 SGSN_STR)
Harald Welte288be162010-05-01 16:48:27 +0200137{
138 vty->node = SGSN_NODE;
139 return CMD_SUCCESS;
140}
141
Harald Weltee300d002010-06-02 12:41:34 +0200142DEFUN(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 Welted193cb32010-05-17 22:58:03 +0200152DEFUN(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 Welte77289c22010-05-18 14:32:29 +0200157 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welte288be162010-05-01 16:48:27 +0200158
Harald Welted193cb32010-05-17 22:58:03 +0200159 inet_aton(argv[1], &ggc->remote_addr);
Harald Welte288be162010-05-01 16:48:27 +0200160
Harald Welted193cb32010-05-17 22:58:03 +0200161 return CMD_SUCCESS;
162}
163
164#if 0
165DEFUN(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 Welte77289c22010-05-18 14:32:29 +0200170 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200171 uint16_t port = atoi(argv[1]);
172
173}
174#endif
175
176DEFUN(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 Welte77289c22010-05-18 14:32:29 +0200181 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200182
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
192DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd,
193 "apn APNAME ggsn <0-255>",
194 "")
195{
196 struct apn_ctx **
197}
198#endif
199
200const 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 Weltebffeff82010-06-09 15:50:45 +0200204 { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" },
Harald Welted193cb32010-05-17 22:58:03 +0200205 { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" },
206 { 0, NULL }
207};
208
209static 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 Welte6abf94e2010-05-18 10:35:06 +0200213 pfx, pdp->mm->imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200214 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 Welteefbdee92010-06-10 00:20:12 +0200220 vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200221}
222
223static 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 Welte8acd88f2010-05-18 10:57:45 +0200236 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
237
Harald Welted193cb32010-05-17 22:58:03 +0200238 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
246DEFUN(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
257DEFUN(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
276DEFUN(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
293DEFUN(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
305DEFUN(show_ggsn, show_ggsn_cmd,
306 "show ggsn",
307 "")
308{
309
310}
311
312DEFUN(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 Welte288be162010-05-01 16:48:27 +0200323
324int sgsn_vty_init(void)
325{
Harald Welted193cb32010-05-17 22:58:03 +0200326 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 Welte288be162010-05-01 16:48:27 +0200331
332 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
333 install_node(&sgsn_node, config_write_sgsn);
334 install_default(SGSN_NODE);
Harald Welte62ab20c2010-05-14 18:59:17 +0200335 install_element(SGSN_NODE, &ournode_exit_cmd);
Harald Welte54f74242010-05-14 19:11:04 +0200336 install_element(SGSN_NODE, &ournode_end_cmd);
Harald Weltee300d002010-06-02 12:41:34 +0200337 install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd);
Harald Welted193cb32010-05-17 22:58:03 +0200338 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 Welte288be162010-05-01 16:48:27 +0200341
342 return 0;
343}
344
345int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
346{
347 int rc;
348
349 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200350 rc = vty_read_config_file(config_file, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200351 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}