blob: 4c4eef33125d93f15aa2e488df34dda6be189ba0 [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 Weltec5d4a0c2010-07-02 22:47:59 +020044#define GSM48_MAX_APN_LEN 102 /* 10.5.6.1 */
45static char *gprs_apn2str(uint8_t *apn, unsigned int len)
46{
47 static char apnbuf[GSM48_MAX_APN_LEN+1];
Holger Hans Peter Freyther80e03652013-07-04 18:44:16 +020048 unsigned int i = 0;
Harald Weltec5d4a0c2010-07-02 22:47:59 +020049
50 if (!apn)
51 return "";
52
53 if (len > sizeof(apnbuf)-1)
54 len = sizeof(apnbuf)-1;
55
56 memcpy(apnbuf, apn, len);
57 apnbuf[len] = '\0';
58
59 /* replace the domain name step sizes with dots */
60 while (i < len) {
61 unsigned int step = apnbuf[i];
62 apnbuf[i] = '.';
63 i += step+1;
64 }
65
66 return apnbuf+1;
67}
68
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +010069char *gprs_pdpaddr2str(uint8_t *pdpa, uint8_t len)
Harald Weltec5d4a0c2010-07-02 22:47:59 +020070{
71 static char str[INET6_ADDRSTRLEN + 10];
72
73 if (!pdpa || len < 2)
74 return "none";
75
76 switch (pdpa[0] & 0x0f) {
77 case PDP_TYPE_ORG_IETF:
78 switch (pdpa[1]) {
79 case PDP_TYPE_N_IETF_IPv4:
80 if (len < 2 + 4)
81 break;
82 strcpy(str, "IPv4 ");
83 inet_ntop(AF_INET, pdpa+2, str+5, sizeof(str)-5);
84 return str;
85 case PDP_TYPE_N_IETF_IPv6:
86 if (len < 2 + 8)
87 break;
88 strcpy(str, "IPv6 ");
89 inet_ntop(AF_INET6, pdpa+2, str+5, sizeof(str)-5);
90 return str;
91 default:
92 break;
93 }
94 break;
95 case PDP_TYPE_ORG_ETSI:
96 if (pdpa[1] == PDP_TYPE_N_ETSI_PPP)
97 return "PPP";
98 break;
99 default:
100 break;
101 }
102
103 return "invalid";
104}
105
Harald Welte288be162010-05-01 16:48:27 +0200106static struct cmd_node sgsn_node = {
107 SGSN_NODE,
Harald Welte570ce242012-08-17 13:16:10 +0200108 "%s(config-sgsn)# ",
Harald Welte288be162010-05-01 16:48:27 +0200109 1,
110};
111
112static int config_write_sgsn(struct vty *vty)
113{
Harald Welte77289c22010-05-18 14:32:29 +0200114 struct sgsn_ggsn_ctx *gctx;
Harald Welte7f6da482013-03-19 11:00:13 +0100115 struct imsi_acl_entry *acl;
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
Harald Welte3dfb5492013-03-19 11:48:54 +0100129 vty_out(vty, " auth-policy %s%s",
130 g_cfg->acl_enabled ? "closed" : "accept-all", VTY_NEWLINE);
Harald Welte7f6da482013-03-19 11:00:13 +0100131 llist_for_each_entry(acl, &g_cfg->imsi_acl, list)
132 vty_out(vty, " imsi-acl add %s%s", acl->imsi, VTY_NEWLINE);
133
Harald Welte288be162010-05-01 16:48:27 +0200134 return CMD_SUCCESS;
135}
136
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100137#define SGSN_STR "Configure the SGSN\n"
138#define GGSN_STR "Configure the GGSN information\n"
Harald Weltee300d002010-06-02 12:41:34 +0200139
140DEFUN(cfg_sgsn, cfg_sgsn_cmd,
141 "sgsn",
142 SGSN_STR)
Harald Welte288be162010-05-01 16:48:27 +0200143{
144 vty->node = SGSN_NODE;
145 return CMD_SUCCESS;
146}
147
Harald Weltee300d002010-06-02 12:41:34 +0200148DEFUN(cfg_sgsn_bind_addr, cfg_sgsn_bind_addr_cmd,
149 "gtp local-ip A.B.C.D",
150 "GTP Parameters\n"
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100151 "Set the IP address for the local GTP bind\n"
152 "IPv4 Address\n")
Harald Weltee300d002010-06-02 12:41:34 +0200153{
154 inet_aton(argv[0], &g_cfg->gtp_listenaddr.sin_addr);
155
156 return CMD_SUCCESS;
157}
158
Harald Welted193cb32010-05-17 22:58:03 +0200159DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd,
160 "ggsn <0-255> remote-ip A.B.C.D",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100161 GGSN_STR "GGSN Number\n" IP_STR "IPv4 Address\n")
Harald Welted193cb32010-05-17 22:58:03 +0200162{
163 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200164 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welte288be162010-05-01 16:48:27 +0200165
Harald Welted193cb32010-05-17 22:58:03 +0200166 inet_aton(argv[1], &ggc->remote_addr);
Harald Welte288be162010-05-01 16:48:27 +0200167
Harald Welted193cb32010-05-17 22:58:03 +0200168 return CMD_SUCCESS;
169}
170
171#if 0
172DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd,
173 "ggsn <0-255> remote-port <0-65535>",
174 "")
175{
176 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200177 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200178 uint16_t port = atoi(argv[1]);
179
180}
181#endif
182
183DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd,
184 "ggsn <0-255> gtp-version (0|1)",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100185 GGSN_STR "GGSN Number\n" "GTP Version\n"
186 "Version 0\n" "Version 1\n")
Harald Welted193cb32010-05-17 22:58:03 +0200187{
188 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200189 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200190
191 if (atoi(argv[1]))
192 ggc->gtp_version = 1;
193 else
194 ggc->gtp_version = 0;
195
196 return CMD_SUCCESS;
197}
198
199#if 0
200DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd,
201 "apn APNAME ggsn <0-255>",
202 "")
203{
204 struct apn_ctx **
205}
206#endif
207
208const struct value_string gprs_mm_st_strs[] = {
209 { GMM_DEREGISTERED, "DEREGISTERED" },
210 { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" },
211 { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" },
Harald Weltebffeff82010-06-09 15:50:45 +0200212 { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" },
Harald Welted193cb32010-05-17 22:58:03 +0200213 { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" },
214 { 0, NULL }
215};
216
217static void vty_dump_pdp(struct vty *vty, const char *pfx,
218 struct sgsn_pdp_ctx *pdp)
219{
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200220 const char *imsi = pdp->mm ? pdp->mm->imsi : "(detaching)";
Harald Welted193cb32010-05-17 22:58:03 +0200221 vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s",
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200222 pfx, imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200223 vty_out(vty, "%s APN: %s%s", pfx,
224 gprs_apn2str(pdp->lib->apn_use.v, pdp->lib->apn_use.l),
225 VTY_NEWLINE);
226 vty_out(vty, "%s PDP Address: %s%s", pfx,
227 gprs_pdpaddr2str(pdp->lib->eua.v, pdp->lib->eua.l),
228 VTY_NEWLINE);
Harald Welteefbdee92010-06-10 00:20:12 +0200229 vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200230}
231
232static void vty_dump_mmctx(struct vty *vty, const char *pfx,
233 struct sgsn_mm_ctx *mm, int pdp)
234{
235 vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s",
236 pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE);
237 vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn,
238 mm->tlli, VTY_NEWLINE);
239 vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, "
240 "Cell ID: %u%s", pfx,
241 get_value_string(gprs_mm_st_strs, mm->mm_state),
242 mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac,
243 mm->cell_id, VTY_NEWLINE);
244
Harald Welte8acd88f2010-05-18 10:57:45 +0200245 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
246
Harald Welted193cb32010-05-17 22:58:03 +0200247 if (pdp) {
248 struct sgsn_pdp_ctx *pdp;
249
250 llist_for_each_entry(pdp, &mm->pdp_list, list)
251 vty_dump_pdp(vty, " ", pdp);
252 }
253}
254
255DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn",
256 SHOW_STR "Display information about the SGSN")
257{
258 /* FIXME: statistics */
259 return CMD_SUCCESS;
260}
261
262#define MMCTX_STR "MM Context\n"
263#define INCLUDE_PDP_STR "Include PDP Context Information\n"
264
265#if 0
266DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd,
267 "show mm-context tlli HEX [pdp]",
268 SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR)
269{
270 uint32_t tlli;
271 struct sgsn_mm_ctx *mm;
272
273 tlli = strtoul(argv[0], NULL, 16);
274 mm = sgsn_mm_ctx_by_tlli(tlli);
275 if (!mm) {
276 vty_out(vty, "No MM context for TLLI %08x%s",
277 tlli, VTY_NEWLINE);
278 return CMD_WARNING;
279 }
280 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
281 return CMD_SUCCESS;
282}
283#endif
284
285DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd,
286 "show mm-context imsi IMSI [pdp]",
287 SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n"
288 INCLUDE_PDP_STR)
289{
290 struct sgsn_mm_ctx *mm;
291
292 mm = sgsn_mm_ctx_by_imsi(argv[0]);
293 if (!mm) {
294 vty_out(vty, "No MM context for IMSI %s%s",
295 argv[0], VTY_NEWLINE);
296 return CMD_WARNING;
297 }
298 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
299 return CMD_SUCCESS;
300}
301
302DEFUN(swow_mmctx_all, show_mmctx_all_cmd,
303 "show mm-context all [pdp]",
304 SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR)
305{
306 struct sgsn_mm_ctx *mm;
307
308 llist_for_each_entry(mm, &sgsn_mm_ctxts, list)
309 vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0);
310
311 return CMD_SUCCESS;
312}
313
Harald Welted193cb32010-05-17 22:58:03 +0200314DEFUN(show_pdpctx_all, show_pdpctx_all_cmd,
315 "show pdp-context all",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100316 SHOW_STR "Display information on PDP Context\n" "Show everything\n")
Harald Welted193cb32010-05-17 22:58:03 +0200317{
318 struct sgsn_pdp_ctx *pdp;
319
320 llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list)
321 vty_dump_pdp(vty, "", pdp);
322
323 return CMD_SUCCESS;
324}
Harald Welte288be162010-05-01 16:48:27 +0200325
Harald Welte7f6da482013-03-19 11:00:13 +0100326
327DEFUN(imsi_acl, cfg_imsi_acl_cmd,
328 "imsi-acl (add|del) IMSI",
329 "Access Control List of foreign IMSIs\n"
330 "Add IMSI to ACL\n"
331 "Remove IMSI from ACL\n"
332 "IMSI of subscriber\n")
333{
334 const char *op = argv[0];
335 const char *imsi = argv[1];
336 int rc;
337
338 if (!strcmp(op, "add"))
Jacob Erlbeck3b5d4072014-10-24 15:11:03 +0200339 rc = sgsn_acl_add(imsi, g_cfg);
Harald Welte7f6da482013-03-19 11:00:13 +0100340 else
Jacob Erlbeck3b5d4072014-10-24 15:11:03 +0200341 rc = sgsn_acl_del(imsi, g_cfg);
Harald Welte7f6da482013-03-19 11:00:13 +0100342
343 if (rc < 0) {
344 vty_out(vty, "%% unable to %s ACL\n", op);
345 return CMD_WARNING;
346 }
347
348 return CMD_SUCCESS;
349}
350
Harald Welte3dfb5492013-03-19 11:48:54 +0100351DEFUN(cfg_auth_policy, cfg_auth_policy_cmd,
352 "auth-policy (accept-all|closed)",
353 "Autorization Policy of SGSN\n"
354 "Accept all IMSIs (DANGEROUS\n"
355 "Accept only home network subscribers or those in ACL\n")
356{
357 if (!strcmp(argv[0], "accept-all"))
358 g_cfg->acl_enabled = 0;
359 else
360 g_cfg->acl_enabled = 1;
361
362 return CMD_SUCCESS;
363}
364
Harald Welte288be162010-05-01 16:48:27 +0200365int sgsn_vty_init(void)
366{
Harald Welted193cb32010-05-17 22:58:03 +0200367 install_element_ve(&show_sgsn_cmd);
368 //install_element_ve(&show_mmctx_tlli_cmd);
369 install_element_ve(&show_mmctx_imsi_cmd);
370 install_element_ve(&show_mmctx_all_cmd);
371 install_element_ve(&show_pdpctx_all_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200372
373 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
374 install_node(&sgsn_node, config_write_sgsn);
Jacob Erlbeck36722e12013-10-29 09:30:30 +0100375 vty_install_default(SGSN_NODE);
Harald Weltee300d002010-06-02 12:41:34 +0200376 install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd);
Harald Welted193cb32010-05-17 22:58:03 +0200377 install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd);
378 //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd);
379 install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd);
Harald Welte7f6da482013-03-19 11:00:13 +0100380 install_element(SGSN_NODE, &cfg_imsi_acl_cmd);
Harald Welte3dfb5492013-03-19 11:48:54 +0100381 install_element(SGSN_NODE, &cfg_auth_policy_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200382
383 return 0;
384}
385
386int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
387{
388 int rc;
389
390 g_cfg = cfg;
Harald Welte7f6da482013-03-19 11:00:13 +0100391
Harald Weltedcccb182010-05-16 20:52:23 +0200392 rc = vty_read_config_file(config_file, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200393 if (rc < 0) {
394 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
395 return rc;
396 }
397
398 return 0;
399}