blob: 55588797b99dfa77e631ee90a087c8f6454457b3 [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
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * 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
Harald Welte288be162010-05-01 16:48:27 +020010 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte288be162010-05-01 16:48:27 +020016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * 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/>.
Harald Welte288be162010-05-01 16:48:27 +020019 *
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>
Harald Welte66f793a2010-05-13 11:50:04 +020028#include <signal.h>
Harald Welte288be162010-05-01 16:48:27 +020029#include <sys/fcntl.h>
30#include <sys/stat.h>
Harald Welte288be162010-05-01 16:48:27 +020031#include <sys/socket.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020035#include <osmocom/core/application.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010036#include <osmocom/core/talloc.h>
37#include <osmocom/core/select.h>
38#include <osmocom/core/rate_ctr.h>
39#include <osmocom/core/logging.h>
Harald Welte288be162010-05-01 16:48:27 +020040
Harald Welteea34a4e2012-06-16 14:59:56 +080041#include <osmocom/gprs/gprs_ns.h>
42#include <osmocom/gprs/gprs_bssgp.h>
43
Harald Welte4b037e42010-05-19 19:45:32 +020044#include <osmocom/vty/telnet_interface.h>
Pablo Neira Ayuso38487702011-03-23 08:49:00 +010045#include <osmocom/vty/logging.h>
Harald Welte4b037e42010-05-19 19:45:32 +020046
Harald Welte288be162010-05-01 16:48:27 +020047#include <openbsc/signal.h>
48#include <openbsc/debug.h>
Harald Welte288be162010-05-01 16:48:27 +020049#include <openbsc/vty.h>
50#include <openbsc/sgsn.h>
Harald Welte2e918a82010-05-18 12:20:12 +020051#include <openbsc/gprs_llc.h>
Harald Welteb1fd9022012-06-17 12:16:31 +080052#include <openbsc/gprs_gmm.h>
Harald Welte288be162010-05-01 16:48:27 +020053
Harald Welte2720e732010-05-17 00:44:57 +020054#include <gtp.h>
55
Harald Weltee2365962010-05-04 07:41:59 +020056#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020057
58/* this is here for the vty... it will never be called */
59void subscr_put() { abort(); }
60
61#define _GNU_SOURCE
62#include <getopt.h>
63
64void *tall_bsc_ctx;
65
66struct gprs_ns_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020067static 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"
Harald Welte9af6ddf2011-01-01 15:25:50 +010070 "License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080071 "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,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020084 struct msgb *msg, uint16_t bvci)
Harald Welte288be162010-05-01 16:48:27 +020085{
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)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +010096 msgb_free(msg);
Harald Welte288be162010-05-01 16:48:27 +020097 rc = -EIO;
98 break;
99 }
100 return rc;
101}
102
Harald Welteb1fd9022012-06-17 12:16:31 +0800103/* call-back function for the BSSGP protocol */
104int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
105{
106 struct osmo_bssgp_prim *bp;
107 bp = container_of(oph, struct osmo_bssgp_prim, oph);
108
109 switch (oph->sap) {
110 case SAP_BSSGP_LL:
111 switch (oph->primitive) {
112 case PRIM_BSSGP_UL_UD:
113 return gprs_llc_rcvmsg(oph->msg, bp->tp);
114 }
115 break;
116 case SAP_BSSGP_GMM:
117 switch (oph->primitive) {
118 case PRIM_BSSGP_GMM_SUSPEND:
119 return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli);
120 case PRIM_BSSGP_GMM_RESUME:
121 return gprs_gmm_rx_resume(bp->ra_id, bp->tlli,
122 *bp->u.resume.suspend_ref);
123 }
124 break;
125 case SAP_BSSGP_NM:
126 break;
127 }
128 return 0;
129}
130
Harald Welte66f793a2010-05-13 11:50:04 +0200131static void signal_handler(int signal)
132{
133 fprintf(stdout, "signal %u received\n", signal);
134
135 switch (signal) {
136 case SIGINT:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200137 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200138 sleep(1);
139 exit(0);
140 break;
141 case SIGABRT:
142 /* in case of abort, we want to obtain a talloc report
143 * and then return to the caller, who will abort the process */
144 case SIGUSR1:
145 talloc_report(tall_vty_ctx, stderr);
146 talloc_report_full(tall_bsc_ctx, stderr);
147 break;
148 case SIGUSR2:
149 talloc_report_full(tall_vty_ctx, stderr);
150 break;
151 default:
152 break;
153 }
154}
155
Harald Welted6c74162010-05-02 21:29:17 +0200156/* NSI that BSSGP uses when transmitting on NS */
157extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200158extern void *tall_msgb_ctx;
Harald Welte288be162010-05-01 16:48:27 +0200159
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +0800160extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200161
162static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100163 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200164 .version = PACKAGE_VERSION,
165 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +0800166 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200167};
168
Harald Welte9ae28a12010-08-27 09:26:44 +0200169static void print_help(void)
170{
171 printf("Some useful help...\n");
172 printf(" -h --help\tthis text\n");
173 printf(" -D --daemonize\tFork the process into a background daemon\n");
174 printf(" -d option --debug\tenable Debugging\n");
175 printf(" -s --disable-color\n");
176 printf(" -c --config-file\tThe config file to use\n");
177 printf(" -e --log-level number\tSet a global log level\n");
178}
179
180static void handle_options(int argc, char **argv)
181{
182 while (1) {
183 int option_index = 0, c;
184 static struct option long_options[] = {
185 {"help", 0, 0, 'h'},
186 {"debug", 1, 0, 'd'},
187 {"daemonize", 0, 0, 'D'},
188 {"config-file", 1, 0, 'c'},
189 {"disable-color", 0, 0, 's'},
190 {"timestamp", 0, 0, 'T'},
191 {"log-level", 1, 0, 'e'},
192 {NULL, 0, 0, 0}
193 };
194
195 c = getopt_long(argc, argv, "hd:Dc:sTe:",
196 long_options, &option_index);
197 if (c == -1)
198 break;
199
200 switch (c) {
201 case 'h':
202 //print_usage();
203 print_help();
204 exit(0);
205 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200206 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200207 break;
208 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200209 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200210 break;
211 case 'D':
212 daemonize = 1;
213 break;
214 case 'c':
215 sgsn_inst.config_file = strdup(optarg);
216 break;
217 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200218 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200219 break;
220 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200221 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200222 break;
223 default:
224 /* ignore */
225 break;
226 }
227 }
228}
229
Harald Welte288be162010-05-01 16:48:27 +0200230int main(int argc, char **argv)
231{
232 struct gsm_network dummy_network;
Harald Welte288be162010-05-01 16:48:27 +0200233 int rc;
234
235 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200236 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
237
238 signal(SIGINT, &signal_handler);
239 signal(SIGABRT, &signal_handler);
240 signal(SIGUSR1, &signal_handler);
241 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200242
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200243 osmo_init_ignore_signals();
244 osmo_init_logging(&log_info);
Harald Welte288be162010-05-01 16:48:27 +0200245
Harald Weltec31f4802010-05-25 23:31:39 +0200246 vty_info.copyright = openbsc_copyright;
247 vty_init(&vty_info);
Pablo Neira Ayuso38487702011-03-23 08:49:00 +0100248 logging_vty_add_cmds(&log_info);
Harald Weltedcccb182010-05-16 20:52:23 +0200249 sgsn_vty_init();
250
Harald Welte9ae28a12010-08-27 09:26:44 +0200251 handle_options(argc, argv);
252
Harald Welte66f793a2010-05-13 11:50:04 +0200253 rate_ctr_init(tall_bsc_ctx);
Harald Weltedcccb182010-05-16 20:52:23 +0200254 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245);
255 if (rc < 0)
256 exit(1);
Harald Welte288be162010-05-01 16:48:27 +0200257
Harald Welte68d85d52012-06-16 17:45:59 +0800258 gprs_ns_set_log_ss(DNS);
259 gprs_bssgp_set_log_ss(DBSSGP);
260
Harald Weltefdd8b3b2012-06-16 16:54:06 +0800261 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_bsc_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200262 if (!sgsn_nsi) {
263 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
264 exit(1);
265 }
Harald Welte2720e732010-05-17 00:44:57 +0200266 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200267
268 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
269
Harald Welte66f793a2010-05-13 11:50:04 +0200270 gprs_ns_vty_init(bssgp_nsi);
Harald Welted2a9ed22010-05-18 08:02:36 +0200271 gprs_bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200272 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200273 gprs_sndcp_vty_init();
Harald Weltea6a20b42012-06-16 16:40:42 +0800274 /* FIXME: register signal handler for SS_L_NS */
Harald Welte66f793a2010-05-13 11:50:04 +0200275
Harald Welte2720e732010-05-17 00:44:57 +0200276 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200277 if (rc < 0) {
278 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
279 exit(2);
280 }
281
Harald Welte2720e732010-05-17 00:44:57 +0200282 rc = sgsn_gtp_init(&sgsn_inst);
Harald Welte6efc1762010-05-19 15:46:31 +0200283 if (rc) {
284 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200285 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200286 }
Harald Welte269ae752010-05-18 14:44:31 +0200287
Harald Welteff3bde82010-05-19 15:09:09 +0200288 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200289 if (rc < 0) {
290 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200291 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200292 }
293
294 rc = gprs_ns_frgre_listen(sgsn_nsi);
295 if (rc < 0) {
296 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
297 "socket. Do you have CAP_NET_RAW?\n");
298 exit(2);
299 }
Harald Welte288be162010-05-01 16:48:27 +0200300
Harald Welte9ae28a12010-08-27 09:26:44 +0200301 if (daemonize) {
302 rc = osmo_daemonize();
303 if (rc < 0) {
304 perror("Error during daemonize");
305 exit(1);
306 }
307 }
308
Harald Welte288be162010-05-01 16:48:27 +0200309 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200310 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200311 if (rc < 0)
312 exit(3);
313 }
314
Harald Weltebaf77002011-07-16 13:47:01 +0200315 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200316 exit(0);
317}