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