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