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> |
| 4 | * (C) 2010 by On Waves |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <getopt.h> |
| 28 | #include <errno.h> |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 29 | #include <signal.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 30 | #include <sys/fcntl.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <netinet/in.h> |
| 35 | #include <arpa/inet.h> |
| 36 | |
| 37 | #include <osmocore/talloc.h> |
| 38 | #include <osmocore/select.h> |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 39 | #include <osmocore/rate_ctr.h> |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 40 | #include <osmocore/logging.h> |
| 41 | #include <osmocore/process.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 42 | |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 43 | #include <osmocom/vty/telnet_interface.h> |
| 44 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 45 | #include <openbsc/signal.h> |
| 46 | #include <openbsc/debug.h> |
Harald Welte | 55fe055 | 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 | 0c938b7 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 51 | #include <openbsc/gprs_llc.h> |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 52 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 53 | #include <gtp.h> |
| 54 | |
Harald Welte | 48030cf | 2010-05-04 07:41:59 +0200 | [diff] [blame] | 55 | #include "../../bscconfig.h" |
Harald Welte | 55fe055 | 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 | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 66 | static struct log_target *stderr_target; |
| 67 | static int daemonize = 0; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 68 | const char *openbsc_copyright = |
| 69 | "Copyright (C) 2010 Harald Welte and On-Waves\n" |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 70 | "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n" |
| 71 | "This is free software: you are free to change and redistribute it.\n" |
| 72 | "There is NO WARRANTY, to the extent permitted by law.\n"; |
| 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 = "./", |
| 78 | }, |
| 79 | }; |
Harald Welte | 17072d1 | 2010-05-17 00:53:10 +0200 | [diff] [blame] | 80 | struct sgsn_instance *sgsn = &sgsn_inst; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 81 | |
| 82 | /* call-back function for the NS protocol */ |
| 83 | static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, |
| 84 | struct msgb *msg, u_int16_t bvci) |
| 85 | { |
| 86 | int rc = 0; |
| 87 | |
| 88 | switch (event) { |
| 89 | case GPRS_NS_EVT_UNIT_DATA: |
| 90 | /* hand the message into the BSSGP implementation */ |
Harald Welte | a6e3270 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 91 | rc = gprs_bssgp_rcvmsg(msg); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 92 | break; |
| 93 | default: |
| 94 | LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event); |
| 95 | if (msg) |
| 96 | talloc_free(msg); |
| 97 | rc = -EIO; |
| 98 | break; |
| 99 | } |
| 100 | return rc; |
| 101 | } |
| 102 | |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 103 | static void signal_handler(int signal) |
| 104 | { |
| 105 | fprintf(stdout, "signal %u received\n", signal); |
| 106 | |
| 107 | switch (signal) { |
| 108 | case SIGINT: |
| 109 | dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL); |
| 110 | sleep(1); |
| 111 | exit(0); |
| 112 | break; |
| 113 | case SIGABRT: |
| 114 | /* in case of abort, we want to obtain a talloc report |
| 115 | * and then return to the caller, who will abort the process */ |
| 116 | case SIGUSR1: |
| 117 | talloc_report(tall_vty_ctx, stderr); |
| 118 | talloc_report_full(tall_bsc_ctx, stderr); |
| 119 | break; |
| 120 | case SIGUSR2: |
| 121 | talloc_report_full(tall_vty_ctx, stderr); |
| 122 | break; |
| 123 | default: |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
Harald Welte | 29a4b8c | 2010-05-02 21:29:17 +0200 | [diff] [blame] | 128 | /* NSI that BSSGP uses when transmitting on NS */ |
| 129 | extern struct gprs_ns_inst *bssgp_nsi; |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 130 | extern void *tall_msgb_ctx; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 131 | |
Holger Hans Peter Freyther | 3538c77 | 2010-06-08 16:11:06 +0800 | [diff] [blame] | 132 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 133 | |
| 134 | static struct vty_app_info vty_info = { |
| 135 | .name = "Osmocom SGSN", |
| 136 | .version = PACKAGE_VERSION, |
| 137 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | ba289d0 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 138 | .is_config_node = bsc_vty_is_config_node, |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 139 | }; |
| 140 | |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 141 | static void print_help(void) |
| 142 | { |
| 143 | printf("Some useful help...\n"); |
| 144 | printf(" -h --help\tthis text\n"); |
| 145 | printf(" -D --daemonize\tFork the process into a background daemon\n"); |
| 146 | printf(" -d option --debug\tenable Debugging\n"); |
| 147 | printf(" -s --disable-color\n"); |
| 148 | printf(" -c --config-file\tThe config file to use\n"); |
| 149 | printf(" -e --log-level number\tSet a global log level\n"); |
| 150 | } |
| 151 | |
| 152 | static void handle_options(int argc, char **argv) |
| 153 | { |
| 154 | while (1) { |
| 155 | int option_index = 0, c; |
| 156 | static struct option long_options[] = { |
| 157 | {"help", 0, 0, 'h'}, |
| 158 | {"debug", 1, 0, 'd'}, |
| 159 | {"daemonize", 0, 0, 'D'}, |
| 160 | {"config-file", 1, 0, 'c'}, |
| 161 | {"disable-color", 0, 0, 's'}, |
| 162 | {"timestamp", 0, 0, 'T'}, |
| 163 | {"log-level", 1, 0, 'e'}, |
| 164 | {NULL, 0, 0, 0} |
| 165 | }; |
| 166 | |
| 167 | c = getopt_long(argc, argv, "hd:Dc:sTe:", |
| 168 | long_options, &option_index); |
| 169 | if (c == -1) |
| 170 | break; |
| 171 | |
| 172 | switch (c) { |
| 173 | case 'h': |
| 174 | //print_usage(); |
| 175 | print_help(); |
| 176 | exit(0); |
| 177 | case 's': |
| 178 | log_set_use_color(stderr_target, 0); |
| 179 | break; |
| 180 | case 'd': |
| 181 | log_parse_category_mask(stderr_target, optarg); |
| 182 | break; |
| 183 | case 'D': |
| 184 | daemonize = 1; |
| 185 | break; |
| 186 | case 'c': |
| 187 | sgsn_inst.config_file = strdup(optarg); |
| 188 | break; |
| 189 | case 'T': |
| 190 | log_set_print_timestamp(stderr_target, 1); |
| 191 | break; |
| 192 | case 'e': |
| 193 | log_set_log_level(stderr_target, atoi(optarg)); |
| 194 | break; |
| 195 | default: |
| 196 | /* ignore */ |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 202 | int main(int argc, char **argv) |
| 203 | { |
| 204 | struct gsm_network dummy_network; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 205 | struct sockaddr_in sin; |
| 206 | int rc; |
| 207 | |
| 208 | tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn"); |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 209 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 210 | |
| 211 | signal(SIGINT, &signal_handler); |
| 212 | signal(SIGABRT, &signal_handler); |
| 213 | signal(SIGUSR1, &signal_handler); |
| 214 | signal(SIGUSR2, &signal_handler); |
| 215 | signal(SIGPIPE, SIG_IGN); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 216 | |
| 217 | log_init(&log_info); |
| 218 | stderr_target = log_target_create_stderr(); |
| 219 | log_add_target(stderr_target); |
| 220 | log_set_all_filter(stderr_target, 1); |
| 221 | |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 222 | vty_info.copyright = openbsc_copyright; |
| 223 | vty_init(&vty_info); |
Harald Welte | 682ee5f | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 224 | logging_vty_add_cmds(); |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 225 | sgsn_vty_init(); |
| 226 | |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 227 | handle_options(argc, argv); |
| 228 | |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 229 | rate_ctr_init(tall_bsc_ctx); |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 230 | rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245); |
| 231 | if (rc < 0) |
| 232 | exit(1); |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 233 | |
| 234 | sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb); |
| 235 | if (!sgsn_nsi) { |
| 236 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 237 | exit(1); |
| 238 | } |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 239 | bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi; |
Harald Welte | 81fb358 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 240 | |
| 241 | gprs_llc_init("/usr/local/lib/osmocom/crypt/"); |
| 242 | |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 243 | gprs_ns_vty_init(bssgp_nsi); |
Harald Welte | e9a6df6 | 2010-05-18 08:02:36 +0200 | [diff] [blame] | 244 | gprs_bssgp_vty_init(); |
Harald Welte | 0c938b7 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 245 | gprs_llc_vty_init(); |
Harald Welte | 9413503 | 2010-06-30 17:21:19 +0200 | [diff] [blame] | 246 | gprs_sndcp_vty_init(); |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 247 | /* FIXME: register signal handler for SS_NS */ |
| 248 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 249 | rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg); |
Harald Welte | fae1fbc | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 250 | if (rc < 0) { |
| 251 | LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n"); |
| 252 | exit(2); |
| 253 | } |
| 254 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 255 | rc = sgsn_gtp_init(&sgsn_inst); |
Harald Welte | 047a688 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 256 | if (rc) { |
| 257 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on GTP socket\n"); |
Harald Welte | e59c56e | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 258 | exit(2); |
Harald Welte | 047a688 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 259 | } |
Harald Welte | e59c56e | 2010-05-18 14:44:31 +0200 | [diff] [blame] | 260 | |
Harald Welte | 31f0a23 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 261 | rc = gprs_ns_nsip_listen(sgsn_nsi); |
Harald Welte | 047a688 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 262 | if (rc < 0) { |
| 263 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen on NSIP socket\n"); |
Harald Welte | 31f0a23 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 264 | exit(2); |
Harald Welte | 047a688 | 2010-05-19 15:46:31 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | rc = gprs_ns_frgre_listen(sgsn_nsi); |
| 268 | if (rc < 0) { |
| 269 | LOGP(DGPRS, LOGL_FATAL, "Cannot bind/listen GRE " |
| 270 | "socket. Do you have CAP_NET_RAW?\n"); |
| 271 | exit(2); |
| 272 | } |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 273 | |
Harald Welte | 599a957 | 2010-08-27 09:26:44 +0200 | [diff] [blame] | 274 | if (daemonize) { |
| 275 | rc = osmo_daemonize(); |
| 276 | if (rc < 0) { |
| 277 | perror("Error during daemonize"); |
| 278 | exit(1); |
| 279 | } |
| 280 | } |
| 281 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 282 | while (1) { |
| 283 | rc = bsc_select_main(0); |
| 284 | if (rc < 0) |
| 285 | exit(3); |
| 286 | } |
| 287 | |
| 288 | exit(0); |
| 289 | } |