blob: ce3b4da9eb89fff03ad1f0c1dab3abdd7e85ba22 [file] [log] [blame]
Harald Welte288be162010-05-01 16:48:27 +02001/*
Harald Welte7f6da482013-03-19 11:00:13 +01002 * (C) 2010-2013 by Harald Welte <laforge@gnumonks.org>
Harald Welte288be162010-05-01 16:48:27 +02003 * (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 Welte7f6da482013-03-19 11:00:13 +010044struct imsi_acl_entry {
45 struct llist_head list;
46 char imsi[16+1];
47};
Harald Weltec5d4a0c2010-07-02 22:47:59 +020048
49#define GSM48_MAX_APN_LEN 102 /* 10.5.6.1 */
50static char *gprs_apn2str(uint8_t *apn, unsigned int len)
51{
52 static char apnbuf[GSM48_MAX_APN_LEN+1];
Holger Hans Peter Freyther80e03652013-07-04 18:44:16 +020053 unsigned int i = 0;
Harald Weltec5d4a0c2010-07-02 22:47:59 +020054
55 if (!apn)
56 return "";
57
58 if (len > sizeof(apnbuf)-1)
59 len = sizeof(apnbuf)-1;
60
61 memcpy(apnbuf, apn, len);
62 apnbuf[len] = '\0';
63
64 /* replace the domain name step sizes with dots */
65 while (i < len) {
66 unsigned int step = apnbuf[i];
67 apnbuf[i] = '.';
68 i += step+1;
69 }
70
71 return apnbuf+1;
72}
73
74static char *gprs_pdpaddr2str(uint8_t *pdpa, uint8_t len)
75{
76 static char str[INET6_ADDRSTRLEN + 10];
77
78 if (!pdpa || len < 2)
79 return "none";
80
81 switch (pdpa[0] & 0x0f) {
82 case PDP_TYPE_ORG_IETF:
83 switch (pdpa[1]) {
84 case PDP_TYPE_N_IETF_IPv4:
85 if (len < 2 + 4)
86 break;
87 strcpy(str, "IPv4 ");
88 inet_ntop(AF_INET, pdpa+2, str+5, sizeof(str)-5);
89 return str;
90 case PDP_TYPE_N_IETF_IPv6:
91 if (len < 2 + 8)
92 break;
93 strcpy(str, "IPv6 ");
94 inet_ntop(AF_INET6, pdpa+2, str+5, sizeof(str)-5);
95 return str;
96 default:
97 break;
98 }
99 break;
100 case PDP_TYPE_ORG_ETSI:
101 if (pdpa[1] == PDP_TYPE_N_ETSI_PPP)
102 return "PPP";
103 break;
104 default:
105 break;
106 }
107
108 return "invalid";
109}
110
Harald Welte288be162010-05-01 16:48:27 +0200111static struct cmd_node sgsn_node = {
112 SGSN_NODE,
Harald Welte570ce242012-08-17 13:16:10 +0200113 "%s(config-sgsn)# ",
Harald Welte288be162010-05-01 16:48:27 +0200114 1,
115};
116
117static int config_write_sgsn(struct vty *vty)
118{
Harald Welte77289c22010-05-18 14:32:29 +0200119 struct sgsn_ggsn_ctx *gctx;
Harald Welte7f6da482013-03-19 11:00:13 +0100120 struct imsi_acl_entry *acl;
Harald Welte288be162010-05-01 16:48:27 +0200121
122 vty_out(vty, "sgsn%s", VTY_NEWLINE);
123
Harald Weltee300d002010-06-02 12:41:34 +0200124 vty_out(vty, " gtp local-ip %s%s",
125 inet_ntoa(g_cfg->gtp_listenaddr.sin_addr), VTY_NEWLINE);
126
Harald Welted193cb32010-05-17 22:58:03 +0200127 llist_for_each_entry(gctx, &sgsn_ggsn_ctxts, list) {
Harald Welteff3bde82010-05-19 15:09:09 +0200128 vty_out(vty, " ggsn %u remote-ip %s%s", gctx->id,
Harald Welted193cb32010-05-17 22:58:03 +0200129 inet_ntoa(gctx->remote_addr), VTY_NEWLINE);
Harald Welteff3bde82010-05-19 15:09:09 +0200130 vty_out(vty, " ggsn %u gtp-version %u%s", gctx->id,
Harald Welted193cb32010-05-17 22:58:03 +0200131 gctx->gtp_version, VTY_NEWLINE);
Harald Welte288be162010-05-01 16:48:27 +0200132 }
133
Harald Welte7f6da482013-03-19 11:00:13 +0100134 llist_for_each_entry(acl, &g_cfg->imsi_acl, list)
135 vty_out(vty, " imsi-acl add %s%s", acl->imsi, VTY_NEWLINE);
136
Harald Welte288be162010-05-01 16:48:27 +0200137 return CMD_SUCCESS;
138}
139
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100140#define SGSN_STR "Configure the SGSN\n"
141#define GGSN_STR "Configure the GGSN information\n"
Harald Weltee300d002010-06-02 12:41:34 +0200142
143DEFUN(cfg_sgsn, cfg_sgsn_cmd,
144 "sgsn",
145 SGSN_STR)
Harald Welte288be162010-05-01 16:48:27 +0200146{
147 vty->node = SGSN_NODE;
148 return CMD_SUCCESS;
149}
150
Harald Weltee300d002010-06-02 12:41:34 +0200151DEFUN(cfg_sgsn_bind_addr, cfg_sgsn_bind_addr_cmd,
152 "gtp local-ip A.B.C.D",
153 "GTP Parameters\n"
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100154 "Set the IP address for the local GTP bind\n"
155 "IPv4 Address\n")
Harald Weltee300d002010-06-02 12:41:34 +0200156{
157 inet_aton(argv[0], &g_cfg->gtp_listenaddr.sin_addr);
158
159 return CMD_SUCCESS;
160}
161
Harald Welted193cb32010-05-17 22:58:03 +0200162DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd,
163 "ggsn <0-255> remote-ip A.B.C.D",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100164 GGSN_STR "GGSN Number\n" IP_STR "IPv4 Address\n")
Harald Welted193cb32010-05-17 22:58:03 +0200165{
166 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200167 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welte288be162010-05-01 16:48:27 +0200168
Harald Welted193cb32010-05-17 22:58:03 +0200169 inet_aton(argv[1], &ggc->remote_addr);
Harald Welte288be162010-05-01 16:48:27 +0200170
Harald Welted193cb32010-05-17 22:58:03 +0200171 return CMD_SUCCESS;
172}
173
174#if 0
175DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd,
176 "ggsn <0-255> remote-port <0-65535>",
177 "")
178{
179 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200180 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200181 uint16_t port = atoi(argv[1]);
182
183}
184#endif
185
186DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd,
187 "ggsn <0-255> gtp-version (0|1)",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100188 GGSN_STR "GGSN Number\n" "GTP Version\n"
189 "Version 0\n" "Version 1\n")
Harald Welted193cb32010-05-17 22:58:03 +0200190{
191 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200192 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200193
194 if (atoi(argv[1]))
195 ggc->gtp_version = 1;
196 else
197 ggc->gtp_version = 0;
198
199 return CMD_SUCCESS;
200}
201
202#if 0
203DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd,
204 "apn APNAME ggsn <0-255>",
205 "")
206{
207 struct apn_ctx **
208}
209#endif
210
211const struct value_string gprs_mm_st_strs[] = {
212 { GMM_DEREGISTERED, "DEREGISTERED" },
213 { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" },
214 { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" },
Harald Weltebffeff82010-06-09 15:50:45 +0200215 { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" },
Harald Welted193cb32010-05-17 22:58:03 +0200216 { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" },
217 { 0, NULL }
218};
219
220static void vty_dump_pdp(struct vty *vty, const char *pfx,
221 struct sgsn_pdp_ctx *pdp)
222{
223 vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s",
Harald Welte6abf94e2010-05-18 10:35:06 +0200224 pfx, pdp->mm->imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200225 vty_out(vty, "%s APN: %s%s", pfx,
226 gprs_apn2str(pdp->lib->apn_use.v, pdp->lib->apn_use.l),
227 VTY_NEWLINE);
228 vty_out(vty, "%s PDP Address: %s%s", pfx,
229 gprs_pdpaddr2str(pdp->lib->eua.v, pdp->lib->eua.l),
230 VTY_NEWLINE);
Harald Welteefbdee92010-06-10 00:20:12 +0200231 vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200232}
233
234static void vty_dump_mmctx(struct vty *vty, const char *pfx,
235 struct sgsn_mm_ctx *mm, int pdp)
236{
237 vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s",
238 pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE);
239 vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn,
240 mm->tlli, VTY_NEWLINE);
241 vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, "
242 "Cell ID: %u%s", pfx,
243 get_value_string(gprs_mm_st_strs, mm->mm_state),
244 mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac,
245 mm->cell_id, VTY_NEWLINE);
246
Harald Welte8acd88f2010-05-18 10:57:45 +0200247 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
248
Harald Welted193cb32010-05-17 22:58:03 +0200249 if (pdp) {
250 struct sgsn_pdp_ctx *pdp;
251
252 llist_for_each_entry(pdp, &mm->pdp_list, list)
253 vty_dump_pdp(vty, " ", pdp);
254 }
255}
256
257DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn",
258 SHOW_STR "Display information about the SGSN")
259{
260 /* FIXME: statistics */
261 return CMD_SUCCESS;
262}
263
264#define MMCTX_STR "MM Context\n"
265#define INCLUDE_PDP_STR "Include PDP Context Information\n"
266
267#if 0
268DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd,
269 "show mm-context tlli HEX [pdp]",
270 SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR)
271{
272 uint32_t tlli;
273 struct sgsn_mm_ctx *mm;
274
275 tlli = strtoul(argv[0], NULL, 16);
276 mm = sgsn_mm_ctx_by_tlli(tlli);
277 if (!mm) {
278 vty_out(vty, "No MM context for TLLI %08x%s",
279 tlli, VTY_NEWLINE);
280 return CMD_WARNING;
281 }
282 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
283 return CMD_SUCCESS;
284}
285#endif
286
287DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd,
288 "show mm-context imsi IMSI [pdp]",
289 SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n"
290 INCLUDE_PDP_STR)
291{
292 struct sgsn_mm_ctx *mm;
293
294 mm = sgsn_mm_ctx_by_imsi(argv[0]);
295 if (!mm) {
296 vty_out(vty, "No MM context for IMSI %s%s",
297 argv[0], VTY_NEWLINE);
298 return CMD_WARNING;
299 }
300 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
301 return CMD_SUCCESS;
302}
303
304DEFUN(swow_mmctx_all, show_mmctx_all_cmd,
305 "show mm-context all [pdp]",
306 SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR)
307{
308 struct sgsn_mm_ctx *mm;
309
310 llist_for_each_entry(mm, &sgsn_mm_ctxts, list)
311 vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0);
312
313 return CMD_SUCCESS;
314}
315
Harald Welted193cb32010-05-17 22:58:03 +0200316DEFUN(show_pdpctx_all, show_pdpctx_all_cmd,
317 "show pdp-context all",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100318 SHOW_STR "Display information on PDP Context\n" "Show everything\n")
Harald Welted193cb32010-05-17 22:58:03 +0200319{
320 struct sgsn_pdp_ctx *pdp;
321
322 llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list)
323 vty_dump_pdp(vty, "", pdp);
324
325 return CMD_SUCCESS;
326}
Harald Welte288be162010-05-01 16:48:27 +0200327
Harald Welte7f6da482013-03-19 11:00:13 +0100328/* temporary IMSI ACL hack */
329struct imsi_acl_entry *sgsn_acl_lookup(const char *imsi)
330{
331 struct imsi_acl_entry *acl;
332 llist_for_each_entry(acl, &g_cfg->imsi_acl, list) {
333 if (!strcmp(imsi, acl->imsi))
334 return acl;
335 }
336 return NULL;
337}
338
339int sgsn_acl_add(const char *imsi)
340{
341 struct imsi_acl_entry *acl;
342
343 if (sgsn_acl_lookup(imsi))
344 return -EEXIST;
345
346 acl = talloc_zero(NULL, struct imsi_acl_entry);
347 if (!acl)
348 return -ENOMEM;
349 strncpy(acl->imsi, imsi, sizeof(acl->imsi));
350
351 llist_add(&acl->list, &g_cfg->imsi_acl);
352
353 return 0;
354}
355
356int sgsn_acl_del(const char *imsi)
357{
358 struct imsi_acl_entry *acl;
359
360 acl = sgsn_acl_lookup(imsi);
361 if (!acl)
362 return -ENODEV;
363
364 llist_del(&acl->list);
365 talloc_free(acl);
366
367 return 0;
368}
369
370
371DEFUN(imsi_acl, cfg_imsi_acl_cmd,
372 "imsi-acl (add|del) IMSI",
373 "Access Control List of foreign IMSIs\n"
374 "Add IMSI to ACL\n"
375 "Remove IMSI from ACL\n"
376 "IMSI of subscriber\n")
377{
378 const char *op = argv[0];
379 const char *imsi = argv[1];
380 int rc;
381
382 if (!strcmp(op, "add"))
383 rc = sgsn_acl_add(imsi);
384 else
385 rc = sgsn_acl_del(imsi);
386
387 if (rc < 0) {
388 vty_out(vty, "%% unable to %s ACL\n", op);
389 return CMD_WARNING;
390 }
391
392 return CMD_SUCCESS;
393}
394
Harald Welte288be162010-05-01 16:48:27 +0200395int sgsn_vty_init(void)
396{
Harald Welted193cb32010-05-17 22:58:03 +0200397 install_element_ve(&show_sgsn_cmd);
398 //install_element_ve(&show_mmctx_tlli_cmd);
399 install_element_ve(&show_mmctx_imsi_cmd);
400 install_element_ve(&show_mmctx_all_cmd);
401 install_element_ve(&show_pdpctx_all_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200402
403 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
404 install_node(&sgsn_node, config_write_sgsn);
405 install_default(SGSN_NODE);
Harald Welte62ab20c2010-05-14 18:59:17 +0200406 install_element(SGSN_NODE, &ournode_exit_cmd);
Harald Welte54f74242010-05-14 19:11:04 +0200407 install_element(SGSN_NODE, &ournode_end_cmd);
Harald Weltee300d002010-06-02 12:41:34 +0200408 install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd);
Harald Welted193cb32010-05-17 22:58:03 +0200409 install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd);
410 //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd);
411 install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd);
Harald Welte7f6da482013-03-19 11:00:13 +0100412 install_element(SGSN_NODE, &cfg_imsi_acl_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200413
414 return 0;
415}
416
417int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
418{
419 int rc;
420
421 g_cfg = cfg;
Harald Welte7f6da482013-03-19 11:00:13 +0100422 INIT_LLIST_HEAD(&g_cfg->imsi_acl);
423
Harald Weltedcccb182010-05-16 20:52:23 +0200424 rc = vty_read_config_file(config_file, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200425 if (rc < 0) {
426 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
427 return rc;
428 }
429
430 return 0;
431}