blob: c852840db6b8ef424e125d75f6bad4bf101d311f [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>
24#include <stdlib.h>
25#include <string.h>
26#include <getopt.h>
27#include <errno.h>
Harald Welte66f793a2010-05-13 11:50:04 +020028#include <signal.h>
Harald Welte288be162010-05-01 16:48:27 +020029#include <sys/fcntl.h>
30#include <sys/stat.h>
Harald Welte288be162010-05-01 16:48:27 +020031#include <sys/socket.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020035#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010036#include <osmocom/core/talloc.h>
37#include <osmocom/core/select.h>
38#include <osmocom/core/rate_ctr.h>
39#include <osmocom/core/logging.h>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010040#include <osmocom/core/stats.h>
Harald Welte288be162010-05-01 16:48:27 +020041
Harald Welteea34a4e2012-06-16 14:59:56 +080042#include <osmocom/gprs/gprs_ns.h>
43#include <osmocom/gprs/gprs_bssgp.h>
44
Harald Welte4b037e42010-05-19 19:45:32 +020045#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010046#include <osmocom/vty/logging.h>
Jacob Erlbeck64630cc2015-10-26 16:25:37 +010047#include <osmocom/vty/stats.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020048#include <osmocom/vty/ports.h>
Harald Welte4b037e42010-05-19 19:45:32 +020049
Neels Hofmeyr73828152016-02-23 15:10:33 +010050#include <osmocom/ctrl/control_vty.h>
51
Harald Welte288be162010-05-01 16:48:27 +020052#include <openbsc/signal.h>
53#include <openbsc/debug.h>
Harald Welte288be162010-05-01 16:48:27 +020054#include <openbsc/vty.h>
55#include <openbsc/sgsn.h>
Harald Welte2e918a82010-05-18 12:20:12 +020056#include <openbsc/gprs_llc.h>
Harald Welteb1fd9022012-06-17 12:16:31 +080057#include <openbsc/gprs_gmm.h>
Harald Welteba874b82014-08-20 23:47:15 +020058#include <osmocom/ctrl/control_if.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020059#include <osmocom/ctrl/ports.h>
Harald Welte288be162010-05-01 16:48:27 +020060
Harald Welte2720e732010-05-17 00:44:57 +020061#include <gtp.h>
62
Harald Weltee2365962010-05-04 07:41:59 +020063#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020064
Harald Welte288be162010-05-01 16:48:27 +020065#define _GNU_SOURCE
66#include <getopt.h>
67
68void *tall_bsc_ctx;
69
70struct gprs_ns_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020071static int daemonize = 0;
Harald Welte288be162010-05-01 16:48:27 +020072const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080073 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte69ca8f02015-10-29 10:35:24 +010074 "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 +080075 "This is free software: you are free to change and redistribute it.\r\n"
76 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte288be162010-05-01 16:48:27 +020077
Harald Welte8fc1a462010-05-17 00:53:10 +020078static struct sgsn_instance sgsn_inst = {
Harald Welte2720e732010-05-17 00:44:57 +020079 .config_file = "osmo_sgsn.cfg",
80 .cfg = {
81 .gtp_statedir = "./",
Jacob Erlbeck106f5472014-11-04 10:08:37 +010082 .auth_policy = SGSN_AUTH_POLICY_CLOSED,
Harald Welte2720e732010-05-17 00:44:57 +020083 },
84};
Harald Welte8fc1a462010-05-17 00:53:10 +020085struct sgsn_instance *sgsn = &sgsn_inst;
Harald Welte288be162010-05-01 16:48:27 +020086
87/* call-back function for the NS protocol */
88static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020089 struct msgb *msg, uint16_t bvci)
Harald Welte288be162010-05-01 16:48:27 +020090{
91 int rc = 0;
92
93 switch (event) {
94 case GPRS_NS_EVT_UNIT_DATA:
95 /* hand the message into the BSSGP implementation */
Harald Weltece95b272012-06-17 13:04:02 +080096 rc = bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +020097 break;
98 default:
99 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
100 if (msg)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100101 msgb_free(msg);
Harald Welte288be162010-05-01 16:48:27 +0200102 rc = -EIO;
103 break;
104 }
105 return rc;
106}
107
Harald Welteb1fd9022012-06-17 12:16:31 +0800108/* call-back function for the BSSGP protocol */
109int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
110{
111 struct osmo_bssgp_prim *bp;
112 bp = container_of(oph, struct osmo_bssgp_prim, oph);
113
114 switch (oph->sap) {
115 case SAP_BSSGP_LL:
116 switch (oph->primitive) {
117 case PRIM_BSSGP_UL_UD:
118 return gprs_llc_rcvmsg(oph->msg, bp->tp);
119 }
120 break;
121 case SAP_BSSGP_GMM:
122 switch (oph->primitive) {
123 case PRIM_BSSGP_GMM_SUSPEND:
124 return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli);
125 case PRIM_BSSGP_GMM_RESUME:
126 return gprs_gmm_rx_resume(bp->ra_id, bp->tlli,
Holger Hans Peter Freyther9b5db282012-08-03 10:01:57 +0200127 bp->u.resume.suspend_ref);
Harald Welteb1fd9022012-06-17 12:16:31 +0800128 }
129 break;
130 case SAP_BSSGP_NM:
131 break;
132 }
133 return 0;
134}
135
Harald Welte66f793a2010-05-13 11:50:04 +0200136static void signal_handler(int signal)
137{
138 fprintf(stdout, "signal %u received\n", signal);
139
140 switch (signal) {
141 case SIGINT:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200142 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200143 sleep(1);
144 exit(0);
145 break;
146 case SIGABRT:
147 /* in case of abort, we want to obtain a talloc report
148 * and then return to the caller, who will abort the process */
149 case SIGUSR1:
150 talloc_report(tall_vty_ctx, stderr);
151 talloc_report_full(tall_bsc_ctx, stderr);
152 break;
153 case SIGUSR2:
154 talloc_report_full(tall_vty_ctx, stderr);
155 break;
156 default:
157 break;
158 }
159}
160
Harald Welted6c74162010-05-02 21:29:17 +0200161/* NSI that BSSGP uses when transmitting on NS */
162extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200163extern void *tall_msgb_ctx;
Harald Welte288be162010-05-01 16:48:27 +0200164
Holger Hans Peter Freyther3e4e8cf2015-09-24 15:38:38 +0200165extern int bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200166
167static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100168 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200169 .version = PACKAGE_VERSION,
170 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +0800171 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200172};
173
Harald Welte9ae28a12010-08-27 09:26:44 +0200174static void print_help(void)
175{
176 printf("Some useful help...\n");
177 printf(" -h --help\tthis text\n");
178 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 Hofmeyr130be402015-09-24 17:28:47 +0200181 printf(" -c --config-file\tThe config file to use [%s]\n", sgsn->config_file);
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'},
196 {"log-level", 1, 0, 'e'},
197 {NULL, 0, 0, 0}
198 };
199
200 c = getopt_long(argc, argv, "hd:Dc:sTe:",
201 long_options, &option_index);
202 if (c == -1)
203 break;
204
205 switch (c) {
206 case 'h':
207 //print_usage();
208 print_help();
209 exit(0);
210 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200211 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200212 break;
213 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200214 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200215 break;
216 case 'D':
217 daemonize = 1;
218 break;
219 case 'c':
220 sgsn_inst.config_file = strdup(optarg);
221 break;
222 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200223 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200224 break;
225 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200226 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200227 break;
228 default:
229 /* ignore */
230 break;
231 }
232 }
233}
234
Harald Welte11461a62012-06-17 12:58:46 +0800235/* default categories */
236static struct log_info_cat gprs_categories[] = {
237 [DMM] = {
238 .name = "DMM",
239 .description = "Layer3 Mobility Management (MM)",
240 .color = "\033[1;33m",
241 .enabled = 1, .loglevel = LOGL_NOTICE,
242 },
Harald Welte11461a62012-06-17 12:58:46 +0800243 [DPAG] = {
244 .name = "DPAG",
245 .description = "Paging Subsystem",
246 .color = "\033[1;38m",
247 .enabled = 1, .loglevel = LOGL_NOTICE,
248 },
249 [DMEAS] = {
250 .name = "DMEAS",
251 .description = "Radio Measurement Processing",
252 .enabled = 0, .loglevel = LOGL_NOTICE,
253 },
254 [DREF] = {
255 .name = "DREF",
256 .description = "Reference Counting",
257 .enabled = 0, .loglevel = LOGL_NOTICE,
258 },
259 [DGPRS] = {
260 .name = "DGPRS",
261 .description = "GPRS Packet Service",
262 .enabled = 1, .loglevel = LOGL_DEBUG,
263 },
264 [DNS] = {
265 .name = "DNS",
266 .description = "GPRS Network Service (NS)",
267 .enabled = 1, .loglevel = LOGL_INFO,
268 },
269 [DBSSGP] = {
270 .name = "DBSSGP",
271 .description = "GPRS BSS Gateway Protocol (BSSGP)",
272 .enabled = 1, .loglevel = LOGL_DEBUG,
273 },
274 [DLLC] = {
275 .name = "DLLC",
276 .description = "GPRS Logical Link Control Protocol (LLC)",
277 .enabled = 1, .loglevel = LOGL_DEBUG,
278 },
279 [DSNDCP] = {
280 .name = "DSNDCP",
281 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
282 .enabled = 1, .loglevel = LOGL_DEBUG,
283 },
Daniel Willmann35a65ed2016-05-20 21:59:45 +0200284 [DRANAP] = {
285 .name = "DRANAP",
286 .description = "RAN Application Part (RANAP)",
287 .enabled = 1, .loglevel = LOGL_DEBUG,
288 },
Daniel Willmann3adb23c2016-05-20 21:59:35 +0200289 [DSUA] = {
290 .name = "DSUA",
291 .description = "SCCP User Adaptation (SUA)",
292 .enabled = 1, .loglevel = LOGL_DEBUG,
293 },
Harald Welte11461a62012-06-17 12:58:46 +0800294};
295
296static const struct log_info gprs_log_info = {
297 .filter_fn = gprs_log_filter_fn,
298 .cat = gprs_categories,
299 .num_cat = ARRAY_SIZE(gprs_categories),
300};
301
302
Harald Welte288be162010-05-01 16:48:27 +0200303int main(int argc, char **argv)
304{
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +0100305 struct ctrl_handle *ctrl;
Harald Welte288be162010-05-01 16:48:27 +0200306 struct gsm_network dummy_network;
Harald Welte288be162010-05-01 16:48:27 +0200307 int rc;
308
309 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200310 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
311
312 signal(SIGINT, &signal_handler);
313 signal(SIGABRT, &signal_handler);
314 signal(SIGUSR1, &signal_handler);
315 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200316
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200317 osmo_init_ignore_signals();
Harald Welte11461a62012-06-17 12:58:46 +0800318 osmo_init_logging(&gprs_log_info);
Jacob Erlbeck64630cc2015-10-26 16:25:37 +0100319 osmo_stats_init(tall_bsc_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200320
Harald Weltec31f4802010-05-25 23:31:39 +0200321 vty_info.copyright = openbsc_copyright;
322 vty_init(&vty_info);
Harald Welte11461a62012-06-17 12:58:46 +0800323 logging_vty_add_cmds(&gprs_log_info);
Jacob Erlbeck64630cc2015-10-26 16:25:37 +0100324 osmo_stats_vty_add_cmds(&gprs_log_info);
325 sgsn_vty_init();
Neels Hofmeyr73828152016-02-23 15:10:33 +0100326 ctrl_vty_init(tall_bsc_ctx);
Harald Weltedcccb182010-05-16 20:52:23 +0200327
Harald Welte9ae28a12010-08-27 09:26:44 +0200328 handle_options(argc, argv);
329
Harald Welte66f793a2010-05-13 11:50:04 +0200330 rate_ctr_init(tall_bsc_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200331
Harald Welte68d85d52012-06-16 17:45:59 +0800332 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800333 bssgp_set_log_ss(DBSSGP);
Harald Welte68d85d52012-06-16 17:45:59 +0800334
Harald Weltefdd8b3b2012-06-16 16:54:06 +0800335 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_bsc_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200336 if (!sgsn_nsi) {
337 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
338 exit(1);
339 }
Harald Welte2720e732010-05-17 00:44:57 +0200340 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200341
342 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100343 sgsn_inst_init();
Harald Welte496aee42010-06-30 19:59:55 +0200344
Harald Welte66f793a2010-05-13 11:50:04 +0200345 gprs_ns_vty_init(bssgp_nsi);
Harald Weltece95b272012-06-17 13:04:02 +0800346 bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200347 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200348 gprs_sndcp_vty_init();
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100349 sgsn_auth_init();
Holger Hans Peter Freyther4f5b8232015-05-05 22:25:48 +0200350 sgsn_cdr_init(&sgsn_inst);
Harald Weltea6a20b42012-06-16 16:40:42 +0800351 /* FIXME: register signal handler for SS_L_NS */
Harald Welte66f793a2010-05-13 11:50:04 +0200352
Harald Welte2720e732010-05-17 00:44:57 +0200353 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200354 if (rc < 0) {
355 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
356 exit(2);
357 }
358
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100359 /* start telnet after reading config for vty_get_bind_addr() */
360 LOGP(DGPRS, LOGL_NOTICE, "VTY at %s %d\n",
361 vty_get_bind_addr(), OSMO_VTY_PORT_SGSN);
362 rc = telnet_init_dynif(tall_bsc_ctx, &dummy_network,
363 vty_get_bind_addr(), OSMO_VTY_PORT_SGSN);
364 if (rc < 0)
365 exit(1);
366
Neels Hofmeyr73828152016-02-23 15:10:33 +0100367 /* start control interface after reading config for
368 * ctrl_vty_get_bind_addr() */
369 LOGP(DGPRS, LOGL_NOTICE, "CTRL at %s %d\n",
370 ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_SGSN);
371 ctrl = sgsn_controlif_setup(NULL, ctrl_vty_get_bind_addr(),
372 OSMO_CTRL_PORT_SGSN);
373 if (!ctrl) {
374 LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
375 exit(1);
376 }
377
378 if (sgsn_ctrl_cmds_install() != 0) {
379 LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
380 exit(1);
381 }
382
383
Harald Welte2720e732010-05-17 00:44:57 +0200384 rc = sgsn_gtp_init(&sgsn_inst);
Harald Welte6efc1762010-05-19 15:46:31 +0200385 if (rc) {
386 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200387 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200388 }
Harald Welte269ae752010-05-18 14:44:31 +0200389
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100390 rc = gprs_subscr_init(&sgsn_inst);
391 if (rc < 0) {
392 LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n");
393 exit(2);
394 }
395
Harald Welteff3bde82010-05-19 15:09:09 +0200396 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200397 if (rc < 0) {
398 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200399 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200400 }
401
402 rc = gprs_ns_frgre_listen(sgsn_nsi);
403 if (rc < 0) {
404 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
405 "socket. Do you have CAP_NET_RAW?\n");
406 exit(2);
407 }
Harald Welte288be162010-05-01 16:48:27 +0200408
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800409 if (sgsn->cfg.dynamic_lookup) {
410 if (sgsn_ares_init(sgsn) != 0) {
411 LOGP(DGPRS, LOGL_FATAL,
412 "Failed to initialize c-ares(%d)\n", rc);
413 exit(4);
414 }
415 }
416
Harald Welte9ae28a12010-08-27 09:26:44 +0200417 if (daemonize) {
418 rc = osmo_daemonize();
419 if (rc < 0) {
420 perror("Error during daemonize");
421 exit(1);
422 }
423 }
424
Harald Welte288be162010-05-01 16:48:27 +0200425 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200426 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200427 if (rc < 0)
428 exit(3);
429 }
430
Harald Weltebaf77002011-07-16 13:47:01 +0200431 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200432 exit(0);
433}