blob: c63a476d79b8e1a5d19d303abe75b25b85054808 [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
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <unistd.h>
24#include <stdio.h>
25#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>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <netinet/in.h>
35#include <arpa/inet.h>
36
37#include <osmocore/talloc.h>
38#include <osmocore/select.h>
Harald Welte66f793a2010-05-13 11:50:04 +020039#include <osmocore/rate_ctr.h>
Harald Welte9ae28a12010-08-27 09:26:44 +020040#include <osmocore/logging.h>
41#include <osmocore/process.h>
Harald Welte288be162010-05-01 16:48:27 +020042
Harald Welte4b037e42010-05-19 19:45:32 +020043#include <osmocom/vty/telnet_interface.h>
44
Harald Welte288be162010-05-01 16:48:27 +020045#include <openbsc/signal.h>
46#include <openbsc/debug.h>
Harald Welte288be162010-05-01 16:48:27 +020047#include <openbsc/vty.h>
48#include <openbsc/sgsn.h>
49#include <openbsc/gprs_ns.h>
50#include <openbsc/gprs_bssgp.h>
Harald Welte2e918a82010-05-18 12:20:12 +020051#include <openbsc/gprs_llc.h>
Harald Welte288be162010-05-01 16:48:27 +020052
Harald Welte2720e732010-05-17 00:44:57 +020053#include <gtp.h>
54
Harald Weltee2365962010-05-04 07:41:59 +020055#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020056
57/* this is here for the vty... it will never be called */
58void subscr_put() { abort(); }
59
60#define _GNU_SOURCE
61#include <getopt.h>
62
63void *tall_bsc_ctx;
64
65struct gprs_ns_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020066static struct log_target *stderr_target;
67static int daemonize = 0;
Harald Welte288be162010-05-01 16:48:27 +020068const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080069 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
70 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\r\n"
71 "This is free software: you are free to change and redistribute it.\r\n"
72 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte288be162010-05-01 16:48:27 +020073
Harald Welte8fc1a462010-05-17 00:53:10 +020074static struct sgsn_instance sgsn_inst = {
Harald Welte2720e732010-05-17 00:44:57 +020075 .config_file = "osmo_sgsn.cfg",
76 .cfg = {
77 .gtp_statedir = "./",
78 },
79};
Harald Welte8fc1a462010-05-17 00:53:10 +020080struct sgsn_instance *sgsn = &sgsn_inst;
Harald Welte288be162010-05-01 16:48:27 +020081
82/* call-back function for the NS protocol */
83static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
84 struct msgb *msg, u_int16_t bvci)
85{
86 int rc = 0;
87
88 switch (event) {
89 case GPRS_NS_EVT_UNIT_DATA:
90 /* hand the message into the BSSGP implementation */
Harald Weltee6afd602010-05-02 11:19:37 +020091 rc = gprs_bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +020092 break;
93 default:
94 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
95 if (msg)
96 talloc_free(msg);
97 rc = -EIO;
98 break;
99 }
100 return rc;
101}
102
Harald Welte66f793a2010-05-13 11:50:04 +0200103static void signal_handler(int signal)
104{
105 fprintf(stdout, "signal %u received\n", signal);
106
107 switch (signal) {
108 case SIGINT:
109 dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
110 sleep(1);
111 exit(0);
112 break;
113 case SIGABRT:
114 /* in case of abort, we want to obtain a talloc report
115 * and then return to the caller, who will abort the process */
116 case SIGUSR1:
117 talloc_report(tall_vty_ctx, stderr);
118 talloc_report_full(tall_bsc_ctx, stderr);
119 break;
120 case SIGUSR2:
121 talloc_report_full(tall_vty_ctx, stderr);
122 break;
123 default:
124 break;
125 }
126}
127
Harald Welted6c74162010-05-02 21:29:17 +0200128/* NSI that BSSGP uses when transmitting on NS */
129extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200130extern void *tall_msgb_ctx;
Harald Welte288be162010-05-01 16:48:27 +0200131
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +0800132extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200133
134static struct vty_app_info vty_info = {
135 .name = "Osmocom SGSN",
136 .version = PACKAGE_VERSION,
137 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +0800138 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200139};
140
Harald Welte9ae28a12010-08-27 09:26:44 +0200141static void print_help(void)
142{
143 printf("Some useful help...\n");
144 printf(" -h --help\tthis text\n");
145 printf(" -D --daemonize\tFork the process into a background daemon\n");
146 printf(" -d option --debug\tenable Debugging\n");
147 printf(" -s --disable-color\n");
148 printf(" -c --config-file\tThe config file to use\n");
149 printf(" -e --log-level number\tSet a global log level\n");
150}
151
152static void handle_options(int argc, char **argv)
153{
154 while (1) {
155 int option_index = 0, c;
156 static struct option long_options[] = {
157 {"help", 0, 0, 'h'},
158 {"debug", 1, 0, 'd'},
159 {"daemonize", 0, 0, 'D'},
160 {"config-file", 1, 0, 'c'},
161 {"disable-color", 0, 0, 's'},
162 {"timestamp", 0, 0, 'T'},
163 {"log-level", 1, 0, 'e'},
164 {NULL, 0, 0, 0}
165 };
166
167 c = getopt_long(argc, argv, "hd:Dc:sTe:",
168 long_options, &option_index);
169 if (c == -1)
170 break;
171
172 switch (c) {
173 case 'h':
174 //print_usage();
175 print_help();
176 exit(0);
177 case 's':
178 log_set_use_color(stderr_target, 0);
179 break;
180 case 'd':
181 log_parse_category_mask(stderr_target, optarg);
182 break;
183 case 'D':
184 daemonize = 1;
185 break;
186 case 'c':
187 sgsn_inst.config_file = strdup(optarg);
188 break;
189 case 'T':
190 log_set_print_timestamp(stderr_target, 1);
191 break;
192 case 'e':
193 log_set_log_level(stderr_target, atoi(optarg));
194 break;
195 default:
196 /* ignore */
197 break;
198 }
199 }
200}
201
Harald Welte288be162010-05-01 16:48:27 +0200202int main(int argc, char **argv)
203{
204 struct gsm_network dummy_network;
Harald Welte288be162010-05-01 16:48:27 +0200205 struct sockaddr_in sin;
206 int rc;
207
208 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200209 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
210
211 signal(SIGINT, &signal_handler);
212 signal(SIGABRT, &signal_handler);
213 signal(SIGUSR1, &signal_handler);
214 signal(SIGUSR2, &signal_handler);
215 signal(SIGPIPE, SIG_IGN);
Harald Welte288be162010-05-01 16:48:27 +0200216
217 log_init(&log_info);
218 stderr_target = log_target_create_stderr();
219 log_add_target(stderr_target);
220 log_set_all_filter(stderr_target, 1);
221
Harald Weltec31f4802010-05-25 23:31:39 +0200222 vty_info.copyright = openbsc_copyright;
223 vty_init(&vty_info);
Harald Welte5bc61dc2010-05-16 22:02:16 +0200224 logging_vty_add_cmds();
Harald Weltedcccb182010-05-16 20:52:23 +0200225 sgsn_vty_init();
226
Harald Welte9ae28a12010-08-27 09:26:44 +0200227 handle_options(argc, argv);
228
Harald Welte66f793a2010-05-13 11:50:04 +0200229 rate_ctr_init(tall_bsc_ctx);
Harald Weltedcccb182010-05-16 20:52:23 +0200230 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245);
231 if (rc < 0)
232 exit(1);
Harald Welte288be162010-05-01 16:48:27 +0200233
234 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb);
235 if (!sgsn_nsi) {
236 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
237 exit(1);
238 }
Harald Welte2720e732010-05-17 00:44:57 +0200239 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200240
241 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
242
Harald Welte66f793a2010-05-13 11:50:04 +0200243 gprs_ns_vty_init(bssgp_nsi);
Harald Welted2a9ed22010-05-18 08:02:36 +0200244 gprs_bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200245 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200246 gprs_sndcp_vty_init();
Harald Welte66f793a2010-05-13 11:50:04 +0200247 /* FIXME: register signal handler for SS_NS */
248
Harald Welte2720e732010-05-17 00:44:57 +0200249 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200250 if (rc < 0) {
251 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
252 exit(2);
253 }
254
Harald Welte2720e732010-05-17 00:44:57 +0200255 rc = sgsn_gtp_init(&sgsn_inst);
Harald Welte6efc1762010-05-19 15:46:31 +0200256 if (rc) {
257 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200258 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200259 }
Harald Welte269ae752010-05-18 14:44:31 +0200260
Harald Welteff3bde82010-05-19 15:09:09 +0200261 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200262 if (rc < 0) {
263 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200264 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200265 }
266
267 rc = gprs_ns_frgre_listen(sgsn_nsi);
268 if (rc < 0) {
269 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
270 "socket. Do you have CAP_NET_RAW?\n");
271 exit(2);
272 }
Harald Welte288be162010-05-01 16:48:27 +0200273
Harald Welte9ae28a12010-08-27 09:26:44 +0200274 if (daemonize) {
275 rc = osmo_daemonize();
276 if (rc < 0) {
277 perror("Error during daemonize");
278 exit(1);
279 }
280 }
281
Harald Welte288be162010-05-01 16:48:27 +0200282 while (1) {
283 rc = bsc_select_main(0);
284 if (rc < 0)
285 exit(3);
286 }
287
288 exit(0);
289}