blob: c59265fb4e241b193412b4179e13fd8b6382a678 [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* 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 Affero General Public License as published by
9 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
16 *
17 * 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/>.
19 *
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>
28#include <signal.h>
29#include <sys/fcntl.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35
36#include <osmocore/talloc.h>
37#include <osmocore/select.h>
38#include <osmocore/rate_ctr.h>
39#include <osmocore/logging.h>
40#include <osmocore/process.h>
41
42#include <osmocom/vty/telnet_interface.h>
43
44#include <openbsc/signal.h>
45#include <openbsc/debug.h>
46#include <openbsc/vty.h>
47#include <openbsc/sgsn.h>
48#include <openbsc/gprs_ns.h>
49#include <openbsc/gprs_bssgp.h>
50#include <openbsc/gprs_llc.h>
51
52#include <gtp.h>
53
54#include "../../bscconfig.h"
55
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;
65static struct log_target *stderr_target;
66static int daemonize = 0;
67const char *openbsc_copyright =
68 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
69 "License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
70 "This is free software: you are free to change and redistribute it.\r\n"
71 "There is NO WARRANTY, to the extent permitted by law.\r\n";
72
73static struct sgsn_instance sgsn_inst = {
74 .config_file = "osmo_sgsn.cfg",
75 .cfg = {
76 .gtp_statedir = "./",
77 },
78};
79struct sgsn_instance *sgsn = &sgsn_inst;
80
81/* call-back function for the NS protocol */
82static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
83 struct msgb *msg, u_int16_t bvci)
84{
85 int rc = 0;
86
87 switch (event) {
88 case GPRS_NS_EVT_UNIT_DATA:
89 /* hand the message into the BSSGP implementation */
90 rc = gprs_bssgp_rcvmsg(msg);
91 break;
92 default:
93 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
94 if (msg)
95 talloc_free(msg);
96 rc = -EIO;
97 break;
98 }
99 return rc;
100}
101
102static void signal_handler(int signal)
103{
104 fprintf(stdout, "signal %u received\n", signal);
105
106 switch (signal) {
107 case SIGINT:
108 dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
109 sleep(1);
110 exit(0);
111 break;
112 case SIGABRT:
113 /* in case of abort, we want to obtain a talloc report
114 * and then return to the caller, who will abort the process */
115 case SIGUSR1:
116 talloc_report(tall_vty_ctx, stderr);
117 talloc_report_full(tall_bsc_ctx, stderr);
118 break;
119 case SIGUSR2:
120 talloc_report_full(tall_vty_ctx, stderr);
121 break;
122 default:
123 break;
124 }
125}
126
127/* NSI that BSSGP uses when transmitting on NS */
128extern struct gprs_ns_inst *bssgp_nsi;
129extern void *tall_msgb_ctx;
130
131extern enum node_type bsc_vty_go_parent(struct vty *vty);
132
133static struct vty_app_info vty_info = {
134 .name = "OsmoSGSN",
135 .version = PACKAGE_VERSION,
136 .go_parent_cb = bsc_vty_go_parent,
137 .is_config_node = bsc_vty_is_config_node,
138};
139
140static 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
201int main(int argc, char **argv)
202{
203 struct gsm_network dummy_network;
204 struct sockaddr_in sin;
205 int rc;
206
207 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
208 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);
215
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
221 vty_info.copyright = openbsc_copyright;
222 vty_init(&vty_info);
223 logging_vty_add_cmds();
224 sgsn_vty_init();
225
226 handle_options(argc, argv);
227
228 rate_ctr_init(tall_bsc_ctx);
229 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245);
230 if (rc < 0)
231 exit(1);
232
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 }
238 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
239
240 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
241
242 gprs_ns_vty_init(bssgp_nsi);
243 gprs_bssgp_vty_init();
244 gprs_llc_vty_init();
245 gprs_sndcp_vty_init();
246 /* FIXME: register signal handler for SS_NS */
247
248 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
249 if (rc < 0) {
250 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
251 exit(2);
252 }
253
254 rc = sgsn_gtp_init(&sgsn_inst);
255 if (rc) {
256 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
257 exit(2);
258 }
259
260 rc = gprs_ns_nsip_listen(sgsn_nsi);
261 if (rc < 0) {
262 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
263 exit(2);
264 }
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 }
272
273 if (daemonize) {
274 rc = osmo_daemonize();
275 if (rc < 0) {
276 perror("Error during daemonize");
277 exit(1);
278 }
279 }
280
281 while (1) {
282 rc = bsc_select_main(0);
283 if (rc < 0)
284 exit(3);
285 }
286
287 exit(0);
288}