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