Harald Welte | 55fe055 | 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 | 5180844 | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Harald Welte | 55fe055 | 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 | 0e3e88e | 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 | 55fe055 | 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 | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 16 | * |
Harald Welte | 0e3e88e | 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 | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <unistd.h> |
| 23 | #include <stdio.h> |
Max | 9943d7a | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 24 | #include <time.h> |
Harald Welte | 55fe055 | 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 | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 29 | #include <signal.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 30 | #include <sys/fcntl.h> |
| 31 | #include <sys/stat.h> |
Harald Welte | 55fe055 | 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 | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 36 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | dd5fff4 | 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 | 63fac68 | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 41 | #include <osmocom/core/stats.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 42 | |
Harald Welte | cfb6b28 | 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 | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 46 | #include <osmocom/vty/telnet_interface.h> |
Pablo Neira Ayuso | f304678 | 2011-03-23 08:49:00 +0100 | [diff] [blame] | 47 | #include <osmocom/vty/logging.h> |
Jacob Erlbeck | 63fac68 | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 48 | #include <osmocom/vty/stats.h> |
Harald Welte | 6bcd303 | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 49 | #include <osmocom/vty/ports.h> |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 50 | |
Neels Hofmeyr | 4d92c43 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 51 | #include <osmocom/ctrl/control_vty.h> |
| 52 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 53 | #include <openbsc/signal.h> |
| 54 | #include <openbsc/debug.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 55 | #include <openbsc/vty.h> |
| 56 | #include <openbsc/sgsn.h> |
Harald Welte | 0c938b7 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 57 | #include <openbsc/gprs_llc.h> |
Harald Welte | a62e9fe | 2012-06-17 12:16:31 +0800 | [diff] [blame] | 58 | #include <openbsc/gprs_gmm.h> |
Daniel Willmann | 945f608 | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 59 | #include <openbsc/iu.h> |
| 60 | |
Harald Welte | 5919b6a | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 61 | #include <osmocom/ctrl/control_if.h> |
Harald Welte | 6bcd303 | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 62 | #include <osmocom/ctrl/ports.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 63 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 64 | #include <gtp.h> |
| 65 | |
Harald Welte | 48030cf | 2010-05-04 07:41:59 +0200 | [diff] [blame] | 66 | #include "../../bscconfig.h" |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 67 | |
Harald Welte | 55fe055 | 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 | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 74 | static int daemonize = 0; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 75 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 76 | "Copyright (C) 2010 Harald Welte and On-Waves\r\n" |
Harald Welte | adf4d79 | 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 | 260646d | 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 | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 80 | |
Harald Welte | 17072d1 | 2010-05-17 00:53:10 +0200 | [diff] [blame] | 81 | static struct sgsn_instance sgsn_inst = { |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 82 | .config_file = "osmo_sgsn.cfg", |
| 83 | .cfg = { |
| 84 | .gtp_statedir = "./", |
Jacob Erlbeck | d7b7773 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 85 | .auth_policy = SGSN_AUTH_POLICY_CLOSED, |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 86 | }, |
| 87 | }; |
Harald Welte | 17072d1 | 2010-05-17 00:53:10 +0200 | [diff] [blame] | 88 | struct sgsn_instance *sgsn = &sgsn_inst; |
Harald Welte | 55fe055 | 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 | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 92 | struct msgb *msg, uint16_t bvci) |
Harald Welte | 55fe055 | 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 | a084f12 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 99 | rc = bssgp_rcvmsg(msg); |
Harald Welte | 55fe055 | 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 | 1bcd637 | 2012-03-01 20:30:32 +0100 | [diff] [blame] | 104 | msgb_free(msg); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 105 | rc = -EIO; |
| 106 | break; |
| 107 | } |
| 108 | return rc; |
| 109 | } |
| 110 | |
Harald Welte | a62e9fe | 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 | b6f053c | 2012-08-03 10:01:57 +0200 | [diff] [blame] | 130 | bp->u.resume.suspend_ref); |
Harald Welte | a62e9fe | 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 | fae1fbc | 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 | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 145 | osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | fae1fbc | 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 | 29a4b8c | 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 | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 166 | |
Holger Hans Peter Freyther | 4ac8cd1 | 2015-09-24 15:38:38 +0200 | [diff] [blame] | 167 | extern int bsc_vty_go_parent(struct vty *vty); |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 168 | |
| 169 | static struct vty_app_info vty_info = { |
Harald Welte | 9af42c3 | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 170 | .name = "OsmoSGSN", |
Harald Welte | 778c4b9 | 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 | ba289d0 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 173 | .is_config_node = bsc_vty_is_config_node, |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 174 | }; |
| 175 | |
Harald Welte | 599a957 | 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 | 42c64ad | 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 | 599a957 | 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'}, |
| 198 | {"log-level", 1, 0, 'e'}, |
| 199 | {NULL, 0, 0, 0} |
| 200 | }; |
| 201 | |
| 202 | c = getopt_long(argc, argv, "hd:Dc:sTe:", |
| 203 | long_options, &option_index); |
| 204 | if (c == -1) |
| 205 | break; |
| 206 | |
| 207 | switch (c) { |
| 208 | case 'h': |
| 209 | //print_usage(); |
| 210 | print_help(); |
| 211 | exit(0); |
| 212 | case 's': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 213 | log_set_use_color(osmo_stderr_target, 0); |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 214 | break; |
| 215 | case 'd': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 216 | log_parse_category_mask(osmo_stderr_target, optarg); |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 217 | break; |
| 218 | case 'D': |
| 219 | daemonize = 1; |
| 220 | break; |
| 221 | case 'c': |
| 222 | sgsn_inst.config_file = strdup(optarg); |
| 223 | break; |
| 224 | case 'T': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 225 | log_set_print_timestamp(osmo_stderr_target, 1); |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 226 | break; |
| 227 | case 'e': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 228 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 229 | break; |
| 230 | default: |
| 231 | /* ignore */ |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Harald Welte | 40b2575 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 237 | /* default categories */ |
| 238 | static struct log_info_cat gprs_categories[] = { |
| 239 | [DMM] = { |
| 240 | .name = "DMM", |
| 241 | .description = "Layer3 Mobility Management (MM)", |
| 242 | .color = "\033[1;33m", |
| 243 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 244 | }, |
Harald Welte | 40b2575 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 245 | [DPAG] = { |
| 246 | .name = "DPAG", |
| 247 | .description = "Paging Subsystem", |
| 248 | .color = "\033[1;38m", |
| 249 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 250 | }, |
| 251 | [DMEAS] = { |
| 252 | .name = "DMEAS", |
| 253 | .description = "Radio Measurement Processing", |
| 254 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 255 | }, |
| 256 | [DREF] = { |
| 257 | .name = "DREF", |
| 258 | .description = "Reference Counting", |
| 259 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 260 | }, |
| 261 | [DGPRS] = { |
| 262 | .name = "DGPRS", |
| 263 | .description = "GPRS Packet Service", |
| 264 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 265 | }, |
| 266 | [DNS] = { |
| 267 | .name = "DNS", |
| 268 | .description = "GPRS Network Service (NS)", |
| 269 | .enabled = 1, .loglevel = LOGL_INFO, |
| 270 | }, |
| 271 | [DBSSGP] = { |
| 272 | .name = "DBSSGP", |
| 273 | .description = "GPRS BSS Gateway Protocol (BSSGP)", |
| 274 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 275 | }, |
| 276 | [DLLC] = { |
| 277 | .name = "DLLC", |
| 278 | .description = "GPRS Logical Link Control Protocol (LLC)", |
| 279 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 280 | }, |
| 281 | [DSNDCP] = { |
| 282 | .name = "DSNDCP", |
| 283 | .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)", |
| 284 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 285 | }, |
Daniel Willmann | 59525c3 | 2016-05-20 21:59:45 +0200 | [diff] [blame] | 286 | [DRANAP] = { |
| 287 | .name = "DRANAP", |
| 288 | .description = "RAN Application Part (RANAP)", |
| 289 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 290 | }, |
Daniel Willmann | 67286df | 2016-05-20 21:59:35 +0200 | [diff] [blame] | 291 | [DSUA] = { |
| 292 | .name = "DSUA", |
| 293 | .description = "SCCP User Adaptation (SUA)", |
| 294 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 295 | }, |
Philipp | 4948209 | 2016-08-26 16:58:41 +0200 | [diff] [blame] | 296 | [DSLHC] = { |
| 297 | .name = "DSLHC", |
| 298 | .description = "RFC1144 TCP/IP Header compression (SLHC)", |
| 299 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 300 | }, |
Philipp | 2c6dd81 | 2016-09-02 13:38:01 +0200 | [diff] [blame] | 301 | [DV42BIS] = { |
| 302 | .name = "DV42BIS", |
| 303 | .description = "V.42bis data compression (SNDCP)", |
| 304 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 305 | } |
Harald Welte | 40b2575 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | static const struct log_info gprs_log_info = { |
| 309 | .filter_fn = gprs_log_filter_fn, |
| 310 | .cat = gprs_categories, |
| 311 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 312 | }; |
| 313 | |
Daniel Willmann | 945f608 | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 314 | /* Implement the extern asn_debug from libasn1c to indicate whether the ASN.1 |
| 315 | * binary code decoded and encoded during Iu communication should be logged to |
| 316 | * stderr. See osmocom's libasn1c, asn_internal.h, at "if (asn_debug)": |
| 317 | * http://git.osmocom.org/libasn1c/tree/include/asn1c/asn_internal.h */ |
| 318 | int asn_debug = 0; |
| 319 | |
| 320 | int sgsn_ranap_iu_event(struct ue_conn_ctx *ctx, enum iu_event_type type, void *data); |
Harald Welte | 40b2575 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 321 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 322 | int main(int argc, char **argv) |
| 323 | { |
Holger Hans Peter Freyther | e8e5ef2 | 2014-03-23 18:08:26 +0100 | [diff] [blame] | 324 | struct ctrl_handle *ctrl; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 325 | struct gsm_network dummy_network; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 326 | int rc; |
| 327 | |
Max | 9943d7a | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 328 | srand(time(NULL)); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 329 | tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn"); |
Neels Hofmeyr | a73e5f1 | 2016-09-16 01:49:08 +0200 | [diff] [blame^] | 330 | msgb_talloc_ctx_init(tall_bsc_ctx, 0); |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 331 | |
| 332 | signal(SIGINT, &signal_handler); |
| 333 | signal(SIGABRT, &signal_handler); |
| 334 | signal(SIGUSR1, &signal_handler); |
| 335 | signal(SIGUSR2, &signal_handler); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 336 | |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 337 | osmo_init_ignore_signals(); |
Harald Welte | 40b2575 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 338 | osmo_init_logging(&gprs_log_info); |
Jacob Erlbeck | 63fac68 | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 339 | osmo_stats_init(tall_bsc_ctx); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 340 | |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 341 | vty_info.copyright = openbsc_copyright; |
| 342 | vty_init(&vty_info); |
Harald Welte | 40b2575 | 2012-06-17 12:58:46 +0800 | [diff] [blame] | 343 | logging_vty_add_cmds(&gprs_log_info); |
Jacob Erlbeck | 63fac68 | 2015-10-26 16:25:37 +0100 | [diff] [blame] | 344 | osmo_stats_vty_add_cmds(&gprs_log_info); |
| 345 | sgsn_vty_init(); |
Neels Hofmeyr | 4d92c43 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 346 | ctrl_vty_init(tall_bsc_ctx); |
Daniel Willmann | 945f608 | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 347 | #ifdef BUILD_IU |
| 348 | iu_vty_init(&asn_debug); |
| 349 | #endif |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 350 | |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 351 | handle_options(argc, argv); |
| 352 | |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 353 | rate_ctr_init(tall_bsc_ctx); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 354 | |
Harald Welte | deb9017 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 355 | gprs_ns_set_log_ss(DNS); |
Harald Welte | a084f12 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 356 | bssgp_set_log_ss(DBSSGP); |
Harald Welte | deb9017 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 357 | |
Harald Welte | 1d12338 | 2012-06-16 16:54:06 +0800 | [diff] [blame] | 358 | sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_bsc_ctx); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 359 | if (!sgsn_nsi) { |
| 360 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 361 | exit(1); |
| 362 | } |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 363 | bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi; |
Harald Welte | 81fb358 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 364 | |
| 365 | gprs_llc_init("/usr/local/lib/osmocom/crypt/"); |
Alexander Couzens | 32fdba0 | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 366 | sgsn_rate_ctr_init(); |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 367 | sgsn_inst_init(); |
Harald Welte | 81fb358 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 368 | |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 369 | gprs_ns_vty_init(bssgp_nsi); |
Harald Welte | a084f12 | 2012-06-17 13:04:02 +0800 | [diff] [blame] | 370 | bssgp_vty_init(); |
Harald Welte | 0c938b7 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 371 | gprs_llc_vty_init(); |
Harald Welte | 9413503 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 372 | gprs_sndcp_vty_init(); |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 373 | sgsn_auth_init(); |
Holger Hans Peter Freyther | 33d469b | 2015-05-05 22:25:48 +0200 | [diff] [blame] | 374 | sgsn_cdr_init(&sgsn_inst); |
Harald Welte | 7ed98e3 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 375 | /* FIXME: register signal handler for SS_L_NS */ |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 376 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 377 | rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg); |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 378 | if (rc < 0) { |
| 379 | LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n"); |
| 380 | exit(2); |
| 381 | } |
| 382 | |
Neels Hofmeyr | 737252c | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 383 | /* start telnet after reading config for vty_get_bind_addr() */ |
Neels Hofmeyr | 737252c | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 384 | rc = telnet_init_dynif(tall_bsc_ctx, &dummy_network, |
| 385 | vty_get_bind_addr(), OSMO_VTY_PORT_SGSN); |
| 386 | if (rc < 0) |
| 387 | exit(1); |
| 388 | |
Neels Hofmeyr | 4d92c43 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 389 | /* start control interface after reading config for |
| 390 | * ctrl_vty_get_bind_addr() */ |
Neels Hofmeyr | 4d92c43 | 2016-02-23 15:10:33 +0100 | [diff] [blame] | 391 | ctrl = sgsn_controlif_setup(NULL, ctrl_vty_get_bind_addr(), |
| 392 | OSMO_CTRL_PORT_SGSN); |
| 393 | if (!ctrl) { |
| 394 | LOGP(DGPRS, LOGL_ERROR, "Failed to create CTRL interface.\n"); |
| 395 | exit(1); |
| 396 | } |
| 397 | |
| 398 | if (sgsn_ctrl_cmds_install() != 0) { |
| 399 | LOGP(DGPRS, LOGL_ERROR, "Failed to install CTRL commands.\n"); |
| 400 | exit(1); |
| 401 | } |
| 402 | |
| 403 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 404 | rc = sgsn_gtp_init(&sgsn_inst); |
Harald Welte | 047a688 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 405 | if (rc) { |
| 406 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n"); |
Harald Welte | e59c56e | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 407 | exit(2); |
Harald Welte | 047a688 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 408 | } |
Harald Welte | e59c56e | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 409 | |
Jacob Erlbeck | 233715c | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 410 | rc = gprs_subscr_init(&sgsn_inst); |
| 411 | if (rc < 0) { |
| 412 | LOGP(DGPRS, LOGL_FATAL, "Cannot set up subscriber management\n"); |
| 413 | exit(2); |
| 414 | } |
| 415 | |
Harald Welte | 31f0a23 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 416 | rc = gprs_ns_nsip_listen(sgsn_nsi); |
Harald Welte | 047a688 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 417 | if (rc < 0) { |
| 418 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n"); |
Harald Welte | 31f0a23 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 419 | exit(2); |
Harald Welte | 047a688 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | rc = gprs_ns_frgre_listen(sgsn_nsi); |
| 423 | if (rc < 0) { |
| 424 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE " |
| 425 | "socket. Do you have CAP_NET_RAW?\n"); |
| 426 | exit(2); |
| 427 | } |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 428 | |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 429 | if (sgsn->cfg.dynamic_lookup) { |
| 430 | if (sgsn_ares_init(sgsn) != 0) { |
| 431 | LOGP(DGPRS, LOGL_FATAL, |
| 432 | "Failed to initialize c-ares(%d)\n", rc); |
| 433 | exit(4); |
| 434 | } |
| 435 | } |
| 436 | |
Daniel Willmann | 945f608 | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 437 | #ifdef BUILD_IU |
| 438 | iu_init(tall_bsc_ctx, "127.0.0.2", 14001, gsm0408_gprs_rcvmsg_iu, sgsn_ranap_iu_event); |
| 439 | #endif |
| 440 | |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 441 | if (daemonize) { |
| 442 | rc = osmo_daemonize(); |
| 443 | if (rc < 0) { |
| 444 | perror("Error during daemonize"); |
| 445 | exit(1); |
| 446 | } |
| 447 | } |
| 448 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 449 | while (1) { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 450 | rc = osmo_select_main(0); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 451 | if (rc < 0) |
| 452 | exit(3); |
| 453 | } |
| 454 | |
Harald Welte | 5c71db2 | 2011-07-16 13:47:01 +0200 | [diff] [blame] | 455 | /* not reached */ |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 456 | exit(0); |
| 457 | } |