blob: bfd5333d3145821f3c5cbe2a1710412c506e80ea [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
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +010074char *gprs_pdpaddr2str(uint8_t *pdpa, uint8_t len)
Harald Weltec5d4a0c2010-07-02 22:47:59 +020075{
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 Welte3dfb5492013-03-19 11:48:54 +0100134 vty_out(vty, " auth-policy %s%s",
135 g_cfg->acl_enabled ? "closed" : "accept-all", VTY_NEWLINE);
Harald Welte7f6da482013-03-19 11:00:13 +0100136 llist_for_each_entry(acl, &g_cfg->imsi_acl, list)
137 vty_out(vty, " imsi-acl add %s%s", acl->imsi, VTY_NEWLINE);
138
Harald Welte288be162010-05-01 16:48:27 +0200139 return CMD_SUCCESS;
140}
141
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100142#define SGSN_STR "Configure the SGSN\n"
143#define GGSN_STR "Configure the GGSN information\n"
Harald Weltee300d002010-06-02 12:41:34 +0200144
145DEFUN(cfg_sgsn, cfg_sgsn_cmd,
146 "sgsn",
147 SGSN_STR)
Harald Welte288be162010-05-01 16:48:27 +0200148{
149 vty->node = SGSN_NODE;
150 return CMD_SUCCESS;
151}
152
Harald Weltee300d002010-06-02 12:41:34 +0200153DEFUN(cfg_sgsn_bind_addr, cfg_sgsn_bind_addr_cmd,
154 "gtp local-ip A.B.C.D",
155 "GTP Parameters\n"
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100156 "Set the IP address for the local GTP bind\n"
157 "IPv4 Address\n")
Harald Weltee300d002010-06-02 12:41:34 +0200158{
159 inet_aton(argv[0], &g_cfg->gtp_listenaddr.sin_addr);
160
161 return CMD_SUCCESS;
162}
163
Harald Welted193cb32010-05-17 22:58:03 +0200164DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd,
165 "ggsn <0-255> remote-ip A.B.C.D",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100166 GGSN_STR "GGSN Number\n" IP_STR "IPv4 Address\n")
Harald Welted193cb32010-05-17 22:58:03 +0200167{
168 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200169 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welte288be162010-05-01 16:48:27 +0200170
Harald Welted193cb32010-05-17 22:58:03 +0200171 inet_aton(argv[1], &ggc->remote_addr);
Harald Welte288be162010-05-01 16:48:27 +0200172
Harald Welted193cb32010-05-17 22:58:03 +0200173 return CMD_SUCCESS;
174}
175
176#if 0
177DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd,
178 "ggsn <0-255> remote-port <0-65535>",
179 "")
180{
181 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200182 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200183 uint16_t port = atoi(argv[1]);
184
185}
186#endif
187
188DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd,
189 "ggsn <0-255> gtp-version (0|1)",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100190 GGSN_STR "GGSN Number\n" "GTP Version\n"
191 "Version 0\n" "Version 1\n")
Harald Welted193cb32010-05-17 22:58:03 +0200192{
193 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200194 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200195
196 if (atoi(argv[1]))
197 ggc->gtp_version = 1;
198 else
199 ggc->gtp_version = 0;
200
201 return CMD_SUCCESS;
202}
203
204#if 0
205DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd,
206 "apn APNAME ggsn <0-255>",
207 "")
208{
209 struct apn_ctx **
210}
211#endif
212
213const struct value_string gprs_mm_st_strs[] = {
214 { GMM_DEREGISTERED, "DEREGISTERED" },
215 { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" },
216 { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" },
Harald Weltebffeff82010-06-09 15:50:45 +0200217 { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" },
Harald Welted193cb32010-05-17 22:58:03 +0200218 { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" },
219 { 0, NULL }
220};
221
222static void vty_dump_pdp(struct vty *vty, const char *pfx,
223 struct sgsn_pdp_ctx *pdp)
224{
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200225 const char *imsi = pdp->mm ? pdp->mm->imsi : "(detaching)";
Harald Welted193cb32010-05-17 22:58:03 +0200226 vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s",
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200227 pfx, imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200228 vty_out(vty, "%s APN: %s%s", pfx,
229 gprs_apn2str(pdp->lib->apn_use.v, pdp->lib->apn_use.l),
230 VTY_NEWLINE);
231 vty_out(vty, "%s PDP Address: %s%s", pfx,
232 gprs_pdpaddr2str(pdp->lib->eua.v, pdp->lib->eua.l),
233 VTY_NEWLINE);
Harald Welteefbdee92010-06-10 00:20:12 +0200234 vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200235}
236
237static void vty_dump_mmctx(struct vty *vty, const char *pfx,
238 struct sgsn_mm_ctx *mm, int pdp)
239{
240 vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s",
241 pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE);
242 vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn,
243 mm->tlli, VTY_NEWLINE);
244 vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, "
245 "Cell ID: %u%s", pfx,
246 get_value_string(gprs_mm_st_strs, mm->mm_state),
247 mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac,
248 mm->cell_id, VTY_NEWLINE);
249
Harald Welte8acd88f2010-05-18 10:57:45 +0200250 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
251
Harald Welted193cb32010-05-17 22:58:03 +0200252 if (pdp) {
253 struct sgsn_pdp_ctx *pdp;
254
255 llist_for_each_entry(pdp, &mm->pdp_list, list)
256 vty_dump_pdp(vty, " ", pdp);
257 }
258}
259
260DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn",
261 SHOW_STR "Display information about the SGSN")
262{
263 /* FIXME: statistics */
264 return CMD_SUCCESS;
265}
266
267#define MMCTX_STR "MM Context\n"
268#define INCLUDE_PDP_STR "Include PDP Context Information\n"
269
270#if 0
271DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd,
272 "show mm-context tlli HEX [pdp]",
273 SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR)
274{
275 uint32_t tlli;
276 struct sgsn_mm_ctx *mm;
277
278 tlli = strtoul(argv[0], NULL, 16);
279 mm = sgsn_mm_ctx_by_tlli(tlli);
280 if (!mm) {
281 vty_out(vty, "No MM context for TLLI %08x%s",
282 tlli, VTY_NEWLINE);
283 return CMD_WARNING;
284 }
285 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
286 return CMD_SUCCESS;
287}
288#endif
289
290DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd,
291 "show mm-context imsi IMSI [pdp]",
292 SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n"
293 INCLUDE_PDP_STR)
294{
295 struct sgsn_mm_ctx *mm;
296
297 mm = sgsn_mm_ctx_by_imsi(argv[0]);
298 if (!mm) {
299 vty_out(vty, "No MM context for IMSI %s%s",
300 argv[0], VTY_NEWLINE);
301 return CMD_WARNING;
302 }
303 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
304 return CMD_SUCCESS;
305}
306
307DEFUN(swow_mmctx_all, show_mmctx_all_cmd,
308 "show mm-context all [pdp]",
309 SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR)
310{
311 struct sgsn_mm_ctx *mm;
312
313 llist_for_each_entry(mm, &sgsn_mm_ctxts, list)
314 vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0);
315
316 return CMD_SUCCESS;
317}
318
Harald Welted193cb32010-05-17 22:58:03 +0200319DEFUN(show_pdpctx_all, show_pdpctx_all_cmd,
320 "show pdp-context all",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100321 SHOW_STR "Display information on PDP Context\n" "Show everything\n")
Harald Welted193cb32010-05-17 22:58:03 +0200322{
323 struct sgsn_pdp_ctx *pdp;
324
325 llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list)
326 vty_dump_pdp(vty, "", pdp);
327
328 return CMD_SUCCESS;
329}
Harald Welte288be162010-05-01 16:48:27 +0200330
Harald Welte7f6da482013-03-19 11:00:13 +0100331/* temporary IMSI ACL hack */
332struct imsi_acl_entry *sgsn_acl_lookup(const char *imsi)
333{
334 struct imsi_acl_entry *acl;
335 llist_for_each_entry(acl, &g_cfg->imsi_acl, list) {
336 if (!strcmp(imsi, acl->imsi))
337 return acl;
338 }
339 return NULL;
340}
341
342int sgsn_acl_add(const char *imsi)
343{
344 struct imsi_acl_entry *acl;
345
346 if (sgsn_acl_lookup(imsi))
347 return -EEXIST;
348
349 acl = talloc_zero(NULL, struct imsi_acl_entry);
350 if (!acl)
351 return -ENOMEM;
352 strncpy(acl->imsi, imsi, sizeof(acl->imsi));
353
354 llist_add(&acl->list, &g_cfg->imsi_acl);
355
356 return 0;
357}
358
359int sgsn_acl_del(const char *imsi)
360{
361 struct imsi_acl_entry *acl;
362
363 acl = sgsn_acl_lookup(imsi);
364 if (!acl)
365 return -ENODEV;
366
367 llist_del(&acl->list);
368 talloc_free(acl);
369
370 return 0;
371}
372
373
374DEFUN(imsi_acl, cfg_imsi_acl_cmd,
375 "imsi-acl (add|del) IMSI",
376 "Access Control List of foreign IMSIs\n"
377 "Add IMSI to ACL\n"
378 "Remove IMSI from ACL\n"
379 "IMSI of subscriber\n")
380{
381 const char *op = argv[0];
382 const char *imsi = argv[1];
383 int rc;
384
385 if (!strcmp(op, "add"))
386 rc = sgsn_acl_add(imsi);
387 else
388 rc = sgsn_acl_del(imsi);
389
390 if (rc < 0) {
391 vty_out(vty, "%% unable to %s ACL\n", op);
392 return CMD_WARNING;
393 }
394
395 return CMD_SUCCESS;
396}
397
Harald Welte3dfb5492013-03-19 11:48:54 +0100398DEFUN(cfg_auth_policy, cfg_auth_policy_cmd,
399 "auth-policy (accept-all|closed)",
400 "Autorization Policy of SGSN\n"
401 "Accept all IMSIs (DANGEROUS\n"
402 "Accept only home network subscribers or those in ACL\n")
403{
404 if (!strcmp(argv[0], "accept-all"))
405 g_cfg->acl_enabled = 0;
406 else
407 g_cfg->acl_enabled = 1;
408
409 return CMD_SUCCESS;
410}
411
Harald Welte288be162010-05-01 16:48:27 +0200412int sgsn_vty_init(void)
413{
Harald Welted193cb32010-05-17 22:58:03 +0200414 install_element_ve(&show_sgsn_cmd);
415 //install_element_ve(&show_mmctx_tlli_cmd);
416 install_element_ve(&show_mmctx_imsi_cmd);
417 install_element_ve(&show_mmctx_all_cmd);
418 install_element_ve(&show_pdpctx_all_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200419
420 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
421 install_node(&sgsn_node, config_write_sgsn);
Jacob Erlbeck36722e12013-10-29 09:30:30 +0100422 vty_install_default(SGSN_NODE);
Harald Weltee300d002010-06-02 12:41:34 +0200423 install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd);
Harald Welted193cb32010-05-17 22:58:03 +0200424 install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd);
425 //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd);
426 install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd);
Harald Welte7f6da482013-03-19 11:00:13 +0100427 install_element(SGSN_NODE, &cfg_imsi_acl_cmd);
Harald Welte3dfb5492013-03-19 11:48:54 +0100428 install_element(SGSN_NODE, &cfg_auth_policy_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200429
430 return 0;
431}
432
433int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
434{
435 int rc;
436
437 g_cfg = cfg;
Harald Welte7f6da482013-03-19 11:00:13 +0100438 INIT_LLIST_HEAD(&g_cfg->imsi_acl);
439
Harald Weltedcccb182010-05-16 20:52:23 +0200440 rc = vty_read_config_file(config_file, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200441 if (rc < 0) {
442 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
443 return rc;
444 }
445
446 return 0;
447}