blob: f5a66e663b38de17566de2223f4ebbef54448bc8 [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>
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +010053#include <openbsc/control_if.h>
Harald Welte288be162010-05-01 16:48:27 +020054
Harald Welte2720e732010-05-17 00:44:57 +020055#include <gtp.h>
56
Harald Weltee2365962010-05-04 07:41:59 +020057#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020058
59/* this is here for the vty... it will never be called */
60void subscr_put() { abort(); }
61
62#define _GNU_SOURCE
63#include <getopt.h>
64
65void *tall_bsc_ctx;
66
67struct gprs_ns_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020068static int daemonize = 0;
Harald Welte288be162010-05-01 16:48:27 +020069const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080070 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010071 "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 +080072 "This is free software: you are free to change and redistribute it.\r\n"
73 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte288be162010-05-01 16:48:27 +020074
Harald Welte8fc1a462010-05-17 00:53:10 +020075static struct sgsn_instance sgsn_inst = {
Harald Welte2720e732010-05-17 00:44:57 +020076 .config_file = "osmo_sgsn.cfg",
77 .cfg = {
78 .gtp_statedir = "./",
Harald Welte7f6da482013-03-19 11:00:13 +010079 .acl_enabled = 1,
Harald Welte2720e732010-05-17 00:44:57 +020080 },
81};
Harald Welte8fc1a462010-05-17 00:53:10 +020082struct sgsn_instance *sgsn = &sgsn_inst;
Harald Welte288be162010-05-01 16:48:27 +020083
84/* call-back function for the NS protocol */
85static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020086 struct msgb *msg, uint16_t bvci)
Harald Welte288be162010-05-01 16:48:27 +020087{
88 int rc = 0;
89
90 switch (event) {
91 case GPRS_NS_EVT_UNIT_DATA:
92 /* hand the message into the BSSGP implementation */
Harald Weltece95b272012-06-17 13:04:02 +080093 rc = bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +020094 break;
95 default:
96 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
97 if (msg)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +010098 msgb_free(msg);
Harald Welte288be162010-05-01 16:48:27 +020099 rc = -EIO;
100 break;
101 }
102 return rc;
103}
104
Harald Welteb1fd9022012-06-17 12:16:31 +0800105/* call-back function for the BSSGP protocol */
106int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
107{
108 struct osmo_bssgp_prim *bp;
109 bp = container_of(oph, struct osmo_bssgp_prim, oph);
110
111 switch (oph->sap) {
112 case SAP_BSSGP_LL:
113 switch (oph->primitive) {
114 case PRIM_BSSGP_UL_UD:
115 return gprs_llc_rcvmsg(oph->msg, bp->tp);
116 }
117 break;
118 case SAP_BSSGP_GMM:
119 switch (oph->primitive) {
120 case PRIM_BSSGP_GMM_SUSPEND:
121 return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli);
122 case PRIM_BSSGP_GMM_RESUME:
123 return gprs_gmm_rx_resume(bp->ra_id, bp->tlli,
Holger Hans Peter Freyther9b5db282012-08-03 10:01:57 +0200124 bp->u.resume.suspend_ref);
Harald Welteb1fd9022012-06-17 12:16:31 +0800125 }
126 break;
127 case SAP_BSSGP_NM:
128 break;
129 }
130 return 0;
131}
132
Harald Welte66f793a2010-05-13 11:50:04 +0200133static void signal_handler(int signal)
134{
135 fprintf(stdout, "signal %u received\n", signal);
136
137 switch (signal) {
138 case SIGINT:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200139 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200140 sleep(1);
141 exit(0);
142 break;
143 case SIGABRT:
144 /* in case of abort, we want to obtain a talloc report
145 * and then return to the caller, who will abort the process */
146 case SIGUSR1:
147 talloc_report(tall_vty_ctx, stderr);
148 talloc_report_full(tall_bsc_ctx, stderr);
149 break;
150 case SIGUSR2:
151 talloc_report_full(tall_vty_ctx, stderr);
152 break;
153 default:
154 break;
155 }
156}
157
Harald Welted6c74162010-05-02 21:29:17 +0200158/* NSI that BSSGP uses when transmitting on NS */
159extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200160extern void *tall_msgb_ctx;
Harald Welte288be162010-05-01 16:48:27 +0200161
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +0800162extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200163
164static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100165 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200166 .version = PACKAGE_VERSION,
167 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +0800168 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200169};
170
Harald Welte9ae28a12010-08-27 09:26:44 +0200171static void print_help(void)
172{
173 printf("Some useful help...\n");
174 printf(" -h --help\tthis text\n");
175 printf(" -D --daemonize\tFork the process into a background daemon\n");
176 printf(" -d option --debug\tenable Debugging\n");
177 printf(" -s --disable-color\n");
178 printf(" -c --config-file\tThe config file to use\n");
179 printf(" -e --log-level number\tSet a global log level\n");
180}
181
182static void handle_options(int argc, char **argv)
183{
184 while (1) {
185 int option_index = 0, c;
186 static struct option long_options[] = {
187 {"help", 0, 0, 'h'},
188 {"debug", 1, 0, 'd'},
189 {"daemonize", 0, 0, 'D'},
190 {"config-file", 1, 0, 'c'},
191 {"disable-color", 0, 0, 's'},
192 {"timestamp", 0, 0, 'T'},
193 {"log-level", 1, 0, 'e'},
194 {NULL, 0, 0, 0}
195 };
196
197 c = getopt_long(argc, argv, "hd:Dc:sTe:",
198 long_options, &option_index);
199 if (c == -1)
200 break;
201
202 switch (c) {
203 case 'h':
204 //print_usage();
205 print_help();
206 exit(0);
207 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200208 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200209 break;
210 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200211 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200212 break;
213 case 'D':
214 daemonize = 1;
215 break;
216 case 'c':
217 sgsn_inst.config_file = strdup(optarg);
218 break;
219 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200220 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200221 break;
222 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200223 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200224 break;
225 default:
226 /* ignore */
227 break;
228 }
229 }
230}
231
Harald Welte11461a62012-06-17 12:58:46 +0800232/* default categories */
233static struct log_info_cat gprs_categories[] = {
234 [DMM] = {
235 .name = "DMM",
236 .description = "Layer3 Mobility Management (MM)",
237 .color = "\033[1;33m",
238 .enabled = 1, .loglevel = LOGL_NOTICE,
239 },
Harald Welte11461a62012-06-17 12:58:46 +0800240 [DPAG] = {
241 .name = "DPAG",
242 .description = "Paging Subsystem",
243 .color = "\033[1;38m",
244 .enabled = 1, .loglevel = LOGL_NOTICE,
245 },
246 [DMEAS] = {
247 .name = "DMEAS",
248 .description = "Radio Measurement Processing",
249 .enabled = 0, .loglevel = LOGL_NOTICE,
250 },
251 [DREF] = {
252 .name = "DREF",
253 .description = "Reference Counting",
254 .enabled = 0, .loglevel = LOGL_NOTICE,
255 },
256 [DGPRS] = {
257 .name = "DGPRS",
258 .description = "GPRS Packet Service",
259 .enabled = 1, .loglevel = LOGL_DEBUG,
260 },
261 [DNS] = {
262 .name = "DNS",
263 .description = "GPRS Network Service (NS)",
264 .enabled = 1, .loglevel = LOGL_INFO,
265 },
266 [DBSSGP] = {
267 .name = "DBSSGP",
268 .description = "GPRS BSS Gateway Protocol (BSSGP)",
269 .enabled = 1, .loglevel = LOGL_DEBUG,
270 },
271 [DLLC] = {
272 .name = "DLLC",
273 .description = "GPRS Logical Link Control Protocol (LLC)",
274 .enabled = 1, .loglevel = LOGL_DEBUG,
275 },
276 [DSNDCP] = {
277 .name = "DSNDCP",
278 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
279 .enabled = 1, .loglevel = LOGL_DEBUG,
280 },
281};
282
283static const struct log_info gprs_log_info = {
284 .filter_fn = gprs_log_filter_fn,
285 .cat = gprs_categories,
286 .num_cat = ARRAY_SIZE(gprs_categories),
287};
288
289
Harald Welte288be162010-05-01 16:48:27 +0200290int main(int argc, char **argv)
291{
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +0100292 struct ctrl_handle *ctrl;
Harald Welte288be162010-05-01 16:48:27 +0200293 struct gsm_network dummy_network;
Harald Welte288be162010-05-01 16:48:27 +0200294 int rc;
295
296 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200297 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
298
299 signal(SIGINT, &signal_handler);
300 signal(SIGABRT, &signal_handler);
301 signal(SIGUSR1, &signal_handler);
302 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200303
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200304 osmo_init_ignore_signals();
Harald Welte11461a62012-06-17 12:58:46 +0800305 osmo_init_logging(&gprs_log_info);
Harald Welte288be162010-05-01 16:48:27 +0200306
Harald Weltec31f4802010-05-25 23:31:39 +0200307 vty_info.copyright = openbsc_copyright;
308 vty_init(&vty_info);
Harald Welte11461a62012-06-17 12:58:46 +0800309 logging_vty_add_cmds(&gprs_log_info);
Harald Weltedcccb182010-05-16 20:52:23 +0200310 sgsn_vty_init();
311
Harald Welte9ae28a12010-08-27 09:26:44 +0200312 handle_options(argc, argv);
313
Harald Welte66f793a2010-05-13 11:50:04 +0200314 rate_ctr_init(tall_bsc_ctx);
Harald Weltedcccb182010-05-16 20:52:23 +0200315 rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245);
316 if (rc < 0)
317 exit(1);
Harald Welte288be162010-05-01 16:48:27 +0200318
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +0100319 ctrl = sgsn_controlif_setup(NULL, 4251);
320 if (!ctrl) {
321 LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
322 exit(1);
323 }
324
325 if (sgsn_ctrl_cmds_install() != 0) {
326 LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
327 exit(1);
328 }
329
Harald Welte68d85d52012-06-16 17:45:59 +0800330 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800331 bssgp_set_log_ss(DBSSGP);
Harald Welte68d85d52012-06-16 17:45:59 +0800332
Harald Weltefdd8b3b2012-06-16 16:54:06 +0800333 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_bsc_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200334 if (!sgsn_nsi) {
335 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
336 exit(1);
337 }
Harald Welte2720e732010-05-17 00:44:57 +0200338 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200339
340 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
341
Harald Welte66f793a2010-05-13 11:50:04 +0200342 gprs_ns_vty_init(bssgp_nsi);
Harald Weltece95b272012-06-17 13:04:02 +0800343 bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200344 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200345 gprs_sndcp_vty_init();
Harald Weltea6a20b42012-06-16 16:40:42 +0800346 /* FIXME: register signal handler for SS_L_NS */
Harald Welte66f793a2010-05-13 11:50:04 +0200347
Harald Welte2720e732010-05-17 00:44:57 +0200348 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200349 if (rc < 0) {
350 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
351 exit(2);
352 }
353
Harald Welte2720e732010-05-17 00:44:57 +0200354 rc = sgsn_gtp_init(&sgsn_inst);
Harald Welte6efc1762010-05-19 15:46:31 +0200355 if (rc) {
356 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200357 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200358 }
Harald Welte269ae752010-05-18 14:44:31 +0200359
Harald Welteff3bde82010-05-19 15:09:09 +0200360 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200361 if (rc < 0) {
362 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200363 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200364 }
365
366 rc = gprs_ns_frgre_listen(sgsn_nsi);
367 if (rc < 0) {
368 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
369 "socket. Do you have CAP_NET_RAW?\n");
370 exit(2);
371 }
Harald Welte288be162010-05-01 16:48:27 +0200372
Harald Welte9ae28a12010-08-27 09:26:44 +0200373 if (daemonize) {
374 rc = osmo_daemonize();
375 if (rc < 0) {
376 perror("Error during daemonize");
377 exit(1);
378 }
379 }
380
Harald Welte288be162010-05-01 16:48:27 +0200381 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200382 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200383 if (rc < 0)
384 exit(3);
385 }
386
Harald Weltebaf77002011-07-16 13:47:01 +0200387 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200388 exit(0);
389}