blob: 63816710ed4ee04ce432e55e9e762859a0204433 [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
Jacob Erlbeck106f5472014-11-04 10:08:37 +010044const struct value_string sgsn_auth_pol_strs[] = {
45 { SGSN_AUTH_POLICY_OPEN, "accept-all" },
46 { SGSN_AUTH_POLICY_CLOSED, "closed" },
47 { SGSN_AUTH_POLICY_ACL_ONLY, "acl-only" },
48 { 0, NULL }
49};
50
51
Harald Weltec5d4a0c2010-07-02 22:47:59 +020052#define GSM48_MAX_APN_LEN 102 /* 10.5.6.1 */
53static char *gprs_apn2str(uint8_t *apn, unsigned int len)
54{
55 static char apnbuf[GSM48_MAX_APN_LEN+1];
Holger Hans Peter Freyther80e03652013-07-04 18:44:16 +020056 unsigned int i = 0;
Harald Weltec5d4a0c2010-07-02 22:47:59 +020057
58 if (!apn)
59 return "";
60
61 if (len > sizeof(apnbuf)-1)
62 len = sizeof(apnbuf)-1;
63
64 memcpy(apnbuf, apn, len);
65 apnbuf[len] = '\0';
66
67 /* replace the domain name step sizes with dots */
68 while (i < len) {
69 unsigned int step = apnbuf[i];
70 apnbuf[i] = '.';
71 i += step+1;
72 }
73
74 return apnbuf+1;
75}
76
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +010077char *gprs_pdpaddr2str(uint8_t *pdpa, uint8_t len)
Harald Weltec5d4a0c2010-07-02 22:47:59 +020078{
79 static char str[INET6_ADDRSTRLEN + 10];
80
81 if (!pdpa || len < 2)
82 return "none";
83
84 switch (pdpa[0] & 0x0f) {
85 case PDP_TYPE_ORG_IETF:
86 switch (pdpa[1]) {
87 case PDP_TYPE_N_IETF_IPv4:
88 if (len < 2 + 4)
89 break;
90 strcpy(str, "IPv4 ");
91 inet_ntop(AF_INET, pdpa+2, str+5, sizeof(str)-5);
92 return str;
93 case PDP_TYPE_N_IETF_IPv6:
94 if (len < 2 + 8)
95 break;
96 strcpy(str, "IPv6 ");
97 inet_ntop(AF_INET6, pdpa+2, str+5, sizeof(str)-5);
98 return str;
99 default:
100 break;
101 }
102 break;
103 case PDP_TYPE_ORG_ETSI:
104 if (pdpa[1] == PDP_TYPE_N_ETSI_PPP)
105 return "PPP";
106 break;
107 default:
108 break;
109 }
110
111 return "invalid";
112}
113
Harald Welte288be162010-05-01 16:48:27 +0200114static struct cmd_node sgsn_node = {
115 SGSN_NODE,
Harald Welte570ce242012-08-17 13:16:10 +0200116 "%s(config-sgsn)# ",
Harald Welte288be162010-05-01 16:48:27 +0200117 1,
118};
119
120static int config_write_sgsn(struct vty *vty)
121{
Harald Welte77289c22010-05-18 14:32:29 +0200122 struct sgsn_ggsn_ctx *gctx;
Harald Welte7f6da482013-03-19 11:00:13 +0100123 struct imsi_acl_entry *acl;
Harald Welte288be162010-05-01 16:48:27 +0200124
125 vty_out(vty, "sgsn%s", VTY_NEWLINE);
126
Harald Weltee300d002010-06-02 12:41:34 +0200127 vty_out(vty, " gtp local-ip %s%s",
128 inet_ntoa(g_cfg->gtp_listenaddr.sin_addr), VTY_NEWLINE);
129
Harald Welted193cb32010-05-17 22:58:03 +0200130 llist_for_each_entry(gctx, &sgsn_ggsn_ctxts, list) {
Harald Welteff3bde82010-05-19 15:09:09 +0200131 vty_out(vty, " ggsn %u remote-ip %s%s", gctx->id,
Harald Welted193cb32010-05-17 22:58:03 +0200132 inet_ntoa(gctx->remote_addr), VTY_NEWLINE);
Harald Welteff3bde82010-05-19 15:09:09 +0200133 vty_out(vty, " ggsn %u gtp-version %u%s", gctx->id,
Harald Welted193cb32010-05-17 22:58:03 +0200134 gctx->gtp_version, VTY_NEWLINE);
Harald Welte288be162010-05-01 16:48:27 +0200135 }
136
Harald Welte3dfb5492013-03-19 11:48:54 +0100137 vty_out(vty, " auth-policy %s%s",
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100138 get_value_string(sgsn_auth_pol_strs, g_cfg->auth_policy),
139 VTY_NEWLINE);
Harald Welte7f6da482013-03-19 11:00:13 +0100140 llist_for_each_entry(acl, &g_cfg->imsi_acl, list)
141 vty_out(vty, " imsi-acl add %s%s", acl->imsi, VTY_NEWLINE);
142
Harald Welte288be162010-05-01 16:48:27 +0200143 return CMD_SUCCESS;
144}
145
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100146#define SGSN_STR "Configure the SGSN\n"
147#define GGSN_STR "Configure the GGSN information\n"
Harald Weltee300d002010-06-02 12:41:34 +0200148
149DEFUN(cfg_sgsn, cfg_sgsn_cmd,
150 "sgsn",
151 SGSN_STR)
Harald Welte288be162010-05-01 16:48:27 +0200152{
153 vty->node = SGSN_NODE;
154 return CMD_SUCCESS;
155}
156
Harald Weltee300d002010-06-02 12:41:34 +0200157DEFUN(cfg_sgsn_bind_addr, cfg_sgsn_bind_addr_cmd,
158 "gtp local-ip A.B.C.D",
159 "GTP Parameters\n"
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100160 "Set the IP address for the local GTP bind\n"
161 "IPv4 Address\n")
Harald Weltee300d002010-06-02 12:41:34 +0200162{
163 inet_aton(argv[0], &g_cfg->gtp_listenaddr.sin_addr);
164
165 return CMD_SUCCESS;
166}
167
Harald Welted193cb32010-05-17 22:58:03 +0200168DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd,
169 "ggsn <0-255> remote-ip A.B.C.D",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100170 GGSN_STR "GGSN Number\n" IP_STR "IPv4 Address\n")
Harald Welted193cb32010-05-17 22:58:03 +0200171{
172 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200173 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welte288be162010-05-01 16:48:27 +0200174
Harald Welted193cb32010-05-17 22:58:03 +0200175 inet_aton(argv[1], &ggc->remote_addr);
Harald Welte288be162010-05-01 16:48:27 +0200176
Harald Welted193cb32010-05-17 22:58:03 +0200177 return CMD_SUCCESS;
178}
179
180#if 0
181DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd,
182 "ggsn <0-255> remote-port <0-65535>",
183 "")
184{
185 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200186 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200187 uint16_t port = atoi(argv[1]);
188
189}
190#endif
191
192DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd,
193 "ggsn <0-255> gtp-version (0|1)",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100194 GGSN_STR "GGSN Number\n" "GTP Version\n"
195 "Version 0\n" "Version 1\n")
Harald Welted193cb32010-05-17 22:58:03 +0200196{
197 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200198 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200199
200 if (atoi(argv[1]))
201 ggc->gtp_version = 1;
202 else
203 ggc->gtp_version = 0;
204
205 return CMD_SUCCESS;
206}
207
208#if 0
209DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd,
210 "apn APNAME ggsn <0-255>",
211 "")
212{
213 struct apn_ctx **
214}
215#endif
216
217const struct value_string gprs_mm_st_strs[] = {
218 { GMM_DEREGISTERED, "DEREGISTERED" },
219 { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" },
220 { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" },
Harald Weltebffeff82010-06-09 15:50:45 +0200221 { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" },
Harald Welted193cb32010-05-17 22:58:03 +0200222 { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" },
223 { 0, NULL }
224};
225
226static void vty_dump_pdp(struct vty *vty, const char *pfx,
227 struct sgsn_pdp_ctx *pdp)
228{
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200229 const char *imsi = pdp->mm ? pdp->mm->imsi : "(detaching)";
Harald Welted193cb32010-05-17 22:58:03 +0200230 vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s",
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200231 pfx, imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200232 vty_out(vty, "%s APN: %s%s", pfx,
233 gprs_apn2str(pdp->lib->apn_use.v, pdp->lib->apn_use.l),
234 VTY_NEWLINE);
235 vty_out(vty, "%s PDP Address: %s%s", pfx,
236 gprs_pdpaddr2str(pdp->lib->eua.v, pdp->lib->eua.l),
237 VTY_NEWLINE);
Harald Welteefbdee92010-06-10 00:20:12 +0200238 vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200239}
240
241static void vty_dump_mmctx(struct vty *vty, const char *pfx,
242 struct sgsn_mm_ctx *mm, int pdp)
243{
244 vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s",
245 pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE);
246 vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn,
247 mm->tlli, VTY_NEWLINE);
248 vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, "
249 "Cell ID: %u%s", pfx,
250 get_value_string(gprs_mm_st_strs, mm->mm_state),
251 mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac,
252 mm->cell_id, VTY_NEWLINE);
253
Harald Welte8acd88f2010-05-18 10:57:45 +0200254 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
255
Harald Welted193cb32010-05-17 22:58:03 +0200256 if (pdp) {
257 struct sgsn_pdp_ctx *pdp;
258
259 llist_for_each_entry(pdp, &mm->pdp_list, list)
260 vty_dump_pdp(vty, " ", pdp);
261 }
262}
263
264DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn",
265 SHOW_STR "Display information about the SGSN")
266{
267 /* FIXME: statistics */
268 return CMD_SUCCESS;
269}
270
271#define MMCTX_STR "MM Context\n"
272#define INCLUDE_PDP_STR "Include PDP Context Information\n"
273
274#if 0
275DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd,
276 "show mm-context tlli HEX [pdp]",
277 SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR)
278{
279 uint32_t tlli;
280 struct sgsn_mm_ctx *mm;
281
282 tlli = strtoul(argv[0], NULL, 16);
283 mm = sgsn_mm_ctx_by_tlli(tlli);
284 if (!mm) {
285 vty_out(vty, "No MM context for TLLI %08x%s",
286 tlli, VTY_NEWLINE);
287 return CMD_WARNING;
288 }
289 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
290 return CMD_SUCCESS;
291}
292#endif
293
294DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd,
295 "show mm-context imsi IMSI [pdp]",
296 SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n"
297 INCLUDE_PDP_STR)
298{
299 struct sgsn_mm_ctx *mm;
300
301 mm = sgsn_mm_ctx_by_imsi(argv[0]);
302 if (!mm) {
303 vty_out(vty, "No MM context for IMSI %s%s",
304 argv[0], VTY_NEWLINE);
305 return CMD_WARNING;
306 }
307 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
308 return CMD_SUCCESS;
309}
310
311DEFUN(swow_mmctx_all, show_mmctx_all_cmd,
312 "show mm-context all [pdp]",
313 SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR)
314{
315 struct sgsn_mm_ctx *mm;
316
317 llist_for_each_entry(mm, &sgsn_mm_ctxts, list)
318 vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0);
319
320 return CMD_SUCCESS;
321}
322
Harald Welted193cb32010-05-17 22:58:03 +0200323DEFUN(show_pdpctx_all, show_pdpctx_all_cmd,
324 "show pdp-context all",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100325 SHOW_STR "Display information on PDP Context\n" "Show everything\n")
Harald Welted193cb32010-05-17 22:58:03 +0200326{
327 struct sgsn_pdp_ctx *pdp;
328
329 llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list)
330 vty_dump_pdp(vty, "", pdp);
331
332 return CMD_SUCCESS;
333}
Harald Welte288be162010-05-01 16:48:27 +0200334
Harald Welte7f6da482013-03-19 11:00:13 +0100335
336DEFUN(imsi_acl, cfg_imsi_acl_cmd,
337 "imsi-acl (add|del) IMSI",
338 "Access Control List of foreign IMSIs\n"
339 "Add IMSI to ACL\n"
340 "Remove IMSI from ACL\n"
341 "IMSI of subscriber\n")
342{
343 const char *op = argv[0];
344 const char *imsi = argv[1];
345 int rc;
346
347 if (!strcmp(op, "add"))
Jacob Erlbeck3b5d4072014-10-24 15:11:03 +0200348 rc = sgsn_acl_add(imsi, g_cfg);
Harald Welte7f6da482013-03-19 11:00:13 +0100349 else
Jacob Erlbeck3b5d4072014-10-24 15:11:03 +0200350 rc = sgsn_acl_del(imsi, g_cfg);
Harald Welte7f6da482013-03-19 11:00:13 +0100351
352 if (rc < 0) {
353 vty_out(vty, "%% unable to %s ACL\n", op);
354 return CMD_WARNING;
355 }
356
357 return CMD_SUCCESS;
358}
359
Harald Welte3dfb5492013-03-19 11:48:54 +0100360DEFUN(cfg_auth_policy, cfg_auth_policy_cmd,
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100361 "auth-policy (accept-all|closed|acl-only)",
Harald Welte3dfb5492013-03-19 11:48:54 +0100362 "Autorization Policy of SGSN\n"
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100363 "Accept all IMSIs (DANGEROUS)\n"
364 "Accept only home network subscribers or those in the ACL\n"
365 "Accept only subscribers in the ACL\n")
Harald Welte3dfb5492013-03-19 11:48:54 +0100366{
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100367 int val = get_string_value(sgsn_auth_pol_strs, argv[0]);
368 OSMO_ASSERT(val >= SGSN_AUTH_POLICY_OPEN && val <= SGSN_AUTH_POLICY_ACL_ONLY);
369 g_cfg->auth_policy = val;
Harald Welte3dfb5492013-03-19 11:48:54 +0100370
371 return CMD_SUCCESS;
372}
373
Harald Welte288be162010-05-01 16:48:27 +0200374int sgsn_vty_init(void)
375{
Harald Welted193cb32010-05-17 22:58:03 +0200376 install_element_ve(&show_sgsn_cmd);
377 //install_element_ve(&show_mmctx_tlli_cmd);
378 install_element_ve(&show_mmctx_imsi_cmd);
379 install_element_ve(&show_mmctx_all_cmd);
380 install_element_ve(&show_pdpctx_all_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200381
382 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
383 install_node(&sgsn_node, config_write_sgsn);
Jacob Erlbeck36722e12013-10-29 09:30:30 +0100384 vty_install_default(SGSN_NODE);
Harald Weltee300d002010-06-02 12:41:34 +0200385 install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd);
Harald Welted193cb32010-05-17 22:58:03 +0200386 install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd);
387 //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd);
388 install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd);
Harald Welte7f6da482013-03-19 11:00:13 +0100389 install_element(SGSN_NODE, &cfg_imsi_acl_cmd);
Harald Welte3dfb5492013-03-19 11:48:54 +0100390 install_element(SGSN_NODE, &cfg_auth_policy_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200391
392 return 0;
393}
394
395int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
396{
397 int rc;
398
399 g_cfg = cfg;
Harald Welte7f6da482013-03-19 11:00:13 +0100400
Harald Weltedcccb182010-05-16 20:52:23 +0200401 rc = vty_read_config_file(config_file, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200402 if (rc < 0) {
403 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
404 return rc;
405 }
406
407 return 0;
408}