blob: 4fe3e9711999609d24dc612d3131fc8abbcf8725 [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 Welte288be162010-05-01 16:48:27 +020029
30#include <openbsc/debug.h>
31#include <openbsc/sgsn.h>
32#include <openbsc/gprs_ns.h>
Harald Welted193cb32010-05-17 22:58:03 +020033#include <openbsc/gprs_sgsn.h>
Harald Welte62ab20c2010-05-14 18:59:17 +020034#include <openbsc/vty.h>
Harald Welte288be162010-05-01 16:48:27 +020035
36#include <vty/command.h>
37#include <vty/vty.h>
38
Harald Welted193cb32010-05-17 22:58:03 +020039#include <pdp.h>
40
Harald Welte288be162010-05-01 16:48:27 +020041static struct sgsn_config *g_cfg = NULL;
42
43static struct cmd_node sgsn_node = {
44 SGSN_NODE,
45 "%s(sgsn)#",
46 1,
47};
48
49static int config_write_sgsn(struct vty *vty)
50{
51 struct in_addr ia;
Harald Welte77289c22010-05-18 14:32:29 +020052 struct sgsn_ggsn_ctx *gctx;
Harald Welte288be162010-05-01 16:48:27 +020053
54 vty_out(vty, "sgsn%s", VTY_NEWLINE);
55
56 if (g_cfg->nsip_listen_ip) {
57 ia.s_addr = htonl(g_cfg->nsip_listen_ip);
58 vty_out(vty, " nsip local ip %s%s", inet_ntoa(ia),
59 VTY_NEWLINE);
60 }
61 vty_out(vty, " nsip local port %u%s", g_cfg->nsip_listen_port,
62 VTY_NEWLINE);
63
Harald Welted193cb32010-05-17 22:58:03 +020064 llist_for_each_entry(gctx, &sgsn_ggsn_ctxts, list) {
65 vty_out(vty, " ggsn %u remote-ip %s%s", gctx->id,
66 inet_ntoa(gctx->remote_addr), VTY_NEWLINE);
67 vty_out(vty, " ggsn %u gtp-version %u%s", gctx->id,
68 gctx->gtp_version, VTY_NEWLINE);
Harald Welte288be162010-05-01 16:48:27 +020069 }
70
71 return CMD_SUCCESS;
72}
73
74DEFUN(cfg_sgsn,
75 cfg_sgsn_cmd,
76 "sgsn",
77 "Configure the SGSN")
78{
79 vty->node = SGSN_NODE;
80 return CMD_SUCCESS;
81}
82
83
84DEFUN(cfg_nsip_local_ip,
85 cfg_nsip_local_ip_cmd,
86 "nsip local ip A.B.C.D",
87 "Set the IP address on which we listen for BSS connects")
88{
89 struct in_addr ia;
90
91 inet_aton(argv[0], &ia);
92 g_cfg->nsip_listen_ip = ntohl(ia.s_addr);
93
94 return CMD_SUCCESS;
95}
96
97DEFUN(cfg_nsip_local_port,
98 cfg_nsip_local_port_cmd,
99 "nsip local port <0-65534>",
100 "Set the UDP port on which we listen for BSS connects")
101{
102 unsigned int port = atoi(argv[0]);
103
104 g_cfg->nsip_listen_port = port;
Harald Welted193cb32010-05-17 22:58:03 +0200105
Harald Welte288be162010-05-01 16:48:27 +0200106 return CMD_SUCCESS;
107}
108
Harald Welted193cb32010-05-17 22:58:03 +0200109DEFUN(cfg_ggsn_remote_ip, cfg_ggsn_remote_ip_cmd,
110 "ggsn <0-255> remote-ip A.B.C.D",
111 "")
112{
113 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200114 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welte288be162010-05-01 16:48:27 +0200115
Harald Welted193cb32010-05-17 22:58:03 +0200116 inet_aton(argv[1], &ggc->remote_addr);
Harald Welte288be162010-05-01 16:48:27 +0200117
Harald Welted193cb32010-05-17 22:58:03 +0200118 return CMD_SUCCESS;
119}
120
121#if 0
122DEFUN(cfg_ggsn_remote_port, cfg_ggsn_remote_port_cmd,
123 "ggsn <0-255> remote-port <0-65535>",
124 "")
125{
126 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200127 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200128 uint16_t port = atoi(argv[1]);
129
130}
131#endif
132
133DEFUN(cfg_ggsn_gtp_version, cfg_ggsn_gtp_version_cmd,
134 "ggsn <0-255> gtp-version (0|1)",
135 "")
136{
137 uint32_t id = atoi(argv[0]);
Harald Welte77289c22010-05-18 14:32:29 +0200138 struct sgsn_ggsn_ctx *ggc = sgsn_ggsn_ctx_find_alloc(id);
Harald Welted193cb32010-05-17 22:58:03 +0200139 uint16_t port = atoi(argv[1]);
140
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}