blob: e72d398a16f6e3f64215034ae9ec19105ea7c739 [file] [log] [blame]
Harald Welte288be162010-05-01 16:48:27 +02001/* GPRS SGSN Implementation */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2010 by On Waves
5 * 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 Welte288be162010-05-01 16:48:27 +020040
41#include <openbsc/signal.h>
42#include <openbsc/debug.h>
43#include <openbsc/telnet_interface.h>
44#include <openbsc/vty.h>
45#include <openbsc/sgsn.h>
46#include <openbsc/gprs_ns.h>
47#include <openbsc/gprs_bssgp.h>
Harald Welte2e918a82010-05-18 12:20:12 +020048#include <openbsc/gprs_llc.h>
Harald Welte288be162010-05-01 16:48:27 +020049
Harald Welte2720e732010-05-17 00:44:57 +020050#include <gtp.h>
51
Harald Weltee2365962010-05-04 07:41:59 +020052#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020053
54/* this is here for the vty... it will never be called */
55void subscr_put() { abort(); }
56
57#define _GNU_SOURCE
58#include <getopt.h>
59
60void *tall_bsc_ctx;
61
62struct gprs_ns_inst *sgsn_nsi;
Harald Welte288be162010-05-01 16:48:27 +020063const char *openbsc_copyright =
64 "Copyright (C) 2010 Harald Welte and On-Waves\n"
Harald Welte288be162010-05-01 16:48:27 +020065 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
66 "This is free software: you are free to change and redistribute it.\n"
67 "There is NO WARRANTY, to the extent permitted by law.\n";
68
Harald Welte8fc1a462010-05-17 00:53:10 +020069static struct sgsn_instance sgsn_inst = {
Harald Welte2720e732010-05-17 00:44:57 +020070 .config_file = "osmo_sgsn.cfg",
71 .cfg = {
72 .gtp_statedir = "./",
73 },
74};
Harald Welte8fc1a462010-05-17 00:53:10 +020075struct sgsn_instance *sgsn = &sgsn_inst;
Harald Welte288be162010-05-01 16:48:27 +020076
77/* call-back function for the NS protocol */
78static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
79 struct msgb *msg, u_int16_t bvci)
80{
81 int rc = 0;
82
83 switch (event) {
84 case GPRS_NS_EVT_UNIT_DATA:
85 /* hand the message into the BSSGP implementation */
Harald Weltee6afd602010-05-02 11:19:37 +020086 rc = gprs_bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +020087 break;
88 default:
89 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
90 if (msg)
91 talloc_free(msg);
92 rc = -EIO;
93 break;
94 }
95 return rc;
96}
97
Harald Welte66f793a2010-05-13 11:50:04 +020098static void signal_handler(int signal)
99{
100 fprintf(stdout, "signal %u received\n", signal);
101
102 switch (signal) {
103 case SIGINT:
104 dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
105 sleep(1);
106 exit(0);
107 break;
108 case SIGABRT:
109 /* in case of abort, we want to obtain a talloc report
110 * and then return to the caller, who will abort the process */
111 case SIGUSR1:
112 talloc_report(tall_vty_ctx, stderr);
113 talloc_report_full(tall_bsc_ctx, stderr);
114 break;
115 case SIGUSR2:
116 talloc_report_full(tall_vty_ctx, stderr);
117 break;
118 default:
119 break;
120 }
121}
122
Harald Welted6c74162010-05-02 21:29:17 +0200123/* NSI that BSSGP uses when transmitting on NS */
124extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200125extern void *tall_msgb_ctx;
Harald Welte77289c22010-05-18 14:32:29 +0200126static struct sgsn_ggsn_ctx _ggsn;
127struct sgsn_ggsn_ctx *dummy_ggsn = &_ggsn;
Harald Welte288be162010-05-01 16:48:27 +0200128
129int main(int argc, char **argv)
130{
131 struct gsm_network dummy_network;
132 struct log_target *stderr_target;
133 struct sockaddr_in sin;
134 int rc;
135
136 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200137 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
138
139 signal(SIGINT, &signal_handler);
140 signal(SIGABRT, &signal_handler);
141 signal(SIGUSR1, &signal_handler);
142 signal(SIGUSR2, &signal_handler);
143 signal(SIGPIPE, SIG_IGN);
Harald Welte288be162010-05-01 16:48:27 +0200144
145 log_init(&log_info);
146 stderr_target = log_target_create_stderr();
147 log_add_target(stderr_target);
148 log_set_all_filter(stderr_target, 1);
149
Harald Weltedcccb182010-05-16 20:52:23 +0200150 vty_init("Osmocom SGSN", PACKAGE_VERSION, openbsc_copyright);
Harald Welte5bc61dc2010-05-16 22:02:16 +0200151 logging_vty_add_cmds();
Harald Weltedcccb182010-05-16 20:52:23 +0200152 sgsn_vty_init();
153
Harald Welte66f793a2010-05-13 11:50:04 +0200154 rate_ctr_init(tall_bsc_ctx);
Harald Weltedcccb182010-05-16 20:52:23 +0200155 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245);
156 if (rc < 0)
157 exit(1);
Harald Welte288be162010-05-01 16:48:27 +0200158
159 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb);
160 if (!sgsn_nsi) {
161 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
162 exit(1);
163 }
Harald Welte2720e732010-05-17 00:44:57 +0200164 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200165 gprs_ns_vty_init(bssgp_nsi);
Harald Welted2a9ed22010-05-18 08:02:36 +0200166 gprs_bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200167 gprs_llc_vty_init();
Harald Welte66f793a2010-05-13 11:50:04 +0200168 /* FIXME: register signal handler for SS_NS */
169
Harald Welte2720e732010-05-17 00:44:57 +0200170 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200171 if (rc < 0) {
172 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
173 exit(2);
174 }
175
Harald Welte2720e732010-05-17 00:44:57 +0200176 rc = sgsn_gtp_init(&sgsn_inst);
177 nsip_listen(sgsn_nsi, sgsn_inst.cfg.nsip_listen_port);
Harald Welte288be162010-05-01 16:48:27 +0200178
Harald Welte77289c22010-05-18 14:32:29 +0200179 _ggsn.gtp_version = 1;
180 inet_aton("192.168.100.239", &_ggsn.remote_addr);
181 _ggsn.gsn = sgsn_inst.gsn;
182
Harald Welte288be162010-05-01 16:48:27 +0200183 while (1) {
184 rc = bsc_select_main(0);
185 if (rc < 0)
186 exit(3);
187 }
188
189 exit(0);
190}