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> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <getopt.h> |
| 27 | #include <errno.h> |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 28 | #include <signal.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 29 | #include <sys/fcntl.h> |
| 30 | #include <sys/stat.h> |
Harald Welte | 288be16 | 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 | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 35 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | 136f453 | 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> |
| 40 | #include <osmocom/core/process.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 41 | |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 42 | #include <osmocom/vty/telnet_interface.h> |
Pablo Neira Ayuso | 3848770 | 2011-03-23 08:49:00 +0100 | [diff] [blame] | 43 | #include <osmocom/vty/logging.h> |
Harald Welte | 4b037e4 | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 44 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 45 | #include <openbsc/signal.h> |
| 46 | #include <openbsc/debug.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 47 | #include <openbsc/vty.h> |
| 48 | #include <openbsc/sgsn.h> |
| 49 | #include <openbsc/gprs_ns.h> |
| 50 | #include <openbsc/gprs_bssgp.h> |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 51 | #include <openbsc/gprs_llc.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 52 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 53 | #include <gtp.h> |
| 54 | |
Harald Welte | e236596 | 2010-05-04 07:41:59 +0200 | [diff] [blame] | 55 | #include "../../bscconfig.h" |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 56 | |
| 57 | /* this is here for the vty... it will never be called */ |
| 58 | void subscr_put() { abort(); } |
| 59 | |
| 60 | #define _GNU_SOURCE |
| 61 | #include <getopt.h> |
| 62 | |
| 63 | void *tall_bsc_ctx; |
| 64 | |
| 65 | struct gprs_ns_inst *sgsn_nsi; |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 66 | static int daemonize = 0; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 67 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 5f54075 | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 68 | "Copyright (C) 2010 Harald Welte and On-Waves\r\n" |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 69 | "License AGPLv3+: GNU AGPL version 2 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] | 70 | "This is free software: you are free to change and redistribute it.\r\n" |
| 71 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 72 | |
Harald Welte | 8fc1a46 | 2010-05-17 00:53:10 +0200 | [diff] [blame] | 73 | static struct sgsn_instance sgsn_inst = { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 74 | .config_file = "osmo_sgsn.cfg", |
| 75 | .cfg = { |
| 76 | .gtp_statedir = "./", |
| 77 | }, |
| 78 | }; |
Harald Welte | 8fc1a46 | 2010-05-17 00:53:10 +0200 | [diff] [blame] | 79 | struct sgsn_instance *sgsn = &sgsn_inst; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 80 | |
| 81 | /* call-back function for the NS protocol */ |
| 82 | 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] | 83 | struct msgb *msg, uint16_t bvci) |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 84 | { |
| 85 | int rc = 0; |
| 86 | |
| 87 | switch (event) { |
| 88 | case GPRS_NS_EVT_UNIT_DATA: |
| 89 | /* hand the message into the BSSGP implementation */ |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 90 | rc = gprs_bssgp_rcvmsg(msg); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 91 | break; |
| 92 | default: |
| 93 | LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event); |
| 94 | if (msg) |
| 95 | talloc_free(msg); |
| 96 | rc = -EIO; |
| 97 | break; |
| 98 | } |
| 99 | return rc; |
| 100 | } |
| 101 | |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 102 | static void signal_handler(int signal) |
| 103 | { |
| 104 | fprintf(stdout, "signal %u received\n", signal); |
| 105 | |
| 106 | switch (signal) { |
| 107 | case SIGINT: |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 108 | osmo_signal_dispatch(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 109 | sleep(1); |
| 110 | exit(0); |
| 111 | break; |
| 112 | case SIGABRT: |
| 113 | /* in case of abort, we want to obtain a talloc report |
| 114 | * and then return to the caller, who will abort the process */ |
| 115 | case SIGUSR1: |
| 116 | talloc_report(tall_vty_ctx, stderr); |
| 117 | talloc_report_full(tall_bsc_ctx, stderr); |
| 118 | break; |
| 119 | case SIGUSR2: |
| 120 | talloc_report_full(tall_vty_ctx, stderr); |
| 121 | break; |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
Harald Welte | d6c7416 | 2010-05-02 21:29:17 +0200 | [diff] [blame] | 127 | /* NSI that BSSGP uses when transmitting on NS */ |
| 128 | extern struct gprs_ns_inst *bssgp_nsi; |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 129 | extern void *tall_msgb_ctx; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 130 | |
Holger Hans Peter Freyther | 57da447 | 2010-06-08 16:11:06 +0800 | [diff] [blame] | 131 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 132 | |
| 133 | static struct vty_app_info vty_info = { |
Harald Welte | ec1921d | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 134 | .name = "OsmoSGSN", |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 135 | .version = PACKAGE_VERSION, |
| 136 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | 81506b4 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 137 | .is_config_node = bsc_vty_is_config_node, |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 140 | static void print_help(void) |
| 141 | { |
| 142 | printf("Some useful help...\n"); |
| 143 | printf(" -h --help\tthis text\n"); |
| 144 | printf(" -D --daemonize\tFork the process into a background daemon\n"); |
| 145 | printf(" -d option --debug\tenable Debugging\n"); |
| 146 | printf(" -s --disable-color\n"); |
| 147 | printf(" -c --config-file\tThe config file to use\n"); |
| 148 | printf(" -e --log-level number\tSet a global log level\n"); |
| 149 | } |
| 150 | |
| 151 | static void handle_options(int argc, char **argv) |
| 152 | { |
| 153 | while (1) { |
| 154 | int option_index = 0, c; |
| 155 | static struct option long_options[] = { |
| 156 | {"help", 0, 0, 'h'}, |
| 157 | {"debug", 1, 0, 'd'}, |
| 158 | {"daemonize", 0, 0, 'D'}, |
| 159 | {"config-file", 1, 0, 'c'}, |
| 160 | {"disable-color", 0, 0, 's'}, |
| 161 | {"timestamp", 0, 0, 'T'}, |
| 162 | {"log-level", 1, 0, 'e'}, |
| 163 | {NULL, 0, 0, 0} |
| 164 | }; |
| 165 | |
| 166 | c = getopt_long(argc, argv, "hd:Dc:sTe:", |
| 167 | long_options, &option_index); |
| 168 | if (c == -1) |
| 169 | break; |
| 170 | |
| 171 | switch (c) { |
| 172 | case 'h': |
| 173 | //print_usage(); |
| 174 | print_help(); |
| 175 | exit(0); |
| 176 | case 's': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 177 | log_set_use_color(osmo_stderr_target, 0); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 178 | break; |
| 179 | case 'd': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 180 | log_parse_category_mask(osmo_stderr_target, optarg); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 181 | break; |
| 182 | case 'D': |
| 183 | daemonize = 1; |
| 184 | break; |
| 185 | case 'c': |
| 186 | sgsn_inst.config_file = strdup(optarg); |
| 187 | break; |
| 188 | case 'T': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 189 | log_set_print_timestamp(osmo_stderr_target, 1); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 190 | break; |
| 191 | case 'e': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 192 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 193 | break; |
| 194 | default: |
| 195 | /* ignore */ |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 201 | int main(int argc, char **argv) |
| 202 | { |
| 203 | struct gsm_network dummy_network; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 204 | struct sockaddr_in sin; |
| 205 | int rc; |
| 206 | |
| 207 | tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn"); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 208 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 209 | |
| 210 | signal(SIGINT, &signal_handler); |
| 211 | signal(SIGABRT, &signal_handler); |
| 212 | signal(SIGUSR1, &signal_handler); |
| 213 | signal(SIGUSR2, &signal_handler); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 214 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 215 | osmo_init_ignore_signals(); |
| 216 | osmo_init_logging(&log_info); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 217 | |
Harald Welte | c31f480 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 218 | vty_info.copyright = openbsc_copyright; |
| 219 | vty_init(&vty_info); |
Pablo Neira Ayuso | 3848770 | 2011-03-23 08:49:00 +0100 | [diff] [blame] | 220 | logging_vty_add_cmds(&log_info); |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 221 | sgsn_vty_init(); |
| 222 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 223 | handle_options(argc, argv); |
| 224 | |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 225 | rate_ctr_init(tall_bsc_ctx); |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 226 | rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245); |
| 227 | if (rc < 0) |
| 228 | exit(1); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 229 | |
| 230 | sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb); |
| 231 | if (!sgsn_nsi) { |
| 232 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 233 | exit(1); |
| 234 | } |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 235 | bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi; |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 236 | |
| 237 | gprs_llc_init("/usr/local/lib/osmocom/crypt/"); |
| 238 | |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 239 | gprs_ns_vty_init(bssgp_nsi); |
Harald Welte | d2a9ed2 | 2010-05-18 08:02:36 +0200 | [diff] [blame] | 240 | gprs_bssgp_vty_init(); |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 241 | gprs_llc_vty_init(); |
Harald Welte | f78a3b2 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 242 | gprs_sndcp_vty_init(); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 243 | /* FIXME: register signal handler for SS_NS */ |
| 244 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 245 | rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 246 | if (rc < 0) { |
| 247 | LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n"); |
| 248 | exit(2); |
| 249 | } |
| 250 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 251 | rc = sgsn_gtp_init(&sgsn_inst); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 252 | if (rc) { |
| 253 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n"); |
Harald Welte | 269ae75 | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 254 | exit(2); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 255 | } |
Harald Welte | 269ae75 | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 256 | |
Harald Welte | ff3bde8 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 257 | rc = gprs_ns_nsip_listen(sgsn_nsi); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 258 | if (rc < 0) { |
| 259 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n"); |
Harald Welte | ff3bde8 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 260 | exit(2); |
Harald Welte | 6efc176 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | rc = gprs_ns_frgre_listen(sgsn_nsi); |
| 264 | if (rc < 0) { |
| 265 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE " |
| 266 | "socket. Do you have CAP_NET_RAW?\n"); |
| 267 | exit(2); |
| 268 | } |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 269 | |
Harald Welte | 9ae28a1 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 270 | if (daemonize) { |
| 271 | rc = osmo_daemonize(); |
| 272 | if (rc < 0) { |
| 273 | perror("Error during daemonize"); |
| 274 | exit(1); |
| 275 | } |
| 276 | } |
| 277 | |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 278 | while (1) { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 279 | rc = osmo_select_main(0); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 280 | if (rc < 0) |
| 281 | exit(3); |
| 282 | } |
| 283 | |
Harald Welte | baf7700 | 2011-07-16 13:47:01 +0200 | [diff] [blame] | 284 | /* not reached */ |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 285 | exit(0); |
| 286 | } |