blob: a065ba46e08746c264cf2e84cbc007baf082ba9e [file] [log] [blame]
Harald Welte288be162010-05-01 16:48:27 +02001/*
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 General Public License as published by
8 * the Free Software Foundation; either version 2 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 General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26
27#include <osmocore/talloc.h>
Harald Welted193cb32010-05-17 22:58:03 +020028#include <osmocore/utils.h>
Harald Weltecd4dd4d2010-05-18 17:20:49 +020029#include <osmocore/rate_ctr.h>
Harald Welte288be162010-05-01 16:48:27 +020030
31#include <openbsc/debug.h>
32#include <openbsc/sgsn.h>
33#include <openbsc/gprs_ns.h>
Harald Welted193cb32010-05-17 22:58:03 +020034#include <openbsc/gprs_sgsn.h>
Harald Welte62ab20c2010-05-14 18:59:17 +020035#include <openbsc/vty.h>
Harald Welte288be162010-05-01 16:48:27 +020036
37#include <vty/command.h>
38#include <vty/vty.h>
39
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
44static struct cmd_node sgsn_node = {
45 SGSN_NODE,
46 "%s(sgsn)#",
47 1,
48};
49
50static int config_write_sgsn(struct vty *vty)
51{
52 struct in_addr ia;
Harald Welte77289c22010-05-18 14:32:29 +020053 struct sgsn_ggsn_ctx *gctx;
Harald Welte288be162010-05-01 16:48:27 +020054
55 vty_out(vty, "sgsn%s", VTY_NEWLINE);
56
57 if (g_cfg->nsip_listen_ip) {
58 ia.s_addr = htonl(g_cfg->nsip_listen_ip);
59 vty_out(vty, " nsip local ip %s%s", inet_ntoa(ia),
60 VTY_NEWLINE);
61 }
62 vty_out(vty, " nsip local port %u%s", g_cfg->nsip_listen_port,
63 VTY_NEWLINE);
64
Harald Welted193cb32010-05-17 22:58:03 +020065 llist_for_each_entry(gctx, &sgsn_ggsn_ctxts, list) {
66 vty_out(vty, " ggsn %u remote-ip %s%s", gctx->id,
67 inet_ntoa(gctx->remote_addr), VTY_NEWLINE);
68 vty_out(vty, " ggsn %u gtp-version %u%s", gctx->id,
69 gctx->gtp_version, VTY_NEWLINE);
Harald Welte288be162010-05-01 16:48:27 +020070 }
71
72 return CMD_SUCCESS;
73}
74
75DEFUN(cfg_sgsn,
76 cfg_sgsn_cmd,
77 "sgsn",
78 "Configure the SGSN")
79{
80 vty->node = SGSN_NODE;
81 return CMD_SUCCESS;
82}
83
84
85DEFUN(cfg_nsip_local_ip,
86 cfg_nsip_local_ip_cmd,
87 "nsip local ip A.B.C.D",
88 "Set the IP address on which we listen for BSS connects")
89{
90 struct in_addr ia;
91
92 inet_aton(argv[0], &ia);
93 g_cfg->nsip_listen_ip = ntohl(ia.s_addr);
94
95 return CMD_SUCCESS;
96}
97
98DEFUN(cfg_nsip_local_port,
99 cfg_nsip_local_port_cmd,
100 "nsip local port <0-65534>",
101 "Set the UDP port on which we listen for BSS connects")
102{
103 unsigned int port = atoi(argv[0]);
104
105 g_cfg->nsip_listen_port = port;
Harald Welted193cb32010-05-17 22:58:03 +0200106
Harald Welte288be162010-05-01 16:48:27 +0200107 return CMD_SUCCESS;
108}
109
Harald Welted193cb32010-05-17 22:58:03 +0200110DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd,
111 "ggsn <0-255> remote-ip A.B.C.D",
112 "")
113{
114 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200115 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welte288be162010-05-01 16:48:27 +0200116
Harald Welted193cb32010-05-17 22:58:03 +0200117 inet_aton(argv[1], &ggc->remote_addr);
Harald Welte288be162010-05-01 16:48:27 +0200118
Harald Welted193cb32010-05-17 22:58:03 +0200119 return CMD_SUCCESS;
120}
121
122#if 0
123DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd,
124 "ggsn <0-255> remote-port <0-65535>",
125 "")
126{
127 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200128 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200129 uint16_t port = atoi(argv[1]);
130
131}
132#endif
133
134DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd,
135 "ggsn <0-255> gtp-version (0|1)",
136 "")
137{
138 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200139 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200140
141 if (atoi(argv[1]))
142 ggc->gtp_version = 1;
143 else
144 ggc->gtp_version = 0;
145
146 return CMD_SUCCESS;
147}
148
149#if 0
150DEFUN(cfg_apn_ggsn, cfg_apn_ggsn_cmd,
151 "apn APNAME ggsn <0-255>",
152 "")
153{
154 struct apn_ctx **
155}
156#endif
157
158const struct value_string gprs_mm_st_strs[] = {
159 { GMM_DEREGISTERED, "DEREGISTERED" },
160 { GMM_COMMON_PROC_INIT, "COMMON PROCEDURE (INIT)" },
161 { GMM_REGISTERED_NORMAL, "REGISTERED (NORMAL)" },
162 { GMM_REGISTERED_SUSPENDED, "REGISTeRED (SUSPENDED)" },
163 { GMM_DEREGISTERED_INIT, "DEREGISTERED (INIT)" },
164 { 0, NULL }
165};
166
167static void vty_dump_pdp(struct vty *vty, const char *pfx,
168 struct sgsn_pdp_ctx *pdp)
169{
170 vty_out(vty, "%sPDP Context IMSI: %s, SAPI: %u, NSAPI: %u%s",
Harald Welte6abf94e2010-05-18 10:35:06 +0200171 pfx, pdp->mm->imsi, pdp->sapi, pdp->nsapi, VTY_NEWLINE);
Harald Welted193cb32010-05-17 22:58:03 +0200172 vty_out(vty, "%s APN: %s\n", pfx, pdp->lib->apn_use.v);
173 /* FIXME: statistics */
Harald Welte8acd88f2010-05-18 10:57:45 +0200174 //vty_out_rate_ctr_group(vty, " ", pdp->ctrg);
Harald Welted193cb32010-05-17 22:58:03 +0200175}
176
177static void vty_dump_mmctx(struct vty *vty, const char *pfx,
178 struct sgsn_mm_ctx *mm, int pdp)
179{
180 vty_out(vty, "%sMM Context for IMSI %s, IMEI %s, P-TMSI %08x%s",
181 pfx, mm->imsi, mm->imei, mm->p_tmsi, VTY_NEWLINE);
182 vty_out(vty, "%s MSISDN: %s, TLLI: %08x%s", pfx, mm->msisdn,
183 mm->tlli, VTY_NEWLINE);
184 vty_out(vty, "%s MM State: %s, Routeing Area: %u-%u-%u-%u, "
185 "Cell ID: %u%s", pfx,
186 get_value_string(gprs_mm_st_strs, mm->mm_state),
187 mm->ra.mcc, mm->ra.mnc, mm->ra.lac, mm->ra.rac,
188 mm->cell_id, VTY_NEWLINE);
189
Harald Welte8acd88f2010-05-18 10:57:45 +0200190 vty_out_rate_ctr_group(vty, " ", mm->ctrg);
191
Harald Welted193cb32010-05-17 22:58:03 +0200192 if (pdp) {
193 struct sgsn_pdp_ctx *pdp;
194
195 llist_for_each_entry(pdp, &mm->pdp_list, list)
196 vty_dump_pdp(vty, " ", pdp);
197 }
198}
199
200DEFUN(show_sgsn, show_sgsn_cmd, "show sgsn",
201 SHOW_STR "Display information about the SGSN")
202{
203 /* FIXME: statistics */
204 return CMD_SUCCESS;
205}
206
207#define MMCTX_STR "MM Context\n"
208#define INCLUDE_PDP_STR "Include PDP Context Information\n"
209
210#if 0
211DEFUN(show_mmctx_tlli, show_mmctx_tlli_cmd,
212 "show mm-context tlli HEX [pdp]",
213 SHOW_STR MMCTX_STR "Identify by TLLI\n" "TLLI\n" INCLUDE_PDP_STR)
214{
215 uint32_t tlli;
216 struct sgsn_mm_ctx *mm;
217
218 tlli = strtoul(argv[0], NULL, 16);
219 mm = sgsn_mm_ctx_by_tlli(tlli);
220 if (!mm) {
221 vty_out(vty, "No MM context for TLLI %08x%s",
222 tlli, VTY_NEWLINE);
223 return CMD_WARNING;
224 }
225 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
226 return CMD_SUCCESS;
227}
228#endif
229
230DEFUN(swow_mmctx_imsi, show_mmctx_imsi_cmd,
231 "show mm-context imsi IMSI [pdp]",
232 SHOW_STR MMCTX_STR "Identify by IMSI\n" "IMSI of the MM Context\n"
233 INCLUDE_PDP_STR)
234{
235 struct sgsn_mm_ctx *mm;
236
237 mm = sgsn_mm_ctx_by_imsi(argv[0]);
238 if (!mm) {
239 vty_out(vty, "No MM context for IMSI %s%s",
240 argv[0], VTY_NEWLINE);
241 return CMD_WARNING;
242 }
243 vty_dump_mmctx(vty, "", mm, argv[1] ? 1 : 0);
244 return CMD_SUCCESS;
245}
246
247DEFUN(swow_mmctx_all, show_mmctx_all_cmd,
248 "show mm-context all [pdp]",
249 SHOW_STR MMCTX_STR "All MM Contexts\n" INCLUDE_PDP_STR)
250{
251 struct sgsn_mm_ctx *mm;
252
253 llist_for_each_entry(mm, &sgsn_mm_ctxts, list)
254 vty_dump_mmctx(vty, "", mm, argv[0] ? 1 : 0);
255
256 return CMD_SUCCESS;
257}
258
259DEFUN(show_ggsn, show_ggsn_cmd,
260 "show ggsn",
261 "")
262{
263
264}
265
266DEFUN(show_pdpctx_all, show_pdpctx_all_cmd,
267 "show pdp-context all",
268 SHOW_STR "Display information on PDP Context\n")
269{
270 struct sgsn_pdp_ctx *pdp;
271
272 llist_for_each_entry(pdp, &sgsn_pdp_ctxts, g_list)
273 vty_dump_pdp(vty, "", pdp);
274
275 return CMD_SUCCESS;
276}
Harald Welte288be162010-05-01 16:48:27 +0200277
278int sgsn_vty_init(void)
279{
Harald Welted193cb32010-05-17 22:58:03 +0200280 install_element_ve(&show_sgsn_cmd);
281 //install_element_ve(&show_mmctx_tlli_cmd);
282 install_element_ve(&show_mmctx_imsi_cmd);
283 install_element_ve(&show_mmctx_all_cmd);
284 install_element_ve(&show_pdpctx_all_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200285
286 install_element(CONFIG_NODE, &cfg_sgsn_cmd);
287 install_node(&sgsn_node, config_write_sgsn);
288 install_default(SGSN_NODE);
Harald Welte62ab20c2010-05-14 18:59:17 +0200289 install_element(SGSN_NODE, &ournode_exit_cmd);
Harald Welte54f74242010-05-14 19:11:04 +0200290 install_element(SGSN_NODE, &ournode_end_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200291 install_element(SGSN_NODE, &cfg_nsip_local_ip_cmd);
292 install_element(SGSN_NODE, &cfg_nsip_local_port_cmd);
Harald Welted193cb32010-05-17 22:58:03 +0200293 install_element(SGSN_NODE, &cfg_ggsn_remote_ip_cmd);
294 //install_element(SGSN_NODE, &cfg_ggsn_remote_port_cmd);
295 install_element(SGSN_NODE, &cfg_ggsn_gtp_version_cmd);
Harald Welte288be162010-05-01 16:48:27 +0200296
297 return 0;
298}
299
300int sgsn_parse_config(const char *config_file, struct sgsn_config *cfg)
301{
302 int rc;
303
304 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200305 rc = vty_read_config_file(config_file, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200306 if (rc < 0) {
307 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
308 return rc;
309 }
310
311 return 0;
312}