blob: ac08bed654483ee0d0d2fa6376ee2b30c5e57311 [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
Holger Hans Peter Freytherc15c61c2015-05-06 17:46:08 +02004 * (C) 2015 by Holger Hans Peter Freyther
Harald Welte288be162010-05-01 16:48:27 +02005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte288be162010-05-01 16:48:27 +020010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte288be162010-05-01 16:48:27 +020016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte288be162010-05-01 16:48:27 +020019 *
20 */
21
Harald Welte288be162010-05-01 16:48:27 +020022#include <sys/socket.h>
23#include <netinet/in.h>
24#include <arpa/inet.h>
Jacob Erlbeck207f4a52014-11-11 14:01:48 +010025#include <time.h>
Harald Welte288be162010-05-01 16:48:27 +020026
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010027#include <osmocom/core/talloc.h>
28#include <osmocom/core/utils.h>
29#include <osmocom/core/rate_ctr.h>
Harald Welte53373bc2016-04-20 17:11:43 +020030#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Harald Welte288be162010-05-01 16:48:27 +020031
32#include <openbsc/debug.h>
33#include <openbsc/sgsn.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080034#include <osmocom/gprs/gprs_ns.h>
Harald Welted193cb32010-05-17 22:58:03 +020035#include <openbsc/gprs_sgsn.h>
Harald Welte62ab20c2010-05-14 18:59:17 +020036#include <openbsc/vty.h>
Jacob Erlbeck80547992014-12-19 19:19:46 +010037#include <openbsc/gprs_gsup_client.h>
Harald Welte288be162010-05-01 16:48:27 +020038
Harald Welte4b037e42010-05-19 19:45:32 +020039#include <osmocom/vty/command.h>
40#include <osmocom/vty/vty.h>
Pablo Neira Ayuso6110a3f2011-03-28 19:35:00 +020041#include <osmocom/vty/misc.h>
Max93408ae2016-06-28 14:10:16 +020042#include <osmocom/crypt/gprs_cipher.h>
Jacob Erlbeck80547992014-12-19 19:19:46 +010043#include <osmocom/abis/ipa.h>
44
Harald Welted193cb32010-05-17 22:58:03 +020045#include <pdp.h>
46
Harald Welte288be162010-05-01 16:48:27 +020047static struct sgsn_config *g_cfg = NULL;
48
Jacob Erlbeck106f5472014-11-04 10:08:37 +010049const struct value_string sgsn_auth_pol_strs[] = {
50 { SGSN_AUTH_POLICY_OPEN, "accept-all" },
51 { SGSN_AUTH_POLICY_CLOSED, "closed" },
52 { SGSN_AUTH_POLICY_ACL_ONLY, "acl-only" },
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +010053 { SGSN_AUTH_POLICY_REMOTE, "remote" },
Jacob Erlbeck106f5472014-11-04 10:08:37 +010054 { 0, NULL }
55};
56
Harald Welte94508822015-08-15 19:08:21 +020057/* Section 11.2.2 / Table 11.3a GPRS Mobility management timers – MS side */
58#define GSM0408_T3312_SECS (10*60) /* periodic RAU interval, default 54min */
59
60/* Section 11.2.2 / Table 11.4 MM timers netwokr side */
61#define GSM0408_T3322_SECS 6 /* DETACH_REQ -> DETACH_ACC */
62#define GSM0408_T3350_SECS 6 /* waiting for ATT/RAU/TMSI COMPL */
63#define GSM0408_T3360_SECS 6 /* waiting for AUTH/CIPH RESP */
64#define GSM0408_T3370_SECS 6 /* waiting for ID RESP */
65
66/* Section 11.2.2 / Table 11.4a MM timers netwokr side */
67#define GSM0408_T3313_SECS 30 /* waiting for paging response */
68#define GSM0408_T3314_SECS 44 /* force to STBY on expiry, Ready timer */
69#define GSM0408_T3316_SECS 44
70
71/* Section 11.3 / Table 11.2d Timers of Session Management - network side */
72#define GSM0408_T3385_SECS 8 /* wait for ACT PDP CTX REQ */
73#define GSM0408_T3386_SECS 8 /* wait for MODIFY PDP CTX ACK */
74#define GSM0408_T3395_SECS 8 /* wait for DEACT PDP CTX ACK */
75#define GSM0408_T3397_SECS 8 /* wait for DEACT AA PDP CTX ACK */
76
77#define DECLARE_TIMER(number, doc) \
78 DEFUN(cfg_sgsn_T##number, \
79 cfg_sgsn_T##number##_cmd, \
80 "timer t" #number " <0-65535>", \
81 "Configure GPRS Timers\n" \
Holger Hans Peter Freytherfe60cfb2015-11-02 12:55:07 +010082 doc "\nTimer Value in seconds\n") \
Harald Welte94508822015-08-15 19:08:21 +020083{ \
84 int value = atoi(argv[0]); \
85 \
86 if (value < 0 || value > 65535) { \
87 vty_out(vty, "Timer value %s out of range.%s", \
88 argv[0], VTY_NEWLINE); \
89 return CMD_WARNING; \
90 } \
91 \
92 g_cfg->timers.T##number = value; \
93 return CMD_SUCCESS; \
94}
95
96DECLARE_TIMER(3312, "Periodic RA Update timer (s)")
Neels Hofmeyr65482c92015-10-19 14:37:12 +020097DECLARE_TIMER(3322, "Detach request -> accept timer (s)")
Harald Welte94508822015-08-15 19:08:21 +020098DECLARE_TIMER(3350, "Waiting for ATT/RAU/TMSI_COMPL timer (s)")
99DECLARE_TIMER(3360, "Waiting for AUTH/CIPH response timer (s)")
100DECLARE_TIMER(3370, "Waiting for IDENTITY response timer (s)")
101
102DECLARE_TIMER(3313, "Waiting for paging response timer (s)")
103DECLARE_TIMER(3314, "Force to STANDBY on expiry timer (s)")
Holger Hans Peter Freytherfe60cfb2015-11-02 12:55:07 +0100104DECLARE_TIMER(3316, "AA-Ready timer (s)")
Harald Welte94508822015-08-15 19:08:21 +0200105
106DECLARE_TIMER(3385, "Wait for ACT PDP CTX REQ timer (s)")
107DECLARE_TIMER(3386, "Wait for MODIFY PDP CTX ACK timer (s)")
108DECLARE_TIMER(3395, "Wait for DEACT PDP CTX ACK timer (s)")
109DECLARE_TIMER(3397, "Wait for DEACT AA PDP CTX ACK timer (s)")
110
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100111
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200112#define GSM48_MAX_APN_LEN 102 /* 10.5.6.1 */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100113/* TODO: consolidate with gprs_apn_to_str(). */
114/** Copy apn to a static buffer, replacing the length octets in apn_enc with '.'
115 * and terminating with a '\0'. Return the static buffer.
116 * len: the length of the encoded APN (which has no terminating zero).
117 */
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200118static char *gprs_apn2str(uint8_t *apn, unsigned int len)
119{
120 static char apnbuf[GSM48_MAX_APN_LEN+1];
Holger Hans Peter Freyther80e03652013-07-04 18:44:16 +0200121 unsigned int i = 0;
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200122
123 if (!apn)
124 return "";
125
126 if (len > sizeof(apnbuf)-1)
127 len = sizeof(apnbuf)-1;
128
129 memcpy(apnbuf, apn, len);
130 apnbuf[len] = '\0';
131
132 /* replace the domain name step sizes with dots */
133 while (i < len) {
134 unsigned int step = apnbuf[i];
135 apnbuf[i] = '.';
136 i += step+1;
137 }
138
139 return apnbuf+1;
140}
141
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +0100142char *gprs_pdpaddr2str(uint8_t *pdpa, uint8_t len)
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200143{
144 static char str[INET6_ADDRSTRLEN + 10];
145
146 if (!pdpa || len < 2)
147 return "none";
148
149 switch (pdpa[0] & 0x0f) {
150 case PDP_TYPE_ORG_IETF:
151 switch (pdpa[1]) {
152 case PDP_TYPE_N_IETF_IPv4:
153 if (len < 2 + 4)
154 break;
155 strcpy(str, "IPv4 ");
156 inet_ntop(AF_INET, pdpa+2, str+5, sizeof(str)-5);
157 return str;
158 case PDP_TYPE_N_IETF_IPv6:
159 if (len < 2 + 8)
160 break;
161 strcpy(str, "IPv6 ");
162 inet_ntop(AF_INET6, pdpa+2, str+5, sizeof(str)-5);
163 return str;
164 default:
165 break;
166 }
167 break;
168 case PDP_TYPE_ORG_ETSI:
169 if (pdpa[1] == PDP_TYPE_N_ETSI_PPP)
170 return "PPP";
171 break;
172 default:
173 break;
174 }
175
176 return "invalid";
177}
178
Harald Welte288be162010-05-01 16:48:27 +0200179static struct cmd_node sgsn_node = {
180 SGSN_NODE,
Harald Welte570ce242012-08-17 13:16:10 +0200181 "%s(config-sgsn)# ",
Harald Welte288be162010-05-01 16:48:27 +0200182 1,
183};
184
185static int config_write_sgsn(struct vty *vty)
186{
Harald Welte77289c22010-05-18 14:32:29 +0200187 struct sgsn_ggsn_ctx *gctx;
Harald Welte7f6da482013-03-19 11:00:13 +0100188 struct imsi_acl_entry *acl;
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100189 struct apn_ctx *actx;
Holger Hans Peter Freythera5a6da42015-05-25 15:20:27 +0800190 struct ares_addr_node *server;
Harald Welte288be162010-05-01 16:48:27 +0200191
192 vty_out(vty, "sgsn%s", VTY_NEWLINE);
193
Harald Weltee300d002010-06-02 12:41:34 +0200194 vty_out(vty, " gtp local-ip %s%s",
195 inet_ntoa(g_cfg->gtp_listenaddr.sin_addr), VTY_NEWLINE);
196
Harald Welted193cb32010-05-17 22:58:03 +0200197 llist_for_each_entry(gctx, &sgsn_ggsn_ctxts, list) {
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800198 if (gctx->id == UINT32_MAX)
199 continue;
200
Harald Welteff3bde82010-05-19 15:09:09 +0200201 vty_out(vty, " ggsn %u remote-ip %s%s", gctx->id,
Harald Welted193cb32010-05-17 22:58:03 +0200202 inet_ntoa(gctx->remote_addr), VTY_NEWLINE);
Harald Welteff3bde82010-05-19 15:09:09 +0200203 vty_out(vty, " ggsn %u gtp-version %u%s", gctx->id,
Harald Welted193cb32010-05-17 22:58:03 +0200204 gctx->gtp_version, VTY_NEWLINE);
Harald Welte288be162010-05-01 16:48:27 +0200205 }
206
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800207 if (sgsn->cfg.dynamic_lookup)
208 vty_out(vty, " ggsn dynamic%s", VTY_NEWLINE);
209
Holger Hans Peter Freythera5a6da42015-05-25 15:20:27 +0800210 for (server = sgsn->ares_servers; server; server = server->next)
211 vty_out(vty, " grx-dns-add %s%s", inet_ntoa(server->addr.addr4), VTY_NEWLINE);
212
Harald Welte3dfb5492013-03-19 11:48:54 +0100213 vty_out(vty, " auth-policy %s%s",
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100214 get_value_string(sgsn_auth_pol_strs, g_cfg->auth_policy),
215 VTY_NEWLINE);
Max93408ae2016-06-28 14:10:16 +0200216 if (g_cfg->cipher != GPRS_ALGO_GEA0)
217 vty_out(vty, " encryption %s%s",
218 get_value_string(gprs_cipher_names, g_cfg->cipher),
219 VTY_NEWLINE);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100220 if (g_cfg->gsup_server_addr.sin_addr.s_addr)
221 vty_out(vty, " gsup remote-ip %s%s",
222 inet_ntoa(g_cfg->gsup_server_addr.sin_addr), VTY_NEWLINE);
223 if (g_cfg->gsup_server_port)
224 vty_out(vty, " gsup remote-port %d%s",
225 g_cfg->gsup_server_port, VTY_NEWLINE);
Neels Hofmeyr568a7272015-10-12 11:57:38 +0200226
227 vty_out(vty, " gsup oap-id %d%s",
228 (int)g_cfg->oap.client_id, VTY_NEWLINE);
229 if (g_cfg->oap.secret_k_present != 0)
230 vty_out(vty, " gsup oap-k %s%s",
231 osmo_hexdump_nospc(g_cfg->oap.secret_k, sizeof(g_cfg->oap.secret_k)),
232 VTY_NEWLINE);
233 if (g_cfg->oap.secret_opc_present != 0)
234 vty_out(vty, " gsup oap-opc %s%s",
235 osmo_hexdump_nospc(g_cfg->oap.secret_opc, sizeof(g_cfg->oap.secret_opc)),
236 VTY_NEWLINE);
237
Harald Welte7f6da482013-03-19 11:00:13 +0100238 llist_for_each_entry(acl, &g_cfg->imsi_acl, list)
239 vty_out(vty, " imsi-acl add %s%s", acl->imsi, VTY_NEWLINE);
240
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100241 if (llist_empty(&sgsn_apn_ctxts))
242 vty_out(vty, " ! apn * ggsn 0%s", VTY_NEWLINE);
243 llist_for_each_entry(actx, &sgsn_apn_ctxts, list) {
244 if (strlen(actx->imsi_prefix) > 0)
Holger Hans Peter Freytherb7ae0b32015-05-29 15:11:55 +0200245 vty_out(vty, " apn %s imsi-prefix %s ggsn %u%s",
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100246 actx->name, actx->imsi_prefix, actx->ggsn->id,
247 VTY_NEWLINE);
248 else
Holger Hans Peter Freytherb7ae0b32015-05-29 15:11:55 +0200249 vty_out(vty, " apn %s ggsn %u%s", actx->name,
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100250 actx->ggsn->id, VTY_NEWLINE);
251 }
252
Holger Hans Peter Freytherc15c61c2015-05-06 17:46:08 +0200253 if (g_cfg->cdr.filename)
254 vty_out(vty, " cdr filename %s%s", g_cfg->cdr.filename, VTY_NEWLINE);
255 else
256 vty_out(vty, " no cdr filename%s", VTY_NEWLINE);
257 vty_out(vty, " cdr interval %d%s", g_cfg->cdr.interval, VTY_NEWLINE);
258
Harald Welte94508822015-08-15 19:08:21 +0200259 vty_out(vty, " timer t3312 %d%s", g_cfg->timers.T3312, VTY_NEWLINE);
260 vty_out(vty, " timer t3322 %d%s", g_cfg->timers.T3322, VTY_NEWLINE);
261 vty_out(vty, " timer t3350 %d%s", g_cfg->timers.T3350, VTY_NEWLINE);
262 vty_out(vty, " timer t3360 %d%s", g_cfg->timers.T3360, VTY_NEWLINE);
263 vty_out(vty, " timer t3370 %d%s", g_cfg->timers.T3370, VTY_NEWLINE);
264 vty_out(vty, " timer t3313 %d%s", g_cfg->timers.T3313, VTY_NEWLINE);
265 vty_out(vty, " timer t3314 %d%s", g_cfg->timers.T3314, VTY_NEWLINE);
266 vty_out(vty, " timer t3316 %d%s", g_cfg->timers.T3316, VTY_NEWLINE);
267 vty_out(vty, " timer t3385 %d%s", g_cfg->timers.T3385, VTY_NEWLINE);
268 vty_out(vty, " timer t3386 %d%s", g_cfg->timers.T3386, VTY_NEWLINE);
269 vty_out(vty, " timer t3395 %d%s", g_cfg->timers.T3395, VTY_NEWLINE);
270 vty_out(vty, " timer t3397 %d%s", g_cfg->timers.T3397, VTY_NEWLINE);
271
Harald Welte288be162010-05-01 16:48:27 +0200272 return CMD_SUCCESS;
273}
274
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100275#define SGSN_STR "Configure the SGSN\n"
276#define GGSN_STR "Configure the GGSN information\n"
Harald Weltee300d002010-06-02 12:41:34 +0200277
278DEFUN(cfg_sgsn, cfg_sgsn_cmd,
279 "sgsn",
280 SGSN_STR)
Harald Welte288be162010-05-01 16:48:27 +0200281{
282 vty->node = SGSN_NODE;
283 return CMD_SUCCESS;
284}
285
Harald Weltee300d002010-06-02 12:41:34 +0200286DEFUN(cfg_sgsn_bind_addr, cfg_sgsn_bind_addr_cmd,
287 "gtp local-ip A.B.C.D",
288 "GTP Parameters\n"
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100289 "Set the IP address for the local GTP bind\n"
290 "IPv4 Address\n")
Harald Weltee300d002010-06-02 12:41:34 +0200291{
292 inet_aton(argv[0], &g_cfg->gtp_listenaddr.sin_addr);
293
294 return CMD_SUCCESS;
295}
296
Harald Welted193cb32010-05-17 22:58:03 +0200297DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd,
298 "ggsn <0-255> remote-ip A.B.C.D",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100299 GGSN_STR "GGSN Number\n" IP_STR "IPv4 Address\n")
Harald Welted193cb32010-05-17 22:58:03 +0200300{
301 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200302 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welte288be162010-05-01 16:48:27 +0200303
Harald Welted193cb32010-05-17 22:58:03 +0200304 inet_aton(argv[1], &ggc->remote_addr);
Harald Welte288be162010-05-01 16:48:27 +0200305
Harald Welted193cb32010-05-17 22:58:03 +0200306 return CMD_SUCCESS;
307}
308
309#if 0
310DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd,
311 "ggsn <0-255> remote-port <0-65535>",
312 "")
313{
314 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200315 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200316 uint16_t port = atoi(argv[1]);
317
318}
319#endif
320
321DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd,
322 "ggsn <0-255> gtp-version (0|1)",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100323 GGSN_STR "GGSN Number\n" "GTP Version\n"
324 "Version 0\n" "Version 1\n")
Harald Welted193cb32010-05-17 22:58:03 +0200325{
326 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200327 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200328
329 if (atoi(argv[1]))
330 ggc->gtp_version = 1;
331 else
332 ggc->gtp_version = 0;
333
334 return CMD_SUCCESS;
335}
336
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800337DEFUN(cfg_ggsn_dynamic_lookup, cfg_ggsn_dynamic_lookup_cmd,
338 "ggsn dynamic",
339 GGSN_STR "Enable dynamic GRX based look-up (requires restart)\n")
340{
341 sgsn->cfg.dynamic_lookup = 1;
342 return CMD_SUCCESS;
343}
344
Holger Hans Peter Freythera5a6da42015-05-25 15:20:27 +0800345DEFUN(cfg_grx_ggsn, cfg_grx_ggsn_cmd,
346 "grx-dns-add A.B.C.D",
347 "Add DNS server\nIPv4 address\n")
348{
349 struct ares_addr_node *node = talloc_zero(tall_bsc_ctx, struct ares_addr_node);
350 node->family = AF_INET;
351 inet_aton(argv[0], &node->addr.addr4);
352
353 node->next = sgsn->ares_servers;
354 sgsn->ares_servers = node;
355 return CMD_SUCCESS;
356}
357
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100358#define APN_STR "Configure the information per APN\n"
359#define APN_GW_STR "The APN gateway name optionally prefixed by '*' (wildcard)\n"
360
361static int add_apn_ggsn_mapping(struct vty *vty, const char *apn_str,
362 const char *imsi_prefix, int ggsn_id)
363{
364 struct apn_ctx *actx;
365 struct sgsn_ggsn_ctx *ggsn;
366
367 ggsn = sgsn_ggsn_ctx_by_id(ggsn_id);
368 if (ggsn == NULL) {
369 vty_out(vty, "%% a GGSN with id %d has not been defined%s",
370 ggsn_id, VTY_NEWLINE);
371 return CMD_WARNING;
372 }
373
374 actx = sgsn_apn_ctx_find_alloc(apn_str, imsi_prefix);
375 if (!actx) {
376 vty_out(vty, "%% unable to create APN context for %s/%s%s",
377 apn_str, imsi_prefix, VTY_NEWLINE);
378 return CMD_WARNING;
379 }
380
381 actx->ggsn = ggsn;
382
383 return CMD_SUCCESS;
384}
385
Harald Welted193cb32010-05-17 22:58:03 +0200386DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd,
387 "apn APNAME ggsn <0-255>",
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100388 APN_STR APN_GW_STR
389 "Select the GGSN to use when the APN gateway prefix matches\n"
390 "The GGSN id")
Harald Welted193cb32010-05-17 22:58:03 +0200391{
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100392
393 return add_apn_ggsn_mapping(vty, argv[0], "", atoi(argv[1]));
Harald Welted193cb32010-05-17 22:58:03 +0200394}
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +0100395
396DEFUN(cfg_apn_imsi_ggsn, cfg_apn_imsi_ggsn_cmd,
397 "apn APNAME imsi-prefix IMSIPRE ggsn <0-255>",
398 APN_STR APN_GW_STR
399 "Restrict rule to a certain IMSI prefix\n"
400 "An IMSI prefix\n"
401 "Select the GGSN to use when APN gateway and IMSI prefix match\n"
402 "The GGSN id")
403{
404
405 return add_apn_ggsn_mapping(vty, argv[0], argv[1], atoi(argv[2]));
406}
Harald Welted193cb32010-05-17 22:58:03 +0200407
408const struct value_string gprs_mm_st_strs[] = {
409 { GMM_DEREGISTERED, "DEREGISTERED" },
410 { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" },
411 { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" },
Harald Weltebffeff82010-06-09 15:50:45 +0200412 { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" },
Harald Welted193cb32010-05-17 22:58:03 +0200413 { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" },
414 { 0, NULL }
415};
416
417static void vty_dump_pdp(struct vty *vty, const char *pfx,
418 struct sgsn_pdp_ctx *pdp)
419{
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200420 const char *imsi = pdp->mm ? pdp->mm->imsi : "(detaching)";
Harald Welted193cb32010-05-17 22:58:03 +0200421 vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s",
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200422 pfx, imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
Harald Weltec5d4a0c2010-07-02 22:47:59 +0200423 vty_out(vty, "%s APN: %s%s", pfx,
424 gprs_apn2str(pdp->lib->apn_use.v, pdp->lib->apn_use.l),
425 VTY_NEWLINE);
426 vty_out(vty, "%s PDP Address: %s%s", pfx,
427 gprs_pdpaddr2str(pdp->lib->eua.v, pdp->lib->eua.l),
428 VTY_NEWLINE);
Harald Welteefbdee92010-06-10 00:20:12 +0200429 vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200430}
431
432static void vty_dump_mmctx(struct vty *vty, const char *pfx,
433 struct sgsn_mm_ctx *mm, int pdp)
434{
435 vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s",
436 pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE);
Holger Hans Peter Freyther8ee13e22015-05-18 10:00:03 +0200437 vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s HLR: %s",
Harald Weltef97ee042015-12-25 19:12:21 +0100438 pfx, mm->msisdn, mm->gb.tlli, mm->hlr, VTY_NEWLINE);
Harald Welted193cb32010-05-17 22:58:03 +0200439 vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, "
440 "Cell ID: %u%s", pfx,
441 get_value_string(gprs_mm_st_strs, mm->mm_state),
442 mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac,
Harald Weltef97ee042015-12-25 19:12:21 +0100443 mm->gb.cell_id, VTY_NEWLINE);
Harald Welted193cb32010-05-17 22:58:03 +0200444
Harald Welte8acd88f2010-05-18 10:57:45 +0200445 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
446
Harald Welted193cb32010-05-17 22:58:03 +0200447 if (pdp) {
448 struct sgsn_pdp_ctx *pdp;
449
450 llist_for_each_entry(pdp, &mm->pdp_list, list)
451 vty_dump_pdp(vty, " ", pdp);
452 }
453}
454
455DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn",
456 SHOW_STR "Display information about the SGSN")
457{
Jacob Erlbeck80547992014-12-19 19:19:46 +0100458 if (sgsn->gsup_client) {
459 struct ipa_client_conn *link = sgsn->gsup_client->link;
460 vty_out(vty,
461 " Remote authorization: %sconnected to %s:%d via GSUP%s",
462 sgsn->gsup_client->is_connected ? "" : "not ",
463 link->addr, link->port,
464 VTY_NEWLINE);
465 }
Harald Welted193cb32010-05-17 22:58:03 +0200466 /* FIXME: statistics */
467 return CMD_SUCCESS;
468}
469
470#define MMCTX_STR "MM Context\n"
471#define INCLUDE_PDP_STR "Include PDP Context Information\n"
472
473#if 0
474DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd,
475 "show mm-context tlli HEX [pdp]",
476 SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR)
477{
478 uint32_t tlli;
479 struct sgsn_mm_ctx *mm;
480
481 tlli = strtoul(argv[0], NULL, 16);
482 mm = sgsn_mm_ctx_by_tlli(tlli);
483 if (!mm) {
484 vty_out(vty, "No MM context for TLLI %08x%s",
485 tlli, VTY_NEWLINE);
486 return CMD_WARNING;
487 }
488 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
489 return CMD_SUCCESS;
490}
491#endif
492
493DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd,
494 "show mm-context imsi IMSI [pdp]",
495 SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n"
496 INCLUDE_PDP_STR)
497{
498 struct sgsn_mm_ctx *mm;
499
500 mm = sgsn_mm_ctx_by_imsi(argv[0]);
501 if (!mm) {
502 vty_out(vty, "No MM context for IMSI %s%s",
503 argv[0], VTY_NEWLINE);
504 return CMD_WARNING;
505 }
506 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
507 return CMD_SUCCESS;
508}
509
510DEFUN(swow_mmctx_all, show_mmctx_all_cmd,
511 "show mm-context all [pdp]",
512 SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR)
513{
514 struct sgsn_mm_ctx *mm;
515
516 llist_for_each_entry(mm, &sgsn_mm_ctxts, list)
517 vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0);
518
519 return CMD_SUCCESS;
520}
521
Harald Welted193cb32010-05-17 22:58:03 +0200522DEFUN(show_pdpctx_all, show_pdpctx_all_cmd,
523 "show pdp-context all",
Holger Hans Peter Freyther1491f2e2011-11-05 15:21:16 +0100524 SHOW_STR "Display information on PDP Context\n" "Show everything\n")
Harald Welted193cb32010-05-17 22:58:03 +0200525{
526 struct sgsn_pdp_ctx *pdp;
527
528 llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list)
529 vty_dump_pdp(vty, "", pdp);
530
531 return CMD_SUCCESS;
532}
Harald Welte288be162010-05-01 16:48:27 +0200533
Harald Welte7f6da482013-03-19 11:00:13 +0100534
535DEFUN(imsi_acl, cfg_imsi_acl_cmd,
536 "imsi-acl (add|del) IMSI",
537 "Access Control List of foreign IMSIs\n"
538 "Add IMSI to ACL\n"
539 "Remove IMSI from ACL\n"
540 "IMSI of subscriber\n")
541{
542 const char *op = argv[0];
543 const char *imsi = argv[1];
544 int rc;
545
546 if (!strcmp(op, "add"))
Jacob Erlbeck3b5d4072014-10-24 15:11:03 +0200547 rc = sgsn_acl_add(imsi, g_cfg);
Harald Welte7f6da482013-03-19 11:00:13 +0100548 else
Jacob Erlbeck3b5d4072014-10-24 15:11:03 +0200549 rc = sgsn_acl_del(imsi, g_cfg);
Harald Welte7f6da482013-03-19 11:00:13 +0100550
551 if (rc < 0) {
Jacob Erlbeck15cc8c82015-01-19 14:29:43 +0100552 vty_out(vty, "%% unable to %s ACL%s", op, VTY_NEWLINE);
553
Harald Welte7f6da482013-03-19 11:00:13 +0100554 return CMD_WARNING;
555 }
556
557 return CMD_SUCCESS;
558}
559
Max93408ae2016-06-28 14:10:16 +0200560DEFUN(cfg_encrypt, cfg_encrypt_cmd,
561 "encryption (GEA0|GEA1|GEA2|GEA3|GEA4)",
562 "Set encryption algorithm for SGSN\n"
563 "Use GEA0 (no encryption)\n"
564 "Use GEA1\nUse GEA2\nUse GEA3\nUse GEA4\n")
565{
566 if (!g_cfg->require_authentication) {
567 vty_out(vty, "%% unable to use encryption without "
568 "authentication: adjust auth-policy%s", VTY_NEWLINE);
569 return CMD_WARNING;
570 }
571
572 enum gprs_ciph_algo c = get_string_value(gprs_cipher_names, argv[0]);
573 if (!gprs_cipher_supported(c)) {
574 vty_out(vty, "%% cipher %s is unsupported in current version%s",
575 argv[0], VTY_NEWLINE);
576 return CMD_WARNING;
577 }
578
579 g_cfg->cipher = c;
580
581 return CMD_SUCCESS;
582}
583
Harald Welte3dfb5492013-03-19 11:48:54 +0100584DEFUN(cfg_auth_policy, cfg_auth_policy_cmd,
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100585 "auth-policy (accept-all|closed|acl-only|remote)",
Harald Welte3dfb5492013-03-19 11:48:54 +0100586 "Autorization Policy of SGSN\n"
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100587 "Accept all IMSIs (DANGEROUS)\n"
588 "Accept only home network subscribers or those in the ACL\n"
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100589 "Accept only subscribers in the ACL\n"
590 "Use remote subscription data only (HLR)\n")
Harald Welte3dfb5492013-03-19 11:48:54 +0100591{
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100592 int val = get_string_value(sgsn_auth_pol_strs, argv[0]);
Jacob Erlbeckbe2c8d92014-11-12 10:18:09 +0100593 OSMO_ASSERT(val >= SGSN_AUTH_POLICY_OPEN && val <= SGSN_AUTH_POLICY_REMOTE);
Jacob Erlbeck106f5472014-11-04 10:08:37 +0100594 g_cfg->auth_policy = val;
Jacob Erlbeck9d4f46c2014-12-17 13:20:08 +0100595 g_cfg->require_authentication = (val == SGSN_AUTH_POLICY_REMOTE);
Jacob Erlbeck771573c2014-12-19 18:08:48 +0100596 g_cfg->require_update_location = (val == SGSN_AUTH_POLICY_REMOTE);
Harald Welte3dfb5492013-03-19 11:48:54 +0100597
598 return CMD_SUCCESS;
599}
600
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100601/* Subscriber */
602#include <openbsc/gsm_subscriber.h>
603
604static void subscr_dump_full_vty(struct vty *vty, struct gsm_subscriber *subscr, int pending)
605{
606 char expire_time[200];
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100607 struct gsm_auth_tuple *at;
608 int at_idx;
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100609 struct sgsn_subscriber_pdp_data *pdp;
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100610
611 vty_out(vty, " ID: %llu, Authorized: %d%s", subscr->id,
612 subscr->authorized, VTY_NEWLINE);
613 if (strlen(subscr->name))
614 vty_out(vty, " Name: '%s'%s", subscr->name, VTY_NEWLINE);
615 if (strlen(subscr->extension))
616 vty_out(vty, " Extension: %s%s", subscr->extension,
617 VTY_NEWLINE);
618 vty_out(vty, " LAC: %d/0x%x%s",
619 subscr->lac, subscr->lac, VTY_NEWLINE);
620 vty_out(vty, " IMSI: %s%s", subscr->imsi, VTY_NEWLINE);
621 if (subscr->tmsi != GSM_RESERVED_TMSI)
622 vty_out(vty, " TMSI: %08X%s", subscr->tmsi,
623 VTY_NEWLINE);
Holger Hans Peter Freytherf7b38262015-04-23 16:58:33 -0400624 if (subscr->sgsn_data->msisdn_len > 0)
625 vty_out(vty, " MSISDN (BCD): %s%s",
626 osmo_hexdump(subscr->sgsn_data->msisdn,
627 subscr->sgsn_data->msisdn_len),
628 VTY_NEWLINE);
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100629
630 if (strlen(subscr->equipment.imei) > 0)
631 vty_out(vty, " IMEI: %s%s", subscr->equipment.imei, VTY_NEWLINE);
632
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100633 for (at_idx = 0; at_idx < ARRAY_SIZE(subscr->sgsn_data->auth_triplets);
634 at_idx++) {
635 at = &subscr->sgsn_data->auth_triplets[at_idx];
636 if (at->key_seq == GSM_KEY_SEQ_INVAL)
637 continue;
638
639 vty_out(vty, " A3A8 tuple (used %d times): ",
640 at->use_count);
641 vty_out(vty, " seq # : %d, ",
642 at->key_seq);
643 vty_out(vty, " RAND : %s, ",
Harald Welte121e9a42016-04-20 13:13:19 +0200644 osmo_hexdump(at->vec.rand, sizeof(at->vec.rand)));
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100645 vty_out(vty, " SRES : %s, ",
Harald Welte121e9a42016-04-20 13:13:19 +0200646 osmo_hexdump(at->vec.sres, sizeof(at->vec.sres)));
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100647 vty_out(vty, " Kc : %s%s",
Harald Welte121e9a42016-04-20 13:13:19 +0200648 osmo_hexdump(at->vec.kc, sizeof(at->vec.kc)),
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100649 VTY_NEWLINE);
650 }
651
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100652 llist_for_each_entry(pdp, &subscr->sgsn_data->pdp_list, list) {
Holger Hans Peter Freytherd05e0692015-04-23 16:59:04 -0400653 vty_out(vty, " PDP info: Id: %d, Type: 0x%04x, APN: '%s' QoS: %s%s",
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100654 pdp->context_id, pdp->pdp_type, pdp->apn_str,
Holger Hans Peter Freytherd05e0692015-04-23 16:59:04 -0400655 osmo_hexdump(pdp->qos_subscribed, pdp->qos_subscribed_len),
Jacob Erlbeck0e8add62014-12-17 14:03:35 +0100656 VTY_NEWLINE);
657 }
658
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100659 /* print the expiration time of a subscriber */
660 if (subscr->expire_lu) {
661 strftime(expire_time, sizeof(expire_time),
662 "%a, %d %b %Y %T %z", localtime(&subscr->expire_lu));
663 expire_time[sizeof(expire_time) - 1] = '\0';
664 vty_out(vty, " Expiration Time: %s%s", expire_time, VTY_NEWLINE);
665 }
666
667 if (subscr->flags)
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100668 vty_out(vty, " Flags: %s%s%s%s%s%s",
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100669 subscr->flags & GSM_SUBSCRIBER_FIRST_CONTACT ?
670 "FIRST_CONTACT " : "",
671 subscr->flags & GPRS_SUBSCRIBER_CANCELLED ?
672 "CANCELLED " : "",
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100673 subscr->flags & GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING ?
674 "UPDATE_LOCATION_PENDING " : "",
675 subscr->flags & GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING ?
676 "AUTH_INFO_PENDING " : "",
Jacob Erlbeck65fa3f72015-01-06 16:32:41 +0100677 subscr->flags & GPRS_SUBSCRIBER_ENABLE_PURGE ?
678 "ENABLE_PURGE " : "",
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100679 VTY_NEWLINE);
680
681 vty_out(vty, " Use count: %u%s", subscr->use_count, VTY_NEWLINE);
682}
683
684DEFUN(show_subscr_cache,
685 show_subscr_cache_cmd,
686 "show subscriber cache",
687 SHOW_STR "Show information about subscribers\n"
688 "Display contents of subscriber cache\n")
689{
690 struct gsm_subscriber *subscr;
691
692 llist_for_each_entry(subscr, &active_subscribers, entry) {
693 vty_out(vty, " Subscriber:%s", VTY_NEWLINE);
694 subscr_dump_full_vty(vty, subscr, 0);
695 }
696
697 return CMD_SUCCESS;
698}
699
700#define UPDATE_SUBSCR_STR "update-subscriber imsi IMSI "
701#define UPDATE_SUBSCR_HELP "Update subscriber list\n" \
702 "Use the IMSI to select the subscriber\n" \
703 "The IMSI\n"
704
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100705#define UPDATE_SUBSCR_INSERT_HELP "Insert data into the subscriber record\n"
706
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100707DEFUN(update_subscr_insert_auth_triplet, update_subscr_insert_auth_triplet_cmd,
708 UPDATE_SUBSCR_STR "insert auth-triplet <1-5> sres SRES rand RAND kc KC",
709 UPDATE_SUBSCR_HELP
710 UPDATE_SUBSCR_INSERT_HELP
711 "Update authentication triplet\n"
712 "Triplet index\n"
713 "Set SRES value\nSRES value (4 byte) in hex\n"
714 "Set RAND value\nRAND value (16 byte) in hex\n"
715 "Set Kc value\nKc value (8 byte) in hex\n")
716{
717 const char *imsi = argv[0];
718 const int cksn = atoi(argv[1]) - 1;
719 const char *sres_str = argv[2];
720 const char *rand_str = argv[3];
721 const char *kc_str = argv[4];
722 struct gsm_auth_tuple at = {0,};
723
724 struct gsm_subscriber *subscr;
725
Jacob Erlbeckd9193432015-01-19 14:11:46 +0100726 subscr = gprs_subscr_get_by_imsi(imsi);
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100727 if (!subscr) {
Jacob Erlbeck15cc8c82015-01-19 14:29:43 +0100728 vty_out(vty, "%% unable get subscriber record for %s%s",
729 imsi, VTY_NEWLINE);
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100730 return CMD_WARNING;
731 }
732
733 OSMO_ASSERT(subscr->sgsn_data);
734
Harald Welte121e9a42016-04-20 13:13:19 +0200735 if (osmo_hexparse(sres_str, &at.vec.sres[0], sizeof(at.vec.sres)) < 0) {
Jacob Erlbeck15cc8c82015-01-19 14:29:43 +0100736 vty_out(vty, "%% invalid SRES value '%s'%s",
737 sres_str, VTY_NEWLINE);
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100738 goto failed;
739 }
Harald Welte121e9a42016-04-20 13:13:19 +0200740 if (osmo_hexparse(rand_str, &at.vec.rand[0], sizeof(at.vec.rand)) < 0) {
Jacob Erlbeck15cc8c82015-01-19 14:29:43 +0100741 vty_out(vty, "%% invalid RAND value '%s'%s",
742 rand_str, VTY_NEWLINE);
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100743 goto failed;
744 }
Harald Welte121e9a42016-04-20 13:13:19 +0200745 if (osmo_hexparse(kc_str, &at.vec.kc[0], sizeof(at.vec.kc)) < 0) {
Jacob Erlbeck15cc8c82015-01-19 14:29:43 +0100746 vty_out(vty, "%% invalid Kc value '%s'%s",
747 kc_str, VTY_NEWLINE);
Jacob Erlbeck7921ab12014-12-08 15:52:00 +0100748 goto failed;
749 }
750 at.key_seq = cksn;
751
752 subscr->sgsn_data->auth_triplets[cksn] = at;
753 subscr->sgsn_data->auth_triplets_updated = 1;
754
755 subscr_put(subscr);
756
757 return CMD_SUCCESS;
758
759failed:
760 subscr_put(subscr);
761 return CMD_SUCCESS;
762}
763
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100764DEFUN(update_subscr_cancel, update_subscr_cancel_cmd,
Jacob Erlbeck8000e0e2015-01-27 14:56:40 +0100765 UPDATE_SUBSCR_STR "cancel (update-procedure|subscription-withdraw)",
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100766 UPDATE_SUBSCR_HELP
Jacob Erlbeck8000e0e2015-01-27 14:56:40 +0100767 "Cancel (remove) subscriber record\n"
768 "The MS moved to another SGSN\n"
769 "The subscription is no longer valid\n")
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100770{
771 const char *imsi = argv[0];
Jacob Erlbeck8000e0e2015-01-27 14:56:40 +0100772 const char *cancel_type = argv[1];
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100773
774 struct gsm_subscriber *subscr;
775
776 subscr = gprs_subscr_get_by_imsi(imsi);
777 if (!subscr) {
Jacob Erlbeck15cc8c82015-01-19 14:29:43 +0100778 vty_out(vty, "%% no subscriber record for %s%s",
779 imsi, VTY_NEWLINE);
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100780 return CMD_WARNING;
781 }
782
Jacob Erlbeck8000e0e2015-01-27 14:56:40 +0100783 if (strcmp(cancel_type, "update-procedure") == 0)
784 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
785 else
786 subscr->sgsn_data->error_cause = GMM_CAUSE_IMPL_DETACHED;
787
Jacob Erlbeck37139e52015-01-23 13:52:55 +0100788 gprs_subscr_cancel(subscr);
789 subscr_put(subscr);
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100790
791 return CMD_SUCCESS;
792}
793
Jacob Erlbeckd9193432015-01-19 14:11:46 +0100794DEFUN(update_subscr_create, update_subscr_create_cmd,
795 UPDATE_SUBSCR_STR "create",
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100796 UPDATE_SUBSCR_HELP
Jacob Erlbeckd9193432015-01-19 14:11:46 +0100797 "Create a subscriber entry\n")
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100798{
799 const char *imsi = argv[0];
800
801 struct gsm_subscriber *subscr;
802
803 subscr = gprs_subscr_get_by_imsi(imsi);
Jacob Erlbeck15cc8c82015-01-19 14:29:43 +0100804 if (subscr) {
805 vty_out(vty, "%% subscriber record already exists for %s%s",
806 imsi, VTY_NEWLINE);
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100807 return CMD_WARNING;
808 }
809
Jacob Erlbeckd9193432015-01-19 14:11:46 +0100810 subscr = gprs_subscr_get_or_create(imsi);
811 subscr->keep_in_ram = 1;
Jacob Erlbeck207f4a52014-11-11 14:01:48 +0100812 subscr_put(subscr);
813
814 return CMD_SUCCESS;
815}
816
Jacob Erlbecke988ae42015-01-27 12:41:19 +0100817DEFUN(update_subscr_destroy, update_subscr_destroy_cmd,
818 UPDATE_SUBSCR_STR "destroy",
819 UPDATE_SUBSCR_HELP
820 "Destroy a subscriber entry\n")
821{
822 const char *imsi = argv[0];
823
824 struct gsm_subscriber *subscr;
825
826 subscr = gprs_subscr_get_by_imsi(imsi);
827 if (!subscr) {
828 vty_out(vty, "%% subscriber record does not exist for %s%s",
829 imsi, VTY_NEWLINE);
830 return CMD_WARNING;
831 }
832
833 subscr->keep_in_ram = 0;
Jacob Erlbeck8000e0e2015-01-27 14:56:40 +0100834 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbecke988ae42015-01-27 12:41:19 +0100835 gprs_subscr_cancel(subscr);
836 if (subscr->use_count > 1)
837 vty_out(vty, "%% subscriber is still in use%s",
838 VTY_NEWLINE);
839 subscr_put(subscr);
840
841 return CMD_SUCCESS;
842}
843
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100844#define UL_ERR_STR "system-failure|data-missing|unexpected-data-value|" \
845 "unknown-subscriber|roaming-not-allowed"
846
847#define UL_ERR_HELP \
848 "Force error code SystemFailure\n" \
849 "Force error code DataMissing\n" \
850 "Force error code UnexpectedDataValue\n" \
851 "Force error code UnknownSubscriber\n" \
852 "Force error code RoamingNotAllowed\n"
853
854DEFUN(update_subscr_update_location_result, update_subscr_update_location_result_cmd,
855 UPDATE_SUBSCR_STR "update-location-result (ok|" UL_ERR_STR ")",
856 UPDATE_SUBSCR_HELP
857 "Complete the update location procedure\n"
858 "The update location request succeeded\n"
859 UL_ERR_HELP)
860{
861 const char *imsi = argv[0];
862 const char *ret_code_str = argv[1];
863
864 struct gsm_subscriber *subscr;
865
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100866 const struct value_string cause_mapping[] = {
867 { GMM_CAUSE_NET_FAIL, "system-failure" },
868 { GMM_CAUSE_INV_MAND_INFO, "data-missing" },
869 { GMM_CAUSE_PROTO_ERR_UNSPEC, "unexpected-data-value" },
870 { GMM_CAUSE_IMSI_UNKNOWN, "unknown-subscriber" },
871 { GMM_CAUSE_GPRS_NOTALLOWED, "roaming-not-allowed" },
872 { 0, NULL }
873 };
874
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100875 subscr = gprs_subscr_get_by_imsi(imsi);
876 if (!subscr) {
Jacob Erlbeck15cc8c82015-01-19 14:29:43 +0100877 vty_out(vty, "%% unable to get subscriber record for %s%s",
878 imsi, VTY_NEWLINE);
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100879 return CMD_WARNING;
880 }
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100881
882 if (strcmp(ret_code_str, "ok") == 0) {
883 subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE;
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100884 subscr->authorized = 1;
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100885 } else {
886 subscr->sgsn_data->error_cause =
887 get_string_value(cause_mapping, ret_code_str);
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100888 subscr->authorized = 0;
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100889 }
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100890
891 gprs_subscr_update(subscr);
892
893 subscr_put(subscr);
894
895 return CMD_SUCCESS;
896}
897
898DEFUN(update_subscr_update_auth_info, update_subscr_update_auth_info_cmd,
899 UPDATE_SUBSCR_STR "update-auth-info",
900 UPDATE_SUBSCR_HELP
901 "Complete the send authentication info procedure\n")
902{
903 const char *imsi = argv[0];
904
905 struct gsm_subscriber *subscr;
906
907 subscr = gprs_subscr_get_by_imsi(imsi);
908 if (!subscr) {
Jacob Erlbeck15cc8c82015-01-19 14:29:43 +0100909 vty_out(vty, "%% unable to get subscriber record for %s%s",
910 imsi, VTY_NEWLINE);
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +0100911 return CMD_WARNING;
912 }
913
914 gprs_subscr_update_auth_info(subscr);
915
916 subscr_put(subscr);
917
918 return CMD_SUCCESS;
919}
920
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100921DEFUN(cfg_gsup_remote_ip, cfg_gsup_remote_ip_cmd,
922 "gsup remote-ip A.B.C.D",
923 "GSUP Parameters\n"
924 "Set the IP address of the remote GSUP server\n"
925 "IPv4 Address\n")
926{
927 inet_aton(argv[0], &g_cfg->gsup_server_addr.sin_addr);
928
929 return CMD_SUCCESS;
930}
931
932DEFUN(cfg_gsup_remote_port, cfg_gsup_remote_port_cmd,
933 "gsup remote-port <0-65535>",
934 "GSUP Parameters\n"
935 "Set the TCP port of the remote GSUP server\n"
936 "Remote TCP port\n")
937{
938 g_cfg->gsup_server_port = atoi(argv[0]);
939
940 return CMD_SUCCESS;
941}
942
Neels Hofmeyr568a7272015-10-12 11:57:38 +0200943DEFUN(cfg_gsup_oap_id, cfg_gsup_oap_id_cmd,
944 "gsup oap-id <0-65535>",
945 "GSUP Parameters\n"
946 "Set the SGSN's OAP client ID\nOAP client ID (0 == disabled)\n")
947{
948 /* VTY ensures range */
949 g_cfg->oap.client_id = (uint16_t)atoi(argv[0]);
950 return CMD_SUCCESS;
951}
952
953DEFUN(cfg_gsup_oap_k, cfg_gsup_oap_k_cmd,
954 "gsup oap-k K",
955 "GSUP Parameters\n"
956 "Set the OAP shared secret K\nK value (16 byte) hex\n")
957{
958 const char *k = argv[0];
959
960 g_cfg->oap.secret_k_present = 0;
961
962 if ((!k) || (strlen(k) == 0))
963 goto disable;
964
965 int k_len = osmo_hexparse(k,
966 g_cfg->oap.secret_k,
967 sizeof(g_cfg->oap.secret_k));
968 if (k_len != 16) {
969 vty_out(vty, "%% need exactly 16 octets for oap-k, got %d.%s",
970 k_len, VTY_NEWLINE);
971 goto disable;
972 }
973
974 g_cfg->oap.secret_k_present = 1;
975 return CMD_SUCCESS;
976
977disable:
978 if (g_cfg->oap.client_id > 0) {
979 vty_out(vty, "%% OAP client ID set, but invalid oap-k value disables OAP.%s",
980 VTY_NEWLINE);
981 return CMD_WARNING;
982 }
983 return CMD_SUCCESS;
984}
985
986DEFUN(cfg_gsup_oap_opc, cfg_gsup_oap_opc_cmd,
987 "gsup oap-opc OPC",
988 "GSUP Parameters\n"
989 "Set the OAP shared secret OPC\nOPC value (16 byte) hex\n")
990{
991 const char *opc = argv[0];
992
993 g_cfg->oap.secret_opc_present = 0;
994
995 if ((!opc) || (strlen(opc) == 0))
996 goto disable;
997
998 int opc_len = osmo_hexparse(opc,
999 g_cfg->oap.secret_opc,
1000 sizeof(g_cfg->oap.secret_opc));
1001 if (opc_len != 16) {
1002 vty_out(vty, "%% need exactly 16 octets for oap-opc, got %d.%s",
1003 opc_len, VTY_NEWLINE);
1004 goto disable;
1005 }
1006
1007 g_cfg->oap.secret_opc_present = 1;
1008 return CMD_SUCCESS;
1009
1010disable:
1011 if (g_cfg->oap.client_id > 0) {
1012 vty_out(vty, "%% OAP client ID set, but invalid oap-opc value disables OAP.%s",
1013 VTY_NEWLINE);
1014 return CMD_WARNING;
1015 }
1016 return CMD_SUCCESS;
1017}
1018
Holger Hans Peter Freyther9c20a5f2015-02-06 16:23:29 +01001019DEFUN(cfg_apn_name, cfg_apn_name_cmd,
1020 "access-point-name NAME",
1021 "Configure a global list of allowed APNs\n"
1022 "Add this NAME to the list\n")
1023{
1024 return add_apn_ggsn_mapping(vty, argv[0], "", 0);
1025}
1026
1027DEFUN(cfg_no_apn_name, cfg_no_apn_name_cmd,
1028 "no access-point-name NAME",
1029 NO_STR "Configure a global list of allowed APNs\n"
1030 "Remove entry with NAME\n")
1031{
1032 struct apn_ctx *apn_ctx = sgsn_apn_ctx_by_name(argv[0], "");
1033 if (!apn_ctx)
1034 return CMD_SUCCESS;
1035
1036 sgsn_apn_ctx_free(apn_ctx);
1037 return CMD_SUCCESS;
1038}
1039
Holger Hans Peter Freytherc15c61c2015-05-06 17:46:08 +02001040DEFUN(cfg_cdr_filename, cfg_cdr_filename_cmd,
1041 "cdr filename NAME",
1042 "CDR\nSet filename\nname\n")
1043{
1044 talloc_free(g_cfg->cdr.filename);
1045 g_cfg->cdr.filename = talloc_strdup(tall_vty_ctx, argv[0]);
1046 return CMD_SUCCESS;
1047}
1048
1049DEFUN(cfg_no_cdr_filename, cfg_no_cdr_filename_cmd,
1050 "no cdr filename",
1051 NO_STR "CDR\nDisable CDR generation\n")
1052{
1053 talloc_free(g_cfg->cdr.filename);
1054 g_cfg->cdr.filename = NULL;
1055 return CMD_SUCCESS;
1056}
1057
1058DEFUN(cfg_cdr_interval, cfg_cdr_interval_cmd,
1059 "cdr interval <1-2147483647>",
1060 "CDR\nPDP periodic log interval\nSeconds\n")
1061{
1062 g_cfg->cdr.interval = atoi(argv[0]);
1063 return CMD_SUCCESS;
1064}
1065
Harald Welte288be162010-05-01 16:48:27 +02001066int sgsn_vty_init(void)
1067{
Harald Welted193cb32010-05-17 22:58:03 +02001068 install_element_ve(&show_sgsn_cmd);
1069 //install_element_ve(&show_mmctx_tlli_cmd);
1070 install_element_ve(&show_mmctx_imsi_cmd);
1071 install_element_ve(&show_mmctx_all_cmd);
1072 install_element_ve(&show_pdpctx_all_cmd);
Jacob Erlbeck207f4a52014-11-11 14:01:48 +01001073 install_element_ve(&show_subscr_cache_cmd);
1074
Jacob Erlbeck7921ab12014-12-08 15:52:00 +01001075 install_element(ENABLE_NODE, &update_subscr_insert_auth_triplet_cmd);
Jacob Erlbeckd9193432015-01-19 14:11:46 +01001076 install_element(ENABLE_NODE, &update_subscr_create_cmd);
Jacob Erlbecke988ae42015-01-27 12:41:19 +01001077 install_element(ENABLE_NODE, &update_subscr_destroy_cmd);
Jacob Erlbeck207f4a52014-11-11 14:01:48 +01001078 install_element(ENABLE_NODE, &update_subscr_cancel_cmd);
Jacob Erlbeck98a95ac2014-11-28 14:55:25 +01001079 install_element(ENABLE_NODE, &update_subscr_update_location_result_cmd);
1080 install_element(ENABLE_NODE, &update_subscr_update_auth_info_cmd);
Harald Welte288be162010-05-01 16:48:27 +02001081
1082 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
1083 install_node(&sgsn_node, config_write_sgsn);
Jacob Erlbeck36722e12013-10-29 09:30:30 +01001084 vty_install_default(SGSN_NODE);
Harald Weltee300d002010-06-02 12:41:34 +02001085 install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd);
Harald Welted193cb32010-05-17 22:58:03 +02001086 install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd);
1087 //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd);
1088 install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd);
Harald Welte7f6da482013-03-19 11:00:13 +01001089 install_element(SGSN_NODE, &cfg_imsi_acl_cmd);
Harald Welte3dfb5492013-03-19 11:48:54 +01001090 install_element(SGSN_NODE, &cfg_auth_policy_cmd);
Max93408ae2016-06-28 14:10:16 +02001091 install_element(SGSN_NODE, &cfg_encrypt_cmd);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +01001092 install_element(SGSN_NODE, &cfg_gsup_remote_ip_cmd);
1093 install_element(SGSN_NODE, &cfg_gsup_remote_port_cmd);
Neels Hofmeyr568a7272015-10-12 11:57:38 +02001094 install_element(SGSN_NODE, &cfg_gsup_oap_id_cmd);
1095 install_element(SGSN_NODE, &cfg_gsup_oap_k_cmd);
1096 install_element(SGSN_NODE, &cfg_gsup_oap_opc_cmd);
Jacob Erlbeckcb1db8b2015-02-03 13:47:53 +01001097 install_element(SGSN_NODE, &cfg_apn_ggsn_cmd);
1098 install_element(SGSN_NODE, &cfg_apn_imsi_ggsn_cmd);
Holger Hans Peter Freyther9c20a5f2015-02-06 16:23:29 +01001099 install_element(SGSN_NODE, &cfg_apn_name_cmd);
1100 install_element(SGSN_NODE, &cfg_no_apn_name_cmd);
Holger Hans Peter Freytherc15c61c2015-05-06 17:46:08 +02001101 install_element(SGSN_NODE, &cfg_cdr_filename_cmd);
1102 install_element(SGSN_NODE, &cfg_no_cdr_filename_cmd);
1103 install_element(SGSN_NODE, &cfg_cdr_interval_cmd);
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +08001104 install_element(SGSN_NODE, &cfg_ggsn_dynamic_lookup_cmd);
Holger Hans Peter Freythera5a6da42015-05-25 15:20:27 +08001105 install_element(SGSN_NODE, &cfg_grx_ggsn_cmd);
Harald Welte288be162010-05-01 16:48:27 +02001106
Harald Welte94508822015-08-15 19:08:21 +02001107 install_element(SGSN_NODE, &cfg_sgsn_T3312_cmd);
1108 install_element(SGSN_NODE, &cfg_sgsn_T3322_cmd);
1109 install_element(SGSN_NODE, &cfg_sgsn_T3350_cmd);
1110 install_element(SGSN_NODE, &cfg_sgsn_T3360_cmd);
1111 install_element(SGSN_NODE, &cfg_sgsn_T3370_cmd);
1112 install_element(SGSN_NODE, &cfg_sgsn_T3313_cmd);
1113 install_element(SGSN_NODE, &cfg_sgsn_T3314_cmd);
1114 install_element(SGSN_NODE, &cfg_sgsn_T3316_cmd);
1115 install_element(SGSN_NODE, &cfg_sgsn_T3385_cmd);
1116 install_element(SGSN_NODE, &cfg_sgsn_T3386_cmd);
1117 install_element(SGSN_NODE, &cfg_sgsn_T3395_cmd);
1118 install_element(SGSN_NODE, &cfg_sgsn_T3397_cmd);
1119
Harald Welte288be162010-05-01 16:48:27 +02001120 return 0;
1121}
1122
1123int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
1124{
1125 int rc;
1126
1127 g_cfg = cfg;
Harald Welte7f6da482013-03-19 11:00:13 +01001128
Harald Welte94508822015-08-15 19:08:21 +02001129 g_cfg->timers.T3312 = GSM0408_T3312_SECS;
1130 g_cfg->timers.T3322 = GSM0408_T3322_SECS;
1131 g_cfg->timers.T3350 = GSM0408_T3350_SECS;
1132 g_cfg->timers.T3360 = GSM0408_T3360_SECS;
1133 g_cfg->timers.T3370 = GSM0408_T3370_SECS;
1134 g_cfg->timers.T3313 = GSM0408_T3313_SECS;
1135 g_cfg->timers.T3314 = GSM0408_T3314_SECS;
1136 g_cfg->timers.T3316 = GSM0408_T3316_SECS;
1137 g_cfg->timers.T3385 = GSM0408_T3385_SECS;
1138 g_cfg->timers.T3386 = GSM0408_T3386_SECS;
1139 g_cfg->timers.T3395 = GSM0408_T3395_SECS;
1140 g_cfg->timers.T3397 = GSM0408_T3397_SECS;
1141
Harald Weltedcccb182010-05-16 20:52:23 +02001142 rc = vty_read_config_file(config_file, NULL);
Harald Welte288be162010-05-01 16:48:27 +02001143 if (rc < 0) {
1144 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1145 return rc;
1146 }
1147
1148 return 0;
1149}