blob: 798e50797b85b54ea8ddd700c814f3424342b007 [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");
212 printf(" -D --daemonize\tFork the process into a background daemon\n");
213 printf(" -d option --debug\tenable Debugging\n");
214 printf(" -s --disable-color\n");
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100215 printf(" -c --config-file\tThe config file to use [%s]\n", CONFIG_FILE_DEFAULT);
Harald Welte9ae28a12010-08-27 09:26:44 +0200216 printf(" -e --log-level number\tSet a global log level\n");
217}
218
219static void handle_options(int argc, char **argv)
220{
221 while (1) {
222 int option_index = 0, c;
223 static struct option long_options[] = {
224 {"help", 0, 0, 'h'},
225 {"debug", 1, 0, 'd'},
226 {"daemonize", 0, 0, 'D'},
227 {"config-file", 1, 0, 'c'},
228 {"disable-color", 0, 0, 's'},
229 {"timestamp", 0, 0, 'T'},
Philipp Maier99c97072017-03-14 17:32:35 +0100230 { "version", 0, 0, 'V' },
Harald Welte9ae28a12010-08-27 09:26:44 +0200231 {"log-level", 1, 0, 'e'},
232 {NULL, 0, 0, 0}
233 };
234
Philipp Maier99c97072017-03-14 17:32:35 +0100235 c = getopt_long(argc, argv, "hd:Dc:sTVe:",
Harald Welte9ae28a12010-08-27 09:26:44 +0200236 long_options, &option_index);
237 if (c == -1)
238 break;
239
240 switch (c) {
241 case 'h':
242 //print_usage();
243 print_help();
244 exit(0);
245 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200246 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200247 break;
248 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200249 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200250 break;
251 case 'D':
252 daemonize = 1;
253 break;
254 case 'c':
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100255 osmo_talloc_replace_string(sgsn, &sgsn->config_file, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200256 break;
257 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200258 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200259 break;
Philipp Maier99c97072017-03-14 17:32:35 +0100260 case 'V':
261 print_version(1);
262 exit(0);
263 break;
Harald Welte9ae28a12010-08-27 09:26:44 +0200264 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200265 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200266 break;
267 default:
268 /* ignore */
269 break;
270 }
271 }
272}
273
Harald Welte11461a62012-06-17 12:58:46 +0800274/* default categories */
275static struct log_info_cat gprs_categories[] = {
276 [DMM] = {
277 .name = "DMM",
278 .description = "Layer3 Mobility Management (MM)",
279 .color = "\033[1;33m",
280 .enabled = 1, .loglevel = LOGL_NOTICE,
281 },
Harald Welte11461a62012-06-17 12:58:46 +0800282 [DPAG] = {
283 .name = "DPAG",
284 .description = "Paging Subsystem",
285 .color = "\033[1;38m",
286 .enabled = 1, .loglevel = LOGL_NOTICE,
287 },
288 [DMEAS] = {
289 .name = "DMEAS",
290 .description = "Radio Measurement Processing",
291 .enabled = 0, .loglevel = LOGL_NOTICE,
292 },
293 [DREF] = {
294 .name = "DREF",
295 .description = "Reference Counting",
296 .enabled = 0, .loglevel = LOGL_NOTICE,
297 },
298 [DGPRS] = {
299 .name = "DGPRS",
300 .description = "GPRS Packet Service",
Philipp Maier56143542017-10-19 18:20:33 +0200301 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800302 },
303 [DNS] = {
304 .name = "DNS",
305 .description = "GPRS Network Service (NS)",
Philipp Maier56143542017-10-19 18:20:33 +0200306 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800307 },
308 [DBSSGP] = {
309 .name = "DBSSGP",
310 .description = "GPRS BSS Gateway Protocol (BSSGP)",
Philipp Maier56143542017-10-19 18:20:33 +0200311 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800312 },
313 [DLLC] = {
314 .name = "DLLC",
315 .description = "GPRS Logical Link Control Protocol (LLC)",
Philipp Maier56143542017-10-19 18:20:33 +0200316 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800317 },
318 [DSNDCP] = {
319 .name = "DSNDCP",
320 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200321 .enabled = 1, .loglevel = LOGL_NOTICE,
Harald Welte11461a62012-06-17 12:58:46 +0800322 },
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200323 [DRANAP] = {
324 .name = "DRANAP",
325 .description = "RAN Application Part (RANAP)",
Philipp Maier56143542017-10-19 18:20:33 +0200326 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200327 },
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200328 [DSUA] = {
329 .name = "DSUA",
330 .description = "SCCP User Adaptation (SUA)",
Philipp Maier56143542017-10-19 18:20:33 +0200331 .enabled = 1, .loglevel = LOGL_NOTICE,
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200332 },
Philipp2c7f8372016-08-26 16:58:41 +0200333 [DSLHC] = {
334 .name = "DSLHC",
335 .description = "RFC1144 TCP/IP Header compression (SLHC)",
Philipp Maier56143542017-10-19 18:20:33 +0200336 .enabled = 1, .loglevel = LOGL_NOTICE,
Philipp2c7f8372016-08-26 16:58:41 +0200337 },
Philipp73f83d52016-09-02 13:38:01 +0200338 [DV42BIS] = {
339 .name = "DV42BIS",
340 .description = "V.42bis data compression (SNDCP)",
Philipp Maier56143542017-10-19 18:20:33 +0200341 .enabled = 1, .loglevel = LOGL_NOTICE,
Philipp73f83d52016-09-02 13:38:01 +0200342 }
Harald Welte11461a62012-06-17 12:58:46 +0800343};
344
345static const struct log_info gprs_log_info = {
346 .filter_fn = gprs_log_filter_fn,
347 .cat = gprs_categories,
348 .num_cat = ARRAY_SIZE(gprs_categories),
349};
350
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200351#if BUILD_IU
352int sgsn_ranap_iu_event(struct ranap_ue_conn_ctx *ctx, enum ranap_iu_event_type type, void *data);
353#endif
Harald Welte11461a62012-06-17 12:58:46 +0800354
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100355static bool file_exists(const char *path)
356{
357 struct stat sb;
358 return stat(path, &sb) ? false : true;
359}
360
Harald Welte288be162010-05-01 16:48:27 +0200361int main(int argc, char **argv)
362{
Harald Welte288be162010-05-01 16:48:27 +0200363 int rc;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200364#if BUILD_IU
365 struct osmo_sccp_instance *sccp;
366#endif
Harald Welte288be162010-05-01 16:48:27 +0200367
Max4011e722016-07-05 15:19:12 +0200368 srand(time(NULL));
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100369 tall_sgsn_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100370 sgsn = sgsn_instance_alloc(tall_sgsn_ctx);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100371 msgb_talloc_ctx_init(tall_sgsn_ctx, 0);
372 vty_info.tall_ctx = tall_sgsn_ctx;
Harald Welte66f793a2010-05-13 11:50:04 +0200373
374 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200375 signal(SIGTERM, &signal_handler);
Harald Welte66f793a2010-05-13 11:50:04 +0200376 signal(SIGABRT, &signal_handler);
377 signal(SIGUSR1, &signal_handler);
378 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200379
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200380 osmo_init_ignore_signals();
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100381 osmo_init_logging2(tall_sgsn_ctx, &gprs_log_info);
382 osmo_stats_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200383
Harald Weltec31f4802010-05-25 23:31:39 +0200384 vty_info.copyright = openbsc_copyright;
385 vty_init(&vty_info);
Maxdb0e3802017-01-12 19:35:11 +0100386 logging_vty_add_cmds(NULL);
Harald Welte731930b2018-02-14 01:02:22 +0100387 osmo_talloc_vty_add_cmds();
Jacob Erlbeck64630cc2015-10-26 16:25:37 +0100388 osmo_stats_vty_add_cmds(&gprs_log_info);
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100389 sgsn_vty_init(&sgsn->cfg);
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100390 ctrl_vty_init(tall_sgsn_ctx);
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200391
392#if BUILD_IU
Philipp Maierefe85d32017-04-09 12:32:51 +0200393 osmo_ss7_init();
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100394 osmo_ss7_vty_init_asp(tall_sgsn_ctx);
Neels Hofmeyr395c5582018-09-27 01:55:08 +0200395 osmo_sccp_vty_init();
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200396#endif
Harald Weltedcccb182010-05-16 20:52:23 +0200397
Harald Welte9ae28a12010-08-27 09:26:44 +0200398 handle_options(argc, argv);
399
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100400 /* Backwards compatibility: for years, the default config file name was
401 * osmo_sgsn.cfg. All other Osmocom programs use osmo-*.cfg with a
402 * dash. To be able to use the new config file name without breaking
403 * previous setups that might rely on the legacy default config file
404 * name, we need to look for the old config file if no -c option was
405 * passed AND no file exists with the new default file name. */
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100406 if (!sgsn->config_file) {
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100407 /* No -c option was passed */
408 if (file_exists(CONFIG_FILE_LEGACY)
409 && !file_exists(CONFIG_FILE_DEFAULT))
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100410 osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_LEGACY);
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100411 else
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100412 osmo_talloc_replace_string(sgsn, &sgsn->config_file, CONFIG_FILE_DEFAULT);
Neels Hofmeyrfea06092017-10-31 01:00:37 +0100413 }
414
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100415 rate_ctr_init(tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200416
Harald Welte68d85d52012-06-16 17:45:59 +0800417 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800418 bssgp_set_log_ss(DBSSGP);
Harald Welte68d85d52012-06-16 17:45:59 +0800419
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100420 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_sgsn_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200421 if (!sgsn_nsi) {
422 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
423 exit(1);
424 }
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100425 bssgp_nsi = sgsn->cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200426
427 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
Alexander Couzens14314bd2016-07-05 09:52:52 +0200428 sgsn_rate_ctr_init();
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100429 sgsn_inst_init(sgsn);
Harald Welte496aee42010-06-30 19:59:55 +0200430
Harald Welte66f793a2010-05-13 11:50:04 +0200431 gprs_ns_vty_init(bssgp_nsi);
Harald Weltece95b272012-06-17 13:04:02 +0800432 bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200433 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200434 gprs_sndcp_vty_init();
Max2e485762018-12-10 18:01:47 +0100435 sgsn_auth_init(sgsn);
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100436 sgsn_cdr_init(sgsn);
Harald Weltea6a20b42012-06-16 16:40:42 +0800437 /* FIXME: register signal handler for SS_L_NS */
Harald Welte66f793a2010-05-13 11:50:04 +0200438
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100439 rc = sgsn_parse_config(sgsn->config_file);
Harald Welte66f793a2010-05-13 11:50:04 +0200440 if (rc < 0) {
Neels Hofmeyr27355c92017-02-24 06:28:31 +0100441 LOGP(DGPRS, LOGL_FATAL, "Error in config file\n");
Harald Welte66f793a2010-05-13 11:50:04 +0200442 exit(2);
443 }
444
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100445 /* start telnet after reading config for vty_get_bind_addr() */
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100446 rc = telnet_init_dynif(tall_sgsn_ctx, NULL,
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100447 vty_get_bind_addr(), OSMO_VTY_PORT_SGSN);
448 if (rc < 0)
449 exit(1);
450
Neels Hofmeyr73828152016-02-23 15:10:33 +0100451 /* start control interface after reading config for
452 * ctrl_vty_get_bind_addr() */
Pau Espin Pedrol2e9ea502017-11-29 14:01:35 +0100453 g_ctrlh = sgsn_controlif_setup(NULL, ctrl_vty_get_bind_addr(),
Neels Hofmeyr73828152016-02-23 15:10:33 +0100454 OSMO_CTRL_PORT_SGSN);
Pau Espin Pedrol2e9ea502017-11-29 14:01:35 +0100455 if (!g_ctrlh) {
Neels Hofmeyr73828152016-02-23 15:10:33 +0100456 LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
457 exit(1);
458 }
459
460 if (sgsn_ctrl_cmds_install() != 0) {
461 LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
462 exit(1);
463 }
464
465
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100466 rc = sgsn_gtp_init(sgsn);
Harald Welte6efc1762010-05-19 15:46:31 +0200467 if (rc) {
468 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200469 exit(2);
Maxbaabc682017-10-20 13:39:57 +0200470 } else
471 LOGP(DGPRS, LOGL_NOTICE, "libGTP v%s initialized\n", gtp_version());
Harald Welte269ae752010-05-18 14:44:31 +0200472
Pau Espin Pedrol73b2bf32018-10-30 18:00:57 +0100473 rc = gprs_subscr_init(sgsn);
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100474 if (rc < 0) {
475 LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n");
476 exit(2);
477 }
478
Harald Welteff3bde82010-05-19 15:09:09 +0200479 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200480 if (rc < 0) {
481 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200482 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200483 }
484
485 rc = gprs_ns_frgre_listen(sgsn_nsi);
486 if (rc < 0) {
487 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
488 "socket. Do you have CAP_NET_RAW?\n");
489 exit(2);
490 }
Harald Welte288be162010-05-01 16:48:27 +0200491
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800492 if (sgsn->cfg.dynamic_lookup) {
493 if (sgsn_ares_init(sgsn) != 0) {
494 LOGP(DGPRS, LOGL_FATAL,
495 "Failed to initialize c-ares(%d)\n", rc);
496 exit(4);
497 }
498 }
499
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200500#if BUILD_IU
Neels Hofmeyr1d047ed2017-11-24 04:33:08 +0100501 /* Note that these are mostly defaults and can be overriden from the VTY */
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100502 sccp = osmo_sccp_simple_client(tall_sgsn_ctx, "OsmoSGSN",
Neels Hofmeyr1d047ed2017-11-24 04:33:08 +0100503 (23 << 3) + 4,
Neels Hofmeyr0010b7e2017-08-10 17:20:44 +0200504 OSMO_SS7_ASP_PROT_M3UA,
505 0, NULL,
506 0, "127.0.0.1");
Philipp Maierefe85d32017-04-09 12:32:51 +0200507 if (!sccp) {
508 printf("Setting up SCCP client failed.\n");
509 return 8;
510 }
511
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +0100512 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 +0200513#endif
514
Harald Welte9ae28a12010-08-27 09:26:44 +0200515 if (daemonize) {
516 rc = osmo_daemonize();
517 if (rc < 0) {
518 perror("Error during daemonize");
519 exit(1);
520 }
521 }
522
Harald Welte288be162010-05-01 16:48:27 +0200523 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200524 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200525 if (rc < 0)
526 exit(3);
527 }
528
Harald Weltebaf77002011-07-16 13:47:01 +0200529 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200530 exit(0);
531}