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