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