Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 1 | /* GPRS SGSN Implementation */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 85531cc | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * 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 Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 10 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * 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 Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <unistd.h> |
| 23 | #include <stdio.h> |
Max | 4011e72 | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 24 | #include <time.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <getopt.h> |
| 28 | #include <errno.h> |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 29 | #include <signal.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 30 | #include <sys/fcntl.h> |
| 31 | #include <sys/stat.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 32 | #include <sys/socket.h> |
| 33 | #include <netinet/in.h> |
| 34 | #include <arpa/inet.h> |
| 35 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 36 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 37 | #include <osmocom/core/talloc.h> |
| 38 | #include <osmocom/core/select.h> |
| 39 | #include <osmocom/core/rate_ctr.h> |
| 40 | #include <osmocom/core/logging.h> |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 41 | #include <osmocom/core/stats.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 42 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 43 | #include <osmocom/gsm/gsup.h> |
| 44 | |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 45 | #include <osmocom/gprs/gprs_ns.h> |
| 46 | #include <osmocom/gprs/gprs_bssgp.h> |
| 47 | |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 48 | #include <osmocom/vty/telnet_interface.h> |
Pablo Neira Ayuso | 3848770 | 2011-03-23 08:49:00 +0100 | [diff] [blame] | 49 | #include <osmocom/vty/logging.h> |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 50 | #include <osmocom/vty/stats.h> |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 51 | #include <osmocom/vty/ports.h> |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 52 | |
Neels Hofmeyr | 7382815 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 53 | #include <osmocom/ctrl/control_vty.h> |
| 54 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 55 | #include <openbsc/signal.h> |
| 56 | #include <openbsc/debug.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 57 | #include <openbsc/vty.h> |
| 58 | #include <openbsc/sgsn.h> |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 59 | #include <openbsc/gprs_llc.h> |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 60 | #include <openbsc/gprs_gmm.h> |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 61 | |
Harald Welte | ba874b8 | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 62 | #include <osmocom/ctrl/control_if.h> |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 63 | #include <osmocom/ctrl/ports.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 64 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 65 | #include <osmocom/sigtran/protocol/m3ua.h> |
| 66 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 67 | #include <gtp.h> |
| 68 | |
Harald Welte | e236596 | 2010-05-04 07:41:59 +0200 | [diff] [blame] | 69 | #include "../../bscconfig.h" |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 70 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 71 | #if BUILD_IU |
| 72 | #include <osmocom/ranap/iu_client.h> |
| 73 | #endif |
| 74 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 75 | #define _GNU_SOURCE |
| 76 | #include <getopt.h> |
| 77 | |
| 78 | void *tall_bsc_ctx; |
| 79 | |
| 80 | struct gprs_ns_inst *sgsn_nsi; |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 81 | static int daemonize = 0; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 82 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 5f54075 | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 83 | "Copyright (C) 2010 Harald Welte and On-Waves\r\n" |
Harald Welte | 69ca8f0 | 2015-10-29 10:35:24 +0100 | [diff] [blame] | 84 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
Holger Hans Peter Freyther | 5f54075 | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 85 | "This is free software: you are free to change and redistribute it.\r\n" |
| 86 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 87 | |
Harald Welte | 8fc1a46 | 2010-05-17 00:53:10 +0200 | [diff] [blame] | 88 | static struct sgsn_instance sgsn_inst = { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 89 | .config_file = "osmo_sgsn.cfg", |
| 90 | .cfg = { |
| 91 | .gtp_statedir = "./", |
Jacob Erlbeck | 106f547 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 92 | .auth_policy = SGSN_AUTH_POLICY_CLOSED, |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 93 | .gsup_server_port = OSMO_GSUP_PORT, |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 94 | }, |
| 95 | }; |
Harald Welte | 8fc1a46 | 2010-05-17 00:53:10 +0200 | [diff] [blame] | 96 | struct sgsn_instance *sgsn = &sgsn_inst; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 97 | |
| 98 | /* call-back function for the NS protocol */ |
| 99 | static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 100 | struct msgb *msg, uint16_t bvci) |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 101 | { |
| 102 | int rc = 0; |
| 103 | |
| 104 | switch (event) { |
| 105 | case GPRS_NS_EVT_UNIT_DATA: |
| 106 | /* hand the message into the BSSGP implementation */ |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 107 | rc = bssgp_rcvmsg(msg); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 108 | break; |
| 109 | default: |
| 110 | LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event); |
| 111 | if (msg) |
Holger Hans Peter Freyther | a8ddb08 | 2012-03-01 20:30:32 +0100 | [diff] [blame] | 112 | msgb_free(msg); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 113 | rc = -EIO; |
| 114 | break; |
| 115 | } |
| 116 | return rc; |
| 117 | } |
| 118 | |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 119 | /* call-back function for the BSSGP protocol */ |
| 120 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 121 | { |
| 122 | struct osmo_bssgp_prim *bp; |
| 123 | bp = container_of(oph, struct osmo_bssgp_prim, oph); |
| 124 | |
| 125 | switch (oph->sap) { |
| 126 | case SAP_BSSGP_LL: |
| 127 | switch (oph->primitive) { |
| 128 | case PRIM_BSSGP_UL_UD: |
| 129 | return gprs_llc_rcvmsg(oph->msg, bp->tp); |
| 130 | } |
| 131 | break; |
| 132 | case SAP_BSSGP_GMM: |
| 133 | switch (oph->primitive) { |
| 134 | case PRIM_BSSGP_GMM_SUSPEND: |
| 135 | return gprs_gmm_rx_suspend(bp->ra_id, bp->tlli); |
| 136 | case PRIM_BSSGP_GMM_RESUME: |
| 137 | return gprs_gmm_rx_resume(bp->ra_id, bp->tlli, |
Holger Hans Peter Freyther | 9b5db28 | 2012-08-03 10:01:57 +0200 | [diff] [blame] | 138 | bp->u.resume.suspend_ref); |
Harald Welte | b1fd902 | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 139 | } |
| 140 | break; |
| 141 | case SAP_BSSGP_NM: |
| 142 | break; |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 147 | static void signal_handler(int signal) |
| 148 | { |
| 149 | fprintf(stdout, "signal %u received\n", signal); |
| 150 | |
| 151 | switch (signal) { |
| 152 | case SIGINT: |
Harald Welte | 28cd944 | 2017-08-20 20:50:06 +0200 | [diff] [blame] | 153 | case SIGTERM: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 154 | osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 155 | sleep(1); |
| 156 | exit(0); |
| 157 | break; |
| 158 | case SIGABRT: |
| 159 | /* in case of abort, we want to obtain a talloc report |
| 160 | * and then return to the caller, who will abort the process */ |
| 161 | case SIGUSR1: |
| 162 | talloc_report(tall_vty_ctx, stderr); |
| 163 | talloc_report_full(tall_bsc_ctx, stderr); |
| 164 | break; |
| 165 | case SIGUSR2: |
| 166 | talloc_report_full(tall_vty_ctx, stderr); |
| 167 | break; |
| 168 | default: |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
Harald Welte | d6c7416 | 2010-05-02 21:29:17 +0200 | [diff] [blame] | 173 | /* NSI that BSSGP uses when transmitting on NS */ |
| 174 | extern struct gprs_ns_inst *bssgp_nsi; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 175 | |
Holger Hans Peter Freyther | 3e4e8cf | 2015-09-24 15:38:38 +0200 | [diff] [blame] | 176 | extern int bsc_vty_go_parent(struct vty *vty); |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 177 | |
| 178 | static struct vty_app_info vty_info = { |
Harald Welte | ec1921d | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 179 | .name = "OsmoSGSN", |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 180 | .version = PACKAGE_VERSION, |
| 181 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | 81506b4 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 182 | .is_config_node = bsc_vty_is_config_node, |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 183 | }; |
| 184 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 185 | static void print_help(void) |
| 186 | { |
| 187 | printf("Some useful help...\n"); |
| 188 | printf(" -h --help\tthis text\n"); |
| 189 | printf(" -D --daemonize\tFork the process into a background daemon\n"); |
| 190 | printf(" -d option --debug\tenable Debugging\n"); |
| 191 | printf(" -s --disable-color\n"); |
Neels Hofmeyr | 130be40 | 2015-09-24 17:28:47 +0200 | [diff] [blame] | 192 | printf(" -c --config-file\tThe config file to use [%s]\n", sgsn->config_file); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 193 | printf(" -e --log-level number\tSet a global log level\n"); |
| 194 | } |
| 195 | |
| 196 | static void handle_options(int argc, char **argv) |
| 197 | { |
| 198 | while (1) { |
| 199 | int option_index = 0, c; |
| 200 | static struct option long_options[] = { |
| 201 | {"help", 0, 0, 'h'}, |
| 202 | {"debug", 1, 0, 'd'}, |
| 203 | {"daemonize", 0, 0, 'D'}, |
| 204 | {"config-file", 1, 0, 'c'}, |
| 205 | {"disable-color", 0, 0, 's'}, |
| 206 | {"timestamp", 0, 0, 'T'}, |
Philipp Maier | 99c9707 | 2017-03-14 17:32:35 +0100 | [diff] [blame] | 207 | { "version", 0, 0, 'V' }, |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 208 | {"log-level", 1, 0, 'e'}, |
| 209 | {NULL, 0, 0, 0} |
| 210 | }; |
| 211 | |
Philipp Maier | 99c9707 | 2017-03-14 17:32:35 +0100 | [diff] [blame] | 212 | c = getopt_long(argc, argv, "hd:Dc:sTVe:", |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 213 | long_options, &option_index); |
| 214 | if (c == -1) |
| 215 | break; |
| 216 | |
| 217 | switch (c) { |
| 218 | case 'h': |
| 219 | //print_usage(); |
| 220 | print_help(); |
| 221 | exit(0); |
| 222 | case 's': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 223 | log_set_use_color(osmo_stderr_target, 0); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 224 | break; |
| 225 | case 'd': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 226 | log_parse_category_mask(osmo_stderr_target, optarg); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 227 | break; |
| 228 | case 'D': |
| 229 | daemonize = 1; |
| 230 | break; |
| 231 | case 'c': |
| 232 | sgsn_inst.config_file = strdup(optarg); |
| 233 | break; |
| 234 | case 'T': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 235 | log_set_print_timestamp(osmo_stderr_target, 1); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 236 | break; |
Philipp Maier | 99c9707 | 2017-03-14 17:32:35 +0100 | [diff] [blame] | 237 | case 'V': |
| 238 | print_version(1); |
| 239 | exit(0); |
| 240 | break; |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 241 | case 'e': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 242 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 243 | break; |
| 244 | default: |
| 245 | /* ignore */ |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 251 | /* default categories */ |
| 252 | static struct log_info_cat gprs_categories[] = { |
| 253 | [DMM] = { |
| 254 | .name = "DMM", |
| 255 | .description = "Layer3 Mobility Management (MM)", |
| 256 | .color = "\033[1;33m", |
| 257 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 258 | }, |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 259 | [DPAG] = { |
| 260 | .name = "DPAG", |
| 261 | .description = "Paging Subsystem", |
| 262 | .color = "\033[1;38m", |
| 263 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 264 | }, |
| 265 | [DMEAS] = { |
| 266 | .name = "DMEAS", |
| 267 | .description = "Radio Measurement Processing", |
| 268 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 269 | }, |
| 270 | [DREF] = { |
| 271 | .name = "DREF", |
| 272 | .description = "Reference Counting", |
| 273 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 274 | }, |
| 275 | [DGPRS] = { |
| 276 | .name = "DGPRS", |
| 277 | .description = "GPRS Packet Service", |
| 278 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 279 | }, |
| 280 | [DNS] = { |
| 281 | .name = "DNS", |
| 282 | .description = "GPRS Network Service (NS)", |
| 283 | .enabled = 1, .loglevel = LOGL_INFO, |
| 284 | }, |
| 285 | [DBSSGP] = { |
| 286 | .name = "DBSSGP", |
| 287 | .description = "GPRS BSS Gateway Protocol (BSSGP)", |
| 288 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 289 | }, |
| 290 | [DLLC] = { |
| 291 | .name = "DLLC", |
| 292 | .description = "GPRS Logical Link Control Protocol (LLC)", |
| 293 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 294 | }, |
| 295 | [DSNDCP] = { |
| 296 | .name = "DSNDCP", |
| 297 | .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)", |
| 298 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 299 | }, |
Daniel Willmann | 35a65ed | 2016-05-20 21:59:45 +0200 | [diff] [blame] | 300 | [DRANAP] = { |
| 301 | .name = "DRANAP", |
| 302 | .description = "RAN Application Part (RANAP)", |
| 303 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 304 | }, |
Daniel Willmann | 3adb23c | 2016-05-20 21:59:35 +0200 | [diff] [blame] | 305 | [DSUA] = { |
| 306 | .name = "DSUA", |
| 307 | .description = "SCCP User Adaptation (SUA)", |
| 308 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 309 | }, |
Philipp | 2c7f837 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 310 | [DSLHC] = { |
| 311 | .name = "DSLHC", |
| 312 | .description = "RFC1144 TCP/IP Header compression (SLHC)", |
| 313 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 314 | }, |
Philipp | 73f83d5 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 315 | [DV42BIS] = { |
| 316 | .name = "DV42BIS", |
| 317 | .description = "V.42bis data compression (SNDCP)", |
| 318 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 319 | } |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | static const struct log_info gprs_log_info = { |
| 323 | .filter_fn = gprs_log_filter_fn, |
| 324 | .cat = gprs_categories, |
| 325 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 326 | }; |
| 327 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 328 | int sgsn_ranap_iu_event(struct ue_conn_ctx *ctx, enum ranap_iu_event_type type, void *data); |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 329 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 330 | int main(int argc, char **argv) |
| 331 | { |
Holger Hans Peter Freyther | a273030 | 2014-03-23 18:08:26 +0100 | [diff] [blame] | 332 | struct ctrl_handle *ctrl; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 333 | struct gsm_network dummy_network; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 334 | struct osmo_sccp_instance *sccp; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 335 | int rc; |
| 336 | |
Max | 4011e72 | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 337 | srand(time(NULL)); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 338 | tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn"); |
Neels Hofmeyr | 39ae17f | 2016-09-16 01:49:08 +0200 | [diff] [blame] | 339 | msgb_talloc_ctx_init(tall_bsc_ctx, 0); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 340 | |
| 341 | signal(SIGINT, &signal_handler); |
Harald Welte | 28cd944 | 2017-08-20 20:50:06 +0200 | [diff] [blame] | 342 | signal(SIGTERM, &signal_handler); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 343 | signal(SIGABRT, &signal_handler); |
| 344 | signal(SIGUSR1, &signal_handler); |
| 345 | signal(SIGUSR2, &signal_handler); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 346 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 347 | osmo_init_ignore_signals(); |
Harald Welte | 11461a6 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 348 | osmo_init_logging(&gprs_log_info); |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 349 | osmo_stats_init(tall_bsc_ctx); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 350 | |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 351 | vty_info.copyright = openbsc_copyright; |
| 352 | vty_init(&vty_info); |
Max | db0e380 | 2017-01-12 19:35:11 +0100 | [diff] [blame] | 353 | logging_vty_add_cmds(NULL); |
Jacob Erlbeck | 64630cc | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 354 | osmo_stats_vty_add_cmds(&gprs_log_info); |
Neels Hofmeyr | 4283675 | 2017-07-20 14:41:20 +0200 | [diff] [blame] | 355 | sgsn_vty_init(&sgsn_inst.cfg); |
Neels Hofmeyr | 7382815 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 356 | ctrl_vty_init(tall_bsc_ctx); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 357 | osmo_ss7_init(); |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 358 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 359 | handle_options(argc, argv); |
| 360 | |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 361 | rate_ctr_init(tall_bsc_ctx); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 362 | |
Harald Welte | 68d85d5 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 363 | gprs_ns_set_log_ss(DNS); |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 364 | bssgp_set_log_ss(DBSSGP); |
Harald Welte | 68d85d5 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 365 | |
Harald Welte | fdd8b3b | 2012-06-16 16:54:06 +0800 | [diff] [blame] | 366 | sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_bsc_ctx); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 367 | if (!sgsn_nsi) { |
| 368 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 369 | exit(1); |
| 370 | } |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 371 | bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi; |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 372 | |
| 373 | gprs_llc_init("/usr/local/lib/osmocom/crypt/"); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 374 | sgsn_rate_ctr_init(); |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 375 | sgsn_inst_init(); |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 376 | |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 377 | gprs_ns_vty_init(bssgp_nsi); |
Harald Welte | ce95b27 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 378 | bssgp_vty_init(); |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 379 | gprs_llc_vty_init(); |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 380 | gprs_sndcp_vty_init(); |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 381 | sgsn_auth_init(); |
Holger Hans Peter Freyther | 4f5b823 | 2015-05-05 22:25:48 +0200 | [diff] [blame] | 382 | sgsn_cdr_init(&sgsn_inst); |
Harald Welte | a6a20b4 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 383 | /* FIXME: register signal handler for SS_L_NS */ |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 384 | |
Neels Hofmeyr | 4283675 | 2017-07-20 14:41:20 +0200 | [diff] [blame] | 385 | rc = sgsn_parse_config(sgsn_inst.config_file); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 386 | if (rc < 0) { |
Neels Hofmeyr | 27355c9 | 2017-02-24 06:28:31 +0100 | [diff] [blame] | 387 | LOGP(DGPRS, LOGL_FATAL, "Error in config file\n"); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 388 | exit(2); |
| 389 | } |
| 390 | |
Neels Hofmeyr | fa0f715 | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 391 | /* start telnet after reading config for vty_get_bind_addr() */ |
Neels Hofmeyr | fa0f715 | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 392 | rc = telnet_init_dynif(tall_bsc_ctx, &dummy_network, |
| 393 | vty_get_bind_addr(), OSMO_VTY_PORT_SGSN); |
| 394 | if (rc < 0) |
| 395 | exit(1); |
| 396 | |
Neels Hofmeyr | 7382815 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 397 | /* start control interface after reading config for |
| 398 | * ctrl_vty_get_bind_addr() */ |
Neels Hofmeyr | 7382815 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 399 | ctrl = sgsn_controlif_setup(NULL, ctrl_vty_get_bind_addr(), |
| 400 | OSMO_CTRL_PORT_SGSN); |
| 401 | if (!ctrl) { |
| 402 | LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n"); |
| 403 | exit(1); |
| 404 | } |
| 405 | |
| 406 | if (sgsn_ctrl_cmds_install() != 0) { |
| 407 | LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n"); |
| 408 | exit(1); |
| 409 | } |
| 410 | |
| 411 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 412 | rc = sgsn_gtp_init(&sgsn_inst); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 413 | if (rc) { |
| 414 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n"); |
Harald Welte | 269ae75 | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 415 | exit(2); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 416 | } |
Harald Welte | 269ae75 | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 417 | |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 418 | rc = gprs_subscr_init(&sgsn_inst); |
| 419 | if (rc < 0) { |
| 420 | LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n"); |
| 421 | exit(2); |
| 422 | } |
| 423 | |
Harald Welte | ff3bde8 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 424 | rc = gprs_ns_nsip_listen(sgsn_nsi); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 425 | if (rc < 0) { |
| 426 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n"); |
Harald Welte | ff3bde8 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 427 | exit(2); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | rc = gprs_ns_frgre_listen(sgsn_nsi); |
| 431 | if (rc < 0) { |
| 432 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE " |
| 433 | "socket. Do you have CAP_NET_RAW?\n"); |
| 434 | exit(2); |
| 435 | } |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 436 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 437 | if (sgsn->cfg.dynamic_lookup) { |
| 438 | if (sgsn_ares_init(sgsn) != 0) { |
| 439 | LOGP(DGPRS, LOGL_FATAL, |
| 440 | "Failed to initialize c-ares(%d)\n", rc); |
| 441 | exit(4); |
| 442 | } |
| 443 | } |
| 444 | |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 445 | #ifdef BUILD_IU |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 446 | sccp = osmo_sccp_simple_client(tall_bsc_ctx, "OsmoSGSN", |
| 447 | 2 /* FIXME: configurable */, |
| 448 | OSMO_SS7_ASP_PROT_M3UA, 0, |
| 449 | "127.0.0.4" /* FIXME: configurable */, |
| 450 | M3UA_PORT, |
| 451 | "127.0.0.1" /* FIXME: configurable */); |
| 452 | if (!sccp) { |
| 453 | printf("Setting up SCCP client failed.\n"); |
| 454 | return 8; |
| 455 | } |
| 456 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 457 | ranap_iu_init(tall_bsc_ctx, DRANAP, "OsmoSGSN-IuPS", sccp, gsm0408_gprs_rcvmsg_iu, sgsn_ranap_iu_event); |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 458 | #endif |
| 459 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 460 | if (daemonize) { |
| 461 | rc = osmo_daemonize(); |
| 462 | if (rc < 0) { |
| 463 | perror("Error during daemonize"); |
| 464 | exit(1); |
| 465 | } |
| 466 | } |
| 467 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 468 | while (1) { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 469 | rc = osmo_select_main(0); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 470 | if (rc < 0) |
| 471 | exit(3); |
| 472 | } |
| 473 | |
Harald Welte | baf7700 | 2011-07-16 13:47:01 +0200 | [diff] [blame] | 474 | /* not reached */ |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 475 | exit(0); |
| 476 | } |