blob: 6d241653d2d4443b3bdce55874c258eb33464bd3 [file] [log] [blame]
Harald Welte799e0c92010-04-30 21:49:24 +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
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 Welte799e0c92010-04-30 21:49:24 +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 Welte799e0c92010-04-30 21:49:24 +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 Welte799e0c92010-04-30 21:49:24 +020018 *
19 */
20
Harald Welte799e0c92010-04-30 21:49:24 +020021#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
Jacob Erlbeck73685282014-05-23 20:48:07 +020024#include <string.h>
Harald Welte799e0c92010-04-30 21:49:24 +020025
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010026#include <osmocom/core/talloc.h>
Harald Welte799e0c92010-04-30 21:49:24 +020027
Harald Welteea34a4e2012-06-16 14:59:56 +080028#include <osmocom/gprs/gprs_ns.h>
29
Harald Welte799e0c92010-04-30 21:49:24 +020030#include <openbsc/debug.h>
Harald Welteb77c6972010-05-01 11:28:43 +020031#include <openbsc/gb_proxy.h>
Harald Welte62ab20c2010-05-14 18:59:17 +020032#include <openbsc/vty.h>
Harald Welte799e0c92010-04-30 21:49:24 +020033
Harald Welte4b037e42010-05-19 19:45:32 +020034#include <osmocom/vty/command.h>
35#include <osmocom/vty/vty.h>
Harald Welte799e0c92010-04-30 21:49:24 +020036
Harald Welte799e0c92010-04-30 21:49:24 +020037static struct gbproxy_config *g_cfg = NULL;
38
39/*
40 * vty code for mgcp below
41 */
Harald Welteb77c6972010-05-01 11:28:43 +020042static struct cmd_node gbproxy_node = {
Harald Welte799e0c92010-04-30 21:49:24 +020043 GBPROXY_NODE,
Harald Welte570ce242012-08-17 13:16:10 +020044 "%s(config-gbproxy)# ",
Harald Welte799e0c92010-04-30 21:49:24 +020045 1,
46};
47
Jacob Erlbeck91fb6802014-05-28 10:59:10 +020048static const struct value_string patch_modes[] = {
49 {GBPROX_PATCH_DEFAULT, "default"},
50 {GBPROX_PATCH_BSSGP, "bssgp"},
51 {GBPROX_PATCH_LLC_ATTACH_REQ, "llc-attach-req"},
52 {GBPROX_PATCH_LLC_ATTACH, "llc-attach"},
53 {GBPROX_PATCH_LLC_GMM, "llc-gmm"},
Jacob Erlbeck73685282014-05-23 20:48:07 +020054 {GBPROX_PATCH_LLC_GSM, "llc-gsm"},
Jacob Erlbeck91fb6802014-05-28 10:59:10 +020055 {GBPROX_PATCH_LLC, "llc"},
56 {0, NULL}
57};
58
Harald Welte799e0c92010-04-30 21:49:24 +020059static int config_write_gbproxy(struct vty *vty)
60{
Harald Welte799e0c92010-04-30 21:49:24 +020061 vty_out(vty, "gbproxy%s", VTY_NEWLINE);
62
Harald Welteff3bde82010-05-19 15:09:09 +020063 vty_out(vty, " sgsn nsei %u%s", g_cfg->nsip_sgsn_nsei,
Harald Welte799e0c92010-04-30 21:49:24 +020064 VTY_NEWLINE);
Harald Welte799e0c92010-04-30 21:49:24 +020065
Jacob Erlbeck67a44452014-05-19 10:14:58 +020066 if (g_cfg->core_mcc > 0)
67 vty_out(vty, " core-mobile-country-code %d%s",
68 g_cfg->core_mcc, VTY_NEWLINE);
69 if (g_cfg->core_mnc > 0)
70 vty_out(vty, " core-mobile-network-code %d%s",
71 g_cfg->core_mnc, VTY_NEWLINE);
Jacob Erlbeck73685282014-05-23 20:48:07 +020072 if (g_cfg->core_apn != NULL) {
73 if (g_cfg->core_apn_size > 0) {
74 char str[500] = {0};
75 vty_out(vty, " core-access-point-name %s%s",
76 gbprox_apn_to_str(str, g_cfg->core_apn,
77 g_cfg->core_apn_size),
78 VTY_NEWLINE);
79 } else {
80 vty_out(vty, " core-access-point-name%s",
81 VTY_NEWLINE);
82 }
83 }
Jacob Erlbeck67a44452014-05-19 10:14:58 +020084
Jacob Erlbeck91fb6802014-05-28 10:59:10 +020085 if (g_cfg->patch_mode != GBPROX_PATCH_DEFAULT)
86 vty_out(vty, " patch-mode %s%s",
87 get_value_string(patch_modes, g_cfg->patch_mode),
88 VTY_NEWLINE);
89
Harald Welte799e0c92010-04-30 21:49:24 +020090 return CMD_SUCCESS;
91}
92
Harald Welte799e0c92010-04-30 21:49:24 +020093DEFUN(cfg_gbproxy,
94 cfg_gbproxy_cmd,
95 "gbproxy",
96 "Configure the Gb proxy")
97{
98 vty->node = GBPROXY_NODE;
99 return CMD_SUCCESS;
100}
101
Harald Welte799e0c92010-04-30 21:49:24 +0200102DEFUN(cfg_nsip_sgsn_nsei,
103 cfg_nsip_sgsn_nsei_cmd,
Harald Welteff3bde82010-05-19 15:09:09 +0200104 "sgsn nsei <0-65534>",
Holger Hans Peter Freyther2eb6e2c2011-11-05 15:14:59 +0100105 "SGSN information\n"
106 "NSEI to be used in the connection with the SGSN\n"
107 "The NSEI\n")
Harald Welte799e0c92010-04-30 21:49:24 +0200108{
109 unsigned int port = atoi(argv[0]);
110
111 g_cfg->nsip_sgsn_nsei = port;
112 return CMD_SUCCESS;
113}
114
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200115#define GBPROXY_CORE_MNC_STR "Use this network code for the core network\n"
116
117DEFUN(cfg_gbproxy_core_mnc,
118 cfg_gbproxy_core_mnc_cmd,
119 "core-mobile-network-code <1-999>",
120 GBPROXY_CORE_MNC_STR "NCC value\n")
121{
122 g_cfg->core_mnc = atoi(argv[0]);
123 return CMD_SUCCESS;
124}
125
126DEFUN(cfg_gbproxy_no_core_mnc,
127 cfg_gbproxy_no_core_mnc_cmd,
128 "no core-mobile-network-code",
129 NO_STR GBPROXY_CORE_MNC_STR)
130{
131 g_cfg->core_mnc = 0;
132 return CMD_SUCCESS;
133}
134
135#define GBPROXY_CORE_MCC_STR "Use this country code for the core network\n"
136
137DEFUN(cfg_gbproxy_core_mcc,
138 cfg_gbproxy_core_mcc_cmd,
139 "core-mobile-country-code <1-999>",
140 GBPROXY_CORE_MCC_STR "MCC value\n")
141{
142 g_cfg->core_mcc = atoi(argv[0]);
143 return CMD_SUCCESS;
144}
145
146DEFUN(cfg_gbproxy_no_core_mcc,
147 cfg_gbproxy_no_core_mcc_cmd,
148 "no core-mobile-country-code",
149 NO_STR GBPROXY_CORE_MCC_STR)
150{
151 g_cfg->core_mcc = 0;
152 return CMD_SUCCESS;
153}
154
Jacob Erlbeck73685282014-05-23 20:48:07 +0200155#define GBPROXY_CORE_APN_STR "Use this access point name (APN) for the backbone\n"
156
157DEFUN(cfg_gbproxy_core_apn_remove,
158 cfg_gbproxy_core_apn_remove_cmd,
159 "core-access-point-name",
160 GBPROXY_CORE_APN_STR)
161{
162 talloc_free(g_cfg->core_apn);
163 /* TODO: replace NULL */
164 g_cfg->core_apn = talloc_zero_size(NULL, 2);
165 g_cfg->core_apn_size = 0;
166 return CMD_SUCCESS;
167}
168
169DEFUN(cfg_gbproxy_core_apn,
170 cfg_gbproxy_core_apn_cmd,
171 "core-access-point-name APN",
172 GBPROXY_CORE_APN_STR "Replacement APN\n")
173{
174 int apn_len = strlen(argv[0]) + 1;
175
176 if (apn_len > 100) {
177 vty_out(vty, "APN string too long (max 99 chars)%s",
178 VTY_NEWLINE);
179 return CMD_WARNING;
180 }
181
182 /* TODO: replace NULL */
183 g_cfg->core_apn = talloc_realloc_size(NULL, g_cfg->core_apn, apn_len);
184 g_cfg->core_apn_size = gbprox_str_to_apn(g_cfg->core_apn, argv[0], apn_len);
185
186 return CMD_SUCCESS;
187}
188
189DEFUN(cfg_gbproxy_no_core_apn,
190 cfg_gbproxy_no_core_apn_cmd,
191 "no core-access-point-name",
192 NO_STR GBPROXY_CORE_APN_STR)
193{
194 talloc_free(g_cfg->core_apn);
195 g_cfg->core_apn = NULL;
196 g_cfg->core_apn_size = 0;
197 return CMD_SUCCESS;
198}
199
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200200DEFUN(cfg_gbproxy_patch_mode,
201 cfg_gbproxy_patch_mode_cmd,
Jacob Erlbeck73685282014-05-23 20:48:07 +0200202 "patch-mode (default|bssgp|llc-attach-req|llc-attach|llc-gmm|llc-gsm|llc)",
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200203 "Set patch mode\n"
Jacob Erlbeck73685282014-05-23 20:48:07 +0200204 "Use build-in default (best effort, try to patch everything)\n"
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200205 "Only patch BSSGP headers\n"
206 "Patch BSSGP headers and LLC Attach Request messages\n"
207 "Patch BSSGP headers and LLC Attach Request/Accept messages\n"
Jacob Erlbeck73685282014-05-23 20:48:07 +0200208 "Patch BSSGP headers and LLC GMM messages\n"
209 "Patch BSSGP headers, LLC GMM, and LLC GSM messages\n"
210 "Patch BSSGP headers and all supported LLC messages\n"
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200211 )
212{
213 int val = get_string_value(patch_modes, argv[0]);
214 OSMO_ASSERT(val >= 0);
215 g_cfg->patch_mode = val;
216 return CMD_SUCCESS;
217}
218
219
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200220
Harald Welte799e0c92010-04-30 21:49:24 +0200221int gbproxy_vty_init(void)
222{
Harald Welte995a2d32010-05-12 16:50:52 +0000223 install_element_ve(&show_gbproxy_cmd);
Harald Welte799e0c92010-04-30 21:49:24 +0200224
Jacob Erlbeck4211d792013-10-24 12:48:23 +0200225 install_element(ENABLE_NODE, &delete_gb_bvci_cmd);
226 install_element(ENABLE_NODE, &delete_gb_nsei_cmd);
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +0200227
Harald Welte799e0c92010-04-30 21:49:24 +0200228 install_element(CONFIG_NODE, &cfg_gbproxy_cmd);
229 install_node(&gbproxy_node, config_write_gbproxy);
Jacob Erlbeck36722e12013-10-29 09:30:30 +0100230 vty_install_default(GBPROXY_NODE);
Harald Welte799e0c92010-04-30 21:49:24 +0200231 install_element(GBPROXY_NODE, &cfg_nsip_sgsn_nsei_cmd);
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200232 install_element(GBPROXY_NODE, &cfg_gbproxy_core_mcc_cmd);
233 install_element(GBPROXY_NODE, &cfg_gbproxy_core_mnc_cmd);
Jacob Erlbeck73685282014-05-23 20:48:07 +0200234 install_element(GBPROXY_NODE, &cfg_gbproxy_core_apn_remove_cmd);
235 install_element(GBPROXY_NODE, &cfg_gbproxy_core_apn_cmd);
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200236 install_element(GBPROXY_NODE, &cfg_gbproxy_no_core_mcc_cmd);
237 install_element(GBPROXY_NODE, &cfg_gbproxy_no_core_mnc_cmd);
Jacob Erlbeck73685282014-05-23 20:48:07 +0200238 install_element(GBPROXY_NODE, &cfg_gbproxy_no_core_apn_cmd);
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200239 install_element(GBPROXY_NODE, &cfg_gbproxy_patch_mode_cmd);
Harald Welte799e0c92010-04-30 21:49:24 +0200240
241 return 0;
242}
243
244int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg)
245{
246 int rc;
247
248 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200249 rc = vty_read_config_file(config_file, NULL);
Harald Welte799e0c92010-04-30 21:49:24 +0200250 if (rc < 0) {
251 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
252 return rc;
253 }
254
255 return 0;
256}
257