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 | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 56 | #include "hnbgw_cn.h" |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 57 | #include "context_map.h" |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 58 | |
| 59 | static void *tall_hnb_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 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 62 | static struct hnb_gw *g_hnb_gw; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 63 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 64 | static int listen_fd_cb(struct osmo_fd *fd, unsigned int what); |
| 65 | |
| 66 | static struct hnb_gw *hnb_gw_create(void *ctx) |
| 67 | { |
| 68 | struct hnb_gw *gw = talloc_zero(ctx, struct hnb_gw); |
| 69 | |
| 70 | gw->config.iuh_listen_port = IUH_DEFAULT_SCTP_PORT; |
| 71 | |
| 72 | gw->listen_fd.cb = listen_fd_cb; |
| 73 | gw->listen_fd.when = BSC_FD_READ; |
| 74 | gw->listen_fd.data = gw; |
| 75 | gw->next_ue_ctx_id = 23; |
| 76 | INIT_LLIST_HEAD(&gw->hnb_list); |
| 77 | INIT_LLIST_HEAD(&gw->ue_list); |
| 78 | INIT_LLIST_HEAD(&gw->cn_list); |
| 79 | |
| 80 | context_map_init(gw); |
| 81 | |
| 82 | return gw; |
| 83 | } |
| 84 | |
| 85 | struct ue_context *ue_context_by_id(struct hnb_gw *gw, uint32_t id) |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 86 | { |
| 87 | struct ue_context *ue; |
| 88 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 89 | llist_for_each_entry(ue, &gw->ue_list, list) { |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 90 | if (ue->context_id == id) |
| 91 | return ue; |
| 92 | } |
| 93 | return NULL; |
| 94 | |
| 95 | } |
| 96 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 97 | struct ue_context *ue_context_by_imsi(struct hnb_gw *gw, const char *imsi) |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 98 | { |
| 99 | struct ue_context *ue; |
| 100 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 101 | llist_for_each_entry(ue, &gw->ue_list, list) { |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 102 | if (!strcmp(ue->imsi, imsi)) |
| 103 | return ue; |
| 104 | } |
| 105 | return NULL; |
| 106 | } |
| 107 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 108 | static uint32_t get_next_ue_ctx_id(struct hnb_gw *gw) |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 109 | { |
| 110 | uint32_t id; |
| 111 | |
| 112 | do { |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 113 | id = gw->next_ue_ctx_id++; |
| 114 | } while (ue_context_by_id(gw, id)); |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 115 | |
| 116 | return id; |
| 117 | } |
| 118 | |
| 119 | struct ue_context *ue_context_alloc(struct hnb_context *hnb, const char *imsi) |
| 120 | { |
| 121 | struct ue_context *ue; |
| 122 | |
| 123 | ue = talloc_zero(tall_hnb_ctx, struct ue_context); |
| 124 | if (!ue) |
| 125 | return NULL; |
| 126 | |
| 127 | ue->hnb = hnb; |
| 128 | strncpy(ue->imsi, imsi, sizeof(ue->imsi)); |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 129 | ue->context_id = get_next_ue_ctx_id(hnb->gw); |
| 130 | llist_add_tail(&ue->list, &hnb->gw->ue_list); |
Harald Welte | b534e5c | 2015-09-11 00:15:16 +0200 | [diff] [blame] | 131 | |
| 132 | return ue; |
| 133 | } |
| 134 | |
| 135 | void ue_context_free(struct ue_context *ue) |
| 136 | { |
| 137 | llist_del(&ue->list); |
| 138 | talloc_free(ue); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 143 | static int hnb_read_cb(struct osmo_fd *fd) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 144 | { |
| 145 | struct hnb_context *hnb = fd->data; |
| 146 | struct sctp_sndrcvinfo sinfo; |
| 147 | struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 148 | int flags = 0; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 149 | int rc; |
| 150 | |
| 151 | if (!msg) |
| 152 | return -ENOMEM; |
| 153 | |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 154 | /* we store a reference to the HomeNodeB in the msg->dest for the |
| 155 | * benefit of varoius downstream processing functions */ |
| 156 | msg->dst = hnb; |
| 157 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 158 | rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg), |
| 159 | NULL, NULL, &sinfo, &flags); |
| 160 | if (rc < 0) { |
| 161 | LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); |
Harald Welte | ee77cff | 2015-08-30 16:57:53 +0200 | [diff] [blame] | 162 | /* FIXME: clean up after disappeared HNB */ |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 163 | close(fd->fd); |
| 164 | osmo_fd_unregister(fd); |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 165 | goto out; |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 166 | } else if (rc == 0) { |
| 167 | LOGP(DMAIN, LOGL_ERROR, "Connection to HNB closed\n"); |
| 168 | /* TODO: Remove all UEs from that connection */ |
| 169 | close(fd->fd); |
| 170 | osmo_fd_unregister(fd); |
| 171 | fd->fd = -1; |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 172 | rc = -1; |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 173 | |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 174 | goto out; |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 175 | } else { |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 176 | msgb_put(msg, rc); |
Daniel Willmann | 4267a29 | 2015-12-15 20:29:27 +0100 | [diff] [blame] | 177 | } |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 178 | |
Harald Welte | 17878e2 | 2015-09-08 00:09:13 +0200 | [diff] [blame] | 179 | if (flags & MSG_NOTIFICATION) { |
Daniel Willmann | 7c27f7b | 2015-12-15 17:41:41 +0100 | [diff] [blame] | 180 | LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n"); |
Harald Welte | 17878e2 | 2015-09-08 00:09:13 +0200 | [diff] [blame] | 181 | msgb_free(msg); |
| 182 | return 0; |
| 183 | } |
| 184 | |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 185 | sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid); |
| 186 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 187 | switch (sinfo.sinfo_ppid) { |
| 188 | case IUH_PPI_HNBAP: |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 189 | hnb->hnbap_stream = sinfo.sinfo_stream; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 190 | rc = hnbgw_hnbap_rx(hnb, msg); |
| 191 | break; |
| 192 | case IUH_PPI_RUA: |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 193 | hnb->rua_stream = sinfo.sinfo_stream; |
Harald Welte | 318e4d5 | 2015-09-10 18:47:08 +0200 | [diff] [blame] | 194 | rc = hnbgw_rua_rx(hnb, msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 195 | break; |
| 196 | case IUH_PPI_SABP: |
| 197 | case IUH_PPI_RNA: |
| 198 | case IUH_PPI_PUA: |
| 199 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
| 200 | sinfo.sinfo_ppid); |
| 201 | rc = 0; |
| 202 | break; |
| 203 | default: |
| 204 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
| 205 | sinfo.sinfo_ppid); |
| 206 | rc = 0; |
| 207 | break; |
| 208 | } |
| 209 | |
Daniel Willmann | 269b8ac | 2015-12-22 16:26:48 +0100 | [diff] [blame] | 210 | out: |
Harald Welte | f2f3000 | 2015-09-08 00:09:23 +0200 | [diff] [blame] | 211 | msgb_free(msg); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 212 | return rc; |
| 213 | } |
| 214 | |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 215 | static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) |
| 216 | { |
| 217 | struct hnb_context *ctx = fd->data; |
| 218 | struct sctp_sndrcvinfo sinfo = { |
Harald Welte | ffa7c0a | 2015-12-23 00:03:41 +0100 | [diff] [blame] | 219 | .sinfo_ppid = htonl(msgb_sctp_ppid(msg)), |
Harald Welte | 3f71256 | 2015-09-07 21:53:25 +0200 | [diff] [blame] | 220 | .sinfo_stream = ctx->hnbap_stream, |
| 221 | }; |
| 222 | int rc; |
| 223 | |
| 224 | rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg), |
| 225 | &sinfo, 0); |
Harald Welte | 9e270b4 | 2015-09-07 22:41:26 +0200 | [diff] [blame] | 226 | /* 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] | 227 | return rc; |
| 228 | } |
| 229 | |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 230 | struct hnb_context *hnb_context_alloc(struct hnb_gw *gw, int new_fd) |
| 231 | { |
| 232 | struct hnb_context *ctx; |
| 233 | |
| 234 | ctx = talloc_zero(tall_hnb_ctx, struct hnb_context); |
| 235 | if (!ctx) |
| 236 | return NULL; |
| 237 | |
| 238 | ctx->gw = gw; |
| 239 | osmo_wqueue_init(&ctx->wqueue, 16); |
| 240 | ctx->wqueue.bfd.data = ctx; |
| 241 | ctx->wqueue.bfd.fd = new_fd; |
| 242 | ctx->wqueue.bfd.when = BSC_FD_READ; |
| 243 | ctx->wqueue.read_cb = hnb_read_cb; |
| 244 | ctx->wqueue.write_cb = hnb_write_cb; |
| 245 | osmo_fd_register(&ctx->wqueue.bfd); |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 246 | INIT_LLIST_HEAD(&ctx->map_list); |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 247 | |
| 248 | llist_add_tail(&ctx->list, &gw->hnb_list); |
| 249 | } |
| 250 | |
| 251 | void hnb_context_release(struct hnb_context *ctx) |
| 252 | { |
| 253 | struct hnbgw_context_map *map, *map2; |
| 254 | |
| 255 | /* remove from the list of HNB contexts */ |
| 256 | llist_del(&ctx->list); |
| 257 | |
| 258 | /* deactivate all context maps */ |
| 259 | llist_for_each_entry_safe(map, map2, &ctx->map_list, hnb_list) { |
| 260 | /* remove it from list, as HNB context will soon be |
| 261 | * gone. Let's hope the seccond osmo_llist_del in the |
| 262 | * map garbage collector wors fine? */ |
| 263 | llist_del(&map->hnb_list); |
| 264 | context_map_deactivate(map); |
| 265 | } |
| 266 | /* FIXME: flush write queue items */ |
| 267 | osmo_fd_unregister(&ctx->wqueue.bfd); |
| 268 | |
| 269 | talloc_free(ctx); |
| 270 | } |
| 271 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 272 | /*! call-back when the listen FD has something to read */ |
| 273 | static int listen_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 274 | { |
| 275 | struct hnb_gw *gw = fd->data; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 276 | struct hnb_context *ctx; |
| 277 | struct sockaddr_storage sockaddr; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 278 | socklen_t len = sizeof(sockaddr); |
| 279 | |
| 280 | int new_fd = accept(fd->fd, (struct sockaddr *)&sockaddr, &len); |
| 281 | if (new_fd < 0) { |
| 282 | LOGP(DMAIN, LOGL_ERROR, "Iuh accept() failed\n"); |
| 283 | return new_fd; |
| 284 | } |
| 285 | |
| 286 | LOGP(DMAIN, LOGL_INFO, "SCTP Connection accept()ed\n"); |
| 287 | |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 288 | ctx = hnb_context_alloc(gw, new_fd); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 289 | if (!ctx) |
| 290 | return -ENOMEM; |
| 291 | |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 295 | /* Entry point for primitives coming up from SCCP User SAP */ |
| 296 | static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link) |
| 297 | { |
| 298 | struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph; |
| 299 | |
| 300 | LOGP(DMAIN, LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph)); |
| 301 | |
| 302 | /* FIXME: Do something */ |
| 303 | |
| 304 | msgb_free(oph->msg); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 309 | static const struct log_info_cat log_cat[] = { |
| 310 | [DMAIN] = { |
Daniel Willmann | 7c27f7b | 2015-12-15 17:41:41 +0100 | [diff] [blame] | 311 | .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1, |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 312 | .color = "", |
| 313 | .description = "Main program", |
| 314 | }, |
Daniel Willmann | bded984 | 2015-12-17 11:51:17 +0100 | [diff] [blame] | 315 | [DHNBAP] = { |
| 316 | .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 317 | .color = "", |
| 318 | .description = "Home Node B Application Part", |
| 319 | }, |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 320 | [DSUA] = { |
| 321 | .name = "DSUA", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 322 | .color = "", |
| 323 | .description = "SCCP User Adaptation", |
| 324 | }, |
Harald Welte | f42317b | 2015-12-23 15:36:31 +0100 | [diff] [blame] | 325 | [DRUA] = { |
| 326 | .name = "DRUA", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 327 | .color = "", |
| 328 | .description = "RANAP User Adaptation", |
| 329 | }, |
| 330 | [DRANAP] = { |
| 331 | .name = "DRANAP", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 332 | .color = "", |
| 333 | .description = "RAN Application Part", |
| 334 | }, |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 335 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 336 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 337 | static const struct log_info hnbgw_log_info = { |
| 338 | .cat = log_cat, |
| 339 | .num_cat = ARRAY_SIZE(log_cat), |
| 340 | }; |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 341 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 342 | static struct vty_app_info vty_info = { |
| 343 | .name = "OsmoHNBGW", |
| 344 | .version = "0", |
| 345 | }; |
| 346 | |
| 347 | static int daemonize = 0; |
| 348 | |
Harald Welte | 5c11c94 | 2015-09-07 19:54:49 +0200 | [diff] [blame] | 349 | static int sctp_sock_init(int fd) |
| 350 | { |
| 351 | struct sctp_event_subscribe event; |
| 352 | int rc; |
| 353 | |
| 354 | /* subscribe for all events */ |
| 355 | memset((uint8_t *)&event, 1, sizeof(event)); |
| 356 | rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, |
| 357 | &event, sizeof(event)); |
| 358 | |
| 359 | return rc; |
| 360 | } |
| 361 | |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 362 | static void vty_dump_hnb_info(struct vty *vty, struct hnb_context *hnb) |
| 363 | { |
| 364 | vty_out(vty, "HNB \"%s\" MCC %u MNC %u LAC %u RAC %u SAC %u CID %u%s", hnb->identity_info, |
| 365 | hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid, |
| 366 | VTY_NEWLINE); |
| 367 | vty_out(vty, " HNBAP ID %u RUA ID %u%s", hnb->hnbap_stream, hnb->rua_stream, VTY_NEWLINE); |
| 368 | } |
| 369 | |
| 370 | static void vty_dump_ue_info(struct vty *vty, struct ue_context *ue) |
| 371 | { |
| 372 | vty_out(vty, "UE IMSI \"%s\" context ID %u%s", ue->imsi, ue->context_id, VTY_NEWLINE); |
| 373 | } |
| 374 | |
| 375 | DEFUN(show_hnb, show_hnb_cmd, "show hnb all", SHOW_STR "Display information about a HNB") |
| 376 | { |
| 377 | struct hnb_context *hnb; |
| 378 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 379 | llist_for_each_entry(hnb, &g_hnb_gw->hnb_list, list) { |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 380 | vty_dump_hnb_info(vty, hnb); |
| 381 | } |
| 382 | |
| 383 | return CMD_SUCCESS; |
| 384 | } |
| 385 | |
| 386 | DEFUN(show_ue, show_ue_cmd, "show ue all", SHOW_STR "Display information about a UE") |
| 387 | { |
| 388 | struct ue_context *ue; |
| 389 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 390 | llist_for_each_entry(ue, &g_hnb_gw->ue_list, list) { |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 391 | vty_dump_ue_info(vty, ue); |
| 392 | } |
| 393 | |
| 394 | return CMD_SUCCESS; |
| 395 | } |
| 396 | |
Daniel Willmann | 28b9ec1 | 2015-12-17 18:02:37 +0100 | [diff] [blame] | 397 | DEFUN(show_talloc, show_talloc_cmd, "show talloc", SHOW_STR "Display talloc info") |
| 398 | { |
| 399 | talloc_report_full(tall_hnb_ctx, stderr); |
Daniel Willmann | 28b9ec1 | 2015-12-17 18:02:37 +0100 | [diff] [blame] | 400 | talloc_report_full(talloc_asn1_ctx, stderr); |
| 401 | |
| 402 | return CMD_SUCCESS; |
| 403 | } |
| 404 | |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 405 | static void hnbgw_vty_init(void) |
| 406 | { |
| 407 | install_element_ve(&show_hnb_cmd); |
| 408 | install_element_ve(&show_ue_cmd); |
Daniel Willmann | 28b9ec1 | 2015-12-17 18:02:37 +0100 | [diff] [blame] | 409 | install_element_ve(&show_talloc_cmd); |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 412 | int main(int argc, char **argv) |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 413 | { |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 414 | struct osmo_sua_user *sua_user; |
| 415 | struct osmo_sua_link *sua_link; |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 416 | int rc; |
| 417 | |
| 418 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
Harald Welte | 08de638 | 2015-08-31 09:54:45 +0200 | [diff] [blame] | 419 | talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 420 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 421 | g_hnb_gw = hnb_gw_create(tall_hnb_ctx); |
Harald Welte | 90256ba | 2015-12-23 20:16:36 +0100 | [diff] [blame] | 422 | |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 423 | rc = osmo_init_logging(&hnbgw_log_info); |
| 424 | if (rc < 0) |
| 425 | exit(1); |
| 426 | |
| 427 | vty_init(&vty_info); |
Daniel Willmann | 56f6273 | 2015-12-02 11:22:53 +0100 | [diff] [blame] | 428 | hnbgw_vty_init(); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 429 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 430 | rc = telnet_init(NULL, g_hnb_gw, 2323); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 431 | if (rc < 0) { |
| 432 | perror("Error binding VTY port"); |
| 433 | exit(1); |
| 434 | } |
| 435 | |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 436 | osmo_sua_set_log_area(DSUA); |
Harald Welte | 75a4e65 | 2015-12-22 23:59:24 +0100 | [diff] [blame] | 437 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 438 | g_hnb_gw->cnlink_cs = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.1", SUA_PORT); |
| 439 | g_hnb_gw->cnlink_ps = hnbgw_cnlink_init(g_hnb_gw, "127.0.0.2", SUA_PORT); |
| 440 | |
| 441 | 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] | 442 | IPPROTO_SCTP, NULL, |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 443 | g_hnb_gw->config.iuh_listen_port, OSMO_SOCK_F_BIND); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 444 | if (rc < 0) { |
| 445 | perror("Error binding Iuh port"); |
| 446 | exit(1); |
| 447 | } |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 448 | sctp_sock_init(g_hnb_gw->listen_fd.fd); |
Harald Welte | b3dae30 | 2015-08-30 12:20:09 +0200 | [diff] [blame] | 449 | |
| 450 | if (daemonize) { |
| 451 | rc = osmo_daemonize(); |
| 452 | if (rc < 0) { |
| 453 | perror("Error during daemonize"); |
| 454 | exit(1); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | while (1) { |
| 459 | rc = osmo_select_main(0); |
| 460 | if (rc < 0) |
| 461 | exit(3); |
| 462 | } |
| 463 | |
| 464 | /* not reached */ |
| 465 | exit(0); |
Harald Welte | a2e6a7a | 2015-08-29 21:47:39 +0200 | [diff] [blame] | 466 | } |