blob: 0db90d5c3c68a427dbc5bdbc797d7c5a5684bd4c [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
Harald Welte288be162010-05-01 16:48:27 +020061#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 = "./",
Jacob Erlbeck106f5472014-11-04 10:08:37 +010078 .auth_policy = SGSN_AUTH_POLICY_CLOSED,
Harald Welte2720e732010-05-17 00:44:57 +020079 },
80};
Harald Welte8fc1a462010-05-17 00:53:10 +020081struct sgsn_instance *sgsn = &sgsn_inst;
Harald Welte288be162010-05-01 16:48:27 +020082
83/* call-back function for the NS protocol */
84static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 struct msgb *msg, uint16_t bvci)
Harald Welte288be162010-05-01 16:48:27 +020086{
87 int rc = 0;
88
89 switch (event) {
90 case GPRS_NS_EVT_UNIT_DATA:
91 /* hand the message into the BSSGP implementation */
Harald Weltece95b272012-06-17 13:04:02 +080092 rc = bssgp_rcvmsg(msg);
Harald Welte288be162010-05-01 16:48:27 +020093 break;
94 default:
95 LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event);
96 if (msg)
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +010097 msgb_free(msg);
Harald Welte288be162010-05-01 16:48:27 +020098 rc = -EIO;
99 break;
100 }
101 return rc;
102}
103
Harald Welteb1fd9022012-06-17 12:16:31 +0800104/* call-back function for the BSSGP protocol */
105int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
106{
107 struct osmo_bssgp_prim *bp;
108 bp = container_of(oph, struct osmo_bssgp_prim, oph);
109
110 switch (oph->sap) {
111 case SAP_BSSGP_LL:
112 switch (oph->primitive) {
113 case PRIM_BSSGP_UL_UD:
114 return gprs_llc_rcvmsg(oph->msg, bp->tp);
115 }
116 break;
117 case SAP_BSSGP_GMM:
118 switch (oph->primitive) {
119 case PRIM_BSSGP_GMM_SUSPEND:
120 return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli);
121 case PRIM_BSSGP_GMM_RESUME:
122 return gprs_gmm_rx_resume(bp->ra_id, bp->tlli,
Holger Hans Peter Freyther9b5db282012-08-03 10:01:57 +0200123 bp->u.resume.suspend_ref);
Harald Welteb1fd9022012-06-17 12:16:31 +0800124 }
125 break;
126 case SAP_BSSGP_NM:
127 break;
128 }
129 return 0;
130}
131
Harald Welte66f793a2010-05-13 11:50:04 +0200132static void signal_handler(int signal)
133{
134 fprintf(stdout, "signal %u received\n", signal);
135
136 switch (signal) {
137 case SIGINT:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200138 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte66f793a2010-05-13 11:50:04 +0200139 sleep(1);
140 exit(0);
141 break;
142 case SIGABRT:
143 /* in case of abort, we want to obtain a talloc report
144 * and then return to the caller, who will abort the process */
145 case SIGUSR1:
146 talloc_report(tall_vty_ctx, stderr);
147 talloc_report_full(tall_bsc_ctx, stderr);
148 break;
149 case SIGUSR2:
150 talloc_report_full(tall_vty_ctx, stderr);
151 break;
152 default:
153 break;
154 }
155}
156
Harald Welted6c74162010-05-02 21:29:17 +0200157/* NSI that BSSGP uses when transmitting on NS */
158extern struct gprs_ns_inst *bssgp_nsi;
Harald Welte66f793a2010-05-13 11:50:04 +0200159extern void *tall_msgb_ctx;
Harald Welte288be162010-05-01 16:48:27 +0200160
Holger Hans Peter Freyther57da4472010-06-08 16:11:06 +0800161extern enum node_type bsc_vty_go_parent(struct vty *vty);
Harald Weltec31f4802010-05-25 23:31:39 +0200162
163static struct vty_app_info vty_info = {
Harald Welteec1921d2011-02-24 23:57:06 +0100164 .name = "OsmoSGSN",
Harald Weltec31f4802010-05-25 23:31:39 +0200165 .version = PACKAGE_VERSION,
166 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther81506b42010-09-04 11:00:01 +0800167 .is_config_node = bsc_vty_is_config_node,
Harald Weltec31f4802010-05-25 23:31:39 +0200168};
169
Harald Welte9ae28a12010-08-27 09:26:44 +0200170static void print_help(void)
171{
172 printf("Some useful help...\n");
173 printf(" -h --help\tthis text\n");
174 printf(" -D --daemonize\tFork the process into a background daemon\n");
175 printf(" -d option --debug\tenable Debugging\n");
176 printf(" -s --disable-color\n");
177 printf(" -c --config-file\tThe config file to use\n");
178 printf(" -e --log-level number\tSet a global log level\n");
179}
180
181static void handle_options(int argc, char **argv)
182{
183 while (1) {
184 int option_index = 0, c;
185 static struct option long_options[] = {
186 {"help", 0, 0, 'h'},
187 {"debug", 1, 0, 'd'},
188 {"daemonize", 0, 0, 'D'},
189 {"config-file", 1, 0, 'c'},
190 {"disable-color", 0, 0, 's'},
191 {"timestamp", 0, 0, 'T'},
192 {"log-level", 1, 0, 'e'},
193 {NULL, 0, 0, 0}
194 };
195
196 c = getopt_long(argc, argv, "hd:Dc:sTe:",
197 long_options, &option_index);
198 if (c == -1)
199 break;
200
201 switch (c) {
202 case 'h':
203 //print_usage();
204 print_help();
205 exit(0);
206 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200207 log_set_use_color(osmo_stderr_target, 0);
Harald Welte9ae28a12010-08-27 09:26:44 +0200208 break;
209 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200210 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte9ae28a12010-08-27 09:26:44 +0200211 break;
212 case 'D':
213 daemonize = 1;
214 break;
215 case 'c':
216 sgsn_inst.config_file = strdup(optarg);
217 break;
218 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200219 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte9ae28a12010-08-27 09:26:44 +0200220 break;
221 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200222 log_set_log_level(osmo_stderr_target, atoi(optarg));
Harald Welte9ae28a12010-08-27 09:26:44 +0200223 break;
224 default:
225 /* ignore */
226 break;
227 }
228 }
229}
230
Harald Welte11461a62012-06-17 12:58:46 +0800231/* default categories */
232static struct log_info_cat gprs_categories[] = {
233 [DMM] = {
234 .name = "DMM",
235 .description = "Layer3 Mobility Management (MM)",
236 .color = "\033[1;33m",
237 .enabled = 1, .loglevel = LOGL_NOTICE,
238 },
Harald Welte11461a62012-06-17 12:58:46 +0800239 [DPAG] = {
240 .name = "DPAG",
241 .description = "Paging Subsystem",
242 .color = "\033[1;38m",
243 .enabled = 1, .loglevel = LOGL_NOTICE,
244 },
245 [DMEAS] = {
246 .name = "DMEAS",
247 .description = "Radio Measurement Processing",
248 .enabled = 0, .loglevel = LOGL_NOTICE,
249 },
250 [DREF] = {
251 .name = "DREF",
252 .description = "Reference Counting",
253 .enabled = 0, .loglevel = LOGL_NOTICE,
254 },
255 [DGPRS] = {
256 .name = "DGPRS",
257 .description = "GPRS Packet Service",
258 .enabled = 1, .loglevel = LOGL_DEBUG,
259 },
260 [DNS] = {
261 .name = "DNS",
262 .description = "GPRS Network Service (NS)",
263 .enabled = 1, .loglevel = LOGL_INFO,
264 },
265 [DBSSGP] = {
266 .name = "DBSSGP",
267 .description = "GPRS BSS Gateway Protocol (BSSGP)",
268 .enabled = 1, .loglevel = LOGL_DEBUG,
269 },
270 [DLLC] = {
271 .name = "DLLC",
272 .description = "GPRS Logical Link Control Protocol (LLC)",
273 .enabled = 1, .loglevel = LOGL_DEBUG,
274 },
275 [DSNDCP] = {
276 .name = "DSNDCP",
277 .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)",
278 .enabled = 1, .loglevel = LOGL_DEBUG,
279 },
280};
281
282static const struct log_info gprs_log_info = {
283 .filter_fn = gprs_log_filter_fn,
284 .cat = gprs_categories,
285 .num_cat = ARRAY_SIZE(gprs_categories),
286};
287
288
Harald Welte288be162010-05-01 16:48:27 +0200289int main(int argc, char **argv)
290{
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +0100291 struct ctrl_handle *ctrl;
Harald Welte288be162010-05-01 16:48:27 +0200292 struct gsm_network dummy_network;
Harald Welte288be162010-05-01 16:48:27 +0200293 int rc;
294
295 tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn");
Harald Welte66f793a2010-05-13 11:50:04 +0200296 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
297
298 signal(SIGINT, &signal_handler);
299 signal(SIGABRT, &signal_handler);
300 signal(SIGUSR1, &signal_handler);
301 signal(SIGUSR2, &signal_handler);
Harald Welte288be162010-05-01 16:48:27 +0200302
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200303 osmo_init_ignore_signals();
Harald Welte11461a62012-06-17 12:58:46 +0800304 osmo_init_logging(&gprs_log_info);
Harald Welte288be162010-05-01 16:48:27 +0200305
Harald Weltec31f4802010-05-25 23:31:39 +0200306 vty_info.copyright = openbsc_copyright;
307 vty_init(&vty_info);
Harald Welte11461a62012-06-17 12:58:46 +0800308 logging_vty_add_cmds(&gprs_log_info);
Harald Weltedcccb182010-05-16 20:52:23 +0200309 sgsn_vty_init();
310
Harald Welte9ae28a12010-08-27 09:26:44 +0200311 handle_options(argc, argv);
312
Harald Welte66f793a2010-05-13 11:50:04 +0200313 rate_ctr_init(tall_bsc_ctx);
Harald Welte55dc31e2014-08-24 17:54:49 +0200314 rc = telnet_init(tall_bsc_ctx, &dummy_network, OSMO_VTY_PORT_SGSN);
Harald Weltedcccb182010-05-16 20:52:23 +0200315 if (rc < 0)
316 exit(1);
Harald Welte288be162010-05-01 16:48:27 +0200317
Harald Welte55dc31e2014-08-24 17:54:49 +0200318 ctrl = sgsn_controlif_setup(NULL, OSMO_CTRL_PORT_SGSN);
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +0100319 if (!ctrl) {
320 LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n");
321 exit(1);
322 }
323
324 if (sgsn_ctrl_cmds_install() != 0) {
325 LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n");
326 exit(1);
327 }
328
Harald Welte68d85d52012-06-16 17:45:59 +0800329 gprs_ns_set_log_ss(DNS);
Harald Weltece95b272012-06-17 13:04:02 +0800330 bssgp_set_log_ss(DBSSGP);
Harald Welte68d85d52012-06-16 17:45:59 +0800331
Harald Weltefdd8b3b2012-06-16 16:54:06 +0800332 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_bsc_ctx);
Harald Welte288be162010-05-01 16:48:27 +0200333 if (!sgsn_nsi) {
334 LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n");
335 exit(1);
336 }
Harald Welte2720e732010-05-17 00:44:57 +0200337 bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi;
Harald Welte496aee42010-06-30 19:59:55 +0200338
339 gprs_llc_init("/usr/local/lib/osmocom/crypt/");
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100340 sgsn_inst_init();
Harald Welte496aee42010-06-30 19:59:55 +0200341
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();
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100346 sgsn_auth_init();
Harald Weltea6a20b42012-06-16 16:40:42 +0800347 /* FIXME: register signal handler for SS_L_NS */
Harald Welte66f793a2010-05-13 11:50:04 +0200348
Harald Welte2720e732010-05-17 00:44:57 +0200349 rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg);
Harald Welte66f793a2010-05-13 11:50:04 +0200350 if (rc < 0) {
351 LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n");
352 exit(2);
353 }
354
Harald Welte2720e732010-05-17 00:44:57 +0200355 rc = sgsn_gtp_init(&sgsn_inst);
Harald Welte6efc1762010-05-19 15:46:31 +0200356 if (rc) {
357 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n");
Harald Welte269ae752010-05-18 14:44:31 +0200358 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200359 }
Harald Welte269ae752010-05-18 14:44:31 +0200360
Jacob Erlbeck39f040d2014-12-18 12:46:47 +0100361 rc = gprs_subscr_init(&sgsn_inst);
362 if (rc < 0) {
363 LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n");
364 exit(2);
365 }
366
Harald Welteff3bde82010-05-19 15:09:09 +0200367 rc = gprs_ns_nsip_listen(sgsn_nsi);
Harald Welte6efc1762010-05-19 15:46:31 +0200368 if (rc < 0) {
369 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n");
Harald Welteff3bde82010-05-19 15:09:09 +0200370 exit(2);
Harald Welte6efc1762010-05-19 15:46:31 +0200371 }
372
373 rc = gprs_ns_frgre_listen(sgsn_nsi);
374 if (rc < 0) {
375 LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE "
376 "socket. Do you have CAP_NET_RAW?\n");
377 exit(2);
378 }
Harald Welte288be162010-05-01 16:48:27 +0200379
Harald Welte9ae28a12010-08-27 09:26:44 +0200380 if (daemonize) {
381 rc = osmo_daemonize();
382 if (rc < 0) {
383 perror("Error during daemonize");
384 exit(1);
385 }
386 }
387
Harald Welte288be162010-05-01 16:48:27 +0200388 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200389 rc = osmo_select_main(0);
Harald Welte288be162010-05-01 16:48:27 +0200390 if (rc < 0)
391 exit(3);
392 }
393
Harald Weltebaf77002011-07-16 13:47:01 +0200394 /* not reached */
Harald Welte288be162010-05-01 16:48:27 +0200395 exit(0);
396}