blob: 19039f694e70de4aba2160d8fc6590ce1d79b8ba [file] [log] [blame]
Harald Welte288be162010-05-01 16:48:27 +02001/* GPRS SGSN Implementation */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther85531cc2010-10-06 20:37:09 +08004 * (C) 2010 by On-Waves
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
22#include <unistd.h>
23#include <stdio.h>
Max4011e722016-07-05 15:19:12 +020024#include <time.h>
Harald Welte288be162010-05-01 16:48:27 +020025#include <stdlib.h>
26#include <string.h>
27#include <getopt.h>
28#include <errno.h>
Harald Welte66f793a2010-05-13 11:50:04 +020029#include <signal.h>
Harald Welte288be162010-05-01 16:48:27 +020030#include <sys/fcntl.h>
31#include <sys/stat.h>
Harald Welte288be162010-05-01 16:48:27 +020032#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020036#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#include <osmocom/core/talloc.h>
38#include <osmocom/core/select.h>
39#include <osmocom/core/rate_ctr.h>
40#include <osmocom/core/logging.h>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010041#include <osmocom/core/stats.h>
Alexander Couzensf23e2db2020-07-27 22:39:58 +020042#include <osmocom/core/sockaddr_str.h>
Harald Welte288be162010-05-01 16:48:27 +020043
Alexander Couzensf23e2db2020-07-27 22:39:58 +020044#include <osmocom/gprs/gprs_ns2.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080045#include <osmocom/gprs/gprs_bssgp.h>
Harald Welte9d16b142020-06-26 15:55:21 +020046#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080047
Harald Welte4b037e42010-05-19 19:45:32 +020048#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010049#include <osmocom/vty/logging.h>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010050#include <osmocom/vty/stats.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020051#include <osmocom/vty/ports.h>
Harald Welte731930b2018-02-14 01:02:22 +010052#include <osmocom/vty/misc.h>
Pau Espin Pedrolaae7daf2020-08-18 13:50:00 +020053#include <osmocom/vty/cpu_sched_vty.h>
Harald Welte4b037e42010-05-19 19:45:32 +020054
Neels Hofmeyr73828152016-02-23 15:10:33 +010055#include <osmocom/ctrl/control_vty.h>
56
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020057#include <osmocom/sgsn/signal.h>
58#include <osmocom/sgsn/debug.h>
59#include <osmocom/sgsn/vty.h>
60#include <osmocom/sgsn/sgsn.h>
61#include <osmocom/sgsn/gprs_llc.h>
62#include <osmocom/sgsn/gprs_gmm.h>
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +020063#include <osmocom/sgsn/gprs_ranap.h>
Alexander Couzensf23e2db2020-07-27 22:39:58 +020064#include <osmocom/sgsn/gprs_gb.h>
Daniel Willmann6292c8d2016-05-21 17:35:57 +020065
Harald Welteba874b82014-08-20 23:47:15 +020066#include <osmocom/ctrl/control_if.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020067#include <osmocom/ctrl/ports.h>
Harald Welte288be162010-05-01 16:48:27 +020068
Harald Welte2720e732010-05-17 00:44:57 +020069#include <gtp.h>
70
Harald Weltee2365962010-05-04 07:41:59 +020071#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020072
Neels Hofmeyra7a39472017-07-05 15:19:52 +020073#if BUILD_IU
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020074#include <osmocom/sigtran/osmo_ss7.h>
75#include <osmocom/sigtran/protocol/m3ua.h>
Neels Hofmeyra7a39472017-07-05 15:19:52 +020076#include <osmocom/ranap/iu_client.h>
77#endif
78
Harald Welte288be162010-05-01 16:48:27 +020079#define _GNU_SOURCE
80#include <getopt.h>
81
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010082void *tall_sgsn_ctx;
Pau Espin Pedrol2e9ea502017-11-29 14:01:35 +010083struct ctrl_handle *g_ctrlh;
Harald Welte288be162010-05-01 16:48:27 +020084
Alexander Couzensf23e2db2020-07-27 22:39:58 +020085struct gprs_ns2_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020086static int daemonize = 0;
Harald Welte288be162010-05-01 16:48:27 +020087const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080088 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte69ca8f02015-10-29 10:35:24 +010089 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080090 "This is free software: you are free to change and redistribute it.\r\n"
91 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte288be162010-05-01 16:48:27 +020092
Neels Hofmeyrfea06092017-10-31 01:00:37 +010093#define CONFIG_FILE_DEFAULT "osmo-sgsn.cfg"
94#define CONFIG_FILE_LEGACY "osmo_sgsn.cfg"
95
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +010096
97struct sgsn_instance *sgsn;
Harald Welte288be162010-05-01 16:48:27 +020098
Harald Welteb1fd9022012-06-17 12:16:31 +080099/* call-back function for the BSSGP protocol */
100int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
101{
102 struct osmo_bssgp_prim *bp;
103 bp = container_of(oph, struct osmo_bssgp_prim, oph);
104
105 switch (oph->sap) {
106 case SAP_BSSGP_LL:
107 switch (oph->primitive) {
108 case PRIM_BSSGP_UL_UD:
109 return gprs_llc_rcvmsg(oph->msg, bp->tp);
110 }
111 break;
112 case SAP_BSSGP_GMM:
113 switch (oph->primitive) {
114 case PRIM_BSSGP_GMM_SUSPEND:
115 return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli);
116 case PRIM_BSSGP_GMM_RESUME:
117 return gprs_gmm_rx_resume(bp->ra_id, bp->tlli,
Holger Hans Peter Freyther9b5db282012-08-03 10:01:57 +0200118 bp->u.resume.suspend_ref);
Harald Welteb1fd9022012-06-17 12:16:31 +0800119 }
120 break;
121 case SAP_BSSGP_NM:
122 break;
123 }
124 return 0;
125}
126
Pau Espin Pedrolbcd77092020-11-25 18:28:55 +0100127static void signal_handler(int signum)
Harald Welte66f793a2010-05-13 11:50:04 +0200128{
Pau Espin Pedrolbcd77092020-11-25 18:28:55 +0100129 fprintf(stdout, "signal %u received\n", signum);
Harald Welte66f793a2010-05-13 11:50:04 +0200130
Pau Espin Pedrolbcd77092020-11-25 18:28:55 +0100131 switch (signum) {
Harald Welte66f793a2010-05-13 11:50:04 +0200132 case SIGINT:
Harald Welte6f750e62017-08-20 20:50:06 +0200133 case SIGTERM:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200134 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200135 sleep(1);
136 exit(0);
137 break;
138 case SIGABRT:
Pau Espin Pedrolbcd77092020-11-25 18:28:55 +0100139 /* in case of abort, we want to obtain a talloc report and
140 * then run default SIGABRT handler, who will generate coredump
141 * and abort the process. abort() should do this for us after we
142 * return, but program wouldn't exit if an external SIGABRT is
143 * received.
144 */
145 talloc_report(tall_vty_ctx, stderr);
146 talloc_report_full(tall_sgsn_ctx, stderr);
147 signal(SIGABRT, SIG_DFL);
148 raise(SIGABRT);
149 break;
Harald Welte66f793a2010-05-13 11:50:04 +0200150 case SIGUSR1:
151 talloc_report(tall_vty_ctx, stderr);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100152 talloc_report_full(tall_sgsn_ctx, stderr);
Harald Welte66f793a2010-05-13 11:50:04 +0200153 break;
154 case SIGUSR2:
155 talloc_report_full(tall_vty_ctx, stderr);
156 break;
157 default:
158 break;
159 }
160}
161
Harald Welted6c74162010-05-02 21:29:17 +0200162/* NSI that BSSGP uses when transmitting on NS */
163extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte288be162010-05-01 16:48:27 +0200164
Harald Weltec31f4802010-05-25 23:31:39 +0200165static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100166 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200167 .version = PACKAGE_VERSION,
Daniel Willmann62fa6192020-11-03 09:07:45 +0100168#if BUILD_IU
169 .go_parent_cb = osmo_ss7_vty_go_parent,
170#endif
Harald Weltec31f4802010-05-25 23:31:39 +0200171};
172
Harald Welte9ae28a12010-08-27 09:26:44 +0200173static void print_help(void)
174{
175 printf("Some useful help...\n");
176 printf(" -h --help\tthis text\n");
Daniel Willmann03a2ed52019-07-26 11:56:31 +0200177 printf(" -V --version\tPrint the version\n");
Harald Welte9ae28a12010-08-27 09:26:44 +0200178 printf(" -D --daemonize\tFork the process into a background daemon\n");
179 printf(" -d option --debug\tenable Debugging\n");
180 printf(" -s --disable-color\n");
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100181 printf(" -c --config-file\tThe config file to use [%s]\n", CONFIG_FILE_DEFAULT);
Harald Welte9ae28a12010-08-27 09:26:44 +0200182 printf(" -e --log-level number\tSet a global log level\n");
183}
184
185static void handle_options(int argc, char **argv)
186{
187 while (1) {
188 int option_index = 0, c;
189 static struct option long_options[] = {
190 {"help", 0, 0, 'h'},
191 {"debug", 1, 0, 'd'},
192 {"daemonize", 0, 0, 'D'},
193 {"config-file", 1, 0, 'c'},
194 {"disable-color", 0, 0, 's'},
195 {"timestamp", 0, 0, 'T'},
Philipp Maier99c97072017-03-14 17:32:35 +0100196 { "version", 0, 0, 'V' },
Harald Welte9ae28a12010-08-27 09:26:44 +0200197 {"log-level", 1, 0, 'e'},
198 {NULL, 0, 0, 0}
199 };
200
Philipp Maier99c97072017-03-14 17:32:35 +0100201 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte9ae28a12010-08-27 09:26:44 +0200202 long_options, &option_index);
203 if (c == -1)
204 break;
205
206 switch (c) {
207 case 'h':
208 //print_usage();
209 print_help();
210 exit(0);
211 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200212 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200213 break;
214 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200215 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200216 break;
217 case 'D':
218 daemonize = 1;
219 break;
220 case 'c':
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100221 osmo_talloc_replace_string(sgsn, &sgsn->config_file, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200222 break;
223 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200224 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200225 break;
Philipp Maier99c97072017-03-14 17:32:35 +0100226 case 'V':
227 print_version(1);
228 exit(0);
229 break;
Harald Welte9ae28a12010-08-27 09:26:44 +0200230 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200231 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200232 break;
233 default:
234 /* ignore */
235 break;
236 }
237 }
Harald Weltede67c002019-12-03 22:28:19 +0100238
239 if (argc > optind) {
240 fprintf(stderr, "Unsupported positional arguments on command line\n");
241 exit(2);
242 }
Harald Welte9ae28a12010-08-27 09:26:44 +0200243}
244
Harald Welte11461a62012-06-17 12:58:46 +0800245/* default categories */
246static struct log_info_cat gprs_categories[] = {
247 [DMM] = {
248 .name = "DMM",
249 .description = "Layer3 Mobility Management (MM)",
250 .color = "\033[1;33m",
251 .enabled = 1, .loglevel = LOGL_NOTICE,
252 },
Harald Welte11461a62012-06-17 12:58:46 +0800253 [DPAG] = {
254 .name = "DPAG",
255 .description = "Paging Subsystem",
256 .color = "\033[1;38m",
257 .enabled = 1, .loglevel = LOGL_NOTICE,
258 },
259 [DMEAS] = {
260 .name = "DMEAS",
261 .description = "Radio Measurement Processing",
262 .enabled = 0, .loglevel = LOGL_NOTICE,
263 },
264 [DREF] = {
265 .name = "DREF",
266 .description = "Reference Counting",
267 .enabled = 0, .loglevel = LOGL_NOTICE,
268 },
269 [DGPRS] = {
270 .name = "DGPRS",
271 .description = "GPRS Packet Service",
Philipp Maier56143542017-10-19 18:20:33 +0200272 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800273 },
274 [DNS] = {
275 .name = "DNS",
276 .description = "GPRS Network Service (NS)",
Philipp Maier56143542017-10-19 18:20:33 +0200277 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800278 },
Harald Welte11461a62012-06-17 12:58:46 +0800279 [DLLC] = {
280 .name = "DLLC",
281 .description = "GPRS Logical Link Control Protocol (LLC)",
Philipp Maier56143542017-10-19 18:20:33 +0200282 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800283 },
284 [DSNDCP] = {
285 .name = "DSNDCP",
286 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200287 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800288 },
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200289 [DRANAP] = {
290 .name = "DRANAP",
291 .description = "RAN Application Part (RANAP)",
Philipp Maier56143542017-10-19 18:20:33 +0200292 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200293 },
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200294 [DSUA] = {
295 .name = "DSUA",
296 .description = "SCCP User Adaptation (SUA)",
Philipp Maier56143542017-10-19 18:20:33 +0200297 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200298 },
Philipp2c7f8372016-08-26 16:58:41 +0200299 [DSLHC] = {
300 .name = "DSLHC",
301 .description = "RFC1144 TCP/IP Header compression (SLHC)",
Philipp Maier56143542017-10-19 18:20:33 +0200302 .enabled = 1, .loglevel = LOGL_NOTICE,
Philipp2c7f8372016-08-26 16:58:41 +0200303 },
Philipp73f83d52016-09-02 13:38:01 +0200304 [DV42BIS] = {
305 .name = "DV42BIS",
306 .description = "V.42bis data compression (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200307 .enabled = 1, .loglevel = LOGL_NOTICE,
Pau Espin Pedrol6ec5dc22019-08-28 16:52:56 +0200308 },
309 [DGTP] = {
310 .name = "DGTP",
311 .description = "GPRS Tunnelling Protocol (GTP)",
312 .enabled = 1, .loglevel = LOGL_NOTICE,
313 },
Harald Welte11461a62012-06-17 12:58:46 +0800314};
315
316static const struct log_info gprs_log_info = {
317 .filter_fn = gprs_log_filter_fn,
318 .cat = gprs_categories,
319 .num_cat = ARRAY_SIZE(gprs_categories),
320};
321
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100322static bool file_exists(const char *path)
323{
324 struct stat sb;
325 return stat(path, &sb) ? false : true;
326}
327
Harald Welte288be162010-05-01 16:48:27 +0200328int main(int argc, char **argv)
329{
Harald Welte288be162010-05-01 16:48:27 +0200330 int rc;
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200331 struct osmo_sockaddr_str bind_address = {
332 .af = AF_INET,
333 .ip = "0.0.0.0",
334 .port = 23000,
335 };
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200336#if BUILD_IU
337 struct osmo_sccp_instance *sccp;
338#endif
Harald Welte288be162010-05-01 16:48:27 +0200339
Max4011e722016-07-05 15:19:12 +0200340 srand(time(NULL));
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100341 tall_sgsn_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100342 sgsn = sgsn_instance_alloc(tall_sgsn_ctx);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100343 msgb_talloc_ctx_init(tall_sgsn_ctx, 0);
344 vty_info.tall_ctx = tall_sgsn_ctx;
Harald Welte66f793a2010-05-13 11:50:04 +0200345
346 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200347 signal(SIGTERM, &signal_handler);
Harald Welte66f793a2010-05-13 11:50:04 +0200348 signal(SIGABRT, &signal_handler);
349 signal(SIGUSR1, &signal_handler);
350 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200351
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200352 osmo_init_ignore_signals();
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100353 osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info);
354 osmo_stats_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200355
Harald Weltec31f4802010-05-25 23:31:39 +0200356 vty_info.copyright = openbsc_copyright;
357 vty_init(&vty_info);
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200358 logging_vty_add_cmds();
Harald Welte731930b2018-02-14 01:02:22 +0100359 osmo_talloc_vty_add_cmds();
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200360 osmo_stats_vty_add_cmds();
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100361 sgsn_vty_init(&sgsn->cfg);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100362 ctrl_vty_init(tall_sgsn_ctx);
Pau Espin Pedrolaae7daf2020-08-18 13:50:00 +0200363 osmo_cpu_sched_vty_init(tall_sgsn_ctx);
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200364
365#if BUILD_IU
Harald Welte61557f42019-12-01 13:25:16 +0100366 OSMO_ASSERT(osmo_ss7_init() == 0);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100367 osmo_ss7_vty_init_asp(tall_sgsn_ctx);
Neels Hofmeyr395c5582018-09-27 01:55:08 +0200368 osmo_sccp_vty_init();
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200369#endif
Harald Weltedcccb182010-05-16 20:52:23 +0200370
Harald Welte9ae28a12010-08-27 09:26:44 +0200371 handle_options(argc, argv);
372
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100373 /* Backwards compatibility: for years, the default config file name was
374 * osmo_sgsn.cfg. All other Osmocom programs use osmo-*.cfg with a
375 * dash. To be able to use the new config file name without breaking
376 * previous setups that might rely on the legacy default config file
377 * name, we need to look for the old config file if no -c option was
378 * passed AND no file exists with the new default file name. */
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100379 if (!sgsn->config_file) {
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100380 /* No -c option was passed */
381 if (file_exists(CONFIG_FILE_LEGACY)
382 && !file_exists(CONFIG_FILE_DEFAULT))
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100383 osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_LEGACY);
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100384 else
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100385 osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_DEFAULT);
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100386 }
387
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100388 rate_ctr_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200389
Harald Welte453a51d2020-12-10 15:18:50 +0100390 logging_vty_add_deprecated_subsys(tall_sgsn_ctx, "bssgp");
Harald Welte68d85d52012-06-16 17:45:59 +0800391
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200392 sgsn_nsi = gprs_ns2_instantiate(tall_sgsn_ctx, &gprs_ns_prim_cb, NULL);
Harald Welte288be162010-05-01 16:48:27 +0200393 if (!sgsn_nsi) {
394 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
395 exit(1);
396 }
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200397 sgsn->cfg.nsi = sgsn_nsi;
398 bssgp_set_bssgp_callback(gprs_gb_send_cb, sgsn_nsi);
Harald Welte496aee42010-06-30 19:59:55 +0200399
400 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
Alexander Couzens14314bd2016-07-05 09:52:52 +0200401 sgsn_rate_ctr_init();
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100402 sgsn_inst_init(sgsn);
Harald Welte496aee42010-06-30 19:59:55 +0200403
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200404
405 gprs_ns2_vty_init(sgsn_nsi, &bind_address);
Harald Weltece95b272012-06-17 13:04:02 +0800406 bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200407 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200408 gprs_sndcp_vty_init();
Max2e485762018-12-10 18:01:47 +0100409 sgsn_auth_init(sgsn);
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100410 sgsn_cdr_init(sgsn);
Harald Welte66f793a2010-05-13 11:50:04 +0200411
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100412 rc = sgsn_parse_config(sgsn->config_file);
Harald Welte66f793a2010-05-13 11:50:04 +0200413 if (rc < 0) {
Neels Hofmeyr27355c92017-02-24 06:28:31 +0100414 LOGP(DGPRS, LOGL_FATAL, "Error in config file\n");
Harald Welte66f793a2010-05-13 11:50:04 +0200415 exit(2);
416 }
417
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100418 /* start telnet after reading config for vty_get_bind_addr() */
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100419 rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100420 vty_get_bind_addr(), OSMO_VTY_PORT_SGSN);
421 if (rc < 0)
422 exit(1);
423
Neels Hofmeyr73828152016-02-23 15:10:33 +0100424 /* start control interface after reading config for
425 * ctrl_vty_get_bind_addr() */
Stefan Sperling25996442018-12-11 14:35:43 +0100426 g_ctrlh = ctrl_interface_setup_dynip(NULL, ctrl_vty_get_bind_addr(),
427 OSMO_CTRL_PORT_SGSN, NULL);
Pau Espin Pedrol2e9ea502017-11-29 14:01:35 +0100428 if (!g_ctrlh) {
Neels Hofmeyr73828152016-02-23 15:10:33 +0100429 LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
430 exit(1);
431 }
432
433 if (sgsn_ctrl_cmds_install() != 0) {
434 LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
435 exit(1);
436 }
437
438
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100439 rc = sgsn_gtp_init(sgsn);
Harald Welte6efc1762010-05-19 15:46:31 +0200440 if (rc) {
441 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200442 exit(2);
Maxbaabc682017-10-20 13:39:57 +0200443 } else
444 LOGP(DGPRS, LOGL_NOTICE, "libGTP v%s initialized\n", gtp_version());
Harald Welte269ae752010-05-18 14:44:31 +0200445
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100446 rc = gprs_subscr_init(sgsn);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100447 if (rc < 0) {
448 LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n");
449 exit(2);
450 }
451
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200452 rc = gprs_ns2_vty_create();
Harald Welte6efc1762010-05-19 15:46:31 +0200453 if (rc < 0) {
454 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200455 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200456 }
457
Alexander Couzensf23e2db2020-07-27 22:39:58 +0200458 gprs_ns2_dynamic_create_nse(sgsn_nsi, true);
Harald Welte288be162010-05-01 16:48:27 +0200459
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800460 if (sgsn->cfg.dynamic_lookup) {
461 if (sgsn_ares_init(sgsn) != 0) {
462 LOGP(DGPRS, LOGL_FATAL,
463 "Failed to initialize c-ares(%d)\n", rc);
464 exit(4);
465 }
466 }
467
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200468#if BUILD_IU
Neels Hofmeyr1d047ed2017-11-24 04:33:08 +0100469 /* Note that these are mostly defaults and can be overriden from the VTY */
Pau Espin Pedrol2c908992019-08-19 19:06:06 +0200470 sccp = osmo_sccp_simple_client_on_ss7_id(tall_sgsn_ctx,
471 sgsn->cfg.iu.cs7_instance,
472 "OsmoSGSN",
Pau Espin Pedrolf0f596f2019-08-19 17:54:16 +0200473 (23 << 3) + 4,
474 OSMO_SS7_ASP_PROT_M3UA,
Pau Espin Pedrole6c5b4a2020-08-20 12:35:07 +0200475 0, "localhost",
476 0, "localhost");
Philipp Maierefe85d32017-04-09 12:32:51 +0200477 if (!sccp) {
478 printf("Setting up SCCP client failed.\n");
479 return 8;
480 }
481
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100482 ranap_iu_init(tall_sgsn_ctx, DRANAP, "OsmoSGSN-IuPS", sccp, gsm0408_gprs_rcvmsg_iu, sgsn_ranap_iu_event);
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200483#endif
484
Harald Welte9ae28a12010-08-27 09:26:44 +0200485 if (daemonize) {
486 rc = osmo_daemonize();
487 if (rc < 0) {
488 perror("Error during daemonize");
489 exit(1);
490 }
491 }
492
Harald Welte288be162010-05-01 16:48:27 +0200493 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200494 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200495 if (rc < 0)
496 exit(3);
497 }
498
Harald Weltebaf77002011-07-16 13:47:01 +0200499 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200500 exit(0);
501}