blob: 5979886bf4c2124b5d7984753f34b242434bfdc3 [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>
Harald Welte288be162010-05-01 16:48:27 +020042
Harald Welteea34a4e2012-06-16 14:59:56 +080043#include <osmocom/gprs/gprs_ns.h>
44#include <osmocom/gprs/gprs_bssgp.h>
45
Harald Welte4b037e42010-05-19 19:45:32 +020046#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010047#include <osmocom/vty/logging.h>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010048#include <osmocom/vty/stats.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020049#include <osmocom/vty/ports.h>
Harald Welte731930b2018-02-14 01:02:22 +010050#include <osmocom/vty/misc.h>
Harald Welte4b037e42010-05-19 19:45:32 +020051
Neels Hofmeyr73828152016-02-23 15:10:33 +010052#include <osmocom/ctrl/control_vty.h>
53
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020054#include <osmocom/sgsn/signal.h>
55#include <osmocom/sgsn/debug.h>
56#include <osmocom/sgsn/vty.h>
57#include <osmocom/sgsn/sgsn.h>
58#include <osmocom/sgsn/gprs_llc.h>
59#include <osmocom/sgsn/gprs_gmm.h>
Daniel Willmann6292c8d2016-05-21 17:35:57 +020060
Harald Welteba874b82014-08-20 23:47:15 +020061#include <osmocom/ctrl/control_if.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020062#include <osmocom/ctrl/ports.h>
Harald Welte288be162010-05-01 16:48:27 +020063
Harald Welte2720e732010-05-17 00:44:57 +020064#include <gtp.h>
65
Harald Weltee2365962010-05-04 07:41:59 +020066#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020067
Neels Hofmeyra7a39472017-07-05 15:19:52 +020068#if BUILD_IU
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020069#include <osmocom/sigtran/osmo_ss7.h>
70#include <osmocom/sigtran/protocol/m3ua.h>
Neels Hofmeyra7a39472017-07-05 15:19:52 +020071#include <osmocom/ranap/iu_client.h>
72#endif
73
Harald Welte288be162010-05-01 16:48:27 +020074#define _GNU_SOURCE
75#include <getopt.h>
76
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010077void *tall_sgsn_ctx;
Pau Espin Pedrol2e9ea502017-11-29 14:01:35 +010078struct ctrl_handle *g_ctrlh;
Harald Welte288be162010-05-01 16:48:27 +020079
80struct gprs_ns_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020081static int daemonize = 0;
Harald Welte288be162010-05-01 16:48:27 +020082const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080083 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte69ca8f02015-10-29 10:35:24 +010084 "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 +080085 "This is free software: you are free to change and redistribute it.\r\n"
86 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte288be162010-05-01 16:48:27 +020087
Neels Hofmeyrfea06092017-10-31 01:00:37 +010088#define CONFIG_FILE_DEFAULT "osmo-sgsn.cfg"
89#define CONFIG_FILE_LEGACY "osmo_sgsn.cfg"
90
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +010091
92struct sgsn_instance *sgsn;
Harald Welte288be162010-05-01 16:48:27 +020093
94/* call-back function for the NS protocol */
95static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020096 struct msgb *msg, uint16_t bvci)
Harald Welte288be162010-05-01 16:48:27 +020097{
98 int rc = 0;
99
100 switch (event) {
101 case GPRS_NS_EVT_UNIT_DATA:
102 /* hand the message into the BSSGP implementation */
Harald Weltece95b272012-06-17 13:04:02 +0800103 rc = bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +0200104 break;
105 default:
106 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
107 if (msg)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100108 msgb_free(msg);
Harald Welte288be162010-05-01 16:48:27 +0200109 rc = -EIO;
110 break;
111 }
112 return rc;
113}
114
Harald Welteb1fd9022012-06-17 12:16:31 +0800115/* call-back function for the BSSGP protocol */
116int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
117{
118 struct osmo_bssgp_prim *bp;
119 bp = container_of(oph, struct osmo_bssgp_prim, oph);
120
121 switch (oph->sap) {
122 case SAP_BSSGP_LL:
123 switch (oph->primitive) {
124 case PRIM_BSSGP_UL_UD:
125 return gprs_llc_rcvmsg(oph->msg, bp->tp);
126 }
127 break;
128 case SAP_BSSGP_GMM:
129 switch (oph->primitive) {
130 case PRIM_BSSGP_GMM_SUSPEND:
131 return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli);
132 case PRIM_BSSGP_GMM_RESUME:
133 return gprs_gmm_rx_resume(bp->ra_id, bp->tlli,
Holger Hans Peter Freyther9b5db282012-08-03 10:01:57 +0200134 bp->u.resume.suspend_ref);
Harald Welteb1fd9022012-06-17 12:16:31 +0800135 }
136 break;
137 case SAP_BSSGP_NM:
138 break;
139 }
140 return 0;
141}
142
Harald Welte66f793a2010-05-13 11:50:04 +0200143static void signal_handler(int signal)
144{
145 fprintf(stdout, "signal %u received\n", signal);
146
147 switch (signal) {
148 case SIGINT:
Harald Welte6f750e62017-08-20 20:50:06 +0200149 case SIGTERM:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200150 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200151 sleep(1);
152 exit(0);
153 break;
154 case SIGABRT:
155 /* in case of abort, we want to obtain a talloc report
156 * and then return to the caller, who will abort the process */
157 case SIGUSR1:
158 talloc_report(tall_vty_ctx, stderr);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100159 talloc_report_full(tall_sgsn_ctx, stderr);
Harald Welte66f793a2010-05-13 11:50:04 +0200160 break;
161 case SIGUSR2:
162 talloc_report_full(tall_vty_ctx, stderr);
163 break;
164 default:
165 break;
166 }
167}
168
Harald Welted6c74162010-05-02 21:29:17 +0200169/* NSI that BSSGP uses when transmitting on NS */
170extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte288be162010-05-01 16:48:27 +0200171
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200172int sgsn_vty_is_config_node(struct vty *vty, int node)
173{
174 /* So far the SGSN has no nested nodes that need parent node
175 * declaration, except for the ss7 vty nodes. */
176 switch (node) {
177 case SGSN_NODE:
178 return 1;
179 default:
180#if BUILD_IU
181 return osmo_ss7_is_config_node(vty, node);
182#else
183 return 0;
184#endif
185 }
186}
187
188int sgsn_vty_go_parent(struct vty *vty)
189{
190 /* So far the SGSN has no nested nodes that need parent node
191 * declaration, except for the ss7 vty nodes. */
192#if BUILD_IU
193 return osmo_ss7_vty_go_parent(vty);
194#else
195 vty->node = CONFIG_NODE;
196 vty->index = NULL;
197 return 0;
198#endif
199}
Harald Weltec31f4802010-05-25 23:31:39 +0200200
201static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100202 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200203 .version = PACKAGE_VERSION,
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200204 .go_parent_cb = sgsn_vty_go_parent,
205 .is_config_node = sgsn_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200206};
207
Harald Welte9ae28a12010-08-27 09:26:44 +0200208static void print_help(void)
209{
210 printf("Some useful help...\n");
211 printf(" -h --help\tthis text\n");
Daniel Willmann03a2ed52019-07-26 11:56:31 +0200212 printf(" -V --version\tPrint the version\n");
Harald Welte9ae28a12010-08-27 09:26:44 +0200213 printf(" -D --daemonize\tFork the process into a background daemon\n");
214 printf(" -d option --debug\tenable Debugging\n");
215 printf(" -s --disable-color\n");
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100216 printf(" -c --config-file\tThe config file to use [%s]\n", CONFIG_FILE_DEFAULT);
Harald Welte9ae28a12010-08-27 09:26:44 +0200217 printf(" -e --log-level number\tSet a global log level\n");
218}
219
220static void handle_options(int argc, char **argv)
221{
222 while (1) {
223 int option_index = 0, c;
224 static struct option long_options[] = {
225 {"help", 0, 0, 'h'},
226 {"debug", 1, 0, 'd'},
227 {"daemonize", 0, 0, 'D'},
228 {"config-file", 1, 0, 'c'},
229 {"disable-color", 0, 0, 's'},
230 {"timestamp", 0, 0, 'T'},
Philipp Maier99c97072017-03-14 17:32:35 +0100231 { "version", 0, 0, 'V' },
Harald Welte9ae28a12010-08-27 09:26:44 +0200232 {"log-level", 1, 0, 'e'},
233 {NULL, 0, 0, 0}
234 };
235
Philipp Maier99c97072017-03-14 17:32:35 +0100236 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte9ae28a12010-08-27 09:26:44 +0200237 long_options, &option_index);
238 if (c == -1)
239 break;
240
241 switch (c) {
242 case 'h':
243 //print_usage();
244 print_help();
245 exit(0);
246 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200247 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200248 break;
249 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200250 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200251 break;
252 case 'D':
253 daemonize = 1;
254 break;
255 case 'c':
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100256 osmo_talloc_replace_string(sgsn, &sgsn->config_file, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200257 break;
258 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200259 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200260 break;
Philipp Maier99c97072017-03-14 17:32:35 +0100261 case 'V':
262 print_version(1);
263 exit(0);
264 break;
Harald Welte9ae28a12010-08-27 09:26:44 +0200265 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200266 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200267 break;
268 default:
269 /* ignore */
270 break;
271 }
272 }
273}
274
Harald Welte11461a62012-06-17 12:58:46 +0800275/* default categories */
276static struct log_info_cat gprs_categories[] = {
277 [DMM] = {
278 .name = "DMM",
279 .description = "Layer3 Mobility Management (MM)",
280 .color = "\033[1;33m",
281 .enabled = 1, .loglevel = LOGL_NOTICE,
282 },
Harald Welte11461a62012-06-17 12:58:46 +0800283 [DPAG] = {
284 .name = "DPAG",
285 .description = "Paging Subsystem",
286 .color = "\033[1;38m",
287 .enabled = 1, .loglevel = LOGL_NOTICE,
288 },
289 [DMEAS] = {
290 .name = "DMEAS",
291 .description = "Radio Measurement Processing",
292 .enabled = 0, .loglevel = LOGL_NOTICE,
293 },
294 [DREF] = {
295 .name = "DREF",
296 .description = "Reference Counting",
297 .enabled = 0, .loglevel = LOGL_NOTICE,
298 },
299 [DGPRS] = {
300 .name = "DGPRS",
301 .description = "GPRS Packet Service",
Philipp Maier56143542017-10-19 18:20:33 +0200302 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800303 },
304 [DNS] = {
305 .name = "DNS",
306 .description = "GPRS Network Service (NS)",
Philipp Maier56143542017-10-19 18:20:33 +0200307 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800308 },
309 [DBSSGP] = {
310 .name = "DBSSGP",
311 .description = "GPRS BSS Gateway Protocol (BSSGP)",
Philipp Maier56143542017-10-19 18:20:33 +0200312 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800313 },
314 [DLLC] = {
315 .name = "DLLC",
316 .description = "GPRS Logical Link Control Protocol (LLC)",
Philipp Maier56143542017-10-19 18:20:33 +0200317 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800318 },
319 [DSNDCP] = {
320 .name = "DSNDCP",
321 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200322 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800323 },
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200324 [DRANAP] = {
325 .name = "DRANAP",
326 .description = "RAN Application Part (RANAP)",
Philipp Maier56143542017-10-19 18:20:33 +0200327 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200328 },
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200329 [DSUA] = {
330 .name = "DSUA",
331 .description = "SCCP User Adaptation (SUA)",
Philipp Maier56143542017-10-19 18:20:33 +0200332 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200333 },
Philipp2c7f8372016-08-26 16:58:41 +0200334 [DSLHC] = {
335 .name = "DSLHC",
336 .description = "RFC1144 TCP/IP Header compression (SLHC)",
Philipp Maier56143542017-10-19 18:20:33 +0200337 .enabled = 1, .loglevel = LOGL_NOTICE,
Philipp2c7f8372016-08-26 16:58:41 +0200338 },
Philipp73f83d52016-09-02 13:38:01 +0200339 [DV42BIS] = {
340 .name = "DV42BIS",
341 .description = "V.42bis data compression (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200342 .enabled = 1, .loglevel = LOGL_NOTICE,
Philipp73f83d52016-09-02 13:38:01 +0200343 }
Harald Welte11461a62012-06-17 12:58:46 +0800344};
345
346static const struct log_info gprs_log_info = {
347 .filter_fn = gprs_log_filter_fn,
348 .cat = gprs_categories,
349 .num_cat = ARRAY_SIZE(gprs_categories),
350};
351
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200352#if BUILD_IU
353int sgsn_ranap_iu_event(struct ranap_ue_conn_ctx *ctx, enum ranap_iu_event_type type, void *data);
354#endif
Harald Welte11461a62012-06-17 12:58:46 +0800355
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100356static bool file_exists(const char *path)
357{
358 struct stat sb;
359 return stat(path, &sb) ? false : true;
360}
361
Harald Welte288be162010-05-01 16:48:27 +0200362int main(int argc, char **argv)
363{
Harald Welte288be162010-05-01 16:48:27 +0200364 int rc;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200365#if BUILD_IU
366 struct osmo_sccp_instance *sccp;
367#endif
Harald Welte288be162010-05-01 16:48:27 +0200368
Max4011e722016-07-05 15:19:12 +0200369 srand(time(NULL));
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100370 tall_sgsn_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100371 sgsn = sgsn_instance_alloc(tall_sgsn_ctx);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100372 msgb_talloc_ctx_init(tall_sgsn_ctx, 0);
373 vty_info.tall_ctx = tall_sgsn_ctx;
Harald Welte66f793a2010-05-13 11:50:04 +0200374
375 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200376 signal(SIGTERM, &signal_handler);
Harald Welte66f793a2010-05-13 11:50:04 +0200377 signal(SIGABRT, &signal_handler);
378 signal(SIGUSR1, &signal_handler);
379 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200380
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200381 osmo_init_ignore_signals();
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100382 osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info);
383 osmo_stats_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200384
Harald Weltec31f4802010-05-25 23:31:39 +0200385 vty_info.copyright = openbsc_copyright;
386 vty_init(&vty_info);
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200387 logging_vty_add_cmds();
Harald Welte731930b2018-02-14 01:02:22 +0100388 osmo_talloc_vty_add_cmds();
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200389 osmo_stats_vty_add_cmds();
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100390 sgsn_vty_init(&sgsn->cfg);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100391 ctrl_vty_init(tall_sgsn_ctx);
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200392
393#if BUILD_IU
Philipp Maierefe85d32017-04-09 12:32:51 +0200394 osmo_ss7_init();
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100395 osmo_ss7_vty_init_asp(tall_sgsn_ctx);
Neels Hofmeyr395c5582018-09-27 01:55:08 +0200396 osmo_sccp_vty_init();
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200397#endif
Harald Weltedcccb182010-05-16 20:52:23 +0200398
Harald Welte9ae28a12010-08-27 09:26:44 +0200399 handle_options(argc, argv);
400
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100401 /* Backwards compatibility: for years, the default config file name was
402 * osmo_sgsn.cfg. All other Osmocom programs use osmo-*.cfg with a
403 * dash. To be able to use the new config file name without breaking
404 * previous setups that might rely on the legacy default config file
405 * name, we need to look for the old config file if no -c option was
406 * passed AND no file exists with the new default file name. */
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100407 if (!sgsn->config_file) {
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100408 /* No -c option was passed */
409 if (file_exists(CONFIG_FILE_LEGACY)
410 && !file_exists(CONFIG_FILE_DEFAULT))
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100411 osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_LEGACY);
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100412 else
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100413 osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_DEFAULT);
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100414 }
415
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100416 rate_ctr_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200417
Harald Welte68d85d52012-06-16 17:45:59 +0800418 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800419 bssgp_set_log_ss(DBSSGP);
Harald Welte68d85d52012-06-16 17:45:59 +0800420
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100421 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200422 if (!sgsn_nsi) {
423 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
424 exit(1);
425 }
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100426 bssgp_nsi = sgsn->cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200427
428 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
Alexander Couzens14314bd2016-07-05 09:52:52 +0200429 sgsn_rate_ctr_init();
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100430 sgsn_inst_init(sgsn);
Harald Welte496aee42010-06-30 19:59:55 +0200431
Harald Welte66f793a2010-05-13 11:50:04 +0200432 gprs_ns_vty_init(bssgp_nsi);
Harald Weltece95b272012-06-17 13:04:02 +0800433 bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200434 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200435 gprs_sndcp_vty_init();
Max2e485762018-12-10 18:01:47 +0100436 sgsn_auth_init(sgsn);
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100437 sgsn_cdr_init(sgsn);
Harald Weltea6a20b42012-06-16 16:40:42 +0800438 /* FIXME: register signal handler for SS_L_NS */
Harald Welte66f793a2010-05-13 11:50:04 +0200439
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100440 rc = sgsn_parse_config(sgsn->config_file);
Harald Welte66f793a2010-05-13 11:50:04 +0200441 if (rc < 0) {
Neels Hofmeyr27355c92017-02-24 06:28:31 +0100442 LOGP(DGPRS, LOGL_FATAL, "Error in config file\n");
Harald Welte66f793a2010-05-13 11:50:04 +0200443 exit(2);
444 }
445
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100446 /* start telnet after reading config for vty_get_bind_addr() */
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100447 rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100448 vty_get_bind_addr(), OSMO_VTY_PORT_SGSN);
449 if (rc < 0)
450 exit(1);
451
Neels Hofmeyr73828152016-02-23 15:10:33 +0100452 /* start control interface after reading config for
453 * ctrl_vty_get_bind_addr() */
Stefan Sperling25996442018-12-11 14:35:43 +0100454 g_ctrlh = ctrl_interface_setup_dynip(NULL, ctrl_vty_get_bind_addr(),
455 OSMO_CTRL_PORT_SGSN, NULL);
Pau Espin Pedrol2e9ea502017-11-29 14:01:35 +0100456 if (!g_ctrlh) {
Neels Hofmeyr73828152016-02-23 15:10:33 +0100457 LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
458 exit(1);
459 }
460
461 if (sgsn_ctrl_cmds_install() != 0) {
462 LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
463 exit(1);
464 }
465
466
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100467 rc = sgsn_gtp_init(sgsn);
Harald Welte6efc1762010-05-19 15:46:31 +0200468 if (rc) {
469 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200470 exit(2);
Maxbaabc682017-10-20 13:39:57 +0200471 } else
472 LOGP(DGPRS, LOGL_NOTICE, "libGTP v%s initialized\n", gtp_version());
Harald Welte269ae752010-05-18 14:44:31 +0200473
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100474 rc = gprs_subscr_init(sgsn);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100475 if (rc < 0) {
476 LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n");
477 exit(2);
478 }
479
Harald Welteff3bde82010-05-19 15:09:09 +0200480 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200481 if (rc < 0) {
482 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200483 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200484 }
485
486 rc = gprs_ns_frgre_listen(sgsn_nsi);
487 if (rc < 0) {
488 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
489 "socket. Do you have CAP_NET_RAW?\n");
490 exit(2);
491 }
Harald Welte288be162010-05-01 16:48:27 +0200492
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800493 if (sgsn->cfg.dynamic_lookup) {
494 if (sgsn_ares_init(sgsn) != 0) {
495 LOGP(DGPRS, LOGL_FATAL,
496 "Failed to initialize c-ares(%d)\n", rc);
497 exit(4);
498 }
499 }
500
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200501#if BUILD_IU
Neels Hofmeyr1d047ed2017-11-24 04:33:08 +0100502 /* Note that these are mostly defaults and can be overriden from the VTY */
Pau Espin Pedrolf0f596f2019-08-19 17:54:16 +0200503 sccp = osmo_sccp_simple_client_on_ss7_id(tall_sgsn_ctx, 0, "OsmoSGSN",
504 (23 << 3) + 4,
505 OSMO_SS7_ASP_PROT_M3UA,
506 0, NULL,
507 0, "127.0.0.1");
Philipp Maierefe85d32017-04-09 12:32:51 +0200508 if (!sccp) {
509 printf("Setting up SCCP client failed.\n");
510 return 8;
511 }
512
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100513 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 +0200514#endif
515
Harald Welte9ae28a12010-08-27 09:26:44 +0200516 if (daemonize) {
517 rc = osmo_daemonize();
518 if (rc < 0) {
519 perror("Error during daemonize");
520 exit(1);
521 }
522 }
523
Harald Welte288be162010-05-01 16:48:27 +0200524 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200525 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200526 if (rc < 0)
527 exit(3);
528 }
529
Harald Weltebaf77002011-07-16 13:47:01 +0200530 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200531 exit(0);
532}