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