blob: b355fb58db7e170a67821db0313f261b29ceb904 [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>
48
Harald Weltee2365962010-05-04 07:41:59 +020049#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020050
51/* this is here for the vty... it will never be called */
52void subscr_put() { abort(); }
53
54#define _GNU_SOURCE
55#include <getopt.h>
56
57void *tall_bsc_ctx;
58
59struct gprs_ns_inst *sgsn_nsi;
60
61const char *openbsc_version = "Osmocom NSIP Proxy " PACKAGE_VERSION;
62const char *openbsc_copyright =
63 "Copyright (C) 2010 Harald Welte and On-Waves\n"
64 "Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\n"
65 "Dieter Spaar, Andreas Eversberg, Holger Freyther\n\n"
66 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
67 "This is free software: you are free to change and redistribute it.\n"
68 "There is NO WARRANTY, to the extent permitted by law.\n";
69
70static char *config_file = "osmo_sgsn.cfg";
71static struct sgsn_config sgcfg;
72
73/* call-back function for the NS protocol */
74static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
75 struct msgb *msg, u_int16_t bvci)
76{
77 int rc = 0;
78
79 switch (event) {
80 case GPRS_NS_EVT_UNIT_DATA:
81 /* hand the message into the BSSGP implementation */
Harald Weltee6afd602010-05-02 11:19:37 +020082 rc = gprs_bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +020083 break;
84 default:
85 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
86 if (msg)
87 talloc_free(msg);
88 rc = -EIO;
89 break;
90 }
91 return rc;
92}
93
Harald Welte66f793a2010-05-13 11:50:04 +020094static void signal_handler(int signal)
95{
96 fprintf(stdout, "signal %u received\n", signal);
97
98 switch (signal) {
99 case SIGINT:
100 dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
101 sleep(1);
102 exit(0);
103 break;
104 case SIGABRT:
105 /* in case of abort, we want to obtain a talloc report
106 * and then return to the caller, who will abort the process */
107 case SIGUSR1:
108 talloc_report(tall_vty_ctx, stderr);
109 talloc_report_full(tall_bsc_ctx, stderr);
110 break;
111 case SIGUSR2:
112 talloc_report_full(tall_vty_ctx, stderr);
113 break;
114 default:
115 break;
116 }
117}
118
Harald Welted6c74162010-05-02 21:29:17 +0200119/* NSI that BSSGP uses when transmitting on NS */
120extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200121extern void *tall_msgb_ctx;
Harald Welte288be162010-05-01 16:48:27 +0200122
123int main(int argc, char **argv)
124{
125 struct gsm_network dummy_network;
126 struct log_target *stderr_target;
127 struct sockaddr_in sin;
128 int rc;
129
130 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200131 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
132
133 signal(SIGINT, &signal_handler);
134 signal(SIGABRT, &signal_handler);
135 signal(SIGUSR1, &signal_handler);
136 signal(SIGUSR2, &signal_handler);
137 signal(SIGPIPE, SIG_IGN);
Harald Welte288be162010-05-01 16:48:27 +0200138
139 log_init(&log_info);
140 stderr_target = log_target_create_stderr();
141 log_add_target(stderr_target);
142 log_set_all_filter(stderr_target, 1);
143
Harald Welte66f793a2010-05-13 11:50:04 +0200144 rate_ctr_init(tall_bsc_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200145 telnet_init(&dummy_network, 4245);
Harald Welte288be162010-05-01 16:48:27 +0200146
147 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb);
148 if (!sgsn_nsi) {
149 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
150 exit(1);
151 }
Harald Welted6c74162010-05-02 21:29:17 +0200152 bssgp_nsi = sgcfg.nsi = sgsn_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200153 gprs_ns_vty_init(bssgp_nsi);
154 /* FIXME: register signal handler for SS_NS */
155
156 rc = sgsn_parse_config(config_file, &sgcfg);
157 if (rc < 0) {
158 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
159 exit(2);
160 }
161
Harald Welte288be162010-05-01 16:48:27 +0200162 nsip_listen(sgsn_nsi, sgcfg.nsip_listen_port);
163
164 while (1) {
165 rc = bsc_select_main(0);
166 if (rc < 0)
167 exit(3);
168 }
169
170 exit(0);
171}
172
173struct gsm_network;
174int bsc_vty_init(struct gsm_network *dummy)
175{
176 cmd_init(1);
177 vty_init();
178
179 openbsc_vty_add_cmds();
180 sgsn_vty_init();
181 return 0;
182}
183