blob: 257c3a2fb1ec19116d97cb1b1cb04336cfb10f5b [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>
Harald Welte9d16b142020-06-26 15:55:21 +020045#include <osmocom/gprs/gprs_bssgp_bss.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080046
Harald Welte4b037e42010-05-19 19:45:32 +020047#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010048#include <osmocom/vty/logging.h>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010049#include <osmocom/vty/stats.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020050#include <osmocom/vty/ports.h>
Harald Welte731930b2018-02-14 01:02:22 +010051#include <osmocom/vty/misc.h>
Pau Espin Pedrolaae7daf2020-08-18 13:50:00 +020052#include <osmocom/vty/cpu_sched_vty.h>
Harald Welte4b037e42010-05-19 19:45:32 +020053
Neels Hofmeyr73828152016-02-23 15:10:33 +010054#include <osmocom/ctrl/control_vty.h>
55
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020056#include <osmocom/sgsn/signal.h>
57#include <osmocom/sgsn/debug.h>
58#include <osmocom/sgsn/vty.h>
59#include <osmocom/sgsn/sgsn.h>
60#include <osmocom/sgsn/gprs_llc.h>
61#include <osmocom/sgsn/gprs_gmm.h>
Pau Espin Pedrol6dfb5fe2019-08-29 17:21:00 +020062#include <osmocom/sgsn/gprs_ranap.h>
Daniel Willmann6292c8d2016-05-21 17:35:57 +020063
Harald Welteba874b82014-08-20 23:47:15 +020064#include <osmocom/ctrl/control_if.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020065#include <osmocom/ctrl/ports.h>
Harald Welte288be162010-05-01 16:48:27 +020066
Harald Welte2720e732010-05-17 00:44:57 +020067#include <gtp.h>
68
Harald Weltee2365962010-05-04 07:41:59 +020069#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020070
Neels Hofmeyra7a39472017-07-05 15:19:52 +020071#if BUILD_IU
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020072#include <osmocom/sigtran/osmo_ss7.h>
73#include <osmocom/sigtran/protocol/m3ua.h>
Neels Hofmeyra7a39472017-07-05 15:19:52 +020074#include <osmocom/ranap/iu_client.h>
75#endif
76
Harald Welte288be162010-05-01 16:48:27 +020077#define _GNU_SOURCE
78#include <getopt.h>
79
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010080void *tall_sgsn_ctx;
Pau Espin Pedrol2e9ea502017-11-29 14:01:35 +010081struct ctrl_handle *g_ctrlh;
Harald Welte288be162010-05-01 16:48:27 +020082
83struct gprs_ns_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020084static int daemonize = 0;
Harald Welte288be162010-05-01 16:48:27 +020085const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080086 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte69ca8f02015-10-29 10:35:24 +010087 "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 +080088 "This is free software: you are free to change and redistribute it.\r\n"
89 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte288be162010-05-01 16:48:27 +020090
Neels Hofmeyrfea06092017-10-31 01:00:37 +010091#define CONFIG_FILE_DEFAULT "osmo-sgsn.cfg"
92#define CONFIG_FILE_LEGACY "osmo_sgsn.cfg"
93
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +010094
95struct sgsn_instance *sgsn;
Harald Welte288be162010-05-01 16:48:27 +020096
97/* call-back function for the NS protocol */
98static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020099 struct msgb *msg, uint16_t bvci)
Harald Welte288be162010-05-01 16:48:27 +0200100{
101 int rc = 0;
102
103 switch (event) {
104 case GPRS_NS_EVT_UNIT_DATA:
105 /* hand the message into the BSSGP implementation */
Harald Weltece95b272012-06-17 13:04:02 +0800106 rc = bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +0200107 break;
108 default:
109 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
110 if (msg)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100111 msgb_free(msg);
Harald Welte288be162010-05-01 16:48:27 +0200112 rc = -EIO;
113 break;
114 }
115 return rc;
116}
117
Harald Welteb1fd9022012-06-17 12:16:31 +0800118/* call-back function for the BSSGP protocol */
119int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
120{
121 struct osmo_bssgp_prim *bp;
122 bp = container_of(oph, struct osmo_bssgp_prim, oph);
123
124 switch (oph->sap) {
125 case SAP_BSSGP_LL:
126 switch (oph->primitive) {
127 case PRIM_BSSGP_UL_UD:
128 return gprs_llc_rcvmsg(oph->msg, bp->tp);
129 }
130 break;
131 case SAP_BSSGP_GMM:
132 switch (oph->primitive) {
133 case PRIM_BSSGP_GMM_SUSPEND:
134 return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli);
135 case PRIM_BSSGP_GMM_RESUME:
136 return gprs_gmm_rx_resume(bp->ra_id, bp->tlli,
Holger Hans Peter Freyther9b5db282012-08-03 10:01:57 +0200137 bp->u.resume.suspend_ref);
Harald Welteb1fd9022012-06-17 12:16:31 +0800138 }
139 break;
140 case SAP_BSSGP_NM:
141 break;
142 }
143 return 0;
144}
145
Harald Welte66f793a2010-05-13 11:50:04 +0200146static void signal_handler(int signal)
147{
148 fprintf(stdout, "signal %u received\n", signal);
149
150 switch (signal) {
151 case SIGINT:
Harald Welte6f750e62017-08-20 20:50:06 +0200152 case SIGTERM:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200153 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200154 sleep(1);
155 exit(0);
156 break;
157 case SIGABRT:
158 /* in case of abort, we want to obtain a talloc report
159 * and then return to the caller, who will abort the process */
160 case SIGUSR1:
161 talloc_report(tall_vty_ctx, stderr);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100162 talloc_report_full(tall_sgsn_ctx, stderr);
Harald Welte66f793a2010-05-13 11:50:04 +0200163 break;
164 case SIGUSR2:
165 talloc_report_full(tall_vty_ctx, stderr);
166 break;
167 default:
168 break;
169 }
170}
171
Harald Welted6c74162010-05-02 21:29:17 +0200172/* NSI that BSSGP uses when transmitting on NS */
173extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte288be162010-05-01 16:48:27 +0200174
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200175int sgsn_vty_is_config_node(struct vty *vty, int node)
176{
177 /* So far the SGSN has no nested nodes that need parent node
178 * declaration, except for the ss7 vty nodes. */
179 switch (node) {
180 case SGSN_NODE:
181 return 1;
182 default:
183#if BUILD_IU
184 return osmo_ss7_is_config_node(vty, node);
185#else
186 return 0;
187#endif
188 }
189}
190
191int sgsn_vty_go_parent(struct vty *vty)
192{
193 /* So far the SGSN has no nested nodes that need parent node
194 * declaration, except for the ss7 vty nodes. */
195#if BUILD_IU
196 return osmo_ss7_vty_go_parent(vty);
197#else
198 vty->node = CONFIG_NODE;
199 vty->index = NULL;
200 return 0;
201#endif
202}
Harald Weltec31f4802010-05-25 23:31:39 +0200203
Harald Welte9d16b142020-06-26 15:55:21 +0200204static void bvc_reset_persistent_nsvcs(void)
205{
206 /* Send BVC-RESET on all persistent NSVCs */
207 struct gprs_nsvc *nsvc;
208
209 llist_for_each_entry(nsvc, &sgsn_nsi->gprs_nsvcs, list) {
210 struct bssgp_bvc_ctx bctx = {
211 .nsei = nsvc->nsei,
212 };
213 if (!nsvc->persistent)
214 continue;
215 /* if it is not marked ALIVE, we cannot send any data over it. */
216 nsvc->state |= NSE_S_ALIVE;
217 bssgp_tx_bvc_reset2(&bctx, BVCI_SIGNALLING, BSSGP_CAUSE_EQUIP_FAIL, false);
218 }
219}
220
Harald Weltec31f4802010-05-25 23:31:39 +0200221static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100222 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200223 .version = PACKAGE_VERSION,
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200224 .go_parent_cb = sgsn_vty_go_parent,
225 .is_config_node = sgsn_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200226};
227
Harald Welte9ae28a12010-08-27 09:26:44 +0200228static void print_help(void)
229{
230 printf("Some useful help...\n");
231 printf(" -h --help\tthis text\n");
Daniel Willmann03a2ed52019-07-26 11:56:31 +0200232 printf(" -V --version\tPrint the version\n");
Harald Welte9ae28a12010-08-27 09:26:44 +0200233 printf(" -D --daemonize\tFork the process into a background daemon\n");
234 printf(" -d option --debug\tenable Debugging\n");
235 printf(" -s --disable-color\n");
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100236 printf(" -c --config-file\tThe config file to use [%s]\n", CONFIG_FILE_DEFAULT);
Harald Welte9ae28a12010-08-27 09:26:44 +0200237 printf(" -e --log-level number\tSet a global log level\n");
238}
239
240static void handle_options(int argc, char **argv)
241{
242 while (1) {
243 int option_index = 0, c;
244 static struct option long_options[] = {
245 {"help", 0, 0, 'h'},
246 {"debug", 1, 0, 'd'},
247 {"daemonize", 0, 0, 'D'},
248 {"config-file", 1, 0, 'c'},
249 {"disable-color", 0, 0, 's'},
250 {"timestamp", 0, 0, 'T'},
Philipp Maier99c97072017-03-14 17:32:35 +0100251 { "version", 0, 0, 'V' },
Harald Welte9ae28a12010-08-27 09:26:44 +0200252 {"log-level", 1, 0, 'e'},
253 {NULL, 0, 0, 0}
254 };
255
Philipp Maier99c97072017-03-14 17:32:35 +0100256 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte9ae28a12010-08-27 09:26:44 +0200257 long_options, &option_index);
258 if (c == -1)
259 break;
260
261 switch (c) {
262 case 'h':
263 //print_usage();
264 print_help();
265 exit(0);
266 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200267 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200268 break;
269 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200270 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200271 break;
272 case 'D':
273 daemonize = 1;
274 break;
275 case 'c':
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100276 osmo_talloc_replace_string(sgsn, &sgsn->config_file, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200277 break;
278 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200279 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200280 break;
Philipp Maier99c97072017-03-14 17:32:35 +0100281 case 'V':
282 print_version(1);
283 exit(0);
284 break;
Harald Welte9ae28a12010-08-27 09:26:44 +0200285 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200286 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200287 break;
288 default:
289 /* ignore */
290 break;
291 }
292 }
Harald Weltede67c002019-12-03 22:28:19 +0100293
294 if (argc > optind) {
295 fprintf(stderr, "Unsupported positional arguments on command line\n");
296 exit(2);
297 }
Harald Welte9ae28a12010-08-27 09:26:44 +0200298}
299
Harald Welte11461a62012-06-17 12:58:46 +0800300/* default categories */
301static struct log_info_cat gprs_categories[] = {
302 [DMM] = {
303 .name = "DMM",
304 .description = "Layer3 Mobility Management (MM)",
305 .color = "\033[1;33m",
306 .enabled = 1, .loglevel = LOGL_NOTICE,
307 },
Harald Welte11461a62012-06-17 12:58:46 +0800308 [DPAG] = {
309 .name = "DPAG",
310 .description = "Paging Subsystem",
311 .color = "\033[1;38m",
312 .enabled = 1, .loglevel = LOGL_NOTICE,
313 },
314 [DMEAS] = {
315 .name = "DMEAS",
316 .description = "Radio Measurement Processing",
317 .enabled = 0, .loglevel = LOGL_NOTICE,
318 },
319 [DREF] = {
320 .name = "DREF",
321 .description = "Reference Counting",
322 .enabled = 0, .loglevel = LOGL_NOTICE,
323 },
324 [DGPRS] = {
325 .name = "DGPRS",
326 .description = "GPRS Packet Service",
Philipp Maier56143542017-10-19 18:20:33 +0200327 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800328 },
329 [DNS] = {
330 .name = "DNS",
331 .description = "GPRS Network Service (NS)",
Philipp Maier56143542017-10-19 18:20:33 +0200332 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800333 },
334 [DBSSGP] = {
335 .name = "DBSSGP",
336 .description = "GPRS BSS Gateway Protocol (BSSGP)",
Philipp Maier56143542017-10-19 18:20:33 +0200337 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800338 },
339 [DLLC] = {
340 .name = "DLLC",
341 .description = "GPRS Logical Link Control Protocol (LLC)",
Philipp Maier56143542017-10-19 18:20:33 +0200342 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800343 },
344 [DSNDCP] = {
345 .name = "DSNDCP",
346 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200347 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800348 },
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200349 [DRANAP] = {
350 .name = "DRANAP",
351 .description = "RAN Application Part (RANAP)",
Philipp Maier56143542017-10-19 18:20:33 +0200352 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200353 },
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200354 [DSUA] = {
355 .name = "DSUA",
356 .description = "SCCP User Adaptation (SUA)",
Philipp Maier56143542017-10-19 18:20:33 +0200357 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200358 },
Philipp2c7f8372016-08-26 16:58:41 +0200359 [DSLHC] = {
360 .name = "DSLHC",
361 .description = "RFC1144 TCP/IP Header compression (SLHC)",
Philipp Maier56143542017-10-19 18:20:33 +0200362 .enabled = 1, .loglevel = LOGL_NOTICE,
Philipp2c7f8372016-08-26 16:58:41 +0200363 },
Philipp73f83d52016-09-02 13:38:01 +0200364 [DV42BIS] = {
365 .name = "DV42BIS",
366 .description = "V.42bis data compression (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200367 .enabled = 1, .loglevel = LOGL_NOTICE,
Pau Espin Pedrol6ec5dc22019-08-28 16:52:56 +0200368 },
369 [DGTP] = {
370 .name = "DGTP",
371 .description = "GPRS Tunnelling Protocol (GTP)",
372 .enabled = 1, .loglevel = LOGL_NOTICE,
373 },
Harald Welte11461a62012-06-17 12:58:46 +0800374};
375
376static const struct log_info gprs_log_info = {
377 .filter_fn = gprs_log_filter_fn,
378 .cat = gprs_categories,
379 .num_cat = ARRAY_SIZE(gprs_categories),
380};
381
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100382static bool file_exists(const char *path)
383{
384 struct stat sb;
385 return stat(path, &sb) ? false : true;
386}
387
Harald Welte288be162010-05-01 16:48:27 +0200388int main(int argc, char **argv)
389{
Harald Welte288be162010-05-01 16:48:27 +0200390 int rc;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200391#if BUILD_IU
392 struct osmo_sccp_instance *sccp;
393#endif
Harald Welte288be162010-05-01 16:48:27 +0200394
Max4011e722016-07-05 15:19:12 +0200395 srand(time(NULL));
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100396 tall_sgsn_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100397 sgsn = sgsn_instance_alloc(tall_sgsn_ctx);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100398 msgb_talloc_ctx_init(tall_sgsn_ctx, 0);
399 vty_info.tall_ctx = tall_sgsn_ctx;
Harald Welte66f793a2010-05-13 11:50:04 +0200400
401 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200402 signal(SIGTERM, &signal_handler);
Harald Welte66f793a2010-05-13 11:50:04 +0200403 signal(SIGABRT, &signal_handler);
404 signal(SIGUSR1, &signal_handler);
405 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200406
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200407 osmo_init_ignore_signals();
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100408 osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info);
409 osmo_stats_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200410
Harald Weltec31f4802010-05-25 23:31:39 +0200411 vty_info.copyright = openbsc_copyright;
412 vty_init(&vty_info);
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200413 logging_vty_add_cmds();
Harald Welte731930b2018-02-14 01:02:22 +0100414 osmo_talloc_vty_add_cmds();
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200415 osmo_stats_vty_add_cmds();
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100416 sgsn_vty_init(&sgsn->cfg);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100417 ctrl_vty_init(tall_sgsn_ctx);
Pau Espin Pedrolaae7daf2020-08-18 13:50:00 +0200418 osmo_cpu_sched_vty_init(tall_sgsn_ctx);
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200419
420#if BUILD_IU
Harald Welte61557f42019-12-01 13:25:16 +0100421 OSMO_ASSERT(osmo_ss7_init() == 0);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100422 osmo_ss7_vty_init_asp(tall_sgsn_ctx);
Neels Hofmeyr395c5582018-09-27 01:55:08 +0200423 osmo_sccp_vty_init();
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200424#endif
Harald Weltedcccb182010-05-16 20:52:23 +0200425
Harald Welte9ae28a12010-08-27 09:26:44 +0200426 handle_options(argc, argv);
427
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100428 /* Backwards compatibility: for years, the default config file name was
429 * osmo_sgsn.cfg. All other Osmocom programs use osmo-*.cfg with a
430 * dash. To be able to use the new config file name without breaking
431 * previous setups that might rely on the legacy default config file
432 * name, we need to look for the old config file if no -c option was
433 * passed AND no file exists with the new default file name. */
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100434 if (!sgsn->config_file) {
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100435 /* No -c option was passed */
436 if (file_exists(CONFIG_FILE_LEGACY)
437 && !file_exists(CONFIG_FILE_DEFAULT))
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100438 osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_LEGACY);
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100439 else
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100440 osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_DEFAULT);
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100441 }
442
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100443 rate_ctr_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200444
Harald Welte68d85d52012-06-16 17:45:59 +0800445 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800446 bssgp_set_log_ss(DBSSGP);
Harald Welte68d85d52012-06-16 17:45:59 +0800447
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100448 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200449 if (!sgsn_nsi) {
450 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
451 exit(1);
452 }
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100453 bssgp_nsi = sgsn->cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200454
455 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
Alexander Couzens14314bd2016-07-05 09:52:52 +0200456 sgsn_rate_ctr_init();
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100457 sgsn_inst_init(sgsn);
Harald Welte496aee42010-06-30 19:59:55 +0200458
Harald Welte66f793a2010-05-13 11:50:04 +0200459 gprs_ns_vty_init(bssgp_nsi);
Harald Weltece95b272012-06-17 13:04:02 +0800460 bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200461 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200462 gprs_sndcp_vty_init();
Max2e485762018-12-10 18:01:47 +0100463 sgsn_auth_init(sgsn);
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100464 sgsn_cdr_init(sgsn);
Harald Weltea6a20b42012-06-16 16:40:42 +0800465 /* FIXME: register signal handler for SS_L_NS */
Harald Welte66f793a2010-05-13 11:50:04 +0200466
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100467 rc = sgsn_parse_config(sgsn->config_file);
Harald Welte66f793a2010-05-13 11:50:04 +0200468 if (rc < 0) {
Neels Hofmeyr27355c92017-02-24 06:28:31 +0100469 LOGP(DGPRS, LOGL_FATAL, "Error in config file\n");
Harald Welte66f793a2010-05-13 11:50:04 +0200470 exit(2);
471 }
472
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100473 /* start telnet after reading config for vty_get_bind_addr() */
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100474 rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100475 vty_get_bind_addr(), OSMO_VTY_PORT_SGSN);
476 if (rc < 0)
477 exit(1);
478
Neels Hofmeyr73828152016-02-23 15:10:33 +0100479 /* start control interface after reading config for
480 * ctrl_vty_get_bind_addr() */
Stefan Sperling25996442018-12-11 14:35:43 +0100481 g_ctrlh = ctrl_interface_setup_dynip(NULL, ctrl_vty_get_bind_addr(),
482 OSMO_CTRL_PORT_SGSN, NULL);
Pau Espin Pedrol2e9ea502017-11-29 14:01:35 +0100483 if (!g_ctrlh) {
Neels Hofmeyr73828152016-02-23 15:10:33 +0100484 LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
485 exit(1);
486 }
487
488 if (sgsn_ctrl_cmds_install() != 0) {
489 LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
490 exit(1);
491 }
492
493
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100494 rc = sgsn_gtp_init(sgsn);
Harald Welte6efc1762010-05-19 15:46:31 +0200495 if (rc) {
496 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200497 exit(2);
Maxbaabc682017-10-20 13:39:57 +0200498 } else
499 LOGP(DGPRS, LOGL_NOTICE, "libGTP v%s initialized\n", gtp_version());
Harald Welte269ae752010-05-18 14:44:31 +0200500
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100501 rc = gprs_subscr_init(sgsn);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100502 if (rc < 0) {
503 LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n");
504 exit(2);
505 }
506
Harald Welteff3bde82010-05-19 15:09:09 +0200507 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200508 if (rc < 0) {
509 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200510 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200511 }
512
513 rc = gprs_ns_frgre_listen(sgsn_nsi);
514 if (rc < 0) {
515 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
516 "socket. Do you have CAP_NET_RAW?\n");
517 exit(2);
518 }
Harald Welte288be162010-05-01 16:48:27 +0200519
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800520 if (sgsn->cfg.dynamic_lookup) {
521 if (sgsn_ares_init(sgsn) != 0) {
522 LOGP(DGPRS, LOGL_FATAL,
523 "Failed to initialize c-ares(%d)\n", rc);
524 exit(4);
525 }
526 }
527
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200528#if BUILD_IU
Neels Hofmeyr1d047ed2017-11-24 04:33:08 +0100529 /* Note that these are mostly defaults and can be overriden from the VTY */
Pau Espin Pedrol2c908992019-08-19 19:06:06 +0200530 sccp = osmo_sccp_simple_client_on_ss7_id(tall_sgsn_ctx,
531 sgsn->cfg.iu.cs7_instance,
532 "OsmoSGSN",
Pau Espin Pedrolf0f596f2019-08-19 17:54:16 +0200533 (23 << 3) + 4,
534 OSMO_SS7_ASP_PROT_M3UA,
535 0, NULL,
536 0, "127.0.0.1");
Philipp Maierefe85d32017-04-09 12:32:51 +0200537 if (!sccp) {
538 printf("Setting up SCCP client failed.\n");
539 return 8;
540 }
541
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100542 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 +0200543#endif
544
Harald Welte9d16b142020-06-26 15:55:21 +0200545 bvc_reset_persistent_nsvcs();
546
Harald Welte9ae28a12010-08-27 09:26:44 +0200547 if (daemonize) {
548 rc = osmo_daemonize();
549 if (rc < 0) {
550 perror("Error during daemonize");
551 exit(1);
552 }
553 }
554
Harald Welte288be162010-05-01 16:48:27 +0200555 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200556 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200557 if (rc < 0)
558 exit(3);
559 }
560
Harald Weltebaf77002011-07-16 13:47:01 +0200561 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200562 exit(0);
563}