blob: 0310cb2b25f58a9ce2adfd535ad0c2c841d5fdf0 [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 Welte55dc31e2014-08-24 17:54:49 +020046#include <osmocom/vty/ports.h>
Harald Welte4b037e42010-05-19 19:45:32 +020047
Harald Welte288be162010-05-01 16:48:27 +020048#include <openbsc/signal.h>
49#include <openbsc/debug.h>
Harald Welte288be162010-05-01 16:48:27 +020050#include <openbsc/vty.h>
51#include <openbsc/sgsn.h>
Harald Welte2e918a82010-05-18 12:20:12 +020052#include <openbsc/gprs_llc.h>
Harald Welteb1fd9022012-06-17 12:16:31 +080053#include <openbsc/gprs_gmm.h>
Harald Welteba874b82014-08-20 23:47:15 +020054#include <osmocom/ctrl/control_if.h>
Harald Welte55dc31e2014-08-24 17:54:49 +020055#include <osmocom/ctrl/ports.h>
Harald Welte288be162010-05-01 16:48:27 +020056
Harald Welte2720e732010-05-17 00:44:57 +020057#include <gtp.h>
58
Harald Weltee2365962010-05-04 07:41:59 +020059#include "../../bscconfig.h"
Harald Welte288be162010-05-01 16:48:27 +020060
61/* this is here for the vty... it will never be called */
62void subscr_put() { abort(); }
63
64#define _GNU_SOURCE
65#include <getopt.h>
66
67void *tall_bsc_ctx;
68
69struct gprs_ns_inst *sgsn_nsi;
Harald Welte9ae28a12010-08-27 09:26:44 +020070static int daemonize = 0;
Harald Welte288be162010-05-01 16:48:27 +020071const char *openbsc_copyright =
Holger Hans Peter Freyther5f540752010-09-11 13:32:30 +080072 "Copyright (C) 2010 Harald Welte and On-Waves\r\n"
Harald Welte9af6ddf2011-01-01 15:25:50 +010073 "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 +080074 "This is free software: you are free to change and redistribute it.\r\n"
75 "There is NO WARRANTY, to the extent permitted by law.\r\n";
Harald Welte288be162010-05-01 16:48:27 +020076
Harald Welte8fc1a462010-05-17 00:53:10 +020077static struct sgsn_instance sgsn_inst = {
Harald Welte2720e732010-05-17 00:44:57 +020078 .config_file = "osmo_sgsn.cfg",
79 .cfg = {
80 .gtp_statedir = "./",
Jacob Erlbeck106f5472014-11-04 10:08:37 +010081 .auth_policy = SGSN_AUTH_POLICY_CLOSED,
Harald Welte2720e732010-05-17 00:44:57 +020082 },
83};
Harald Welte8fc1a462010-05-17 00:53:10 +020084struct sgsn_instance *sgsn = &sgsn_inst;
Harald Welte288be162010-05-01 16:48:27 +020085
86/* call-back function for the NS protocol */
87static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020088 struct msgb *msg, uint16_t bvci)
Harald Welte288be162010-05-01 16:48:27 +020089{
90 int rc = 0;
91
92 switch (event) {
93 case GPRS_NS_EVT_UNIT_DATA:
94 /* hand the message into the BSSGP implementation */
Harald Weltece95b272012-06-17 13:04:02 +080095 rc = bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +020096 break;
97 default:
98 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
99 if (msg)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100100 msgb_free(msg);
Harald Welte288be162010-05-01 16:48:27 +0200101 rc = -EIO;
102 break;
103 }
104 return rc;
105}
106
Harald Welteb1fd9022012-06-17 12:16:31 +0800107/* call-back function for the BSSGP protocol */
108int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
109{
110 struct osmo_bssgp_prim *bp;
111 bp = container_of(oph, struct osmo_bssgp_prim, oph);
112
113 switch (oph->sap) {
114 case SAP_BSSGP_LL:
115 switch (oph->primitive) {
116 case PRIM_BSSGP_UL_UD:
117 return gprs_llc_rcvmsg(oph->msg, bp->tp);
118 }
119 break;
120 case SAP_BSSGP_GMM:
121 switch (oph->primitive) {
122 case PRIM_BSSGP_GMM_SUSPEND:
123 return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli);
124 case PRIM_BSSGP_GMM_RESUME:
125 return gprs_gmm_rx_resume(bp->ra_id, bp->tlli,
Holger Hans Peter Freyther9b5db282012-08-03 10:01:57 +0200126 bp->u.resume.suspend_ref);
Harald Welteb1fd9022012-06-17 12:16:31 +0800127 }
128 break;
129 case SAP_BSSGP_NM:
130 break;
131 }
132 return 0;
133}
134
Harald Welte66f793a2010-05-13 11:50:04 +0200135static void signal_handler(int signal)
136{
137 fprintf(stdout, "signal %u received\n", signal);
138
139 switch (signal) {
140 case SIGINT:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200141 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200142 sleep(1);
143 exit(0);
144 break;
145 case SIGABRT:
146 /* in case of abort, we want to obtain a talloc report
147 * and then return to the caller, who will abort the process */
148 case SIGUSR1:
149 talloc_report(tall_vty_ctx, stderr);
150 talloc_report_full(tall_bsc_ctx, stderr);
151 break;
152 case SIGUSR2:
153 talloc_report_full(tall_vty_ctx, stderr);
154 break;
155 default:
156 break;
157 }
158}
159
Harald Welted6c74162010-05-02 21:29:17 +0200160/* NSI that BSSGP uses when transmitting on NS */
161extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200162extern void *tall_msgb_ctx;
Harald Welte288be162010-05-01 16:48:27 +0200163
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +0800164extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200165
166static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100167 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200168 .version = PACKAGE_VERSION,
169 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +0800170 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200171};
172
Harald Welte9ae28a12010-08-27 09:26:44 +0200173static void print_help(void)
174{
175 printf("Some useful help...\n");
176 printf(" -h --help\tthis text\n");
177 printf(" -D --daemonize\tFork the process into a background daemon\n");
178 printf(" -d option --debug\tenable Debugging\n");
179 printf(" -s --disable-color\n");
180 printf(" -c --config-file\tThe config file to use\n");
181 printf(" -e --log-level number\tSet a global log level\n");
182}
183
184static void handle_options(int argc, char **argv)
185{
186 while (1) {
187 int option_index = 0, c;
188 static struct option long_options[] = {
189 {"help", 0, 0, 'h'},
190 {"debug", 1, 0, 'd'},
191 {"daemonize", 0, 0, 'D'},
192 {"config-file", 1, 0, 'c'},
193 {"disable-color", 0, 0, 's'},
194 {"timestamp", 0, 0, 'T'},
195 {"log-level", 1, 0, 'e'},
196 {NULL, 0, 0, 0}
197 };
198
199 c = getopt_long(argc, argv, "hd:Dc:sTe:",
200 long_options, &option_index);
201 if (c == -1)
202 break;
203
204 switch (c) {
205 case 'h':
206 //print_usage();
207 print_help();
208 exit(0);
209 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200210 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200211 break;
212 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200213 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200214 break;
215 case 'D':
216 daemonize = 1;
217 break;
218 case 'c':
219 sgsn_inst.config_file = strdup(optarg);
220 break;
221 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200222 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200223 break;
224 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200225 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200226 break;
227 default:
228 /* ignore */
229 break;
230 }
231 }
232}
233
Harald Welte11461a62012-06-17 12:58:46 +0800234/* default categories */
235static struct log_info_cat gprs_categories[] = {
236 [DMM] = {
237 .name = "DMM",
238 .description = "Layer3 Mobility Management (MM)",
239 .color = "\033[1;33m",
240 .enabled = 1, .loglevel = LOGL_NOTICE,
241 },
Harald Welte11461a62012-06-17 12:58:46 +0800242 [DPAG] = {
243 .name = "DPAG",
244 .description = "Paging Subsystem",
245 .color = "\033[1;38m",
246 .enabled = 1, .loglevel = LOGL_NOTICE,
247 },
248 [DMEAS] = {
249 .name = "DMEAS",
250 .description = "Radio Measurement Processing",
251 .enabled = 0, .loglevel = LOGL_NOTICE,
252 },
253 [DREF] = {
254 .name = "DREF",
255 .description = "Reference Counting",
256 .enabled = 0, .loglevel = LOGL_NOTICE,
257 },
258 [DGPRS] = {
259 .name = "DGPRS",
260 .description = "GPRS Packet Service",
261 .enabled = 1, .loglevel = LOGL_DEBUG,
262 },
263 [DNS] = {
264 .name = "DNS",
265 .description = "GPRS Network Service (NS)",
266 .enabled = 1, .loglevel = LOGL_INFO,
267 },
268 [DBSSGP] = {
269 .name = "DBSSGP",
270 .description = "GPRS BSS Gateway Protocol (BSSGP)",
271 .enabled = 1, .loglevel = LOGL_DEBUG,
272 },
273 [DLLC] = {
274 .name = "DLLC",
275 .description = "GPRS Logical Link Control Protocol (LLC)",
276 .enabled = 1, .loglevel = LOGL_DEBUG,
277 },
278 [DSNDCP] = {
279 .name = "DSNDCP",
280 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
281 .enabled = 1, .loglevel = LOGL_DEBUG,
282 },
283};
284
285static const struct log_info gprs_log_info = {
286 .filter_fn = gprs_log_filter_fn,
287 .cat = gprs_categories,
288 .num_cat = ARRAY_SIZE(gprs_categories),
289};
290
291
Harald Welte288be162010-05-01 16:48:27 +0200292int main(int argc, char **argv)
293{
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +0100294 struct ctrl_handle *ctrl;
Harald Welte288be162010-05-01 16:48:27 +0200295 struct gsm_network dummy_network;
Harald Welte288be162010-05-01 16:48:27 +0200296 int rc;
297
298 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200299 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
300
301 signal(SIGINT, &signal_handler);
302 signal(SIGABRT, &signal_handler);
303 signal(SIGUSR1, &signal_handler);
304 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200305
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200306 osmo_init_ignore_signals();
Harald Welte11461a62012-06-17 12:58:46 +0800307 osmo_init_logging(&gprs_log_info);
Harald Welte288be162010-05-01 16:48:27 +0200308
Harald Weltec31f4802010-05-25 23:31:39 +0200309 vty_info.copyright = openbsc_copyright;
310 vty_init(&vty_info);
Harald Welte11461a62012-06-17 12:58:46 +0800311 logging_vty_add_cmds(&gprs_log_info);
Harald Weltedcccb182010-05-16 20:52:23 +0200312 sgsn_vty_init();
313
Harald Welte9ae28a12010-08-27 09:26:44 +0200314 handle_options(argc, argv);
315
Harald Welte66f793a2010-05-13 11:50:04 +0200316 rate_ctr_init(tall_bsc_ctx);
Harald Welte55dc31e2014-08-24 17:54:49 +0200317 rc = telnet_init(tall_bsc_ctx, &dummy_network, OSMO_VTY_PORT_SGSN);
Harald Weltedcccb182010-05-16 20:52:23 +0200318 if (rc < 0)
319 exit(1);
Harald Welte288be162010-05-01 16:48:27 +0200320
Harald Welte55dc31e2014-08-24 17:54:49 +0200321 ctrl = sgsn_controlif_setup(NULL, OSMO_CTRL_PORT_SGSN);
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +0100322 if (!ctrl) {
323 LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
324 exit(1);
325 }
326
327 if (sgsn_ctrl_cmds_install() != 0) {
328 LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
329 exit(1);
330 }
331
Harald Welte68d85d52012-06-16 17:45:59 +0800332 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800333 bssgp_set_log_ss(DBSSGP);
Harald Welte68d85d52012-06-16 17:45:59 +0800334
Harald Weltefdd8b3b2012-06-16 16:54:06 +0800335 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_bsc_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200336 if (!sgsn_nsi) {
337 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
338 exit(1);
339 }
Harald Welte2720e732010-05-17 00:44:57 +0200340 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200341
342 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
343
Harald Welte66f793a2010-05-13 11:50:04 +0200344 gprs_ns_vty_init(bssgp_nsi);
Harald Weltece95b272012-06-17 13:04:02 +0800345 bssgp_vty_init();
Harald Welte2e918a82010-05-18 12:20:12 +0200346 gprs_llc_vty_init();
Harald Weltef78a3b22010-06-30 17:21:19 +0200347 gprs_sndcp_vty_init();
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100348 sgsn_auth_init();
Harald Weltea6a20b42012-06-16 16:40:42 +0800349 /* FIXME: register signal handler for SS_L_NS */
Harald Welte66f793a2010-05-13 11:50:04 +0200350
Harald Welte2720e732010-05-17 00:44:57 +0200351 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200352 if (rc < 0) {
353 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
354 exit(2);
355 }
356
Harald Welte2720e732010-05-17 00:44:57 +0200357 rc = sgsn_gtp_init(&sgsn_inst);
Harald Welte6efc1762010-05-19 15:46:31 +0200358 if (rc) {
359 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200360 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200361 }
Harald Welte269ae752010-05-18 14:44:31 +0200362
Harald Welteff3bde82010-05-19 15:09:09 +0200363 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200364 if (rc < 0) {
365 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200366 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200367 }
368
369 rc = gprs_ns_frgre_listen(sgsn_nsi);
370 if (rc < 0) {
371 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
372 "socket. Do you have CAP_NET_RAW?\n");
373 exit(2);
374 }
Harald Welte288be162010-05-01 16:48:27 +0200375
Harald Welte9ae28a12010-08-27 09:26:44 +0200376 if (daemonize) {
377 rc = osmo_daemonize();
378 if (rc < 0) {
379 perror("Error during daemonize");
380 exit(1);
381 }
382 }
383
Harald Welte288be162010-05-01 16:48:27 +0200384 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200385 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200386 if (rc < 0)
387 exit(3);
388 }
389
Harald Weltebaf77002011-07-16 13:47:01 +0200390 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200391 exit(0);
392}