blob: 8c42edc6187e234d1b6c3edad82a383bea5928da [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 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 =
69 "Copyright (C) 2010 Harald Welte and On-Waves\n"
Harald Welte288be162010-05-01 16:48:27 +020070 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n"
71 "This is free software: you are free to change and redistribute it.\n"
72 "There is NO WARRANTY, to the extent permitted by law.\n";
73
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,
138};
139
Harald Welte9ae28a12010-08-27 09:26:44 +0200140static void print_help(void)
141{
142 printf("Some useful help...\n");
143 printf(" -h --help\tthis text\n");
144 printf(" -D --daemonize\tFork the process into a background daemon\n");
145 printf(" -d option --debug\tenable Debugging\n");
146 printf(" -s --disable-color\n");
147 printf(" -c --config-file\tThe config file to use\n");
148 printf(" -e --log-level number\tSet a global log level\n");
149}
150
151static void handle_options(int argc, char **argv)
152{
153 while (1) {
154 int option_index = 0, c;
155 static struct option long_options[] = {
156 {"help", 0, 0, 'h'},
157 {"debug", 1, 0, 'd'},
158 {"daemonize", 0, 0, 'D'},
159 {"config-file", 1, 0, 'c'},
160 {"disable-color", 0, 0, 's'},
161 {"timestamp", 0, 0, 'T'},
162 {"log-level", 1, 0, 'e'},
163 {NULL, 0, 0, 0}
164 };
165
166 c = getopt_long(argc, argv, "hd:Dc:sTe:",
167 long_options, &option_index);
168 if (c == -1)
169 break;
170
171 switch (c) {
172 case 'h':
173 //print_usage();
174 print_help();
175 exit(0);
176 case 's':
177 log_set_use_color(stderr_target, 0);
178 break;
179 case 'd':
180 log_parse_category_mask(stderr_target, optarg);
181 break;
182 case 'D':
183 daemonize = 1;
184 break;
185 case 'c':
186 sgsn_inst.config_file = strdup(optarg);
187 break;
188 case 'T':
189 log_set_print_timestamp(stderr_target, 1);
190 break;
191 case 'e':
192 log_set_log_level(stderr_target, atoi(optarg));
193 break;
194 default:
195 /* ignore */
196 break;
197 }
198 }
199}
200
Harald Welte288be162010-05-01 16:48:27 +0200201int main(int argc, char **argv)
202{
203 struct gsm_network dummy_network;
Harald Welte288be162010-05-01 16:48:27 +0200204 struct sockaddr_in sin;
205 int rc;
206
207 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200208 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
209
210 signal(SIGINT, &signal_handler);
211 signal(SIGABRT, &signal_handler);
212 signal(SIGUSR1, &signal_handler);
213 signal(SIGUSR2, &signal_handler);
214 signal(SIGPIPE, SIG_IGN);
Harald Welte288be162010-05-01 16:48:27 +0200215
216 log_init(&log_info);
217 stderr_target = log_target_create_stderr();
218 log_add_target(stderr_target);
219 log_set_all_filter(stderr_target, 1);
220
Harald Weltec31f4802010-05-25 23:31:39 +0200221 vty_info.copyright = openbsc_copyright;
222 vty_init(&vty_info);
Harald Welte5bc61dc2010-05-16 22:02:16 +0200223 logging_vty_add_cmds();
Harald Weltedcccb182010-05-16 20:52:23 +0200224 sgsn_vty_init();
225
Harald Welte9ae28a12010-08-27 09:26:44 +0200226 handle_options(argc, argv);
227
Harald Welte66f793a2010-05-13 11:50:04 +0200228 rate_ctr_init(tall_bsc_ctx);
Harald Weltedcccb182010-05-16 20:52:23 +0200229 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245);
230 if (rc < 0)
231 exit(1);
Harald Welte288be162010-05-01 16:48:27 +0200232
233 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb);
234 if (!sgsn_nsi) {
235 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
236 exit(1);
237 }
Harald Welte2720e732010-05-17 00:44:57 +0200238 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200239
240 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
241
Harald Welte66f793a2010-05-13 11:50:04 +0200242 gprs_ns_vty_init(bssgp_nsi);
Harald Welted2a9ed22010-05-18 08:02:36 +0200243 gprs_bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200244 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200245 gprs_sndcp_vty_init();
Harald Welte66f793a2010-05-13 11:50:04 +0200246 /* FIXME: register signal handler for SS_NS */
247
Harald Welte2720e732010-05-17 00:44:57 +0200248 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200249 if (rc < 0) {
250 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
251 exit(2);
252 }
253
Harald Welte2720e732010-05-17 00:44:57 +0200254 rc = sgsn_gtp_init(&sgsn_inst);
Harald Welte6efc1762010-05-19 15:46:31 +0200255 if (rc) {
256 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200257 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200258 }
Harald Welte269ae752010-05-18 14:44:31 +0200259
Harald Welteff3bde82010-05-19 15:09:09 +0200260 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200261 if (rc < 0) {
262 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200263 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200264 }
265
266 rc = gprs_ns_frgre_listen(sgsn_nsi);
267 if (rc < 0) {
268 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
269 "socket. Do you have CAP_NET_RAW?\n");
270 exit(2);
271 }
Harald Welte288be162010-05-01 16:48:27 +0200272
Harald Welte9ae28a12010-08-27 09:26:44 +0200273 if (daemonize) {
274 rc = osmo_daemonize();
275 if (rc < 0) {
276 perror("Error during daemonize");
277 exit(1);
278 }
279 }
280
Harald Welte288be162010-05-01 16:48:27 +0200281 while (1) {
282 rc = bsc_select_main(0);
283 if (rc < 0)
284 exit(3);
285 }
286
287 exit(0);
288}