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> |
| 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 | 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> |
| 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 | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 39 | #include <osmocore/rate_ctr.h> |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 40 | |
| 41 | #include <openbsc/signal.h> |
| 42 | #include <openbsc/debug.h> |
| 43 | #include <openbsc/telnet_interface.h> |
| 44 | #include <openbsc/vty.h> |
| 45 | #include <openbsc/sgsn.h> |
| 46 | #include <openbsc/gprs_ns.h> |
| 47 | #include <openbsc/gprs_bssgp.h> |
| 48 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 49 | #include <gtp.h> |
| 50 | |
Harald Welte | e236596 | 2010-05-04 07:41:59 +0200 | [diff] [blame] | 51 | #include "../../bscconfig.h" |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 52 | |
| 53 | /* this is here for the vty... it will never be called */ |
| 54 | void subscr_put() { abort(); } |
| 55 | |
| 56 | #define _GNU_SOURCE |
| 57 | #include <getopt.h> |
| 58 | |
| 59 | void *tall_bsc_ctx; |
| 60 | |
| 61 | struct gprs_ns_inst *sgsn_nsi; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 62 | const char *openbsc_copyright = |
| 63 | "Copyright (C) 2010 Harald Welte and On-Waves\n" |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 64 | "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n" |
| 65 | "This is free software: you are free to change and redistribute it.\n" |
| 66 | "There is NO WARRANTY, to the extent permitted by law.\n"; |
| 67 | |
Harald Welte | 8fc1a46 | 2010-05-17 00:53:10 +0200 | [diff] [blame] | 68 | static struct sgsn_instance sgsn_inst = { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 69 | .config_file = "osmo_sgsn.cfg", |
| 70 | .cfg = { |
| 71 | .gtp_statedir = "./", |
| 72 | }, |
| 73 | }; |
Harald Welte | 8fc1a46 | 2010-05-17 00:53:10 +0200 | [diff] [blame] | 74 | struct sgsn_instance *sgsn = &sgsn_inst; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 75 | |
| 76 | /* call-back function for the NS protocol */ |
| 77 | static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, |
| 78 | struct msgb *msg, u_int16_t bvci) |
| 79 | { |
| 80 | int rc = 0; |
| 81 | |
| 82 | switch (event) { |
| 83 | case GPRS_NS_EVT_UNIT_DATA: |
| 84 | /* hand the message into the BSSGP implementation */ |
Harald Welte | e6afd60 | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 85 | rc = gprs_bssgp_rcvmsg(msg); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 86 | break; |
| 87 | default: |
| 88 | LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event); |
| 89 | if (msg) |
| 90 | talloc_free(msg); |
| 91 | rc = -EIO; |
| 92 | break; |
| 93 | } |
| 94 | return rc; |
| 95 | } |
| 96 | |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 97 | static void signal_handler(int signal) |
| 98 | { |
| 99 | fprintf(stdout, "signal %u received\n", signal); |
| 100 | |
| 101 | switch (signal) { |
| 102 | case SIGINT: |
| 103 | dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL); |
| 104 | sleep(1); |
| 105 | exit(0); |
| 106 | break; |
| 107 | case SIGABRT: |
| 108 | /* in case of abort, we want to obtain a talloc report |
| 109 | * and then return to the caller, who will abort the process */ |
| 110 | case SIGUSR1: |
| 111 | talloc_report(tall_vty_ctx, stderr); |
| 112 | talloc_report_full(tall_bsc_ctx, stderr); |
| 113 | break; |
| 114 | case SIGUSR2: |
| 115 | talloc_report_full(tall_vty_ctx, stderr); |
| 116 | break; |
| 117 | default: |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
Harald Welte | d6c7416 | 2010-05-02 21:29:17 +0200 | [diff] [blame] | 122 | /* NSI that BSSGP uses when transmitting on NS */ |
| 123 | extern struct gprs_ns_inst *bssgp_nsi; |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 124 | extern void *tall_msgb_ctx; |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 125 | |
| 126 | int main(int argc, char **argv) |
| 127 | { |
| 128 | struct gsm_network dummy_network; |
| 129 | struct log_target *stderr_target; |
| 130 | struct sockaddr_in sin; |
| 131 | int rc; |
| 132 | |
| 133 | tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn"); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 134 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 135 | |
| 136 | signal(SIGINT, &signal_handler); |
| 137 | signal(SIGABRT, &signal_handler); |
| 138 | signal(SIGUSR1, &signal_handler); |
| 139 | signal(SIGUSR2, &signal_handler); |
| 140 | signal(SIGPIPE, SIG_IGN); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 141 | |
| 142 | log_init(&log_info); |
| 143 | stderr_target = log_target_create_stderr(); |
| 144 | log_add_target(stderr_target); |
| 145 | log_set_all_filter(stderr_target, 1); |
| 146 | |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 147 | vty_init("Osmocom SGSN", PACKAGE_VERSION, openbsc_copyright); |
Harald Welte | 5bc61dc | 2010-05-16 22:02:16 +0200 | [diff] [blame] | 148 | logging_vty_add_cmds(); |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 149 | sgsn_vty_init(); |
| 150 | |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 151 | rate_ctr_init(tall_bsc_ctx); |
Harald Welte | dcccb18 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 152 | rc = telnet_init(tall_bsc_ctx, &dummy_network, 4245); |
| 153 | if (rc < 0) |
| 154 | exit(1); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 155 | |
| 156 | sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb); |
| 157 | if (!sgsn_nsi) { |
| 158 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 159 | exit(1); |
| 160 | } |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 161 | bssgp_nsi = sgsn_inst.cfg.nsi = sgsn_nsi; |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 162 | gprs_ns_vty_init(bssgp_nsi); |
| 163 | /* FIXME: register signal handler for SS_NS */ |
| 164 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 165 | rc = sgsn_parse_config(sgsn_inst.config_file, &sgsn_inst.cfg); |
Harald Welte | 66f793a | 2010-05-13 11:50:04 +0200 | [diff] [blame] | 166 | if (rc < 0) { |
| 167 | LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n"); |
| 168 | exit(2); |
| 169 | } |
| 170 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 171 | rc = sgsn_gtp_init(&sgsn_inst); |
| 172 | nsip_listen(sgsn_nsi, sgsn_inst.cfg.nsip_listen_port); |
Harald Welte | 288be16 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 173 | |
| 174 | while (1) { |
| 175 | rc = bsc_select_main(0); |
| 176 | if (rc < 0) |
| 177 | exit(3); |
| 178 | } |
| 179 | |
| 180 | exit(0); |
| 181 | } |