blob: 4c9cf2841fa61cac9b6690896c4b608c91c69b75 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/*
2 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2010 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * 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
9 * (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
14 * GNU Affero General Public License for more details.
15 *
16 * 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/>.
18 *
19 */
20
Jonathan Santos03fd8d02011-05-25 13:54:02 -040021#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24
Jonathan Santos5a45b152011-08-17 15:33:57 -040025#include <osmocom/core/talloc.h>
26#include <osmocom/core/utils.h>
27#include <osmocom/core/rate_ctr.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040028
29#include <openbsc/debug.h>
30#include <openbsc/sgsn.h>
31#include <openbsc/gprs_ns.h>
32#include <openbsc/gprs_sgsn.h>
33#include <openbsc/vty.h>
34#include <openbsc/gsm_04_08_gprs.h>
35
36#include <osmocom/vty/command.h>
37#include <osmocom/vty/vty.h>
Jonathan Santos5a45b152011-08-17 15:33:57 -040038#include <osmocom/vty/misc.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040039
40#include <pdp.h>
41
42static struct sgsn_config *g_cfg = NULL;
43
44
45#define GSM48_MAX_APN_LEN 102 /* 10.5.6.1 */
46static char *gprs_apn2str(uint8_t *apn, unsigned int len)
47{
48 static char apnbuf[GSM48_MAX_APN_LEN+1];
49 unsigned int i;
50
51 if (!apn)
52 return "";
53
54 if (len > sizeof(apnbuf)-1)
55 len = sizeof(apnbuf)-1;
56
57 memcpy(apnbuf, apn, len);
58 apnbuf[len] = '\0';
59
60 /* replace the domain name step sizes with dots */
61 while (i < len) {
62 unsigned int step = apnbuf[i];
63 apnbuf[i] = '.';
64 i += step+1;
65 }
66
67 return apnbuf+1;
68}
69
70static char *gprs_pdpaddr2str(uint8_t *pdpa, uint8_t len)
71{
72 static char str[INET6_ADDRSTRLEN + 10];
73
74 if (!pdpa || len < 2)
75 return "none";
76
77 switch (pdpa[0] & 0x0f) {
78 case PDP_TYPE_ORG_IETF:
79 switch (pdpa[1]) {
80 case PDP_TYPE_N_IETF_IPv4:
81 if (len < 2 + 4)
82 break;
83 strcpy(str, "IPv4 ");
84 inet_ntop(AF_INET, pdpa+2, str+5, sizeof(str)-5);
85 return str;
86 case PDP_TYPE_N_IETF_IPv6:
87 if (len < 2 + 8)
88 break;
89 strcpy(str, "IPv6 ");
90 inet_ntop(AF_INET6, pdpa+2, str+5, sizeof(str)-5);
91 return str;
92 default:
93 break;
94 }
95 break;
96 case PDP_TYPE_ORG_ETSI:
97 if (pdpa[1] == PDP_TYPE_N_ETSI_PPP)
98 return "PPP";
99 break;
100 default:
101 break;
102 }
103
104 return "invalid";
105}
106
107static struct cmd_node sgsn_node = {
108 SGSN_NODE,
109 "%s(sgsn)#",
110 1,
111};
112
113static int config_write_sgsn(struct vty *vty)
114{
115 struct sgsn_ggsn_ctx *gctx;
116
117 vty_out(vty, "sgsn%s", VTY_NEWLINE);
118
119 vty_out(vty, " gtp local-ip %s%s",
120 inet_ntoa(g_cfg->gtp_listenaddr.sin_addr), VTY_NEWLINE);
121
122 llist_for_each_entry(gctx, &sgsn_ggsn_ctxts, list) {
123 vty_out(vty, " ggsn %u remote-ip %s%s", gctx->id,
124 inet_ntoa(gctx->remote_addr), VTY_NEWLINE);
125 vty_out(vty, " ggsn %u gtp-version %u%s", gctx->id,
126 gctx->gtp_version, VTY_NEWLINE);
127 }
128
129 return CMD_SUCCESS;
130}
131
132#define SGSN_STR "Configure the SGSN"
133
134DEFUN(cfg_sgsn, cfg_sgsn_cmd,
135 "sgsn",
136 SGSN_STR)
137{
138 vty->node = SGSN_NODE;
139 return CMD_SUCCESS;
140}
141
142DEFUN(cfg_sgsn_bind_addr, cfg_sgsn_bind_addr_cmd,
143 "gtp local-ip A.B.C.D",
144 "GTP Parameters\n"
145 "Set the IP address for the local GTP bind\n")
146{
147 inet_aton(argv[0], &g_cfg->gtp_listenaddr.sin_addr);
148
149 return CMD_SUCCESS;
150}
151
152DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd,
153 "ggsn <0-255> remote-ip A.B.C.D",
154 "")
155{
156 uint32_t id = atoi(argv[0]);
157 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
158
159 inet_aton(argv[1], &ggc->remote_addr);
160
161 return CMD_SUCCESS;
162}
163
164#if 0
165DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd,
166 "ggsn <0-255> remote-port <0-65535>",
167 "")
168{
169 uint32_t id = atoi(argv[0]);
170 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
171 uint16_t port = atoi(argv[1]);
172
173}
174#endif
175
176DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd,
177 "ggsn <0-255> gtp-version (0|1)",
178 "")
179{
180 uint32_t id = atoi(argv[0]);
181 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
182
183 if (atoi(argv[1]))
184 ggc->gtp_version = 1;
185 else
186 ggc->gtp_version = 0;
187
188 return CMD_SUCCESS;
189}
190
191#if 0
192DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd,
193 "apn APNAME ggsn <0-255>",
194 "")
195{
196 struct apn_ctx **
197}
198#endif
199
200const struct value_string gprs_mm_st_strs[] = {
201 { GMM_DEREGISTERED, "DEREGISTERED" },
202 { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" },
203 { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" },
204 { GMM_REGISTERED_SUSPENDED, "REGISTERED (SUSPENDED)" },
205 { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" },
206 { 0, NULL }
207};
208
209static void vty_dump_pdp(struct vty *vty, const char *pfx,
210 struct sgsn_pdp_ctx *pdp)
211{
212 vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s",
213 pfx, pdp->mm->imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
214 vty_out(vty, "%s APN: %s%s", pfx,
215 gprs_apn2str(pdp->lib->apn_use.v, pdp->lib->apn_use.l),
216 VTY_NEWLINE);
217 vty_out(vty, "%s PDP Address: %s%s", pfx,
218 gprs_pdpaddr2str(pdp->lib->eua.v, pdp->lib->eua.l),
219 VTY_NEWLINE);
220 vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
221}
222
223static void vty_dump_mmctx(struct vty *vty, const char *pfx,
224 struct sgsn_mm_ctx *mm, int pdp)
225{
226 vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s",
227 pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE);
228 vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn,
229 mm->tlli, VTY_NEWLINE);
230 vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, "
231 "Cell ID: %u%s", pfx,
232 get_value_string(gprs_mm_st_strs, mm->mm_state),
233 mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac,
234 mm->cell_id, VTY_NEWLINE);
235
236 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
237
238 if (pdp) {
239 struct sgsn_pdp_ctx *pdp;
240
241 llist_for_each_entry(pdp, &mm->pdp_list, list)
242 vty_dump_pdp(vty, " ", pdp);
243 }
244}
245
246DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn",
247 SHOW_STR "Display information about the SGSN")
248{
249 /* FIXME: statistics */
250 return CMD_SUCCESS;
251}
252
253#define MMCTX_STR "MM Context\n"
254#define INCLUDE_PDP_STR "Include PDP Context Information\n"
255
256#if 0
257DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd,
258 "show mm-context tlli HEX [pdp]",
259 SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR)
260{
261 uint32_t tlli;
262 struct sgsn_mm_ctx *mm;
263
264 tlli = strtoul(argv[0], NULL, 16);
265 mm = sgsn_mm_ctx_by_tlli(tlli);
266 if (!mm) {
267 vty_out(vty, "No MM context for TLLI %08x%s",
268 tlli, VTY_NEWLINE);
269 return CMD_WARNING;
270 }
271 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
272 return CMD_SUCCESS;
273}
274#endif
275
276DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd,
277 "show mm-context imsi IMSI [pdp]",
278 SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n"
279 INCLUDE_PDP_STR)
280{
281 struct sgsn_mm_ctx *mm;
282
283 mm = sgsn_mm_ctx_by_imsi(argv[0]);
284 if (!mm) {
285 vty_out(vty, "No MM context for IMSI %s%s",
286 argv[0], VTY_NEWLINE);
287 return CMD_WARNING;
288 }
289 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
290 return CMD_SUCCESS;
291}
292
293DEFUN(swow_mmctx_all, show_mmctx_all_cmd,
294 "show mm-context all [pdp]",
295 SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR)
296{
297 struct sgsn_mm_ctx *mm;
298
299 llist_for_each_entry(mm, &sgsn_mm_ctxts, list)
300 vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0);
301
302 return CMD_SUCCESS;
303}
304
305DEFUN(show_ggsn, show_ggsn_cmd,
306 "show ggsn",
307 "")
308{
309
310}
311
312DEFUN(show_pdpctx_all, show_pdpctx_all_cmd,
313 "show pdp-context all",
314 SHOW_STR "Display information on PDP Context\n")
315{
316 struct sgsn_pdp_ctx *pdp;
317
318 llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list)
319 vty_dump_pdp(vty, "", pdp);
320
321 return CMD_SUCCESS;
322}
323
324int sgsn_vty_init(void)
325{
326 install_element_ve(&show_sgsn_cmd);
327 //install_element_ve(&show_mmctx_tlli_cmd);
328 install_element_ve(&show_mmctx_imsi_cmd);
329 install_element_ve(&show_mmctx_all_cmd);
330 install_element_ve(&show_pdpctx_all_cmd);
331
332 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
333 install_node(&sgsn_node, config_write_sgsn);
334 install_default(SGSN_NODE);
335 install_element(SGSN_NODE, &ournode_exit_cmd);
336 install_element(SGSN_NODE, &ournode_end_cmd);
337 install_element(SGSN_NODE, &cfg_sgsn_bind_addr_cmd);
338 install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd);
339 //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd);
340 install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd);
341
342 return 0;
343}
344
345int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
346{
347 int rc;
348
349 g_cfg = cfg;
350 rc = vty_read_config_file(config_file, NULL);
351 if (rc < 0) {
352 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
353 return rc;
354 }
355
356 return 0;
357}