blob: f7eb2214dbefd83e97928cbf3d9e5c4df97ba5e1 [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>
Harald Welte288be162010-05-01 16:48:27 +020040
Harald Welte4b037e42010-05-19 19:45:32 +020041#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010042#include <osmocom/vty/logging.h>
Harald Welte4b037e42010-05-19 19:45:32 +020043
Harald Welte288be162010-05-01 16:48:27 +020044#include <openbsc/signal.h>
45#include <openbsc/debug.h>
Harald Welte288be162010-05-01 16:48:27 +020046#include <openbsc/vty.h>
47#include <openbsc/sgsn.h>
48#include <openbsc/gprs_ns.h>
49#include <openbsc/gprs_bssgp.h>
Harald Welte2e918a82010-05-18 12:20:12 +020050#include <openbsc/gprs_llc.h>
Harald Welte288be162010-05-01 16:48:27 +020051
Harald Welte2720e732010-05-17 00:44:57 +020052#include <gtp.h>
53
Harald Weltee2365962010-05-04 07:41:59 +020054#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020055
56/* this is here for the vty... it will never be called */
57void subscr_put() { abort(); }
58
59#define _GNU_SOURCE
60#include <getopt.h>
61
62void *tall_bsc_ctx;
63
64struct gprs_ns_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020065static int daemonize = 0;
Harald Welte288be162010-05-01 16:48:27 +020066const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080067 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010068 "License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080069 "This is free software: you are free to change and redistribute it.\r\n"
70 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte288be162010-05-01 16:48:27 +020071
Harald Welte8fc1a462010-05-17 00:53:10 +020072static struct sgsn_instance sgsn_inst = {
Harald Welte2720e732010-05-17 00:44:57 +020073 .config_file = "osmo_sgsn.cfg",
74 .cfg = {
75 .gtp_statedir = "./",
76 },
77};
Harald Welte8fc1a462010-05-17 00:53:10 +020078struct sgsn_instance *sgsn = &sgsn_inst;
Harald Welte288be162010-05-01 16:48:27 +020079
80/* call-back function for the NS protocol */
81static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020082 struct msgb *msg, uint16_t bvci)
Harald Welte288be162010-05-01 16:48:27 +020083{
84 int rc = 0;
85
86 switch (event) {
87 case GPRS_NS_EVT_UNIT_DATA:
88 /* hand the message into the BSSGP implementation */
Harald Weltee6afd602010-05-02 11:19:37 +020089 rc = gprs_bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +020090 break;
91 default:
92 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
93 if (msg)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +010094 msgb_free(msg);
Harald Welte288be162010-05-01 16:48:27 +020095 rc = -EIO;
96 break;
97 }
98 return rc;
99}
100
Harald Welte66f793a2010-05-13 11:50:04 +0200101static void signal_handler(int signal)
102{
103 fprintf(stdout, "signal %u received\n", signal);
104
105 switch (signal) {
106 case SIGINT:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200107 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200108 sleep(1);
109 exit(0);
110 break;
111 case SIGABRT:
112 /* in case of abort, we want to obtain a talloc report
113 * and then return to the caller, who will abort the process */
114 case SIGUSR1:
115 talloc_report(tall_vty_ctx, stderr);
116 talloc_report_full(tall_bsc_ctx, stderr);
117 break;
118 case SIGUSR2:
119 talloc_report_full(tall_vty_ctx, stderr);
120 break;
121 default:
122 break;
123 }
124}
125
Harald Welted6c74162010-05-02 21:29:17 +0200126/* NSI that BSSGP uses when transmitting on NS */
127extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200128extern void *tall_msgb_ctx;
Harald Welte288be162010-05-01 16:48:27 +0200129
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +0800130extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200131
132static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100133 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200134 .version = PACKAGE_VERSION,
135 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +0800136 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200137};
138
Harald Welte9ae28a12010-08-27 09:26:44 +0200139static void print_help(void)
140{
141 printf("Some useful help...\n");
142 printf(" -h --help\tthis text\n");
143 printf(" -D --daemonize\tFork the process into a background daemon\n");
144 printf(" -d option --debug\tenable Debugging\n");
145 printf(" -s --disable-color\n");
146 printf(" -c --config-file\tThe config file to use\n");
147 printf(" -e --log-level number\tSet a global log level\n");
148}
149
150static void handle_options(int argc, char **argv)
151{
152 while (1) {
153 int option_index = 0, c;
154 static struct option long_options[] = {
155 {"help", 0, 0, 'h'},
156 {"debug", 1, 0, 'd'},
157 {"daemonize", 0, 0, 'D'},
158 {"config-file", 1, 0, 'c'},
159 {"disable-color", 0, 0, 's'},
160 {"timestamp", 0, 0, 'T'},
161 {"log-level", 1, 0, 'e'},
162 {NULL, 0, 0, 0}
163 };
164
165 c = getopt_long(argc, argv, "hd:Dc:sTe:",
166 long_options, &option_index);
167 if (c == -1)
168 break;
169
170 switch (c) {
171 case 'h':
172 //print_usage();
173 print_help();
174 exit(0);
175 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200176 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200177 break;
178 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200179 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200180 break;
181 case 'D':
182 daemonize = 1;
183 break;
184 case 'c':
185 sgsn_inst.config_file = strdup(optarg);
186 break;
187 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200188 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200189 break;
190 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200191 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200192 break;
193 default:
194 /* ignore */
195 break;
196 }
197 }
198}
199
Harald Welte288be162010-05-01 16:48:27 +0200200int main(int argc, char **argv)
201{
202 struct gsm_network dummy_network;
Harald Welte288be162010-05-01 16:48:27 +0200203 int rc;
204
205 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200206 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
207
208 signal(SIGINT, &signal_handler);
209 signal(SIGABRT, &signal_handler);
210 signal(SIGUSR1, &signal_handler);
211 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200212
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200213 osmo_init_ignore_signals();
214 osmo_init_logging(&log_info);
Harald Welte288be162010-05-01 16:48:27 +0200215
Harald Weltec31f4802010-05-25 23:31:39 +0200216 vty_info.copyright = openbsc_copyright;
217 vty_init(&vty_info);
Pablo Neira Ayuso38487702011-03-23 08:49:00 +0100218 logging_vty_add_cmds(&log_info);
Harald Weltedcccb182010-05-16 20:52:23 +0200219 sgsn_vty_init();
220
Harald Welte9ae28a12010-08-27 09:26:44 +0200221 handle_options(argc, argv);
222
Harald Welte66f793a2010-05-13 11:50:04 +0200223 rate_ctr_init(tall_bsc_ctx);
Harald Weltedcccb182010-05-16 20:52:23 +0200224 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245);
225 if (rc < 0)
226 exit(1);
Harald Welte288be162010-05-01 16:48:27 +0200227
228 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb);
229 if (!sgsn_nsi) {
230 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
231 exit(1);
232 }
Harald Welte2720e732010-05-17 00:44:57 +0200233 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200234
235 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
236
Harald Welte66f793a2010-05-13 11:50:04 +0200237 gprs_ns_vty_init(bssgp_nsi);
Harald Welted2a9ed22010-05-18 08:02:36 +0200238 gprs_bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200239 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200240 gprs_sndcp_vty_init();
Harald Welte66f793a2010-05-13 11:50:04 +0200241 /* FIXME: register signal handler for SS_NS */
242
Harald Welte2720e732010-05-17 00:44:57 +0200243 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200244 if (rc < 0) {
245 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
246 exit(2);
247 }
248
Harald Welte2720e732010-05-17 00:44:57 +0200249 rc = sgsn_gtp_init(&sgsn_inst);
Harald Welte6efc1762010-05-19 15:46:31 +0200250 if (rc) {
251 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200252 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200253 }
Harald Welte269ae752010-05-18 14:44:31 +0200254
Harald Welteff3bde82010-05-19 15:09:09 +0200255 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200256 if (rc < 0) {
257 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200258 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200259 }
260
261 rc = gprs_ns_frgre_listen(sgsn_nsi);
262 if (rc < 0) {
263 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
264 "socket. Do you have CAP_NET_RAW?\n");
265 exit(2);
266 }
Harald Welte288be162010-05-01 16:48:27 +0200267
Harald Welte9ae28a12010-08-27 09:26:44 +0200268 if (daemonize) {
269 rc = osmo_daemonize();
270 if (rc < 0) {
271 perror("Error during daemonize");
272 exit(1);
273 }
274 }
275
Harald Welte288be162010-05-01 16:48:27 +0200276 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200277 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200278 if (rc < 0)
279 exit(3);
280 }
281
Harald Weltebaf77002011-07-16 13:47:01 +0200282 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200283 exit(0);
284}