Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 1 | /* main application for hnb-gw part of osmo-iuh */ |
| 2 | |
| 3 | /* (C) 2015 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <getopt.h> |
| 26 | #include <errno.h> |
| 27 | #include <signal.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 28 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/socket.h> |
Harald Welte | 1d2c39d | 2015-09-11 17:49:37 +0200 | [diff] [blame] | 31 | #include <netinet/in.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 32 | #include <netinet/sctp.h> |
Harald Welte | 1d2c39d | 2015-09-11 17:49:37 +0200 | [diff] [blame] | 33 | #include <arpa/inet.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 34 | |
| 35 | #include <osmocom/core/application.h> |
| 36 | #include <osmocom/core/talloc.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 37 | #include <osmocom/core/select.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 38 | #include <osmocom/core/logging.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 39 | #include <osmocom/core/socket.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 40 | #include <osmocom/core/msgb.h> |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 41 | #include <osmocom/core/write_queue.h> |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 42 | |
| 43 | #include <osmocom/vty/telnet_interface.h> |
| 44 | #include <osmocom/vty/logging.h> |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 45 | #include <osmocom/vty/command.h> |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 46 | |
Harald Welte | ffa7c0a | 2015-12-23 00:03:41 +0100 | [diff] [blame^] | 47 | #include <osmocom/netif/stream.h> |
| 48 | |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 49 | #include <osmocom/sigtran/sua.h> |
| 50 | #include <osmocom/sigtran/protocol/sua.h> |
| 51 | #include <osmocom/sigtran/sccp_sap.h> |
| 52 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 53 | #include "hnbgw.h" |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 54 | #include "hnbgw_hnbap.h" |
Harald Welte | 318e4d5 | 2015-09-10 18:47:08 +0200 | [diff] [blame] | 55 | #include "hnbgw_rua.h" |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 56 | |
| 57 | static void *tall_hnb_ctx; |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 58 | static void *tall_ue_ctx; |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 59 | static void *tall_sua_ctx; |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 60 | void *talloc_asn1_ctx; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 61 | |
| 62 | struct hnb_gw g_hnb_gw = { |
| 63 | .config = { |
| 64 | .iuh_listen_port = IUH_DEFAULT_SCTP_PORT, |
| 65 | }, |
| 66 | }; |
| 67 | |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 68 | struct ue_context *ue_context_by_id(uint32_t id) |
| 69 | { |
| 70 | struct ue_context *ue; |
| 71 | |
| 72 | llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) { |
| 73 | if (ue->context_id == id) |
| 74 | return ue; |
| 75 | } |
| 76 | return NULL; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | struct ue_context *ue_context_by_imsi(const char *imsi) |
| 81 | { |
| 82 | struct ue_context *ue; |
| 83 | |
| 84 | llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) { |
| 85 | if (!strcmp(ue->imsi, imsi)) |
| 86 | return ue; |
| 87 | } |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | static uint32_t get_next_ue_ctx_id(void) |
| 92 | { |
| 93 | uint32_t id; |
| 94 | |
| 95 | do { |
| 96 | id = g_hnb_gw.next_ue_ctx_id++; |
| 97 | } while (ue_context_by_id(id)); |
| 98 | |
| 99 | return id; |
| 100 | } |
| 101 | |
| 102 | struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi) |
| 103 | { |
| 104 | struct ue_context *ue; |
| 105 | |
| 106 | ue = talloc_zero(tall_hnb_ctx, struct ue_context); |
| 107 | if (!ue) |
| 108 | return NULL; |
| 109 | |
| 110 | ue->hnb = hnb; |
| 111 | strncpy(ue->imsi, imsi, sizeof(ue->imsi)); |
| 112 | ue->context_id = get_next_ue_ctx_id(); |
Harald Welte | 256c67a | 2015-09-11 01:22:29 +0200 | [diff] [blame] | 113 | llist_add_tail(&ue->list, &g_hnb_gw.ue_list); |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 114 | |
| 115 | return ue; |
| 116 | } |
| 117 | |
| 118 | void ue_context_free(struct ue_context *ue) |
| 119 | { |
| 120 | llist_del(&ue->list); |
| 121 | talloc_free(ue); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 126 | static int hnb_read_cb(struct osmo_fd *fd) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 127 | { |
| 128 | struct hnb_context *hnb = fd->data; |
| 129 | struct sctp_sndrcvinfo sinfo; |
| 130 | struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 131 | int flags = 0; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 132 | int rc; |
| 133 | |
| 134 | if (!msg) |
| 135 | return -ENOMEM; |
| 136 | |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 137 | /* we store a reference to the HomeNodeB in the msg->dest for the |
| 138 | * benefit of varoius downstream processing functions */ |
| 139 | msg->dst = hnb; |
| 140 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 141 | rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg), |
| 142 | NULL, NULL, &sinfo, &flags); |
| 143 | if (rc < 0) { |
| 144 | LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); |
Harald Welte | ee77cff | 2015-08-30 16:57:53 +0200 | [diff] [blame] | 145 | /* FIXME: clean up after disappeared HNB */ |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 146 | close(fd->fd); |
| 147 | osmo_fd_unregister(fd); |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 148 | goto out; |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 149 | } else if (rc == 0) { |
| 150 | LOGP(DMAIN, LOGL_ERROR, "Connection to HNB closed\n"); |
| 151 | /* TODO: Remove all UEs from that connection */ |
| 152 | close(fd->fd); |
| 153 | osmo_fd_unregister(fd); |
| 154 | fd->fd = -1; |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 155 | rc = -1; |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 156 | |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 157 | goto out; |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 158 | } else { |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 159 | msgb_put(msg, rc); |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 160 | } |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 161 | |
Harald Welte | 17878e2 | 2015-09-08 00:09:13 +0200 | [diff] [blame] | 162 | if (flags & MSG_NOTIFICATION) { |
Daniel Willmann | 7c27f7b | 2015-12-15 17:41:41 +0100 | [diff] [blame] | 163 | LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n"); |
Harald Welte | 17878e2 | 2015-09-08 00:09:13 +0200 | [diff] [blame] | 164 | msgb_free(msg); |
| 165 | return 0; |
| 166 | } |
| 167 | |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 168 | sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid); |
| 169 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 170 | switch (sinfo.sinfo_ppid) { |
| 171 | case IUH_PPI_HNBAP: |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 172 | hnb->hnbap_stream = sinfo.sinfo_stream; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 173 | rc = hnbgw_hnbap_rx(hnb, msg); |
| 174 | break; |
| 175 | case IUH_PPI_RUA: |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 176 | hnb->rua_stream = sinfo.sinfo_stream; |
Harald Welte | 318e4d5 | 2015-09-10 18:47:08 +0200 | [diff] [blame] | 177 | rc = hnbgw_rua_rx(hnb, msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 178 | break; |
| 179 | case IUH_PPI_SABP: |
| 180 | case IUH_PPI_RNA: |
| 181 | case IUH_PPI_PUA: |
| 182 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
| 183 | sinfo.sinfo_ppid); |
| 184 | rc = 0; |
| 185 | break; |
| 186 | default: |
| 187 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
| 188 | sinfo.sinfo_ppid); |
| 189 | rc = 0; |
| 190 | break; |
| 191 | } |
| 192 | |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 193 | out: |
Harald Welte | f2f3000 | 2015-09-08 00:09:23 +0200 | [diff] [blame] | 194 | msgb_free(msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 195 | return rc; |
| 196 | } |
| 197 | |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 198 | static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) |
| 199 | { |
| 200 | struct hnb_context *ctx = fd->data; |
| 201 | struct sctp_sndrcvinfo sinfo = { |
Harald Welte | ffa7c0a | 2015-12-23 00:03:41 +0100 | [diff] [blame^] | 202 | .sinfo_ppid = htonl(msgb_sctp_ppid(msg)), |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 203 | .sinfo_stream = ctx->hnbap_stream, |
| 204 | }; |
| 205 | int rc; |
| 206 | |
| 207 | rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg), |
| 208 | &sinfo, 0); |
Harald Welte | 9e270b4 | 2015-09-07 22:41:26 +0200 | [diff] [blame] | 209 | /* we don't need to msgb_free(), write_queue does this for us */ |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 210 | return rc; |
| 211 | } |
| 212 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 213 | /*! call-back when the listen FD has something to read */ |
| 214 | static int listen_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 215 | { |
| 216 | struct hnb_gw *gw = fd->data; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 217 | struct hnb_context *ctx; |
| 218 | struct sockaddr_storage sockaddr; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 219 | socklen_t len = sizeof(sockaddr); |
| 220 | |
| 221 | int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len); |
| 222 | if (new_fd < 0) { |
| 223 | LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n"); |
| 224 | return new_fd; |
| 225 | } |
| 226 | |
| 227 | LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n"); |
| 228 | |
| 229 | ctx = talloc_zero(tall_hnb_ctx, struct hnb_context); |
| 230 | if (!ctx) |
| 231 | return -ENOMEM; |
| 232 | |
| 233 | ctx->gw = gw; |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 234 | osmo_wqueue_init(&ctx->wqueue, 16); |
| 235 | ctx->wqueue.bfd.data = ctx; |
| 236 | ctx->wqueue.bfd.fd = new_fd; |
| 237 | ctx->wqueue.bfd.when = BSC_FD_READ; |
| 238 | ctx->wqueue.read_cb = hnb_read_cb; |
| 239 | ctx->wqueue.write_cb = hnb_write_cb; |
| 240 | osmo_fd_register(&ctx->wqueue.bfd); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 241 | |
| 242 | llist_add_tail(&ctx->list, &gw->hnb_list); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 247 | /* Entry point for primitives coming up from SCCP User SAP */ |
| 248 | static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link) |
| 249 | { |
| 250 | struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph; |
| 251 | |
| 252 | LOGP(DMAIN, LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph)); |
| 253 | |
| 254 | /* FIXME: Do something */ |
| 255 | |
| 256 | msgb_free(oph->msg); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 261 | static const struct log_info_cat log_cat[] = { |
| 262 | [DMAIN] = { |
Daniel Willmann | 7c27f7b | 2015-12-15 17:41:41 +0100 | [diff] [blame] | 263 | .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1, |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 264 | .color = "", |
| 265 | .description = "Main program", |
| 266 | }, |
Daniel Willmann | bded984 | 2015-12-17 11:51:17 +0100 | [diff] [blame] | 267 | [DHNBAP] = { |
| 268 | .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 269 | .color = "", |
| 270 | .description = "Home Node B Application Part", |
| 271 | }, |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 272 | [DSUA] = { |
| 273 | .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 274 | .color = "", |
| 275 | .description = "SCCP User Adaptation", |
| 276 | }, |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 277 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 278 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 279 | static const struct log_info hnbgw_log_info = { |
| 280 | .cat = log_cat, |
| 281 | .num_cat = ARRAY_SIZE(log_cat), |
| 282 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 283 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 284 | static struct vty_app_info vty_info = { |
| 285 | .name = "OsmoHNBGW", |
| 286 | .version = "0", |
| 287 | }; |
| 288 | |
| 289 | static int daemonize = 0; |
| 290 | |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 291 | static int sctp_sock_init(int fd) |
| 292 | { |
| 293 | struct sctp_event_subscribe event; |
| 294 | int rc; |
| 295 | |
| 296 | /* subscribe for all events */ |
| 297 | memset((uint8_t *)&event, 1, sizeof(event)); |
| 298 | rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, |
| 299 | &event, sizeof(event)); |
| 300 | |
| 301 | return rc; |
| 302 | } |
| 303 | |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 304 | static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb) |
| 305 | { |
| 306 | vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info, |
| 307 | hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid, |
| 308 | VTY_NEWLINE); |
| 309 | vty_out(vty, " HNBAP ID %u RUA ID %u%s", hnb->hnbap_stream, hnb->rua_stream, VTY_NEWLINE); |
| 310 | } |
| 311 | |
| 312 | static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue) |
| 313 | { |
| 314 | vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE); |
| 315 | } |
| 316 | |
| 317 | DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB") |
| 318 | { |
| 319 | struct hnb_context *hnb; |
| 320 | |
| 321 | llist_for_each_entry(hnb, &g_hnb_gw.hnb_list, list) { |
| 322 | vty_dump_hnb_info(vty, hnb); |
| 323 | } |
| 324 | |
| 325 | return CMD_SUCCESS; |
| 326 | } |
| 327 | |
| 328 | DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE") |
| 329 | { |
| 330 | struct ue_context *ue; |
| 331 | |
| 332 | llist_for_each_entry(ue, &g_hnb_gw.ue_list, list) { |
| 333 | vty_dump_ue_info(vty, ue); |
| 334 | } |
| 335 | |
| 336 | return CMD_SUCCESS; |
| 337 | } |
| 338 | |
Daniel Willmann | 28b9ec1 | 2015-12-17 18:02:37 +0100 | [diff] [blame] | 339 | DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info") |
| 340 | { |
| 341 | talloc_report_full(tall_hnb_ctx, stderr); |
| 342 | talloc_report_full(tall_ue_ctx, stderr); |
| 343 | talloc_report_full(talloc_asn1_ctx, stderr); |
| 344 | |
| 345 | return CMD_SUCCESS; |
| 346 | } |
| 347 | |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 348 | static void hnbgw_vty_init(void) |
| 349 | { |
| 350 | install_element_ve(&show_hnb_cmd); |
| 351 | install_element_ve(&show_ue_cmd); |
Daniel Willmann | 28b9ec1 | 2015-12-17 18:02:37 +0100 | [diff] [blame] | 352 | install_element_ve(&show_talloc_cmd); |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 355 | int main(int argc, char **argv) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 356 | { |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 357 | struct osmo_sua_user *sua_user; |
| 358 | struct osmo_sua_link *sua_link; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 359 | int rc; |
| 360 | |
| 361 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 362 | tall_ue_ctx = talloc_named_const(NULL, 0, "ue_context"); |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 363 | tall_sua_ctx = talloc_named_const(NULL, 0, "sua"); |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 364 | talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 365 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 366 | g_hnb_gw.listen_fd.cb = listen_fd_cb; |
| 367 | g_hnb_gw.listen_fd.when = BSC_FD_READ; |
| 368 | g_hnb_gw.listen_fd.data = &g_hnb_gw; |
Harald Welte | 10dfc5a | 2015-09-11 01:34:45 +0200 | [diff] [blame] | 369 | g_hnb_gw.next_ue_ctx_id = 23; |
Harald Welte | 08a793b | 2015-09-07 19:53:46 +0200 | [diff] [blame] | 370 | INIT_LLIST_HEAD(&g_hnb_gw.hnb_list); |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 371 | INIT_LLIST_HEAD(&g_hnb_gw.ue_list); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 372 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 373 | rc = osmo_init_logging(&hnbgw_log_info); |
| 374 | if (rc < 0) |
| 375 | exit(1); |
| 376 | |
| 377 | vty_init(&vty_info); |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 378 | hnbgw_vty_init(); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 379 | |
| 380 | rc = telnet_init(NULL, &g_hnb_gw, 2323); |
| 381 | if (rc < 0) { |
| 382 | perror("Error binding VTY port"); |
| 383 | exit(1); |
| 384 | } |
| 385 | |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 386 | osmo_sua_set_log_area(DSUA); |
| 387 | sua_user = osmo_sua_user_create(tall_sua_ctx, sccp_sap_up); |
| 388 | if (!sua_user) { |
| 389 | perror("Failed to init SUA"); |
| 390 | exit(1); |
| 391 | } |
| 392 | rc = osmo_sua_client_connect(sua_user, "127.0.0.1", SUA_PORT); |
| 393 | if (rc < 0) { |
| 394 | perror("Failed to connect SUA"); |
| 395 | exit(1); |
| 396 | } |
| 397 | sua_link = osmo_sua_client_get_link(sua_user); |
| 398 | if (!sua_link) { |
| 399 | perror("Failed to get SUA link"); |
| 400 | exit(1); |
| 401 | } |
| 402 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 403 | rc = osmo_sock_init_ofd(&g_hnb_gw.listen_fd, AF_INET, SOCK_STREAM, |
Harald Welte | 1c0f538 | 2015-09-07 18:46:58 +0200 | [diff] [blame] | 404 | IPPROTO_SCTP, NULL, |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 405 | g_hnb_gw.config.iuh_listen_port, OSMO_SOCK_F_BIND); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 406 | if (rc < 0) { |
| 407 | perror("Error binding Iuh port"); |
| 408 | exit(1); |
| 409 | } |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 410 | sctp_sock_init(g_hnb_gw.listen_fd.fd); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 411 | |
| 412 | if (daemonize) { |
| 413 | rc = osmo_daemonize(); |
| 414 | if (rc < 0) { |
| 415 | perror("Error during daemonize"); |
| 416 | exit(1); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | while (1) { |
| 421 | rc = osmo_select_main(0); |
| 422 | if (rc < 0) |
| 423 | exit(3); |
| 424 | } |
| 425 | |
| 426 | /* not reached */ |
| 427 | exit(0); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 428 | } |