blob: 31c94f796d8e288cb1e1a7461a918d5127fbd6c5 [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{
225 vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s",
Harald Welte6abf94e2010-05-18 10:35:06 +0200226 pfx, pdp->mm->imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200227 vty_out(vty, "%s APN: %s%s", pfx,
228 gprs_apn2str(pdp->lib->apn_use.v, pdp->lib->apn_use.l),
229 VTY_NEWLINE);
230 vty_out(vty, "%s PDP Address: %s%s", pfx,
231 gprs_pdpaddr2str(pdp->lib->eua.v, pdp->lib->eua.l),
232 VTY_NEWLINE);
Harald Welteefbdee92010-06-10 00:20:12 +0200233 vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200234}
235
236static void vty_dump_mmctx(struct vty *vty, const char *pfx,
237 struct sgsn_mm_ctx *mm, int pdp)
238{
239 vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s",
240 pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE);
241 vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn,
242 mm->tlli, VTY_NEWLINE);
243 vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, "
244 "Cell ID: %u%s", pfx,
245 get_value_string(gprs_mm_st_strs, mm->mm_state),
246 mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac,
247 mm->cell_id, VTY_NEWLINE);
248
Harald Welte8acd88f2010-05-18 10:57:45 +0200249 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
250
Harald Welted193cb32010-05-17 22:58:03 +0200251 if (pdp) {
252 struct sgsn_pdp_ctx *pdp;
253
254 llist_for_each_entry(pdp, &mm->pdp_list, list)
255 vty_dump_pdp(vty, " ", pdp);
256 }
257}
258
259DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn",
260 SHOW_STR "Display information about the SGSN")
261{
262 /* FIXME: statistics */
263 return CMD_SUCCESS;
264}
265
266#define MMCTX_STR "MM Context\n"
267#define INCLUDE_PDP_STR "Include PDP Context Information\n"
268
269#if 0
270DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd,
271 "show mm-context tlli HEX [pdp]",
272 SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR)
273{
274 uint32_t tlli;
275 struct sgsn_mm_ctx *mm;
276
277 tlli = strtoul(argv[0], NULL, 16);
278 mm = sgsn_mm_ctx_by_tlli(tlli);
279 if (!mm) {
280 vty_out(vty, "No MM context for TLLI %08x%s",
281 tlli, VTY_NEWLINE);
282 return CMD_WARNING;
283 }
284 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
285 return CMD_SUCCESS;
286}
287#endif
288
289DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd,
290 "show mm-context imsi IMSI [pdp]",
291 SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n"
292 INCLUDE_PDP_STR)
293{
294 struct sgsn_mm_ctx *mm;
295
296 mm = sgsn_mm_ctx_by_imsi(argv[0]);
297 if (!mm) {
298 vty_out(vty, "No MM context for IMSI %s%s",
299 argv[0], VTY_NEWLINE);
300 return CMD_WARNING;
301 }
302 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
303 return CMD_SUCCESS;
304}
305
306DEFUN(swow_mmctx_all, show_mmctx_all_cmd,
307 "show mm-context all [pdp]",
308 SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR)
309{
310 struct sgsn_mm_ctx *mm;
311
312 llist_for_each_entry(mm, &sgsn_mm_ctxts, list)
313 vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0);
314
315 return CMD_SUCCESS;
316}
317
Harald Welted193cb32010-05-17 22:58:03 +0200318DEFUN(show_pdpctx_all, show_pdpctx_all_cmd,
319 "show pdp-context all",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100320 SHOW_STR "Display information on PDP Context\n" "Show everything\n")
Harald Welted193cb32010-05-17 22:58:03 +0200321{
322 struct sgsn_pdp_ctx *pdp;
323
324 llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list)
325 vty_dump_pdp(vty, "", pdp);
326
327 return CMD_SUCCESS;
328}
Harald Welte288be162010-05-01 16:48:27 +0200329
Harald Welte7f6da482013-03-19 11:00:13 +0100330/* temporary IMSI ACL hack */
331struct imsi_acl_entry *sgsn_acl_lookup(const char *imsi)
332{
333 struct imsi_acl_entry *acl;
334 llist_for_each_entry(acl, &g_cfg->imsi_acl, list) {
335 if (!strcmp(imsi, acl->imsi))
336 return acl;
337 }
338 return NULL;
339}
340
341int sgsn_acl_add(const char *imsi)
342{
343 struct imsi_acl_entry *acl;
344
345 if (sgsn_acl_lookup(imsi))
346 return -EEXIST;
347
348 acl = talloc_zero(NULL, struct imsi_acl_entry);
349 if (!acl)
350 return -ENOMEM;
351 strncpy(acl->imsi, imsi, sizeof(acl->imsi));
352
353 llist_add(&acl->list, &g_cfg->imsi_acl);
354
355 return 0;
356}
357
358int sgsn_acl_del(const char *imsi)
359{
360 struct imsi_acl_entry *acl;
361
362 acl = sgsn_acl_lookup(imsi);
363 if (!acl)
364 return -ENODEV;
365
366 llist_del(&acl->list);
367 talloc_free(acl);
368
369 return 0;
370}
371
372
373DEFUN(imsi_acl, cfg_imsi_acl_cmd,
374 "imsi-acl (add|del) IMSI",
375 "Access Control List of foreign IMSIs\n"
376 "Add IMSI to ACL\n"
377 "Remove IMSI from ACL\n"
378 "IMSI of subscriber\n")
379{
380 const char *op = argv[0];
381 const char *imsi = argv[1];
382 int rc;
383
384 if (!strcmp(op, "add"))
385 rc = sgsn_acl_add(imsi);
386 else
387 rc = sgsn_acl_del(imsi);
388
389 if (rc < 0) {
390 vty_out(vty, "%% unable to %s ACL\n", op);
391 return CMD_WARNING;
392 }
393
394 return CMD_SUCCESS;
395}
396
Harald Welte3dfb5492013-03-19 11:48:54 +0100397DEFUN(cfg_auth_policy, cfg_auth_policy_cmd,
398 "auth-policy (accept-all|closed)",
399 "Autorization Policy of SGSN\n"
400 "Accept all IMSIs (DANGEROUS\n"
401 "Accept only home network subscribers or those in ACL\n")
402{
403 if (!strcmp(argv[0], "accept-all"))
404 g_cfg->acl_enabled = 0;
405 else
406 g_cfg->acl_enabled = 1;
407
408 return CMD_SUCCESS;
409}
410
Harald Welte288be162010-05-01 16:48:27 +0200411int sgsn_vty_init(void)
412{
Harald Welted193cb32010-05-17 22:58:03 +0200413 install_element_ve(&show_sgsn_cmd);
414 //install_element_ve(&show_mmctx_tlli_cmd);
415 install_element_ve(&show_mmctx_imsi_cmd);
416 install_element_ve(&show_mmctx_all_cmd);
417 install_element_ve(&show_pdpctx_all_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200418
419 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
420 install_node(&sgsn_node, config_write_sgsn);
Jacob Erlbeck36722e12013-10-29 09:30:30 +0100421 vty_install_default(SGSN_NODE);
Harald Weltee300d002010-06-02 12:41:34 +0200422 install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd);
Harald Welted193cb32010-05-17 22:58:03 +0200423 install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd);
424 //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd);
425 install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd);
Harald Welte7f6da482013-03-19 11:00:13 +0100426 install_element(SGSN_NODE, &cfg_imsi_acl_cmd);
Harald Welte3dfb5492013-03-19 11:48:54 +0100427 install_element(SGSN_NODE, &cfg_auth_policy_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200428
429 return 0;
430}
431
432int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
433{
434 int rc;
435
436 g_cfg = cfg;
Harald Welte7f6da482013-03-19 11:00:13 +0100437 INIT_LLIST_HEAD(&g_cfg->imsi_acl);
438
Harald Weltedcccb182010-05-16 20:52:23 +0200439 rc = vty_read_config_file(config_file, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200440 if (rc < 0) {
441 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
442 return rc;
443 }
444
445 return 0;
446}