blob: 7d80dc8bd15d5f721de0cec1626d21515ed21be0 [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 Weltece95b272012-06-17 13:04:02 +080091 rc = 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,
Holger Hans Peter Freyther9b5db282012-08-03 10:01:57 +0200122 bp->u.resume.suspend_ref);
Harald Welteb1fd9022012-06-17 12:16:31 +0800123 }
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 Welte11461a62012-06-17 12:58:46 +0800230/* default categories */
231static struct log_info_cat gprs_categories[] = {
232 [DMM] = {
233 .name = "DMM",
234 .description = "Layer3 Mobility Management (MM)",
235 .color = "\033[1;33m",
236 .enabled = 1, .loglevel = LOGL_NOTICE,
237 },
238 [DSMS] = {
239 .name = "DSMS",
240 .description = "Layer3 Short Message Service (SMS)",
241 .color = "\033[1;37m",
242 .enabled = 1, .loglevel = LOGL_NOTICE,
243 },
244 [DPAG] = {
245 .name = "DPAG",
246 .description = "Paging Subsystem",
247 .color = "\033[1;38m",
248 .enabled = 1, .loglevel = LOGL_NOTICE,
249 },
250 [DMEAS] = {
251 .name = "DMEAS",
252 .description = "Radio Measurement Processing",
253 .enabled = 0, .loglevel = LOGL_NOTICE,
254 },
255 [DREF] = {
256 .name = "DREF",
257 .description = "Reference Counting",
258 .enabled = 0, .loglevel = LOGL_NOTICE,
259 },
260 [DGPRS] = {
261 .name = "DGPRS",
262 .description = "GPRS Packet Service",
263 .enabled = 1, .loglevel = LOGL_DEBUG,
264 },
265 [DNS] = {
266 .name = "DNS",
267 .description = "GPRS Network Service (NS)",
268 .enabled = 1, .loglevel = LOGL_INFO,
269 },
270 [DBSSGP] = {
271 .name = "DBSSGP",
272 .description = "GPRS BSS Gateway Protocol (BSSGP)",
273 .enabled = 1, .loglevel = LOGL_DEBUG,
274 },
275 [DLLC] = {
276 .name = "DLLC",
277 .description = "GPRS Logical Link Control Protocol (LLC)",
278 .enabled = 1, .loglevel = LOGL_DEBUG,
279 },
280 [DSNDCP] = {
281 .name = "DSNDCP",
282 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
283 .enabled = 1, .loglevel = LOGL_DEBUG,
284 },
285};
286
287static const struct log_info gprs_log_info = {
288 .filter_fn = gprs_log_filter_fn,
289 .cat = gprs_categories,
290 .num_cat = ARRAY_SIZE(gprs_categories),
291};
292
293
Harald Welte288be162010-05-01 16:48:27 +0200294int main(int argc, char **argv)
295{
296 struct gsm_network dummy_network;
Harald Welte288be162010-05-01 16:48:27 +0200297 int rc;
298
299 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200300 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
301
302 signal(SIGINT, &signal_handler);
303 signal(SIGABRT, &signal_handler);
304 signal(SIGUSR1, &signal_handler);
305 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200306
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200307 osmo_init_ignore_signals();
Harald Welte11461a62012-06-17 12:58:46 +0800308 osmo_init_logging(&gprs_log_info);
Harald Welte288be162010-05-01 16:48:27 +0200309
Harald Weltec31f4802010-05-25 23:31:39 +0200310 vty_info.copyright = openbsc_copyright;
311 vty_init(&vty_info);
Harald Welte11461a62012-06-17 12:58:46 +0800312 logging_vty_add_cmds(&gprs_log_info);
Harald Weltedcccb182010-05-16 20:52:23 +0200313 sgsn_vty_init();
314
Harald Welte9ae28a12010-08-27 09:26:44 +0200315 handle_options(argc, argv);
316
Harald Welte66f793a2010-05-13 11:50:04 +0200317 rate_ctr_init(tall_bsc_ctx);
Harald Weltedcccb182010-05-16 20:52:23 +0200318 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245);
319 if (rc < 0)
320 exit(1);
Harald Welte288be162010-05-01 16:48:27 +0200321
Harald Welte68d85d52012-06-16 17:45:59 +0800322 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800323 bssgp_set_log_ss(DBSSGP);
Harald Welte68d85d52012-06-16 17:45:59 +0800324
Harald Weltefdd8b3b2012-06-16 16:54:06 +0800325 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_bsc_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200326 if (!sgsn_nsi) {
327 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
328 exit(1);
329 }
Harald Welte2720e732010-05-17 00:44:57 +0200330 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200331
332 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
333
Harald Welte66f793a2010-05-13 11:50:04 +0200334 gprs_ns_vty_init(bssgp_nsi);
Harald Weltece95b272012-06-17 13:04:02 +0800335 bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200336 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200337 gprs_sndcp_vty_init();
Harald Weltea6a20b42012-06-16 16:40:42 +0800338 /* FIXME: register signal handler for SS_L_NS */
Harald Welte66f793a2010-05-13 11:50:04 +0200339
Harald Welte2720e732010-05-17 00:44:57 +0200340 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200341 if (rc < 0) {
342 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
343 exit(2);
344 }
345
Harald Welte2720e732010-05-17 00:44:57 +0200346 rc = sgsn_gtp_init(&sgsn_inst);
Harald Welte6efc1762010-05-19 15:46:31 +0200347 if (rc) {
348 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200349 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200350 }
Harald Welte269ae752010-05-18 14:44:31 +0200351
Harald Welteff3bde82010-05-19 15:09:09 +0200352 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200353 if (rc < 0) {
354 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200355 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200356 }
357
358 rc = gprs_ns_frgre_listen(sgsn_nsi);
359 if (rc < 0) {
360 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
361 "socket. Do you have CAP_NET_RAW?\n");
362 exit(2);
363 }
Harald Welte288be162010-05-01 16:48:27 +0200364
Harald Welte9ae28a12010-08-27 09:26:44 +0200365 if (daemonize) {
366 rc = osmo_daemonize();
367 if (rc < 0) {
368 perror("Error during daemonize");
369 exit(1);
370 }
371 }
372
Harald Welte288be162010-05-01 16:48:27 +0200373 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200374 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200375 if (rc < 0)
376 exit(3);
377 }
378
Harald Weltebaf77002011-07-16 13:47:01 +0200379 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200380 exit(0);
381}